source: mainline/uspace/srv/hid/display/window.c@ 0e6e77f

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

Window resize by client request

  • Property mode set to 100644
File size: 14.0 KB
Line 
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
50static errno_t ds_window_set_color(void *, gfx_color_t *);
51static errno_t ds_window_fill_rect(void *, gfx_rect_t *);
52static errno_t ds_window_bitmap_create(void *, gfx_bitmap_params_t *,
53 gfx_bitmap_alloc_t *, void **);
54static errno_t ds_window_bitmap_destroy(void *);
55static errno_t ds_window_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
56static errno_t ds_window_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
57
58gfx_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 */
76static 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 */
97static 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 */
128errno_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;
148error:
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 */
159static 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 */
175static 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 */
243static 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 */
260errno_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 bparams.rect = params->rect;
285
286 dgc = ds_display_get_gc(wnd->display); // XXX
287 if (dgc != NULL) {
288 rc = gfx_bitmap_create(dgc, &bparams, NULL, &wnd->bitmap);
289 if (rc != EOK)
290 goto error;
291
292 rc = gfx_bitmap_get_alloc(wnd->bitmap, &alloc);
293 if (rc != EOK)
294 goto error;
295
296 gfx_rect_dims(&params->rect, &dims);
297 wnd->pixelmap.width = dims.x;
298 wnd->pixelmap.height = dims.y;
299 wnd->pixelmap.data = alloc.pixels;
300 }
301
302 wnd->rect = params->rect;
303 wnd->gc = gc;
304 *rgc = wnd;
305 return EOK;
306error:
307 if (wnd != NULL) {
308 if (wnd->bitmap != NULL)
309 gfx_bitmap_destroy(wnd->bitmap);
310 free(wnd);
311 }
312
313 gfx_context_delete(gc);
314 return rc;
315}
316
317/** Destroy window.
318 *
319 * @param wnd Window
320 */
321void ds_window_destroy(ds_window_t *wnd)
322{
323 ds_display_t *disp;
324
325 disp = wnd->display;
326
327 ds_client_remove_window(wnd);
328 ds_display_remove_window(wnd);
329
330 (void) gfx_context_delete(wnd->gc);
331 if (wnd->bitmap != NULL)
332 gfx_bitmap_destroy(wnd->bitmap);
333
334 free(wnd);
335
336 (void) ds_display_paint(disp, NULL);
337}
338
339/** Resize window.
340 *
341 * @param wnd Window
342 */
343errno_t ds_window_resize(ds_window_t *wnd, gfx_coord2_t *offs,
344 gfx_rect_t *nrect)
345{
346 gfx_context_t *dgc;
347 gfx_bitmap_params_t bparams;
348 gfx_bitmap_t *nbitmap;
349 pixelmap_t npixelmap;
350 gfx_coord2_t dims;
351 gfx_bitmap_alloc_t alloc;
352 gfx_coord2_t ndpos;
353 errno_t rc;
354
355 dgc = ds_display_get_gc(wnd->display); // XXX
356 if (dgc != NULL) {
357 bparams.rect = *nrect;
358
359 rc = gfx_bitmap_create(dgc, &bparams, NULL, &nbitmap);
360 if (rc != EOK)
361 return ENOMEM;
362
363 rc = gfx_bitmap_get_alloc(nbitmap, &alloc);
364 if (rc != EOK) {
365 gfx_bitmap_destroy(nbitmap);
366 return ENOMEM;
367 }
368
369 gfx_rect_dims(nrect, &dims);
370 npixelmap.width = dims.x;
371 npixelmap.height = dims.y;
372 npixelmap.data = alloc.pixels;
373
374 /* TODO: Transfer contents within overlap */
375
376 if (wnd->bitmap != NULL)
377 gfx_bitmap_destroy(wnd->bitmap);
378
379 wnd->bitmap = nbitmap;
380 wnd->pixelmap = npixelmap;
381 }
382
383 gfx_coord2_add(&wnd->dpos, offs, &ndpos);
384
385 wnd->dpos = ndpos;
386 wnd->rect = *nrect;
387
388 (void) ds_display_paint(wnd->display, NULL);
389 return EOK;
390}
391
392/** Get generic graphic context from window.
393 *
394 * @param wnd Window
395 * @return Graphic context
396 */
397gfx_context_t *ds_window_get_ctx(ds_window_t *wnd)
398{
399 return wnd->gc;
400}
401
402/** Paint a window using its backing bitmap.
403 *
404 * @param wnd Window to paint
405 * @param rect Display rectangle to paint to
406 * @return EOK on success or an error code
407 */
408errno_t ds_window_paint(ds_window_t *wnd, gfx_rect_t *rect)
409{
410 gfx_rect_t srect;
411 gfx_rect_t *brect;
412 gfx_rect_t crect;
413
414 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_paint");
415
416 if (rect != NULL) {
417 gfx_rect_rtranslate(&wnd->dpos, rect, &srect);
418
419 /* Determine if we have anything to do */
420 gfx_rect_clip(&srect, &wnd->rect, &crect);
421 if (gfx_rect_is_empty(&crect))
422 return EOK;
423
424 brect = &srect;
425 } else {
426 brect = NULL;
427 }
428
429 /* This can happen in unit tests */
430 if (wnd->bitmap == NULL)
431 return EOK;
432
433 return gfx_bitmap_render(wnd->bitmap, brect, &wnd->dpos);
434}
435
436/** Start moving a window by mouse drag.
437 *
438 * @param wnd Window
439 * @param event Button press event
440 */
441static void ds_window_start_move(ds_window_t *wnd, pos_event_t *event)
442{
443 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_start_move (%d, %d)",
444 (int) event->hpos, (int) event->vpos);
445
446 assert(wnd->state == dsw_idle);
447
448 wnd->orig_pos.x = event->hpos;
449 wnd->orig_pos.y = event->vpos;
450 wnd->state = dsw_moving;
451}
452
453/** Finish moving a window by mouse drag.
454 *
455 * @param wnd Window
456 * @param event Button release event
457 */
458static void ds_window_finish_move(ds_window_t *wnd, pos_event_t *event)
459{
460 gfx_coord2_t pos;
461 gfx_coord2_t dmove;
462 gfx_coord2_t nwpos;
463
464 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_finish_move (%d, %d)",
465 (int) event->hpos, (int) event->vpos);
466
467 assert(wnd->state == dsw_moving);
468 pos.x = event->hpos;
469 pos.y = event->vpos;
470 gfx_coord2_subtract(&pos, &wnd->orig_pos, &dmove);
471
472 gfx_coord2_add(&wnd->dpos, &dmove, &nwpos);
473 wnd->dpos = nwpos;
474 wnd->state = dsw_idle;
475
476 (void) ds_display_paint(wnd->display, NULL);
477}
478
479/** Update window position when moving by mouse drag.
480 *
481 * @param wnd Window
482 * @param event Position update event
483 */
484static void ds_window_update_move(ds_window_t *wnd, pos_event_t *event)
485{
486 gfx_coord2_t pos;
487 gfx_coord2_t dmove;
488 gfx_coord2_t nwpos;
489 gfx_rect_t drect;
490 gfx_color_t *color;
491 gfx_context_t *gc;
492 errno_t rc;
493
494 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_update_move (%d, %d)",
495 (int) event->hpos, (int) event->vpos);
496
497 gfx_rect_translate(&wnd->dpos, &wnd->rect, &drect);
498
499 gc = ds_display_get_gc(wnd->display); // XXX
500 if (gc != NULL) {
501 gfx_set_color(gc, wnd->display->bg_color);
502 gfx_fill_rect(gc, &drect);
503 }
504
505 assert(wnd->state == dsw_moving);
506 pos.x = event->hpos;
507 pos.y = event->vpos;
508 gfx_coord2_subtract(&pos, &wnd->orig_pos, &dmove);
509
510 gfx_coord2_add(&wnd->dpos, &dmove, &nwpos);
511 gfx_rect_translate(&nwpos, &wnd->rect, &drect);
512
513 wnd->orig_pos = pos;
514 wnd->dpos = nwpos;
515
516 rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color);
517 if (rc != EOK)
518 return;
519
520 gc = ds_display_get_gc(wnd->display); // XXX
521 if (gc != NULL) {
522 gfx_set_color(gc, color);
523 gfx_fill_rect(gc, &drect);
524 }
525
526 gfx_color_delete(color);
527}
528
529/** Post position event to window.
530 *
531 * @param wnd Window
532 * @param event Position event
533 */
534errno_t ds_window_post_pos_event(ds_window_t *wnd, pos_event_t *event)
535{
536 pos_event_t tevent;
537
538 log_msg(LOG_DEFAULT, LVL_DEBUG,
539 "ds_window_post_pos_event type=%d pos=%d,%d\n", event->type,
540 (int) event->hpos, (int) event->vpos);
541
542 if (event->type == POS_PRESS) {
543 if (wnd->state == dsw_idle)
544 ds_window_start_move(wnd, event);
545 }
546
547 if (event->type == POS_RELEASE) {
548 if (wnd->state == dsw_moving)
549 ds_window_finish_move(wnd, event);
550 }
551
552 if (event->type == POS_UPDATE) {
553 if (wnd->state == dsw_moving)
554 ds_window_update_move(wnd, event);
555 }
556
557 /* Transform event coordinates to window-local */
558 tevent = *event;
559 tevent.hpos -= wnd->dpos.x;
560 tevent.vpos -= wnd->dpos.y;
561
562 return ds_client_post_pos_event(wnd->client, wnd, &tevent);
563}
564
565/** Post focus event to window.
566 *
567 * @param wnd Window
568 */
569errno_t ds_window_post_focus_event(ds_window_t *wnd)
570{
571 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_post_focus_event\n");
572
573 return ds_client_post_focus_event(wnd->client, wnd);
574}
575
576/** Post unfocus event to window.
577 *
578 * @param wnd Window
579 */
580errno_t ds_window_post_unfocus_event(ds_window_t *wnd)
581{
582 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_post_unfocus_event\n");
583
584 return ds_client_post_unfocus_event(wnd->client, wnd);
585}
586
587/** @}
588 */
Note: See TracBrowser for help on using the repository browser.