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>
23 #include <netinet/in.h>
31 #include <sys/socket.h>
33 #include <sys/types.h>
34 #include <linux/input.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"
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
;
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
))
67 fd
= socket(addrinfo
->ai_family
, addrinfo
->ai_socktype
,
68 addrinfo
->ai_protocol
);
70 if (connect(fd
, addrinfo
->ai_addr
, addrinfo
->ai_addrlen
) == -1) {
72 freeaddrinfo(addrinfo
);
75 freeaddrinfo(addrinfo
);
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
)
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
;
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
)
106 clientinit
.shared
= 1;
107 write(fd
, &clientinit
, sizeof(clientinit
));
108 read(fd
, &serverinit
, sizeof(serverinit
));
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());
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
));
139 static int vnc_free(void)
145 static int vnc_refresh(int fd
, int inc
)
147 struct vnc_client_fbup fbup_req
;
148 fbup_req
.type
= VNC_CLIENT_FBUP
;
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 */
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
)
174 while (nr
< len
&& (n
= read(fd
, buf
+ nr
, len
- nr
)) > 0)
177 printf("partial vnc read!\n");
182 static int vnc_event(int fd
)
184 struct vnc_rect uprect
;
186 struct vnc_server_fbup
*fbup
= (void *) msg
;
187 struct vnc_server_cuttext
*cuttext
= (void *) msg
;
188 struct vnc_server_colormap
*colormap
= (void *) msg
;
192 if (read(fd
, msg
, 1) != 1)
195 case VNC_SERVER_FBUP
:
196 xread(fd
, msg
+ 1, sizeof(*fbup
) - 1);
198 for (j
= 0; j
< n
; j
++) {
200 xread(fd
, &uprect
, sizeof(uprect
));
205 if (x
>= srv_cols
|| x
+ w
> srv_cols
)
207 if (y
>= srv_rows
|| y
+ h
> srv_rows
)
209 for (i
= 0; i
< h
; i
++) {
210 xread(fd
, buf
, w
* bpp
);
212 drawfb(buf
, x
, y
+ i
, w
, 1);
216 case VNC_SERVER_BELL
:
218 case VNC_SERVER_CUTTEXT
:
219 xread(fd
, msg
+ 1, sizeof(*cuttext
) - 1);
220 xread(fd
, buf
, ntohl(cuttext
->len
));
222 case VNC_SERVER_COLORMAP
:
223 xread(fd
, msg
+ 1, sizeof(*colormap
) - 1);
224 xread(fd
, buf
, ntohs(colormap
->n
) * 3 * 2);
227 fprintf(stderr
, "unknown vnc msg: %d\n", msg
[0]);
233 static int rat_event(int fd
, int ratfd
)
236 struct vnc_client_ratevent me
= {VNC_CLIENT_RATEVENT
};
238 int or_
= or, oc_
= oc
;
239 if (read(ratfd
, &ie
, sizeof(ie
)) != 4)
241 /* ignore mouse movements when nodraw */
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
);
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
));
258 mask
|= VNC_BUTTON1_MASK
;
260 mask
|= VNC_BUTTON2_MASK
;
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
;
271 write(fd
, &me
, sizeof(me
));
272 if (or != or_
|| oc
!= oc_
)
273 if (vnc_refresh(fd
, 0))
278 static int press(int fd
, int key
, int down
)
280 struct vnc_client_keyevent ke
= {VNC_CLIENT_KEYEVENT
};
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
)
296 if ((nr
= read(kbdfd
, key
, sizeof(key
))) <= 0 )
298 for (i
= 0; i
< nr
; i
++) {
311 if (i
+ 2 < nr
&& key
[i
+ 1] == '[') {
312 if (key
[i
+ 2] == 'A')
314 if (key
[i
+ 2] == 'B')
316 if (key
[i
+ 2] == 'C')
318 if (key
[i
+ 2] == 'D')
320 if (key
[i
+ 2] == 'H')
329 mod
[nmod
++] = 0xffe9;
331 if (k
== 0x03) /* esc-^C: quit */
338 case 0x0: /* c-space: stop/start drawing */
344 if (vnc_refresh(fd
, 0))
348 k
= (unsigned char) key
[i
];
350 if (k
>= 'A' && k
<= 'Z' || strchr(":\"<>?{}|+_()*&^%$#@!~", k
))
351 mod
[nmod
++] = 0xffe1;
352 if (k
>= 1 && k
<= 26) {
354 mod
[nmod
++] = 0xffe3;
358 for (j
= 0; j
< nmod
; j
++)
359 press(fd
, mod
[j
], 1);
362 for (j
= 0; j
< nmod
; j
++)
363 press(fd
, mod
[j
], 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 */
375 tcgetattr(0, &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];
393 ufds
[0].events
= POLLIN
;
395 ufds
[1].events
= POLLIN
;
397 ufds
[2].events
= POLLIN
;
398 if (vnc_refresh(vnc_fd
, 0))
401 err
= poll(ufds
, 3, 500);
402 if (err
== -1 && errno
!= EINTR
)
406 if (ufds
[0].revents
& POLLIN
)
407 if (kbd_event(vnc_fd
, kbd_fd
) == -1)
409 if (ufds
[1].revents
& POLLIN
) {
410 if (vnc_event(vnc_fd
) == -1)
414 if (ufds
[2].revents
& POLLIN
)
415 if (rat_event(vnc_fd
, rat_fd
) == -1)
418 if (vnc_refresh(vnc_fd
, 1))
423 int main(int argc
, char * argv
[])
425 char *port
= VNC_PORT
;
426 char *host
= "127.0.0.1";
433 if ((vnc_fd
= vnc_connect(host
, port
)) < 0) {
434 fprintf(stderr
, "could not connect!\n");
437 if (vnc_init(vnc_fd
) < 0) {
439 fprintf(stderr
, "vnc init failed!\n");
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
);