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