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";
53 uint16_t screen_width
= 744;
54 uint16_t screen_height
= 400;
59 urlencode(char *buf
, const char *value
)
61 static const char *hexchar
= "0123456789abcdef";
64 int l
= strlen(value
);
65 for (i
= 0; i
< l
; i
++) {
67 if (('a' <= c
&& c
<= 'z') ||
68 ('A' <= c
&& c
<= 'Z') ||
69 ('0' <= c
&& c
<= '9')) {
75 *p
++ = hexchar
[c
>> 4];
76 *p
++ = hexchar
[c
& 15];
85 pve_auth_verify(const char *clientip
, const char *username
, const char *passwd
)
87 struct sockaddr_in server
;
89 int sfd
= socket(AF_INET
, SOCK_STREAM
, 0);
91 perror("pve_auth_verify: socket failed");
96 if ((he
= gethostbyname("localhost")) == NULL
) {
97 fprintf(stderr
, "pve_auth_verify: error resolving hostname\n");
101 memcpy(&server
.sin_addr
, he
->h_addr_list
[0], he
->h_length
);
102 server
.sin_family
= AF_INET
;
103 server
.sin_port
= htons(85);
105 if (connect(sfd
, (struct sockaddr
*)&server
, sizeof(server
))) {
106 perror("pve_auth_verify: error connecting to server");
114 p
= urlencode(p
, "username");
116 p
= urlencode(p
, username
);
119 p
= urlencode(p
, "password");
121 p
= urlencode(p
, passwd
);
124 p
= urlencode(p
, "path");
126 p
= urlencode(p
, auth_path
);
129 p
= urlencode(p
, "privs");
131 p
= urlencode(p
, auth_perm
);
133 sprintf(buf
, "POST /api2/json/access/ticket HTTP/1.1\n"
134 "Host: localhost:85\n"
135 "Connection: close\n"
137 "Content-Type: application/x-www-form-urlencoded\n"
138 "Content-Length: %zd\n\n%s\n", clientip
, strlen(form
), form
);
139 ssize_t len
= strlen(buf
);
140 ssize_t sb
= send(sfd
, buf
, len
, 0);
142 perror("pve_auth_verify: send failed");
146 fprintf(stderr
, "pve_auth_verify: partial send error\n");
150 len
= recv(sfd
, buf
, sizeof(buf
) - 1, 0);
152 perror("pve_auth_verify: recv failed");
158 //printf("DATA:%s\n", buf);
160 shutdown(sfd
, SHUT_RDWR
);
162 return strncmp(buf
, "HTTP/1.1 200 OK", 15);
165 shutdown(sfd
, SHUT_RDWR
);
170 static void vnc_debug_gnutls_log(int level
, const char* str
) {
171 fprintf(stderr
, "%d %s", level
, str
);
176 static gnutls_dh_params_t dh_params
;
179 gnutls_session_t session
;
184 gnutls_transport_ptr_t transport
,
188 rfbClientPtr cl
= (rfbClientPtr
)transport
;
192 n
= send(cl
->sock
, data
, len
, 0);
203 gnutls_transport_ptr_t transport
,
207 rfbClientPtr cl
= (rfbClientPtr
)transport
;
211 n
= recv(cl
->sock
, data
, len
, 0);
220 ssize_t
vnc_tls_read(rfbClientPtr cl
, void *buf
, size_t count
)
222 tls_client_t
*sd
= (tls_client_t
*)cl
->clientData
;
224 int ret
= gnutls_read(sd
->session
, buf
, count
);
226 if (ret
== GNUTLS_E_AGAIN
)
235 ssize_t
vnc_tls_write(rfbClientPtr cl
, void *buf
, size_t count
)
237 tls_client_t
*sd
= (tls_client_t
*)cl
->clientData
;
239 int ret
= gnutls_write(sd
->session
, buf
, count
);
241 if (ret
== GNUTLS_E_AGAIN
)
251 static gnutls_anon_server_credentials
252 tls_initialize_anon_cred(void)
254 gnutls_anon_server_credentials anon_cred
;
257 if ((ret
= gnutls_anon_allocate_server_credentials(&anon_cred
)) < 0) {
258 rfbLog("can't allocate credentials: %s\n", gnutls_strerror(ret
));
262 #if GNUTLS_VERSION_NUMBER >= 0x030506
263 gnutls_anon_set_server_known_dh_params(anon_cred
, GNUTLS_SEC_PARAM_MEDIUM
);
265 gnutls_anon_set_server_dh_params(anon_cred
, dh_params
);
271 static gnutls_certificate_credentials_t
272 tls_initialize_x509_cred(void)
274 gnutls_certificate_credentials_t x509_cred
;
277 /* Paths to x509 certs/keys */
278 char *x509cacert
= "/etc/pve/pve-root-ca.pem";
279 char *x509cert
= "/etc/pve/local/pve-ssl.pem";
280 char *x509key
= "/etc/pve/local/pve-ssl.key";
282 if ((ret
= gnutls_certificate_allocate_credentials(&x509_cred
)) < 0) {
283 rfbLog("can't allocate credentials: %s\n", gnutls_strerror(ret
));
287 if ((ret
= gnutls_certificate_set_x509_trust_file
288 (x509_cred
, x509cacert
, GNUTLS_X509_FMT_PEM
)) < 0) {
289 rfbLog("can't load CA certificate: %s\n", gnutls_strerror(ret
));
290 gnutls_certificate_free_credentials(x509_cred
);
294 if ((ret
= gnutls_certificate_set_x509_key_file
295 (x509_cred
, x509cert
, x509key
, GNUTLS_X509_FMT_PEM
)) < 0) {
296 rfbLog("can't load certificate & key: %s\n", gnutls_strerror(ret
));
297 gnutls_certificate_free_credentials(x509_cred
);
300 #if GNUTLS_VERSION_NUMBER >= 0x030506
301 /* only available since GnuTLS 3.5.6, on previous versions see
302 * gnutls_certificate_set_dh_params(). */
303 gnutls_certificate_set_known_dh_params(x509_cred
, GNUTLS_SEC_PARAM_MEDIUM
);
305 gnutls_certificate_set_dh_params (x509_cred
, dh_params
);
311 /* rfb tls security handler */
313 #define rfbSecTypeVencrypt 19
314 #define rfbVencryptTlsPlain 259
315 #define rfbVencryptX509Plain 262
317 void rfbEncodeU32(char *buf
, uint32_t value
)
319 buf
[0] = (value
>> 24) & 0xFF;
320 buf
[1] = (value
>> 16) & 0xFF;
321 buf
[2] = (value
>> 8) & 0xFF;
322 buf
[3] = value
& 0xFF;
325 uint32_t rfbDecodeU32(char *data
, size_t offset
)
327 return ((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
328 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
332 vencrypt_subauth_plain(rfbClientPtr cl
)
334 const char *err
= NULL
;
338 char clientip
[INET6_ADDRSTRLEN
];
340 struct sockaddr_in client
;
341 socklen_t addrlen
= sizeof(client
);
342 if (getpeername(cl
->sock
, &client
, &addrlen
) == 0) {
343 inet_ntop(client
.sin_family
, &client
.sin_addr
,
344 clientip
, sizeof(clientip
));
347 if ((n
= rfbReadExact(cl
, buf
, 8)) <= 0) {
348 err
= n
? "read failed" : "client gone";
352 uint32_t ulen
= rfbDecodeU32(buf
, 0);
353 uint32_t pwlen
= rfbDecodeU32(buf
, 4);
356 err
= "No User name.";
360 err
= "User name too long.";
364 err
= "Password too short";
368 err
= "Password too long.";
372 if ((n
= rfbReadExact(cl
, buf
, ulen
)) <= 0) {
373 err
= n
? "read failed" : "client gone";
377 char *username
= buf
;
378 char *passwd
= buf
+ ulen
+ 1;
379 if ((n
= rfbReadExact(cl
, passwd
, pwlen
)) <= 0) {
380 err
= n
? "read failed" : "client gone";
385 rfbLog("VencryptPlain: username: %s pw: %s\n", username
, passwd
);
387 if (pve_auth_verify(clientip
, username
, passwd
) == 0) {
388 rfbEncodeU32(buf
, 0); /* Accept auth completion */
389 rfbWriteExact(cl
, buf
, 4);
390 cl
->state
= RFB_INITIALISATION
;
394 err
= "Authentication failed";
396 rfbLog("VencryptPlain: %s\n", err
? err
: "no reason specified");
398 rfbEncodeU32(buf
, 1); /* Reject auth */
399 rfbWriteExact(cl
, buf
, 4);
400 if (cl
->protocolMinorVersion
>= 8) {
401 int elen
= strlen(err
);
402 rfbEncodeU32(buf
, elen
);
403 rfbWriteExact(cl
, buf
, 4);
404 rfbWriteExact(cl
, err
, elen
);
412 rfbVncAuthVencrypt(rfbClientPtr cl
)
416 /* Send VeNCrypt version 0.2 */
421 if (rfbWriteExact(cl
, buf
, 2) < 0) {
422 rfbLogPerror("rfbVncAuthVencrypt: write");
427 int n
= rfbReadExact(cl
, buf
, 2);
430 rfbLog("rfbVncAuthVencrypt: client gone\n");
432 rfbLogPerror("rfbVncAuthVencrypt: read");
437 if (buf
[0] != 0 || buf
[1] != 2) {
438 rfbLog("Unsupported VeNCrypt protocol %d.%d\n",
439 (int)buf
[0], (int)buf
[1]);
440 buf
[0] = 1; /* Reject version */
441 rfbWriteExact(cl
, buf
, 1);
446 /* Sending allowed auth */
447 int req_auth
= use_x509
? rfbVencryptX509Plain
: rfbVencryptTlsPlain
;
449 buf
[0] = 0; /* Accept version */
450 buf
[1] = 1; /* number of sub auths */
451 rfbEncodeU32(buf
+2, req_auth
);
452 if (rfbWriteExact(cl
, buf
, 6) < 0) {
453 rfbLogPerror("rfbVncAuthVencrypt: write");
458 n
= rfbReadExact(cl
, buf
, 4);
461 rfbLog("rfbVncAuthVencrypt: client gone\n");
463 rfbLogPerror("rfbVncAuthVencrypt: read");
468 int auth
= rfbDecodeU32(buf
, 0);
469 if (auth
!= req_auth
) {
470 buf
[0] = 1; /* Reject auth*/
471 rfbWriteExact(cl
, buf
, 1);
476 buf
[0] = 1; /* Accept auth */
477 if (rfbWriteExact(cl
, buf
, 1) < 0) {
478 rfbLogPerror("rfbVncAuthVencrypt: write");
483 tls_client_t
*sd
= calloc(1, sizeof(tls_client_t
));
485 if (sd
->session
== NULL
) {
486 if (gnutls_init(&sd
->session
, GNUTLS_SERVER
) < 0) {
487 rfbLog("gnutls_init failed\n");
493 if ((ret
= gnutls_set_default_priority(sd
->session
)) < 0) {
494 rfbLog("gnutls_set_default_priority failed: %s\n", gnutls_strerror(ret
));
500 static const char *priority_str_x509
= "NORMAL";
501 static const char *priority_str_anon
= "NORMAL:+ANON-ECDH:+ANON-DH";
502 if ((ret
= gnutls_priority_set_direct(sd
->session
, use_x509
? priority_str_x509
: priority_str_anon
, NULL
)) < 0) {
503 rfbLog("gnutls_priority_set_direct failed: %s\n", gnutls_strerror(ret
));
510 gnutls_certificate_server_credentials x509_cred
;
512 if (!(x509_cred
= tls_initialize_x509_cred())) {
518 if (gnutls_credentials_set(sd
->session
, GNUTLS_CRD_CERTIFICATE
, x509_cred
) < 0) {
520 gnutls_certificate_free_credentials(x509_cred
);
526 gnutls_anon_server_credentials anon_cred
;
528 if (!(anon_cred
= tls_initialize_anon_cred())) {
534 if ((ret
= gnutls_credentials_set(sd
->session
, GNUTLS_CRD_ANON
, anon_cred
)) < 0) {
535 rfbLog("gnutls_credentials_set failed: %s\n", gnutls_strerror(ret
));
536 gnutls_anon_free_server_credentials(anon_cred
);
543 gnutls_transport_set_ptr(sd
->session
, (gnutls_transport_ptr_t
)cl
);
544 gnutls_transport_set_push_function(sd
->session
, vnc_tls_push
);
545 gnutls_transport_set_pull_function(sd
->session
, vnc_tls_pull
);
550 if ((ret
= gnutls_handshake(sd
->session
)) < 0) {
551 if (!gnutls_error_is_fatal(ret
)) {
555 rfbLog("rfbVncAuthVencrypt: handshake failed\n");
560 /* set up TLS read/write hooks */
562 cl
->sock_read_fn
= &vnc_tls_read
;
563 cl
->sock_write_fn
= &vnc_tls_write
;
565 vencrypt_subauth_plain(cl
);
568 static rfbSecurityHandler VncSecurityHandlerVencrypt
= {
576 #define TERMIDCODE "[?1;2c" // vt100 ID
578 #define CHECK_ARGC(argc,argv,i) if (i >= argc-1) { \
579 fprintf (stderr, "ERROR: not enough arguments for: %s\n", argv[i]); \
580 print_usage (NULL); \
584 /* these colours are from linux kernel drivers/char/vt.c */
586 static int idle_timeout
= 1;
588 unsigned char color_table
[] = { 0, 4, 2, 6, 1, 5, 3, 7,
589 8,12,10,14, 9,13,11,15 };
591 /* the default colour table, for VGA+ colour systems */
592 int default_red
[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
593 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
594 int default_grn
[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
595 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
596 int default_blu
[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
597 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
600 print_usage (const char *msg
)
602 if (msg
) { fprintf (stderr
, "ERROR: %s\n", msg
); }
603 fprintf (stderr
, "USAGE: vncterm [vncopts] [-c command [args]]\n");
606 /* Convert UCS2 to UTF8 sequence, trailing zero */
608 ucs2_to_utf8 (unicode c
, char *out
)
611 out
[0] = c
; // 0*******
614 } else if (c
< 0x800) {
615 out
[0] = 0xc0 | (c
>> 6); // 110***** 10******
616 out
[1] = 0x80 | (c
& 0x3f);
620 out
[0] = 0xe0 | (c
>> 12); // 1110**** 10****** 10******
621 out
[1] = 0x80 | ((c
>> 6) & 0x3f);
622 out
[2] = 0x80 | (c
& 0x3f);
631 rfb_draw_char (rfbScreenInfoPtr rfbScreen
, int x
, int y
,
632 unsigned short c
, rfbPixel col
)
634 if (c
> vt_font_size
) {
635 rfbLog ("undefined font glyph %d\n", c
);
640 unsigned char *data
= vt_font_data
+ c
*16;
641 unsigned char d
=*data
;
642 int rowstride
=rfbScreen
->paddedWidthInBytes
;
643 char *colour
=(char*)&col
;
645 for(j
= 0; j
< 16; j
++) {
646 for(i
= 0; i
< 8; i
++) {
652 *(rfbScreen
->frameBuffer
+(y
+j
)*rowstride
+(x
+i
)) = *colour
;
659 draw_char_at (vncTerm
*vt
, int x
, int y
, unicode ch
, TextAttributes attrib
)
661 if (x
< 0 || y
< 0 || x
>= vt
->width
|| y
>= vt
->height
) { return; }
678 int ec
= vt_fontmap
[ch
];
680 rfbFillRect (vt
->screen
, rx
, ry
, rxe
, rye
, bg
);
686 // unsuported attributes = (attrib.blink || attrib.unvisible)
688 rfb_draw_char (vt
->screen
, rx
, ry
, ec
, fg
);
691 rfbDrawLine (vt
->screen
, rx
, ry
+ 14, rxe
, ry
+ 14, fg
);
694 rfbMarkRectAsModified (vt
->screen
, rx
, ry
, rxe
, rye
);
699 vncterm_update_xy (vncTerm
*vt
, int x
, int y
)
701 if (x
< 0 || y
< 0 || x
>= vt
->width
|| y
>= vt
->height
) { return; }
703 int y1
= (vt
->y_base
+ y
) % vt
->total_height
;
704 int y2
= y1
- vt
->y_displ
;
706 y2
+= vt
->total_height
;
708 if (y2
< vt
->height
) {
709 TextCell
*c
= &vt
->cells
[y1
* vt
->width
+ x
];
710 draw_char_at (vt
, x
, y2
, c
->ch
, c
->attrib
);
715 vncterm_clear_xy (vncTerm
*vt
, int x
, int y
)
717 if (x
< 0 || y
< 0 || x
>= vt
->width
|| y
>= vt
->height
) { return; }
719 int y1
= (vt
->y_base
+ y
) % vt
->total_height
;
720 int y2
= y1
- vt
->y_displ
;
722 y2
+= vt
->total_height
;
724 if (y2
< vt
->height
) {
725 TextCell
*c
= &vt
->cells
[y1
* vt
->width
+ x
];
727 c
->attrib
= vt
->default_attrib
;
728 c
->attrib
.fgcol
= vt
->cur_attrib
.fgcol
;
729 c
->attrib
.bgcol
= vt
->cur_attrib
.bgcol
;
731 draw_char_at (vt
, x
, y
, c
->ch
, c
->attrib
);
736 vncterm_show_cursor (vncTerm
*vt
, int show
)
739 if (x
>= vt
->width
) {
743 int y1
= (vt
->y_base
+ vt
->cy
) % vt
->total_height
;
744 int y
= y1
- vt
->y_displ
;
746 y
+= vt
->total_height
;
749 if (y
< vt
->height
) {
751 TextCell
*c
= &vt
->cells
[y1
* vt
->width
+ x
];
754 TextAttributes attrib
= vt
->default_attrib
;
755 attrib
.invers
= !(attrib
.invers
); /* invert fg and bg */
756 draw_char_at (vt
, x
, y
, c
->ch
, attrib
);
758 draw_char_at (vt
, x
, y
, c
->ch
, c
->attrib
);
764 vncterm_refresh (vncTerm
*vt
)
768 rfbFillRect (vt
->screen
, 0, 0, vt
->maxx
, vt
->maxy
, vt
->default_attrib
.bgcol
);
771 for(y
= 0; y
< vt
->height
; y
++) {
772 TextCell
*c
= vt
->cells
+ y1
* vt
->width
;
773 for(x
= 0; x
< vt
->width
; x
++) {
774 draw_char_at (vt
, x
, y
, c
->ch
, c
->attrib
);
777 if (++y1
== vt
->total_height
)
780 rfbMarkRectAsModified (vt
->screen
, 0, 0, vt
->maxx
, vt
->maxy
);
782 vncterm_show_cursor (vt
, 1);
786 vncterm_scroll_down (vncTerm
*vt
, int top
, int bottom
, int lines
)
788 if ((top
+ lines
) >= bottom
) {
789 lines
= bottom
- top
-1;
792 if (top
< 0 || bottom
> vt
->height
|| top
>= bottom
|| lines
< 1) {
800 int rowstride
= vt
->screen
->paddedWidthInBytes
;
801 int rows
= (bottom
- top
- lines
)*16;
803 char *in
= vt
->screen
->frameBuffer
+y0
*rowstride
;
804 char *out
= vt
->screen
->frameBuffer
+y1
*rowstride
;
805 memmove(out
,in
, rowstride
*rows
);
807 memset(vt
->screen
->frameBuffer
+y0
*rowstride
, 0, h
*rowstride
);
808 rfbMarkRectAsModified (vt
->screen
, 0, y0
, vt
->screen
->width
, y2
);
811 for(i
= bottom
- top
- lines
- 1; i
>= 0; i
--) {
812 int src
= ((vt
->y_base
+ top
+ i
) % vt
->total_height
)*vt
->width
;
813 int dst
= ((vt
->y_base
+ top
+ lines
+ i
) % vt
->total_height
)*vt
->width
;
815 memmove(vt
->cells
+ dst
, vt
->cells
+ src
, vt
->width
*sizeof (TextCell
));
818 for (i
= 0; i
< lines
; i
++) {
820 TextCell
*c
= vt
->cells
+ ((vt
->y_base
+ top
+ i
) % vt
->total_height
)*vt
->width
;
821 for(j
= 0; j
< vt
->width
; j
++) {
822 c
->attrib
= vt
->default_attrib
;
830 vncterm_scroll_up (vncTerm
*vt
, int top
, int bottom
, int lines
, int moveattr
)
832 if ((top
+ lines
) >= bottom
) {
833 lines
= bottom
- top
- 1;
836 if (top
< 0 || bottom
> vt
->height
|| top
>= bottom
|| lines
< 1) {
842 int y1
= (top
+ lines
)*16;
844 int rowstride
= vt
->screen
->paddedWidthInBytes
;
845 int rows
= (bottom
- top
- lines
)*16;
847 char *in
= vt
->screen
->frameBuffer
+y1
*rowstride
;
848 char *out
= vt
->screen
->frameBuffer
+y0
*rowstride
;
849 memmove(out
,in
, rowstride
*rows
);
851 memset(vt
->screen
->frameBuffer
+(y2
-h
)*rowstride
, 0, h
*rowstride
);
853 rfbMarkRectAsModified (vt
->screen
, 0, y0
, vt
->screen
->width
, y2
);
855 if (!moveattr
) return;
860 for(i
= 0; i
< (bottom
- top
- lines
); i
++) {
861 int dst
= ((vt
->y_base
+ top
+ i
) % vt
->total_height
)*vt
->width
;
862 int src
= ((vt
->y_base
+ top
+ lines
+ i
) % vt
->total_height
)*vt
->width
;
864 memmove(vt
->cells
+ dst
, vt
->cells
+ src
, vt
->width
*sizeof (TextCell
));
867 for (i
= 1; i
<= lines
; i
++) {
869 TextCell
*c
= vt
->cells
+ ((vt
->y_base
+ bottom
- i
) % vt
->total_height
)*vt
->width
;
870 for(j
= 0; j
< vt
->width
; j
++) {
871 c
->attrib
= vt
->default_attrib
;
879 vncterm_virtual_scroll (vncTerm
*vt
, int lines
)
881 if (vt
->altbuf
|| lines
== 0) return;
885 int i
= vt
->scroll_height
;
886 if (i
> vt
->total_height
- vt
->height
)
887 i
= vt
->total_height
- vt
->height
;
888 int y1
= vt
->y_base
- i
;
890 y1
+= vt
->total_height
;
891 for(i
= 0; i
< lines
; i
++) {
892 if (vt
->y_displ
== y1
) break;
893 if (--vt
->y_displ
< 0) {
894 vt
->y_displ
= vt
->total_height
- 1;
899 for(i
= 0; i
< lines
; i
++) {
900 if (vt
->y_displ
== vt
->y_base
) break;
901 if (++vt
->y_displ
== vt
->total_height
) {
908 vncterm_refresh (vt
);
911 vncterm_respond_esc (vncTerm
*vt
, const char *esc
)
913 int len
= strlen (esc
);
916 if (vt
->ibuf_count
< (IBUFSIZE
- 1 - len
)) {
917 vt
->ibuf
[vt
->ibuf_count
++] = 27;
918 for (i
= 0; i
< len
; i
++) {
919 vt
->ibuf
[vt
->ibuf_count
++] = esc
[i
];
925 vncterm_put_lf (vncTerm
*vt
)
927 if (vt
->cy
+ 1 == vt
->region_bottom
) {
929 if (vt
->altbuf
|| vt
->region_top
!= 0 || vt
->region_bottom
!= vt
->height
) {
930 vncterm_scroll_up (vt
, vt
->region_top
, vt
->region_bottom
, 1, 1);
934 if (vt
->y_displ
== vt
->y_base
) {
935 vncterm_scroll_up (vt
, vt
->region_top
, vt
->region_bottom
, 1, 0);
938 if (vt
->y_displ
== vt
->y_base
) {
939 if (++vt
->y_displ
== vt
->total_height
) {
944 if (++vt
->y_base
== vt
->total_height
) {
948 if (vt
->scroll_height
< vt
->total_height
) {
952 int y1
= (vt
->y_base
+ vt
->height
- 1) % vt
->total_height
;
953 TextCell
*c
= &vt
->cells
[y1
* vt
->width
];
955 for (x
= 0; x
< vt
->width
; x
++) {
957 c
->attrib
= vt
->default_attrib
;
961 // fprintf (stderr, "BASE: %d DISPLAY %d\n", vt->y_base, vt->y_displ);
963 } else if (vt
->cy
< vt
->height
- 1) {
970 vncterm_csi_m (vncTerm
*vt
)
974 for (i
= 0; i
< vt
->esc_count
; i
++) {
975 switch (vt
->esc_buf
[i
]) {
976 case 0: /* reset all console attributes to default */
977 vt
->cur_attrib
= vt
->default_attrib
;
980 vt
->cur_attrib
.bold
= 1;
983 vt
->cur_attrib
.uline
= 1;
986 vt
->cur_attrib
.blink
= 1;
989 vt
->cur_attrib
.invers
= 1;
992 vt
->cur_attrib
.unvisible
= 1;
995 vt
->cur_enc
= LAT1_MAP
;
996 // fixme: dispaly controls = 0 ?
997 // fixme: toggle meta = 0 ?
1000 vt
->cur_enc
= IBMPC_MAP
;
1001 // fixme: dispaly controls = 1 ?
1002 // fixme: toggle meta = 0 ?
1005 vt
->cur_enc
= IBMPC_MAP
;
1006 // fixme: dispaly controls = 1 ?
1007 // fixme: toggle meta = 1 ?
1010 vt
->cur_attrib
.bold
= 0;
1013 vt
->cur_attrib
.uline
= 0;
1016 vt
->cur_attrib
.blink
= 0;
1019 vt
->cur_attrib
.invers
= 0;
1022 vt
->cur_attrib
.unvisible
= 0;
1032 /* set foreground color */
1033 vt
->cur_attrib
.fgcol
= color_table
[vt
->esc_buf
[i
] - 30];
1036 /* reset color to default, enable underline */
1037 vt
->cur_attrib
.fgcol
= vt
->default_attrib
.fgcol
;
1038 vt
->cur_attrib
.uline
= 1;
1041 /* reset color to default, disable underline */
1042 vt
->cur_attrib
.fgcol
= vt
->default_attrib
.fgcol
;
1043 vt
->cur_attrib
.uline
= 0;
1053 /* set background color */
1054 vt
->cur_attrib
.bgcol
= color_table
[vt
->esc_buf
[i
] - 40];
1057 /* reset background color */
1058 vt
->cur_attrib
.bgcol
= vt
->default_attrib
.bgcol
;
1061 fprintf (stderr
, "unhandled ESC[%d m code\n",vt
->esc_buf
[i
]);
1068 vncterm_save_cursor (vncTerm
*vt
)
1070 vt
->cx_saved
= vt
->cx
;
1071 vt
->cy_saved
= vt
->cy
;
1072 vt
->cur_attrib_saved
= vt
->cur_attrib
;
1073 vt
->charset_saved
= vt
->charset
;
1074 vt
->g0enc_saved
= vt
->g0enc
;
1075 vt
->g1enc_saved
= vt
->g1enc
;
1076 vt
->cur_enc_saved
= vt
->cur_enc
;
1080 vncterm_restore_cursor (vncTerm
*vt
)
1082 vt
->cx
= vt
->cx_saved
;
1083 vt
->cy
= vt
->cy_saved
;
1084 vt
->cur_attrib
= vt
->cur_attrib_saved
;
1085 vt
->charset
= vt
->charset_saved
;
1086 vt
->g0enc
= vt
->g0enc_saved
;
1087 vt
->g1enc
= vt
->g1enc_saved
;
1088 vt
->cur_enc
= vt
->cur_enc_saved
;
1092 vncterm_set_alternate_buffer (vncTerm
*vt
, int on_off
)
1096 vt
->y_displ
= vt
->y_base
;
1100 if (vt
->altbuf
) return;
1104 /* alternate buffer & cursor */
1106 vncterm_save_cursor (vt
);
1107 /* save screen to altcels */
1108 for (y
= 0; y
< vt
->height
; y
++) {
1109 int y1
= (vt
->y_base
+ y
) % vt
->total_height
;
1110 for (x
= 0; x
< vt
->width
; x
++) {
1111 vt
->altcells
[y
*vt
->width
+ x
] = vt
->cells
[y1
*vt
->width
+ x
];
1116 for (y
= 0; y
<= vt
->height
; y
++) {
1117 for (x
= 0; x
< vt
->width
; x
++) {
1118 vncterm_clear_xy (vt
, x
, y
);
1124 if (vt
->altbuf
== 0) return;
1128 /* restore saved data */
1129 for (y
= 0; y
< vt
->height
; y
++) {
1130 int y1
= (vt
->y_base
+ y
) % vt
->total_height
;
1131 for (x
= 0; x
< vt
->width
; x
++) {
1132 vt
->cells
[y1
*vt
->width
+ x
] = vt
->altcells
[y
*vt
->width
+ x
];
1136 vncterm_restore_cursor (vt
);
1139 vncterm_refresh (vt
);
1143 vncterm_set_mode (vncTerm
*vt
, int on_off
)
1147 for (i
= 0; i
<= vt
->esc_count
; i
++) {
1148 if (vt
->esc_ques
) { /* DEC private modes set/reset */
1149 switch(vt
->esc_buf
[i
]) {
1150 case 10: /* X11 mouse reporting on/off */
1152 vt
->report_mouse
= on_off
;
1154 case 1049: /* start/end special app mode (smcup/rmcup) */
1155 vncterm_set_alternate_buffer (vt
, on_off
);
1157 case 25: /* Cursor on/off */
1158 case 9: /* X10 mouse reporting on/off */
1159 case 6: /* Origin relative/absolute */
1160 case 1: /* Cursor keys in appl mode*/
1161 case 5: /* Inverted screen on/off */
1162 case 7: /* Autowrap on/off */
1163 case 8: /* Autorepeat on/off */
1166 } else { /* ANSI modes set/reset */
1167 /* fixme: implement me */
1173 vncterm_gotoxy (vncTerm
*vt
, int x
, int y
)
1175 /* verify all boundaries */
1181 if (x
>= vt
->width
) {
1191 if (y
>= vt
->height
) {
1198 enum { ESnormal
, ESesc
, ESsquare
, ESgetpars
, ESgotpars
, ESfunckey
,
1199 EShash
, ESsetG0
, ESsetG1
, ESpercent
, ESignore
, ESnonstd
,
1200 ESpalette
, ESidquery
, ESosc1
, ESosc2
};
1203 vncterm_putchar (vncTerm
*vt
, unicode ch
)
1209 fprintf (stderr
, "CHAR:%2d: %4x '%c' (cur_enc %d) %d %d\n", vt
->tty_state
, ch
, ch
, vt
->cur_enc
, vt
->cx
, vt
->cy
);
1212 switch(vt
->tty_state
) {
1214 vt
->tty_state
= ESnormal
;
1217 vt
->tty_state
= ESsquare
;
1220 vt
->tty_state
= ESnonstd
;
1223 vt
->tty_state
= ESpercent
;
1226 vncterm_save_cursor (vt
);
1229 vncterm_restore_cursor (vt
);
1232 vt
->tty_state
= ESsetG0
; // SET G0
1235 vt
->tty_state
= ESsetG1
; // SET G1
1238 /* cursor up (ri) */
1239 if (vt
->cy
== vt
->region_top
)
1240 vncterm_scroll_down (vt
, vt
->region_top
, vt
->region_bottom
, 1);
1241 else if (vt
->cy
> 0) {
1246 /* numeric keypad - ignored */
1249 /* appl. keypad - ignored */
1253 fprintf(stderr
, "got unhandled ESC%c %d\n", ch
, ch
);
1258 case ESnonstd
: /* Operating System Controls */
1259 vt
->tty_state
= ESnormal
;
1262 case 'P': /* palette escape sequence */
1263 for(i
= 0; i
< MAX_ESC_PARAMS
; i
++) {
1268 vt
->tty_state
= ESpalette
;
1270 case 'R': /* reset palette */
1271 // fixme: reset_palette(vc);
1278 vt
->osc_textbuf
[0] = 0;
1279 vt
->tty_state
= ESosc1
;
1283 fprintf (stderr
, "unhandled OSC %c\n", ch
);
1285 vt
->tty_state
= ESnormal
;
1290 vt
->tty_state
= ESnormal
;
1292 vt
->tty_state
= ESosc2
;
1295 fprintf (stderr
, "got illegal OSC sequence\n");
1300 if (ch
!= 0x9c && ch
!= 7) {
1302 while (vt
->osc_textbuf
[i
]) i
++;
1303 vt
->osc_textbuf
[i
++] = ch
;
1304 vt
->osc_textbuf
[i
] = 0;
1307 fprintf (stderr
, "OSC:%c:%s\n", vt
->osc_cmd
, vt
->osc_textbuf
);
1309 vt
->tty_state
= ESnormal
;
1313 if ((ch
>= '0' && ch
<= '9') || (ch
>= 'A' && ch
<= 'F')
1314 || (ch
>= 'a' && ch
<= 'f')) {
1315 vt
->esc_buf
[vt
->esc_count
++] = (ch
> '9' ? (ch
& 0xDF) - 'A' + 10 : ch
- '0');
1316 if (vt
->esc_count
== 7) {
1317 // fixme: this does not work - please test
1318 rfbColourMap
*cmap
=&vt
->screen
->colourMap
;
1320 int i
= color_table
[vt
->esc_buf
[0]] * 3, j
= 1;
1321 cmap
->data
.bytes
[i
] = 16 * vt
->esc_buf
[j
++];
1322 cmap
->data
.bytes
[i
++] += vt
->esc_buf
[j
++];
1323 cmap
->data
.bytes
[i
] = 16 * vt
->esc_buf
[j
++];
1324 cmap
->data
.bytes
[i
++] += vt
->esc_buf
[j
++];
1325 cmap
->data
.bytes
[i
] = 16 * vt
->esc_buf
[j
++];
1326 cmap
->data
.bytes
[i
] += vt
->esc_buf
[j
];
1328 //set_palette(vc); ?
1330 vt
->tty_state
= ESnormal
;
1333 vt
->tty_state
= ESnormal
;
1336 for(i
= 0; i
< MAX_ESC_PARAMS
; i
++) {
1341 vt
->esc_has_par
= 0;
1342 vt
->tty_state
= ESgetpars
;
1345 vt
->tty_state
= ESidquery
;
1349 if ((vt
->esc_ques
= (ch
== '?'))) {
1353 if (ch
>= '0' && ch
<= '9') {
1354 vt
->esc_has_par
= 1;
1355 if (vt
->esc_count
< MAX_ESC_PARAMS
) {
1356 vt
->esc_buf
[vt
->esc_count
] = vt
->esc_buf
[vt
->esc_count
] * 10 + ch
- '0';
1359 } else if (ch
== ';') {
1360 vt
->esc_has_par
= 1;
1364 if (vt
->esc_has_par
) {
1367 vt
->tty_state
= ESgotpars
;
1371 vt
->tty_state
= ESnormal
;
1374 char *qes
= vt
->esc_ques
? "?" : "";
1375 if (vt
->esc_count
== 0) {
1376 fprintf(stderr
, "ESC[%s%c\n", qes
, ch
);
1377 } else if (vt
->esc_count
== 1) {
1378 fprintf(stderr
, "ESC[%s%d%c\n", qes
, vt
->esc_buf
[0], ch
);
1381 fprintf(stderr
, "ESC[%s%d", qes
, vt
->esc_buf
[0]);
1382 for (i
= 1; i
< vt
->esc_count
; i
++) {
1383 fprintf(stderr
, ";%d", vt
->esc_buf
[i
]);
1385 fprintf (stderr
, "%c\n", ch
);
1391 vncterm_set_mode (vt
, 1);
1394 vncterm_set_mode (vt
, 0);
1397 if (!vt
->esc_count
) {
1398 vt
->esc_count
++; // default parameter 0
1403 /* report cursor position */
1404 /* TODO: send ESC[row;colR */
1407 /* move cursor up */
1408 if (vt
->esc_buf
[0] == 0) {
1411 vncterm_gotoxy (vt
, vt
->cx
, vt
->cy
- vt
->esc_buf
[0]);
1415 /* move cursor down */
1416 if (vt
->esc_buf
[0] == 0) {
1419 vncterm_gotoxy (vt
, vt
->cx
, vt
->cy
+ vt
->esc_buf
[0]);
1423 /* move cursor right */
1424 if (vt
->esc_buf
[0] == 0) {
1427 vncterm_gotoxy (vt
, vt
->cx
+ vt
->esc_buf
[0], vt
->cy
);
1430 /* move cursor left */
1431 if (vt
->esc_buf
[0] == 0) {
1434 vncterm_gotoxy (vt
, vt
->cx
- vt
->esc_buf
[0], vt
->cy
);
1438 /* move cursor to column */
1439 vncterm_gotoxy (vt
, vt
->esc_buf
[0] - 1, vt
->cy
);
1442 /* move cursor to row */
1443 vncterm_gotoxy (vt
, vt
->cx
, vt
->esc_buf
[0] - 1);
1447 /* move cursor to row, column */
1448 vncterm_gotoxy (vt
, vt
->esc_buf
[1] - 1, vt
->esc_buf
[0] - 1);
1451 switch (vt
->esc_buf
[0]) {
1453 /* clear to end of screen */
1454 for (y
= vt
->cy
; y
< vt
->height
; y
++) {
1455 for (x
= 0; x
< vt
->width
; x
++) {
1456 if (y
== vt
->cy
&& x
< vt
->cx
) {
1459 vncterm_clear_xy (vt
, x
, y
);
1464 /* clear from beginning of screen */
1465 for (y
= 0; y
<= vt
->cy
; y
++) {
1466 for (x
= 0; x
< vt
->width
; x
++) {
1467 if (y
== vt
->cy
&& x
> vt
->cx
) {
1470 vncterm_clear_xy (vt
, x
, y
);
1475 /* clear entire screen */
1476 for (y
= 0; y
<= vt
->height
; y
++) {
1477 for (x
= 0; x
< vt
->width
; x
++) {
1478 vncterm_clear_xy (vt
, x
, y
);
1485 switch (vt
->esc_buf
[0]) {
1488 for(x
= vt
->cx
; x
< vt
->width
; x
++) {
1489 vncterm_clear_xy (vt
, x
, vt
->cy
);
1493 /* clear from beginning of line */
1494 for (x
= 0; x
<= vt
->cx
; x
++) {
1495 vncterm_clear_xy (vt
, x
, vt
->cy
);
1499 /* clear entire line */
1500 for(x
= 0; x
< vt
->width
; x
++) {
1501 vncterm_clear_xy (vt
, x
, vt
->cy
);
1510 if (c
> vt
->height
- vt
->cy
)
1511 c
= vt
->height
- vt
->cy
;
1515 vncterm_scroll_down (vt
, vt
->cy
, vt
->region_bottom
, c
);
1521 if (c
> vt
->height
- vt
->cy
)
1522 c
= vt
->height
- vt
->cy
;
1526 vncterm_scroll_up (vt
, vt
->cy
, vt
->region_bottom
, c
, 1);
1532 vncterm_scroll_down (vt
, vt
->region_top
, vt
->region_bottom
, c
);
1538 vncterm_scroll_up (vt
, vt
->region_top
, vt
->region_bottom
, c
, 1);
1541 /* delete c character */
1544 if (c
> vt
->width
- vt
->cx
)
1545 c
= vt
->width
- vt
->cx
;
1549 for (x
= vt
->cx
; x
< vt
->width
- c
; x
++) {
1550 int y1
= (vt
->y_base
+ vt
->cy
) % vt
->total_height
;
1551 TextCell
*dst
= &vt
->cells
[y1
* vt
->width
+ x
];
1552 TextCell
*src
= dst
+ c
;
1554 vncterm_update_xy (vt
, x
+ c
, vt
->cy
);
1556 src
->attrib
= vt
->default_attrib
;
1557 vncterm_update_xy (vt
, x
, vt
->cy
);
1561 /* save cursor position */
1562 vncterm_save_cursor (vt
);
1565 /* restore cursor position */
1566 vncterm_restore_cursor (vt
);
1569 /* erase c characters */
1573 if (c
> (vt
->width
- vt
->cx
)) c
= vt
->width
- vt
->cx
;
1575 for(i
= 0; i
< c
; i
++) {
1576 vncterm_clear_xy (vt
, vt
->cx
+ i
, vt
->cy
);
1580 /* insert c character */
1582 if (c
> (vt
->width
- vt
->cx
)) {
1583 c
= vt
->width
- vt
->cx
;
1587 for (x
= vt
->width
- c
; x
>= vt
->cx
; x
--) {
1588 int y1
= (vt
->y_base
+ vt
->cy
) % vt
->total_height
;
1589 TextCell
*src
= &vt
->cells
[y1
* vt
->width
+ x
];
1590 TextCell
*dst
= src
+ c
;
1592 vncterm_update_xy (vt
, x
+ c
, vt
->cy
);
1594 src
->attrib
= vt
->cur_attrib
;
1595 vncterm_update_xy (vt
, x
, vt
->cy
);
1601 if (!vt
->esc_buf
[0])
1603 if (!vt
->esc_buf
[1])
1604 vt
->esc_buf
[1] = vt
->height
;
1605 /* Minimum allowed region is 2 lines */
1606 if (vt
->esc_buf
[0] < vt
->esc_buf
[1] &&
1607 vt
->esc_buf
[1] <= vt
->height
) {
1608 vt
->region_top
= vt
->esc_buf
[0] - 1;
1609 vt
->region_bottom
= vt
->esc_buf
[1];
1611 vt
->cy
= vt
->region_top
;
1613 fprintf (stderr
, "set region %d %d\n", vt
->region_top
, vt
->region_bottom
);
1620 if (vt
->esc_count
== 0) {
1621 fprintf(stderr
, "unhandled escape ESC[%s%c\n", qes
, ch
);
1622 } else if (vt
->esc_count
== 1) {
1623 fprintf(stderr
, "unhandled escape ESC[%s%d%c\n", qes
, vt
->esc_buf
[0], ch
);
1626 fprintf(stderr
, "unhandled escape ESC[%s%d", qes
, vt
->esc_buf
[0]);
1627 for (i
= 1; i
< vt
->esc_count
; i
++) {
1628 fprintf(stderr
, ";%d", vt
->esc_buf
[i
]);
1630 fprintf (stderr
, "%c\n", ch
);
1637 case ESsetG0
: // Set G0
1638 vt
->tty_state
= ESnormal
;
1641 vt
->g0enc
= GRAF_MAP
;
1643 vt
->g0enc
= LAT1_MAP
;
1645 vt
->g0enc
= IBMPC_MAP
;
1647 vt
->g0enc
= USER_MAP
;
1649 if (vt
->charset
== 0)
1650 vt
->cur_enc
= vt
->g0enc
;
1653 case ESsetG1
: // Set G1
1654 vt
->tty_state
= ESnormal
;
1657 vt
->g1enc
= GRAF_MAP
;
1659 vt
->g1enc
= LAT1_MAP
;
1661 vt
->g1enc
= IBMPC_MAP
;
1663 vt
->g1enc
= USER_MAP
;
1665 if (vt
->charset
== 1)
1666 vt
->cur_enc
= vt
->g1enc
;
1669 case ESidquery
: // vt100 query id
1670 vt
->tty_state
= ESnormal
;
1674 fprintf (stderr
, "ESC[>c Query term ID\n");
1676 vncterm_respond_esc (vt
, TERMIDCODE
);
1680 vt
->tty_state
= ESnormal
;
1682 case '@': /* defined in ISO 2022 */
1685 case 'G': /* prelim official escape code */
1686 case '8': /* retained for compatibility */
1691 default: // ESnormal
1692 vt
->tty_state
= ESnormal
;
1697 case 7: /* alert aka. bell */
1698 rfbSendBell(vt
->screen
);
1700 case 8: /* backspace */
1704 case 9: /* tabspace */
1705 if (vt
->cx
+ (8 - (vt
->cx
% 8)) > vt
->width
) {
1707 vncterm_put_lf (vt
);
1709 vt
->cx
= vt
->cx
+ (8 - (vt
->cx
% 8));
1715 vncterm_put_lf (vt
);
1717 case 13: /* carriage return */
1721 /* SI (shift in), select character set 1 */
1723 vt
->cur_enc
= vt
->g1enc
;
1724 /* fixme: display controls = 1 */
1727 /* SO (shift out), select character set 0 */
1729 vt
->cur_enc
= vt
->g0enc
;
1730 /* fixme: display controls = 0 */
1733 vt
->tty_state
= ESesc
;
1735 case 127: /* delete */
1738 case 128+27: /* csi */
1739 vt
->tty_state
= ESsquare
;
1742 if (vt
->cx
>= vt
->width
) {
1745 vncterm_put_lf (vt
);
1748 int y1
= (vt
->y_base
+ vt
->cy
) % vt
->total_height
;
1749 TextCell
*c
= &vt
->cells
[y1
*vt
->width
+ vt
->cx
];
1750 c
->attrib
= vt
->cur_attrib
;
1752 vncterm_update_xy (vt
, vt
->cx
, vt
->cy
);
1761 vncterm_puts (vncTerm
*vt
, const char *buf
, int len
)
1765 vncterm_show_cursor (vt
, 0);
1768 unsigned char c
= *buf
;
1772 if (vt
->tty_state
!= ESnormal
) {
1773 // never translate escape sequence
1775 } else if (vt
->utf8
&& !vt
->cur_enc
) {
1777 if(c
& 0x80) { // utf8 multi-byte sequence
1779 if (vt
->utf_count
> 0 && (c
& 0xc0) == 0x80) {
1780 // inside UTF8 sequence
1781 vt
->utf_char
= (vt
->utf_char
<< 6) | (c
& 0x3f);
1783 if (vt
->utf_count
== 0) {
1784 if (vt
->utf_char
<= USHRT_MAX
) {
1793 // first char of a UTF8 sequence
1794 if ((c
& 0xe0) == 0xc0) {
1796 vt
->utf_char
= (c
& 0x1f);
1797 } else if ((c
& 0xf0) == 0xe0) {
1799 vt
->utf_char
= (c
& 0x0f);
1800 } else if ((c
& 0xf8) == 0xf0) {
1802 vt
->utf_char
= (c
& 0x07);
1803 } else if ((c
& 0xfc) == 0xf8) {
1805 vt
->utf_char
= (c
& 0x03);
1806 } else if ((c
& 0xfe) == 0xfc) {
1808 vt
->utf_char
= (c
& 0x01);
1821 // never translate controls
1822 if (c
>= 32 && c
!= 127 && c
!= (128+27)) {
1823 tc
= translations
[vt
->cur_enc
][c
& 0x0ff];
1829 vncterm_putchar (vt
, tc
);
1832 vncterm_show_cursor (vt
, 1);
1837 vncterm_kbd_event (rfbBool down
, rfbKeySym keySym
, rfbClientPtr cl
)
1839 vncTerm
*vt
=(vncTerm
*)cl
->screen
->screenData
;
1840 static int control
= 0;
1841 static int shift
= 0;
1844 //fprintf (stderr, "KEYEVENT:%d: %08x\n", down == 0, keySym);fflush (stderr);
1846 //fprintf (stderr, "KEYPRESS: %d\n", keySym);fflush (stderr);
1848 if (keySym
== XK_Shift_L
|| keySym
== XK_Shift_R
) {
1850 } if (keySym
== XK_Control_L
|| keySym
== XK_Control_R
) {
1852 } else if (vt
->ibuf_count
< (IBUFSIZE
- 32)) {
1855 if(keySym
>= 'a' && keySym
<= 'z')
1857 else if (keySym
>= 'A' && keySym
<= 'Z')
1871 case XK_Delete
: /* kdch1 */
1874 case XK_Home
: /* khome */
1878 case XK_KP_End
: /* kend */
1880 case XK_Insert
: /* kich1 */
1884 case XK_KP_Up
: /* kcuu1 */
1886 case XK_Down
: /* kcud1 */
1890 case XK_KP_Right
: /* kcuf1 */
1893 case XK_KP_Left
: /* kcub1 */
1897 vncterm_virtual_scroll (vt
, -vt
->height
/2);
1903 vncterm_virtual_scroll (vt
, vt
->height
/2);
1937 fprintf (stderr
, "KEYPRESS OUT:%s: %d\n", esc
, keySym
); fflush (stderr
);
1940 if (vt
->y_displ
!= vt
->y_base
) {
1941 vt
->y_displ
= vt
->y_base
;
1942 vncterm_refresh (vt
);
1946 vncterm_respond_esc (vt
, esc
);
1947 } else if(keySym
<0x100) {
1949 int len
= ucs2_to_utf8 (keySym
& 0x0fff, &vt
->ibuf
[vt
->ibuf_count
]);
1950 vt
->ibuf_count
+= len
;
1952 vt
->ibuf
[vt
->ibuf_count
++] = (char)keySym
;
1957 if (keySym
== XK_Shift_L
|| keySym
== XK_Shift_R
) {
1959 } else if (keySym
== XK_Control_L
|| keySym
== XK_Control_R
) {
1966 vncterm_set_xcut_text (char* str
, int len
, struct _rfbClientRec
* cl
)
1968 vncTerm
*vt
=(vncTerm
*)cl
->screen
->screenData
;
1970 // seems str is Latin-1 encoded
1971 if (vt
->selection
) free (vt
->selection
);
1972 vt
->selection
= (unicode
*)malloc (len
*sizeof (unicode
));
1974 for (i
= 0; i
< len
; i
++) {
1975 vt
->selection
[i
] = str
[i
] & 0xff;
1977 vt
->selection_len
= len
;
1981 mouse_report (vncTerm
*vt
, int butt
, int mrx
, int mry
)
1985 sprintf (buf
, "[M%c%c%c", (char)(' ' + butt
), (char)('!' + mrx
),
1988 vncterm_respond_esc (vt
, buf
);
1992 vncterm_toggle_marked_cell (vncTerm
*vt
, int pos
)
1994 int x
= (pos
%vt
->width
)*8;
1995 int y
= (pos
/vt
->width
)*16;
1998 rfbScreenInfoPtr s
=vt
->screen
;
2000 char *b
= s
->frameBuffer
+y
*s
->width
+x
;
2002 for (j
=0; j
< 16; j
++) {
2003 for(i
=0; i
< 8; i
++) {
2004 b
[j
*s
->width
+i
] ^= 0x0f;
2005 rfbMarkRectAsModified (s
, x
, y
, x
+8, y
+16);
2011 vncterm_pointer_event (int buttonMask
, int x
, int y
, rfbClientPtr cl
)
2013 vncTerm
*vt
=(vncTerm
*)cl
->screen
->screenData
;
2014 static int button2_released
= 1;
2015 static int last_mask
= 0;
2016 static int sel_start_pos
= 0;
2017 static int sel_end_pos
= 0;
2024 if (cx
>= vt
->width
) cx
= vt
->width
- 1;
2026 if (cy
>= vt
->height
) cy
= vt
->height
- 1;
2028 if (vt
->report_mouse
&& buttonMask
!= last_mask
) {
2029 last_mask
= buttonMask
;
2030 if (buttonMask
& 1) {
2031 mouse_report (vt
, 0, cx
, cy
);
2033 if (buttonMask
& 2) {
2034 mouse_report (vt
, 1, cx
, cy
);
2036 if (buttonMask
& 4) {
2037 mouse_report (vt
, 2, cx
, cy
);
2040 mouse_report (vt
, 3, cx
, cy
);
2044 if (buttonMask
& 2) {
2045 if(button2_released
&& vt
->selection
) {
2047 for(i
= 0; i
< vt
->selection_len
; i
++) {
2048 if (vt
->ibuf_count
< IBUFSIZE
- 6) { // uft8 is max 6 characters wide
2050 vt
->ibuf_count
+= ucs2_to_utf8 (vt
->selection
[i
], &vt
->ibuf
[vt
->ibuf_count
]);
2052 vt
->ibuf
[vt
->ibuf_count
++] = vt
->selection
[i
];
2056 if (vt
->y_displ
!= vt
->y_base
) {
2057 vt
->y_displ
= vt
->y_base
;
2058 vncterm_refresh (vt
);
2061 button2_released
= 0;
2063 button2_released
= 1;
2066 if (buttonMask
& 1) {
2067 int pos
= cy
*vt
->width
+ cx
;
2069 // code borrowed from libvncserver (VNConsole.c)
2071 if (!vt
->mark_active
) {
2073 vt
->mark_active
= 1;
2074 sel_start_pos
= sel_end_pos
= pos
;
2075 vncterm_toggle_marked_cell (vt
, pos
);
2079 if (pos
!= sel_end_pos
) {
2081 if (pos
> sel_end_pos
) {
2082 cx
= sel_end_pos
; cy
=pos
;
2084 cx
=pos
; cy
=sel_end_pos
;
2087 if (cx
< sel_start_pos
) {
2088 if (cy
< sel_start_pos
) cy
--;
2094 vncterm_toggle_marked_cell (vt
, cx
);
2102 } else if (vt
->mark_active
) {
2103 vt
->mark_active
= 0;
2105 if (sel_start_pos
> sel_end_pos
) {
2106 int tmp
= sel_start_pos
- 1;
2107 sel_start_pos
= sel_end_pos
;
2111 int len
= sel_end_pos
- sel_start_pos
+ 1;
2113 if (vt
->selection
) free (vt
->selection
);
2114 vt
->selection
= (unicode
*)malloc (len
*sizeof (unicode
));
2115 vt
->selection_len
= len
;
2116 char *sel_latin1
= (char *)malloc (len
+ 1);
2118 for (i
= 0; i
< len
; i
++) {
2119 int pos
= sel_start_pos
+ i
;
2120 int x
= pos
% vt
->width
;
2121 int y1
= ((pos
/ vt
->width
) + vt
->y_displ
) % vt
->total_height
;
2122 TextCell
*c
= &vt
->cells
[y1
*vt
->width
+ x
];
2123 vt
->selection
[i
] = c
->ch
;
2124 sel_latin1
[i
] = (char)c
->ch
;
2127 sel_latin1
[len
] = 0;
2128 rfbGotXCutText (vt
->screen
, sel_latin1
, len
);
2131 while (sel_start_pos
<= sel_end_pos
) {
2132 vncterm_toggle_marked_cell (vt
, sel_start_pos
++);
2137 rfbDefaultPtrAddEvent (buttonMask
, x
, y
, cl
);
2140 static int client_count
= 0;
2141 static int client_connected
= 0;
2142 static int last_client
= 1;
2143 static time_t last_time
= 0;
2146 client_gone (rfbClientPtr client
)
2150 last_time
= time (NULL
);
2152 if (client_count
<= 0) {
2157 /* libvncserver callback for when a new client connects */
2158 enum rfbNewClientAction
2159 new_client (rfbClientPtr client
)
2161 client
->clientGoneHook
= client_gone
;
2164 last_time
= time (NULL
);
2167 client_connected
= 1;
2169 return RFB_CLIENT_ACCEPT
;
2172 static char *vncticket
= NULL
;
2175 create_vncterm (int argc
, char** argv
, int maxx
, int maxy
)
2179 rfbScreenInfoPtr screen
= rfbGetScreen (&argc
, argv
, maxx
, maxy
, 8, 1, 1);
2180 screen
->frameBuffer
=(char*)calloc(maxx
*maxy
, 1);
2182 char **passwds
= calloc(sizeof(char**), 2);
2184 vncTerm
*vt
= (vncTerm
*)calloc (sizeof(vncTerm
), 1);
2186 rfbColourMap
*cmap
=&screen
->colourMap
;
2187 cmap
->data
.bytes
= malloc (16*3);
2189 cmap
->data
.bytes
[i
*3 + 0] = default_red
[color_table
[i
]];
2190 cmap
->data
.bytes
[i
*3 + 1] = default_grn
[color_table
[i
]];
2191 cmap
->data
.bytes
[i
*3 + 2] = default_blu
[color_table
[i
]];
2195 screen
->serverFormat
.trueColour
= FALSE
;
2197 screen
->kbdAddEvent
= vncterm_kbd_event
;
2199 screen
->setXCutText
= vncterm_set_xcut_text
;
2201 screen
->ptrAddEvent
= vncterm_pointer_event
;
2203 screen
->desktopName
= "VNC Command Terminal";
2205 screen
->newClientHook
= new_client
;
2207 vt
->maxx
= screen
->width
;
2208 vt
->maxy
= screen
->height
;
2210 vt
->width
= vt
->maxx
/ 8;
2211 vt
->height
= vt
->maxy
/ 16;
2213 vt
->total_height
= vt
->height
* 20;
2214 vt
->scroll_height
= 0;
2219 vt
->region_bottom
= vt
->height
;
2221 vt
->g0enc
= LAT1_MAP
;
2222 vt
->g1enc
= GRAF_MAP
;
2223 vt
->cur_enc
= vt
->g0enc
;
2226 /* default text attributes */
2227 vt
->default_attrib
.bold
= 0;
2228 vt
->default_attrib
.uline
= 0;
2229 vt
->default_attrib
.blink
= 0;
2230 vt
->default_attrib
.invers
= 0;
2231 vt
->default_attrib
.unvisible
= 0;
2232 vt
->default_attrib
.fgcol
= 7;
2233 vt
->default_attrib
.bgcol
= 0;
2235 vt
->cur_attrib
= vt
->default_attrib
;
2237 vt
->cells
= (TextCell
*)calloc (sizeof (TextCell
), vt
->width
*vt
->total_height
);
2239 for (i
= 0; i
< vt
->width
*vt
->total_height
; i
++) {
2240 vt
->cells
[i
].ch
= ' ';
2241 vt
->cells
[i
].attrib
= vt
->default_attrib
;
2244 vt
->altcells
= (TextCell
*)calloc (sizeof (TextCell
), vt
->width
*vt
->height
);
2246 vt
->screen
= screen
;
2248 screen
->screenData
= (void*)vt
;
2250 //screen->autoPort = 1;
2253 passwds
[0] = vncticket
;
2256 screen
->authPasswdData
= (void *)passwds
;
2257 screen
->passwordCheck
= rfbCheckPasswordByList
;
2259 rfbRegisterSecurityHandler(&VncSecurityHandlerVencrypt
);
2262 rfbInitServer(screen
);
2268 main (int argc
, char** argv
)
2271 char **cmdargv
= NULL
;
2272 char *command
= "/bin/bash"; // execute normal shell as default
2277 struct timeval tv
, tv1
;
2278 time_t elapsed
, cur_time
;
2279 struct winsize dimensions
;
2280 unsigned long width
= 0;
2281 unsigned long height
= 0;
2283 if (gnutls_global_init () < 0) {
2284 fprintf(stderr
, "gnutls_global_init failed\n");
2288 if (gnutls_dh_params_init (&dh_params
) < 0) {
2289 fprintf(stderr
, "gnutls_dh_params_init failed\n");
2293 if (gnutls_dh_params_generate2 (dh_params
, DH_BITS
) < 0) {
2294 fprintf(stderr
, "gnutls_dh_params_init failed\n");
2298 for (i
= 1; i
< argc
; i
++) {
2299 if (!strcmp (argv
[i
], "-c")) {
2300 command
= argv
[i
+1];
2301 cmdargv
= &argv
[i
+1];
2308 for (i
= 1; i
< argc
; i
++) {
2309 if (!strcmp (argv
[i
], "-timeout")) {
2310 CHECK_ARGC (argc
, argv
, i
);
2311 idle_timeout
= atoi(argv
[i
+1]);
2312 rfbPurgeArguments(&argc
, &i
, 2, argv
); i
--;
2313 } else if (!strcmp (argv
[i
], "-authpath")) {
2314 CHECK_ARGC (argc
, argv
, i
);
2315 auth_path
= argv
[i
+1];
2316 rfbPurgeArguments(&argc
, &i
, 2, argv
); i
--;
2317 } else if (!strcmp (argv
[i
], "-perm")) {
2318 CHECK_ARGC (argc
, argv
, i
);
2319 auth_perm
= argv
[i
+1];
2320 rfbPurgeArguments(&argc
, &i
, 2, argv
); i
--;
2321 } else if (!strcmp (argv
[i
], "-width")) {
2322 CHECK_ARGC (argc
, argv
, i
);
2324 width
= strtoul(argv
[i
+1], NULL
, 10);
2325 if (errno
== 0 && width
>= 16 && width
< 0xFFFF) {
2326 screen_width
= width
;
2328 rfbPurgeArguments(&argc
, &i
, 2, argv
); i
--;
2329 } else if (!strcmp (argv
[i
], "-height")) {
2330 CHECK_ARGC (argc
, argv
, i
);
2332 height
= strtoul(argv
[i
+1], NULL
, 10);
2333 if (errno
== 0 && height
>= 32 && height
< 0xFFFF) {
2334 screen_height
= height
;
2336 rfbPurgeArguments(&argc
, &i
, 2, argv
); i
--;
2337 } else if (!strcmp (argv
[i
], "-notls")) {
2338 rfbPurgeArguments(&argc
, &i
, 1, argv
); i
--;
2339 if ((vncticket
= getenv("PVE_VNC_TICKET")) == NULL
) {
2340 fprintf(stderr
, "missing env PVE_VNC_TICKET (-notls)\n");
2346 unsetenv("PVE_VNC_TICKET"); // do not expose this to child
2350 gnutls_global_set_log_level(10);
2351 gnutls_global_set_log_function(vnc_debug_gnutls_log
);
2356 vncTerm
*vt
= create_vncterm (argc
, argv
, screen_width
, screen_height
);
2358 setlocale(LC_ALL
, ""); // set from environment
2360 char *ctype
= setlocale (LC_CTYPE
, NULL
); // query LC_CTYPE
2362 // fixme: ist there a standard way to detect utf8 mode ?
2363 if (strcasestr (ctype
, ".utf-8")||strcasestr (ctype
, ".utf8")) {
2367 dimensions
.ws_col
= vt
->width
;
2368 dimensions
.ws_row
= vt
->height
;
2370 setenv ("TERM", TERM
, 1);
2372 pid
= forkpty (&master
, ptyname
, NULL
, &dimensions
);
2375 // install default signal handlers
2376 signal (SIGQUIT
, SIG_DFL
);
2377 signal (SIGTERM
, SIG_DFL
);
2378 signal (SIGINT
, SIG_DFL
);
2381 execvp (command
, cmdargv
);
2383 execlp (command
, command
, NULL
);
2385 perror ("Error: exec failed\n");
2386 exit (-1); // should not be reached
2387 } else if (pid
== -1) {
2388 perror ("Error: fork failed\n");
2393 FD_SET (master
, &fs
);
2395 tv
.tv_usec
= 5000; /* 5 ms */
2397 last_time
= time (NULL
);
2405 cur_time
= time (NULL
);
2407 elapsed
= cur_time
- last_time
;
2408 //printf ("Elapsed %ld\n", elapsed);
2411 if (client_connected
) {
2412 if (idle_timeout
&& (elapsed
>= idle_timeout
)) {
2416 // wait at least 20 seconds for initial connect
2417 if (idle_timeout
&& (elapsed
>= (idle_timeout
> 20 ? idle_timeout
: 20))) {
2422 // exit after 30 minutes idle time
2423 if (elapsed
>= 30*60) {
2428 rfbProcessEvents (vt
->screen
, 40000); /* 40 ms */
2430 if (vt
->ibuf_count
> 0) {
2431 //printf ("DEBUG: WRITE %d %d\n", vt->ibuf[0], vt->ibuf_count);
2432 write (master
, vt
->ibuf
, vt
->ibuf_count
);
2434 last_time
= time (NULL
);
2437 if (!vt
->mark_active
) {
2439 int num_fds
= select (master
+1, &fs1
, NULL
, NULL
, &tv1
);
2441 if (FD_ISSET (master
, &fs1
)) {
2444 while ((c
= read (master
, buffer
, 1024)) == -1) {
2445 if (errno
!= EAGAIN
) break;
2448 vncterm_puts (vt
, buffer
, c
);
2458 waitpid(pid
, &status
, 0);