3 Copyright (C) 2007-2011 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>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <arpa/inet.h>
34 #include <pty.h> /* for openpty and forkpty */
37 #include <sys/ioctl.h>
42 #include "spiceterm.h"
46 #include <spice/enums.h>
47 #include <spice/macros.h>
48 #include <spice/qxl_dev.h>
50 #include <gdk/gdkkeysyms.h>
52 #include "event_loop.h"
53 #include "translations.h"
55 /* define this for debugging */
60 #define TERMIDCODE "[?1;2c" // vt100 ID
62 #define CHECK_ARGC(argc,argv,i) if (i >= argc-1) { \
63 fprintf (stderr, "ERROR: not enough arguments for: %s\n", argv[i]); \
68 /* these colours are from linux kernel drivers/char/vt.c */
70 unsigned char color_table
[] = { 0, 4, 2, 6, 1, 5, 3, 7,
71 8,12,10,14, 9,13,11,15 };
75 print_usage (const char *msg
)
77 if (msg
) { fprintf (stderr
, "ERROR: %s\n", msg
); }
78 fprintf (stderr
, "USAGE: spiceterm [spiceopts] [-c command [args]]\n");
81 /* Convert UCS2 to UTF8 sequence, trailing zero */
84 ucs2_to_utf8 (unicode c, char *out)
87 out[0] = c; // 0*******
90 } else if (c < 0x800) {
91 out[0] = 0xc0 | (c >> 6); // 110***** 10******
92 out[1] = 0x80 | (c & 0x3f);
96 out[0] = 0xe0 | (c >> 12); // 1110**** 10****** 10******
97 out[1] = 0x80 | ((c >> 6) & 0x3f);
98 out[2] = 0x80 | (c & 0x3f);
108 draw_char_at (spiceTerm
*vt
, int x
, int y
, unicode ch
, TextAttributes attrib
)
110 if (x
< 0 || y
< 0 || x
>= vt
->width
|| y
>= vt
->height
) {
114 spice_screen_draw_char(vt
->screen
, x
, y
, ch
, attrib
);
118 spiceterm_update_xy (spiceTerm
*vt
, int x
, int y
)
120 if (x
< 0 || y
< 0 || x
>= vt
->width
|| y
>= vt
->height
) { return; }
122 int y1
= (vt
->y_base
+ y
) % vt
->total_height
;
123 int y2
= y1
- vt
->y_displ
;
125 y2
+= vt
->total_height
;
127 if (y2
< vt
->height
) {
128 TextCell
*c
= &vt
->cells
[y1
* vt
->width
+ x
];
129 draw_char_at (vt
, x
, y2
, c
->ch
, c
->attrib
);
134 spiceterm_clear_xy (spiceTerm
*vt
, int x
, int y
)
136 if (x
< 0 || y
< 0 || x
>= vt
->width
|| y
>= vt
->height
) { return; }
138 int y1
= (vt
->y_base
+ y
) % vt
->total_height
;
139 int y2
= y1
- vt
->y_displ
;
141 y2
+= vt
->total_height
;
143 if (y2
< vt
->height
) {
144 TextCell
*c
= &vt
->cells
[y1
* vt
->width
+ x
];
146 c
->attrib
= vt
->default_attrib
;
147 c
->attrib
.fgcol
= vt
->cur_attrib
.fgcol
;
148 c
->attrib
.bgcol
= vt
->cur_attrib
.bgcol
;
150 draw_char_at (vt
, x
, y
, c
->ch
, c
->attrib
);
155 spiceterm_show_cursor (spiceTerm
*vt
, int show
)
158 if (x
>= vt
->width
) {
162 int y1
= (vt
->y_base
+ vt
->cy
) % vt
->total_height
;
163 int y
= y1
- vt
->y_displ
;
165 y
+= vt
->total_height
;
168 if (y
< vt
->height
) {
170 TextCell
*c
= &vt
->cells
[y1
* vt
->width
+ x
];
173 TextAttributes attrib
= vt
->default_attrib
;
174 attrib
.invers
= !(attrib
.invers
); /* invert fg and bg */
175 draw_char_at (vt
, x
, y
, c
->ch
, attrib
);
177 draw_char_at (vt
, x
, y
, c
->ch
, c
->attrib
);
183 spiceterm_refresh (spiceTerm
*vt
)
187 // rfbFillRect (vt->screen, 0, 0, vt->maxx, vt->maxy, vt->default_attrib.bgcol);
190 for(y
= 0; y
< vt
->height
; y
++) {
191 TextCell
*c
= vt
->cells
+ y1
* vt
->width
;
192 for(x
= 0; x
< vt
->width
; x
++) {
193 draw_char_at (vt
, x
, y
, c
->ch
, c
->attrib
);
196 if (++y1
== vt
->total_height
)
199 //rfbMarkRectAsModified (vt->screen, 0, 0, vt->maxx, vt->maxy);
201 spiceterm_show_cursor (vt
, 1);
205 spiceterm_scroll_down (spiceTerm
*vt
, int top
, int bottom
, int lines
)
207 if ((top
+ lines
) >= bottom
) {
208 lines
= bottom
- top
-1;
211 if (top
< 0 || bottom
> vt
->height
|| top
>= bottom
|| lines
< 1) {
216 for(i
= bottom
- top
- lines
- 1; i
>= 0; i
--) {
217 int src
= ((vt
->y_base
+ top
+ i
) % vt
->total_height
)*vt
->width
;
218 int dst
= ((vt
->y_base
+ top
+ lines
+ i
) % vt
->total_height
)*vt
->width
;
220 memmove(vt
->cells
+ dst
, vt
->cells
+ src
, vt
->width
*sizeof (TextCell
));
223 for (i
= 0; i
< lines
; i
++) {
225 TextCell
*c
= vt
->cells
+ ((vt
->y_base
+ top
+ i
) % vt
->total_height
)*vt
->width
;
226 for(j
= 0; j
< vt
->width
; j
++) {
227 c
->attrib
= vt
->default_attrib
;
238 spice_screen_scroll(vt
->screen
, 0, y1
, vt
->screen
->primary_width
, y2
, 0, y0
);
239 spice_screen_clear(vt
->screen
, 0, y0
, vt
->screen
->primary_width
, y1
);
243 spiceterm_scroll_up (spiceTerm
*vt
, int top
, int bottom
, int lines
, int moveattr
)
245 if ((top
+ lines
) >= bottom
) {
246 lines
= bottom
- top
- 1;
249 if (top
< 0 || bottom
> vt
->height
|| top
>= bottom
|| lines
< 1) {
256 int y1
= (top
+ lines
)*16;
259 spice_screen_scroll(vt
->screen
, 0, y0
, vt
->screen
->primary_width
, y2
-h
, 0, y1
);
260 spice_screen_clear(vt
->screen
, 0, y2
-h
, vt
->screen
->primary_width
, y2
);
269 for(i
= 0; i
< (bottom
- top
- lines
); i
++) {
270 int dst
= ((vt
->y_base
+ top
+ i
) % vt
->total_height
)*vt
->width
;
271 int src
= ((vt
->y_base
+ top
+ lines
+ i
) % vt
->total_height
)*vt
->width
;
273 memmove(vt
->cells
+ dst
, vt
->cells
+ src
, vt
->width
*sizeof (TextCell
));
276 for (i
= 1; i
<= lines
; i
++) {
278 TextCell
*c
= vt
->cells
+ ((vt
->y_base
+ bottom
- i
) % vt
->total_height
)*vt
->width
;
279 for(j
= 0; j
< vt
->width
; j
++) {
280 c
->attrib
= vt
->default_attrib
;
288 spiceterm_virtual_scroll (spiceTerm
*vt
, int lines
)
290 if (vt
->altbuf
|| lines
== 0) return;
294 int i
= vt
->scroll_height
;
295 if (i
> vt
->total_height
- vt
->height
)
296 i
= vt
->total_height
- vt
->height
;
297 int y1
= vt
->y_base
- i
;
299 y1
+= vt
->total_height
;
300 for(i
= 0; i
< lines
; i
++) {
301 if (vt
->y_displ
== y1
) break;
302 if (--vt
->y_displ
< 0) {
303 vt
->y_displ
= vt
->total_height
- 1;
308 for(i
= 0; i
< lines
; i
++) {
309 if (vt
->y_displ
== vt
->y_base
) break;
310 if (++vt
->y_displ
== vt
->total_height
) {
317 spiceterm_refresh (vt
);
320 spiceterm_respond_esc (spiceTerm
*vt
, const char *esc
)
322 int len
= strlen (esc
);
325 if (vt
->ibuf_count
< (IBUFSIZE
- 1 - len
)) {
326 vt
->ibuf
[vt
->ibuf_count
++] = 27;
327 for (i
= 0; i
< len
; i
++) {
328 vt
->ibuf
[vt
->ibuf_count
++] = esc
[i
];
334 spiceterm_put_lf (spiceTerm
*vt
)
336 if (vt
->cy
+ 1 == vt
->region_bottom
) {
338 if (vt
->altbuf
|| vt
->region_top
!= 0 || vt
->region_bottom
!= vt
->height
) {
339 spiceterm_scroll_up (vt
, vt
->region_top
, vt
->region_bottom
, 1, 1);
343 if (vt
->y_displ
== vt
->y_base
) {
344 spiceterm_scroll_up (vt
, vt
->region_top
, vt
->region_bottom
, 1, 0);
347 if (vt
->y_displ
== vt
->y_base
) {
348 if (++vt
->y_displ
== vt
->total_height
) {
353 if (++vt
->y_base
== vt
->total_height
) {
357 if (vt
->scroll_height
< vt
->total_height
) {
361 int y1
= (vt
->y_base
+ vt
->height
- 1) % vt
->total_height
;
362 TextCell
*c
= &vt
->cells
[y1
* vt
->width
];
364 for (x
= 0; x
< vt
->width
; x
++) {
366 c
->attrib
= vt
->default_attrib
;
370 // fprintf (stderr, "BASE: %d DISPLAY %d\n", vt->y_base, vt->y_displ);
372 } else if (vt
->cy
< vt
->height
- 1) {
379 spiceterm_csi_m (spiceTerm
*vt
)
383 for (i
= 0; i
< vt
->esc_count
; i
++) {
384 switch (vt
->esc_buf
[i
]) {
385 case 0: /* reset all console attributes to default */
386 vt
->cur_attrib
= vt
->default_attrib
;
389 vt
->cur_attrib
.bold
= 1;
392 vt
->cur_attrib
.uline
= 1;
395 vt
->cur_attrib
.blink
= 1;
398 vt
->cur_attrib
.invers
= 1;
401 vt
->cur_attrib
.unvisible
= 1;
404 vt
->cur_enc
= LAT1_MAP
;
405 // fixme: dispaly controls = 0 ?
406 // fixme: toggle meta = 0 ?
409 vt
->cur_enc
= IBMPC_MAP
;
410 // fixme: dispaly controls = 1 ?
411 // fixme: toggle meta = 0 ?
414 vt
->cur_enc
= IBMPC_MAP
;
415 // fixme: dispaly controls = 1 ?
416 // fixme: toggle meta = 1 ?
419 vt
->cur_attrib
.bold
= 0;
422 vt
->cur_attrib
.uline
= 0;
425 vt
->cur_attrib
.blink
= 0;
428 vt
->cur_attrib
.invers
= 0;
431 vt
->cur_attrib
.unvisible
= 0;
441 /* set foreground color */
442 vt
->cur_attrib
.fgcol
= color_table
[vt
->esc_buf
[i
] - 30];
445 /* reset color to default, enable underline */
446 vt
->cur_attrib
.fgcol
= vt
->default_attrib
.fgcol
;
447 vt
->cur_attrib
.uline
= 1;
450 /* reset color to default, disable underline */
451 vt
->cur_attrib
.fgcol
= vt
->default_attrib
.fgcol
;
452 vt
->cur_attrib
.uline
= 0;
462 /* set background color */
463 vt
->cur_attrib
.bgcol
= color_table
[vt
->esc_buf
[i
] - 40];
466 /* reset background color */
467 vt
->cur_attrib
.bgcol
= vt
->default_attrib
.bgcol
;
470 fprintf (stderr
, "unhandled ESC[%d m code\n",vt
->esc_buf
[i
]);
477 spiceterm_save_cursor (spiceTerm
*vt
)
479 vt
->cx_saved
= vt
->cx
;
480 vt
->cy_saved
= vt
->cy
;
481 vt
->cur_attrib_saved
= vt
->cur_attrib
;
482 vt
->charset_saved
= vt
->charset
;
483 vt
->g0enc_saved
= vt
->g0enc
;
484 vt
->g1enc_saved
= vt
->g1enc
;
485 vt
->cur_enc_saved
= vt
->cur_enc
;
489 spiceterm_restore_cursor (spiceTerm
*vt
)
491 vt
->cx
= vt
->cx_saved
;
492 vt
->cy
= vt
->cy_saved
;
493 vt
->cur_attrib
= vt
->cur_attrib_saved
;
494 vt
->charset
= vt
->charset_saved
;
495 vt
->g0enc
= vt
->g0enc_saved
;
496 vt
->g1enc
= vt
->g1enc_saved
;
497 vt
->cur_enc
= vt
->cur_enc_saved
;
501 spiceterm_set_alternate_buffer (spiceTerm
*vt
, int on_off
)
505 vt
->y_displ
= vt
->y_base
;
509 if (vt
->altbuf
) return;
513 /* alternate buffer & cursor */
515 spiceterm_save_cursor (vt
);
516 /* save screen to altcels */
517 for (y
= 0; y
< vt
->height
; y
++) {
518 int y1
= (vt
->y_base
+ y
) % vt
->total_height
;
519 for (x
= 0; x
< vt
->width
; x
++) {
520 vt
->altcells
[y
*vt
->width
+ x
] = vt
->cells
[y1
*vt
->width
+ x
];
525 for (y
= 0; y
<= vt
->height
; y
++) {
526 for (x
= 0; x
< vt
->width
; x
++) {
527 spiceterm_clear_xy (vt
, x
, y
);
533 if (vt
->altbuf
== 0) return;
537 /* restore saved data */
538 for (y
= 0; y
< vt
->height
; y
++) {
539 int y1
= (vt
->y_base
+ y
) % vt
->total_height
;
540 for (x
= 0; x
< vt
->width
; x
++) {
541 vt
->cells
[y1
*vt
->width
+ x
] = vt
->altcells
[y
*vt
->width
+ x
];
545 spiceterm_restore_cursor (vt
);
548 spiceterm_refresh (vt
);
552 spiceterm_set_mode (spiceTerm
*vt
, int on_off
)
556 for (i
= 0; i
<= vt
->esc_count
; i
++) {
557 if (vt
->esc_ques
) { /* DEC private modes set/reset */
558 switch(vt
->esc_buf
[i
]) {
559 case 10: /* X11 mouse reporting on/off */
561 vt
->report_mouse
= on_off
;
563 case 1049: /* start/end special app mode (smcup/rmcup) */
564 spiceterm_set_alternate_buffer (vt
, on_off
);
566 case 25: /* Cursor on/off */
567 case 9: /* X10 mouse reporting on/off */
568 case 6: /* Origin relative/absolute */
569 case 1: /* Cursor keys in appl mode*/
570 case 5: /* Inverted screen on/off */
571 case 7: /* Autowrap on/off */
572 case 8: /* Autorepeat on/off */
575 } else { /* ANSI modes set/reset */
576 /* fixme: implement me */
582 spiceterm_gotoxy (spiceTerm
*vt
, int x
, int y
)
584 /* verify all boundaries */
590 if (x
>= vt
->width
) {
600 if (y
>= vt
->height
) {
607 enum { ESnormal
, ESesc
, ESsquare
, ESgetpars
, ESgotpars
, ESfunckey
,
608 EShash
, ESsetG0
, ESsetG1
, ESpercent
, ESignore
, ESnonstd
,
609 ESpalette
, ESidquery
, ESosc1
, ESosc2
};
612 spiceterm_putchar (spiceTerm
*vt
, unicode ch
)
618 fprintf (stderr
, "CHAR:%2d: %4x '%c' (cur_enc %d) %d %d\n", vt
->tty_state
, ch
, ch
, vt
->cur_enc
, vt
->cx
, vt
->cy
);
621 switch(vt
->tty_state
) {
623 vt
->tty_state
= ESnormal
;
626 vt
->tty_state
= ESsquare
;
629 vt
->tty_state
= ESnonstd
;
632 vt
->tty_state
= ESpercent
;
635 spiceterm_save_cursor (vt
);
638 spiceterm_restore_cursor (vt
);
641 vt
->tty_state
= ESsetG0
; // SET G0
644 vt
->tty_state
= ESsetG1
; // SET G1
648 if (vt
->cy
== vt
->region_top
)
649 spiceterm_scroll_down (vt
, vt
->region_top
, vt
->region_bottom
, 1);
650 else if (vt
->cy
> 0) {
655 /* numeric keypad - ignored */
658 /* appl. keypad - ignored */
662 fprintf(stderr
, "got unhandled ESC%c %d\n", ch
, ch
);
667 case ESnonstd
: /* Operating System Controls */
668 vt
->tty_state
= ESnormal
;
671 case 'P': /* palette escape sequence */
672 for(i
= 0; i
< MAX_ESC_PARAMS
; i
++) {
677 vt
->tty_state
= ESpalette
;
679 case 'R': /* reset palette */
680 // fixme: reset_palette(vc);
687 vt
->osc_textbuf
[0] = 0;
688 vt
->tty_state
= ESosc1
;
692 fprintf (stderr
, "unhandled OSC %c\n", ch
);
694 vt
->tty_state
= ESnormal
;
699 vt
->tty_state
= ESnormal
;
701 vt
->tty_state
= ESosc2
;
704 fprintf (stderr
, "got illegal OSC sequence\n");
709 if (ch
!= 0x9c && ch
!= 7) {
711 while (vt
->osc_textbuf
[i
]) i
++;
712 vt
->osc_textbuf
[i
++] = ch
;
713 vt
->osc_textbuf
[i
] = 0;
716 fprintf (stderr
, "OSC:%c:%s\n", vt
->osc_cmd
, vt
->osc_textbuf
);
718 vt
->tty_state
= ESnormal
;
722 if ((ch
>= '0' && ch
<= '9') || (ch
>= 'A' && ch
<= 'F')
723 || (ch
>= 'a' && ch
<= 'f')) {
724 vt
->esc_buf
[vt
->esc_count
++] = (ch
> '9' ? (ch
& 0xDF) - 'A' + 10 : ch
- '0');
725 if (vt
->esc_count
== 7) {
726 // fixme: this does not work - please test
728 rfbColourMap *cmap =&vt->screen->colourMap;
730 int i = color_table[vt->esc_buf[0]] * 3, j = 1;
731 cmap->data.bytes[i] = 16 * vt->esc_buf[j++];
732 cmap->data.bytes[i++] += vt->esc_buf[j++];
733 cmap->data.bytes[i] = 16 * vt->esc_buf[j++];
734 cmap->data.bytes[i++] += vt->esc_buf[j++];
735 cmap->data.bytes[i] = 16 * vt->esc_buf[j++];
736 cmap->data.bytes[i] += vt->esc_buf[j];
740 vt
->tty_state
= ESnormal
;
743 vt
->tty_state
= ESnormal
;
746 for(i
= 0; i
< MAX_ESC_PARAMS
; i
++) {
752 vt
->tty_state
= ESgetpars
;
755 vt
->tty_state
= ESidquery
;
759 if ((vt
->esc_ques
= (ch
== '?'))) {
763 if (ch
>= '0' && ch
<= '9') {
765 if (vt
->esc_count
< MAX_ESC_PARAMS
) {
766 vt
->esc_buf
[vt
->esc_count
] = vt
->esc_buf
[vt
->esc_count
] * 10 + ch
- '0';
769 } else if (ch
== ';') {
773 if (vt
->esc_has_par
) {
776 vt
->tty_state
= ESgotpars
;
780 vt
->tty_state
= ESnormal
;
783 char *qes
= vt
->esc_ques
? "?" : "";
784 if (vt
->esc_count
== 0) {
785 fprintf(stderr
, "ESC[%s%c\n", qes
, ch
);
786 } else if (vt
->esc_count
== 1) {
787 fprintf(stderr
, "ESC[%s%d%c\n", qes
, vt
->esc_buf
[0], ch
);
790 fprintf(stderr
, "ESC[%s%d", qes
, vt
->esc_buf
[0]);
791 for (i
= 1; i
< vt
->esc_count
; i
++) {
792 fprintf(stderr
, ";%d", vt
->esc_buf
[i
]);
794 fprintf (stderr
, "%c\n", ch
);
800 spiceterm_set_mode (vt
, 1);
803 spiceterm_set_mode (vt
, 0);
806 if (!vt
->esc_count
) {
807 vt
->esc_count
++; // default parameter 0
809 spiceterm_csi_m (vt
);
812 /* report cursor position */
813 /* TODO: send ESC[row;colR */
817 if (vt
->esc_buf
[0] == 0) {
820 vt
->cy
-= vt
->esc_buf
[0];
827 /* move cursor down */
828 if (vt
->esc_buf
[0] == 0) {
831 vt
->cy
+= vt
->esc_buf
[0];
832 if (vt
->cy
>= vt
->height
) {
833 vt
->cy
= vt
->height
- 1;
838 /* move cursor right */
839 if (vt
->esc_buf
[0] == 0) {
842 vt
->cx
+= vt
->esc_buf
[0];
843 if (vt
->cx
>= vt
->width
) {
844 vt
->cx
= vt
->width
- 1;
848 /* move cursor left */
849 if (vt
->esc_buf
[0] == 0) {
852 vt
->cx
-= vt
->esc_buf
[0];
859 /* move cursor to column */
860 spiceterm_gotoxy (vt
, vt
->esc_buf
[0] - 1, vt
->cy
);
863 /* move cursor to row */
864 spiceterm_gotoxy (vt
, vt
->cx
, vt
->esc_buf
[0] - 1);
868 /* move cursor to row, column */
869 spiceterm_gotoxy (vt
, vt
->esc_buf
[1] - 1, vt
->esc_buf
[0] - 1);
872 switch (vt
->esc_buf
[0]) {
874 /* clear to end of screen */
875 for (y
= vt
->cy
; y
< vt
->height
; y
++) {
876 for (x
= 0; x
< vt
->width
; x
++) {
877 if (y
== vt
->cy
&& x
< vt
->cx
) {
880 spiceterm_clear_xy (vt
, x
, y
);
885 /* clear from beginning of screen */
886 for (y
= 0; y
<= vt
->cy
; y
++) {
887 for (x
= 0; x
< vt
->width
; x
++) {
888 if (y
== vt
->cy
&& x
> vt
->cx
) {
891 spiceterm_clear_xy (vt
, x
, y
);
896 /* clear entire screen */
897 for (y
= 0; y
<= vt
->height
; y
++) {
898 for (x
= 0; x
< vt
->width
; x
++) {
899 spiceterm_clear_xy (vt
, x
, y
);
906 switch (vt
->esc_buf
[0]) {
909 for(x
= vt
->cx
; x
< vt
->width
; x
++) {
910 spiceterm_clear_xy (vt
, x
, vt
->cy
);
914 /* clear from beginning of line */
915 for (x
= 0; x
<= vt
->cx
; x
++) {
916 spiceterm_clear_xy (vt
, x
, vt
->cy
);
920 /* clear entire line */
921 for(x
= 0; x
< vt
->width
; x
++) {
922 spiceterm_clear_xy (vt
, x
, vt
->cy
);
931 if (c
> vt
->height
- vt
->cy
)
932 c
= vt
->height
- vt
->cy
;
936 spiceterm_scroll_down (vt
, vt
->cy
, vt
->region_bottom
, c
);
942 if (c
> vt
->height
- vt
->cy
)
943 c
= vt
->height
- vt
->cy
;
947 spiceterm_scroll_up (vt
, vt
->cy
, vt
->region_bottom
, c
, 1);
953 spiceterm_scroll_down (vt
, vt
->region_top
, vt
->region_bottom
, c
);
959 spiceterm_scroll_up (vt
, vt
->region_top
, vt
->region_bottom
, c
, 1);
962 /* delete c character */
965 if (c
> vt
->width
- vt
->cx
)
966 c
= vt
->width
- vt
->cx
;
970 for (x
= vt
->cx
; x
< vt
->width
- c
; x
++) {
971 int y1
= (vt
->y_base
+ vt
->cy
) % vt
->total_height
;
972 TextCell
*dst
= &vt
->cells
[y1
* vt
->width
+ x
];
973 TextCell
*src
= dst
+ c
;
975 spiceterm_update_xy (vt
, x
+ c
, vt
->cy
);
977 src
->attrib
= vt
->default_attrib
;
978 spiceterm_update_xy (vt
, x
, vt
->cy
);
982 /* save cursor position */
983 spiceterm_save_cursor (vt
);
986 /* restore cursor position */
987 spiceterm_restore_cursor (vt
);
990 /* erase c characters */
994 if (c
> (vt
->width
- vt
->cx
)) c
= vt
->width
- vt
->cx
;
996 for(i
= 0; i
< c
; i
++) {
997 spiceterm_clear_xy (vt
, vt
->cx
+ i
, vt
->cy
);
1001 /* insert c character */
1003 if (c
> (vt
->width
- vt
->cx
)) {
1004 c
= vt
->width
- vt
->cx
;
1008 for (x
= vt
->width
- c
; x
>= vt
->cx
; x
--) {
1009 int y1
= (vt
->y_base
+ vt
->cy
) % vt
->total_height
;
1010 TextCell
*src
= &vt
->cells
[y1
* vt
->width
+ x
];
1011 TextCell
*dst
= src
+ c
;
1013 spiceterm_update_xy (vt
, x
+ c
, vt
->cy
);
1015 src
->attrib
= vt
->cur_attrib
;
1016 spiceterm_update_xy (vt
, x
, vt
->cy
);
1022 if (!vt
->esc_buf
[0])
1024 if (!vt
->esc_buf
[1])
1025 vt
->esc_buf
[1] = vt
->height
;
1026 /* Minimum allowed region is 2 lines */
1027 if (vt
->esc_buf
[0] < vt
->esc_buf
[1] &&
1028 vt
->esc_buf
[1] <= vt
->height
) {
1029 vt
->region_top
= vt
->esc_buf
[0] - 1;
1030 vt
->region_bottom
= vt
->esc_buf
[1];
1032 vt
->cy
= vt
->region_top
;
1034 fprintf (stderr
, "set region %d %d\n", vt
->region_top
, vt
->region_bottom
);
1041 if (vt
->esc_count
== 0) {
1042 fprintf(stderr
, "unhandled escape ESC[%s%c\n", qes
, ch
);
1043 } else if (vt
->esc_count
== 1) {
1044 fprintf(stderr
, "unhandled escape ESC[%s%d%c\n", qes
, vt
->esc_buf
[0], ch
);
1047 fprintf(stderr
, "unhandled escape ESC[%s%d", qes
, vt
->esc_buf
[0]);
1048 for (i
= 1; i
< vt
->esc_count
; i
++) {
1049 fprintf(stderr
, ";%d", vt
->esc_buf
[i
]);
1051 fprintf (stderr
, "%c\n", ch
);
1058 case ESsetG0
: // Set G0
1059 vt
->tty_state
= ESnormal
;
1062 vt
->g0enc
= GRAF_MAP
;
1064 vt
->g0enc
= LAT1_MAP
;
1066 vt
->g0enc
= IBMPC_MAP
;
1068 vt
->g0enc
= USER_MAP
;
1070 if (vt
->charset
== 0)
1071 vt
->cur_enc
= vt
->g0enc
;
1074 case ESsetG1
: // Set G1
1075 vt
->tty_state
= ESnormal
;
1078 vt
->g1enc
= GRAF_MAP
;
1080 vt
->g1enc
= LAT1_MAP
;
1082 vt
->g1enc
= IBMPC_MAP
;
1084 vt
->g1enc
= USER_MAP
;
1086 if (vt
->charset
== 1)
1087 vt
->cur_enc
= vt
->g1enc
;
1090 case ESidquery
: // vt100 query id
1091 vt
->tty_state
= ESnormal
;
1095 fprintf (stderr
, "ESC[>c Query term ID\n");
1097 spiceterm_respond_esc (vt
, TERMIDCODE
);
1101 vt
->tty_state
= ESnormal
;
1103 case '@': /* defined in ISO 2022 */
1106 case 'G': /* prelim official escape code */
1107 case '8': /* retained for compatibility */
1112 default: // ESnormal
1113 vt
->tty_state
= ESnormal
;
1118 case 7: /* alert aka. bell */
1120 //rfbSendBell(vt->screen);
1122 case 8: /* backspace */
1126 case 9: /* tabspace */
1127 if (vt
->cx
+ (8 - (vt
->cx
% 8)) > vt
->width
) {
1129 spiceterm_put_lf (vt
);
1131 vt
->cx
= vt
->cx
+ (8 - (vt
->cx
% 8));
1137 spiceterm_put_lf (vt
);
1139 case 13: /* carriage return */
1143 /* SI (shift in), select character set 1 */
1145 vt
->cur_enc
= vt
->g1enc
;
1146 /* fixme: display controls = 1 */
1149 /* SO (shift out), select character set 0 */
1151 vt
->cur_enc
= vt
->g0enc
;
1152 /* fixme: display controls = 0 */
1155 vt
->tty_state
= ESesc
;
1157 case 127: /* delete */
1160 case 128+27: /* csi */
1161 vt
->tty_state
= ESsquare
;
1164 if (vt
->cx
>= vt
->width
) {
1167 spiceterm_put_lf (vt
);
1170 int y1
= (vt
->y_base
+ vt
->cy
) % vt
->total_height
;
1171 TextCell
*c
= &vt
->cells
[y1
*vt
->width
+ vt
->cx
];
1172 c
->attrib
= vt
->cur_attrib
;
1174 spiceterm_update_xy (vt
, vt
->cx
, vt
->cy
);
1183 spiceterm_puts (spiceTerm
*vt
, const char *buf
, int len
)
1187 spiceterm_show_cursor (vt
, 0);
1190 unsigned char c
= *buf
;
1194 if (vt
->tty_state
!= ESnormal
) {
1195 // never translate escape sequence
1197 } else if (vt
->utf8
&& !vt
->cur_enc
) {
1199 if(c
& 0x80) { // utf8 multi-byte sequence
1201 if (vt
->utf_count
> 0 && (c
& 0xc0) == 0x80) {
1202 // inside UTF8 sequence
1203 vt
->utf_char
= (vt
->utf_char
<< 6) | (c
& 0x3f);
1205 if (vt
->utf_count
== 0) {
1211 // first char of a UTF8 sequence
1212 if ((c
& 0xe0) == 0xc0) {
1214 vt
->utf_char
= (c
& 0x1f);
1215 } else if ((c
& 0xf0) == 0xe0) {
1217 vt
->utf_char
= (c
& 0x0f);
1218 } else if ((c
& 0xf8) == 0xf0) {
1220 vt
->utf_char
= (c
& 0x07);
1221 } else if ((c
& 0xfc) == 0xf8) {
1223 vt
->utf_char
= (c
& 0x03);
1224 } else if ((c
& 0xfe) == 0xfc) {
1226 vt
->utf_char
= (c
& 0x01);
1239 // never translate controls
1240 if (c
>= 32 && c
!= 127 && c
!= (128+27)) {
1241 tc
= translations
[vt
->cur_enc
][c
& 0x0ff];
1247 spiceterm_putchar (vt
, tc
);
1250 spiceterm_show_cursor (vt
, 1);
1257 spiceterm_set_xcut_text (char* str, int len, struct _rfbClientRec* cl)
1259 spiceTerm *vt =(spiceTerm *)cl->screen->screenData;
1261 // seems str is Latin-1 encoded
1262 if (vt->selection) free (vt->selection);
1263 vt->selection = (unicode *)malloc (len*sizeof (unicode));
1265 for (i = 0; i < len; i++) {
1266 vt->selection[i] = str[i] & 0xff;
1268 vt->selection_len = len;
1273 mouse_report (spiceTerm *vt, int butt, int mrx, int mry)
1277 sprintf (buf, "[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1280 spiceterm_respond_esc (vt, buf);
1285 spiceterm_toggle_marked_cell (spiceTerm
*vt
, int pos
)
1289 int x= (pos%vt->width)*8;
1290 int y= (pos/vt->width)*16;
1293 rfbScreenInfoPtr s=vt->screen;
1295 char *b = s->frameBuffer+y*s->width+x;
1297 for (j=0; j < 16; j++) {
1298 for(i=0; i < 8; i++) {
1299 b[j*s->width+i] ^= 0x0f;
1300 rfbMarkRectAsModified (s, x, y, x+8, y+16);
1309 spiceterm_pointer_event (int buttonMask, int x, int y, rfbClientPtr cl)
1312 spiceTerm *vt =(spiceTerm *)cl->screen->screenData;
1313 static int button2_released = 1;
1314 static int last_mask = 0;
1315 static int sel_start_pos = 0;
1316 static int sel_end_pos = 0;
1323 if (cx >= vt->width) cx = vt->width - 1;
1325 if (cy >= vt->height) cy = vt->height - 1;
1327 if (vt->report_mouse && buttonMask != last_mask) {
1328 last_mask = buttonMask;
1329 if (buttonMask & 1) {
1330 mouse_report (vt, 0, cx, cy);
1332 if (buttonMask & 2) {
1333 mouse_report (vt, 1, cx, cy);
1335 if (buttonMask & 4) {
1336 mouse_report (vt, 2, cx, cy);
1339 mouse_report (vt, 3, cx, cy);
1343 if (buttonMask & 2) {
1344 if(button2_released && vt->selection) {
1346 for(i = 0; i < vt->selection_len; i++) {
1347 if (vt->ibuf_count < IBUFSIZE - 6) { // uft8 is max 6 characters wide
1349 vt->ibuf_count += ucs2_to_utf8 (vt->selection[i], &vt->ibuf[vt->ibuf_count]);
1351 vt->ibuf[vt->ibuf_count++] = vt->selection[i];
1355 if (vt->y_displ != vt->y_base) {
1356 vt->y_displ = vt->y_base;
1357 spiceterm_refresh (vt);
1360 button2_released = 0;
1362 button2_released = 1;
1365 if (buttonMask & 1) {
1366 int pos = cy*vt->width + cx;
1368 // code borrowed from libspiceserver (SPICEonsole.c)
1370 if (!vt->mark_active) {
1372 vt->mark_active = 1;
1373 sel_start_pos = sel_end_pos = pos;
1374 spiceterm_toggle_marked_cell (vt, pos);
1378 if (pos != sel_end_pos) {
1380 if (pos > sel_end_pos) {
1381 cx = sel_end_pos; cy=pos;
1383 cx=pos; cy=sel_end_pos;
1386 if (cx < sel_start_pos) {
1387 if (cy < sel_start_pos) cy--;
1393 spiceterm_toggle_marked_cell (vt, cx);
1401 } else if (vt->mark_active) {
1402 vt->mark_active = 0;
1404 if (sel_start_pos > sel_end_pos) {
1405 int tmp = sel_start_pos - 1;
1406 sel_start_pos = sel_end_pos;
1410 int len = sel_end_pos - sel_start_pos + 1;
1412 if (vt->selection) free (vt->selection);
1413 vt->selection = (unicode *)malloc (len*sizeof (unicode));
1414 vt->selection_len = len;
1415 char *sel_latin1 = (char *)malloc (len + 1);
1417 for (i = 0; i < len; i++) {
1418 int pos = sel_start_pos + i;
1419 int x = pos % vt->width;
1420 int y1 = ((pos / vt->width) + vt->y_displ) % vt->total_height;
1421 TextCell *c = &vt->cells[y1*vt->width + x];
1422 vt->selection[i] = c->ch;
1423 sel_latin1[i] = (char)c->ch;
1426 sel_latin1[len] = 0;
1427 rfbGotXCutText (vt->screen, sel_latin1, len);
1430 while (sel_start_pos <= sel_end_pos) {
1431 spiceterm_toggle_marked_cell (vt, sel_start_pos++);
1436 rfbDefaultPtrAddEvent (buttonMask, x, y, cl);
1441 static void my_kbd_push_key(SpiceKbdInstance
*sin
, uint8_t frag
)
1443 // spiceTerm *vt = SPICE_CONTAINEROF(sin, spiceTerm, keyboard_sin);
1445 /* we no not need this */
1450 static void my_kbd_push_keyval(SpiceKbdInstance
*sin
, uint32_t keySym
, int flags
)
1452 spiceTerm
*vt
= SPICE_CONTAINEROF(sin
, spiceTerm
, keyboard_sin
);
1453 static int control
= 0;
1454 static int shift
= 0;
1459 fprintf (stderr
, "KEYEVENT:%d: %08x\n", flags
, keySym
);fflush (stderr
);
1461 if (keySym
== GDK_KEY_Shift_L
|| keySym
== GDK_KEY_Shift_R
) {
1463 } if (keySym
== GDK_KEY_Control_L
|| keySym
== GDK_KEY_Control_R
) {
1465 } else if (vt
->ibuf_count
< (IBUFSIZE
- 32)) {
1468 if(keySym
>= 'a' && keySym
<= 'z')
1469 uc
= keySym
- 'a' + 1;
1470 else if (keySym
>= 'A' && keySym
<= 'Z')
1471 uc
= keySym
- 'A' + 1;
1476 //printf("CONTROL: %08x %d\n", keySym, uc);
1480 case GDK_KEY_Escape
:
1482 case GDK_KEY_Return
:
1484 case GDK_KEY_BackSpace
:
1488 case GDK_KEY_Delete
: /* kdch1 */
1489 case GDK_KEY_KP_Delete
:
1491 case GDK_KEY_Home
: /* khome */
1492 case GDK_KEY_KP_Home
:
1495 case GDK_KEY_KP_End
: /* kend */
1497 case GDK_KEY_Insert
: /* kich1 */
1498 case GDK_KEY_KP_Insert
:
1501 case GDK_KEY_KP_Up
: /* kcuu1 */
1503 case GDK_KEY_Down
: /* kcud1 */
1504 case GDK_KEY_KP_Down
:
1507 case GDK_KEY_KP_Right
: /* kcuf1 */
1510 case GDK_KEY_KP_Left
: /* kcub1 */
1512 case GDK_KEY_Page_Up
:
1514 spiceterm_virtual_scroll (vt
, -vt
->height
/2);
1518 case GDK_KEY_Page_Down
:
1520 spiceterm_virtual_scroll (vt
, vt
->height
/2);
1549 if (keySym
< 0x100) {
1557 fprintf(stderr
, "KEYPRESS OUT:%s: %08x\n", esc
, uc
); fflush (stderr
);
1560 if (vt
->y_displ
!= vt
->y_base
) {
1561 vt
->y_displ
= vt
->y_base
;
1562 spiceterm_refresh (vt
);
1566 spiceterm_respond_esc(vt
, esc
);
1567 } else if (uc
> 0) {
1570 gint len
= g_unichar_to_utf8(uc
, buf
);
1574 for (i
= 0; i
< len
; i
++) {
1575 vt
->ibuf
[vt
->ibuf_count
++] = buf
[i
];
1579 vt
->ibuf
[vt
->ibuf_count
++] = (char)uc
;
1588 if (flags
& 2) { // UP
1589 //printf("KEYRELEASE %08x\n", keySym);
1591 if (keySym
== GDK_KEY_Shift_L
|| keySym
== GDK_KEY_Shift_R
) {
1593 } else if (keySym
== GDK_KEY_Control_L
|| keySym
== GDK_KEY_Control_R
) {
1598 vt
->screen
->core
->watch_update_mask(vt
->screen
->mwatch
,
1599 SPICE_WATCH_EVENT_READ
|SPICE_WATCH_EVENT_WRITE
);
1602 static uint8_t my_kbd_get_leds(SpiceKbdInstance
*sin
)
1607 static SpiceKbdInterface my_keyboard_sif
= {
1608 .base
.type
= SPICE_INTERFACE_KEYBOARD
,
1609 .base
.description
= "spiceterm keyboard device",
1610 .base
.major_version
= SPICE_INTERFACE_KEYBOARD_MAJOR
,
1611 .base
.minor_version
= SPICE_INTERFACE_KEYBOARD_MINOR
,
1612 .push_keyval
= my_kbd_push_keyval
,
1613 .push_scan_freg
= my_kbd_push_key
,
1614 .get_leds
= my_kbd_get_leds
,
1618 create_spiceterm (int argc
, char** argv
, int maxx
, int maxy
)
1622 SpiceScreen
*spice_screen
;
1624 SpiceCoreInterface
*core
= basic_event_loop_init();
1625 spice_screen
= spice_screen_new(core
);
1626 //spice_server_set_image_compression(server, SPICE_IMAGE_COMPRESS_OFF);
1627 spice_screen_add_display_interface(spice_screen
);
1628 spice_screen_add_agent_interface(spice_screen
->server
);
1630 spiceTerm
*vt
= (spiceTerm
*)calloc (sizeof(spiceTerm
), 1);
1632 vt
->keyboard_sin
.base
.sif
= &my_keyboard_sif
.base
;
1633 spice_server_add_interface(spice_screen
->server
, &vt
->keyboard_sin
.base
);
1635 // screen->setXCutText = spiceterm_set_xcut_text;
1636 // screen->ptrAddEvent = spiceterm_pointer_event;
1637 // screen->newClientHook = new_client;
1638 // screen->desktopName = "SPICE Command Terminal";
1640 vt
->maxx
= spice_screen
->width
;
1641 vt
->maxy
= spice_screen
->height
;
1643 vt
->width
= vt
->maxx
/ 8;
1644 vt
->height
= vt
->maxy
/ 16;
1646 vt
->total_height
= vt
->height
* 20;
1647 vt
->scroll_height
= 0;
1652 vt
->region_bottom
= vt
->height
;
1654 vt
->g0enc
= LAT1_MAP
;
1655 vt
->g1enc
= GRAF_MAP
;
1656 vt
->cur_enc
= vt
->g0enc
;
1659 /* default text attributes */
1660 vt
->default_attrib
.bold
= 0;
1661 vt
->default_attrib
.uline
= 0;
1662 vt
->default_attrib
.blink
= 0;
1663 vt
->default_attrib
.invers
= 0;
1664 vt
->default_attrib
.unvisible
= 0;
1665 vt
->default_attrib
.fgcol
= 7;
1666 vt
->default_attrib
.bgcol
= 0;
1668 vt
->cur_attrib
= vt
->default_attrib
;
1670 vt
->cells
= (TextCell
*)calloc (sizeof (TextCell
), vt
->width
*vt
->total_height
);
1672 for (i
= 0; i
< vt
->width
*vt
->total_height
; i
++) {
1673 vt
->cells
[i
].ch
= ' ';
1674 vt
->cells
[i
].attrib
= vt
->default_attrib
;
1677 vt
->altcells
= (TextCell
*)calloc (sizeof (TextCell
), vt
->width
*vt
->height
);
1679 vt
->screen
= spice_screen
;
1684 static void master_watch(int master
, int event
, void *opaque
)
1686 spiceTerm
*vt
= (spiceTerm
*)opaque
;
1688 printf("CHANNEL EVENT %d\n", event
);
1690 // fixme: if (!vt->mark_active) {
1692 if (event
== SPICE_WATCH_EVENT_READ
) {
1695 while ((c
= read(master
, buffer
, 1024)) == -1) {
1696 if (errno
!= EAGAIN
) break;
1699 g_error("got read error"); // fixme
1701 spiceterm_puts (vt
, buffer
, c
);
1703 if (vt
->ibuf_count
> 0) {
1704 printf ("DEBUG: WRITE %x %d\n", vt
->ibuf
[0], vt
->ibuf_count
);
1705 write (master
, vt
->ibuf
, vt
->ibuf_count
);
1706 vt
->ibuf_count
= 0; // fixme: what if not all data written
1708 vt
->screen
->core
->watch_update_mask(vt
->screen
->mwatch
, SPICE_WATCH_EVENT_READ
);
1713 main (int argc
, char** argv
)
1716 char **cmdargv
= NULL
;
1717 char *command
= "/bin/bash"; // execute normal shell as default
1721 struct winsize dimensions
;
1723 g_thread_init(NULL
);
1725 for (i
= 1; i
< argc
; i
++) {
1726 if (!strcmp (argv
[i
], "-c")) {
1727 command
= argv
[i
+1];
1728 cmdargv
= &argv
[i
+1];
1735 if (0) print_usage(NULL
); // fixme:
1737 spiceTerm
*vt
= create_spiceterm (argc
, argv
, 745, 400);
1739 setlocale(LC_ALL
, ""); // set from environment
1741 char *ctype
= setlocale (LC_CTYPE
, NULL
); // query LC_CTYPE
1743 // fixme: ist there a standard way to detect utf8 mode ?
1744 if (strcasestr (ctype
, ".utf-8")||strcasestr (ctype
, ".utf8")) {
1748 dimensions
.ws_col
= vt
->width
;
1749 dimensions
.ws_row
= vt
->height
;
1751 setenv ("TERM", TERM
, 1);
1753 printf("EXEC: %s\n", command
);
1755 pid
= forkpty (&master
, ptyname
, NULL
, &dimensions
);
1758 // install default signal handlers
1759 signal (SIGQUIT
, SIG_DFL
);
1760 signal (SIGTERM
, SIG_DFL
);
1761 signal (SIGINT
, SIG_DFL
);
1764 execvp (command
, cmdargv
);
1766 execlp (command
, command
, NULL
);
1768 perror ("Error: exec failed\n");
1769 exit (-1); // should not be reached
1770 } else if (pid
== -1) {
1771 perror ("Error: fork failed\n");
1776 vt
->screen
->mwatch
= vt
->screen
->core
->watch_add(
1777 master
, SPICE_WATCH_EVENT_READ
/* |SPICE_WATCH_EVENT_WRITE */,
1780 basic_event_loop_mainloop();
1782 //rfbProcessEvents (vt->screen, 40000); /* 40 ms */
1785 if (vt->ibuf_count > 0) {
1786 printf ("DEBUG: WRITE %d %d\n", vt->ibuf[0], vt->ibuf_count);
1787 write (master, vt->ibuf, vt->ibuf_count);
1789 last_time = time (NULL);
1795 waitpid(pid
, &status
, 0);