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 <gfx/context.h>
|
---|
39 | #include <io/log.h>
|
---|
40 | #include <stdlib.h>
|
---|
41 | #include "display.h"
|
---|
42 | #include "window.h"
|
---|
43 |
|
---|
44 | static errno_t disp_window_create(void *, sysarg_t *);
|
---|
45 | static errno_t disp_window_destroy(void *, sysarg_t);
|
---|
46 |
|
---|
47 | display_ops_t display_srv_ops = {
|
---|
48 | .window_create = disp_window_create,
|
---|
49 | .window_destroy = disp_window_destroy
|
---|
50 | };
|
---|
51 |
|
---|
52 | static errno_t disp_window_create(void *arg, sysarg_t *rwnd_id)
|
---|
53 | {
|
---|
54 | errno_t rc;
|
---|
55 | ds_display_t *disp = (ds_display_t *) arg;
|
---|
56 | ds_window_t *wnd;
|
---|
57 |
|
---|
58 | log_msg(LOG_DEFAULT, LVL_DEBUG, "disp_window_create()");
|
---|
59 |
|
---|
60 | rc = ds_window_create(disp, &wnd);
|
---|
61 | log_msg(LOG_DEFAULT, LVL_DEBUG, "disp_window_create() - ds_window_create -> %d", rc);
|
---|
62 | if (rc != EOK)
|
---|
63 | return rc;
|
---|
64 |
|
---|
65 | log_msg(LOG_DEFAULT, LVL_DEBUG, "disp_window_create() -> EOK, id=%zu",
|
---|
66 | wnd->id);
|
---|
67 | *rwnd_id = wnd->id;
|
---|
68 | return EOK;
|
---|
69 | }
|
---|
70 |
|
---|
71 | static errno_t disp_window_destroy(void *arg, sysarg_t wnd_id)
|
---|
72 | {
|
---|
73 | ds_display_t *disp = (ds_display_t *) arg;
|
---|
74 | ds_window_t *wnd;
|
---|
75 |
|
---|
76 | wnd = ds_display_find_window(disp, wnd_id);
|
---|
77 | if (wnd == NULL)
|
---|
78 | return ENOENT;
|
---|
79 |
|
---|
80 | log_msg(LOG_DEFAULT, LVL_DEBUG, "disp_window_destroy()");
|
---|
81 | ds_display_remove_window(wnd);
|
---|
82 | ds_window_delete(wnd);
|
---|
83 | return EOK;
|
---|
84 | }
|
---|
85 |
|
---|
86 | /** Create display.
|
---|
87 | *
|
---|
88 | * @param gc Graphics context for displaying output
|
---|
89 | * @param rdisp Place to store pointer to new display.
|
---|
90 | * @return EOK on success, ENOMEM if out of memory
|
---|
91 | */
|
---|
92 | errno_t ds_display_create(gfx_context_t *gc, ds_display_t **rdisp)
|
---|
93 | {
|
---|
94 | ds_display_t *disp;
|
---|
95 |
|
---|
96 | disp = calloc(1, sizeof(ds_display_t));
|
---|
97 | if (disp == NULL)
|
---|
98 | return ENOMEM;
|
---|
99 |
|
---|
100 | list_initialize(&disp->windows);
|
---|
101 | disp->next_wnd_id = 1;
|
---|
102 | disp->gc = gc;
|
---|
103 | *rdisp = disp;
|
---|
104 | return EOK;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /** Destroy display.
|
---|
108 | *
|
---|
109 | * @param disp Display
|
---|
110 | */
|
---|
111 | void ds_display_destroy(ds_display_t *disp)
|
---|
112 | {
|
---|
113 | assert(list_empty(&disp->windows));
|
---|
114 | free(disp);
|
---|
115 | }
|
---|
116 |
|
---|
117 | /** Add window to display.
|
---|
118 | *
|
---|
119 | * @param disp Display
|
---|
120 | * @param wnd Window
|
---|
121 | * @return EOK on success, ENOMEM if there are no free window identifiers
|
---|
122 | */
|
---|
123 | errno_t ds_display_add_window(ds_display_t *disp, ds_window_t *wnd)
|
---|
124 | {
|
---|
125 | assert(wnd->display == NULL);
|
---|
126 | assert(!link_used(&wnd->lwindows));
|
---|
127 |
|
---|
128 | wnd->display = disp;
|
---|
129 | wnd->id = disp->next_wnd_id++;
|
---|
130 | list_append(&wnd->lwindows, &disp->windows);
|
---|
131 |
|
---|
132 | return EOK;
|
---|
133 | }
|
---|
134 |
|
---|
135 | /** Remove window from display.
|
---|
136 | *
|
---|
137 | * @param wnd Window
|
---|
138 | */
|
---|
139 | void ds_display_remove_window(ds_window_t *wnd)
|
---|
140 | {
|
---|
141 | list_remove(&wnd->lwindows);
|
---|
142 | wnd->display = NULL;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /** Find window by ID.
|
---|
146 | *
|
---|
147 | * @param disp Display
|
---|
148 | * @param id Window ID
|
---|
149 | */
|
---|
150 | ds_window_t *ds_display_find_window(ds_display_t *disp, ds_wnd_id_t id)
|
---|
151 | {
|
---|
152 | ds_window_t *wnd;
|
---|
153 |
|
---|
154 | // TODO Make this faster
|
---|
155 | wnd = ds_display_first_window(disp);
|
---|
156 | while (wnd != NULL) {
|
---|
157 | if (wnd->id == id)
|
---|
158 | return wnd;
|
---|
159 | wnd = ds_display_next_window(wnd);
|
---|
160 | }
|
---|
161 |
|
---|
162 | return NULL;
|
---|
163 | }
|
---|
164 |
|
---|
165 | /** Get first window in display.
|
---|
166 | *
|
---|
167 | * @param disp Display
|
---|
168 | * @return First window or @c NULL if there is none
|
---|
169 | */
|
---|
170 | ds_window_t *ds_display_first_window(ds_display_t *disp)
|
---|
171 | {
|
---|
172 | link_t *link = list_first(&disp->windows);
|
---|
173 |
|
---|
174 | if (link == NULL)
|
---|
175 | return NULL;
|
---|
176 |
|
---|
177 | return list_get_instance(link, ds_window_t, lwindows);
|
---|
178 | }
|
---|
179 |
|
---|
180 | /** Get next window in display.
|
---|
181 | *
|
---|
182 | * @param wnd Current window
|
---|
183 | * @return Next window or @c NULL if there is none
|
---|
184 | */
|
---|
185 | ds_window_t *ds_display_next_window(ds_window_t *wnd)
|
---|
186 | {
|
---|
187 | link_t *link = list_next(&wnd->lwindows, &wnd->display->windows);
|
---|
188 |
|
---|
189 | if (link == NULL)
|
---|
190 | return NULL;
|
---|
191 |
|
---|
192 | return list_get_instance(link, ds_window_t, lwindows);
|
---|
193 | }
|
---|
194 |
|
---|
195 | /** @}
|
---|
196 | */
|
---|