[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 | /**
|
---|
| 33 | * @file GFX window backend
|
---|
| 34 | *
|
---|
| 35 | * This implements a graphics context over display server window.
|
---|
| 36 | */
|
---|
| 37 |
|
---|
[0008c0f] | 38 | #include <gfx/bitmap.h>
|
---|
[c8cf261] | 39 | #include <gfx/color.h>
|
---|
[0008c0f] | 40 | #include <gfx/coord.h>
|
---|
[c8cf261] | 41 | #include <gfx/context.h>
|
---|
| 42 | #include <gfx/render.h>
|
---|
| 43 | #include <io/log.h>
|
---|
| 44 | #include <stdlib.h>
|
---|
[b3c185b6] | 45 | #include "client.h"
|
---|
[6af4b4f] | 46 | #include "display.h"
|
---|
| 47 | #include "window.h"
|
---|
[c8cf261] | 48 |
|
---|
[6af4b4f] | 49 | static errno_t ds_window_set_color(void *, gfx_color_t *);
|
---|
| 50 | static errno_t ds_window_fill_rect(void *, gfx_rect_t *);
|
---|
[0008c0f] | 51 | static errno_t ds_window_bitmap_create(void *, gfx_bitmap_params_t *,
|
---|
| 52 | gfx_bitmap_alloc_t *, void **);
|
---|
| 53 | static errno_t ds_window_bitmap_destroy(void *);
|
---|
| 54 | static errno_t ds_window_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
|
---|
| 55 | static errno_t ds_window_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
|
---|
[c8cf261] | 56 |
|
---|
[6af4b4f] | 57 | gfx_context_ops_t ds_window_ops = {
|
---|
| 58 | .set_color = ds_window_set_color,
|
---|
[0008c0f] | 59 | .fill_rect = ds_window_fill_rect,
|
---|
| 60 | .bitmap_create = ds_window_bitmap_create,
|
---|
| 61 | .bitmap_destroy = ds_window_bitmap_destroy,
|
---|
| 62 | .bitmap_render = ds_window_bitmap_render,
|
---|
| 63 | .bitmap_get_alloc = ds_window_bitmap_get_alloc
|
---|
[c8cf261] | 64 | };
|
---|
| 65 |
|
---|
| 66 | /** Set color on window GC.
|
---|
| 67 | *
|
---|
| 68 | * Set drawing color on window GC.
|
---|
| 69 | *
|
---|
| 70 | * @param arg Console GC
|
---|
| 71 | * @param color Color
|
---|
| 72 | *
|
---|
| 73 | * @return EOK on success or an error code
|
---|
| 74 | */
|
---|
[6af4b4f] | 75 | static errno_t ds_window_set_color(void *arg, gfx_color_t *color)
|
---|
[c8cf261] | 76 | {
|
---|
[6af4b4f] | 77 | ds_window_t *wnd = (ds_window_t *) arg;
|
---|
[c8cf261] | 78 |
|
---|
[87a7cdb] | 79 | log_msg(LOG_DEFAULT, LVL_NOTE, "gc_set_color gc=%p",
|
---|
| 80 | ds_display_get_gc(wnd->display));
|
---|
| 81 | return gfx_set_color(ds_display_get_gc(wnd->display), color);
|
---|
[c8cf261] | 82 | }
|
---|
| 83 |
|
---|
| 84 | /** Fill rectangle on window GC.
|
---|
| 85 | *
|
---|
[6af4b4f] | 86 | * @param arg Window GC
|
---|
[c8cf261] | 87 | * @param rect Rectangle
|
---|
| 88 | *
|
---|
| 89 | * @return EOK on success or an error code
|
---|
| 90 | */
|
---|
[6af4b4f] | 91 | static errno_t ds_window_fill_rect(void *arg, gfx_rect_t *rect)
|
---|
[c8cf261] | 92 | {
|
---|
[6af4b4f] | 93 | ds_window_t *wnd = (ds_window_t *) arg;
|
---|
[65160d7] | 94 | gfx_rect_t crect;
|
---|
[22faaf2] | 95 | gfx_rect_t drect;
|
---|
[c8cf261] | 96 |
|
---|
| 97 | log_msg(LOG_DEFAULT, LVL_NOTE, "gc_fill_rect");
|
---|
[65160d7] | 98 | gfx_rect_clip(rect, &wnd->rect, &crect);
|
---|
| 99 | gfx_rect_translate(&wnd->dpos, &crect, &drect);
|
---|
[87a7cdb] | 100 | return gfx_fill_rect(ds_display_get_gc(wnd->display), &drect);
|
---|
[c8cf261] | 101 | }
|
---|
| 102 |
|
---|
[0008c0f] | 103 | /** Create bitmap in canvas GC.
|
---|
| 104 | *
|
---|
| 105 | * @param arg Canvas GC
|
---|
| 106 | * @param params Bitmap params
|
---|
| 107 | * @param alloc Bitmap allocation info or @c NULL
|
---|
| 108 | * @param rbm Place to store pointer to new bitmap
|
---|
| 109 | * @return EOK on success or an error code
|
---|
| 110 | */
|
---|
| 111 | errno_t ds_window_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
---|
| 112 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
---|
| 113 | {
|
---|
| 114 | ds_window_t *wnd = (ds_window_t *) arg;
|
---|
| 115 | ds_window_bitmap_t *cbm = NULL;
|
---|
| 116 | errno_t rc;
|
---|
| 117 |
|
---|
| 118 | cbm = calloc(1, sizeof(ds_window_bitmap_t));
|
---|
| 119 | if (cbm == NULL)
|
---|
| 120 | return ENOMEM;
|
---|
| 121 |
|
---|
[87a7cdb] | 122 | rc = gfx_bitmap_create(ds_display_get_gc(wnd->display), params, alloc,
|
---|
[b3c185b6] | 123 | &cbm->bitmap);
|
---|
[0008c0f] | 124 | if (rc != EOK)
|
---|
| 125 | goto error;
|
---|
| 126 |
|
---|
| 127 | cbm->wnd = wnd;
|
---|
[65160d7] | 128 | cbm->rect = params->rect;
|
---|
[0008c0f] | 129 | *rbm = (void *)cbm;
|
---|
| 130 | return EOK;
|
---|
| 131 | error:
|
---|
| 132 | if (cbm != NULL)
|
---|
| 133 | free(cbm);
|
---|
| 134 | return rc;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | /** Destroy bitmap in canvas GC.
|
---|
| 138 | *
|
---|
| 139 | * @param bm Bitmap
|
---|
| 140 | * @return EOK on success or an error code
|
---|
| 141 | */
|
---|
| 142 | static errno_t ds_window_bitmap_destroy(void *bm)
|
---|
| 143 | {
|
---|
| 144 | ds_window_bitmap_t *cbm = (ds_window_bitmap_t *)bm;
|
---|
| 145 |
|
---|
| 146 | gfx_bitmap_destroy(cbm->bitmap);
|
---|
| 147 | free(cbm);
|
---|
| 148 | return EOK;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | /** Render bitmap in canvas GC.
|
---|
| 152 | *
|
---|
| 153 | * @param bm Bitmap
|
---|
| 154 | * @param srect0 Source rectangle or @c NULL
|
---|
| 155 | * @param offs0 Offset or @c NULL
|
---|
| 156 | * @return EOK on success or an error code
|
---|
| 157 | */
|
---|
| 158 | static errno_t ds_window_bitmap_render(void *bm, gfx_rect_t *srect0,
|
---|
| 159 | gfx_coord2_t *offs0)
|
---|
| 160 | {
|
---|
| 161 | ds_window_bitmap_t *cbm = (ds_window_bitmap_t *)bm;
|
---|
[22faaf2] | 162 | gfx_coord2_t doffs;
|
---|
[65160d7] | 163 | gfx_coord2_t offs;
|
---|
| 164 | gfx_rect_t srect;
|
---|
| 165 | gfx_rect_t swrect;
|
---|
| 166 | gfx_rect_t crect;
|
---|
| 167 |
|
---|
| 168 | if (srect0 != NULL) {
|
---|
| 169 | /* Clip source rectangle to bitmap rectangle */
|
---|
| 170 | gfx_rect_clip(srect0, &cbm->rect, &srect);
|
---|
| 171 | } else {
|
---|
| 172 | /* Source is entire bitmap rectangle */
|
---|
| 173 | srect = cbm->rect;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | if (offs0 != NULL) {
|
---|
| 177 | offs = *offs0;
|
---|
| 178 | } else {
|
---|
| 179 | offs.x = 0;
|
---|
| 180 | offs.y = 0;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | /* Transform window rectangle back to bitmap coordinate system */
|
---|
| 184 | gfx_rect_rtranslate(&offs, &cbm->wnd->rect, &swrect);
|
---|
| 185 |
|
---|
| 186 | /* Clip so that transformed rectangle will be inside the window */
|
---|
| 187 | gfx_rect_clip(&srect, &swrect, &crect);
|
---|
| 188 |
|
---|
| 189 | /* Offset for rendering on screen = window pos + offs */
|
---|
| 190 | gfx_coord2_add(&cbm->wnd->dpos, &offs, &doffs);
|
---|
[22faaf2] | 191 |
|
---|
| 192 | return gfx_bitmap_render(cbm->bitmap, srect0, &doffs);
|
---|
[0008c0f] | 193 | }
|
---|
| 194 |
|
---|
| 195 | /** Get allocation info for bitmap in canvas GC.
|
---|
| 196 | *
|
---|
| 197 | * @param bm Bitmap
|
---|
| 198 | * @param alloc Place to store allocation info
|
---|
| 199 | * @return EOK on success or an error code
|
---|
| 200 | */
|
---|
| 201 | static errno_t ds_window_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
|
---|
| 202 | {
|
---|
| 203 | ds_window_bitmap_t *cbm = (ds_window_bitmap_t *)bm;
|
---|
| 204 |
|
---|
| 205 | return gfx_bitmap_get_alloc(cbm->bitmap, alloc);
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[6af4b4f] | 208 | /** Create window.
|
---|
[c8cf261] | 209 | *
|
---|
| 210 | * Create graphics context for rendering into a window.
|
---|
| 211 | *
|
---|
[8e9781f] | 212 | * @param client Client owning the window
|
---|
[3434233] | 213 | * @param params Window parameters
|
---|
[c8cf261] | 214 | * @param rgc Place to store pointer to new GC.
|
---|
| 215 | *
|
---|
| 216 | * @return EOK on success or an error code
|
---|
| 217 | */
|
---|
[3434233] | 218 | errno_t ds_window_create(ds_client_t *client, display_wnd_params_t *params,
|
---|
| 219 | ds_window_t **rgc)
|
---|
[c8cf261] | 220 | {
|
---|
[6af4b4f] | 221 | ds_window_t *wnd = NULL;
|
---|
[c8cf261] | 222 | gfx_context_t *gc = NULL;
|
---|
| 223 | errno_t rc;
|
---|
| 224 |
|
---|
[6af4b4f] | 225 | wnd = calloc(1, sizeof(ds_window_t));
|
---|
| 226 | if (wnd == NULL) {
|
---|
[c8cf261] | 227 | rc = ENOMEM;
|
---|
| 228 | goto error;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
[6af4b4f] | 231 | rc = gfx_context_new(&ds_window_ops, wnd, &gc);
|
---|
[c8cf261] | 232 | if (rc != EOK)
|
---|
| 233 | goto error;
|
---|
| 234 |
|
---|
[b3c185b6] | 235 | ds_client_add_window(client, wnd);
|
---|
[fd777a2] | 236 | ds_display_add_window(client->display, wnd);
|
---|
[6af4b4f] | 237 |
|
---|
[3434233] | 238 | wnd->rect = params->rect;
|
---|
[6af4b4f] | 239 | wnd->gc = gc;
|
---|
| 240 | *rgc = wnd;
|
---|
[c8cf261] | 241 | return EOK;
|
---|
| 242 | error:
|
---|
[6af4b4f] | 243 | if (wnd != NULL)
|
---|
| 244 | free(wnd);
|
---|
[c8cf261] | 245 | gfx_context_delete(gc);
|
---|
| 246 | return rc;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | /** Delete window GC.
|
---|
| 250 | *
|
---|
[6af4b4f] | 251 | * @param wnd Window GC
|
---|
[c8cf261] | 252 | */
|
---|
[da412547] | 253 | void ds_window_destroy(ds_window_t *wnd)
|
---|
[c8cf261] | 254 | {
|
---|
[b3c185b6] | 255 | ds_client_remove_window(wnd);
|
---|
[fd777a2] | 256 | ds_display_remove_window(wnd);
|
---|
[da412547] | 257 | (void) gfx_context_delete(wnd->gc);
|
---|
[c8cf261] | 258 |
|
---|
[6af4b4f] | 259 | free(wnd);
|
---|
[c8cf261] | 260 | }
|
---|
| 261 |
|
---|
[6af4b4f] | 262 | /** Get generic graphic context from window.
|
---|
[c8cf261] | 263 | *
|
---|
[6af4b4f] | 264 | * @param wnd Window
|
---|
[c8cf261] | 265 | * @return Graphic context
|
---|
| 266 | */
|
---|
[6af4b4f] | 267 | gfx_context_t *ds_window_get_ctx(ds_window_t *wnd)
|
---|
[c8cf261] | 268 | {
|
---|
[6af4b4f] | 269 | return wnd->gc;
|
---|
[c8cf261] | 270 | }
|
---|
| 271 |
|
---|
[a40ae0d] | 272 | /** Start moving a window by mouse drag.
|
---|
| 273 | *
|
---|
| 274 | * @param wnd Window
|
---|
| 275 | * @param event Button press event
|
---|
| 276 | */
|
---|
| 277 | static void ds_window_start_move(ds_window_t *wnd, pos_event_t *event)
|
---|
| 278 | {
|
---|
| 279 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_start_move (%d, %d)",
|
---|
| 280 | (int) event->hpos, (int) event->vpos);
|
---|
| 281 |
|
---|
| 282 | assert(wnd->state == dsw_idle);
|
---|
| 283 |
|
---|
| 284 | wnd->orig_pos.x = event->hpos;
|
---|
| 285 | wnd->orig_pos.y = event->vpos;
|
---|
| 286 | wnd->state = dsw_moving;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | /** Finish moving a window by mouse drag.
|
---|
| 290 | *
|
---|
| 291 | * @param wnd Window
|
---|
| 292 | * @param event Button release event
|
---|
| 293 | */
|
---|
| 294 | static void ds_window_finish_move(ds_window_t *wnd, pos_event_t *event)
|
---|
| 295 | {
|
---|
| 296 | gfx_coord2_t pos;
|
---|
| 297 | gfx_coord2_t dmove;
|
---|
| 298 | gfx_coord2_t nwpos;
|
---|
| 299 |
|
---|
| 300 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_finish_move (%d, %d)",
|
---|
| 301 | (int) event->hpos, (int) event->vpos);
|
---|
| 302 |
|
---|
| 303 | assert(wnd->state == dsw_moving);
|
---|
| 304 | pos.x = event->hpos;
|
---|
| 305 | pos.y = event->vpos;
|
---|
| 306 | gfx_coord2_subtract(&pos, &wnd->orig_pos, &dmove);
|
---|
| 307 |
|
---|
| 308 | gfx_coord2_add(&wnd->dpos, &dmove, &nwpos);
|
---|
| 309 | wnd->dpos = nwpos;
|
---|
| 310 | wnd->state = dsw_idle;
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | /** Update window position when moving by mouse drag.
|
---|
| 314 | *
|
---|
| 315 | * @param wnd Window
|
---|
| 316 | * @param event Position update event
|
---|
| 317 | */
|
---|
| 318 | static void ds_window_update_move(ds_window_t *wnd, pos_event_t *event)
|
---|
| 319 | {
|
---|
| 320 | gfx_coord2_t pos;
|
---|
| 321 | gfx_coord2_t dmove;
|
---|
| 322 | gfx_coord2_t nwpos;
|
---|
| 323 | gfx_rect_t drect;
|
---|
| 324 | gfx_color_t *color;
|
---|
| 325 | gfx_context_t *gc;
|
---|
| 326 | errno_t rc;
|
---|
| 327 |
|
---|
| 328 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ds_window_update_move (%d, %d)",
|
---|
| 329 | (int) event->hpos, (int) event->vpos);
|
---|
| 330 |
|
---|
| 331 | assert(wnd->state == dsw_moving);
|
---|
| 332 | pos.x = event->hpos;
|
---|
| 333 | pos.y = event->vpos;
|
---|
| 334 | gfx_coord2_subtract(&pos, &wnd->orig_pos, &dmove);
|
---|
| 335 |
|
---|
| 336 | gfx_coord2_add(&wnd->dpos, &dmove, &nwpos);
|
---|
| 337 | gfx_rect_translate(&nwpos, &wnd->rect, &drect);
|
---|
| 338 |
|
---|
| 339 | rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color);
|
---|
| 340 | if (rc != EOK)
|
---|
| 341 | return;
|
---|
| 342 |
|
---|
| 343 | gc = ds_display_get_gc(wnd->display); // XXX
|
---|
| 344 | if (gc != NULL) {
|
---|
| 345 | gfx_set_color(ds_display_get_gc(wnd->display), color);
|
---|
| 346 | gfx_fill_rect(ds_display_get_gc(wnd->display), &drect);
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | gfx_color_delete(color);
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | /** Post position event to window.
|
---|
| 353 | *
|
---|
| 354 | * @param wnd Window
|
---|
| 355 | * @param event Position event
|
---|
| 356 | */
|
---|
| 357 | errno_t ds_window_post_pos_event(ds_window_t *wnd, pos_event_t *event)
|
---|
| 358 | {
|
---|
| 359 | log_msg(LOG_DEFAULT, LVL_DEBUG,
|
---|
| 360 | "ds_window_post_pos_event type=%d pos=%d,%d\n", event->type,
|
---|
| 361 | (int) event->hpos, (int) event->vpos);
|
---|
| 362 |
|
---|
| 363 | if (event->type == POS_PRESS) {
|
---|
| 364 | if (wnd->state == dsw_idle)
|
---|
| 365 | ds_window_start_move(wnd, event);
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 | if (event->type == POS_RELEASE) {
|
---|
| 369 | if (wnd->state == dsw_moving)
|
---|
| 370 | ds_window_finish_move(wnd, event);
|
---|
| 371 | }
|
---|
| 372 |
|
---|
| 373 | if (event->type == POS_UPDATE) {
|
---|
| 374 | if (wnd->state == dsw_moving)
|
---|
| 375 | ds_window_update_move(wnd, event);
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 | return EOK;
|
---|
| 379 | }
|
---|
| 380 |
|
---|
[c8cf261] | 381 | /** @}
|
---|
| 382 | */
|
---|