]> git.proxmox.com Git - spiceterm.git/blob - spiceterm.c
start mouse support
[spiceterm.git] / spiceterm.c
1 /*
2
3 Copyright (C) 2013 Proxmox Server Solutions GmbH
4
5 Copyright: spiceterm is under GNU GPL, the GNU General Public License.
6
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.
10
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.
15
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
19 02111-1307, USA.
20
21 Author: Dietmar Maurer <dietmar@proxmox.com>
22
23 Note: most of the code here is copied from vncterm (which is
24 also written by me).
25
26 */
27
28 #define _GNU_SOURCE
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <arpa/inet.h>
36 #include <netdb.h>
37 #include <pty.h> /* for openpty and forkpty */
38 #include <string.h>
39 #include <errno.h>
40 #include <sys/ioctl.h>
41 #include <sys/wait.h>
42 #include <signal.h>
43 #include <locale.h>
44
45 #include "spiceterm.h"
46
47 #include <glib.h>
48 #include <spice.h>
49 #include <spice/enums.h>
50 #include <spice/macros.h>
51 #include <spice/qxl_dev.h>
52
53 #include <gdk/gdkkeysyms.h>
54
55 #include "event_loop.h"
56 #include "translations.h"
57
58 static int debug = 0;
59
60 #define DPRINTF(x, format, ...) { \
61 if (x <= debug) { \
62 printf("%s: " format "\n" , __FUNCTION__, ## __VA_ARGS__); \
63 } \
64 }
65
66 #define TERM "xterm"
67
68 #define TERMIDCODE "[?1;2c" // vt100 ID
69
70 #define CHECK_ARGC(argc,argv,i) if (i >= argc-1) { \
71 fprintf(stderr, "ERROR: not enough arguments for: %s\n", argv[i]); \
72 print_usage(NULL); \
73 exit(1); \
74 }
75
76 /* these colours are from linux kernel drivers/char/vt.c */
77
78 unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
79 8,12,10,14, 9,13,11,15 };
80
81
82 static void
83 print_usage(const char *msg)
84 {
85 if (msg) { fprintf(stderr, "ERROR: %s\n", msg); }
86 fprintf(stderr, "USAGE: spiceterm [spiceopts] [-c command [args]]\n");
87 }
88
89 /* Convert UCS2 to UTF8 sequence, trailing zero */
90 /*
91 static int
92 ucs2_to_utf8 (gunichar2 c, char *out)
93 {
94 if (c < 0x80) {
95 out[0] = c; // 0*******
96 out[1] = 0;
97 return 1;
98 } else if (c < 0x800) {
99 out[0] = 0xc0 | (c >> 6); // 110***** 10******
100 out[1] = 0x80 | (c & 0x3f);
101 out[2] = 0;
102 return 2;
103 } else {
104 out[0] = 0xe0 | (c >> 12); // 1110**** 10****** 10******
105 out[1] = 0x80 | ((c >> 6) & 0x3f);
106 out[2] = 0x80 | (c & 0x3f);
107 out[3] = 0;
108 return 3;
109 }
110
111 return 0;
112 }
113 */
114
115 static void
116 draw_char_at (spiceTerm *vt, int x, int y, gunichar2 ch, TextAttributes attrib)
117 {
118 if (x < 0 || y < 0 || x >= vt->width || y >= vt->height) {
119 return;
120 }
121
122 spice_screen_draw_char(vt->screen, x, y, ch, attrib);
123 }
124
125 static void
126 spiceterm_update_xy (spiceTerm *vt, int x, int y)
127 {
128 if (x < 0 || y < 0 || x >= vt->width || y >= vt->height) { return; }
129
130 int y1 = (vt->y_base + y) % vt->total_height;
131 int y2 = y1 - vt->y_displ;
132 if (y2 < 0) {
133 y2 += vt->total_height;
134 }
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);
138 }
139 }
140
141 static void
142 spiceterm_clear_xy (spiceTerm *vt, int x, int y)
143 {
144 if (x < 0 || y < 0 || x >= vt->width || y >= vt->height) { return; }
145
146 int y1 = (vt->y_base + y) % vt->total_height;
147 int y2 = y1 - vt->y_displ;
148 if (y2 < 0) {
149 y2 += vt->total_height;
150 }
151 if (y2 < vt->height) {
152 TextCell *c = &vt->cells[y1 * vt->width + x];
153 c->ch = ' ';
154 c->attrib = vt->default_attrib;
155 c->attrib.fgcol = vt->cur_attrib.fgcol;
156 c->attrib.bgcol = vt->cur_attrib.bgcol;
157
158 draw_char_at (vt, x, y, c->ch, c->attrib);
159 }
160 }
161
162 static void
163 spiceterm_show_cursor (spiceTerm *vt, int show)
164 {
165 int x = vt->cx;
166 if (x >= vt->width) {
167 x = vt->width - 1;
168 }
169
170 int y1 = (vt->y_base + vt->cy) % vt->total_height;
171 int y = y1 - vt->y_displ;
172 if (y < 0) {
173 y += vt->total_height;
174 }
175
176 if (y < vt->height) {
177
178 TextCell *c = &vt->cells[y1 * vt->width + x];
179
180 if (show) {
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);
184 } else {
185 draw_char_at (vt, x, y, c->ch, c->attrib);
186 }
187 }
188 }
189
190 static void
191 spiceterm_refresh (spiceTerm *vt)
192 {
193 int x, y, y1;
194
195 y1 = vt->y_displ;
196 for(y = 0; y < vt->height; y++) {
197 TextCell *c = vt->cells + y1 * vt->width;
198 for(x = 0; x < vt->width; x++) {
199 draw_char_at (vt, x, y, c->ch, c->attrib);
200 c++;
201 }
202 if (++y1 == vt->total_height)
203 y1 = 0;
204 }
205
206 spiceterm_show_cursor (vt, 1);
207 }
208
209 static void
210 spiceterm_scroll_down (spiceTerm *vt, int top, int bottom, int lines)
211 {
212 if ((top + lines) >= bottom) {
213 lines = bottom - top -1;
214 }
215
216 if (top < 0 || bottom > vt->height || top >= bottom || lines < 1) {
217 return;
218 }
219
220 int i;
221 for(i = bottom - top - lines - 1; i >= 0; i--) {
222 int src = ((vt->y_base + top + i) % vt->total_height)*vt->width;
223 int dst = ((vt->y_base + top + lines + i) % vt->total_height)*vt->width;
224
225 memmove(vt->cells + dst, vt->cells + src, vt->width*sizeof (TextCell));
226 }
227
228 for (i = 0; i < lines; i++) {
229 int j;
230 TextCell *c = vt->cells + ((vt->y_base + top + i) % vt->total_height)*vt->width;
231 for(j = 0; j < vt->width; j++) {
232 c->attrib = vt->default_attrib;
233 c->ch = ' ';
234 c++;
235 }
236 }
237
238 int h = lines * 16;
239 int y0 = top*16;
240 int y1 = y0 + h;
241 int y2 = bottom*16;
242
243 spice_screen_scroll(vt->screen, 0, y1, vt->screen->primary_width, y2, 0, y0);
244 spice_screen_clear(vt->screen, 0, y0, vt->screen->primary_width, y1);
245 }
246
247 static void
248 spiceterm_scroll_up (spiceTerm *vt, int top, int bottom, int lines, int moveattr)
249 {
250 if ((top + lines) >= bottom) {
251 lines = bottom - top - 1;
252 }
253
254 if (top < 0 || bottom > vt->height || top >= bottom || lines < 1) {
255 return;
256 }
257
258
259 int h = lines * 16;
260 int y0 = top*16;
261 int y1 = (top + lines)*16;
262 int y2 = bottom*16;
263
264 spice_screen_scroll(vt->screen, 0, y0, vt->screen->primary_width, y2 -h, 0, y1);
265 spice_screen_clear(vt->screen, 0, y2 -h, vt->screen->primary_width, y2);
266
267 if (!moveattr) {
268 return;
269 }
270
271 // move attributes
272
273 int i;
274 for(i = 0; i < (bottom - top - lines); i++) {
275 int dst = ((vt->y_base + top + i) % vt->total_height)*vt->width;
276 int src = ((vt->y_base + top + lines + i) % vt->total_height)*vt->width;
277
278 memmove(vt->cells + dst, vt->cells + src, vt->width*sizeof (TextCell));
279 }
280
281 for (i = 1; i <= lines; i++) {
282 int j;
283 TextCell *c = vt->cells + ((vt->y_base + bottom - i) % vt->total_height)*vt->width;
284 for(j = 0; j < vt->width; j++) {
285 c->attrib = vt->default_attrib;
286 c->ch = ' ';
287 c++;
288 }
289 }
290 }
291
292 static void
293 spiceterm_virtual_scroll (spiceTerm *vt, int lines)
294 {
295 if (vt->altbuf || lines == 0) return;
296
297 if (lines < 0) {
298 lines = -lines;
299 int i = vt->scroll_height;
300 if (i > vt->total_height - vt->height)
301 i = vt->total_height - vt->height;
302 int y1 = vt->y_base - i;
303 if (y1 < 0)
304 y1 += vt->total_height;
305 for(i = 0; i < lines; i++) {
306 if (vt->y_displ == y1) break;
307 if (--vt->y_displ < 0) {
308 vt->y_displ = vt->total_height - 1;
309 }
310 }
311 } else {
312 int i;
313 for(i = 0; i < lines; i++) {
314 if (vt->y_displ == vt->y_base) break;
315 if (++vt->y_displ == vt->total_height) {
316 vt->y_displ = 0;
317 }
318 }
319 }
320
321 spiceterm_refresh (vt);
322 }
323
324 static void
325 spiceterm_respond_esc (spiceTerm *vt, const char *esc)
326 {
327 int len = strlen (esc);
328 int i;
329
330 if (vt->ibuf_count < (IBUFSIZE - 1 - len)) {
331 vt->ibuf[vt->ibuf_count++] = 27;
332 for (i = 0; i < len; i++) {
333 vt->ibuf[vt->ibuf_count++] = esc[i];
334 }
335 }
336 }
337
338 static void
339 spiceterm_put_lf (spiceTerm *vt)
340 {
341 if (vt->cy + 1 == vt->region_bottom) {
342
343 if (vt->altbuf || vt->region_top != 0 || vt->region_bottom != vt->height) {
344 spiceterm_scroll_up (vt, vt->region_top, vt->region_bottom, 1, 1);
345 return;
346 }
347
348 if (vt->y_displ == vt->y_base) {
349 spiceterm_scroll_up (vt, vt->region_top, vt->region_bottom, 1, 0);
350 }
351
352 if (vt->y_displ == vt->y_base) {
353 if (++vt->y_displ == vt->total_height) {
354 vt->y_displ = 0;
355 }
356 }
357
358 if (++vt->y_base == vt->total_height) {
359 vt->y_base = 0;
360 }
361
362 if (vt->scroll_height < vt->total_height) {
363 vt->scroll_height++;
364 }
365
366 int y1 = (vt->y_base + vt->height - 1) % vt->total_height;
367 TextCell *c = &vt->cells[y1 * vt->width];
368 int x;
369 for (x = 0; x < vt->width; x++) {
370 c->ch = ' ';
371 c->attrib = vt->default_attrib;
372 c++;
373 }
374
375 // fprintf (stderr, "BASE: %d DISPLAY %d\n", vt->y_base, vt->y_displ);
376
377 } else if (vt->cy < vt->height - 1) {
378 vt->cy += 1;
379 }
380 }
381
382 static void
383 spiceterm_csi_m (spiceTerm *vt)
384 {
385 int i;
386
387 for (i = 0; i < vt->esc_count; i++) {
388 switch (vt->esc_buf[i]) {
389 case 0: /* reset all console attributes to default */
390 vt->cur_attrib = vt->default_attrib;
391 break;
392 case 1:
393 vt->cur_attrib.bold = 1;
394 break;
395 case 4:
396 vt->cur_attrib.uline = 1;
397 break;
398 case 5:
399 vt->cur_attrib.blink = 1;
400 break;
401 case 7:
402 vt->cur_attrib.invers = 1;
403 break;
404 case 8:
405 vt->cur_attrib.unvisible = 1;
406 break;
407 case 10:
408 vt->cur_enc = LAT1_MAP;
409 // fixme: dispaly controls = 0 ?
410 // fixme: toggle meta = 0 ?
411 break;
412 case 11:
413 vt->cur_enc = IBMPC_MAP;
414 // fixme: dispaly controls = 1 ?
415 // fixme: toggle meta = 0 ?
416 break;
417 case 12:
418 vt->cur_enc = IBMPC_MAP;
419 // fixme: dispaly controls = 1 ?
420 // fixme: toggle meta = 1 ?
421 break;
422 case 22:
423 vt->cur_attrib.bold = 0;
424 break;
425 case 24:
426 vt->cur_attrib.uline = 0;
427 break;
428 case 25:
429 vt->cur_attrib.blink = 0;
430 break;
431 case 27:
432 vt->cur_attrib.invers = 0;
433 break;
434 case 28:
435 vt->cur_attrib.unvisible = 0;
436 break;
437 case 30:
438 case 31:
439 case 32:
440 case 33:
441 case 34:
442 case 35:
443 case 36:
444 case 37:
445 /* set foreground color */
446 vt->cur_attrib.fgcol = color_table [vt->esc_buf[i] - 30];
447 break;
448 case 38:
449 /* reset color to default, enable underline */
450 vt->cur_attrib.fgcol = vt->default_attrib.fgcol;
451 vt->cur_attrib.uline = 1;
452 break;
453 case 39:
454 /* reset color to default, disable underline */
455 vt->cur_attrib.fgcol = vt->default_attrib.fgcol;
456 vt->cur_attrib.uline = 0;
457 break;
458 case 40:
459 case 41:
460 case 42:
461 case 43:
462 case 44:
463 case 45:
464 case 46:
465 case 47:
466 /* set background color */
467 vt->cur_attrib.bgcol = color_table [vt->esc_buf[i] - 40];
468 break;
469 case 49:
470 /* reset background color */
471 vt->cur_attrib.bgcol = vt->default_attrib.bgcol;
472 break;
473 default:
474 fprintf(stderr, "unhandled ESC[%d m code\n",vt->esc_buf[i]);
475 //fixme: implement
476 }
477 }
478 }
479
480 static void
481 spiceterm_save_cursor (spiceTerm *vt)
482 {
483 vt->cx_saved = vt->cx;
484 vt->cy_saved = vt->cy;
485 vt->cur_attrib_saved = vt->cur_attrib;
486 vt->charset_saved = vt->charset;
487 vt->g0enc_saved = vt->g0enc;
488 vt->g1enc_saved = vt->g1enc;
489 vt->cur_enc_saved = vt->cur_enc;
490 }
491
492 static void
493 spiceterm_restore_cursor (spiceTerm *vt)
494 {
495 vt->cx = vt->cx_saved;
496 vt->cy = vt->cy_saved;
497 vt->cur_attrib = vt->cur_attrib_saved;
498 vt->charset = vt->charset_saved;
499 vt->g0enc = vt->g0enc_saved;
500 vt->g1enc = vt->g1enc_saved;
501 vt->cur_enc = vt->cur_enc_saved;
502 }
503
504 static void
505 spiceterm_set_alternate_buffer (spiceTerm *vt, int on_off)
506 {
507 int x, y;
508
509 vt->y_displ = vt->y_base;
510
511 if (on_off) {
512
513 if (vt->altbuf) return;
514
515 vt->altbuf = 1;
516
517 /* alternate buffer & cursor */
518
519 spiceterm_save_cursor (vt);
520 /* save screen to altcels */
521 for (y = 0; y < vt->height; y++) {
522 int y1 = (vt->y_base + y) % vt->total_height;
523 for (x = 0; x < vt->width; x++) {
524 vt->altcells[y*vt->width + x] = vt->cells[y1*vt->width + x];
525 }
526 }
527
528 /* clear screen */
529 for (y = 0; y <= vt->height; y++) {
530 for (x = 0; x < vt->width; x++) {
531 spiceterm_clear_xy (vt, x, y);
532 }
533 }
534
535 } else {
536
537 if (vt->altbuf == 0) return;
538
539 vt->altbuf = 0;
540
541 /* restore saved data */
542 for (y = 0; y < vt->height; y++) {
543 int y1 = (vt->y_base + y) % vt->total_height;
544 for (x = 0; x < vt->width; x++) {
545 vt->cells[y1*vt->width + x] = vt->altcells[y*vt->width + x];
546 }
547 }
548
549 spiceterm_restore_cursor (vt);
550 }
551
552 spiceterm_refresh (vt);
553 }
554
555 static void
556 spiceterm_set_mode (spiceTerm *vt, int on_off)
557 {
558 int i;
559
560 for (i = 0; i <= vt->esc_count; i++) {
561 if (vt->esc_ques) { /* DEC private modes set/reset */
562 switch(vt->esc_buf[i]) {
563 case 10: /* X11 mouse reporting on/off */
564 case 1000:
565 vt->report_mouse = on_off;
566 break;
567 case 1049: /* start/end special app mode (smcup/rmcup) */
568 spiceterm_set_alternate_buffer (vt, on_off);
569 break;
570 case 25: /* Cursor on/off */
571 case 9: /* X10 mouse reporting on/off */
572 case 6: /* Origin relative/absolute */
573 case 1: /* Cursor keys in appl mode*/
574 case 5: /* Inverted screen on/off */
575 case 7: /* Autowrap on/off */
576 case 8: /* Autorepeat on/off */
577 break;
578 }
579 } else { /* ANSI modes set/reset */
580 /* fixme: implement me */
581 }
582 }
583 }
584
585 static void
586 spiceterm_gotoxy (spiceTerm *vt, int x, int y)
587 {
588 /* verify all boundaries */
589
590 if (x < 0) {
591 x = 0;
592 }
593
594 if (x >= vt->width) {
595 x = vt->width - 1;
596 }
597
598 vt->cx = x;
599
600 if (y < 0) {
601 y = 0;
602 }
603
604 if (y >= vt->height) {
605 y = vt->height - 1;
606 }
607
608 vt->cy = y;
609 }
610
611 static void
612 debug_print_escape_buffer(spiceTerm *vt, const char *func, const char *prefix,
613 const char *qes, gunichar2 ch)
614 {
615 if (debug >=1 ) {
616 if (vt->esc_count == 0) {
617 printf("%s:%s ESC[%s%c\n", func, prefix, qes, ch);
618 } else if (vt->esc_count == 1) {
619 printf("%s:%s ESC[%s%d%c\n", func, prefix, qes, vt->esc_buf[0], ch);
620 } else {
621 int i;
622 printf("%s:%s ESC[%s%d", func, prefix, qes, vt->esc_buf[0]);
623 for (i = 1; i < vt->esc_count; i++) {
624 printf(";%d", vt->esc_buf[i]);
625 }
626 printf("%c\n", ch);
627 }
628 }
629 }
630
631 enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey,
632 EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
633 ESpalette, ESidquery, ESosc1, ESosc2};
634
635 static void
636 spiceterm_putchar (spiceTerm *vt, gunichar2 ch)
637 {
638 int x, y, i, c;
639
640 if (debug && !vt->tty_state) {
641 DPRINTF(1, "CHAR:%2d: %4x '%c' (cur_enc %d) %d %d",
642 vt->tty_state, ch, ch, vt->cur_enc, vt->cx, vt->cy);
643 }
644
645 switch(vt->tty_state) {
646 case ESesc:
647 vt->tty_state = ESnormal;
648 switch (ch) {
649 case '[':
650 vt->tty_state = ESsquare;
651 break;
652 case ']':
653 vt->tty_state = ESnonstd;
654 break;
655 case '%':
656 vt->tty_state = ESpercent;
657 break;
658 case '7':
659 spiceterm_save_cursor (vt);
660 break;
661 case '8':
662 spiceterm_restore_cursor (vt);
663 break;
664 case '(':
665 vt->tty_state = ESsetG0; // SET G0
666 break;
667 case ')':
668 vt->tty_state = ESsetG1; // SET G1
669 break;
670 case 'M':
671 /* cursor up (ri) */
672 if (vt->cy == vt->region_top)
673 spiceterm_scroll_down (vt, vt->region_top, vt->region_bottom, 1);
674 else if (vt->cy > 0) {
675 vt->cy--;
676 }
677 break;
678 case '>':
679 /* numeric keypad - ignored */
680 break;
681 case '=':
682 /* appl. keypad - ignored */
683 break;
684 default:
685 DPRINTF(1, "got unhandled ESC%c %d", ch, ch);
686 break;
687 }
688 break;
689 case ESnonstd: /* Operating System Controls */
690 vt->tty_state = ESnormal;
691
692 switch (ch) {
693 case 'P': /* palette escape sequence */
694 for(i = 0; i < MAX_ESC_PARAMS; i++) {
695 vt->esc_buf[i] = 0;
696 }
697
698 vt->esc_count = 0;
699 vt->tty_state = ESpalette;
700 break;
701 case 'R': /* reset palette */
702 // fixme: reset_palette(vc);
703 break;
704 case '0':
705 case '1':
706 case '2':
707 case '4':
708 vt->osc_cmd = ch;
709 vt->osc_textbuf[0] = 0;
710 vt->tty_state = ESosc1;
711 break;
712 default:
713 DPRINTF(1, "got unhandled OSC %c", ch);
714 vt->tty_state = ESnormal;
715 break;
716 }
717 break;
718 case ESosc1:
719 vt->tty_state = ESnormal;
720 if (ch == ';') {
721 vt->tty_state = ESosc2;
722 } else {
723 DPRINTF(1, "got illegal OSC sequence");
724 }
725 break;
726 case ESosc2:
727 if (ch != 0x9c && ch != 7) {
728 int i = 0;
729 while (vt->osc_textbuf[i]) i++;
730 vt->osc_textbuf[i++] = ch;
731 vt->osc_textbuf[i] = 0;
732 } else {
733 DPRINTF(1, "OSC:%c:%s", vt->osc_cmd, vt->osc_textbuf);
734 vt->tty_state = ESnormal;
735 }
736 break;
737 case ESpalette:
738 if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F')
739 || (ch >= 'a' && ch <= 'f')) {
740 vt->esc_buf[vt->esc_count++] = (ch > '9' ? (ch & 0xDF) - 'A' + 10 : ch - '0');
741 if (vt->esc_count == 7) {
742 // fixme: this does not work - please test
743 /*
744 rfbColourMap *cmap =&vt->screen->colourMap;
745
746 int i = color_table[vt->esc_buf[0]] * 3, j = 1;
747 cmap->data.bytes[i] = 16 * vt->esc_buf[j++];
748 cmap->data.bytes[i++] += vt->esc_buf[j++];
749 cmap->data.bytes[i] = 16 * vt->esc_buf[j++];
750 cmap->data.bytes[i++] += vt->esc_buf[j++];
751 cmap->data.bytes[i] = 16 * vt->esc_buf[j++];
752 cmap->data.bytes[i] += vt->esc_buf[j];
753 */
754 //set_palette(vc); ?
755
756 vt->tty_state = ESnormal;
757 }
758 } else
759 vt->tty_state = ESnormal;
760 break;
761 case ESsquare:
762 for(i = 0; i < MAX_ESC_PARAMS; i++) {
763 vt->esc_buf[i] = 0;
764 }
765
766 vt->esc_count = 0;
767 vt->esc_has_par = 0;
768 vt->tty_state = ESgetpars;
769
770 if (ch == '>') {
771 vt->tty_state = ESidquery;
772 break;
773 }
774
775 if ((vt->esc_ques = (ch == '?'))) {
776 break;
777 }
778 case ESgetpars:
779 if (ch >= '0' && ch <= '9') {
780 vt->esc_has_par = 1;
781 if (vt->esc_count < MAX_ESC_PARAMS) {
782 vt->esc_buf[vt->esc_count] = vt->esc_buf[vt->esc_count] * 10 + ch - '0';
783 }
784 break;
785 } else if (ch == ';') {
786 vt->esc_count++;
787 break;
788 } else {
789 if (vt->esc_has_par) {
790 vt->esc_count++;
791 }
792 vt->tty_state = ESgotpars;
793 }
794 case ESgotpars:
795
796 vt->tty_state = ESnormal;
797
798 char *qes = vt->esc_ques ? "?" : "";
799
800 if (debug) {
801 debug_print_escape_buffer(vt, __func__, "", qes, ch);
802 }
803
804 switch (ch) {
805 case 'h':
806 spiceterm_set_mode (vt, 1);
807 break;
808 case 'l':
809 spiceterm_set_mode (vt, 0);
810 break;
811 case 'm':
812 if (!vt->esc_count) {
813 vt->esc_count++; // default parameter 0
814 }
815 spiceterm_csi_m (vt);
816 break;
817 case 'n':
818 /* report cursor position */
819 /* TODO: send ESC[row;colR */
820 break;
821 case 'A':
822 /* move cursor up */
823 if (vt->esc_buf[0] == 0) {
824 vt->esc_buf[0] = 1;
825 }
826 vt->cy -= vt->esc_buf[0];
827 if (vt->cy < 0) {
828 vt->cy = 0;
829 }
830 break;
831 case 'B':
832 case 'e':
833 /* move cursor down */
834 if (vt->esc_buf[0] == 0) {
835 vt->esc_buf[0] = 1;
836 }
837 vt->cy += vt->esc_buf[0];
838 if (vt->cy >= vt->height) {
839 vt->cy = vt->height - 1;
840 }
841 break;
842 case 'C':
843 case 'a':
844 /* move cursor right */
845 if (vt->esc_buf[0] == 0) {
846 vt->esc_buf[0] = 1;
847 }
848 vt->cx += vt->esc_buf[0];
849 if (vt->cx >= vt->width) {
850 vt->cx = vt->width - 1;
851 }
852 break;
853 case 'D':
854 /* move cursor left */
855 if (vt->esc_buf[0] == 0) {
856 vt->esc_buf[0] = 1;
857 }
858 vt->cx -= vt->esc_buf[0];
859 if (vt->cx < 0) {
860 vt->cx = 0;
861 }
862 break;
863 case 'G':
864 case '`':
865 /* move cursor to column */
866 spiceterm_gotoxy (vt, vt->esc_buf[0] - 1, vt->cy);
867 break;
868 case 'd':
869 /* move cursor to row */
870 spiceterm_gotoxy (vt, vt->cx , vt->esc_buf[0] - 1);
871 break;
872 case 'f':
873 case 'H':
874 /* move cursor to row, column */
875 spiceterm_gotoxy (vt, vt->esc_buf[1] - 1, vt->esc_buf[0] - 1);
876 break;
877 case 'J':
878 switch (vt->esc_buf[0]) {
879 case 0:
880 /* clear to end of screen */
881 for (y = vt->cy; y < vt->height; y++) {
882 for (x = 0; x < vt->width; x++) {
883 if (y == vt->cy && x < vt->cx) {
884 continue;
885 }
886 spiceterm_clear_xy (vt, x, y);
887 }
888 }
889 break;
890 case 1:
891 /* clear from beginning of screen */
892 for (y = 0; y <= vt->cy; y++) {
893 for (x = 0; x < vt->width; x++) {
894 if (y == vt->cy && x > vt->cx) {
895 break;
896 }
897 spiceterm_clear_xy (vt, x, y);
898 }
899 }
900 break;
901 case 2:
902 /* clear entire screen */
903 for (y = 0; y <= vt->height; y++) {
904 for (x = 0; x < vt->width; x++) {
905 spiceterm_clear_xy (vt, x, y);
906 }
907 }
908 break;
909 }
910 break;
911 case 'K':
912 switch (vt->esc_buf[0]) {
913 case 0:
914 /* clear to eol */
915 for(x = vt->cx; x < vt->width; x++) {
916 spiceterm_clear_xy (vt, x, vt->cy);
917 }
918 break;
919 case 1:
920 /* clear from beginning of line */
921 for (x = 0; x <= vt->cx; x++) {
922 spiceterm_clear_xy (vt, x, vt->cy);
923 }
924 break;
925 case 2:
926 /* clear entire line */
927 for(x = 0; x < vt->width; x++) {
928 spiceterm_clear_xy (vt, x, vt->cy);
929 }
930 break;
931 }
932 break;
933 case 'L':
934 /* insert line */
935 c = vt->esc_buf[0];
936
937 if (c > vt->height - vt->cy)
938 c = vt->height - vt->cy;
939 else if (!c)
940 c = 1;
941
942 spiceterm_scroll_down (vt, vt->cy, vt->region_bottom, c);
943 break;
944 case 'M':
945 /* delete line */
946 c = vt->esc_buf[0];
947
948 if (c > vt->height - vt->cy)
949 c = vt->height - vt->cy;
950 else if (!c)
951 c = 1;
952
953 spiceterm_scroll_up (vt, vt->cy, vt->region_bottom, c, 1);
954 break;
955 case 'T':
956 /* scroll down */
957 c = vt->esc_buf[0];
958 if (!c) c = 1;
959 spiceterm_scroll_down (vt, vt->region_top, vt->region_bottom, c);
960 break;
961 case 'S':
962 /* scroll up */
963 c = vt->esc_buf[0];
964 if (!c) c = 1;
965 spiceterm_scroll_up (vt, vt->region_top, vt->region_bottom, c, 1);
966 break;
967 case 'P':
968 /* delete c character */
969 c = vt->esc_buf[0];
970
971 if (c > vt->width - vt->cx)
972 c = vt->width - vt->cx;
973 else if (!c)
974 c = 1;
975
976 for (x = vt->cx; x < vt->width - c; x++) {
977 int y1 = (vt->y_base + vt->cy) % vt->total_height;
978 TextCell *dst = &vt->cells[y1 * vt->width + x];
979 TextCell *src = dst + c;
980 *dst = *src;
981 spiceterm_update_xy (vt, x + c, vt->cy);
982 src->ch = ' ';
983 src->attrib = vt->default_attrib;
984 spiceterm_update_xy (vt, x, vt->cy);
985 }
986 break;
987 case 's':
988 /* save cursor position */
989 spiceterm_save_cursor (vt);
990 break;
991 case 'u':
992 /* restore cursor position */
993 spiceterm_restore_cursor (vt);
994 break;
995 case 'X':
996 /* erase c characters */
997 c = vt->esc_buf[0];
998 if (!c) c = 1;
999
1000 if (c > (vt->width - vt->cx)) c = vt->width - vt->cx;
1001
1002 for(i = 0; i < c; i++) {
1003 spiceterm_clear_xy (vt, vt->cx + i, vt->cy);
1004 }
1005 break;
1006 case '@':
1007 /* insert c character */
1008 c = vt->esc_buf[0];
1009 if (c > (vt->width - vt->cx)) {
1010 c = vt->width - vt->cx;
1011 }
1012 if (!c) c = 1;
1013
1014 for (x = vt->width - c; x >= vt->cx; x--) {
1015 int y1 = (vt->y_base + vt->cy) % vt->total_height;
1016 TextCell *src = &vt->cells[y1 * vt->width + x];
1017 TextCell *dst = src + c;
1018 *dst = *src;
1019 spiceterm_update_xy (vt, x + c, vt->cy);
1020 src->ch = ' ';
1021 src->attrib = vt->cur_attrib;
1022 spiceterm_update_xy (vt, x, vt->cy);
1023 }
1024
1025 break;
1026 case 'r':
1027 /* set region */
1028 if (!vt->esc_buf[0])
1029 vt->esc_buf[0]++;
1030 if (!vt->esc_buf[1])
1031 vt->esc_buf[1] = vt->height;
1032 /* Minimum allowed region is 2 lines */
1033 if (vt->esc_buf[0] < vt->esc_buf[1] &&
1034 vt->esc_buf[1] <= vt->height) {
1035 vt->region_top = vt->esc_buf[0] - 1;
1036 vt->region_bottom = vt->esc_buf[1];
1037 vt->cx = 0;
1038 vt->cy = vt->region_top;
1039 DPRINTF(1, "set region %d %d", vt->region_top, vt->region_bottom);
1040 }
1041
1042 break;
1043 default:
1044 if (debug) {
1045 debug_print_escape_buffer(vt, __func__, " unhandled escape", qes, ch);
1046 }
1047 break;
1048 }
1049 vt->esc_ques = 0;
1050 break;
1051 case ESsetG0: // Set G0
1052 vt->tty_state = ESnormal;
1053
1054 if (ch == '0')
1055 vt->g0enc = GRAF_MAP;
1056 else if (ch == 'B')
1057 vt->g0enc = LAT1_MAP;
1058 else if (ch == 'U')
1059 vt->g0enc = IBMPC_MAP;
1060 else if (ch == 'K')
1061 vt->g0enc = USER_MAP;
1062
1063 if (vt->charset == 0)
1064 vt->cur_enc = vt->g0enc;
1065
1066 break;
1067 case ESsetG1: // Set G1
1068 vt->tty_state = ESnormal;
1069
1070 if (ch == '0')
1071 vt->g1enc = GRAF_MAP;
1072 else if (ch == 'B')
1073 vt->g1enc = LAT1_MAP;
1074 else if (ch == 'U')
1075 vt->g1enc = IBMPC_MAP;
1076 else if (ch == 'K')
1077 vt->g1enc = USER_MAP;
1078
1079 if (vt->charset == 1)
1080 vt->cur_enc = vt->g1enc;
1081
1082 break;
1083 case ESidquery: // vt100 query id
1084 vt->tty_state = ESnormal;
1085
1086 if (ch == 'c') {
1087 DPRINTF(1, "ESC[>c Query term ID");
1088 spiceterm_respond_esc (vt, TERMIDCODE);
1089 }
1090 break;
1091 case ESpercent:
1092 vt->tty_state = ESnormal;
1093 switch (ch) {
1094 case '@': /* defined in ISO 2022 */
1095 vt->utf8 = 0;
1096 break;
1097 case 'G': /* prelim official escape code */
1098 case '8': /* retained for compatibility */
1099 vt->utf8 = 1;
1100 break;
1101 }
1102 break;
1103 default: // ESnormal
1104 vt->tty_state = ESnormal;
1105
1106 switch(ch) {
1107 case 0:
1108 break;
1109 case 7: /* alert aka. bell */
1110 // fixme:
1111 //rfbSendBell(vt->screen);
1112 break;
1113 case 8: /* backspace */
1114 if (vt->cx > 0)
1115 vt->cx--;
1116 break;
1117 case 9: /* tabspace */
1118 if (vt->cx + (8 - (vt->cx % 8)) > vt->width) {
1119 vt->cx = 0;
1120 spiceterm_put_lf (vt);
1121 } else {
1122 vt->cx = vt->cx + (8 - (vt->cx % 8));
1123 }
1124 break;
1125 case 10: /* LF,*/
1126 case 11: /* VT */
1127 case 12: /* FF */
1128 spiceterm_put_lf (vt);
1129 break;
1130 case 13: /* carriage return */
1131 vt->cx = 0;
1132 break;
1133 case 14:
1134 /* SI (shift in), select character set 1 */
1135 vt->charset = 1;
1136 vt->cur_enc = vt->g1enc;
1137 /* fixme: display controls = 1 */
1138 break;
1139 case 15:
1140 /* SO (shift out), select character set 0 */
1141 vt->charset = 0;
1142 vt->cur_enc = vt->g0enc;
1143 /* fixme: display controls = 0 */
1144 break;
1145 case 27: /* esc */
1146 vt->tty_state = ESesc;
1147 break;
1148 case 127: /* delete */
1149 /* ignore */
1150 break;
1151 case 128+27: /* csi */
1152 vt->tty_state = ESsquare;
1153 break;
1154 default:
1155 if (vt->cx >= vt->width) {
1156 /* line wrap */
1157 vt->cx = 0;
1158 spiceterm_put_lf (vt);
1159 }
1160
1161 int y1 = (vt->y_base + vt->cy) % vt->total_height;
1162 TextCell *c = &vt->cells[y1*vt->width + vt->cx];
1163 c->attrib = vt->cur_attrib;
1164 c->ch = ch;
1165 spiceterm_update_xy (vt, vt->cx, vt->cy);
1166 vt->cx++;
1167 break;
1168 }
1169 break;
1170 }
1171 }
1172
1173 static int
1174 spiceterm_puts (spiceTerm *vt, const char *buf, int len)
1175 {
1176 gunichar2 tc;
1177
1178 spiceterm_show_cursor (vt, 0);
1179
1180 while (len) {
1181 unsigned char c = *buf;
1182 len--;
1183 buf++;
1184
1185 if (vt->tty_state != ESnormal) {
1186 // never translate escape sequence
1187 tc = c;
1188 } else if (vt->utf8 && !vt->cur_enc) {
1189
1190 if(c & 0x80) { // utf8 multi-byte sequence
1191
1192 if (vt->utf_count > 0 && (c & 0xc0) == 0x80) {
1193 // inside UTF8 sequence
1194 vt->utf_char = (vt->utf_char << 6) | (c & 0x3f);
1195 vt->utf_count--;
1196 if (vt->utf_count == 0) {
1197 tc = vt->utf_char;
1198 } else {
1199 continue;
1200 }
1201 } else {
1202 // first char of a UTF8 sequence
1203 if ((c & 0xe0) == 0xc0) {
1204 vt->utf_count = 1;
1205 vt->utf_char = (c & 0x1f);
1206 } else if ((c & 0xf0) == 0xe0) {
1207 vt->utf_count = 2;
1208 vt->utf_char = (c & 0x0f);
1209 } else if ((c & 0xf8) == 0xf0) {
1210 vt->utf_count = 3;
1211 vt->utf_char = (c & 0x07);
1212 } else if ((c & 0xfc) == 0xf8) {
1213 vt->utf_count = 4;
1214 vt->utf_char = (c & 0x03);
1215 } else if ((c & 0xfe) == 0xfc) {
1216 vt->utf_count = 5;
1217 vt->utf_char = (c & 0x01);
1218 } else
1219 vt->utf_count = 0;
1220
1221 continue;
1222 }
1223 } else {
1224 // utf8 single byte
1225 tc = c;
1226 vt->utf_count = 0;
1227 }
1228
1229 } else {
1230 // never translate controls
1231 if (c >= 32 && c != 127 && c != (128+27)) {
1232 tc = translations[vt->cur_enc][c & 0x0ff];
1233 } else {
1234 tc = c;
1235 }
1236 }
1237
1238 spiceterm_putchar (vt, tc);
1239 }
1240
1241 spiceterm_show_cursor (vt, 1);
1242
1243 return len;
1244 }
1245
1246 /* fixme:
1247 void
1248 spiceterm_set_xcut_text (char* str, int len, struct _rfbClientRec* cl)
1249 {
1250 spiceTerm *vt =(spiceTerm *)cl->screen->screenData;
1251
1252 // seems str is Latin-1 encoded
1253 if (vt->selection) free (vt->selection);
1254 vt->selection = (gunichar2 *)malloc (len*sizeof (gunichar2));
1255 int i;
1256 for (i = 0; i < len; i++) {
1257 vt->selection[i] = str[i] & 0xff;
1258 }
1259 vt->selection_len = len;
1260 }
1261 */
1262 /*
1263 static void
1264 mouse_report (spiceTerm *vt, int butt, int mrx, int mry)
1265 {
1266 char buf[8];
1267
1268 sprintf (buf, "[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1269 (char)('!' + mry));
1270
1271 spiceterm_respond_esc (vt, buf);
1272 }
1273 */
1274
1275 void
1276 spiceterm_toggle_marked_cell (spiceTerm *vt, int pos)
1277 {
1278
1279 /* fixme:
1280 int x= (pos%vt->width)*8;
1281 int y= (pos/vt->width)*16;
1282
1283 int i,j;
1284 rfbScreenInfoPtr s=vt->screen;
1285
1286 char *b = s->frameBuffer+y*s->width+x;
1287
1288 for (j=0; j < 16; j++) {
1289 for(i=0; i < 8; i++) {
1290 b[j*s->width+i] ^= 0x0f;
1291 rfbMarkRectAsModified (s, x, y, x+8, y+16);
1292 }
1293 }
1294 */
1295 }
1296
1297 /* fixme:
1298
1299 void
1300 spiceterm_pointer_event (int buttonMask, int x, int y, rfbClientPtr cl)
1301 {
1302
1303 spiceTerm *vt =(spiceTerm *)cl->screen->screenData;
1304 static int button2_released = 1;
1305 static int last_mask = 0;
1306 static int sel_start_pos = 0;
1307 static int sel_end_pos = 0;
1308 int i;
1309
1310 int cx = x/8;
1311 int cy = y/16;
1312
1313 if (cx < 0) cx = 0;
1314 if (cx >= vt->width) cx = vt->width - 1;
1315 if (cy < 0) cy = 0;
1316 if (cy >= vt->height) cy = vt->height - 1;
1317
1318 if (vt->report_mouse && buttonMask != last_mask) {
1319 last_mask = buttonMask;
1320 if (buttonMask & 1) {
1321 mouse_report (vt, 0, cx, cy);
1322 }
1323 if (buttonMask & 2) {
1324 mouse_report (vt, 1, cx, cy);
1325 }
1326 if (buttonMask & 4) {
1327 mouse_report (vt, 2, cx, cy);
1328 }
1329 if (!buttonMask) {
1330 mouse_report (vt, 3, cx, cy);
1331 }
1332 }
1333
1334 if (buttonMask & 2) {
1335 if(button2_released && vt->selection) {
1336 int i;
1337 for(i = 0; i < vt->selection_len; i++) {
1338 if (vt->ibuf_count < IBUFSIZE - 6) { // uft8 is max 6 characters wide
1339 if (vt->utf8) {
1340 vt->ibuf_count += ucs2_to_utf8 (vt->selection[i], &vt->ibuf[vt->ibuf_count]);
1341 } else {
1342 vt->ibuf[vt->ibuf_count++] = vt->selection[i];
1343 }
1344 }
1345 }
1346 if (vt->y_displ != vt->y_base) {
1347 vt->y_displ = vt->y_base;
1348 spiceterm_refresh (vt);
1349 }
1350 }
1351 button2_released = 0;
1352 } else {
1353 button2_released = 1;
1354 }
1355
1356 if (buttonMask & 1) {
1357 int pos = cy*vt->width + cx;
1358
1359 // code borrowed from libvncserver (VNCconsole.c)
1360
1361 if (!vt->mark_active) {
1362
1363 vt->mark_active = 1;
1364 sel_start_pos = sel_end_pos = pos;
1365 spiceterm_toggle_marked_cell (vt, pos);
1366
1367 } else {
1368
1369 if (pos != sel_end_pos) {
1370
1371 if (pos > sel_end_pos) {
1372 cx = sel_end_pos; cy=pos;
1373 } else {
1374 cx=pos; cy=sel_end_pos;
1375 }
1376
1377 if (cx < sel_start_pos) {
1378 if (cy < sel_start_pos) cy--;
1379 } else {
1380 cx++;
1381 }
1382
1383 while (cx <= cy) {
1384 spiceterm_toggle_marked_cell (vt, cx);
1385 cx++;
1386 }
1387
1388 sel_end_pos = pos;
1389 }
1390 }
1391
1392 } else if (vt->mark_active) {
1393 vt->mark_active = 0;
1394
1395 if (sel_start_pos > sel_end_pos) {
1396 int tmp = sel_start_pos - 1;
1397 sel_start_pos = sel_end_pos;
1398 sel_end_pos = tmp;
1399 }
1400
1401 int len = sel_end_pos - sel_start_pos + 1;
1402
1403 if (vt->selection) free (vt->selection);
1404 vt->selection = (gunichar2 *)malloc (len*sizeof (gunichar2));
1405 vt->selection_len = len;
1406 char *sel_latin1 = (char *)malloc (len + 1);
1407
1408 for (i = 0; i < len; i++) {
1409 int pos = sel_start_pos + i;
1410 int x = pos % vt->width;
1411 int y1 = ((pos / vt->width) + vt->y_displ) % vt->total_height;
1412 TextCell *c = &vt->cells[y1*vt->width + x];
1413 vt->selection[i] = c->ch;
1414 sel_latin1[i] = (char)c->ch;
1415 c++;
1416 }
1417 sel_latin1[len] = 0;
1418 rfbGotXCutText (vt->screen, sel_latin1, len);
1419 free (sel_latin1);
1420
1421 while (sel_start_pos <= sel_end_pos) {
1422 spiceterm_toggle_marked_cell (vt, sel_start_pos++);
1423 }
1424
1425 }
1426
1427 rfbDefaultPtrAddEvent (buttonMask, x, y, cl);
1428
1429 }
1430 */
1431
1432 static void
1433 spiceterm_motion_event(spiceTerm *vt, uint32_t x, uint32_t y, uint32_t buttons)
1434 {
1435 DPRINTF(0, "mask=%08x x=%d y=%d", buttons, x ,y);
1436 }
1437
1438 static void
1439 my_kbd_push_key(SpiceKbdInstance *sin, uint8_t frag)
1440 {
1441 // spiceTerm *vt = SPICE_CONTAINEROF(sin, spiceTerm, keyboard_sin);
1442
1443 /* we no not need this */
1444
1445 return;
1446 }
1447
1448 static void
1449 my_kbd_push_keyval(SpiceKbdInstance *sin, uint32_t keySym, int flags)
1450 {
1451 spiceTerm *vt = SPICE_CONTAINEROF(sin, spiceTerm, keyboard_sin);
1452 static int control = 0;
1453 static int shift = 0;
1454 char *esc = NULL;
1455
1456 guint uc = 0;
1457
1458 DPRINTF(1, "flags=%d keySym=%08x", flags, keySym);
1459
1460 if (flags & 1) {
1461 if (keySym == GDK_KEY_Shift_L || keySym == GDK_KEY_Shift_R) {
1462 shift = 1;
1463 } if (keySym == GDK_KEY_Control_L || keySym == GDK_KEY_Control_R) {
1464 control = 1;
1465 } else if (vt->ibuf_count < (IBUFSIZE - 32)) {
1466
1467 if (control) {
1468 if(keySym >= 'a' && keySym <= 'z')
1469 uc = keySym - 'a' + 1;
1470 else if (keySym >= 'A' && keySym <= 'Z')
1471 uc = keySym - 'A' + 1;
1472 else
1473 uc = 0;
1474
1475 } else {
1476 switch (keySym) {
1477 case GDK_KEY_Escape:
1478 uc = 27; break;
1479 case GDK_KEY_Return:
1480 uc = '\r'; break;
1481 case GDK_KEY_BackSpace:
1482 uc = 8; break;
1483 case GDK_KEY_Tab:
1484 uc = '\t'; break;
1485 case GDK_KEY_Delete: /* kdch1 */
1486 case GDK_KEY_KP_Delete:
1487 esc = "[3~";break;
1488 case GDK_KEY_Home: /* khome */
1489 case GDK_KEY_KP_Home:
1490 esc = "OH";break;
1491 case GDK_KEY_End:
1492 case GDK_KEY_KP_End: /* kend */
1493 esc = "OF";break;
1494 case GDK_KEY_Insert: /* kich1 */
1495 case GDK_KEY_KP_Insert:
1496 esc = "[2~";break;
1497 case GDK_KEY_Up:
1498 case GDK_KEY_KP_Up: /* kcuu1 */
1499 esc = "OA";break;
1500 case GDK_KEY_Down: /* kcud1 */
1501 case GDK_KEY_KP_Down:
1502 esc = "OB";break;
1503 case GDK_KEY_Right:
1504 case GDK_KEY_KP_Right: /* kcuf1 */
1505 esc = "OC";break;
1506 case GDK_KEY_Left:
1507 case GDK_KEY_KP_Left: /* kcub1 */
1508 esc = "OD";break;
1509 case GDK_KEY_Page_Up:
1510 if (shift) {
1511 spiceterm_virtual_scroll (vt, -vt->height/2);
1512 goto ret;
1513 }
1514 esc = "[5~";break;
1515 case GDK_KEY_Page_Down:
1516 if (shift) {
1517 spiceterm_virtual_scroll (vt, vt->height/2);
1518 goto ret;
1519 }
1520 esc = "[6~";break;
1521 case GDK_KEY_F1:
1522 esc = "OP";break;
1523 case GDK_KEY_F2:
1524 esc = "OQ";break;
1525 case GDK_KEY_F3:
1526 esc = "OR";break;
1527 case GDK_KEY_F4:
1528 esc = "OS";break;
1529 case GDK_KEY_F5:
1530 esc = "[15~";break;
1531 case GDK_KEY_F6:
1532 esc = "[17~";break;
1533 case GDK_KEY_F7:
1534 esc = "[18~";break;
1535 case GDK_KEY_F8:
1536 esc = "[19~";break;
1537 case GDK_KEY_F9:
1538 esc = "[20~";break;
1539 case GDK_KEY_F10:
1540 esc = "[21~";break;
1541 case GDK_KEY_F11:
1542 esc = "[23~";break;
1543 case GDK_KEY_F12:
1544 esc = "[24~";break;
1545 default:
1546 if (keySym < 0x100) {
1547 uc = keySym;
1548 }
1549 break;
1550 }
1551 }
1552
1553 DPRINTF(1, "escape=%s unicode=%08x\n", esc, uc);
1554
1555 if (vt->y_displ != vt->y_base) {
1556 vt->y_displ = vt->y_base;
1557 spiceterm_refresh (vt);
1558 }
1559
1560 if (esc) {
1561 spiceterm_respond_esc(vt, esc);
1562 } else if (uc > 0) {
1563 if (vt->utf8) {
1564 gchar buf[10];
1565 gint len = g_unichar_to_utf8(uc, buf);
1566
1567 if (len > 0) {
1568 int i;
1569 for (i = 0; i < len; i++) {
1570 vt->ibuf[vt->ibuf_count++] = buf[i];
1571 }
1572 }
1573 } else {
1574 vt->ibuf[vt->ibuf_count++] = (char)uc;
1575 }
1576 }
1577 }
1578 }
1579
1580
1581 ret:
1582
1583 if (flags & 2) { // UP
1584 if (keySym == GDK_KEY_Shift_L || keySym == GDK_KEY_Shift_R) {
1585 shift = 0;
1586 } else if (keySym == GDK_KEY_Control_L || keySym == GDK_KEY_Control_R) {
1587 control = 0;
1588 }
1589 }
1590
1591 vt->screen->core->watch_update_mask(vt->screen->mwatch,
1592 SPICE_WATCH_EVENT_READ|SPICE_WATCH_EVENT_WRITE);
1593 }
1594
1595 static uint8_t
1596 my_kbd_get_leds(SpiceKbdInstance *sin)
1597 {
1598 return 0;
1599 }
1600
1601 static SpiceKbdInterface my_keyboard_sif = {
1602 .base.type = SPICE_INTERFACE_KEYBOARD,
1603 .base.description = "spiceterm keyboard device",
1604 .base.major_version = SPICE_INTERFACE_KEYBOARD_MAJOR,
1605 .base.minor_version = SPICE_INTERFACE_KEYBOARD_MINOR,
1606 .push_keyval = my_kbd_push_keyval,
1607 .push_scan_freg = my_kbd_push_key,
1608 .get_leds = my_kbd_get_leds,
1609 };
1610
1611 /* vdagent interface - to get mouse/clipboarde support */
1612 static int
1613 vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
1614 {
1615 spiceTerm *vt = SPICE_CONTAINEROF(sin, spiceTerm, vdagent_sin);
1616
1617 VDIChunkHeader *hdr = (VDIChunkHeader *)buf;
1618 VDAgentMessage *msg = (VDAgentMessage *)&hdr[1];
1619
1620 //g_assert(hdr->port == VDP_SERVER_PORT);
1621 g_assert(msg->protocol == VD_AGENT_PROTOCOL);
1622
1623 DPRINTF(1, "%d %d %d %d", len, hdr->port, msg->protocol, msg->type);
1624
1625 if (msg->type == VD_AGENT_MOUSE_STATE) {
1626 VDAgentMouseState *info = (VDAgentMouseState *)&msg[1];
1627 spiceterm_motion_event(vt, info->x, info->y, info->buttons);
1628 } else if (msg->type == VD_AGENT_ANNOUNCE_CAPABILITIES) {
1629 /* ignore for now */
1630 } else if (msg->type == VD_AGENT_MONITORS_CONFIG) {
1631 /* ignore for now */
1632 } else {
1633 DPRINTF(0, "got uknown vdagent message type %d\n", msg->type);
1634 }
1635
1636 return len;
1637 }
1638
1639 static int
1640 vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len)
1641 {
1642 DPRINTF(0, "%d", len);
1643
1644 return 0;
1645 }
1646
1647 static void
1648 vmc_state(SpiceCharDeviceInstance *sin, int connected)
1649 {
1650
1651 }
1652
1653 static SpiceCharDeviceInterface my_vdagent_sif = {
1654 .base.type = SPICE_INTERFACE_CHAR_DEVICE,
1655 .base.description = "spice virtual channel char device",
1656 .base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR,
1657 .base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR,
1658 .state = vmc_state,
1659 .write = vmc_write,
1660 .read = vmc_read,
1661 };
1662
1663 static spiceTerm *
1664 create_spiceterm(int argc, char** argv, int maxx, int maxy, guint timeout)
1665 {
1666 int i;
1667
1668 SpiceScreen *spice_screen;
1669
1670 SpiceCoreInterface *core = basic_event_loop_init();
1671 spice_screen = spice_screen_new(core, timeout);
1672 //spice_server_set_image_compression(server, SPICE_IMAGE_COMPRESS_OFF);
1673
1674 spiceTerm *vt = (spiceTerm *)calloc (sizeof(spiceTerm), 1);
1675
1676 vt->keyboard_sin.base.sif = &my_keyboard_sif.base;
1677 spice_server_add_interface(spice_screen->server, &vt->keyboard_sin.base);
1678
1679 vt->vdagent_sin.base.sif = &my_vdagent_sif.base;
1680 vt->vdagent_sin.subtype = "vdagent";
1681 spice_server_add_interface(spice_screen->server, &vt->vdagent_sin.base);
1682
1683 // screen->setXCutText = spiceterm_set_xcut_text;
1684 // screen->ptrAddEvent = spiceterm_pointer_event;
1685 // screen->newClientHook = new_client;
1686 // screen->desktopName = "SPICE Command Terminal";
1687
1688 vt->maxx = spice_screen->width;
1689 vt->maxy = spice_screen->height;
1690
1691 vt->width = vt->maxx / 8;
1692 vt->height = vt->maxy / 16;
1693
1694 vt->total_height = vt->height * 20;
1695 vt->scroll_height = 0;
1696 vt->y_base = 0;
1697 vt->y_displ = 0;
1698
1699 vt->region_top = 0;
1700 vt->region_bottom = vt->height;
1701
1702 vt->g0enc = LAT1_MAP;
1703 vt->g1enc = GRAF_MAP;
1704 vt->cur_enc = vt->g0enc;
1705 vt->charset = 0;
1706
1707 /* default text attributes */
1708 vt->default_attrib.bold = 0;
1709 vt->default_attrib.uline = 0;
1710 vt->default_attrib.blink = 0;
1711 vt->default_attrib.invers = 0;
1712 vt->default_attrib.unvisible = 0;
1713 vt->default_attrib.fgcol = 7;
1714 vt->default_attrib.bgcol = 0;
1715
1716 vt->cur_attrib = vt->default_attrib;
1717
1718 vt->cells = (TextCell *)calloc (sizeof (TextCell), vt->width*vt->total_height);
1719
1720 for (i = 0; i < vt->width*vt->total_height; i++) {
1721 vt->cells[i].ch = ' ';
1722 vt->cells[i].attrib = vt->default_attrib;
1723 }
1724
1725 vt->altcells = (TextCell *)calloc (sizeof (TextCell), vt->width*vt->height);
1726
1727 vt->screen = spice_screen;
1728
1729 return vt;
1730 }
1731
1732 static gboolean
1733 master_error_callback(GIOChannel *channel, GIOCondition condition,
1734 gpointer data)
1735 {
1736 //spiceTerm *vt = (spiceTerm *)data;
1737
1738 DPRINTF(1, "condition %d", condition);
1739
1740 exit(0);
1741
1742 return FALSE;
1743 }
1744
1745 static void
1746 master_watch(int master, int event, void *opaque)
1747 {
1748 spiceTerm *vt = (spiceTerm *)opaque;
1749
1750 // fixme: if (!vt->mark_active) {
1751
1752 if (event == SPICE_WATCH_EVENT_READ) {
1753 char buffer[1024];
1754 int c;
1755 while ((c = read(master, buffer, 1024)) == -1) {
1756 if (errno != EAGAIN) break;
1757 }
1758 if (c == -1) {
1759 g_error("got read error"); // fixme
1760 }
1761 spiceterm_puts (vt, buffer, c);
1762 } else {
1763 if (vt->ibuf_count > 0) {
1764 DPRINTF(1, "write input %x %d", vt->ibuf[0], vt->ibuf_count);
1765 write (master, vt->ibuf, vt->ibuf_count);
1766 vt->ibuf_count = 0; // fixme: what if not all data written
1767 }
1768 vt->screen->core->watch_update_mask(vt->screen->mwatch, SPICE_WATCH_EVENT_READ);
1769 }
1770 }
1771
1772 int
1773 main (int argc, char** argv)
1774 {
1775 int i;
1776 char **cmdargv = NULL;
1777 char *command = "/bin/bash"; // execute normal shell as default
1778 int pid;
1779 int master;
1780 char ptyname[1024];
1781 struct winsize dimensions;
1782
1783 g_thread_init(NULL);
1784
1785 for (i = 1; i < argc; i++) {
1786 if (!strcmp (argv[i], "-c")) {
1787 command = argv[i+1];
1788 cmdargv = &argv[i+1];
1789 argc = i;
1790 argv[i] = NULL;
1791 break;
1792 }
1793 }
1794
1795 if (0) print_usage(NULL); // fixme:
1796
1797 spiceTerm *vt = create_spiceterm (argc, argv, 745, 400, 10);
1798
1799 setlocale(LC_ALL, ""); // set from environment
1800
1801 char *ctype = setlocale (LC_CTYPE, NULL); // query LC_CTYPE
1802
1803 // fixme: ist there a standard way to detect utf8 mode ?
1804 if (strcasestr (ctype, ".utf-8")||strcasestr (ctype, ".utf8")) {
1805 vt->utf8 = 1;
1806 }
1807
1808 dimensions.ws_col = vt->width;
1809 dimensions.ws_row = vt->height;
1810
1811 setenv ("TERM", TERM, 1);
1812
1813 DPRINTF(1, "execute %s", command);
1814
1815 pid = forkpty (&master, ptyname, NULL, &dimensions);
1816 if(!pid) {
1817
1818 // install default signal handlers
1819 signal (SIGQUIT, SIG_DFL);
1820 signal (SIGTERM, SIG_DFL);
1821 signal (SIGINT, SIG_DFL);
1822
1823 if (cmdargv) {
1824 execvp (command, cmdargv);
1825 } else {
1826 execlp (command, command, NULL);
1827 }
1828 perror ("Error: exec failed\n");
1829 exit (-1); // should not be reached
1830 } else if (pid == -1) {
1831 perror ("Error: fork failed\n");
1832 exit (-1);
1833 }
1834
1835 /* watch for errors - we need to use glib directly because spice
1836 * does not have SPICE_WATCH_EVENT for this */
1837 GIOChannel *channel = g_io_channel_unix_new(master);
1838 g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL);
1839 g_io_channel_set_encoding(channel, NULL, NULL);
1840 g_io_add_watch(channel, G_IO_ERR|G_IO_HUP, master_error_callback, vt);
1841
1842 vt->screen->mwatch = vt->screen->core->watch_add(
1843 master, SPICE_WATCH_EVENT_READ /* |SPICE_WATCH_EVENT_WRITE */,
1844 master_watch, vt);
1845
1846 basic_event_loop_mainloop();
1847
1848 kill (pid, 9);
1849 int status;
1850 waitpid(pid, &status, 0);
1851
1852 exit (0);
1853 }