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 my_kbd_push_keyval(SpiceKbdInstance
*sin
, uint32_t keySym
, int flags
)
1343 spiceTerm
*vt
= SPICE_CONTAINEROF(sin
, spiceTerm
, keyboard_sin
);
1344 static int control
= 0;
1345 static int shift
= 0;
1350 DPRINTF(1, "flags=%d keySym=%08x", flags
, keySym
);
1353 if (keySym
== GDK_KEY_Shift_L
|| keySym
== GDK_KEY_Shift_R
) {
1355 } if (keySym
== GDK_KEY_Control_L
|| keySym
== GDK_KEY_Control_R
) {
1357 } else if (vt
->ibuf_count
< (IBUFSIZE
- 32)) {
1360 if(keySym
>= 'a' && keySym
<= 'z')
1361 uc
= keySym
- 'a' + 1;
1362 else if (keySym
>= 'A' && keySym
<= 'Z')
1363 uc
= keySym
- 'A' + 1;
1369 case GDK_KEY_Escape
:
1371 case GDK_KEY_Return
:
1373 case GDK_KEY_BackSpace
:
1377 case GDK_KEY_Delete
: /* kdch1 */
1378 case GDK_KEY_KP_Delete
:
1380 case GDK_KEY_Home
: /* khome */
1381 case GDK_KEY_KP_Home
:
1384 case GDK_KEY_KP_End
: /* kend */
1386 case GDK_KEY_Insert
: /* kich1 */
1387 case GDK_KEY_KP_Insert
:
1390 case GDK_KEY_KP_Up
: /* kcuu1 */
1392 case GDK_KEY_Down
: /* kcud1 */
1393 case GDK_KEY_KP_Down
:
1396 case GDK_KEY_KP_Right
: /* kcuf1 */
1399 case GDK_KEY_KP_Left
: /* kcub1 */
1401 case GDK_KEY_Page_Up
:
1403 spiceterm_virtual_scroll (vt
, -vt
->height
/2);
1407 case GDK_KEY_Page_Down
:
1409 spiceterm_virtual_scroll (vt
, vt
->height
/2);
1438 if (keySym
< 0x100) {
1445 DPRINTF(1, "escape=%s unicode=%08x\n", esc
, uc
);
1447 if (vt
->y_displ
!= vt
->y_base
) {
1448 vt
->y_displ
= vt
->y_base
;
1449 spiceterm_refresh(vt
);
1453 spiceterm_respond_esc(vt
, esc
);
1454 } else if (uc
> 0) {
1455 spiceterm_respond_unichar2(vt
, uc
);
1463 if (flags
& 2) { // UP
1464 if (keySym
== GDK_KEY_Shift_L
|| keySym
== GDK_KEY_Shift_R
) {
1466 } else if (keySym
== GDK_KEY_Control_L
|| keySym
== GDK_KEY_Control_R
) {
1471 spiceterm_update_watch_mask(vt
, TRUE
);
1475 my_kbd_get_leds(SpiceKbdInstance
*sin
)
1480 static SpiceKbdInterface my_keyboard_sif
= {
1481 .base
.type
= SPICE_INTERFACE_KEYBOARD
,
1482 .base
.description
= "spiceterm keyboard device",
1483 .base
.major_version
= SPICE_INTERFACE_KEYBOARD_MAJOR
,
1484 .base
.minor_version
= SPICE_INTERFACE_KEYBOARD_MINOR
,
1485 .push_keyval
= my_kbd_push_keyval
,
1486 .push_scan_freg
= my_kbd_push_key
,
1487 .get_leds
= my_kbd_get_leds
,
1491 /* vdagent interface - to get mouse/clipboarde support */
1493 #define VDAGENT_WBUF_SIZE (1024*50)
1494 static unsigned char vdagent_write_buffer
[VDAGENT_WBUF_SIZE
];
1495 static int vdagent_write_buffer_pos
= 0;
1496 static int agent_owns_clipboard
[256] = { 0, };
1499 spiceterm_motion_event(spiceTerm
*vt
, uint32_t x
, uint32_t y
, uint32_t buttons
)
1501 DPRINTF(1, "mask=%08x x=%d y=%d", buttons
, x
,y
);
1503 static int last_mask
= 0;
1504 static int sel_start_pos
= 0;
1505 static int sel_end_pos
= 0;
1506 static int button2_released
= 1;
1513 if (cx
>= vt
->width
) cx
= vt
->width
- 1;
1515 if (cy
>= vt
->height
) cy
= vt
->height
- 1;
1517 if (vt
->report_mouse
&& buttons
!= last_mask
) {
1518 last_mask
= buttons
;
1520 mouse_report(vt
, 0, cx
, cy
);
1523 mouse_report (vt
, 1, cx
, cy
);
1526 mouse_report(vt
, 2, cx
, cy
);
1529 mouse_report(vt
, 3, cx
, cy
);
1535 if(button2_released
) {
1537 if (agent_owns_clipboard
[VD_AGENT_CLIPBOARD_SELECTION_PRIMARY
]) {
1538 if (vt
->selection
) {
1540 for(i
= 0; i
< vt
->selection_len
; i
++) {
1541 spiceterm_respond_unichar2(vt
, vt
->selection
[i
]);
1543 spiceterm_update_watch_mask(vt
, TRUE
);
1544 if (vt
->y_displ
!= vt
->y_base
) {
1545 vt
->y_displ
= vt
->y_base
;
1546 spiceterm_refresh(vt
);
1550 vdagent_request_clipboard(vt
, VD_AGENT_CLIPBOARD_SELECTION_PRIMARY
);
1554 button2_released
= 0;
1556 button2_released
= 1;
1560 int pos
= cy
*vt
->width
+ cx
;
1562 // code borrowed from libvncserver (VNCconsole.c)
1564 if (!vt
->mark_active
) {
1566 spiceterm_unselect_all(vt
);
1568 vt
->mark_active
= 1;
1569 sel_start_pos
= sel_end_pos
= pos
;
1570 spiceterm_toggle_marked_cell(vt
, pos
);
1574 if (pos
!= sel_end_pos
) {
1575 if (pos
> sel_end_pos
) {
1576 cx
= sel_end_pos
; cy
=pos
;
1578 cx
=pos
; cy
=sel_end_pos
;
1581 if (cx
< sel_start_pos
) {
1582 if (cy
< sel_start_pos
) cy
--;
1588 spiceterm_toggle_marked_cell(vt
, cx
);
1596 } else if (vt
->mark_active
) {
1597 vt
->mark_active
= 0;
1599 if (sel_start_pos
> sel_end_pos
) {
1600 int tmp
= sel_start_pos
- 1;
1601 sel_start_pos
= sel_end_pos
;
1605 int len
= sel_end_pos
- sel_start_pos
+ 1;
1607 if (vt
->selection
) free (vt
->selection
);
1608 vt
->selection
= (gunichar2
*)malloc (len
*sizeof(gunichar2
));
1609 vt
->selection_len
= len
;
1611 for (i
= 0; i
< len
; i
++) {
1612 int pos
= sel_start_pos
+ i
;
1613 int x
= pos
% vt
->width
;
1614 int y1
= ((pos
/ vt
->width
) + vt
->y_displ
) % vt
->total_height
;
1615 TextCell
*c
= &vt
->cells
[y1
*vt
->width
+ x
];
1616 vt
->selection
[i
] = c
->ch
;
1620 DPRINTF(1, "selection length = %d", vt
->selection_len
);
1622 vdagent_grab_clipboard(vt
, VD_AGENT_CLIPBOARD_SELECTION_PRIMARY
);
1627 vdagent_reply(spiceTerm
*vt
, uint32_t type
, uint32_t error
)
1631 size
= sizeof(VDAgentReply
);
1633 int msg_size
= sizeof(VDIChunkHeader
) + sizeof(VDAgentMessage
) + size
;
1634 g_assert((vdagent_write_buffer_pos
+ msg_size
) < VDAGENT_WBUF_SIZE
);
1636 unsigned char *buf
= vdagent_write_buffer
+ vdagent_write_buffer_pos
;
1637 vdagent_write_buffer_pos
+= msg_size
;
1639 memset(buf
, 0, msg_size
);
1641 VDIChunkHeader
*hdr
= (VDIChunkHeader
*)buf
;
1642 VDAgentMessage
*msg
= (VDAgentMessage
*)&hdr
[1];
1643 VDAgentReply
*reply
= (VDAgentReply
*)&msg
[1];
1645 reply
->error
= error
;
1647 hdr
->port
= VDP_CLIENT_PORT
;
1648 hdr
->size
= sizeof(VDAgentMessage
) + size
;
1650 msg
->protocol
= VD_AGENT_PROTOCOL
;
1651 msg
->type
= VD_AGENT_REPLY
;
1655 spice_server_char_device_wakeup(&vt
->vdagent_sin
);
1658 static void vdagent_send_capabilities(spiceTerm
*vt
, uint32_t request
)
1660 VDAgentAnnounceCapabilities
*caps
;
1663 size
= sizeof(*caps
) + VD_AGENT_CAPS_BYTES
;
1664 caps
= calloc(1, size
);
1665 g_assert(caps
!= NULL
);
1667 caps
->request
= request
;
1668 VD_AGENT_SET_CAPABILITY(caps
->caps
, VD_AGENT_CAP_MOUSE_STATE
);
1669 VD_AGENT_SET_CAPABILITY(caps
->caps
, VD_AGENT_CAP_MONITORS_CONFIG
);
1670 VD_AGENT_SET_CAPABILITY(caps
->caps
, VD_AGENT_CAP_REPLY
);
1671 VD_AGENT_SET_CAPABILITY(caps
->caps
, VD_AGENT_CAP_CLIPBOARD_BY_DEMAND
);
1672 VD_AGENT_SET_CAPABILITY(caps
->caps
, VD_AGENT_CAP_CLIPBOARD_SELECTION
);
1673 VD_AGENT_SET_CAPABILITY(caps
->caps
, VD_AGENT_CAP_SPARSE_MONITORS_CONFIG
);
1674 VD_AGENT_SET_CAPABILITY(caps
->caps
, VD_AGENT_CAP_GUEST_LINEEND_LF
);
1676 int msg_size
= sizeof(VDIChunkHeader
) + sizeof(VDAgentMessage
) + size
;
1677 g_assert((vdagent_write_buffer_pos
+ msg_size
) < VDAGENT_WBUF_SIZE
);
1679 unsigned char *buf
= vdagent_write_buffer
+ vdagent_write_buffer_pos
;
1680 vdagent_write_buffer_pos
+= msg_size
;
1682 VDIChunkHeader
*hdr
= (VDIChunkHeader
*)buf
;
1683 VDAgentMessage
*msg
= (VDAgentMessage
*)&hdr
[1];
1685 hdr
->port
= VDP_CLIENT_PORT
;
1686 hdr
->size
= sizeof(VDAgentMessage
) + size
;
1687 msg
->protocol
= VD_AGENT_PROTOCOL
;
1688 msg
->type
= VD_AGENT_ANNOUNCE_CAPABILITIES
;
1692 memcpy(buf
+ sizeof(VDIChunkHeader
) + sizeof(VDAgentMessage
), (uint8_t *)caps
, size
);
1694 spice_server_char_device_wakeup(&vt
->vdagent_sin
);
1700 dump_message(unsigned char *buf
, int size
)
1704 for (i
= 0; i
< size
; i
++) {
1705 printf("%d %02X\n", i
, buf
[i
]);
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_MOUSE_STATE
: {
1850 VDAgentMouseState
*info
= (VDAgentMouseState
*)&msg
[1];
1851 spiceterm_motion_event(vt
, info
->x
, info
->y
, info
->buttons
);
1854 case VD_AGENT_ANNOUNCE_CAPABILITIES
: {
1855 VDAgentAnnounceCapabilities
*caps
= (VDAgentAnnounceCapabilities
*)&msg
[1];
1856 DPRINTF(1, "VD_AGENT_ANNOUNCE_CAPABILITIES %d", caps
->request
);
1859 int caps_size
= VD_AGENT_CAPS_SIZE_FROM_MSG_SIZE(hdr
->size
);
1860 for (i
= 0; i
< VD_AGENT_END_CAP
; i
++) {
1861 DPRINTF(1, "CAPABILITIES %d %d", i
, VD_AGENT_HAS_CAPABILITY(caps
->caps
, caps_size
, i
));
1864 vdagent_send_capabilities(vt
, 0);
1867 case VD_AGENT_CLIPBOARD_GRAB
: {
1868 VDAgentClipboardGrab
*grab
= (VDAgentClipboardGrab
*)&msg
[1];
1869 uint8_t selection
= *((uint8_t *)grab
);
1870 DPRINTF(1, "VD_AGENT_CLIPBOARD_GRAB %d", selection
);
1871 agent_owns_clipboard
[selection
] = 0;
1874 case VD_AGENT_CLIPBOARD_REQUEST
: {
1875 uint8_t *req
= (uint8_t *)&msg
[1];
1876 uint8_t selection
= *((uint8_t *)req
);
1877 uint32_t type
= *((uint32_t *)(req
+ 4));
1879 DPRINTF(1, "VD_AGENT_CLIPBOARD_REQUEST %d %d", selection
, type
);
1881 vdagent_send_clipboard(vt
, selection
);
1885 case VD_AGENT_CLIPBOARD
: {
1886 uint8_t *data
= (uint8_t *)&msg
[1];
1887 uint8_t selection
= data
[0];
1888 uint32_t type
= *(uint32_t *)(data
+ 4);
1889 int size
= msg
->size
- 8;
1890 DPRINTF(1, "VD_AGENT_CLIPBOARD %d %d %d", selection
, type
, size
);
1892 if (type
== VD_AGENT_CLIPBOARD_UTF8_TEXT
) {
1894 for (i
= 0; i
< size
; i
++) {
1895 if ((vt
->ibuf_count
+ 1) < IBUFSIZE
) {
1896 vt
->ibuf
[vt
->ibuf_count
++] = *(char *)(data
+ 8 + i
);
1898 fprintf(stderr
, "warning: input buffer overflow\n");
1901 spiceterm_update_watch_mask(vt
, TRUE
);
1905 case VD_AGENT_CLIPBOARD_RELEASE
: {
1906 uint8_t *data
= (uint8_t *)&msg
[1];
1907 uint8_t selection
= data
[0];
1909 DPRINTF(0, "VD_AGENT_CLIPBOARD_RELEASE %d", selection
);
1913 case VD_AGENT_MONITORS_CONFIG
: {
1914 VDAgentMonitorsConfig
*list
= (VDAgentMonitorsConfig
*)&msg
[1];
1915 g_assert(list
->num_of_monitors
> 0);
1916 DPRINTF(0, "VD_AGENT_MONITORS_CONFIG %d %d %d", list
->num_of_monitors
,
1917 list
->monitors
[0].width
, list
->monitors
[0].height
);
1919 spiceterm_resize(vt
, list
->monitors
[0].width
, list
->monitors
[0].height
);
1921 vdagent_reply(vt
, VD_AGENT_MONITORS_CONFIG
, VD_AGENT_SUCCESS
);
1925 DPRINTF(0, "got uknown vdagent message type %d\n", msg
->type
);
1932 vmc_read(SpiceCharDeviceInstance
*sin
, uint8_t *buf
, int len
)
1934 DPRINTF(1, "%d %d", len
, vdagent_write_buffer_pos
);
1937 if (!vdagent_write_buffer_pos
) {
1941 int size
= (len
>= vdagent_write_buffer_pos
) ? vdagent_write_buffer_pos
: len
;
1942 memcpy(buf
, vdagent_write_buffer
, size
);
1943 if (size
< vdagent_write_buffer_pos
) {
1944 memmove(vdagent_write_buffer
, vdagent_write_buffer
+ size
,
1945 vdagent_write_buffer_pos
- size
);
1947 vdagent_write_buffer_pos
-= size
;
1949 DPRINTF(1, "RET %d %d", size
, vdagent_write_buffer_pos
);
1954 vmc_state(SpiceCharDeviceInstance
*sin
, int connected
)
1959 static SpiceCharDeviceInterface my_vdagent_sif
= {
1960 .base
.type
= SPICE_INTERFACE_CHAR_DEVICE
,
1961 .base
.description
= "spice virtual channel char device",
1962 .base
.major_version
= SPICE_INTERFACE_CHAR_DEVICE_MAJOR
,
1963 .base
.minor_version
= SPICE_INTERFACE_CHAR_DEVICE_MINOR
,
1970 init_spiceterm(spiceTerm
*vt
, uint32_t width
, uint32_t height
)
1974 g_assert(vt
!= NULL
);
1975 g_assert(vt
->screen
!= NULL
);
1977 vt
->width
= width
/ 8;
1978 vt
->height
= height
/ 16;
1980 vt
->total_height
= vt
->height
* 20;
1981 vt
->scroll_height
= 0;
1986 vt
->region_bottom
= vt
->height
;
1988 vt
->g0enc
= LAT1_MAP
;
1989 vt
->g1enc
= GRAF_MAP
;
1990 vt
->cur_enc
= vt
->g0enc
;
1993 /* default text attributes */
1994 vt
->default_attrib
.bold
= 0;
1995 vt
->default_attrib
.uline
= 0;
1996 vt
->default_attrib
.blink
= 0;
1997 vt
->default_attrib
.invers
= 0;
1998 vt
->default_attrib
.unvisible
= 0;
1999 vt
->default_attrib
.fgcol
= 7;
2000 vt
->default_attrib
.bgcol
= 0;
2002 vt
->cur_attrib
= vt
->default_attrib
;
2012 vt
->cells
= (TextCell
*)calloc (sizeof (TextCell
), vt
->width
*vt
->total_height
);
2014 for (i
= 0; i
< vt
->width
*vt
->total_height
; i
++) {
2015 vt
->cells
[i
].ch
= ' ';
2016 vt
->cells
[i
].attrib
= vt
->default_attrib
;
2020 g_free(vt
->altcells
);
2023 vt
->altcells
= (TextCell
*)calloc (sizeof (TextCell
), vt
->width
*vt
->height
);
2027 spiceterm_resize(spiceTerm
*vt
, uint32_t width
, uint32_t height
)
2029 DPRINTF(0, "width=%u height=%u", width
, height
);
2031 if (vt
->screen
->width
== width
&& vt
->screen
->height
== height
) {
2035 spice_screen_resize(vt
->screen
, width
, height
);
2037 init_spiceterm(vt
, width
, height
);
2039 struct winsize dimensions
;
2040 dimensions
.ws_col
= vt
->width
;
2041 dimensions
.ws_row
= vt
->height
;
2043 ioctl(vt
->pty
, TIOCSWINSZ
, &dimensions
);
2047 create_spiceterm(int argc
, char** argv
, uint32_t maxx
, uint32_t maxy
, guint timeout
)
2049 SpiceCoreInterface
*core
= basic_event_loop_init();
2050 SpiceScreen
*spice_screen
= spice_screen_new(core
, maxx
, maxy
, timeout
);
2052 //spice_server_set_image_compression(server, SPICE_IMAGE_COMPRESS_OFF);
2054 spiceTerm
*vt
= (spiceTerm
*)calloc (sizeof(spiceTerm
), 1);
2056 vt
->keyboard_sin
.base
.sif
= &my_keyboard_sif
.base
;
2057 spice_server_add_interface(spice_screen
->server
, &vt
->keyboard_sin
.base
);
2059 vt
->vdagent_sin
.base
.sif
= &my_vdagent_sif
.base
;
2060 vt
->vdagent_sin
.subtype
= "vdagent";
2061 spice_server_add_interface(spice_screen
->server
, &vt
->vdagent_sin
.base
);
2062 vt
->screen
= spice_screen
;
2064 init_spiceterm(vt
, maxx
, maxy
);
2070 master_error_callback(GIOChannel
*channel
, GIOCondition condition
,
2073 //spiceTerm *vt = (spiceTerm *)data;
2075 DPRINTF(1, "condition %d", condition
);
2083 master_watch(int master
, int event
, void *opaque
)
2085 spiceTerm
*vt
= (spiceTerm
*)opaque
;
2088 // fixme: if (!vt->mark_active) {
2090 if (event
== SPICE_WATCH_EVENT_READ
) {
2092 while ((c
= read(master
, buffer
, 1024)) == -1) {
2093 if (errno
!= EAGAIN
) break;
2096 perror("master pipe read error"); // fixme
2098 spiceterm_puts (vt
, buffer
, c
);
2100 if (vt
->ibuf_count
> 0) {
2101 DPRINTF(1, "write input %x %d", vt
->ibuf
[0], vt
->ibuf_count
);
2102 if ((c
= write (master
, vt
->ibuf
, vt
->ibuf_count
)) >= 0) {
2103 if (c
== vt
->ibuf_count
) {
2106 // not all data written
2107 memmove(vt
->ibuf
, vt
->ibuf
+ c
, vt
->ibuf_count
- c
);
2108 vt
->ibuf_count
-= c
;
2110 // nothing written -ignore and try later
2113 perror("master pipe write error");
2116 if (vt
->ibuf_count
== 0) {
2117 spiceterm_update_watch_mask(vt
, FALSE
);
2123 main (int argc
, char** argv
)
2126 char **cmdargv
= NULL
;
2127 char *command
= "/bin/bash"; // execute normal shell as default
2131 struct winsize dimensions
;
2133 g_thread_init(NULL
);
2135 for (i
= 1; i
< argc
; i
++) {
2136 if (!strcmp (argv
[i
], "-c")) {
2137 command
= argv
[i
+1];
2138 cmdargv
= &argv
[i
+1];
2145 if (0) print_usage(NULL
); // fixme:
2147 spiceTerm
*vt
= create_spiceterm (argc
, argv
, 745, 400, 10);
2149 setlocale(LC_ALL
, ""); // set from environment
2151 char *ctype
= setlocale (LC_CTYPE
, NULL
); // query LC_CTYPE
2153 // fixme: ist there a standard way to detect utf8 mode ?
2154 if (strcasestr (ctype
, ".utf-8")||strcasestr (ctype
, ".utf8")) {
2158 dimensions
.ws_col
= vt
->width
;
2159 dimensions
.ws_row
= vt
->height
;
2161 setenv("TERM", TERM
, 1);
2163 DPRINTF(1, "execute %s", command
);
2165 pid
= forkpty (&master
, ptyname
, NULL
, &dimensions
);
2168 // install default signal handlers
2169 signal (SIGQUIT
, SIG_DFL
);
2170 signal (SIGTERM
, SIG_DFL
);
2171 signal (SIGINT
, SIG_DFL
);
2174 execvp (command
, cmdargv
);
2176 execlp (command
, command
, NULL
);
2178 perror ("Error: exec failed\n");
2179 exit (-1); // should not be reached
2180 } else if (pid
== -1) {
2181 perror ("Error: fork failed\n");
2187 /* watch for errors - we need to use glib directly because spice
2188 * does not have SPICE_WATCH_EVENT for this */
2189 GIOChannel
*channel
= g_io_channel_unix_new(master
);
2190 g_io_channel_set_flags(channel
, G_IO_FLAG_NONBLOCK
, NULL
);
2191 g_io_channel_set_encoding(channel
, NULL
, NULL
);
2192 g_io_add_watch(channel
, G_IO_ERR
|G_IO_HUP
, master_error_callback
, vt
);
2194 vt
->screen
->mwatch
= vt
->screen
->core
->watch_add(
2195 master
, SPICE_WATCH_EVENT_READ
/* |SPICE_WATCH_EVENT_WRITE */,
2198 basic_event_loop_mainloop();
2202 waitpid(pid
, &status
, 0);