source: mainline/uspace/srv/hid/rfb/main.c@ 252d03c

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

Rendering text in different colors via colorization

  • Property mode set to 100644
File size: 11.0 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.p0.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 /* Check that we support all required flags */
198 if ((params->flags & ~(bmpf_color_key | bmpf_colorize)) != 0)
199 return ENOTSUP;
200
201 rfbbm = calloc(1, sizeof(rfb_bitmap_t));
202 if (rfbbm == NULL)
203 return ENOMEM;
204
205 gfx_coord2_subtract(&params->rect.p1, &params->rect.p0, &dim);
206 rfbbm->rect = params->rect;
207 rfbbm->flags = params->flags;
208 rfbbm->key_color = params->key_color;
209
210 if (alloc == NULL) {
211 rfbbm->alloc.pitch = dim.x * sizeof(uint32_t);
212 rfbbm->alloc.off0 = 0;
213 rfbbm->alloc.pixels = malloc(rfbbm->alloc.pitch * dim.y);
214 rfbbm->myalloc = true;
215
216 if (rfbbm->alloc.pixels == NULL) {
217 rc = ENOMEM;
218 goto error;
219 }
220 } else {
221 rfbbm->alloc = *alloc;
222 }
223
224 rfbbm->rfb = rfb;
225 *rbm = (void *)rfbbm;
226 return EOK;
227error:
228 if (rbm != NULL)
229 free(rfbbm);
230 return rc;
231}
232
233/** Destroy bitmap in RFB GC.
234 *
235 * @param bm Bitmap
236 * @return EOK on success or an error code
237 */
238static errno_t rfb_gc_bitmap_destroy(void *bm)
239{
240 rfb_bitmap_t *rfbbm = (rfb_bitmap_t *)bm;
241 if (rfbbm->myalloc)
242 free(rfbbm->alloc.pixels);
243 free(rfbbm);
244 return EOK;
245}
246
247/** Render bitmap in RFB GC.
248 *
249 * @param bm Bitmap
250 * @param srect0 Source rectangle or @c NULL
251 * @param offs0 Offset or @c NULL
252 * @return EOK on success or an error code
253 */
254static errno_t rfb_gc_bitmap_render(void *bm, gfx_rect_t *srect0,
255 gfx_coord2_t *offs0)
256{
257 rfb_bitmap_t *rfbbm = (rfb_bitmap_t *)bm;
258 gfx_rect_t srect;
259 gfx_rect_t drect;
260 gfx_coord2_t offs;
261 gfx_coord2_t bmdim;
262 gfx_coord2_t dim;
263 gfx_coord_t x, y;
264 pixelmap_t pbm;
265 pixel_t color;
266
267 if (srect0 != NULL)
268 srect = *srect0;
269 else
270 srect = rfbbm->rect;
271
272 if (offs0 != NULL) {
273 offs = *offs0;
274 } else {
275 offs.x = 0;
276 offs.y = 0;
277 }
278
279 /* Destination rectangle */
280 gfx_rect_translate(&offs, &srect, &drect);
281 gfx_coord2_subtract(&drect.p1, &drect.p0, &dim);
282 gfx_coord2_subtract(&rfbbm->rect.p1, &rfbbm->rect.p0, &bmdim);
283
284 pbm.width = bmdim.x;
285 pbm.height = bmdim.y;
286 pbm.data = rfbbm->alloc.pixels;
287
288 if ((rfbbm->flags & bmpf_color_key) == 0) {
289 /* Simple copy */
290 for (y = srect.p0.y; y < srect.p1.y; y++) {
291 for (x = srect.p0.x; x < srect.p1.x; x++) {
292 color = pixelmap_get_pixel(&pbm, x, y);
293 pixelmap_put_pixel(&rfbbm->rfb->rfb.framebuffer,
294 x + offs.x, y + offs.y, color);
295 }
296 }
297 } else if ((rfbbm->flags & bmpf_colorize) == 0) {
298 /* Color key */
299 for (y = srect.p0.y; y < srect.p1.y; y++) {
300 for (x = srect.p0.x; x < srect.p1.x; x++) {
301 color = pixelmap_get_pixel(&pbm, x, y);
302 if (color != rfbbm->key_color) {
303 pixelmap_put_pixel(&rfbbm->rfb->rfb.framebuffer,
304 x + offs.x, y + offs.y, color);
305 }
306 }
307 }
308 } else {
309 /* Color key & colorization */
310 for (y = srect.p0.y; y < srect.p1.y; y++) {
311 for (x = srect.p0.x; x < srect.p1.x; x++) {
312 color = pixelmap_get_pixel(&pbm, x, y);
313 if (color != rfbbm->key_color) {
314 pixelmap_put_pixel(&rfbbm->rfb->rfb.framebuffer,
315 x + offs.x, y + offs.y,
316 rfbbm->rfb->color);
317 }
318 }
319 }
320 }
321
322 rfb_gc_invalidate_rect(rfbbm->rfb, &drect);
323
324 return EOK;
325}
326
327/** Get allocation info for bitmap in RFB GC.
328 *
329 * @param bm Bitmap
330 * @param alloc Place to store allocation info
331 * @return EOK on success or an error code
332 */
333static errno_t rfb_gc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
334{
335 rfb_bitmap_t *rfbbm = (rfb_bitmap_t *)bm;
336 *alloc = rfbbm->alloc;
337 return EOK;
338}
339
340static void syntax_print(void)
341{
342 fprintf(stderr, "Usage: %s <name> <width> <height> [port]\n", NAME);
343}
344
345static void client_connection(ipc_call_t *icall, void *arg)
346{
347 rfb_t *rfb = (rfb_t *) arg;
348 ddev_srv_t srv;
349 sysarg_t svc_id;
350 gfx_context_t *gc;
351 errno_t rc;
352
353 svc_id = ipc_get_arg2(icall);
354
355 if (svc_id != 0) {
356 /* Set up protocol structure */
357 ddev_srv_initialize(&srv);
358 srv.ops = &rfb_ddev_ops;
359 srv.arg = (void *) rfb;
360
361 /* Handle connection */
362 ddev_conn(icall, &srv);
363 } else {
364 rc = gfx_context_new(&rfb_gc_ops, (void *) rfb, &gc);
365 if (rc != EOK) {
366 async_answer_0(icall, ENOMEM);
367 return;
368 }
369
370 /* GC connection */
371 gc_conn(icall, gc);
372 }
373}
374
375int main(int argc, char **argv)
376{
377 rfb_t rfb;
378
379 log_init(NAME);
380
381 if (argc <= 3) {
382 syntax_print();
383 return 1;
384 }
385
386 const char *rfb_name = argv[1];
387
388 char *endptr;
389 unsigned long width = strtoul(argv[2], &endptr, 0);
390 if (*endptr != 0) {
391 fprintf(stderr, "Invalid width\n");
392 syntax_print();
393 return 1;
394 }
395
396 unsigned long height = strtoul(argv[3], &endptr, 0);
397 if (*endptr != 0) {
398 fprintf(stderr, "Invalid height\n");
399 syntax_print();
400 return 1;
401 }
402
403 unsigned long port = 5900;
404 if (argc > 4) {
405 port = strtoul(argv[4], &endptr, 0);
406 if (*endptr != 0) {
407 fprintf(stderr, "Invalid port number\n");
408 syntax_print();
409 return 1;
410 }
411 }
412
413 rfb_init(&rfb, width, height, rfb_name);
414
415 async_set_fallback_port_handler(client_connection, &rfb);
416
417 errno_t rc = loc_server_register(NAME);
418 if (rc != EOK) {
419 printf("%s: Unable to register server.\n", NAME);
420 return rc;
421 }
422
423 char *service_name;
424 rc = asprintf(&service_name, "rfb/%s", rfb_name);
425 if (rc < 0) {
426 printf(NAME ": Unable to create service name\n");
427 return rc;
428 }
429
430 service_id_t service_id;
431
432 rc = loc_service_register(service_name, &service_id);
433 if (rc != EOK) {
434 printf(NAME ": Unable to register service %s.\n", service_name);
435 return rc;
436 }
437
438 free(service_name);
439
440 category_id_t ddev_cid;
441 rc = loc_category_get_id("display-device", &ddev_cid, IPC_FLAG_BLOCKING);
442 if (rc != EOK) {
443 fprintf(stderr, NAME ": Unable to get display device category id.\n");
444 return 1;
445 }
446
447 rc = loc_service_add_to_cat(service_id, ddev_cid);
448 if (rc != EOK) {
449 fprintf(stderr, NAME ": Unable to add service to display device category.\n");
450 return 1;
451 }
452
453 rc = rfb_listen(&rfb, port);
454 if (rc != EOK) {
455 fprintf(stderr, NAME ": Unable to listen at rfb port\n");
456 return 2;
457 }
458
459 printf("%s: Accepting connections\n", NAME);
460 task_retval(0);
461 async_manager();
462
463 /* Not reached */
464 return 0;
465}
Note: See TracBrowser for help on using the repository browser.