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

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

Switch compositor → display server

Convert KFB from visualizer to display device interface. Add ability
of display device implementor to provide client with arg2, arg3 needed
to connect to GC.

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