source: mainline/uspace/lib/gui/window.c@ 8bf9058

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

Clean up vestiges of visualizer interface

  • Property mode set to 100644
File size: 23.6 KB
Line 
1/*
2 * Copyright (c) 2012 Petr Koupy
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 gui
30 * @{
31 */
32/**
33 * @file
34 */
35
36#include <stdbool.h>
37#include <errno.h>
38#include <stdio.h>
39#include <stdlib.h>
40
41#include <as.h>
42#include <stdlib.h>
43#include <str.h>
44
45#include <fibril.h>
46#include <task.h>
47#include <adt/prodcons.h>
48#include <adt/list.h>
49
50#include <loc.h>
51
52#include <io/pixel.h>
53#include <draw/source.h>
54#include <draw/font.h>
55#include <draw/drawctx.h>
56#include <draw/surface.h>
57#include <display.h>
58
59#include "common.h"
60#include "connection.h"
61#include "widget.h"
62#include "window.h"
63
64static sysarg_t border_thickness = 4;
65static sysarg_t bevel_thickness = 1;
66static sysarg_t header_height = 20;
67static sysarg_t header_min_width = 40;
68static sysarg_t close_thickness = 20;
69static sysarg_t corner_size = 24;
70static sysarg_t window_initial_size = 1;
71
72static pixel_t color_highlight = PIXEL(255, 255, 255, 255);
73static pixel_t color_shadow = PIXEL(255, 85, 85, 85);
74static pixel_t color_surface = PIXEL(255, 186, 186, 186);
75
76static pixel_t color_header_focus_highlight = PIXEL(255, 120, 145, 255);
77static pixel_t color_header_focus_shadow = PIXEL(255, 40, 48, 89);
78static pixel_t color_header_focus_surface = PIXEL(255, 88, 106, 196);
79
80static pixel_t color_header_unfocus_highlight = PIXEL(255, 16, 78, 126);
81static pixel_t color_header_unfocus_shadow = PIXEL(255, 5, 26, 42);
82static pixel_t color_header_unfocus_surface = PIXEL(255, 12, 57, 92);
83
84static pixel_t color_caption_focus = PIXEL(255, 255, 255, 255);
85static pixel_t color_caption_unfocus = PIXEL(255, 207, 207, 207);
86
87static void window_close_event(void *);
88static void window_focus_event(void *);
89static void window_kbd_event(void *, kbd_event_t *);
90static void window_pos_event(void *, pos_event_t *);
91static void window_resize_event(void *, gfx_rect_t *);
92static void window_unfocus_event(void *);
93
94static display_wnd_cb_t window_cb = {
95 .close_event = window_close_event,
96 .focus_event = window_focus_event,
97 .kbd_event = window_kbd_event,
98 .pos_event = window_pos_event,
99 .resize_event = window_resize_event,
100 .unfocus_event = window_unfocus_event
101};
102
103static void set_cursor(window_t *window, display_stock_cursor_t cursor)
104{
105 if (cursor != window->cursor) {
106 (void) display_window_set_cursor(window->dwindow, cursor);
107 window->cursor = cursor;
108 }
109}
110
111static void paint_internal(widget_t *widget)
112{
113 surface_t *surface = window_claim(widget->window);
114 if (!surface)
115 window_yield(widget->window);
116
117 source_t source;
118 source_init(&source);
119
120 drawctx_t drawctx;
121 drawctx_init(&drawctx, surface);
122 drawctx_set_source(&drawctx, &source);
123
124 /* Window border outer bevel */
125
126 draw_bevel(&drawctx, &source, widget->vpos, widget->hpos,
127 widget->width, widget->height, color_highlight, color_shadow);
128
129 /* Window border surface */
130
131 source_set_color(&source, color_surface);
132 drawctx_transfer(&drawctx, widget->hpos + 1, widget->vpos + 1,
133 widget->width - 2, 2);
134 drawctx_transfer(&drawctx, widget->hpos + 1, widget->vpos + 1,
135 2, widget->height - 2);
136 drawctx_transfer(&drawctx, widget->hpos + 1,
137 widget->vpos + widget->height - 3, widget->width - 2, 2);
138 drawctx_transfer(&drawctx, widget->hpos + widget->width - 3,
139 widget->vpos + 1, 2, widget->height - 4);
140
141 /* Window border inner bevel */
142
143 draw_bevel(&drawctx, &source, widget->hpos + 3, widget->vpos + 3,
144 widget->width - 6, widget->height - 6, color_shadow,
145 color_highlight);
146
147 /* Header bevel */
148
149 sysarg_t header_hpos = widget->hpos + border_thickness;
150 sysarg_t header_vpos = widget->vpos + border_thickness;
151 sysarg_t header_width = widget->width - 2 * border_thickness -
152 close_thickness;
153
154 draw_bevel(&drawctx, &source, header_hpos, header_vpos,
155 header_width, header_height, widget->window->is_focused ?
156 color_header_focus_highlight : color_header_unfocus_highlight,
157 widget->window->is_focused ?
158 color_header_focus_shadow : color_header_unfocus_shadow);
159
160 /* Header surface */
161
162 source_set_color(&source, widget->window->is_focused ?
163 color_header_focus_surface : color_header_unfocus_surface);
164 drawctx_transfer(&drawctx, header_hpos + 1, header_vpos + 1,
165 header_width - 2, header_height - 2);
166
167 /* Close button bevel */
168
169 sysarg_t close_hpos = widget->hpos + widget->width -
170 border_thickness - close_thickness;
171 sysarg_t close_vpos = widget->vpos + border_thickness;
172
173 draw_bevel(&drawctx, &source, close_hpos, close_vpos,
174 close_thickness, close_thickness, color_highlight, color_shadow);
175
176 /* Close button surface */
177
178 source_set_color(&source, color_surface);
179 drawctx_transfer(&drawctx, close_hpos + 1, close_vpos + 1,
180 close_thickness - 2, close_thickness - 2);
181
182 /* Close button icon */
183
184 draw_icon_cross(surface, close_hpos + 3, close_vpos + 3,
185 color_highlight, color_shadow);
186
187 /* Window caption */
188
189 font_t *font;
190 errno_t rc = embedded_font_create(&font, 16);
191 if (rc != EOK) {
192 window_yield(widget->window);
193 return;
194 }
195
196 drawctx_set_font(&drawctx, font);
197 source_set_color(&source, widget->window->is_focused ?
198 color_caption_focus : color_caption_unfocus);
199
200 sysarg_t cpt_width;
201 sysarg_t cpt_height;
202 font_get_box(font, widget->window->caption, &cpt_width, &cpt_height);
203
204 bool draw_title =
205 (widget->width >= 2 * border_thickness + 2 * bevel_thickness +
206 close_thickness + cpt_width);
207 if (draw_title) {
208 sysarg_t cpt_x = ((widget->width - cpt_width) / 2) + widget->hpos;
209 sysarg_t cpt_y = ((header_height - cpt_height) / 2) +
210 widget->vpos + border_thickness;
211
212 if (widget->window->caption)
213 drawctx_print(&drawctx, widget->window->caption, cpt_x, cpt_y);
214 }
215
216 font_release(font);
217 window_yield(widget->window);
218}
219
220static void root_destroy(widget_t *widget)
221{
222 widget_deinit(widget);
223}
224
225static void root_reconfigure(widget_t *widget)
226{
227 if (widget->window->is_decorated) {
228 list_foreach(widget->children, link, widget_t, child) {
229 child->rearrange(child,
230 widget->hpos + border_thickness,
231 widget->vpos + border_thickness + header_height,
232 widget->width - 2 * border_thickness,
233 widget->height - 2 * border_thickness - header_height);
234 }
235 } else {
236 list_foreach(widget->children, link, widget_t, child) {
237 child->rearrange(child, widget->hpos, widget->vpos,
238 widget->width, widget->height);
239 }
240 }
241}
242
243static void root_rearrange(widget_t *widget, sysarg_t hpos, sysarg_t vpos,
244 sysarg_t width, sysarg_t height)
245{
246 widget_modify(widget, hpos, vpos, width, height);
247 if (widget->window->is_decorated) {
248 paint_internal(widget);
249 list_foreach(widget->children, link, widget_t, child) {
250 child->rearrange(child,
251 hpos + border_thickness,
252 vpos + border_thickness + header_height,
253 width - 2 * border_thickness,
254 height - 2 * border_thickness - header_height);
255 }
256 } else {
257 list_foreach(widget->children, link, widget_t, child) {
258 child->rearrange(child, hpos, vpos, width, height);
259 }
260 }
261}
262
263static void root_repaint(widget_t *widget)
264{
265 if (widget->window->is_decorated) {
266 paint_internal(widget);
267 }
268 list_foreach(widget->children, link, widget_t, child) {
269 child->repaint(child);
270 }
271 if (widget->window->is_decorated) {
272 window_damage(widget->window);
273 }
274}
275
276static void root_handle_keyboard_event(widget_t *widget, kbd_event_t event)
277{
278 if (!list_empty(&widget->children)) {
279 widget_t *child = (widget_t *) list_first(&widget->children);
280 child->handle_keyboard_event(child, event);
281 }
282}
283
284static void root_handle_position_event(widget_t *widget, pos_event_t event)
285{
286 gfx_coord2_t pos;
287
288 if (widget->window->is_decorated) {
289 sysarg_t width = widget->width;
290 sysarg_t height = widget->height;
291
292 bool btn_left = (event.btn_num == 1) && (event.type == POS_PRESS);
293
294 bool left = (event.hpos < border_thickness);
295 bool right = (event.hpos >= width - border_thickness);
296 bool top = (event.vpos < border_thickness);
297 bool bottom = (event.vpos >= height - border_thickness);
298 bool edge = left || right || top || bottom;
299
300 bool cleft = (event.hpos < corner_size);
301 bool cright = (event.hpos >= width - corner_size);
302 bool ctop = (event.vpos < corner_size);
303 bool cbottom = (event.vpos >= height - corner_size);
304
305 bool header = (event.hpos >= border_thickness) &&
306 (event.hpos < width - border_thickness) &&
307 (event.vpos >= border_thickness) &&
308 (event.vpos < border_thickness + header_height);
309 bool close = (header) &&
310 (event.hpos >= width - border_thickness - close_thickness);
311
312 bool isresize = widget->window->is_resizable;
313 display_wnd_rsztype_t rsztype = 0;
314
315 if (edge && ctop && cleft) {
316 rsztype = display_wr_top_left;
317 } else if (edge && cbottom && cleft) {
318 rsztype = display_wr_bottom_left;
319 } else if (edge && cbottom && cright) {
320 rsztype = display_wr_bottom_right;
321 } else if (edge && ctop && cright) {
322 rsztype = display_wr_top_right;
323 } else if (top) {
324 rsztype = display_wr_top;
325 } else if (left) {
326 rsztype = display_wr_left;
327 } else if (bottom) {
328 rsztype = display_wr_bottom;
329 } else if (right) {
330 rsztype = display_wr_right;
331 } else {
332 isresize = false;
333 }
334
335 if (isresize) {
336 (void) set_cursor(widget->window,
337 display_cursor_from_wrsz(rsztype));
338 } else {
339 (void) set_cursor(widget->window, dcurs_arrow);
340 }
341
342 pos.x = event.hpos;
343 pos.y = event.vpos;
344
345 if (isresize && btn_left) {
346 (void) display_window_resize_req(
347 widget->window->dwindow, rsztype, &pos);
348 } else if (close && btn_left) {
349 window_close(widget->window);
350 } else if (header && btn_left) {
351 (void) display_window_move_req(widget->window->dwindow,
352 &pos);
353 } else {
354 list_foreach(widget->children, link, widget_t, child) {
355 child->handle_position_event(child, event);
356 }
357 }
358 } else {
359 list_foreach(widget->children, link, widget_t, child) {
360 child->handle_position_event(child, event);
361 }
362 }
363}
364
365static void deliver_keyboard_event(window_t *win, kbd_event_t event)
366{
367 if (win->focus) {
368 win->focus->handle_keyboard_event(win->focus, event);
369 } else {
370 win->root.handle_keyboard_event(&win->root, event);
371 }
372}
373
374static void deliver_position_event(window_t *win, pos_event_t event)
375{
376 if (win->grab) {
377 win->grab->handle_position_event(win->grab, event);
378 } else {
379 win->root.handle_position_event(&win->root, event);
380 }
381}
382
383static void handle_signal_event(window_t *win, signal_event_t event)
384{
385 widget_t *widget = (widget_t *) event.object;
386 slot_t slot = (slot_t) event.slot;
387 void *data = (void *) event.argument;
388
389 slot(widget, data);
390
391 free(data);
392}
393
394static void handle_resize(window_t *win, sysarg_t offset_x, sysarg_t offset_y,
395 sysarg_t width, sysarg_t height, window_placement_flags_t placement_flags)
396{
397 gfx_bitmap_params_t params;
398 gfx_bitmap_alloc_t alloc;
399 gfx_bitmap_t *new_bitmap = NULL;
400 gfx_coord2_t offs;
401 gfx_coord2_t dpos;
402 display_info_t dinfo;
403 gfx_rect_t drect;
404 gfx_rect_t nrect;
405 errno_t rc;
406
407 if (width < 2 * border_thickness + header_min_width)
408 return;
409
410 if (height < 2 * border_thickness + header_height)
411 return;
412
413 fibril_mutex_lock(&win->guard);
414
415 /* Deallocate old bitmap. */
416 if (win->bitmap != NULL) {
417 gfx_bitmap_destroy(win->bitmap);
418 win->bitmap = NULL;
419 }
420
421 /* Deallocate old surface. */
422 if (win->surface != NULL) {
423 surface_destroy(win->surface);
424 win->surface = NULL;
425 }
426
427 /* Place window, if appropriate */
428 if (placement_flags != WINDOW_PLACEMENT_ANY) {
429 dpos.x = 0;
430 dpos.y = 0;
431
432 rc = display_get_info(win->display, &dinfo);
433 if (rc != EOK) {
434 fibril_mutex_unlock(&win->guard);
435 return;
436 }
437
438 drect = dinfo.rect;
439
440 if (placement_flags & WINDOW_PLACEMENT_LEFT)
441 dpos.x = drect.p0.x;
442 else if (placement_flags & WINDOW_PLACEMENT_CENTER_X)
443 dpos.x = (drect.p0.x + drect.p1.x - width) / 2;
444 else
445 dpos.x = drect.p1.x - width;
446
447 if (placement_flags & WINDOW_PLACEMENT_TOP)
448 dpos.y = drect.p0.y;
449 else if (placement_flags & WINDOW_PLACEMENT_CENTER_Y)
450 dpos.y = (drect.p0.y + drect.p1.y - height) / 2;
451 else
452 dpos.y = drect.p1.y - height;
453
454 (void) display_window_move(win->dwindow, &dpos);
455 }
456
457 /* Resize the display window. */
458 offs.x = offset_x;
459 offs.y = offset_y;
460 nrect.p0.x = 0;
461 nrect.p0.y = 0;
462 nrect.p1.x = width;
463 nrect.p1.y = height;
464
465 rc = display_window_resize(win->dwindow, &offs, &nrect);
466 if (rc != EOK) {
467 fibril_mutex_unlock(&win->guard);
468 return;
469 }
470
471 gfx_bitmap_params_init(&params);
472#ifndef CONFIG_WIN_DOUBLE_BUF
473 params.flags = bmpf_direct_output;
474#else
475 params.flags = 0;
476#endif
477 params.rect.p0.x = 0;
478 params.rect.p0.y = 0;
479 params.rect.p1.x = width;
480 params.rect.p1.y = height;
481
482 rc = gfx_bitmap_create(win->gc, &params, NULL, &new_bitmap);
483 if (rc != EOK) {
484 if (rc == ENOTSUP) {
485 /* Direct output is not supported */
486 params.flags &= ~bmpf_direct_output;
487 rc = gfx_bitmap_create(win->gc, &params, NULL, &new_bitmap);
488 if (rc != EOK) {
489 fibril_mutex_unlock(&win->guard);
490 return;
491 }
492 }
493 }
494
495 rc = gfx_bitmap_get_alloc(new_bitmap, &alloc);
496 if (rc != EOK) {
497 fibril_mutex_unlock(&win->guard);
498 return;
499 }
500
501 /* Allocate new surface. */
502 surface_t *new_surface = surface_create(width, height, alloc.pixels,
503 SURFACE_FLAG_SHARED);
504 if (!new_surface) {
505 gfx_bitmap_destroy(new_bitmap);
506 fibril_mutex_unlock(&win->guard);
507 return;
508 }
509
510 /* Switch in new surface and bitmap. */
511 win->surface = new_surface;
512 win->bitmap = new_bitmap;
513 fibril_mutex_unlock(&win->guard);
514
515 /*
516 * Let all widgets in the tree alter their position and size.
517 * Widgets might also paint themselves onto the new surface.
518 */
519 win->root.rearrange(&win->root, 0, 0, width, height);
520
521 fibril_mutex_lock(&win->guard);
522 surface_reset_damaged_region(win->surface);
523 fibril_mutex_unlock(&win->guard);
524
525 (void) gfx_bitmap_render(win->bitmap, NULL, NULL);
526}
527
528static void handle_refresh(window_t *win)
529{
530 win->root.repaint(&win->root);
531}
532
533static void handle_damage(window_t *win)
534{
535 sysarg_t x, y, width, height;
536 gfx_rect_t rect;
537 fibril_mutex_lock(&win->guard);
538 surface_get_damaged_region(win->surface, &x, &y, &width, &height);
539 surface_reset_damaged_region(win->surface);
540 fibril_mutex_unlock(&win->guard);
541
542 if (width > 0 && height > 0) {
543 rect.p0.x = x;
544 rect.p0.y = y;
545 rect.p1.x = x + width;
546 rect.p1.y = y + height;
547
548 if (win->bitmap != NULL)
549 (void) gfx_bitmap_render(win->bitmap, &rect, NULL);
550 }
551}
552
553static void destroy_children(widget_t *widget)
554{
555 /* Recursively destroy widget tree in bottom-top order. */
556 while (!list_empty(&widget->children)) {
557 widget_t *child =
558 list_get_instance(list_first(&widget->children), widget_t, link);
559 destroy_children(child);
560 child->destroy(child);
561 }
562}
563
564static void handle_close(window_t *win)
565{
566 destroy_children(&win->root);
567 win->root.destroy(&win->root);
568 win->grab = NULL;
569 win->focus = NULL;
570
571 gfx_bitmap_destroy(win->bitmap);
572
573 /*
574 * XXX Here we should properly destroy the IPC GC. We only have
575 * the generic GC so either it would need to be cast back or
576 * GC needs a virtual destructor.
577 */
578
579 display_window_destroy(win->dwindow);
580 display_close(win->display);
581
582 while (!list_empty(&win->events.list)) {
583 window_event_t *event = (window_event_t *) list_first(&win->events.list);
584 list_remove(&event->link);
585 free(event);
586 }
587
588 if (win->surface) {
589 surface_destroy(win->surface);
590 }
591
592 free(win->caption);
593
594 free(win);
595}
596
597/* Window event loop. Runs in own dedicated fibril. */
598static errno_t event_loop(void *arg)
599{
600 bool is_main = false;
601 bool terminate = false;
602 window_t *win = (window_t *) arg;
603
604 while (true) {
605 window_event_t *event = (window_event_t *) prodcons_consume(&win->events);
606
607 switch (event->type) {
608 case ET_KEYBOARD_EVENT:
609 deliver_keyboard_event(win, event->data.kbd);
610 break;
611 case ET_POSITION_EVENT:
612 deliver_position_event(win, event->data.pos);
613 break;
614 case ET_SIGNAL_EVENT:
615 handle_signal_event(win, event->data.signal);
616 break;
617 case ET_WINDOW_RESIZE:
618 handle_resize(win, event->data.resize.offset_x,
619 event->data.resize.offset_y, event->data.resize.width,
620 event->data.resize.height, event->data.resize.placement_flags);
621 break;
622 case ET_WINDOW_FOCUS:
623 if (!win->is_focused) {
624 win->is_focused = true;
625 handle_refresh(win);
626 }
627 break;
628 case ET_WINDOW_UNFOCUS:
629 if (win->is_focused) {
630 win->is_focused = false;
631 handle_refresh(win);
632 }
633 break;
634 case ET_WINDOW_REFRESH:
635 handle_refresh(win);
636 break;
637 case ET_WINDOW_DAMAGE:
638 handle_damage(win);
639 break;
640 case ET_WINDOW_CLOSE:
641 is_main = win->is_main;
642 handle_close(win);
643 terminate = true;
644 break;
645 default:
646 break;
647 }
648
649 free(event);
650 if (terminate) {
651 break;
652 }
653 }
654
655 if (is_main) {
656 exit(0); /* Terminate whole task. */
657 }
658 return 0;
659}
660
661window_t *window_open(const char *winreg, const void *data,
662 window_flags_t flags, const char *caption)
663{
664 display_wnd_params_t wparams;
665
666 window_t *win = (window_t *) calloc(1, sizeof(window_t));
667 if (!win)
668 return NULL;
669
670 win->is_main = flags & WINDOW_MAIN;
671 win->is_decorated = flags & WINDOW_DECORATED;
672 win->is_resizable = flags & WINDOW_RESIZEABLE;
673 win->is_focused = true;
674 prodcons_initialize(&win->events);
675 fibril_mutex_initialize(&win->guard);
676
677 widget_init(&win->root, NULL, data);
678 win->root.window = win;
679 win->root.destroy = root_destroy;
680 win->root.reconfigure = root_reconfigure;
681 win->root.rearrange = root_rearrange;
682 win->root.repaint = root_repaint;
683 win->root.handle_keyboard_event = root_handle_keyboard_event;
684 win->root.handle_position_event = root_handle_position_event;
685 win->grab = NULL;
686 win->focus = NULL;
687 win->cursor = dcurs_arrow;
688
689 /* Allocate resources for new surface. */
690 win->surface = surface_create(window_initial_size,
691 window_initial_size, NULL, SURFACE_FLAG_SHARED);
692 if (win->surface == NULL) {
693 free(win);
694 return NULL;
695 }
696
697 errno_t rc = display_open(winreg, &win->display);
698 if (rc != EOK) {
699 surface_destroy(win->surface);
700 free(win);
701 return NULL;
702 }
703
704 /* Window dimensions are not know at this time */
705 display_wnd_params_init(&wparams);
706 wparams.rect.p0.x = 0;
707 wparams.rect.p0.y = 0;
708 wparams.rect.p1.x = window_initial_size;
709 wparams.rect.p1.y = window_initial_size;
710 wparams.min_size.x = 2 * border_thickness + header_min_width;
711 wparams.min_size.y = 2 * border_thickness + header_height;
712
713 rc = display_window_create(win->display, &wparams, &window_cb,
714 (void *) win, &win->dwindow);
715 if (rc != EOK) {
716 display_close(win->display);
717 surface_destroy(win->surface);
718 free(win);
719 return NULL;
720 }
721
722 rc = display_window_get_gc(win->dwindow, &win->gc);
723 if (rc != EOK) {
724 display_window_destroy(win->dwindow);
725 display_close(win->display);
726 surface_destroy(win->surface);
727 free(win);
728 return NULL;
729 }
730
731 if (caption == NULL)
732 win->caption = NULL;
733 else
734 win->caption = str_dup(caption);
735
736 return win;
737}
738
739void window_resize(window_t *win, sysarg_t offset_x, sysarg_t offset_y,
740 sysarg_t width, sysarg_t height, window_placement_flags_t placement_flags)
741{
742 window_event_t *event = (window_event_t *) malloc(sizeof(window_event_t));
743 if (event) {
744 link_initialize(&event->link);
745 event->type = ET_WINDOW_RESIZE;
746 event->data.resize.offset_x = offset_x;
747 event->data.resize.offset_y = offset_y;
748 event->data.resize.width = width;
749 event->data.resize.height = height;
750 event->data.resize.placement_flags = placement_flags;
751 prodcons_produce(&win->events, &event->link);
752 }
753}
754
755errno_t window_set_caption(window_t *win, const char *caption)
756{
757 char *cap;
758
759 if (caption == NULL) {
760 win->caption = NULL;
761 } else {
762 cap = str_dup(caption);
763 if (cap == NULL)
764 return ENOMEM;
765 free(win->caption);
766 win->caption = cap;
767 }
768
769 win->is_focused = false;
770 handle_refresh(win);
771
772 return EOK;
773}
774
775void window_refresh(window_t *win)
776{
777 window_event_t *event = (window_event_t *) malloc(sizeof(window_event_t));
778 if (event) {
779 link_initialize(&event->link);
780 event->type = ET_WINDOW_REFRESH;
781 prodcons_produce(&win->events, &event->link);
782 }
783}
784
785void window_damage(window_t *win)
786{
787 window_event_t *event = (window_event_t *) malloc(sizeof(window_event_t));
788 if (event) {
789 link_initialize(&event->link);
790 event->type = ET_WINDOW_DAMAGE;
791 prodcons_produce(&win->events, &event->link);
792 }
793}
794
795widget_t *window_root(window_t *win)
796{
797 return &win->root;
798}
799
800void window_exec(window_t *win)
801{
802 fid_t ev_fid = fibril_create(event_loop, win);
803 if (!ev_fid) {
804 return;
805 }
806 fibril_add_ready(ev_fid);
807}
808
809surface_t *window_claim(window_t *win)
810{
811 fibril_mutex_lock(&win->guard);
812 return win->surface;
813}
814
815void window_yield(window_t *win)
816{
817 fibril_mutex_unlock(&win->guard);
818}
819
820void window_close(window_t *win)
821{
822 window_event_t *event;
823
824 event = (window_event_t *) calloc(1, sizeof(window_event_t));
825 if (event == NULL)
826 return;
827
828 link_initialize(&event->link);
829 event->type = ET_WINDOW_CLOSE;
830 prodcons_produce(&win->events, &event->link);
831}
832
833static void window_close_event(void *arg)
834{
835 window_t *win = (window_t *) arg;
836
837 window_close(win);
838}
839
840static void window_focus_event(void *arg)
841{
842 window_t *win = (window_t *) arg;
843 window_event_t *event;
844
845 event = (window_event_t *) calloc(1, sizeof(window_event_t));
846 if (event == NULL)
847 return;
848
849 link_initialize(&event->link);
850 event->type = ET_WINDOW_FOCUS;
851 prodcons_produce(&win->events, &event->link);
852}
853
854static void window_kbd_event(void *arg, kbd_event_t *kevent)
855{
856 window_t *win = (window_t *) arg;
857 window_event_t *event;
858
859 event = (window_event_t *) calloc(1, sizeof(window_event_t));
860 if (event == NULL)
861 return;
862
863 link_initialize(&event->link);
864 event->type = ET_KEYBOARD_EVENT;
865 event->data.kbd = *kevent;
866 prodcons_produce(&win->events, &event->link);
867}
868
869static void window_pos_event(void *arg, pos_event_t *pevent)
870{
871 window_t *win = (window_t *) arg;
872 window_event_t *event;
873
874 event = (window_event_t *) calloc(1, sizeof(window_event_t));
875 if (event == NULL)
876 return;
877
878 link_initialize(&event->link);
879 event->type = ET_POSITION_EVENT;
880 event->data.pos = *pevent;
881 prodcons_produce(&win->events, &event->link);
882}
883
884static void window_resize_event(void *arg, gfx_rect_t *nrect)
885{
886 window_t *win = (window_t *) arg;
887 window_event_t *event;
888
889 if (!win->is_resizable)
890 return;
891
892 event = (window_event_t *) calloc(1, sizeof(window_event_t));
893 if (event == NULL)
894 return;
895
896 link_initialize(&event->link);
897 event->type = ET_WINDOW_RESIZE;
898 event->data.resize.offset_x = nrect->p0.x;
899 event->data.resize.offset_y = nrect->p0.y;
900 event->data.resize.width = nrect->p1.x - nrect->p0.x;
901 event->data.resize.height = nrect->p1.y - nrect->p0.y;
902 event->data.resize.placement_flags = WINDOW_PLACEMENT_ANY;
903 prodcons_produce(&win->events, &event->link);
904}
905
906static void window_unfocus_event(void *arg)
907{
908 window_t *win = (window_t *) arg;
909 window_event_t *event;
910
911 event = (window_event_t *) calloc(1, sizeof(window_event_t));
912 if (event == NULL)
913 return;
914
915 link_initialize(&event->link);
916 event->type = ET_WINDOW_UNFOCUS;
917 prodcons_produce(&win->events, &event->link);
918}
919
920/** @}
921 */
Note: See TracBrowser for help on using the repository browser.