fbvnc: update copyright notice
[fbvnc.git] / fbvnc.c
blobe72464b67968b1663e6c4daba0c37516855a22a2
1 /*
2 * FBVNC: a small Linux framebuffer VNC viewer
4 * Copyright (C) 2009-2020 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;
92 struct vnc_client_init clientinit;
93 struct vnc_server_init serverinit;
94 struct vnc_client_pixelfmt pixfmt_cmd;
95 int connstat = VNC_CONN_FAILED;
97 read(fd, vncver, 12);
98 strcpy(vncver, "RFB 003.003\n");
99 write(fd, vncver, 12);
101 read(fd, &connstat, sizeof(connstat));
103 if (ntohl(connstat) != VNC_CONN_NOAUTH)
104 return -1;
106 clientinit.shared = 1;
107 write(fd, &clientinit, sizeof(clientinit));
108 read(fd, &serverinit, sizeof(serverinit));
110 if (fb_init())
111 return -1;
112 srv_cols = ntohs(serverinit.w);
113 srv_rows = ntohs(serverinit.h);
114 cols = MIN(srv_cols, fb_cols());
115 rows = MIN(srv_rows, fb_rows());
116 bpp = FBM_BPP(fb_mode());
117 mr = rows / 2;
118 mc = cols / 2;
120 read(fd, buf, ntohl(serverinit.len));
121 pixfmt_cmd.type = VNC_CLIENT_PIXFMT;
122 pixfmt_cmd.format.bpp = bpp << 3;
123 pixfmt_cmd.format.depth = bpp << 3;
124 pixfmt_cmd.format.bigendian = 0;
125 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);
131 /* assuming colors packed as RGB; shall handle other cases later */
132 pixfmt_cmd.format.rshl = rg + rb;
133 pixfmt_cmd.format.gshl = rb;
134 pixfmt_cmd.format.bshl = 0;
135 write(fd, &pixfmt_cmd, sizeof(pixfmt_cmd));
136 return 0;
139 static int vnc_free(void)
141 fb_free();
142 return 0;
145 static int vnc_refresh(int fd, int inc)
147 struct vnc_client_fbup fbup_req;
148 fbup_req.type = VNC_CLIENT_FBUP;
149 fbup_req.inc = inc;
150 fbup_req.x = htons(oc);
151 fbup_req.y = htons(or);
152 fbup_req.w = htons(cols);
153 fbup_req.h = htons(rows);
154 return write(fd, &fbup_req, sizeof(fbup_req)) != sizeof(fbup_req);
157 static void drawfb(char *s, int x, int y, int w, int h)
159 int sc; /* screen column offset */
160 int bc, bw; /* buffer column offset / row width */
161 int i;
162 sc = MAX(0, x - oc);
163 bc = x > oc ? 0 : oc - x;
164 bw = x + w < oc + cols ? w - bc : w - bc - (x + w - oc - cols);
165 for (i = y; i < y + h; i++)
166 if (i - or >= 0 && i - or < rows && bw > 0)
167 fb_set(i - or, sc, s + ((i - y) * w + bc) * bpp, bw);
170 static void xread(int fd, void *buf, int len)
172 int nr = 0;
173 int n;
174 while (nr < len && (n = read(fd, buf + nr, len - nr)) > 0)
175 nr += n;
176 if (nr < len) {
177 printf("partial vnc read!\n");
178 exit(1);
182 static int vnc_event(int fd)
184 struct vnc_rect uprect;
185 char msg[1 << 12];
186 struct vnc_server_fbup *fbup = (void *) msg;
187 struct vnc_server_cuttext *cuttext = (void *) msg;
188 struct vnc_server_colormap *colormap = (void *) msg;
189 int i, j;
190 int n;
192 if (read(fd, msg, 1) != 1)
193 return -1;
194 switch (msg[0]) {
195 case VNC_SERVER_FBUP:
196 xread(fd, msg + 1, sizeof(*fbup) - 1);
197 n = ntohs(fbup->n);
198 for (j = 0; j < n; j++) {
199 int x, y, w, h;
200 xread(fd, &uprect, sizeof(uprect));
201 x = ntohs(uprect.x);
202 y = ntohs(uprect.y);
203 w = ntohs(uprect.w);
204 h = ntohs(uprect.h);
205 if (x >= srv_cols || x + w > srv_cols)
206 return -1;
207 if (y >= srv_rows || y + h > srv_rows)
208 return -1;
209 for (i = 0; i < h; i++) {
210 xread(fd, buf, w * bpp);
211 if (!nodraw)
212 drawfb(buf, x, y + i, w, 1);
215 break;
216 case VNC_SERVER_BELL:
217 break;
218 case VNC_SERVER_CUTTEXT:
219 xread(fd, msg + 1, sizeof(*cuttext) - 1);
220 xread(fd, buf, ntohl(cuttext->len));
221 break;
222 case VNC_SERVER_COLORMAP:
223 xread(fd, msg + 1, sizeof(*colormap) - 1);
224 xread(fd, buf, ntohs(colormap->n) * 3 * 2);
225 break;
226 default:
227 fprintf(stderr, "unknown vnc msg: %d\n", msg[0]);
228 return -1;
230 return 0;
233 static int rat_event(int fd, int ratfd)
235 char ie[4];
236 struct vnc_client_ratevent me = {VNC_CLIENT_RATEVENT};
237 int mask = 0;
238 int or_ = or, oc_ = oc;
239 if (read(ratfd, &ie, sizeof(ie)) != 4)
240 return -1;
241 /* ignore mouse movements when nodraw */
242 if (nodraw)
243 return 0;
244 mc += ie[1];
245 mr -= ie[2];
247 if (mc < oc)
248 oc = MAX(0, oc - cols / SCRSCRL);
249 if (mc >= oc + cols && oc + cols < srv_cols)
250 oc = MIN(srv_cols - cols, oc + cols / SCRSCRL);
251 if (mr < or)
252 or = MAX(0, or - rows / SCRSCRL);
253 if (mr >= or + rows && or + rows < srv_rows)
254 or = MIN(srv_rows - rows, or + rows / SCRSCRL);
255 mc = MAX(oc, MIN(oc + cols - 1, mc));
256 mr = MAX(or, MIN(or + rows - 1, mr));
257 if (ie[0] & 0x01)
258 mask |= VNC_BUTTON1_MASK;
259 if (ie[0] & 0x04)
260 mask |= VNC_BUTTON2_MASK;
261 if (ie[0] & 0x02)
262 mask |= VNC_BUTTON3_MASK;
263 if (ie[3] > 0) /* wheel up */
264 mask |= VNC_BUTTON4_MASK;
265 if (ie[3] < 0) /* wheel down */
266 mask |= VNC_BUTTON5_MASK;
268 me.y = htons(mr);
269 me.x = htons(mc);
270 me.mask = mask;
271 write(fd, &me, sizeof(me));
272 if (or != or_ || oc != oc_)
273 if (vnc_refresh(fd, 0))
274 return -1;
275 return 0;
278 static int press(int fd, int key, int down)
280 struct vnc_client_keyevent ke = {VNC_CLIENT_KEYEVENT};
281 ke.key = htonl(key);
282 ke.down = down;
283 return write(fd, &ke, sizeof(ke));
286 static void showmsg(void)
288 OUT("\x1b[H\t\t\t*** fbvnc ***\r");
291 static int kbd_event(int fd, int kbdfd)
293 char key[1024];
294 int i, nr;
296 if ((nr = read(kbdfd, key, sizeof(key))) <= 0 )
297 return -1;
298 for (i = 0; i < nr; i++) {
299 int k = -1;
300 int mod[4];
301 int nmod = 0;
302 switch (key[i]) {
303 case 0x08:
304 case 0x7f:
305 k = 0xff08;
306 break;
307 case 0x09:
308 k = 0xff09;
309 break;
310 case 0x1b:
311 if (i + 2 < nr && key[i + 1] == '[') {
312 if (key[i + 2] == 'A')
313 k = 0xff52;
314 if (key[i + 2] == 'B')
315 k = 0xff54;
316 if (key[i + 2] == 'C')
317 k = 0xff53;
318 if (key[i + 2] == 'D')
319 k = 0xff51;
320 if (key[i + 2] == 'H')
321 k = 0xff50;
322 if (k > 0) {
323 i += 2;
324 break;
327 k = 0xff1b;
328 if (i + 1 < nr) {
329 mod[nmod++] = 0xffe9;
330 k = key[++i];
331 if (k == 0x03) /* esc-^C: quit */
332 return -1;
334 break;
335 case 0x0d:
336 k = 0xff0d;
337 break;
338 case 0x0: /* c-space: stop/start drawing */
339 if (!nodraw) {
340 nodraw = 1;
341 showmsg();
342 } else {
343 nodraw = 0;
344 if (vnc_refresh(fd, 0))
345 return -1;
347 default:
348 k = (unsigned char) key[i];
350 if (k >= 'A' && k <= 'Z' || strchr(":\"<>?{}|+_()*&^%$#@!~", k))
351 mod[nmod++] = 0xffe1;
352 if (k >= 1 && k <= 26) {
353 k = 'a' + k - 1;
354 mod[nmod++] = 0xffe3;
356 if (k > 0) {
357 int j;
358 for (j = 0; j < nmod; j++)
359 press(fd, mod[j], 1);
360 press(fd, k, 1);
361 press(fd, k, 0);
362 for (j = 0; j < nmod; j++)
363 press(fd, mod[j], 0);
366 return 0;
369 static void term_setup(struct termios *ti)
371 struct termios termios;
372 OUT("\033[2J"); /* clear the screen */
373 OUT("\033[?25l"); /* hide the cursor */
374 showmsg();
375 tcgetattr(0, &termios);
376 *ti = termios;
377 cfmakeraw(&termios);
378 tcsetattr(0, TCSANOW, &termios);
381 static void term_cleanup(struct termios *ti)
383 tcsetattr(0, TCSANOW, ti);
384 OUT("\r\n\033[?25h"); /* show the cursor */
387 static void mainloop(int vnc_fd, int kbd_fd, int rat_fd)
389 struct pollfd ufds[3];
390 int pending = 0;
391 int err;
392 ufds[0].fd = kbd_fd;
393 ufds[0].events = POLLIN;
394 ufds[1].fd = vnc_fd;
395 ufds[1].events = POLLIN;
396 ufds[2].fd = rat_fd;
397 ufds[2].events = POLLIN;
398 if (vnc_refresh(vnc_fd, 0))
399 return;
400 while (1) {
401 err = poll(ufds, 3, 500);
402 if (err == -1 && errno != EINTR)
403 break;
404 if (!err)
405 continue;
406 if (ufds[0].revents & POLLIN)
407 if (kbd_event(vnc_fd, kbd_fd) == -1)
408 break;
409 if (ufds[1].revents & POLLIN) {
410 if (vnc_event(vnc_fd) == -1)
411 break;
412 pending = 0;
414 if (ufds[2].revents & POLLIN)
415 if (rat_event(vnc_fd, rat_fd) == -1)
416 break;
417 if (!pending++)
418 if (vnc_refresh(vnc_fd, 1))
419 break;
423 int main(int argc, char * argv[])
425 char *port = VNC_PORT;
426 char *host = "127.0.0.1";
427 struct termios ti;
428 int vnc_fd, rat_fd;
429 if (argc >= 2)
430 host = argv[1];
431 if (argc >= 3)
432 port = argv[2];
433 if ((vnc_fd = vnc_connect(host, port)) < 0) {
434 fprintf(stderr, "could not connect!\n");
435 return 1;
437 if (vnc_init(vnc_fd) < 0) {
438 close(vnc_fd);
439 fprintf(stderr, "vnc init failed!\n");
440 return 1;
442 term_setup(&ti);
444 /* entering intellimouse for using mouse wheel */
445 rat_fd = open("/dev/input/mice", O_RDWR);
446 write(rat_fd, "\xf3\xc8\xf3\x64\xf3\x50", 6);
447 read(rat_fd, buf, 1);
449 mainloop(vnc_fd, 0, rat_fd);
451 term_cleanup(&ti);
452 vnc_free();
453 close(vnc_fd);
454 close(rat_fd);
455 return 0;