source: mainline/uspace/srv/hid/rfb/main.c@ 5f483be

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

Implement bitmap color key to allow transparent cursor background

This seems to be the simplest solution of them all. It will work
on any bit depth except 1 bit per pixel (monochrome), where we would
need to extend the bitmap with a bit mask instead.

  • Property mode set to 100644
File size: 10.4 KB
Line 
1/*
2 * Copyright (c) 2013 Martin Sucha
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#include <ddev/info.h>
30#include <ddev_srv.h>
31#include <errno.h>
32#include <fibril_synch.h>
33#include <gfx/color.h>
34#include <gfx/context.h>
35#include <gfx/coord.h>
36#include <inttypes.h>
37#include <io/log.h>
38#include <io/pixelmap.h>
39#include <ipcgfx/server.h>
40#include <loc.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <task.h>
44
45#include "rfb.h"
46
47#define NAME "rfb"
48
49static errno_t rfb_ddev_get_gc(void *, sysarg_t *, sysarg_t *);
50static errno_t rfb_ddev_get_info(void *, ddev_info_t *);
51
52static errno_t rfb_gc_set_color(void *, gfx_color_t *);
53static errno_t rfb_gc_fill_rect(void *, gfx_rect_t *);
54static errno_t rfb_gc_bitmap_create(void *, gfx_bitmap_params_t *,
55 gfx_bitmap_alloc_t *, void **);
56static errno_t rfb_gc_bitmap_destroy(void *);
57static errno_t rfb_gc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
58static errno_t rfb_gc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
59
60static ddev_ops_t rfb_ddev_ops = {
61 .get_gc = rfb_ddev_get_gc,
62 .get_info = rfb_ddev_get_info
63};
64
65typedef struct {
66 rfb_t rfb;
67 pixel_t color;
68} rfb_gc_t;
69
70typedef struct {
71 rfb_gc_t *rfb;
72 gfx_bitmap_alloc_t alloc;
73 gfx_rect_t rect;
74 gfx_bitmap_flags_t flags;
75 pixel_t key_color;
76 bool myalloc;
77} rfb_bitmap_t;
78
79static gfx_context_ops_t rfb_gc_ops = {
80 .set_color = rfb_gc_set_color,
81 .fill_rect = rfb_gc_fill_rect,
82 .bitmap_create = rfb_gc_bitmap_create,
83 .bitmap_destroy = rfb_gc_bitmap_destroy,
84 .bitmap_render = rfb_gc_bitmap_render,
85 .bitmap_get_alloc = rfb_gc_bitmap_get_alloc
86};
87
88static void rfb_gc_invalidate_rect(rfb_gc_t *rfbgc, gfx_rect_t *rect)
89{
90 rfb_t *rfb = &rfbgc->rfb;
91 gfx_rect_t old_rect;
92 gfx_rect_t new_rect;
93
94 if (gfx_rect_is_empty(rect))
95 return;
96
97 if (!rfb->damage_valid) {
98 old_rect.p0.x = old_rect.p0.y = 0;
99 old_rect.p1.x = old_rect.p1.y = 0;
100 } else {
101 old_rect.p0.x = rfb->damage_rect.x;
102 old_rect.p0.y = rfb->damage_rect.y;
103 old_rect.p1.x = rfb->damage_rect.x + rfb->damage_rect.width;
104 old_rect.p1.y = rfb->damage_rect.y + rfb->damage_rect.height;
105 }
106
107 gfx_rect_envelope(&old_rect, rect, &new_rect);
108
109 rfb->damage_rect.x = new_rect.p0.x;
110 rfb->damage_rect.y = new_rect.p0.y;
111 rfb->damage_rect.width = new_rect.p1.x - new_rect.p0.x;
112 rfb->damage_rect.height = new_rect.p1.y - new_rect.p1.y;
113}
114
115static errno_t rfb_ddev_get_gc(void *arg, sysarg_t *arg2, sysarg_t *arg3)
116{
117 *arg2 = 0;
118 *arg3 = 42;
119 return EOK;
120}
121
122static errno_t rfb_ddev_get_info(void *arg, ddev_info_t *info)
123{
124 rfb_t *rfb = (rfb_t *) arg;
125
126 ddev_info_init(info);
127
128 info->rect.p0.x = 0;
129 info->rect.p0.y = 0;
130 info->rect.p1.x = rfb->width;
131 info->rect.p1.y = rfb->height;
132
133 return EOK;
134}
135
136/** Set color on RFB.
137 *
138 * Set drawing color on RFB GC.
139 *
140 * @param arg RFB
141 * @param color Color
142 *
143 * @return EOK on success or an error code
144 */
145static errno_t rfb_gc_set_color(void *arg, gfx_color_t *color)
146{
147 rfb_gc_t *rfb = (rfb_gc_t *) arg;
148 uint16_t r, g, b;
149
150 gfx_color_get_rgb_i16(color, &r, &g, &b);
151 rfb->color = PIXEL(0, r >> 8, g >> 8, b >> 8);
152 return EOK;
153}
154
155/** Fill rectangle on RFB.
156 *
157 * @param arg RFB
158 * @param rect Rectangle
159 *
160 * @return EOK on success or an error code
161 */
162static errno_t rfb_gc_fill_rect(void *arg, gfx_rect_t *rect)
163{
164 rfb_gc_t *rfb = (rfb_gc_t *) arg;
165 gfx_coord_t x, y;
166
167 // XXX We should handle p0.x > p1.x and p0.y > p1.y
168
169 for (y = rect->p0.y; y < rect->p1.y; y++) {
170 for (x = rect->p0.x; x < rect->p1.x; x++) {
171 pixelmap_put_pixel(&rfb->rfb.framebuffer, x, y,
172 rfb->color);
173 }
174 }
175
176 rfb_gc_invalidate_rect(rfb, rect);
177
178 return EOK;
179}
180
181/** Create bitmap in RFB GC.
182 *
183 * @param arg RFB
184 * @param params Bitmap params
185 * @param alloc Bitmap allocation info or @c NULL
186 * @param rbm Place to store pointer to new bitmap
187 * @return EOK on success or an error code
188 */
189errno_t rfb_gc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
190 gfx_bitmap_alloc_t *alloc, void **rbm)
191{
192 rfb_gc_t *rfb = (rfb_gc_t *) arg;
193 rfb_bitmap_t *rfbbm = NULL;
194 gfx_coord2_t dim;
195 errno_t rc;
196
197 rfbbm = calloc(1, sizeof(rfb_bitmap_t));
198 if (rfbbm == NULL)
199 return ENOMEM;
200
201 gfx_coord2_subtract(&params->rect.p1, &params->rect.p0, &dim);
202 rfbbm->rect = params->rect;
203 rfbbm->flags = params->flags;
204 rfbbm->key_color = params->key_color;
205
206 if (alloc == NULL) {
207 rfbbm->alloc.pitch = dim.x * sizeof(uint32_t);
208 rfbbm->alloc.off0 = 0;
209 rfbbm->alloc.pixels = malloc(rfbbm->alloc.pitch * dim.y);
210 rfbbm->myalloc = true;
211
212 if (rfbbm->alloc.pixels == NULL) {
213 rc = ENOMEM;
214 goto error;
215 }
216 } else {
217 rfbbm->alloc = *alloc;
218 }
219
220 rfbbm->rfb = rfb;
221 *rbm = (void *)rfbbm;
222 return EOK;
223error:
224 if (rbm != NULL)
225 free(rfbbm);
226 return rc;
227}
228
229/** Destroy bitmap in RFB GC.
230 *
231 * @param bm Bitmap
232 * @return EOK on success or an error code
233 */
234static errno_t rfb_gc_bitmap_destroy(void *bm)
235{
236 rfb_bitmap_t *rfbbm = (rfb_bitmap_t *)bm;
237 if (rfbbm->myalloc)
238 free(rfbbm->alloc.pixels);
239 free(rfbbm);
240 return EOK;
241}
242
243/** Render bitmap in RFB GC.
244 *
245 * @param bm Bitmap
246 * @param srect0 Source rectangle or @c NULL
247 * @param offs0 Offset or @c NULL
248 * @return EOK on success or an error code
249 */
250static errno_t rfb_gc_bitmap_render(void *bm, gfx_rect_t *srect0,
251 gfx_coord2_t *offs0)
252{
253 rfb_bitmap_t *rfbbm = (rfb_bitmap_t *)bm;
254 gfx_rect_t srect;
255 gfx_rect_t drect;
256 gfx_coord2_t offs;
257 gfx_coord2_t bmdim;
258 gfx_coord2_t dim;
259 gfx_coord_t x, y;
260 pixelmap_t pbm;
261 pixel_t color;
262
263 if (srect0 != NULL)
264 srect = *srect0;
265 else
266 srect = rfbbm->rect;
267
268 if (offs0 != NULL) {
269 offs = *offs0;
270 } else {
271 offs.x = 0;
272 offs.y = 0;
273 }
274
275 /* Destination rectangle */
276 gfx_rect_translate(&offs, &srect, &drect);
277 gfx_coord2_subtract(&drect.p1, &drect.p0, &dim);
278 gfx_coord2_subtract(&rfbbm->rect.p1, &rfbbm->rect.p0, &bmdim);
279
280 pbm.width = bmdim.x;
281 pbm.height = bmdim.y;
282 pbm.data = rfbbm->alloc.pixels;
283
284 if ((rfbbm->flags & bmpf_color_key) == 0) {
285 for (y = srect.p0.y; y < srect.p1.y; y++) {
286 for (x = srect.p0.x; x < srect.p1.x; x++) {
287 color = pixelmap_get_pixel(&pbm, x, y);
288 pixelmap_put_pixel(&rfbbm->rfb->rfb.framebuffer,
289 x + offs.x, y + offs.y, color);
290 }
291 }
292 } else {
293 for (y = srect.p0.y; y < srect.p1.y; y++) {
294 for (x = srect.p0.x; x < srect.p1.x; x++) {
295 color = pixelmap_get_pixel(&pbm, x, y);
296 if (color != rfbbm->key_color) {
297 pixelmap_put_pixel(&rfbbm->rfb->rfb.framebuffer,
298 x + offs.x, y + offs.y, color);
299 }
300 }
301 }
302 }
303
304 rfb_gc_invalidate_rect(rfbbm->rfb, &drect);
305
306 return EOK;
307}
308
309/** Get allocation info for bitmap in RFB GC.
310 *
311 * @param bm Bitmap
312 * @param alloc Place to store allocation info
313 * @return EOK on success or an error code
314 */
315static errno_t rfb_gc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
316{
317 rfb_bitmap_t *rfbbm = (rfb_bitmap_t *)bm;
318 *alloc = rfbbm->alloc;
319 return EOK;
320}
321
322static void syntax_print(void)
323{
324 fprintf(stderr, "Usage: %s <name> <width> <height> [port]\n", NAME);
325}
326
327static void client_connection(ipc_call_t *icall, void *arg)
328{
329 rfb_t *rfb = (rfb_t *) arg;
330 ddev_srv_t srv;
331 sysarg_t svc_id;
332 gfx_context_t *gc;
333 errno_t rc;
334
335 svc_id = ipc_get_arg2(icall);
336
337 if (svc_id != 0) {
338 /* Set up protocol structure */
339 ddev_srv_initialize(&srv);
340 srv.ops = &rfb_ddev_ops;
341 srv.arg = (void *) rfb;
342
343 /* Handle connection */
344 ddev_conn(icall, &srv);
345 } else {
346 rc = gfx_context_new(&rfb_gc_ops, (void *) rfb, &gc);
347 if (rc != EOK) {
348 async_answer_0(icall, ENOMEM);
349 return;
350 }
351
352 /* GC connection */
353 gc_conn(icall, gc);
354 }
355}
356
357int main(int argc, char **argv)
358{
359 rfb_t rfb;
360
361 log_init(NAME);
362
363 if (argc <= 3) {
364 syntax_print();
365 return 1;
366 }
367
368 const char *rfb_name = argv[1];
369
370 char *endptr;
371 unsigned long width = strtoul(argv[2], &endptr, 0);
372 if (*endptr != 0) {
373 fprintf(stderr, "Invalid width\n");
374 syntax_print();
375 return 1;
376 }
377
378 unsigned long height = strtoul(argv[3], &endptr, 0);
379 if (*endptr != 0) {
380 fprintf(stderr, "Invalid height\n");
381 syntax_print();
382 return 1;
383 }
384
385 unsigned long port = 5900;
386 if (argc > 4) {
387 port = strtoul(argv[4], &endptr, 0);
388 if (*endptr != 0) {
389 fprintf(stderr, "Invalid port number\n");
390 syntax_print();
391 return 1;
392 }
393 }
394
395 rfb_init(&rfb, width, height, rfb_name);
396
397 async_set_fallback_port_handler(client_connection, &rfb);
398
399 errno_t rc = loc_server_register(NAME);
400 if (rc != EOK) {
401 printf("%s: Unable to register server.\n", NAME);
402 return rc;
403 }
404
405 char *service_name;
406 rc = asprintf(&service_name, "rfb/%s", rfb_name);
407 if (rc < 0) {
408 printf(NAME ": Unable to create service name\n");
409 return rc;
410 }
411
412 service_id_t service_id;
413
414 rc = loc_service_register(service_name, &service_id);
415 if (rc != EOK) {
416 printf(NAME ": Unable to register service %s.\n", service_name);
417 return rc;
418 }
419
420 free(service_name);
421
422 category_id_t ddev_cid;
423 rc = loc_category_get_id("display-device", &ddev_cid, IPC_FLAG_BLOCKING);
424 if (rc != EOK) {
425 fprintf(stderr, NAME ": Unable to get visualizer category id.\n");
426 return 1;
427 }
428
429 rc = loc_service_add_to_cat(service_id, ddev_cid);
430 if (rc != EOK) {
431 fprintf(stderr, NAME ": Unable to add service to visualizer category.\n");
432 return 1;
433 }
434
435 rc = rfb_listen(&rfb, port);
436 if (rc != EOK) {
437 fprintf(stderr, NAME ": Unable to listen at rfb port\n");
438 return 2;
439 }
440
441 printf("%s: Accepting connections\n", NAME);
442 task_retval(0);
443 async_manager();
444
445 /* Not reached */
446 return 0;
447}
Note: See TracBrowser for help on using the repository browser.