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