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 };
82 static void spiceterm_resize(spiceTerm
*vt
, uint32_t width
, uint32_t height
);
84 static void vdagent_grab_clipboard(spiceTerm
*vt
, uint8_t selection
);
85 static void vdagent_request_clipboard(spiceTerm
*vt
, uint8_t selection
);
88 print_usage(const char *msg
)
90 if (msg
) { fprintf(stderr
, "ERROR: %s\n", msg
); }
91 fprintf(stderr
, "USAGE: spiceterm [spiceopts] [-c command [args]]\n");
95 draw_char_at(spiceTerm
*vt
, int x
, int y
, gunichar2 ch
, TextAttributes attrib
)
97 if (x
< 0 || y
< 0 || x
>= vt
->width
|| y
>= vt
->height
) {
101 spice_screen_draw_char(vt
->screen
, x
, y
, ch
, attrib
);
105 spiceterm_update_xy(spiceTerm
*vt
, int x
, int y
)
107 if (x
< 0 || y
< 0 || x
>= vt
->width
|| y
>= vt
->height
) { return; }
109 int y1
= (vt
->y_base
+ y
) % vt
->total_height
;
110 int y2
= y1
- vt
->y_displ
;
112 y2
+= vt
->total_height
;
114 if (y2
< vt
->height
) {
115 TextCell
*c
= &vt
->cells
[y1
* vt
->width
+ x
];
116 draw_char_at(vt
, x
, y2
, c
->ch
, c
->attrib
);
121 spiceterm_clear_xy(spiceTerm
*vt
, int x
, int y
)
123 if (x
< 0 || y
< 0 || x
>= vt
->width
|| y
>= vt
->height
) { return; }
125 int y1
= (vt
->y_base
+ y
) % vt
->total_height
;
126 int y2
= y1
- vt
->y_displ
;
128 y2
+= vt
->total_height
;
130 if (y2
< vt
->height
) {
131 TextCell
*c
= &vt
->cells
[y1
* vt
->width
+ x
];
133 c
->attrib
= vt
->default_attrib
;
134 c
->attrib
.fgcol
= vt
->cur_attrib
.fgcol
;
135 c
->attrib
.bgcol
= vt
->cur_attrib
.bgcol
;
137 draw_char_at(vt
, x
, y
, c
->ch
, c
->attrib
);
142 spiceterm_toggle_marked_cell(spiceTerm
*vt
, int pos
)
144 int x
= (pos
%vt
->width
);
145 int y
= (pos
/vt
->width
);
147 if (x
< 0 || y
< 0 || x
>= vt
->width
|| y
>= vt
->height
) { return; }
149 int y1
= (vt
->y_displ
+ y
) % vt
->total_height
;
151 TextCell
*c
= &vt
->cells
[y1
* vt
->width
+ x
];
152 c
->attrib
.selected
= c
->attrib
.selected
? 0 : 1;
154 if (y
< vt
->height
) {
155 draw_char_at(vt
, x
, y
, c
->ch
, c
->attrib
);
160 spiceterm_show_cursor(spiceTerm
*vt
, int show
)
163 if (x
>= vt
->width
) {
167 int y1
= (vt
->y_base
+ vt
->cy
) % vt
->total_height
;
168 int y
= y1
- vt
->y_displ
;
170 y
+= vt
->total_height
;
173 if (y
< vt
->height
) {
175 TextCell
*c
= &vt
->cells
[y1
* vt
->width
+ x
];
178 TextAttributes attrib
= vt
->default_attrib
;
179 attrib
.invers
= !(attrib
.invers
); /* invert fg and bg */
180 draw_char_at(vt
, x
, y
, c
->ch
, attrib
);
182 draw_char_at(vt
, x
, y
, c
->ch
, c
->attrib
);
188 spiceterm_refresh(spiceTerm
*vt
)
193 for(y
= 0; y
< vt
->height
; y
++) {
194 TextCell
*c
= vt
->cells
+ y1
* vt
->width
;
195 for(x
= 0; x
< vt
->width
; x
++) {
196 draw_char_at(vt
, x
, y
, c
->ch
, c
->attrib
);
199 if (++y1
== vt
->total_height
)
203 spiceterm_show_cursor(vt
, 1);
207 spiceterm_unselect_all(spiceTerm
*vt
)
211 for (i
= 0; i
< vt
->width
*vt
->total_height
; i
++) {
212 if (vt
->cells
[i
].attrib
.selected
) {
213 vt
->cells
[i
].attrib
.selected
= 0;
217 spiceterm_refresh(vt
);
221 spiceterm_scroll_down(spiceTerm
*vt
, int top
, int bottom
, int lines
)
223 if ((top
+ lines
) >= bottom
) {
224 lines
= bottom
- top
-1;
227 if (top
< 0 || bottom
> vt
->height
|| top
>= bottom
|| lines
< 1) {
232 for(i
= bottom
- top
- lines
- 1; i
>= 0; i
--) {
233 int src
= ((vt
->y_base
+ top
+ i
) % vt
->total_height
)*vt
->width
;
234 int dst
= ((vt
->y_base
+ top
+ lines
+ i
) % vt
->total_height
)*vt
->width
;
236 memmove(vt
->cells
+ dst
, vt
->cells
+ src
, vt
->width
*sizeof(TextCell
));
239 for (i
= 0; i
< lines
; i
++) {
241 TextCell
*c
= vt
->cells
+ ((vt
->y_base
+ top
+ i
) % vt
->total_height
)*vt
->width
;
242 for(j
= 0; j
< vt
->width
; j
++) {
243 c
->attrib
= vt
->default_attrib
;
254 spice_screen_scroll(vt
->screen
, 0, y1
, vt
->screen
->primary_width
, y2
, 0, y0
);
255 spice_screen_clear(vt
->screen
, 0, y0
, vt
->screen
->primary_width
, y1
);
259 spiceterm_scroll_up(spiceTerm
*vt
, int top
, int bottom
, int lines
, int moveattr
)
261 if ((top
+ lines
) >= bottom
) {
262 lines
= bottom
- top
- 1;
265 if (top
< 0 || bottom
> vt
->height
|| top
>= bottom
|| lines
< 1) {
272 int y1
= (top
+ lines
)*16;
275 spice_screen_scroll(vt
->screen
, 0, y0
, vt
->screen
->primary_width
, y2
-h
, 0, y1
);
276 spice_screen_clear(vt
->screen
, 0, y2
-h
, vt
->screen
->primary_width
, y2
);
285 for(i
= 0; i
< (bottom
- top
- lines
); i
++) {
286 int dst
= ((vt
->y_base
+ top
+ i
) % vt
->total_height
)*vt
->width
;
287 int src
= ((vt
->y_base
+ top
+ lines
+ i
) % vt
->total_height
)*vt
->width
;
289 memmove(vt
->cells
+ dst
, vt
->cells
+ src
, vt
->width
*sizeof(TextCell
));
292 for (i
= 1; i
<= lines
; i
++) {
294 TextCell
*c
= vt
->cells
+ ((vt
->y_base
+ bottom
- i
) % vt
->total_height
)*vt
->width
;
295 for(j
= 0; j
< vt
->width
; j
++) {
296 c
->attrib
= vt
->default_attrib
;
304 spiceterm_virtual_scroll(spiceTerm
*vt
, int lines
)
306 if (vt
->altbuf
|| lines
== 0) return;
310 int i
= vt
->scroll_height
;
311 if (i
> vt
->total_height
- vt
->height
)
312 i
= vt
->total_height
- vt
->height
;
313 int y1
= vt
->y_base
- i
;
315 y1
+= vt
->total_height
;
316 for(i
= 0; i
< lines
; i
++) {
317 if (vt
->y_displ
== y1
) break;
318 if (--vt
->y_displ
< 0) {
319 vt
->y_displ
= vt
->total_height
- 1;
324 for(i
= 0; i
< lines
; i
++) {
325 if (vt
->y_displ
== vt
->y_base
) break;
326 if (++vt
->y_displ
== vt
->total_height
) {
332 spiceterm_refresh(vt
);
336 spiceterm_respond_esc(spiceTerm
*vt
, const char *esc
)
338 int len
= strlen(esc
);
341 if (vt
->ibuf_count
< (IBUFSIZE
- 1 - len
)) {
342 vt
->ibuf
[vt
->ibuf_count
++] = 27;
343 for (i
= 0; i
< len
; i
++) {
344 vt
->ibuf
[vt
->ibuf_count
++] = esc
[i
];
350 spiceterm_put_lf(spiceTerm
*vt
)
352 if (vt
->cy
+ 1 == vt
->region_bottom
) {
354 if (vt
->altbuf
|| vt
->region_top
!= 0 || vt
->region_bottom
!= vt
->height
) {
355 spiceterm_scroll_up(vt
, vt
->region_top
, vt
->region_bottom
, 1, 1);
359 if (vt
->y_displ
== vt
->y_base
) {
360 spiceterm_scroll_up(vt
, vt
->region_top
, vt
->region_bottom
, 1, 0);
363 if (vt
->y_displ
== vt
->y_base
) {
364 if (++vt
->y_displ
== vt
->total_height
) {
369 if (++vt
->y_base
== vt
->total_height
) {
373 if (vt
->scroll_height
< vt
->total_height
) {
377 int y1
= (vt
->y_base
+ vt
->height
- 1) % vt
->total_height
;
378 TextCell
*c
= &vt
->cells
[y1
* vt
->width
];
380 for (x
= 0; x
< vt
->width
; x
++) {
382 c
->attrib
= vt
->default_attrib
;
386 // fprintf (stderr, "BASE: %d DISPLAY %d\n", vt->y_base, vt->y_displ);
388 } else if (vt
->cy
< vt
->height
- 1) {
394 spiceterm_csi_m(spiceTerm
*vt
)
398 for (i
= 0; i
< vt
->esc_count
; i
++) {
399 switch (vt
->esc_buf
[i
]) {
400 case 0: /* reset all console attributes to default */
401 vt
->cur_attrib
= vt
->default_attrib
;
404 vt
->cur_attrib
.bold
= 1;
407 vt
->cur_attrib
.uline
= 1;
410 vt
->cur_attrib
.blink
= 1;
413 vt
->cur_attrib
.invers
= 1;
416 vt
->cur_attrib
.unvisible
= 1;
419 vt
->cur_enc
= LAT1_MAP
;
420 // fixme: dispaly controls = 0 ?
421 // fixme: toggle meta = 0 ?
424 vt
->cur_enc
= IBMPC_MAP
;
425 // fixme: dispaly controls = 1 ?
426 // fixme: toggle meta = 0 ?
429 vt
->cur_enc
= IBMPC_MAP
;
430 // fixme: dispaly controls = 1 ?
431 // fixme: toggle meta = 1 ?
434 vt
->cur_attrib
.bold
= 0;
437 vt
->cur_attrib
.uline
= 0;
440 vt
->cur_attrib
.blink
= 0;
443 vt
->cur_attrib
.invers
= 0;
446 vt
->cur_attrib
.unvisible
= 0;
456 /* set foreground color */
457 vt
->cur_attrib
.fgcol
= color_table
[vt
->esc_buf
[i
] - 30];
460 /* reset color to default, enable underline */
461 vt
->cur_attrib
.fgcol
= vt
->default_attrib
.fgcol
;
462 vt
->cur_attrib
.uline
= 1;
465 /* reset color to default, disable underline */
466 vt
->cur_attrib
.fgcol
= vt
->default_attrib
.fgcol
;
467 vt
->cur_attrib
.uline
= 0;
477 /* set background color */
478 vt
->cur_attrib
.bgcol
= color_table
[vt
->esc_buf
[i
] - 40];
481 /* reset background color */
482 vt
->cur_attrib
.bgcol
= vt
->default_attrib
.bgcol
;
485 fprintf(stderr
, "unhandled ESC[%d m code\n",vt
->esc_buf
[i
]);
492 spiceterm_save_cursor(spiceTerm
*vt
)
494 vt
->cx_saved
= vt
->cx
;
495 vt
->cy_saved
= vt
->cy
;
496 vt
->cur_attrib_saved
= vt
->cur_attrib
;
497 vt
->charset_saved
= vt
->charset
;
498 vt
->g0enc_saved
= vt
->g0enc
;
499 vt
->g1enc_saved
= vt
->g1enc
;
500 vt
->cur_enc_saved
= vt
->cur_enc
;
504 spiceterm_restore_cursor(spiceTerm
*vt
)
506 vt
->cx
= vt
->cx_saved
;
507 vt
->cy
= vt
->cy_saved
;
508 vt
->cur_attrib
= vt
->cur_attrib_saved
;
509 vt
->charset
= vt
->charset_saved
;
510 vt
->g0enc
= vt
->g0enc_saved
;
511 vt
->g1enc
= vt
->g1enc_saved
;
512 vt
->cur_enc
= vt
->cur_enc_saved
;
516 spiceterm_set_alternate_buffer(spiceTerm
*vt
, int on_off
)
520 vt
->y_displ
= vt
->y_base
;
524 if (vt
->altbuf
) return;
528 /* alternate buffer & cursor */
530 spiceterm_save_cursor(vt
);
531 /* save screen to altcels */
532 for (y
= 0; y
< vt
->height
; y
++) {
533 int y1
= (vt
->y_base
+ y
) % vt
->total_height
;
534 for (x
= 0; x
< vt
->width
; x
++) {
535 vt
->altcells
[y
*vt
->width
+ x
] = vt
->cells
[y1
*vt
->width
+ x
];
540 for (y
= 0; y
<= vt
->height
; y
++) {
541 for (x
= 0; x
< vt
->width
; x
++) {
542 // spiceterm_clear_xy(vt, x, y);
548 if (vt
->altbuf
== 0) return;
552 /* restore saved data */
553 for (y
= 0; y
< vt
->height
; y
++) {
554 int y1
= (vt
->y_base
+ y
) % vt
->total_height
;
555 for (x
= 0; x
< vt
->width
; x
++) {
556 vt
->cells
[y1
*vt
->width
+ x
] = vt
->altcells
[y
*vt
->width
+ x
];
560 spiceterm_restore_cursor(vt
);
563 spiceterm_refresh(vt
);
567 spiceterm_set_mode(spiceTerm
*vt
, int on_off
)
571 for (i
= 0; i
<= vt
->esc_count
; i
++) {
572 if (vt
->esc_ques
) { /* DEC private modes set/reset */
573 switch(vt
->esc_buf
[i
]) {
574 case 10: /* X11 mouse reporting on/off */
575 case 1000: /* SET_VT200_MOUSE */
576 case 1002: /* xterm SET_BTN_EVENT_MOUSE */
577 vt
->report_mouse
= on_off
;
579 case 1049: /* start/end special app mode (smcup/rmcup) */
580 spiceterm_set_alternate_buffer (vt
, on_off
);
582 case 25: /* Cursor on/off */
583 case 9: /* X10 mouse reporting on/off */
584 case 6: /* Origin relative/absolute */
585 case 1: /* Cursor keys in appl mode*/
586 case 5: /* Inverted screen on/off */
587 case 7: /* Autowrap on/off */
588 case 8: /* Autorepeat on/off */
591 } else { /* ANSI modes set/reset */
592 //g_assert_not_reached();
594 /* fixme: implement me */
600 spiceterm_gotoxy(spiceTerm
*vt
, int x
, int y
)
602 /* verify all boundaries */
608 if (x
>= vt
->width
) {
618 if (y
>= vt
->height
) {
626 debug_print_escape_buffer(spiceTerm
*vt
, const char *func
, const char *prefix
,
627 const char *qes
, gunichar2 ch
)
630 if (vt
->esc_count
== 0) {
631 printf("%s:%s ESC[%s%c\n", func
, prefix
, qes
, ch
);
632 } else if (vt
->esc_count
== 1) {
633 printf("%s:%s ESC[%s%d%c\n", func
, prefix
, qes
, vt
->esc_buf
[0], ch
);
636 printf("%s:%s ESC[%s%d", func
, prefix
, qes
, vt
->esc_buf
[0]);
637 for (i
= 1; i
< vt
->esc_count
; i
++) {
638 printf(";%d", vt
->esc_buf
[i
]);
645 enum { ESnormal
, ESesc
, ESsquare
, ESgetpars
, ESgotpars
, ESfunckey
,
646 EShash
, ESsetG0
, ESsetG1
, ESpercent
, ESignore
, ESnonstd
,
647 ESpalette
, ESidquery
, ESosc1
, ESosc2
};
650 spiceterm_putchar(spiceTerm
*vt
, gunichar2 ch
)
654 if (debug
&& !vt
->tty_state
) {
655 DPRINTF(1, "CHAR:%2d: %4x '%c' (cur_enc %d) %d %d",
656 vt
->tty_state
, ch
, ch
, vt
->cur_enc
, vt
->cx
, vt
->cy
);
659 switch(vt
->tty_state
) {
661 vt
->tty_state
= ESnormal
;
664 vt
->tty_state
= ESsquare
;
667 vt
->tty_state
= ESnonstd
;
670 vt
->tty_state
= ESpercent
;
673 spiceterm_save_cursor(vt
);
676 spiceterm_restore_cursor(vt
);
679 vt
->tty_state
= ESsetG0
; // SET G0
682 vt
->tty_state
= ESsetG1
; // SET G1
686 if (vt
->cy
== vt
->region_top
)
687 spiceterm_scroll_down(vt
, vt
->region_top
, vt
->region_bottom
, 1);
688 else if (vt
->cy
> 0) {
693 /* numeric keypad - ignored */
696 /* appl. keypad - ignored */
699 DPRINTF(1, "got unhandled ESC%c %d", ch
, ch
);
703 case ESnonstd
: /* Operating System Controls */
704 vt
->tty_state
= ESnormal
;
707 case 'P': /* palette escape sequence */
708 for(i
= 0; i
< MAX_ESC_PARAMS
; i
++) {
713 vt
->tty_state
= ESpalette
;
715 case 'R': /* reset palette */
716 // fixme: reset_palette(vc);
723 vt
->osc_textbuf
[0] = 0;
724 vt
->tty_state
= ESosc1
;
727 DPRINTF(1, "got unhandled OSC %c", ch
);
728 vt
->tty_state
= ESnormal
;
733 vt
->tty_state
= ESnormal
;
735 vt
->tty_state
= ESosc2
;
737 DPRINTF(1, "got illegal OSC sequence");
741 if (ch
!= 0x9c && ch
!= 7) {
743 while (vt
->osc_textbuf
[i
]) i
++;
744 vt
->osc_textbuf
[i
++] = ch
;
745 vt
->osc_textbuf
[i
] = 0;
747 DPRINTF(1, "OSC:%c:%s", vt
->osc_cmd
, vt
->osc_textbuf
);
748 vt
->tty_state
= ESnormal
;
752 if ((ch
>= '0' && ch
<= '9') || (ch
>= 'A' && ch
<= 'F')
753 || (ch
>= 'a' && ch
<= 'f')) {
754 vt
->esc_buf
[vt
->esc_count
++] = (ch
> '9' ? (ch
& 0xDF) - 'A' + 10 : ch
- '0');
755 if (vt
->esc_count
== 7) {
756 // fixme: this does not work - please test
758 rfbColourMap *cmap =&vt->screen->colourMap;
760 int i = color_table[vt->esc_buf[0]] * 3, j = 1;
761 cmap->data.bytes[i] = 16 * vt->esc_buf[j++];
762 cmap->data.bytes[i++] += vt->esc_buf[j++];
763 cmap->data.bytes[i] = 16 * vt->esc_buf[j++];
764 cmap->data.bytes[i++] += vt->esc_buf[j++];
765 cmap->data.bytes[i] = 16 * vt->esc_buf[j++];
766 cmap->data.bytes[i] += vt->esc_buf[j];
770 vt
->tty_state
= ESnormal
;
773 vt
->tty_state
= ESnormal
;
776 for(i
= 0; i
< MAX_ESC_PARAMS
; i
++) {
782 vt
->tty_state
= ESgetpars
;
785 vt
->tty_state
= ESidquery
;
789 if ((vt
->esc_ques
= (ch
== '?'))) {
793 if (ch
>= '0' && ch
<= '9') {
795 if (vt
->esc_count
< MAX_ESC_PARAMS
) {
796 vt
->esc_buf
[vt
->esc_count
] = vt
->esc_buf
[vt
->esc_count
] * 10 + ch
- '0';
799 } else if (ch
== ';') {
803 if (vt
->esc_has_par
) {
806 vt
->tty_state
= ESgotpars
;
810 vt
->tty_state
= ESnormal
;
812 char *qes
= vt
->esc_ques
? "?" : "";
815 debug_print_escape_buffer(vt
, __func__
, "", qes
, ch
);
820 spiceterm_set_mode(vt
, 1);
823 spiceterm_set_mode(vt
, 0);
826 if (!vt
->esc_count
) {
827 vt
->esc_count
++; // default parameter 0
832 /* report cursor position */
833 /* TODO: send ESC[row;colR */
837 if (vt
->esc_buf
[0] == 0) {
840 vt
->cy
-= vt
->esc_buf
[0];
847 /* move cursor down */
848 if (vt
->esc_buf
[0] == 0) {
851 vt
->cy
+= vt
->esc_buf
[0];
852 if (vt
->cy
>= vt
->height
) {
853 vt
->cy
= vt
->height
- 1;
858 /* move cursor right */
859 if (vt
->esc_buf
[0] == 0) {
862 vt
->cx
+= vt
->esc_buf
[0];
863 if (vt
->cx
>= vt
->width
) {
864 vt
->cx
= vt
->width
- 1;
868 /* move cursor left */
869 if (vt
->esc_buf
[0] == 0) {
872 vt
->cx
-= vt
->esc_buf
[0];
879 /* move cursor to column */
880 spiceterm_gotoxy(vt
, vt
->esc_buf
[0] - 1, vt
->cy
);
883 /* move cursor to row */
884 spiceterm_gotoxy(vt
, vt
->cx
, vt
->esc_buf
[0] - 1);
888 /* move cursor to row, column */
889 spiceterm_gotoxy(vt
, vt
->esc_buf
[1] - 1, vt
->esc_buf
[0] - 1);
892 switch (vt
->esc_buf
[0]) {
894 /* clear to end of screen */
895 for (y
= vt
->cy
; y
< vt
->height
; y
++) {
896 for (x
= 0; x
< vt
->width
; x
++) {
897 if (y
== vt
->cy
&& x
< vt
->cx
) {
900 spiceterm_clear_xy (vt
, x
, y
);
905 /* clear from beginning of screen */
906 for (y
= 0; y
<= vt
->cy
; y
++) {
907 for (x
= 0; x
< vt
->width
; x
++) {
908 if (y
== vt
->cy
&& x
> vt
->cx
) {
911 spiceterm_clear_xy(vt
, x
, y
);
916 /* clear entire screen */
917 for (y
= 0; y
<= vt
->height
; y
++) {
918 for (x
= 0; x
< vt
->width
; x
++) {
919 spiceterm_clear_xy(vt
, x
, y
);
926 switch (vt
->esc_buf
[0]) {
929 for(x
= vt
->cx
; x
< vt
->width
; x
++) {
930 spiceterm_clear_xy(vt
, x
, vt
->cy
);
934 /* clear from beginning of line */
935 for (x
= 0; x
<= vt
->cx
; x
++) {
936 spiceterm_clear_xy(vt
, x
, vt
->cy
);
940 /* clear entire line */
941 for(x
= 0; x
< vt
->width
; x
++) {
942 spiceterm_clear_xy(vt
, x
, vt
->cy
);
951 if (c
> vt
->height
- vt
->cy
)
952 c
= vt
->height
- vt
->cy
;
956 spiceterm_scroll_down(vt
, vt
->cy
, vt
->region_bottom
, c
);
962 if (c
> vt
->height
- vt
->cy
)
963 c
= vt
->height
- vt
->cy
;
967 spiceterm_scroll_up(vt
, vt
->cy
, vt
->region_bottom
, c
, 1);
973 spiceterm_scroll_down(vt
, vt
->region_top
, vt
->region_bottom
, c
);
979 spiceterm_scroll_up(vt
, vt
->region_top
, vt
->region_bottom
, c
, 1);
982 /* delete c character */
985 if (c
> vt
->width
- vt
->cx
)
986 c
= vt
->width
- vt
->cx
;
990 for (x
= vt
->cx
; x
< vt
->width
- c
; x
++) {
991 int y1
= (vt
->y_base
+ vt
->cy
) % vt
->total_height
;
992 TextCell
*dst
= &vt
->cells
[y1
* vt
->width
+ x
];
993 TextCell
*src
= dst
+ c
;
995 spiceterm_update_xy(vt
, x
+ c
, vt
->cy
);
997 src
->attrib
= vt
->default_attrib
;
998 spiceterm_update_xy(vt
, x
, vt
->cy
);
1002 /* save cursor position */
1003 spiceterm_save_cursor(vt
);
1006 /* restore cursor position */
1007 spiceterm_restore_cursor(vt
);
1010 /* erase c characters */
1014 if (c
> (vt
->width
- vt
->cx
)) c
= vt
->width
- vt
->cx
;
1016 for(i
= 0; i
< c
; i
++) {
1017 spiceterm_clear_xy(vt
, vt
->cx
+ i
, vt
->cy
);
1021 /* insert c character */
1023 if (c
> (vt
->width
- vt
->cx
)) {
1024 c
= vt
->width
- vt
->cx
;
1028 for (x
= vt
->width
- c
; x
>= vt
->cx
; x
--) {
1029 int y1
= (vt
->y_base
+ vt
->cy
) % vt
->total_height
;
1030 TextCell
*src
= &vt
->cells
[y1
* vt
->width
+ x
];
1031 TextCell
*dst
= src
+ c
;
1033 spiceterm_update_xy (vt
, x
+ c
, vt
->cy
);
1035 src
->attrib
= vt
->cur_attrib
;
1036 spiceterm_update_xy(vt
, x
, vt
->cy
);
1042 if (!vt
->esc_buf
[0])
1044 if (!vt
->esc_buf
[1])
1045 vt
->esc_buf
[1] = vt
->height
;
1046 /* Minimum allowed region is 2 lines */
1047 if (vt
->esc_buf
[0] < vt
->esc_buf
[1] &&
1048 vt
->esc_buf
[1] <= vt
->height
) {
1049 vt
->region_top
= vt
->esc_buf
[0] - 1;
1050 vt
->region_bottom
= vt
->esc_buf
[1];
1052 vt
->cy
= vt
->region_top
;
1053 DPRINTF(1, "set region %d %d", vt
->region_top
, vt
->region_bottom
);
1059 debug_print_escape_buffer(vt
, __func__
, " unhandled escape", qes
, ch
);
1065 case ESsetG0
: // Set G0
1066 vt
->tty_state
= ESnormal
;
1069 vt
->g0enc
= GRAF_MAP
;
1071 vt
->g0enc
= LAT1_MAP
;
1073 vt
->g0enc
= IBMPC_MAP
;
1075 vt
->g0enc
= USER_MAP
;
1077 if (vt
->charset
== 0)
1078 vt
->cur_enc
= vt
->g0enc
;
1081 case ESsetG1
: // Set G1
1082 vt
->tty_state
= ESnormal
;
1085 vt
->g1enc
= GRAF_MAP
;
1087 vt
->g1enc
= LAT1_MAP
;
1089 vt
->g1enc
= IBMPC_MAP
;
1091 vt
->g1enc
= USER_MAP
;
1093 if (vt
->charset
== 1)
1094 vt
->cur_enc
= vt
->g1enc
;
1097 case ESidquery
: // vt100 query id
1098 vt
->tty_state
= ESnormal
;
1101 DPRINTF(1, "ESC[>c Query term ID");
1102 spiceterm_respond_esc(vt
, TERMIDCODE
);
1106 vt
->tty_state
= ESnormal
;
1108 case '@': /* defined in ISO 2022 */
1111 case 'G': /* prelim official escape code */
1112 case '8': /* retained for compatibility */
1117 default: // ESnormal
1118 vt
->tty_state
= ESnormal
;
1123 case 7: /* alert aka. bell */
1125 //rfbSendBell(vt->screen);
1127 case 8: /* backspace */
1131 case 9: /* tabspace */
1132 if (vt
->cx
+ (8 - (vt
->cx
% 8)) > vt
->width
) {
1134 spiceterm_put_lf(vt
);
1136 vt
->cx
= vt
->cx
+ (8 - (vt
->cx
% 8));
1142 spiceterm_put_lf(vt
);
1144 case 13: /* carriage return */
1148 /* SI (shift in), select character set 1 */
1150 vt
->cur_enc
= vt
->g1enc
;
1151 /* fixme: display controls = 1 */
1154 /* SO (shift out), select character set 0 */
1156 vt
->cur_enc
= vt
->g0enc
;
1157 /* fixme: display controls = 0 */
1160 vt
->tty_state
= ESesc
;
1162 case 127: /* delete */
1165 case 128+27: /* csi */
1166 vt
->tty_state
= ESsquare
;
1169 if (vt
->cx
>= vt
->width
) {
1172 spiceterm_put_lf(vt
);
1175 int y1
= (vt
->y_base
+ vt
->cy
) % vt
->total_height
;
1176 TextCell
*c
= &vt
->cells
[y1
*vt
->width
+ vt
->cx
];
1177 c
->attrib
= vt
->cur_attrib
;
1179 spiceterm_update_xy(vt
, vt
->cx
, vt
->cy
);
1188 spiceterm_puts(spiceTerm
*vt
, const char *buf
, int len
)
1192 spiceterm_show_cursor(vt
, 0);
1195 unsigned char c
= *buf
;
1199 if (vt
->tty_state
!= ESnormal
) {
1200 // never translate escape sequence
1202 } else if (vt
->utf8
&& !vt
->cur_enc
) {
1204 if(c
& 0x80) { // utf8 multi-byte sequence
1206 if (vt
->utf_count
> 0 && (c
& 0xc0) == 0x80) {
1207 // inside UTF8 sequence
1208 vt
->utf_char
= (vt
->utf_char
<< 6) | (c
& 0x3f);
1210 if (vt
->utf_count
== 0) {
1216 // first char of a UTF8 sequence
1217 if ((c
& 0xe0) == 0xc0) {
1219 vt
->utf_char
= (c
& 0x1f);
1220 } else if ((c
& 0xf0) == 0xe0) {
1222 vt
->utf_char
= (c
& 0x0f);
1223 } else if ((c
& 0xf8) == 0xf0) {
1225 vt
->utf_char
= (c
& 0x07);
1226 } else if ((c
& 0xfc) == 0xf8) {
1228 vt
->utf_char
= (c
& 0x03);
1229 } else if ((c
& 0xfe) == 0xfc) {
1231 vt
->utf_char
= (c
& 0x01);
1244 // never translate controls
1245 if (c
>= 32 && c
!= 127 && c
!= (128+27)) {
1246 tc
= translations
[vt
->cur_enc
][c
& 0x0ff];
1252 spiceterm_putchar(vt
, tc
);
1255 spiceterm_show_cursor(vt
, 1);
1262 spiceterm_set_xcut_text(char* str, int len, struct _rfbClientRec* cl)
1264 spiceTerm *vt =(spiceTerm *)cl->screen->screenData;
1266 // seems str is Latin-1 encoded
1267 if (vt->selection) free (vt->selection);
1268 vt->selection = (gunichar2 *)malloc (len*sizeof (gunichar2));
1270 for (i = 0; i < len; i++) {
1271 vt->selection[i] = str[i] & 0xff;
1273 vt->selection_len = len;
1278 spiceterm_update_watch_mask(spiceTerm
*vt
, gboolean writable
)
1280 g_assert(vt
!= NULL
);
1282 int mask
= SPICE_WATCH_EVENT_READ
;
1285 mask
|= SPICE_WATCH_EVENT_WRITE
;
1288 vt
->screen
->core
->watch_update_mask(vt
->screen
->mwatch
, mask
);
1292 mouse_report(spiceTerm
*vt
, int butt
, int mrx
, int mry
)
1296 sprintf(buf
, "[M%c%c%c", (char)(' ' + butt
), (char)('!' + mrx
),
1299 spiceterm_respond_esc(vt
, buf
);
1301 spiceterm_update_watch_mask(vt
, TRUE
);
1305 spiceterm_respond_unichar2(spiceTerm
*vt
, gunichar2 uc
)
1309 gint len
= g_unichar_to_utf8(uc
, buf
);
1312 if ((vt
->ibuf_count
+ len
) < IBUFSIZE
) {
1314 for (i
= 0; i
< len
; i
++) {
1315 vt
->ibuf
[vt
->ibuf_count
++] = buf
[i
];
1318 fprintf(stderr
, "warning: input buffer overflow\n");
1322 if ((vt
->ibuf_count
+ 1) < IBUFSIZE
) {
1323 vt
->ibuf
[vt
->ibuf_count
++] = (char)uc
;
1325 fprintf(stderr
, "warning: input buffer overflow\n");
1331 my_kbd_push_key(SpiceKbdInstance
*sin
, uint8_t frag
)
1333 // spiceTerm *vt = SPICE_CONTAINEROF(sin, spiceTerm, keyboard_sin);
1335 /* we no not need this */
1341 spiceterm_push_keyval(spiceTerm
*vt
, uint32_t keySym
, uint32_t flags
)
1343 static int control
= 0;
1344 static int shift
= 0;
1349 DPRINTF(1, "flags=%d keySym=%08x", flags
, keySym
);
1351 if (flags
& VD_AGENT_KEYVAL_FLAG_DOWN
) {
1352 if (keySym
== GDK_KEY_Shift_L
|| keySym
== GDK_KEY_Shift_R
) {
1354 } if (keySym
== GDK_KEY_Control_L
|| keySym
== GDK_KEY_Control_R
) {
1356 } else if (vt
->ibuf_count
< (IBUFSIZE
- 32)) {
1359 if(keySym
>= 'a' && keySym
<= 'z')
1360 uc
= keySym
- 'a' + 1;
1361 else if (keySym
>= 'A' && keySym
<= 'Z')
1362 uc
= keySym
- 'A' + 1;
1368 case GDK_KEY_Escape
:
1370 case GDK_KEY_Return
:
1372 case GDK_KEY_BackSpace
:
1376 case GDK_KEY_Delete
: /* kdch1 */
1377 case GDK_KEY_KP_Delete
:
1379 case GDK_KEY_Home
: /* khome */
1380 case GDK_KEY_KP_Home
:
1383 case GDK_KEY_KP_End
: /* kend */
1385 case GDK_KEY_Insert
: /* kich1 */
1386 case GDK_KEY_KP_Insert
:
1389 case GDK_KEY_KP_Up
: /* kcuu1 */
1391 case GDK_KEY_Down
: /* kcud1 */
1392 case GDK_KEY_KP_Down
:
1395 case GDK_KEY_KP_Right
: /* kcuf1 */
1398 case GDK_KEY_KP_Left
: /* kcub1 */
1400 case GDK_KEY_Page_Up
:
1402 spiceterm_virtual_scroll (vt
, -vt
->height
/2);
1406 case GDK_KEY_Page_Down
:
1408 spiceterm_virtual_scroll (vt
, vt
->height
/2);
1437 if (keySym
< 0x100) {
1444 DPRINTF(1, "escape=%s unicode=%08x\n", esc
, uc
);
1446 if (vt
->y_displ
!= vt
->y_base
) {
1447 vt
->y_displ
= vt
->y_base
;
1448 spiceterm_refresh(vt
);
1452 spiceterm_respond_esc(vt
, esc
);
1453 } else if (uc
> 0) {
1454 spiceterm_respond_unichar2(vt
, uc
);
1461 if (!(flags
& VD_AGENT_KEYVAL_FLAG_DOWN
)) { // UP
1462 if (keySym
== GDK_KEY_Shift_L
|| keySym
== GDK_KEY_Shift_R
) {
1464 } else if (keySym
== GDK_KEY_Control_L
|| keySym
== GDK_KEY_Control_R
) {
1469 spiceterm_update_watch_mask(vt
, TRUE
);
1473 my_kbd_get_leds(SpiceKbdInstance
*sin
)
1478 static SpiceKbdInterface my_keyboard_sif
= {
1479 .base
.type
= SPICE_INTERFACE_KEYBOARD
,
1480 .base
.description
= "spiceterm keyboard device",
1481 .base
.major_version
= SPICE_INTERFACE_KEYBOARD_MAJOR
,
1482 .base
.minor_version
= SPICE_INTERFACE_KEYBOARD_MINOR
,
1483 .push_scan_freg
= my_kbd_push_key
,
1484 .get_leds
= my_kbd_get_leds
,
1488 /* vdagent interface - to get mouse/clipboarde support */
1490 #define VDAGENT_WBUF_SIZE (1024*50)
1491 static unsigned char vdagent_write_buffer
[VDAGENT_WBUF_SIZE
];
1492 static int vdagent_write_buffer_pos
= 0;
1493 static int agent_owns_clipboard
[256] = { 0, };
1496 spiceterm_motion_event(spiceTerm
*vt
, uint32_t x
, uint32_t y
, uint32_t buttons
)
1498 DPRINTF(1, "mask=%08x x=%d y=%d", buttons
, x
,y
);
1500 static int last_mask
= 0;
1501 static int sel_start_pos
= 0;
1502 static int sel_end_pos
= 0;
1503 static int button2_released
= 1;
1510 if (cx
>= vt
->width
) cx
= vt
->width
- 1;
1512 if (cy
>= vt
->height
) cy
= vt
->height
- 1;
1514 if (vt
->report_mouse
&& buttons
!= last_mask
) {
1515 last_mask
= buttons
;
1517 mouse_report(vt
, 0, cx
, cy
);
1520 mouse_report (vt
, 1, cx
, cy
);
1523 mouse_report(vt
, 2, cx
, cy
);
1526 mouse_report(vt
, 3, cx
, cy
);
1532 if(button2_released
) {
1534 if (agent_owns_clipboard
[VD_AGENT_CLIPBOARD_SELECTION_PRIMARY
]) {
1535 if (vt
->selection
) {
1537 for(i
= 0; i
< vt
->selection_len
; i
++) {
1538 spiceterm_respond_unichar2(vt
, vt
->selection
[i
]);
1540 spiceterm_update_watch_mask(vt
, TRUE
);
1541 if (vt
->y_displ
!= vt
->y_base
) {
1542 vt
->y_displ
= vt
->y_base
;
1543 spiceterm_refresh(vt
);
1547 vdagent_request_clipboard(vt
, VD_AGENT_CLIPBOARD_SELECTION_PRIMARY
);
1551 button2_released
= 0;
1553 button2_released
= 1;
1557 int pos
= cy
*vt
->width
+ cx
;
1559 // code borrowed from libvncserver (VNCconsole.c)
1561 if (!vt
->mark_active
) {
1563 spiceterm_unselect_all(vt
);
1565 vt
->mark_active
= 1;
1566 sel_start_pos
= sel_end_pos
= pos
;
1567 spiceterm_toggle_marked_cell(vt
, pos
);
1571 if (pos
!= sel_end_pos
) {
1572 if (pos
> sel_end_pos
) {
1573 cx
= sel_end_pos
; cy
=pos
;
1575 cx
=pos
; cy
=sel_end_pos
;
1578 if (cx
< sel_start_pos
) {
1579 if (cy
< sel_start_pos
) cy
--;
1585 spiceterm_toggle_marked_cell(vt
, cx
);
1593 } else if (vt
->mark_active
) {
1594 vt
->mark_active
= 0;
1596 if (sel_start_pos
> sel_end_pos
) {
1597 int tmp
= sel_start_pos
- 1;
1598 sel_start_pos
= sel_end_pos
;
1602 int len
= sel_end_pos
- sel_start_pos
+ 1;
1604 if (vt
->selection
) free (vt
->selection
);
1605 vt
->selection
= (gunichar2
*)malloc (len
*sizeof(gunichar2
));
1606 vt
->selection_len
= len
;
1608 for (i
= 0; i
< len
; i
++) {
1609 int pos
= sel_start_pos
+ i
;
1610 int x
= pos
% vt
->width
;
1611 int y1
= ((pos
/ vt
->width
) + vt
->y_displ
) % vt
->total_height
;
1612 TextCell
*c
= &vt
->cells
[y1
*vt
->width
+ x
];
1613 vt
->selection
[i
] = c
->ch
;
1617 DPRINTF(1, "selection length = %d", vt
->selection_len
);
1619 vdagent_grab_clipboard(vt
, VD_AGENT_CLIPBOARD_SELECTION_PRIMARY
);
1624 vdagent_reply(spiceTerm
*vt
, uint32_t type
, uint32_t error
)
1628 size
= sizeof(VDAgentReply
);
1630 int msg_size
= sizeof(VDIChunkHeader
) + sizeof(VDAgentMessage
) + size
;
1631 g_assert((vdagent_write_buffer_pos
+ msg_size
) < VDAGENT_WBUF_SIZE
);
1633 unsigned char *buf
= vdagent_write_buffer
+ vdagent_write_buffer_pos
;
1634 vdagent_write_buffer_pos
+= msg_size
;
1636 memset(buf
, 0, msg_size
);
1638 VDIChunkHeader
*hdr
= (VDIChunkHeader
*)buf
;
1639 VDAgentMessage
*msg
= (VDAgentMessage
*)&hdr
[1];
1640 VDAgentReply
*reply
= (VDAgentReply
*)&msg
[1];
1642 reply
->error
= error
;
1644 hdr
->port
= VDP_CLIENT_PORT
;
1645 hdr
->size
= sizeof(VDAgentMessage
) + size
;
1647 msg
->protocol
= VD_AGENT_PROTOCOL
;
1648 msg
->type
= VD_AGENT_REPLY
;
1652 spice_server_char_device_wakeup(&vt
->vdagent_sin
);
1656 dump_message(unsigned char *buf
, int size
)
1660 for (i
= 0; i
< size
; i
++) {
1661 printf("%d %02X\n", i
, buf
[i
]);
1667 static void vdagent_send_capabilities(spiceTerm
*vt
, uint32_t request
)
1669 VDAgentAnnounceCapabilities
*caps
;
1672 size
= sizeof(*caps
) + VD_AGENT_CAPS_BYTES
;
1673 caps
= calloc(1, size
);
1674 g_assert(caps
!= NULL
);
1676 caps
->request
= request
;
1677 VD_AGENT_SET_CAPABILITY(caps
->caps
, VD_AGENT_CAP_MOUSE_STATE
);
1678 VD_AGENT_SET_CAPABILITY(caps
->caps
, VD_AGENT_CAP_MONITORS_CONFIG
);
1679 VD_AGENT_SET_CAPABILITY(caps
->caps
, VD_AGENT_CAP_REPLY
);
1680 VD_AGENT_SET_CAPABILITY(caps
->caps
, VD_AGENT_CAP_CLIPBOARD_BY_DEMAND
);
1681 VD_AGENT_SET_CAPABILITY(caps
->caps
, VD_AGENT_CAP_CLIPBOARD_SELECTION
);
1682 VD_AGENT_SET_CAPABILITY(caps
->caps
, VD_AGENT_CAP_SPARSE_MONITORS_CONFIG
);
1683 VD_AGENT_SET_CAPABILITY(caps
->caps
, VD_AGENT_CAP_GUEST_LINEEND_LF
);
1684 VD_AGENT_SET_CAPABILITY(caps
->caps
, VD_AGENT_CAP_KEYVAL
);
1686 int msg_size
= sizeof(VDIChunkHeader
) + sizeof(VDAgentMessage
) + size
;
1687 g_assert((vdagent_write_buffer_pos
+ msg_size
) < VDAGENT_WBUF_SIZE
);
1689 unsigned char *buf
= vdagent_write_buffer
+ vdagent_write_buffer_pos
;
1690 vdagent_write_buffer_pos
+= msg_size
;
1692 VDIChunkHeader
*hdr
= (VDIChunkHeader
*)buf
;
1693 VDAgentMessage
*msg
= (VDAgentMessage
*)&hdr
[1];
1695 hdr
->port
= VDP_CLIENT_PORT
;
1696 hdr
->size
= sizeof(VDAgentMessage
) + size
;
1697 msg
->protocol
= VD_AGENT_PROTOCOL
;
1698 msg
->type
= VD_AGENT_ANNOUNCE_CAPABILITIES
;
1702 memcpy(buf
+ sizeof(VDIChunkHeader
) + sizeof(VDAgentMessage
), (uint8_t *)caps
, size
);
1704 if (0) dump_message(buf
, msg_size
);
1706 spice_server_char_device_wakeup(&vt
->vdagent_sin
);
1711 static void vdagent_grab_clipboard(spiceTerm
*vt
, uint8_t selection
)
1715 agent_owns_clipboard
[selection
] = 1;
1719 int msg_size
= sizeof(VDIChunkHeader
) + sizeof(VDAgentMessage
) + size
;
1720 g_assert((vdagent_write_buffer_pos
+ msg_size
) < VDAGENT_WBUF_SIZE
);
1722 unsigned char *buf
= vdagent_write_buffer
+ vdagent_write_buffer_pos
;
1723 vdagent_write_buffer_pos
+= msg_size
;
1725 memset(buf
, 0, msg_size
);
1727 VDIChunkHeader
*hdr
= (VDIChunkHeader
*)buf
;
1728 VDAgentMessage
*msg
= (VDAgentMessage
*)&hdr
[1];
1729 uint8_t *grab
= (uint8_t *)&msg
[1];
1730 *((uint8_t *)grab
) = selection
;
1731 *((uint32_t *)(grab
+ 4)) = VD_AGENT_CLIPBOARD_UTF8_TEXT
;
1733 hdr
->port
= VDP_CLIENT_PORT
;
1734 hdr
->size
= sizeof(VDAgentMessage
) + size
;
1736 msg
->protocol
= VD_AGENT_PROTOCOL
;
1737 msg
->type
= VD_AGENT_CLIPBOARD_GRAB
;
1741 if (0) dump_message(buf
, msg_size
);
1743 spice_server_char_device_wakeup(&vt
->vdagent_sin
);
1746 static void vdagent_request_clipboard(spiceTerm
*vt
, uint8_t selection
)
1750 agent_owns_clipboard
[selection
] = 1;
1752 size
= 4 + sizeof(VDAgentClipboardRequest
);
1754 int msg_size
= sizeof(VDIChunkHeader
) + sizeof(VDAgentMessage
) + size
;
1755 g_assert((vdagent_write_buffer_pos
+ msg_size
) < VDAGENT_WBUF_SIZE
);
1757 unsigned char *buf
= vdagent_write_buffer
+ vdagent_write_buffer_pos
;
1758 vdagent_write_buffer_pos
+= msg_size
;
1760 memset(buf
, 0, msg_size
);
1762 VDIChunkHeader
*hdr
= (VDIChunkHeader
*)buf
;
1763 VDAgentMessage
*msg
= (VDAgentMessage
*)&hdr
[1];
1764 uint8_t *data
= (uint8_t *)&msg
[1];
1765 *((uint32_t *)data
) = 0;
1766 data
[0] = selection
;
1767 ((uint32_t *)data
)[1] = VD_AGENT_CLIPBOARD_UTF8_TEXT
;
1769 hdr
->port
= VDP_CLIENT_PORT
;
1770 hdr
->size
= sizeof(VDAgentMessage
) + size
;
1772 msg
->protocol
= VD_AGENT_PROTOCOL
;
1773 msg
->type
= VD_AGENT_CLIPBOARD_REQUEST
;
1777 if (0) dump_message(buf
, msg_size
);
1779 spice_server_char_device_wakeup(&vt
->vdagent_sin
);
1782 static void vdagent_send_clipboard(spiceTerm
*vt
, uint8_t selection
)
1786 if (selection
!= VD_AGENT_CLIPBOARD_SELECTION_PRIMARY
) {
1787 fprintf(stderr
, "clipboard select %d is not supported\n", selection
);
1794 sel_data
= g_utf16_to_utf8(vt
->selection
, vt
->selection_len
, NULL
, &sel_len
, NULL
);
1796 sel_len
= vt
->selection_len
;
1797 sel_data
= g_malloc(sel_len
);
1799 for (i
= 0; i
< sel_len
; i
++) { sel_data
[i
] = (char)vt
->selection
[i
]; }
1800 sel_data
[sel_len
] = 0;
1805 int msg_size
= sizeof(VDIChunkHeader
) + sizeof(VDAgentMessage
) + size
;
1806 g_assert((vdagent_write_buffer_pos
+ msg_size
) < VDAGENT_WBUF_SIZE
);
1808 unsigned char *buf
= vdagent_write_buffer
+ vdagent_write_buffer_pos
;
1809 vdagent_write_buffer_pos
+= msg_size
;
1811 memset(buf
, 0, msg_size
);
1813 VDIChunkHeader
*hdr
= (VDIChunkHeader
*)buf
;
1814 VDAgentMessage
*msg
= (VDAgentMessage
*)&hdr
[1];
1815 uint8_t *data
= (uint8_t *)&msg
[1];
1816 *((uint8_t *)data
) = selection
;
1818 *((uint32_t *)data
) = VD_AGENT_CLIPBOARD_UTF8_TEXT
;
1821 memcpy(data
, sel_data
, sel_len
);
1824 hdr
->port
= VDP_CLIENT_PORT
;
1825 hdr
->size
= sizeof(VDAgentMessage
) + size
;
1827 msg
->protocol
= VD_AGENT_PROTOCOL
;
1828 msg
->type
= VD_AGENT_CLIPBOARD
;
1832 spice_server_char_device_wakeup(&vt
->vdagent_sin
);
1836 vmc_write(SpiceCharDeviceInstance
*sin
, const uint8_t *buf
, int len
)
1838 spiceTerm
*vt
= SPICE_CONTAINEROF(sin
, spiceTerm
, vdagent_sin
);
1840 VDIChunkHeader
*hdr
= (VDIChunkHeader
*)buf
;
1841 VDAgentMessage
*msg
= (VDAgentMessage
*)&hdr
[1];
1843 //g_assert(hdr->port == VDP_SERVER_PORT);
1844 g_assert(msg
->protocol
== VD_AGENT_PROTOCOL
);
1846 DPRINTF(1, "%d %d %d %d", len
, hdr
->port
, msg
->protocol
, msg
->type
);
1848 switch (msg
->type
) {
1849 case VD_AGENT_KEYVAL
: {
1850 VDAgentKeyval
*info
= (VDAgentKeyval
*)&msg
[1];
1851 spiceterm_push_keyval(vt
, info
->keyval
, info
->flags
);
1854 case VD_AGENT_MOUSE_STATE
: {
1855 VDAgentMouseState
*info
= (VDAgentMouseState
*)&msg
[1];
1856 spiceterm_motion_event(vt
, info
->x
, info
->y
, info
->buttons
);
1859 case VD_AGENT_ANNOUNCE_CAPABILITIES
: {
1860 VDAgentAnnounceCapabilities
*caps
= (VDAgentAnnounceCapabilities
*)&msg
[1];
1861 DPRINTF(1, "VD_AGENT_ANNOUNCE_CAPABILITIES %d", caps
->request
);
1864 int caps_size
= VD_AGENT_CAPS_SIZE_FROM_MSG_SIZE(hdr
->size
);
1865 for (i
= 0; i
< VD_AGENT_END_CAP
; i
++) {
1866 DPRINTF(1, "CAPABILITIES %d %d", i
, VD_AGENT_HAS_CAPABILITY(caps
->caps
, caps_size
, i
));
1869 vdagent_send_capabilities(vt
, 0);
1872 case VD_AGENT_CLIPBOARD_GRAB
: {
1873 VDAgentClipboardGrab
*grab
= (VDAgentClipboardGrab
*)&msg
[1];
1874 uint8_t selection
= *((uint8_t *)grab
);
1875 DPRINTF(1, "VD_AGENT_CLIPBOARD_GRAB %d", selection
);
1876 agent_owns_clipboard
[selection
] = 0;
1879 case VD_AGENT_CLIPBOARD_REQUEST
: {
1880 uint8_t *req
= (uint8_t *)&msg
[1];
1881 uint8_t selection
= *((uint8_t *)req
);
1882 uint32_t type
= *((uint32_t *)(req
+ 4));
1884 DPRINTF(1, "VD_AGENT_CLIPBOARD_REQUEST %d %d", selection
, type
);
1886 vdagent_send_clipboard(vt
, selection
);
1890 case VD_AGENT_CLIPBOARD
: {
1891 uint8_t *data
= (uint8_t *)&msg
[1];
1892 uint8_t selection
= data
[0];
1893 uint32_t type
= *(uint32_t *)(data
+ 4);
1894 int size
= msg
->size
- 8;
1895 DPRINTF(1, "VD_AGENT_CLIPBOARD %d %d %d", selection
, type
, size
);
1897 if (type
== VD_AGENT_CLIPBOARD_UTF8_TEXT
) {
1899 for (i
= 0; i
< size
; i
++) {
1900 if ((vt
->ibuf_count
+ 1) < IBUFSIZE
) {
1901 vt
->ibuf
[vt
->ibuf_count
++] = *(char *)(data
+ 8 + i
);
1903 fprintf(stderr
, "warning: input buffer overflow\n");
1906 spiceterm_update_watch_mask(vt
, TRUE
);
1910 case VD_AGENT_CLIPBOARD_RELEASE
: {
1911 uint8_t *data
= (uint8_t *)&msg
[1];
1912 uint8_t selection
= data
[0];
1914 DPRINTF(0, "VD_AGENT_CLIPBOARD_RELEASE %d", selection
);
1918 case VD_AGENT_MONITORS_CONFIG
: {
1919 VDAgentMonitorsConfig
*list
= (VDAgentMonitorsConfig
*)&msg
[1];
1920 g_assert(list
->num_of_monitors
> 0);
1921 DPRINTF(0, "VD_AGENT_MONITORS_CONFIG %d %d %d", list
->num_of_monitors
,
1922 list
->monitors
[0].width
, list
->monitors
[0].height
);
1924 spiceterm_resize(vt
, list
->monitors
[0].width
, list
->monitors
[0].height
);
1926 vdagent_reply(vt
, VD_AGENT_MONITORS_CONFIG
, VD_AGENT_SUCCESS
);
1930 DPRINTF(0, "got uknown vdagent message type %d\n", msg
->type
);
1937 vmc_read(SpiceCharDeviceInstance
*sin
, uint8_t *buf
, int len
)
1939 DPRINTF(1, "%d %d", len
, vdagent_write_buffer_pos
);
1942 if (!vdagent_write_buffer_pos
) {
1946 int size
= (len
>= vdagent_write_buffer_pos
) ? vdagent_write_buffer_pos
: len
;
1947 memcpy(buf
, vdagent_write_buffer
, size
);
1948 if (size
< vdagent_write_buffer_pos
) {
1949 memmove(vdagent_write_buffer
, vdagent_write_buffer
+ size
,
1950 vdagent_write_buffer_pos
- size
);
1952 vdagent_write_buffer_pos
-= size
;
1954 DPRINTF(1, "RET %d %d", size
, vdagent_write_buffer_pos
);
1959 vmc_state(SpiceCharDeviceInstance
*sin
, int connected
)
1964 static SpiceCharDeviceInterface my_vdagent_sif
= {
1965 .base
.type
= SPICE_INTERFACE_CHAR_DEVICE
,
1966 .base
.description
= "spice virtual channel char device",
1967 .base
.major_version
= SPICE_INTERFACE_CHAR_DEVICE_MAJOR
,
1968 .base
.minor_version
= SPICE_INTERFACE_CHAR_DEVICE_MINOR
,
1975 init_spiceterm(spiceTerm
*vt
, uint32_t width
, uint32_t height
)
1979 g_assert(vt
!= NULL
);
1980 g_assert(vt
->screen
!= NULL
);
1982 vt
->width
= width
/ 8;
1983 vt
->height
= height
/ 16;
1985 vt
->total_height
= vt
->height
* 20;
1986 vt
->scroll_height
= 0;
1991 vt
->region_bottom
= vt
->height
;
1993 vt
->g0enc
= LAT1_MAP
;
1994 vt
->g1enc
= GRAF_MAP
;
1995 vt
->cur_enc
= vt
->g0enc
;
1998 /* default text attributes */
1999 vt
->default_attrib
.bold
= 0;
2000 vt
->default_attrib
.uline
= 0;
2001 vt
->default_attrib
.blink
= 0;
2002 vt
->default_attrib
.invers
= 0;
2003 vt
->default_attrib
.unvisible
= 0;
2004 vt
->default_attrib
.fgcol
= 7;
2005 vt
->default_attrib
.bgcol
= 0;
2007 vt
->cur_attrib
= vt
->default_attrib
;
2017 vt
->cells
= (TextCell
*)calloc (sizeof (TextCell
), vt
->width
*vt
->total_height
);
2019 for (i
= 0; i
< vt
->width
*vt
->total_height
; i
++) {
2020 vt
->cells
[i
].ch
= ' ';
2021 vt
->cells
[i
].attrib
= vt
->default_attrib
;
2025 g_free(vt
->altcells
);
2028 vt
->altcells
= (TextCell
*)calloc (sizeof (TextCell
), vt
->width
*vt
->height
);
2032 spiceterm_resize(spiceTerm
*vt
, uint32_t width
, uint32_t height
)
2034 width
= (width
/8)*8;
2035 height
= (height
/16)*16;
2037 if (vt
->screen
->width
== width
&& vt
->screen
->height
== height
) {
2041 DPRINTF(0, "width=%u height=%u", width
, height
);
2043 spice_screen_resize(vt
->screen
, width
, height
);
2045 init_spiceterm(vt
, width
, height
);
2047 struct winsize dimensions
;
2048 dimensions
.ws_col
= vt
->width
;
2049 dimensions
.ws_row
= vt
->height
;
2051 ioctl(vt
->pty
, TIOCSWINSZ
, &dimensions
);
2055 create_spiceterm(int argc
, char** argv
, uint32_t maxx
, uint32_t maxy
, guint timeout
)
2057 SpiceCoreInterface
*core
= basic_event_loop_init();
2058 SpiceScreen
*spice_screen
= spice_screen_new(core
, maxx
, maxy
, timeout
);
2060 //spice_server_set_image_compression(server, SPICE_IMAGE_COMPRESS_OFF);
2062 spice_screen
->image_cache
= g_hash_table_new(g_int_hash
, g_int_equal
);
2064 spiceTerm
*vt
= (spiceTerm
*)calloc (sizeof(spiceTerm
), 1);
2066 vt
->keyboard_sin
.base
.sif
= &my_keyboard_sif
.base
;
2067 spice_server_add_interface(spice_screen
->server
, &vt
->keyboard_sin
.base
);
2069 vt
->vdagent_sin
.base
.sif
= &my_vdagent_sif
.base
;
2070 vt
->vdagent_sin
.subtype
= "vdagent";
2071 spice_server_add_interface(spice_screen
->server
, &vt
->vdagent_sin
.base
);
2072 vt
->screen
= spice_screen
;
2074 init_spiceterm(vt
, maxx
, maxy
);
2080 master_error_callback(GIOChannel
*channel
, GIOCondition condition
,
2083 //spiceTerm *vt = (spiceTerm *)data;
2085 DPRINTF(1, "condition %d", condition
);
2093 master_watch(int master
, int event
, void *opaque
)
2095 spiceTerm
*vt
= (spiceTerm
*)opaque
;
2098 // fixme: if (!vt->mark_active) {
2100 if (event
== SPICE_WATCH_EVENT_READ
) {
2102 while ((c
= read(master
, buffer
, 1024)) == -1) {
2103 if (errno
!= EAGAIN
) break;
2106 perror("master pipe read error"); // fixme
2108 spiceterm_puts (vt
, buffer
, c
);
2110 if (vt
->ibuf_count
> 0) {
2111 DPRINTF(1, "write input %x %d", vt
->ibuf
[0], vt
->ibuf_count
);
2112 if ((c
= write (master
, vt
->ibuf
, vt
->ibuf_count
)) >= 0) {
2113 if (c
== vt
->ibuf_count
) {
2116 // not all data written
2117 memmove(vt
->ibuf
, vt
->ibuf
+ c
, vt
->ibuf_count
- c
);
2118 vt
->ibuf_count
-= c
;
2120 // nothing written -ignore and try later
2123 perror("master pipe write error");
2126 if (vt
->ibuf_count
== 0) {
2127 spiceterm_update_watch_mask(vt
, FALSE
);
2133 main (int argc
, char** argv
)
2136 char **cmdargv
= NULL
;
2137 char *command
= "/bin/bash"; // execute normal shell as default
2141 struct winsize dimensions
;
2143 g_thread_init(NULL
);
2145 for (i
= 1; i
< argc
; i
++) {
2146 if (!strcmp (argv
[i
], "-c")) {
2147 command
= argv
[i
+1];
2148 cmdargv
= &argv
[i
+1];
2155 if (0) print_usage(NULL
); // fixme:
2157 spiceTerm
*vt
= create_spiceterm (argc
, argv
, 744, 400, 10);
2159 setlocale(LC_ALL
, ""); // set from environment
2161 char *ctype
= setlocale (LC_CTYPE
, NULL
); // query LC_CTYPE
2163 // fixme: ist there a standard way to detect utf8 mode ?
2164 if (strcasestr (ctype
, ".utf-8")||strcasestr (ctype
, ".utf8")) {
2168 dimensions
.ws_col
= vt
->width
;
2169 dimensions
.ws_row
= vt
->height
;
2171 setenv("TERM", TERM
, 1);
2173 DPRINTF(1, "execute %s", command
);
2175 pid
= forkpty (&master
, ptyname
, NULL
, &dimensions
);
2178 // install default signal handlers
2179 signal (SIGQUIT
, SIG_DFL
);
2180 signal (SIGTERM
, SIG_DFL
);
2181 signal (SIGINT
, SIG_DFL
);
2184 execvp (command
, cmdargv
);
2186 execlp (command
, command
, NULL
);
2188 perror ("Error: exec failed\n");
2189 exit (-1); // should not be reached
2190 } else if (pid
== -1) {
2191 perror ("Error: fork failed\n");
2197 /* watch for errors - we need to use glib directly because spice
2198 * does not have SPICE_WATCH_EVENT for this */
2199 GIOChannel
*channel
= g_io_channel_unix_new(master
);
2200 g_io_channel_set_flags(channel
, G_IO_FLAG_NONBLOCK
, NULL
);
2201 g_io_channel_set_encoding(channel
, NULL
, NULL
);
2202 g_io_add_watch(channel
, G_IO_ERR
|G_IO_HUP
, master_error_callback
, vt
);
2204 vt
->screen
->mwatch
= vt
->screen
->core
->watch_add(
2205 master
, SPICE_WATCH_EVENT_READ
/* |SPICE_WATCH_EVENT_WRITE */,
2208 basic_event_loop_mainloop();
2212 waitpid(pid
, &status
, 0);