3 Copyright (C) 2007-2011 Proxmox Server Solutions GmbH
5 Copyright: vzdump is under GNU GPL, the GNU General Public License.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; version 2 dated June, 1991.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 Author: Dietmar Maurer <dietmar@proxmox.com>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <arpa/inet.h>
32 #include <rfb/keysym.h>
33 #include <pty.h> /* for openpty and forkpty */
36 #include <sys/ioctl.h>
44 #include <gnutls/gnutls.h>
45 #include <gnutls/x509.h>
47 /* define this for debugging */
50 char *auth_path
= "/";
51 char *auth_perm
= "Sys.Console";
56 urlencode(char *buf
, const char *value
)
58 static const char *hexchar
= "0123456789abcdef";
61 int l
= strlen(value
);
62 for (i
= 0; i
< l
; i
++) {
64 if (('a' <= c
&& c
<= 'z') ||
65 ('A' <= c
&& c
<= 'Z') ||
66 ('0' <= c
&& c
<= '9')) {
72 *p
++ = hexchar
[c
>> 4];
73 *p
++ = hexchar
[c
& 15];
82 pve_auth_verify(const char *clientip
, const char *username
, const char *passwd
)
84 struct sockaddr_in server
;
86 int sfd
= socket(AF_INET
, SOCK_STREAM
, 0);
88 perror("pve_auth_verify: socket failed");
93 if ((he
= gethostbyname("localhost")) == NULL
) {
94 fprintf(stderr
, "pve_auth_verify: error resolving hostname\n");
98 memcpy(&server
.sin_addr
, he
->h_addr_list
[0], he
->h_length
);
99 server
.sin_family
= AF_INET
;
100 server
.sin_port
= htons(85);
102 if (connect(sfd
, (struct sockaddr
*)&server
, sizeof(server
))) {
103 perror("pve_auth_verify: error connecting to server");
111 p
= urlencode(p
, "username");
113 p
= urlencode(p
, username
);
116 p
= urlencode(p
, "password");
118 p
= urlencode(p
, passwd
);
121 p
= urlencode(p
, "path");
123 p
= urlencode(p
, auth_path
);
126 p
= urlencode(p
, "privs");
128 p
= urlencode(p
, auth_perm
);
130 sprintf(buf
, "POST /api2/json/access/ticket HTTP/1.1\n"
131 "Host: localhost:85\n"
132 "Connection: close\n"
134 "Content-Type: application/x-www-form-urlencoded\n"
135 "Content-Length: %zd\n\n%s\n", clientip
, strlen(form
), form
);
136 ssize_t len
= strlen(buf
);
137 ssize_t sb
= send(sfd
, buf
, len
, 0);
139 perror("pve_auth_verify: send failed");
143 fprintf(stderr
, "pve_auth_verify: partial send error\n");
147 len
= recv(sfd
, buf
, sizeof(buf
) - 1, 0);
149 perror("pve_auth_verify: recv failed");
155 //printf("DATA:%s\n", buf);
157 shutdown(sfd
, SHUT_RDWR
);
159 return strncmp(buf
, "HTTP/1.1 200 OK", 15);
162 shutdown(sfd
, SHUT_RDWR
);
167 static void vnc_debug_gnutls_log(int level
, const char* str
) {
168 fprintf(stderr
, "%d %s", level
, str
);
173 static gnutls_dh_params_t dh_params
;
176 gnutls_session_t session
;
181 gnutls_transport_ptr_t transport
,
185 rfbClientPtr cl
= (rfbClientPtr
)transport
;
189 n
= send(cl
->sock
, data
, len
, 0);
200 gnutls_transport_ptr_t transport
,
204 rfbClientPtr cl
= (rfbClientPtr
)transport
;
208 n
= recv(cl
->sock
, data
, len
, 0);
217 ssize_t
vnc_tls_read(rfbClientPtr cl
, void *buf
, size_t count
)
219 tls_client_t
*sd
= (tls_client_t
*)cl
->clientData
;
221 int ret
= gnutls_read(sd
->session
, buf
, count
);
223 if (ret
== GNUTLS_E_AGAIN
)
232 ssize_t
vnc_tls_write(rfbClientPtr cl
, void *buf
, size_t count
)
234 tls_client_t
*sd
= (tls_client_t
*)cl
->clientData
;
236 int ret
= gnutls_write(sd
->session
, buf
, count
);
238 if (ret
== GNUTLS_E_AGAIN
)
248 static gnutls_anon_server_credentials
249 tls_initialize_anon_cred(void)
251 gnutls_anon_server_credentials anon_cred
;
254 if ((ret
= gnutls_anon_allocate_server_credentials(&anon_cred
)) < 0) {
255 rfbLog("can't allocate credentials: %s\n", gnutls_strerror(ret
));
259 #if GNUTLS_VERSION_NUMBER >= 0x030506
260 gnutls_anon_set_server_known_dh_params(anon_cred
, GNUTLS_SEC_PARAM_MEDIUM
);
262 gnutls_anon_set_server_dh_params(anon_cred
, dh_params
);
268 static gnutls_certificate_credentials_t
269 tls_initialize_x509_cred(void)
271 gnutls_certificate_credentials_t x509_cred
;
274 /* Paths to x509 certs/keys */
275 char *x509cacert
= "/etc/pve/pve-root-ca.pem";
276 char *x509cert
= "/etc/pve/local/pve-ssl.pem";
277 char *x509key
= "/etc/pve/local/pve-ssl.key";
279 if ((ret
= gnutls_certificate_allocate_credentials(&x509_cred
)) < 0) {
280 rfbLog("can't allocate credentials: %s\n", gnutls_strerror(ret
));
284 if ((ret
= gnutls_certificate_set_x509_trust_file
285 (x509_cred
, x509cacert
, GNUTLS_X509_FMT_PEM
)) < 0) {
286 rfbLog("can't load CA certificate: %s\n", gnutls_strerror(ret
));
287 gnutls_certificate_free_credentials(x509_cred
);
291 if ((ret
= gnutls_certificate_set_x509_key_file
292 (x509_cred
, x509cert
, x509key
, GNUTLS_X509_FMT_PEM
)) < 0) {
293 rfbLog("can't load certificate & key: %s\n", gnutls_strerror(ret
));
294 gnutls_certificate_free_credentials(x509_cred
);
297 #if GNUTLS_VERSION_NUMBER >= 0x030506
298 /* only available since GnuTLS 3.5.6, on previous versions see
299 * gnutls_certificate_set_dh_params(). */
300 gnutls_certificate_set_known_dh_params(x509_cred
, GNUTLS_SEC_PARAM_MEDIUM
);
302 gnutls_certificate_set_dh_params (x509_cred
, dh_params
);
308 /* rfb tls security handler */
310 #define rfbSecTypeVencrypt 19
311 #define rfbVencryptTlsPlain 259
312 #define rfbVencryptX509Plain 262
314 void rfbEncodeU32(char *buf
, uint32_t value
)
316 buf
[0] = (value
>> 24) & 0xFF;
317 buf
[1] = (value
>> 16) & 0xFF;
318 buf
[2] = (value
>> 8) & 0xFF;
319 buf
[3] = value
& 0xFF;
322 uint32_t rfbDecodeU32(char *data
, size_t offset
)
324 return ((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
325 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
329 vencrypt_subauth_plain(rfbClientPtr cl
)
331 const char *err
= NULL
;
335 char clientip
[INET6_ADDRSTRLEN
];
337 struct sockaddr_in client
;
338 socklen_t addrlen
= sizeof(client
);
339 if (getpeername(cl
->sock
, &client
, &addrlen
) == 0) {
340 inet_ntop(client
.sin_family
, &client
.sin_addr
,
341 clientip
, sizeof(clientip
));
344 if ((n
= rfbReadExact(cl
, buf
, 8)) <= 0) {
345 err
= n
? "read failed" : "client gone";
349 uint32_t ulen
= rfbDecodeU32(buf
, 0);
350 uint32_t pwlen
= rfbDecodeU32(buf
, 4);
353 err
= "No User name.";
357 err
= "User name too long.";
361 err
= "Password too short";
365 err
= "Password too long.";
369 if ((n
= rfbReadExact(cl
, buf
, ulen
)) <= 0) {
370 err
= n
? "read failed" : "client gone";
374 char *username
= buf
;
375 char *passwd
= buf
+ ulen
+ 1;
376 if ((n
= rfbReadExact(cl
, passwd
, pwlen
)) <= 0) {
377 err
= n
? "read failed" : "client gone";
382 rfbLog("VencryptPlain: username: %s pw: %s\n", username
, passwd
);
384 if (pve_auth_verify(clientip
, username
, passwd
) == 0) {
385 rfbEncodeU32(buf
, 0); /* Accept auth completion */
386 rfbWriteExact(cl
, buf
, 4);
387 cl
->state
= RFB_INITIALISATION
;
391 err
= "Authentication failed";
393 rfbLog("VencryptPlain: %s\n", err
? err
: "no reason specified");
395 rfbEncodeU32(buf
, 1); /* Reject auth */
396 rfbWriteExact(cl
, buf
, 4);
397 if (cl
->protocolMinorVersion
>= 8) {
398 int elen
= strlen(err
);
399 rfbEncodeU32(buf
, elen
);
400 rfbWriteExact(cl
, buf
, 4);
401 rfbWriteExact(cl
, err
, elen
);
409 rfbVncAuthVencrypt(rfbClientPtr cl
)
413 /* Send VeNCrypt version 0.2 */
418 if (rfbWriteExact(cl
, buf
, 2) < 0) {
419 rfbLogPerror("rfbVncAuthVencrypt: write");
424 int n
= rfbReadExact(cl
, buf
, 2);
427 rfbLog("rfbVncAuthVencrypt: client gone\n");
429 rfbLogPerror("rfbVncAuthVencrypt: read");
434 if (buf
[0] != 0 || buf
[1] != 2) {
435 rfbLog("Unsupported VeNCrypt protocol %d.%d\n",
436 (int)buf
[0], (int)buf
[1]);
437 buf
[0] = 1; /* Reject version */
438 rfbWriteExact(cl
, buf
, 1);
443 /* Sending allowed auth */
444 int req_auth
= use_x509
? rfbVencryptX509Plain
: rfbVencryptTlsPlain
;
446 buf
[0] = 0; /* Accept version */
447 buf
[1] = 1; /* number of sub auths */
448 rfbEncodeU32(buf
+2, req_auth
);
449 if (rfbWriteExact(cl
, buf
, 6) < 0) {
450 rfbLogPerror("rfbVncAuthVencrypt: write");
455 n
= rfbReadExact(cl
, buf
, 4);
458 rfbLog("rfbVncAuthVencrypt: client gone\n");
460 rfbLogPerror("rfbVncAuthVencrypt: read");
465 int auth
= rfbDecodeU32(buf
, 0);
466 if (auth
!= req_auth
) {
467 buf
[0] = 1; /* Reject auth*/
468 rfbWriteExact(cl
, buf
, 1);
473 buf
[0] = 1; /* Accept auth */
474 if (rfbWriteExact(cl
, buf
, 1) < 0) {
475 rfbLogPerror("rfbVncAuthVencrypt: write");
480 tls_client_t
*sd
= calloc(1, sizeof(tls_client_t
));
482 if (sd
->session
== NULL
) {
483 if (gnutls_init(&sd
->session
, GNUTLS_SERVER
) < 0) {
484 rfbLog("gnutls_init failed\n");
490 if ((ret
= gnutls_set_default_priority(sd
->session
)) < 0) {
491 rfbLog("gnutls_set_default_priority failed: %s\n", gnutls_strerror(ret
));
497 static const char *priority_str_x509
= "NORMAL";
498 static const char *priority_str_anon
= "NORMAL:+ANON-ECDH:+ANON-DH";
499 if ((ret
= gnutls_priority_set_direct(sd
->session
, use_x509
? priority_str_x509
: priority_str_anon
, NULL
)) < 0) {
500 rfbLog("gnutls_priority_set_direct failed: %s\n", gnutls_strerror(ret
));
507 gnutls_certificate_server_credentials x509_cred
;
509 if (!(x509_cred
= tls_initialize_x509_cred())) {
515 if (gnutls_credentials_set(sd
->session
, GNUTLS_CRD_CERTIFICATE
, x509_cred
) < 0) {
517 gnutls_certificate_free_credentials(x509_cred
);
523 gnutls_anon_server_credentials anon_cred
;
525 if (!(anon_cred
= tls_initialize_anon_cred())) {
531 if ((ret
= gnutls_credentials_set(sd
->session
, GNUTLS_CRD_ANON
, anon_cred
)) < 0) {
532 rfbLog("gnutls_credentials_set failed: %s\n", gnutls_strerror(ret
));
533 gnutls_anon_free_server_credentials(anon_cred
);
540 gnutls_transport_set_ptr(sd
->session
, (gnutls_transport_ptr_t
)cl
);
541 gnutls_transport_set_push_function(sd
->session
, vnc_tls_push
);
542 gnutls_transport_set_pull_function(sd
->session
, vnc_tls_pull
);
547 if ((ret
= gnutls_handshake(sd
->session
)) < 0) {
548 if (!gnutls_error_is_fatal(ret
)) {
552 rfbLog("rfbVncAuthVencrypt: handshake failed\n");
557 /* set up TLS read/write hooks */
559 cl
->sock_read_fn
= &vnc_tls_read
;
560 cl
->sock_write_fn
= &vnc_tls_write
;
562 vencrypt_subauth_plain(cl
);
565 static rfbSecurityHandler VncSecurityHandlerVencrypt
= {
573 #define TERMIDCODE "[?1;2c" // vt100 ID
575 #define CHECK_ARGC(argc,argv,i) if (i >= argc-1) { \
576 fprintf (stderr, "ERROR: not enough arguments for: %s\n", argv[i]); \
577 print_usage (NULL); \
581 /* these colours are from linux kernel drivers/char/vt.c */
583 static int idle_timeout
= 1;
585 unsigned char color_table
[] = { 0, 4, 2, 6, 1, 5, 3, 7,
586 8,12,10,14, 9,13,11,15 };
588 /* the default colour table, for VGA+ colour systems */
589 int default_red
[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
590 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
591 int default_grn
[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
592 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
593 int default_blu
[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
594 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
597 print_usage (const char *msg
)
599 if (msg
) { fprintf (stderr
, "ERROR: %s\n", msg
); }
600 fprintf (stderr
, "USAGE: vncterm [vncopts] [-c command [args]]\n");
603 /* Convert UCS2 to UTF8 sequence, trailing zero */
605 ucs2_to_utf8 (unicode c
, char *out
)
608 out
[0] = c
; // 0*******
611 } else if (c
< 0x800) {
612 out
[0] = 0xc0 | (c
>> 6); // 110***** 10******
613 out
[1] = 0x80 | (c
& 0x3f);
617 out
[0] = 0xe0 | (c
>> 12); // 1110**** 10****** 10******
618 out
[1] = 0x80 | ((c
>> 6) & 0x3f);
619 out
[2] = 0x80 | (c
& 0x3f);
628 rfb_draw_char (rfbScreenInfoPtr rfbScreen
, int x
, int y
,
629 unsigned short c
, rfbPixel col
)
631 if (c
> vt_font_size
) {
632 rfbLog ("undefined font glyph %d\n", c
);
637 unsigned char *data
= vt_font_data
+ c
*16;
638 unsigned char d
=*data
;
639 int rowstride
=rfbScreen
->paddedWidthInBytes
;
640 char *colour
=(char*)&col
;
642 for(j
= 0; j
< 16; j
++) {
643 for(i
= 0; i
< 8; i
++) {
649 *(rfbScreen
->frameBuffer
+(y
+j
)*rowstride
+(x
+i
)) = *colour
;
656 draw_char_at (vncTerm
*vt
, int x
, int y
, unicode ch
, TextAttributes attrib
)
658 if (x
< 0 || y
< 0 || x
>= vt
->width
|| y
>= vt
->height
) { return; }
675 int ec
= vt_fontmap
[ch
];
677 rfbFillRect (vt
->screen
, rx
, ry
, rxe
, rye
, bg
);
683 // unsuported attributes = (attrib.blink || attrib.unvisible)
685 rfb_draw_char (vt
->screen
, rx
, ry
, ec
, fg
);
688 rfbDrawLine (vt
->screen
, rx
, ry
+ 14, rxe
, ry
+ 14, fg
);
691 rfbMarkRectAsModified (vt
->screen
, rx
, ry
, rxe
, rye
);
696 vncterm_update_xy (vncTerm
*vt
, int x
, int y
)
698 if (x
< 0 || y
< 0 || x
>= vt
->width
|| y
>= vt
->height
) { return; }
700 int y1
= (vt
->y_base
+ y
) % vt
->total_height
;
701 int y2
= y1
- vt
->y_displ
;
703 y2
+= vt
->total_height
;
705 if (y2
< vt
->height
) {
706 TextCell
*c
= &vt
->cells
[y1
* vt
->width
+ x
];
707 draw_char_at (vt
, x
, y2
, c
->ch
, c
->attrib
);
712 vncterm_clear_xy (vncTerm
*vt
, int x
, int y
)
714 if (x
< 0 || y
< 0 || x
>= vt
->width
|| y
>= vt
->height
) { return; }
716 int y1
= (vt
->y_base
+ y
) % vt
->total_height
;
717 int y2
= y1
- vt
->y_displ
;
719 y2
+= vt
->total_height
;
721 if (y2
< vt
->height
) {
722 TextCell
*c
= &vt
->cells
[y1
* vt
->width
+ x
];
724 c
->attrib
= vt
->default_attrib
;
725 c
->attrib
.fgcol
= vt
->cur_attrib
.fgcol
;
726 c
->attrib
.bgcol
= vt
->cur_attrib
.bgcol
;
728 draw_char_at (vt
, x
, y
, c
->ch
, c
->attrib
);
733 vncterm_show_cursor (vncTerm
*vt
, int show
)
736 if (x
>= vt
->width
) {
740 int y1
= (vt
->y_base
+ vt
->cy
) % vt
->total_height
;
741 int y
= y1
- vt
->y_displ
;
743 y
+= vt
->total_height
;
746 if (y
< vt
->height
) {
748 TextCell
*c
= &vt
->cells
[y1
* vt
->width
+ x
];
751 TextAttributes attrib
= vt
->default_attrib
;
752 attrib
.invers
= !(attrib
.invers
); /* invert fg and bg */
753 draw_char_at (vt
, x
, y
, c
->ch
, attrib
);
755 draw_char_at (vt
, x
, y
, c
->ch
, c
->attrib
);
761 vncterm_refresh (vncTerm
*vt
)
765 rfbFillRect (vt
->screen
, 0, 0, vt
->maxx
, vt
->maxy
, vt
->default_attrib
.bgcol
);
768 for(y
= 0; y
< vt
->height
; y
++) {
769 TextCell
*c
= vt
->cells
+ y1
* vt
->width
;
770 for(x
= 0; x
< vt
->width
; x
++) {
771 draw_char_at (vt
, x
, y
, c
->ch
, c
->attrib
);
774 if (++y1
== vt
->total_height
)
777 rfbMarkRectAsModified (vt
->screen
, 0, 0, vt
->maxx
, vt
->maxy
);
779 vncterm_show_cursor (vt
, 1);
783 vncterm_scroll_down (vncTerm
*vt
, int top
, int bottom
, int lines
)
785 if ((top
+ lines
) >= bottom
) {
786 lines
= bottom
- top
-1;
789 if (top
< 0 || bottom
> vt
->height
|| top
>= bottom
|| lines
< 1) {
797 int rowstride
= vt
->screen
->paddedWidthInBytes
;
798 int rows
= (bottom
- top
- lines
)*16;
800 char *in
= vt
->screen
->frameBuffer
+y0
*rowstride
;
801 char *out
= vt
->screen
->frameBuffer
+y1
*rowstride
;
802 memmove(out
,in
, rowstride
*rows
);
804 memset(vt
->screen
->frameBuffer
+y0
*rowstride
, 0, h
*rowstride
);
805 rfbMarkRectAsModified (vt
->screen
, 0, y0
, vt
->screen
->width
, y2
);
808 for(i
= bottom
- top
- lines
- 1; i
>= 0; i
--) {
809 int src
= ((vt
->y_base
+ top
+ i
) % vt
->total_height
)*vt
->width
;
810 int dst
= ((vt
->y_base
+ top
+ lines
+ i
) % vt
->total_height
)*vt
->width
;
812 memmove(vt
->cells
+ dst
, vt
->cells
+ src
, vt
->width
*sizeof (TextCell
));
815 for (i
= 0; i
< lines
; i
++) {
817 TextCell
*c
= vt
->cells
+ ((vt
->y_base
+ top
+ i
) % vt
->total_height
)*vt
->width
;
818 for(j
= 0; j
< vt
->width
; j
++) {
819 c
->attrib
= vt
->default_attrib
;
827 vncterm_scroll_up (vncTerm
*vt
, int top
, int bottom
, int lines
, int moveattr
)
829 if ((top
+ lines
) >= bottom
) {
830 lines
= bottom
- top
- 1;
833 if (top
< 0 || bottom
> vt
->height
|| top
>= bottom
|| lines
< 1) {
839 int y1
= (top
+ lines
)*16;
841 int rowstride
= vt
->screen
->paddedWidthInBytes
;
842 int rows
= (bottom
- top
- lines
)*16;
844 char *in
= vt
->screen
->frameBuffer
+y1
*rowstride
;
845 char *out
= vt
->screen
->frameBuffer
+y0
*rowstride
;
846 memmove(out
,in
, rowstride
*rows
);
848 memset(vt
->screen
->frameBuffer
+(y2
-h
)*rowstride
, 0, h
*rowstride
);
850 rfbMarkRectAsModified (vt
->screen
, 0, y0
, vt
->screen
->width
, y2
);
852 if (!moveattr
) return;
857 for(i
= 0; i
< (bottom
- top
- lines
); i
++) {
858 int dst
= ((vt
->y_base
+ top
+ i
) % vt
->total_height
)*vt
->width
;
859 int src
= ((vt
->y_base
+ top
+ lines
+ i
) % vt
->total_height
)*vt
->width
;
861 memmove(vt
->cells
+ dst
, vt
->cells
+ src
, vt
->width
*sizeof (TextCell
));
864 for (i
= 1; i
<= lines
; i
++) {
866 TextCell
*c
= vt
->cells
+ ((vt
->y_base
+ bottom
- i
) % vt
->total_height
)*vt
->width
;
867 for(j
= 0; j
< vt
->width
; j
++) {
868 c
->attrib
= vt
->default_attrib
;
876 vncterm_virtual_scroll (vncTerm
*vt
, int lines
)
878 if (vt
->altbuf
|| lines
== 0) return;
882 int i
= vt
->scroll_height
;
883 if (i
> vt
->total_height
- vt
->height
)
884 i
= vt
->total_height
- vt
->height
;
885 int y1
= vt
->y_base
- i
;
887 y1
+= vt
->total_height
;
888 for(i
= 0; i
< lines
; i
++) {
889 if (vt
->y_displ
== y1
) break;
890 if (--vt
->y_displ
< 0) {
891 vt
->y_displ
= vt
->total_height
- 1;
896 for(i
= 0; i
< lines
; i
++) {
897 if (vt
->y_displ
== vt
->y_base
) break;
898 if (++vt
->y_displ
== vt
->total_height
) {
905 vncterm_refresh (vt
);
908 vncterm_respond_esc (vncTerm
*vt
, const char *esc
)
910 int len
= strlen (esc
);
913 if (vt
->ibuf_count
< (IBUFSIZE
- 1 - len
)) {
914 vt
->ibuf
[vt
->ibuf_count
++] = 27;
915 for (i
= 0; i
< len
; i
++) {
916 vt
->ibuf
[vt
->ibuf_count
++] = esc
[i
];
922 vncterm_put_lf (vncTerm
*vt
)
924 if (vt
->cy
+ 1 == vt
->region_bottom
) {
926 if (vt
->altbuf
|| vt
->region_top
!= 0 || vt
->region_bottom
!= vt
->height
) {
927 vncterm_scroll_up (vt
, vt
->region_top
, vt
->region_bottom
, 1, 1);
931 if (vt
->y_displ
== vt
->y_base
) {
932 vncterm_scroll_up (vt
, vt
->region_top
, vt
->region_bottom
, 1, 0);
935 if (vt
->y_displ
== vt
->y_base
) {
936 if (++vt
->y_displ
== vt
->total_height
) {
941 if (++vt
->y_base
== vt
->total_height
) {
945 if (vt
->scroll_height
< vt
->total_height
) {
949 int y1
= (vt
->y_base
+ vt
->height
- 1) % vt
->total_height
;
950 TextCell
*c
= &vt
->cells
[y1
* vt
->width
];
952 for (x
= 0; x
< vt
->width
; x
++) {
954 c
->attrib
= vt
->default_attrib
;
958 // fprintf (stderr, "BASE: %d DISPLAY %d\n", vt->y_base, vt->y_displ);
960 } else if (vt
->cy
< vt
->height
- 1) {
967 vncterm_csi_m (vncTerm
*vt
)
971 for (i
= 0; i
< vt
->esc_count
; i
++) {
972 switch (vt
->esc_buf
[i
]) {
973 case 0: /* reset all console attributes to default */
974 vt
->cur_attrib
= vt
->default_attrib
;
977 vt
->cur_attrib
.bold
= 1;
980 vt
->cur_attrib
.uline
= 1;
983 vt
->cur_attrib
.blink
= 1;
986 vt
->cur_attrib
.invers
= 1;
989 vt
->cur_attrib
.unvisible
= 1;
992 vt
->cur_enc
= LAT1_MAP
;
993 // fixme: dispaly controls = 0 ?
994 // fixme: toggle meta = 0 ?
997 vt
->cur_enc
= IBMPC_MAP
;
998 // fixme: dispaly controls = 1 ?
999 // fixme: toggle meta = 0 ?
1002 vt
->cur_enc
= IBMPC_MAP
;
1003 // fixme: dispaly controls = 1 ?
1004 // fixme: toggle meta = 1 ?
1007 vt
->cur_attrib
.bold
= 0;
1010 vt
->cur_attrib
.uline
= 0;
1013 vt
->cur_attrib
.blink
= 0;
1016 vt
->cur_attrib
.invers
= 0;
1019 vt
->cur_attrib
.unvisible
= 0;
1029 /* set foreground color */
1030 vt
->cur_attrib
.fgcol
= color_table
[vt
->esc_buf
[i
] - 30];
1033 /* reset color to default, enable underline */
1034 vt
->cur_attrib
.fgcol
= vt
->default_attrib
.fgcol
;
1035 vt
->cur_attrib
.uline
= 1;
1038 /* reset color to default, disable underline */
1039 vt
->cur_attrib
.fgcol
= vt
->default_attrib
.fgcol
;
1040 vt
->cur_attrib
.uline
= 0;
1050 /* set background color */
1051 vt
->cur_attrib
.bgcol
= color_table
[vt
->esc_buf
[i
] - 40];
1054 /* reset background color */
1055 vt
->cur_attrib
.bgcol
= vt
->default_attrib
.bgcol
;
1058 fprintf (stderr
, "unhandled ESC[%d m code\n",vt
->esc_buf
[i
]);
1065 vncterm_save_cursor (vncTerm
*vt
)
1067 vt
->cx_saved
= vt
->cx
;
1068 vt
->cy_saved
= vt
->cy
;
1069 vt
->cur_attrib_saved
= vt
->cur_attrib
;
1070 vt
->charset_saved
= vt
->charset
;
1071 vt
->g0enc_saved
= vt
->g0enc
;
1072 vt
->g1enc_saved
= vt
->g1enc
;
1073 vt
->cur_enc_saved
= vt
->cur_enc
;
1077 vncterm_restore_cursor (vncTerm
*vt
)
1079 vt
->cx
= vt
->cx_saved
;
1080 vt
->cy
= vt
->cy_saved
;
1081 vt
->cur_attrib
= vt
->cur_attrib_saved
;
1082 vt
->charset
= vt
->charset_saved
;
1083 vt
->g0enc
= vt
->g0enc_saved
;
1084 vt
->g1enc
= vt
->g1enc_saved
;
1085 vt
->cur_enc
= vt
->cur_enc_saved
;
1089 vncterm_set_alternate_buffer (vncTerm
*vt
, int on_off
)
1093 vt
->y_displ
= vt
->y_base
;
1097 if (vt
->altbuf
) return;
1101 /* alternate buffer & cursor */
1103 vncterm_save_cursor (vt
);
1104 /* save screen to altcels */
1105 for (y
= 0; y
< vt
->height
; y
++) {
1106 int y1
= (vt
->y_base
+ y
) % vt
->total_height
;
1107 for (x
= 0; x
< vt
->width
; x
++) {
1108 vt
->altcells
[y
*vt
->width
+ x
] = vt
->cells
[y1
*vt
->width
+ x
];
1113 for (y
= 0; y
<= vt
->height
; y
++) {
1114 for (x
= 0; x
< vt
->width
; x
++) {
1115 vncterm_clear_xy (vt
, x
, y
);
1121 if (vt
->altbuf
== 0) return;
1125 /* restore saved data */
1126 for (y
= 0; y
< vt
->height
; y
++) {
1127 int y1
= (vt
->y_base
+ y
) % vt
->total_height
;
1128 for (x
= 0; x
< vt
->width
; x
++) {
1129 vt
->cells
[y1
*vt
->width
+ x
] = vt
->altcells
[y
*vt
->width
+ x
];
1133 vncterm_restore_cursor (vt
);
1136 vncterm_refresh (vt
);
1140 vncterm_set_mode (vncTerm
*vt
, int on_off
)
1144 for (i
= 0; i
<= vt
->esc_count
; i
++) {
1145 if (vt
->esc_ques
) { /* DEC private modes set/reset */
1146 switch(vt
->esc_buf
[i
]) {
1147 case 10: /* X11 mouse reporting on/off */
1149 vt
->report_mouse
= on_off
;
1151 case 1049: /* start/end special app mode (smcup/rmcup) */
1152 vncterm_set_alternate_buffer (vt
, on_off
);
1154 case 25: /* Cursor on/off */
1155 case 9: /* X10 mouse reporting on/off */
1156 case 6: /* Origin relative/absolute */
1157 case 1: /* Cursor keys in appl mode*/
1158 case 5: /* Inverted screen on/off */
1159 case 7: /* Autowrap on/off */
1160 case 8: /* Autorepeat on/off */
1163 } else { /* ANSI modes set/reset */
1164 /* fixme: implement me */
1170 vncterm_gotoxy (vncTerm
*vt
, int x
, int y
)
1172 /* verify all boundaries */
1178 if (x
>= vt
->width
) {
1188 if (y
>= vt
->height
) {
1195 enum { ESnormal
, ESesc
, ESsquare
, ESgetpars
, ESgotpars
, ESfunckey
,
1196 EShash
, ESsetG0
, ESsetG1
, ESpercent
, ESignore
, ESnonstd
,
1197 ESpalette
, ESidquery
, ESosc1
, ESosc2
};
1200 vncterm_putchar (vncTerm
*vt
, unicode ch
)
1206 fprintf (stderr
, "CHAR:%2d: %4x '%c' (cur_enc %d) %d %d\n", vt
->tty_state
, ch
, ch
, vt
->cur_enc
, vt
->cx
, vt
->cy
);
1209 switch(vt
->tty_state
) {
1211 vt
->tty_state
= ESnormal
;
1214 vt
->tty_state
= ESsquare
;
1217 vt
->tty_state
= ESnonstd
;
1220 vt
->tty_state
= ESpercent
;
1223 vncterm_save_cursor (vt
);
1226 vncterm_restore_cursor (vt
);
1229 vt
->tty_state
= ESsetG0
; // SET G0
1232 vt
->tty_state
= ESsetG1
; // SET G1
1235 /* cursor up (ri) */
1236 if (vt
->cy
== vt
->region_top
)
1237 vncterm_scroll_down (vt
, vt
->region_top
, vt
->region_bottom
, 1);
1238 else if (vt
->cy
> 0) {
1243 /* numeric keypad - ignored */
1246 /* appl. keypad - ignored */
1250 fprintf(stderr
, "got unhandled ESC%c %d\n", ch
, ch
);
1255 case ESnonstd
: /* Operating System Controls */
1256 vt
->tty_state
= ESnormal
;
1259 case 'P': /* palette escape sequence */
1260 for(i
= 0; i
< MAX_ESC_PARAMS
; i
++) {
1265 vt
->tty_state
= ESpalette
;
1267 case 'R': /* reset palette */
1268 // fixme: reset_palette(vc);
1275 vt
->osc_textbuf
[0] = 0;
1276 vt
->tty_state
= ESosc1
;
1280 fprintf (stderr
, "unhandled OSC %c\n", ch
);
1282 vt
->tty_state
= ESnormal
;
1287 vt
->tty_state
= ESnormal
;
1289 vt
->tty_state
= ESosc2
;
1292 fprintf (stderr
, "got illegal OSC sequence\n");
1297 if (ch
!= 0x9c && ch
!= 7) {
1299 while (vt
->osc_textbuf
[i
]) i
++;
1300 vt
->osc_textbuf
[i
++] = ch
;
1301 vt
->osc_textbuf
[i
] = 0;
1304 fprintf (stderr
, "OSC:%c:%s\n", vt
->osc_cmd
, vt
->osc_textbuf
);
1306 vt
->tty_state
= ESnormal
;
1310 if ((ch
>= '0' && ch
<= '9') || (ch
>= 'A' && ch
<= 'F')
1311 || (ch
>= 'a' && ch
<= 'f')) {
1312 vt
->esc_buf
[vt
->esc_count
++] = (ch
> '9' ? (ch
& 0xDF) - 'A' + 10 : ch
- '0');
1313 if (vt
->esc_count
== 7) {
1314 // fixme: this does not work - please test
1315 rfbColourMap
*cmap
=&vt
->screen
->colourMap
;
1317 int i
= color_table
[vt
->esc_buf
[0]] * 3, j
= 1;
1318 cmap
->data
.bytes
[i
] = 16 * vt
->esc_buf
[j
++];
1319 cmap
->data
.bytes
[i
++] += vt
->esc_buf
[j
++];
1320 cmap
->data
.bytes
[i
] = 16 * vt
->esc_buf
[j
++];
1321 cmap
->data
.bytes
[i
++] += vt
->esc_buf
[j
++];
1322 cmap
->data
.bytes
[i
] = 16 * vt
->esc_buf
[j
++];
1323 cmap
->data
.bytes
[i
] += vt
->esc_buf
[j
];
1325 //set_palette(vc); ?
1327 vt
->tty_state
= ESnormal
;
1330 vt
->tty_state
= ESnormal
;
1333 for(i
= 0; i
< MAX_ESC_PARAMS
; i
++) {
1338 vt
->esc_has_par
= 0;
1339 vt
->tty_state
= ESgetpars
;
1342 vt
->tty_state
= ESidquery
;
1346 if ((vt
->esc_ques
= (ch
== '?'))) {
1350 if (ch
>= '0' && ch
<= '9') {
1351 vt
->esc_has_par
= 1;
1352 if (vt
->esc_count
< MAX_ESC_PARAMS
) {
1353 vt
->esc_buf
[vt
->esc_count
] = vt
->esc_buf
[vt
->esc_count
] * 10 + ch
- '0';
1356 } else if (ch
== ';') {
1357 vt
->esc_has_par
= 1;
1361 if (vt
->esc_has_par
) {
1364 vt
->tty_state
= ESgotpars
;
1368 vt
->tty_state
= ESnormal
;
1371 char *qes
= vt
->esc_ques
? "?" : "";
1372 if (vt
->esc_count
== 0) {
1373 fprintf(stderr
, "ESC[%s%c\n", qes
, ch
);
1374 } else if (vt
->esc_count
== 1) {
1375 fprintf(stderr
, "ESC[%s%d%c\n", qes
, vt
->esc_buf
[0], ch
);
1378 fprintf(stderr
, "ESC[%s%d", qes
, vt
->esc_buf
[0]);
1379 for (i
= 1; i
< vt
->esc_count
; i
++) {
1380 fprintf(stderr
, ";%d", vt
->esc_buf
[i
]);
1382 fprintf (stderr
, "%c\n", ch
);
1388 vncterm_set_mode (vt
, 1);
1391 vncterm_set_mode (vt
, 0);
1394 if (!vt
->esc_count
) {
1395 vt
->esc_count
++; // default parameter 0
1400 /* report cursor position */
1401 /* TODO: send ESC[row;colR */
1404 /* move cursor up */
1405 if (vt
->esc_buf
[0] == 0) {
1408 vncterm_gotoxy (vt
, vt
->cx
, vt
->cy
- vt
->esc_buf
[0]);
1412 /* move cursor down */
1413 if (vt
->esc_buf
[0] == 0) {
1416 vncterm_gotoxy (vt
, vt
->cx
, vt
->cy
+ vt
->esc_buf
[0]);
1420 /* move cursor right */
1421 if (vt
->esc_buf
[0] == 0) {
1424 vncterm_gotoxy (vt
, vt
->cx
+ vt
->esc_buf
[0], vt
->cy
);
1427 /* move cursor left */
1428 if (vt
->esc_buf
[0] == 0) {
1431 vncterm_gotoxy (vt
, vt
->cx
- vt
->esc_buf
[0], vt
->cy
);
1435 /* move cursor to column */
1436 vncterm_gotoxy (vt
, vt
->esc_buf
[0] - 1, vt
->cy
);
1439 /* move cursor to row */
1440 vncterm_gotoxy (vt
, vt
->cx
, vt
->esc_buf
[0] - 1);
1444 /* move cursor to row, column */
1445 vncterm_gotoxy (vt
, vt
->esc_buf
[1] - 1, vt
->esc_buf
[0] - 1);
1448 switch (vt
->esc_buf
[0]) {
1450 /* clear to end of screen */
1451 for (y
= vt
->cy
; y
< vt
->height
; y
++) {
1452 for (x
= 0; x
< vt
->width
; x
++) {
1453 if (y
== vt
->cy
&& x
< vt
->cx
) {
1456 vncterm_clear_xy (vt
, x
, y
);
1461 /* clear from beginning of screen */
1462 for (y
= 0; y
<= vt
->cy
; y
++) {
1463 for (x
= 0; x
< vt
->width
; x
++) {
1464 if (y
== vt
->cy
&& x
> vt
->cx
) {
1467 vncterm_clear_xy (vt
, x
, y
);
1472 /* clear entire screen */
1473 for (y
= 0; y
<= vt
->height
; y
++) {
1474 for (x
= 0; x
< vt
->width
; x
++) {
1475 vncterm_clear_xy (vt
, x
, y
);
1482 switch (vt
->esc_buf
[0]) {
1485 for(x
= vt
->cx
; x
< vt
->width
; x
++) {
1486 vncterm_clear_xy (vt
, x
, vt
->cy
);
1490 /* clear from beginning of line */
1491 for (x
= 0; x
<= vt
->cx
; x
++) {
1492 vncterm_clear_xy (vt
, x
, vt
->cy
);
1496 /* clear entire line */
1497 for(x
= 0; x
< vt
->width
; x
++) {
1498 vncterm_clear_xy (vt
, x
, vt
->cy
);
1507 if (c
> vt
->height
- vt
->cy
)
1508 c
= vt
->height
- vt
->cy
;
1512 vncterm_scroll_down (vt
, vt
->cy
, vt
->region_bottom
, c
);
1518 if (c
> vt
->height
- vt
->cy
)
1519 c
= vt
->height
- vt
->cy
;
1523 vncterm_scroll_up (vt
, vt
->cy
, vt
->region_bottom
, c
, 1);
1529 vncterm_scroll_down (vt
, vt
->region_top
, vt
->region_bottom
, c
);
1535 vncterm_scroll_up (vt
, vt
->region_top
, vt
->region_bottom
, c
, 1);
1538 /* delete c character */
1541 if (c
> vt
->width
- vt
->cx
)
1542 c
= vt
->width
- vt
->cx
;
1546 for (x
= vt
->cx
; x
< vt
->width
- c
; x
++) {
1547 int y1
= (vt
->y_base
+ vt
->cy
) % vt
->total_height
;
1548 TextCell
*dst
= &vt
->cells
[y1
* vt
->width
+ x
];
1549 TextCell
*src
= dst
+ c
;
1551 vncterm_update_xy (vt
, x
+ c
, vt
->cy
);
1553 src
->attrib
= vt
->default_attrib
;
1554 vncterm_update_xy (vt
, x
, vt
->cy
);
1558 /* save cursor position */
1559 vncterm_save_cursor (vt
);
1562 /* restore cursor position */
1563 vncterm_restore_cursor (vt
);
1566 /* erase c characters */
1570 if (c
> (vt
->width
- vt
->cx
)) c
= vt
->width
- vt
->cx
;
1572 for(i
= 0; i
< c
; i
++) {
1573 vncterm_clear_xy (vt
, vt
->cx
+ i
, vt
->cy
);
1577 /* insert c character */
1579 if (c
> (vt
->width
- vt
->cx
)) {
1580 c
= vt
->width
- vt
->cx
;
1584 for (x
= vt
->width
- c
; x
>= vt
->cx
; x
--) {
1585 int y1
= (vt
->y_base
+ vt
->cy
) % vt
->total_height
;
1586 TextCell
*src
= &vt
->cells
[y1
* vt
->width
+ x
];
1587 TextCell
*dst
= src
+ c
;
1589 vncterm_update_xy (vt
, x
+ c
, vt
->cy
);
1591 src
->attrib
= vt
->cur_attrib
;
1592 vncterm_update_xy (vt
, x
, vt
->cy
);
1598 if (!vt
->esc_buf
[0])
1600 if (!vt
->esc_buf
[1])
1601 vt
->esc_buf
[1] = vt
->height
;
1602 /* Minimum allowed region is 2 lines */
1603 if (vt
->esc_buf
[0] < vt
->esc_buf
[1] &&
1604 vt
->esc_buf
[1] <= vt
->height
) {
1605 vt
->region_top
= vt
->esc_buf
[0] - 1;
1606 vt
->region_bottom
= vt
->esc_buf
[1];
1608 vt
->cy
= vt
->region_top
;
1610 fprintf (stderr
, "set region %d %d\n", vt
->region_top
, vt
->region_bottom
);
1617 if (vt
->esc_count
== 0) {
1618 fprintf(stderr
, "unhandled escape ESC[%s%c\n", qes
, ch
);
1619 } else if (vt
->esc_count
== 1) {
1620 fprintf(stderr
, "unhandled escape ESC[%s%d%c\n", qes
, vt
->esc_buf
[0], ch
);
1623 fprintf(stderr
, "unhandled escape ESC[%s%d", qes
, vt
->esc_buf
[0]);
1624 for (i
= 1; i
< vt
->esc_count
; i
++) {
1625 fprintf(stderr
, ";%d", vt
->esc_buf
[i
]);
1627 fprintf (stderr
, "%c\n", ch
);
1634 case ESsetG0
: // Set G0
1635 vt
->tty_state
= ESnormal
;
1638 vt
->g0enc
= GRAF_MAP
;
1640 vt
->g0enc
= LAT1_MAP
;
1642 vt
->g0enc
= IBMPC_MAP
;
1644 vt
->g0enc
= USER_MAP
;
1646 if (vt
->charset
== 0)
1647 vt
->cur_enc
= vt
->g0enc
;
1650 case ESsetG1
: // Set G1
1651 vt
->tty_state
= ESnormal
;
1654 vt
->g1enc
= GRAF_MAP
;
1656 vt
->g1enc
= LAT1_MAP
;
1658 vt
->g1enc
= IBMPC_MAP
;
1660 vt
->g1enc
= USER_MAP
;
1662 if (vt
->charset
== 1)
1663 vt
->cur_enc
= vt
->g1enc
;
1666 case ESidquery
: // vt100 query id
1667 vt
->tty_state
= ESnormal
;
1671 fprintf (stderr
, "ESC[>c Query term ID\n");
1673 vncterm_respond_esc (vt
, TERMIDCODE
);
1677 vt
->tty_state
= ESnormal
;
1679 case '@': /* defined in ISO 2022 */
1682 case 'G': /* prelim official escape code */
1683 case '8': /* retained for compatibility */
1688 default: // ESnormal
1689 vt
->tty_state
= ESnormal
;
1694 case 7: /* alert aka. bell */
1695 rfbSendBell(vt
->screen
);
1697 case 8: /* backspace */
1701 case 9: /* tabspace */
1702 if (vt
->cx
+ (8 - (vt
->cx
% 8)) > vt
->width
) {
1704 vncterm_put_lf (vt
);
1706 vt
->cx
= vt
->cx
+ (8 - (vt
->cx
% 8));
1712 vncterm_put_lf (vt
);
1714 case 13: /* carriage return */
1718 /* SI (shift in), select character set 1 */
1720 vt
->cur_enc
= vt
->g1enc
;
1721 /* fixme: display controls = 1 */
1724 /* SO (shift out), select character set 0 */
1726 vt
->cur_enc
= vt
->g0enc
;
1727 /* fixme: display controls = 0 */
1730 vt
->tty_state
= ESesc
;
1732 case 127: /* delete */
1735 case 128+27: /* csi */
1736 vt
->tty_state
= ESsquare
;
1739 if (vt
->cx
>= vt
->width
) {
1742 vncterm_put_lf (vt
);
1745 int y1
= (vt
->y_base
+ vt
->cy
) % vt
->total_height
;
1746 TextCell
*c
= &vt
->cells
[y1
*vt
->width
+ vt
->cx
];
1747 c
->attrib
= vt
->cur_attrib
;
1749 vncterm_update_xy (vt
, vt
->cx
, vt
->cy
);
1758 vncterm_puts (vncTerm
*vt
, const char *buf
, int len
)
1762 vncterm_show_cursor (vt
, 0);
1765 unsigned char c
= *buf
;
1769 if (vt
->tty_state
!= ESnormal
) {
1770 // never translate escape sequence
1772 } else if (vt
->utf8
&& !vt
->cur_enc
) {
1774 if(c
& 0x80) { // utf8 multi-byte sequence
1776 if (vt
->utf_count
> 0 && (c
& 0xc0) == 0x80) {
1777 // inside UTF8 sequence
1778 vt
->utf_char
= (vt
->utf_char
<< 6) | (c
& 0x3f);
1780 if (vt
->utf_count
== 0) {
1781 if (vt
->utf_char
<= USHRT_MAX
) {
1790 // first char of a UTF8 sequence
1791 if ((c
& 0xe0) == 0xc0) {
1793 vt
->utf_char
= (c
& 0x1f);
1794 } else if ((c
& 0xf0) == 0xe0) {
1796 vt
->utf_char
= (c
& 0x0f);
1797 } else if ((c
& 0xf8) == 0xf0) {
1799 vt
->utf_char
= (c
& 0x07);
1800 } else if ((c
& 0xfc) == 0xf8) {
1802 vt
->utf_char
= (c
& 0x03);
1803 } else if ((c
& 0xfe) == 0xfc) {
1805 vt
->utf_char
= (c
& 0x01);
1818 // never translate controls
1819 if (c
>= 32 && c
!= 127 && c
!= (128+27)) {
1820 tc
= translations
[vt
->cur_enc
][c
& 0x0ff];
1826 vncterm_putchar (vt
, tc
);
1829 vncterm_show_cursor (vt
, 1);
1834 vncterm_kbd_event (rfbBool down
, rfbKeySym keySym
, rfbClientPtr cl
)
1836 vncTerm
*vt
=(vncTerm
*)cl
->screen
->screenData
;
1837 static int control
= 0;
1838 static int shift
= 0;
1841 //fprintf (stderr, "KEYEVENT:%d: %08x\n", down == 0, keySym);fflush (stderr);
1843 //fprintf (stderr, "KEYPRESS: %d\n", keySym);fflush (stderr);
1845 if (keySym
== XK_Shift_L
|| keySym
== XK_Shift_R
) {
1847 } if (keySym
== XK_Control_L
|| keySym
== XK_Control_R
) {
1849 } else if (vt
->ibuf_count
< (IBUFSIZE
- 32)) {
1852 if(keySym
>= 'a' && keySym
<= 'z')
1854 else if (keySym
>= 'A' && keySym
<= 'Z')
1868 case XK_Delete
: /* kdch1 */
1871 case XK_Home
: /* khome */
1875 case XK_KP_End
: /* kend */
1877 case XK_Insert
: /* kich1 */
1881 case XK_KP_Up
: /* kcuu1 */
1883 case XK_Down
: /* kcud1 */
1887 case XK_KP_Right
: /* kcuf1 */
1890 case XK_KP_Left
: /* kcub1 */
1894 vncterm_virtual_scroll (vt
, -vt
->height
/2);
1900 vncterm_virtual_scroll (vt
, vt
->height
/2);
1934 fprintf (stderr
, "KEYPRESS OUT:%s: %d\n", esc
, keySym
); fflush (stderr
);
1937 if (vt
->y_displ
!= vt
->y_base
) {
1938 vt
->y_displ
= vt
->y_base
;
1939 vncterm_refresh (vt
);
1943 vncterm_respond_esc (vt
, esc
);
1944 } else if(keySym
<0x100) {
1946 int len
= ucs2_to_utf8 (keySym
& 0x0fff, &vt
->ibuf
[vt
->ibuf_count
]);
1947 vt
->ibuf_count
+= len
;
1949 vt
->ibuf
[vt
->ibuf_count
++] = (char)keySym
;
1954 if (keySym
== XK_Shift_L
|| keySym
== XK_Shift_R
) {
1956 } else if (keySym
== XK_Control_L
|| keySym
== XK_Control_R
) {
1963 vncterm_set_xcut_text (char* str
, int len
, struct _rfbClientRec
* cl
)
1965 vncTerm
*vt
=(vncTerm
*)cl
->screen
->screenData
;
1967 // seems str is Latin-1 encoded
1968 if (vt
->selection
) free (vt
->selection
);
1969 vt
->selection
= (unicode
*)malloc (len
*sizeof (unicode
));
1971 for (i
= 0; i
< len
; i
++) {
1972 vt
->selection
[i
] = str
[i
] & 0xff;
1974 vt
->selection_len
= len
;
1978 mouse_report (vncTerm
*vt
, int butt
, int mrx
, int mry
)
1982 sprintf (buf
, "[M%c%c%c", (char)(' ' + butt
), (char)('!' + mrx
),
1985 vncterm_respond_esc (vt
, buf
);
1989 vncterm_toggle_marked_cell (vncTerm
*vt
, int pos
)
1991 int x
= (pos
%vt
->width
)*8;
1992 int y
= (pos
/vt
->width
)*16;
1995 rfbScreenInfoPtr s
=vt
->screen
;
1997 char *b
= s
->frameBuffer
+y
*s
->width
+x
;
1999 for (j
=0; j
< 16; j
++) {
2000 for(i
=0; i
< 8; i
++) {
2001 b
[j
*s
->width
+i
] ^= 0x0f;
2002 rfbMarkRectAsModified (s
, x
, y
, x
+8, y
+16);
2008 vncterm_pointer_event (int buttonMask
, int x
, int y
, rfbClientPtr cl
)
2010 vncTerm
*vt
=(vncTerm
*)cl
->screen
->screenData
;
2011 static int button2_released
= 1;
2012 static int last_mask
= 0;
2013 static int sel_start_pos
= 0;
2014 static int sel_end_pos
= 0;
2021 if (cx
>= vt
->width
) cx
= vt
->width
- 1;
2023 if (cy
>= vt
->height
) cy
= vt
->height
- 1;
2025 if (vt
->report_mouse
&& buttonMask
!= last_mask
) {
2026 last_mask
= buttonMask
;
2027 if (buttonMask
& 1) {
2028 mouse_report (vt
, 0, cx
, cy
);
2030 if (buttonMask
& 2) {
2031 mouse_report (vt
, 1, cx
, cy
);
2033 if (buttonMask
& 4) {
2034 mouse_report (vt
, 2, cx
, cy
);
2037 mouse_report (vt
, 3, cx
, cy
);
2041 if (buttonMask
& 2) {
2042 if(button2_released
&& vt
->selection
) {
2044 for(i
= 0; i
< vt
->selection_len
; i
++) {
2045 if (vt
->ibuf_count
< IBUFSIZE
- 6) { // uft8 is max 6 characters wide
2047 vt
->ibuf_count
+= ucs2_to_utf8 (vt
->selection
[i
], &vt
->ibuf
[vt
->ibuf_count
]);
2049 vt
->ibuf
[vt
->ibuf_count
++] = vt
->selection
[i
];
2053 if (vt
->y_displ
!= vt
->y_base
) {
2054 vt
->y_displ
= vt
->y_base
;
2055 vncterm_refresh (vt
);
2058 button2_released
= 0;
2060 button2_released
= 1;
2063 if (buttonMask
& 1) {
2064 int pos
= cy
*vt
->width
+ cx
;
2066 // code borrowed from libvncserver (VNConsole.c)
2068 if (!vt
->mark_active
) {
2070 vt
->mark_active
= 1;
2071 sel_start_pos
= sel_end_pos
= pos
;
2072 vncterm_toggle_marked_cell (vt
, pos
);
2076 if (pos
!= sel_end_pos
) {
2078 if (pos
> sel_end_pos
) {
2079 cx
= sel_end_pos
; cy
=pos
;
2081 cx
=pos
; cy
=sel_end_pos
;
2084 if (cx
< sel_start_pos
) {
2085 if (cy
< sel_start_pos
) cy
--;
2091 vncterm_toggle_marked_cell (vt
, cx
);
2099 } else if (vt
->mark_active
) {
2100 vt
->mark_active
= 0;
2102 if (sel_start_pos
> sel_end_pos
) {
2103 int tmp
= sel_start_pos
- 1;
2104 sel_start_pos
= sel_end_pos
;
2108 int len
= sel_end_pos
- sel_start_pos
+ 1;
2110 if (vt
->selection
) free (vt
->selection
);
2111 vt
->selection
= (unicode
*)malloc (len
*sizeof (unicode
));
2112 vt
->selection_len
= len
;
2113 char *sel_latin1
= (char *)malloc (len
+ 1);
2115 for (i
= 0; i
< len
; i
++) {
2116 int pos
= sel_start_pos
+ i
;
2117 int x
= pos
% vt
->width
;
2118 int y1
= ((pos
/ vt
->width
) + vt
->y_displ
) % vt
->total_height
;
2119 TextCell
*c
= &vt
->cells
[y1
*vt
->width
+ x
];
2120 vt
->selection
[i
] = c
->ch
;
2121 sel_latin1
[i
] = (char)c
->ch
;
2124 sel_latin1
[len
] = 0;
2125 rfbGotXCutText (vt
->screen
, sel_latin1
, len
);
2128 while (sel_start_pos
<= sel_end_pos
) {
2129 vncterm_toggle_marked_cell (vt
, sel_start_pos
++);
2134 rfbDefaultPtrAddEvent (buttonMask
, x
, y
, cl
);
2137 static int client_count
= 0;
2138 static int client_connected
= 0;
2139 static int last_client
= 1;
2140 static time_t last_time
= 0;
2143 client_gone (rfbClientPtr client
)
2147 last_time
= time (NULL
);
2149 if (client_count
<= 0) {
2154 /* libvncserver callback for when a new client connects */
2155 enum rfbNewClientAction
2156 new_client (rfbClientPtr client
)
2158 client
->clientGoneHook
= client_gone
;
2161 last_time
= time (NULL
);
2164 client_connected
= 1;
2166 return RFB_CLIENT_ACCEPT
;
2169 static char *vncticket
= NULL
;
2172 create_vncterm (int argc
, char** argv
, int maxx
, int maxy
)
2176 rfbScreenInfoPtr screen
= rfbGetScreen (&argc
, argv
, maxx
, maxy
, 8, 1, 1);
2177 screen
->frameBuffer
=(char*)calloc(maxx
*maxy
, 1);
2179 char **passwds
= calloc(sizeof(char**), 2);
2181 vncTerm
*vt
= (vncTerm
*)calloc (sizeof(vncTerm
), 1);
2183 rfbColourMap
*cmap
=&screen
->colourMap
;
2184 cmap
->data
.bytes
= malloc (16*3);
2186 cmap
->data
.bytes
[i
*3 + 0] = default_red
[color_table
[i
]];
2187 cmap
->data
.bytes
[i
*3 + 1] = default_grn
[color_table
[i
]];
2188 cmap
->data
.bytes
[i
*3 + 2] = default_blu
[color_table
[i
]];
2192 screen
->serverFormat
.trueColour
= FALSE
;
2194 screen
->kbdAddEvent
= vncterm_kbd_event
;
2196 screen
->setXCutText
= vncterm_set_xcut_text
;
2198 screen
->ptrAddEvent
= vncterm_pointer_event
;
2200 screen
->desktopName
= "VNC Command Terminal";
2202 screen
->newClientHook
= new_client
;
2204 vt
->maxx
= screen
->width
;
2205 vt
->maxy
= screen
->height
;
2207 vt
->width
= vt
->maxx
/ 8;
2208 vt
->height
= vt
->maxy
/ 16;
2210 vt
->total_height
= vt
->height
* 20;
2211 vt
->scroll_height
= 0;
2216 vt
->region_bottom
= vt
->height
;
2218 vt
->g0enc
= LAT1_MAP
;
2219 vt
->g1enc
= GRAF_MAP
;
2220 vt
->cur_enc
= vt
->g0enc
;
2223 /* default text attributes */
2224 vt
->default_attrib
.bold
= 0;
2225 vt
->default_attrib
.uline
= 0;
2226 vt
->default_attrib
.blink
= 0;
2227 vt
->default_attrib
.invers
= 0;
2228 vt
->default_attrib
.unvisible
= 0;
2229 vt
->default_attrib
.fgcol
= 7;
2230 vt
->default_attrib
.bgcol
= 0;
2232 vt
->cur_attrib
= vt
->default_attrib
;
2234 vt
->cells
= (TextCell
*)calloc (sizeof (TextCell
), vt
->width
*vt
->total_height
);
2236 for (i
= 0; i
< vt
->width
*vt
->total_height
; i
++) {
2237 vt
->cells
[i
].ch
= ' ';
2238 vt
->cells
[i
].attrib
= vt
->default_attrib
;
2241 vt
->altcells
= (TextCell
*)calloc (sizeof (TextCell
), vt
->width
*vt
->height
);
2243 vt
->screen
= screen
;
2245 screen
->screenData
= (void*)vt
;
2247 //screen->autoPort = 1;
2250 passwds
[0] = vncticket
;
2253 screen
->authPasswdData
= (void *)passwds
;
2254 screen
->passwordCheck
= rfbCheckPasswordByList
;
2256 rfbRegisterSecurityHandler(&VncSecurityHandlerVencrypt
);
2259 rfbInitServer(screen
);
2265 main (int argc
, char** argv
)
2268 char **cmdargv
= NULL
;
2269 char *command
= "/bin/bash"; // execute normal shell as default
2274 struct timeval tv
, tv1
;
2275 time_t elapsed
, cur_time
;
2276 struct winsize dimensions
;
2278 if (gnutls_global_init () < 0) {
2279 fprintf(stderr
, "gnutls_global_init failed\n");
2283 if (gnutls_dh_params_init (&dh_params
) < 0) {
2284 fprintf(stderr
, "gnutls_dh_params_init failed\n");
2288 if (gnutls_dh_params_generate2 (dh_params
, DH_BITS
) < 0) {
2289 fprintf(stderr
, "gnutls_dh_params_init failed\n");
2293 for (i
= 1; i
< argc
; i
++) {
2294 if (!strcmp (argv
[i
], "-c")) {
2295 command
= argv
[i
+1];
2296 cmdargv
= &argv
[i
+1];
2303 for (i
= 1; i
< argc
; i
++) {
2304 if (!strcmp (argv
[i
], "-timeout")) {
2305 CHECK_ARGC (argc
, argv
, i
);
2306 idle_timeout
= atoi(argv
[i
+1]);
2307 rfbPurgeArguments(&argc
, &i
, 2, argv
); i
--;
2308 } else if (!strcmp (argv
[i
], "-authpath")) {
2309 CHECK_ARGC (argc
, argv
, i
);
2310 auth_path
= argv
[i
+1];
2311 rfbPurgeArguments(&argc
, &i
, 2, argv
); i
--;
2312 } else if (!strcmp (argv
[i
], "-perm")) {
2313 CHECK_ARGC (argc
, argv
, i
);
2314 auth_perm
= argv
[i
+1];
2315 rfbPurgeArguments(&argc
, &i
, 2, argv
); i
--;
2316 } else if (!strcmp (argv
[i
], "-notls")) {
2317 rfbPurgeArguments(&argc
, &i
, 1, argv
); i
--;
2318 if ((vncticket
= getenv("PVE_VNC_TICKET")) == NULL
) {
2319 fprintf(stderr
, "missing env PVE_VNC_TICKET (-notls)\n");
2325 unsetenv("PVE_VNC_TICKET"); // do not expose this to child
2329 gnutls_global_set_log_level(10);
2330 gnutls_global_set_log_function(vnc_debug_gnutls_log
);
2335 vncTerm
*vt
= create_vncterm (argc
, argv
, 745, 400);
2337 setlocale(LC_ALL
, ""); // set from environment
2339 char *ctype
= setlocale (LC_CTYPE
, NULL
); // query LC_CTYPE
2341 // fixme: ist there a standard way to detect utf8 mode ?
2342 if (strcasestr (ctype
, ".utf-8")||strcasestr (ctype
, ".utf8")) {
2346 dimensions
.ws_col
= vt
->width
;
2347 dimensions
.ws_row
= vt
->height
;
2349 setenv ("TERM", TERM
, 1);
2351 pid
= forkpty (&master
, ptyname
, NULL
, &dimensions
);
2354 // install default signal handlers
2355 signal (SIGQUIT
, SIG_DFL
);
2356 signal (SIGTERM
, SIG_DFL
);
2357 signal (SIGINT
, SIG_DFL
);
2360 execvp (command
, cmdargv
);
2362 execlp (command
, command
, NULL
);
2364 perror ("Error: exec failed\n");
2365 exit (-1); // should not be reached
2366 } else if (pid
== -1) {
2367 perror ("Error: fork failed\n");
2372 FD_SET (master
, &fs
);
2374 tv
.tv_usec
= 5000; /* 5 ms */
2376 last_time
= time (NULL
);
2384 cur_time
= time (NULL
);
2386 elapsed
= cur_time
- last_time
;
2387 //printf ("Elapsed %ld\n", elapsed);
2390 if (client_connected
) {
2391 if (idle_timeout
&& (elapsed
>= idle_timeout
)) {
2395 // wait at least 20 seconds for initial connect
2396 if (idle_timeout
&& (elapsed
>= (idle_timeout
> 20 ? idle_timeout
: 20))) {
2401 // exit after 30 minutes idle time
2402 if (elapsed
>= 30*60) {
2407 rfbProcessEvents (vt
->screen
, 40000); /* 40 ms */
2409 if (vt
->ibuf_count
> 0) {
2410 //printf ("DEBUG: WRITE %d %d\n", vt->ibuf[0], vt->ibuf_count);
2411 write (master
, vt
->ibuf
, vt
->ibuf_count
);
2413 last_time
= time (NULL
);
2416 if (!vt
->mark_active
) {
2418 int num_fds
= select (master
+1, &fs1
, NULL
, NULL
, &tv1
);
2420 if (FD_ISSET (master
, &fs1
)) {
2423 while ((c
= read (master
, buffer
, 1024)) == -1) {
2424 if (errno
!= EAGAIN
) break;
2427 vncterm_puts (vt
, buffer
, c
);
2437 waitpid(pid
, &status
, 0);