2 * Copyright © 2008 Kristian Høgsberg
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
34 #include <sys/epoll.h>
36 #include <wayland-client.h>
40 static int option_fullscreen;
42 #define MOD_SHIFT 0x01
46 #define ATTRMASK_BOLD 0x01
47 #define ATTRMASK_UNDERLINE 0x02
48 #define ATTRMASK_BLINK 0x04
49 #define ATTRMASK_INVERSE 0x08
50 #define ATTRMASK_CONCEALED 0x10
53 #define MAX_RESPONSE 256
54 #define MAX_ESCAPE 255
57 #define MODE_SHOW_CURSOR 0x00000001
58 #define MODE_INVERSE 0x00000002
59 #define MODE_AUTOWRAP 0x00000004
60 #define MODE_AUTOREPEAT 0x00000008
61 #define MODE_LF_NEWLINE 0x00000010
62 #define MODE_IRM 0x00000020
63 #define MODE_DELETE_SENDS_DEL 0x00000040
64 #define MODE_ALT_SENDS_ESC 0x00000080
67 unsigned char byte[4];
80 struct utf8_state_machine {
81 enum utf8_state state;
87 init_state_machine(struct utf8_state_machine *machine)
89 machine->state = utf8state_start;
94 static enum utf8_state
95 utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
97 switch(machine->state) {
99 case utf8state_accept:
100 case utf8state_reject:
103 if(c == 0xC0 || c == 0xC1) {
104 /* overlong encoding, reject */
105 machine->state = utf8state_reject;
106 } else if((c & 0x80) == 0) {
107 /* single byte, accept */
108 machine->s.byte[machine->len++] = c;
109 machine->state = utf8state_accept;
110 } else if((c & 0xC0) == 0x80) {
111 /* parser out of sync, ignore byte */
112 machine->state = utf8state_start;
113 } else if((c & 0xE0) == 0xC0) {
114 /* start of two byte sequence */
115 machine->s.byte[machine->len++] = c;
116 machine->state = utf8state_expect1;
117 } else if((c & 0xF0) == 0xE0) {
118 /* start of three byte sequence */
119 machine->s.byte[machine->len++] = c;
120 machine->state = utf8state_expect2;
121 } else if((c & 0xF8) == 0xF0) {
122 /* start of four byte sequence */
123 machine->s.byte[machine->len++] = c;
124 machine->state = utf8state_expect3;
126 /* overlong encoding, reject */
127 machine->state = utf8state_reject;
130 case utf8state_expect3:
131 machine->s.byte[machine->len++] = c;
132 if((c & 0xC0) == 0x80) {
133 /* all good, continue */
134 machine->state = utf8state_expect2;
136 /* missing extra byte, reject */
137 machine->state = utf8state_reject;
140 case utf8state_expect2:
141 machine->s.byte[machine->len++] = c;
142 if((c & 0xC0) == 0x80) {
143 /* all good, continue */
144 machine->state = utf8state_expect1;
146 /* missing extra byte, reject */
147 machine->state = utf8state_reject;
150 case utf8state_expect1:
151 machine->s.byte[machine->len++] = c;
152 if((c & 0xC0) == 0x80) {
153 /* all good, accept */
154 machine->state = utf8state_accept;
156 /* missing extra byte, reject */
157 machine->state = utf8state_reject;
161 machine->state = utf8state_reject;
165 return machine->state;
169 union utf8_char match;
170 union utf8_char replace;
172 /* Set last char_sub match to NULL char */
173 typedef struct char_sub *character_set;
175 struct char_sub CS_US[] = {
178 static struct char_sub CS_UK[] = {
179 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
182 static struct char_sub CS_SPECIAL[] = {
183 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond: ♦ */
184 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell: ▒ */
185 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT: ␉ */
186 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF: ␌ */
187 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR: ␍ */
188 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF: ␊ */
189 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree: ° */
190 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus: ± */
191 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL:  */
192 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT: ␋ */
193 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB: ┘ */
194 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT: ┐ */
195 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT: ┌ */
196 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_LB: └ */
197 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS: ┼ */
198 {{{'o', 0, }}, {{0xE2, 0x8E, 0xBA, 0}}}, /* Horiz. Scan Line 1: ⎺ */
199 {{{'p', 0, }}, {{0xE2, 0x8E, 0xBB, 0}}}, /* Horiz. Scan Line 3: ⎻ */
200 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* Horiz. Scan Line 5: ─ */
201 {{{'r', 0, }}, {{0xE2, 0x8E, 0xBC, 0}}}, /* Horiz. Scan Line 7: ⎼ */
202 {{{'s', 0, }}, {{0xE2, 0x8E, 0xBD, 0}}}, /* Horiz. Scan Line 9: ⎽ */
203 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR: ├ */
204 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL: ┤ */
205 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU: ┴ */
206 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD: ┬ */
207 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V: │ */
208 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE: ≤ */
209 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE: ≥ */
210 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI: π */
211 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ: ≠ */
212 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
213 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT: ⋅ */
218 apply_char_set(character_set cs, union utf8_char *utf8)
222 while (cs[i].match.byte[0]) {
223 if ((*utf8).ch == cs[i].match.ch) {
224 *utf8 = cs[i].replace;
237 /* Set last key_sub sym to NULL */
238 typedef struct key_map *keyboard_mode;
240 static struct key_map KM_NORMAL[] = {
241 { XKB_KEY_Left, 1, '[', 'D' },
242 { XKB_KEY_Right, 1, '[', 'C' },
243 { XKB_KEY_Up, 1, '[', 'A' },
244 { XKB_KEY_Down, 1, '[', 'B' },
245 { XKB_KEY_Home, 1, '[', 'H' },
246 { XKB_KEY_End, 1, '[', 'F' },
249 static struct key_map KM_APPLICATION[] = {
250 { XKB_KEY_Left, 1, 'O', 'D' },
251 { XKB_KEY_Right, 1, 'O', 'C' },
252 { XKB_KEY_Up, 1, 'O', 'A' },
253 { XKB_KEY_Down, 1, 'O', 'B' },
254 { XKB_KEY_Home, 1, 'O', 'H' },
255 { XKB_KEY_End, 1, 'O', 'F' },
256 { XKB_KEY_KP_Enter, 1, 'O', 'M' },
257 { XKB_KEY_KP_Multiply, 1, 'O', 'j' },
258 { XKB_KEY_KP_Add, 1, 'O', 'k' },
259 { XKB_KEY_KP_Separator, 1, 'O', 'l' },
260 { XKB_KEY_KP_Subtract, 1, 'O', 'm' },
261 { XKB_KEY_KP_Divide, 1, 'O', 'o' },
266 function_key_response(char escape, int num, uint32_t modifiers,
267 char code, char *response)
272 if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
273 if (modifiers & MOD_ALT_MASK) mod_num |= 2;
274 if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
277 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
278 num, mod_num + 1, code);
279 else if (code != '~')
280 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
283 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
286 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
290 /* returns the number of bytes written into response,
291 * which must have room for MAX_RESPONSE bytes */
293 apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
299 while (mode[i].sym) {
301 if (sym == map.sym) {
302 len = function_key_response(map.escape, map.num,
312 struct terminal_color { double r, g, b, a; };
314 unsigned char fg, bg;
315 char a; /* attributes format:
318 char s; /* in selection */
320 struct color_scheme {
321 struct terminal_color palette[16];
323 struct attr default_attr;
327 attr_init(struct attr *data_attr, struct attr attr, int n)
330 for (i = 0; i < n; i++) {
336 escape_state_normal = 0,
341 escape_state_inner_escape,
346 #define ESC_FLAG_WHAT 0x01
347 #define ESC_FLAG_GT 0x02
348 #define ESC_FLAG_BANG 0x04
349 #define ESC_FLAG_CASH 0x08
350 #define ESC_FLAG_SQUOTE 0x10
351 #define ESC_FLAG_DQUOTE 0x20
352 #define ESC_FLAG_SPACE 0x40
355 struct window *window;
356 struct widget *widget;
357 struct display *display;
358 union utf8_char *data;
361 struct attr *data_attr;
362 struct attr curr_attr;
365 char saved_origin_mode;
366 struct attr saved_attr;
367 union utf8_char last_char;
368 int margin_top, margin_bottom;
369 character_set cs, g0, g1;
370 character_set saved_cs, saved_g0, saved_g1;
371 keyboard_mode key_mode;
372 int data_pitch, attr_pitch; /* The width in bytes of a line */
373 int width, height, start, row, column;
374 int saved_row, saved_column;
375 int send_cursor_position;
378 char escape[MAX_ESCAPE+1];
380 enum escape_state state;
381 enum escape_state outer_state;
383 struct utf8_state_machine state_machine;
387 struct color_scheme *color_scheme;
388 struct terminal_color color_table[256];
389 cairo_font_extents_t extents;
390 cairo_scaled_font_t *font_normal, *font_bold;
392 struct wl_data_source *selection;
394 int selection_start_x, selection_start_y;
395 int selection_end_x, selection_end_y;
398 /* Create default tab stops, every 8 characters */
400 terminal_init_tabs(struct terminal *terminal)
404 while (i < terminal->width) {
406 terminal->tab_ruler[i] = 1;
408 terminal->tab_ruler[i] = 0;
414 terminal_init(struct terminal *terminal)
416 terminal->curr_attr = terminal->color_scheme->default_attr;
417 terminal->origin_mode = 0;
418 terminal->mode = MODE_SHOW_CURSOR |
424 terminal->column = 0;
426 terminal->g0 = CS_US;
427 terminal->g1 = CS_US;
428 terminal->cs = terminal->g0;
429 terminal->key_mode = KM_NORMAL;
431 terminal->saved_g0 = terminal->g0;
432 terminal->saved_g1 = terminal->g1;
433 terminal->saved_cs = terminal->cs;
435 terminal->saved_attr = terminal->curr_attr;
436 terminal->saved_origin_mode = terminal->origin_mode;
437 terminal->saved_row = terminal->row;
438 terminal->saved_column = terminal->column;
440 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
444 init_color_table(struct terminal *terminal)
447 struct terminal_color *color_table = terminal->color_table;
449 for (c = 0; c < 256; c ++) {
451 color_table[c] = terminal->color_scheme->palette[c];
452 } else if (c < 232) {
454 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
455 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
456 color_table[c].r = ((double)(r % 6) / 6.0);
457 color_table[c].a = 1.0;
459 r = (c - 232) * 10 + 8;
460 color_table[c].r = ((double) r) / 256.0;
461 color_table[c].g = color_table[c].r;
462 color_table[c].b = color_table[c].r;
463 color_table[c].a = 1.0;
468 static union utf8_char *
469 terminal_get_row(struct terminal *terminal, int row)
473 index = (row + terminal->start) % terminal->height;
475 return &terminal->data[index * terminal->width];
479 terminal_get_attr_row(struct terminal *terminal, int row)
483 index = (row + terminal->start) % terminal->height;
485 return &terminal->data_attr[index * terminal->width];
494 terminal_compare_position(struct terminal *terminal,
495 int x, int y, int32_t ref_row, int32_t ref_col)
497 struct rectangle allocation;
498 int top_margin, side_margin, col, row, ref_x;
500 widget_get_allocation(terminal->widget, &allocation);
501 side_margin = allocation.x + (allocation.width - terminal->width * terminal->extents.max_x_advance) / 2;
502 top_margin = allocation.y + (allocation.height - terminal->height * terminal->extents.height) / 2;
504 col = (x - side_margin) / terminal->extents.max_x_advance;
505 row = (y - top_margin) / terminal->extents.height;
507 ref_x = side_margin + ref_col * terminal->extents.max_x_advance +
508 terminal->extents.max_x_advance / 2;
512 if (row == ref_row) {
515 if (col == ref_col && x < ref_x)
523 terminal_decode_attr(struct terminal *terminal, int row, int col,
524 union decoded_attr *decoded)
527 int foreground, background, tmp;
528 int start_cmp, end_cmp;
531 terminal_compare_position(terminal,
532 terminal->selection_start_x,
533 terminal->selection_start_y,
536 terminal_compare_position(terminal,
537 terminal->selection_end_x,
538 terminal->selection_end_y,
541 if (start_cmp < 0 && end_cmp > 0)
543 else if (end_cmp < 0 && start_cmp > 0)
546 /* get the attributes for this character cell */
547 attr = terminal_get_attr_row(terminal, row)[col];
548 if ((attr.a & ATTRMASK_INVERSE) ||
550 ((terminal->mode & MODE_SHOW_CURSOR) &&
551 terminal->focused && terminal->row == row &&
552 terminal->column == col)) {
553 foreground = attr.bg;
554 background = attr.fg;
555 if (attr.a & ATTRMASK_BOLD) {
556 if (foreground <= 16) foreground |= 0x08;
557 if (background <= 16) background &= 0x07;
560 foreground = attr.fg;
561 background = attr.bg;
564 if (terminal->mode & MODE_INVERSE) {
566 foreground = background;
568 if (attr.a & ATTRMASK_BOLD) {
569 if (foreground <= 16) foreground |= 0x08;
570 if (background <= 16) background &= 0x07;
574 decoded->attr.fg = foreground;
575 decoded->attr.bg = background;
576 decoded->attr.a = attr.a;
581 terminal_scroll_buffer(struct terminal *terminal, int d)
585 d = d % (terminal->height + 1);
586 terminal->start = (terminal->start + d) % terminal->height;
587 if (terminal->start < 0) terminal->start = terminal->height + terminal->start;
590 for(i = 0; i < d; i++) {
591 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
592 attr_init(terminal_get_attr_row(terminal, i),
593 terminal->curr_attr, terminal->width);
596 for(i = terminal->height - d; i < terminal->height; i++) {
597 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
598 attr_init(terminal_get_attr_row(terminal, i),
599 terminal->curr_attr, terminal->width);
605 terminal_scroll_window(struct terminal *terminal, int d)
609 int from_row, to_row;
611 // scrolling range is inclusive
612 window_height = terminal->margin_bottom - terminal->margin_top + 1;
613 d = d % (window_height + 1);
616 to_row = terminal->margin_bottom;
617 from_row = terminal->margin_bottom - d;
619 for (i = 0; i < (window_height - d); i++) {
620 memcpy(terminal_get_row(terminal, to_row - i),
621 terminal_get_row(terminal, from_row - i),
622 terminal->data_pitch);
623 memcpy(terminal_get_attr_row(terminal, to_row - i),
624 terminal_get_attr_row(terminal, from_row - i),
625 terminal->attr_pitch);
627 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
628 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
629 attr_init(terminal_get_attr_row(terminal, i),
630 terminal->curr_attr, terminal->width);
633 to_row = terminal->margin_top;
634 from_row = terminal->margin_top + d;
636 for (i = 0; i < (window_height - d); i++) {
637 memcpy(terminal_get_row(terminal, to_row + i),
638 terminal_get_row(terminal, from_row + i),
639 terminal->data_pitch);
640 memcpy(terminal_get_attr_row(terminal, to_row + i),
641 terminal_get_attr_row(terminal, from_row + i),
642 terminal->attr_pitch);
644 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
645 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
646 attr_init(terminal_get_attr_row(terminal, i),
647 terminal->curr_attr, terminal->width);
653 terminal_scroll(struct terminal *terminal, int d)
655 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
656 terminal_scroll_buffer(terminal, d);
658 terminal_scroll_window(terminal, d);
662 terminal_shift_line(struct terminal *terminal, int d)
664 union utf8_char *row;
665 struct attr *attr_row;
667 row = terminal_get_row(terminal, terminal->row);
668 attr_row = terminal_get_attr_row(terminal, terminal->row);
670 if ((terminal->width + d) <= terminal->column)
671 d = terminal->column + 1 - terminal->width;
672 if ((terminal->column + d) >= terminal->width)
673 d = terminal->width - terminal->column - 1;
677 memmove(&row[terminal->column],
678 &row[terminal->column + d],
679 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
680 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
681 (terminal->width - terminal->column - d) * sizeof(struct attr));
682 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
683 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
685 memmove(&row[terminal->column + d], &row[terminal->column],
686 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
687 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
688 (terminal->width - terminal->column - d) * sizeof(struct attr));
689 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
690 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
695 terminal_resize_cells(struct terminal *terminal, int width, int height)
698 union utf8_char *data;
699 struct attr *data_attr;
701 int data_pitch, attr_pitch;
702 int i, l, total_rows;
703 struct rectangle allocation;
710 if (terminal->width == width && terminal->height == height)
713 data_pitch = width * sizeof(union utf8_char);
714 size = data_pitch * height;
716 attr_pitch = width * sizeof(struct attr);
717 data_attr = malloc(attr_pitch * height);
718 tab_ruler = malloc(width);
719 memset(data, 0, size);
720 memset(tab_ruler, 0, width);
721 attr_init(data_attr, terminal->curr_attr, width * height);
722 if (terminal->data && terminal->data_attr) {
723 if (width > terminal->width)
728 if (terminal->height > height) {
731 total_rows = terminal->height;
734 for (i = 0; i < total_rows; i++) {
735 memcpy(&data[width * i],
736 terminal_get_row(terminal, i),
737 l * sizeof(union utf8_char));
738 memcpy(&data_attr[width * i],
739 terminal_get_attr_row(terminal, i),
740 l * sizeof(struct attr));
743 free(terminal->data);
744 free(terminal->data_attr);
745 free(terminal->tab_ruler);
748 terminal->data_pitch = data_pitch;
749 terminal->attr_pitch = attr_pitch;
750 terminal->margin_bottom =
751 height - (terminal->height - terminal->margin_bottom);
752 terminal->width = width;
753 terminal->height = height;
754 terminal->data = data;
755 terminal->data_attr = data_attr;
756 terminal->tab_ruler = tab_ruler;
757 terminal_init_tabs(terminal);
759 /* Update the window size */
760 ws.ws_row = terminal->height;
761 ws.ws_col = terminal->width;
762 widget_get_allocation(terminal->widget, &allocation);
763 ws.ws_xpixel = allocation.width;
764 ws.ws_ypixel = allocation.height;
765 ioctl(terminal->master, TIOCSWINSZ, &ws);
769 resize_handler(struct widget *widget,
770 int32_t width, int32_t height, void *data)
772 struct terminal *terminal = data;
773 int32_t columns, rows, m;
781 m = 2 * terminal->margin;
782 columns = (width - m) / (int32_t) terminal->extents.max_x_advance;
783 rows = (height - m) / (int32_t) terminal->extents.height;
785 if (!terminal->fullscreen) {
786 width = columns * terminal->extents.max_x_advance + m;
787 height = rows * terminal->extents.height + m;
788 widget_set_size(terminal->widget, width, height);
791 terminal_resize_cells(terminal, columns, rows);
795 terminal_resize(struct terminal *terminal, int columns, int rows)
797 int32_t width, height, m;
799 if (terminal->fullscreen)
802 m = 2 * terminal->margin;
803 width = columns * terminal->extents.max_x_advance + m;
804 height = rows * terminal->extents.height + m;
805 widget_schedule_resize(terminal->widget, width, height);
808 struct color_scheme DEFAULT_COLORS = {
810 {0, 0, 0, 1}, /* black */
811 {0.66, 0, 0, 1}, /* red */
812 {0 , 0.66, 0, 1}, /* green */
813 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
814 {0 , 0 , 0.66, 1}, /* blue */
815 {0.66, 0 , 0.66, 1}, /* magenta */
816 {0, 0.66, 0.66, 1}, /* cyan */
817 {0.66, 0.66, 0.66, 1}, /* light grey */
818 {0.22, 0.33, 0.33, 1}, /* dark grey */
819 {1, 0.33, 0.33, 1}, /* high red */
820 {0.33, 1, 0.33, 1}, /* high green */
821 {1, 1, 0.33, 1}, /* high yellow */
822 {0.33, 0.33, 1, 1}, /* high blue */
823 {1, 0.33, 1, 1}, /* high magenta */
824 {0.33, 1, 1, 1}, /* high cyan */
825 {1, 1, 1, 1} /* white */
827 0, /* black border */
828 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
832 terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
834 cairo_set_source_rgba(cr,
835 terminal->color_table[index].r,
836 terminal->color_table[index].g,
837 terminal->color_table[index].b,
838 terminal->color_table[index].a);
842 terminal_send_selection(struct terminal *terminal, int fd)
845 union utf8_char *p_row;
846 union decoded_attr attr;
850 fp = fdopen(fd, "w");
851 for (row = 0; row < terminal->height; row++) {
852 p_row = terminal_get_row(terminal, row);
853 for (col = 0; col < terminal->width; col++) {
854 /* get the attributes for this character cell */
855 terminal_decode_attr(terminal, row, col, &attr);
858 len = strnlen((char *) p_row[col].byte, 4);
859 fwrite(p_row[col].byte, 1, len, fp);
866 struct terminal *terminal;
869 union decoded_attr attr;
870 cairo_glyph_t glyphs[256], *g;
874 glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
876 run->terminal = terminal;
878 run->g = run->glyphs;
884 glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
886 cairo_scaled_font_t *font;
888 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
889 (attr.key != run->attr.key)) {
890 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
891 font = run->terminal->font_bold;
893 font = run->terminal->font_normal;
894 cairo_set_scaled_font(run->cr, font);
895 terminal_set_color(run->terminal, run->cr,
898 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
899 cairo_show_glyphs (run->cr, run->glyphs, run->count);
900 run->g = run->glyphs;
907 glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
910 cairo_scaled_font_t *font;
912 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
914 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
915 font = run->terminal->font_bold;
917 font = run->terminal->font_normal;
919 cairo_move_to(run->cr, x, y);
920 cairo_scaled_font_text_to_glyphs (font, x, y,
922 &run->g, &num_glyphs,
924 run->g += num_glyphs;
925 run->count += num_glyphs;
930 redraw_handler(struct widget *widget, void *data)
932 struct terminal *terminal = data;
933 struct rectangle allocation;
935 int top_margin, side_margin;
936 int row, col, cursor_x, cursor_y;
937 union utf8_char *p_row;
938 union decoded_attr attr;
940 cairo_surface_t *surface;
942 struct glyph_run run;
943 cairo_font_extents_t extents;
945 surface = window_get_surface(terminal->window);
946 widget_get_allocation(terminal->widget, &allocation);
947 cr = cairo_create(surface);
948 cairo_rectangle(cr, allocation.x, allocation.y,
949 allocation.width, allocation.height);
951 cairo_push_group(cr);
953 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
954 terminal_set_color(terminal, cr, terminal->color_scheme->border);
957 cairo_set_scaled_font(cr, terminal->font_normal);
959 extents = terminal->extents;
960 side_margin = (allocation.width - terminal->width * extents.max_x_advance) / 2;
961 top_margin = (allocation.height - terminal->height * extents.height) / 2;
963 cairo_set_line_width(cr, 1.0);
964 cairo_translate(cr, allocation.x + side_margin,
965 allocation.y + top_margin);
966 /* paint the background */
967 for (row = 0; row < terminal->height; row++) {
968 for (col = 0; col < terminal->width; col++) {
969 /* get the attributes for this character cell */
970 terminal_decode_attr(terminal, row, col, &attr);
972 if (attr.attr.bg == terminal->color_scheme->border)
975 terminal_set_color(terminal, cr, attr.attr.bg);
976 cairo_move_to(cr, col * extents.max_x_advance,
977 row * extents.height);
978 cairo_rel_line_to(cr, extents.max_x_advance, 0);
979 cairo_rel_line_to(cr, 0, extents.height);
980 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
981 cairo_close_path(cr);
986 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
988 /* paint the foreground */
989 glyph_run_init(&run, terminal, cr);
990 for (row = 0; row < terminal->height; row++) {
991 p_row = terminal_get_row(terminal, row);
992 for (col = 0; col < terminal->width; col++) {
993 /* get the attributes for this character cell */
994 terminal_decode_attr(terminal, row, col, &attr);
996 glyph_run_flush(&run, attr);
998 text_x = col * extents.max_x_advance;
999 text_y = extents.ascent + row * extents.height;
1000 if (attr.attr.a & ATTRMASK_UNDERLINE) {
1001 terminal_set_color(terminal, cr, attr.attr.fg);
1002 cairo_move_to(cr, text_x, (double)text_y + 1.5);
1003 cairo_line_to(cr, text_x + extents.max_x_advance, (double) text_y + 1.5);
1007 glyph_run_add(&run, text_x, text_y, &p_row[col]);
1012 glyph_run_flush(&run, attr);
1014 if ((terminal->mode & MODE_SHOW_CURSOR) && !terminal->focused) {
1017 cairo_set_line_width(cr, 1);
1018 cairo_move_to(cr, terminal->column * extents.max_x_advance + d,
1019 terminal->row * extents.height + d);
1020 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
1021 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
1022 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
1023 cairo_close_path(cr);
1028 cairo_pop_group_to_source(cr);
1031 cairo_surface_destroy(surface);
1033 if (terminal->send_cursor_position) {
1034 cursor_x = side_margin + allocation.x +
1035 terminal->column * extents.max_x_advance;
1036 cursor_y = top_margin + allocation.y +
1037 terminal->row * extents.height;
1038 window_set_text_cursor_position(terminal->window,
1039 cursor_x, cursor_y);
1040 terminal->send_cursor_position = 0;
1045 terminal_write(struct terminal *terminal, const char *data, size_t length)
1047 if (write(terminal->master, data, length) < 0)
1049 terminal->send_cursor_position = 1;
1053 terminal_data(struct terminal *terminal, const char *data, size_t length);
1056 handle_char(struct terminal *terminal, union utf8_char utf8);
1059 handle_sgr(struct terminal *terminal, int code);
1062 handle_term_parameter(struct terminal *terminal, int code, int sr)
1066 if (terminal->escape_flags & ESC_FLAG_WHAT) {
1068 case 1: /* DECCKM */
1069 if (sr) terminal->key_mode = KM_APPLICATION;
1070 else terminal->key_mode = KM_NORMAL;
1072 case 2: /* DECANM */
1073 /* No VT52 support yet */
1074 terminal->g0 = CS_US;
1075 terminal->g1 = CS_US;
1076 terminal->cs = terminal->g0;
1078 case 3: /* DECCOLM */
1080 terminal_resize(terminal, 132, 24);
1082 terminal_resize(terminal, 80, 24);
1084 /* set columns, but also home cursor and clear screen */
1085 terminal->row = 0; terminal->column = 0;
1086 for (i = 0; i < terminal->height; i++) {
1087 memset(terminal_get_row(terminal, i),
1088 0, terminal->data_pitch);
1089 attr_init(terminal_get_attr_row(terminal, i),
1090 terminal->curr_attr, terminal->width);
1093 case 5: /* DECSCNM */
1094 if (sr) terminal->mode |= MODE_INVERSE;
1095 else terminal->mode &= ~MODE_INVERSE;
1098 terminal->origin_mode = sr;
1099 if (terminal->origin_mode)
1100 terminal->row = terminal->margin_top;
1103 terminal->column = 0;
1105 case 7: /* DECAWM */
1106 if (sr) terminal->mode |= MODE_AUTOWRAP;
1107 else terminal->mode &= ~MODE_AUTOWRAP;
1109 case 8: /* DECARM */
1110 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1111 else terminal->mode &= ~MODE_AUTOREPEAT;
1114 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1115 else terminal->mode &= ~MODE_SHOW_CURSOR;
1117 case 1037: /* deleteSendsDel */
1118 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1119 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1121 case 1039: /* altSendsEscape */
1122 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1123 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1126 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1132 if (sr) terminal->mode |= MODE_IRM;
1133 else terminal->mode &= ~MODE_IRM;
1136 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1137 else terminal->mode &= ~MODE_LF_NEWLINE;
1140 fprintf(stderr, "Unknown parameter: %d\n", code);
1147 handle_dcs(struct terminal *terminal)
1152 handle_osc(struct terminal *terminal)
1157 terminal->escape[terminal->escape_length++] = '\0';
1158 p = &terminal->escape[2];
1159 code = strtol(p, &p, 10);
1163 case 0: /* Icon name and window title */
1164 case 1: /* Icon label */
1165 case 2: /* Window title*/
1166 window_set_title(terminal->window, p);
1169 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1175 handle_escape(struct terminal *terminal)
1177 union utf8_char *row;
1178 struct attr *attr_row;
1180 int i, count, x, y, top, bottom;
1181 int args[10], set[10] = { 0, };
1182 char response[MAX_RESPONSE] = {0, };
1183 struct rectangle allocation;
1185 terminal->escape[terminal->escape_length++] = '\0';
1187 p = &terminal->escape[2];
1188 while ((isdigit(*p) || *p == ';') && i < 10) {
1197 args[i] = strtol(p, &p, 10);
1204 count = set[0] ? args[0] : 1;
1205 if (count == 0) count = 1;
1206 terminal_shift_line(terminal, count);
1209 count = set[0] ? args[0] : 1;
1210 if (count == 0) count = 1;
1211 if (terminal->row - count >= terminal->margin_top)
1212 terminal->row -= count;
1214 terminal->row = terminal->margin_top;
1217 count = set[0] ? args[0] : 1;
1218 if (count == 0) count = 1;
1219 if (terminal->row + count <= terminal->margin_bottom)
1220 terminal->row += count;
1222 terminal->row = terminal->margin_bottom;
1225 count = set[0] ? args[0] : 1;
1226 if (count == 0) count = 1;
1227 if ((terminal->column + count) < terminal->width)
1228 terminal->column += count;
1230 terminal->column = terminal->width - 1;
1233 count = set[0] ? args[0] : 1;
1234 if (count == 0) count = 1;
1235 if ((terminal->column - count) >= 0)
1236 terminal->column -= count;
1238 terminal->column = 0;
1241 count = set[0] ? args[0] : 1;
1242 if (terminal->row + count <= terminal->margin_bottom)
1243 terminal->row += count;
1245 terminal->row = terminal->margin_bottom;
1246 terminal->column = 0;
1249 count = set[0] ? args[0] : 1;
1250 if (terminal->row - count >= terminal->margin_top)
1251 terminal->row -= count;
1253 terminal->row = terminal->margin_top;
1254 terminal->column = 0;
1257 y = set[0] ? args[0] : 1;
1258 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1260 terminal->column = y - 1;
1264 x = (set[1] ? args[1] : 1) - 1;
1266 (x >= terminal->width ? terminal->width - 1 : x);
1268 y = (set[0] ? args[0] : 1) - 1;
1269 if (terminal->origin_mode) {
1270 y += terminal->margin_top;
1271 y = y < terminal->margin_top ? terminal->margin_top :
1272 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1275 (y >= terminal->height ? terminal->height - 1 : y);
1279 terminal->column = x;
1282 count = set[0] ? args[0] : 1;
1283 if (count == 0) count = 1;
1284 while (count > 0 && terminal->column < terminal->width) {
1285 if (terminal->tab_ruler[terminal->column]) count--;
1291 row = terminal_get_row(terminal, terminal->row);
1292 attr_row = terminal_get_attr_row(terminal, terminal->row);
1293 if (!set[0] || args[0] == 0 || args[0] > 2) {
1294 memset(&row[terminal->column],
1295 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1296 attr_init(&attr_row[terminal->column],
1297 terminal->curr_attr, terminal->width - terminal->column);
1298 for (i = terminal->row + 1; i < terminal->height; i++) {
1299 memset(terminal_get_row(terminal, i),
1300 0, terminal->data_pitch);
1301 attr_init(terminal_get_attr_row(terminal, i),
1302 terminal->curr_attr, terminal->width);
1304 } else if (args[0] == 1) {
1305 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1306 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1307 for (i = 0; i < terminal->row; i++) {
1308 memset(terminal_get_row(terminal, i),
1309 0, terminal->data_pitch);
1310 attr_init(terminal_get_attr_row(terminal, i),
1311 terminal->curr_attr, terminal->width);
1313 } else if (args[0] == 2) {
1314 for (i = 0; i < terminal->height; i++) {
1315 memset(terminal_get_row(terminal, i),
1316 0, terminal->data_pitch);
1317 attr_init(terminal_get_attr_row(terminal, i),
1318 terminal->curr_attr, terminal->width);
1323 row = terminal_get_row(terminal, terminal->row);
1324 attr_row = terminal_get_attr_row(terminal, terminal->row);
1325 if (!set[0] || args[0] == 0 || args[0] > 2) {
1326 memset(&row[terminal->column], 0,
1327 (terminal->width - terminal->column) * sizeof(union utf8_char));
1328 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1329 terminal->width - terminal->column);
1330 } else if (args[0] == 1) {
1331 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1332 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1333 } else if (args[0] == 2) {
1334 memset(row, 0, terminal->data_pitch);
1335 attr_init(attr_row, terminal->curr_attr, terminal->width);
1339 count = set[0] ? args[0] : 1;
1340 if (count == 0) count = 1;
1341 if (terminal->row >= terminal->margin_top &&
1342 terminal->row < terminal->margin_bottom)
1344 top = terminal->margin_top;
1345 terminal->margin_top = terminal->row;
1346 terminal_scroll(terminal, 0 - count);
1347 terminal->margin_top = top;
1348 } else if (terminal->row == terminal->margin_bottom) {
1349 memset(terminal_get_row(terminal, terminal->row),
1350 0, terminal->data_pitch);
1351 attr_init(terminal_get_attr_row(terminal, terminal->row),
1352 terminal->curr_attr, terminal->width);
1356 count = set[0] ? args[0] : 1;
1357 if (count == 0) count = 1;
1358 if (terminal->row >= terminal->margin_top &&
1359 terminal->row < terminal->margin_bottom)
1361 top = terminal->margin_top;
1362 terminal->margin_top = terminal->row;
1363 terminal_scroll(terminal, count);
1364 terminal->margin_top = top;
1365 } else if (terminal->row == terminal->margin_bottom) {
1366 memset(terminal_get_row(terminal, terminal->row),
1367 0, terminal->data_pitch);
1371 count = set[0] ? args[0] : 1;
1372 if (count == 0) count = 1;
1373 terminal_shift_line(terminal, 0 - count);
1376 terminal_scroll(terminal, set[0] ? args[0] : 1);
1379 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1382 count = set[0] ? args[0] : 1;
1383 if (count == 0) count = 1;
1384 if ((terminal->column + count) > terminal->width)
1385 count = terminal->width - terminal->column;
1386 row = terminal_get_row(terminal, terminal->row);
1387 attr_row = terminal_get_attr_row(terminal, terminal->row);
1388 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1389 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1392 count = set[0] ? args[0] : 1;
1393 if (count == 0) count = 1;
1394 while (count > 0 && terminal->column >= 0) {
1395 if (terminal->tab_ruler[terminal->column]) count--;
1401 y = set[0] ? args[0] : 1;
1402 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1404 terminal->column = y - 1;
1407 count = set[0] ? args[0] : 1;
1408 if (count == 0) count = 1;
1409 if (terminal->last_char.byte[0])
1410 for (i = 0; i < count; i++)
1411 handle_char(terminal, terminal->last_char);
1412 terminal->last_char.byte[0] = 0;
1414 case 'c': /* Primary DA */
1415 terminal_write(terminal, "\e[?6c", 5);
1418 x = set[0] ? args[0] : 1;
1419 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1421 terminal->row = x - 1;
1424 if (!set[0] || args[0] == 0) {
1425 terminal->tab_ruler[terminal->column] = 0;
1426 } else if (args[0] == 3) {
1427 memset(terminal->tab_ruler, 0, terminal->width);
1431 for(i = 0; i < 10 && set[i]; i++) {
1432 handle_term_parameter(terminal, args[i], 1);
1436 for(i = 0; i < 10 && set[i]; i++) {
1437 handle_term_parameter(terminal, args[i], 0);
1441 for(i = 0; i < 10; i++) {
1442 if (i <= 7 && set[i] && set[i + 1] &&
1443 set[i + 2] && args[i + 1] == 5)
1445 if (args[i] == 38) {
1446 handle_sgr(terminal, args[i + 2] + 256);
1448 } else if (args[i] == 48) {
1449 handle_sgr(terminal, args[i + 2] + 512);
1454 handle_sgr(terminal, args[i]);
1456 handle_sgr(terminal, 0);
1464 i = set[0] ? args[0] : 0;
1465 if (i == 0 || i == 5) {
1466 terminal_write(terminal, "\e[0n", 4);
1467 } else if (i == 6) {
1468 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1469 terminal->origin_mode ?
1470 terminal->row+terminal->margin_top : terminal->row+1,
1471 terminal->column+1);
1472 terminal_write(terminal, response, strlen(response));
1477 terminal->margin_top = 0;
1478 terminal->margin_bottom = terminal->height-1;
1480 terminal->column = 0;
1482 top = (set[0] ? args[0] : 1) - 1;
1484 (top >= terminal->height ? terminal->height - 1 : top);
1485 bottom = (set[1] ? args[1] : 1) - 1;
1486 bottom = bottom < 0 ? 0 :
1487 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1489 terminal->margin_top = top;
1490 terminal->margin_bottom = bottom;
1492 terminal->margin_top = 0;
1493 terminal->margin_bottom = terminal->height-1;
1495 if(terminal->origin_mode)
1496 terminal->row = terminal->margin_top;
1499 terminal->column = 0;
1503 terminal->saved_row = terminal->row;
1504 terminal->saved_column = terminal->column;
1506 case 't': /* windowOps */
1509 case 4: /* resize px */
1510 if (set[1] && set[2]) {
1511 widget_schedule_resize(terminal->widget,
1515 case 8: /* resize ch */
1516 if (set[1] && set[2]) {
1517 terminal_resize(terminal, args[2], args[1]);
1520 case 13: /* report position */
1521 widget_get_allocation(terminal->widget, &allocation);
1522 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1523 allocation.x, allocation.y);
1524 terminal_write(terminal, response, strlen(response));
1526 case 14: /* report px */
1527 widget_get_allocation(terminal->widget, &allocation);
1528 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1529 allocation.height, allocation.width);
1530 terminal_write(terminal, response, strlen(response));
1532 case 18: /* report ch */
1533 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1534 terminal->height, terminal->width);
1535 terminal_write(terminal, response, strlen(response));
1537 case 21: /* report title */
1538 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1539 window_get_title(terminal->window));
1540 terminal_write(terminal, response, strlen(response));
1544 terminal_resize(terminal, terminal->width, args[0]);
1546 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1550 terminal->row = terminal->saved_row;
1551 terminal->column = terminal->saved_column;
1554 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
1560 handle_non_csi_escape(struct terminal *terminal, char code)
1565 if(terminal->row < terminal->margin_top) {
1566 terminal->row = terminal->margin_top;
1567 terminal_scroll(terminal, -1);
1571 terminal->column = 0;
1575 if(terminal->row > terminal->margin_bottom) {
1576 terminal->row = terminal->margin_bottom;
1577 terminal_scroll(terminal, +1);
1581 terminal_init(terminal);
1584 terminal->tab_ruler[terminal->column] = 1;
1586 case '7': /* DECSC */
1587 terminal->saved_row = terminal->row;
1588 terminal->saved_column = terminal->column;
1589 terminal->saved_attr = terminal->curr_attr;
1590 terminal->saved_origin_mode = terminal->origin_mode;
1591 terminal->saved_cs = terminal->cs;
1592 terminal->saved_g0 = terminal->g0;
1593 terminal->saved_g1 = terminal->g1;
1595 case '8': /* DECRC */
1596 terminal->row = terminal->saved_row;
1597 terminal->column = terminal->saved_column;
1598 terminal->curr_attr = terminal->saved_attr;
1599 terminal->origin_mode = terminal->saved_origin_mode;
1600 terminal->cs = terminal->saved_cs;
1601 terminal->g0 = terminal->saved_g0;
1602 terminal->g1 = terminal->saved_g1;
1604 case '=': /* DECPAM */
1605 terminal->key_mode = KM_APPLICATION;
1607 case '>': /* DECPNM */
1608 terminal->key_mode = KM_NORMAL;
1611 fprintf(stderr, "Unknown escape code: %c\n", code);
1617 handle_special_escape(struct terminal *terminal, char special, char code)
1621 if (special == '#') {
1624 /* fill with 'E', no cheap way to do this */
1625 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1626 numChars = terminal->width * terminal->height;
1627 for(i = 0; i < numChars; i++) {
1628 terminal->data[i].byte[0] = 'E';
1632 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1635 } else if (special == '(' || special == ')') {
1639 terminal->g0 = CS_SPECIAL;
1641 terminal->g1 = CS_SPECIAL;
1645 terminal->g0 = CS_UK;
1647 terminal->g1 = CS_UK;
1651 terminal->g0 = CS_US;
1653 terminal->g1 = CS_US;
1656 fprintf(stderr, "Unknown character set %c\n", code);
1660 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1665 handle_sgr(struct terminal *terminal, int code)
1669 terminal->curr_attr = terminal->color_scheme->default_attr;
1672 terminal->curr_attr.a |= ATTRMASK_BOLD;
1673 if (terminal->curr_attr.fg < 8)
1674 terminal->curr_attr.fg += 8;
1677 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1680 terminal->curr_attr.a |= ATTRMASK_BLINK;
1683 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1688 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1689 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1690 terminal->curr_attr.fg -= 8;
1693 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1696 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1700 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1703 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1706 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1709 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1712 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1715 if(code >= 30 && code <= 37) {
1716 terminal->curr_attr.fg = code - 30;
1717 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1718 terminal->curr_attr.fg += 8;
1719 } else if(code >= 40 && code <= 47) {
1720 terminal->curr_attr.bg = code - 40;
1721 } else if (code >= 90 && code <= 97) {
1722 terminal->curr_attr.fg = code - 90 + 8;
1723 } else if (code >= 100 && code <= 107) {
1724 terminal->curr_attr.bg = code - 100 + 8;
1725 } else if(code >= 256 && code < 512) {
1726 terminal->curr_attr.fg = code - 256;
1727 } else if(code >= 512 && code < 768) {
1728 terminal->curr_attr.bg = code - 512;
1730 fprintf(stderr, "Unknown SGR code: %d\n", code);
1736 /* Returns 1 if c was special, otherwise 0 */
1738 handle_special_char(struct terminal *terminal, char c)
1740 union utf8_char *row;
1741 struct attr *attr_row;
1743 row = terminal_get_row(terminal, terminal->row);
1744 attr_row = terminal_get_attr_row(terminal, terminal->row);
1748 terminal->column = 0;
1751 if (terminal->mode & MODE_LF_NEWLINE) {
1752 terminal->column = 0;
1758 if(terminal->row > terminal->margin_bottom) {
1759 terminal->row = terminal->margin_bottom;
1760 terminal_scroll(terminal, +1);
1765 while (terminal->column < terminal->width) {
1766 if (terminal->tab_ruler[terminal->column]) break;
1767 if (terminal->mode & MODE_IRM)
1768 terminal_shift_line(terminal, +1);
1769 row[terminal->column].byte[0] = ' ';
1770 row[terminal->column].byte[1] = '\0';
1771 attr_row[terminal->column] = terminal->curr_attr;
1774 if (terminal->column >= terminal->width) {
1775 terminal->column = terminal->width - 1;
1780 if (terminal->column >= terminal->width) {
1781 terminal->column = terminal->width - 2;
1782 } else if (terminal->column > 0) {
1784 } else if (terminal->mode & MODE_AUTOWRAP) {
1785 terminal->column = terminal->width - 1;
1787 if (terminal->row < terminal->margin_top) {
1788 terminal->row = terminal->margin_top;
1789 terminal_scroll(terminal, -1);
1797 case '\x0E': /* SO */
1798 terminal->cs = terminal->g1;
1800 case '\x0F': /* SI */
1801 terminal->cs = terminal->g0;
1811 handle_char(struct terminal *terminal, union utf8_char utf8)
1813 union utf8_char *row;
1814 struct attr *attr_row;
1816 if (handle_special_char(terminal, utf8.byte[0])) return;
1818 apply_char_set(terminal->cs, &utf8);
1820 /* There are a whole lot of non-characters, control codes,
1821 * and formatting codes that should probably be ignored,
1823 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1828 /* Some of these non-characters should be translated, e.g.: */
1829 if (utf8.byte[0] < 32) {
1830 utf8.byte[0] = utf8.byte[0] + 64;
1833 /* handle right margin effects */
1834 if (terminal->column >= terminal->width) {
1835 if (terminal->mode & MODE_AUTOWRAP) {
1836 terminal->column = 0;
1838 if (terminal->row > terminal->margin_bottom) {
1839 terminal->row = terminal->margin_bottom;
1840 terminal_scroll(terminal, +1);
1847 row = terminal_get_row(terminal, terminal->row);
1848 attr_row = terminal_get_attr_row(terminal, terminal->row);
1850 if (terminal->mode & MODE_IRM)
1851 terminal_shift_line(terminal, +1);
1852 row[terminal->column] = utf8;
1853 attr_row[terminal->column++] = terminal->curr_attr;
1855 if (utf8.ch != terminal->last_char.ch)
1856 terminal->last_char = utf8;
1860 escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1864 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1865 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1866 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1867 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1868 else len = 1; /* Invalid, cannot happen */
1870 if (terminal->escape_length + len <= MAX_ESCAPE) {
1871 for (i = 0; i < len; i++)
1872 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1873 terminal->escape_length += len;
1874 } else if (terminal->escape_length < MAX_ESCAPE) {
1875 terminal->escape[terminal->escape_length++] = 0;
1880 terminal_data(struct terminal *terminal, const char *data, size_t length)
1883 union utf8_char utf8;
1884 enum utf8_state parser_state;
1886 for (i = 0; i < length; i++) {
1888 utf8_next_char(&terminal->state_machine, data[i]);
1889 switch(parser_state) {
1890 case utf8state_accept:
1891 utf8.ch = terminal->state_machine.s.ch;
1893 case utf8state_reject:
1894 /* the unicode replacement character */
1895 utf8.byte[0] = 0xEF;
1896 utf8.byte[1] = 0xBF;
1897 utf8.byte[2] = 0xBD;
1898 utf8.byte[3] = 0x00;
1904 /* assume escape codes never use non-ASCII characters */
1905 switch (terminal->state) {
1906 case escape_state_escape:
1907 escape_append_utf8(terminal, utf8);
1908 switch (utf8.byte[0]) {
1910 terminal->state = escape_state_dcs;
1913 terminal->state = escape_state_csi;
1916 terminal->state = escape_state_osc;
1920 case ')': /* special */
1921 terminal->state = escape_state_special;
1923 case '^': /* PM (not implemented) */
1924 case '_': /* APC (not implemented) */
1925 terminal->state = escape_state_ignore;
1928 terminal->state = escape_state_normal;
1929 handle_non_csi_escape(terminal, utf8.byte[0]);
1933 case escape_state_csi:
1934 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1936 } else if (utf8.byte[0] == '?') {
1937 terminal->escape_flags |= ESC_FLAG_WHAT;
1938 } else if (utf8.byte[0] == '>') {
1939 terminal->escape_flags |= ESC_FLAG_GT;
1940 } else if (utf8.byte[0] == '!') {
1941 terminal->escape_flags |= ESC_FLAG_BANG;
1942 } else if (utf8.byte[0] == '$') {
1943 terminal->escape_flags |= ESC_FLAG_CASH;
1944 } else if (utf8.byte[0] == '\'') {
1945 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1946 } else if (utf8.byte[0] == '"') {
1947 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1948 } else if (utf8.byte[0] == ' ') {
1949 terminal->escape_flags |= ESC_FLAG_SPACE;
1951 escape_append_utf8(terminal, utf8);
1952 if (terminal->escape_length >= MAX_ESCAPE)
1953 terminal->state = escape_state_normal;
1956 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1957 utf8.byte[0] == '`')
1959 terminal->state = escape_state_normal;
1960 handle_escape(terminal);
1964 case escape_state_inner_escape:
1965 if (utf8.byte[0] == '\\') {
1966 terminal->state = escape_state_normal;
1967 if (terminal->outer_state == escape_state_dcs) {
1968 handle_dcs(terminal);
1969 } else if (terminal->outer_state == escape_state_osc) {
1970 handle_osc(terminal);
1972 } else if (utf8.byte[0] == '\e') {
1973 terminal->state = terminal->outer_state;
1974 escape_append_utf8(terminal, utf8);
1975 if (terminal->escape_length >= MAX_ESCAPE)
1976 terminal->state = escape_state_normal;
1978 terminal->state = terminal->outer_state;
1979 if (terminal->escape_length < MAX_ESCAPE)
1980 terminal->escape[terminal->escape_length++] = '\e';
1981 escape_append_utf8(terminal, utf8);
1982 if (terminal->escape_length >= MAX_ESCAPE)
1983 terminal->state = escape_state_normal;
1986 case escape_state_dcs:
1987 case escape_state_osc:
1988 case escape_state_ignore:
1989 if (utf8.byte[0] == '\e') {
1990 terminal->outer_state = terminal->state;
1991 terminal->state = escape_state_inner_escape;
1992 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
1993 terminal->state = escape_state_normal;
1994 handle_osc(terminal);
1996 escape_append_utf8(terminal, utf8);
1997 if (terminal->escape_length >= MAX_ESCAPE)
1998 terminal->state = escape_state_normal;
2001 case escape_state_special:
2002 escape_append_utf8(terminal, utf8);
2003 terminal->state = escape_state_normal;
2004 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2005 handle_special_escape(terminal, terminal->escape[1],
2013 /* this is valid, because ASCII characters are never used to
2014 * introduce a multibyte sequence in UTF-8 */
2015 if (utf8.byte[0] == '\e') {
2016 terminal->state = escape_state_escape;
2017 terminal->outer_state = escape_state_normal;
2018 terminal->escape[0] = '\e';
2019 terminal->escape_length = 1;
2020 terminal->escape_flags = 0;
2022 handle_char(terminal, utf8);
2026 window_schedule_redraw(terminal->window);
2030 data_source_target(void *data,
2031 struct wl_data_source *source, const char *mime_type)
2033 fprintf(stderr, "data_source_target, %s\n", mime_type);
2037 data_source_send(void *data,
2038 struct wl_data_source *source,
2039 const char *mime_type, int32_t fd)
2041 struct terminal *terminal = data;
2043 terminal_send_selection(terminal, fd);
2047 data_source_cancelled(void *data, struct wl_data_source *source)
2049 wl_data_source_destroy(source);
2052 static const struct wl_data_source_listener data_source_listener = {
2055 data_source_cancelled
2059 handle_bound_key(struct terminal *terminal,
2060 struct input *input, uint32_t sym, uint32_t time)
2064 /* Cut selection; terminal doesn't do cut, fall
2065 * through to copy. */
2067 terminal->selection =
2068 display_create_data_source(terminal->display);
2069 wl_data_source_offer(terminal->selection,
2070 "text/plain;charset=utf-8");
2071 wl_data_source_add_listener(terminal->selection,
2072 &data_source_listener, terminal);
2073 input_set_selection(input, terminal->selection,
2074 display_get_serial(terminal->display));
2077 input_receive_selection_data_to_fd(input,
2078 "text/plain;charset=utf-8",
2088 key_handler(struct window *window, struct input *input, uint32_t time,
2089 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2092 struct terminal *terminal = data;
2093 char ch[MAX_RESPONSE];
2097 modifiers = input_get_modifiers(input);
2098 if ((modifiers & MOD_CONTROL_MASK) &&
2099 (modifiers & MOD_SHIFT_MASK) &&
2100 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2101 handle_bound_key(terminal, input, sym, time))
2106 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
2108 terminal->fullscreen ^= 1;
2109 window_set_fullscreen(window, terminal->fullscreen);
2112 case XKB_KEY_BackSpace:
2114 case XKB_KEY_Linefeed:
2117 case XKB_KEY_Scroll_Lock:
2118 case XKB_KEY_Sys_Req:
2119 case XKB_KEY_Escape:
2120 ch[len++] = sym & 0x7f;
2123 case XKB_KEY_Return:
2124 if (terminal->mode & MODE_LF_NEWLINE) {
2132 case XKB_KEY_Shift_L:
2133 case XKB_KEY_Shift_R:
2134 case XKB_KEY_Control_L:
2135 case XKB_KEY_Control_R:
2140 case XKB_KEY_Insert:
2141 len = function_key_response('[', 2, modifiers, '~', ch);
2143 case XKB_KEY_Delete:
2144 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2147 len = function_key_response('[', 3, modifiers, '~', ch);
2150 case XKB_KEY_Page_Up:
2151 len = function_key_response('[', 5, modifiers, '~', ch);
2153 case XKB_KEY_Page_Down:
2154 len = function_key_response('[', 6, modifiers, '~', ch);
2157 len = function_key_response('O', 1, modifiers, 'P', ch);
2160 len = function_key_response('O', 1, modifiers, 'Q', ch);
2163 len = function_key_response('O', 1, modifiers, 'R', ch);
2166 len = function_key_response('O', 1, modifiers, 'S', ch);
2169 len = function_key_response('[', 15, modifiers, '~', ch);
2172 len = function_key_response('[', 17, modifiers, '~', ch);
2175 len = function_key_response('[', 18, modifiers, '~', ch);
2178 len = function_key_response('[', 19, modifiers, '~', ch);
2181 len = function_key_response('[', 20, modifiers, '~', ch);
2184 len = function_key_response('[', 21, modifiers, '~', ch);
2187 len = function_key_response('[', 24, modifiers, '~', ch);
2190 /* Handle special keys with alternate mappings */
2191 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2192 if (len != 0) break;
2194 if (modifiers & MOD_CONTROL_MASK) {
2195 if (sym >= '3' && sym <= '7')
2196 sym = (sym & 0x1f) + 8;
2198 if (!((sym >= '!' && sym <= '/') ||
2199 (sym >= '8' && sym <= '?') ||
2200 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2201 else if (sym == '2') sym = 0x00;
2202 else if (sym == '/') sym = 0x1F;
2203 else if (sym == '8' || sym == '?') sym = 0x7F;
2204 } else if ((terminal->mode & MODE_ALT_SENDS_ESC) &&
2205 (modifiers & MOD_ALT_MASK))
2208 } else if (modifiers & MOD_ALT_MASK) {
2217 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0)
2218 terminal_write(terminal, ch, len);
2222 keyboard_focus_handler(struct window *window,
2223 struct input *device, void *data)
2225 struct terminal *terminal = data;
2227 terminal->focused = (device != NULL);
2228 window_schedule_redraw(terminal->window);
2232 button_handler(struct widget *widget,
2233 struct input *input, uint32_t time,
2235 enum wl_pointer_button_state state, void *data)
2237 struct terminal *terminal = data;
2241 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2242 terminal->dragging = 1;
2243 input_get_position(input,
2244 &terminal->selection_start_x,
2245 &terminal->selection_start_y);
2246 terminal->selection_end_x = terminal->selection_start_x;
2247 terminal->selection_end_y = terminal->selection_start_y;
2248 widget_schedule_redraw(widget);
2250 terminal->dragging = 0;
2257 motion_handler(struct widget *widget,
2258 struct input *input, uint32_t time,
2259 float x, float y, void *data)
2261 struct terminal *terminal = data;
2263 if (terminal->dragging) {
2264 input_get_position(input,
2265 &terminal->selection_end_x,
2266 &terminal->selection_end_y);
2267 widget_schedule_redraw(widget);
2270 return CURSOR_IBEAM;
2273 static struct terminal *
2274 terminal_create(struct display *display, int fullscreen)
2276 struct terminal *terminal;
2277 cairo_surface_t *surface;
2280 terminal = malloc(sizeof *terminal);
2281 if (terminal == NULL)
2284 memset(terminal, 0, sizeof *terminal);
2285 terminal->fullscreen = fullscreen;
2286 terminal->color_scheme = &DEFAULT_COLORS;
2287 terminal_init(terminal);
2288 terminal->margin_top = 0;
2289 terminal->margin_bottom = -1;
2290 terminal->window = window_create(display);
2291 terminal->widget = frame_create(terminal->window, terminal);
2292 window_set_title(terminal->window, "Wayland Terminal");
2293 widget_set_transparent(terminal->widget, 0);
2295 init_state_machine(&terminal->state_machine);
2296 init_color_table(terminal);
2298 terminal->display = display;
2299 terminal->margin = 5;
2301 window_set_user_data(terminal->window, terminal);
2302 window_set_key_handler(terminal->window, key_handler);
2303 window_set_keyboard_focus_handler(terminal->window,
2304 keyboard_focus_handler);
2305 widget_set_redraw_handler(terminal->widget, redraw_handler);
2306 widget_set_resize_handler(terminal->widget, resize_handler);
2307 widget_set_button_handler(terminal->widget, button_handler);
2308 widget_set_motion_handler(terminal->widget, motion_handler);
2310 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2311 cr = cairo_create(surface);
2312 cairo_set_font_size(cr, 14);
2313 cairo_select_font_face (cr, "mono",
2314 CAIRO_FONT_SLANT_NORMAL,
2315 CAIRO_FONT_WEIGHT_BOLD);
2316 terminal->font_bold = cairo_get_scaled_font (cr);
2317 cairo_scaled_font_reference(terminal->font_bold);
2319 cairo_select_font_face (cr, "mono",
2320 CAIRO_FONT_SLANT_NORMAL,
2321 CAIRO_FONT_WEIGHT_NORMAL);
2322 terminal->font_normal = cairo_get_scaled_font (cr);
2323 cairo_scaled_font_reference(terminal->font_normal);
2325 cairo_font_extents(cr, &terminal->extents);
2327 cairo_surface_destroy(surface);
2329 window_schedule_resize(terminal->window, 500, 400);
2335 io_handler(struct task *task, uint32_t events)
2337 struct terminal *terminal =
2338 container_of(task, struct terminal, io_task);
2342 if (events & EPOLLHUP)
2345 len = read(terminal->master, buffer, sizeof buffer);
2349 terminal_data(terminal, buffer, len);
2353 terminal_run(struct terminal *terminal, const char *path)
2358 pid = forkpty(&master, NULL, NULL, NULL);
2360 setenv("TERM", "xterm-256color", 1);
2361 setenv("COLORTERM", "xterm-256color", 1);
2362 if (execl(path, path, NULL)) {
2363 printf("exec failed: %m\n");
2366 } else if (pid < 0) {
2367 fprintf(stderr, "failed to fork and create pty (%m).\n");
2371 terminal->master = master;
2372 fcntl(master, F_SETFL, O_NONBLOCK);
2373 terminal->io_task.run = io_handler;
2374 display_watch_fd(terminal->display, terminal->master,
2375 EPOLLIN | EPOLLHUP, &terminal->io_task);
2377 window_set_fullscreen(terminal->window, terminal->fullscreen);
2378 if (!terminal->fullscreen)
2379 terminal_resize(terminal, 80, 24);
2384 static const struct weston_option terminal_options[] = {
2385 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
2388 int main(int argc, char *argv[])
2391 struct terminal *terminal;
2394 argc = parse_options(terminal_options,
2395 ARRAY_LENGTH(terminal_options), argc, argv);
2397 d = display_create(argc, argv);
2399 fprintf(stderr, "failed to create display: %m\n");
2403 shell = getenv("SHELL");
2405 shell = "/bin/bash";
2407 terminal = terminal_create(d, option_fullscreen);
2408 if (terminal_run(terminal, shell))