2 * fbvnc - a small linux framebuffer vnc viewer
4 * Copyright (C) 2009-2011 Ali Gholami Rudi
6 * This program is released under GNU GPL version 2.
13 #include <netinet/in.h>
21 #include <sys/socket.h>
23 #include <sys/types.h>
24 #include <linux/input.h>
28 /* framebuffer depth */
29 typedef unsigned int fbval_t
;
31 /* optimized version of fb_val() */
32 #define FB_VAL(r, g, b) fb_val((r), (g), (b))
34 #define MIN(a, b) ((a) < (b) ? (a) : (b))
35 #define MAX(a, b) ((a) > (b) ? (a) : (b))
37 #define VNC_PORT "5900"
39 #define MAXRES (1 << 21)
40 #define MIN(a, b) ((a) < (b) ? (a) : (b))
42 static int cols
, rows
;
43 static int mr
, mc
; /* mouse position */
45 static char buf
[MAXRES
];
47 static int vnc_connect(char *addr
, char *port
)
49 struct addrinfo hints
, *addrinfo
;
52 memset(&hints
, 0, sizeof(hints
));
53 hints
.ai_family
= AF_UNSPEC
;
54 hints
.ai_socktype
= SOCK_STREAM
;
55 hints
.ai_flags
= AI_PASSIVE
;
57 if (getaddrinfo(addr
, port
, &hints
, &addrinfo
))
59 fd
= socket(addrinfo
->ai_family
, addrinfo
->ai_socktype
,
60 addrinfo
->ai_protocol
);
62 if (connect(fd
, addrinfo
->ai_addr
, addrinfo
->ai_addrlen
) == -1) {
64 freeaddrinfo(addrinfo
);
67 freeaddrinfo(addrinfo
);
71 static int vnc_init(int fd
)
73 char vncver
[] = "RFB 003.003\n";
74 struct vnc_client_init clientinit
;
75 struct vnc_server_init serverinit
;
76 struct vnc_client_pixelfmt pixfmt_cmd
;
77 int connstat
= VNC_CONN_FAILED
;
79 write(fd
, vncver
, 12);
82 read(fd
, &connstat
, sizeof(connstat
));
84 if (ntohl(connstat
) != VNC_CONN_NOAUTH
)
87 clientinit
.shared
= 1;
88 write(fd
, &clientinit
, sizeof(clientinit
));
89 read(fd
, &serverinit
, sizeof(serverinit
));
93 if (FBM_BPP(fb_mode()) != sizeof(fbval_t
)) {
94 fprintf(stderr
, "fbvnc: fbval_t doesn't match fb depth\n");
97 cols
= MIN(ntohs(serverinit
.w
), fb_cols());
98 rows
= MIN(ntohs(serverinit
.h
), fb_rows());
102 read(fd
, buf
, ntohl(serverinit
.len
));
103 pixfmt_cmd
.type
= VNC_CLIENT_PIXFMT
;
104 pixfmt_cmd
.format
.bpp
= 8;
105 pixfmt_cmd
.format
.depth
= 8;
106 pixfmt_cmd
.format
.bigendian
= 0;
107 pixfmt_cmd
.format
.truecolor
= 1;
109 pixfmt_cmd
.format
.rmax
= htons(3);
110 pixfmt_cmd
.format
.gmax
= htons(7);
111 pixfmt_cmd
.format
.bmax
= htons(7);
112 pixfmt_cmd
.format
.rshl
= 0;
113 pixfmt_cmd
.format
.gshl
= 2;
114 pixfmt_cmd
.format
.bshl
= 5;
116 write(fd
, &pixfmt_cmd
, sizeof(pixfmt_cmd
));
120 static int vnc_free(void)
126 static int vnc_refresh(int fd
, int inc
)
128 struct vnc_client_fbup fbup_req
;
129 fbup_req
.type
= VNC_CLIENT_FBUP
;
131 fbup_req
.x
= htons(0);
132 fbup_req
.y
= htons(0);
133 fbup_req
.w
= htons(cols
);
134 fbup_req
.h
= htons(rows
);
135 write(fd
, &fbup_req
, sizeof(fbup_req
));
139 static void drawfb(char *s
, int x
, int y
, int w
, int h
)
141 fbval_t slice
[1 << 14];
143 for (i
= 0; i
< h
; i
++) {
144 for (j
= 0; j
< w
; j
++) {
145 int c
= *(unsigned char *) &s
[i
* w
+ j
];
146 int r
= (c
& 0x3) << 6;
147 int g
= ((c
>> 2) & 0x7) << 5;
148 int b
= ((c
>> 5) & 0x7) << 5;
149 slice
[j
] = FB_VAL(r
, g
, b
);
151 fb_set(y
+ i
, x
, slice
, w
);
155 static void xread(int fd
, void *buf
, int len
)
159 while (nr
< len
&& (n
= read(fd
, buf
+ nr
, len
- nr
)) > 0)
162 printf("partial vnc read!\n");
167 static int vnc_event(int fd
)
169 struct vnc_rect uprect
;
171 struct vnc_server_fbup
*fbup
= (void *) msg
;
172 struct vnc_server_cuttext
*cuttext
= (void *) msg
;
173 struct vnc_server_colormap
*colormap
= (void *) msg
;
177 if (read(fd
, msg
, 1) != 1)
180 case VNC_SERVER_FBUP
:
181 xread(fd
, msg
+ 1, sizeof(*fbup
) - 1);
183 for (j
= 0; j
< n
; j
++) {
185 xread(fd
, &uprect
, sizeof(uprect
));
190 if (x
>= cols
|| x
+ w
> cols
)
192 if (y
>= rows
|| y
+ h
> rows
)
194 xread(fd
, buf
, w
* h
);
195 drawfb(buf
, x
, y
, w
, h
);
198 case VNC_SERVER_BELL
:
200 case VNC_SERVER_CUTTEXT
:
201 xread(fd
, msg
+ 1, sizeof(*cuttext
) - 1);
202 xread(fd
, buf
, ntohl(cuttext
->len
));
204 case VNC_SERVER_COLORMAP
:
205 xread(fd
, msg
+ 1, sizeof(*colormap
) - 1);
206 xread(fd
, buf
, ntohs(colormap
->n
) * 3 * 2);
209 fprintf(stderr
, "unknown vnc msg: %d\n", msg
[0]);
215 static int rat_event(int fd
, int ratfd
)
218 struct vnc_client_ratevent me
= {VNC_CLIENT_RATEVENT
};
220 if (read(ratfd
, &ie
, sizeof(ie
)) != 3)
224 mc
= MAX(0, MIN(cols
- 1, mc
));
225 mr
= MAX(0, MIN(rows
- 1, mr
));
227 mask
|= VNC_BUTTON1_MASK
;
229 mask
|= VNC_BUTTON2_MASK
;
231 mask
|= VNC_BUTTON3_MASK
;
235 write(fd
, &me
, sizeof(me
));
239 static int press(int fd
, int key
, int down
)
241 struct vnc_client_keyevent ke
= {VNC_CLIENT_KEYEVENT
};
244 return write(fd
, &ke
, sizeof(ke
));
247 static int kbd_event(int fd
, int kbdfd
)
252 if ((nr
= read(kbdfd
, key
, sizeof(key
))) <= 0 )
254 for (i
= 0; i
< nr
; i
++) {
267 if (i
+ 2 < nr
&& key
[i
+ 1] == '[') {
268 if (key
[i
+ 2] == 'A')
270 if (key
[i
+ 2] == 'B')
272 if (key
[i
+ 2] == 'C')
274 if (key
[i
+ 2] == 'D')
276 if (key
[i
+ 2] == 'H')
285 mod
[nmod
++] = 0xffe9;
287 if (k
== 0x03) /* esc-^C: quit */
294 case 0x0c: /* ^L: redraw */
295 if (vnc_refresh(fd
, 0))
298 k
= (unsigned char) key
[i
];
300 if (k
>= 'A' && k
<= 'Z' || strchr(":\"<>?{}|+_()*&^%$#@!~", k
))
301 mod
[nmod
++] = 0xffe1;
302 if (k
>= 1 && k
<= 26) {
304 mod
[nmod
++] = 0xffe3;
308 for (j
= 0; j
< nmod
; j
++)
309 press(fd
, mod
[j
], 1);
312 for (j
= 0; j
< nmod
; j
++)
313 press(fd
, mod
[j
], 0);
319 static void term_setup(struct termios
*ti
)
321 struct termios termios
;
322 char *hide
= "\x1b[?25l";
323 char *clear
= "\x1b[2J\x1b[H";
324 char *msg
= "\t\t\t*** fbvnc ***\r\n";
326 write(STDIN_FILENO
, hide
, strlen(hide
));
327 write(STDOUT_FILENO
, clear
, strlen(clear
));
328 write(STDOUT_FILENO
, msg
, strlen(msg
));
329 tcgetattr (0, &termios
);
332 tcsetattr(0, TCSANOW
, &termios
);
335 static void term_cleanup(struct termios
*ti
)
337 char *show
= "\x1b[?25h";
338 tcsetattr(0, TCSANOW
, ti
);
339 write(STDIN_FILENO
, show
, strlen(show
));
342 static void mainloop(int vnc_fd
, int kbd_fd
, int rat_fd
)
344 struct pollfd ufds
[3];
348 ufds
[0].events
= POLLIN
;
350 ufds
[1].events
= POLLIN
;
352 ufds
[2].events
= POLLIN
;
353 if (vnc_refresh(vnc_fd
, 0))
356 err
= poll(ufds
, 3, 500);
357 if (err
== -1 && errno
!= EINTR
)
361 if (ufds
[0].revents
& POLLIN
)
362 if (kbd_event(vnc_fd
, kbd_fd
) == -1)
364 if (ufds
[1].revents
& POLLIN
) {
365 if (vnc_event(vnc_fd
) == -1)
369 if (ufds
[2].revents
& POLLIN
)
370 if (rat_event(vnc_fd
, rat_fd
) == -1)
373 if (vnc_refresh(vnc_fd
, 1))
378 int main(int argc
, char * argv
[])
380 char *port
= VNC_PORT
;
381 char *host
= "127.0.0.1";
388 if ((vnc_fd
= vnc_connect(host
, port
)) == -1) {
389 fprintf(stderr
, "could not connect!\n");
392 if (vnc_init(vnc_fd
) == -1) {
393 fprintf(stderr
, "vnc init failed!\n");
397 rat_fd
= open("/dev/input/mice", O_RDONLY
);
399 mainloop(vnc_fd
, 0, rat_fd
);