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