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