1 | /*
|
---|
2 | * Copyright (c) 2019 Jiri Svoboda
|
---|
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 display
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file GFX window backend
|
---|
34 | *
|
---|
35 | * This implements a graphics context over display server window.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <gfx/bitmap.h>
|
---|
39 | #include <gfx/color.h>
|
---|
40 | #include <gfx/coord.h>
|
---|
41 | #include <gfx/context.h>
|
---|
42 | #include <gfx/render.h>
|
---|
43 | #include <io/log.h>
|
---|
44 | #include <io/pixelmap.h>
|
---|
45 | #include <stdlib.h>
|
---|
46 | #include "client.h"
|
---|
47 | #include "display.h"
|
---|
48 | #include "window.h"
|
---|
49 |
|
---|
50 | static errno_t ds_window_set_color(void *, gfx_color_t *);
|
---|
51 | static errno_t ds_window_fill_rect(void *, gfx_rect_t *);
|
---|
52 | static errno_t ds_window_bitmap_create(void *, gfx_bitmap_params_t *,
|
---|
53 | gfx_bitmap_alloc_t *, void **);
|
---|
54 | static errno_t ds_window_bitmap_destroy(void *);
|
---|
55 | static errno_t ds_window_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
|
---|
56 | static errno_t ds_window_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
|
---|
57 |
|
---|
58 | gfx_context_ops_t ds_window_ops = {
|
---|
59 | .set_color = ds_window_set_color,
|
---|
60 | .fill_rect = ds_window_fill_rect,
|
---|
61 | .bitmap_create = ds_window_bitmap_create,
|
---|
62 | .bitmap_destroy = ds_window_bitmap_destroy,
|
---|
63 | .bitmap_render = ds_window_bitmap_render,
|
---|
64 | .bitmap_get_alloc = ds_window_bitmap_get_alloc
|
---|
65 | };
|
---|
66 |
|
---|
67 | /** Set color on window GC.
|
---|
68 | *
|
---|
69 | * Set drawing color on window GC.
|
---|
70 | *
|
---|
71 | * @param arg Console GC
|
---|
72 | * @param color Color
|
---|
73 | *
|
---|
74 | * @return EOK on success or an error code
|
---|
75 | */
|
---|
76 | static errno_t ds_window_set_color(void *arg, gfx_color_t *color)
|
---|
77 | {
|
---|
78 | ds_window_t *wnd = (ds_window_t *) arg;
|
---|
79 | uint16_t r, g, b;
|
---|
80 |
|
---|
81 | log_msg(LOG_DEFAULT, LVL_NOTE, "gc_set_color gc=%p",
|
---|
82 | ds_display_get_gc(wnd->display));
|
---|
83 |
|
---|
84 | gfx_color_get_rgb_i16(color, &r, &g, &b);
|
---|
85 | wnd->color = PIXEL(0, r >> 8, g >> 8, b >> 8);
|
---|
86 |
|
---|
87 | return gfx_set_color(ds_display_get_gc(wnd->display), color);
|
---|
88 | }
|
---|
89 |
|
---|
90 | /** Fill rectangle on window GC.
|
---|
91 | *
|
---|
92 | * @param arg Window GC
|
---|
93 | * @param rect Rectangle
|
---|
94 | *
|
---|
95 | * @return EOK on success or an error code
|
---|
96 | */
|
---|
97 | static errno_t ds_window_fill_rect(void *arg, gfx_rect_t *rect)
|
---|
98 | {
|
---|
99 | ds_window_t *wnd = (ds_window_t *) arg;
|
---|
100 | gfx_rect_t crect;
|
---|
101 | gfx_rect_t drect;
|
---|
102 | gfx_coord_t x, y;
|
---|
103 |
|
---|
104 | log_msg(LOG_DEFAULT, LVL_NOTE, "gc_fill_rect");
|
---|
105 |
|
---|
106 | gfx_rect_clip(rect, &wnd->rect, &crect);
|
---|
107 | gfx_rect_translate(&wnd->dpos, &crect, &drect);
|
---|
108 |
|
---|
109 | /* Render a copy to the backbuffer */
|
---|
110 | for (y = crect.p0.y; y < crect.p1.y; y++) {
|
---|
111 | for (x = crect.p0.x; x < crect.p1.x; x++) {
|
---|
112 | pixelmap_put_pixel(&wnd->pixelmap, x - wnd->rect.p0.x,
|
---|
113 | y - wnd->rect.p0.y, wnd->color);
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | return gfx_fill_rect(ds_display_get_gc(wnd->display), &drect);
|
---|
118 | }
|
---|
119 |
|
---|
120 | /** Create bitmap in window GC.
|
---|
121 | *
|
---|
122 | * @param arg Window GC
|
---|
123 | * @param params Bitmap params
|
---|
124 | * @param alloc Bitmap allocation info or @c NULL
|
---|
125 | * @param rbm Place to store pointer to new bitmap
|
---|
126 | * @return EOK on success or an error code
|
---|
127 | */
|
---|
128 | errno_t ds_window_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
---|
129 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
---|
130 | {
|
---|
131 | ds_window_t *wnd = (ds_window_t *) arg;
|
---|
132 | ds_window_bitmap_t *cbm = NULL;
|
---|
133 | errno_t rc;
|
---|
134 |
|
---|
135 | cbm = calloc(1, sizeof(ds_window_bitmap_t));
|
---|
136 | if (cbm == NULL)
|
---|
137 | return ENOMEM;
|
---|
138 |
|
---|
139 | rc = gfx_bitmap_create(ds_display_get_gc(wnd->display), params, alloc,
|
---|
140 | &cbm->bitmap);
|
---|
141 | if (rc != EOK)
|
---|
142 | goto error;
|
---|
143 |
|
---|
144 | cbm->wnd = wnd;
|
---|
145 | cbm->rect = params->rect;
|
---|
146 | *rbm = (void *)cbm;
|
---|
147 | return EOK;
|
---|
148 | error:
|
---|
149 | if (cbm != NULL)
|
---|
150 | free(cbm);
|
---|
151 | return rc;
|
---|
152 | }
|
---|
153 |
|
---|
154 | /** Destroy bitmap in window GC.
|
---|
155 | *
|
---|
156 | * @param bm Bitmap
|
---|
157 | * @return EOK on success or an error code
|
---|
158 | */
|
---|
159 | static errno_t ds_window_bitmap_destroy(void *bm)
|
---|
160 | {
|
---|
161 | ds_window_bitmap_t *cbm = (ds_window_bitmap_t *)bm;
|
---|
162 |
|
---|
163 | gfx_bitmap_destroy(cbm->bitmap);
|
---|
164 | free(cbm);
|
---|
165 | return EOK;
|
---|
166 | }
|
---|
167 |
|
---|
168 | /** Render bitmap in window GC.
|
---|
169 | *
|
---|
170 | * @param bm Bitmap
|
---|
171 | * @param srect0 Source rectangle or @c NULL
|
---|
172 | * @param offs0 Offset or @c NULL
|
---|
173 | * @return EOK on success or an error code
|
---|
174 | */
|
---|
175 | static errno_t ds_window_bitmap_render(void *bm, gfx_rect_t *srect0,
|
---|
176 | gfx_coord2_t *offs0)
|
---|
177 | {
|
---|
178 | ds_window_bitmap_t *cbm = (ds_window_bitmap_t *)bm;
|
---|
179 | gfx_coord2_t doffs;
|
---|
180 | gfx_coord2_t offs;
|
---|
181 | gfx_rect_t srect;
|
---|
182 | gfx_rect_t swrect;
|
---|
183 | gfx_rect_t crect;
|
---|
184 | gfx_coord_t x, y;
|
---|
185 | pixelmap_t pixelmap;
|
---|
186 | gfx_bitmap_alloc_t alloc;
|
---|
187 | pixel_t pixel;
|
---|
188 | errno_t rc;
|
---|
189 |
|
---|
190 | if (srect0 != NULL) {
|
---|
191 | /* Clip source rectangle to bitmap rectangle */
|
---|
192 | gfx_rect_clip(srect0, &cbm->rect, &srect);
|
---|
193 | } else {
|
---|
194 | /* Source is entire bitmap rectangle */
|
---|
195 | srect = cbm->rect;
|
---|
196 | }
|
---|
197 |
|
---|
198 | if (offs0 != NULL) {
|
---|
199 | offs = *offs0;
|
---|
200 | } else {
|
---|
201 | offs.x = 0;
|
---|
202 | offs.y = 0;
|
---|
203 | }
|
---|
204 |
|
---|
205 | /* Transform window rectangle back to bitmap coordinate system */
|
---|
206 | gfx_rect_rtranslate(&offs, &cbm->wnd->rect, &swrect);
|
---|
207 |
|
---|
208 | /* Clip so that transformed rectangle will be inside the window */
|
---|
209 | gfx_rect_clip(&srect, &swrect, &crect);
|
---|
210 |
|
---|
211 | /* Offset for rendering on screen = window pos + offs */
|
---|
212 | gfx_coord2_add(&cbm->wnd->dpos, &offs, &doffs);
|
---|
213 |
|
---|
214 | rc = gfx_bitmap_get_alloc(cbm->bitmap, &alloc);
|
---|
215 | if (rc != EOK)
|
---|
216 | return rc;
|
---|
217 |
|
---|
218 | pixelmap.width = cbm->rect.p1.x - cbm->rect.p0.x;
|
---|
219 | pixelmap.height = cbm->rect.p1.y - cbm->rect.p0.y;
|
---|
220 | pixelmap.data = alloc.pixels;
|
---|
221 |
|
---|
222 | /* Render a copy to the backbuffer */
|
---|
223 | for (y = crect.p0.y; y < crect.p1.y; y++) {
|
---|
224 | for (x = crect.p0.x; x < crect.p1.x; x++) {
|
---|
225 | pixel = pixelmap_get_pixel(&pixelmap,
|
---|
226 | x - cbm->rect.p0.x, y - cbm->rect.p0.y);
|
---|
227 | pixelmap_put_pixel(&cbm->wnd->pixelmap,
|
---|
228 | x + offs.x - cbm->rect.p0.x + cbm->wnd->rect.p0.x,
|
---|
229 | y + offs.y - cbm->rect.p0.y + cbm->wnd->rect.p0.y,
|
---|
230 | pixel);
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | return gfx_bitmap_render(cbm->bitmap, &crect, &doffs);
|
---|
235 | }
|
---|
236 |
|
---|
237 | /** Get allocation info for bitmap in window GC.
|
---|
238 | *
|
---|
239 | * @param bm Bitmap
|
---|
240 | * @param alloc Place to store allocation info
|
---|
241 | * @return EOK on success or an error code
|
---|
242 | */
|
---|
243 | static errno_t ds_window_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
|
---|
244 | {
|
---|
245 | ds_window_bitmap_t *cbm = (ds_window_bitmap_t *)bm;
|
---|
246 |
|
---|
247 | return gfx_bitmap_get_alloc(cbm->bitmap, alloc);
|
---|
248 | }
|
---|
249 |
|
---|
250 | /** Create window.
|
---|
251 | *
|
---|
252 | * Create graphics context for rendering into a window.
|
---|
253 | *
|
---|
254 | * @param client Client owning the window
|
---|
255 | * @param params Window parameters
|
---|
256 | * @param rgc Place to store pointer to new GC.
|
---|
257 | *
|
---|
258 | * @return EOK on success or an error code
|
---|
259 | */
|
---|
260 | errno_t ds_window_create(ds_client_t *client, display_wnd_params_t *params,
|
---|
261 | ds_window_t **rgc)
|
---|
262 | {
|
---|
263 | ds_window_t *wnd = NULL;
|
---|
264 | gfx_context_t *gc = NULL;
|
---|
265 | gfx_context_t *dgc;
|
---|
266 | gfx_coord2_t dims;
|
---|
267 | gfx_bitmap_params_t bparams;
|
---|
268 | gfx_bitmap_alloc_t alloc;
|
---|
269 | errno_t rc;
|
---|
270 |
|
---|
271 | wnd = calloc(1, sizeof(ds_window_t));
|
---|
272 | if (wnd == NULL) {
|
---|
273 | rc = ENOMEM;
|
---|
274 | goto error;
|
---|
275 | }
|
---|
276 |
|
---|
277 | rc = gfx_context_new(&ds_window_ops, wnd, &gc);
|
---|
278 | if (rc != EOK)
|
---|
279 | goto error;
|
---|
280 |
|
---|
281 | ds_client_add_window(client, wnd);
|
---|
282 | ds_display_add_window(client->display, wnd);
|
---|
283 |
|
---|
284 | gfx_bitmap_params_init(&bparams);
|
---|
285 | bparams.rect = params->rect;
|
---|
286 |
|
---|
287 | dgc = ds_display_get_gc(wnd->display); // XXX
|
---|
288 | if (dgc != NULL) {
|
---|
289 | rc = gfx_bitmap_create(dgc, &bparams, NULL, &wnd->bitmap);
|
---|
290 | if (rc != EOK)
|
---|
291 | goto error;
|
---|
292 |
|
---|
293 | rc = gfx_bitmap_get_alloc(wnd->bitmap, &alloc);
|
---|
294 | if (rc != EOK)
|
---|
295 | goto error;
|
---|
296 |
|
---|
297 | gfx_rect_dims(¶ms->rect, &dims);
|
---|
298 | wnd->pixelmap.width = dims.x;
|
---|
299 | wnd->pixelmap.height = dims.y;
|
---|
300 | wnd->pixelmap.data = alloc.pixels;
|
---|
301 | }
|
---|
302 |
|
---|
303 | wnd->rect = params->rect;
|
---|
304 | wnd->gc = gc;
|
---|
305 | *rgc = wnd;
|
---|
306 | return EOK;
|
---|
307 | error:
|
---|
308 | if (wnd != NULL) {
|
---|
309 | if (wnd->bitmap != NULL)
|
---|
310 | gfx_bitmap_destroy(wnd->bitmap);
|
---|
311 | free(wnd);
|
---|
312 | }
|
---|
313 |
|
---|
314 | gfx_context_delete(gc);
|
---|
315 | return rc;
|
---|
316 | }
|
---|
317 |
|
---|
318 | /** Destroy window.
|
---|
319 | *
|
---|
320 | * @param wnd Window
|
---|
321 | */
|
---|
322 | void ds_window_destroy(ds_window_t *wnd)
|
---|
323 | {
|
---|
324 | ds_display_t *disp;
|
---|
325 |
|
---|
326 | disp = wnd->display;
|
---|
327 |
|
---|
328 | ds_client_remove_window(wnd);
|
---|
329 | ds_display_remove_window(wnd);
|
---|
330 |
|
---|
331 | (void) gfx_context_delete(wnd->gc);
|
---|
332 | if (wnd->bitmap != NULL)
|
---|
333 | gfx_bitmap_destroy(wnd->bitmap);
|
---|
334 |
|
---|
335 | free(wnd);
|
---|
336 |
|
---|
337 | (void) ds_display_paint(disp, NULL);
|
---|
338 | }
|
---|
339 |
|
---|
340 | /** Resize window.
|
---|
341 | *
|
---|
342 | * @param wnd Window
|
---|
343 | */
|
---|
344 | errno_t ds_window_resize(ds_window_t *wnd, gfx_coord2_t *offs,
|
---|
345 | gfx_rect_t *nrect)
|
---|
346 | {
|
---|
347 | gfx_context_t *dgc;
|
---|
348 | gfx_bitmap_params_t bparams;
|
---|
349 | gfx_bitmap_t *nbitmap;
|
---|
350 | pixelmap_t npixelmap;
|
---|
351 | gfx_coord2_t dims;
|
---|
352 | gfx_bitmap_alloc_t alloc;
|
---|
353 | gfx_coord2_t ndpos;
|
---|
354 | errno_t rc;
|
---|
355 |
|
---|
356 | dgc = ds_display_get_gc(wnd->display); // XXX
|
---|
357 | if (dgc != NULL) {
|
---|
358 | gfx_bitmap_params_init(&bparams);
|
---|
359 | bparams.rect = *nrect;
|
---|
360 |
|
---|
361 | rc = gfx_bitmap_create(dgc, &bparams, NULL, &nbitmap);
|
---|
362 | if (rc != EOK)
|
---|
363 | return ENOMEM;
|
---|
364 |
|
---|
365 | rc = gfx_bitmap_get_alloc(nbitmap, &alloc);
|
---|
366 | if (rc != EOK) {
|
---|
367 | gfx_bitmap_destroy(nbitmap);
|
---|
368 | return ENOMEM;
|
---|
369 | }
|
---|
370 |
|
---|
371 | gfx_rect_dims(nrect, &dims);
|
---|
372 | npixelmap.width = dims.x;
|
---|
373 | npixelmap.height = dims.y;
|
---|
374 | npixelmap.data = alloc.pixels;
|
---|
375 |
|
---|
376 | /* TODO: Transfer contents within overlap */
|
---|
377 |
|
---|
378 | if (wnd->bitmap != NULL)
|
---|
379 | gfx_bitmap_destroy(wnd->bitmap);
|
---|
380 |
|
---|
381 | wnd->bitmap = nbitmap;
|
---|
382 | wnd->pixelmap = npixelmap;
|
---|
383 | }
|
---|
384 |
|
---|
385 | gfx_coord2_add(&wnd->dpos, offs, &ndpos);
|
---|
386 |
|
---|
387 | wnd->dpos = ndpos;
|
---|
388 | wnd->rect = *nrect;
|
---|
389 |
|
---|
390 | (void) ds_display_paint(wnd->display, NULL);
|
---|
391 | return EOK;
|
---|
392 | }
|
---|
393 |
|
---|
394 | /** Get generic graphic context from window.
|
---|
395 | *
|
---|
396 | * @param wnd Window
|
---|
397 | * @return Graphic context
|
---|
398 | */
|
---|
399 | gfx_context_t *ds_window_get_ctx(ds_window_t *wnd)
|
---|
400 | {
|
---|
401 | return wnd->gc;
|
---|
402 | }
|
---|
403 |
|
---|
404 | /** Paint a window using its backing bitmap.
|
---|
405 | *
|
---|
406 | * @param wnd Window to paint
|
---|
407 | * @param rect Display rectangle to paint to
|
---|
408 | * @return EOK on success or an error code
|
---|
409 | */
|
---|
410 | errno_t ds_window_paint(ds_window_t *wnd, gfx_rect_t *rect)
|
---|
411 | {
|
---|
412 | gfx_rect_t srect;
|
---|
413 | gfx_rect_t *brect;
|
---|
414 | gfx_rect_t crect;
|
---|
415 |
|
---|
416 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_paint");
|
---|
417 |
|
---|
418 | if (rect != NULL) {
|
---|
419 | gfx_rect_rtranslate(&wnd->dpos, rect, &srect);
|
---|
420 |
|
---|
421 | /* Determine if we have anything to do */
|
---|
422 | gfx_rect_clip(&srect, &wnd->rect, &crect);
|
---|
423 | if (gfx_rect_is_empty(&crect))
|
---|
424 | return EOK;
|
---|
425 |
|
---|
426 | brect = &srect;
|
---|
427 | } else {
|
---|
428 | brect = NULL;
|
---|
429 | }
|
---|
430 |
|
---|
431 | /* This can happen in unit tests */
|
---|
432 | if (wnd->bitmap == NULL)
|
---|
433 | return EOK;
|
---|
434 |
|
---|
435 | return gfx_bitmap_render(wnd->bitmap, brect, &wnd->dpos);
|
---|
436 | }
|
---|
437 |
|
---|
438 | /** Start moving a window, detected by client.
|
---|
439 | *
|
---|
440 | * @param wnd Window
|
---|
441 | * @param pos Position where the pointer was when the move started
|
---|
442 | * relative to the window
|
---|
443 | * @param event Button press event
|
---|
444 | */
|
---|
445 | void ds_window_move_req(ds_window_t *wnd, gfx_coord2_t *pos)
|
---|
446 | {
|
---|
447 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_move_req (%d, %d)",
|
---|
448 | (int) pos->x, (int) pos->y);
|
---|
449 |
|
---|
450 | if (wnd->state != dsw_idle)
|
---|
451 | return;
|
---|
452 |
|
---|
453 | gfx_coord2_add(&wnd->dpos, pos, &wnd->orig_pos);
|
---|
454 | wnd->state = dsw_moving;
|
---|
455 | }
|
---|
456 |
|
---|
457 | /** Start moving a window by mouse drag.
|
---|
458 | *
|
---|
459 | * @param wnd Window
|
---|
460 | * @param event Button press event
|
---|
461 | */
|
---|
462 | static void ds_window_start_move(ds_window_t *wnd, pos_event_t *event)
|
---|
463 | {
|
---|
464 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_start_move (%d, %d)",
|
---|
465 | (int) event->hpos, (int) event->vpos);
|
---|
466 |
|
---|
467 | if (wnd->state != dsw_idle)
|
---|
468 | return;
|
---|
469 |
|
---|
470 | wnd->orig_pos.x = event->hpos;
|
---|
471 | wnd->orig_pos.y = event->vpos;
|
---|
472 | wnd->state = dsw_moving;
|
---|
473 | }
|
---|
474 |
|
---|
475 | /** Finish moving a window by mouse drag.
|
---|
476 | *
|
---|
477 | * @param wnd Window
|
---|
478 | * @param event Button release event
|
---|
479 | */
|
---|
480 | static void ds_window_finish_move(ds_window_t *wnd, pos_event_t *event)
|
---|
481 | {
|
---|
482 | gfx_coord2_t pos;
|
---|
483 | gfx_coord2_t dmove;
|
---|
484 | gfx_coord2_t nwpos;
|
---|
485 |
|
---|
486 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_finish_move (%d, %d)",
|
---|
487 | (int) event->hpos, (int) event->vpos);
|
---|
488 |
|
---|
489 | if (wnd->state != dsw_moving)
|
---|
490 | return;
|
---|
491 |
|
---|
492 | pos.x = event->hpos;
|
---|
493 | pos.y = event->vpos;
|
---|
494 | gfx_coord2_subtract(&pos, &wnd->orig_pos, &dmove);
|
---|
495 |
|
---|
496 | gfx_coord2_add(&wnd->dpos, &dmove, &nwpos);
|
---|
497 | wnd->dpos = nwpos;
|
---|
498 | wnd->state = dsw_idle;
|
---|
499 |
|
---|
500 | (void) ds_display_paint(wnd->display, NULL);
|
---|
501 | }
|
---|
502 |
|
---|
503 | /** Update window position when moving by mouse drag.
|
---|
504 | *
|
---|
505 | * @param wnd Window
|
---|
506 | * @param event Position update event
|
---|
507 | */
|
---|
508 | static void ds_window_update_move(ds_window_t *wnd, pos_event_t *event)
|
---|
509 | {
|
---|
510 | gfx_coord2_t pos;
|
---|
511 | gfx_coord2_t dmove;
|
---|
512 | gfx_coord2_t nwpos;
|
---|
513 | gfx_rect_t drect;
|
---|
514 | gfx_color_t *color;
|
---|
515 | gfx_context_t *gc;
|
---|
516 | errno_t rc;
|
---|
517 |
|
---|
518 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_update_move (%d, %d)",
|
---|
519 | (int) event->hpos, (int) event->vpos);
|
---|
520 |
|
---|
521 | if (wnd->state != dsw_moving)
|
---|
522 | return;
|
---|
523 |
|
---|
524 | gfx_rect_translate(&wnd->dpos, &wnd->rect, &drect);
|
---|
525 |
|
---|
526 | gc = ds_display_get_gc(wnd->display); // XXX
|
---|
527 | if (gc != NULL) {
|
---|
528 | gfx_set_color(gc, wnd->display->bg_color);
|
---|
529 | gfx_fill_rect(gc, &drect);
|
---|
530 | }
|
---|
531 |
|
---|
532 | pos.x = event->hpos;
|
---|
533 | pos.y = event->vpos;
|
---|
534 | gfx_coord2_subtract(&pos, &wnd->orig_pos, &dmove);
|
---|
535 |
|
---|
536 | gfx_coord2_add(&wnd->dpos, &dmove, &nwpos);
|
---|
537 | gfx_rect_translate(&nwpos, &wnd->rect, &drect);
|
---|
538 |
|
---|
539 | wnd->orig_pos = pos;
|
---|
540 | wnd->dpos = nwpos;
|
---|
541 |
|
---|
542 | rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color);
|
---|
543 | if (rc != EOK)
|
---|
544 | return;
|
---|
545 |
|
---|
546 | gc = ds_display_get_gc(wnd->display); // XXX
|
---|
547 | if (gc != NULL) {
|
---|
548 | gfx_set_color(gc, color);
|
---|
549 | gfx_fill_rect(gc, &drect);
|
---|
550 | }
|
---|
551 |
|
---|
552 | gfx_color_delete(color);
|
---|
553 | }
|
---|
554 |
|
---|
555 | /** Post keyboard event to window.
|
---|
556 | *
|
---|
557 | * @param wnd Window
|
---|
558 | * @param event Event
|
---|
559 | *
|
---|
560 | * @return EOK on success or an error code
|
---|
561 | */
|
---|
562 | errno_t ds_window_post_kbd_event(ds_window_t *wnd, kbd_event_t *event)
|
---|
563 | {
|
---|
564 | bool alt_or_shift;
|
---|
565 |
|
---|
566 | alt_or_shift = event->mods & (KM_SHIFT | KM_ALT);
|
---|
567 |
|
---|
568 | if (event->type == KEY_PRESS && alt_or_shift && event->key == KC_F4) {
|
---|
569 | /* On Alt-F4 or Shift-F4 send close event to the window */
|
---|
570 | ds_client_post_close_event(wnd->client, wnd);
|
---|
571 | return EOK;
|
---|
572 | }
|
---|
573 |
|
---|
574 | return ds_client_post_kbd_event(wnd->client, wnd, event);
|
---|
575 | }
|
---|
576 |
|
---|
577 | /** Post position event to window.
|
---|
578 | *
|
---|
579 | * @param wnd Window
|
---|
580 | * @param event Position event
|
---|
581 | */
|
---|
582 | errno_t ds_window_post_pos_event(ds_window_t *wnd, pos_event_t *event)
|
---|
583 | {
|
---|
584 | pos_event_t tevent;
|
---|
585 | gfx_coord2_t pos;
|
---|
586 | gfx_rect_t drect;
|
---|
587 | bool inside;
|
---|
588 |
|
---|
589 | log_msg(LOG_DEFAULT, LVL_DEBUG,
|
---|
590 | "ds_window_post_pos_event type=%d pos=%d,%d\n", event->type,
|
---|
591 | (int) event->hpos, (int) event->vpos);
|
---|
592 |
|
---|
593 | pos.x = event->hpos;
|
---|
594 | pos.y = event->vpos;
|
---|
595 | gfx_rect_translate(&wnd->dpos, &wnd->rect, &drect);
|
---|
596 | inside = gfx_pix_inside_rect(&pos, &drect);
|
---|
597 |
|
---|
598 | if (event->type == POS_PRESS && event->btn_num == 2 && inside)
|
---|
599 | ds_window_start_move(wnd, event);
|
---|
600 |
|
---|
601 | if (event->type == POS_RELEASE)
|
---|
602 | ds_window_finish_move(wnd, event);
|
---|
603 |
|
---|
604 | if (event->type == POS_UPDATE)
|
---|
605 | ds_window_update_move(wnd, event);
|
---|
606 |
|
---|
607 | /* Transform event coordinates to window-local */
|
---|
608 | tevent = *event;
|
---|
609 | tevent.hpos -= wnd->dpos.x;
|
---|
610 | tevent.vpos -= wnd->dpos.y;
|
---|
611 |
|
---|
612 | return ds_client_post_pos_event(wnd->client, wnd, &tevent);
|
---|
613 | }
|
---|
614 |
|
---|
615 | /** Post focus event to window.
|
---|
616 | *
|
---|
617 | * @param wnd Window
|
---|
618 | */
|
---|
619 | errno_t ds_window_post_focus_event(ds_window_t *wnd)
|
---|
620 | {
|
---|
621 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_post_focus_event\n");
|
---|
622 |
|
---|
623 | return ds_client_post_focus_event(wnd->client, wnd);
|
---|
624 | }
|
---|
625 |
|
---|
626 | /** Post unfocus event to window.
|
---|
627 | *
|
---|
628 | * @param wnd Window
|
---|
629 | */
|
---|
630 | errno_t ds_window_post_unfocus_event(ds_window_t *wnd)
|
---|
631 | {
|
---|
632 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_post_unfocus_event\n");
|
---|
633 |
|
---|
634 | return ds_client_post_unfocus_event(wnd->client, wnd);
|
---|
635 | }
|
---|
636 |
|
---|
637 | /** @}
|
---|
638 | */
|
---|