fbvnc: send set encoding message
[fbvnc.git] / fbvnc.c
blobedf3d2d4f812b034a97d6979eb23b62d0bb9fcd7
1 /*
2 * FBVNC: a small Linux framebuffer VNC viewer
4 * Copyright (C) 2009-2021 Ali Gholami Rudi
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <arpa/inet.h>
19 #include <ctype.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <netdb.h>
23 #include <netinet/in.h>
24 #include <poll.h>
25 #include <pwd.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <termios.h>
30 #include <unistd.h>
31 #include <sys/socket.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <linux/input.h>
35 #include "draw.h"
36 #include "vnc.h"
38 #define MIN(a, b) ((a) < (b) ? (a) : (b))
39 #define MAX(a, b) ((a) > (b) ? (a) : (b))
40 #define OUT(msg) write(1, (msg), strlen(msg))
42 #define VNC_PORT "5900"
43 #define SCRSCRL 2
44 #define MAXRES (1 << 16)
46 static int cols, rows; /* framebuffer dimensions */
47 static int bpp; /* bytes per pixel */
48 static int srv_cols, srv_rows; /* server screen dimensions */
49 static int or, oc; /* visible screen offset */
50 static int mr, mc; /* mouse position */
51 static int nodraw; /* don't draw anything */
53 static char buf[MAXRES];
55 static int vnc_connect(char *addr, char *port)
57 struct addrinfo hints, *addrinfo;
58 int fd;
60 memset(&hints, 0, sizeof(hints));
61 hints.ai_family = AF_UNSPEC;
62 hints.ai_socktype = SOCK_STREAM;
63 hints.ai_flags = AI_PASSIVE;
65 if (getaddrinfo(addr, port, &hints, &addrinfo))
66 return -1;
67 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
68 addrinfo->ai_protocol);
70 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) == -1) {
71 close(fd);
72 freeaddrinfo(addrinfo);
73 return -1;
75 freeaddrinfo(addrinfo);
76 return fd;
79 static void fbmode_bits(int *rr, int *rg, int *rb)
81 int mode = FBM_COLORS(fb_mode());
82 *rr = (mode >> 8) & 0xf;
83 *rg = (mode >> 4) & 0xf;
84 *rb = (mode >> 0) & 0xf;
87 static int vnc_init(int fd)
89 char vncver[16];
90 int rr, rg, rb;
91 struct vnc_clientinit clientinit;
92 struct vnc_serverinit serverinit;
93 struct vnc_setpixelformat pixfmt_cmd;
94 struct vnc_setencoding enc_cmd;
95 u32 enc[] = {VNC_ENC_RAW};
96 int connstat = VNC_CONN_FAILED;
98 /* handshake */
99 read(fd, vncver, 12);
100 strcpy(vncver, "RFB 003.003\n");
101 write(fd, vncver, 12);
102 read(fd, &connstat, sizeof(connstat));
103 if (ntohl(connstat) != VNC_CONN_NOAUTH)
104 return -1;
105 clientinit.shared = 1;
106 write(fd, &clientinit, sizeof(clientinit));
107 read(fd, &serverinit, sizeof(serverinit));
108 read(fd, buf, ntohl(serverinit.len));
109 srv_cols = ntohs(serverinit.w);
110 srv_rows = ntohs(serverinit.h);
112 /* set up the framebuffer */
113 if (fb_init())
114 return -1;
115 cols = MIN(srv_cols, fb_cols());
116 rows = MIN(srv_rows, fb_rows());
117 bpp = FBM_BPP(fb_mode());
118 mr = rows / 2;
119 mc = cols / 2;
121 /* send framebuffer configuration */
122 pixfmt_cmd.type = VNC_SETPIXELFORMAT;
123 pixfmt_cmd.format.bpp = bpp << 3;
124 pixfmt_cmd.format.depth = bpp << 3;
125 pixfmt_cmd.format.bigendian = 0;
126 pixfmt_cmd.format.truecolor = 1;
127 fbmode_bits(&rr, &rg, &rb);
128 pixfmt_cmd.format.rmax = htons((1 << rr) - 1);
129 pixfmt_cmd.format.gmax = htons((1 << rg) - 1);
130 pixfmt_cmd.format.bmax = htons((1 << rb) - 1);
132 /* assuming colors packed as RGB; shall handle other cases later */
133 pixfmt_cmd.format.rshl = rg + rb;
134 pixfmt_cmd.format.gshl = rb;
135 pixfmt_cmd.format.bshl = 0;
136 write(fd, &pixfmt_cmd, sizeof(pixfmt_cmd));
138 /* send pixel format */
139 enc_cmd.type = VNC_SETENCODING;
140 enc_cmd.pad = 0;
141 enc_cmd.n = htons(1);
142 write(fd, &enc_cmd, sizeof(enc_cmd));
143 write(fd, enc, ntohs(enc_cmd.n) * sizeof(enc[0]));
144 return 0;
147 static int vnc_free(void)
149 fb_free();
150 return 0;
153 static int vnc_refresh(int fd, int inc)
155 struct vnc_updaterequest fbup_req;
156 fbup_req.type = VNC_UPDATEREQUEST;
157 fbup_req.inc = inc;
158 fbup_req.x = htons(oc);
159 fbup_req.y = htons(or);
160 fbup_req.w = htons(cols);
161 fbup_req.h = htons(rows);
162 return write(fd, &fbup_req, sizeof(fbup_req)) != sizeof(fbup_req);
165 static void drawfb(char *s, int x, int y, int w, int h)
167 int sc; /* screen column offset */
168 int bc, bw; /* buffer column offset / row width */
169 int i;
170 sc = MAX(0, x - oc);
171 bc = x > oc ? 0 : oc - x;
172 bw = x + w < oc + cols ? w - bc : w - bc - (x + w - oc - cols);
173 for (i = y; i < y + h; i++)
174 if (i - or >= 0 && i - or < rows && bw > 0)
175 fb_set(i - or, sc, s + ((i - y) * w + bc) * bpp, bw);
178 static void xread(int fd, void *buf, int len)
180 int nr = 0;
181 int n;
182 while (nr < len && (n = read(fd, buf + nr, len - nr)) > 0)
183 nr += n;
184 if (nr < len) {
185 printf("partial vnc read!\n");
186 exit(1);
190 static int vnc_event(int fd)
192 struct vnc_rect uprect;
193 char msg[1 << 12];
194 struct vnc_update *fbup = (void *) msg;
195 struct vnc_servercuttext *cuttext = (void *) msg;
196 struct vnc_setcolormapentries *colormap = (void *) msg;
197 int i, j;
198 int n;
200 if (read(fd, msg, 1) != 1)
201 return -1;
202 switch (msg[0]) {
203 case VNC_UPDATE:
204 xread(fd, msg + 1, sizeof(*fbup) - 1);
205 n = ntohs(fbup->n);
206 for (j = 0; j < n; j++) {
207 int x, y, w, h;
208 xread(fd, &uprect, sizeof(uprect));
209 x = ntohs(uprect.x);
210 y = ntohs(uprect.y);
211 w = ntohs(uprect.w);
212 h = ntohs(uprect.h);
213 if (x >= srv_cols || x + w > srv_cols)
214 return -1;
215 if (y >= srv_rows || y + h > srv_rows)
216 return -1;
217 for (i = 0; i < h; i++) {
218 xread(fd, buf, w * bpp);
219 if (!nodraw)
220 drawfb(buf, x, y + i, w, 1);
223 break;
224 case VNC_BELL:
225 break;
226 case VNC_SERVERCUTTEXT:
227 xread(fd, msg + 1, sizeof(*cuttext) - 1);
228 xread(fd, buf, ntohl(cuttext->len));
229 break;
230 case VNC_SETCOLORMAPENTRIES:
231 xread(fd, msg + 1, sizeof(*colormap) - 1);
232 xread(fd, buf, ntohs(colormap->n) * 3 * 2);
233 break;
234 default:
235 fprintf(stderr, "unknown vnc msg: %d\n", msg[0]);
236 return -1;
238 return 0;
241 static int rat_event(int fd, int ratfd)
243 char ie[4] = {0};
244 struct vnc_pointerevent me = {VNC_POINTEREVENT};
245 int mask = 0;
246 int or_ = or, oc_ = oc;
247 if (ratfd > 0 && read(ratfd, &ie, sizeof(ie)) != 4)
248 return -1;
249 /* ignore mouse movements when nodraw */
250 if (nodraw)
251 return 0;
252 mc += ie[1];
253 mr -= ie[2];
255 if (mc < oc)
256 oc = MAX(0, oc - cols / SCRSCRL);
257 if (mc >= oc + cols && oc + cols < srv_cols)
258 oc = MIN(srv_cols - cols, oc + cols / SCRSCRL);
259 if (mr < or)
260 or = MAX(0, or - rows / SCRSCRL);
261 if (mr >= or + rows && or + rows < srv_rows)
262 or = MIN(srv_rows - rows, or + rows / SCRSCRL);
263 mc = MAX(oc, MIN(oc + cols - 1, mc));
264 mr = MAX(or, MIN(or + rows - 1, mr));
265 if (ie[0] & 0x01)
266 mask |= VNC_BUTTON1_MASK;
267 if (ie[0] & 0x04)
268 mask |= VNC_BUTTON2_MASK;
269 if (ie[0] & 0x02)
270 mask |= VNC_BUTTON3_MASK;
271 if (ie[3] > 0) /* wheel up */
272 mask |= VNC_BUTTON4_MASK;
273 if (ie[3] < 0) /* wheel down */
274 mask |= VNC_BUTTON5_MASK;
276 me.y = htons(mr);
277 me.x = htons(mc);
278 me.mask = mask;
279 write(fd, &me, sizeof(me));
280 if (or != or_ || oc != oc_)
281 if (vnc_refresh(fd, 0))
282 return -1;
283 return 0;
286 static int press(int fd, int key, int down)
288 struct vnc_keyevent ke = {VNC_KEYEVENT};
289 ke.key = htonl(key);
290 ke.down = down;
291 return write(fd, &ke, sizeof(ke));
294 static void showmsg(void)
296 OUT("\x1b[H\t\t\t*** fbvnc ***\r");
299 static int kbd_event(int fd, int kbdfd)
301 char key[1024];
302 int i, nr;
304 if ((nr = read(kbdfd, key, sizeof(key))) <= 0 )
305 return -1;
306 for (i = 0; i < nr; i++) {
307 int k = -1;
308 int mod[4];
309 int nmod = 0;
310 switch (key[i]) {
311 case 0x08:
312 case 0x7f:
313 k = 0xff08;
314 break;
315 case 0x09:
316 k = 0xff09;
317 break;
318 case 0x1b:
319 if (i + 2 < nr && key[i + 1] == '[') {
320 if (key[i + 2] == 'A')
321 k = 0xff52;
322 if (key[i + 2] == 'B')
323 k = 0xff54;
324 if (key[i + 2] == 'C')
325 k = 0xff53;
326 if (key[i + 2] == 'D')
327 k = 0xff51;
328 if (key[i + 2] == 'H')
329 k = 0xff50;
330 if (k > 0) {
331 i += 2;
332 break;
335 k = 0xff1b;
336 if (i + 1 < nr) {
337 mod[nmod++] = 0xffe9;
338 k = key[++i];
339 if (k == 0x03) /* esc-^C: quit */
340 return -1;
342 break;
343 case 0x0d:
344 k = 0xff0d;
345 break;
346 case 0x0: /* c-space: stop/start drawing */
347 if (!nodraw) {
348 nodraw = 1;
349 showmsg();
350 } else {
351 nodraw = 0;
352 if (vnc_refresh(fd, 0))
353 return -1;
355 default:
356 k = (unsigned char) key[i];
358 if ((k >= 'A' && k <= 'Z') || strchr(":\"<>?{}|+_()*&^%$#@!~", k))
359 mod[nmod++] = 0xffe1;
360 if (k >= 1 && k <= 26) {
361 k = 'a' + k - 1;
362 mod[nmod++] = 0xffe3;
364 if (k > 0) {
365 int j;
366 for (j = 0; j < nmod; j++)
367 press(fd, mod[j], 1);
368 press(fd, k, 1);
369 press(fd, k, 0);
370 for (j = 0; j < nmod; j++)
371 press(fd, mod[j], 0);
374 return 0;
377 static void term_setup(struct termios *ti)
379 struct termios termios;
380 OUT("\033[2J"); /* clear the screen */
381 OUT("\033[?25l"); /* hide the cursor */
382 showmsg();
383 tcgetattr(0, &termios);
384 *ti = termios;
385 cfmakeraw(&termios);
386 tcsetattr(0, TCSANOW, &termios);
389 static void term_cleanup(struct termios *ti)
391 tcsetattr(0, TCSANOW, ti);
392 OUT("\r\n\033[?25h"); /* show the cursor */
395 static void mainloop(int vnc_fd, int kbd_fd, int rat_fd)
397 struct pollfd ufds[3];
398 int pending = 0;
399 int err;
400 ufds[0].fd = kbd_fd;
401 ufds[0].events = POLLIN;
402 ufds[1].fd = vnc_fd;
403 ufds[1].events = POLLIN;
404 ufds[2].fd = rat_fd;
405 ufds[2].events = POLLIN;
406 rat_event(vnc_fd, -1);
407 if (vnc_refresh(vnc_fd, 0))
408 return;
409 while (1) {
410 err = poll(ufds, 3, 500);
411 if (err == -1 && errno != EINTR)
412 break;
413 if (!err)
414 continue;
415 if (ufds[0].revents & POLLIN)
416 if (kbd_event(vnc_fd, kbd_fd) == -1)
417 break;
418 if (ufds[1].revents & POLLIN) {
419 if (vnc_event(vnc_fd) == -1)
420 break;
421 pending = 0;
423 if (ufds[2].revents & POLLIN)
424 if (rat_event(vnc_fd, rat_fd) == -1)
425 break;
426 if (!pending++)
427 if (vnc_refresh(vnc_fd, 1))
428 break;
432 int main(int argc, char * argv[])
434 char *port = VNC_PORT;
435 char *host = "127.0.0.1";
436 struct termios ti;
437 int vnc_fd, rat_fd;
438 if (argc >= 2)
439 host = argv[1];
440 if (argc >= 3)
441 port = argv[2];
442 if ((vnc_fd = vnc_connect(host, port)) < 0) {
443 fprintf(stderr, "could not connect!\n");
444 return 1;
446 if (vnc_init(vnc_fd) < 0) {
447 close(vnc_fd);
448 fprintf(stderr, "vnc init failed!\n");
449 return 1;
451 term_setup(&ti);
453 /* entering intellimouse for using mouse wheel */
454 rat_fd = open("/dev/input/mice", O_RDWR);
455 write(rat_fd, "\xf3\xc8\xf3\x64\xf3\x50", 6);
456 read(rat_fd, buf, 1);
458 mainloop(vnc_fd, 0, rat_fd);
460 term_cleanup(&ti);
461 vnc_free();
462 close(vnc_fd);
463 close(rat_fd);
464 return 0;