| 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 <stdlib.h>
|
|---|
| 30 | #include <errno.h>
|
|---|
| 31 | #include <loc.h>
|
|---|
| 32 | #include <stdio.h>
|
|---|
| 33 | #include <fibril_synch.h>
|
|---|
| 34 | #include <abi/ipc/methods.h>
|
|---|
| 35 | #include <inttypes.h>
|
|---|
| 36 | #include <io/log.h>
|
|---|
| 37 |
|
|---|
| 38 | #include <abi/fb/visuals.h>
|
|---|
| 39 | #include <adt/list.h>
|
|---|
| 40 | #include <io/mode.h>
|
|---|
| 41 | #include <io/pixelmap.h>
|
|---|
| 42 | #include <io/chargrid.h>
|
|---|
| 43 | #include <graph.h>
|
|---|
| 44 |
|
|---|
| 45 | #include "rfb.h"
|
|---|
| 46 |
|
|---|
| 47 | #define NAME "rfb"
|
|---|
| 48 |
|
|---|
| 49 | static vslmode_list_element_t pixel_mode;
|
|---|
| 50 | static visualizer_t *vis;
|
|---|
| 51 | static rfb_t rfb;
|
|---|
| 52 |
|
|---|
| 53 | static int rfb_claim(visualizer_t *vs)
|
|---|
| 54 | {
|
|---|
| 55 | return EOK;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | static int rfb_yield(visualizer_t *vs)
|
|---|
| 59 | {
|
|---|
| 60 | return EOK;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | static int rfb_suspend(visualizer_t *vs)
|
|---|
| 64 | {
|
|---|
| 65 | return EOK;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | static int rfb_wakeup(visualizer_t *vs)
|
|---|
| 69 | {
|
|---|
| 70 | return EOK;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | static int rfb_handle_damage_pixels(visualizer_t *vs,
|
|---|
| 74 | sysarg_t x0, sysarg_t y0, sysarg_t width, sysarg_t height,
|
|---|
| 75 | sysarg_t x_offset, sysarg_t y_offset)
|
|---|
| 76 | {
|
|---|
| 77 | fibril_mutex_lock(&rfb.lock);
|
|---|
| 78 |
|
|---|
| 79 | if (x0 + width > rfb.width || y0 + height > rfb.height) {
|
|---|
| 80 | fibril_mutex_unlock(&rfb.lock);
|
|---|
| 81 | return EINVAL;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | /* TODO update surface_t and use it */
|
|---|
| 85 | if (!rfb.damage_valid) {
|
|---|
| 86 | rfb.damage_rect.x = x0;
|
|---|
| 87 | rfb.damage_rect.y = y0;
|
|---|
| 88 | rfb.damage_rect.width = width;
|
|---|
| 89 | rfb.damage_rect.height = height;
|
|---|
| 90 | rfb.damage_valid = true;
|
|---|
| 91 | }
|
|---|
| 92 | else {
|
|---|
| 93 | if (x0 < rfb.damage_rect.x) {
|
|---|
| 94 | rfb.damage_rect.width += rfb.damage_rect.x - x0;
|
|---|
| 95 | rfb.damage_rect.x = x0;
|
|---|
| 96 | }
|
|---|
| 97 | if (y0 < rfb.damage_rect.y) {
|
|---|
| 98 | rfb.damage_rect.height += rfb.damage_rect.y - y0;
|
|---|
| 99 | rfb.damage_rect.y = y0;
|
|---|
| 100 | }
|
|---|
| 101 | sysarg_t x1 = x0 + width;
|
|---|
| 102 | sysarg_t dx1 = rfb.damage_rect.x + rfb.damage_rect.width;
|
|---|
| 103 | if (x1 > dx1) {
|
|---|
| 104 | rfb.damage_rect.width += x1 - dx1;
|
|---|
| 105 | }
|
|---|
| 106 | sysarg_t y1 = y0 + height;
|
|---|
| 107 | sysarg_t dy1 = rfb.damage_rect.y + rfb.damage_rect.height;
|
|---|
| 108 | if (y1 > dy1) {
|
|---|
| 109 | rfb.damage_rect.height += y1 - dy1;
|
|---|
| 110 | }
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | pixelmap_t *map = &vs->cells;
|
|---|
| 114 |
|
|---|
| 115 | for (sysarg_t y = y0; y < height + y0; ++y) {
|
|---|
| 116 | for (sysarg_t x = x0; x < width + x0; ++x) {
|
|---|
| 117 | pixel_t pix = pixelmap_get_pixel(map, (x + x_offset) % map->width,
|
|---|
| 118 | (y + y_offset) % map->height);
|
|---|
| 119 | pixelmap_put_pixel(&rfb.framebuffer, x, y, pix);
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | fibril_mutex_unlock(&rfb.lock);
|
|---|
| 124 | return EOK;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | static int rfb_change_mode(visualizer_t *vs, vslmode_t new_mode)
|
|---|
| 128 | {
|
|---|
| 129 | return EOK;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | static visualizer_ops_t rfb_ops = {
|
|---|
| 133 | .claim = rfb_claim,
|
|---|
| 134 | .yield = rfb_yield,
|
|---|
| 135 | .change_mode = rfb_change_mode,
|
|---|
| 136 | .handle_damage = rfb_handle_damage_pixels,
|
|---|
| 137 | .suspend = rfb_suspend,
|
|---|
| 138 | .wakeup = rfb_wakeup
|
|---|
| 139 | };
|
|---|
| 140 |
|
|---|
| 141 | static void syntax_print(void)
|
|---|
| 142 | {
|
|---|
| 143 | fprintf(stderr, "Usage: %s <name> <width> <height> [port]\n", NAME);
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | static void client_connection(ipc_callid_t callid, ipc_call_t *call, void *data)
|
|---|
| 147 | {
|
|---|
| 148 | graph_visualizer_connection(vis, callid, call, data);
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | static int socket_fibril(void *unused)
|
|---|
| 152 | {
|
|---|
| 153 | rfb_accept(&rfb);
|
|---|
| 154 |
|
|---|
| 155 | /* Not reached */
|
|---|
| 156 | return EOK;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | int main(int argc, char **argv)
|
|---|
| 160 | {
|
|---|
| 161 | log_init(NAME);
|
|---|
| 162 |
|
|---|
| 163 | if (argc <= 3) {
|
|---|
| 164 | syntax_print();
|
|---|
| 165 | return 1;
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | const char *rfb_name = argv[1];
|
|---|
| 169 |
|
|---|
| 170 | char *endptr;
|
|---|
| 171 | unsigned long width = strtoul(argv[2], &endptr, 0);
|
|---|
| 172 | if (*endptr != 0) {
|
|---|
| 173 | fprintf(stderr, "Invalid width\n");
|
|---|
| 174 | syntax_print();
|
|---|
| 175 | return 1;
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | unsigned long height = strtoul(argv[3], &endptr, 0);
|
|---|
| 179 | if (*endptr != 0) {
|
|---|
| 180 | fprintf(stderr, "Invalid height\n");
|
|---|
| 181 | syntax_print();
|
|---|
| 182 | return 1;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | unsigned long port = 5900;
|
|---|
| 186 | if (argc > 4) {
|
|---|
| 187 | port = strtoul(argv[4], &endptr, 0);
|
|---|
| 188 | if (*endptr != 0) {
|
|---|
| 189 | fprintf(stderr, "Invalid port number\n");
|
|---|
| 190 | syntax_print();
|
|---|
| 191 | return 1;
|
|---|
| 192 | }
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | rfb_init(&rfb, width, height, rfb_name);
|
|---|
| 196 |
|
|---|
| 197 | vis = malloc(sizeof(visualizer_t));
|
|---|
| 198 | if (vis == NULL) {
|
|---|
| 199 | fprintf(stderr, "Failed allocating visualizer struct\n");
|
|---|
| 200 | return 3;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | graph_init_visualizer(vis);
|
|---|
| 204 |
|
|---|
| 205 | pixel_mode.mode.index = 0;
|
|---|
| 206 | pixel_mode.mode.version = 0;
|
|---|
| 207 | pixel_mode.mode.refresh_rate = 0;
|
|---|
| 208 | pixel_mode.mode.screen_aspect.width = rfb.width;
|
|---|
| 209 | pixel_mode.mode.screen_aspect.height = rfb.height;
|
|---|
| 210 | pixel_mode.mode.screen_width = rfb.width;
|
|---|
| 211 | pixel_mode.mode.screen_height = rfb.height;
|
|---|
| 212 | pixel_mode.mode.cell_aspect.width = 1;
|
|---|
| 213 | pixel_mode.mode.cell_aspect.height = 1;
|
|---|
| 214 | pixel_mode.mode.cell_visual.pixel_visual = VISUAL_RGB_8_8_8;
|
|---|
| 215 |
|
|---|
| 216 | link_initialize(&pixel_mode.link);
|
|---|
| 217 | list_append(&pixel_mode.link, &vis->modes);
|
|---|
| 218 |
|
|---|
| 219 | vis->def_mode_idx = 0;
|
|---|
| 220 |
|
|---|
| 221 | vis->ops = rfb_ops;
|
|---|
| 222 | vis->dev_ctx = NULL;
|
|---|
| 223 |
|
|---|
| 224 | async_set_client_connection(client_connection);
|
|---|
| 225 |
|
|---|
| 226 | int rc = loc_server_register(NAME);
|
|---|
| 227 | if (rc != EOK) {
|
|---|
| 228 | printf("%s: Unable to register server.\n", NAME);
|
|---|
| 229 | return rc;
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | char *service_name;
|
|---|
| 233 | rc = asprintf(&service_name, "rfb/%s", rfb_name);
|
|---|
| 234 | if (rc < 0) {
|
|---|
| 235 | printf(NAME ": Unable to create service name\n");
|
|---|
| 236 | return rc;
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | service_id_t service_id;
|
|---|
| 240 |
|
|---|
| 241 | rc = loc_service_register(service_name, &service_id);
|
|---|
| 242 | if (rc != EOK) {
|
|---|
| 243 | printf(NAME ": Unable to register service %s.\n", service_name);
|
|---|
| 244 | return rc;
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | free(service_name);
|
|---|
| 248 |
|
|---|
| 249 | category_id_t visualizer_category;
|
|---|
| 250 | rc = loc_category_get_id("visualizer", &visualizer_category, IPC_FLAG_BLOCKING);
|
|---|
| 251 | if (rc != EOK) {
|
|---|
| 252 | fprintf(stderr, NAME ": Unable to get visualizer category id.\n");
|
|---|
| 253 | return 1;
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | rc = loc_service_add_to_cat(service_id, visualizer_category);
|
|---|
| 257 | if (rc != EOK) {
|
|---|
| 258 | fprintf(stderr, NAME ": Unable to add service to visualizer category.\n");
|
|---|
| 259 | return 1;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | rc = rfb_listen(&rfb, port);
|
|---|
| 263 | if (rc != EOK) {
|
|---|
| 264 | fprintf(stderr, NAME ": Unable to listen at rfb port\n");
|
|---|
| 265 | return 2;
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | fid_t fib = fibril_create(socket_fibril, NULL);
|
|---|
| 269 | if (!fib) {
|
|---|
| 270 | fprintf(stderr, NAME ": Unable to create socket fibril.\n");
|
|---|
| 271 | return 2;
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | fibril_add_ready(fib);
|
|---|
| 275 |
|
|---|
| 276 | printf("%s: Accepting connections\n", NAME);
|
|---|
| 277 | task_retval(0);
|
|---|
| 278 | async_manager();
|
|---|
| 279 |
|
|---|
| 280 | /* Not reached */
|
|---|
| 281 | return 0;
|
|---|
| 282 | }
|
|---|