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