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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since faca61b8 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
RevLine 
[bd0e6a1]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
[e1f2079]29#include <ddev/info.h>
[87a7cdb]30#include <ddev_srv.h>
[bd0e6a1]31#include <errno.h>
32#include <fibril_synch.h>
[87a7cdb]33#include <gfx/color.h>
34#include <gfx/context.h>
35#include <gfx/coord.h>
[bd0e6a1]36#include <inttypes.h>
[47b27b40]37#include <io/log.h>
[bd0e6a1]38#include <io/pixelmap.h>
[87a7cdb]39#include <ipcgfx/server.h>
40#include <loc.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <task.h>
[bd0e6a1]44
45#include "rfb.h"
46
47#define NAME "rfb"
48
[e1f2079]49static errno_t rfb_ddev_get_gc(void *, sysarg_t *, sysarg_t *);
50static errno_t rfb_ddev_get_info(void *, ddev_info_t *);
51
[87a7cdb]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 = {
[e1f2079]61 .get_gc = rfb_ddev_get_gc,
62 .get_info = rfb_ddev_get_info
[87a7cdb]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;
[bea947f]74 gfx_bitmap_flags_t flags;
75 pixel_t key_color;
[87a7cdb]76 bool myalloc;
77} rfb_bitmap_t;
78
[0b63dc2]79static gfx_context_ops_t rfb_gc_ops = {
[87a7cdb]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;
[5271e4c]112 rfb->damage_rect.height = new_rect.p1.y - new_rect.p0.y;
[87a7cdb]113}
114
[e1f2079]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
[87a7cdb]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);
[bd0e6a1]177
[87a7cdb]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)
[bd0e6a1]191{
[87a7cdb]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
[afcf704]197 /* Check that we support all required flags */
[0d62c10]198 if ((params->flags & ~(bmpf_color_key | bmpf_colorize)) != 0)
[afcf704]199 return ENOTSUP;
200
[87a7cdb]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;
[bea947f]207 rfbbm->flags = params->flags;
208 rfbbm->key_color = params->key_color;
[87a7cdb]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;
[bd0e6a1]226 return EOK;
[87a7cdb]227error:
228 if (rbm != NULL)
229 free(rfbbm);
230 return rc;
[bd0e6a1]231}
232
[87a7cdb]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)
[bd0e6a1]239{
[87a7cdb]240 rfb_bitmap_t *rfbbm = (rfb_bitmap_t *)bm;
241 if (rfbbm->myalloc)
242 free(rfbbm->alloc.pixels);
243 free(rfbbm);
[bd0e6a1]244 return EOK;
245}
246
[87a7cdb]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)
[bd0e6a1]256{
[87a7cdb]257 rfb_bitmap_t *rfbbm = (rfb_bitmap_t *)bm;
258 gfx_rect_t srect;
259 gfx_rect_t drect;
260 gfx_coord2_t offs;
[71cbe5c]261 gfx_coord2_t bmdim;
[87a7cdb]262 gfx_coord2_t dim;
[71cbe5c]263 gfx_coord_t x, y;
264 pixelmap_t pbm;
265 pixel_t color;
[87a7cdb]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);
[71cbe5c]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
[bea947f]288 if ((rfbbm->flags & bmpf_color_key) == 0) {
[0d62c10]289 /* Simple copy */
[bea947f]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 }
[0d62c10]297 } else if ((rfbbm->flags & bmpf_colorize) == 0) {
298 /* Color key */
[bea947f]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 }
[71cbe5c]307 }
[0d62c10]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 }
[71cbe5c]320 }
321
322 rfb_gc_invalidate_rect(rfbbm->rfb, &drect);
[87a7cdb]323
[bd0e6a1]324 return EOK;
325}
326
[87a7cdb]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)
[bd0e6a1]334{
[87a7cdb]335 rfb_bitmap_t *rfbbm = (rfb_bitmap_t *)bm;
336 *alloc = rfbbm->alloc;
[bd0e6a1]337 return EOK;
338}
339
340static void syntax_print(void)
341{
[0d23cc0]342 fprintf(stderr, "Usage: %s <name> <width> <height> [port]\n", NAME);
[bd0e6a1]343}
344
[87a7cdb]345static void client_connection(ipc_call_t *icall, void *arg)
[bd0e6a1]346{
[e1f2079]347 rfb_t *rfb = (rfb_t *) arg;
[87a7cdb]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;
[e1f2079]359 srv.arg = (void *) rfb;
[87a7cdb]360
361 /* Handle connection */
362 ddev_conn(icall, &srv);
363 } else {
[e1f2079]364 rc = gfx_context_new(&rfb_gc_ops, (void *) rfb, &gc);
[87a7cdb]365 if (rc != EOK) {
366 async_answer_0(icall, ENOMEM);
367 return;
368 }
369
370 /* GC connection */
371 gc_conn(icall, gc);
372 }
[bd0e6a1]373}
374
375int main(int argc, char **argv)
376{
[87a7cdb]377 rfb_t rfb;
378
[47b27b40]379 log_init(NAME);
380
[0d23cc0]381 if (argc <= 3) {
[bd0e6a1]382 syntax_print();
383 return 1;
384 }
385
[0d23cc0]386 const char *rfb_name = argv[1];
[a35b458]387
[0d23cc0]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 }
[a35b458]395
[0d23cc0]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 }
[a35b458]402
[0d23cc0]403 unsigned long port = 5900;
404 if (argc > 4) {
405 port = strtoul(argv[4], &endptr, 0);
[bd0e6a1]406 if (*endptr != 0) {
407 fprintf(stderr, "Invalid port number\n");
408 syntax_print();
409 return 1;
410 }
411 }
[a35b458]412
[0d23cc0]413 rfb_init(&rfb, width, height, rfb_name);
[a35b458]414
[87a7cdb]415 async_set_fallback_port_handler(client_connection, &rfb);
[bd0e6a1]416
[b7fd2a0]417 errno_t rc = loc_server_register(NAME);
[bd0e6a1]418 if (rc != EOK) {
419 printf("%s: Unable to register server.\n", NAME);
420 return rc;
[0d23cc0]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 }
[bd0e6a1]429
430 service_id_t service_id;
[a35b458]431
[bd0e6a1]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 }
[a35b458]437
[0d23cc0]438 free(service_name);
[bd0e6a1]439
[87a7cdb]440 category_id_t ddev_cid;
441 rc = loc_category_get_id("display-device", &ddev_cid, IPC_FLAG_BLOCKING);
[bd0e6a1]442 if (rc != EOK) {
[de19d4a]443 fprintf(stderr, NAME ": Unable to get display device category id.\n");
[bd0e6a1]444 return 1;
445 }
[a35b458]446
[87a7cdb]447 rc = loc_service_add_to_cat(service_id, ddev_cid);
[bd0e6a1]448 if (rc != EOK) {
[de19d4a]449 fprintf(stderr, NAME ": Unable to add service to display device category.\n");
[bd0e6a1]450 return 1;
451 }
[a35b458]452
[bd0e6a1]453 rc = rfb_listen(&rfb, port);
[47b27b40]454 if (rc != EOK) {
455 fprintf(stderr, NAME ": Unable to listen at rfb port\n");
[bd0e6a1]456 return 2;
[47b27b40]457 }
[a35b458]458
[bd0e6a1]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.