[c8cf261] | 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 | /**
|
---|
[4d8002d] | 33 | * @file Display server window
|
---|
[c8cf261] | 34 | */
|
---|
| 35 |
|
---|
[0008c0f] | 36 | #include <gfx/bitmap.h>
|
---|
[c8cf261] | 37 | #include <gfx/color.h>
|
---|
[0008c0f] | 38 | #include <gfx/coord.h>
|
---|
[c8cf261] | 39 | #include <gfx/context.h>
|
---|
| 40 | #include <gfx/render.h>
|
---|
| 41 | #include <io/log.h>
|
---|
[946a666] | 42 | #include <io/pixelmap.h>
|
---|
[9b502dd] | 43 | #include <macros.h>
|
---|
[dbef30f] | 44 | #include <memgfx/memgc.h>
|
---|
[c8cf261] | 45 | #include <stdlib.h>
|
---|
[b3c185b6] | 46 | #include "client.h"
|
---|
[6af4b4f] | 47 | #include "display.h"
|
---|
[9901f267] | 48 | #include "seat.h"
|
---|
[6af4b4f] | 49 | #include "window.h"
|
---|
[c8cf261] | 50 |
|
---|
[dbef30f] | 51 | static void ds_window_update_cb(void *, gfx_rect_t *);
|
---|
[6301a24f] | 52 | static void ds_window_get_preview_rect(ds_window_t *, gfx_rect_t *);
|
---|
[0008c0f] | 53 |
|
---|
[6af4b4f] | 54 | /** Create window.
|
---|
[c8cf261] | 55 | *
|
---|
| 56 | * Create graphics context for rendering into a window.
|
---|
| 57 | *
|
---|
[8e9781f] | 58 | * @param client Client owning the window
|
---|
[3434233] | 59 | * @param params Window parameters
|
---|
[c8cf261] | 60 | * @param rgc Place to store pointer to new GC.
|
---|
| 61 | *
|
---|
| 62 | * @return EOK on success or an error code
|
---|
| 63 | */
|
---|
[3434233] | 64 | errno_t ds_window_create(ds_client_t *client, display_wnd_params_t *params,
|
---|
| 65 | ds_window_t **rgc)
|
---|
[c8cf261] | 66 | {
|
---|
[6af4b4f] | 67 | ds_window_t *wnd = NULL;
|
---|
[946a666] | 68 | gfx_context_t *dgc;
|
---|
| 69 | gfx_coord2_t dims;
|
---|
| 70 | gfx_bitmap_params_t bparams;
|
---|
| 71 | gfx_bitmap_alloc_t alloc;
|
---|
[c8cf261] | 72 | errno_t rc;
|
---|
| 73 |
|
---|
[6af4b4f] | 74 | wnd = calloc(1, sizeof(ds_window_t));
|
---|
| 75 | if (wnd == NULL) {
|
---|
[c8cf261] | 76 | rc = ENOMEM;
|
---|
| 77 | goto error;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[b3c185b6] | 80 | ds_client_add_window(client, wnd);
|
---|
[fd777a2] | 81 | ds_display_add_window(client->display, wnd);
|
---|
[6af4b4f] | 82 |
|
---|
[a8eed5f] | 83 | gfx_bitmap_params_init(&bparams);
|
---|
[946a666] | 84 | bparams.rect = params->rect;
|
---|
| 85 |
|
---|
| 86 | dgc = ds_display_get_gc(wnd->display); // XXX
|
---|
| 87 | if (dgc != NULL) {
|
---|
| 88 | rc = gfx_bitmap_create(dgc, &bparams, NULL, &wnd->bitmap);
|
---|
| 89 | if (rc != EOK)
|
---|
| 90 | goto error;
|
---|
| 91 |
|
---|
| 92 | rc = gfx_bitmap_get_alloc(wnd->bitmap, &alloc);
|
---|
| 93 | if (rc != EOK)
|
---|
| 94 | goto error;
|
---|
| 95 |
|
---|
[0e6e77f] | 96 | gfx_rect_dims(¶ms->rect, &dims);
|
---|
[946a666] | 97 | wnd->pixelmap.width = dims.x;
|
---|
| 98 | wnd->pixelmap.height = dims.y;
|
---|
| 99 | wnd->pixelmap.data = alloc.pixels;
|
---|
[dbef30f] | 100 | } else {
|
---|
| 101 | /* This is just for unit tests */
|
---|
| 102 | gfx_rect_dims(¶ms->rect, &dims);
|
---|
| 103 | alloc.pitch = dims.x * sizeof(uint32_t);
|
---|
| 104 | alloc.off0 = 0;
|
---|
| 105 | alloc.pixels = calloc(1, alloc.pitch * dims.y);
|
---|
[946a666] | 106 | }
|
---|
| 107 |
|
---|
[dbef30f] | 108 | rc = mem_gc_create(¶ms->rect, &alloc, ds_window_update_cb,
|
---|
| 109 | (void *)wnd, &wnd->mgc);
|
---|
| 110 | if (rc != EOK)
|
---|
| 111 | goto error;
|
---|
| 112 |
|
---|
[3434233] | 113 | wnd->rect = params->rect;
|
---|
[9b502dd] | 114 | wnd->min_size = params->min_size;
|
---|
[dbef30f] | 115 | wnd->gc = mem_gc_get_ctx(wnd->mgc);
|
---|
[9242ad9] | 116 | wnd->cursor = wnd->display->cursor[dcurs_arrow];
|
---|
[6af4b4f] | 117 | *rgc = wnd;
|
---|
[c8cf261] | 118 | return EOK;
|
---|
| 119 | error:
|
---|
[946a666] | 120 | if (wnd != NULL) {
|
---|
| 121 | if (wnd->bitmap != NULL)
|
---|
| 122 | gfx_bitmap_destroy(wnd->bitmap);
|
---|
[6af4b4f] | 123 | free(wnd);
|
---|
[946a666] | 124 | }
|
---|
| 125 |
|
---|
[c8cf261] | 126 | return rc;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[0e6e77f] | 129 | /** Destroy window.
|
---|
[c8cf261] | 130 | *
|
---|
[0e6e77f] | 131 | * @param wnd Window
|
---|
[c8cf261] | 132 | */
|
---|
[da412547] | 133 | void ds_window_destroy(ds_window_t *wnd)
|
---|
[c8cf261] | 134 | {
|
---|
[cc90846] | 135 | ds_display_t *disp;
|
---|
| 136 |
|
---|
| 137 | disp = wnd->display;
|
---|
| 138 |
|
---|
[b3c185b6] | 139 | ds_client_remove_window(wnd);
|
---|
[fd777a2] | 140 | ds_display_remove_window(wnd);
|
---|
[cc90846] | 141 |
|
---|
[dbef30f] | 142 | mem_gc_delete(wnd->mgc);
|
---|
| 143 |
|
---|
[946a666] | 144 | if (wnd->bitmap != NULL)
|
---|
| 145 | gfx_bitmap_destroy(wnd->bitmap);
|
---|
[c8cf261] | 146 |
|
---|
[6af4b4f] | 147 | free(wnd);
|
---|
[cc90846] | 148 |
|
---|
| 149 | (void) ds_display_paint(disp, NULL);
|
---|
[c8cf261] | 150 | }
|
---|
| 151 |
|
---|
[1a1271d] | 152 | /** Bring window to top.
|
---|
| 153 | *
|
---|
| 154 | * @param wnd Window
|
---|
| 155 | */
|
---|
[b5c7cee] | 156 | void ds_window_bring_to_top(ds_window_t *wnd)
|
---|
| 157 | {
|
---|
| 158 | ds_display_t *disp = wnd->display;
|
---|
| 159 |
|
---|
| 160 | ds_display_remove_window(wnd);
|
---|
| 161 | ds_display_add_window(disp, wnd);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[6af4b4f] | 164 | /** Get generic graphic context from window.
|
---|
[c8cf261] | 165 | *
|
---|
[6af4b4f] | 166 | * @param wnd Window
|
---|
[c8cf261] | 167 | * @return Graphic context
|
---|
| 168 | */
|
---|
[6af4b4f] | 169 | gfx_context_t *ds_window_get_ctx(ds_window_t *wnd)
|
---|
[c8cf261] | 170 | {
|
---|
[6af4b4f] | 171 | return wnd->gc;
|
---|
[c8cf261] | 172 | }
|
---|
| 173 |
|
---|
[2012fe0] | 174 | /** Paint a window using its backing bitmap.
|
---|
[946a666] | 175 | *
|
---|
[2012fe0] | 176 | * @param wnd Window to paint
|
---|
| 177 | * @param rect Display rectangle to paint to
|
---|
[946a666] | 178 | * @return EOK on success or an error code
|
---|
| 179 | */
|
---|
[2012fe0] | 180 | errno_t ds_window_paint(ds_window_t *wnd, gfx_rect_t *rect)
|
---|
[946a666] | 181 | {
|
---|
[2012fe0] | 182 | gfx_rect_t srect;
|
---|
| 183 | gfx_rect_t *brect;
|
---|
| 184 | gfx_rect_t crect;
|
---|
| 185 |
|
---|
| 186 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_paint");
|
---|
| 187 |
|
---|
| 188 | if (rect != NULL) {
|
---|
| 189 | gfx_rect_rtranslate(&wnd->dpos, rect, &srect);
|
---|
| 190 |
|
---|
| 191 | /* Determine if we have anything to do */
|
---|
[01c2759] | 192 | gfx_rect_clip(&srect, &wnd->rect, &crect);
|
---|
[2012fe0] | 193 | if (gfx_rect_is_empty(&crect))
|
---|
| 194 | return EOK;
|
---|
| 195 |
|
---|
| 196 | brect = &srect;
|
---|
| 197 | } else {
|
---|
| 198 | brect = NULL;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[f5191b4] | 201 | /* This can happen in unit tests */
|
---|
| 202 | if (wnd->bitmap == NULL)
|
---|
| 203 | return EOK;
|
---|
| 204 |
|
---|
[2012fe0] | 205 | return gfx_bitmap_render(wnd->bitmap, brect, &wnd->dpos);
|
---|
[946a666] | 206 | }
|
---|
| 207 |
|
---|
[6301a24f] | 208 | /** Get the preview rectangle for a window.
|
---|
| 209 | *
|
---|
| 210 | * Get the preview rectangle if the window is being resized or moved.
|
---|
| 211 | * If the window is not being resized or moved, return an empty rectangle.
|
---|
[a40ae0d] | 212 | *
|
---|
| 213 | * @param wnd Window
|
---|
[6301a24f] | 214 | * @param rect Place to store preview rectangle
|
---|
[a40ae0d] | 215 | */
|
---|
[6301a24f] | 216 | static void ds_window_get_preview_rect(ds_window_t *wnd, gfx_rect_t *rect)
|
---|
| 217 | {
|
---|
| 218 | switch (wnd->state) {
|
---|
| 219 | case dsw_idle:
|
---|
| 220 | break;
|
---|
| 221 | case dsw_moving:
|
---|
| 222 | gfx_rect_translate(&wnd->preview_pos, &wnd->rect, rect);
|
---|
| 223 | return;
|
---|
| 224 | case dsw_resizing:
|
---|
| 225 | gfx_rect_translate(&wnd->dpos, &wnd->preview_rect, rect);
|
---|
| 226 | return;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | rect->p0.x = 0;
|
---|
| 230 | rect->p0.y = 0;
|
---|
| 231 | rect->p1.x = 0;
|
---|
| 232 | rect->p1.y = 0;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | /** Paint window preview if the window is being moved or resized.
|
---|
| 236 | *
|
---|
| 237 | * If the window is not being resized or moved, take no action and return
|
---|
| 238 | * success.
|
---|
| 239 | *
|
---|
| 240 | * @param wnd Window for which to paint preview
|
---|
| 241 | * @param rect Clipping rectangle
|
---|
| 242 | * @return EOK on success or an error code
|
---|
| 243 | */
|
---|
| 244 | errno_t ds_window_paint_preview(ds_window_t *wnd, gfx_rect_t *rect)
|
---|
[a40ae0d] | 245 | {
|
---|
[6301a24f] | 246 | errno_t rc;
|
---|
[1b443cc0] | 247 | gfx_color_t *color;
|
---|
[6301a24f] | 248 | gfx_rect_t prect;
|
---|
[1b443cc0] | 249 | gfx_rect_t drect;
|
---|
[6301a24f] | 250 | gfx_context_t *gc;
|
---|
[1b443cc0] | 251 |
|
---|
[6301a24f] | 252 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_paint_preview");
|
---|
[a40ae0d] | 253 |
|
---|
[6301a24f] | 254 | /*
|
---|
| 255 | * Get preview rectangle. If the window is not being resized/moved,
|
---|
| 256 | * we should get an empty rectangle.
|
---|
| 257 | */
|
---|
| 258 | ds_window_get_preview_rect(wnd, &prect);
|
---|
| 259 | if (gfx_rect_is_empty(&prect)) {
|
---|
| 260 | /* There is nothing to paint */
|
---|
| 261 | return EOK;
|
---|
| 262 | }
|
---|
[a40ae0d] | 263 |
|
---|
[6301a24f] | 264 | /* Clip rendering to the clipping rectangle */
|
---|
| 265 | gfx_rect_clip(&prect, rect, &drect);
|
---|
[1b443cc0] | 266 |
|
---|
| 267 | rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color);
|
---|
| 268 | if (rc != EOK)
|
---|
[6301a24f] | 269 | return rc;
|
---|
[1b443cc0] | 270 |
|
---|
| 271 | gc = ds_display_get_gc(wnd->display); // XXX
|
---|
| 272 | if (gc != NULL) {
|
---|
| 273 | gfx_set_color(gc, color);
|
---|
| 274 | gfx_fill_rect(gc, &drect);
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | gfx_color_delete(color);
|
---|
[6301a24f] | 278 | return EOK;
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | /** Repaint window preview when resizing or moving.
|
---|
| 282 | *
|
---|
| 283 | * Repaint the window preview wich was previously at rectangle @a old_rect.
|
---|
| 284 | * The current preview rectangle is determined from window state. If
|
---|
| 285 | * the window did not previously have a preview, @a old_rect should point
|
---|
| 286 | * to an empty rectangle or be NULL. When window has finished
|
---|
| 287 | * moving or resizing, the preview will be cleared.
|
---|
| 288 | *
|
---|
| 289 | * @param wnd Window for which to paint preview
|
---|
| 290 | * @param rect Clipping rectangle
|
---|
| 291 | * @return EOK on success or an error code
|
---|
| 292 | */
|
---|
| 293 | static errno_t ds_window_repaint_preview(ds_window_t *wnd, gfx_rect_t *old_rect)
|
---|
| 294 | {
|
---|
| 295 | errno_t rc;
|
---|
| 296 | gfx_rect_t prect;
|
---|
| 297 | gfx_rect_t envelope;
|
---|
| 298 | bool oldr;
|
---|
| 299 | bool newr;
|
---|
| 300 |
|
---|
| 301 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_repaint_preview");
|
---|
| 302 |
|
---|
| 303 | /*
|
---|
| 304 | * Get current preview rectangle. If the window is not being resized/moved,
|
---|
| 305 | * we should get an empty rectangle.
|
---|
| 306 | */
|
---|
| 307 | ds_window_get_preview_rect(wnd, &prect);
|
---|
| 308 |
|
---|
| 309 | oldr = (old_rect != NULL) && !gfx_rect_is_empty(old_rect);
|
---|
| 310 | newr = !gfx_rect_is_empty(&prect);
|
---|
| 311 |
|
---|
| 312 | if (oldr && newr && gfx_rect_is_incident(old_rect, &prect)) {
|
---|
| 313 | /*
|
---|
| 314 | * As an optimization, repaint both rectangles in a single
|
---|
| 315 | * operation.
|
---|
| 316 | */
|
---|
| 317 |
|
---|
| 318 | gfx_rect_envelope(old_rect, &prect, &envelope);
|
---|
| 319 |
|
---|
| 320 | rc = ds_display_paint(wnd->display, &envelope);
|
---|
| 321 | if (rc != EOK)
|
---|
| 322 | return rc;
|
---|
| 323 | } else {
|
---|
| 324 | /* Repaint each rectangle separately */
|
---|
| 325 | if (oldr) {
|
---|
| 326 | rc = ds_display_paint(wnd->display, old_rect);
|
---|
| 327 | if (rc != EOK)
|
---|
| 328 | return rc;
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | if (newr) {
|
---|
| 332 | rc = ds_display_paint(wnd->display, &prect);
|
---|
| 333 | if (rc != EOK)
|
---|
| 334 | return rc;
|
---|
| 335 | }
|
---|
| 336 | }
|
---|
| 337 |
|
---|
| 338 | return EOK;
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | /** Start moving a window by mouse drag.
|
---|
| 342 | *
|
---|
| 343 | * @param wnd Window
|
---|
| 344 | * @param pos Position where mouse button was pressed
|
---|
| 345 | */
|
---|
| 346 | static void ds_window_start_move(ds_window_t *wnd, gfx_coord2_t *pos)
|
---|
| 347 | {
|
---|
| 348 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_start_move (%d, %d)",
|
---|
| 349 | (int) pos->x, (int) pos->y);
|
---|
| 350 |
|
---|
| 351 | if (wnd->state != dsw_idle)
|
---|
| 352 | return;
|
---|
| 353 |
|
---|
| 354 | wnd->orig_pos = *pos;
|
---|
| 355 | wnd->state = dsw_moving;
|
---|
| 356 | wnd->preview_pos = wnd->dpos;
|
---|
| 357 |
|
---|
| 358 | (void) ds_window_repaint_preview(wnd, NULL);
|
---|
[a40ae0d] | 359 | }
|
---|
| 360 |
|
---|
| 361 | /** Finish moving a window by mouse drag.
|
---|
| 362 | *
|
---|
| 363 | * @param wnd Window
|
---|
[76a02db] | 364 | * @param pos Position where mouse button was released
|
---|
[a40ae0d] | 365 | */
|
---|
[76a02db] | 366 | static void ds_window_finish_move(ds_window_t *wnd, gfx_coord2_t *pos)
|
---|
[a40ae0d] | 367 | {
|
---|
| 368 | gfx_coord2_t dmove;
|
---|
| 369 | gfx_coord2_t nwpos;
|
---|
[6301a24f] | 370 | gfx_rect_t old_rect;
|
---|
[a40ae0d] | 371 |
|
---|
| 372 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_finish_move (%d, %d)",
|
---|
[76a02db] | 373 | (int) pos->x, (int) pos->y);
|
---|
[a40ae0d] | 374 |
|
---|
[5f483be] | 375 | assert(wnd->state == dsw_moving);
|
---|
[a2e104e] | 376 |
|
---|
[76a02db] | 377 | gfx_coord2_subtract(pos, &wnd->orig_pos, &dmove);
|
---|
[a40ae0d] | 378 | gfx_coord2_add(&wnd->dpos, &dmove, &nwpos);
|
---|
[6301a24f] | 379 |
|
---|
| 380 | ds_window_get_preview_rect(wnd, &old_rect);
|
---|
| 381 |
|
---|
[a40ae0d] | 382 | wnd->dpos = nwpos;
|
---|
| 383 | wnd->state = dsw_idle;
|
---|
[946a666] | 384 |
|
---|
[2012fe0] | 385 | (void) ds_display_paint(wnd->display, NULL);
|
---|
[a40ae0d] | 386 | }
|
---|
| 387 |
|
---|
| 388 | /** Update window position when moving by mouse drag.
|
---|
| 389 | *
|
---|
| 390 | * @param wnd Window
|
---|
[76a02db] | 391 | * @param pos Current mouse position
|
---|
[a40ae0d] | 392 | */
|
---|
[76a02db] | 393 | static void ds_window_update_move(ds_window_t *wnd, gfx_coord2_t *pos)
|
---|
[a40ae0d] | 394 | {
|
---|
| 395 | gfx_coord2_t dmove;
|
---|
| 396 | gfx_coord2_t nwpos;
|
---|
[6301a24f] | 397 | gfx_rect_t old_rect;
|
---|
[a40ae0d] | 398 |
|
---|
| 399 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_update_move (%d, %d)",
|
---|
[76a02db] | 400 | (int) pos->x, (int) pos->y);
|
---|
[a40ae0d] | 401 |
|
---|
[5f483be] | 402 | assert(wnd->state == dsw_moving);
|
---|
[a2e104e] | 403 |
|
---|
[76a02db] | 404 | gfx_coord2_subtract(pos, &wnd->orig_pos, &dmove);
|
---|
[a40ae0d] | 405 | gfx_coord2_add(&wnd->dpos, &dmove, &nwpos);
|
---|
[c79545e] | 406 |
|
---|
[6301a24f] | 407 | ds_window_get_preview_rect(wnd, &old_rect);
|
---|
| 408 | wnd->preview_pos = nwpos;
|
---|
[a40ae0d] | 409 |
|
---|
[6301a24f] | 410 | (void) ds_window_repaint_preview(wnd, &old_rect);
|
---|
[a40ae0d] | 411 | }
|
---|
| 412 |
|
---|
[76a02db] | 413 | /** Start resizing a window by mouse drag.
|
---|
| 414 | *
|
---|
| 415 | * @param wnd Window
|
---|
| 416 | * @param rsztype Resize type (which part of window is being dragged)
|
---|
| 417 | * @param pos Position where mouse button was pressed
|
---|
| 418 | */
|
---|
| 419 | static void ds_window_start_resize(ds_window_t *wnd,
|
---|
| 420 | display_wnd_rsztype_t rsztype, gfx_coord2_t *pos)
|
---|
| 421 | {
|
---|
[9901f267] | 422 | ds_seat_t *seat;
|
---|
| 423 | display_stock_cursor_t ctype;
|
---|
| 424 |
|
---|
[76a02db] | 425 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_start_resize (%d, %d)",
|
---|
| 426 | (int) pos->x, (int) pos->y);
|
---|
| 427 |
|
---|
| 428 | if (wnd->state != dsw_idle)
|
---|
| 429 | return;
|
---|
| 430 |
|
---|
| 431 | wnd->orig_pos = *pos;
|
---|
| 432 | wnd->state = dsw_resizing;
|
---|
| 433 | wnd->rsztype = rsztype;
|
---|
[71eff34] | 434 | wnd->preview_rect = wnd->rect;
|
---|
[9901f267] | 435 |
|
---|
| 436 | // XXX Need client to tell us which seat started the resize!
|
---|
| 437 | seat = ds_display_first_seat(wnd->display);
|
---|
| 438 | ctype = display_cursor_from_wrsz(rsztype);
|
---|
| 439 | ds_seat_set_wm_cursor(seat, wnd->display->cursor[ctype]);
|
---|
[6301a24f] | 440 |
|
---|
| 441 | (void) ds_window_repaint_preview(wnd, NULL);
|
---|
[76a02db] | 442 | }
|
---|
| 443 |
|
---|
[e022819] | 444 | /** Finish resizing a window by mouse drag.
|
---|
| 445 | *
|
---|
| 446 | * @param wnd Window
|
---|
[76a02db] | 447 | * @param pos Position where mouse button was released
|
---|
[e022819] | 448 | */
|
---|
[76a02db] | 449 | static void ds_window_finish_resize(ds_window_t *wnd, gfx_coord2_t *pos)
|
---|
[e022819] | 450 | {
|
---|
| 451 | gfx_coord2_t dresize;
|
---|
| 452 | gfx_rect_t nrect;
|
---|
[9901f267] | 453 | ds_seat_t *seat;
|
---|
[e022819] | 454 |
|
---|
| 455 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_finish_resize (%d, %d)",
|
---|
[76a02db] | 456 | (int) pos->x, (int) pos->y);
|
---|
[e022819] | 457 |
|
---|
[5f483be] | 458 | assert(wnd->state == dsw_resizing);
|
---|
[76a02db] | 459 | gfx_coord2_subtract(pos, &wnd->orig_pos, &dresize);
|
---|
[e022819] | 460 |
|
---|
| 461 | /* Compute new rectangle */
|
---|
| 462 | ds_window_calc_resize(wnd, &dresize, &nrect);
|
---|
| 463 |
|
---|
| 464 | wnd->state = dsw_idle;
|
---|
| 465 | ds_client_post_resize_event(wnd->client, wnd, &nrect);
|
---|
[9901f267] | 466 |
|
---|
| 467 | // XXX Need to know which seat started the resize!
|
---|
| 468 | seat = ds_display_first_seat(wnd->display);
|
---|
| 469 | ds_seat_set_wm_cursor(seat, NULL);
|
---|
[6301a24f] | 470 |
|
---|
| 471 | (void) ds_display_paint(wnd->display, NULL);
|
---|
[e022819] | 472 | }
|
---|
| 473 |
|
---|
| 474 | /** Update window position when resizing by mouse drag.
|
---|
| 475 | *
|
---|
| 476 | * @param wnd Window
|
---|
[76a02db] | 477 | * @param pos Current mouse position
|
---|
[e022819] | 478 | */
|
---|
[76a02db] | 479 | static void ds_window_update_resize(ds_window_t *wnd, gfx_coord2_t *pos)
|
---|
[e022819] | 480 | {
|
---|
| 481 | gfx_coord2_t dresize;
|
---|
| 482 | gfx_rect_t nrect;
|
---|
[6301a24f] | 483 | gfx_rect_t old_rect;
|
---|
[e022819] | 484 |
|
---|
| 485 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_update_resize (%d, %d)",
|
---|
[76a02db] | 486 | (int) pos->x, (int) pos->y);
|
---|
[e022819] | 487 |
|
---|
[5f483be] | 488 | assert(wnd->state == dsw_resizing);
|
---|
[e022819] | 489 |
|
---|
[76a02db] | 490 | gfx_coord2_subtract(pos, &wnd->orig_pos, &dresize);
|
---|
[e022819] | 491 | ds_window_calc_resize(wnd, &dresize, &nrect);
|
---|
| 492 |
|
---|
[6301a24f] | 493 | ds_window_get_preview_rect(wnd, &old_rect);
|
---|
| 494 | wnd->preview_rect = nrect;
|
---|
| 495 | (void) ds_window_repaint_preview(wnd, &old_rect);
|
---|
[e022819] | 496 | }
|
---|
| 497 |
|
---|
[338d0935] | 498 | /** Post keyboard event to window.
|
---|
| 499 | *
|
---|
| 500 | * @param wnd Window
|
---|
| 501 | * @param event Event
|
---|
| 502 | *
|
---|
| 503 | * @return EOK on success or an error code
|
---|
| 504 | */
|
---|
| 505 | errno_t ds_window_post_kbd_event(ds_window_t *wnd, kbd_event_t *event)
|
---|
| 506 | {
|
---|
| 507 | bool alt_or_shift;
|
---|
| 508 |
|
---|
| 509 | alt_or_shift = event->mods & (KM_SHIFT | KM_ALT);
|
---|
| 510 |
|
---|
| 511 | if (event->type == KEY_PRESS && alt_or_shift && event->key == KC_F4) {
|
---|
| 512 | /* On Alt-F4 or Shift-F4 send close event to the window */
|
---|
| 513 | ds_client_post_close_event(wnd->client, wnd);
|
---|
| 514 | return EOK;
|
---|
| 515 | }
|
---|
| 516 |
|
---|
| 517 | return ds_client_post_kbd_event(wnd->client, wnd, event);
|
---|
| 518 | }
|
---|
| 519 |
|
---|
[a40ae0d] | 520 | /** Post position event to window.
|
---|
| 521 | *
|
---|
| 522 | * @param wnd Window
|
---|
| 523 | * @param event Position event
|
---|
| 524 | */
|
---|
| 525 | errno_t ds_window_post_pos_event(ds_window_t *wnd, pos_event_t *event)
|
---|
| 526 | {
|
---|
[f7fb2b21] | 527 | pos_event_t tevent;
|
---|
[a2e104e] | 528 | gfx_coord2_t pos;
|
---|
| 529 | gfx_rect_t drect;
|
---|
| 530 | bool inside;
|
---|
[f7fb2b21] | 531 |
|
---|
[a40ae0d] | 532 | log_msg(LOG_DEFAULT, LVL_DEBUG,
|
---|
| 533 | "ds_window_post_pos_event type=%d pos=%d,%d\n", event->type,
|
---|
| 534 | (int) event->hpos, (int) event->vpos);
|
---|
| 535 |
|
---|
[a2e104e] | 536 | pos.x = event->hpos;
|
---|
| 537 | pos.y = event->vpos;
|
---|
| 538 | gfx_rect_translate(&wnd->dpos, &wnd->rect, &drect);
|
---|
| 539 | inside = gfx_pix_inside_rect(&pos, &drect);
|
---|
[a40ae0d] | 540 |
|
---|
[5f483be] | 541 | if (event->type == POS_PRESS && event->btn_num == 2 && inside) {
|
---|
[76a02db] | 542 | ds_window_start_move(wnd, &pos);
|
---|
[5f483be] | 543 | return EOK;
|
---|
| 544 | }
|
---|
[a40ae0d] | 545 |
|
---|
[e022819] | 546 | if (event->type == POS_RELEASE) {
|
---|
[5f483be] | 547 | if (wnd->state == dsw_moving) {
|
---|
| 548 | ds_window_finish_move(wnd, &pos);
|
---|
| 549 | return EOK;
|
---|
| 550 | }
|
---|
| 551 |
|
---|
| 552 | if (wnd->state == dsw_resizing) {
|
---|
| 553 | ds_window_finish_resize(wnd, &pos);
|
---|
| 554 | return EOK;
|
---|
| 555 | }
|
---|
[e022819] | 556 | }
|
---|
[a2e104e] | 557 |
|
---|
[e022819] | 558 | if (event->type == POS_UPDATE) {
|
---|
[5f483be] | 559 | if (wnd->state == dsw_moving) {
|
---|
| 560 | ds_window_update_move(wnd, &pos);
|
---|
| 561 | return EOK;
|
---|
| 562 | }
|
---|
| 563 |
|
---|
| 564 | if (wnd->state == dsw_resizing) {
|
---|
| 565 | ds_window_update_resize(wnd, &pos);
|
---|
| 566 | return EOK;
|
---|
| 567 | }
|
---|
[e022819] | 568 | }
|
---|
[a40ae0d] | 569 |
|
---|
[f7fb2b21] | 570 | /* Transform event coordinates to window-local */
|
---|
| 571 | tevent = *event;
|
---|
| 572 | tevent.hpos -= wnd->dpos.x;
|
---|
| 573 | tevent.vpos -= wnd->dpos.y;
|
---|
| 574 |
|
---|
| 575 | return ds_client_post_pos_event(wnd->client, wnd, &tevent);
|
---|
[a40ae0d] | 576 | }
|
---|
| 577 |
|
---|
[b0a94854] | 578 | /** Post focus event to window.
|
---|
| 579 | *
|
---|
| 580 | * @param wnd Window
|
---|
| 581 | */
|
---|
| 582 | errno_t ds_window_post_focus_event(ds_window_t *wnd)
|
---|
| 583 | {
|
---|
| 584 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_post_focus_event\n");
|
---|
| 585 |
|
---|
| 586 | return ds_client_post_focus_event(wnd->client, wnd);
|
---|
| 587 | }
|
---|
| 588 |
|
---|
| 589 | /** Post unfocus event to window.
|
---|
| 590 | *
|
---|
| 591 | * @param wnd Window
|
---|
| 592 | */
|
---|
| 593 | errno_t ds_window_post_unfocus_event(ds_window_t *wnd)
|
---|
| 594 | {
|
---|
| 595 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_post_unfocus_event\n");
|
---|
| 596 |
|
---|
| 597 | return ds_client_post_unfocus_event(wnd->client, wnd);
|
---|
| 598 | }
|
---|
| 599 |
|
---|
[e022819] | 600 | /** Start moving a window, detected by client.
|
---|
| 601 | *
|
---|
| 602 | * @param wnd Window
|
---|
| 603 | * @param pos Position where the pointer was when the move started
|
---|
| 604 | * relative to the window
|
---|
| 605 | * @param event Button press event
|
---|
| 606 | */
|
---|
| 607 | void ds_window_move_req(ds_window_t *wnd, gfx_coord2_t *pos)
|
---|
| 608 | {
|
---|
[76a02db] | 609 | gfx_coord2_t orig_pos;
|
---|
| 610 |
|
---|
[e022819] | 611 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_move_req (%d, %d)",
|
---|
| 612 | (int) pos->x, (int) pos->y);
|
---|
| 613 |
|
---|
[76a02db] | 614 | gfx_coord2_add(&wnd->dpos, pos, &orig_pos);
|
---|
| 615 | ds_window_start_move(wnd, &orig_pos);
|
---|
[e022819] | 616 | }
|
---|
| 617 |
|
---|
[0680854] | 618 | /** Move window.
|
---|
| 619 | *
|
---|
| 620 | * @param wnd Window
|
---|
| 621 | */
|
---|
| 622 | void ds_window_move(ds_window_t *wnd, gfx_coord2_t *dpos)
|
---|
| 623 | {
|
---|
| 624 | wnd->dpos = *dpos;
|
---|
| 625 | (void) ds_display_paint(wnd->display, NULL);
|
---|
| 626 | }
|
---|
| 627 |
|
---|
[e022819] | 628 | /** Start resizing a window, detected by client.
|
---|
| 629 | *
|
---|
| 630 | * @param wnd Window
|
---|
| 631 | * @param rsztype Resize type (which part of window is being dragged)
|
---|
| 632 | * @param pos Position where the pointer was when the resize started
|
---|
| 633 | * relative to the window
|
---|
| 634 | * @param event Button press event
|
---|
| 635 | */
|
---|
| 636 | void ds_window_resize_req(ds_window_t *wnd, display_wnd_rsztype_t rsztype,
|
---|
| 637 | gfx_coord2_t *pos)
|
---|
| 638 | {
|
---|
[76a02db] | 639 | gfx_coord2_t orig_pos;
|
---|
| 640 |
|
---|
[e022819] | 641 | log_msg(LOG_DEFAULT, LVL_NOTE, "ds_window_resize_req (%d, %d, %d)",
|
---|
| 642 | (int) rsztype, (int) pos->x, (int) pos->y);
|
---|
| 643 |
|
---|
[76a02db] | 644 | gfx_coord2_add(&wnd->dpos, pos, &orig_pos);
|
---|
| 645 | ds_window_start_resize(wnd, rsztype, &orig_pos);
|
---|
[e022819] | 646 | }
|
---|
| 647 |
|
---|
[0680854] | 648 | /** Resize window.
|
---|
| 649 | *
|
---|
| 650 | * @param wnd Window
|
---|
| 651 | */
|
---|
| 652 | errno_t ds_window_resize(ds_window_t *wnd, gfx_coord2_t *offs,
|
---|
| 653 | gfx_rect_t *nrect)
|
---|
| 654 | {
|
---|
| 655 | gfx_context_t *dgc;
|
---|
| 656 | gfx_bitmap_params_t bparams;
|
---|
| 657 | gfx_bitmap_t *nbitmap;
|
---|
| 658 | pixelmap_t npixelmap;
|
---|
| 659 | gfx_coord2_t dims;
|
---|
| 660 | gfx_bitmap_alloc_t alloc;
|
---|
| 661 | gfx_coord2_t ndpos;
|
---|
| 662 | errno_t rc;
|
---|
| 663 |
|
---|
| 664 | dgc = ds_display_get_gc(wnd->display); // XXX
|
---|
| 665 | if (dgc != NULL) {
|
---|
| 666 | gfx_bitmap_params_init(&bparams);
|
---|
| 667 | bparams.rect = *nrect;
|
---|
| 668 |
|
---|
| 669 | rc = gfx_bitmap_create(dgc, &bparams, NULL, &nbitmap);
|
---|
| 670 | if (rc != EOK)
|
---|
| 671 | return ENOMEM;
|
---|
| 672 |
|
---|
| 673 | rc = gfx_bitmap_get_alloc(nbitmap, &alloc);
|
---|
| 674 | if (rc != EOK) {
|
---|
| 675 | gfx_bitmap_destroy(nbitmap);
|
---|
| 676 | return ENOMEM;
|
---|
| 677 | }
|
---|
| 678 |
|
---|
| 679 | gfx_rect_dims(nrect, &dims);
|
---|
| 680 | npixelmap.width = dims.x;
|
---|
| 681 | npixelmap.height = dims.y;
|
---|
| 682 | npixelmap.data = alloc.pixels;
|
---|
| 683 |
|
---|
| 684 | /* TODO: Transfer contents within overlap */
|
---|
| 685 |
|
---|
| 686 | if (wnd->bitmap != NULL)
|
---|
| 687 | gfx_bitmap_destroy(wnd->bitmap);
|
---|
| 688 |
|
---|
| 689 | wnd->bitmap = nbitmap;
|
---|
| 690 | wnd->pixelmap = npixelmap;
|
---|
[dbef30f] | 691 |
|
---|
| 692 | /* Point memory GC to the new bitmap */
|
---|
| 693 | mem_gc_retarget(wnd->mgc, nrect, &alloc);
|
---|
[0680854] | 694 | }
|
---|
| 695 |
|
---|
| 696 | gfx_coord2_add(&wnd->dpos, offs, &ndpos);
|
---|
| 697 |
|
---|
| 698 | wnd->dpos = ndpos;
|
---|
| 699 | wnd->rect = *nrect;
|
---|
| 700 |
|
---|
| 701 | (void) ds_display_paint(wnd->display, NULL);
|
---|
| 702 | return EOK;
|
---|
| 703 | }
|
---|
| 704 |
|
---|
[e022819] | 705 | /** Compute new window rectangle after resize operation.
|
---|
| 706 | *
|
---|
| 707 | * @param wnd Window which is being resized (in dsw_resizing state and thus
|
---|
| 708 | * has rsztype set)
|
---|
| 709 | * @param dresize Amount by which to resize
|
---|
| 710 | * @param nrect Place to store new rectangle
|
---|
| 711 | */
|
---|
| 712 | void ds_window_calc_resize(ds_window_t *wnd, gfx_coord2_t *dresize,
|
---|
| 713 | gfx_rect_t *nrect)
|
---|
| 714 | {
|
---|
[9b502dd] | 715 | if ((wnd->rsztype & display_wr_top) != 0) {
|
---|
| 716 | nrect->p0.y = min(wnd->rect.p0.y + dresize->y,
|
---|
| 717 | wnd->rect.p1.y - wnd->min_size.y);
|
---|
| 718 | } else {
|
---|
| 719 | nrect->p0.y = wnd->rect.p0.y;
|
---|
| 720 | }
|
---|
| 721 |
|
---|
| 722 | if ((wnd->rsztype & display_wr_left) != 0) {
|
---|
| 723 | nrect->p0.x = min(wnd->rect.p0.x + dresize->x,
|
---|
| 724 | wnd->rect.p1.x - wnd->min_size.x);
|
---|
| 725 | } else {
|
---|
| 726 | nrect->p0.x = wnd->rect.p0.x;
|
---|
| 727 | }
|
---|
| 728 |
|
---|
| 729 | if ((wnd->rsztype & display_wr_bottom) != 0) {
|
---|
| 730 | nrect->p1.y = max(wnd->rect.p1.y + dresize->y,
|
---|
| 731 | wnd->rect.p0.y + wnd->min_size.y);
|
---|
| 732 | } else {
|
---|
| 733 | nrect->p1.y = wnd->rect.p1.y;
|
---|
| 734 | }
|
---|
| 735 |
|
---|
| 736 | if ((wnd->rsztype & display_wr_right) != 0) {
|
---|
| 737 | nrect->p1.x = max(wnd->rect.p1.x + dresize->x,
|
---|
| 738 | wnd->rect.p0.x + wnd->min_size.x);
|
---|
| 739 | } else {
|
---|
| 740 | nrect->p1.x = wnd->rect.p1.x;
|
---|
| 741 | }
|
---|
[e022819] | 742 | }
|
---|
| 743 |
|
---|
[9242ad9] | 744 | /** Set window cursor.
|
---|
| 745 | *
|
---|
| 746 | * @param wnd Window
|
---|
| 747 | * @return EOK on success, EINVAL if @a cursor is invalid
|
---|
| 748 | */
|
---|
| 749 | errno_t ds_window_set_cursor(ds_window_t *wnd, display_stock_cursor_t cursor)
|
---|
| 750 | {
|
---|
| 751 | if (cursor >= dcurs_arrow &&
|
---|
| 752 | cursor < (display_stock_cursor_t) dcurs_limit) {
|
---|
| 753 | wnd->cursor = wnd->display->cursor[cursor];
|
---|
| 754 | return EOK;
|
---|
| 755 | } else {
|
---|
| 756 | return EINVAL;
|
---|
| 757 | }
|
---|
| 758 | }
|
---|
| 759 |
|
---|
[d70e7b7b] | 760 | /** Window memory GC update callback.
|
---|
[dbef30f] | 761 | *
|
---|
| 762 | * This is called by the window's memory GC when a rectangle us updated.
|
---|
| 763 | */
|
---|
| 764 | static void ds_window_update_cb(void *arg, gfx_rect_t *rect)
|
---|
| 765 | {
|
---|
| 766 | ds_window_t *wnd = (ds_window_t *)arg;
|
---|
| 767 | gfx_rect_t drect;
|
---|
| 768 |
|
---|
| 769 | /* Repaint the corresponding part of the display */
|
---|
| 770 | gfx_rect_translate(&wnd->dpos, rect, &drect);
|
---|
| 771 | (void) ds_display_paint(wnd->display, &drect);
|
---|
| 772 | }
|
---|
| 773 |
|
---|
[c8cf261] | 774 | /** @}
|
---|
| 775 | */
|
---|