4 * Copyright (c) 2011 David Herrmann <dh.herrmann@googlemail.com>
5 * Copyright (c) 2011 University of Tuebingen
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files
9 * (the "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * Virtual Terminal Emulator
29 * This is the VT implementation. It is written from scratch. It uses the
30 * screen state-machine as output and is tightly bound to it. It supports
31 * functionality from vt100 up to vt500 series. It doesn't implement an
32 * explicitly selected terminal but tries to support the most important commands
33 * to be compatible with existing implementations. However, full vt102
34 * compatibility is the least that is provided.
36 * The main parser in this file controls the parser-state and dispatches the
37 * actions to the related handlers. The parser is based on the state-diagram
38 * from Paul Williams: http://vt100.net/emu/
39 * It is written from scratch, though.
40 * This parser is fully compatible up to the vt500 series. It requires UTF-8 and
41 * does not support any other input encoding. The G0 and G1 sets are therefore
42 * defined as subsets of UTF-8. You may still map G0-G3 into GL, though.
44 * However, the CSI/DCS/etc handlers are not designed after a specific VT
45 * series. We try to support all vt102 commands but implement several other
46 * often used sequences, too. Feel free to add further.
48 * See ./doc/vte.txt for more information on this VT-emulator.
55 #include <xkbcommon/xkbcommon-keysyms.h>
57 #include "tsm_screen.h"
58 #include "tsm_unicode.h"
61 #define LLOG_SUBSYSTEM "tsm_vte"
63 /* Input parser states */
65 STATE_NONE, /* placeholder */
66 STATE_GROUND, /* initial state and ground */
67 STATE_ESC, /* ESC sequence was started */
68 STATE_ESC_INT, /* intermediate escape characters */
69 STATE_CSI_ENTRY, /* starting CSI sequence */
70 STATE_CSI_PARAM, /* CSI parameters */
71 STATE_CSI_INT, /* intermediate CSI characters */
72 STATE_CSI_IGNORE, /* CSI error; ignore this CSI sequence */
73 STATE_DCS_ENTRY, /* starting DCS sequence */
74 STATE_DCS_PARAM, /* DCS parameters */
75 STATE_DCS_INT, /* intermediate DCS characters */
76 STATE_DCS_PASS, /* DCS data passthrough */
77 STATE_DCS_IGNORE, /* DCS error; ignore this DCS sequence */
78 STATE_OSC_STRING, /* parsing OCS sequence */
79 STATE_ST_IGNORE, /* unimplemented seq; ignore until ST */
83 /* Input parser actions */
85 ACTION_NONE, /* placeholder */
86 ACTION_IGNORE, /* ignore the character entirely */
87 ACTION_PRINT, /* print the character on the console */
88 ACTION_EXECUTE, /* execute single control character (C0/C1) */
89 ACTION_CLEAR, /* clear current parameter state */
90 ACTION_COLLECT, /* collect intermediate character */
91 ACTION_PARAM, /* collect parameter character */
92 ACTION_ESC_DISPATCH, /* dispatch escape sequence */
93 ACTION_CSI_DISPATCH, /* dispatch csi sequence */
94 ACTION_DCS_START, /* start of DCS data */
95 ACTION_DCS_COLLECT, /* collect DCS data */
96 ACTION_DCS_END, /* end of DCS data */
97 ACTION_OSC_START, /* start of OSC data */
98 ACTION_OSC_COLLECT, /* collect OSC data */
99 ACTION_OSC_END, /* end of OSC data */
104 #define CSI_BANG 0x0001 /* CSI: ! */
105 #define CSI_CASH 0x0002 /* CSI: $ */
106 #define CSI_WHAT 0x0004 /* CSI: ? */
107 #define CSI_GT 0x0008 /* CSI: > */
108 #define CSI_SPACE 0x0010 /* CSI: */
109 #define CSI_SQUOTE 0x0020 /* CSI: ' */
110 #define CSI_DQUOTE 0x0040 /* CSI: " */
111 #define CSI_MULT 0x0080 /* CSI: * */
112 #define CSI_PLUS 0x0100 /* CSI: + */
113 #define CSI_POPEN 0x0200 /* CSI: ( */
114 #define CSI_PCLOSE 0x0400 /* CSI: ) */
116 /* max CSI arguments */
117 #define CSI_ARG_MAX 16
120 #define FLAG_CURSOR_KEY_MODE 0x00000001 /* DEC cursor key mode */
121 #define FLAG_KEYPAD_APPLICATION_MODE 0x00000002 /* DEC keypad application mode; TODO: toggle on numlock? */
122 #define FLAG_LINE_FEED_NEW_LINE_MODE 0x00000004 /* DEC line-feed/new-line mode */
123 #define FLAG_8BIT_MODE 0x00000008 /* Disable UTF-8 mode and enable 8bit compatible mode */
124 #define FLAG_7BIT_MODE 0x00000010 /* Disable 8bit mode and use 7bit compatible mode */
125 #define FLAG_USE_C1 0x00000020 /* Explicitely use 8bit C1 codes; TODO: implement */
126 #define FLAG_KEYBOARD_ACTION_MODE 0x00000040 /* Disable keyboard; TODO: implement? */
127 #define FLAG_INSERT_REPLACE_MODE 0x00000080 /* Enable insert mode */
128 #define FLAG_SEND_RECEIVE_MODE 0x00000100 /* Disable local echo */
129 #define FLAG_TEXT_CURSOR_MODE 0x00000200 /* Show cursor */
130 #define FLAG_INVERSE_SCREEN_MODE 0x00000400 /* Inverse colors */
131 #define FLAG_ORIGIN_MODE 0x00000800 /* Relative origin for cursor */
132 #define FLAG_AUTO_WRAP_MODE 0x00001000 /* Auto line wrap mode */
133 #define FLAG_AUTO_REPEAT_MODE 0x00002000 /* Auto repeat key press; TODO: implement */
134 #define FLAG_NATIONAL_CHARSET_MODE 0x00004000 /* Send keys from nation charsets; TODO: implement */
135 #define FLAG_BACKGROUND_COLOR_ERASE_MODE 0x00008000 /* Set background color on erase (bce) */
136 #define FLAG_PREPEND_ESCAPE 0x00010000 /* Prepend escape character to next output */
138 struct vte_saved_state {
139 unsigned int cursor_x;
140 unsigned int cursor_y;
141 struct tsm_screen_attr cattr;
151 struct tsm_screen *con;
152 tsm_vte_write_cb write_cb;
156 struct tsm_utf8_mach *mach;
157 unsigned long parse_cnt;
160 unsigned int csi_argc;
161 int csi_argv[CSI_ARG_MAX];
162 unsigned int csi_flags;
164 uint8_t (*palette)[3];
165 struct tsm_screen_attr def_attr;
166 struct tsm_screen_attr cattr;
171 tsm_vte_charset *glt;
172 tsm_vte_charset *grt;
178 struct vte_saved_state saved_state;
203 static uint8_t color_palette[COLOR_NUM][3] = {
204 [COLOR_BLACK] = { 0, 0, 0 }, /* black */
205 [COLOR_RED] = { 205, 0, 0 }, /* red */
206 [COLOR_GREEN] = { 0, 205, 0 }, /* green */
207 [COLOR_YELLOW] = { 205, 205, 0 }, /* yellow */
208 [COLOR_BLUE] = { 0, 0, 238 }, /* blue */
209 [COLOR_MAGENTA] = { 205, 0, 205 }, /* magenta */
210 [COLOR_CYAN] = { 0, 205, 205 }, /* cyan */
211 [COLOR_LIGHT_GREY] = { 229, 229, 229 }, /* light grey */
212 [COLOR_DARK_GREY] = { 127, 127, 127 }, /* dark grey */
213 [COLOR_LIGHT_RED] = { 255, 0, 0 }, /* light red */
214 [COLOR_LIGHT_GREEN] = { 0, 255, 0 }, /* light green */
215 [COLOR_LIGHT_YELLOW] = { 255, 255, 0 }, /* light yellow */
216 [COLOR_LIGHT_BLUE] = { 92, 92, 255 }, /* light blue */
217 [COLOR_LIGHT_MAGENTA] = { 255, 0, 255 }, /* light magenta */
218 [COLOR_LIGHT_CYAN] = { 0, 255, 255 }, /* light cyan */
219 [COLOR_WHITE] = { 255, 255, 255 }, /* white */
221 [COLOR_FOREGROUND] = { 229, 229, 229 }, /* light grey */
222 [COLOR_BACKGROUND] = { 0, 0, 0 }, /* black */
225 static uint8_t color_palette_solarized[COLOR_NUM][3] = {
226 [COLOR_BLACK] = { 7, 54, 66 }, /* black */
227 [COLOR_RED] = { 220, 50, 47 }, /* red */
228 [COLOR_GREEN] = { 133, 153, 0 }, /* green */
229 [COLOR_YELLOW] = { 181, 137, 0 }, /* yellow */
230 [COLOR_BLUE] = { 38, 139, 210 }, /* blue */
231 [COLOR_MAGENTA] = { 211, 54, 130 }, /* magenta */
232 [COLOR_CYAN] = { 42, 161, 152 }, /* cyan */
233 [COLOR_LIGHT_GREY] = { 238, 232, 213 }, /* light grey */
234 [COLOR_DARK_GREY] = { 0, 43, 54 }, /* dark grey */
235 [COLOR_LIGHT_RED] = { 203, 75, 22 }, /* light red */
236 [COLOR_LIGHT_GREEN] = { 88, 110, 117 }, /* light green */
237 [COLOR_LIGHT_YELLOW] = { 101, 123, 131 }, /* light yellow */
238 [COLOR_LIGHT_BLUE] = { 131, 148, 150 }, /* light blue */
239 [COLOR_LIGHT_MAGENTA] = { 108, 113, 196 }, /* light magenta */
240 [COLOR_LIGHT_CYAN] = { 147, 161, 161 }, /* light cyan */
241 [COLOR_WHITE] = { 253, 246, 227 }, /* white */
243 [COLOR_FOREGROUND] = { 238, 232, 213 }, /* light grey */
244 [COLOR_BACKGROUND] = { 7, 54, 66 }, /* black */
247 static uint8_t color_palette_solarized_black[COLOR_NUM][3] = {
248 [COLOR_BLACK] = { 0, 0, 0 }, /* black */
249 [COLOR_RED] = { 220, 50, 47 }, /* red */
250 [COLOR_GREEN] = { 133, 153, 0 }, /* green */
251 [COLOR_YELLOW] = { 181, 137, 0 }, /* yellow */
252 [COLOR_BLUE] = { 38, 139, 210 }, /* blue */
253 [COLOR_MAGENTA] = { 211, 54, 130 }, /* magenta */
254 [COLOR_CYAN] = { 42, 161, 152 }, /* cyan */
255 [COLOR_LIGHT_GREY] = { 238, 232, 213 }, /* light grey */
256 [COLOR_DARK_GREY] = { 0, 43, 54 }, /* dark grey */
257 [COLOR_LIGHT_RED] = { 203, 75, 22 }, /* light red */
258 [COLOR_LIGHT_GREEN] = { 88, 110, 117 }, /* light green */
259 [COLOR_LIGHT_YELLOW] = { 101, 123, 131 }, /* light yellow */
260 [COLOR_LIGHT_BLUE] = { 131, 148, 150 }, /* light blue */
261 [COLOR_LIGHT_MAGENTA] = { 108, 113, 196 }, /* light magenta */
262 [COLOR_LIGHT_CYAN] = { 147, 161, 161 }, /* light cyan */
263 [COLOR_WHITE] = { 253, 246, 227 }, /* white */
265 [COLOR_FOREGROUND] = { 238, 232, 213 }, /* light grey */
266 [COLOR_BACKGROUND] = { 0, 0, 0 }, /* black */
269 static uint8_t color_palette_solarized_white[COLOR_NUM][3] = {
270 [COLOR_BLACK] = { 7, 54, 66 }, /* black */
271 [COLOR_RED] = { 220, 50, 47 }, /* red */
272 [COLOR_GREEN] = { 133, 153, 0 }, /* green */
273 [COLOR_YELLOW] = { 181, 137, 0 }, /* yellow */
274 [COLOR_BLUE] = { 38, 139, 210 }, /* blue */
275 [COLOR_MAGENTA] = { 211, 54, 130 }, /* magenta */
276 [COLOR_CYAN] = { 42, 161, 152 }, /* cyan */
277 [COLOR_LIGHT_GREY] = { 238, 232, 213 }, /* light grey */
278 [COLOR_DARK_GREY] = { 0, 43, 54 }, /* dark grey */
279 [COLOR_LIGHT_RED] = { 203, 75, 22 }, /* light red */
280 [COLOR_LIGHT_GREEN] = { 88, 110, 117 }, /* light green */
281 [COLOR_LIGHT_YELLOW] = { 101, 123, 131 }, /* light yellow */
282 [COLOR_LIGHT_BLUE] = { 131, 148, 150 }, /* light blue */
283 [COLOR_LIGHT_MAGENTA] = { 108, 113, 196 }, /* light magenta */
284 [COLOR_LIGHT_CYAN] = { 147, 161, 161 }, /* light cyan */
285 [COLOR_WHITE] = { 253, 246, 227 }, /* white */
287 [COLOR_FOREGROUND] = { 7, 54, 66 }, /* black */
288 [COLOR_BACKGROUND] = { 238, 232, 213 }, /* light grey */
291 static uint8_t (*get_palette(struct tsm_vte *vte))[3]
293 if (!vte->palette_name)
294 return color_palette;
296 if (!strcmp(vte->palette_name, "solarized"))
297 return color_palette_solarized;
298 if (!strcmp(vte->palette_name, "solarized-black"))
299 return color_palette_solarized_black;
300 if (!strcmp(vte->palette_name, "solarized-white"))
301 return color_palette_solarized_white;
303 return color_palette;
306 /* Several effects may occur when non-RGB colors are used. For instance, if bold
307 * is enabled, then a dark color code is always converted to a light color to
308 * simulate bold (even though bold may actually be supported!). To support this,
309 * we need to differentiate between a set color-code and a set rgb-color.
310 * This function actually converts a set color-code into an RGB color. This must
311 * be called before passing the attribute to the console layer so the console
312 * layer can always work with RGB values and does not have to care for color
314 static void to_rgb(struct tsm_vte *vte, struct tsm_screen_attr *attr)
320 /* bold causes light colors */
321 if (attr->bold && code < 8)
323 if (code >= COLOR_NUM)
324 code = COLOR_FOREGROUND;
326 attr->fr = vte->palette[code][0];
327 attr->fg = vte->palette[code][1];
328 attr->fb = vte->palette[code][2];
333 if (code >= COLOR_NUM)
334 code = COLOR_BACKGROUND;
336 attr->br = vte->palette[code][0];
337 attr->bg = vte->palette[code][1];
338 attr->bb = vte->palette[code][2];
342 static void copy_fcolor(struct tsm_screen_attr *dest,
343 const struct tsm_screen_attr *src)
345 dest->fccode = src->fccode;
351 static void copy_bcolor(struct tsm_screen_attr *dest,
352 const struct tsm_screen_attr *src)
354 dest->bccode = src->bccode;
360 int tsm_vte_new(struct tsm_vte **out, struct tsm_screen *con,
361 tsm_vte_write_cb write_cb, void *data,
367 if (!out || !con || !write_cb)
370 vte = malloc(sizeof(*vte));
374 memset(vte, 0, sizeof(*vte));
378 vte->write_cb = write_cb;
380 vte->palette = get_palette(vte);
381 vte->def_attr.fccode = COLOR_FOREGROUND;
382 vte->def_attr.bccode = COLOR_BACKGROUND;
383 to_rgb(vte, &vte->def_attr);
385 ret = tsm_utf8_mach_new(&vte->mach);
390 tsm_screen_erase_screen(vte->con, false);
392 llog_debug(vte, "new vte object");
393 tsm_screen_ref(vte->con);
402 void tsm_vte_ref(struct tsm_vte *vte)
410 void tsm_vte_unref(struct tsm_vte *vte)
412 if (!vte || !vte->ref)
418 llog_debug(vte, "destroying vte object");
419 tsm_screen_unref(vte->con);
420 tsm_utf8_mach_free(vte->mach);
424 int tsm_vte_set_palette(struct tsm_vte *vte, const char *palette)
432 tmp = strdup(palette);
437 free(vte->palette_name);
438 vte->palette_name = tmp;
440 vte->palette = get_palette(vte);
441 vte->def_attr.fccode = COLOR_FOREGROUND;
442 vte->def_attr.bccode = COLOR_BACKGROUND;
444 to_rgb(vte, &vte->def_attr);
445 memcpy(&vte->cattr, &vte->def_attr, sizeof(vte->cattr));
447 tsm_screen_set_def_attr(vte->con, &vte->def_attr);
448 tsm_screen_erase_screen(vte->con, false);
454 * Write raw byte-stream to pty.
455 * When writing data to the client we must make sure that we send the correct
456 * encoding. For backwards-compatibility reasons we should always send 7bit
457 * characters exclusively. However, when FLAG_7BIT_MODE is not set, then we can
458 * also send raw 8bit characters. For instance, in FLAG_8BIT_MODE we can use the
459 * GR characters as keyboard input and send them directly or even use the C1
460 * escape characters. In unicode mode (default) we can send multi-byte utf-8
461 * characters which are also 8bit. When sending these characters, set the \raw
462 * flag to true so this function does not perform debug checks on data we send.
463 * If debugging is disabled, these checks are also disabled and won't affect
465 * For better debugging, we also use the __LINE__ and __FILE__ macros. Use the
466 * vte_write() and vte_write_raw() macros below for more convenient use.
468 * As a rule of thumb do never send 8bit characters in escape sequences and also
469 * avoid all 8bit escape codes including the C1 codes. This will guarantee that
470 * all kind of clients are always compatible to us.
472 * If SEND_RECEIVE_MODE is off (that is, local echo is on) we have to send all
473 * data directly to ourself again. However, we must avoid recursion when
474 * tsm_vte_input() itself calls vte_write*(), therefore, we increase the
475 * PARSER counter when entering tsm_vte_input() and reset it when leaving it
476 * so we never echo data that origins from tsm_vte_input().
477 * But note that SEND_RECEIVE_MODE is inherently broken for escape sequences
478 * that request answers. That is, if we send a request to the client that awaits
479 * a response and parse that request via local echo ourself, then we will also
480 * send a response to the client even though he didn't request one. This
481 * recursion fix does not avoid this but only prevents us from endless loops
482 * here. Anyway, only few applications rely on local echo so we can safely
485 static void vte_write_debug(struct tsm_vte *vte, const char *u8, size_t len,
486 bool raw, const char *file, int line)
488 #ifdef BUILD_ENABLE_DEBUG
489 /* in debug mode we check that escape sequences are always <0x7f so they
490 * are correctly parsed by non-unicode and non-8bit-mode clients. */
494 for (i = 0; i < len; ++i) {
496 llog_warning(vte, "sending 8bit character inline to client in %s:%d",
502 /* in local echo mode, directly parse the data again */
503 if (!vte->parse_cnt && !(vte->flags & FLAG_SEND_RECEIVE_MODE)) {
504 if (vte->flags & FLAG_PREPEND_ESCAPE)
505 tsm_vte_input(vte, "\e", 1);
506 tsm_vte_input(vte, u8, len);
509 if (vte->flags & FLAG_PREPEND_ESCAPE)
510 vte->write_cb(vte, "\e", 1, vte->data);
511 vte->write_cb(vte, u8, len, vte->data);
513 vte->flags &= ~FLAG_PREPEND_ESCAPE;
516 #define vte_write(_vte, _u8, _len) \
517 vte_write_debug((_vte), (_u8), (_len), false, __FILE__, __LINE__)
518 #define vte_write_raw(_vte, _u8, _len) \
519 vte_write_debug((_vte), (_u8), (_len), true, __FILE__, __LINE__)
521 /* write to console */
522 static void write_console(struct tsm_vte *vte, tsm_symbol_t sym)
524 to_rgb(vte, &vte->cattr);
525 tsm_screen_write(vte->con, sym, &vte->cattr);
528 static void reset_state(struct tsm_vte *vte)
530 vte->saved_state.cursor_x = 0;
531 vte->saved_state.cursor_y = 0;
532 vte->saved_state.origin_mode = false;
533 vte->saved_state.wrap_mode = true;
534 vte->saved_state.gl = &tsm_vte_unicode_lower;
535 vte->saved_state.gr = &tsm_vte_unicode_upper;
537 copy_fcolor(&vte->saved_state.cattr, &vte->def_attr);
538 copy_bcolor(&vte->saved_state.cattr, &vte->def_attr);
539 vte->saved_state.cattr.bold = 0;
540 vte->saved_state.cattr.underline = 0;
541 vte->saved_state.cattr.inverse = 0;
542 vte->saved_state.cattr.protect = 0;
545 static void save_state(struct tsm_vte *vte)
547 vte->saved_state.cursor_x = tsm_screen_get_cursor_x(vte->con);
548 vte->saved_state.cursor_y = tsm_screen_get_cursor_y(vte->con);
549 vte->saved_state.cattr = vte->cattr;
550 vte->saved_state.gl = vte->gl;
551 vte->saved_state.gr = vte->gr;
552 vte->saved_state.wrap_mode = vte->flags & FLAG_AUTO_WRAP_MODE;
553 vte->saved_state.origin_mode = vte->flags & FLAG_ORIGIN_MODE;
556 static void restore_state(struct tsm_vte *vte)
558 tsm_screen_move_to(vte->con, vte->saved_state.cursor_x,
559 vte->saved_state.cursor_y);
560 vte->cattr = vte->saved_state.cattr;
561 to_rgb(vte, &vte->cattr);
562 if (vte->flags & FLAG_BACKGROUND_COLOR_ERASE_MODE)
563 tsm_screen_set_def_attr(vte->con, &vte->cattr);
564 vte->gl = vte->saved_state.gl;
565 vte->gr = vte->saved_state.gr;
567 if (vte->saved_state.wrap_mode) {
568 vte->flags |= FLAG_AUTO_WRAP_MODE;
569 tsm_screen_set_flags(vte->con, TSM_SCREEN_AUTO_WRAP);
571 vte->flags &= ~FLAG_AUTO_WRAP_MODE;
572 tsm_screen_reset_flags(vte->con, TSM_SCREEN_AUTO_WRAP);
575 if (vte->saved_state.origin_mode) {
576 vte->flags |= FLAG_ORIGIN_MODE;
577 tsm_screen_set_flags(vte->con, TSM_SCREEN_REL_ORIGIN);
579 vte->flags &= ~FLAG_ORIGIN_MODE;
580 tsm_screen_reset_flags(vte->con, TSM_SCREEN_REL_ORIGIN);
586 * This performs a soft reset of the VTE. That is, everything is reset to the
587 * same state as when the VTE was created. This does not affect the console,
590 void tsm_vte_reset(struct tsm_vte *vte)
596 vte->flags |= FLAG_TEXT_CURSOR_MODE;
597 vte->flags |= FLAG_AUTO_REPEAT_MODE;
598 vte->flags |= FLAG_SEND_RECEIVE_MODE;
599 vte->flags |= FLAG_AUTO_WRAP_MODE;
600 vte->flags |= FLAG_BACKGROUND_COLOR_ERASE_MODE;
601 tsm_screen_reset(vte->con);
602 tsm_screen_set_flags(vte->con, TSM_SCREEN_AUTO_WRAP);
604 tsm_utf8_mach_reset(vte->mach);
605 vte->state = STATE_GROUND;
606 vte->gl = &tsm_vte_unicode_lower;
607 vte->gr = &tsm_vte_unicode_upper;
610 vte->g0 = &tsm_vte_unicode_lower;
611 vte->g1 = &tsm_vte_unicode_upper;
612 vte->g2 = &tsm_vte_unicode_lower;
613 vte->g3 = &tsm_vte_unicode_upper;
615 memcpy(&vte->cattr, &vte->def_attr, sizeof(vte->cattr));
616 to_rgb(vte, &vte->cattr);
617 tsm_screen_set_def_attr(vte->con, &vte->def_attr);
622 static void hard_reset(struct tsm_vte *vte)
625 tsm_screen_erase_screen(vte->con, false);
626 tsm_screen_clear_sb(vte->con);
627 tsm_screen_move_to(vte->con, 0, 0);
630 static void send_primary_da(struct tsm_vte *vte)
632 vte_write(vte, "\e[?60;1;6;9;15c", 17);
635 /* execute control character (C0 or C1) */
636 static void do_execute(struct tsm_vte *vte, uint32_t ctrl)
640 /* Ignore on input */
643 /* Transmit answerback message */
644 /* TODO: is there a better answer than ACK? */
645 vte_write(vte, "\x06", 1);
648 /* Sound bell tone */
649 /* TODO: I always considered this annying, however, we
650 * should at least provide some way to enable it if the
651 * user *really* wants it.
655 /* Move cursor one position left */
656 tsm_screen_move_left(vte->con, 1);
659 /* Move to next tab stop or end of line */
660 tsm_screen_tab_right(vte->con, 1);
665 /* Line feed or newline (CR/NL mode) */
666 if (vte->flags & FLAG_LINE_FEED_NEW_LINE_MODE)
667 tsm_screen_newline(vte->con);
669 tsm_screen_move_down(vte->con, 1, true);
672 /* Move cursor to left margin */
673 tsm_screen_move_line_home(vte->con);
676 /* Map G1 character set into GL */
680 /* Map G0 character set into GL */
684 /* Resume transmission */
687 case 0x13: /* XOFF */
688 /* Stop transmission */
692 /* Cancel escape sequence */
693 /* nothing to do here */
696 /* Discard current escape sequence and show err-sym */
697 write_console(vte, 0xbf);
700 /* Invokes an escape sequence */
701 /* nothing to do here */
707 /* Move down one row, perform scroll-up if needed */
708 tsm_screen_move_down(vte->con, 1, true);
711 /* CR/NL with scroll-up if needed */
712 tsm_screen_newline(vte->con);
715 /* Set tab stop at current position */
716 tsm_screen_set_tabstop(vte->con);
719 /* Move up one row, perform scroll-down if needed */
720 tsm_screen_move_up(vte->con, 1, true);
723 /* Temporarily map G2 into GL for next char only */
727 /* Temporarily map G3 into GL for next char only */
730 case 0x9a: /* DECID */
731 /* Send device attributes response like ANSI DA */
732 send_primary_da(vte);
735 /* End control string */
736 /* nothing to do here */
739 llog_warn(vte, "unhandled control char %u", ctrl);
743 static void do_clear(struct tsm_vte *vte)
748 for (i = 0; i < CSI_ARG_MAX; ++i)
749 vte->csi_argv[i] = -1;
753 static void do_collect(struct tsm_vte *vte, uint32_t data)
757 vte->csi_flags |= CSI_BANG;
760 vte->csi_flags |= CSI_CASH;
763 vte->csi_flags |= CSI_WHAT;
766 vte->csi_flags |= CSI_GT;
769 vte->csi_flags |= CSI_SPACE;
772 vte->csi_flags |= CSI_SQUOTE;
775 vte->csi_flags |= CSI_DQUOTE;
778 vte->csi_flags |= CSI_MULT;
781 vte->csi_flags |= CSI_PLUS;
784 vte->csi_flags |= CSI_POPEN;
787 vte->csi_flags |= CSI_PCLOSE;
792 static void do_param(struct tsm_vte *vte, uint32_t data)
797 if (vte->csi_argc < CSI_ARG_MAX)
802 if (vte->csi_argc >= CSI_ARG_MAX)
805 /* avoid integer overflows; max allowed value is 16384 anyway */
806 if (vte->csi_argv[vte->csi_argc] > 0xffff)
809 if (data >= '0' && data <= '9') {
810 new = vte->csi_argv[vte->csi_argc];
814 new = new * 10 + data - '0';
815 vte->csi_argv[vte->csi_argc] = new;
819 static bool set_charset(struct tsm_vte *vte, tsm_vte_charset *set)
821 if (vte->csi_flags & CSI_POPEN)
823 else if (vte->csi_flags & CSI_PCLOSE)
825 else if (vte->csi_flags & CSI_MULT)
827 else if (vte->csi_flags & CSI_PLUS)
835 static void do_esc(struct tsm_vte *vte, uint32_t data)
838 case 'B': /* map ASCII into G0-G3 */
839 if (set_charset(vte, &tsm_vte_unicode_lower))
842 case '<': /* map DEC supplemental into G0-G3 */
843 if (set_charset(vte, &tsm_vte_dec_supplemental_graphics))
846 case '0': /* map DEC special into G0-G3 */
847 if (set_charset(vte, &tsm_vte_dec_special_graphics))
850 case 'A': /* map British into G0-G3 */
851 /* TODO: create British charset from DEC */
852 if (set_charset(vte, &tsm_vte_unicode_upper))
855 case '4': /* map Dutch into G0-G3 */
856 /* TODO: create Dutch charset from DEC */
857 if (set_charset(vte, &tsm_vte_unicode_upper))
861 case '5': /* map Finnish into G0-G3 */
862 /* TODO: create Finnish charset from DEC */
863 if (set_charset(vte, &tsm_vte_unicode_upper))
866 case 'R': /* map French into G0-G3 */
867 /* TODO: create French charset from DEC */
868 if (set_charset(vte, &tsm_vte_unicode_upper))
871 case 'Q': /* map French-Canadian into G0-G3 */
872 /* TODO: create French-Canadian charset from DEC */
873 if (set_charset(vte, &tsm_vte_unicode_upper))
876 case 'K': /* map German into G0-G3 */
877 /* TODO: create German charset from DEC */
878 if (set_charset(vte, &tsm_vte_unicode_upper))
881 case 'Y': /* map Italian into G0-G3 */
882 /* TODO: create Italian charset from DEC */
883 if (set_charset(vte, &tsm_vte_unicode_upper))
887 case '6': /* map Norwegian/Danish into G0-G3 */
888 /* TODO: create Norwegian/Danish charset from DEC */
889 if (set_charset(vte, &tsm_vte_unicode_upper))
892 case 'Z': /* map Spanish into G0-G3 */
893 /* TODO: create Spanish charset from DEC */
894 if (set_charset(vte, &tsm_vte_unicode_upper))
898 case '7': /* map Swedish into G0-G3 */
899 /* TODO: create Swedish charset from DEC */
900 if (set_charset(vte, &tsm_vte_unicode_upper))
903 case '=': /* map Swiss into G0-G3 */
904 /* TODO: create Swiss charset from DEC */
905 if (set_charset(vte, &tsm_vte_unicode_upper))
909 if (vte->csi_flags & CSI_SPACE) {
911 /* Disable 8bit C1 mode */
912 vte->flags &= ~FLAG_USE_C1;
917 if (vte->csi_flags & CSI_SPACE) {
919 /* Enable 8bit C1 mode */
920 vte->flags |= FLAG_USE_C1;
926 /* everything below is only valid without CSI flags */
927 if (vte->csi_flags) {
928 llog_debug(vte, "unhandled escape seq %u", data);
934 /* Move down one row, perform scroll-up if needed */
935 tsm_screen_move_down(vte->con, 1, true);
938 /* CR/NL with scroll-up if needed */
939 tsm_screen_newline(vte->con);
942 /* Set tab stop at current position */
943 tsm_screen_set_tabstop(vte->con);
946 /* Move up one row, perform scroll-down if needed */
947 tsm_screen_move_up(vte->con, 1, true);
950 /* Temporarily map G2 into GL for next char only */
954 /* Temporarily map G3 into GL for next char only */
957 case 'Z': /* DECID */
958 /* Send device attributes response like ANSI DA */
959 send_primary_da(vte);
962 /* End control string */
963 /* nothing to do here */
966 /* Invoke G1 into GR */
970 /* Invoke G2 into GL */
974 /* Invoke G2 into GR */
978 /* Invoke G3 into GL */
982 /* Invoke G3 into GR */
985 case '=': /* DECKPAM */
986 /* Set application keypad mode */
987 vte->flags |= FLAG_KEYPAD_APPLICATION_MODE;
989 case '>': /* DECKPNM */
990 /* Set numeric keypad mode */
991 vte->flags &= ~FLAG_KEYPAD_APPLICATION_MODE;
997 case '7': /* DECSC */
998 /* save console state */
1001 case '8': /* DECRC */
1002 /* restore console state */
1006 llog_debug(vte, "unhandled escape seq %u", data);
1010 static void csi_attribute(struct tsm_vte *vte)
1012 static const uint8_t bval[6] = { 0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff };
1013 unsigned int i, code;
1015 if (vte->csi_argc <= 1 && vte->csi_argv[0] == -1) {
1017 vte->csi_argv[0] = 0;
1020 for (i = 0; i < vte->csi_argc; ++i) {
1021 switch (vte->csi_argv[i]) {
1025 copy_fcolor(&vte->cattr, &vte->def_attr);
1026 copy_bcolor(&vte->cattr, &vte->def_attr);
1027 vte->cattr.bold = 0;
1028 vte->cattr.underline = 0;
1029 vte->cattr.inverse = 0;
1032 vte->cattr.bold = 1;
1035 vte->cattr.underline = 1;
1038 vte->cattr.inverse = 1;
1041 vte->cattr.bold = 0;
1044 vte->cattr.underline = 0;
1047 vte->cattr.inverse = 0;
1050 vte->cattr.fccode = COLOR_BLACK;
1053 vte->cattr.fccode = COLOR_RED;
1056 vte->cattr.fccode = COLOR_GREEN;
1059 vte->cattr.fccode = COLOR_YELLOW;
1062 vte->cattr.fccode = COLOR_BLUE;
1065 vte->cattr.fccode = COLOR_MAGENTA;
1068 vte->cattr.fccode = COLOR_CYAN;
1071 vte->cattr.fccode = COLOR_LIGHT_GREY;
1074 copy_fcolor(&vte->cattr, &vte->def_attr);
1077 vte->cattr.bccode = COLOR_BLACK;
1080 vte->cattr.bccode = COLOR_RED;
1083 vte->cattr.bccode = COLOR_GREEN;
1086 vte->cattr.bccode = COLOR_YELLOW;
1089 vte->cattr.bccode = COLOR_BLUE;
1092 vte->cattr.bccode = COLOR_MAGENTA;
1095 vte->cattr.bccode = COLOR_CYAN;
1098 vte->cattr.bccode = COLOR_LIGHT_GREY;
1101 copy_bcolor(&vte->cattr, &vte->def_attr);
1104 vte->cattr.fccode = COLOR_DARK_GREY;
1107 vte->cattr.fccode = COLOR_LIGHT_RED;
1110 vte->cattr.fccode = COLOR_LIGHT_GREEN;
1113 vte->cattr.fccode = COLOR_LIGHT_YELLOW;
1116 vte->cattr.fccode = COLOR_LIGHT_BLUE;
1119 vte->cattr.fccode = COLOR_LIGHT_MAGENTA;
1122 vte->cattr.fccode = COLOR_LIGHT_CYAN;
1125 vte->cattr.fccode = COLOR_WHITE;
1128 vte->cattr.bccode = COLOR_DARK_GREY;
1131 vte->cattr.bccode = COLOR_LIGHT_RED;
1134 vte->cattr.bccode = COLOR_LIGHT_GREEN;
1137 vte->cattr.bccode = COLOR_LIGHT_YELLOW;
1140 vte->cattr.bccode = COLOR_LIGHT_BLUE;
1143 vte->cattr.bccode = COLOR_LIGHT_MAGENTA;
1146 vte->cattr.bccode = COLOR_LIGHT_CYAN;
1149 vte->cattr.bccode = COLOR_WHITE;
1154 if (i + 2 >= vte->csi_argc ||
1155 vte->csi_argv[i + 1] != 5 ||
1156 vte->csi_argv[i + 2] < 0) {
1157 llog_debug(vte, "invalid 256color SGR");
1161 code = vte->csi_argv[i + 2];
1162 if (vte->csi_argv[i] == 38) {
1164 vte->cattr.fccode = code;
1165 } else if (code < 232) {
1166 vte->cattr.fccode = -1;
1168 vte->cattr.fb = bval[code % 6];
1170 vte->cattr.fg = bval[code % 6];
1172 vte->cattr.fr = bval[code % 6];
1174 vte->cattr.fccode = -1;
1175 code = (code - 232) * 10 + 8;
1176 vte->cattr.fr = code;
1177 vte->cattr.fg = code;
1178 vte->cattr.fb = code;
1182 vte->cattr.bccode = code;
1183 } else if (code < 232) {
1184 vte->cattr.bccode = -1;
1186 vte->cattr.bb = bval[code % 6];
1188 vte->cattr.bg = bval[code % 6];
1190 vte->cattr.br = bval[code % 6];
1192 vte->cattr.bccode = -1;
1193 code = (code - 232) * 10 + 8;
1194 vte->cattr.br = code;
1195 vte->cattr.bg = code;
1196 vte->cattr.bb = code;
1203 llog_debug(vte, "unhandled SGR attr %i",
1208 to_rgb(vte, &vte->cattr);
1209 if (vte->flags & FLAG_BACKGROUND_COLOR_ERASE_MODE)
1210 tsm_screen_set_def_attr(vte->con, &vte->cattr);
1213 static void csi_soft_reset(struct tsm_vte *vte)
1218 static void csi_compat_mode(struct tsm_vte *vte)
1220 /* always perform soft reset */
1221 csi_soft_reset(vte);
1223 if (vte->csi_argv[0] == 61) {
1224 /* Switching to VT100 compatibility mode. We do
1225 * not support this mode, so ignore it. In fact,
1226 * we are almost compatible to it, anyway, so
1227 * there is no need to explicitely select it.
1228 * However, we enable 7bit mode to avoid
1229 * character-table problems */
1230 vte->flags |= FLAG_7BIT_MODE;
1231 vte->gl = &tsm_vte_unicode_lower;
1232 vte->gr = &tsm_vte_dec_supplemental_graphics;
1233 } else if (vte->csi_argv[0] == 62 ||
1234 vte->csi_argv[0] == 63 ||
1235 vte->csi_argv[0] == 64) {
1236 /* Switching to VT2/3/4 compatibility mode. We
1237 * are always compatible with this so ignore it.
1238 * We always send 7bit controls so we also do
1239 * not care for the parameter value here that
1240 * select the control-mode.
1241 * VT220 defines argument 2 as 7bit mode but
1242 * VT3xx up to VT5xx use it as 8bit mode. We
1243 * choose to conform with the latter here.
1244 * We also enable 8bit mode when VT220
1245 * compatibility is requested explicitely. */
1246 if (vte->csi_argv[1] == 1 ||
1247 vte->csi_argv[1] == 2)
1248 vte->flags |= FLAG_USE_C1;
1250 vte->flags |= FLAG_8BIT_MODE;
1251 vte->gl = &tsm_vte_unicode_lower;
1252 vte->gr = &tsm_vte_dec_supplemental_graphics;
1254 llog_debug(vte, "unhandled DECSCL 'p' CSI %i, switching to utf-8 mode again",
1259 static inline void set_reset_flag(struct tsm_vte *vte, bool set,
1265 vte->flags &= ~flag;
1268 static void csi_mode(struct tsm_vte *vte, bool set)
1272 for (i = 0; i < vte->csi_argc; ++i) {
1273 if (!(vte->csi_flags & CSI_WHAT)) {
1274 switch (vte->csi_argv[i]) {
1278 set_reset_flag(vte, set,
1279 FLAG_KEYBOARD_ACTION_MODE);
1282 set_reset_flag(vte, set,
1283 FLAG_INSERT_REPLACE_MODE);
1285 tsm_screen_set_flags(vte->con,
1286 TSM_SCREEN_INSERT_MODE);
1288 tsm_screen_reset_flags(vte->con,
1289 TSM_SCREEN_INSERT_MODE);
1292 set_reset_flag(vte, set,
1293 FLAG_SEND_RECEIVE_MODE);
1296 set_reset_flag(vte, set,
1297 FLAG_LINE_FEED_NEW_LINE_MODE);
1300 llog_debug(vte, "unknown non-DEC (Re)Set-Mode %d",
1306 switch (vte->csi_argv[i]) {
1309 case 1: /* DECCKM */
1310 set_reset_flag(vte, set, FLAG_CURSOR_KEY_MODE);
1312 case 2: /* DECANM */
1313 /* Select VT52 mode */
1314 /* We do not support VT52 mode. Is there any reason why
1315 * we should support it? We ignore it here and do not
1316 * mark it as to-do item unless someone has strong
1317 * arguments to support it. */
1319 case 3: /* DECCOLM */
1320 /* If set, select 132 column mode, otherwise use 80
1321 * column mode. If neither is selected explicitely, we
1322 * use dynamic mode, that is, we send SIGWCH when the
1323 * size changes and we allow arbitrary buffer
1324 * dimensions. On soft-reset, we automatically fall back
1325 * to the default, that is, dynamic mode.
1326 * Dynamic-mode can be forced to a static mode in the
1327 * config. That is, everytime dynamic-mode becomes
1328 * active, the terminal will be set to the dimensions
1329 * that were selected in the config. This allows setting
1330 * a fixed size for the terminal regardless of the
1332 * TODO: Implement this */
1334 case 4: /* DECSCLM */
1335 /* Select smooth scrolling. We do not support the
1336 * classic smooth scrolling because we have a scrollback
1337 * buffer. There is no need to implement smooth
1338 * scrolling so ignore this here. */
1340 case 5: /* DECSCNM */
1341 set_reset_flag(vte, set, FLAG_INVERSE_SCREEN_MODE);
1343 tsm_screen_set_flags(vte->con,
1344 TSM_SCREEN_INVERSE);
1346 tsm_screen_reset_flags(vte->con,
1347 TSM_SCREEN_INVERSE);
1350 set_reset_flag(vte, set, FLAG_ORIGIN_MODE);
1352 tsm_screen_set_flags(vte->con,
1353 TSM_SCREEN_REL_ORIGIN);
1355 tsm_screen_reset_flags(vte->con,
1356 TSM_SCREEN_REL_ORIGIN);
1358 case 7: /* DECAWN */
1359 set_reset_flag(vte, set, FLAG_AUTO_WRAP_MODE);
1361 tsm_screen_set_flags(vte->con,
1362 TSM_SCREEN_AUTO_WRAP);
1364 tsm_screen_reset_flags(vte->con,
1365 TSM_SCREEN_AUTO_WRAP);
1367 case 8: /* DECARM */
1368 set_reset_flag(vte, set, FLAG_AUTO_REPEAT_MODE);
1370 case 18: /* DECPFF */
1371 /* If set, a form feed (FF) is sent to the printer after
1372 * every screen that is printed. We don't have printers
1373 * these days directly attached to terminals so we
1374 * ignore this here. */
1376 case 19: /* DECPEX */
1377 /* If set, the full screen is printed instead of
1378 * scrolling region only. We have no printer so ignore
1381 case 25: /* DECTCEM */
1382 set_reset_flag(vte, set, FLAG_TEXT_CURSOR_MODE);
1384 tsm_screen_reset_flags(vte->con,
1385 TSM_SCREEN_HIDE_CURSOR);
1387 tsm_screen_set_flags(vte->con,
1388 TSM_SCREEN_HIDE_CURSOR);
1390 case 42: /* DECNRCM */
1391 set_reset_flag(vte, set, FLAG_NATIONAL_CHARSET_MODE);
1394 llog_debug(vte, "unknown DEC %set-Mode %d",
1395 set?"S":"Res", vte->csi_argv[i]);
1401 static void csi_dev_attr(struct tsm_vte *vte)
1403 if (vte->csi_argc <= 1 && vte->csi_argv[0] <= 0) {
1404 if (vte->csi_flags == 0) {
1405 send_primary_da(vte);
1407 } else if (vte->csi_flags & CSI_GT) {
1408 vte_write(vte, "\e[>1;1;0c", 9);
1413 llog_debug(vte, "unhandled DA: %x %d %d %d...", vte->csi_flags,
1414 vte->csi_argv[0], vte->csi_argv[1], vte->csi_argv[2]);
1417 static void csi_dsr(struct tsm_vte *vte)
1420 unsigned int x, y, len;
1422 if (vte->csi_argv[0] == 5) {
1423 vte_write(vte, "\e[0n", 4);
1424 } else if (vte->csi_argv[0] == 6) {
1425 x = tsm_screen_get_cursor_x(vte->con);
1426 y = tsm_screen_get_cursor_y(vte->con);
1427 len = snprintf(buf, sizeof(buf), "\e[%u;%uR", x, y);
1428 if (len >= sizeof(buf))
1429 vte_write(vte, "\e[0;0R", 6);
1431 vte_write(vte, buf, len);
1435 static void do_csi(struct tsm_vte *vte, uint32_t data)
1437 int num, x, y, upper, lower;
1440 if (vte->csi_argc < CSI_ARG_MAX)
1445 /* move cursor up */
1446 num = vte->csi_argv[0];
1449 tsm_screen_move_up(vte->con, num, false);
1452 /* move cursor down */
1453 num = vte->csi_argv[0];
1456 tsm_screen_move_down(vte->con, num, false);
1459 /* move cursor forward */
1460 num = vte->csi_argv[0];
1463 tsm_screen_move_right(vte->con, num);
1466 /* move cursor backward */
1467 num = vte->csi_argv[0];
1470 tsm_screen_move_left(vte->con, num);
1473 /* Vertical Line Position Absolute */
1474 num = vte->csi_argv[0];
1477 x = tsm_screen_get_cursor_x(vte->con);
1478 tsm_screen_move_to(vte->con, x, num - 1);
1481 /* Vertical Line Position Relative */
1482 num = vte->csi_argv[0];
1485 x = tsm_screen_get_cursor_x(vte->con);
1486 y = tsm_screen_get_cursor_y(vte->con);
1487 tsm_screen_move_to(vte->con, x, y + num);
1491 /* position cursor */
1492 x = vte->csi_argv[0];
1495 y = vte->csi_argv[1];
1498 tsm_screen_move_to(vte->con, y - 1, x - 1);
1501 /* Cursor Character Absolute */
1502 num = vte->csi_argv[0];
1505 y = tsm_screen_get_cursor_y(vte->con);
1506 tsm_screen_move_to(vte->con, num - 1, y);
1509 if (vte->csi_flags & CSI_WHAT)
1514 if (vte->csi_argv[0] <= 0)
1515 tsm_screen_erase_cursor_to_screen(vte->con,
1517 else if (vte->csi_argv[0] == 1)
1518 tsm_screen_erase_screen_to_cursor(vte->con,
1520 else if (vte->csi_argv[0] == 2)
1521 tsm_screen_erase_screen(vte->con, protect);
1523 llog_debug(vte, "unknown parameter to CSI-J: %d",
1527 if (vte->csi_flags & CSI_WHAT)
1532 if (vte->csi_argv[0] <= 0)
1533 tsm_screen_erase_cursor_to_end(vte->con, protect);
1534 else if (vte->csi_argv[0] == 1)
1535 tsm_screen_erase_home_to_cursor(vte->con, protect);
1536 else if (vte->csi_argv[0] == 2)
1537 tsm_screen_erase_current_line(vte->con, protect);
1539 llog_debug(vte, "unknown parameter to CSI-K: %d",
1543 /* erase characters */
1544 num = vte->csi_argv[0];
1547 tsm_screen_erase_chars(vte->con, num);
1553 if (vte->csi_flags & CSI_GT) {
1554 /* xterm: select X11 visual cursor mode */
1555 csi_soft_reset(vte);
1556 } else if (vte->csi_flags & CSI_BANG) {
1557 /* DECSTR: Soft Reset */
1558 csi_soft_reset(vte);
1559 } else if (vte->csi_flags & CSI_CASH) {
1560 /* DECRQM: Request DEC Private Mode */
1561 /* If CSI_WHAT is set, then enable,
1562 * otherwise disable */
1563 csi_soft_reset(vte);
1565 /* DECSCL: Compatibility Level */
1566 /* Sometimes CSI_DQUOTE is set here, too */
1567 csi_compat_mode(vte);
1570 case 'h': /* SM: Set Mode */
1571 csi_mode(vte, true);
1573 case 'l': /* RM: Reset Mode */
1574 csi_mode(vte, false);
1576 case 'r': /* DECSTBM */
1577 /* set margin size */
1578 upper = vte->csi_argv[0];
1581 lower = vte->csi_argv[1];
1584 tsm_screen_set_margins(vte->con, upper, lower);
1587 /* device attributes */
1592 num = vte->csi_argv[0];
1595 tsm_screen_insert_lines(vte->con, num);
1599 num = vte->csi_argv[0];
1602 tsm_screen_delete_lines(vte->con, num);
1605 /* tabulation clear */
1606 num = vte->csi_argv[0];
1608 tsm_screen_reset_tabstop(vte->con);
1610 tsm_screen_reset_all_tabstops(vte->con);
1612 llog_debug(vte, "invalid parameter %d to TBC CSI", num);
1615 /* insert characters */
1616 num = vte->csi_argv[0];
1619 tsm_screen_insert_chars(vte->con, num);
1622 /* delete characters */
1623 num = vte->csi_argv[0];
1626 tsm_screen_delete_chars(vte->con, num);
1629 /* cursor horizontal backwards tab */
1630 num = vte->csi_argv[0];
1633 tsm_screen_tab_left(vte->con, num);
1636 /* cursor horizontal forward tab */
1637 num = vte->csi_argv[0];
1640 tsm_screen_tab_right(vte->con, num);
1643 /* device status reports */
1648 num = vte->csi_argv[0];
1651 tsm_screen_scroll_up(vte->con, num);
1655 num = vte->csi_argv[0];
1658 tsm_screen_scroll_down(vte->con, num);
1661 llog_debug(vte, "unhandled CSI sequence %c", data);
1665 /* map a character according to current GL and GR maps */
1666 static uint32_t vte_map(struct tsm_vte *vte, uint32_t val)
1668 /* 32, 127, 160 and 255 map to identity like all values >255 */
1672 val = (*vte->glt)[val - 32];
1675 val = (*vte->gl)[val - 32];
1680 val = (*vte->grt)[val - 160];
1683 val = (*vte->gr)[val - 160];
1691 /* perform parser action */
1692 static void do_action(struct tsm_vte *vte, uint32_t data, int action)
1701 /* ignore character */
1704 sym = tsm_symbol_make(vte_map(vte, data));
1705 write_console(vte, sym);
1707 case ACTION_EXECUTE:
1708 do_execute(vte, data);
1713 case ACTION_COLLECT:
1714 do_collect(vte, data);
1717 do_param(vte, data);
1719 case ACTION_ESC_DISPATCH:
1722 case ACTION_CSI_DISPATCH:
1725 case ACTION_DCS_START:
1727 case ACTION_DCS_COLLECT:
1729 case ACTION_DCS_END:
1731 case ACTION_OSC_START:
1733 case ACTION_OSC_COLLECT:
1735 case ACTION_OSC_END:
1738 llog_warn(vte, "invalid action %d", action);
1742 /* entry actions to be performed when entering the selected state */
1743 static const int entry_action[] = {
1744 [STATE_CSI_ENTRY] = ACTION_CLEAR,
1745 [STATE_DCS_ENTRY] = ACTION_CLEAR,
1746 [STATE_DCS_PASS] = ACTION_DCS_START,
1747 [STATE_ESC] = ACTION_CLEAR,
1748 [STATE_OSC_STRING] = ACTION_OSC_START,
1749 [STATE_NUM] = ACTION_NONE,
1752 /* exit actions to be performed when leaving the selected state */
1753 static const int exit_action[] = {
1754 [STATE_DCS_PASS] = ACTION_DCS_END,
1755 [STATE_OSC_STRING] = ACTION_OSC_END,
1756 [STATE_NUM] = ACTION_NONE,
1759 /* perform state transision and dispatch related actions */
1760 static void do_trans(struct tsm_vte *vte, uint32_t data, int state, int act)
1762 if (state != STATE_NONE) {
1763 /* A state transition occurs. Perform exit-action,
1764 * transition-action and entry-action. Even when performing a
1765 * transition to the same state as the current state we do this.
1766 * Use STATE_NONE if this is not the desired behavior.
1768 do_action(vte, data, exit_action[vte->state]);
1769 do_action(vte, data, act);
1770 do_action(vte, data, entry_action[state]);
1773 do_action(vte, data, act);
1778 * Escape sequence parser
1779 * This parses the new input character \data. It performs state transition and
1780 * calls the right callbacks for each action.
1782 static void parse_data(struct tsm_vte *vte, uint32_t raw)
1784 /* events that may occur in any state */
1793 do_trans(vte, raw, STATE_GROUND, ACTION_EXECUTE);
1796 do_trans(vte, raw, STATE_ESC, ACTION_NONE);
1801 do_trans(vte, raw, STATE_ST_IGNORE, ACTION_NONE);
1804 do_trans(vte, raw, STATE_DCS_ENTRY, ACTION_NONE);
1807 do_trans(vte, raw, STATE_OSC_STRING, ACTION_NONE);
1810 do_trans(vte, raw, STATE_CSI_ENTRY, ACTION_NONE);
1814 /* events that depend on the current state */
1815 switch (vte->state) {
1824 do_trans(vte, raw, STATE_NONE, ACTION_EXECUTE);
1827 do_trans(vte, raw, STATE_NONE, ACTION_PRINT);
1830 do_trans(vte, raw, STATE_NONE, ACTION_PRINT);
1837 do_trans(vte, raw, STATE_NONE, ACTION_EXECUTE);
1840 do_trans(vte, raw, STATE_NONE, ACTION_IGNORE);
1843 do_trans(vte, raw, STATE_ESC_INT, ACTION_COLLECT);
1851 do_trans(vte, raw, STATE_GROUND, ACTION_ESC_DISPATCH);
1854 do_trans(vte, raw, STATE_CSI_ENTRY, ACTION_NONE);
1857 do_trans(vte, raw, STATE_OSC_STRING, ACTION_NONE);
1860 do_trans(vte, raw, STATE_DCS_ENTRY, ACTION_NONE);
1865 do_trans(vte, raw, STATE_ST_IGNORE, ACTION_NONE);
1868 do_trans(vte, raw, STATE_ESC_INT, ACTION_COLLECT);
1875 do_trans(vte, raw, STATE_NONE, ACTION_EXECUTE);
1878 do_trans(vte, raw, STATE_NONE, ACTION_COLLECT);
1881 do_trans(vte, raw, STATE_NONE, ACTION_IGNORE);
1884 do_trans(vte, raw, STATE_GROUND, ACTION_ESC_DISPATCH);
1887 do_trans(vte, raw, STATE_NONE, ACTION_COLLECT);
1889 case STATE_CSI_ENTRY:
1894 do_trans(vte, raw, STATE_NONE, ACTION_EXECUTE);
1897 do_trans(vte, raw, STATE_NONE, ACTION_IGNORE);
1900 do_trans(vte, raw, STATE_CSI_INT, ACTION_COLLECT);
1903 do_trans(vte, raw, STATE_CSI_IGNORE, ACTION_NONE);
1907 do_trans(vte, raw, STATE_CSI_PARAM, ACTION_PARAM);
1910 do_trans(vte, raw, STATE_CSI_PARAM, ACTION_COLLECT);
1913 do_trans(vte, raw, STATE_GROUND, ACTION_CSI_DISPATCH);
1916 do_trans(vte, raw, STATE_CSI_IGNORE, ACTION_NONE);
1918 case STATE_CSI_PARAM:
1923 do_trans(vte, raw, STATE_NONE, ACTION_EXECUTE);
1927 do_trans(vte, raw, STATE_NONE, ACTION_PARAM);
1930 do_trans(vte, raw, STATE_NONE, ACTION_IGNORE);
1934 do_trans(vte, raw, STATE_CSI_IGNORE, ACTION_NONE);
1937 do_trans(vte, raw, STATE_CSI_INT, ACTION_COLLECT);
1940 do_trans(vte, raw, STATE_GROUND, ACTION_CSI_DISPATCH);
1943 do_trans(vte, raw, STATE_CSI_IGNORE, ACTION_NONE);
1950 do_trans(vte, raw, STATE_NONE, ACTION_EXECUTE);
1953 do_trans(vte, raw, STATE_NONE, ACTION_COLLECT);
1956 do_trans(vte, raw, STATE_NONE, ACTION_IGNORE);
1959 do_trans(vte, raw, STATE_CSI_IGNORE, ACTION_NONE);
1962 do_trans(vte, raw, STATE_GROUND, ACTION_CSI_DISPATCH);
1965 do_trans(vte, raw, STATE_CSI_IGNORE, ACTION_NONE);
1967 case STATE_CSI_IGNORE:
1972 do_trans(vte, raw, STATE_NONE, ACTION_EXECUTE);
1976 do_trans(vte, raw, STATE_NONE, ACTION_IGNORE);
1979 do_trans(vte, raw, STATE_GROUND, ACTION_NONE);
1982 do_trans(vte, raw, STATE_NONE, ACTION_IGNORE);
1984 case STATE_DCS_ENTRY:
1990 do_trans(vte, raw, STATE_NONE, ACTION_IGNORE);
1993 do_trans(vte, raw, STATE_DCS_IGNORE, ACTION_NONE);
1996 do_trans(vte, raw, STATE_DCS_INT, ACTION_COLLECT);
2000 do_trans(vte, raw, STATE_DCS_PARAM, ACTION_PARAM);
2003 do_trans(vte, raw, STATE_DCS_PARAM, ACTION_COLLECT);
2006 do_trans(vte, raw, STATE_DCS_PASS, ACTION_NONE);
2009 do_trans(vte, raw, STATE_DCS_PASS, ACTION_NONE);
2011 case STATE_DCS_PARAM:
2017 do_trans(vte, raw, STATE_NONE, ACTION_IGNORE);
2021 do_trans(vte, raw, STATE_NONE, ACTION_PARAM);
2025 do_trans(vte, raw, STATE_DCS_IGNORE, ACTION_NONE);
2028 do_trans(vte, raw, STATE_DCS_INT, ACTION_COLLECT);
2031 do_trans(vte, raw, STATE_DCS_PASS, ACTION_NONE);
2034 do_trans(vte, raw, STATE_DCS_PASS, ACTION_NONE);
2042 do_trans(vte, raw, STATE_NONE, ACTION_IGNORE);
2045 do_trans(vte, raw, STATE_NONE, ACTION_COLLECT);
2048 do_trans(vte, raw, STATE_DCS_IGNORE, ACTION_NONE);
2051 do_trans(vte, raw, STATE_DCS_PASS, ACTION_NONE);
2054 do_trans(vte, raw, STATE_DCS_PASS, ACTION_NONE);
2056 case STATE_DCS_PASS:
2062 do_trans(vte, raw, STATE_NONE, ACTION_DCS_COLLECT);
2065 do_trans(vte, raw, STATE_NONE, ACTION_IGNORE);
2068 do_trans(vte, raw, STATE_GROUND, ACTION_NONE);
2071 do_trans(vte, raw, STATE_NONE, ACTION_DCS_COLLECT);
2073 case STATE_DCS_IGNORE:
2079 do_trans(vte, raw, STATE_NONE, ACTION_IGNORE);
2082 do_trans(vte, raw, STATE_GROUND, ACTION_NONE);
2085 do_trans(vte, raw, STATE_NONE, ACTION_IGNORE);
2087 case STATE_OSC_STRING:
2093 do_trans(vte, raw, STATE_NONE, ACTION_IGNORE);
2096 do_trans(vte, raw, STATE_NONE, ACTION_OSC_COLLECT);
2100 do_trans(vte, raw, STATE_GROUND, ACTION_NONE);
2103 do_trans(vte, raw, STATE_NONE, ACTION_OSC_COLLECT);
2105 case STATE_ST_IGNORE:
2111 do_trans(vte, raw, STATE_NONE, ACTION_IGNORE);
2114 do_trans(vte, raw, STATE_GROUND, ACTION_NONE);
2117 do_trans(vte, raw, STATE_NONE, ACTION_IGNORE);
2121 llog_warn(vte, "unhandled input %u in state %d", raw, vte->state);
2124 void tsm_vte_input(struct tsm_vte *vte, const char *u8, size_t len)
2130 if (!vte || !vte->con)
2134 for (i = 0; i < len; ++i) {
2135 if (vte->flags & FLAG_7BIT_MODE) {
2137 llog_debug(vte, "receiving 8bit character U+%d from pty while in 7bit mode",
2139 parse_data(vte, u8[i] & 0x7f);
2140 } else if (vte->flags & FLAG_8BIT_MODE) {
2141 parse_data(vte, u8[i]);
2143 state = tsm_utf8_mach_feed(vte->mach, u8[i]);
2144 if (state == TSM_UTF8_ACCEPT ||
2145 state == TSM_UTF8_REJECT) {
2146 ucs4 = tsm_utf8_mach_get(vte->mach);
2147 parse_data(vte, ucs4);
2154 bool tsm_vte_handle_keyboard(struct tsm_vte *vte, uint32_t keysym,
2155 unsigned int mods, uint32_t unicode)
2160 /* MOD1 (mostly labeled 'Alt') prepends an escape character to every
2161 * input that is sent by a key.
2162 * TODO: Transform this huge handler into a lookup table to save a lot
2163 * of code and make such modifiers easier to implement.
2164 * Also check whether altSendsEscape should be the default (xterm
2165 * disables this by default, why?) and whether we should implement the
2166 * fallback shifting that xterm does. */
2167 if (mods & TSM_MOD1_MASK)
2168 vte->flags |= FLAG_PREPEND_ESCAPE;
2170 if (mods & TSM_CONTROL_MASK) {
2174 vte_write(vte, "\x00", 1);
2178 vte_write(vte, "\x01", 1);
2182 vte_write(vte, "\x02", 1);
2186 vte_write(vte, "\x03", 1);
2190 vte_write(vte, "\x04", 1);
2194 vte_write(vte, "\x05", 1);
2198 vte_write(vte, "\x06", 1);
2202 vte_write(vte, "\x07", 1);
2206 vte_write(vte, "\x08", 1);
2210 vte_write(vte, "\x09", 1);
2214 vte_write(vte, "\x0a", 1);
2218 vte_write(vte, "\x0b", 1);
2222 vte_write(vte, "\x0c", 1);
2226 vte_write(vte, "\x0d", 1);
2230 vte_write(vte, "\x0e", 1);
2234 vte_write(vte, "\x0f", 1);
2238 vte_write(vte, "\x10", 1);
2242 vte_write(vte, "\x11", 1);
2246 vte_write(vte, "\x12", 1);
2250 vte_write(vte, "\x13", 1);
2254 vte_write(vte, "\x14", 1);
2258 vte_write(vte, "\x15", 1);
2262 vte_write(vte, "\x16", 1);
2266 vte_write(vte, "\x17", 1);
2270 vte_write(vte, "\x18", 1);
2274 vte_write(vte, "\x19", 1);
2278 vte_write(vte, "\x1a", 1);
2281 case XKB_KEY_bracketleft:
2282 case XKB_KEY_braceleft:
2283 vte_write(vte, "\x1b", 1);
2286 case XKB_KEY_backslash:
2288 vte_write(vte, "\x1c", 1);
2291 case XKB_KEY_bracketright:
2292 case XKB_KEY_braceright:
2293 vte_write(vte, "\x1d", 1);
2297 case XKB_KEY_asciitilde:
2298 vte_write(vte, "\x1e", 1);
2302 case XKB_KEY_question:
2303 vte_write(vte, "\x1f", 1);
2306 vte_write(vte, "\x7f", 1);
2312 case XKB_KEY_BackSpace:
2313 vte_write(vte, "\x08", 1);
2316 case XKB_KEY_KP_Tab:
2317 vte_write(vte, "\x09", 1);
2319 case XKB_KEY_Linefeed:
2320 vte_write(vte, "\x0a", 1);
2323 vte_write(vte, "\x0b", 1);
2326 vte_write(vte, "\x13", 1);
2328 case XKB_KEY_Scroll_Lock:
2329 /* TODO: do we need scroll lock impl.? */
2330 vte_write(vte, "\x14", 1);
2332 case XKB_KEY_Sys_Req:
2333 vte_write(vte, "\x15", 1);
2335 case XKB_KEY_Escape:
2336 vte_write(vte, "\x1b", 1);
2338 case XKB_KEY_KP_Enter:
2339 if (vte->flags & FLAG_KEYPAD_APPLICATION_MODE) {
2340 vte_write(vte, "\eOM", 3);
2344 case XKB_KEY_Return:
2345 if (vte->flags & FLAG_LINE_FEED_NEW_LINE_MODE)
2346 vte_write(vte, "\x0d\x0a", 2);
2348 vte_write(vte, "\x0d", 1);
2351 vte_write(vte, "\e[1~", 4);
2353 case XKB_KEY_Insert:
2354 vte_write(vte, "\e[2~", 4);
2356 case XKB_KEY_Delete:
2357 vte_write(vte, "\e[3~", 4);
2359 case XKB_KEY_Select:
2360 vte_write(vte, "\e[4~", 4);
2362 case XKB_KEY_Page_Up:
2363 vte_write(vte, "\e[5~", 4);
2365 case XKB_KEY_Page_Down:
2366 vte_write(vte, "\e[6~", 4);
2369 if (vte->flags & FLAG_CURSOR_KEY_MODE)
2370 vte_write(vte, "\eOA", 3);
2372 vte_write(vte, "\e[A", 3);
2375 if (vte->flags & FLAG_CURSOR_KEY_MODE)
2376 vte_write(vte, "\eOB", 3);
2378 vte_write(vte, "\e[B", 3);
2381 if (vte->flags & FLAG_CURSOR_KEY_MODE)
2382 vte_write(vte, "\eOC", 3);
2384 vte_write(vte, "\e[C", 3);
2387 if (vte->flags & FLAG_CURSOR_KEY_MODE)
2388 vte_write(vte, "\eOD", 3);
2390 vte_write(vte, "\e[D", 3);
2392 case XKB_KEY_KP_Insert:
2394 if (vte->flags & FLAG_KEYPAD_APPLICATION_MODE)
2395 vte_write(vte, "\eOp", 3);
2397 vte_write(vte, "0", 1);
2399 case XKB_KEY_KP_End:
2401 if (vte->flags & FLAG_KEYPAD_APPLICATION_MODE)
2402 vte_write(vte, "\eOq", 3);
2404 vte_write(vte, "1", 1);
2406 case XKB_KEY_KP_Down:
2408 if (vte->flags & FLAG_KEYPAD_APPLICATION_MODE)
2409 vte_write(vte, "\eOr", 3);
2411 vte_write(vte, "2", 1);
2413 case XKB_KEY_KP_Page_Down:
2415 if (vte->flags & FLAG_KEYPAD_APPLICATION_MODE)
2416 vte_write(vte, "\eOs", 3);
2418 vte_write(vte, "3", 1);
2420 case XKB_KEY_KP_Left:
2422 if (vte->flags & FLAG_KEYPAD_APPLICATION_MODE)
2423 vte_write(vte, "\eOt", 3);
2425 vte_write(vte, "4", 1);
2427 case XKB_KEY_KP_Begin:
2429 if (vte->flags & FLAG_KEYPAD_APPLICATION_MODE)
2430 vte_write(vte, "\eOu", 3);
2432 vte_write(vte, "5", 1);
2434 case XKB_KEY_KP_Right:
2436 if (vte->flags & FLAG_KEYPAD_APPLICATION_MODE)
2437 vte_write(vte, "\eOv", 3);
2439 vte_write(vte, "6", 1);
2441 case XKB_KEY_KP_Home:
2443 if (vte->flags & FLAG_KEYPAD_APPLICATION_MODE)
2444 vte_write(vte, "\eOw", 3);
2446 vte_write(vte, "7", 1);
2450 if (vte->flags & FLAG_KEYPAD_APPLICATION_MODE)
2451 vte_write(vte, "\eOx", 3);
2453 vte_write(vte, "8", 1);
2455 case XKB_KEY_KP_Page_Up:
2457 if (vte->flags & FLAG_KEYPAD_APPLICATION_MODE)
2458 vte_write(vte, "\eOy", 3);
2460 vte_write(vte, "9", 1);
2462 case XKB_KEY_KP_Subtract:
2463 if (vte->flags & FLAG_KEYPAD_APPLICATION_MODE)
2464 vte_write(vte, "\eOm", 3);
2466 vte_write(vte, "-", 1);
2468 case XKB_KEY_KP_Separator:
2469 if (vte->flags & FLAG_KEYPAD_APPLICATION_MODE)
2470 vte_write(vte, "\eOl", 3);
2472 vte_write(vte, ",", 1);
2474 case XKB_KEY_KP_Delete:
2475 case XKB_KEY_KP_Decimal:
2476 if (vte->flags & FLAG_KEYPAD_APPLICATION_MODE)
2477 vte_write(vte, "\eOn", 3);
2479 vte_write(vte, ".", 1);
2481 case XKB_KEY_KP_Equal:
2482 case XKB_KEY_KP_Divide:
2483 if (vte->flags & FLAG_KEYPAD_APPLICATION_MODE)
2484 vte_write(vte, "\eOj", 3);
2486 vte_write(vte, "/", 1);
2488 case XKB_KEY_KP_Multiply:
2489 if (vte->flags & FLAG_KEYPAD_APPLICATION_MODE)
2490 vte_write(vte, "\eOo", 3);
2492 vte_write(vte, "*", 1);
2494 case XKB_KEY_KP_Add:
2495 if (vte->flags & FLAG_KEYPAD_APPLICATION_MODE)
2496 vte_write(vte, "\eOk", 3);
2498 vte_write(vte, "+", 1);
2501 if (vte->flags & FLAG_CURSOR_KEY_MODE)
2502 vte_write(vte, "\eOH", 3);
2504 vte_write(vte, "\e[H", 3);
2507 if (vte->flags & FLAG_CURSOR_KEY_MODE)
2508 vte_write(vte, "\eOF", 3);
2510 vte_write(vte, "\e[F", 3);
2512 case XKB_KEY_KP_Space:
2513 vte_write(vte, " ", 1);
2515 /* TODO: check what to transmit for functions keys when
2516 * shift/ctrl etc. are pressed. Every terminal behaves
2517 * differently here which is really weird.
2518 * We now map F4 to F14 if shift is pressed and so on for all
2519 * keys. However, such mappings should rather be done via
2520 * xkb-configurations and we should instead add a flags argument
2521 * to the CSIs as some of the keys here already do. */
2524 if (mods & TSM_SHIFT_MASK)
2525 vte_write(vte, "\e[23~", 5);
2527 vte_write(vte, "\eOP", 3);
2531 if (mods & TSM_SHIFT_MASK)
2532 vte_write(vte, "\e[24~", 5);
2534 vte_write(vte, "\eOQ", 3);
2538 if (mods & TSM_SHIFT_MASK)
2539 vte_write(vte, "\e[25~", 5);
2541 vte_write(vte, "\eOR", 3);
2545 if (mods & TSM_SHIFT_MASK)
2546 //vte_write(vte, "\e[1;2S", 6);
2547 vte_write(vte, "\e[26~", 5);
2549 vte_write(vte, "\eOS", 3);
2552 if (mods & TSM_SHIFT_MASK)
2553 //vte_write(vte, "\e[15;2~", 7);
2554 vte_write(vte, "\e[28~", 5);
2556 vte_write(vte, "\e[15~", 5);
2559 if (mods & TSM_SHIFT_MASK)
2560 //vte_write(vte, "\e[17;2~", 7);
2561 vte_write(vte, "\e[29~", 5);
2563 vte_write(vte, "\e[17~", 5);
2566 if (mods & TSM_SHIFT_MASK)
2567 //vte_write(vte, "\e[18;2~", 7);
2568 vte_write(vte, "\e[31~", 5);
2570 vte_write(vte, "\e[18~", 5);
2573 if (mods & TSM_SHIFT_MASK)
2574 //vte_write(vte, "\e[19;2~", 7);
2575 vte_write(vte, "\e[32~", 5);
2577 vte_write(vte, "\e[19~", 5);
2580 if (mods & TSM_SHIFT_MASK)
2581 //vte_write(vte, "\e[20;2~", 7);
2582 vte_write(vte, "\e[33~", 5);
2584 vte_write(vte, "\e[20~", 5);
2587 if (mods & TSM_SHIFT_MASK)
2588 //vte_write(vte, "\e[21;2~", 7);
2589 vte_write(vte, "\e[34~", 5);
2591 vte_write(vte, "\e[21~", 5);
2594 if (mods & TSM_SHIFT_MASK)
2595 vte_write(vte, "\e[23;2~", 7);
2597 vte_write(vte, "\e[23~", 5);
2600 if (mods & TSM_SHIFT_MASK)
2601 vte_write(vte, "\e[24;2~", 7);
2603 vte_write(vte, "\e[24~", 5);
2606 if (mods & TSM_SHIFT_MASK)
2607 vte_write(vte, "\e[25;2~", 7);
2609 vte_write(vte, "\e[25~", 5);
2612 if (mods & TSM_SHIFT_MASK)
2613 vte_write(vte, "\e[26;2~", 7);
2615 vte_write(vte, "\e[26~", 5);
2618 if (mods & TSM_SHIFT_MASK)
2619 vte_write(vte, "\e[28;2~", 7);
2621 vte_write(vte, "\e[28~", 5);
2624 if (mods & TSM_SHIFT_MASK)
2625 vte_write(vte, "\e[29;2~", 7);
2627 vte_write(vte, "\e[29~", 5);
2630 if (mods & TSM_SHIFT_MASK)
2631 vte_write(vte, "\e[31;2~", 7);
2633 vte_write(vte, "\e[31~", 5);
2636 if (mods & TSM_SHIFT_MASK)
2637 vte_write(vte, "\e[32;2~", 7);
2639 vte_write(vte, "\e[32~", 5);
2642 if (mods & TSM_SHIFT_MASK)
2643 vte_write(vte, "\e[33;2~", 7);
2645 vte_write(vte, "\e[33~", 5);
2648 if (mods & TSM_SHIFT_MASK)
2649 vte_write(vte, "\e[34;2~", 7);
2651 vte_write(vte, "\e[34~", 5);
2655 if (unicode != TSM_VTE_INVALID) {
2656 if (vte->flags & FLAG_7BIT_MODE) {
2658 if (unicode & 0x80) {
2659 llog_debug(vte, "invalid keyboard input in 7bit mode U+%x; mapping to '?'",
2663 vte_write(vte, &val, 1);
2664 } else if (vte->flags & FLAG_8BIT_MODE) {
2666 if (unicode > 0xff) {
2667 llog_debug(vte, "invalid keyboard input in 8bit mode U+%x; mapping to '?'",
2671 vte_write_raw(vte, &val, 1);
2673 len = tsm_ucs4_to_utf8(tsm_symbol_make(unicode), u8);
2674 vte_write_raw(vte, u8, len);
2679 vte->flags &= ~FLAG_PREPEND_ESCAPE;