wok annotate busybox/stuff/busybox-1.23-fbvnc.u @ rev 19981

Up gphoto2 (2.5.14), thanks Pasquale
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun Jun 18 19:23:12 2017 +0200 (2017-06-18)
parents 2be6ee0ddc5a
children
rev   line source
pascal@19350 1 text data bss dec hex filename
pascal@19350 2 3122 0 0 3122 c32 util-linux/fbvnc.o
pascal@19350 3 --- /dev/null
pascal@19350 4 +++ busybox/util-linux/fbvnc.c
pascal@19350 5 @@ -0,0 +1,551 @@
pascal@19350 6 +/* vi: set sw=4 ts=4: */
pascal@19350 7 +/*
pascal@19350 8 + * A small linux framebuffer VNC viewer
pascal@19350 9 + *
pascal@19350 10 + * pascal.bellard@ads-lu.com
pascal@19350 11 + *
pascal@19350 12 + * Based on Ali Gholami Rudi's fbvnc.c
pascal@19350 13 + * http://repo.or.cz/w/fbvnc.git
pascal@19350 14 + *
pascal@19350 15 + * Licensed under GPLv2 or later, see file LICENSE in this source tree.
pascal@19350 16 + */
pascal@19350 17 +
pascal@19350 18 +//applet:IF_FBVNC(APPLET(fbvnc, BB_DIR_BIN, BB_SUID_DROP))
pascal@19350 19 +
pascal@19350 20 +//kbuild:lib-$(CONFIG_FBVNC) += fbvnc.o
pascal@19350 21 +
pascal@19350 22 +//config:config FBVNC
pascal@19350 23 +//config: bool "fbvnc"
pascal@19350 24 +//config: default n
pascal@19350 25 +//config: depends on PLATFORM_LINUX
pascal@19350 26 +//config: help
pascal@19350 27 +//config: A linux framebuffer VNC viewer.
pascal@19350 28 +
pascal@19350 29 +//usage:#define fbvnc_trivial_usage
pascal@19350 30 +//usage: "[VNC_SERVER] [PORT]"
pascal@19350 31 +//usage:#define fbvnc_full_usage "\n\n"
pascal@19350 32 +//usage: "A linux framebuffer VNC viewer."
pascal@19350 33 +//usage: "\nTo exit, press any mouse button and press ESC."
pascal@19350 34 +
pascal@19350 35 +#include "libbb.h"
pascal@19350 36 +#include "vnc.h"
pascal@19350 37 +
pascal@19350 38 +/* Stuff stolen from the kernel's fb.h */
pascal@19350 39 +#define FB_ACTIVATE_ALL 64
pascal@19350 40 +enum {
pascal@19350 41 + FBIOGET_VSCREENINFO = 0x4600,
pascal@19350 42 + FBIOPUT_VSCREENINFO = 0x4601,
pascal@19350 43 + FBIOGET_FSCREENINFO = 0x4602,
pascal@19350 44 + FBIOGETCMAP = 0x4604,
pascal@19350 45 + FBIOPUTCMAP = 0x4605
pascal@19350 46 +};
pascal@19350 47 +
pascal@19350 48 +struct fb_bitfield {
pascal@19350 49 + uint32_t offset; /* beginning of bitfield */
pascal@19350 50 + uint32_t length; /* length of bitfield */
pascal@19350 51 + uint32_t msb_right; /* !=0: Most significant bit is right */
pascal@19350 52 +};
pascal@19350 53 +struct fb_var_screeninfo {
pascal@19350 54 + uint32_t xres; /* visible resolution */
pascal@19350 55 + uint32_t yres;
pascal@19350 56 + uint32_t xres_virtual; /* virtual resolution */
pascal@19350 57 + uint32_t yres_virtual;
pascal@19350 58 + uint32_t xoffset; /* offset from virtual to visible */
pascal@19350 59 + uint32_t yoffset; /* resolution */
pascal@19350 60 +
pascal@19350 61 + uint32_t bits_per_pixel;
pascal@19350 62 + uint32_t grayscale; /* !=0 Graylevels instead of colors */
pascal@19350 63 +
pascal@19350 64 + struct fb_bitfield red; /* bitfield in fb mem if true color, */
pascal@19350 65 + struct fb_bitfield green; /* else only length is significant */
pascal@19350 66 + struct fb_bitfield blue;
pascal@19350 67 + struct fb_bitfield transp; /* transparency */
pascal@19350 68 +
pascal@19350 69 + uint32_t nonstd; /* !=0 Non standard pixel format */
pascal@19350 70 +
pascal@19350 71 + uint32_t activate; /* see FB_ACTIVATE_x */
pascal@19350 72 +
pascal@19350 73 + uint32_t height; /* height of picture in mm */
pascal@19350 74 + uint32_t width; /* width of picture in mm */
pascal@19350 75 +
pascal@19350 76 + uint32_t accel_flags; /* acceleration flags (hints) */
pascal@19350 77 +
pascal@19350 78 + /* Timing: All values in pixclocks, except pixclock (of course) */
pascal@19350 79 + uint32_t pixclock; /* pixel clock in ps (pico seconds) */
pascal@19350 80 + uint32_t left_margin; /* time from sync to picture */
pascal@19350 81 + uint32_t right_margin; /* time from picture to sync */
pascal@19350 82 + uint32_t upper_margin; /* time from sync to picture */
pascal@19350 83 + uint32_t lower_margin;
pascal@19350 84 + uint32_t hsync_len; /* length of horizontal sync */
pascal@19350 85 + uint32_t vsync_len; /* length of vertical sync */
pascal@19350 86 + uint32_t sync; /* see FB_SYNC_x */
pascal@19350 87 + uint32_t vmode; /* see FB_VMODE_x */
pascal@19350 88 + uint32_t reserved[6]; /* Reserved for future compatibility */
pascal@19350 89 +};
pascal@19350 90 +
pascal@19350 91 +#define DEFAULTFBDEV FB_0
pascal@19350 92 +
pascal@19350 93 +struct fb_fix_screeninfo {
pascal@19350 94 + char id[16]; /* identification string eg "TT Builtin" */
pascal@19350 95 + unsigned long smem_start; /* Start of frame buffer mem */
pascal@19350 96 + /* (physical address) */
pascal@19350 97 + uint32_t smem_len; /* Length of frame buffer mem */
pascal@19350 98 + uint32_t type; /* see FB_TYPE_* */
pascal@19350 99 + uint32_t type_aux; /* Interleave for interleaved Planes */
pascal@19350 100 + uint32_t visual; /* see FB_VISUAL_* */
pascal@19350 101 + uint16_t xpanstep; /* zero if no hardware panning */
pascal@19350 102 + uint16_t ypanstep; /* zero if no hardware panning */
pascal@19350 103 + uint16_t ywrapstep; /* zero if no hardware ywrap */
pascal@19350 104 + uint32_t line_length; /* length of a line in bytes */
pascal@19350 105 + unsigned long mmio_start; /* Start of Memory Mapped I/O */
pascal@19350 106 + /* (physical address) */
pascal@19350 107 + uint32_t mmio_len; /* Length of Memory Mapped I/O */
pascal@19350 108 + uint32_t accel; /* Indicate to driver which */
pascal@19350 109 + /* specific chip/card we have */
pascal@19350 110 + uint16_t reserved[3]; /* Reserved for future compatibility */
pascal@19350 111 +};
pascal@19350 112 +
pascal@19350 113 +struct fb_cmap {
pascal@19350 114 + uint32_t start; /* First entry */
pascal@19350 115 + uint32_t len; /* Number of entries */
pascal@19350 116 + uint16_t *red; /* Red values */
pascal@19350 117 + uint16_t *green;
pascal@19350 118 + uint16_t *blue;
pascal@19350 119 + uint16_t *transp; /* transparency, can be NULL */
pascal@19350 120 +};
pascal@19350 121 +
pascal@19350 122 +#define FB_VISUAL_TRUECOLOR 2 /* True color */
pascal@19350 123 +
pascal@19350 124 +#define COLORLEVELS (1 << 8)
pascal@19350 125 +
pascal@19350 126 +struct scroll_data {
pascal@19350 127 + int size;
pascal@19350 128 + int srv_size;
pascal@19350 129 + int offset;
pascal@19350 130 + int pos;
pascal@19350 131 +};
pascal@19350 132 +
pascal@19350 133 +struct globals {
pascal@19350 134 + struct termios term_orig;
pascal@19350 135 + struct pollfd ufds[3];
pascal@19350 136 +#define kbd_fd ufds[0].fd
pascal@19350 137 +#define vnc_fd ufds[1].fd
pascal@19350 138 +#define rat_fd ufds[2].fd
pascal@19350 139 + struct scroll_data scroll[2];
pascal@19350 140 +#define cols scroll[0].size
pascal@19350 141 +#define srv_cols scroll[0].srv_size
pascal@19350 142 +#define oc scroll[0].offset
pascal@19350 143 +#define mc scroll[0].pos
pascal@19350 144 +#define rows scroll[1].size
pascal@19350 145 +#define srv_rows scroll[1].srv_size
pascal@19350 146 +#define or scroll[1].offset
pascal@19350 147 +#define mr scroll[1].pos
pascal@19350 148 + char rat_buttons;
pascal@19350 149 + int fb_fd;
pascal@19350 150 + void *fb_ptr;
pascal@19350 151 + int bpp;
pascal@19350 152 + int nr, ng, nb;
pascal@19350 153 + struct fb_var_screeninfo vinfo;
pascal@19350 154 + struct fb_fix_screeninfo finfo;
pascal@19350 155 + unsigned short red[COLORLEVELS], green[COLORLEVELS], blue[COLORLEVELS];
pascal@19350 156 +};
pascal@19350 157 +
pascal@19350 158 +#define G (*ptr_to_globals)
pascal@19350 159 +#define INIT_G() do { \
pascal@19350 160 + SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
pascal@19350 161 +} while (0)
pascal@19350 162 +
pascal@19350 163 +static int fb_len(void)
pascal@19350 164 +{
pascal@19350 165 + return G.finfo.line_length * G.vinfo.yres_virtual;
pascal@19350 166 +}
pascal@19350 167 +
pascal@19350 168 +static void fb_ioctl_cmap(int fct, struct fb_cmap *cmap)
pascal@19350 169 +{
pascal@19350 170 + if (G.finfo.visual == FB_VISUAL_TRUECOLOR)
pascal@19350 171 + return;
pascal@19350 172 + cmap->start = 0;
pascal@19350 173 + cmap->len = MAX(G.nr, MAX(G.ng, G.nb));
pascal@19350 174 + cmap->transp = NULL;
pascal@19350 175 + xioctl(G.fb_fd, fct, cmap);
pascal@19350 176 +}
pascal@19350 177 +
pascal@19350 178 +static void fb_cmap_save(int save)
pascal@19350 179 +{
pascal@19350 180 + struct fb_cmap cmap;
pascal@19350 181 +
pascal@19350 182 + cmap.red = G.red;
pascal@19350 183 + cmap.green = G.green;
pascal@19350 184 + cmap.blue = G.blue;
pascal@19350 185 + fb_ioctl_cmap(save ? FBIOGETCMAP : FBIOPUTCMAP, &cmap);
pascal@19350 186 +}
pascal@19350 187 +
pascal@19350 188 +static void fb_build_cmap(unsigned short *color, int n)
pascal@19350 189 +{
pascal@19350 190 + int i, inc = 65535 / (n - 1);
pascal@19350 191 +
pascal@19350 192 + for (i = 0; n--; i += inc)
pascal@19350 193 + *color++ = i;
pascal@19350 194 +}
pascal@19350 195 +
pascal@19350 196 +static void fb_cmap(void)
pascal@19350 197 +{
pascal@19350 198 + unsigned short red[COLORLEVELS], green[COLORLEVELS], blue[COLORLEVELS];
pascal@19350 199 + struct fb_cmap cmap;
pascal@19350 200 +
pascal@19350 201 + fb_build_cmap(cmap.red = red, G.nr);
pascal@19350 202 + fb_build_cmap(cmap.green = green, G.ng);
pascal@19350 203 + fb_build_cmap(cmap.blue = blue, G.nb);
pascal@19350 204 + fb_ioctl_cmap(FBIOPUTCMAP, &cmap);
pascal@19350 205 +}
pascal@19350 206 +
pascal@19350 207 +static void fb_init(void)
pascal@19350 208 +{
pascal@19350 209 + G.fb_fd = xopen(DEFAULTFBDEV, O_RDWR);
pascal@19350 210 + xioctl(G.fb_fd, FBIOGET_VSCREENINFO, &G.vinfo);
pascal@19350 211 + xioctl(G.fb_fd, FBIOGET_FSCREENINFO, &G.finfo);
pascal@19350 212 + close_on_exec_on(G.fb_fd);
pascal@19350 213 + G.fb_ptr = mmap(NULL, fb_len(), PROT_READ | PROT_WRITE, MAP_SHARED, G.fb_fd, 0);
pascal@19350 214 + if (G.fb_ptr == MAP_FAILED)
pascal@19350 215 + bb_perror_msg_and_die("mmap");
pascal@19350 216 + G.bpp = (G.vinfo.bits_per_pixel + 7) >> 3;
pascal@19350 217 + G.nr = 1 << G.vinfo.red.length;
pascal@19350 218 + G.nb = 1 << G.vinfo.blue.length;
pascal@19350 219 + G.ng = 1 << G.vinfo.green.length;
pascal@19350 220 + fb_cmap_save(1);
pascal@19350 221 + fb_cmap();
pascal@19350 222 +}
pascal@19350 223 +
pascal@19350 224 +static void fb_free(void)
pascal@19350 225 +{
pascal@19350 226 + fb_cmap_save(0);
pascal@19350 227 + munmap(G.fb_ptr, fb_len());
pascal@19350 228 + close(G.fb_fd);
pascal@19350 229 +}
pascal@19350 230 +
pascal@19350 231 +#define fb_rows vinfo.yres
pascal@19350 232 +#define fb_cols vinfo.xres
pascal@19350 233 +
pascal@19350 234 +static void fb_set(int r, int c, void *mem, int len)
pascal@19350 235 +{
pascal@19350 236 + memcpy(G.fb_ptr + (r + G.vinfo.yoffset) * G.finfo.line_length +
pascal@19350 237 + (c + G.vinfo.xoffset) * G.bpp, mem, len * G.bpp);
pascal@19350 238 +}
pascal@19350 239 +
pascal@19350 240 +#define line_buffer bb_common_bufsiz1
pascal@19350 241 +#define MAXPIX (sizeof(line_buffer)/sizeof(uint32_t))
pascal@19350 242 +
pascal@19350 243 +static void skip(int len)
pascal@19350 244 +{
pascal@19350 245 + int n;
pascal@19350 246 + while (len > 0 && (n = read(G.vnc_fd, line_buffer,
pascal@19350 247 + MIN(len, sizeof(line_buffer)))) > 0)
pascal@19350 248 + len -= n;
pascal@19350 249 +}
pascal@19350 250 +
pascal@19350 251 +static void vnc_init(void)
pascal@19350 252 +{
pascal@19350 253 + struct vnc_client_init clientinit;
pascal@19350 254 + struct vnc_server_init serverinit;
pascal@19350 255 + struct vnc_client_pixelfmt pixfmt_cmd;
pascal@19350 256 + int connstat = VNC_CONN_FAILED;
pascal@19350 257 +
pascal@19350 258 + write(G.vnc_fd, "RFB 003.003\n", 12);
pascal@19350 259 + skip(12);
pascal@19350 260 +
pascal@19350 261 + xread(G.vnc_fd, &connstat, sizeof(connstat));
pascal@19350 262 +
pascal@19350 263 + if (ntohl(connstat) != VNC_CONN_NOAUTH)
pascal@19350 264 + bb_perror_msg_and_die("vnc auth");
pascal@19350 265 +
pascal@19350 266 + clientinit.shared = 1;
pascal@19350 267 + write(G.vnc_fd, &clientinit, sizeof(clientinit));
pascal@19350 268 + read(G.vnc_fd, &serverinit, sizeof(serverinit));
pascal@19350 269 +
pascal@19350 270 + fb_init();
pascal@19350 271 + G.srv_cols = ntohs(serverinit.w);
pascal@19350 272 + G.srv_rows = ntohs(serverinit.h);
pascal@19350 273 + G.cols = MIN(G.srv_cols, G.fb_cols);
pascal@19350 274 + G.rows = MIN(G.srv_rows, G.fb_rows);
pascal@19350 275 + G.mr = G.rows / 2;
pascal@19350 276 + G.mc = G.cols / 2;
pascal@19350 277 +
pascal@19350 278 + skip(ntohl(serverinit.len));
pascal@19350 279 + pixfmt_cmd.type = VNC_CLIENT_PIXFMT;
pascal@19350 280 + pixfmt_cmd.format.bigendian = 0;
pascal@19350 281 + pixfmt_cmd.format.truecolor = 1;
pascal@19350 282 + pixfmt_cmd.format.bpp =
pascal@19350 283 + pixfmt_cmd.format.depth = G.bpp << 3;
pascal@19350 284 + pixfmt_cmd.format.rmax = htons(G.nr - 1);
pascal@19350 285 + pixfmt_cmd.format.gmax = htons(G.ng - 1);
pascal@19350 286 + pixfmt_cmd.format.bmax = htons(G.nb - 1);
pascal@19350 287 + pixfmt_cmd.format.rshl = G.vinfo.red.offset;
pascal@19350 288 + pixfmt_cmd.format.gshl = G.vinfo.green.offset;
pascal@19350 289 + pixfmt_cmd.format.bshl = G.vinfo.blue.offset;
pascal@19350 290 + write(G.vnc_fd, &pixfmt_cmd, sizeof(pixfmt_cmd));
pascal@19350 291 +}
pascal@19350 292 +
pascal@19350 293 +static void vnc_refresh(int inc)
pascal@19350 294 +{
pascal@19350 295 + struct vnc_client_fbup fbup_req;
pascal@19350 296 + fbup_req.type = VNC_CLIENT_FBUP;
pascal@19350 297 + fbup_req.inc = inc;
pascal@19350 298 + fbup_req.x = htons(G.oc);
pascal@19350 299 + fbup_req.y = htons(G.or);
pascal@19350 300 + fbup_req.w = htons(G.oc + G.cols);
pascal@19350 301 + fbup_req.h = htons(G.or + G.rows);
pascal@19350 302 + write(G.vnc_fd, &fbup_req, sizeof(fbup_req));
pascal@19350 303 +}
pascal@19350 304 +
pascal@19350 305 +static void cleanup(void)
pascal@19350 306 +{
pascal@19350 307 +#define RESETSTR "\x1b[?25h\x1b[2J\x1b[H"
pascal@19350 308 + fb_free();
pascal@19350 309 + tcsetattr_stdin_TCSANOW(&G.term_orig);
pascal@19350 310 + write(STDOUT_FILENO, RESETSTR, sizeof(RESETSTR));
pascal@19350 311 + if (ENABLE_FEATURE_CLEAN_UP) {
pascal@19350 312 + close(G.vnc_fd);
pascal@19350 313 + close(G.rat_fd);
pascal@19350 314 + }
pascal@19350 315 +}
pascal@19350 316 +
pascal@19350 317 +static void killed(int code) NORETURN;
pascal@19350 318 +static void killed(int code)
pascal@19350 319 +{
pascal@19350 320 + cleanup();
pascal@19350 321 + if (code > EXIT_FAILURE)
pascal@19350 322 + kill_myself_with_sig(code);
pascal@19350 323 + exit(code);
pascal@19350 324 +}
pascal@19350 325 +
pascal@19350 326 +static void vnc_event(void)
pascal@19350 327 +{
pascal@19350 328 + struct vnc_rect uprect;
pascal@19350 329 + union {
pascal@19350 330 + struct vnc_server_fbup fbup;
pascal@19350 331 + struct vnc_server_cuttext cuttext;
pascal@19350 332 + struct vnc_server_colormap colormap;
pascal@19350 333 + } msg;
pascal@19350 334 + int n;
pascal@19350 335 +
pascal@19350 336 + switch (xread_char(G.vnc_fd)) {
pascal@19350 337 + case VNC_SERVER_FBUP:
pascal@19350 338 + xread(G.vnc_fd, &msg.fbup.pad, sizeof(msg.fbup) - 1);
pascal@19350 339 + n = ntohs(msg.fbup.n);
pascal@19350 340 + while (n--) {
pascal@19350 341 + int x, y, w, h, l, i;
pascal@19350 342 + xread(G.vnc_fd, &uprect, sizeof(uprect));
pascal@19350 343 + if (uprect.enc != 0)
pascal@19350 344 + killed(1);
pascal@19350 345 + i = 0;
pascal@19350 346 + x = ntohs(uprect.x) - G.oc;
pascal@19350 347 + y = ntohs(uprect.y) - G.or;
pascal@19350 348 + w = ntohs(uprect.w);
pascal@19350 349 + h = ntohs(uprect.h);
pascal@19350 350 + l = MIN(w, G.cols - x);
pascal@19350 351 + if (x < 0) {
pascal@19350 352 + l = MIN(w + x, G.cols);
pascal@19350 353 + i = -x;
pascal@19350 354 + x = 0;
pascal@19350 355 + }
pascal@19350 356 + for (; h--; y++) {
pascal@19350 357 + int a, b, c = i;
pascal@19350 358 + for (a = b = 0; w > b; b += a, c = 0) {
pascal@19350 359 + int len;
pascal@19350 360 + a = MIN(w - b, MAXPIX);
pascal@19350 361 + len = MIN(a, l - b) - c;
pascal@19350 362 + xread(G.vnc_fd, line_buffer, a * G.bpp);
pascal@19350 363 + if (y >= 0 && y < G.rows && len > 0)
pascal@19350 364 + fb_set(y, x + b,
pascal@19350 365 + line_buffer + (c * G.bpp),
pascal@19350 366 + len);
pascal@19350 367 + }
pascal@19350 368 + }
pascal@19350 369 + }
pascal@19350 370 + break;
pascal@19350 371 + case VNC_SERVER_BELL:
pascal@19350 372 + break;
pascal@19350 373 + case VNC_SERVER_CUTTEXT:
pascal@19350 374 + xread(G.vnc_fd, &msg.cuttext.pad1, sizeof(msg.cuttext) - 1);
pascal@19350 375 + skip(ntohl(msg.cuttext.len));
pascal@19350 376 + break;
pascal@19350 377 + case VNC_SERVER_COLORMAP:
pascal@19350 378 + xread(G.vnc_fd, &msg.colormap.pad, sizeof(msg.colormap) - 1);
pascal@19350 379 + skip(ntohs(msg.colormap.n) * 3 * 2);
pascal@19350 380 + break;
pascal@19350 381 + default:
pascal@19350 382 + killed(1);
pascal@19350 383 + }
pascal@19350 384 +}
pascal@19350 385 +
pascal@19350 386 +static int update_scroll(struct scroll_data *s)
pascal@19350 387 +{
pascal@19350 388 + int shift = s->size / 5;
pascal@19350 389 + int max = s->srv_size - s->size;
pascal@19350 390 + int status = 0;
pascal@19350 391 + if (s->pos < s->offset) {
pascal@19350 392 + if ((s->offset -= shift) < 0)
pascal@19350 393 + s->offset = 0;
pascal@19350 394 + }
pascal@19350 395 + else if (s->pos >= s->offset + s->size && s->offset < max) {
pascal@19350 396 + if ((s->offset += shift) > max)
pascal@19350 397 + s->offset = max;
pascal@19350 398 + }
pascal@19350 399 + else status++;
pascal@19350 400 + s->pos = MAX(s->offset, MIN(s->offset + s->size - 1, s->pos));
pascal@19350 401 + return status;
pascal@19350 402 +}
pascal@19350 403 +
pascal@19350 404 +static void rat_event(void)
pascal@19350 405 +{
pascal@19350 406 + static u8 btn2vnc[8] = {
pascal@19350 407 + 0, VNC_BUTTON1_MASK, VNC_BUTTON3_MASK,
pascal@19350 408 + VNC_BUTTON1_MASK + VNC_BUTTON3_MASK, VNC_BUTTON2_MASK,
pascal@19350 409 + VNC_BUTTON1_MASK + VNC_BUTTON2_MASK,
pascal@19350 410 + VNC_BUTTON2_MASK + VNC_BUTTON3_MASK,
pascal@19350 411 + VNC_BUTTON1_MASK + VNC_BUTTON2_MASK + VNC_BUTTON3_MASK
pascal@19350 412 + };
pascal@19350 413 + signed char ie[4];
pascal@19350 414 + struct vnc_client_ratevent me = {VNC_CLIENT_RATEVENT};
pascal@19350 415 + int refresh;
pascal@19350 416 +
pascal@19350 417 + xread(G.rat_fd, &ie, sizeof(ie));
pascal@19350 418 + G.mc += ie[1];
pascal@19350 419 + G.mr -= ie[2];
pascal@19350 420 + refresh = 2 - update_scroll(&G.scroll[0]) - update_scroll(&G.scroll[1]);
pascal@19611 421 + me.mask = btn2vnc[(int)(G.rat_buttons = ie[0] & 7)];
pascal@19350 422 + if (ie[3] > 0) /* wheel up */
pascal@19350 423 + me.mask |= VNC_BUTTON4_MASK;
pascal@19350 424 + if (ie[3] < 0) /* wheel down */
pascal@19350 425 + me.mask |= VNC_BUTTON5_MASK;
pascal@19350 426 + me.y = htons(G.mr);
pascal@19350 427 + me.x = htons(G.mc);
pascal@19350 428 + write(G.vnc_fd, &me, sizeof(me));
pascal@19350 429 + if (refresh)
pascal@19350 430 + vnc_refresh(0);
pascal@19350 431 +}
pascal@19350 432 +
pascal@19350 433 +static int press(int key, int down)
pascal@19350 434 +{
pascal@19350 435 + struct vnc_client_keyevent ke = {VNC_CLIENT_KEYEVENT};
pascal@19350 436 + ke.key = htonl(key);
pascal@19350 437 + ke.down = down;
pascal@19350 438 + return write(G.vnc_fd, &ke, sizeof(ke));
pascal@19350 439 +}
pascal@19350 440 +
pascal@19350 441 +static void kbd_event(void)
pascal@19350 442 +{
pascal@19350 443 + char key[1024];
pascal@19350 444 + int i, nr;
pascal@19350 445 +
pascal@19350 446 + if ((nr = read(0, key, sizeof(key))) <= 0 )
pascal@19350 447 + killed(1);
pascal@19350 448 + for (i = 0; i < nr; i++) {
pascal@19350 449 + int j, k;
pascal@19350 450 + int mod[4];
pascal@19350 451 + int nmod;
pascal@19350 452 +
pascal@19350 453 + k = nmod = 0;
pascal@19350 454 + switch (key[i]) {
pascal@19350 455 + case 0x08:
pascal@19350 456 + case 0x7f:
pascal@19350 457 + k = 0xff08;
pascal@19350 458 + break;
pascal@19350 459 + case 0x1b:
pascal@19350 460 + if (G.rat_buttons)
pascal@19350 461 + killed(0);
pascal@19350 462 + if (i + 2 < nr && key[i + 1] == '[') {
pascal@19350 463 + static const char arr2vnc[] = "HDACB";
pascal@19350 464 + char *p = strchr(arr2vnc, key[i + 2]);
pascal@19350 465 +
pascal@19350 466 + if (p) {
pascal@19350 467 + k = p - arr2vnc + 0xff50;
pascal@19350 468 + i += 2;
pascal@19350 469 + break;
pascal@19350 470 + }
pascal@19350 471 + }
pascal@19350 472 + if (i + 1 < nr) {
pascal@19350 473 + mod[nmod++] = 0xffe9;
pascal@19350 474 + i++;
pascal@19350 475 + }
pascal@19350 476 + case 0x09:
pascal@19350 477 + case 0x0d:
pascal@19350 478 + k = 0xff00;
pascal@19350 479 + goto getkey;
pascal@19350 480 + case 0x0c: /* Mouse button + ^L: redraw */
pascal@19350 481 + if (G.rat_buttons) {
pascal@19350 482 + vnc_refresh(0);
pascal@19350 483 + continue;
pascal@19350 484 + }
pascal@19350 485 + default:
pascal@19350 486 + getkey:
pascal@19350 487 + k += (unsigned char) key[i];
pascal@19350 488 + }
pascal@19350 489 + if ((k >= 'A' && k <= 'Z') || strchr(":\"<>?{}|+_()*&^%$#@!~", k))
pascal@19350 490 + mod[nmod++] = 0xffe1;
pascal@19350 491 + if (k >= 1 && k <= 26) {
pascal@19350 492 + k += 'a' - 1;
pascal@19350 493 + mod[nmod++] = 0xffe3;
pascal@19350 494 + }
pascal@19350 495 + mod[nmod] = k;
pascal@19350 496 + for (j = 0; j <= nmod; j++)
pascal@19350 497 + press(mod[j], 1);
pascal@19350 498 + press(k, 0);
pascal@19350 499 + for (j = 0; j < nmod; j++)
pascal@19350 500 + press(mod[j], 0);
pascal@19350 501 + }
pascal@19350 502 +}
pascal@19350 503 +
pascal@19350 504 +static void term_setup(void)
pascal@19350 505 +{
pascal@19350 506 + struct termios termios;
pascal@19350 507 +#define INITSTR "\x1b[?25l\x1b[2J\x1b[H** fbvnc **"
pascal@19350 508 +
pascal@19350 509 + write(STDOUT_FILENO, INITSTR, sizeof(INITSTR));
pascal@19350 510 + tcgetattr (STDIN_FILENO, &termios);
pascal@19350 511 + G.term_orig = termios;
pascal@19350 512 + cfmakeraw(&termios);
pascal@19350 513 + tcsetattr_stdin_TCSANOW(&termios);
pascal@19350 514 +}
pascal@19350 515 +
pascal@19350 516 +int fbvnc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
pascal@19350 517 +int fbvnc_main(int argc, char **argv)
pascal@19350 518 +{
pascal@19350 519 + char *host = (char *) "127.0.0.1";
pascal@19350 520 + int port, pending = 0;
pascal@19350 521 +
pascal@19350 522 + INIT_G();
pascal@19350 523 + if (argc >= 2)
pascal@19350 524 + host = argv[1];
pascal@19350 525 + port = bb_lookup_port((argc >= 3) ? argv[2] : "vnc", "tcp", 5900);
pascal@19350 526 + G.vnc_fd = create_and_connect_stream_or_die(host, port);
pascal@19350 527 + vnc_init();
pascal@19350 528 + G.rat_fd = open("/dev/input/mice", O_RDWR);
pascal@19350 529 + write(G.rat_fd, "\xf3\xc8\xf3\x64\xf3\x50", 6); /* for using mouse wheel */
pascal@19350 530 + read(G.rat_fd, line_buffer, 1);
pascal@19350 531 + term_setup();
pascal@19350 532 + atexit(cleanup);
pascal@19350 533 + bb_signals(BB_FATAL_SIGS, killed);
pascal@19350 534 +
pascal@19350 535 + G.ufds[0].events =
pascal@19350 536 + G.ufds[1].events =
pascal@19350 537 + G.ufds[2].events = POLLIN;
pascal@19350 538 + vnc_refresh(0);
pascal@19350 539 + while (1) {
pascal@19350 540 + int status = poll(G.ufds, 3, 500);
pascal@19350 541 + if (status == -1 && errno != EINTR)
pascal@19350 542 + killed(1);
pascal@19350 543 + if (!status)
pascal@19350 544 + continue;
pascal@19350 545 + if (G.ufds[0].revents & POLLIN)
pascal@19350 546 + kbd_event();
pascal@19350 547 + if (G.ufds[1].revents & POLLIN) {
pascal@19350 548 + vnc_event();
pascal@19350 549 + pending = 0;
pascal@19350 550 + }
pascal@19350 551 + if (G.ufds[2].revents & POLLIN)
pascal@19350 552 + rat_event();
pascal@19350 553 + if (!pending++)
pascal@19350 554 + vnc_refresh(1);
pascal@19350 555 + }
pascal@19350 556 +}
pascal@19350 557 --- /dev/null
pascal@19350 558 +++ busybox/util-linux/vnc.h
pascal@19350 559 @@ -0,0 +1,124 @@
pascal@19350 560 +#define VNC_CONN_FAILED 0
pascal@19350 561 +#define VNC_CONN_NOAUTH 1
pascal@19350 562 +#define VNC_CONN_AUTH 2
pascal@19350 563 +
pascal@19350 564 +#define VNC_AUTH_OK 0
pascal@19350 565 +#define VNC_AUTH_FAILED 1
pascal@19350 566 +#define VNC_AUTH_TOOMANY 2
pascal@19350 567 +
pascal@19350 568 +#define VNC_SERVER_FBUP 0
pascal@19350 569 +#define VNC_SERVER_COLORMAP 1
pascal@19350 570 +#define VNC_SERVER_BELL 2
pascal@19350 571 +#define VNC_SERVER_CUTTEXT 3
pascal@19350 572 +
pascal@19350 573 +#define VNC_CLIENT_PIXFMT 0
pascal@19350 574 +#define VNC_CLIENT_COLORMAP 1
pascal@19350 575 +#define VNC_CLIENT_SETENC 2
pascal@19350 576 +#define VNC_CLIENT_FBUP 3
pascal@19350 577 +#define VNC_CLIENT_KEYEVENT 4
pascal@19350 578 +#define VNC_CLIENT_RATEVENT 5
pascal@19350 579 +#define VNC_CLIENT_CUTTEXT 6
pascal@19350 580 +
pascal@19350 581 +#define VNC_ENC_RAW 0
pascal@19350 582 +#define VNC_ENC_COPYRECT 1
pascal@19350 583 +#define VNC_ENC_RRE 2
pascal@19350 584 +#define VNC_ENC_CORRE 4
pascal@19350 585 +#define VNC_ENC_HEXTILE 5
pascal@19350 586 +
pascal@19350 587 +#define VNC_BUTTON1_MASK 0x01
pascal@19350 588 +#define VNC_BUTTON2_MASK 0x02
pascal@19350 589 +#define VNC_BUTTON3_MASK 0x04
pascal@19350 590 +#define VNC_BUTTON4_MASK 0x10
pascal@19350 591 +#define VNC_BUTTON5_MASK 0x08
pascal@19350 592 +
pascal@19350 593 +typedef unsigned char u8;
pascal@19350 594 +typedef unsigned short u16;
pascal@19350 595 +typedef unsigned int u32;
pascal@19350 596 +
pascal@19350 597 +struct vnc_pixelfmt {
pascal@19350 598 + u8 bpp;
pascal@19350 599 + u8 depth;
pascal@19350 600 + u8 bigendian;
pascal@19350 601 + u8 truecolor;
pascal@19350 602 + u16 rmax;
pascal@19350 603 + u16 gmax;
pascal@19350 604 + u16 bmax;
pascal@19350 605 + u8 rshl;
pascal@19350 606 + u8 gshl;
pascal@19350 607 + u8 bshl;
pascal@19350 608 +
pascal@19350 609 + u8 pad1;
pascal@19350 610 + u16 pad2;
pascal@19350 611 +};
pascal@19350 612 +
pascal@19350 613 +struct vnc_client_init {
pascal@19350 614 + u8 shared;
pascal@19350 615 +};
pascal@19350 616 +
pascal@19350 617 +struct vnc_server_init {
pascal@19350 618 + u16 w;
pascal@19350 619 + u16 h;
pascal@19350 620 + struct vnc_pixelfmt fmt;
pascal@19350 621 + u32 len;
pascal@19350 622 + /* char name[len]; */
pascal@19350 623 +};
pascal@19350 624 +
pascal@19350 625 +struct vnc_rect {
pascal@19350 626 + u16 x, y;
pascal@19350 627 + u16 w, h;
pascal@19350 628 + u32 enc;
pascal@19350 629 + /* rect bytes */
pascal@19350 630 +};
pascal@19350 631 +
pascal@19350 632 +struct vnc_server_fbup {
pascal@19350 633 + u8 type;
pascal@19350 634 + u8 pad;
pascal@19350 635 + u16 n;
pascal@19350 636 + /* struct vnc_rect rects[n]; */
pascal@19350 637 +};
pascal@19350 638 +
pascal@19350 639 +struct vnc_server_cuttext {
pascal@19350 640 + u8 type;
pascal@19350 641 + u8 pad1;
pascal@19350 642 + u16 pad2;
pascal@19350 643 + u32 len;
pascal@19350 644 + /* char text[length] */
pascal@19350 645 +};
pascal@19350 646 +
pascal@19350 647 +struct vnc_server_colormap {
pascal@19350 648 + u8 type;
pascal@19350 649 + u8 pad;
pascal@19350 650 + u16 first;
pascal@19350 651 + u16 n;
pascal@19350 652 + /* u8 colors[n * 3 * 2]; */
pascal@19350 653 +};
pascal@19350 654 +
pascal@19350 655 +struct vnc_client_pixelfmt {
pascal@19350 656 + u8 type;
pascal@19350 657 + u8 pad1;
pascal@19350 658 + u16 pad2;
pascal@19350 659 + struct vnc_pixelfmt format;
pascal@19350 660 +};
pascal@19350 661 +
pascal@19350 662 +struct vnc_client_fbup {
pascal@19350 663 + u8 type;
pascal@19350 664 + u8 inc;
pascal@19350 665 + u16 x;
pascal@19350 666 + u16 y;
pascal@19350 667 + u16 w;
pascal@19350 668 + u16 h;
pascal@19350 669 +};
pascal@19350 670 +
pascal@19350 671 +struct vnc_client_keyevent {
pascal@19350 672 + u8 type;
pascal@19350 673 + u8 down;
pascal@19350 674 + u16 pad;
pascal@19350 675 + u32 key;
pascal@19350 676 +};
pascal@19350 677 +
pascal@19350 678 +struct vnc_client_ratevent {
pascal@19350 679 + u8 type;
pascal@19350 680 + u8 mask;
pascal@19350 681 + u16 x;
pascal@19350 682 + u16 y;
pascal@19350 683 +};