wok annotate busybox/stuff/busybox-1.26-fbvnc.u @ rev 19793

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