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 <stdlib.h>
|
---|
45 | #include "client.h"
|
---|
46 | #include "display.h"
|
---|
47 | #include "window.h"
|
---|
48 |
|
---|
49 | static errno_t ds_window_set_color(void *, gfx_color_t *);
|
---|
50 | static errno_t ds_window_fill_rect(void *, gfx_rect_t *);
|
---|
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 *);
|
---|
56 |
|
---|
57 | gfx_context_ops_t ds_window_ops = {
|
---|
58 | .set_color = ds_window_set_color,
|
---|
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
|
---|
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 | */
|
---|
75 | static errno_t ds_window_set_color(void *arg, gfx_color_t *color)
|
---|
76 | {
|
---|
77 | ds_window_t *wnd = (ds_window_t *) arg;
|
---|
78 |
|
---|
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);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /** Fill rectangle on window GC.
|
---|
85 | *
|
---|
86 | * @param arg Window GC
|
---|
87 | * @param rect Rectangle
|
---|
88 | *
|
---|
89 | * @return EOK on success or an error code
|
---|
90 | */
|
---|
91 | static errno_t ds_window_fill_rect(void *arg, gfx_rect_t *rect)
|
---|
92 | {
|
---|
93 | ds_window_t *wnd = (ds_window_t *) arg;
|
---|
94 | gfx_rect_t crect;
|
---|
95 | gfx_rect_t drect;
|
---|
96 |
|
---|
97 | log_msg(LOG_DEFAULT, LVL_NOTE, "gc_fill_rect");
|
---|
98 | gfx_rect_clip(rect, &wnd->rect, &crect);
|
---|
99 | gfx_rect_translate(&wnd->dpos, &crect, &drect);
|
---|
100 | return gfx_fill_rect(ds_display_get_gc(wnd->display), &drect);
|
---|
101 | }
|
---|
102 |
|
---|
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 |
|
---|
122 | rc = gfx_bitmap_create(ds_display_get_gc(wnd->display), params, alloc,
|
---|
123 | &cbm->bitmap);
|
---|
124 | if (rc != EOK)
|
---|
125 | goto error;
|
---|
126 |
|
---|
127 | cbm->wnd = wnd;
|
---|
128 | cbm->rect = params->rect;
|
---|
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;
|
---|
162 | gfx_coord2_t doffs;
|
---|
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);
|
---|
191 |
|
---|
192 | return gfx_bitmap_render(cbm->bitmap, srect0, &doffs);
|
---|
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 |
|
---|
208 | /** Create window.
|
---|
209 | *
|
---|
210 | * Create graphics context for rendering into a window.
|
---|
211 | *
|
---|
212 | * @param client Client owning the window
|
---|
213 | * @param params Window parameters
|
---|
214 | * @param rgc Place to store pointer to new GC.
|
---|
215 | *
|
---|
216 | * @return EOK on success or an error code
|
---|
217 | */
|
---|
218 | errno_t ds_window_create(ds_client_t *client, display_wnd_params_t *params,
|
---|
219 | ds_window_t **rgc)
|
---|
220 | {
|
---|
221 | ds_window_t *wnd = NULL;
|
---|
222 | gfx_context_t *gc = NULL;
|
---|
223 | errno_t rc;
|
---|
224 |
|
---|
225 | wnd = calloc(1, sizeof(ds_window_t));
|
---|
226 | if (wnd == NULL) {
|
---|
227 | rc = ENOMEM;
|
---|
228 | goto error;
|
---|
229 | }
|
---|
230 |
|
---|
231 | rc = gfx_context_new(&ds_window_ops, wnd, &gc);
|
---|
232 | if (rc != EOK)
|
---|
233 | goto error;
|
---|
234 |
|
---|
235 | ds_client_add_window(client, wnd);
|
---|
236 | ds_display_add_window(client->display, wnd);
|
---|
237 |
|
---|
238 | wnd->rect = params->rect;
|
---|
239 | wnd->gc = gc;
|
---|
240 | *rgc = wnd;
|
---|
241 | return EOK;
|
---|
242 | error:
|
---|
243 | if (wnd != NULL)
|
---|
244 | free(wnd);
|
---|
245 | gfx_context_delete(gc);
|
---|
246 | return rc;
|
---|
247 | }
|
---|
248 |
|
---|
249 | /** Delete window GC.
|
---|
250 | *
|
---|
251 | * @param wnd Window GC
|
---|
252 | */
|
---|
253 | void ds_window_destroy(ds_window_t *wnd)
|
---|
254 | {
|
---|
255 | ds_client_remove_window(wnd);
|
---|
256 | ds_display_remove_window(wnd);
|
---|
257 | (void) gfx_context_delete(wnd->gc);
|
---|
258 |
|
---|
259 | free(wnd);
|
---|
260 | }
|
---|
261 |
|
---|
262 | /** Get generic graphic context from window.
|
---|
263 | *
|
---|
264 | * @param wnd Window
|
---|
265 | * @return Graphic context
|
---|
266 | */
|
---|
267 | gfx_context_t *ds_window_get_ctx(ds_window_t *wnd)
|
---|
268 | {
|
---|
269 | return wnd->gc;
|
---|
270 | }
|
---|
271 |
|
---|
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 |
|
---|
381 | /** @}
|
---|
382 | */
|
---|