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