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