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

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c48192e was 88d828e, checked in by Jiri Svoboda <jiri@…>, 3 years ago

Keyboard events need device ID too + some DS multiseat work

  • Property mode set to 100644
File size: 23.4 KB
Line 
1/*
2 * Copyright (c) 2022 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 Display server window
34 */
35
36#include <gfx/bitmap.h>
37#include <gfx/color.h>
38#include <gfx/coord.h>
39#include <gfx/context.h>
40#include <gfx/render.h>
41#include <io/log.h>
42#include <io/pixelmap.h>
43#include <macros.h>
44#include <memgfx/memgc.h>
45#include <stdlib.h>
46#include <str.h>
47#include <wndmgt.h>
48#include "client.h"
49#include "display.h"
50#include "seat.h"
51#include "window.h"
52#include "wmclient.h"
53
54static void ds_window_invalidate_cb(void *, gfx_rect_t *);
55static void ds_window_update_cb(void *);
56static void ds_window_get_preview_rect(ds_window_t *, gfx_rect_t *);
57
58static mem_gc_cb_t ds_window_mem_gc_cb = {
59 .invalidate = ds_window_invalidate_cb,
60 .update = ds_window_update_cb
61};
62
63/** Create window.
64 *
65 * Create graphics context for rendering into a window.
66 *
67 * @param client Client owning the window
68 * @param params Window parameters
69 * @param rgc Place to store pointer to new GC.
70 *
71 * @return EOK on success or an error code
72 */
73errno_t ds_window_create(ds_client_t *client, display_wnd_params_t *params,
74 ds_window_t **rgc)
75{
76 ds_window_t *wnd = NULL;
77 ds_seat_t *seat;
78 gfx_context_t *dgc;
79 gfx_coord2_t dims;
80 gfx_bitmap_params_t bparams;
81 gfx_bitmap_alloc_t alloc;
82 errno_t rc;
83
84 wnd = calloc(1, sizeof(ds_window_t));
85 if (wnd == NULL) {
86 rc = ENOMEM;
87 goto error;
88 }
89
90 wnd->caption = str_dup(params->caption);
91 if (wnd->caption == NULL) {
92 rc = ENOMEM;
93 goto error;
94 }
95
96 ds_client_add_window(client, wnd);
97 ds_display_add_window(client->display, wnd);
98
99 gfx_bitmap_params_init(&bparams);
100 bparams.rect = params->rect;
101
102 dgc = ds_display_get_gc(wnd->display);
103 if (dgc != NULL) {
104 rc = gfx_bitmap_create(dgc, &bparams, NULL, &wnd->bitmap);
105 if (rc != EOK)
106 goto error;
107
108 rc = gfx_bitmap_get_alloc(wnd->bitmap, &alloc);
109 if (rc != EOK)
110 goto error;
111
112 gfx_rect_dims(&params->rect, &dims);
113 wnd->pixelmap.width = dims.x;
114 wnd->pixelmap.height = dims.y;
115 wnd->pixelmap.data = alloc.pixels;
116 } else {
117 /* This is just for unit tests */
118 gfx_rect_dims(&params->rect, &dims);
119 alloc.pitch = dims.x * sizeof(uint32_t);
120 alloc.off0 = 0;
121 alloc.pixels = calloc(1, alloc.pitch * dims.y);
122 }
123
124 rc = mem_gc_create(&params->rect, &alloc, &ds_window_mem_gc_cb,
125 (void *)wnd, &wnd->mgc);
126 if (rc != EOK)
127 goto error;
128
129 wnd->rect = params->rect;
130 wnd->min_size = params->min_size;
131 wnd->gc = mem_gc_get_ctx(wnd->mgc);
132 wnd->cursor = wnd->display->cursor[dcurs_arrow];
133 wnd->flags = params->flags;
134
135 if ((params->flags & wndf_setpos) != 0) {
136 /* Specific window position */
137 wnd->dpos = params->pos;
138 } else {
139 /* Automatic window placement */
140 wnd->dpos.x = ((wnd->id - 1) & 1) * 400;
141 wnd->dpos.y = ((wnd->id - 1) & 2) / 2 * 300;
142 }
143
144 // TODO Multi-seat: which seat should own the new window?
145 seat = ds_display_first_seat(client->display);
146
147 if ((params->flags & wndf_popup) != 0)
148 ds_seat_set_popup(seat, wnd);
149 else
150 ds_seat_set_focus(seat, wnd);
151
152 (void) ds_display_paint(wnd->display, NULL);
153
154 *rgc = wnd;
155 return EOK;
156error:
157 if (wnd != NULL) {
158 ds_client_remove_window(wnd);
159 ds_display_remove_window(wnd);
160 if (wnd->mgc != NULL)
161 mem_gc_delete(wnd->mgc);
162 if (wnd->bitmap != NULL)
163 gfx_bitmap_destroy(wnd->bitmap);
164 if (wnd->caption != NULL)
165 free(wnd->caption);
166 free(wnd);
167 }
168
169 return rc;
170}
171
172/** Destroy window.
173 *
174 * @param wnd Window
175 */
176void ds_window_destroy(ds_window_t *wnd)
177{
178 ds_display_t *disp;
179
180 disp = wnd->display;
181
182 ds_client_remove_window(wnd);
183 ds_display_remove_window(wnd);
184
185 mem_gc_delete(wnd->mgc);
186
187 if (wnd->bitmap != NULL)
188 gfx_bitmap_destroy(wnd->bitmap);
189
190 free(wnd->caption);
191 free(wnd);
192
193 (void) ds_display_paint(disp, NULL);
194}
195
196/** Bring window to top.
197 *
198 * @param wnd Window
199 */
200void ds_window_bring_to_top(ds_window_t *wnd)
201{
202 ds_display_window_to_top(wnd);
203 (void) ds_display_paint(wnd->display, NULL);
204}
205
206/** Get generic graphic context from window.
207 *
208 * @param wnd Window
209 * @return Graphic context
210 */
211gfx_context_t *ds_window_get_ctx(ds_window_t *wnd)
212{
213 return wnd->gc;
214}
215
216/** Paint a window using its backing bitmap.
217 *
218 * @param wnd Window to paint
219 * @param rect Display rectangle to paint to
220 * @return EOK on success or an error code
221 */
222errno_t ds_window_paint(ds_window_t *wnd, gfx_rect_t *rect)
223{
224 gfx_rect_t srect;
225 gfx_rect_t *brect;
226 gfx_rect_t crect;
227
228 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_paint");
229
230 if (rect != NULL) {
231 gfx_rect_rtranslate(&wnd->dpos, rect, &srect);
232
233 /* Determine if we have anything to do */
234 gfx_rect_clip(&srect, &wnd->rect, &crect);
235 if (gfx_rect_is_empty(&crect))
236 return EOK;
237
238 brect = &srect;
239 } else {
240 brect = NULL;
241 }
242
243 /* This can happen in unit tests */
244 if (wnd->bitmap == NULL)
245 return EOK;
246
247 return gfx_bitmap_render(wnd->bitmap, brect, &wnd->dpos);
248}
249
250/** Get the preview rectangle for a window.
251 *
252 * Get the preview rectangle if the window is being resized or moved.
253 * If the window is not being resized or moved, return an empty rectangle.
254 *
255 * @param wnd Window
256 * @param rect Place to store preview rectangle
257 */
258static void ds_window_get_preview_rect(ds_window_t *wnd, gfx_rect_t *rect)
259{
260 switch (wnd->state) {
261 case dsw_idle:
262 break;
263 case dsw_moving:
264 gfx_rect_translate(&wnd->preview_pos, &wnd->rect, rect);
265 return;
266 case dsw_resizing:
267 gfx_rect_translate(&wnd->dpos, &wnd->preview_rect, rect);
268 return;
269 }
270
271 rect->p0.x = 0;
272 rect->p0.y = 0;
273 rect->p1.x = 0;
274 rect->p1.y = 0;
275}
276
277/** Paint window preview if the window is being moved or resized.
278 *
279 * If the window is not being resized or moved, take no action and return
280 * success.
281 *
282 * @param wnd Window for which to paint preview
283 * @param rect Clipping rectangle
284 * @return EOK on success or an error code
285 */
286errno_t ds_window_paint_preview(ds_window_t *wnd, gfx_rect_t *rect)
287{
288 errno_t rc;
289 gfx_color_t *color;
290 gfx_rect_t prect;
291 gfx_rect_t dr;
292 gfx_rect_t pr;
293 gfx_context_t *gc;
294
295 /*
296 * Get preview rectangle. If the window is not being resized/moved,
297 * we should get an empty rectangle.
298 */
299 ds_window_get_preview_rect(wnd, &prect);
300 if (gfx_rect_is_empty(&prect)) {
301 /* There is nothing to paint */
302 return EOK;
303 }
304
305 rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color);
306 if (rc != EOK)
307 return rc;
308
309 gc = ds_display_get_gc(wnd->display);
310 if (gc != NULL) {
311 gfx_set_color(gc, color);
312
313 /*
314 * TODO: Ideally we'd want XOR operation to make the preview
315 * frame visible on any background. If we wanted to get really
316 * fancy, we'd fill it with a pattern
317 */
318
319 pr.p0.x = prect.p0.x;
320 pr.p0.y = prect.p0.y;
321 pr.p1.x = prect.p1.x;
322 pr.p1.y = prect.p0.y + 1;
323 gfx_rect_clip(&pr, rect, &dr);
324 gfx_fill_rect(gc, &dr);
325
326 pr.p0.x = prect.p0.x;
327 pr.p0.y = prect.p1.y - 1;
328 pr.p1.x = prect.p1.x;
329 pr.p1.y = prect.p1.y;
330 gfx_rect_clip(&pr, rect, &dr);
331 gfx_fill_rect(gc, &dr);
332
333 pr.p0.x = prect.p0.x;
334 pr.p0.y = prect.p0.y;
335 pr.p1.x = prect.p0.x + 1;
336 pr.p1.y = prect.p1.y;
337 gfx_rect_clip(&pr, rect, &dr);
338 gfx_fill_rect(gc, &dr);
339
340 pr.p0.x = prect.p1.x - 1;
341 pr.p0.y = prect.p0.y;
342 pr.p1.x = prect.p1.x;
343 pr.p1.y = prect.p1.y;
344 gfx_rect_clip(&pr, rect, &dr);
345 gfx_fill_rect(gc, &dr);
346
347 }
348
349 gfx_color_delete(color);
350 return EOK;
351}
352
353/** Repaint window preview when resizing or moving.
354 *
355 * Repaint the window preview wich was previously at rectangle @a old_rect.
356 * The current preview rectangle is determined from window state. If
357 * the window did not previously have a preview, @a old_rect should point
358 * to an empty rectangle or be NULL. When window has finished
359 * moving or resizing, the preview will be cleared.
360 *
361 * @param wnd Window for which to paint preview
362 * @param rect Clipping rectangle
363 * @return EOK on success or an error code
364 */
365static errno_t ds_window_repaint_preview(ds_window_t *wnd, gfx_rect_t *old_rect)
366{
367 errno_t rc;
368 gfx_rect_t prect;
369 gfx_rect_t envelope;
370 bool oldr;
371 bool newr;
372
373 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_repaint_preview");
374
375 /*
376 * Get current preview rectangle. If the window is not being resized/moved,
377 * we should get an empty rectangle.
378 */
379 ds_window_get_preview_rect(wnd, &prect);
380
381 oldr = (old_rect != NULL) && !gfx_rect_is_empty(old_rect);
382 newr = !gfx_rect_is_empty(&prect);
383
384 if (oldr && newr && gfx_rect_is_incident(old_rect, &prect)) {
385 /*
386 * As an optimization, repaint both rectangles in a single
387 * operation.
388 */
389
390 gfx_rect_envelope(old_rect, &prect, &envelope);
391
392 rc = ds_display_paint(wnd->display, &envelope);
393 if (rc != EOK)
394 return rc;
395 } else {
396 /* Repaint each rectangle separately */
397 if (oldr) {
398 rc = ds_display_paint(wnd->display, old_rect);
399 if (rc != EOK)
400 return rc;
401 }
402
403 if (newr) {
404 rc = ds_display_paint(wnd->display, &prect);
405 if (rc != EOK)
406 return rc;
407 }
408 }
409
410 return EOK;
411}
412
413/** Start moving a window by mouse drag.
414 *
415 * @param wnd Window
416 * @param pos Position where mouse button was pressed
417 */
418static void ds_window_start_move(ds_window_t *wnd, gfx_coord2_t *pos)
419{
420 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_start_move (%d, %d)",
421 (int) pos->x, (int) pos->y);
422
423 if (wnd->state != dsw_idle)
424 return;
425
426 wnd->orig_pos = *pos;
427 wnd->state = dsw_moving;
428 wnd->preview_pos = wnd->dpos;
429
430 (void) ds_window_repaint_preview(wnd, NULL);
431}
432
433/** Finish moving a window by mouse drag.
434 *
435 * @param wnd Window
436 * @param pos Position where mouse button was released
437 */
438static void ds_window_finish_move(ds_window_t *wnd, gfx_coord2_t *pos)
439{
440 gfx_coord2_t dmove;
441 gfx_coord2_t nwpos;
442 gfx_rect_t old_rect;
443
444 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_finish_move (%d, %d)",
445 (int) pos->x, (int) pos->y);
446
447 assert(wnd->state == dsw_moving);
448
449 gfx_coord2_subtract(pos, &wnd->orig_pos, &dmove);
450 gfx_coord2_add(&wnd->dpos, &dmove, &nwpos);
451
452 ds_window_get_preview_rect(wnd, &old_rect);
453
454 wnd->dpos = nwpos;
455 wnd->state = dsw_idle;
456
457 (void) ds_display_paint(wnd->display, NULL);
458}
459
460/** Update window position when moving by mouse drag.
461 *
462 * @param wnd Window
463 * @param pos Current mouse position
464 */
465static void ds_window_update_move(ds_window_t *wnd, gfx_coord2_t *pos)
466{
467 gfx_coord2_t dmove;
468 gfx_coord2_t nwpos;
469 gfx_rect_t old_rect;
470
471 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_update_move (%d, %d)",
472 (int) pos->x, (int) pos->y);
473
474 assert(wnd->state == dsw_moving);
475
476 gfx_coord2_subtract(pos, &wnd->orig_pos, &dmove);
477 gfx_coord2_add(&wnd->dpos, &dmove, &nwpos);
478
479 ds_window_get_preview_rect(wnd, &old_rect);
480 wnd->preview_pos = nwpos;
481
482 (void) ds_window_repaint_preview(wnd, &old_rect);
483}
484
485/** Start resizing a window by mouse drag.
486 *
487 * @param wnd Window
488 * @param rsztype Resize type (which part of window is being dragged)
489 * @param pos Position where mouse button was pressed
490 */
491static void ds_window_start_resize(ds_window_t *wnd,
492 display_wnd_rsztype_t rsztype, gfx_coord2_t *pos)
493{
494 ds_seat_t *seat;
495 display_stock_cursor_t ctype;
496
497 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_start_resize (%d, %d)",
498 (int) pos->x, (int) pos->y);
499
500 if (wnd->state != dsw_idle)
501 return;
502
503 wnd->orig_pos = *pos;
504 wnd->state = dsw_resizing;
505 wnd->rsztype = rsztype;
506 wnd->preview_rect = wnd->rect;
507
508 // TODO Multi-seat: need client to tell us which seat started the resize!
509 seat = ds_display_first_seat(wnd->display);
510 ctype = display_cursor_from_wrsz(rsztype);
511 ds_seat_set_wm_cursor(seat, wnd->display->cursor[ctype]);
512
513 (void) ds_window_repaint_preview(wnd, NULL);
514}
515
516/** Finish resizing a window by mouse drag.
517 *
518 * @param wnd Window
519 * @param pos Position where mouse button was released
520 */
521static void ds_window_finish_resize(ds_window_t *wnd, gfx_coord2_t *pos)
522{
523 gfx_coord2_t dresize;
524 gfx_rect_t nrect;
525 ds_seat_t *seat;
526
527 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_finish_resize (%d, %d)",
528 (int) pos->x, (int) pos->y);
529
530 assert(wnd->state == dsw_resizing);
531 gfx_coord2_subtract(pos, &wnd->orig_pos, &dresize);
532
533 /* Compute new rectangle */
534 ds_window_calc_resize(wnd, &dresize, &nrect);
535
536 wnd->state = dsw_idle;
537 ds_client_post_resize_event(wnd->client, wnd, &nrect);
538
539 // TODO Multi-seat: Need to know which seat started the resize!
540 seat = ds_display_first_seat(wnd->display);
541 ds_seat_set_wm_cursor(seat, NULL);
542
543 (void) ds_display_paint(wnd->display, NULL);
544}
545
546/** Update window position when resizing by mouse drag.
547 *
548 * @param wnd Window
549 * @param pos Current mouse position
550 */
551static void ds_window_update_resize(ds_window_t *wnd, gfx_coord2_t *pos)
552{
553 gfx_coord2_t dresize;
554 gfx_rect_t nrect;
555 gfx_rect_t old_rect;
556
557 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_update_resize (%d, %d)",
558 (int) pos->x, (int) pos->y);
559
560 assert(wnd->state == dsw_resizing);
561
562 gfx_coord2_subtract(pos, &wnd->orig_pos, &dresize);
563 ds_window_calc_resize(wnd, &dresize, &nrect);
564
565 ds_window_get_preview_rect(wnd, &old_rect);
566 wnd->preview_rect = nrect;
567 (void) ds_window_repaint_preview(wnd, &old_rect);
568}
569
570/** Post keyboard event to window.
571 *
572 * @param wnd Window
573 * @param event Event
574 *
575 * @return EOK on success or an error code
576 */
577errno_t ds_window_post_kbd_event(ds_window_t *wnd, kbd_event_t *event)
578{
579 bool alt_or_shift;
580
581 alt_or_shift = event->mods & (KM_SHIFT | KM_ALT);
582
583 if (event->type == KEY_PRESS && alt_or_shift && event->key == KC_F4) {
584 /* On Alt-F4 or Shift-F4 send close event to the window */
585 ds_client_post_close_event(wnd->client, wnd);
586 return EOK;
587 }
588
589 return ds_client_post_kbd_event(wnd->client, wnd, event);
590}
591
592/** Post position event to window.
593 *
594 * @param wnd Window
595 * @param event Position event
596 *
597 * @return EOK on success or an error code
598 */
599errno_t ds_window_post_pos_event(ds_window_t *wnd, pos_event_t *event)
600{
601 pos_event_t tevent;
602 gfx_coord2_t pos;
603 gfx_rect_t drect;
604 bool inside;
605
606 log_msg(LOG_DEFAULT, LVL_DEBUG,
607 "ds_window_post_pos_event type=%d pos=%d,%d", event->type,
608 (int) event->hpos, (int) event->vpos);
609
610 pos.x = event->hpos;
611 pos.y = event->vpos;
612 gfx_rect_translate(&wnd->dpos, &wnd->rect, &drect);
613 inside = gfx_pix_inside_rect(&pos, &drect);
614
615 if (event->type == POS_PRESS && event->btn_num == 2 && inside &&
616 (wnd->flags & wndf_maximized) == 0) {
617 ds_window_start_move(wnd, &pos);
618 return EOK;
619 }
620
621 if (event->type == POS_RELEASE) {
622 if (wnd->state == dsw_moving) {
623 ds_window_finish_move(wnd, &pos);
624 return EOK;
625 }
626
627 if (wnd->state == dsw_resizing) {
628 ds_window_finish_resize(wnd, &pos);
629 return EOK;
630 }
631 }
632
633 if (event->type == POS_UPDATE) {
634 if (wnd->state == dsw_moving) {
635 ds_window_update_move(wnd, &pos);
636 return EOK;
637 }
638
639 if (wnd->state == dsw_resizing) {
640 ds_window_update_resize(wnd, &pos);
641 return EOK;
642 }
643 }
644
645 /* Transform event coordinates to window-local */
646 tevent = *event;
647 tevent.hpos -= wnd->dpos.x;
648 tevent.vpos -= wnd->dpos.y;
649
650 return ds_client_post_pos_event(wnd->client, wnd, &tevent);
651}
652
653/** Post focus event to window.
654 *
655 * @param wnd Window
656 * @return EOK on success or an error code
657 */
658errno_t ds_window_post_focus_event(ds_window_t *wnd)
659{
660 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_post_focus_event");
661
662 return ds_client_post_focus_event(wnd->client, wnd);
663}
664
665/** Post unfocus event to window.
666 *
667 * @param wnd Window
668 * @return EOK on success or an error code
669 */
670errno_t ds_window_post_unfocus_event(ds_window_t *wnd)
671{
672 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_post_unfocus_event");
673
674 return ds_client_post_unfocus_event(wnd->client, wnd);
675}
676
677/** Start moving a window, detected by client.
678 *
679 * @param wnd Window
680 * @param pos Position where the pointer was when the move started
681 * relative to the window
682 * @param event Button press event
683 */
684void ds_window_move_req(ds_window_t *wnd, gfx_coord2_t *pos)
685{
686 gfx_coord2_t orig_pos;
687
688 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_move_req (%d, %d)",
689 (int) pos->x, (int) pos->y);
690
691 gfx_coord2_add(&wnd->dpos, pos, &orig_pos);
692 ds_window_start_move(wnd, &orig_pos);
693}
694
695/** Move window.
696 *
697 * @param wnd Window
698 */
699void ds_window_move(ds_window_t *wnd, gfx_coord2_t *dpos)
700{
701 wnd->dpos = *dpos;
702 (void) ds_display_paint(wnd->display, NULL);
703}
704
705/** Get window position.
706 *
707 * @param wnd Window
708 */
709void ds_window_get_pos(ds_window_t *wnd, gfx_coord2_t *dpos)
710{
711 *dpos = wnd->dpos;
712}
713
714/** Get maximized window rectangle.
715 *
716 * @param wnd Window
717 */
718void ds_window_get_max_rect(ds_window_t *wnd, gfx_rect_t *rect)
719{
720 *rect = wnd->display->rect;
721}
722
723/** Start resizing a window, detected by client.
724 *
725 * @param wnd Window
726 * @param rsztype Resize type (which part of window is being dragged)
727 * @param pos Position where the pointer was when the resize started
728 * relative to the window
729 * @param event Button press event
730 */
731void ds_window_resize_req(ds_window_t *wnd, display_wnd_rsztype_t rsztype,
732 gfx_coord2_t *pos)
733{
734 gfx_coord2_t orig_pos;
735
736 log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_resize_req (%d, %d, %d)",
737 (int) rsztype, (int) pos->x, (int) pos->y);
738
739 gfx_coord2_add(&wnd->dpos, pos, &orig_pos);
740 ds_window_start_resize(wnd, rsztype, &orig_pos);
741}
742
743/** Resize window.
744 *
745 * @param wnd Window
746 * @return EOK on success or an error code
747 */
748errno_t ds_window_resize(ds_window_t *wnd, gfx_coord2_t *offs,
749 gfx_rect_t *nrect)
750{
751 gfx_context_t *dgc;
752 gfx_bitmap_params_t bparams;
753 gfx_bitmap_t *nbitmap;
754 pixelmap_t npixelmap;
755 gfx_coord2_t dims;
756 gfx_bitmap_alloc_t alloc;
757 gfx_coord2_t ndpos;
758 errno_t rc;
759
760 dgc = ds_display_get_gc(wnd->display);
761 if (dgc != NULL) {
762 gfx_bitmap_params_init(&bparams);
763 bparams.rect = *nrect;
764
765 rc = gfx_bitmap_create(dgc, &bparams, NULL, &nbitmap);
766 if (rc != EOK)
767 return ENOMEM;
768
769 rc = gfx_bitmap_get_alloc(nbitmap, &alloc);
770 if (rc != EOK) {
771 gfx_bitmap_destroy(nbitmap);
772 return ENOMEM;
773 }
774
775 gfx_rect_dims(nrect, &dims);
776 npixelmap.width = dims.x;
777 npixelmap.height = dims.y;
778 npixelmap.data = alloc.pixels;
779
780 /* TODO: Transfer contents within overlap */
781
782 if (wnd->bitmap != NULL)
783 gfx_bitmap_destroy(wnd->bitmap);
784
785 wnd->bitmap = nbitmap;
786 wnd->pixelmap = npixelmap;
787
788 /* Point memory GC to the new bitmap */
789 mem_gc_retarget(wnd->mgc, nrect, &alloc);
790 }
791
792 gfx_coord2_add(&wnd->dpos, offs, &ndpos);
793
794 wnd->dpos = ndpos;
795 wnd->rect = *nrect;
796
797 (void) ds_display_paint(wnd->display, NULL);
798 return EOK;
799}
800
801/** Maximize window.
802 *
803 * @param wnd Window
804 * @return EOK on success or an error code
805 */
806errno_t ds_window_maximize(ds_window_t *wnd)
807{
808 gfx_coord2_t old_dpos;
809 gfx_rect_t old_rect;
810 gfx_coord2_t offs;
811 gfx_rect_t max_rect;
812 gfx_rect_t nrect;
813 errno_t rc;
814
815 /* If already maximized, do nothing and return success. */
816 if ((wnd->flags & wndf_maximized) != 0)
817 return EOK;
818
819 /* Remember the old window rectangle and display position */
820 old_rect = wnd->rect;
821 old_dpos = wnd->dpos;
822
823 ds_window_get_max_rect(wnd, &max_rect);
824
825 /* Keep window contents on the same position on the screen */
826 offs.x = max_rect.p0.x - wnd->dpos.x;
827 offs.y = max_rect.p0.y - wnd->dpos.y;
828
829 /* Maximized window's coordinates will start at 0,0 */
830 gfx_rect_rtranslate(&max_rect.p0, &max_rect, &nrect);
831
832 rc = ds_window_resize(wnd, &offs, &nrect);
833 if (rc != EOK)
834 return rc;
835
836 /* Set window flags, remember normal rectangle */
837 wnd->flags |= wndf_maximized;
838 wnd->normal_rect = old_rect;
839 wnd->normal_dpos = old_dpos;
840
841 return EOK;
842}
843
844/** Unmaximize window.
845 *
846 * @param wnd Window
847 * @return EOK on success or an error code
848 */
849errno_t ds_window_unmaximize(ds_window_t *wnd)
850{
851 gfx_coord2_t offs;
852 errno_t rc;
853
854 /* If not maximized, do nothing and return success. */
855 if ((wnd->flags & wndf_maximized) == 0)
856 return EOK;
857
858 /* Keep window contents on the same position on the screen */
859 offs.x = wnd->normal_dpos.x - wnd->dpos.x;
860 offs.y = wnd->normal_dpos.y - wnd->dpos.y;
861
862 rc = ds_window_resize(wnd, &offs, &wnd->normal_rect);
863 if (rc != EOK)
864 return rc;
865
866 /* Clear maximized flag */
867 wnd->flags &= ~wndf_maximized;
868
869 return EOK;
870}
871
872/** Compute new window rectangle after resize operation.
873 *
874 * @param wnd Window which is being resized (in dsw_resizing state and thus
875 * has rsztype set)
876 * @param dresize Amount by which to resize
877 * @param nrect Place to store new rectangle
878 */
879void ds_window_calc_resize(ds_window_t *wnd, gfx_coord2_t *dresize,
880 gfx_rect_t *nrect)
881{
882 if ((wnd->rsztype & display_wr_top) != 0) {
883 nrect->p0.y = min(wnd->rect.p0.y + dresize->y,
884 wnd->rect.p1.y - wnd->min_size.y);
885 } else {
886 nrect->p0.y = wnd->rect.p0.y;
887 }
888
889 if ((wnd->rsztype & display_wr_left) != 0) {
890 nrect->p0.x = min(wnd->rect.p0.x + dresize->x,
891 wnd->rect.p1.x - wnd->min_size.x);
892 } else {
893 nrect->p0.x = wnd->rect.p0.x;
894 }
895
896 if ((wnd->rsztype & display_wr_bottom) != 0) {
897 nrect->p1.y = max(wnd->rect.p1.y + dresize->y,
898 wnd->rect.p0.y + wnd->min_size.y);
899 } else {
900 nrect->p1.y = wnd->rect.p1.y;
901 }
902
903 if ((wnd->rsztype & display_wr_right) != 0) {
904 nrect->p1.x = max(wnd->rect.p1.x + dresize->x,
905 wnd->rect.p0.x + wnd->min_size.x);
906 } else {
907 nrect->p1.x = wnd->rect.p1.x;
908 }
909}
910
911/** Set window cursor.
912 *
913 * @param wnd Window
914 * @param cursor New cursor
915 * @return EOK on success, EINVAL if @a cursor is invalid
916 */
917errno_t ds_window_set_cursor(ds_window_t *wnd, display_stock_cursor_t cursor)
918{
919 if (cursor >= dcurs_arrow &&
920 cursor < (display_stock_cursor_t) dcurs_limit) {
921 wnd->cursor = wnd->display->cursor[cursor];
922 return EOK;
923 } else {
924 return EINVAL;
925 }
926}
927
928/** Set window caption.
929 *
930 * @param wnd Window
931 * @param caption New caption
932 *
933 * @return EOK on success, EINVAL if @a cursor is invalid
934 */
935errno_t ds_window_set_caption(ds_window_t *wnd, const char *caption)
936{
937 char *dcaption;
938 ds_wmclient_t *wmclient;
939
940 dcaption = str_dup(caption);
941 if (dcaption == NULL)
942 return ENOMEM;
943
944 free(wnd->caption);
945 wnd->caption = dcaption;
946
947 /* Notify window managers about window information change */
948 wmclient = ds_display_first_wmclient(wnd->display);
949 while (wmclient != NULL) {
950 ds_wmclient_post_wnd_changed_event(wmclient, wnd->id);
951 wmclient = ds_display_next_wmclient(wmclient);
952 }
953
954 return EOK;
955}
956
957/** Window memory GC invalidate callback.
958 *
959 * This is called by the window's memory GC when a rectangle is modified.
960 */
961static void ds_window_invalidate_cb(void *arg, gfx_rect_t *rect)
962{
963 ds_window_t *wnd = (ds_window_t *)arg;
964 gfx_rect_t drect;
965
966 /* Repaint the corresponding part of the display */
967
968 gfx_rect_translate(&wnd->dpos, rect, &drect);
969 ds_display_lock(wnd->display);
970 (void) ds_display_paint(wnd->display, &drect);
971 ds_display_unlock(wnd->display);
972}
973
974/** Window memory GC update callback.
975 *
976 * This is called by the window's memory GC when it is to be updated.
977 */
978static void ds_window_update_cb(void *arg)
979{
980 ds_window_t *wnd = (ds_window_t *)arg;
981
982 (void) wnd;
983}
984
985/** @}
986 */
Note: See TracBrowser for help on using the repository browser.