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 /* optimize for speed */
498 static const int cipher_priority_performance
[] = {
499 GNUTLS_CIPHER_ARCFOUR_128
,
500 GNUTLS_CIPHER_AES_128_CBC
,
501 GNUTLS_CIPHER_3DES_CBC
, 0
504 if ((ret
= gnutls_cipher_set_priority(sd
->session
, cipher_priority_performance
)) < 0) {
505 rfbLog("gnutls_cipher_set_priority failed: %s\n", gnutls_strerror(ret
));
511 static const int kx_anon
[] = {GNUTLS_KX_ANON_DH
, 0};
512 static const int kx_x509
[] = {GNUTLS_KX_DHE_DSS
, GNUTLS_KX_RSA
, GNUTLS_KX_DHE_RSA
, GNUTLS_KX_SRP
, 0};
513 if ((ret
= gnutls_kx_set_priority(sd
->session
, use_x509
? kx_x509
: kx_anon
)) < 0) {
514 rfbLog("gnutls_kx_set_priority failed: %s\n", gnutls_strerror(ret
));
520 static const int cert_type_priority
[] = { GNUTLS_CRT_X509
, 0 };
521 if ((ret
= gnutls_certificate_type_set_priority(sd
->session
, cert_type_priority
)) < 0) {
522 rfbLog("gnutls_certificate_type_set_priority failed: %s\n",
523 gnutls_strerror(ret
));
529 static const int protocol_priority
[]= { GNUTLS_TLS1_1
, GNUTLS_TLS1_0
, GNUTLS_SSL3
, 0 };
530 if ((ret
= gnutls_protocol_set_priority(sd
->session
, protocol_priority
)) < 0) {
531 rfbLog("gnutls_protocol_set_priority failed: %s\n",
532 gnutls_strerror(ret
));
539 gnutls_certificate_server_credentials x509_cred
;
541 if (!(x509_cred
= tls_initialize_x509_cred())) {
547 if (gnutls_credentials_set(sd
->session
, GNUTLS_CRD_CERTIFICATE
, x509_cred
) < 0) {
549 gnutls_certificate_free_credentials(x509_cred
);
555 gnutls_anon_server_credentials anon_cred
;
557 if (!(anon_cred
= tls_initialize_anon_cred())) {
563 if ((ret
= gnutls_credentials_set(sd
->session
, GNUTLS_CRD_ANON
, anon_cred
)) < 0) {
564 rfbLog("gnutls_credentials_set failed: %s\n", gnutls_strerror(ret
));
565 gnutls_anon_free_server_credentials(anon_cred
);
572 gnutls_transport_set_ptr(sd
->session
, (gnutls_transport_ptr_t
)cl
);
573 gnutls_transport_set_push_function(sd
->session
, vnc_tls_push
);
574 gnutls_transport_set_pull_function(sd
->session
, vnc_tls_pull
);
579 if ((ret
= gnutls_handshake(sd
->session
)) < 0) {
580 if (!gnutls_error_is_fatal(ret
)) {
584 rfbLog("rfbVncAuthVencrypt: handshake failed\n");
589 /* set up TLS read/write hooks */
591 cl
->sock_read_fn
= &vnc_tls_read
;
592 cl
->sock_write_fn
= &vnc_tls_write
;
594 vencrypt_subauth_plain(cl
);
597 static rfbSecurityHandler VncSecurityHandlerVencrypt
= {
605 #define TERMIDCODE "[?1;2c" // vt100 ID
607 #define CHECK_ARGC(argc,argv,i) if (i >= argc-1) { \
608 fprintf (stderr, "ERROR: not enough arguments for: %s\n", argv[i]); \
609 print_usage (NULL); \
613 /* these colours are from linux kernel drivers/char/vt.c */
615 static int idle_timeout
= 1;
617 unsigned char color_table
[] = { 0, 4, 2, 6, 1, 5, 3, 7,
618 8,12,10,14, 9,13,11,15 };
620 /* the default colour table, for VGA+ colour systems */
621 int default_red
[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
622 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
623 int default_grn
[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
624 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
625 int default_blu
[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
626 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
629 print_usage (const char *msg
)
631 if (msg
) { fprintf (stderr
, "ERROR: %s\n", msg
); }
632 fprintf (stderr
, "USAGE: vncterm [vncopts] [-c command [args]]\n");
635 /* Convert UCS2 to UTF8 sequence, trailing zero */
637 ucs2_to_utf8 (unicode c
, char *out
)
640 out
[0] = c
; // 0*******
643 } else if (c
< 0x800) {
644 out
[0] = 0xc0 | (c
>> 6); // 110***** 10******
645 out
[1] = 0x80 | (c
& 0x3f);
649 out
[0] = 0xe0 | (c
>> 12); // 1110**** 10****** 10******
650 out
[1] = 0x80 | ((c
>> 6) & 0x3f);
651 out
[2] = 0x80 | (c
& 0x3f);
660 rfb_draw_char (rfbScreenInfoPtr rfbScreen
, int x
, int y
,
661 unsigned short c
, rfbPixel col
)
663 if (c
> vt_font_size
) {
664 rfbLog ("undefined font glyph %d\n", c
);
669 unsigned char *data
= vt_font_data
+ c
*16;
670 unsigned char d
=*data
;
671 int rowstride
=rfbScreen
->paddedWidthInBytes
;
672 char *colour
=(char*)&col
;
674 for(j
= 0; j
< 16; j
++) {
675 for(i
= 0; i
< 8; i
++) {
681 *(rfbScreen
->frameBuffer
+(y
+j
)*rowstride
+(x
+i
)) = *colour
;
688 draw_char_at (vncTerm
*vt
, int x
, int y
, unicode ch
, TextAttributes attrib
)
690 if (x
< 0 || y
< 0 || x
>= vt
->width
|| y
>= vt
->height
) { return; }
707 int ec
= vt_fontmap
[ch
];
709 rfbFillRect (vt
->screen
, rx
, ry
, rxe
, rye
, bg
);
715 // unsuported attributes = (attrib.blink || attrib.unvisible)
717 rfb_draw_char (vt
->screen
, rx
, ry
, ec
, fg
);
720 rfbDrawLine (vt
->screen
, rx
, ry
+ 14, rxe
, ry
+ 14, fg
);
723 rfbMarkRectAsModified (vt
->screen
, rx
, ry
, rxe
, rye
);
728 vncterm_update_xy (vncTerm
*vt
, int x
, int y
)
730 if (x
< 0 || y
< 0 || x
>= vt
->width
|| y
>= vt
->height
) { return; }
732 int y1
= (vt
->y_base
+ y
) % vt
->total_height
;
733 int y2
= y1
- vt
->y_displ
;
735 y2
+= vt
->total_height
;
737 if (y2
< vt
->height
) {
738 TextCell
*c
= &vt
->cells
[y1
* vt
->width
+ x
];
739 draw_char_at (vt
, x
, y2
, c
->ch
, c
->attrib
);
744 vncterm_clear_xy (vncTerm
*vt
, int x
, int y
)
746 if (x
< 0 || y
< 0 || x
>= vt
->width
|| y
>= vt
->height
) { return; }
748 int y1
= (vt
->y_base
+ y
) % vt
->total_height
;
749 int y2
= y1
- vt
->y_displ
;
751 y2
+= vt
->total_height
;
753 if (y2
< vt
->height
) {
754 TextCell
*c
= &vt
->cells
[y1
* vt
->width
+ x
];
756 c
->attrib
= vt
->default_attrib
;
757 c
->attrib
.fgcol
= vt
->cur_attrib
.fgcol
;
758 c
->attrib
.bgcol
= vt
->cur_attrib
.bgcol
;
760 draw_char_at (vt
, x
, y
, c
->ch
, c
->attrib
);
765 vncterm_show_cursor (vncTerm
*vt
, int show
)
768 if (x
>= vt
->width
) {
772 int y1
= (vt
->y_base
+ vt
->cy
) % vt
->total_height
;
773 int y
= y1
- vt
->y_displ
;
775 y
+= vt
->total_height
;
778 if (y
< vt
->height
) {
780 TextCell
*c
= &vt
->cells
[y1
* vt
->width
+ x
];
783 TextAttributes attrib
= vt
->default_attrib
;
784 attrib
.invers
= !(attrib
.invers
); /* invert fg and bg */
785 draw_char_at (vt
, x
, y
, c
->ch
, attrib
);
787 draw_char_at (vt
, x
, y
, c
->ch
, c
->attrib
);
793 vncterm_refresh (vncTerm
*vt
)
797 rfbFillRect (vt
->screen
, 0, 0, vt
->maxx
, vt
->maxy
, vt
->default_attrib
.bgcol
);
800 for(y
= 0; y
< vt
->height
; y
++) {
801 TextCell
*c
= vt
->cells
+ y1
* vt
->width
;
802 for(x
= 0; x
< vt
->width
; x
++) {
803 draw_char_at (vt
, x
, y
, c
->ch
, c
->attrib
);
806 if (++y1
== vt
->total_height
)
809 rfbMarkRectAsModified (vt
->screen
, 0, 0, vt
->maxx
, vt
->maxy
);
811 vncterm_show_cursor (vt
, 1);
815 vncterm_scroll_down (vncTerm
*vt
, int top
, int bottom
, int lines
)
817 if ((top
+ lines
) >= bottom
) {
818 lines
= bottom
- top
-1;
821 if (top
< 0 || bottom
> vt
->height
|| top
>= bottom
|| lines
< 1) {
829 int rowstride
= vt
->screen
->paddedWidthInBytes
;
830 int rows
= (bottom
- top
- lines
)*16;
832 char *in
= vt
->screen
->frameBuffer
+y0
*rowstride
;
833 char *out
= vt
->screen
->frameBuffer
+y1
*rowstride
;
834 memmove(out
,in
, rowstride
*rows
);
836 memset(vt
->screen
->frameBuffer
+y0
*rowstride
, 0, h
*rowstride
);
837 rfbMarkRectAsModified (vt
->screen
, 0, y0
, vt
->screen
->width
, y2
);
840 for(i
= bottom
- top
- lines
- 1; i
>= 0; i
--) {
841 int src
= ((vt
->y_base
+ top
+ i
) % vt
->total_height
)*vt
->width
;
842 int dst
= ((vt
->y_base
+ top
+ lines
+ i
) % vt
->total_height
)*vt
->width
;
844 memmove(vt
->cells
+ dst
, vt
->cells
+ src
, vt
->width
*sizeof (TextCell
));
847 for (i
= 0; i
< lines
; i
++) {
849 TextCell
*c
= vt
->cells
+ ((vt
->y_base
+ top
+ i
) % vt
->total_height
)*vt
->width
;
850 for(j
= 0; j
< vt
->width
; j
++) {
851 c
->attrib
= vt
->default_attrib
;
859 vncterm_scroll_up (vncTerm
*vt
, int top
, int bottom
, int lines
, int moveattr
)
861 if ((top
+ lines
) >= bottom
) {
862 lines
= bottom
- top
- 1;
865 if (top
< 0 || bottom
> vt
->height
|| top
>= bottom
|| lines
< 1) {
871 int y1
= (top
+ lines
)*16;
873 int rowstride
= vt
->screen
->paddedWidthInBytes
;
874 int rows
= (bottom
- top
- lines
)*16;
876 char *in
= vt
->screen
->frameBuffer
+y1
*rowstride
;
877 char *out
= vt
->screen
->frameBuffer
+y0
*rowstride
;
878 memmove(out
,in
, rowstride
*rows
);
880 memset(vt
->screen
->frameBuffer
+(y2
-h
)*rowstride
, 0, h
*rowstride
);
882 rfbMarkRectAsModified (vt
->screen
, 0, y0
, vt
->screen
->width
, y2
);
884 if (!moveattr
) return;
889 for(i
= 0; i
< (bottom
- top
- lines
); i
++) {
890 int dst
= ((vt
->y_base
+ top
+ i
) % vt
->total_height
)*vt
->width
;
891 int src
= ((vt
->y_base
+ top
+ lines
+ i
) % vt
->total_height
)*vt
->width
;
893 memmove(vt
->cells
+ dst
, vt
->cells
+ src
, vt
->width
*sizeof (TextCell
));
896 for (i
= 1; i
<= lines
; i
++) {
898 TextCell
*c
= vt
->cells
+ ((vt
->y_base
+ bottom
- i
) % vt
->total_height
)*vt
->width
;
899 for(j
= 0; j
< vt
->width
; j
++) {
900 c
->attrib
= vt
->default_attrib
;
908 vncterm_virtual_scroll (vncTerm
*vt
, int lines
)
910 if (vt
->altbuf
|| lines
== 0) return;
914 int i
= vt
->scroll_height
;
915 if (i
> vt
->total_height
- vt
->height
)
916 i
= vt
->total_height
- vt
->height
;
917 int y1
= vt
->y_base
- i
;
919 y1
+= vt
->total_height
;
920 for(i
= 0; i
< lines
; i
++) {
921 if (vt
->y_displ
== y1
) break;
922 if (--vt
->y_displ
< 0) {
923 vt
->y_displ
= vt
->total_height
- 1;
928 for(i
= 0; i
< lines
; i
++) {
929 if (vt
->y_displ
== vt
->y_base
) break;
930 if (++vt
->y_displ
== vt
->total_height
) {
937 vncterm_refresh (vt
);
940 vncterm_respond_esc (vncTerm
*vt
, const char *esc
)
942 int len
= strlen (esc
);
945 if (vt
->ibuf_count
< (IBUFSIZE
- 1 - len
)) {
946 vt
->ibuf
[vt
->ibuf_count
++] = 27;
947 for (i
= 0; i
< len
; i
++) {
948 vt
->ibuf
[vt
->ibuf_count
++] = esc
[i
];
954 vncterm_put_lf (vncTerm
*vt
)
956 if (vt
->cy
+ 1 == vt
->region_bottom
) {
958 if (vt
->altbuf
|| vt
->region_top
!= 0 || vt
->region_bottom
!= vt
->height
) {
959 vncterm_scroll_up (vt
, vt
->region_top
, vt
->region_bottom
, 1, 1);
963 if (vt
->y_displ
== vt
->y_base
) {
964 vncterm_scroll_up (vt
, vt
->region_top
, vt
->region_bottom
, 1, 0);
967 if (vt
->y_displ
== vt
->y_base
) {
968 if (++vt
->y_displ
== vt
->total_height
) {
973 if (++vt
->y_base
== vt
->total_height
) {
977 if (vt
->scroll_height
< vt
->total_height
) {
981 int y1
= (vt
->y_base
+ vt
->height
- 1) % vt
->total_height
;
982 TextCell
*c
= &vt
->cells
[y1
* vt
->width
];
984 for (x
= 0; x
< vt
->width
; x
++) {
986 c
->attrib
= vt
->default_attrib
;
990 // fprintf (stderr, "BASE: %d DISPLAY %d\n", vt->y_base, vt->y_displ);
992 } else if (vt
->cy
< vt
->height
- 1) {
999 vncterm_csi_m (vncTerm
*vt
)
1003 for (i
= 0; i
< vt
->esc_count
; i
++) {
1004 switch (vt
->esc_buf
[i
]) {
1005 case 0: /* reset all console attributes to default */
1006 vt
->cur_attrib
= vt
->default_attrib
;
1009 vt
->cur_attrib
.bold
= 1;
1012 vt
->cur_attrib
.uline
= 1;
1015 vt
->cur_attrib
.blink
= 1;
1018 vt
->cur_attrib
.invers
= 1;
1021 vt
->cur_attrib
.unvisible
= 1;
1024 vt
->cur_enc
= LAT1_MAP
;
1025 // fixme: dispaly controls = 0 ?
1026 // fixme: toggle meta = 0 ?
1029 vt
->cur_enc
= IBMPC_MAP
;
1030 // fixme: dispaly controls = 1 ?
1031 // fixme: toggle meta = 0 ?
1034 vt
->cur_enc
= IBMPC_MAP
;
1035 // fixme: dispaly controls = 1 ?
1036 // fixme: toggle meta = 1 ?
1039 vt
->cur_attrib
.bold
= 0;
1042 vt
->cur_attrib
.uline
= 0;
1045 vt
->cur_attrib
.blink
= 0;
1048 vt
->cur_attrib
.invers
= 0;
1051 vt
->cur_attrib
.unvisible
= 0;
1061 /* set foreground color */
1062 vt
->cur_attrib
.fgcol
= color_table
[vt
->esc_buf
[i
] - 30];
1065 /* reset color to default, enable underline */
1066 vt
->cur_attrib
.fgcol
= vt
->default_attrib
.fgcol
;
1067 vt
->cur_attrib
.uline
= 1;
1070 /* reset color to default, disable underline */
1071 vt
->cur_attrib
.fgcol
= vt
->default_attrib
.fgcol
;
1072 vt
->cur_attrib
.uline
= 0;
1082 /* set background color */
1083 vt
->cur_attrib
.bgcol
= color_table
[vt
->esc_buf
[i
] - 40];
1086 /* reset background color */
1087 vt
->cur_attrib
.bgcol
= vt
->default_attrib
.bgcol
;
1090 fprintf (stderr
, "unhandled ESC[%d m code\n",vt
->esc_buf
[i
]);
1097 vncterm_save_cursor (vncTerm
*vt
)
1099 vt
->cx_saved
= vt
->cx
;
1100 vt
->cy_saved
= vt
->cy
;
1101 vt
->cur_attrib_saved
= vt
->cur_attrib
;
1102 vt
->charset_saved
= vt
->charset
;
1103 vt
->g0enc_saved
= vt
->g0enc
;
1104 vt
->g1enc_saved
= vt
->g1enc
;
1105 vt
->cur_enc_saved
= vt
->cur_enc
;
1109 vncterm_restore_cursor (vncTerm
*vt
)
1111 vt
->cx
= vt
->cx_saved
;
1112 vt
->cy
= vt
->cy_saved
;
1113 vt
->cur_attrib
= vt
->cur_attrib_saved
;
1114 vt
->charset
= vt
->charset_saved
;
1115 vt
->g0enc
= vt
->g0enc_saved
;
1116 vt
->g1enc
= vt
->g1enc_saved
;
1117 vt
->cur_enc
= vt
->cur_enc_saved
;
1121 vncterm_set_alternate_buffer (vncTerm
*vt
, int on_off
)
1125 vt
->y_displ
= vt
->y_base
;
1129 if (vt
->altbuf
) return;
1133 /* alternate buffer & cursor */
1135 vncterm_save_cursor (vt
);
1136 /* save screen to altcels */
1137 for (y
= 0; y
< vt
->height
; y
++) {
1138 int y1
= (vt
->y_base
+ y
) % vt
->total_height
;
1139 for (x
= 0; x
< vt
->width
; x
++) {
1140 vt
->altcells
[y
*vt
->width
+ x
] = vt
->cells
[y1
*vt
->width
+ x
];
1145 for (y
= 0; y
<= vt
->height
; y
++) {
1146 for (x
= 0; x
< vt
->width
; x
++) {
1147 vncterm_clear_xy (vt
, x
, y
);
1153 if (vt
->altbuf
== 0) return;
1157 /* restore saved data */
1158 for (y
= 0; y
< vt
->height
; y
++) {
1159 int y1
= (vt
->y_base
+ y
) % vt
->total_height
;
1160 for (x
= 0; x
< vt
->width
; x
++) {
1161 vt
->cells
[y1
*vt
->width
+ x
] = vt
->altcells
[y
*vt
->width
+ x
];
1165 vncterm_restore_cursor (vt
);
1168 vncterm_refresh (vt
);
1172 vncterm_set_mode (vncTerm
*vt
, int on_off
)
1176 for (i
= 0; i
<= vt
->esc_count
; i
++) {
1177 if (vt
->esc_ques
) { /* DEC private modes set/reset */
1178 switch(vt
->esc_buf
[i
]) {
1179 case 10: /* X11 mouse reporting on/off */
1181 vt
->report_mouse
= on_off
;
1183 case 1049: /* start/end special app mode (smcup/rmcup) */
1184 vncterm_set_alternate_buffer (vt
, on_off
);
1186 case 25: /* Cursor on/off */
1187 case 9: /* X10 mouse reporting on/off */
1188 case 6: /* Origin relative/absolute */
1189 case 1: /* Cursor keys in appl mode*/
1190 case 5: /* Inverted screen on/off */
1191 case 7: /* Autowrap on/off */
1192 case 8: /* Autorepeat on/off */
1195 } else { /* ANSI modes set/reset */
1196 /* fixme: implement me */
1202 vncterm_gotoxy (vncTerm
*vt
, int x
, int y
)
1204 /* verify all boundaries */
1210 if (x
>= vt
->width
) {
1220 if (y
>= vt
->height
) {
1227 enum { ESnormal
, ESesc
, ESsquare
, ESgetpars
, ESgotpars
, ESfunckey
,
1228 EShash
, ESsetG0
, ESsetG1
, ESpercent
, ESignore
, ESnonstd
,
1229 ESpalette
, ESidquery
, ESosc1
, ESosc2
};
1232 vncterm_putchar (vncTerm
*vt
, unicode ch
)
1238 fprintf (stderr
, "CHAR:%2d: %4x '%c' (cur_enc %d) %d %d\n", vt
->tty_state
, ch
, ch
, vt
->cur_enc
, vt
->cx
, vt
->cy
);
1241 switch(vt
->tty_state
) {
1243 vt
->tty_state
= ESnormal
;
1246 vt
->tty_state
= ESsquare
;
1249 vt
->tty_state
= ESnonstd
;
1252 vt
->tty_state
= ESpercent
;
1255 vncterm_save_cursor (vt
);
1258 vncterm_restore_cursor (vt
);
1261 vt
->tty_state
= ESsetG0
; // SET G0
1264 vt
->tty_state
= ESsetG1
; // SET G1
1267 /* cursor up (ri) */
1268 if (vt
->cy
== vt
->region_top
)
1269 vncterm_scroll_down (vt
, vt
->region_top
, vt
->region_bottom
, 1);
1270 else if (vt
->cy
> 0) {
1275 /* numeric keypad - ignored */
1278 /* appl. keypad - ignored */
1282 fprintf(stderr
, "got unhandled ESC%c %d\n", ch
, ch
);
1287 case ESnonstd
: /* Operating System Controls */
1288 vt
->tty_state
= ESnormal
;
1291 case 'P': /* palette escape sequence */
1292 for(i
= 0; i
< MAX_ESC_PARAMS
; i
++) {
1297 vt
->tty_state
= ESpalette
;
1299 case 'R': /* reset palette */
1300 // fixme: reset_palette(vc);
1307 vt
->osc_textbuf
[0] = 0;
1308 vt
->tty_state
= ESosc1
;
1312 fprintf (stderr
, "unhandled OSC %c\n", ch
);
1314 vt
->tty_state
= ESnormal
;
1319 vt
->tty_state
= ESnormal
;
1321 vt
->tty_state
= ESosc2
;
1324 fprintf (stderr
, "got illegal OSC sequence\n");
1329 if (ch
!= 0x9c && ch
!= 7) {
1331 while (vt
->osc_textbuf
[i
]) i
++;
1332 vt
->osc_textbuf
[i
++] = ch
;
1333 vt
->osc_textbuf
[i
] = 0;
1336 fprintf (stderr
, "OSC:%c:%s\n", vt
->osc_cmd
, vt
->osc_textbuf
);
1338 vt
->tty_state
= ESnormal
;
1342 if ((ch
>= '0' && ch
<= '9') || (ch
>= 'A' && ch
<= 'F')
1343 || (ch
>= 'a' && ch
<= 'f')) {
1344 vt
->esc_buf
[vt
->esc_count
++] = (ch
> '9' ? (ch
& 0xDF) - 'A' + 10 : ch
- '0');
1345 if (vt
->esc_count
== 7) {
1346 // fixme: this does not work - please test
1347 rfbColourMap
*cmap
=&vt
->screen
->colourMap
;
1349 int i
= color_table
[vt
->esc_buf
[0]] * 3, j
= 1;
1350 cmap
->data
.bytes
[i
] = 16 * vt
->esc_buf
[j
++];
1351 cmap
->data
.bytes
[i
++] += vt
->esc_buf
[j
++];
1352 cmap
->data
.bytes
[i
] = 16 * vt
->esc_buf
[j
++];
1353 cmap
->data
.bytes
[i
++] += vt
->esc_buf
[j
++];
1354 cmap
->data
.bytes
[i
] = 16 * vt
->esc_buf
[j
++];
1355 cmap
->data
.bytes
[i
] += vt
->esc_buf
[j
];
1357 //set_palette(vc); ?
1359 vt
->tty_state
= ESnormal
;
1362 vt
->tty_state
= ESnormal
;
1365 for(i
= 0; i
< MAX_ESC_PARAMS
; i
++) {
1370 vt
->esc_has_par
= 0;
1371 vt
->tty_state
= ESgetpars
;
1374 vt
->tty_state
= ESidquery
;
1378 if ((vt
->esc_ques
= (ch
== '?'))) {
1382 if (ch
>= '0' && ch
<= '9') {
1383 vt
->esc_has_par
= 1;
1384 if (vt
->esc_count
< MAX_ESC_PARAMS
) {
1385 vt
->esc_buf
[vt
->esc_count
] = vt
->esc_buf
[vt
->esc_count
] * 10 + ch
- '0';
1388 } else if (ch
== ';') {
1392 if (vt
->esc_has_par
) {
1395 vt
->tty_state
= ESgotpars
;
1399 vt
->tty_state
= ESnormal
;
1402 char *qes
= vt
->esc_ques
? "?" : "";
1403 if (vt
->esc_count
== 0) {
1404 fprintf(stderr
, "ESC[%s%c\n", qes
, ch
);
1405 } else if (vt
->esc_count
== 1) {
1406 fprintf(stderr
, "ESC[%s%d%c\n", qes
, vt
->esc_buf
[0], ch
);
1409 fprintf(stderr
, "ESC[%s%d", qes
, vt
->esc_buf
[0]);
1410 for (i
= 1; i
< vt
->esc_count
; i
++) {
1411 fprintf(stderr
, ";%d", vt
->esc_buf
[i
]);
1413 fprintf (stderr
, "%c\n", ch
);
1419 vncterm_set_mode (vt
, 1);
1422 vncterm_set_mode (vt
, 0);
1425 if (!vt
->esc_count
) {
1426 vt
->esc_count
++; // default parameter 0
1431 /* report cursor position */
1432 /* TODO: send ESC[row;colR */
1435 /* move cursor up */
1436 if (vt
->esc_buf
[0] == 0) {
1439 vt
->cy
-= vt
->esc_buf
[0];
1446 /* move cursor down */
1447 if (vt
->esc_buf
[0] == 0) {
1450 vt
->cy
+= vt
->esc_buf
[0];
1451 if (vt
->cy
>= vt
->height
) {
1452 vt
->cy
= vt
->height
- 1;
1457 /* move cursor right */
1458 if (vt
->esc_buf
[0] == 0) {
1461 vt
->cx
+= vt
->esc_buf
[0];
1462 if (vt
->cx
>= vt
->width
) {
1463 vt
->cx
= vt
->width
- 1;
1467 /* move cursor left */
1468 if (vt
->esc_buf
[0] == 0) {
1471 vt
->cx
-= vt
->esc_buf
[0];
1478 /* move cursor to column */
1479 vncterm_gotoxy (vt
, vt
->esc_buf
[0] - 1, vt
->cy
);
1482 /* move cursor to row */
1483 vncterm_gotoxy (vt
, vt
->cx
, vt
->esc_buf
[0] - 1);
1487 /* move cursor to row, column */
1488 vncterm_gotoxy (vt
, vt
->esc_buf
[1] - 1, vt
->esc_buf
[0] - 1);
1491 switch (vt
->esc_buf
[0]) {
1493 /* clear to end of screen */
1494 for (y
= vt
->cy
; y
< vt
->height
; y
++) {
1495 for (x
= 0; x
< vt
->width
; x
++) {
1496 if (y
== vt
->cy
&& x
< vt
->cx
) {
1499 vncterm_clear_xy (vt
, x
, y
);
1504 /* clear from beginning of screen */
1505 for (y
= 0; y
<= vt
->cy
; y
++) {
1506 for (x
= 0; x
< vt
->width
; x
++) {
1507 if (y
== vt
->cy
&& x
> vt
->cx
) {
1510 vncterm_clear_xy (vt
, x
, y
);
1515 /* clear entire screen */
1516 for (y
= 0; y
<= vt
->height
; y
++) {
1517 for (x
= 0; x
< vt
->width
; x
++) {
1518 vncterm_clear_xy (vt
, x
, y
);
1525 switch (vt
->esc_buf
[0]) {
1528 for(x
= vt
->cx
; x
< vt
->width
; x
++) {
1529 vncterm_clear_xy (vt
, x
, vt
->cy
);
1533 /* clear from beginning of line */
1534 for (x
= 0; x
<= vt
->cx
; x
++) {
1535 vncterm_clear_xy (vt
, x
, vt
->cy
);
1539 /* clear entire line */
1540 for(x
= 0; x
< vt
->width
; x
++) {
1541 vncterm_clear_xy (vt
, x
, vt
->cy
);
1550 if (c
> vt
->height
- vt
->cy
)
1551 c
= vt
->height
- vt
->cy
;
1555 vncterm_scroll_down (vt
, vt
->cy
, vt
->region_bottom
, c
);
1561 if (c
> vt
->height
- vt
->cy
)
1562 c
= vt
->height
- vt
->cy
;
1566 vncterm_scroll_up (vt
, vt
->cy
, vt
->region_bottom
, c
, 1);
1572 vncterm_scroll_down (vt
, vt
->region_top
, vt
->region_bottom
, c
);
1578 vncterm_scroll_up (vt
, vt
->region_top
, vt
->region_bottom
, c
, 1);
1581 /* delete c character */
1584 if (c
> vt
->width
- vt
->cx
)
1585 c
= vt
->width
- vt
->cx
;
1589 for (x
= vt
->cx
; x
< vt
->width
- c
; x
++) {
1590 int y1
= (vt
->y_base
+ vt
->cy
) % vt
->total_height
;
1591 TextCell
*dst
= &vt
->cells
[y1
* vt
->width
+ x
];
1592 TextCell
*src
= dst
+ c
;
1594 vncterm_update_xy (vt
, x
+ c
, vt
->cy
);
1596 src
->attrib
= vt
->default_attrib
;
1597 vncterm_update_xy (vt
, x
, vt
->cy
);
1601 /* save cursor position */
1602 vncterm_save_cursor (vt
);
1605 /* restore cursor position */
1606 vncterm_restore_cursor (vt
);
1609 /* erase c characters */
1613 if (c
> (vt
->width
- vt
->cx
)) c
= vt
->width
- vt
->cx
;
1615 for(i
= 0; i
< c
; i
++) {
1616 vncterm_clear_xy (vt
, vt
->cx
+ i
, vt
->cy
);
1620 /* insert c character */
1622 if (c
> (vt
->width
- vt
->cx
)) {
1623 c
= vt
->width
- vt
->cx
;
1627 for (x
= vt
->width
- c
; x
>= vt
->cx
; x
--) {
1628 int y1
= (vt
->y_base
+ vt
->cy
) % vt
->total_height
;
1629 TextCell
*src
= &vt
->cells
[y1
* vt
->width
+ x
];
1630 TextCell
*dst
= src
+ c
;
1632 vncterm_update_xy (vt
, x
+ c
, vt
->cy
);
1634 src
->attrib
= vt
->cur_attrib
;
1635 vncterm_update_xy (vt
, x
, vt
->cy
);
1641 if (!vt
->esc_buf
[0])
1643 if (!vt
->esc_buf
[1])
1644 vt
->esc_buf
[1] = vt
->height
;
1645 /* Minimum allowed region is 2 lines */
1646 if (vt
->esc_buf
[0] < vt
->esc_buf
[1] &&
1647 vt
->esc_buf
[1] <= vt
->height
) {
1648 vt
->region_top
= vt
->esc_buf
[0] - 1;
1649 vt
->region_bottom
= vt
->esc_buf
[1];
1651 vt
->cy
= vt
->region_top
;
1653 fprintf (stderr
, "set region %d %d\n", vt
->region_top
, vt
->region_bottom
);
1660 if (vt
->esc_count
== 0) {
1661 fprintf(stderr
, "unhandled escape ESC[%s%c\n", qes
, ch
);
1662 } else if (vt
->esc_count
== 1) {
1663 fprintf(stderr
, "unhandled escape ESC[%s%d%c\n", qes
, vt
->esc_buf
[0], ch
);
1666 fprintf(stderr
, "unhandled escape ESC[%s%d", qes
, vt
->esc_buf
[0]);
1667 for (i
= 1; i
< vt
->esc_count
; i
++) {
1668 fprintf(stderr
, ";%d", vt
->esc_buf
[i
]);
1670 fprintf (stderr
, "%c\n", ch
);
1677 case ESsetG0
: // Set G0
1678 vt
->tty_state
= ESnormal
;
1681 vt
->g0enc
= GRAF_MAP
;
1683 vt
->g0enc
= LAT1_MAP
;
1685 vt
->g0enc
= IBMPC_MAP
;
1687 vt
->g0enc
= USER_MAP
;
1689 if (vt
->charset
== 0)
1690 vt
->cur_enc
= vt
->g0enc
;
1693 case ESsetG1
: // Set G1
1694 vt
->tty_state
= ESnormal
;
1697 vt
->g1enc
= GRAF_MAP
;
1699 vt
->g1enc
= LAT1_MAP
;
1701 vt
->g1enc
= IBMPC_MAP
;
1703 vt
->g1enc
= USER_MAP
;
1705 if (vt
->charset
== 1)
1706 vt
->cur_enc
= vt
->g1enc
;
1709 case ESidquery
: // vt100 query id
1710 vt
->tty_state
= ESnormal
;
1714 fprintf (stderr
, "ESC[>c Query term ID\n");
1716 vncterm_respond_esc (vt
, TERMIDCODE
);
1720 vt
->tty_state
= ESnormal
;
1722 case '@': /* defined in ISO 2022 */
1725 case 'G': /* prelim official escape code */
1726 case '8': /* retained for compatibility */
1731 default: // ESnormal
1732 vt
->tty_state
= ESnormal
;
1737 case 7: /* alert aka. bell */
1738 rfbSendBell(vt
->screen
);
1740 case 8: /* backspace */
1744 case 9: /* tabspace */
1745 if (vt
->cx
+ (8 - (vt
->cx
% 8)) > vt
->width
) {
1747 vncterm_put_lf (vt
);
1749 vt
->cx
= vt
->cx
+ (8 - (vt
->cx
% 8));
1755 vncterm_put_lf (vt
);
1757 case 13: /* carriage return */
1761 /* SI (shift in), select character set 1 */
1763 vt
->cur_enc
= vt
->g1enc
;
1764 /* fixme: display controls = 1 */
1767 /* SO (shift out), select character set 0 */
1769 vt
->cur_enc
= vt
->g0enc
;
1770 /* fixme: display controls = 0 */
1773 vt
->tty_state
= ESesc
;
1775 case 127: /* delete */
1778 case 128+27: /* csi */
1779 vt
->tty_state
= ESsquare
;
1782 if (vt
->cx
>= vt
->width
) {
1785 vncterm_put_lf (vt
);
1788 int y1
= (vt
->y_base
+ vt
->cy
) % vt
->total_height
;
1789 TextCell
*c
= &vt
->cells
[y1
*vt
->width
+ vt
->cx
];
1790 c
->attrib
= vt
->cur_attrib
;
1792 vncterm_update_xy (vt
, vt
->cx
, vt
->cy
);
1801 vncterm_puts (vncTerm
*vt
, const char *buf
, int len
)
1805 vncterm_show_cursor (vt
, 0);
1808 unsigned char c
= *buf
;
1812 if (vt
->tty_state
!= ESnormal
) {
1813 // never translate escape sequence
1815 } else if (vt
->utf8
&& !vt
->cur_enc
) {
1817 if(c
& 0x80) { // utf8 multi-byte sequence
1819 if (vt
->utf_count
> 0 && (c
& 0xc0) == 0x80) {
1820 // inside UTF8 sequence
1821 vt
->utf_char
= (vt
->utf_char
<< 6) | (c
& 0x3f);
1823 if (vt
->utf_count
== 0) {
1829 // first char of a UTF8 sequence
1830 if ((c
& 0xe0) == 0xc0) {
1832 vt
->utf_char
= (c
& 0x1f);
1833 } else if ((c
& 0xf0) == 0xe0) {
1835 vt
->utf_char
= (c
& 0x0f);
1836 } else if ((c
& 0xf8) == 0xf0) {
1838 vt
->utf_char
= (c
& 0x07);
1839 } else if ((c
& 0xfc) == 0xf8) {
1841 vt
->utf_char
= (c
& 0x03);
1842 } else if ((c
& 0xfe) == 0xfc) {
1844 vt
->utf_char
= (c
& 0x01);
1857 // never translate controls
1858 if (c
>= 32 && c
!= 127 && c
!= (128+27)) {
1859 tc
= translations
[vt
->cur_enc
][c
& 0x0ff];
1865 vncterm_putchar (vt
, tc
);
1868 vncterm_show_cursor (vt
, 1);
1873 vncterm_kbd_event (rfbBool down
, rfbKeySym keySym
, rfbClientPtr cl
)
1875 vncTerm
*vt
=(vncTerm
*)cl
->screen
->screenData
;
1876 static int control
= 0;
1877 static int shift
= 0;
1880 //fprintf (stderr, "KEYEVENT:%d: %08x\n", down == 0, keySym);fflush (stderr);
1882 //fprintf (stderr, "KEYPRESS: %d\n", keySym);fflush (stderr);
1884 if (keySym
== XK_Shift_L
|| keySym
== XK_Shift_R
) {
1886 } if (keySym
== XK_Control_L
|| keySym
== XK_Control_R
) {
1888 } else if (vt
->ibuf_count
< (IBUFSIZE
- 32)) {
1891 if(keySym
>= 'a' && keySym
<= 'z')
1893 else if (keySym
>= 'A' && keySym
<= 'Z')
1907 case XK_Delete
: /* kdch1 */
1910 case XK_Home
: /* khome */
1914 case XK_KP_End
: /* kend */
1916 case XK_Insert
: /* kich1 */
1920 case XK_KP_Up
: /* kcuu1 */
1922 case XK_Down
: /* kcud1 */
1926 case XK_KP_Right
: /* kcuf1 */
1929 case XK_KP_Left
: /* kcub1 */
1933 vncterm_virtual_scroll (vt
, -vt
->height
/2);
1939 vncterm_virtual_scroll (vt
, vt
->height
/2);
1973 fprintf (stderr
, "KEYPRESS OUT:%s: %d\n", esc
, keySym
); fflush (stderr
);
1976 if (vt
->y_displ
!= vt
->y_base
) {
1977 vt
->y_displ
= vt
->y_base
;
1978 vncterm_refresh (vt
);
1982 vncterm_respond_esc (vt
, esc
);
1983 } else if(keySym
<0x100) {
1985 int len
= ucs2_to_utf8 (keySym
& 0x0fff, &vt
->ibuf
[vt
->ibuf_count
]);
1986 vt
->ibuf_count
+= len
;
1988 vt
->ibuf
[vt
->ibuf_count
++] = (char)keySym
;
1993 if (keySym
== XK_Shift_L
|| keySym
== XK_Shift_R
) {
1995 } else if (keySym
== XK_Control_L
|| keySym
== XK_Control_R
) {
2002 vncterm_set_xcut_text (char* str
, int len
, struct _rfbClientRec
* cl
)
2004 vncTerm
*vt
=(vncTerm
*)cl
->screen
->screenData
;
2006 // seems str is Latin-1 encoded
2007 if (vt
->selection
) free (vt
->selection
);
2008 vt
->selection
= (unicode
*)malloc (len
*sizeof (unicode
));
2010 for (i
= 0; i
< len
; i
++) {
2011 vt
->selection
[i
] = str
[i
] & 0xff;
2013 vt
->selection_len
= len
;
2017 mouse_report (vncTerm
*vt
, int butt
, int mrx
, int mry
)
2021 sprintf (buf
, "[M%c%c%c", (char)(' ' + butt
), (char)('!' + mrx
),
2024 vncterm_respond_esc (vt
, buf
);
2028 vncterm_toggle_marked_cell (vncTerm
*vt
, int pos
)
2030 int x
= (pos
%vt
->width
)*8;
2031 int y
= (pos
/vt
->width
)*16;
2034 rfbScreenInfoPtr s
=vt
->screen
;
2036 char *b
= s
->frameBuffer
+y
*s
->width
+x
;
2038 for (j
=0; j
< 16; j
++) {
2039 for(i
=0; i
< 8; i
++) {
2040 b
[j
*s
->width
+i
] ^= 0x0f;
2041 rfbMarkRectAsModified (s
, x
, y
, x
+8, y
+16);
2047 vncterm_pointer_event (int buttonMask
, int x
, int y
, rfbClientPtr cl
)
2049 vncTerm
*vt
=(vncTerm
*)cl
->screen
->screenData
;
2050 static int button2_released
= 1;
2051 static int last_mask
= 0;
2052 static int sel_start_pos
= 0;
2053 static int sel_end_pos
= 0;
2060 if (cx
>= vt
->width
) cx
= vt
->width
- 1;
2062 if (cy
>= vt
->height
) cy
= vt
->height
- 1;
2064 if (vt
->report_mouse
&& buttonMask
!= last_mask
) {
2065 last_mask
= buttonMask
;
2066 if (buttonMask
& 1) {
2067 mouse_report (vt
, 0, cx
, cy
);
2069 if (buttonMask
& 2) {
2070 mouse_report (vt
, 1, cx
, cy
);
2072 if (buttonMask
& 4) {
2073 mouse_report (vt
, 2, cx
, cy
);
2076 mouse_report (vt
, 3, cx
, cy
);
2080 if (buttonMask
& 2) {
2081 if(button2_released
&& vt
->selection
) {
2083 for(i
= 0; i
< vt
->selection_len
; i
++) {
2084 if (vt
->ibuf_count
< IBUFSIZE
- 6) { // uft8 is max 6 characters wide
2086 vt
->ibuf_count
+= ucs2_to_utf8 (vt
->selection
[i
], &vt
->ibuf
[vt
->ibuf_count
]);
2088 vt
->ibuf
[vt
->ibuf_count
++] = vt
->selection
[i
];
2092 if (vt
->y_displ
!= vt
->y_base
) {
2093 vt
->y_displ
= vt
->y_base
;
2094 vncterm_refresh (vt
);
2097 button2_released
= 0;
2099 button2_released
= 1;
2102 if (buttonMask
& 1) {
2103 int pos
= cy
*vt
->width
+ cx
;
2105 // code borrowed from libvncserver (VNConsole.c)
2107 if (!vt
->mark_active
) {
2109 vt
->mark_active
= 1;
2110 sel_start_pos
= sel_end_pos
= pos
;
2111 vncterm_toggle_marked_cell (vt
, pos
);
2115 if (pos
!= sel_end_pos
) {
2117 if (pos
> sel_end_pos
) {
2118 cx
= sel_end_pos
; cy
=pos
;
2120 cx
=pos
; cy
=sel_end_pos
;
2123 if (cx
< sel_start_pos
) {
2124 if (cy
< sel_start_pos
) cy
--;
2130 vncterm_toggle_marked_cell (vt
, cx
);
2138 } else if (vt
->mark_active
) {
2139 vt
->mark_active
= 0;
2141 if (sel_start_pos
> sel_end_pos
) {
2142 int tmp
= sel_start_pos
- 1;
2143 sel_start_pos
= sel_end_pos
;
2147 int len
= sel_end_pos
- sel_start_pos
+ 1;
2149 if (vt
->selection
) free (vt
->selection
);
2150 vt
->selection
= (unicode
*)malloc (len
*sizeof (unicode
));
2151 vt
->selection_len
= len
;
2152 char *sel_latin1
= (char *)malloc (len
+ 1);
2154 for (i
= 0; i
< len
; i
++) {
2155 int pos
= sel_start_pos
+ i
;
2156 int x
= pos
% vt
->width
;
2157 int y1
= ((pos
/ vt
->width
) + vt
->y_displ
) % vt
->total_height
;
2158 TextCell
*c
= &vt
->cells
[y1
*vt
->width
+ x
];
2159 vt
->selection
[i
] = c
->ch
;
2160 sel_latin1
[i
] = (char)c
->ch
;
2163 sel_latin1
[len
] = 0;
2164 rfbGotXCutText (vt
->screen
, sel_latin1
, len
);
2167 while (sel_start_pos
<= sel_end_pos
) {
2168 vncterm_toggle_marked_cell (vt
, sel_start_pos
++);
2173 rfbDefaultPtrAddEvent (buttonMask
, x
, y
, cl
);
2176 static int client_count
= 0;
2177 static int client_connected
= 0;
2178 static int last_client
= 1;
2179 static time_t last_time
= 0;
2182 client_gone (rfbClientPtr client
)
2186 last_time
= time (NULL
);
2188 if (client_count
<= 0) {
2193 /* libvncserver callback for when a new client connects */
2194 enum rfbNewClientAction
2195 new_client (rfbClientPtr client
)
2197 client
->clientGoneHook
= client_gone
;
2200 last_time
= time (NULL
);
2203 client_connected
= 1;
2205 return RFB_CLIENT_ACCEPT
;
2208 static char *vncticket
= NULL
;
2211 create_vncterm (int argc
, char** argv
, int maxx
, int maxy
)
2215 rfbScreenInfoPtr screen
= rfbGetScreen (&argc
, argv
, maxx
, maxy
, 8, 1, 1);
2216 screen
->frameBuffer
=(char*)calloc(maxx
*maxy
, 1);
2218 char **passwds
= calloc(sizeof(char**), 2);
2220 vncTerm
*vt
= (vncTerm
*)calloc (sizeof(vncTerm
), 1);
2222 rfbColourMap
*cmap
=&screen
->colourMap
;
2223 cmap
->data
.bytes
= malloc (16*3);
2225 cmap
->data
.bytes
[i
*3 + 0] = default_red
[color_table
[i
]];
2226 cmap
->data
.bytes
[i
*3 + 1] = default_grn
[color_table
[i
]];
2227 cmap
->data
.bytes
[i
*3 + 2] = default_blu
[color_table
[i
]];
2231 screen
->serverFormat
.trueColour
= FALSE
;
2233 screen
->kbdAddEvent
= vncterm_kbd_event
;
2235 screen
->setXCutText
= vncterm_set_xcut_text
;
2237 screen
->ptrAddEvent
= vncterm_pointer_event
;
2239 screen
->desktopName
= "VNC Command Terminal";
2241 screen
->newClientHook
= new_client
;
2243 vt
->maxx
= screen
->width
;
2244 vt
->maxy
= screen
->height
;
2246 vt
->width
= vt
->maxx
/ 8;
2247 vt
->height
= vt
->maxy
/ 16;
2249 vt
->total_height
= vt
->height
* 20;
2250 vt
->scroll_height
= 0;
2255 vt
->region_bottom
= vt
->height
;
2257 vt
->g0enc
= LAT1_MAP
;
2258 vt
->g1enc
= GRAF_MAP
;
2259 vt
->cur_enc
= vt
->g0enc
;
2262 /* default text attributes */
2263 vt
->default_attrib
.bold
= 0;
2264 vt
->default_attrib
.uline
= 0;
2265 vt
->default_attrib
.blink
= 0;
2266 vt
->default_attrib
.invers
= 0;
2267 vt
->default_attrib
.unvisible
= 0;
2268 vt
->default_attrib
.fgcol
= 7;
2269 vt
->default_attrib
.bgcol
= 0;
2271 vt
->cur_attrib
= vt
->default_attrib
;
2273 vt
->cells
= (TextCell
*)calloc (sizeof (TextCell
), vt
->width
*vt
->total_height
);
2275 for (i
= 0; i
< vt
->width
*vt
->total_height
; i
++) {
2276 vt
->cells
[i
].ch
= ' ';
2277 vt
->cells
[i
].attrib
= vt
->default_attrib
;
2280 vt
->altcells
= (TextCell
*)calloc (sizeof (TextCell
), vt
->width
*vt
->height
);
2282 vt
->screen
= screen
;
2284 screen
->screenData
= (void*)vt
;
2286 //screen->autoPort = 1;
2289 passwds
[0] = vncticket
;
2292 screen
->authPasswdData
= (void *)passwds
;
2293 screen
->passwordCheck
= rfbCheckPasswordByList
;
2295 rfbRegisterSecurityHandler(&VncSecurityHandlerVencrypt
);
2298 rfbInitServer(screen
);
2304 main (int argc
, char** argv
)
2307 char **cmdargv
= NULL
;
2308 char *command
= "/bin/bash"; // execute normal shell as default
2313 struct timeval tv
, tv1
;
2314 time_t elapsed
, cur_time
;
2315 struct winsize dimensions
;
2317 if (gnutls_global_init () < 0) {
2318 fprintf(stderr
, "gnutls_global_init failed\n");
2322 if (gnutls_dh_params_init (&dh_params
) < 0) {
2323 fprintf(stderr
, "gnutls_dh_params_init failed\n");
2327 if (gnutls_dh_params_generate2 (dh_params
, DH_BITS
) < 0) {
2328 fprintf(stderr
, "gnutls_dh_params_init failed\n");
2332 for (i
= 1; i
< argc
; i
++) {
2333 if (!strcmp (argv
[i
], "-c")) {
2334 command
= argv
[i
+1];
2335 cmdargv
= &argv
[i
+1];
2342 for (i
= 1; i
< argc
; i
++) {
2343 if (!strcmp (argv
[i
], "-timeout")) {
2344 CHECK_ARGC (argc
, argv
, i
);
2345 idle_timeout
= atoi(argv
[i
+1]);
2346 rfbPurgeArguments(&argc
, &i
, 2, argv
); i
--;
2347 } else if (!strcmp (argv
[i
], "-authpath")) {
2348 CHECK_ARGC (argc
, argv
, i
);
2349 auth_path
= argv
[i
+1];
2350 rfbPurgeArguments(&argc
, &i
, 2, argv
); i
--;
2351 } else if (!strcmp (argv
[i
], "-perm")) {
2352 CHECK_ARGC (argc
, argv
, i
);
2353 auth_perm
= argv
[i
+1];
2354 rfbPurgeArguments(&argc
, &i
, 2, argv
); i
--;
2355 } else if (!strcmp (argv
[i
], "-notls")) {
2356 rfbPurgeArguments(&argc
, &i
, 1, argv
); i
--;
2357 if ((vncticket
= getenv("PVE_VNC_TICKET")) == NULL
) {
2358 fprintf(stderr
, "missing env PVE_VNC_TICKET (-notls)\n");
2364 unsetenv("PVE_VNC_TICKET"); // do not expose this to child
2368 gnutls_global_set_log_level(10);
2369 gnutls_global_set_log_function(vnc_debug_gnutls_log
);
2374 vncTerm
*vt
= create_vncterm (argc
, argv
, 745, 400);
2376 setlocale(LC_ALL
, ""); // set from environment
2378 char *ctype
= setlocale (LC_CTYPE
, NULL
); // query LC_CTYPE
2380 // fixme: ist there a standard way to detect utf8 mode ?
2381 if (strcasestr (ctype
, ".utf-8")||strcasestr (ctype
, ".utf8")) {
2385 dimensions
.ws_col
= vt
->width
;
2386 dimensions
.ws_row
= vt
->height
;
2388 setenv ("TERM", TERM
, 1);
2390 pid
= forkpty (&master
, ptyname
, NULL
, &dimensions
);
2393 // install default signal handlers
2394 signal (SIGQUIT
, SIG_DFL
);
2395 signal (SIGTERM
, SIG_DFL
);
2396 signal (SIGINT
, SIG_DFL
);
2399 execvp (command
, cmdargv
);
2401 execlp (command
, command
, NULL
);
2403 perror ("Error: exec failed\n");
2404 exit (-1); // should not be reached
2405 } else if (pid
== -1) {
2406 perror ("Error: fork failed\n");
2411 FD_SET (master
, &fs
);
2413 tv
.tv_usec
= 5000; /* 5 ms */
2415 last_time
= time (NULL
);
2423 cur_time
= time (NULL
);
2425 elapsed
= cur_time
- last_time
;
2426 //printf ("Elapsed %ld\n", elapsed);
2429 if (client_connected
) {
2430 if (idle_timeout
&& (elapsed
>= idle_timeout
)) {
2434 // wait at least 20 seconds for initial connect
2435 if (idle_timeout
&& (elapsed
>= (idle_timeout
> 20 ? idle_timeout
: 20))) {
2440 // exit after 30 minutes idle time
2441 if (elapsed
>= 30*60) {
2446 rfbProcessEvents (vt
->screen
, 40000); /* 40 ms */
2448 if (vt
->ibuf_count
> 0) {
2449 //printf ("DEBUG: WRITE %d %d\n", vt->ibuf[0], vt->ibuf_count);
2450 write (master
, vt
->ibuf
, vt
->ibuf_count
);
2452 last_time
= time (NULL
);
2455 if (!vt
->mark_active
) {
2457 int num_fds
= select (master
+1, &fs1
, NULL
, NULL
, &tv1
);
2459 if (FD_ISSET (master
, &fs1
)) {
2462 while ((c
= read (master
, buffer
, 1024)) == -1) {
2463 if (errno
!= EAGAIN
) break;
2466 vncterm_puts (vt
, buffer
, c
);
2476 waitpid(pid
, &status
, 0);