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_focus_event(void *);
|
---|
86 | static void window_kbd_event(void *, kbd_event_t *);
|
---|
87 | static void window_pos_event(void *, pos_event_t *);
|
---|
88 | static void window_unfocus_event(void *);
|
---|
89 |
|
---|
90 | static 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 |
|
---|
97 | static 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 |
|
---|
206 | static void root_destroy(widget_t *widget)
|
---|
207 | {
|
---|
208 | widget_deinit(widget);
|
---|
209 | }
|
---|
210 |
|
---|
211 | static 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 |
|
---|
229 | static 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 |
|
---|
249 | static 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 |
|
---|
262 | static 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 |
|
---|
270 | static 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 |
|
---|
352 | static 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 |
|
---|
361 | static 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 |
|
---|
370 | static 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 |
|
---|
381 | static 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 | gfx_bitmap_params_t params;
|
---|
385 | gfx_bitmap_alloc_t alloc;
|
---|
386 | gfx_bitmap_t *new_bitmap = NULL;
|
---|
387 | gfx_coord2_t offs;
|
---|
388 | gfx_rect_t nrect;
|
---|
389 | errno_t rc;
|
---|
390 |
|
---|
391 | if (width < 2 * border_thickness + header_min_width) {
|
---|
392 | //win_damage(win->osess, 0, 0, 0, 0);
|
---|
393 | return;
|
---|
394 | }
|
---|
395 |
|
---|
396 | if (height < 2 * border_thickness + header_height) {
|
---|
397 | //win_damage(win->osess, 0, 0, 0, 0);
|
---|
398 | return;
|
---|
399 | }
|
---|
400 |
|
---|
401 | /* Allocate resources for new surface. */
|
---|
402 | surface_t *new_surface = surface_create(width, height, NULL,
|
---|
403 | SURFACE_FLAG_SHARED);
|
---|
404 | if (!new_surface)
|
---|
405 | return;
|
---|
406 |
|
---|
407 | gfx_bitmap_params_init(¶ms);
|
---|
408 | params.rect.p0.x = 0;
|
---|
409 | params.rect.p0.y = 0;
|
---|
410 | params.rect.p1.x = width;
|
---|
411 | params.rect.p1.y = height;
|
---|
412 |
|
---|
413 | alloc.pitch = width * sizeof(uint32_t);
|
---|
414 | alloc.off0 = 0;
|
---|
415 | alloc.pixels = surface_direct_access(new_surface);
|
---|
416 |
|
---|
417 | rc = gfx_bitmap_create(win->gc, ¶ms, &alloc, &new_bitmap);
|
---|
418 | if (rc != EOK) {
|
---|
419 | surface_destroy(new_surface);
|
---|
420 | return;
|
---|
421 | }
|
---|
422 |
|
---|
423 | /* Switch new and old surface. */
|
---|
424 | fibril_mutex_lock(&win->guard);
|
---|
425 | surface_t *old_surface = win->surface;
|
---|
426 | gfx_bitmap_t *old_bitmap = win->bitmap;
|
---|
427 | win->surface = new_surface;
|
---|
428 | win->bitmap = new_bitmap;
|
---|
429 | fibril_mutex_unlock(&win->guard);
|
---|
430 |
|
---|
431 | /*
|
---|
432 | * Let all widgets in the tree alter their position and size.
|
---|
433 | * Widgets might also paint themselves onto the new surface.
|
---|
434 | */
|
---|
435 | win->root.rearrange(&win->root, 0, 0, width, height);
|
---|
436 |
|
---|
437 | fibril_mutex_lock(&win->guard);
|
---|
438 | surface_reset_damaged_region(win->surface);
|
---|
439 | fibril_mutex_unlock(&win->guard);
|
---|
440 |
|
---|
441 | /* Resize the display window. */
|
---|
442 | offs.x = offset_x;
|
---|
443 | offs.y = offset_y;
|
---|
444 | nrect.p0.x = 0;
|
---|
445 | nrect.p0.y = 0;
|
---|
446 | nrect.p1.x = width;
|
---|
447 | nrect.p1.y = height;
|
---|
448 |
|
---|
449 | rc = display_window_resize(win->dwindow, &offs, &nrect);
|
---|
450 | if (rc != EOK) {
|
---|
451 | /* Rollback to old surface. Reverse all changes. */
|
---|
452 |
|
---|
453 | sysarg_t old_width = 0;
|
---|
454 | sysarg_t old_height = 0;
|
---|
455 | if (old_surface)
|
---|
456 | surface_get_resolution(old_surface, &old_width, &old_height);
|
---|
457 |
|
---|
458 | fibril_mutex_lock(&win->guard);
|
---|
459 | new_surface = win->surface;
|
---|
460 | win->surface = old_surface;
|
---|
461 | win->bitmap = old_bitmap;
|
---|
462 | fibril_mutex_unlock(&win->guard);
|
---|
463 |
|
---|
464 | win->root.rearrange(&win->root, 0, 0, old_width, old_height);
|
---|
465 |
|
---|
466 | if (win->surface) {
|
---|
467 | fibril_mutex_lock(&win->guard);
|
---|
468 | surface_reset_damaged_region(win->surface);
|
---|
469 | fibril_mutex_unlock(&win->guard);
|
---|
470 | }
|
---|
471 |
|
---|
472 | surface_destroy(new_surface);
|
---|
473 | } else {
|
---|
474 | if (old_bitmap != NULL)
|
---|
475 | gfx_bitmap_destroy(old_bitmap);
|
---|
476 | /* Deallocate old surface. */
|
---|
477 | if (old_surface)
|
---|
478 | surface_destroy(old_surface);
|
---|
479 |
|
---|
480 | (void) gfx_bitmap_render(win->bitmap, NULL, NULL);
|
---|
481 | }
|
---|
482 | }
|
---|
483 |
|
---|
484 | static void handle_refresh(window_t *win)
|
---|
485 | {
|
---|
486 | win->root.repaint(&win->root);
|
---|
487 | }
|
---|
488 |
|
---|
489 | static void handle_damage(window_t *win)
|
---|
490 | {
|
---|
491 | sysarg_t x, y, width, height;
|
---|
492 | gfx_rect_t rect;
|
---|
493 | fibril_mutex_lock(&win->guard);
|
---|
494 | surface_get_damaged_region(win->surface, &x, &y, &width, &height);
|
---|
495 | surface_reset_damaged_region(win->surface);
|
---|
496 | fibril_mutex_unlock(&win->guard);
|
---|
497 |
|
---|
498 | if (width > 0 && height > 0) {
|
---|
499 | /* Notify compositor. */
|
---|
500 | //win_damage(win->osess, x, y, width, height);
|
---|
501 |
|
---|
502 | rect.p0.x = x;
|
---|
503 | rect.p0.y = y;
|
---|
504 | rect.p1.x = x + width;
|
---|
505 | rect.p1.y = y + height;
|
---|
506 |
|
---|
507 | if (win->bitmap != NULL)
|
---|
508 | (void) gfx_bitmap_render(win->bitmap, &rect, NULL);
|
---|
509 | }
|
---|
510 | }
|
---|
511 |
|
---|
512 | static void destroy_children(widget_t *widget)
|
---|
513 | {
|
---|
514 | /* Recursively destroy widget tree in bottom-top order. */
|
---|
515 | while (!list_empty(&widget->children)) {
|
---|
516 | widget_t *child =
|
---|
517 | list_get_instance(list_first(&widget->children), widget_t, link);
|
---|
518 | destroy_children(child);
|
---|
519 | child->destroy(child);
|
---|
520 | }
|
---|
521 | }
|
---|
522 |
|
---|
523 | static void handle_close(window_t *win)
|
---|
524 | {
|
---|
525 | destroy_children(&win->root);
|
---|
526 | win->root.destroy(&win->root);
|
---|
527 | win->grab = NULL;
|
---|
528 | win->focus = NULL;
|
---|
529 |
|
---|
530 | display_window_destroy(win->dwindow);
|
---|
531 | display_close(win->display);
|
---|
532 |
|
---|
533 | while (!list_empty(&win->events.list)) {
|
---|
534 | window_event_t *event = (window_event_t *) list_first(&win->events.list);
|
---|
535 | list_remove(&event->link);
|
---|
536 | free(event);
|
---|
537 | }
|
---|
538 |
|
---|
539 | if (win->surface) {
|
---|
540 | surface_destroy(win->surface);
|
---|
541 | }
|
---|
542 |
|
---|
543 | free(win->caption);
|
---|
544 |
|
---|
545 | free(win);
|
---|
546 | }
|
---|
547 |
|
---|
548 | /* Window event loop. Runs in own dedicated fibril. */
|
---|
549 | static errno_t event_loop(void *arg)
|
---|
550 | {
|
---|
551 | bool is_main = false;
|
---|
552 | bool terminate = false;
|
---|
553 | window_t *win = (window_t *) arg;
|
---|
554 |
|
---|
555 | while (true) {
|
---|
556 | window_event_t *event = (window_event_t *) prodcons_consume(&win->events);
|
---|
557 |
|
---|
558 | switch (event->type) {
|
---|
559 | case ET_KEYBOARD_EVENT:
|
---|
560 | deliver_keyboard_event(win, event->data.kbd);
|
---|
561 | break;
|
---|
562 | case ET_POSITION_EVENT:
|
---|
563 | deliver_position_event(win, event->data.pos);
|
---|
564 | break;
|
---|
565 | case ET_SIGNAL_EVENT:
|
---|
566 | handle_signal_event(win, event->data.signal);
|
---|
567 | break;
|
---|
568 | case ET_WINDOW_RESIZE:
|
---|
569 | handle_resize(win, event->data.resize.offset_x,
|
---|
570 | event->data.resize.offset_y, event->data.resize.width,
|
---|
571 | event->data.resize.height, event->data.resize.placement_flags);
|
---|
572 | break;
|
---|
573 | case ET_WINDOW_FOCUS:
|
---|
574 | if (!win->is_focused) {
|
---|
575 | win->is_focused = true;
|
---|
576 | handle_refresh(win);
|
---|
577 | }
|
---|
578 | break;
|
---|
579 | case ET_WINDOW_UNFOCUS:
|
---|
580 | if (win->is_focused) {
|
---|
581 | win->is_focused = false;
|
---|
582 | handle_refresh(win);
|
---|
583 | }
|
---|
584 | break;
|
---|
585 | case ET_WINDOW_REFRESH:
|
---|
586 | handle_refresh(win);
|
---|
587 | break;
|
---|
588 | case ET_WINDOW_DAMAGE:
|
---|
589 | handle_damage(win);
|
---|
590 | break;
|
---|
591 | case ET_WINDOW_CLOSE:
|
---|
592 | is_main = win->is_main;
|
---|
593 | handle_close(win);
|
---|
594 | terminate = true;
|
---|
595 | break;
|
---|
596 | default:
|
---|
597 | break;
|
---|
598 | }
|
---|
599 |
|
---|
600 | free(event);
|
---|
601 | if (terminate) {
|
---|
602 | break;
|
---|
603 | }
|
---|
604 | }
|
---|
605 |
|
---|
606 | if (is_main) {
|
---|
607 | exit(0); /* Terminate whole task. */
|
---|
608 | }
|
---|
609 | return 0;
|
---|
610 | }
|
---|
611 |
|
---|
612 | window_t *window_open(const char *winreg, const void *data,
|
---|
613 | window_flags_t flags, const char *caption)
|
---|
614 | {
|
---|
615 | display_wnd_params_t wparams;
|
---|
616 |
|
---|
617 | window_t *win = (window_t *) calloc(1, sizeof(window_t));
|
---|
618 | if (!win)
|
---|
619 | return NULL;
|
---|
620 |
|
---|
621 | win->is_main = flags & WINDOW_MAIN;
|
---|
622 | win->is_decorated = flags & WINDOW_DECORATED;
|
---|
623 | win->is_focused = true;
|
---|
624 | prodcons_initialize(&win->events);
|
---|
625 | fibril_mutex_initialize(&win->guard);
|
---|
626 |
|
---|
627 | widget_init(&win->root, NULL, data);
|
---|
628 | win->root.window = win;
|
---|
629 | win->root.destroy = root_destroy;
|
---|
630 | win->root.reconfigure = root_reconfigure;
|
---|
631 | win->root.rearrange = root_rearrange;
|
---|
632 | win->root.repaint = root_repaint;
|
---|
633 | win->root.handle_keyboard_event = root_handle_keyboard_event;
|
---|
634 | win->root.handle_position_event = root_handle_position_event;
|
---|
635 | win->grab = NULL;
|
---|
636 | win->focus = NULL;
|
---|
637 |
|
---|
638 | /* Allocate resources for new surface. */
|
---|
639 | win->surface = surface_create(100, 100, NULL, SURFACE_FLAG_SHARED);
|
---|
640 | if (win->surface == NULL) {
|
---|
641 | free(win);
|
---|
642 | return NULL;
|
---|
643 | }
|
---|
644 |
|
---|
645 | errno_t rc = display_open(winreg, &win->display);
|
---|
646 | if (rc != EOK) {
|
---|
647 | surface_destroy(win->surface);
|
---|
648 | free(win);
|
---|
649 | return NULL;
|
---|
650 | }
|
---|
651 |
|
---|
652 | /* Window dimensions are not know at this time */
|
---|
653 | display_wnd_params_init(&wparams);
|
---|
654 | wparams.rect.p0.x = 0;
|
---|
655 | wparams.rect.p0.y = 0;
|
---|
656 | wparams.rect.p1.x = 100;
|
---|
657 | wparams.rect.p1.y = 100;
|
---|
658 |
|
---|
659 | rc = display_window_create(win->display, &wparams, &window_cb,
|
---|
660 | (void *) win, &win->dwindow);
|
---|
661 | if (rc != EOK) {
|
---|
662 | display_close(win->display);
|
---|
663 | surface_destroy(win->surface);
|
---|
664 | free(win);
|
---|
665 | return NULL;
|
---|
666 | }
|
---|
667 |
|
---|
668 | rc = display_window_get_gc(win->dwindow, &win->gc);
|
---|
669 | if (rc != EOK) {
|
---|
670 | display_window_destroy(win->dwindow);
|
---|
671 | display_close(win->display);
|
---|
672 | surface_destroy(win->surface);
|
---|
673 | free(win);
|
---|
674 | return NULL;
|
---|
675 | }
|
---|
676 |
|
---|
677 | if (caption == NULL)
|
---|
678 | win->caption = NULL;
|
---|
679 | else
|
---|
680 | win->caption = str_dup(caption);
|
---|
681 |
|
---|
682 | return win;
|
---|
683 | }
|
---|
684 |
|
---|
685 | void window_resize(window_t *win, sysarg_t offset_x, sysarg_t offset_y,
|
---|
686 | sysarg_t width, sysarg_t height, window_placement_flags_t placement_flags)
|
---|
687 | {
|
---|
688 | window_event_t *event = (window_event_t *) malloc(sizeof(window_event_t));
|
---|
689 | if (event) {
|
---|
690 | link_initialize(&event->link);
|
---|
691 | event->type = ET_WINDOW_RESIZE;
|
---|
692 | event->data.resize.offset_x = offset_x;
|
---|
693 | event->data.resize.offset_y = offset_y;
|
---|
694 | event->data.resize.width = width;
|
---|
695 | event->data.resize.height = height;
|
---|
696 | event->data.resize.placement_flags = placement_flags;
|
---|
697 | prodcons_produce(&win->events, &event->link);
|
---|
698 | }
|
---|
699 | }
|
---|
700 |
|
---|
701 | errno_t window_set_caption(window_t *win, const char *caption)
|
---|
702 | {
|
---|
703 | char *cap;
|
---|
704 |
|
---|
705 | if (caption == NULL) {
|
---|
706 | win->caption = NULL;
|
---|
707 | } else {
|
---|
708 | cap = str_dup(caption);
|
---|
709 | if (cap == NULL)
|
---|
710 | return ENOMEM;
|
---|
711 | free(win->caption);
|
---|
712 | win->caption = cap;
|
---|
713 | }
|
---|
714 |
|
---|
715 | win->is_focused = false;
|
---|
716 | handle_refresh(win);
|
---|
717 |
|
---|
718 | return EOK;
|
---|
719 | }
|
---|
720 |
|
---|
721 | void window_refresh(window_t *win)
|
---|
722 | {
|
---|
723 | window_event_t *event = (window_event_t *) malloc(sizeof(window_event_t));
|
---|
724 | if (event) {
|
---|
725 | link_initialize(&event->link);
|
---|
726 | event->type = ET_WINDOW_REFRESH;
|
---|
727 | prodcons_produce(&win->events, &event->link);
|
---|
728 | }
|
---|
729 | }
|
---|
730 |
|
---|
731 | void window_damage(window_t *win)
|
---|
732 | {
|
---|
733 | window_event_t *event = (window_event_t *) malloc(sizeof(window_event_t));
|
---|
734 | if (event) {
|
---|
735 | link_initialize(&event->link);
|
---|
736 | event->type = ET_WINDOW_DAMAGE;
|
---|
737 | prodcons_produce(&win->events, &event->link);
|
---|
738 | }
|
---|
739 | }
|
---|
740 |
|
---|
741 | widget_t *window_root(window_t *win)
|
---|
742 | {
|
---|
743 | return &win->root;
|
---|
744 | }
|
---|
745 |
|
---|
746 | void window_exec(window_t *win)
|
---|
747 | {
|
---|
748 | fid_t ev_fid = fibril_create(event_loop, win);
|
---|
749 | if (!ev_fid) {
|
---|
750 | return;
|
---|
751 | }
|
---|
752 | fibril_add_ready(ev_fid);
|
---|
753 | }
|
---|
754 |
|
---|
755 | surface_t *window_claim(window_t *win)
|
---|
756 | {
|
---|
757 | fibril_mutex_lock(&win->guard);
|
---|
758 | return win->surface;
|
---|
759 | }
|
---|
760 |
|
---|
761 | void window_yield(window_t *win)
|
---|
762 | {
|
---|
763 | fibril_mutex_unlock(&win->guard);
|
---|
764 | }
|
---|
765 |
|
---|
766 | void window_close(window_t *win)
|
---|
767 | {
|
---|
768 | /* Request compositor to init closing cascade. */
|
---|
769 | //win_close_request(win->osess);
|
---|
770 | }
|
---|
771 |
|
---|
772 | static void window_focus_event(void *arg)
|
---|
773 | {
|
---|
774 | window_t *win = (window_t *) arg;
|
---|
775 | window_event_t *event;
|
---|
776 |
|
---|
777 | event = (window_event_t *) calloc(1, sizeof(window_event_t));
|
---|
778 | if (event == NULL)
|
---|
779 | return;
|
---|
780 |
|
---|
781 | link_initialize(&event->link);
|
---|
782 | event->type = ET_WINDOW_FOCUS;
|
---|
783 | prodcons_produce(&win->events, &event->link);
|
---|
784 | }
|
---|
785 |
|
---|
786 | static void window_kbd_event(void *arg, kbd_event_t *kevent)
|
---|
787 | {
|
---|
788 | window_t *win = (window_t *) arg;
|
---|
789 | window_event_t *event;
|
---|
790 |
|
---|
791 | event = (window_event_t *) calloc(1, sizeof(window_event_t));
|
---|
792 | if (event == NULL)
|
---|
793 | return;
|
---|
794 |
|
---|
795 | link_initialize(&event->link);
|
---|
796 | event->type = ET_KEYBOARD_EVENT;
|
---|
797 | event->data.kbd = *kevent;
|
---|
798 | prodcons_produce(&win->events, &event->link);
|
---|
799 | }
|
---|
800 |
|
---|
801 | static void window_pos_event(void *arg, pos_event_t *pevent)
|
---|
802 | {
|
---|
803 | window_t *win = (window_t *) arg;
|
---|
804 | window_event_t *event;
|
---|
805 |
|
---|
806 | event = (window_event_t *) calloc(1, sizeof(window_event_t));
|
---|
807 | if (event == NULL)
|
---|
808 | return;
|
---|
809 |
|
---|
810 | link_initialize(&event->link);
|
---|
811 | event->type = ET_POSITION_EVENT;
|
---|
812 | event->data.pos = *pevent;
|
---|
813 | prodcons_produce(&win->events, &event->link);
|
---|
814 | }
|
---|
815 |
|
---|
816 | static void window_unfocus_event(void *arg)
|
---|
817 | {
|
---|
818 | window_t *win = (window_t *) arg;
|
---|
819 | window_event_t *event;
|
---|
820 |
|
---|
821 | event = (window_event_t *) calloc(1, sizeof(window_event_t));
|
---|
822 | if (event == NULL)
|
---|
823 | return;
|
---|
824 |
|
---|
825 | link_initialize(&event->link);
|
---|
826 | event->type = ET_WINDOW_UNFOCUS;
|
---|
827 | prodcons_produce(&win->events, &event->link);
|
---|
828 | }
|
---|
829 |
|
---|
830 | /** @}
|
---|
831 | */
|
---|