3 Copyright (C) 2013 Proxmox Server Solutions GmbH
5 Copyright: spiceterm 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>
23 Note: most of the code here is copied from vncterm (which is
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <arpa/inet.h>
37 #include <pty.h> /* for openpty and forkpty */
40 #include <sys/ioctl.h>
45 #include "spiceterm.h"
49 #include <spice/enums.h>
50 #include <spice/macros.h>
51 #include <spice/qxl_dev.h>
53 #include <gdk/gdkkeysyms.h>
55 #include "event_loop.h"
56 #include "translations.h"
60 #define DPRINTF(x, format, ...) { \
62 printf("%s: " format "\n" , __FUNCTION__, ## __VA_ARGS__); \
68 #define TERMIDCODE "[?1;2c" // vt100 ID
70 #define CHECK_ARGC(argc,argv,i) if (i >= argc-1) { \
71 fprintf(stderr, "ERROR: not enough arguments for: %s\n", argv[i]); \
76 /* these colours are from linux kernel drivers/char/vt.c */
78 unsigned char color_table
[] = { 0, 4, 2, 6, 1, 5, 3, 7,
79 8,12,10,14, 9,13,11,15 };
83 print_usage(const char *msg
)
85 if (msg
) { fprintf(stderr
, "ERROR: %s\n", msg
); }
86 fprintf(stderr
, "USAGE: spiceterm [spiceopts] [-c command [args]]\n");
89 /* Convert UCS2 to UTF8 sequence, trailing zero */
92 ucs2_to_utf8 (gunichar2 c, char *out)
95 out[0] = c; // 0*******
98 } else if (c < 0x800) {
99 out[0] = 0xc0 | (c >> 6); // 110***** 10******
100 out[1] = 0x80 | (c & 0x3f);
104 out[0] = 0xe0 | (c >> 12); // 1110**** 10****** 10******
105 out[1] = 0x80 | ((c >> 6) & 0x3f);
106 out[2] = 0x80 | (c & 0x3f);
116 draw_char_at (spiceTerm
*vt
, int x
, int y
, gunichar2 ch
, TextAttributes attrib
)
118 if (x
< 0 || y
< 0 || x
>= vt
->width
|| y
>= vt
->height
) {
122 spice_screen_draw_char(vt
->screen
, x
, y
, ch
, attrib
);
126 spiceterm_update_xy (spiceTerm
*vt
, int x
, int y
)
128 if (x
< 0 || y
< 0 || x
>= vt
->width
|| y
>= vt
->height
) { return; }
130 int y1
= (vt
->y_base
+ y
) % vt
->total_height
;
131 int y2
= y1
- vt
->y_displ
;
133 y2
+= vt
->total_height
;
135 if (y2
< vt
->height
) {
136 TextCell
*c
= &vt
->cells
[y1
* vt
->width
+ x
];
137 draw_char_at (vt
, x
, y2
, c
->ch
, c
->attrib
);
142 spiceterm_clear_xy (spiceTerm
*vt
, int x
, int y
)
144 if (x
< 0 || y
< 0 || x
>= vt
->width
|| y
>= vt
->height
) { return; }
146 int y1
= (vt
->y_base
+ y
) % vt
->total_height
;
147 int y2
= y1
- vt
->y_displ
;
149 y2
+= vt
->total_height
;
151 if (y2
< vt
->height
) {
152 TextCell
*c
= &vt
->cells
[y1
* vt
->width
+ x
];
154 c
->attrib
= vt
->default_attrib
;
155 c
->attrib
.fgcol
= vt
->cur_attrib
.fgcol
;
156 c
->attrib
.bgcol
= vt
->cur_attrib
.bgcol
;
158 draw_char_at (vt
, x
, y
, c
->ch
, c
->attrib
);
163 spiceterm_show_cursor (spiceTerm
*vt
, int show
)
166 if (x
>= vt
->width
) {
170 int y1
= (vt
->y_base
+ vt
->cy
) % vt
->total_height
;
171 int y
= y1
- vt
->y_displ
;
173 y
+= vt
->total_height
;
176 if (y
< vt
->height
) {
178 TextCell
*c
= &vt
->cells
[y1
* vt
->width
+ x
];
181 TextAttributes attrib
= vt
->default_attrib
;
182 attrib
.invers
= !(attrib
.invers
); /* invert fg and bg */
183 draw_char_at (vt
, x
, y
, c
->ch
, attrib
);
185 draw_char_at (vt
, x
, y
, c
->ch
, c
->attrib
);
191 spiceterm_refresh (spiceTerm
*vt
)
196 for(y
= 0; y
< vt
->height
; y
++) {
197 TextCell
*c
= vt
->cells
+ y1
* vt
->width
;
198 for(x
= 0; x
< vt
->width
; x
++) {
199 draw_char_at (vt
, x
, y
, c
->ch
, c
->attrib
);
202 if (++y1
== vt
->total_height
)
206 spiceterm_show_cursor (vt
, 1);
210 spiceterm_scroll_down (spiceTerm
*vt
, int top
, int bottom
, int lines
)
212 if ((top
+ lines
) >= bottom
) {
213 lines
= bottom
- top
-1;
216 if (top
< 0 || bottom
> vt
->height
|| top
>= bottom
|| lines
< 1) {
221 for(i
= bottom
- top
- lines
- 1; i
>= 0; i
--) {
222 int src
= ((vt
->y_base
+ top
+ i
) % vt
->total_height
)*vt
->width
;
223 int dst
= ((vt
->y_base
+ top
+ lines
+ i
) % vt
->total_height
)*vt
->width
;
225 memmove(vt
->cells
+ dst
, vt
->cells
+ src
, vt
->width
*sizeof (TextCell
));
228 for (i
= 0; i
< lines
; i
++) {
230 TextCell
*c
= vt
->cells
+ ((vt
->y_base
+ top
+ i
) % vt
->total_height
)*vt
->width
;
231 for(j
= 0; j
< vt
->width
; j
++) {
232 c
->attrib
= vt
->default_attrib
;
243 spice_screen_scroll(vt
->screen
, 0, y1
, vt
->screen
->primary_width
, y2
, 0, y0
);
244 spice_screen_clear(vt
->screen
, 0, y0
, vt
->screen
->primary_width
, y1
);
248 spiceterm_scroll_up (spiceTerm
*vt
, int top
, int bottom
, int lines
, int moveattr
)
250 if ((top
+ lines
) >= bottom
) {
251 lines
= bottom
- top
- 1;
254 if (top
< 0 || bottom
> vt
->height
|| top
>= bottom
|| lines
< 1) {
261 int y1
= (top
+ lines
)*16;
264 spice_screen_scroll(vt
->screen
, 0, y0
, vt
->screen
->primary_width
, y2
-h
, 0, y1
);
265 spice_screen_clear(vt
->screen
, 0, y2
-h
, vt
->screen
->primary_width
, y2
);
274 for(i
= 0; i
< (bottom
- top
- lines
); i
++) {
275 int dst
= ((vt
->y_base
+ top
+ i
) % vt
->total_height
)*vt
->width
;
276 int src
= ((vt
->y_base
+ top
+ lines
+ i
) % vt
->total_height
)*vt
->width
;
278 memmove(vt
->cells
+ dst
, vt
->cells
+ src
, vt
->width
*sizeof (TextCell
));
281 for (i
= 1; i
<= lines
; i
++) {
283 TextCell
*c
= vt
->cells
+ ((vt
->y_base
+ bottom
- i
) % vt
->total_height
)*vt
->width
;
284 for(j
= 0; j
< vt
->width
; j
++) {
285 c
->attrib
= vt
->default_attrib
;
293 spiceterm_virtual_scroll (spiceTerm
*vt
, int lines
)
295 if (vt
->altbuf
|| lines
== 0) return;
299 int i
= vt
->scroll_height
;
300 if (i
> vt
->total_height
- vt
->height
)
301 i
= vt
->total_height
- vt
->height
;
302 int y1
= vt
->y_base
- i
;
304 y1
+= vt
->total_height
;
305 for(i
= 0; i
< lines
; i
++) {
306 if (vt
->y_displ
== y1
) break;
307 if (--vt
->y_displ
< 0) {
308 vt
->y_displ
= vt
->total_height
- 1;
313 for(i
= 0; i
< lines
; i
++) {
314 if (vt
->y_displ
== vt
->y_base
) break;
315 if (++vt
->y_displ
== vt
->total_height
) {
321 spiceterm_refresh (vt
);
325 spiceterm_respond_esc (spiceTerm
*vt
, const char *esc
)
327 int len
= strlen (esc
);
330 if (vt
->ibuf_count
< (IBUFSIZE
- 1 - len
)) {
331 vt
->ibuf
[vt
->ibuf_count
++] = 27;
332 for (i
= 0; i
< len
; i
++) {
333 vt
->ibuf
[vt
->ibuf_count
++] = esc
[i
];
339 spiceterm_put_lf (spiceTerm
*vt
)
341 if (vt
->cy
+ 1 == vt
->region_bottom
) {
343 if (vt
->altbuf
|| vt
->region_top
!= 0 || vt
->region_bottom
!= vt
->height
) {
344 spiceterm_scroll_up (vt
, vt
->region_top
, vt
->region_bottom
, 1, 1);
348 if (vt
->y_displ
== vt
->y_base
) {
349 spiceterm_scroll_up (vt
, vt
->region_top
, vt
->region_bottom
, 1, 0);
352 if (vt
->y_displ
== vt
->y_base
) {
353 if (++vt
->y_displ
== vt
->total_height
) {
358 if (++vt
->y_base
== vt
->total_height
) {
362 if (vt
->scroll_height
< vt
->total_height
) {
366 int y1
= (vt
->y_base
+ vt
->height
- 1) % vt
->total_height
;
367 TextCell
*c
= &vt
->cells
[y1
* vt
->width
];
369 for (x
= 0; x
< vt
->width
; x
++) {
371 c
->attrib
= vt
->default_attrib
;
375 // fprintf (stderr, "BASE: %d DISPLAY %d\n", vt->y_base, vt->y_displ);
377 } else if (vt
->cy
< vt
->height
- 1) {
383 spiceterm_csi_m (spiceTerm
*vt
)
387 for (i
= 0; i
< vt
->esc_count
; i
++) {
388 switch (vt
->esc_buf
[i
]) {
389 case 0: /* reset all console attributes to default */
390 vt
->cur_attrib
= vt
->default_attrib
;
393 vt
->cur_attrib
.bold
= 1;
396 vt
->cur_attrib
.uline
= 1;
399 vt
->cur_attrib
.blink
= 1;
402 vt
->cur_attrib
.invers
= 1;
405 vt
->cur_attrib
.unvisible
= 1;
408 vt
->cur_enc
= LAT1_MAP
;
409 // fixme: dispaly controls = 0 ?
410 // fixme: toggle meta = 0 ?
413 vt
->cur_enc
= IBMPC_MAP
;
414 // fixme: dispaly controls = 1 ?
415 // fixme: toggle meta = 0 ?
418 vt
->cur_enc
= IBMPC_MAP
;
419 // fixme: dispaly controls = 1 ?
420 // fixme: toggle meta = 1 ?
423 vt
->cur_attrib
.bold
= 0;
426 vt
->cur_attrib
.uline
= 0;
429 vt
->cur_attrib
.blink
= 0;
432 vt
->cur_attrib
.invers
= 0;
435 vt
->cur_attrib
.unvisible
= 0;
445 /* set foreground color */
446 vt
->cur_attrib
.fgcol
= color_table
[vt
->esc_buf
[i
] - 30];
449 /* reset color to default, enable underline */
450 vt
->cur_attrib
.fgcol
= vt
->default_attrib
.fgcol
;
451 vt
->cur_attrib
.uline
= 1;
454 /* reset color to default, disable underline */
455 vt
->cur_attrib
.fgcol
= vt
->default_attrib
.fgcol
;
456 vt
->cur_attrib
.uline
= 0;
466 /* set background color */
467 vt
->cur_attrib
.bgcol
= color_table
[vt
->esc_buf
[i
] - 40];
470 /* reset background color */
471 vt
->cur_attrib
.bgcol
= vt
->default_attrib
.bgcol
;
474 fprintf(stderr
, "unhandled ESC[%d m code\n",vt
->esc_buf
[i
]);
481 spiceterm_save_cursor (spiceTerm
*vt
)
483 vt
->cx_saved
= vt
->cx
;
484 vt
->cy_saved
= vt
->cy
;
485 vt
->cur_attrib_saved
= vt
->cur_attrib
;
486 vt
->charset_saved
= vt
->charset
;
487 vt
->g0enc_saved
= vt
->g0enc
;
488 vt
->g1enc_saved
= vt
->g1enc
;
489 vt
->cur_enc_saved
= vt
->cur_enc
;
493 spiceterm_restore_cursor (spiceTerm
*vt
)
495 vt
->cx
= vt
->cx_saved
;
496 vt
->cy
= vt
->cy_saved
;
497 vt
->cur_attrib
= vt
->cur_attrib_saved
;
498 vt
->charset
= vt
->charset_saved
;
499 vt
->g0enc
= vt
->g0enc_saved
;
500 vt
->g1enc
= vt
->g1enc_saved
;
501 vt
->cur_enc
= vt
->cur_enc_saved
;
505 spiceterm_set_alternate_buffer (spiceTerm
*vt
, int on_off
)
509 vt
->y_displ
= vt
->y_base
;
513 if (vt
->altbuf
) return;
517 /* alternate buffer & cursor */
519 spiceterm_save_cursor (vt
);
520 /* save screen to altcels */
521 for (y
= 0; y
< vt
->height
; y
++) {
522 int y1
= (vt
->y_base
+ y
) % vt
->total_height
;
523 for (x
= 0; x
< vt
->width
; x
++) {
524 vt
->altcells
[y
*vt
->width
+ x
] = vt
->cells
[y1
*vt
->width
+ x
];
529 for (y
= 0; y
<= vt
->height
; y
++) {
530 for (x
= 0; x
< vt
->width
; x
++) {
531 spiceterm_clear_xy (vt
, x
, y
);
537 if (vt
->altbuf
== 0) return;
541 /* restore saved data */
542 for (y
= 0; y
< vt
->height
; y
++) {
543 int y1
= (vt
->y_base
+ y
) % vt
->total_height
;
544 for (x
= 0; x
< vt
->width
; x
++) {
545 vt
->cells
[y1
*vt
->width
+ x
] = vt
->altcells
[y
*vt
->width
+ x
];
549 spiceterm_restore_cursor (vt
);
552 spiceterm_refresh (vt
);
556 spiceterm_set_mode (spiceTerm
*vt
, int on_off
)
560 for (i
= 0; i
<= vt
->esc_count
; i
++) {
561 if (vt
->esc_ques
) { /* DEC private modes set/reset */
562 switch(vt
->esc_buf
[i
]) {
563 case 10: /* X11 mouse reporting on/off */
565 vt
->report_mouse
= on_off
;
567 case 1049: /* start/end special app mode (smcup/rmcup) */
568 spiceterm_set_alternate_buffer (vt
, on_off
);
570 case 25: /* Cursor on/off */
571 case 9: /* X10 mouse reporting on/off */
572 case 6: /* Origin relative/absolute */
573 case 1: /* Cursor keys in appl mode*/
574 case 5: /* Inverted screen on/off */
575 case 7: /* Autowrap on/off */
576 case 8: /* Autorepeat on/off */
579 } else { /* ANSI modes set/reset */
580 /* fixme: implement me */
586 spiceterm_gotoxy (spiceTerm
*vt
, int x
, int y
)
588 /* verify all boundaries */
594 if (x
>= vt
->width
) {
604 if (y
>= vt
->height
) {
612 debug_print_escape_buffer(spiceTerm
*vt
, const char *func
, const char *prefix
,
613 const char *qes
, gunichar2 ch
)
616 if (vt
->esc_count
== 0) {
617 printf("%s:%s ESC[%s%c\n", func
, prefix
, qes
, ch
);
618 } else if (vt
->esc_count
== 1) {
619 printf("%s:%s ESC[%s%d%c\n", func
, prefix
, qes
, vt
->esc_buf
[0], ch
);
622 printf("%s:%s ESC[%s%d", func
, prefix
, qes
, vt
->esc_buf
[0]);
623 for (i
= 1; i
< vt
->esc_count
; i
++) {
624 printf(";%d", vt
->esc_buf
[i
]);
631 enum { ESnormal
, ESesc
, ESsquare
, ESgetpars
, ESgotpars
, ESfunckey
,
632 EShash
, ESsetG0
, ESsetG1
, ESpercent
, ESignore
, ESnonstd
,
633 ESpalette
, ESidquery
, ESosc1
, ESosc2
};
636 spiceterm_putchar (spiceTerm
*vt
, gunichar2 ch
)
640 if (debug
&& !vt
->tty_state
) {
641 DPRINTF(1, "CHAR:%2d: %4x '%c' (cur_enc %d) %d %d",
642 vt
->tty_state
, ch
, ch
, vt
->cur_enc
, vt
->cx
, vt
->cy
);
645 switch(vt
->tty_state
) {
647 vt
->tty_state
= ESnormal
;
650 vt
->tty_state
= ESsquare
;
653 vt
->tty_state
= ESnonstd
;
656 vt
->tty_state
= ESpercent
;
659 spiceterm_save_cursor (vt
);
662 spiceterm_restore_cursor (vt
);
665 vt
->tty_state
= ESsetG0
; // SET G0
668 vt
->tty_state
= ESsetG1
; // SET G1
672 if (vt
->cy
== vt
->region_top
)
673 spiceterm_scroll_down (vt
, vt
->region_top
, vt
->region_bottom
, 1);
674 else if (vt
->cy
> 0) {
679 /* numeric keypad - ignored */
682 /* appl. keypad - ignored */
685 DPRINTF(1, "got unhandled ESC%c %d", ch
, ch
);
689 case ESnonstd
: /* Operating System Controls */
690 vt
->tty_state
= ESnormal
;
693 case 'P': /* palette escape sequence */
694 for(i
= 0; i
< MAX_ESC_PARAMS
; i
++) {
699 vt
->tty_state
= ESpalette
;
701 case 'R': /* reset palette */
702 // fixme: reset_palette(vc);
709 vt
->osc_textbuf
[0] = 0;
710 vt
->tty_state
= ESosc1
;
713 DPRINTF(1, "got unhandled OSC %c", ch
);
714 vt
->tty_state
= ESnormal
;
719 vt
->tty_state
= ESnormal
;
721 vt
->tty_state
= ESosc2
;
723 DPRINTF(1, "got illegal OSC sequence");
727 if (ch
!= 0x9c && ch
!= 7) {
729 while (vt
->osc_textbuf
[i
]) i
++;
730 vt
->osc_textbuf
[i
++] = ch
;
731 vt
->osc_textbuf
[i
] = 0;
733 DPRINTF(1, "OSC:%c:%s", vt
->osc_cmd
, vt
->osc_textbuf
);
734 vt
->tty_state
= ESnormal
;
738 if ((ch
>= '0' && ch
<= '9') || (ch
>= 'A' && ch
<= 'F')
739 || (ch
>= 'a' && ch
<= 'f')) {
740 vt
->esc_buf
[vt
->esc_count
++] = (ch
> '9' ? (ch
& 0xDF) - 'A' + 10 : ch
- '0');
741 if (vt
->esc_count
== 7) {
742 // fixme: this does not work - please test
744 rfbColourMap *cmap =&vt->screen->colourMap;
746 int i = color_table[vt->esc_buf[0]] * 3, j = 1;
747 cmap->data.bytes[i] = 16 * vt->esc_buf[j++];
748 cmap->data.bytes[i++] += vt->esc_buf[j++];
749 cmap->data.bytes[i] = 16 * vt->esc_buf[j++];
750 cmap->data.bytes[i++] += vt->esc_buf[j++];
751 cmap->data.bytes[i] = 16 * vt->esc_buf[j++];
752 cmap->data.bytes[i] += vt->esc_buf[j];
756 vt
->tty_state
= ESnormal
;
759 vt
->tty_state
= ESnormal
;
762 for(i
= 0; i
< MAX_ESC_PARAMS
; i
++) {
768 vt
->tty_state
= ESgetpars
;
771 vt
->tty_state
= ESidquery
;
775 if ((vt
->esc_ques
= (ch
== '?'))) {
779 if (ch
>= '0' && ch
<= '9') {
781 if (vt
->esc_count
< MAX_ESC_PARAMS
) {
782 vt
->esc_buf
[vt
->esc_count
] = vt
->esc_buf
[vt
->esc_count
] * 10 + ch
- '0';
785 } else if (ch
== ';') {
789 if (vt
->esc_has_par
) {
792 vt
->tty_state
= ESgotpars
;
796 vt
->tty_state
= ESnormal
;
798 char *qes
= vt
->esc_ques
? "?" : "";
801 debug_print_escape_buffer(vt
, __func__
, "", qes
, ch
);
806 spiceterm_set_mode (vt
, 1);
809 spiceterm_set_mode (vt
, 0);
812 if (!vt
->esc_count
) {
813 vt
->esc_count
++; // default parameter 0
815 spiceterm_csi_m (vt
);
818 /* report cursor position */
819 /* TODO: send ESC[row;colR */
823 if (vt
->esc_buf
[0] == 0) {
826 vt
->cy
-= vt
->esc_buf
[0];
833 /* move cursor down */
834 if (vt
->esc_buf
[0] == 0) {
837 vt
->cy
+= vt
->esc_buf
[0];
838 if (vt
->cy
>= vt
->height
) {
839 vt
->cy
= vt
->height
- 1;
844 /* move cursor right */
845 if (vt
->esc_buf
[0] == 0) {
848 vt
->cx
+= vt
->esc_buf
[0];
849 if (vt
->cx
>= vt
->width
) {
850 vt
->cx
= vt
->width
- 1;
854 /* move cursor left */
855 if (vt
->esc_buf
[0] == 0) {
858 vt
->cx
-= vt
->esc_buf
[0];
865 /* move cursor to column */
866 spiceterm_gotoxy (vt
, vt
->esc_buf
[0] - 1, vt
->cy
);
869 /* move cursor to row */
870 spiceterm_gotoxy (vt
, vt
->cx
, vt
->esc_buf
[0] - 1);
874 /* move cursor to row, column */
875 spiceterm_gotoxy (vt
, vt
->esc_buf
[1] - 1, vt
->esc_buf
[0] - 1);
878 switch (vt
->esc_buf
[0]) {
880 /* clear to end of screen */
881 for (y
= vt
->cy
; y
< vt
->height
; y
++) {
882 for (x
= 0; x
< vt
->width
; x
++) {
883 if (y
== vt
->cy
&& x
< vt
->cx
) {
886 spiceterm_clear_xy (vt
, x
, y
);
891 /* clear from beginning of screen */
892 for (y
= 0; y
<= vt
->cy
; y
++) {
893 for (x
= 0; x
< vt
->width
; x
++) {
894 if (y
== vt
->cy
&& x
> vt
->cx
) {
897 spiceterm_clear_xy (vt
, x
, y
);
902 /* clear entire screen */
903 for (y
= 0; y
<= vt
->height
; y
++) {
904 for (x
= 0; x
< vt
->width
; x
++) {
905 spiceterm_clear_xy (vt
, x
, y
);
912 switch (vt
->esc_buf
[0]) {
915 for(x
= vt
->cx
; x
< vt
->width
; x
++) {
916 spiceterm_clear_xy (vt
, x
, vt
->cy
);
920 /* clear from beginning of line */
921 for (x
= 0; x
<= vt
->cx
; x
++) {
922 spiceterm_clear_xy (vt
, x
, vt
->cy
);
926 /* clear entire line */
927 for(x
= 0; x
< vt
->width
; x
++) {
928 spiceterm_clear_xy (vt
, x
, vt
->cy
);
937 if (c
> vt
->height
- vt
->cy
)
938 c
= vt
->height
- vt
->cy
;
942 spiceterm_scroll_down (vt
, vt
->cy
, vt
->region_bottom
, c
);
948 if (c
> vt
->height
- vt
->cy
)
949 c
= vt
->height
- vt
->cy
;
953 spiceterm_scroll_up (vt
, vt
->cy
, vt
->region_bottom
, c
, 1);
959 spiceterm_scroll_down (vt
, vt
->region_top
, vt
->region_bottom
, c
);
965 spiceterm_scroll_up (vt
, vt
->region_top
, vt
->region_bottom
, c
, 1);
968 /* delete c character */
971 if (c
> vt
->width
- vt
->cx
)
972 c
= vt
->width
- vt
->cx
;
976 for (x
= vt
->cx
; x
< vt
->width
- c
; x
++) {
977 int y1
= (vt
->y_base
+ vt
->cy
) % vt
->total_height
;
978 TextCell
*dst
= &vt
->cells
[y1
* vt
->width
+ x
];
979 TextCell
*src
= dst
+ c
;
981 spiceterm_update_xy (vt
, x
+ c
, vt
->cy
);
983 src
->attrib
= vt
->default_attrib
;
984 spiceterm_update_xy (vt
, x
, vt
->cy
);
988 /* save cursor position */
989 spiceterm_save_cursor (vt
);
992 /* restore cursor position */
993 spiceterm_restore_cursor (vt
);
996 /* erase c characters */
1000 if (c
> (vt
->width
- vt
->cx
)) c
= vt
->width
- vt
->cx
;
1002 for(i
= 0; i
< c
; i
++) {
1003 spiceterm_clear_xy (vt
, vt
->cx
+ i
, vt
->cy
);
1007 /* insert c character */
1009 if (c
> (vt
->width
- vt
->cx
)) {
1010 c
= vt
->width
- vt
->cx
;
1014 for (x
= vt
->width
- c
; x
>= vt
->cx
; x
--) {
1015 int y1
= (vt
->y_base
+ vt
->cy
) % vt
->total_height
;
1016 TextCell
*src
= &vt
->cells
[y1
* vt
->width
+ x
];
1017 TextCell
*dst
= src
+ c
;
1019 spiceterm_update_xy (vt
, x
+ c
, vt
->cy
);
1021 src
->attrib
= vt
->cur_attrib
;
1022 spiceterm_update_xy (vt
, x
, vt
->cy
);
1028 if (!vt
->esc_buf
[0])
1030 if (!vt
->esc_buf
[1])
1031 vt
->esc_buf
[1] = vt
->height
;
1032 /* Minimum allowed region is 2 lines */
1033 if (vt
->esc_buf
[0] < vt
->esc_buf
[1] &&
1034 vt
->esc_buf
[1] <= vt
->height
) {
1035 vt
->region_top
= vt
->esc_buf
[0] - 1;
1036 vt
->region_bottom
= vt
->esc_buf
[1];
1038 vt
->cy
= vt
->region_top
;
1039 DPRINTF(1, "set region %d %d", vt
->region_top
, vt
->region_bottom
);
1045 debug_print_escape_buffer(vt
, __func__
, " unhandled escape", qes
, ch
);
1051 case ESsetG0
: // Set G0
1052 vt
->tty_state
= ESnormal
;
1055 vt
->g0enc
= GRAF_MAP
;
1057 vt
->g0enc
= LAT1_MAP
;
1059 vt
->g0enc
= IBMPC_MAP
;
1061 vt
->g0enc
= USER_MAP
;
1063 if (vt
->charset
== 0)
1064 vt
->cur_enc
= vt
->g0enc
;
1067 case ESsetG1
: // Set G1
1068 vt
->tty_state
= ESnormal
;
1071 vt
->g1enc
= GRAF_MAP
;
1073 vt
->g1enc
= LAT1_MAP
;
1075 vt
->g1enc
= IBMPC_MAP
;
1077 vt
->g1enc
= USER_MAP
;
1079 if (vt
->charset
== 1)
1080 vt
->cur_enc
= vt
->g1enc
;
1083 case ESidquery
: // vt100 query id
1084 vt
->tty_state
= ESnormal
;
1087 DPRINTF(1, "ESC[>c Query term ID");
1088 spiceterm_respond_esc (vt
, TERMIDCODE
);
1092 vt
->tty_state
= ESnormal
;
1094 case '@': /* defined in ISO 2022 */
1097 case 'G': /* prelim official escape code */
1098 case '8': /* retained for compatibility */
1103 default: // ESnormal
1104 vt
->tty_state
= ESnormal
;
1109 case 7: /* alert aka. bell */
1111 //rfbSendBell(vt->screen);
1113 case 8: /* backspace */
1117 case 9: /* tabspace */
1118 if (vt
->cx
+ (8 - (vt
->cx
% 8)) > vt
->width
) {
1120 spiceterm_put_lf (vt
);
1122 vt
->cx
= vt
->cx
+ (8 - (vt
->cx
% 8));
1128 spiceterm_put_lf (vt
);
1130 case 13: /* carriage return */
1134 /* SI (shift in), select character set 1 */
1136 vt
->cur_enc
= vt
->g1enc
;
1137 /* fixme: display controls = 1 */
1140 /* SO (shift out), select character set 0 */
1142 vt
->cur_enc
= vt
->g0enc
;
1143 /* fixme: display controls = 0 */
1146 vt
->tty_state
= ESesc
;
1148 case 127: /* delete */
1151 case 128+27: /* csi */
1152 vt
->tty_state
= ESsquare
;
1155 if (vt
->cx
>= vt
->width
) {
1158 spiceterm_put_lf (vt
);
1161 int y1
= (vt
->y_base
+ vt
->cy
) % vt
->total_height
;
1162 TextCell
*c
= &vt
->cells
[y1
*vt
->width
+ vt
->cx
];
1163 c
->attrib
= vt
->cur_attrib
;
1165 spiceterm_update_xy (vt
, vt
->cx
, vt
->cy
);
1174 spiceterm_puts (spiceTerm
*vt
, const char *buf
, int len
)
1178 spiceterm_show_cursor (vt
, 0);
1181 unsigned char c
= *buf
;
1185 if (vt
->tty_state
!= ESnormal
) {
1186 // never translate escape sequence
1188 } else if (vt
->utf8
&& !vt
->cur_enc
) {
1190 if(c
& 0x80) { // utf8 multi-byte sequence
1192 if (vt
->utf_count
> 0 && (c
& 0xc0) == 0x80) {
1193 // inside UTF8 sequence
1194 vt
->utf_char
= (vt
->utf_char
<< 6) | (c
& 0x3f);
1196 if (vt
->utf_count
== 0) {
1202 // first char of a UTF8 sequence
1203 if ((c
& 0xe0) == 0xc0) {
1205 vt
->utf_char
= (c
& 0x1f);
1206 } else if ((c
& 0xf0) == 0xe0) {
1208 vt
->utf_char
= (c
& 0x0f);
1209 } else if ((c
& 0xf8) == 0xf0) {
1211 vt
->utf_char
= (c
& 0x07);
1212 } else if ((c
& 0xfc) == 0xf8) {
1214 vt
->utf_char
= (c
& 0x03);
1215 } else if ((c
& 0xfe) == 0xfc) {
1217 vt
->utf_char
= (c
& 0x01);
1230 // never translate controls
1231 if (c
>= 32 && c
!= 127 && c
!= (128+27)) {
1232 tc
= translations
[vt
->cur_enc
][c
& 0x0ff];
1238 spiceterm_putchar (vt
, tc
);
1241 spiceterm_show_cursor (vt
, 1);
1248 spiceterm_set_xcut_text (char* str, int len, struct _rfbClientRec* cl)
1250 spiceTerm *vt =(spiceTerm *)cl->screen->screenData;
1252 // seems str is Latin-1 encoded
1253 if (vt->selection) free (vt->selection);
1254 vt->selection = (gunichar2 *)malloc (len*sizeof (gunichar2));
1256 for (i = 0; i < len; i++) {
1257 vt->selection[i] = str[i] & 0xff;
1259 vt->selection_len = len;
1264 mouse_report (spiceTerm *vt, int butt, int mrx, int mry)
1268 sprintf (buf, "[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1271 spiceterm_respond_esc (vt, buf);
1276 spiceterm_toggle_marked_cell (spiceTerm
*vt
, int pos
)
1280 int x= (pos%vt->width)*8;
1281 int y= (pos/vt->width)*16;
1284 rfbScreenInfoPtr s=vt->screen;
1286 char *b = s->frameBuffer+y*s->width+x;
1288 for (j=0; j < 16; j++) {
1289 for(i=0; i < 8; i++) {
1290 b[j*s->width+i] ^= 0x0f;
1291 rfbMarkRectAsModified (s, x, y, x+8, y+16);
1300 spiceterm_pointer_event (int buttonMask, int x, int y, rfbClientPtr cl)
1303 spiceTerm *vt =(spiceTerm *)cl->screen->screenData;
1304 static int button2_released = 1;
1305 static int last_mask = 0;
1306 static int sel_start_pos = 0;
1307 static int sel_end_pos = 0;
1314 if (cx >= vt->width) cx = vt->width - 1;
1316 if (cy >= vt->height) cy = vt->height - 1;
1318 if (vt->report_mouse && buttonMask != last_mask) {
1319 last_mask = buttonMask;
1320 if (buttonMask & 1) {
1321 mouse_report (vt, 0, cx, cy);
1323 if (buttonMask & 2) {
1324 mouse_report (vt, 1, cx, cy);
1326 if (buttonMask & 4) {
1327 mouse_report (vt, 2, cx, cy);
1330 mouse_report (vt, 3, cx, cy);
1334 if (buttonMask & 2) {
1335 if(button2_released && vt->selection) {
1337 for(i = 0; i < vt->selection_len; i++) {
1338 if (vt->ibuf_count < IBUFSIZE - 6) { // uft8 is max 6 characters wide
1340 vt->ibuf_count += ucs2_to_utf8 (vt->selection[i], &vt->ibuf[vt->ibuf_count]);
1342 vt->ibuf[vt->ibuf_count++] = vt->selection[i];
1346 if (vt->y_displ != vt->y_base) {
1347 vt->y_displ = vt->y_base;
1348 spiceterm_refresh (vt);
1351 button2_released = 0;
1353 button2_released = 1;
1356 if (buttonMask & 1) {
1357 int pos = cy*vt->width + cx;
1359 // code borrowed from libvncserver (VNCconsole.c)
1361 if (!vt->mark_active) {
1363 vt->mark_active = 1;
1364 sel_start_pos = sel_end_pos = pos;
1365 spiceterm_toggle_marked_cell (vt, pos);
1369 if (pos != sel_end_pos) {
1371 if (pos > sel_end_pos) {
1372 cx = sel_end_pos; cy=pos;
1374 cx=pos; cy=sel_end_pos;
1377 if (cx < sel_start_pos) {
1378 if (cy < sel_start_pos) cy--;
1384 spiceterm_toggle_marked_cell (vt, cx);
1392 } else if (vt->mark_active) {
1393 vt->mark_active = 0;
1395 if (sel_start_pos > sel_end_pos) {
1396 int tmp = sel_start_pos - 1;
1397 sel_start_pos = sel_end_pos;
1401 int len = sel_end_pos - sel_start_pos + 1;
1403 if (vt->selection) free (vt->selection);
1404 vt->selection = (gunichar2 *)malloc (len*sizeof (gunichar2));
1405 vt->selection_len = len;
1406 char *sel_latin1 = (char *)malloc (len + 1);
1408 for (i = 0; i < len; i++) {
1409 int pos = sel_start_pos + i;
1410 int x = pos % vt->width;
1411 int y1 = ((pos / vt->width) + vt->y_displ) % vt->total_height;
1412 TextCell *c = &vt->cells[y1*vt->width + x];
1413 vt->selection[i] = c->ch;
1414 sel_latin1[i] = (char)c->ch;
1417 sel_latin1[len] = 0;
1418 rfbGotXCutText (vt->screen, sel_latin1, len);
1421 while (sel_start_pos <= sel_end_pos) {
1422 spiceterm_toggle_marked_cell (vt, sel_start_pos++);
1427 rfbDefaultPtrAddEvent (buttonMask, x, y, cl);
1433 my_kbd_push_key(SpiceKbdInstance
*sin
, uint8_t frag
)
1435 // spiceTerm *vt = SPICE_CONTAINEROF(sin, spiceTerm, keyboard_sin);
1437 /* we no not need this */
1443 my_kbd_push_keyval(SpiceKbdInstance
*sin
, uint32_t keySym
, int flags
)
1445 spiceTerm
*vt
= SPICE_CONTAINEROF(sin
, spiceTerm
, keyboard_sin
);
1446 static int control
= 0;
1447 static int shift
= 0;
1452 DPRINTF(1, "flags=%d keySym=%08x", flags
, keySym
);
1455 if (keySym
== GDK_KEY_Shift_L
|| keySym
== GDK_KEY_Shift_R
) {
1457 } if (keySym
== GDK_KEY_Control_L
|| keySym
== GDK_KEY_Control_R
) {
1459 } else if (vt
->ibuf_count
< (IBUFSIZE
- 32)) {
1462 if(keySym
>= 'a' && keySym
<= 'z')
1463 uc
= keySym
- 'a' + 1;
1464 else if (keySym
>= 'A' && keySym
<= 'Z')
1465 uc
= keySym
- 'A' + 1;
1471 case GDK_KEY_Escape
:
1473 case GDK_KEY_Return
:
1475 case GDK_KEY_BackSpace
:
1479 case GDK_KEY_Delete
: /* kdch1 */
1480 case GDK_KEY_KP_Delete
:
1482 case GDK_KEY_Home
: /* khome */
1483 case GDK_KEY_KP_Home
:
1486 case GDK_KEY_KP_End
: /* kend */
1488 case GDK_KEY_Insert
: /* kich1 */
1489 case GDK_KEY_KP_Insert
:
1492 case GDK_KEY_KP_Up
: /* kcuu1 */
1494 case GDK_KEY_Down
: /* kcud1 */
1495 case GDK_KEY_KP_Down
:
1498 case GDK_KEY_KP_Right
: /* kcuf1 */
1501 case GDK_KEY_KP_Left
: /* kcub1 */
1503 case GDK_KEY_Page_Up
:
1505 spiceterm_virtual_scroll (vt
, -vt
->height
/2);
1509 case GDK_KEY_Page_Down
:
1511 spiceterm_virtual_scroll (vt
, vt
->height
/2);
1540 if (keySym
< 0x100) {
1547 DPRINTF(1, "escape=%s unicode=%08x\n", esc
, uc
);
1549 if (vt
->y_displ
!= vt
->y_base
) {
1550 vt
->y_displ
= vt
->y_base
;
1551 spiceterm_refresh (vt
);
1555 spiceterm_respond_esc(vt
, esc
);
1556 } else if (uc
> 0) {
1559 gint len
= g_unichar_to_utf8(uc
, buf
);
1563 for (i
= 0; i
< len
; i
++) {
1564 vt
->ibuf
[vt
->ibuf_count
++] = buf
[i
];
1568 vt
->ibuf
[vt
->ibuf_count
++] = (char)uc
;
1577 if (flags
& 2) { // UP
1578 if (keySym
== GDK_KEY_Shift_L
|| keySym
== GDK_KEY_Shift_R
) {
1580 } else if (keySym
== GDK_KEY_Control_L
|| keySym
== GDK_KEY_Control_R
) {
1585 vt
->screen
->core
->watch_update_mask(vt
->screen
->mwatch
,
1586 SPICE_WATCH_EVENT_READ
|SPICE_WATCH_EVENT_WRITE
);
1590 my_kbd_get_leds(SpiceKbdInstance
*sin
)
1595 static SpiceKbdInterface my_keyboard_sif
= {
1596 .base
.type
= SPICE_INTERFACE_KEYBOARD
,
1597 .base
.description
= "spiceterm keyboard device",
1598 .base
.major_version
= SPICE_INTERFACE_KEYBOARD_MAJOR
,
1599 .base
.minor_version
= SPICE_INTERFACE_KEYBOARD_MINOR
,
1600 .push_keyval
= my_kbd_push_keyval
,
1601 .push_scan_freg
= my_kbd_push_key
,
1602 .get_leds
= my_kbd_get_leds
,
1606 create_spiceterm(int argc
, char** argv
, int maxx
, int maxy
)
1610 SpiceScreen
*spice_screen
;
1612 SpiceCoreInterface
*core
= basic_event_loop_init();
1613 spice_screen
= spice_screen_new(core
);
1614 //spice_server_set_image_compression(server, SPICE_IMAGE_COMPRESS_OFF);
1615 spice_screen_add_display_interface(spice_screen
);
1616 spice_screen_add_agent_interface(spice_screen
->server
);
1618 spiceTerm
*vt
= (spiceTerm
*)calloc (sizeof(spiceTerm
), 1);
1620 vt
->keyboard_sin
.base
.sif
= &my_keyboard_sif
.base
;
1621 spice_server_add_interface(spice_screen
->server
, &vt
->keyboard_sin
.base
);
1623 // screen->setXCutText = spiceterm_set_xcut_text;
1624 // screen->ptrAddEvent = spiceterm_pointer_event;
1625 // screen->newClientHook = new_client;
1626 // screen->desktopName = "SPICE Command Terminal";
1628 vt
->maxx
= spice_screen
->width
;
1629 vt
->maxy
= spice_screen
->height
;
1631 vt
->width
= vt
->maxx
/ 8;
1632 vt
->height
= vt
->maxy
/ 16;
1634 vt
->total_height
= vt
->height
* 20;
1635 vt
->scroll_height
= 0;
1640 vt
->region_bottom
= vt
->height
;
1642 vt
->g0enc
= LAT1_MAP
;
1643 vt
->g1enc
= GRAF_MAP
;
1644 vt
->cur_enc
= vt
->g0enc
;
1647 /* default text attributes */
1648 vt
->default_attrib
.bold
= 0;
1649 vt
->default_attrib
.uline
= 0;
1650 vt
->default_attrib
.blink
= 0;
1651 vt
->default_attrib
.invers
= 0;
1652 vt
->default_attrib
.unvisible
= 0;
1653 vt
->default_attrib
.fgcol
= 7;
1654 vt
->default_attrib
.bgcol
= 0;
1656 vt
->cur_attrib
= vt
->default_attrib
;
1658 vt
->cells
= (TextCell
*)calloc (sizeof (TextCell
), vt
->width
*vt
->total_height
);
1660 for (i
= 0; i
< vt
->width
*vt
->total_height
; i
++) {
1661 vt
->cells
[i
].ch
= ' ';
1662 vt
->cells
[i
].attrib
= vt
->default_attrib
;
1665 vt
->altcells
= (TextCell
*)calloc (sizeof (TextCell
), vt
->width
*vt
->height
);
1667 vt
->screen
= spice_screen
;
1673 master_watch(int master
, int event
, void *opaque
)
1675 spiceTerm
*vt
= (spiceTerm
*)opaque
;
1677 // fixme: if (!vt->mark_active) {
1679 if (event
== SPICE_WATCH_EVENT_READ
) {
1682 while ((c
= read(master
, buffer
, 1024)) == -1) {
1683 if (errno
!= EAGAIN
) break;
1686 g_error("got read error"); // fixme
1688 spiceterm_puts (vt
, buffer
, c
);
1690 if (vt
->ibuf_count
> 0) {
1691 DPRINTF(1, "write input %x %d", vt
->ibuf
[0], vt
->ibuf_count
);
1692 write (master
, vt
->ibuf
, vt
->ibuf_count
);
1693 vt
->ibuf_count
= 0; // fixme: what if not all data written
1695 vt
->screen
->core
->watch_update_mask(vt
->screen
->mwatch
, SPICE_WATCH_EVENT_READ
);
1700 main (int argc
, char** argv
)
1703 char **cmdargv
= NULL
;
1704 char *command
= "/bin/bash"; // execute normal shell as default
1708 struct winsize dimensions
;
1710 g_thread_init(NULL
);
1712 for (i
= 1; i
< argc
; i
++) {
1713 if (!strcmp (argv
[i
], "-c")) {
1714 command
= argv
[i
+1];
1715 cmdargv
= &argv
[i
+1];
1722 if (0) print_usage(NULL
); // fixme:
1724 spiceTerm
*vt
= create_spiceterm (argc
, argv
, 745, 400);
1726 setlocale(LC_ALL
, ""); // set from environment
1728 char *ctype
= setlocale (LC_CTYPE
, NULL
); // query LC_CTYPE
1730 // fixme: ist there a standard way to detect utf8 mode ?
1731 if (strcasestr (ctype
, ".utf-8")||strcasestr (ctype
, ".utf8")) {
1735 dimensions
.ws_col
= vt
->width
;
1736 dimensions
.ws_row
= vt
->height
;
1738 setenv ("TERM", TERM
, 1);
1740 DPRINTF(1, "execute %s", command
);
1742 pid
= forkpty (&master
, ptyname
, NULL
, &dimensions
);
1745 // install default signal handlers
1746 signal (SIGQUIT
, SIG_DFL
);
1747 signal (SIGTERM
, SIG_DFL
);
1748 signal (SIGINT
, SIG_DFL
);
1751 execvp (command
, cmdargv
);
1753 execlp (command
, command
, NULL
);
1755 perror ("Error: exec failed\n");
1756 exit (-1); // should not be reached
1757 } else if (pid
== -1) {
1758 perror ("Error: fork failed\n");
1763 vt
->screen
->mwatch
= vt
->screen
->core
->watch_add(
1764 master
, SPICE_WATCH_EVENT_READ
/* |SPICE_WATCH_EVENT_WRITE */,
1767 basic_event_loop_mainloop();
1769 //rfbProcessEvents (vt->screen, 40000); /* 40 ms */
1772 if (vt->ibuf_count > 0) {
1773 printf ("DEBUG: WRITE %d %d\n", vt->ibuf[0], vt->ibuf_count);
1774 write (master, vt->ibuf, vt->ibuf_count);
1776 last_time = time (NULL);
1782 waitpid(pid
, &status
, 0);