source: mainline/uspace/srv/hid/display/display.c@ 6af4b4f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6af4b4f was 6af4b4f, checked in by Jiri Svoboda <jiri@…>, 6 years ago

Introduce ds_display_t, ds_window_t

  • Property mode set to 100644
File size: 4.7 KB
Line 
1/*
2 * Copyright (c) 2019 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup display
30 * @{
31 */
32/**
33 * @file Display management
34 */
35
36#include <disp_srv.h>
37#include <errno.h>
38#include <io/log.h>
39#include <stdlib.h>
40#include "display.h"
41#include "window.h"
42
43static errno_t disp_window_create(void *, sysarg_t *);
44static errno_t disp_window_destroy(void *, sysarg_t);
45
46display_ops_t display_srv_ops = {
47 .window_create = disp_window_create,
48 .window_destroy = disp_window_destroy
49};
50
51static errno_t disp_window_create(void *arg, sysarg_t *rwnd_id)
52{
53 errno_t rc;
54 ds_display_t *disp = (ds_display_t *) arg;
55 ds_window_t *wnd;
56
57 log_msg(LOG_DEFAULT, LVL_DEBUG, "disp_window_create()");
58
59 rc = ds_window_create(disp, &wnd);
60 log_msg(LOG_DEFAULT, LVL_DEBUG, "disp_window_create() - ds_window_create -> %d", rc);
61 if (rc != EOK)
62 return rc;
63
64 log_msg(LOG_DEFAULT, LVL_DEBUG, "disp_window_create() -> EOK, id=%zu",
65 wnd->id);
66 *rwnd_id = wnd->id;
67 return EOK;
68}
69
70static errno_t disp_window_destroy(void *arg, sysarg_t wnd_id)
71{
72 ds_display_t *disp = (ds_display_t *) arg;
73 ds_window_t *wnd;
74
75 wnd = ds_display_find_window(disp, wnd_id);
76 if (wnd == NULL)
77 return ENOENT;
78
79 log_msg(LOG_DEFAULT, LVL_DEBUG, "disp_window_destroy()");
80 ds_display_remove_window(wnd);
81 ds_window_delete(wnd);
82 return EOK;
83}
84
85/** Create display.
86 *
87 * @param rdisp Place to store pointer to new display.
88 * @return EOK on success, ENOMEM if out of memory
89 */
90errno_t ds_display_create(ds_display_t **rdisp)
91{
92 ds_display_t *disp;
93
94 disp = calloc(1, sizeof(ds_display_t));
95 if (disp == NULL)
96 return ENOMEM;
97
98 list_initialize(&disp->windows);
99 disp->next_wnd_id = 1;
100 *rdisp = disp;
101 return EOK;
102}
103
104/** Destroy display.
105 *
106 * @param disp Display
107 */
108void ds_display_destroy(ds_display_t *disp)
109{
110 assert(list_empty(&disp->windows));
111 free(disp);
112}
113
114/** Add window to display.
115 *
116 * @param disp Display
117 * @param wnd Window
118 * @return EOK on success, ENOMEM if there are no free window identifiers
119 */
120errno_t ds_display_add_window(ds_display_t *disp, ds_window_t *wnd)
121{
122 assert(wnd->display == NULL);
123 assert(!link_used(&wnd->lwindows));
124
125 wnd->display = disp;
126 wnd->id = disp->next_wnd_id++;
127 list_append(&wnd->lwindows, &disp->windows);
128
129 return EOK;
130}
131
132/** Remove window from display.
133 *
134 * @param wnd Window
135 */
136void ds_display_remove_window(ds_window_t *wnd)
137{
138 list_remove(&wnd->lwindows);
139 wnd->display = NULL;
140}
141
142/** Find window by ID.
143 *
144 * @param disp Display
145 * @param id Window ID
146 */
147ds_window_t *ds_display_find_window(ds_display_t *disp, ds_wnd_id_t id)
148{
149 ds_window_t *wnd;
150
151 // TODO Make this faster
152 wnd = ds_display_first_window(disp);
153 while (wnd != NULL) {
154 if (wnd->id == id)
155 return wnd;
156 wnd = ds_display_next_window(wnd);
157 }
158
159 return NULL;
160}
161
162/** Get first window in display.
163 *
164 * @param disp Display
165 * @return First window or @c NULL if there is none
166 */
167ds_window_t *ds_display_first_window(ds_display_t *disp)
168{
169 link_t *link = list_first(&disp->windows);
170
171 if (link == NULL)
172 return NULL;
173
174 return list_get_instance(link, ds_window_t, lwindows);
175}
176
177/** Get next window in display.
178 *
179 * @param wnd Current window
180 * @return Next window or @c NULL if there is none
181 */
182ds_window_t *ds_display_next_window(ds_window_t *wnd)
183{
184 link_t *link = list_next(&wnd->lwindows, &wnd->display->windows);
185
186 if (link == NULL)
187 return NULL;
188
189 return list_get_instance(link, ds_window_t, lwindows);
190}
191
192/** @}
193 */
Note: See TracBrowser for help on using the repository browser.