source: mainline/uspace/lib/display/src/display.c@ 2a515dcd

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

Deliver window focus and unfocus events

  • Property mode set to 100644
File size: 9.3 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#include <async.h>
30#include <display.h>
31#include <display/event.h>
32#include <errno.h>
33#include <fibril_synch.h>
34#include <ipc/display.h>
35#include <ipc/services.h>
36#include <ipcgfx/client.h>
37#include <loc.h>
38#include <mem.h>
39#include <stdlib.h>
40
41static errno_t display_callback_create(display_t *);
42static void display_cb_conn(ipc_call_t *, void *);
43static errno_t display_get_window(display_t *, sysarg_t, display_window_t **);
44
45/** Open display service.
46 *
47 * @param dsname Display service name or @c NULL to use default display
48 * @param rdisplay Place to store pointer to display session
49 * @return EOK on success or an error code
50 */
51errno_t display_open(const char *dsname, display_t **rdisplay)
52{
53 service_id_t display_svc;
54 display_t *display;
55 errno_t rc;
56
57 display = calloc(1, sizeof(display_t));
58 if (display == NULL)
59 return ENOMEM;
60
61 fibril_mutex_initialize(&display->lock);
62 fibril_condvar_initialize(&display->cv);
63 list_initialize(&display->windows);
64
65 if (dsname == NULL)
66 dsname = SERVICE_NAME_DISPLAY;
67
68 rc = loc_service_get_id(dsname, &display_svc, IPC_FLAG_BLOCKING);
69 if (rc != EOK) {
70 free(display);
71 return ENOENT;
72 }
73
74 display->sess = loc_service_connect(display_svc, INTERFACE_DISPLAY,
75 IPC_FLAG_BLOCKING);
76 if (display->sess == NULL) {
77 free(display);
78 return ENOENT;
79 }
80
81 rc = display_callback_create(display);
82 if (rc != EOK) {
83 async_hangup(display->sess);
84 free(display);
85 return EIO;
86 }
87
88 *rdisplay = display;
89 return EOK;
90}
91
92/** Create callback connection from display service.
93 *
94 * @param display Display session
95 * @return EOK on success or an error code
96 */
97static errno_t display_callback_create(display_t *display)
98{
99 async_exch_t *exch = async_exchange_begin(display->sess);
100
101 aid_t req = async_send_0(exch, DISPLAY_CALLBACK_CREATE, NULL);
102
103 port_id_t port;
104 errno_t rc = async_create_callback_port(exch, INTERFACE_DISPLAY_CB, 0, 0,
105 display_cb_conn, display, &port);
106
107 async_exchange_end(exch);
108
109 if (rc != EOK)
110 return rc;
111
112 errno_t retval;
113 async_wait_for(req, &retval);
114
115 return retval;
116}
117
118/** Close display service.
119 *
120 * @param display Display session
121 */
122void display_close(display_t *display)
123{
124 async_hangup(display->sess);
125
126 /* Wait for callback handler to terminate */
127
128 fibril_mutex_lock(&display->lock);
129 while (!display->cb_done)
130 fibril_condvar_wait(&display->cv, &display->lock);
131 fibril_mutex_unlock(&display->lock);
132
133 free(display);
134}
135
136/** Initialize window parameters structure.
137 *
138 * @param params Window parameters structure
139 */
140void display_wnd_params_init(display_wnd_params_t *params)
141{
142 memset(params, 0, sizeof(*params));
143}
144
145/** Create a display window.
146 *
147 * @param display Display
148 * @param params Window parameters
149 * @param cb Callback functions
150 * @param cb_arg Argument to callback functions
151 * @param rwindow Place to store pointer to new window
152 * @return EOK on success or an error code
153 */
154errno_t display_window_create(display_t *display, display_wnd_params_t *params,
155 display_wnd_cb_t *cb, void *cb_arg, display_window_t **rwindow)
156{
157 display_window_t *window;
158 async_exch_t *exch;
159 aid_t req;
160 ipc_call_t answer;
161 errno_t rc;
162
163 window = calloc(1, sizeof(display_window_t));
164 if (window == NULL)
165 return ENOMEM;
166
167 exch = async_exchange_begin(display->sess);
168 req = async_send_0(exch, DISPLAY_WINDOW_CREATE, &answer);
169 rc = async_data_write_start(exch, params, sizeof (display_wnd_params_t));
170 async_exchange_end(exch);
171 if (rc != EOK) {
172 async_forget(req);
173 return rc;
174 }
175
176 async_wait_for(req, &rc);
177 if (rc != EOK)
178 return rc;
179
180 window->display = display;
181 window->id = ipc_get_arg1(&answer);
182 window->cb = cb;
183 window->cb_arg = cb_arg;
184
185 list_append(&window->lwindows, &display->windows);
186 *rwindow = window;
187 return EOK;
188}
189
190/** Destroy display window.
191 *
192 * @param window Window
193 * @return EOK on success or an error code. In both cases @a window must
194 * not be accessed anymore
195 */
196errno_t display_window_destroy(display_window_t *window)
197{
198 async_exch_t *exch;
199 errno_t rc;
200
201 exch = async_exchange_begin(window->display->sess);
202 rc = async_req_1_0(exch, DISPLAY_WINDOW_DESTROY, window->id);
203
204 async_exchange_end(exch);
205
206 list_remove(&window->lwindows);
207 free(window);
208 return rc;
209}
210
211/** Create graphics context for drawing into a window.
212 *
213 * @param window Window
214 * @param rgc Place to store pointer to new graphics context
215 */
216errno_t display_window_get_gc(display_window_t *window, gfx_context_t **rgc)
217{
218 async_sess_t *sess;
219 async_exch_t *exch;
220 ipc_gc_t *gc;
221 errno_t rc;
222
223 exch = async_exchange_begin(window->display->sess);
224 sess = async_connect_me_to(exch, INTERFACE_GC, 0, window->id);
225 if (sess == NULL) {
226 async_exchange_end(exch);
227 return EIO;
228 }
229
230 async_exchange_end(exch);
231
232 rc = ipc_gc_create(sess, &gc);
233 if (rc != EOK) {
234 async_hangup(sess);
235 return ENOMEM;
236 }
237
238 *rgc = ipc_gc_get_ctx(gc);
239 return EOK;
240}
241
242/** Get display event.
243 *
244 * @param display Display
245 * @param rwindow Place to store pointe to window that received event
246 * @param event Place to store event
247 * @return EOK on success or an error code
248 */
249static errno_t display_get_event(display_t *display, display_window_t **rwindow,
250 display_wnd_ev_t *event)
251{
252 async_exch_t *exch;
253 ipc_call_t answer;
254 aid_t req;
255 errno_t rc;
256 sysarg_t wnd_id;
257 display_window_t *window;
258
259 exch = async_exchange_begin(display->sess);
260 req = async_send_0(exch, DISPLAY_GET_EVENT, &answer);
261 rc = async_data_read_start(exch, event, sizeof(*event));
262 async_exchange_end(exch);
263 if (rc != EOK) {
264 async_forget(req);
265 return rc;
266 }
267
268 async_wait_for(req, &rc);
269 if (rc != EOK)
270 return rc;
271
272 wnd_id = ipc_get_arg1(&answer);
273 rc = display_get_window(display, wnd_id, &window);
274 if (rc != EOK)
275 return EIO;
276
277 *rwindow = window;
278 return EOK;
279}
280
281/** Display events are pending.
282 *
283 * @param display Display
284 * @param icall Call data
285 */
286static void display_ev_pending(display_t *display, ipc_call_t *icall)
287{
288 errno_t rc;
289 display_window_t *window = NULL;
290 display_wnd_ev_t event;
291
292 while (true) {
293 rc = display_get_event(display, &window, &event);
294 if (rc != EOK)
295 break;
296
297 switch (event.etype) {
298 case wev_focus:
299 if (window->cb != NULL && window->cb->focus_event != NULL) {
300 window->cb->focus_event(window->cb_arg);
301 }
302 break;
303 case wev_kbd:
304 if (window->cb != NULL && window->cb->kbd_event != NULL) {
305 window->cb->kbd_event(window->cb_arg,
306 &event.ev.kbd);
307 }
308 break;
309 case wev_pos:
310 if (window->cb != NULL && window->cb->pos_event != NULL) {
311 window->cb->pos_event(window->cb_arg,
312 &event.ev.pos);
313 }
314 break;
315 case wev_unfocus:
316 if (window->cb != NULL && window->cb->unfocus_event != NULL) {
317 window->cb->unfocus_event(window->cb_arg);
318 }
319 break;
320 }
321 }
322
323 async_answer_0(icall, EOK);
324}
325
326/** Callback connection handler.
327 *
328 * @param icall Connect call data
329 * @param arg Argument, display_t *
330 */
331static void display_cb_conn(ipc_call_t *icall, void *arg)
332{
333 display_t *display = (display_t *) arg;
334
335 while (true) {
336 ipc_call_t call;
337 async_get_call(&call);
338
339 if (!ipc_get_imethod(&call)) {
340 /* Hangup */
341 async_answer_0(&call, EOK);
342 goto out;
343 }
344
345 switch (ipc_get_imethod(&call)) {
346 case DISPLAY_EV_PENDING:
347 display_ev_pending(display, &call);
348 break;
349 default:
350 async_answer_0(&call, ENOTSUP);
351 break;
352 }
353 }
354
355out:
356 fibril_mutex_lock(&display->lock);
357 display->cb_done = true;
358 fibril_mutex_unlock(&display->lock);
359 fibril_condvar_broadcast(&display->cv);
360}
361
362/** Find window by ID.
363 *
364 * @param display Display
365 * @param wnd_id Window ID
366 * @param rwindow Place to store pointer to window
367 * @return EOK on success, ENOENT if not found
368 */
369static errno_t display_get_window(display_t *display, sysarg_t wnd_id,
370 display_window_t **rwindow)
371{
372 link_t *link;
373 display_window_t *window;
374
375 link = list_first(&display->windows);
376 while (link != NULL) {
377 window = list_get_instance(link, display_window_t, lwindows);
378 if (window->id == wnd_id) {
379 *rwindow = window;
380 return EOK;
381 }
382
383 link = list_next(link, &display->windows);
384 }
385
386 return ENOENT;
387}
388
389/** @}
390 */
Note: See TracBrowser for help on using the repository browser.