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