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>
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
)
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
;
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
)
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 */
115 cols
= MIN(srv_cols
, fb_cols());
116 rows
= MIN(srv_rows
, fb_rows());
117 bpp
= FBM_BPP(fb_mode());
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
;
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]));
147 static int vnc_free(void)
153 static int vnc_refresh(int fd
, int inc
)
155 struct vnc_updaterequest fbup_req
;
156 fbup_req
.type
= VNC_UPDATEREQUEST
;
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 */
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
)
182 while (nr
< len
&& (n
= read(fd
, buf
+ nr
, len
- nr
)) > 0)
185 printf("partial vnc read!\n");
190 static int vnc_event(int fd
)
192 struct vnc_rect uprect
;
194 struct vnc_update
*fbup
= (void *) msg
;
195 struct vnc_servercuttext
*cuttext
= (void *) msg
;
196 struct vnc_setcolormapentries
*colormap
= (void *) msg
;
200 if (read(fd
, msg
, 1) != 1)
204 xread(fd
, msg
+ 1, sizeof(*fbup
) - 1);
206 for (j
= 0; j
< n
; j
++) {
208 xread(fd
, &uprect
, sizeof(uprect
));
213 if (x
>= srv_cols
|| x
+ w
> srv_cols
)
215 if (y
>= srv_rows
|| y
+ h
> srv_rows
)
217 for (i
= 0; i
< h
; i
++) {
218 xread(fd
, buf
, w
* bpp
);
220 drawfb(buf
, x
, y
+ i
, w
, 1);
226 case VNC_SERVERCUTTEXT
:
227 xread(fd
, msg
+ 1, sizeof(*cuttext
) - 1);
228 xread(fd
, buf
, ntohl(cuttext
->len
));
230 case VNC_SETCOLORMAPENTRIES
:
231 xread(fd
, msg
+ 1, sizeof(*colormap
) - 1);
232 xread(fd
, buf
, ntohs(colormap
->n
) * 3 * 2);
235 fprintf(stderr
, "unknown vnc msg: %d\n", msg
[0]);
241 static int rat_event(int fd
, int ratfd
)
244 struct vnc_pointerevent me
= {VNC_POINTEREVENT
};
246 int or_
= or, oc_
= oc
;
247 if (ratfd
> 0 && read(ratfd
, &ie
, sizeof(ie
)) != 4)
249 /* ignore mouse movements when nodraw */
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
);
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
));
266 mask
|= VNC_BUTTON1_MASK
;
268 mask
|= VNC_BUTTON2_MASK
;
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
;
279 write(fd
, &me
, sizeof(me
));
280 if (or != or_
|| oc
!= oc_
)
281 if (vnc_refresh(fd
, 0))
286 static int press(int fd
, int key
, int down
)
288 struct vnc_keyevent ke
= {VNC_KEYEVENT
};
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
)
304 if ((nr
= read(kbdfd
, key
, sizeof(key
))) <= 0 )
306 for (i
= 0; i
< nr
; i
++) {
319 if (i
+ 2 < nr
&& key
[i
+ 1] == '[') {
320 if (key
[i
+ 2] == 'A')
322 if (key
[i
+ 2] == 'B')
324 if (key
[i
+ 2] == 'C')
326 if (key
[i
+ 2] == 'D')
328 if (key
[i
+ 2] == 'H')
337 mod
[nmod
++] = 0xffe9;
339 if (k
== 0x03) /* esc-^C: quit */
346 case 0x0: /* c-space: stop/start drawing */
352 if (vnc_refresh(fd
, 0))
356 k
= (unsigned char) key
[i
];
358 if ((k
>= 'A' && k
<= 'Z') || strchr(":\"<>?{}|+_()*&^%$#@!~", k
))
359 mod
[nmod
++] = 0xffe1;
360 if (k
>= 1 && k
<= 26) {
362 mod
[nmod
++] = 0xffe3;
366 for (j
= 0; j
< nmod
; j
++)
367 press(fd
, mod
[j
], 1);
370 for (j
= 0; j
< nmod
; j
++)
371 press(fd
, mod
[j
], 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 */
383 tcgetattr(0, &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];
401 ufds
[0].events
= POLLIN
;
403 ufds
[1].events
= POLLIN
;
405 ufds
[2].events
= POLLIN
;
406 rat_event(vnc_fd
, -1);
407 if (vnc_refresh(vnc_fd
, 0))
410 err
= poll(ufds
, 3, 500);
411 if (err
== -1 && errno
!= EINTR
)
415 if (ufds
[0].revents
& POLLIN
)
416 if (kbd_event(vnc_fd
, kbd_fd
) == -1)
418 if (ufds
[1].revents
& POLLIN
) {
419 if (vnc_event(vnc_fd
) == -1)
423 if (ufds
[2].revents
& POLLIN
)
424 if (rat_event(vnc_fd
, rat_fd
) == -1)
427 if (vnc_refresh(vnc_fd
, 1))
432 int main(int argc
, char * argv
[])
434 char *port
= VNC_PORT
;
435 char *host
= "127.0.0.1";
442 if ((vnc_fd
= vnc_connect(host
, port
)) < 0) {
443 fprintf(stderr
, "could not connect!\n");
446 if (vnc_init(vnc_fd
) < 0) {
448 fprintf(stderr
, "vnc init failed!\n");
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
);