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 (unicode 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
, unicode 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
)
195 // rfbFillRect (vt->screen, 0, 0, vt->maxx, vt->maxy, vt->default_attrib.bgcol);
198 for(y
= 0; y
< vt
->height
; y
++) {
199 TextCell
*c
= vt
->cells
+ y1
* vt
->width
;
200 for(x
= 0; x
< vt
->width
; x
++) {
201 draw_char_at (vt
, x
, y
, c
->ch
, c
->attrib
);
204 if (++y1
== vt
->total_height
)
207 //rfbMarkRectAsModified (vt->screen, 0, 0, vt->maxx, vt->maxy);
209 spiceterm_show_cursor (vt
, 1);
213 spiceterm_scroll_down (spiceTerm
*vt
, int top
, int bottom
, int lines
)
215 if ((top
+ lines
) >= bottom
) {
216 lines
= bottom
- top
-1;
219 if (top
< 0 || bottom
> vt
->height
|| top
>= bottom
|| lines
< 1) {
224 for(i
= bottom
- top
- lines
- 1; i
>= 0; i
--) {
225 int src
= ((vt
->y_base
+ top
+ i
) % vt
->total_height
)*vt
->width
;
226 int dst
= ((vt
->y_base
+ top
+ lines
+ i
) % vt
->total_height
)*vt
->width
;
228 memmove(vt
->cells
+ dst
, vt
->cells
+ src
, vt
->width
*sizeof (TextCell
));
231 for (i
= 0; i
< lines
; i
++) {
233 TextCell
*c
= vt
->cells
+ ((vt
->y_base
+ top
+ i
) % vt
->total_height
)*vt
->width
;
234 for(j
= 0; j
< vt
->width
; j
++) {
235 c
->attrib
= vt
->default_attrib
;
246 spice_screen_scroll(vt
->screen
, 0, y1
, vt
->screen
->primary_width
, y2
, 0, y0
);
247 spice_screen_clear(vt
->screen
, 0, y0
, vt
->screen
->primary_width
, y1
);
251 spiceterm_scroll_up (spiceTerm
*vt
, int top
, int bottom
, int lines
, int moveattr
)
253 if ((top
+ lines
) >= bottom
) {
254 lines
= bottom
- top
- 1;
257 if (top
< 0 || bottom
> vt
->height
|| top
>= bottom
|| lines
< 1) {
264 int y1
= (top
+ lines
)*16;
267 spice_screen_scroll(vt
->screen
, 0, y0
, vt
->screen
->primary_width
, y2
-h
, 0, y1
);
268 spice_screen_clear(vt
->screen
, 0, y2
-h
, vt
->screen
->primary_width
, y2
);
277 for(i
= 0; i
< (bottom
- top
- lines
); i
++) {
278 int dst
= ((vt
->y_base
+ top
+ i
) % vt
->total_height
)*vt
->width
;
279 int src
= ((vt
->y_base
+ top
+ lines
+ i
) % vt
->total_height
)*vt
->width
;
281 memmove(vt
->cells
+ dst
, vt
->cells
+ src
, vt
->width
*sizeof (TextCell
));
284 for (i
= 1; i
<= lines
; i
++) {
286 TextCell
*c
= vt
->cells
+ ((vt
->y_base
+ bottom
- i
) % vt
->total_height
)*vt
->width
;
287 for(j
= 0; j
< vt
->width
; j
++) {
288 c
->attrib
= vt
->default_attrib
;
296 spiceterm_virtual_scroll (spiceTerm
*vt
, int lines
)
298 if (vt
->altbuf
|| lines
== 0) return;
302 int i
= vt
->scroll_height
;
303 if (i
> vt
->total_height
- vt
->height
)
304 i
= vt
->total_height
- vt
->height
;
305 int y1
= vt
->y_base
- i
;
307 y1
+= vt
->total_height
;
308 for(i
= 0; i
< lines
; i
++) {
309 if (vt
->y_displ
== y1
) break;
310 if (--vt
->y_displ
< 0) {
311 vt
->y_displ
= vt
->total_height
- 1;
316 for(i
= 0; i
< lines
; i
++) {
317 if (vt
->y_displ
== vt
->y_base
) break;
318 if (++vt
->y_displ
== vt
->total_height
) {
325 spiceterm_refresh (vt
);
328 spiceterm_respond_esc (spiceTerm
*vt
, const char *esc
)
330 int len
= strlen (esc
);
333 if (vt
->ibuf_count
< (IBUFSIZE
- 1 - len
)) {
334 vt
->ibuf
[vt
->ibuf_count
++] = 27;
335 for (i
= 0; i
< len
; i
++) {
336 vt
->ibuf
[vt
->ibuf_count
++] = esc
[i
];
342 spiceterm_put_lf (spiceTerm
*vt
)
344 if (vt
->cy
+ 1 == vt
->region_bottom
) {
346 if (vt
->altbuf
|| vt
->region_top
!= 0 || vt
->region_bottom
!= vt
->height
) {
347 spiceterm_scroll_up (vt
, vt
->region_top
, vt
->region_bottom
, 1, 1);
351 if (vt
->y_displ
== vt
->y_base
) {
352 spiceterm_scroll_up (vt
, vt
->region_top
, vt
->region_bottom
, 1, 0);
355 if (vt
->y_displ
== vt
->y_base
) {
356 if (++vt
->y_displ
== vt
->total_height
) {
361 if (++vt
->y_base
== vt
->total_height
) {
365 if (vt
->scroll_height
< vt
->total_height
) {
369 int y1
= (vt
->y_base
+ vt
->height
- 1) % vt
->total_height
;
370 TextCell
*c
= &vt
->cells
[y1
* vt
->width
];
372 for (x
= 0; x
< vt
->width
; x
++) {
374 c
->attrib
= vt
->default_attrib
;
378 // fprintf (stderr, "BASE: %d DISPLAY %d\n", vt->y_base, vt->y_displ);
380 } else if (vt
->cy
< vt
->height
- 1) {
387 spiceterm_csi_m (spiceTerm
*vt
)
391 for (i
= 0; i
< vt
->esc_count
; i
++) {
392 switch (vt
->esc_buf
[i
]) {
393 case 0: /* reset all console attributes to default */
394 vt
->cur_attrib
= vt
->default_attrib
;
397 vt
->cur_attrib
.bold
= 1;
400 vt
->cur_attrib
.uline
= 1;
403 vt
->cur_attrib
.blink
= 1;
406 vt
->cur_attrib
.invers
= 1;
409 vt
->cur_attrib
.unvisible
= 1;
412 vt
->cur_enc
= LAT1_MAP
;
413 // fixme: dispaly controls = 0 ?
414 // fixme: toggle meta = 0 ?
417 vt
->cur_enc
= IBMPC_MAP
;
418 // fixme: dispaly controls = 1 ?
419 // fixme: toggle meta = 0 ?
422 vt
->cur_enc
= IBMPC_MAP
;
423 // fixme: dispaly controls = 1 ?
424 // fixme: toggle meta = 1 ?
427 vt
->cur_attrib
.bold
= 0;
430 vt
->cur_attrib
.uline
= 0;
433 vt
->cur_attrib
.blink
= 0;
436 vt
->cur_attrib
.invers
= 0;
439 vt
->cur_attrib
.unvisible
= 0;
449 /* set foreground color */
450 vt
->cur_attrib
.fgcol
= color_table
[vt
->esc_buf
[i
] - 30];
453 /* reset color to default, enable underline */
454 vt
->cur_attrib
.fgcol
= vt
->default_attrib
.fgcol
;
455 vt
->cur_attrib
.uline
= 1;
458 /* reset color to default, disable underline */
459 vt
->cur_attrib
.fgcol
= vt
->default_attrib
.fgcol
;
460 vt
->cur_attrib
.uline
= 0;
470 /* set background color */
471 vt
->cur_attrib
.bgcol
= color_table
[vt
->esc_buf
[i
] - 40];
474 /* reset background color */
475 vt
->cur_attrib
.bgcol
= vt
->default_attrib
.bgcol
;
478 fprintf(stderr
, "unhandled ESC[%d m code\n",vt
->esc_buf
[i
]);
485 spiceterm_save_cursor (spiceTerm
*vt
)
487 vt
->cx_saved
= vt
->cx
;
488 vt
->cy_saved
= vt
->cy
;
489 vt
->cur_attrib_saved
= vt
->cur_attrib
;
490 vt
->charset_saved
= vt
->charset
;
491 vt
->g0enc_saved
= vt
->g0enc
;
492 vt
->g1enc_saved
= vt
->g1enc
;
493 vt
->cur_enc_saved
= vt
->cur_enc
;
497 spiceterm_restore_cursor (spiceTerm
*vt
)
499 vt
->cx
= vt
->cx_saved
;
500 vt
->cy
= vt
->cy_saved
;
501 vt
->cur_attrib
= vt
->cur_attrib_saved
;
502 vt
->charset
= vt
->charset_saved
;
503 vt
->g0enc
= vt
->g0enc_saved
;
504 vt
->g1enc
= vt
->g1enc_saved
;
505 vt
->cur_enc
= vt
->cur_enc_saved
;
509 spiceterm_set_alternate_buffer (spiceTerm
*vt
, int on_off
)
513 vt
->y_displ
= vt
->y_base
;
517 if (vt
->altbuf
) return;
521 /* alternate buffer & cursor */
523 spiceterm_save_cursor (vt
);
524 /* save screen to altcels */
525 for (y
= 0; y
< vt
->height
; y
++) {
526 int y1
= (vt
->y_base
+ y
) % vt
->total_height
;
527 for (x
= 0; x
< vt
->width
; x
++) {
528 vt
->altcells
[y
*vt
->width
+ x
] = vt
->cells
[y1
*vt
->width
+ x
];
533 for (y
= 0; y
<= vt
->height
; y
++) {
534 for (x
= 0; x
< vt
->width
; x
++) {
535 spiceterm_clear_xy (vt
, x
, y
);
541 if (vt
->altbuf
== 0) return;
545 /* restore saved data */
546 for (y
= 0; y
< vt
->height
; y
++) {
547 int y1
= (vt
->y_base
+ y
) % vt
->total_height
;
548 for (x
= 0; x
< vt
->width
; x
++) {
549 vt
->cells
[y1
*vt
->width
+ x
] = vt
->altcells
[y
*vt
->width
+ x
];
553 spiceterm_restore_cursor (vt
);
556 spiceterm_refresh (vt
);
560 spiceterm_set_mode (spiceTerm
*vt
, int on_off
)
564 for (i
= 0; i
<= vt
->esc_count
; i
++) {
565 if (vt
->esc_ques
) { /* DEC private modes set/reset */
566 switch(vt
->esc_buf
[i
]) {
567 case 10: /* X11 mouse reporting on/off */
569 vt
->report_mouse
= on_off
;
571 case 1049: /* start/end special app mode (smcup/rmcup) */
572 spiceterm_set_alternate_buffer (vt
, on_off
);
574 case 25: /* Cursor on/off */
575 case 9: /* X10 mouse reporting on/off */
576 case 6: /* Origin relative/absolute */
577 case 1: /* Cursor keys in appl mode*/
578 case 5: /* Inverted screen on/off */
579 case 7: /* Autowrap on/off */
580 case 8: /* Autorepeat on/off */
583 } else { /* ANSI modes set/reset */
584 /* fixme: implement me */
590 spiceterm_gotoxy (spiceTerm
*vt
, int x
, int y
)
592 /* verify all boundaries */
598 if (x
>= vt
->width
) {
608 if (y
>= vt
->height
) {
615 enum { ESnormal
, ESesc
, ESsquare
, ESgetpars
, ESgotpars
, ESfunckey
,
616 EShash
, ESsetG0
, ESsetG1
, ESpercent
, ESignore
, ESnonstd
,
617 ESpalette
, ESidquery
, ESosc1
, ESosc2
};
620 spiceterm_putchar (spiceTerm
*vt
, unicode ch
)
624 if (!vt
->tty_state
) {
625 DPRINTF(1, "%s: CHAR:%2d: %4x '%c' (cur_enc %d) %d %d", __func__
,
626 vt
->tty_state
, ch
, ch
, vt
->cur_enc
, vt
->cx
, vt
->cy
);
629 switch(vt
->tty_state
) {
631 vt
->tty_state
= ESnormal
;
634 vt
->tty_state
= ESsquare
;
637 vt
->tty_state
= ESnonstd
;
640 vt
->tty_state
= ESpercent
;
643 spiceterm_save_cursor (vt
);
646 spiceterm_restore_cursor (vt
);
649 vt
->tty_state
= ESsetG0
; // SET G0
652 vt
->tty_state
= ESsetG1
; // SET G1
656 if (vt
->cy
== vt
->region_top
)
657 spiceterm_scroll_down (vt
, vt
->region_top
, vt
->region_bottom
, 1);
658 else if (vt
->cy
> 0) {
663 /* numeric keypad - ignored */
666 /* appl. keypad - ignored */
669 DPRINTF(1, "%s: got unhandled ESC%c %d", __func__
, ch
, ch
);
673 case ESnonstd
: /* Operating System Controls */
674 vt
->tty_state
= ESnormal
;
677 case 'P': /* palette escape sequence */
678 for(i
= 0; i
< MAX_ESC_PARAMS
; i
++) {
683 vt
->tty_state
= ESpalette
;
685 case 'R': /* reset palette */
686 // fixme: reset_palette(vc);
693 vt
->osc_textbuf
[0] = 0;
694 vt
->tty_state
= ESosc1
;
697 DPRINTF(1, "%s: got unhandled OSC %c", __func__
, ch
);
698 vt
->tty_state
= ESnormal
;
703 vt
->tty_state
= ESnormal
;
705 vt
->tty_state
= ESosc2
;
707 DPRINTF(1, "%s: got illegal OSC sequence", __func__
);
711 if (ch
!= 0x9c && ch
!= 7) {
713 while (vt
->osc_textbuf
[i
]) i
++;
714 vt
->osc_textbuf
[i
++] = ch
;
715 vt
->osc_textbuf
[i
] = 0;
717 DPRINTF(1, "%s: OSC:%c:%s", __func__
, 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
;
782 char *qes
= vt
->esc_ques
? "?" : "";
785 if (vt
->esc_count
== 0) {
786 DPRINTF(1, "%s: ESC[%s%c", __func__
, qes
, ch
);
787 } else if (vt
->esc_count
== 1) {
788 DPRINTF(1, "%s: ESC[%s%d%c\n", __func__
, qes
, vt
->esc_buf
[0], ch
);
791 printf("ESC[%s%d", qes
, vt
->esc_buf
[0]);
792 for (i
= 1; i
< vt
->esc_count
; i
++) {
793 printf(";%d", vt
->esc_buf
[i
]);
801 spiceterm_set_mode (vt
, 1);
804 spiceterm_set_mode (vt
, 0);
807 if (!vt
->esc_count
) {
808 vt
->esc_count
++; // default parameter 0
810 spiceterm_csi_m (vt
);
813 /* report cursor position */
814 /* TODO: send ESC[row;colR */
818 if (vt
->esc_buf
[0] == 0) {
821 vt
->cy
-= vt
->esc_buf
[0];
828 /* move cursor down */
829 if (vt
->esc_buf
[0] == 0) {
832 vt
->cy
+= vt
->esc_buf
[0];
833 if (vt
->cy
>= vt
->height
) {
834 vt
->cy
= vt
->height
- 1;
839 /* move cursor right */
840 if (vt
->esc_buf
[0] == 0) {
843 vt
->cx
+= vt
->esc_buf
[0];
844 if (vt
->cx
>= vt
->width
) {
845 vt
->cx
= vt
->width
- 1;
849 /* move cursor left */
850 if (vt
->esc_buf
[0] == 0) {
853 vt
->cx
-= vt
->esc_buf
[0];
860 /* move cursor to column */
861 spiceterm_gotoxy (vt
, vt
->esc_buf
[0] - 1, vt
->cy
);
864 /* move cursor to row */
865 spiceterm_gotoxy (vt
, vt
->cx
, vt
->esc_buf
[0] - 1);
869 /* move cursor to row, column */
870 spiceterm_gotoxy (vt
, vt
->esc_buf
[1] - 1, vt
->esc_buf
[0] - 1);
873 switch (vt
->esc_buf
[0]) {
875 /* clear to end of screen */
876 for (y
= vt
->cy
; y
< vt
->height
; y
++) {
877 for (x
= 0; x
< vt
->width
; x
++) {
878 if (y
== vt
->cy
&& x
< vt
->cx
) {
881 spiceterm_clear_xy (vt
, x
, y
);
886 /* clear from beginning of screen */
887 for (y
= 0; y
<= vt
->cy
; y
++) {
888 for (x
= 0; x
< vt
->width
; x
++) {
889 if (y
== vt
->cy
&& x
> vt
->cx
) {
892 spiceterm_clear_xy (vt
, x
, y
);
897 /* clear entire screen */
898 for (y
= 0; y
<= vt
->height
; y
++) {
899 for (x
= 0; x
< vt
->width
; x
++) {
900 spiceterm_clear_xy (vt
, x
, y
);
907 switch (vt
->esc_buf
[0]) {
910 for(x
= vt
->cx
; x
< vt
->width
; x
++) {
911 spiceterm_clear_xy (vt
, x
, vt
->cy
);
915 /* clear from beginning of line */
916 for (x
= 0; x
<= vt
->cx
; x
++) {
917 spiceterm_clear_xy (vt
, x
, vt
->cy
);
921 /* clear entire line */
922 for(x
= 0; x
< vt
->width
; x
++) {
923 spiceterm_clear_xy (vt
, x
, vt
->cy
);
932 if (c
> vt
->height
- vt
->cy
)
933 c
= vt
->height
- vt
->cy
;
937 spiceterm_scroll_down (vt
, vt
->cy
, vt
->region_bottom
, c
);
943 if (c
> vt
->height
- vt
->cy
)
944 c
= vt
->height
- vt
->cy
;
948 spiceterm_scroll_up (vt
, vt
->cy
, vt
->region_bottom
, c
, 1);
954 spiceterm_scroll_down (vt
, vt
->region_top
, vt
->region_bottom
, c
);
960 spiceterm_scroll_up (vt
, vt
->region_top
, vt
->region_bottom
, c
, 1);
963 /* delete c character */
966 if (c
> vt
->width
- vt
->cx
)
967 c
= vt
->width
- vt
->cx
;
971 for (x
= vt
->cx
; x
< vt
->width
- c
; x
++) {
972 int y1
= (vt
->y_base
+ vt
->cy
) % vt
->total_height
;
973 TextCell
*dst
= &vt
->cells
[y1
* vt
->width
+ x
];
974 TextCell
*src
= dst
+ c
;
976 spiceterm_update_xy (vt
, x
+ c
, vt
->cy
);
978 src
->attrib
= vt
->default_attrib
;
979 spiceterm_update_xy (vt
, x
, vt
->cy
);
983 /* save cursor position */
984 spiceterm_save_cursor (vt
);
987 /* restore cursor position */
988 spiceterm_restore_cursor (vt
);
991 /* erase c characters */
995 if (c
> (vt
->width
- vt
->cx
)) c
= vt
->width
- vt
->cx
;
997 for(i
= 0; i
< c
; i
++) {
998 spiceterm_clear_xy (vt
, vt
->cx
+ i
, vt
->cy
);
1002 /* insert c character */
1004 if (c
> (vt
->width
- vt
->cx
)) {
1005 c
= vt
->width
- vt
->cx
;
1009 for (x
= vt
->width
- c
; x
>= vt
->cx
; x
--) {
1010 int y1
= (vt
->y_base
+ vt
->cy
) % vt
->total_height
;
1011 TextCell
*src
= &vt
->cells
[y1
* vt
->width
+ x
];
1012 TextCell
*dst
= src
+ c
;
1014 spiceterm_update_xy (vt
, x
+ c
, vt
->cy
);
1016 src
->attrib
= vt
->cur_attrib
;
1017 spiceterm_update_xy (vt
, x
, vt
->cy
);
1023 if (!vt
->esc_buf
[0])
1025 if (!vt
->esc_buf
[1])
1026 vt
->esc_buf
[1] = vt
->height
;
1027 /* Minimum allowed region is 2 lines */
1028 if (vt
->esc_buf
[0] < vt
->esc_buf
[1] &&
1029 vt
->esc_buf
[1] <= vt
->height
) {
1030 vt
->region_top
= vt
->esc_buf
[0] - 1;
1031 vt
->region_bottom
= vt
->esc_buf
[1];
1033 vt
->cy
= vt
->region_top
;
1034 DPRINTF(1, "%s: set region %d %d", __func__
, vt
->region_top
, vt
->region_bottom
);
1040 if (vt
->esc_count
== 0) {
1041 DPRINTF(1, "%s: unhandled escape ESC[%s%c", __func__
, qes
, ch
);
1042 } else if (vt
->esc_count
== 1) {
1043 DPRINTF(1, "%s: unhandled escape ESC[%s%d%c\n", __func__
, qes
, vt
->esc_buf
[0], ch
);
1046 printf("unhandled escape ESC[%s%d", qes
, vt
->esc_buf
[0]);
1047 for (i
= 1; i
< vt
->esc_count
; i
++) {
1048 printf(";%d", vt
->esc_buf
[i
]);
1057 case ESsetG0
: // Set G0
1058 vt
->tty_state
= ESnormal
;
1061 vt
->g0enc
= GRAF_MAP
;
1063 vt
->g0enc
= LAT1_MAP
;
1065 vt
->g0enc
= IBMPC_MAP
;
1067 vt
->g0enc
= USER_MAP
;
1069 if (vt
->charset
== 0)
1070 vt
->cur_enc
= vt
->g0enc
;
1073 case ESsetG1
: // Set G1
1074 vt
->tty_state
= ESnormal
;
1077 vt
->g1enc
= GRAF_MAP
;
1079 vt
->g1enc
= LAT1_MAP
;
1081 vt
->g1enc
= IBMPC_MAP
;
1083 vt
->g1enc
= USER_MAP
;
1085 if (vt
->charset
== 1)
1086 vt
->cur_enc
= vt
->g1enc
;
1089 case ESidquery
: // vt100 query id
1090 vt
->tty_state
= ESnormal
;
1093 DPRINTF(1, "%s: ESC[>c Query term ID", __func__
);
1094 spiceterm_respond_esc (vt
, TERMIDCODE
);
1098 vt
->tty_state
= ESnormal
;
1100 case '@': /* defined in ISO 2022 */
1103 case 'G': /* prelim official escape code */
1104 case '8': /* retained for compatibility */
1109 default: // ESnormal
1110 vt
->tty_state
= ESnormal
;
1115 case 7: /* alert aka. bell */
1117 //rfbSendBell(vt->screen);
1119 case 8: /* backspace */
1123 case 9: /* tabspace */
1124 if (vt
->cx
+ (8 - (vt
->cx
% 8)) > vt
->width
) {
1126 spiceterm_put_lf (vt
);
1128 vt
->cx
= vt
->cx
+ (8 - (vt
->cx
% 8));
1134 spiceterm_put_lf (vt
);
1136 case 13: /* carriage return */
1140 /* SI (shift in), select character set 1 */
1142 vt
->cur_enc
= vt
->g1enc
;
1143 /* fixme: display controls = 1 */
1146 /* SO (shift out), select character set 0 */
1148 vt
->cur_enc
= vt
->g0enc
;
1149 /* fixme: display controls = 0 */
1152 vt
->tty_state
= ESesc
;
1154 case 127: /* delete */
1157 case 128+27: /* csi */
1158 vt
->tty_state
= ESsquare
;
1161 if (vt
->cx
>= vt
->width
) {
1164 spiceterm_put_lf (vt
);
1167 int y1
= (vt
->y_base
+ vt
->cy
) % vt
->total_height
;
1168 TextCell
*c
= &vt
->cells
[y1
*vt
->width
+ vt
->cx
];
1169 c
->attrib
= vt
->cur_attrib
;
1171 spiceterm_update_xy (vt
, vt
->cx
, vt
->cy
);
1180 spiceterm_puts (spiceTerm
*vt
, const char *buf
, int len
)
1184 spiceterm_show_cursor (vt
, 0);
1187 unsigned char c
= *buf
;
1191 if (vt
->tty_state
!= ESnormal
) {
1192 // never translate escape sequence
1194 } else if (vt
->utf8
&& !vt
->cur_enc
) {
1196 if(c
& 0x80) { // utf8 multi-byte sequence
1198 if (vt
->utf_count
> 0 && (c
& 0xc0) == 0x80) {
1199 // inside UTF8 sequence
1200 vt
->utf_char
= (vt
->utf_char
<< 6) | (c
& 0x3f);
1202 if (vt
->utf_count
== 0) {
1208 // first char of a UTF8 sequence
1209 if ((c
& 0xe0) == 0xc0) {
1211 vt
->utf_char
= (c
& 0x1f);
1212 } else if ((c
& 0xf0) == 0xe0) {
1214 vt
->utf_char
= (c
& 0x0f);
1215 } else if ((c
& 0xf8) == 0xf0) {
1217 vt
->utf_char
= (c
& 0x07);
1218 } else if ((c
& 0xfc) == 0xf8) {
1220 vt
->utf_char
= (c
& 0x03);
1221 } else if ((c
& 0xfe) == 0xfc) {
1223 vt
->utf_char
= (c
& 0x01);
1236 // never translate controls
1237 if (c
>= 32 && c
!= 127 && c
!= (128+27)) {
1238 tc
= translations
[vt
->cur_enc
][c
& 0x0ff];
1244 spiceterm_putchar (vt
, tc
);
1247 spiceterm_show_cursor (vt
, 1);
1254 spiceterm_set_xcut_text (char* str, int len, struct _rfbClientRec* cl)
1256 spiceTerm *vt =(spiceTerm *)cl->screen->screenData;
1258 // seems str is Latin-1 encoded
1259 if (vt->selection) free (vt->selection);
1260 vt->selection = (unicode *)malloc (len*sizeof (unicode));
1262 for (i = 0; i < len; i++) {
1263 vt->selection[i] = str[i] & 0xff;
1265 vt->selection_len = len;
1270 mouse_report (spiceTerm *vt, int butt, int mrx, int mry)
1274 sprintf (buf, "[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1277 spiceterm_respond_esc (vt, buf);
1282 spiceterm_toggle_marked_cell (spiceTerm
*vt
, int pos
)
1286 int x= (pos%vt->width)*8;
1287 int y= (pos/vt->width)*16;
1290 rfbScreenInfoPtr s=vt->screen;
1292 char *b = s->frameBuffer+y*s->width+x;
1294 for (j=0; j < 16; j++) {
1295 for(i=0; i < 8; i++) {
1296 b[j*s->width+i] ^= 0x0f;
1297 rfbMarkRectAsModified (s, x, y, x+8, y+16);
1306 spiceterm_pointer_event (int buttonMask, int x, int y, rfbClientPtr cl)
1309 spiceTerm *vt =(spiceTerm *)cl->screen->screenData;
1310 static int button2_released = 1;
1311 static int last_mask = 0;
1312 static int sel_start_pos = 0;
1313 static int sel_end_pos = 0;
1320 if (cx >= vt->width) cx = vt->width - 1;
1322 if (cy >= vt->height) cy = vt->height - 1;
1324 if (vt->report_mouse && buttonMask != last_mask) {
1325 last_mask = buttonMask;
1326 if (buttonMask & 1) {
1327 mouse_report (vt, 0, cx, cy);
1329 if (buttonMask & 2) {
1330 mouse_report (vt, 1, cx, cy);
1332 if (buttonMask & 4) {
1333 mouse_report (vt, 2, cx, cy);
1336 mouse_report (vt, 3, cx, cy);
1340 if (buttonMask & 2) {
1341 if(button2_released && vt->selection) {
1343 for(i = 0; i < vt->selection_len; i++) {
1344 if (vt->ibuf_count < IBUFSIZE - 6) { // uft8 is max 6 characters wide
1346 vt->ibuf_count += ucs2_to_utf8 (vt->selection[i], &vt->ibuf[vt->ibuf_count]);
1348 vt->ibuf[vt->ibuf_count++] = vt->selection[i];
1352 if (vt->y_displ != vt->y_base) {
1353 vt->y_displ = vt->y_base;
1354 spiceterm_refresh (vt);
1357 button2_released = 0;
1359 button2_released = 1;
1362 if (buttonMask & 1) {
1363 int pos = cy*vt->width + cx;
1365 // code borrowed from libspiceserver (SPICEonsole.c)
1367 if (!vt->mark_active) {
1369 vt->mark_active = 1;
1370 sel_start_pos = sel_end_pos = pos;
1371 spiceterm_toggle_marked_cell (vt, pos);
1375 if (pos != sel_end_pos) {
1377 if (pos > sel_end_pos) {
1378 cx = sel_end_pos; cy=pos;
1380 cx=pos; cy=sel_end_pos;
1383 if (cx < sel_start_pos) {
1384 if (cy < sel_start_pos) cy--;
1390 spiceterm_toggle_marked_cell (vt, cx);
1398 } else if (vt->mark_active) {
1399 vt->mark_active = 0;
1401 if (sel_start_pos > sel_end_pos) {
1402 int tmp = sel_start_pos - 1;
1403 sel_start_pos = sel_end_pos;
1407 int len = sel_end_pos - sel_start_pos + 1;
1409 if (vt->selection) free (vt->selection);
1410 vt->selection = (unicode *)malloc (len*sizeof (unicode));
1411 vt->selection_len = len;
1412 char *sel_latin1 = (char *)malloc (len + 1);
1414 for (i = 0; i < len; i++) {
1415 int pos = sel_start_pos + i;
1416 int x = pos % vt->width;
1417 int y1 = ((pos / vt->width) + vt->y_displ) % vt->total_height;
1418 TextCell *c = &vt->cells[y1*vt->width + x];
1419 vt->selection[i] = c->ch;
1420 sel_latin1[i] = (char)c->ch;
1423 sel_latin1[len] = 0;
1424 rfbGotXCutText (vt->screen, sel_latin1, len);
1427 while (sel_start_pos <= sel_end_pos) {
1428 spiceterm_toggle_marked_cell (vt, sel_start_pos++);
1433 rfbDefaultPtrAddEvent (buttonMask, x, y, cl);
1438 static void my_kbd_push_key(SpiceKbdInstance
*sin
, uint8_t frag
)
1440 // spiceTerm *vt = SPICE_CONTAINEROF(sin, spiceTerm, keyboard_sin);
1442 /* we no not need this */
1447 static void my_kbd_push_keyval(SpiceKbdInstance
*sin
, uint32_t keySym
, int flags
)
1449 spiceTerm
*vt
= SPICE_CONTAINEROF(sin
, spiceTerm
, keyboard_sin
);
1450 static int control
= 0;
1451 static int shift
= 0;
1456 DPRINTF(1, "%s: flags=%d keySym=%08x", __func__
, flags
, keySym
);
1459 if (keySym
== GDK_KEY_Shift_L
|| keySym
== GDK_KEY_Shift_R
) {
1461 } if (keySym
== GDK_KEY_Control_L
|| keySym
== GDK_KEY_Control_R
) {
1463 } else if (vt
->ibuf_count
< (IBUFSIZE
- 32)) {
1466 if(keySym
>= 'a' && keySym
<= 'z')
1467 uc
= keySym
- 'a' + 1;
1468 else if (keySym
>= 'A' && keySym
<= 'Z')
1469 uc
= keySym
- 'A' + 1;
1475 case GDK_KEY_Escape
:
1477 case GDK_KEY_Return
:
1479 case GDK_KEY_BackSpace
:
1483 case GDK_KEY_Delete
: /* kdch1 */
1484 case GDK_KEY_KP_Delete
:
1486 case GDK_KEY_Home
: /* khome */
1487 case GDK_KEY_KP_Home
:
1490 case GDK_KEY_KP_End
: /* kend */
1492 case GDK_KEY_Insert
: /* kich1 */
1493 case GDK_KEY_KP_Insert
:
1496 case GDK_KEY_KP_Up
: /* kcuu1 */
1498 case GDK_KEY_Down
: /* kcud1 */
1499 case GDK_KEY_KP_Down
:
1502 case GDK_KEY_KP_Right
: /* kcuf1 */
1505 case GDK_KEY_KP_Left
: /* kcub1 */
1507 case GDK_KEY_Page_Up
:
1509 spiceterm_virtual_scroll (vt
, -vt
->height
/2);
1513 case GDK_KEY_Page_Down
:
1515 spiceterm_virtual_scroll (vt
, vt
->height
/2);
1544 if (keySym
< 0x100) {
1551 DPRINTF(1, "%s: escape=%s unicode=%08x\n", __func__
, esc
, uc
);
1553 if (vt
->y_displ
!= vt
->y_base
) {
1554 vt
->y_displ
= vt
->y_base
;
1555 spiceterm_refresh (vt
);
1559 spiceterm_respond_esc(vt
, esc
);
1560 } else if (uc
> 0) {
1563 gint len
= g_unichar_to_utf8(uc
, buf
);
1567 for (i
= 0; i
< len
; i
++) {
1568 vt
->ibuf
[vt
->ibuf_count
++] = buf
[i
];
1572 vt
->ibuf
[vt
->ibuf_count
++] = (char)uc
;
1581 if (flags
& 2) { // UP
1582 if (keySym
== GDK_KEY_Shift_L
|| keySym
== GDK_KEY_Shift_R
) {
1584 } else if (keySym
== GDK_KEY_Control_L
|| keySym
== GDK_KEY_Control_R
) {
1589 vt
->screen
->core
->watch_update_mask(vt
->screen
->mwatch
,
1590 SPICE_WATCH_EVENT_READ
|SPICE_WATCH_EVENT_WRITE
);
1593 static uint8_t my_kbd_get_leds(SpiceKbdInstance
*sin
)
1598 static SpiceKbdInterface my_keyboard_sif
= {
1599 .base
.type
= SPICE_INTERFACE_KEYBOARD
,
1600 .base
.description
= "spiceterm keyboard device",
1601 .base
.major_version
= SPICE_INTERFACE_KEYBOARD_MAJOR
,
1602 .base
.minor_version
= SPICE_INTERFACE_KEYBOARD_MINOR
,
1603 .push_keyval
= my_kbd_push_keyval
,
1604 .push_scan_freg
= my_kbd_push_key
,
1605 .get_leds
= my_kbd_get_leds
,
1609 create_spiceterm (int argc
, char** argv
, int maxx
, int maxy
)
1613 SpiceScreen
*spice_screen
;
1615 SpiceCoreInterface
*core
= basic_event_loop_init();
1616 spice_screen
= spice_screen_new(core
);
1617 //spice_server_set_image_compression(server, SPICE_IMAGE_COMPRESS_OFF);
1618 spice_screen_add_display_interface(spice_screen
);
1619 spice_screen_add_agent_interface(spice_screen
->server
);
1621 spiceTerm
*vt
= (spiceTerm
*)calloc (sizeof(spiceTerm
), 1);
1623 vt
->keyboard_sin
.base
.sif
= &my_keyboard_sif
.base
;
1624 spice_server_add_interface(spice_screen
->server
, &vt
->keyboard_sin
.base
);
1626 // screen->setXCutText = spiceterm_set_xcut_text;
1627 // screen->ptrAddEvent = spiceterm_pointer_event;
1628 // screen->newClientHook = new_client;
1629 // screen->desktopName = "SPICE Command Terminal";
1631 vt
->maxx
= spice_screen
->width
;
1632 vt
->maxy
= spice_screen
->height
;
1634 vt
->width
= vt
->maxx
/ 8;
1635 vt
->height
= vt
->maxy
/ 16;
1637 vt
->total_height
= vt
->height
* 20;
1638 vt
->scroll_height
= 0;
1643 vt
->region_bottom
= vt
->height
;
1645 vt
->g0enc
= LAT1_MAP
;
1646 vt
->g1enc
= GRAF_MAP
;
1647 vt
->cur_enc
= vt
->g0enc
;
1650 /* default text attributes */
1651 vt
->default_attrib
.bold
= 0;
1652 vt
->default_attrib
.uline
= 0;
1653 vt
->default_attrib
.blink
= 0;
1654 vt
->default_attrib
.invers
= 0;
1655 vt
->default_attrib
.unvisible
= 0;
1656 vt
->default_attrib
.fgcol
= 7;
1657 vt
->default_attrib
.bgcol
= 0;
1659 vt
->cur_attrib
= vt
->default_attrib
;
1661 vt
->cells
= (TextCell
*)calloc (sizeof (TextCell
), vt
->width
*vt
->total_height
);
1663 for (i
= 0; i
< vt
->width
*vt
->total_height
; i
++) {
1664 vt
->cells
[i
].ch
= ' ';
1665 vt
->cells
[i
].attrib
= vt
->default_attrib
;
1668 vt
->altcells
= (TextCell
*)calloc (sizeof (TextCell
), vt
->width
*vt
->height
);
1670 vt
->screen
= spice_screen
;
1675 static void master_watch(int master
, int event
, void *opaque
)
1677 spiceTerm
*vt
= (spiceTerm
*)opaque
;
1679 // fixme: if (!vt->mark_active) {
1681 if (event
== SPICE_WATCH_EVENT_READ
) {
1684 while ((c
= read(master
, buffer
, 1024)) == -1) {
1685 if (errno
!= EAGAIN
) break;
1688 g_error("got read error"); // fixme
1690 spiceterm_puts (vt
, buffer
, c
);
1692 if (vt
->ibuf_count
> 0) {
1693 DPRINTF(1, "%s: write input %x %d", __func__
, vt
->ibuf
[0], vt
->ibuf_count
);
1694 write (master
, vt
->ibuf
, vt
->ibuf_count
);
1695 vt
->ibuf_count
= 0; // fixme: what if not all data written
1697 vt
->screen
->core
->watch_update_mask(vt
->screen
->mwatch
, SPICE_WATCH_EVENT_READ
);
1702 main (int argc
, char** argv
)
1705 char **cmdargv
= NULL
;
1706 char *command
= "/bin/bash"; // execute normal shell as default
1710 struct winsize dimensions
;
1712 g_thread_init(NULL
);
1714 for (i
= 1; i
< argc
; i
++) {
1715 if (!strcmp (argv
[i
], "-c")) {
1716 command
= argv
[i
+1];
1717 cmdargv
= &argv
[i
+1];
1724 if (0) print_usage(NULL
); // fixme:
1726 spiceTerm
*vt
= create_spiceterm (argc
, argv
, 745, 400);
1728 setlocale(LC_ALL
, ""); // set from environment
1730 char *ctype
= setlocale (LC_CTYPE
, NULL
); // query LC_CTYPE
1732 // fixme: ist there a standard way to detect utf8 mode ?
1733 if (strcasestr (ctype
, ".utf-8")||strcasestr (ctype
, ".utf8")) {
1737 dimensions
.ws_col
= vt
->width
;
1738 dimensions
.ws_row
= vt
->height
;
1740 setenv ("TERM", TERM
, 1);
1742 DPRINTF(1, "%s: execute %s", __func__
, command
);
1744 pid
= forkpty (&master
, ptyname
, NULL
, &dimensions
);
1747 // install default signal handlers
1748 signal (SIGQUIT
, SIG_DFL
);
1749 signal (SIGTERM
, SIG_DFL
);
1750 signal (SIGINT
, SIG_DFL
);
1753 execvp (command
, cmdargv
);
1755 execlp (command
, command
, NULL
);
1757 perror ("Error: exec failed\n");
1758 exit (-1); // should not be reached
1759 } else if (pid
== -1) {
1760 perror ("Error: fork failed\n");
1765 vt
->screen
->mwatch
= vt
->screen
->core
->watch_add(
1766 master
, SPICE_WATCH_EVENT_READ
/* |SPICE_WATCH_EVENT_WRITE */,
1769 basic_event_loop_mainloop();
1771 //rfbProcessEvents (vt->screen, 40000); /* 40 ms */
1774 if (vt->ibuf_count > 0) {
1775 printf ("DEBUG: WRITE %d %d\n", vt->ibuf[0], vt->ibuf_count);
1776 write (master, vt->ibuf, vt->ibuf_count);
1778 last_time = time (NULL);
1784 waitpid(pid
, &status
, 0);