source: mainline/uspace/lib/gui/window.c@ 66a408f7

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

Do not resize non-resizable windows

  • Property mode set to 100644
File size: 23.5 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;
70
71static pixel_t color_highlight = PIXEL(255, 255, 255, 255);
72static pixel_t color_shadow = PIXEL(255, 85, 85, 85);
73static pixel_t color_surface = PIXEL(255, 186, 186, 186);
74
75static pixel_t color_header_focus_highlight = PIXEL(255, 120, 145, 255);
76static pixel_t color_header_focus_shadow = PIXEL(255, 40, 48, 89);
77static pixel_t color_header_focus_surface = PIXEL(255, 88, 106, 196);
78
79static pixel_t color_header_unfocus_highlight = PIXEL(255, 16, 78, 126);
80static pixel_t color_header_unfocus_shadow = PIXEL(255, 5, 26, 42);
81static pixel_t color_header_unfocus_surface = PIXEL(255, 12, 57, 92);
82
83static pixel_t color_caption_focus = PIXEL(255, 255, 255, 255);
84static pixel_t color_caption_unfocus = PIXEL(255, 207, 207, 207);
85
86static void window_close_event(void *);
87static void window_focus_event(void *);
88static void window_kbd_event(void *, kbd_event_t *);
89static void window_pos_event(void *, pos_event_t *);
90static void window_resize_event(void *, gfx_rect_t *);
91static void window_unfocus_event(void *);
92
93static display_wnd_cb_t window_cb = {
94 .close_event = window_close_event,
95 .focus_event = window_focus_event,
96 .kbd_event = window_kbd_event,
97 .pos_event = window_pos_event,
98 .resize_event = window_resize_event,
99 .unfocus_event = window_unfocus_event
100};
101
102static void set_cursor(window_t *window, display_stock_cursor_t cursor)
103{
104 if (cursor != window->cursor) {
105 (void) display_window_set_cursor(window->dwindow, cursor);
106 window->cursor = cursor;
107 }
108}
109
110static void paint_internal(widget_t *widget)
111{
112 surface_t *surface = window_claim(widget->window);
113 if (!surface)
114 window_yield(widget->window);
115
116 source_t source;
117 source_init(&source);
118
119 drawctx_t drawctx;
120 drawctx_init(&drawctx, surface);
121 drawctx_set_source(&drawctx, &source);
122
123 /* Window border outer bevel */
124
125 draw_bevel(&drawctx, &source, widget->vpos, widget->hpos,
126 widget->width, widget->height, color_highlight, color_shadow);
127
128 /* Window border surface */
129
130 source_set_color(&source, color_surface);
131 drawctx_transfer(&drawctx, widget->hpos + 1, widget->vpos + 1,
132 widget->width - 2, 2);
133 drawctx_transfer(&drawctx, widget->hpos + 1, widget->vpos + 1,
134 2, widget->height - 2);
135 drawctx_transfer(&drawctx, widget->hpos + 1,
136 widget->vpos + widget->height - 3, widget->width - 2, 2);
137 drawctx_transfer(&drawctx, widget->hpos + widget->width - 3,
138 widget->vpos + 1, 2, widget->height - 4);
139
140 /* Window border inner bevel */
141
142 draw_bevel(&drawctx, &source, widget->hpos + 3, widget->vpos + 3,
143 widget->width - 6, widget->height - 6, color_shadow,
144 color_highlight);
145
146 /* Header bevel */
147
148 sysarg_t header_hpos = widget->hpos + border_thickness;
149 sysarg_t header_vpos = widget->vpos + border_thickness;
150 sysarg_t header_width = widget->width - 2 * border_thickness -
151 close_thickness;
152
153 draw_bevel(&drawctx, &source, header_hpos, header_vpos,
154 header_width, header_height, widget->window->is_focused ?
155 color_header_focus_highlight : color_header_unfocus_highlight,
156 widget->window->is_focused ?
157 color_header_focus_shadow : color_header_unfocus_shadow);
158
159 /* Header surface */
160
161 source_set_color(&source, widget->window->is_focused ?
162 color_header_focus_surface : color_header_unfocus_surface);
163 drawctx_transfer(&drawctx, header_hpos + 1, header_vpos + 1,
164 header_width - 2, header_height - 2);
165
166 /* Close button bevel */
167
168 sysarg_t close_hpos = widget->hpos + widget->width -
169 border_thickness - close_thickness;
170 sysarg_t close_vpos = widget->vpos + border_thickness;
171
172 draw_bevel(&drawctx, &source, close_hpos, close_vpos,
173 close_thickness, close_thickness, color_highlight, color_shadow);
174
175 /* Close button surface */
176
177 source_set_color(&source, color_surface);
178 drawctx_transfer(&drawctx, close_hpos + 1, close_vpos + 1,
179 close_thickness - 2, close_thickness - 2);
180
181 /* Close button icon */
182
183 draw_icon_cross(surface, close_hpos + 3, close_vpos + 3,
184 color_highlight, color_shadow);
185
186 /* Window caption */
187
188 font_t *font;
189 errno_t rc = embedded_font_create(&font, 16);
190 if (rc != EOK) {
191 window_yield(widget->window);
192 return;
193 }
194
195 drawctx_set_font(&drawctx, font);
196 source_set_color(&source, widget->window->is_focused ?
197 color_caption_focus : color_caption_unfocus);
198
199 sysarg_t cpt_width;
200 sysarg_t cpt_height;
201 font_get_box(font, widget->window->caption, &cpt_width, &cpt_height);
202
203 bool draw_title =
204 (widget->width >= 2 * border_thickness + 2 * bevel_thickness +
205 close_thickness + cpt_width);
206 if (draw_title) {
207 sysarg_t cpt_x = ((widget->width - cpt_width) / 2) + widget->hpos;
208 sysarg_t cpt_y = ((header_height - cpt_height) / 2) +
209 widget->vpos + border_thickness;
210
211 if (widget->window->caption)
212 drawctx_print(&drawctx, widget->window->caption, cpt_x, cpt_y);
213 }
214
215 font_release(font);
216 window_yield(widget->window);
217}
218
219static void root_destroy(widget_t *widget)
220{
221 widget_deinit(widget);
222}
223
224static void root_reconfigure(widget_t *widget)
225{
226 if (widget->window->is_decorated) {
227 list_foreach(widget->children, link, widget_t, child) {
228 child->rearrange(child,
229 widget->hpos + border_thickness,
230 widget->vpos + border_thickness + header_height,
231 widget->width - 2 * border_thickness,
232 widget->height - 2 * border_thickness - header_height);
233 }
234 } else {
235 list_foreach(widget->children, link, widget_t, child) {
236 child->rearrange(child, widget->hpos, widget->vpos,
237 widget->width, widget->height);
238 }
239 }
240}
241
242static void root_rearrange(widget_t *widget, sysarg_t hpos, sysarg_t vpos,
243 sysarg_t width, sysarg_t height)
244{
245 widget_modify(widget, hpos, vpos, width, height);
246 if (widget->window->is_decorated) {
247 paint_internal(widget);
248 list_foreach(widget->children, link, widget_t, child) {
249 child->rearrange(child,
250 hpos + border_thickness,
251 vpos + border_thickness + header_height,
252 width - 2 * border_thickness,
253 height - 2 * border_thickness - header_height);
254 }
255 } else {
256 list_foreach(widget->children, link, widget_t, child) {
257 child->rearrange(child, hpos, vpos, width, height);
258 }
259 }
260}
261
262static void root_repaint(widget_t *widget)
263{
264 if (widget->window->is_decorated) {
265 paint_internal(widget);
266 }
267 list_foreach(widget->children, link, widget_t, child) {
268 child->repaint(child);
269 }
270 if (widget->window->is_decorated) {
271 window_damage(widget->window);
272 }
273}
274
275static void root_handle_keyboard_event(widget_t *widget, kbd_event_t event)
276{
277 if (!list_empty(&widget->children)) {
278 widget_t *child = (widget_t *) list_first(&widget->children);
279 child->handle_keyboard_event(child, event);
280 }
281}
282
283static void root_handle_position_event(widget_t *widget, pos_event_t event)
284{
285 gfx_coord2_t pos;
286
287 if (widget->window->is_decorated) {
288 sysarg_t width = widget->width;
289 sysarg_t height = widget->height;
290
291 bool btn_left = (event.btn_num == 1) && (event.type == POS_PRESS);
292
293 bool left = (event.hpos < border_thickness);
294 bool right = (event.hpos >= width - border_thickness);
295 bool top = (event.vpos < border_thickness);
296 bool bottom = (event.vpos >= height - border_thickness);
297 bool edge = left || right || top || bottom;
298
299 bool cleft = (event.hpos < corner_size);
300 bool cright = (event.hpos >= width - corner_size);
301 bool ctop = (event.vpos < corner_size);
302 bool cbottom = (event.vpos >= height - corner_size);
303
304 bool header = (event.hpos >= border_thickness) &&
305 (event.hpos < width - border_thickness) &&
306 (event.vpos >= border_thickness) &&
307 (event.vpos < border_thickness + header_height);
308 bool close = (header) &&
309 (event.hpos >= width - border_thickness - close_thickness);
310
311 bool isresize = widget->window->is_resizable;
312 display_wnd_rsztype_t rsztype = 0;
313
314 if (edge && ctop && cleft) {
315 rsztype = display_wr_top_left;
316 } else if (edge && cbottom && cleft) {
317 rsztype = display_wr_bottom_left;
318 } else if (edge && cbottom && cright) {
319 rsztype = display_wr_bottom_right;
320 } else if (edge && ctop && cright) {
321 rsztype = display_wr_top_right;
322 } else if (top) {
323 rsztype = display_wr_top;
324 } else if (left) {
325 rsztype = display_wr_left;
326 } else if (bottom) {
327 rsztype = display_wr_bottom;
328 } else if (right) {
329 rsztype = display_wr_right;
330 } else {
331 isresize = false;
332 }
333
334 if (isresize) {
335 (void) set_cursor(widget->window,
336 display_cursor_from_wrsz(rsztype));
337 } else {
338 (void) set_cursor(widget->window, dcurs_arrow);
339 }
340
341 pos.x = event.hpos;
342 pos.y = event.vpos;
343
344 if (isresize && btn_left) {
345 (void) display_window_resize_req(
346 widget->window->dwindow, rsztype, &pos);
347 } else if (close && btn_left) {
348 window_close(widget->window);
349 } else if (header && btn_left) {
350 (void) display_window_move_req(widget->window->dwindow,
351 &pos);
352 } else {
353 list_foreach(widget->children, link, widget_t, child) {
354 child->handle_position_event(child, event);
355 }
356 }
357 } else {
358 list_foreach(widget->children, link, widget_t, child) {
359 child->handle_position_event(child, event);
360 }
361 }
362}
363
364static void deliver_keyboard_event(window_t *win, kbd_event_t event)
365{
366 if (win->focus) {
367 win->focus->handle_keyboard_event(win->focus, event);
368 } else {
369 win->root.handle_keyboard_event(&win->root, event);
370 }
371}
372
373static void deliver_position_event(window_t *win, pos_event_t event)
374{
375 if (win->grab) {
376 win->grab->handle_position_event(win->grab, event);
377 } else {
378 win->root.handle_position_event(&win->root, event);
379 }
380}
381
382static void handle_signal_event(window_t *win, signal_event_t event)
383{
384 widget_t *widget = (widget_t *) event.object;
385 slot_t slot = (slot_t) event.slot;
386 void *data = (void *) event.argument;
387
388 slot(widget, data);
389
390 free(data);
391}
392
393static void handle_resize(window_t *win, sysarg_t offset_x, sysarg_t offset_y,
394 sysarg_t width, sysarg_t height, window_placement_flags_t placement_flags)
395{
396 gfx_bitmap_params_t params;
397 gfx_bitmap_alloc_t alloc;
398 gfx_bitmap_t *new_bitmap = NULL;
399 gfx_coord2_t offs;
400 gfx_coord2_t dpos;
401 display_info_t dinfo;
402 gfx_rect_t drect;
403 gfx_rect_t nrect;
404 errno_t rc;
405
406 if (width < 2 * border_thickness + header_min_width) {
407 //win_damage(win->osess, 0, 0, 0, 0);
408 return;
409 }
410
411 if (height < 2 * border_thickness + header_height) {
412 //win_damage(win->osess, 0, 0, 0, 0);
413 return;
414 }
415
416 fibril_mutex_lock(&win->guard);
417
418 /* Deallocate old bitmap. */
419 if (win->bitmap != NULL) {
420 gfx_bitmap_destroy(win->bitmap);
421 win->bitmap = NULL;
422 }
423
424 /* Deallocate old surface. */
425 if (win->surface != NULL) {
426 surface_destroy(win->surface);
427 win->surface = NULL;
428 }
429
430 /* Resize the display window. */
431 offs.x = offset_x;
432 offs.y = offset_y;
433 nrect.p0.x = 0;
434 nrect.p0.y = 0;
435 nrect.p1.x = width;
436 nrect.p1.y = height;
437
438 rc = display_window_resize(win->dwindow, &offs, &nrect);
439 if (rc != EOK)
440 return;
441
442 gfx_bitmap_params_init(&params);
443#ifndef CONFIG_WIN_DOUBLE_BUF
444 params.flags = bmpf_direct_output;
445#else
446 params.flags = 0;
447#endif
448 params.rect.p0.x = 0;
449 params.rect.p0.y = 0;
450 params.rect.p1.x = width;
451 params.rect.p1.y = height;
452
453 rc = gfx_bitmap_create(win->gc, &params, NULL, &new_bitmap);
454 if (rc != EOK) {
455 if (rc == ENOTSUP) {
456 /* Direct output is not supported */
457 params.flags &= ~bmpf_direct_output;
458 rc = gfx_bitmap_create(win->gc, &params, NULL, &new_bitmap);
459 if (rc != EOK) {
460 fibril_mutex_unlock(&win->guard);
461 return;
462 }
463 }
464 }
465
466 rc = gfx_bitmap_get_alloc(new_bitmap, &alloc);
467 if (rc != EOK) {
468 fibril_mutex_unlock(&win->guard);
469 return;
470 }
471
472 /* Allocate new surface. */
473 surface_t *new_surface = surface_create(width, height, alloc.pixels,
474 SURFACE_FLAG_SHARED);
475 if (!new_surface) {
476 gfx_bitmap_destroy(new_bitmap);
477 fibril_mutex_unlock(&win->guard);
478 return;
479 }
480
481 /* Switch in new surface and bitmap. */
482 win->surface = new_surface;
483 win->bitmap = new_bitmap;
484 fibril_mutex_unlock(&win->guard);
485
486 /*
487 * Let all widgets in the tree alter their position and size.
488 * Widgets might also paint themselves onto the new surface.
489 */
490 win->root.rearrange(&win->root, 0, 0, width, height);
491
492 fibril_mutex_lock(&win->guard);
493 surface_reset_damaged_region(win->surface);
494 fibril_mutex_unlock(&win->guard);
495
496 if (placement_flags != WINDOW_PLACEMENT_ANY) {
497 dpos.x = 0;
498 dpos.y = 0;
499
500 rc = display_get_info(win->display, &dinfo);
501 if (rc != EOK) {
502 (void) gfx_bitmap_render(win->bitmap, NULL, NULL);
503 return;
504 }
505
506 drect = dinfo.rect;
507
508 if (placement_flags & WINDOW_PLACEMENT_LEFT)
509 dpos.x = drect.p0.x;
510 else if (placement_flags & WINDOW_PLACEMENT_CENTER_X)
511 dpos.x = (drect.p0.x + drect.p1.x - width) / 2;
512 else
513 dpos.x = drect.p1.x - width;
514
515 if (placement_flags & WINDOW_PLACEMENT_TOP)
516 dpos.y = drect.p0.y;
517 else if (placement_flags & WINDOW_PLACEMENT_CENTER_Y)
518 dpos.y = (drect.p0.y + drect.p1.y - height) / 2;
519 else
520 dpos.y = drect.p1.y - height;
521
522 (void) display_window_move(win->dwindow, &dpos);
523 }
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(100, 100, NULL, SURFACE_FLAG_SHARED);
691 if (win->surface == NULL) {
692 free(win);
693 return NULL;
694 }
695
696 errno_t rc = display_open(winreg, &win->display);
697 if (rc != EOK) {
698 surface_destroy(win->surface);
699 free(win);
700 return NULL;
701 }
702
703 /* Window dimensions are not know at this time */
704 display_wnd_params_init(&wparams);
705 wparams.rect.p0.x = 0;
706 wparams.rect.p0.y = 0;
707 wparams.rect.p1.x = 100;
708 wparams.rect.p1.y = 100;
709 wparams.min_size.x = 2 * border_thickness + header_min_width;
710 wparams.min_size.y = 2 * border_thickness + header_height;
711
712 rc = display_window_create(win->display, &wparams, &window_cb,
713 (void *) win, &win->dwindow);
714 if (rc != EOK) {
715 display_close(win->display);
716 surface_destroy(win->surface);
717 free(win);
718 return NULL;
719 }
720
721 rc = display_window_get_gc(win->dwindow, &win->gc);
722 if (rc != EOK) {
723 display_window_destroy(win->dwindow);
724 display_close(win->display);
725 surface_destroy(win->surface);
726 free(win);
727 return NULL;
728 }
729
730 if (caption == NULL)
731 win->caption = NULL;
732 else
733 win->caption = str_dup(caption);
734
735 return win;
736}
737
738void window_resize(window_t *win, sysarg_t offset_x, sysarg_t offset_y,
739 sysarg_t width, sysarg_t height, window_placement_flags_t placement_flags)
740{
741 window_event_t *event = (window_event_t *) malloc(sizeof(window_event_t));
742 if (event) {
743 link_initialize(&event->link);
744 event->type = ET_WINDOW_RESIZE;
745 event->data.resize.offset_x = offset_x;
746 event->data.resize.offset_y = offset_y;
747 event->data.resize.width = width;
748 event->data.resize.height = height;
749 event->data.resize.placement_flags = placement_flags;
750 prodcons_produce(&win->events, &event->link);
751 }
752}
753
754errno_t window_set_caption(window_t *win, const char *caption)
755{
756 char *cap;
757
758 if (caption == NULL) {
759 win->caption = NULL;
760 } else {
761 cap = str_dup(caption);
762 if (cap == NULL)
763 return ENOMEM;
764 free(win->caption);
765 win->caption = cap;
766 }
767
768 win->is_focused = false;
769 handle_refresh(win);
770
771 return EOK;
772}
773
774void window_refresh(window_t *win)
775{
776 window_event_t *event = (window_event_t *) malloc(sizeof(window_event_t));
777 if (event) {
778 link_initialize(&event->link);
779 event->type = ET_WINDOW_REFRESH;
780 prodcons_produce(&win->events, &event->link);
781 }
782}
783
784void window_damage(window_t *win)
785{
786 window_event_t *event = (window_event_t *) malloc(sizeof(window_event_t));
787 if (event) {
788 link_initialize(&event->link);
789 event->type = ET_WINDOW_DAMAGE;
790 prodcons_produce(&win->events, &event->link);
791 }
792}
793
794widget_t *window_root(window_t *win)
795{
796 return &win->root;
797}
798
799void window_exec(window_t *win)
800{
801 fid_t ev_fid = fibril_create(event_loop, win);
802 if (!ev_fid) {
803 return;
804 }
805 fibril_add_ready(ev_fid);
806}
807
808surface_t *window_claim(window_t *win)
809{
810 fibril_mutex_lock(&win->guard);
811 return win->surface;
812}
813
814void window_yield(window_t *win)
815{
816 fibril_mutex_unlock(&win->guard);
817}
818
819void window_close(window_t *win)
820{
821 window_event_t *event;
822
823 event = (window_event_t *) calloc(1, sizeof(window_event_t));
824 if (event == NULL)
825 return;
826
827 link_initialize(&event->link);
828 event->type = ET_WINDOW_CLOSE;
829 prodcons_produce(&win->events, &event->link);
830}
831
832static void window_close_event(void *arg)
833{
834 window_t *win = (window_t *) arg;
835
836 window_close(win);
837}
838
839static void window_focus_event(void *arg)
840{
841 window_t *win = (window_t *) arg;
842 window_event_t *event;
843
844 event = (window_event_t *) calloc(1, sizeof(window_event_t));
845 if (event == NULL)
846 return;
847
848 link_initialize(&event->link);
849 event->type = ET_WINDOW_FOCUS;
850 prodcons_produce(&win->events, &event->link);
851}
852
853static void window_kbd_event(void *arg, kbd_event_t *kevent)
854{
855 window_t *win = (window_t *) arg;
856 window_event_t *event;
857
858 event = (window_event_t *) calloc(1, sizeof(window_event_t));
859 if (event == NULL)
860 return;
861
862 link_initialize(&event->link);
863 event->type = ET_KEYBOARD_EVENT;
864 event->data.kbd = *kevent;
865 prodcons_produce(&win->events, &event->link);
866}
867
868static void window_pos_event(void *arg, pos_event_t *pevent)
869{
870 window_t *win = (window_t *) arg;
871 window_event_t *event;
872
873 event = (window_event_t *) calloc(1, sizeof(window_event_t));
874 if (event == NULL)
875 return;
876
877 link_initialize(&event->link);
878 event->type = ET_POSITION_EVENT;
879 event->data.pos = *pevent;
880 prodcons_produce(&win->events, &event->link);
881}
882
883static void window_resize_event(void *arg, gfx_rect_t *nrect)
884{
885 window_t *win = (window_t *) arg;
886 window_event_t *event;
887
888 if (!win->is_resizable)
889 return;
890
891 event = (window_event_t *) calloc(1, sizeof(window_event_t));
892 if (event == NULL)
893 return;
894
895 link_initialize(&event->link);
896 event->type = ET_WINDOW_RESIZE;
897 event->data.resize.offset_x = nrect->p0.x;
898 event->data.resize.offset_y = nrect->p0.y;
899 event->data.resize.width = nrect->p1.x - nrect->p0.x;
900 event->data.resize.height = nrect->p1.y - nrect->p0.y;
901 event->data.resize.placement_flags = WINDOW_PLACEMENT_ANY;
902 prodcons_produce(&win->events, &event->link);
903}
904
905static void window_unfocus_event(void *arg)
906{
907 window_t *win = (window_t *) arg;
908 window_event_t *event;
909
910 event = (window_event_t *) calloc(1, sizeof(window_event_t));
911 if (event == NULL)
912 return;
913
914 link_initialize(&event->link);
915 event->type = ET_WINDOW_UNFOCUS;
916 prodcons_produce(&win->events, &event->link);
917}
918
919/** @}
920 */
Note: See TracBrowser for help on using the repository browser.