use 8-bit colors by default
[fbvnc.git] / fbvnc.c
blob2d0518783411d0fb3bdafa8beadb2e483068da6a
1 /*
2 * fbvnc - a small linux framebuffer vnc viewer
4 * Copyright (C) 2009-2010 Ali Gholami Rudi
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License, as published by the
8 * Free Software Foundation.
9 */
10 #include <arpa/inet.h>
11 #include <ctype.h>
12 #include <errno.h>
13 #include <fcntl.h>
14 #include <netdb.h>
15 #include <netinet/in.h>
16 #include <poll.h>
17 #include <pwd.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <termios.h>
22 #include <unistd.h>
23 #include <sys/socket.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <linux/input.h>
27 #include "draw.h"
28 #include "vnc.h"
30 #define VNC_PORT "5900"
32 #define MAXRES (1 << 21)
33 #define MIN(a, b) ((a) < (b) ? (a) : (b))
35 static int cols, rows;
36 static int mr, mc; /* mouse position */
38 static char buf[MAXRES];
40 static int vnc_connect(char *addr, char *port)
42 struct addrinfo hints, *addrinfo;
43 int fd;
45 memset(&hints, 0, sizeof(hints));
46 hints.ai_family = AF_UNSPEC;
47 hints.ai_socktype = SOCK_STREAM;
48 hints.ai_flags = AI_PASSIVE;
50 if (getaddrinfo(addr, port, &hints, &addrinfo))
51 return -1;
52 fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
53 addrinfo->ai_protocol);
55 if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) == -1) {
56 close(fd);
57 freeaddrinfo(addrinfo);
58 return -1;
60 freeaddrinfo(addrinfo);
61 return fd;
64 static int vnc_init(int fd)
66 char vncver[] = "RFB 003.003\n";
67 struct vnc_client_init clientinit;
68 struct vnc_server_init serverinit;
69 struct vnc_client_pixelfmt pixfmt_cmd;
70 int connstat = VNC_CONN_FAILED;
72 write(fd, vncver, 12);
73 read(fd, vncver, 12);
75 read(fd, &connstat, sizeof(connstat));
77 if (ntohl(connstat) != VNC_CONN_NOAUTH)
78 return -1;
80 clientinit.shared = 1;
81 write(fd, &clientinit, sizeof(clientinit));
82 read(fd, &serverinit, sizeof(serverinit));
84 fb_init();
85 cols = MIN(ntohs(serverinit.w), fb_cols());
86 rows = MIN(ntohs(serverinit.h), fb_rows());
87 mr = rows / 2;
88 mc = cols / 2;
90 read(fd, buf, ntohl(serverinit.len));
91 pixfmt_cmd.type = VNC_CLIENT_PIXFMT;
92 pixfmt_cmd.format.bpp = 8;
93 pixfmt_cmd.format.depth = 8;
94 pixfmt_cmd.format.bigendian = 0;
95 pixfmt_cmd.format.truecolor = 1;
97 pixfmt_cmd.format.rmax = htons(3);
98 pixfmt_cmd.format.gmax = htons(7);
99 pixfmt_cmd.format.bmax = htons(7);
100 pixfmt_cmd.format.rshl = 0;
101 pixfmt_cmd.format.gshl = 2;
102 pixfmt_cmd.format.bshl = 5;
104 write(fd, &pixfmt_cmd, sizeof(pixfmt_cmd));
105 return fd;
108 static int vnc_free(void)
110 fb_free();
111 return 0;
114 static int vnc_refresh(int fd, int inc)
116 struct vnc_client_fbup fbup_req;
117 fbup_req.type = VNC_CLIENT_FBUP;
118 fbup_req.inc = inc;
119 fbup_req.x = htons(0);
120 fbup_req.y = htons(0);
121 fbup_req.w = htons(cols);
122 fbup_req.h = htons(rows);
123 write(fd, &fbup_req, sizeof(fbup_req));
124 return 0;
127 static void drawfb(char *s, int x, int y, int w, int h)
129 fbval_t slice[1 << 14];
130 int i, j;
131 for (i = 0; i < h; i++) {
132 for (j = 0; j < w; j++) {
133 int c = *(unsigned char *) &s[i * w + j];
134 int r = (c & 0x3) << 6;
135 int g = ((c >> 2) & 0x7) << 5;
136 int b = ((c >> 5) & 0x7) << 5;
137 slice[j] = fb_color(r, g, b);
139 fb_set(y + i, x, slice, w);
143 static int vnc_event(int fd)
145 struct vnc_rect uprect;
146 char msg[1 << 12];
147 struct vnc_server_fbup *fbup = (void *) msg;
148 struct vnc_server_cuttext *cuttext = (void *) msg;
149 struct vnc_server_colormap *colormap = (void *) msg;
150 int nr, i, j;
151 int n;
153 if ((nr = read(fd, msg, 1)) != 1)
154 return -1;
156 switch (msg[0]) {
157 case VNC_SERVER_FBUP:
158 nr = read(fd, msg + 1, sizeof(*fbup) - 1);
159 n = ntohs(fbup->n);
160 for (j = 0; j < n; j++) {
161 int x, y, w, h;
162 nr = read(fd, &uprect, sizeof(uprect));
163 if (nr != sizeof(uprect))
164 return -1;
165 x = ntohs(uprect.x);
166 y = ntohs(uprect.y);
167 w = ntohs(uprect.w);
168 h = ntohs(uprect.h);
169 if (x >= cols || x + w > cols)
170 return -1;
171 if (y >= rows || y + h > rows)
172 return -1;
173 for (i = 0; i < w * h; ) {
174 nr = read(fd, buf + i, w * h - i);
175 if (nr <= 0)
176 return -1;
177 i += nr;
179 drawfb(buf, x, y, w, h);
181 break;
182 case VNC_SERVER_BELL:
183 break;
184 case VNC_SERVER_CUTTEXT:
185 nr = read(fd, msg + 1, sizeof(*cuttext) - 1);
186 nr = read(fd, buf, cuttext->len);
187 break;
188 case VNC_SERVER_COLORMAP:
189 nr = read(fd, msg + 1, sizeof(*colormap) - 1);
190 nr = read(fd, buf, ntohs(colormap->n) * 3 * 2);
191 break;
192 default:
193 fprintf(stderr, "unknown vnc msg: %d\n", msg[0]);
194 return -1;
196 return 0;
199 static int rat_event(int fd, int ratfd)
201 char ie[3];
202 struct vnc_client_ratevent me = {VNC_CLIENT_RATEVENT};
203 int mask = 0;
204 if (read(ratfd, &ie, sizeof(ie)) != 3)
205 return -1;
206 mc += ie[1];
207 mr -= ie[2];
208 mc = MAX(0, MIN(cols - 1, mc));
209 mr = MAX(0, MIN(rows - 1, mr));
210 if (ie[0] & 0x01)
211 mask |= VNC_BUTTON1_MASK;
212 if (ie[0] & 0x02)
213 mask |= VNC_BUTTON1_MASK;
214 if (ie[0] & 0x04)
215 mask |= VNC_BUTTON1_MASK;
216 me.y = htons(mr);
217 me.x = htons(mc);
218 me.mask = mask;
219 write(fd, &me, sizeof(me));
220 return 0;
223 static int press(int fd, int key, int down)
225 struct vnc_client_keyevent ke = {VNC_CLIENT_KEYEVENT};
226 ke.key = htonl(key);
227 ke.down = down;
228 return write(fd, &ke, sizeof(ke));
231 static int kbd_event(int fd, int kbdfd)
233 char key[1024];
234 int i, nr;
236 if ((nr = read(kbdfd, key, sizeof(key))) <= 0 )
237 return -1;
238 for (i = 0; i < nr; i++) {
239 int k = -1;
240 int mod[4];
241 int nmod = 0;
242 switch (key[i]) {
243 case 0x08:
244 case 0x7f:
245 k = 0xff08;
246 break;
247 case 0x09:
248 k = 0xff09;
249 break;
250 case 0x1b:
251 if (i + 2 < nr && key[i + 1] == '[') {
252 if (key[i + 2] == 'A')
253 k = 0xff52;
254 if (key[i + 2] == 'B')
255 k = 0xff54;
256 if (key[i + 2] == 'C')
257 k = 0xff53;
258 if (key[i + 2] == 'D')
259 k = 0xff51;
260 if (key[i + 2] == 'H')
261 k = 0xff50;
262 if (k > 0) {
263 i += 2;
264 break;
267 k = 0xff1b;
268 if (i + 1 < nr) {
269 mod[nmod++] = 0xffe9;
270 k = key[++i];
272 break;
273 case 0x0d:
274 k = 0xff0d;
275 break;
276 case 0x03:
277 return -1;
278 default:
279 k = (unsigned char) key[i];
281 if (isupper(k) || strchr(":\"<>?{}|+_()*&^%$#@!~", k))
282 mod[nmod++] = 0xffe1;
283 if (k >= 1 && k <= 26) {
284 k = 'a' + k - 1;
285 mod[nmod++] = 0xffe3;
287 if (k > 0) {
288 int j;
289 for (j = 0; j < nmod; j++)
290 press(fd, mod[j], 1);
291 press(fd, k, 1);
292 press(fd, k, 0);
293 for (j = 0; j < nmod; j++)
294 press(fd, mod[j], 0);
297 return 0;
300 static void term_setup(struct termios *ti)
302 struct termios termios;
303 char *hide = "\x1b[?25l";
304 char *clear = "\x1b[2J\x1b[H";
305 char *msg = "\t\t\t*** fbvnc ***\r\n";
307 write(STDIN_FILENO, hide, strlen(hide));
308 write(STDOUT_FILENO, clear, strlen(clear));
309 write(STDOUT_FILENO, msg, strlen(msg));
310 tcgetattr (0, &termios);
311 *ti = termios;
312 cfmakeraw(&termios);
313 tcsetattr(0, TCSANOW, &termios);
316 static void term_cleanup(struct termios *ti)
318 char *show = "\x1b[?25h";
319 tcsetattr(0, TCSANOW, ti);
320 write(STDIN_FILENO, show, strlen(show));
323 static void mainloop(int vnc_fd, int kbd_fd, int rat_fd)
325 struct pollfd ufds[3];
326 int pending = 0;
327 int update = 1;
328 int err;
329 ufds[0].fd = kbd_fd;
330 ufds[0].events = POLLIN;
331 ufds[1].fd = vnc_fd;
332 ufds[1].events = POLLIN;
333 ufds[2].fd = rat_fd;
334 ufds[2].events = POLLIN;
335 while (1) {
336 if (update && !pending) {
337 static int i;
338 if (vnc_refresh(vnc_fd, i++) == -1)
339 break;
340 pending = 1;
341 update = 0;
343 err = poll(ufds, 3, 500);
344 if (err == -1 && errno != EINTR)
345 break;
346 if (!err)
347 continue;
348 if (ufds[0].revents & POLLIN) {
349 if (kbd_event(vnc_fd, kbd_fd) == -1)
350 break;
351 update = 1;
353 if (ufds[1].revents & POLLIN) {
354 if (vnc_event(vnc_fd) == -1)
355 break;
356 pending = 0;
358 if (ufds[2].revents & POLLIN) {
359 if (rat_event(vnc_fd, rat_fd) == -1)
360 break;
361 update = 1;
366 int main(int argc, char * argv[])
368 char *port = VNC_PORT;
369 char *host = "127.0.0.1";
370 struct termios ti;
371 int vnc_fd, rat_fd;
372 if (argc >= 2)
373 host = argv[1];
374 if (argc >= 3)
375 port = argv[2];
376 if ((vnc_fd = vnc_connect(host, port)) == -1) {
377 fprintf(stderr, "could not connect!\n");
378 return 1;
380 if (vnc_init(vnc_fd) == -1) {
381 fprintf(stderr, "vnc init failed!\n");
382 return 1;
384 term_setup(&ti);
385 rat_fd = open("/dev/input/mice", O_RDONLY);
387 mainloop(vnc_fd, 0, rat_fd);
389 term_cleanup(&ti);
390 vnc_free();
391 close(vnc_fd);
392 close(rat_fd);
393 return 0;