source: mainline/uspace/srv/hid/display/display.c@ 4af6fb1

Last change on this file since 4af6fb1 was 4af6fb1, checked in by Jiri Svoboda <jiri@…>, 10 months ago

Remove forgotten debug messages

  • Property mode set to 100644
File size: 28.6 KB
RevLine 
[c8cf261]1/*
[9546146]2 * Copyright (c) 2024 Jiri Svoboda
[c8cf261]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/**
[38e5f36c]33 * @file Display server display
[c8cf261]34 */
35
36#include <errno.h>
[8aef01c]37#include <gfx/bitmap.h>
[159776f]38#include <gfx/context.h>
[c79545e]39#include <gfx/render.h>
[c8cf261]40#include <io/log.h>
[8aef01c]41#include <memgfx/memgc.h>
[6af4b4f]42#include <stdlib.h>
[9546146]43#include <str.h>
[b3c185b6]44#include "client.h"
[5271e4c]45#include "clonegc.h"
[4d8002d]46#include "cursimg.h"
47#include "cursor.h"
[913add60]48#include "display.h"
[9546146]49#include "idevcfg.h"
[cf32dbd]50#include "seat.h"
[6af4b4f]51#include "window.h"
[913add60]52#include "wmclient.h"
[c8cf261]53
[8aef01c]54static gfx_context_t *ds_display_get_unbuf_gc(ds_display_t *);
[2ab8ab3]55static void ds_display_invalidate_cb(void *, gfx_rect_t *);
56static void ds_display_update_cb(void *);
[8aef01c]57
[1215db9]58static mem_gc_cb_t ds_display_mem_gc_cb = {
59 .invalidate = ds_display_invalidate_cb,
60 .update = ds_display_update_cb
61};
62
[6af4b4f]63/** Create display.
64 *
[159776f]65 * @param gc Graphics context for displaying output
[8aef01c]66 * @param flags Display flags
[6af4b4f]67 * @param rdisp Place to store pointer to new display.
68 * @return EOK on success, ENOMEM if out of memory
69 */
[8aef01c]70errno_t ds_display_create(gfx_context_t *gc, ds_display_flags_t flags,
71 ds_display_t **rdisp)
[6af4b4f]72{
73 ds_display_t *disp;
[4d8002d]74 ds_cursor_t *cursor;
75 int i;
[c79545e]76 errno_t rc;
[6af4b4f]77
78 disp = calloc(1, sizeof(ds_display_t));
79 if (disp == NULL)
80 return ENOMEM;
81
[c79545e]82 rc = gfx_color_new_rgb_i16(0x8000, 0xc800, 0xffff, &disp->bg_color);
83 if (rc != EOK) {
84 free(disp);
85 return ENOMEM;
86 }
87
[4d8002d]88 list_initialize(&disp->cursors);
89
90 for (i = 0; i < dcurs_limit; i++) {
91 rc = ds_cursor_create(disp, &ds_cursimg[i].rect,
92 ds_cursimg[i].image, &cursor);
93 if (rc != EOK)
94 goto error;
95
96 disp->cursor[i] = cursor;
97 }
98
[c11ee605]99 fibril_mutex_initialize(&disp->lock);
[b3c185b6]100 list_initialize(&disp->clients);
[913add60]101 list_initialize(&disp->wmclients);
[d8503fd]102 list_initialize(&disp->cfgclients);
[b3c185b6]103 disp->next_wnd_id = 1;
[9546146]104 disp->next_seat_id = 1;
[87a7cdb]105 list_initialize(&disp->ddevs);
[b3eeae5]106 list_initialize(&disp->idevcfgs);
[cf32dbd]107 list_initialize(&disp->seats);
[fd777a2]108 list_initialize(&disp->windows);
[8aef01c]109 disp->flags = flags;
[6af4b4f]110 *rdisp = disp;
111 return EOK;
[4d8002d]112error:
113 ds_display_destroy(disp);
114 return rc;
[6af4b4f]115}
116
117/** Destroy display.
118 *
119 * @param disp Display
120 */
121void ds_display_destroy(ds_display_t *disp)
122{
[4e7b0ad]123 int i;
124
[b3c185b6]125 assert(list_empty(&disp->clients));
[913add60]126 assert(list_empty(&disp->wmclients));
[d8503fd]127 assert(list_empty(&disp->cfgclients));
[cf32dbd]128 assert(list_empty(&disp->seats));
[4e7b0ad]129 assert(list_empty(&disp->ddevs));
[b3eeae5]130 assert(list_empty(&disp->idevcfgs));
[4e7b0ad]131 assert(list_empty(&disp->seats));
132 assert(list_empty(&disp->windows));
133
134 /* Destroy cursors */
135 for (i = 0; i < dcurs_limit; i++) {
136 ds_cursor_destroy(disp->cursor[i]);
137 disp->cursor[i] = NULL;
138 }
139
[c79545e]140 gfx_color_delete(disp->bg_color);
[6af4b4f]141 free(disp);
142}
143
[9546146]144/** Load display configuration from SIF file.
145 *
146 * @param display Display
147 * @param cfgpath Configuration file path
148 *
149 * @return EOK on success or an error code
150 */
151errno_t ds_display_load_cfg(ds_display_t *display, const char *cfgpath)
152{
153 sif_doc_t *doc = NULL;
154 sif_node_t *rnode;
155 sif_node_t *ndisplay;
156 sif_node_t *nseats;
157 sif_node_t *nseat;
158 ds_seat_t *seat;
159 sif_node_t *nidevcfgs;
160 sif_node_t *nidevcfg;
161 const char *ntype;
162 ds_idevcfg_t *idevcfg;
163 errno_t rc;
164
165 rc = sif_load(cfgpath, &doc);
166 if (rc != EOK)
167 goto error;
168
169 rnode = sif_get_root(doc);
170 ndisplay = sif_node_first_child(rnode);
171 ntype = sif_node_get_type(ndisplay);
172 if (str_cmp(ntype, "display") != 0) {
173 rc = EIO;
174 goto error;
175 }
176
177 nseats = sif_node_first_child(ndisplay);
178 ntype = sif_node_get_type(nseats);
179 if (str_cmp(ntype, "seats") != 0) {
180 rc = EIO;
181 goto error;
182 }
183
184 /* Load individual seats */
185 nseat = sif_node_first_child(nseats);
186 while (nseat != NULL) {
187 ntype = sif_node_get_type(nseat);
188 if (str_cmp(ntype, "seat") != 0) {
189 rc = EIO;
190 goto error;
191 }
192
193 rc = ds_seat_load(display, nseat, &seat);
194 if (rc != EOK)
195 goto error;
196
197 (void)seat;
198 nseat = sif_node_next_child(nseat);
199 }
200
201 nidevcfgs = sif_node_next_child(nseats);
202 ntype = sif_node_get_type(nidevcfgs);
203 if (str_cmp(ntype, "idevcfgs") != 0) {
204 rc = EIO;
205 goto error;
206 }
207
208 /* Load individual input device configuration entries */
209 nidevcfg = sif_node_first_child(nidevcfgs);
210 while (nidevcfg != NULL) {
211 ntype = sif_node_get_type(nidevcfg);
212 if (str_cmp(ntype, "idevcfg") != 0) {
213 rc = EIO;
214 goto error;
215 }
216
217 rc = ds_idevcfg_load(display, nidevcfg, &idevcfg);
218 if (rc != EOK)
219 goto error;
220
221 (void)idevcfg;
222 nidevcfg = sif_node_next_child(nidevcfg);
223 }
224
225 sif_delete(doc);
226 return EOK;
227error:
228 if (doc != NULL)
229 sif_delete(doc);
230
231 seat = ds_display_first_seat(display);
232 while (seat != NULL) {
233 ds_seat_destroy(seat);
234 seat = ds_display_first_seat(display);
235 }
236 return rc;
237}
238
239/** Save seat to SIF node.
240 *
241 * @param display Display
242 * @param cfgpath Configuration file path
243 *
244 * @return EOK on success or an error code
245 */
246errno_t ds_display_save_cfg(ds_display_t *display, const char *cfgpath)
247{
248 sif_doc_t *doc = NULL;
249 sif_node_t *rnode;
250 sif_node_t *ndisplay;
251 sif_node_t *nseats;
252 sif_node_t *nseat;
253 ds_seat_t *seat;
254 sif_node_t *nidevcfgs;
255 sif_node_t *nidevcfg;
256 ds_idevcfg_t *idevcfg;
257 errno_t rc;
258
259 rc = sif_new(&doc);
260 if (rc != EOK)
261 goto error;
262
263 rnode = sif_get_root(doc);
264 rc = sif_node_append_child(rnode, "display", &ndisplay);
265 if (rc != EOK)
266 goto error;
267
268 rc = sif_node_append_child(ndisplay, "seats", &nseats);
269 if (rc != EOK)
270 goto error;
271
272 /* Save individual seats */
273 seat = ds_display_first_seat(display);
274 while (seat != NULL) {
275 rc = sif_node_append_child(nseats, "seat", &nseat);
276 if (rc != EOK)
277 goto error;
278
279 rc = ds_seat_save(seat, nseat);
280 if (rc != EOK)
281 goto error;
282
283 seat = ds_display_next_seat(seat);
284 }
285
286 rc = sif_node_append_child(ndisplay, "idevcfgs", &nidevcfgs);
287 if (rc != EOK)
288 goto error;
289
290 /* Save individual input device configuration entries */
291 idevcfg = ds_display_first_idevcfg(display);
292 while (idevcfg != NULL) {
293 rc = sif_node_append_child(nidevcfgs, "idevcfg", &nidevcfg);
294 if (rc != EOK)
295 goto error;
296
297 rc = ds_idevcfg_save(idevcfg, nidevcfg);
298 if (rc != EOK)
299 goto error;
300
301 idevcfg = ds_display_next_idevcfg(idevcfg);
302 }
303
304 rc = sif_save(doc, cfgpath);
305 if (rc != EOK)
306 goto error;
307
308 sif_delete(doc);
309 return EOK;
310error:
311 if (doc != NULL)
312 sif_delete(doc);
313 return rc;
314}
315
[c11ee605]316/** Lock display.
317 *
318 * This should be called in any thread that wishes to access the display
319 * or its child objects (e.g. windows).
320 *
321 * @param disp Display
322 */
323void ds_display_lock(ds_display_t *disp)
324{
325 fibril_mutex_lock(&disp->lock);
326}
327
328/** Unlock display.
329 *
330 * @param disp Display
331 */
332void ds_display_unlock(ds_display_t *disp)
333{
334 fibril_mutex_unlock(&disp->lock);
335}
336
[aeb3037]337/** Get display information.
338 *
339 * @param disp Display
340 */
341void ds_display_get_info(ds_display_t *disp, display_info_t *info)
342{
343 info->rect = disp->rect;
344}
345
[b3c185b6]346/** Add client to display.
[6af4b4f]347 *
348 * @param disp Display
[cf32dbd]349 * @param client Client
[6af4b4f]350 */
[b3c185b6]351void ds_display_add_client(ds_display_t *disp, ds_client_t *client)
[6af4b4f]352{
[b3c185b6]353 assert(client->display == NULL);
354 assert(!link_used(&client->lclients));
[6af4b4f]355
[b3c185b6]356 client->display = disp;
357 list_append(&client->lclients, &disp->clients);
[c8cf261]358}
359
[b3c185b6]360/** Remove client from display.
[6af4b4f]361 *
[cf32dbd]362 * @param client Client
[6af4b4f]363 */
[b3c185b6]364void ds_display_remove_client(ds_client_t *client)
[6af4b4f]365{
[b3c185b6]366 list_remove(&client->lclients);
367 client->display = NULL;
[6af4b4f]368}
369
[b3c185b6]370/** Get first client in display.
[6af4b4f]371 *
372 * @param disp Display
[b3c185b6]373 * @return First client or @c NULL if there is none
[6af4b4f]374 */
[b3c185b6]375ds_client_t *ds_display_first_client(ds_display_t *disp)
[6af4b4f]376{
[b3c185b6]377 link_t *link = list_first(&disp->clients);
[6af4b4f]378
[b3c185b6]379 if (link == NULL)
380 return NULL;
[6af4b4f]381
[b3c185b6]382 return list_get_instance(link, ds_client_t, lclients);
[6af4b4f]383}
384
[b3c185b6]385/** Get next client in display.
[6af4b4f]386 *
[b3c185b6]387 * @param client Current client
388 * @return Next client or @c NULL if there is none
[6af4b4f]389 */
[b3c185b6]390ds_client_t *ds_display_next_client(ds_client_t *client)
[6af4b4f]391{
[b3c185b6]392 link_t *link = list_next(&client->lclients, &client->display->clients);
[6af4b4f]393
394 if (link == NULL)
395 return NULL;
396
[b3c185b6]397 return list_get_instance(link, ds_client_t, lclients);
[6af4b4f]398}
399
[913add60]400/** Add WM client to display.
401 *
402 * @param disp Display
403 * @param wmclient WM client
404 */
405void ds_display_add_wmclient(ds_display_t *disp, ds_wmclient_t *wmclient)
406{
407 assert(wmclient->display == NULL);
408 assert(!link_used(&wmclient->lwmclients));
409
410 wmclient->display = disp;
411 list_append(&wmclient->lwmclients, &disp->wmclients);
412}
413
414/** Remove WM client from display.
415 *
416 * @param wmclient WM client
417 */
418void ds_display_remove_wmclient(ds_wmclient_t *wmclient)
419{
420 list_remove(&wmclient->lwmclients);
421 wmclient->display = NULL;
422}
423
[d8503fd]424/** Add CFG client to display.
425 *
426 * @param disp Display
427 * @param cfgclient CFG client
428 */
429void ds_display_add_cfgclient(ds_display_t *disp, ds_cfgclient_t *cfgclient)
430{
431 assert(cfgclient->display == NULL);
432 assert(!link_used(&cfgclient->lcfgclients));
433
434 cfgclient->display = disp;
435 list_append(&cfgclient->lcfgclients, &disp->cfgclients);
436}
437
438/** Remove CFG client from display.
439 *
440 * @param cfgclient CFG client
441 */
442void ds_display_remove_cfgclient(ds_cfgclient_t *cfgclient)
443{
444 list_remove(&cfgclient->lcfgclients);
445 cfgclient->display = NULL;
446}
447
[913add60]448/** Get first WM client in display.
449 *
450 * @param disp Display
451 * @return First WM client or @c NULL if there is none
452 */
453ds_wmclient_t *ds_display_first_wmclient(ds_display_t *disp)
454{
455 link_t *link = list_first(&disp->wmclients);
456
457 if (link == NULL)
458 return NULL;
459
460 return list_get_instance(link, ds_wmclient_t, lwmclients);
461}
462
463/** Get next WM client in display.
464 *
465 * @param wmclient Current WM client
466 * @return Next WM client or @c NULL if there is none
467 */
468ds_wmclient_t *ds_display_next_wmclient(ds_wmclient_t *wmclient)
469{
470 link_t *link = list_next(&wmclient->lwmclients,
471 &wmclient->display->wmclients);
472
473 if (link == NULL)
474 return NULL;
475
476 return list_get_instance(link, ds_wmclient_t, lwmclients);
477}
478
[b3c185b6]479/** Find window in all clients by ID.
[6af4b4f]480 *
[b3c185b6]481 * XXX This is just a hack needed to match GC connection to a window,
482 * as we don't have a good safe way to pass the GC endpoint to our client
483 * on demand.
484 *
485 * @param display Display
486 * @param id Window ID
[6af4b4f]487 */
[b3c185b6]488ds_window_t *ds_display_find_window(ds_display_t *display, ds_wnd_id_t id)
[6af4b4f]489{
[b3c185b6]490 ds_client_t *client;
491 ds_window_t *wnd;
[6af4b4f]492
[b3c185b6]493 client = ds_display_first_client(display);
494 while (client != NULL) {
495 wnd = ds_client_find_window(client, id);
[195b7b3]496 if (wnd != NULL)
[b3c185b6]497 return wnd;
[195b7b3]498
[b3c185b6]499 client = ds_display_next_client(client);
500 }
501
502 return NULL;
503}
504
[24cf391a]505/** Find window by display position.
506 *
507 * @param display Display
508 * @param pos Display position
509 */
510ds_window_t *ds_display_window_by_pos(ds_display_t *display, gfx_coord2_t *pos)
511{
512 ds_window_t *wnd;
[fb420e48]513 gfx_rect_t drect;
[24cf391a]514
515 wnd = ds_display_first_window(display);
516 while (wnd != NULL) {
[fb420e48]517 /* Window bounding rectangle on display */
518 gfx_rect_translate(&wnd->dpos, &wnd->rect, &drect);
519
[06176e1]520 if (gfx_pix_inside_rect(pos, &drect) &&
521 ds_window_is_visible(wnd))
[24cf391a]522 return wnd;
523
524 wnd = ds_display_next_window(wnd);
525 }
526
527 return NULL;
528}
529
[5d62130]530/** Add window to window list.
531 *
532 * Topmost windows are enlisted before any other window. Non-topmost
533 * windows are enlisted before any other non-topmost window.
534 *
535 * @param display Display
536 * @param wnd Window
537 */
538void ds_display_enlist_window(ds_display_t *display, ds_window_t *wnd)
539{
540 ds_window_t *w;
541
542 assert(wnd->display == display);
543 assert(!link_used(&wnd->ldwindows));
544
545 if ((wnd->flags & wndf_topmost) == 0) {
546 /* Find the first non-topmost window */
547 w = ds_display_first_window(display);
548 while (w != NULL && (w->flags & wndf_topmost) != 0)
549 w = ds_display_next_window(w);
550
551 if (w != NULL)
552 list_insert_before(&wnd->ldwindows, &w->ldwindows);
553 else
554 list_append(&wnd->ldwindows, &display->windows);
555 } else {
556 /* Insert at the beginning */
557 list_prepend(&wnd->ldwindows, &display->windows);
558 }
559
560}
561
[fd777a2]562/** Add window to display.
563 *
564 * @param display Display
565 * @param wnd Window
566 */
567void ds_display_add_window(ds_display_t *display, ds_window_t *wnd)
568{
[913add60]569 ds_wmclient_t *wmclient;
570
[fd777a2]571 assert(wnd->display == NULL);
572 assert(!link_used(&wnd->ldwindows));
573
574 wnd->display = display;
[5d62130]575 ds_display_enlist_window(display, wnd);
[913add60]576
577 /* Notify window managers about the new window */
578 wmclient = ds_display_first_wmclient(display);
579 while (wmclient != NULL) {
580 ds_wmclient_post_wnd_added_event(wmclient, wnd->id);
581 wmclient = ds_display_next_wmclient(wmclient);
582 }
[fd777a2]583}
584
585/** Remove window from display.
586 *
587 * @param wnd Window
588 */
589void ds_display_remove_window(ds_window_t *wnd)
590{
[913add60]591 ds_wmclient_t *wmclient;
592 ds_display_t *display;
593
594 display = wnd->display;
595
[fd777a2]596 list_remove(&wnd->ldwindows);
597 wnd->display = NULL;
[913add60]598
599 /* Notify window managers about the removed window */
600 wmclient = ds_display_first_wmclient(display);
601 while (wmclient != NULL) {
602 ds_wmclient_post_wnd_removed_event(wmclient, wnd->id);
603 wmclient = ds_display_next_wmclient(wmclient);
604 }
605}
606
607/** Move window to top.
608 *
609 * @param display Display
610 * @param wnd Window
611 */
612void ds_display_window_to_top(ds_window_t *wnd)
613{
614 assert(wnd->display != NULL);
615 assert(link_used(&wnd->ldwindows));
616
617 list_remove(&wnd->ldwindows);
[5d62130]618 ds_display_enlist_window(wnd->display, wnd);
[fd777a2]619}
620
621/** Get first window in display.
622 *
623 * @param display Display
624 * @return First window or @c NULL if there is none
625 */
626ds_window_t *ds_display_first_window(ds_display_t *display)
627{
628 link_t *link = list_first(&display->windows);
629
630 if (link == NULL)
631 return NULL;
632
633 return list_get_instance(link, ds_window_t, ldwindows);
634}
635
[2012fe0]636/** Get last window in display.
637 *
638 * @param display Display
639 * @return Last window or @c NULL if there is none
640 */
641ds_window_t *ds_display_last_window(ds_display_t *display)
642{
643 link_t *link = list_last(&display->windows);
644
645 if (link == NULL)
646 return NULL;
647
648 return list_get_instance(link, ds_window_t, ldwindows);
649}
650
[fd777a2]651/** Get next window in client.
652 *
653 * @param wnd Current window
654 * @return Next window or @c NULL if there is none
655 */
656ds_window_t *ds_display_next_window(ds_window_t *wnd)
657{
658 link_t *link = list_next(&wnd->ldwindows, &wnd->display->windows);
659
660 if (link == NULL)
661 return NULL;
662
663 return list_get_instance(link, ds_window_t, ldwindows);
664}
665
[2012fe0]666/** Get previous window in client.
667 *
668 * @param wnd Current window
669 * @return Previous window or @c NULL if there is none
670 */
671ds_window_t *ds_display_prev_window(ds_window_t *wnd)
672{
673 link_t *link = list_prev(&wnd->ldwindows, &wnd->display->windows);
674
675 if (link == NULL)
676 return NULL;
677
678 return list_get_instance(link, ds_window_t, ldwindows);
679}
680
[cf32dbd]681/** Post keyboard event to a display.
682 *
683 * The event is routed to the correct window by first determining the
684 * seat the keyboard device belongs to and then the event is sent to the
685 * window focused by that seat.
686 *
687 * @param display Display
688 * @param event Event
689 */
[b3c185b6]690errno_t ds_display_post_kbd_event(ds_display_t *display, kbd_event_t *event)
691{
[cf32dbd]692 ds_seat_t *seat;
[b3c185b6]693
[88d828e]694 /* Determine which seat the event belongs to */
695 seat = ds_display_seat_by_idev(display, event->kbd_id);
[cf32dbd]696 if (seat == NULL)
[b3c185b6]697 return EOK;
698
[cf32dbd]699 return ds_seat_post_kbd_event(seat, event);
700}
701
[24cf391a]702/** Post position event to a display.
703 *
704 * @param display Display
705 * @param event Event
706 */
[4fbdc3d]707errno_t ds_display_post_ptd_event(ds_display_t *display, ptd_event_t *event)
[24cf391a]708{
709 ds_seat_t *seat;
710
[88d828e]711 /* Determine which seat the event belongs to */
712 seat = ds_display_seat_by_idev(display, event->pos_id);
[4fbdc3d]713 if (seat == NULL)
714 return EOK;
[24cf391a]715
[4fbdc3d]716 return ds_seat_post_ptd_event(seat, event);
[24cf391a]717}
718
[cf32dbd]719/** Add seat to display.
720 *
721 * @param disp Display
722 * @param seat Seat
723 */
724void ds_display_add_seat(ds_display_t *disp, ds_seat_t *seat)
725{
726 assert(seat->display == NULL);
727 assert(!link_used(&seat->lseats));
728
729 seat->display = disp;
[d8503fd]730 seat->id = disp->next_seat_id++;
[cf32dbd]731 list_append(&seat->lseats, &disp->seats);
732}
733
734/** Remove seat from display.
735 *
736 * @param seat Seat
737 */
738void ds_display_remove_seat(ds_seat_t *seat)
739{
740 list_remove(&seat->lseats);
741 seat->display = NULL;
742}
743
744/** Get first seat in display.
745 *
746 * @param disp Display
747 * @return First seat or @c NULL if there is none
748 */
749ds_seat_t *ds_display_first_seat(ds_display_t *disp)
750{
751 link_t *link = list_first(&disp->seats);
752
753 if (link == NULL)
754 return NULL;
755
756 return list_get_instance(link, ds_seat_t, lseats);
757}
758
759/** Get next seat in display.
760 *
761 * @param seat Current seat
762 * @return Next seat or @c NULL if there is none
763 */
764ds_seat_t *ds_display_next_seat(ds_seat_t *seat)
765{
766 link_t *link = list_next(&seat->lseats, &seat->display->seats);
767
768 if (link == NULL)
769 return NULL;
[6af4b4f]770
[cf32dbd]771 return list_get_instance(link, ds_seat_t, lseats);
[6af4b4f]772}
773
[5d380b6]774/** Get default seat in display.
775 *
776 * @param disp Display
777 * @return First seat or @c NULL if there is none
778 */
779ds_seat_t *ds_display_default_seat(ds_display_t *disp)
780{
781 /* XXX Probably not the best solution */
782 return ds_display_first_seat(disp);
783}
784
[d8503fd]785/** Find seat by ID.
786 *
787 * @param display Display
788 * @param id Seat ID
789 */
790ds_seat_t *ds_display_find_seat(ds_display_t *display, ds_seat_id_t id)
791{
792 ds_seat_t *seat;
793
794 seat = ds_display_first_seat(display);
795 while (seat != NULL) {
796 if (seat->id == id)
797 return seat;
798
799 seat = ds_display_next_seat(seat);
800 }
801
802 return NULL;
803}
804
[88d828e]805/** Get seat which owns the specified input device.
806 *
807 * @param disp Display
808 * @param idev_id Input device ID
809 * @return Seat which owns device with ID @a idev_id or @c NULL if not found
810 */
811ds_seat_t *ds_display_seat_by_idev(ds_display_t *disp, ds_idev_id_t idev_id)
812{
[b3eeae5]813 ds_idevcfg_t *idevcfg;
[88d828e]814
[b3eeae5]815 /*
816 * Find input device configuration entry that maps this input device
817 * to a seat.
818 */
819 idevcfg = ds_display_first_idevcfg(disp);
820 while (idevcfg != NULL) {
821 if (idevcfg->svc_id == idev_id)
822 return idevcfg->seat;
823
824 idevcfg = ds_display_next_idevcfg(idevcfg);
825 }
826
827 /* If none was found, return the default seat */
[5d380b6]828 return ds_display_default_seat(disp);
[88d828e]829}
830
[8aef01c]831/** Allocate back buffer for display.
832 *
833 * @param disp Display
834 * @return EOK on success or if no back buffer is required, otherwise
835 * an error code.
836 */
837static errno_t ds_display_alloc_backbuf(ds_display_t *disp)
838{
839 gfx_context_t *ugc;
840 gfx_bitmap_params_t params;
841 gfx_bitmap_alloc_t alloc;
842 errno_t rc;
843
844 /* Allocate backbuffer */
845 if ((disp->flags & df_disp_double_buf) == 0) {
846 /* Not double buffering. Nothing to do. */
847 return EOK;
848 }
849
850 ugc = ds_display_get_unbuf_gc(disp);
851
852 gfx_bitmap_params_init(&params);
853 params.rect = disp->rect;
854
855 rc = gfx_bitmap_create(ugc, &params, NULL,
856 &disp->backbuf);
857 if (rc != EOK)
858 goto error;
859
860 rc = gfx_bitmap_get_alloc(disp->backbuf, &alloc);
861 if (rc != EOK)
862 goto error;
863
[1215db9]864 rc = mem_gc_create(&disp->rect, &alloc, &ds_display_mem_gc_cb,
865 (void *) disp, &disp->bbgc);
[8aef01c]866 if (rc != EOK)
867 goto error;
868
869 disp->dirty_rect.p0.x = 0;
870 disp->dirty_rect.p0.y = 0;
871 disp->dirty_rect.p1.x = 0;
872 disp->dirty_rect.p1.y = 0;
873
874 return EOK;
875error:
876 if (disp->backbuf != NULL) {
877 gfx_bitmap_destroy(disp->backbuf);
878 disp->backbuf = NULL;
879 }
880
881 return rc;
882}
883
[87a7cdb]884/** Add display device to display.
885 *
886 * @param disp Display
887 * @param ddev Display device
[8aef01c]888 * @return EOK on success, or an error code
[87a7cdb]889 */
[8aef01c]890errno_t ds_display_add_ddev(ds_display_t *disp, ds_ddev_t *ddev)
[87a7cdb]891{
[8aef01c]892 errno_t rc;
[d9d6f29]893 gfx_rect_t old_disp_rect;
[8aef01c]894
[87a7cdb]895 assert(ddev->display == NULL);
896 assert(!link_used(&ddev->lddevs));
897
[d9d6f29]898 old_disp_rect = disp->rect;
899
[87a7cdb]900 ddev->display = disp;
901 list_append(&ddev->lddevs, &disp->ddevs);
[8aef01c]902
903 /* First display device */
904 if (gfx_rect_is_empty(&disp->rect)) {
905 /* Set screen dimensions */
906 disp->rect = ddev->info.rect;
907
[5271e4c]908 /* Create cloning GC */
909 rc = ds_clonegc_create(ddev->gc, &disp->fbgc);
[d9d6f29]910 if (rc != EOK)
911 goto error;
[5271e4c]912
[8aef01c]913 /* Allocate backbuffer */
914 rc = ds_display_alloc_backbuf(disp);
[5271e4c]915 if (rc != EOK) {
[d9d6f29]916 ds_clonegc_delete(disp->fbgc);
917 disp->fbgc = NULL;
[5271e4c]918 goto error;
919 }
920 } else {
921 /* Add new output device to cloning GC */
922 rc = ds_clonegc_add_output(disp->fbgc, ddev->gc);
[8aef01c]923 if (rc != EOK)
924 goto error;
925 }
926
[29a5a99]927 ds_display_update_max_rect(disp);
928
[8aef01c]929 return EOK;
930error:
[d9d6f29]931 disp->rect = old_disp_rect;
[8aef01c]932 list_remove(&ddev->lddevs);
933 return rc;
[87a7cdb]934}
935
936/** Remove display device from display.
937 *
938 * @param ddev Display device
939 */
940void ds_display_remove_ddev(ds_ddev_t *ddev)
941{
942 list_remove(&ddev->lddevs);
943 ddev->display = NULL;
944}
945
946/** Get first display device in display.
947 *
948 * @param disp Display
949 * @return First display device or @c NULL if there is none
950 */
951ds_ddev_t *ds_display_first_ddev(ds_display_t *disp)
952{
953 link_t *link = list_first(&disp->ddevs);
954
955 if (link == NULL)
956 return NULL;
957
958 return list_get_instance(link, ds_ddev_t, lddevs);
959}
960
961/** Get next display device in display.
962 *
963 * @param ddev Current display device
964 * @return Next display device or @c NULL if there is none
965 */
966ds_ddev_t *ds_display_next_ddev(ds_ddev_t *ddev)
967{
968 link_t *link = list_next(&ddev->lddevs, &ddev->display->ddevs);
969
970 if (link == NULL)
971 return NULL;
972
973 return list_get_instance(link, ds_ddev_t, lddevs);
974}
975
[b3eeae5]976/** Add input device configuration entry to display.
977 *
978 * @param disp Display
979 * @param idevcfg Input device configuration
980 */
981void ds_display_add_idevcfg(ds_display_t *disp, ds_idevcfg_t *idevcfg)
982{
983 assert(idevcfg->display == NULL);
[a0d4afe]984 assert(!link_used(&idevcfg->ldispidcfgs));
[b3eeae5]985
986 idevcfg->display = disp;
[a0d4afe]987 list_append(&idevcfg->ldispidcfgs, &disp->idevcfgs);
[b3eeae5]988}
989
990/** Remove input device configuration entry from display.
991 *
992 * @param idevcfg Input device configuration entry
993 */
994void ds_display_remove_idevcfg(ds_idevcfg_t *idevcfg)
995{
[a0d4afe]996 list_remove(&idevcfg->ldispidcfgs);
[b3eeae5]997 idevcfg->display = NULL;
998}
999
1000/** Get first input device configuration entry in display.
1001 *
1002 * @param disp Display
1003 * @return First input device configuration entry or @c NULL if there is none
1004 */
1005ds_idevcfg_t *ds_display_first_idevcfg(ds_display_t *disp)
1006{
1007 link_t *link = list_first(&disp->idevcfgs);
1008
1009 if (link == NULL)
1010 return NULL;
1011
[a0d4afe]1012 return list_get_instance(link, ds_idevcfg_t, ldispidcfgs);
[b3eeae5]1013}
1014
1015/** Get next input device configuration entry in display.
1016 *
1017 * @param idevcfg Current input device configuration entry
1018 * @return Next input device configuration entry or @c NULL if there is none
1019 */
1020ds_idevcfg_t *ds_display_next_idevcfg(ds_idevcfg_t *idevcfg)
1021{
[a0d4afe]1022 link_t *link = list_next(&idevcfg->ldispidcfgs, &idevcfg->display->idevcfgs);
[b3eeae5]1023
1024 if (link == NULL)
1025 return NULL;
1026
[a0d4afe]1027 return list_get_instance(link, ds_idevcfg_t, ldispidcfgs);
[b3eeae5]1028}
1029
[4d8002d]1030/** Add cursor to display.
1031 *
1032 * @param display Display
1033 * @param cursor Cursor
1034 */
1035void ds_display_add_cursor(ds_display_t *display, ds_cursor_t *cursor)
1036{
1037 assert(cursor->display == NULL);
1038 assert(!link_used(&cursor->ldisplay));
1039
1040 cursor->display = display;
1041 list_prepend(&cursor->ldisplay, &display->cursors);
1042}
1043
1044/** Remove cursor from display.
1045 *
1046 * @param cursor Cursor
1047 */
1048void ds_display_remove_cursor(ds_cursor_t *cursor)
1049{
1050 list_remove(&cursor->ldisplay);
1051 cursor->display = NULL;
1052}
1053
[29a5a99]1054/** Update display maximize rectangle.
1055 *
1056 * Recalculate the maximize rectangle (the rectangle used for maximized
1057 * windows).
1058 *
1059 * @param display Display
1060 */
1061void ds_display_update_max_rect(ds_display_t *display)
1062{
1063 ds_window_t *wnd;
1064 gfx_rect_t max_rect;
1065 gfx_rect_t drect;
1066
1067 /* Start with the entire display */
1068 max_rect = display->rect;
1069
1070 wnd = ds_display_first_window(display);
1071 while (wnd != NULL) {
1072 /* Should maximized windows avoid this window? */
1073 if ((wnd->flags & wndf_avoid) != 0) {
1074 /* Window bounding rectangle on display */
1075 gfx_rect_translate(&wnd->dpos, &wnd->rect, &drect);
1076
1077 /* Crop maximized rectangle */
1078 ds_display_crop_max_rect(&drect, &max_rect);
1079 }
1080
1081 wnd = ds_display_next_window(wnd);
1082 }
1083
1084 /* Update the maximize rectangle */
1085 display->max_rect = max_rect;
1086}
1087
1088/** Crop maximize rectangle.
1089 *
1090 * Use the avoid rectangle @a arect to crop off maximization rectangle
1091 * @a mrect. If @a arect covers the top, bottom, left or right part
1092 * of @a mrect, it will be cropped off. Otherwise there will be
1093 * no effect.
1094 *
1095 * @param arect Avoid rectangle
1096 * @param mrect Maximize rectangle to be modified
1097 */
1098void ds_display_crop_max_rect(gfx_rect_t *arect, gfx_rect_t *mrect)
1099{
1100 if (arect->p0.x == mrect->p0.x && arect->p0.y == mrect->p0.y &&
1101 arect->p1.x == mrect->p1.x) {
1102 /* Cropp off top part */
1103 mrect->p0.y = arect->p1.y;
1104 } else if (arect->p0.x == mrect->p0.x && arect->p1.x == mrect->p1.x &&
1105 arect->p1.y == mrect->p1.y) {
1106 /* Cropp off bottom part */
1107 mrect->p1.y = arect->p0.y;
1108 } else if (arect->p0.x == mrect->p0.x && arect->p0.y == mrect->p0.y &&
1109 arect->p1.y == mrect->p1.y) {
1110 /* Cropp off left part */
1111 mrect->p0.x = arect->p1.x;
1112 } else if (arect->p0.y == mrect->p0.y && arect->p1.x == mrect->p1.x &&
1113 arect->p1.y == mrect->p1.y) {
1114 /* Cropp off right part */
1115 mrect->p1.x = arect->p0.x;
1116 }
1117}
1118
[84e74ea]1119/** Get unbuffered GC.
1120 *
1121 * Get the display's (unbuffered) graphic context. If the display
1122 * is double-buffered, this returns GC of the front buffer. If the display
1123 * is unbuffered, this is the same as @c ds_display_get_gc().
1124 *
1125 * @param display Display
1126 * @return Unbuffered GC
1127 */
[8aef01c]1128static gfx_context_t *ds_display_get_unbuf_gc(ds_display_t *display)
[87a7cdb]1129{
[5271e4c]1130 /* In case of unit tests */
1131 if (display->fbgc == NULL)
[4fbdc3d]1132 return NULL;
[87a7cdb]1133
[5271e4c]1134 return ds_clonegc_get_ctx(display->fbgc);
[87a7cdb]1135}
1136
[84e74ea]1137/** Get display GC.
1138 *
1139 * Get the graphic context used to paint the display. This is to be used
1140 * for all display server paint operations.
1141 *
1142 * @param display Display
1143 * @return Graphic context for painting to the display
1144 */
[8aef01c]1145gfx_context_t *ds_display_get_gc(ds_display_t *display)
1146{
1147 if ((display->flags & df_disp_double_buf) != 0)
1148 return mem_gc_get_ctx(display->bbgc);
1149 else
1150 return ds_display_get_unbuf_gc(display);
1151}
1152
[c79545e]1153/** Paint display background.
1154 *
1155 * @param display Display
1156 * @param rect Bounding rectangle or @c NULL to repaint entire display
1157 */
1158errno_t ds_display_paint_bg(ds_display_t *disp, gfx_rect_t *rect)
1159{
1160 gfx_rect_t crect;
1161 gfx_context_t *gc;
1162 errno_t rc;
1163
1164 if (rect != NULL)
[e1f2079]1165 gfx_rect_clip(&disp->rect, rect, &crect);
[c79545e]1166 else
[e1f2079]1167 crect = disp->rect;
[c79545e]1168
[84e74ea]1169 gc = ds_display_get_gc(disp);
[f5191b4]1170 if (gc == NULL)
1171 return EOK;
[c79545e]1172
1173 rc = gfx_set_color(gc, disp->bg_color);
1174 if (rc != EOK)
1175 return rc;
1176
1177 return gfx_fill_rect(gc, &crect);
1178}
1179
[8aef01c]1180/** Update front buffer from back buffer.
1181 *
1182 * If the display is not double-buffered, no action is taken.
1183 *
1184 * @param disp Display
1185 * @return EOK on success, or an error code
1186 */
1187static errno_t ds_display_update(ds_display_t *disp)
1188{
1189 errno_t rc;
1190
1191 if (disp->backbuf == NULL) {
1192 /* Not double-buffered, nothing to do. */
1193 return EOK;
1194 }
1195
1196 rc = gfx_bitmap_render(disp->backbuf, &disp->dirty_rect, NULL);
1197 if (rc != EOK)
1198 return rc;
1199
1200 disp->dirty_rect.p0.x = 0;
1201 disp->dirty_rect.p0.y = 0;
1202 disp->dirty_rect.p1.x = 0;
1203 disp->dirty_rect.p1.y = 0;
1204
1205 return EOK;
1206}
1207
[2012fe0]1208/** Paint display.
1209 *
1210 * @param display Display
1211 * @param rect Bounding rectangle or @c NULL to repaint entire display
1212 */
1213errno_t ds_display_paint(ds_display_t *disp, gfx_rect_t *rect)
1214{
1215 errno_t rc;
1216 ds_window_t *wnd;
[978c9bc5]1217 ds_seat_t *seat;
[2012fe0]1218
1219 /* Paint background */
1220 rc = ds_display_paint_bg(disp, rect);
1221 if (rc != EOK)
1222 return rc;
1223
1224 /* Paint windows bottom to top */
1225 wnd = ds_display_last_window(disp);
1226 while (wnd != NULL) {
1227 rc = ds_window_paint(wnd, rect);
1228 if (rc != EOK)
1229 return rc;
1230
1231 wnd = ds_display_prev_window(wnd);
1232 }
1233
[6301a24f]1234 /* Paint window previews for windows being resized or moved */
1235 wnd = ds_display_last_window(disp);
1236 while (wnd != NULL) {
1237 rc = ds_window_paint_preview(wnd, rect);
1238 if (rc != EOK)
1239 return rc;
1240
1241 wnd = ds_display_prev_window(wnd);
1242 }
1243
1244 /* Paint pointers */
[978c9bc5]1245 seat = ds_display_first_seat(disp);
1246 while (seat != NULL) {
1247 rc = ds_seat_paint_pointer(seat, rect);
1248 if (rc != EOK)
1249 return rc;
1250
1251 seat = ds_display_next_seat(seat);
1252 }
1253
[8aef01c]1254 return ds_display_update(disp);
1255}
1256
[2ab8ab3]1257/** Display invalidate callback.
[8aef01c]1258 *
1259 * Called by backbuffer memory GC when something is rendered into it.
1260 * Updates the display's dirty rectangle.
1261 *
1262 * @param arg Argument (display cast as void *)
1263 * @param rect Rectangle to update
1264 */
[2ab8ab3]1265static void ds_display_invalidate_cb(void *arg, gfx_rect_t *rect)
[8aef01c]1266{
1267 ds_display_t *disp = (ds_display_t *) arg;
1268 gfx_rect_t env;
1269
1270 gfx_rect_envelope(&disp->dirty_rect, rect, &env);
1271 disp->dirty_rect = env;
[2012fe0]1272}
1273
[2ab8ab3]1274/** Display update callback.
1275 *
1276 * @param arg Argument (display cast as void *)
1277 */
1278static void ds_display_update_cb(void *arg)
1279{
1280 ds_display_t *disp = (ds_display_t *) arg;
1281
1282 (void) disp;
1283}
1284
[c8cf261]1285/** @}
1286 */
Note: See TracBrowser for help on using the repository browser.