[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 |
|
---|
[87a7cdb] | 29 | #include <ddev_srv.h>
|
---|
[bd0e6a1] | 30 | #include <errno.h>
|
---|
| 31 | #include <fibril_synch.h>
|
---|
[87a7cdb] | 32 | #include <gfx/color.h>
|
---|
| 33 | #include <gfx/context.h>
|
---|
| 34 | #include <gfx/coord.h>
|
---|
[bd0e6a1] | 35 | #include <inttypes.h>
|
---|
[47b27b40] | 36 | #include <io/log.h>
|
---|
[bd0e6a1] | 37 | #include <io/pixelmap.h>
|
---|
[87a7cdb] | 38 | #include <ipcgfx/server.h>
|
---|
| 39 | #include <loc.h>
|
---|
| 40 | #include <stdio.h>
|
---|
| 41 | #include <stdlib.h>
|
---|
| 42 | #include <task.h>
|
---|
[bd0e6a1] | 43 |
|
---|
| 44 | #include "rfb.h"
|
---|
| 45 |
|
---|
| 46 | #define NAME "rfb"
|
---|
| 47 |
|
---|
[87a7cdb] | 48 | static errno_t rfb_gc_set_color(void *, gfx_color_t *);
|
---|
| 49 | static errno_t rfb_gc_fill_rect(void *, gfx_rect_t *);
|
---|
| 50 | static errno_t rfb_gc_bitmap_create(void *, gfx_bitmap_params_t *,
|
---|
| 51 | gfx_bitmap_alloc_t *, void **);
|
---|
| 52 | static errno_t rfb_gc_bitmap_destroy(void *);
|
---|
| 53 | static errno_t rfb_gc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
|
---|
| 54 | static errno_t rfb_gc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
|
---|
| 55 |
|
---|
| 56 | static ddev_ops_t rfb_ddev_ops = {
|
---|
| 57 | };
|
---|
| 58 |
|
---|
| 59 | typedef struct {
|
---|
| 60 | rfb_t rfb;
|
---|
| 61 | pixel_t color;
|
---|
| 62 | } rfb_gc_t;
|
---|
| 63 |
|
---|
| 64 | typedef 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 |
|
---|
[0b63dc2] | 71 | static gfx_context_ops_t rfb_gc_ops = {
|
---|
[87a7cdb] | 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 |
|
---|
| 80 | static 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 | */
|
---|
| 116 | static 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 | */
|
---|
| 133 | static 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);
|
---|
[bd0e6a1] | 148 |
|
---|
[87a7cdb] | 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 | */
|
---|
| 160 | errno_t rfb_gc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
---|
| 161 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
---|
[bd0e6a1] | 162 | {
|
---|
[87a7cdb] | 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(¶ms->rect.p1, ¶ms->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;
|
---|
[bd0e6a1] | 191 | return EOK;
|
---|
[87a7cdb] | 192 | error:
|
---|
| 193 | if (rbm != NULL)
|
---|
| 194 | free(rfbbm);
|
---|
| 195 | return rc;
|
---|
[bd0e6a1] | 196 | }
|
---|
| 197 |
|
---|
[87a7cdb] | 198 | /** Destroy bitmap in RFB GC.
|
---|
| 199 | *
|
---|
| 200 | * @param bm Bitmap
|
---|
| 201 | * @return EOK on success or an error code
|
---|
| 202 | */
|
---|
| 203 | static errno_t rfb_gc_bitmap_destroy(void *bm)
|
---|
[bd0e6a1] | 204 | {
|
---|
[87a7cdb] | 205 | rfb_bitmap_t *rfbbm = (rfb_bitmap_t *)bm;
|
---|
| 206 | if (rfbbm->myalloc)
|
---|
| 207 | free(rfbbm->alloc.pixels);
|
---|
| 208 | free(rfbbm);
|
---|
[bd0e6a1] | 209 | return EOK;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
[87a7cdb] | 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 | */
|
---|
| 219 | static errno_t rfb_gc_bitmap_render(void *bm, gfx_rect_t *srect0,
|
---|
| 220 | gfx_coord2_t *offs0)
|
---|
[bd0e6a1] | 221 | {
|
---|
[87a7cdb] | 222 | rfb_bitmap_t *rfbbm = (rfb_bitmap_t *)bm;
|
---|
| 223 | gfx_rect_t srect;
|
---|
| 224 | gfx_rect_t drect;
|
---|
| 225 | gfx_coord2_t offs;
|
---|
[71cbe5c] | 226 | gfx_coord2_t bmdim;
|
---|
[87a7cdb] | 227 | gfx_coord2_t dim;
|
---|
[71cbe5c] | 228 | gfx_coord_t x, y;
|
---|
| 229 | pixelmap_t pbm;
|
---|
| 230 | pixel_t color;
|
---|
[87a7cdb] | 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);
|
---|
[71cbe5c] | 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);
|
---|
[87a7cdb] | 262 |
|
---|
[bd0e6a1] | 263 | return EOK;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
[87a7cdb] | 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 | */
|
---|
| 272 | static errno_t rfb_gc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
|
---|
[bd0e6a1] | 273 | {
|
---|
[87a7cdb] | 274 | rfb_bitmap_t *rfbbm = (rfb_bitmap_t *)bm;
|
---|
| 275 | *alloc = rfbbm->alloc;
|
---|
[bd0e6a1] | 276 | return EOK;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | static void syntax_print(void)
|
---|
| 280 | {
|
---|
[0d23cc0] | 281 | fprintf(stderr, "Usage: %s <name> <width> <height> [port]\n", NAME);
|
---|
[bd0e6a1] | 282 | }
|
---|
| 283 |
|
---|
[87a7cdb] | 284 | static void client_connection(ipc_call_t *icall, void *arg)
|
---|
[bd0e6a1] | 285 | {
|
---|
[87a7cdb] | 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 | }
|
---|
[bd0e6a1] | 311 | }
|
---|
| 312 |
|
---|
| 313 | int main(int argc, char **argv)
|
---|
| 314 | {
|
---|
[87a7cdb] | 315 | rfb_t rfb;
|
---|
| 316 |
|
---|
[47b27b40] | 317 | log_init(NAME);
|
---|
| 318 |
|
---|
[0d23cc0] | 319 | if (argc <= 3) {
|
---|
[bd0e6a1] | 320 | syntax_print();
|
---|
| 321 | return 1;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
[0d23cc0] | 324 | const char *rfb_name = argv[1];
|
---|
[a35b458] | 325 |
|
---|
[0d23cc0] | 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 | }
|
---|
[a35b458] | 333 |
|
---|
[0d23cc0] | 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 | }
|
---|
[a35b458] | 340 |
|
---|
[0d23cc0] | 341 | unsigned long port = 5900;
|
---|
| 342 | if (argc > 4) {
|
---|
| 343 | port = strtoul(argv[4], &endptr, 0);
|
---|
[bd0e6a1] | 344 | if (*endptr != 0) {
|
---|
| 345 | fprintf(stderr, "Invalid port number\n");
|
---|
| 346 | syntax_print();
|
---|
| 347 | return 1;
|
---|
| 348 | }
|
---|
| 349 | }
|
---|
[a35b458] | 350 |
|
---|
[0d23cc0] | 351 | rfb_init(&rfb, width, height, rfb_name);
|
---|
[a35b458] | 352 |
|
---|
[87a7cdb] | 353 | async_set_fallback_port_handler(client_connection, &rfb);
|
---|
[bd0e6a1] | 354 |
|
---|
[b7fd2a0] | 355 | errno_t rc = loc_server_register(NAME);
|
---|
[bd0e6a1] | 356 | if (rc != EOK) {
|
---|
| 357 | printf("%s: Unable to register server.\n", NAME);
|
---|
| 358 | return rc;
|
---|
[0d23cc0] | 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 | }
|
---|
[bd0e6a1] | 367 |
|
---|
| 368 | service_id_t service_id;
|
---|
[a35b458] | 369 |
|
---|
[bd0e6a1] | 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 | }
|
---|
[a35b458] | 375 |
|
---|
[0d23cc0] | 376 | free(service_name);
|
---|
[bd0e6a1] | 377 |
|
---|
[87a7cdb] | 378 | category_id_t ddev_cid;
|
---|
| 379 | rc = loc_category_get_id("display-device", &ddev_cid, IPC_FLAG_BLOCKING);
|
---|
[bd0e6a1] | 380 | if (rc != EOK) {
|
---|
| 381 | fprintf(stderr, NAME ": Unable to get visualizer category id.\n");
|
---|
| 382 | return 1;
|
---|
| 383 | }
|
---|
[a35b458] | 384 |
|
---|
[87a7cdb] | 385 | rc = loc_service_add_to_cat(service_id, ddev_cid);
|
---|
[bd0e6a1] | 386 | if (rc != EOK) {
|
---|
| 387 | fprintf(stderr, NAME ": Unable to add service to visualizer category.\n");
|
---|
| 388 | return 1;
|
---|
| 389 | }
|
---|
[a35b458] | 390 |
|
---|
[bd0e6a1] | 391 | rc = rfb_listen(&rfb, port);
|
---|
[47b27b40] | 392 | if (rc != EOK) {
|
---|
| 393 | fprintf(stderr, NAME ": Unable to listen at rfb port\n");
|
---|
[bd0e6a1] | 394 | return 2;
|
---|
[47b27b40] | 395 | }
|
---|
[a35b458] | 396 |
|
---|
[bd0e6a1] | 397 | printf("%s: Accepting connections\n", NAME);
|
---|
| 398 | task_retval(0);
|
---|
| 399 | async_manager();
|
---|
| 400 |
|
---|
| 401 | /* Not reached */
|
---|
| 402 | return 0;
|
---|
| 403 | }
|
---|