source: mainline/uspace/lib/gui/window.c@ 265989d

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

Deliver window focus and unfocus events

  • Property mode set to 100644
File size: 22.2 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;
69
70static pixel_t color_highlight = PIXEL(255, 255, 255, 255);
71static pixel_t color_shadow = PIXEL(255, 85, 85, 85);
72static pixel_t color_surface = PIXEL(255, 186, 186, 186);
73
74static pixel_t color_header_focus_highlight = PIXEL(255, 120, 145, 255);
75static pixel_t color_header_focus_shadow = PIXEL(255, 40, 48, 89);
76static pixel_t color_header_focus_surface = PIXEL(255, 88, 106, 196);
77
78static pixel_t color_header_unfocus_highlight = PIXEL(255, 16, 78, 126);
79static pixel_t color_header_unfocus_shadow = PIXEL(255, 5, 26, 42);
80static pixel_t color_header_unfocus_surface = PIXEL(255, 12, 57, 92);
81
82static pixel_t color_caption_focus = PIXEL(255, 255, 255, 255);
83static pixel_t color_caption_unfocus = PIXEL(255, 207, 207, 207);
84
85static void window_focus_event(void *);
86static void window_kbd_event(void *, kbd_event_t *);
87static void window_pos_event(void *, pos_event_t *);
88static void window_unfocus_event(void *);
89
90static display_wnd_cb_t window_cb = {
91 .focus_event = window_focus_event,
92 .kbd_event = window_kbd_event,
93 .pos_event = window_pos_event,
94 .unfocus_event = window_unfocus_event
95};
96
97static void paint_internal(widget_t *widget)
98{
99 surface_t *surface = window_claim(widget->window);
100 if (!surface)
101 window_yield(widget->window);
102
103 source_t source;
104 source_init(&source);
105
106 drawctx_t drawctx;
107 drawctx_init(&drawctx, surface);
108 drawctx_set_source(&drawctx, &source);
109
110 /* Window border outer bevel */
111
112 draw_bevel(&drawctx, &source, widget->vpos, widget->hpos,
113 widget->width, widget->height, color_highlight, color_shadow);
114
115 /* Window border surface */
116
117 source_set_color(&source, color_surface);
118 drawctx_transfer(&drawctx, widget->hpos + 1, widget->vpos + 1,
119 widget->width - 2, 2);
120 drawctx_transfer(&drawctx, widget->hpos + 1, widget->vpos + 1,
121 2, widget->height - 2);
122 drawctx_transfer(&drawctx, widget->hpos + 1,
123 widget->vpos + widget->height - 3, widget->width - 2, 2);
124 drawctx_transfer(&drawctx, widget->hpos + widget->width - 3,
125 widget->vpos + 1, 2, widget->height - 4);
126
127 /* Window border inner bevel */
128
129 draw_bevel(&drawctx, &source, widget->hpos + 3, widget->vpos + 3,
130 widget->width - 6, widget->height - 6, color_shadow,
131 color_highlight);
132
133 /* Header bevel */
134
135 sysarg_t header_hpos = widget->hpos + border_thickness;
136 sysarg_t header_vpos = widget->vpos + border_thickness;
137 sysarg_t header_width = widget->width - 2 * border_thickness -
138 close_thickness;
139
140 draw_bevel(&drawctx, &source, header_hpos, header_vpos,
141 header_width, header_height, widget->window->is_focused ?
142 color_header_focus_highlight : color_header_unfocus_highlight,
143 widget->window->is_focused ?
144 color_header_focus_shadow : color_header_unfocus_shadow);
145
146 /* Header surface */
147
148 source_set_color(&source, widget->window->is_focused ?
149 color_header_focus_surface : color_header_unfocus_surface);
150 drawctx_transfer(&drawctx, header_hpos + 1, header_vpos + 1,
151 header_width - 2, header_height - 2);
152
153 /* Close button bevel */
154
155 sysarg_t close_hpos = widget->hpos + widget->width -
156 border_thickness - close_thickness;
157 sysarg_t close_vpos = widget->vpos + border_thickness;
158
159 draw_bevel(&drawctx, &source, close_hpos, close_vpos,
160 close_thickness, close_thickness, color_highlight, color_shadow);
161
162 /* Close button surface */
163
164 source_set_color(&source, color_surface);
165 drawctx_transfer(&drawctx, close_hpos + 1, close_vpos + 1,
166 close_thickness - 2, close_thickness - 2);
167
168 /* Close button icon */
169
170 draw_icon_cross(surface, close_hpos + 3, close_vpos + 3,
171 color_highlight, color_shadow);
172
173 /* Window caption */
174
175 font_t *font;
176 errno_t rc = embedded_font_create(&font, 16);
177 if (rc != EOK) {
178 window_yield(widget->window);
179 return;
180 }
181
182 drawctx_set_font(&drawctx, font);
183 source_set_color(&source, widget->window->is_focused ?
184 color_caption_focus : color_caption_unfocus);
185
186 sysarg_t cpt_width;
187 sysarg_t cpt_height;
188 font_get_box(font, widget->window->caption, &cpt_width, &cpt_height);
189
190 bool draw_title =
191 (widget->width >= 2 * border_thickness + 2 * bevel_thickness +
192 close_thickness + cpt_width);
193 if (draw_title) {
194 sysarg_t cpt_x = ((widget->width - cpt_width) / 2) + widget->hpos;
195 sysarg_t cpt_y = ((header_height - cpt_height) / 2) +
196 widget->vpos + border_thickness;
197
198 if (widget->window->caption)
199 drawctx_print(&drawctx, widget->window->caption, cpt_x, cpt_y);
200 }
201
202 font_release(font);
203 window_yield(widget->window);
204}
205
206static void root_destroy(widget_t *widget)
207{
208 widget_deinit(widget);
209}
210
211static void root_reconfigure(widget_t *widget)
212{
213 if (widget->window->is_decorated) {
214 list_foreach(widget->children, link, widget_t, child) {
215 child->rearrange(child,
216 widget->hpos + border_thickness,
217 widget->vpos + border_thickness + header_height,
218 widget->width - 2 * border_thickness,
219 widget->height - 2 * border_thickness - header_height);
220 }
221 } else {
222 list_foreach(widget->children, link, widget_t, child) {
223 child->rearrange(child, widget->hpos, widget->vpos,
224 widget->width, widget->height);
225 }
226 }
227}
228
229static void root_rearrange(widget_t *widget, sysarg_t hpos, sysarg_t vpos,
230 sysarg_t width, sysarg_t height)
231{
232 widget_modify(widget, hpos, vpos, width, height);
233 if (widget->window->is_decorated) {
234 paint_internal(widget);
235 list_foreach(widget->children, link, widget_t, child) {
236 child->rearrange(child,
237 hpos + border_thickness,
238 vpos + border_thickness + header_height,
239 width - 2 * border_thickness,
240 height - 2 * border_thickness - header_height);
241 }
242 } else {
243 list_foreach(widget->children, link, widget_t, child) {
244 child->rearrange(child, hpos, vpos, width, height);
245 }
246 }
247}
248
249static void root_repaint(widget_t *widget)
250{
251 if (widget->window->is_decorated) {
252 paint_internal(widget);
253 }
254 list_foreach(widget->children, link, widget_t, child) {
255 child->repaint(child);
256 }
257 if (widget->window->is_decorated) {
258 window_damage(widget->window);
259 }
260}
261
262static void root_handle_keyboard_event(widget_t *widget, kbd_event_t event)
263{
264 if (!list_empty(&widget->children)) {
265 widget_t *child = (widget_t *) list_first(&widget->children);
266 child->handle_keyboard_event(child, event);
267 }
268}
269
270static void root_handle_position_event(widget_t *widget, pos_event_t event)
271{
272 if (widget->window->is_decorated) {
273 sysarg_t width = widget->width;
274 sysarg_t height = widget->height;
275
276 bool btn_left = (event.btn_num == 1) && (event.type == POS_PRESS);
277 bool btn_right = (event.btn_num == 2) && (event.type == POS_PRESS);
278 bool allowed_button = btn_left || btn_right;
279
280 bool left = (event.hpos < border_thickness);
281 bool right = (event.hpos >= width - border_thickness);
282 bool top = (event.vpos < border_thickness);
283 bool bottom = (event.vpos >= height - border_thickness);
284 bool header = (event.hpos >= border_thickness) &&
285 (event.hpos < width - border_thickness) &&
286 (event.vpos >= border_thickness) &&
287 (event.vpos < border_thickness + header_height);
288 bool close = (header) &&
289 (event.hpos >= width - border_thickness - close_thickness);
290
291 if (top && left && allowed_button) {
292 window_grab_flags_t flags = GF_EMPTY;
293 flags |= GF_MOVE_X;
294 flags |= GF_MOVE_Y;
295 flags |= btn_left ? GF_RESIZE_X : GF_SCALE_X;
296 flags |= btn_left ? GF_RESIZE_Y : GF_SCALE_Y;
297 //win_grab(widget->window->osess, event.pos_id, flags);
298 } else if (bottom && left && allowed_button) {
299 window_grab_flags_t flags = GF_EMPTY;
300 flags |= GF_MOVE_X;
301 flags |= btn_left ? GF_RESIZE_X : GF_SCALE_X;
302 flags |= btn_left ? GF_RESIZE_Y : GF_SCALE_Y;
303 //win_grab(widget->window->osess, event.pos_id, flags);
304 } else if (bottom && right && allowed_button) {
305 window_grab_flags_t flags = GF_EMPTY;
306 flags |= btn_left ? GF_RESIZE_X : GF_SCALE_X;
307 flags |= btn_left ? GF_RESIZE_Y : GF_SCALE_Y;
308 //win_grab(widget->window->osess, event.pos_id, flags);
309 } else if (top && right && allowed_button) {
310 window_grab_flags_t flags = GF_EMPTY;
311 flags |= GF_MOVE_Y;
312 flags |= btn_left ? GF_RESIZE_X : GF_SCALE_X;
313 flags |= btn_left ? GF_RESIZE_Y : GF_SCALE_Y;
314 //win_grab(widget->window->osess, event.pos_id, flags);
315 } else if (top && allowed_button) {
316 window_grab_flags_t flags = GF_EMPTY;
317 flags |= GF_MOVE_Y;
318 flags |= btn_left ? GF_RESIZE_Y : GF_SCALE_Y;
319 //win_grab(widget->window->osess, event.pos_id, flags);
320 } else if (left && allowed_button) {
321 window_grab_flags_t flags = GF_EMPTY;
322 flags |= GF_MOVE_X;
323 flags |= btn_left ? GF_RESIZE_X : GF_SCALE_X;
324 //win_grab(widget->window->osess, event.pos_id, flags);
325 } else if (bottom && allowed_button) {
326 window_grab_flags_t flags = GF_EMPTY;
327 flags |= btn_left ? GF_RESIZE_Y : GF_SCALE_Y;
328 //win_grab(widget->window->osess, event.pos_id, flags);
329 } else if (right && allowed_button) {
330 window_grab_flags_t flags = GF_EMPTY;
331 flags |= btn_left ? GF_RESIZE_X : GF_SCALE_X;
332 //win_grab(widget->window->osess, event.pos_id, flags);
333 } else if (close && btn_left) {
334 //win_close_request(widget->window->osess);
335 } else if (header && btn_left) {
336 window_grab_flags_t flags = GF_EMPTY;
337 flags |= GF_MOVE_X;
338 flags |= GF_MOVE_Y;
339 //win_grab(widget->window->osess, event.pos_id, flags);
340 } else {
341 list_foreach(widget->children, link, widget_t, child) {
342 child->handle_position_event(child, event);
343 }
344 }
345 } else {
346 list_foreach(widget->children, link, widget_t, child) {
347 child->handle_position_event(child, event);
348 }
349 }
350}
351
352static void deliver_keyboard_event(window_t *win, kbd_event_t event)
353{
354 if (win->focus) {
355 win->focus->handle_keyboard_event(win->focus, event);
356 } else {
357 win->root.handle_keyboard_event(&win->root, event);
358 }
359}
360
361static void deliver_position_event(window_t *win, pos_event_t event)
362{
363 if (win->grab) {
364 win->grab->handle_position_event(win->grab, event);
365 } else {
366 win->root.handle_position_event(&win->root, event);
367 }
368}
369
370static void handle_signal_event(window_t *win, signal_event_t event)
371{
372 widget_t *widget = (widget_t *) event.object;
373 slot_t slot = (slot_t) event.slot;
374 void *data = (void *) event.argument;
375
376 slot(widget, data);
377
378 free(data);
379}
380
381static void handle_resize(window_t *win, sysarg_t offset_x, sysarg_t offset_y,
382 sysarg_t width, sysarg_t height, window_placement_flags_t placement_flags)
383{
384 display_wnd_params_t wparams;
385 display_window_t *new_window = NULL;
386 gfx_bitmap_params_t params;
387 gfx_bitmap_alloc_t alloc;
388 gfx_bitmap_t *new_bitmap = NULL;
389 gfx_context_t *new_gc = NULL;
390 errno_t rc;
391
392 if (width < 2 * border_thickness + header_min_width) {
393 //win_damage(win->osess, 0, 0, 0, 0);
394 return;
395 }
396
397 if (height < 2 * border_thickness + header_height) {
398 //win_damage(win->osess, 0, 0, 0, 0);
399 return;
400 }
401
402 /* Allocate resources for new surface. */
403 surface_t *new_surface = surface_create(width, height, NULL,
404 SURFACE_FLAG_SHARED);
405 if (!new_surface)
406 return;
407
408 display_wnd_params_init(&wparams);
409 wparams.rect.p0.x = 0;
410 wparams.rect.p0.y = 0;
411 wparams.rect.p1.x = width;
412 wparams.rect.p1.y = height;
413
414 rc = display_window_create(win->display, &wparams, &window_cb,
415 (void *) win, &new_window);
416 if (rc != EOK) {
417 surface_destroy(new_surface);
418 return;
419 }
420
421 rc = display_window_get_gc(new_window, &new_gc);
422 if (rc != EOK) {
423 display_window_destroy(new_window);
424 surface_destroy(new_surface);
425 return;
426 }
427
428 params.rect.p0.x = 0;
429 params.rect.p0.y = 0;
430 params.rect.p1.x = width;
431 params.rect.p1.y = height;
432
433 alloc.pitch = width * sizeof(uint32_t);
434 alloc.off0 = 0;
435 alloc.pixels = surface_direct_access(new_surface);
436
437 rc = gfx_bitmap_create(new_gc, &params, &alloc, &new_bitmap);
438 if (rc != EOK) {
439 gfx_context_delete(new_gc);
440 display_window_destroy(new_window);
441 surface_destroy(new_surface);
442 return;
443 }
444
445 /* Switch new and old surface. */
446 fibril_mutex_lock(&win->guard);
447 surface_t *old_surface = win->surface;
448 gfx_bitmap_t *old_bitmap = win->bitmap;
449 display_window_t *old_window = win->dwindow;
450 gfx_context_t *old_gc = win->gc;
451 win->surface = new_surface;
452 win->bitmap = new_bitmap;
453 win->dwindow = new_window;
454 win->gc = new_gc;
455 fibril_mutex_unlock(&win->guard);
456
457 /*
458 * Let all widgets in the tree alter their position and size.
459 * Widgets might also paint themselves onto the new surface.
460 */
461 win->root.rearrange(&win->root, 0, 0, width, height);
462
463 fibril_mutex_lock(&win->guard);
464 surface_reset_damaged_region(win->surface);
465 fibril_mutex_unlock(&win->guard);
466
467 /* Inform compositor about new surface. */
468#if 0
469 errno_t rc = win_resize(win->osess, offset_x, offset_y, width, height,
470 placement_flags, surface_direct_access(new_surface));
471#endif
472 rc = EOK;
473
474 if (rc != EOK) {
475 /* Rollback to old surface. Reverse all changes. */
476
477 sysarg_t old_width = 0;
478 sysarg_t old_height = 0;
479 if (old_surface)
480 surface_get_resolution(old_surface, &old_width, &old_height);
481
482 fibril_mutex_lock(&win->guard);
483 new_surface = win->surface;
484 win->surface = old_surface;
485 win->bitmap = old_bitmap;
486 win->dwindow = old_window;
487 win->gc = old_gc;
488 fibril_mutex_unlock(&win->guard);
489
490 win->root.rearrange(&win->root, 0, 0, old_width, old_height);
491
492 if (win->surface) {
493 fibril_mutex_lock(&win->guard);
494 surface_reset_damaged_region(win->surface);
495 fibril_mutex_unlock(&win->guard);
496 }
497
498 surface_destroy(new_surface);
499 } else {
500 if (old_window != NULL)
501 display_window_destroy(old_window);
502 if (old_gc != NULL)
503 gfx_context_delete(old_gc);
504 if (old_bitmap != NULL)
505 gfx_bitmap_destroy(old_bitmap);
506 /* Deallocate old surface. */
507 if (old_surface)
508 surface_destroy(old_surface);
509
510 (void) gfx_bitmap_render(win->bitmap, NULL, NULL);
511 }
512}
513
514static void handle_refresh(window_t *win)
515{
516 win->root.repaint(&win->root);
517}
518
519static void handle_damage(window_t *win)
520{
521 sysarg_t x, y, width, height;
522 gfx_rect_t rect;
523 fibril_mutex_lock(&win->guard);
524 surface_get_damaged_region(win->surface, &x, &y, &width, &height);
525 surface_reset_damaged_region(win->surface);
526 fibril_mutex_unlock(&win->guard);
527
528 if (width > 0 && height > 0) {
529 /* Notify compositor. */
530 //win_damage(win->osess, x, y, width, height);
531
532 rect.p0.x = x;
533 rect.p0.y = y;
534 rect.p1.x = x + width;
535 rect.p1.y = y + height;
536
537 if (win->bitmap != NULL)
538 (void) gfx_bitmap_render(win->bitmap, &rect, NULL);
539 }
540}
541
542static void destroy_children(widget_t *widget)
543{
544 /* Recursively destroy widget tree in bottom-top order. */
545 while (!list_empty(&widget->children)) {
546 widget_t *child =
547 list_get_instance(list_first(&widget->children), widget_t, link);
548 destroy_children(child);
549 child->destroy(child);
550 }
551}
552
553static void handle_close(window_t *win)
554{
555 destroy_children(&win->root);
556 win->root.destroy(&win->root);
557 win->grab = NULL;
558 win->focus = NULL;
559
560 display_window_destroy(win->dwindow);
561 display_close(win->display);
562
563 while (!list_empty(&win->events.list)) {
564 window_event_t *event = (window_event_t *) list_first(&win->events.list);
565 list_remove(&event->link);
566 free(event);
567 }
568
569 if (win->surface) {
570 surface_destroy(win->surface);
571 }
572
573 free(win->caption);
574
575 free(win);
576}
577
578/* Window event loop. Runs in own dedicated fibril. */
579static errno_t event_loop(void *arg)
580{
581 bool is_main = false;
582 bool terminate = false;
583 window_t *win = (window_t *) arg;
584
585 while (true) {
586 window_event_t *event = (window_event_t *) prodcons_consume(&win->events);
587
588 switch (event->type) {
589 case ET_KEYBOARD_EVENT:
590 deliver_keyboard_event(win, event->data.kbd);
591 break;
592 case ET_POSITION_EVENT:
593 deliver_position_event(win, event->data.pos);
594 break;
595 case ET_SIGNAL_EVENT:
596 handle_signal_event(win, event->data.signal);
597 break;
598 case ET_WINDOW_RESIZE:
599 handle_resize(win, event->data.resize.offset_x,
600 event->data.resize.offset_y, event->data.resize.width,
601 event->data.resize.height, event->data.resize.placement_flags);
602 break;
603 case ET_WINDOW_FOCUS:
604 if (!win->is_focused) {
605 win->is_focused = true;
606 handle_refresh(win);
607 }
608 break;
609 case ET_WINDOW_UNFOCUS:
610 if (win->is_focused) {
611 win->is_focused = false;
612 handle_refresh(win);
613 }
614 break;
615 case ET_WINDOW_REFRESH:
616 handle_refresh(win);
617 break;
618 case ET_WINDOW_DAMAGE:
619 handle_damage(win);
620 break;
621 case ET_WINDOW_CLOSE:
622 is_main = win->is_main;
623 handle_close(win);
624 terminate = true;
625 break;
626 default:
627 break;
628 }
629
630 free(event);
631 if (terminate) {
632 break;
633 }
634 }
635
636 if (is_main) {
637 exit(0); /* Terminate whole task. */
638 }
639 return 0;
640}
641
642window_t *window_open(const char *winreg, const void *data,
643 window_flags_t flags, const char *caption)
644{
645 window_t *win = (window_t *) calloc(1, sizeof(window_t));
646 if (!win)
647 return NULL;
648
649 win->is_main = flags & WINDOW_MAIN;
650 win->is_decorated = flags & WINDOW_DECORATED;
651 win->is_focused = true;
652 prodcons_initialize(&win->events);
653 fibril_mutex_initialize(&win->guard);
654
655 widget_init(&win->root, NULL, data);
656 win->root.window = win;
657 win->root.destroy = root_destroy;
658 win->root.reconfigure = root_reconfigure;
659 win->root.rearrange = root_rearrange;
660 win->root.repaint = root_repaint;
661 win->root.handle_keyboard_event = root_handle_keyboard_event;
662 win->root.handle_position_event = root_handle_position_event;
663 win->grab = NULL;
664 win->focus = NULL;
665 win->surface = NULL;
666
667 errno_t rc = display_open(winreg, &win->display);
668 if (rc != EOK) {
669 free(win);
670 return NULL;
671 }
672
673 if (caption == NULL)
674 win->caption = NULL;
675 else
676 win->caption = str_dup(caption);
677
678 return win;
679}
680
681void window_resize(window_t *win, sysarg_t offset_x, sysarg_t offset_y,
682 sysarg_t width, sysarg_t height, window_placement_flags_t placement_flags)
683{
684 window_event_t *event = (window_event_t *) malloc(sizeof(window_event_t));
685 if (event) {
686 link_initialize(&event->link);
687 event->type = ET_WINDOW_RESIZE;
688 event->data.resize.offset_x = offset_x;
689 event->data.resize.offset_y = offset_y;
690 event->data.resize.width = width;
691 event->data.resize.height = height;
692 event->data.resize.placement_flags = placement_flags;
693 prodcons_produce(&win->events, &event->link);
694 }
695}
696
697errno_t window_set_caption(window_t *win, const char *caption)
698{
699 char *cap;
700
701 if (caption == NULL) {
702 win->caption = NULL;
703 } else {
704 cap = str_dup(caption);
705 if (cap == NULL)
706 return ENOMEM;
707 free(win->caption);
708 win->caption = cap;
709 }
710
711 win->is_focused = false;
712 handle_refresh(win);
713
714 return EOK;
715}
716
717void window_refresh(window_t *win)
718{
719 window_event_t *event = (window_event_t *) malloc(sizeof(window_event_t));
720 if (event) {
721 link_initialize(&event->link);
722 event->type = ET_WINDOW_REFRESH;
723 prodcons_produce(&win->events, &event->link);
724 }
725}
726
727void window_damage(window_t *win)
728{
729 window_event_t *event = (window_event_t *) malloc(sizeof(window_event_t));
730 if (event) {
731 link_initialize(&event->link);
732 event->type = ET_WINDOW_DAMAGE;
733 prodcons_produce(&win->events, &event->link);
734 }
735}
736
737widget_t *window_root(window_t *win)
738{
739 return &win->root;
740}
741
742void window_exec(window_t *win)
743{
744 fid_t ev_fid = fibril_create(event_loop, win);
745 if (!ev_fid) {
746 return;
747 }
748 fibril_add_ready(ev_fid);
749}
750
751surface_t *window_claim(window_t *win)
752{
753 fibril_mutex_lock(&win->guard);
754 return win->surface;
755}
756
757void window_yield(window_t *win)
758{
759 fibril_mutex_unlock(&win->guard);
760}
761
762void window_close(window_t *win)
763{
764 /* Request compositor to init closing cascade. */
765 //win_close_request(win->osess);
766}
767
768static void window_focus_event(void *arg)
769{
770 window_t *win = (window_t *) arg;
771 window_event_t *event;
772
773 event = (window_event_t *) calloc(1, sizeof(window_event_t));
774 if (event == NULL)
775 return;
776
777 link_initialize(&event->link);
778 event->type = ET_WINDOW_FOCUS;
779 prodcons_produce(&win->events, &event->link);
780}
781
782static void window_kbd_event(void *arg, kbd_event_t *kevent)
783{
784 window_t *win = (window_t *) arg;
785 window_event_t *event;
786
787 event = (window_event_t *) calloc(1, sizeof(window_event_t));
788 if (event == NULL)
789 return;
790
791 link_initialize(&event->link);
792 event->type = ET_KEYBOARD_EVENT;
793 event->data.kbd = *kevent;
794 prodcons_produce(&win->events, &event->link);
795}
796
797static void window_pos_event(void *arg, pos_event_t *pevent)
798{
799 window_t *win = (window_t *) arg;
800 window_event_t *event;
801
802 event = (window_event_t *) calloc(1, sizeof(window_event_t));
803 if (event == NULL)
804 return;
805
806 link_initialize(&event->link);
807 event->type = ET_POSITION_EVENT;
808 event->data.pos = *pevent;
809 prodcons_produce(&win->events, &event->link);
810}
811
812static void window_unfocus_event(void *arg)
813{
814 window_t *win = (window_t *) arg;
815 window_event_t *event;
816
817 event = (window_event_t *) calloc(1, sizeof(window_event_t));
818 if (event == NULL)
819 return;
820
821 link_initialize(&event->link);
822 event->type = ET_WINDOW_UNFOCUS;
823 prodcons_produce(&win->events, &event->link);
824}
825
826/** @}
827 */
Note: See TracBrowser for help on using the repository browser.