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>
37 #include <wayland-cursor.h>
41 static int option_fullscreen;
43 #define MOD_SHIFT 0x01
47 #define ATTRMASK_BOLD 0x01
48 #define ATTRMASK_UNDERLINE 0x02
49 #define ATTRMASK_BLINK 0x04
50 #define ATTRMASK_INVERSE 0x08
51 #define ATTRMASK_CONCEALED 0x10
54 #define MAX_RESPONSE 256
55 #define MAX_ESCAPE 255
58 #define MODE_SHOW_CURSOR 0x00000001
59 #define MODE_INVERSE 0x00000002
60 #define MODE_AUTOWRAP 0x00000004
61 #define MODE_AUTOREPEAT 0x00000008
62 #define MODE_LF_NEWLINE 0x00000010
63 #define MODE_IRM 0x00000020
64 #define MODE_DELETE_SENDS_DEL 0x00000040
65 #define MODE_ALT_SENDS_ESC 0x00000080
68 unsigned char byte[4];
81 struct utf8_state_machine {
82 enum utf8_state state;
88 init_state_machine(struct utf8_state_machine *machine)
90 machine->state = utf8state_start;
95 static enum utf8_state
96 utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
98 switch(machine->state) {
100 case utf8state_accept:
101 case utf8state_reject:
104 if(c == 0xC0 || c == 0xC1) {
105 /* overlong encoding, reject */
106 machine->state = utf8state_reject;
107 } else if((c & 0x80) == 0) {
108 /* single byte, accept */
109 machine->s.byte[machine->len++] = c;
110 machine->state = utf8state_accept;
111 } else if((c & 0xC0) == 0x80) {
112 /* parser out of sync, ignore byte */
113 machine->state = utf8state_start;
114 } else if((c & 0xE0) == 0xC0) {
115 /* start of two byte sequence */
116 machine->s.byte[machine->len++] = c;
117 machine->state = utf8state_expect1;
118 } else if((c & 0xF0) == 0xE0) {
119 /* start of three byte sequence */
120 machine->s.byte[machine->len++] = c;
121 machine->state = utf8state_expect2;
122 } else if((c & 0xF8) == 0xF0) {
123 /* start of four byte sequence */
124 machine->s.byte[machine->len++] = c;
125 machine->state = utf8state_expect3;
127 /* overlong encoding, reject */
128 machine->state = utf8state_reject;
131 case utf8state_expect3:
132 machine->s.byte[machine->len++] = c;
133 if((c & 0xC0) == 0x80) {
134 /* all good, continue */
135 machine->state = utf8state_expect2;
137 /* missing extra byte, reject */
138 machine->state = utf8state_reject;
141 case utf8state_expect2:
142 machine->s.byte[machine->len++] = c;
143 if((c & 0xC0) == 0x80) {
144 /* all good, continue */
145 machine->state = utf8state_expect1;
147 /* missing extra byte, reject */
148 machine->state = utf8state_reject;
151 case utf8state_expect1:
152 machine->s.byte[machine->len++] = c;
153 if((c & 0xC0) == 0x80) {
154 /* all good, accept */
155 machine->state = utf8state_accept;
157 /* missing extra byte, reject */
158 machine->state = utf8state_reject;
162 machine->state = utf8state_reject;
166 return machine->state;
170 union utf8_char match;
171 union utf8_char replace;
173 /* Set last char_sub match to NULL char */
174 typedef struct char_sub *character_set;
176 struct char_sub CS_US[] = {
179 static struct char_sub CS_UK[] = {
180 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}},
183 static struct char_sub CS_SPECIAL[] = {
184 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond */
185 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell */
186 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT */
187 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF */
188 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR */
189 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF */
190 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree */
191 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus */
192 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL */
193 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT */
194 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB */
195 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT */
196 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT */
197 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_RB */
198 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS */
199 {{{'o', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
200 {{{'p', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
201 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
202 {{{'r', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
203 {{{'s', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
204 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR */
205 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL */
206 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU */
207 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD */
208 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V */
209 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE */
210 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE */
211 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI */
212 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ */
213 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND */
214 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT */
219 apply_char_set(character_set cs, union utf8_char *utf8)
223 while (cs[i].match.byte[0]) {
224 if ((*utf8).ch == cs[i].match.ch) {
225 *utf8 = cs[i].replace;
238 /* Set last key_sub sym to NULL */
239 typedef struct key_map *keyboard_mode;
241 static struct key_map KM_NORMAL[] = {
242 { XKB_KEY_Left, 1, '[', 'D' },
243 { XKB_KEY_Right, 1, '[', 'C' },
244 { XKB_KEY_Up, 1, '[', 'A' },
245 { XKB_KEY_Down, 1, '[', 'B' },
246 { XKB_KEY_Home, 1, '[', 'H' },
247 { XKB_KEY_End, 1, '[', 'F' },
250 static struct key_map KM_APPLICATION[] = {
251 { XKB_KEY_Left, 1, 'O', 'D' },
252 { XKB_KEY_Right, 1, 'O', 'C' },
253 { XKB_KEY_Up, 1, 'O', 'A' },
254 { XKB_KEY_Down, 1, 'O', 'B' },
255 { XKB_KEY_Home, 1, 'O', 'H' },
256 { XKB_KEY_End, 1, 'O', 'F' },
257 { XKB_KEY_KP_Enter, 1, 'O', 'M' },
258 { XKB_KEY_KP_Multiply, 1, 'O', 'j' },
259 { XKB_KEY_KP_Add, 1, 'O', 'k' },
260 { XKB_KEY_KP_Separator, 1, 'O', 'l' },
261 { XKB_KEY_KP_Subtract, 1, 'O', 'm' },
262 { XKB_KEY_KP_Divide, 1, 'O', 'o' },
267 function_key_response(char escape, int num, uint32_t modifiers,
268 char code, char *response)
273 if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
274 if (modifiers & MOD_ALT_MASK) mod_num |= 2;
275 if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
278 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
279 num, mod_num + 1, code);
280 else if (code != '~')
281 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
284 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
287 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
291 /* returns the number of bytes written into response,
292 * which must have room for MAX_RESPONSE bytes */
294 apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
300 while (mode[i].sym) {
302 if (sym == map.sym) {
303 len = function_key_response(map.escape, map.num,
313 struct terminal_color { double r, g, b, a; };
315 unsigned char fg, bg;
316 char a; /* attributes format:
319 char s; /* in selection */
321 struct color_scheme {
322 struct terminal_color palette[16];
324 struct attr default_attr;
328 attr_init(struct attr *data_attr, struct attr attr, int n)
331 for (i = 0; i < n; i++) {
337 escape_state_normal = 0,
342 escape_state_inner_escape,
347 #define ESC_FLAG_WHAT 0x01
348 #define ESC_FLAG_GT 0x02
349 #define ESC_FLAG_BANG 0x04
350 #define ESC_FLAG_CASH 0x08
351 #define ESC_FLAG_SQUOTE 0x10
352 #define ESC_FLAG_DQUOTE 0x20
353 #define ESC_FLAG_SPACE 0x40
356 struct window *window;
357 struct widget *widget;
358 struct display *display;
359 union utf8_char *data;
362 struct attr *data_attr;
363 struct attr curr_attr;
366 char saved_origin_mode;
367 struct attr saved_attr;
368 union utf8_char last_char;
369 int margin_top, margin_bottom;
370 character_set cs, g0, g1;
371 character_set saved_cs, saved_g0, saved_g1;
372 keyboard_mode key_mode;
373 int data_pitch, attr_pitch; /* The width in bytes of a line */
374 int width, height, start, row, column;
375 int saved_row, saved_column;
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;
775 m = 2 * terminal->margin;
776 columns = (width - m) / (int32_t) terminal->extents.max_x_advance;
777 rows = (height - m) / (int32_t) terminal->extents.height;
779 if (!terminal->fullscreen) {
780 width = columns * terminal->extents.max_x_advance + m;
781 height = rows * terminal->extents.height + m;
782 widget_set_size(terminal->widget, width, height);
785 terminal_resize_cells(terminal, columns, rows);
789 terminal_resize(struct terminal *terminal, int columns, int rows)
791 int32_t width, height, m;
793 if (terminal->fullscreen)
796 m = 2 * terminal->margin;
797 width = columns * terminal->extents.max_x_advance + m;
798 height = rows * terminal->extents.height + m;
799 widget_schedule_resize(terminal->widget, width, height);
802 struct color_scheme DEFAULT_COLORS = {
804 {0, 0, 0, 1}, /* black */
805 {0.66, 0, 0, 1}, /* red */
806 {0 , 0.66, 0, 1}, /* green */
807 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
808 {0 , 0 , 0.66, 1}, /* blue */
809 {0.66, 0 , 0.66, 1}, /* magenta */
810 {0, 0.66, 0.66, 1}, /* cyan */
811 {0.66, 0.66, 0.66, 1}, /* light grey */
812 {0.22, 0.33, 0.33, 1}, /* dark grey */
813 {1, 0.33, 0.33, 1}, /* high red */
814 {0.33, 1, 0.33, 1}, /* high green */
815 {1, 1, 0.33, 1}, /* high yellow */
816 {0.33, 0.33, 1, 1}, /* high blue */
817 {1, 0.33, 1, 1}, /* high magenta */
818 {0.33, 1, 1, 1}, /* high cyan */
819 {1, 1, 1, 1} /* white */
821 0, /* black border */
822 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
826 terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
828 cairo_set_source_rgba(cr,
829 terminal->color_table[index].r,
830 terminal->color_table[index].g,
831 terminal->color_table[index].b,
832 terminal->color_table[index].a);
836 terminal_send_selection(struct terminal *terminal, int fd)
839 union utf8_char *p_row;
840 union decoded_attr attr;
844 fp = fdopen(fd, "w");
845 for (row = 0; row < terminal->height; row++) {
846 p_row = terminal_get_row(terminal, row);
847 for (col = 0; col < terminal->width; col++) {
848 /* get the attributes for this character cell */
849 terminal_decode_attr(terminal, row, col, &attr);
852 len = strnlen((char *) p_row[col].byte, 4);
853 fwrite(p_row[col].byte, 1, len, fp);
860 struct terminal *terminal;
863 union decoded_attr attr;
864 cairo_glyph_t glyphs[256], *g;
868 glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
870 run->terminal = terminal;
872 run->g = run->glyphs;
878 glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
880 cairo_scaled_font_t *font;
882 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
883 (attr.key != run->attr.key)) {
884 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
885 font = run->terminal->font_bold;
887 font = run->terminal->font_normal;
888 cairo_set_scaled_font(run->cr, font);
889 terminal_set_color(run->terminal, run->cr,
892 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
893 cairo_show_glyphs (run->cr, run->glyphs, run->count);
894 run->g = run->glyphs;
901 glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
904 cairo_scaled_font_t *font;
906 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
908 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
909 font = run->terminal->font_bold;
911 font = run->terminal->font_normal;
913 cairo_move_to(run->cr, x, y);
914 cairo_scaled_font_text_to_glyphs (font, x, y,
916 &run->g, &num_glyphs,
918 run->g += num_glyphs;
919 run->count += num_glyphs;
924 redraw_handler(struct widget *widget, void *data)
926 struct terminal *terminal = data;
927 struct rectangle allocation;
929 int top_margin, side_margin;
931 union utf8_char *p_row;
932 union decoded_attr attr;
934 cairo_surface_t *surface;
936 struct glyph_run run;
937 cairo_font_extents_t extents;
939 surface = window_get_surface(terminal->window);
940 widget_get_allocation(terminal->widget, &allocation);
941 cr = cairo_create(surface);
942 cairo_rectangle(cr, allocation.x, allocation.y,
943 allocation.width, allocation.height);
945 cairo_push_group(cr);
947 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
948 terminal_set_color(terminal, cr, terminal->color_scheme->border);
951 cairo_set_scaled_font(cr, terminal->font_normal);
953 extents = terminal->extents;
954 side_margin = (allocation.width - terminal->width * extents.max_x_advance) / 2;
955 top_margin = (allocation.height - terminal->height * extents.height) / 2;
957 cairo_set_line_width(cr, 1.0);
958 cairo_translate(cr, allocation.x + side_margin,
959 allocation.y + top_margin);
960 /* paint the background */
961 for (row = 0; row < terminal->height; row++) {
962 for (col = 0; col < terminal->width; col++) {
963 /* get the attributes for this character cell */
964 terminal_decode_attr(terminal, row, col, &attr);
966 if (attr.attr.bg == terminal->color_scheme->border)
969 terminal_set_color(terminal, cr, attr.attr.bg);
970 cairo_move_to(cr, col * extents.max_x_advance,
971 row * extents.height);
972 cairo_rel_line_to(cr, extents.max_x_advance, 0);
973 cairo_rel_line_to(cr, 0, extents.height);
974 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
975 cairo_close_path(cr);
980 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
982 /* paint the foreground */
983 glyph_run_init(&run, terminal, cr);
984 for (row = 0; row < terminal->height; row++) {
985 p_row = terminal_get_row(terminal, row);
986 for (col = 0; col < terminal->width; col++) {
987 /* get the attributes for this character cell */
988 terminal_decode_attr(terminal, row, col, &attr);
990 glyph_run_flush(&run, attr);
992 text_x = col * extents.max_x_advance;
993 text_y = extents.ascent + row * extents.height;
994 if (attr.attr.a & ATTRMASK_UNDERLINE) {
995 terminal_set_color(terminal, cr, attr.attr.fg);
996 cairo_move_to(cr, text_x, (double)text_y + 1.5);
997 cairo_line_to(cr, text_x + extents.max_x_advance, (double) text_y + 1.5);
1001 glyph_run_add(&run, text_x, text_y, &p_row[col]);
1006 glyph_run_flush(&run, attr);
1008 if ((terminal->mode & MODE_SHOW_CURSOR) && !terminal->focused) {
1011 cairo_set_line_width(cr, 1);
1012 cairo_move_to(cr, terminal->column * extents.max_x_advance + d,
1013 terminal->row * extents.height + d);
1014 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
1015 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
1016 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
1017 cairo_close_path(cr);
1022 cairo_pop_group_to_source(cr);
1025 cairo_surface_destroy(surface);
1029 terminal_write(struct terminal *terminal, const char *data, size_t length)
1031 if (write(terminal->master, data, length) < 0)
1036 terminal_data(struct terminal *terminal, const char *data, size_t length);
1039 handle_char(struct terminal *terminal, union utf8_char utf8);
1042 handle_sgr(struct terminal *terminal, int code);
1045 handle_term_parameter(struct terminal *terminal, int code, int sr)
1049 if (terminal->escape_flags & ESC_FLAG_WHAT) {
1051 case 1: /* DECCKM */
1052 if (sr) terminal->key_mode = KM_APPLICATION;
1053 else terminal->key_mode = KM_NORMAL;
1055 case 2: /* DECANM */
1056 /* No VT52 support yet */
1057 terminal->g0 = CS_US;
1058 terminal->g1 = CS_US;
1059 terminal->cs = terminal->g0;
1061 case 3: /* DECCOLM */
1063 terminal_resize(terminal, 132, 24);
1065 terminal_resize(terminal, 80, 24);
1067 /* set columns, but also home cursor and clear screen */
1068 terminal->row = 0; terminal->column = 0;
1069 for (i = 0; i < terminal->height; i++) {
1070 memset(terminal_get_row(terminal, i),
1071 0, terminal->data_pitch);
1072 attr_init(terminal_get_attr_row(terminal, i),
1073 terminal->curr_attr, terminal->width);
1076 case 5: /* DECSCNM */
1077 if (sr) terminal->mode |= MODE_INVERSE;
1078 else terminal->mode &= ~MODE_INVERSE;
1081 terminal->origin_mode = sr;
1082 if (terminal->origin_mode)
1083 terminal->row = terminal->margin_top;
1086 terminal->column = 0;
1088 case 7: /* DECAWM */
1089 if (sr) terminal->mode |= MODE_AUTOWRAP;
1090 else terminal->mode &= ~MODE_AUTOWRAP;
1092 case 8: /* DECARM */
1093 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1094 else terminal->mode &= ~MODE_AUTOREPEAT;
1097 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1098 else terminal->mode &= ~MODE_SHOW_CURSOR;
1100 case 1037: /* deleteSendsDel */
1101 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1102 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1104 case 1039: /* altSendsEscape */
1105 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1106 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1109 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1115 if (sr) terminal->mode |= MODE_IRM;
1116 else terminal->mode &= ~MODE_IRM;
1119 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1120 else terminal->mode &= ~MODE_LF_NEWLINE;
1123 fprintf(stderr, "Unknown parameter: %d\n", code);
1130 handle_dcs(struct terminal *terminal)
1135 handle_osc(struct terminal *terminal)
1140 terminal->escape[terminal->escape_length++] = '\0';
1141 p = &terminal->escape[2];
1142 code = strtol(p, &p, 10);
1146 case 0: /* Icon name and window title */
1147 case 1: /* Icon label */
1148 case 2: /* Window title*/
1149 window_set_title(terminal->window, p);
1152 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1158 handle_escape(struct terminal *terminal)
1160 union utf8_char *row;
1161 struct attr *attr_row;
1163 int i, count, x, y, top, bottom;
1164 int args[10], set[10] = { 0, };
1165 char response[MAX_RESPONSE] = {0, };
1166 struct rectangle allocation;
1168 terminal->escape[terminal->escape_length++] = '\0';
1170 p = &terminal->escape[2];
1171 while ((isdigit(*p) || *p == ';') && i < 10) {
1180 args[i] = strtol(p, &p, 10);
1187 count = set[0] ? args[0] : 1;
1188 if (count == 0) count = 1;
1189 terminal_shift_line(terminal, count);
1192 count = set[0] ? args[0] : 1;
1193 if (count == 0) count = 1;
1194 if (terminal->row - count >= terminal->margin_top)
1195 terminal->row -= count;
1197 terminal->row = terminal->margin_top;
1200 count = set[0] ? args[0] : 1;
1201 if (count == 0) count = 1;
1202 if (terminal->row + count <= terminal->margin_bottom)
1203 terminal->row += count;
1205 terminal->row = terminal->margin_bottom;
1208 count = set[0] ? args[0] : 1;
1209 if (count == 0) count = 1;
1210 if ((terminal->column + count) < terminal->width)
1211 terminal->column += count;
1213 terminal->column = terminal->width - 1;
1216 count = set[0] ? args[0] : 1;
1217 if (count == 0) count = 1;
1218 if ((terminal->column - count) >= 0)
1219 terminal->column -= count;
1221 terminal->column = 0;
1224 count = set[0] ? args[0] : 1;
1225 if (terminal->row + count <= terminal->margin_bottom)
1226 terminal->row += count;
1228 terminal->row = terminal->margin_bottom;
1229 terminal->column = 0;
1232 count = set[0] ? args[0] : 1;
1233 if (terminal->row - count >= terminal->margin_top)
1234 terminal->row -= count;
1236 terminal->row = terminal->margin_top;
1237 terminal->column = 0;
1240 y = set[0] ? args[0] : 1;
1241 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1243 terminal->column = y - 1;
1247 x = (set[1] ? args[1] : 1) - 1;
1249 (x >= terminal->width ? terminal->width - 1 : x);
1251 y = (set[0] ? args[0] : 1) - 1;
1252 if (terminal->origin_mode) {
1253 y += terminal->margin_top;
1254 y = y < terminal->margin_top ? terminal->margin_top :
1255 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1258 (y >= terminal->height ? terminal->height - 1 : y);
1262 terminal->column = x;
1265 count = set[0] ? args[0] : 1;
1266 if (count == 0) count = 1;
1267 while (count > 0 && terminal->column < terminal->width) {
1268 if (terminal->tab_ruler[terminal->column]) count--;
1274 row = terminal_get_row(terminal, terminal->row);
1275 attr_row = terminal_get_attr_row(terminal, terminal->row);
1276 if (!set[0] || args[0] == 0 || args[0] > 2) {
1277 memset(&row[terminal->column],
1278 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1279 attr_init(&attr_row[terminal->column],
1280 terminal->curr_attr, terminal->width - terminal->column);
1281 for (i = terminal->row + 1; i < terminal->height; i++) {
1282 memset(terminal_get_row(terminal, i),
1283 0, terminal->data_pitch);
1284 attr_init(terminal_get_attr_row(terminal, i),
1285 terminal->curr_attr, terminal->width);
1287 } else if (args[0] == 1) {
1288 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1289 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1290 for (i = 0; i < terminal->row; i++) {
1291 memset(terminal_get_row(terminal, i),
1292 0, terminal->data_pitch);
1293 attr_init(terminal_get_attr_row(terminal, i),
1294 terminal->curr_attr, terminal->width);
1296 } else if (args[0] == 2) {
1297 for (i = 0; i < terminal->height; i++) {
1298 memset(terminal_get_row(terminal, i),
1299 0, terminal->data_pitch);
1300 attr_init(terminal_get_attr_row(terminal, i),
1301 terminal->curr_attr, terminal->width);
1306 row = terminal_get_row(terminal, terminal->row);
1307 attr_row = terminal_get_attr_row(terminal, terminal->row);
1308 if (!set[0] || args[0] == 0 || args[0] > 2) {
1309 memset(&row[terminal->column], 0,
1310 (terminal->width - terminal->column) * sizeof(union utf8_char));
1311 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1312 terminal->width - terminal->column);
1313 } else if (args[0] == 1) {
1314 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1315 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1316 } else if (args[0] == 2) {
1317 memset(row, 0, terminal->data_pitch);
1318 attr_init(attr_row, terminal->curr_attr, terminal->width);
1322 count = set[0] ? args[0] : 1;
1323 if (count == 0) count = 1;
1324 if (terminal->row >= terminal->margin_top &&
1325 terminal->row < terminal->margin_bottom)
1327 top = terminal->margin_top;
1328 terminal->margin_top = terminal->row;
1329 terminal_scroll(terminal, 0 - count);
1330 terminal->margin_top = top;
1331 } else if (terminal->row == terminal->margin_bottom) {
1332 memset(terminal_get_row(terminal, terminal->row),
1333 0, terminal->data_pitch);
1334 attr_init(terminal_get_attr_row(terminal, terminal->row),
1335 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, 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);
1354 count = set[0] ? args[0] : 1;
1355 if (count == 0) count = 1;
1356 terminal_shift_line(terminal, 0 - count);
1359 terminal_scroll(terminal, set[0] ? args[0] : 1);
1362 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1365 count = set[0] ? args[0] : 1;
1366 if (count == 0) count = 1;
1367 if ((terminal->column + count) > terminal->width)
1368 count = terminal->width - terminal->column;
1369 row = terminal_get_row(terminal, terminal->row);
1370 attr_row = terminal_get_attr_row(terminal, terminal->row);
1371 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1372 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1375 count = set[0] ? args[0] : 1;
1376 if (count == 0) count = 1;
1377 while (count > 0 && terminal->column >= 0) {
1378 if (terminal->tab_ruler[terminal->column]) count--;
1384 y = set[0] ? args[0] : 1;
1385 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1387 terminal->column = y - 1;
1390 count = set[0] ? args[0] : 1;
1391 if (count == 0) count = 1;
1392 if (terminal->last_char.byte[0])
1393 for (i = 0; i < count; i++)
1394 handle_char(terminal, terminal->last_char);
1395 terminal->last_char.byte[0] = 0;
1397 case 'c': /* Primary DA */
1398 terminal_write(terminal, "\e[?6c", 5);
1401 x = set[0] ? args[0] : 1;
1402 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1404 terminal->row = x - 1;
1407 if (!set[0] || args[0] == 0) {
1408 terminal->tab_ruler[terminal->column] = 0;
1409 } else if (args[0] == 3) {
1410 memset(terminal->tab_ruler, 0, terminal->width);
1414 for(i = 0; i < 10 && set[i]; i++) {
1415 handle_term_parameter(terminal, args[i], 1);
1419 for(i = 0; i < 10 && set[i]; i++) {
1420 handle_term_parameter(terminal, args[i], 0);
1424 for(i = 0; i < 10; i++) {
1425 if (i <= 7 && set[i] && set[i + 1] &&
1426 set[i + 2] && args[i + 1] == 5)
1428 if (args[i] == 38) {
1429 handle_sgr(terminal, args[i + 2] + 256);
1431 } else if (args[i] == 48) {
1432 handle_sgr(terminal, args[i + 2] + 512);
1437 handle_sgr(terminal, args[i]);
1439 handle_sgr(terminal, 0);
1447 i = set[0] ? args[0] : 0;
1448 if (i == 0 || i == 5) {
1449 terminal_write(terminal, "\e[0n", 4);
1450 } else if (i == 6) {
1451 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1452 terminal->origin_mode ?
1453 terminal->row+terminal->margin_top : terminal->row+1,
1454 terminal->column+1);
1455 terminal_write(terminal, response, strlen(response));
1460 terminal->margin_top = 0;
1461 terminal->margin_bottom = terminal->height-1;
1463 terminal->column = 0;
1465 top = (set[0] ? args[0] : 1) - 1;
1467 (top >= terminal->height ? terminal->height - 1 : top);
1468 bottom = (set[1] ? args[1] : 1) - 1;
1469 bottom = bottom < 0 ? 0 :
1470 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1472 terminal->margin_top = top;
1473 terminal->margin_bottom = bottom;
1475 terminal->margin_top = 0;
1476 terminal->margin_bottom = terminal->height-1;
1478 if(terminal->origin_mode)
1479 terminal->row = terminal->margin_top;
1482 terminal->column = 0;
1486 terminal->saved_row = terminal->row;
1487 terminal->saved_column = terminal->column;
1489 case 't': /* windowOps */
1492 case 4: /* resize px */
1493 if (set[1] && set[2]) {
1494 widget_schedule_resize(terminal->widget,
1498 case 8: /* resize ch */
1499 if (set[1] && set[2]) {
1500 terminal_resize(terminal, args[2], args[1]);
1503 case 13: /* report position */
1504 widget_get_allocation(terminal->widget, &allocation);
1505 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1506 allocation.x, allocation.y);
1507 terminal_write(terminal, response, strlen(response));
1509 case 14: /* report px */
1510 widget_get_allocation(terminal->widget, &allocation);
1511 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1512 allocation.height, allocation.width);
1513 terminal_write(terminal, response, strlen(response));
1515 case 18: /* report ch */
1516 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1517 terminal->height, terminal->width);
1518 terminal_write(terminal, response, strlen(response));
1520 case 21: /* report title */
1521 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1522 window_get_title(terminal->window));
1523 terminal_write(terminal, response, strlen(response));
1527 terminal_resize(terminal, terminal->width, args[0]);
1529 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1533 terminal->row = terminal->saved_row;
1534 terminal->column = terminal->saved_column;
1537 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
1543 handle_non_csi_escape(struct terminal *terminal, char code)
1548 if(terminal->row < terminal->margin_top) {
1549 terminal->row = terminal->margin_top;
1550 terminal_scroll(terminal, -1);
1554 terminal->column = 0;
1558 if(terminal->row > terminal->margin_bottom) {
1559 terminal->row = terminal->margin_bottom;
1560 terminal_scroll(terminal, +1);
1564 terminal_init(terminal);
1567 terminal->tab_ruler[terminal->column] = 1;
1569 case '7': /* DECSC */
1570 terminal->saved_row = terminal->row;
1571 terminal->saved_column = terminal->column;
1572 terminal->saved_attr = terminal->curr_attr;
1573 terminal->saved_origin_mode = terminal->origin_mode;
1574 terminal->saved_cs = terminal->cs;
1575 terminal->saved_g0 = terminal->g0;
1576 terminal->saved_g1 = terminal->g1;
1578 case '8': /* DECRC */
1579 terminal->row = terminal->saved_row;
1580 terminal->column = terminal->saved_column;
1581 terminal->curr_attr = terminal->saved_attr;
1582 terminal->origin_mode = terminal->saved_origin_mode;
1583 terminal->cs = terminal->saved_cs;
1584 terminal->g0 = terminal->saved_g0;
1585 terminal->g1 = terminal->saved_g1;
1587 case '=': /* DECPAM */
1588 terminal->key_mode = KM_APPLICATION;
1590 case '>': /* DECPNM */
1591 terminal->key_mode = KM_NORMAL;
1594 fprintf(stderr, "Unknown escape code: %c\n", code);
1600 handle_special_escape(struct terminal *terminal, char special, char code)
1604 if (special == '#') {
1607 /* fill with 'E', no cheap way to do this */
1608 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1609 numChars = terminal->width * terminal->height;
1610 for(i = 0; i < numChars; i++) {
1611 terminal->data[i].byte[0] = 'E';
1615 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1618 } else if (special == '(' || special == ')') {
1622 terminal->g0 = CS_SPECIAL;
1624 terminal->g1 = CS_SPECIAL;
1628 terminal->g0 = CS_UK;
1630 terminal->g1 = CS_UK;
1634 terminal->g0 = CS_US;
1636 terminal->g1 = CS_US;
1639 fprintf(stderr, "Unknown character set %c\n", code);
1643 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1648 handle_sgr(struct terminal *terminal, int code)
1652 terminal->curr_attr = terminal->color_scheme->default_attr;
1655 terminal->curr_attr.a |= ATTRMASK_BOLD;
1656 if (terminal->curr_attr.fg < 8)
1657 terminal->curr_attr.fg += 8;
1660 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1663 terminal->curr_attr.a |= ATTRMASK_BLINK;
1666 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1671 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1672 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1673 terminal->curr_attr.fg -= 8;
1676 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1679 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1683 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1686 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1689 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1692 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1695 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1698 if(code >= 30 && code <= 37) {
1699 terminal->curr_attr.fg = code - 30;
1700 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1701 terminal->curr_attr.fg += 8;
1702 } else if(code >= 40 && code <= 47) {
1703 terminal->curr_attr.bg = code - 40;
1704 } else if (code >= 90 && code <= 97) {
1705 terminal->curr_attr.fg = code - 90 + 8;
1706 } else if (code >= 100 && code <= 107) {
1707 terminal->curr_attr.bg = code - 100 + 8;
1708 } else if(code >= 256 && code < 512) {
1709 terminal->curr_attr.fg = code - 256;
1710 } else if(code >= 512 && code < 768) {
1711 terminal->curr_attr.bg = code - 512;
1713 fprintf(stderr, "Unknown SGR code: %d\n", code);
1719 /* Returns 1 if c was special, otherwise 0 */
1721 handle_special_char(struct terminal *terminal, char c)
1723 union utf8_char *row;
1724 struct attr *attr_row;
1726 row = terminal_get_row(terminal, terminal->row);
1727 attr_row = terminal_get_attr_row(terminal, terminal->row);
1731 terminal->column = 0;
1734 if (terminal->mode & MODE_LF_NEWLINE) {
1735 terminal->column = 0;
1741 if(terminal->row > terminal->margin_bottom) {
1742 terminal->row = terminal->margin_bottom;
1743 terminal_scroll(terminal, +1);
1748 while (terminal->column < terminal->width) {
1749 if (terminal->tab_ruler[terminal->column]) break;
1750 if (terminal->mode & MODE_IRM)
1751 terminal_shift_line(terminal, +1);
1752 row[terminal->column].byte[0] = ' ';
1753 row[terminal->column].byte[1] = '\0';
1754 attr_row[terminal->column] = terminal->curr_attr;
1757 if (terminal->column >= terminal->width) {
1758 terminal->column = terminal->width - 1;
1763 if (terminal->column >= terminal->width) {
1764 terminal->column = terminal->width - 2;
1765 } else if (terminal->column > 0) {
1767 } else if (terminal->mode & MODE_AUTOWRAP) {
1768 terminal->column = terminal->width - 1;
1770 if (terminal->row < terminal->margin_top) {
1771 terminal->row = terminal->margin_top;
1772 terminal_scroll(terminal, -1);
1780 case '\x0E': /* SO */
1781 terminal->cs = terminal->g1;
1783 case '\x0F': /* SI */
1784 terminal->cs = terminal->g0;
1794 handle_char(struct terminal *terminal, union utf8_char utf8)
1796 union utf8_char *row;
1797 struct attr *attr_row;
1799 if (handle_special_char(terminal, utf8.byte[0])) return;
1801 apply_char_set(terminal->cs, &utf8);
1803 /* There are a whole lot of non-characters, control codes,
1804 * and formatting codes that should probably be ignored,
1806 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1811 /* Some of these non-characters should be translated, e.g.: */
1812 if (utf8.byte[0] < 32) {
1813 utf8.byte[0] = utf8.byte[0] + 64;
1816 /* handle right margin effects */
1817 if (terminal->column >= terminal->width) {
1818 if (terminal->mode & MODE_AUTOWRAP) {
1819 terminal->column = 0;
1821 if (terminal->row > terminal->margin_bottom) {
1822 terminal->row = terminal->margin_bottom;
1823 terminal_scroll(terminal, +1);
1830 row = terminal_get_row(terminal, terminal->row);
1831 attr_row = terminal_get_attr_row(terminal, terminal->row);
1833 if (terminal->mode & MODE_IRM)
1834 terminal_shift_line(terminal, +1);
1835 row[terminal->column] = utf8;
1836 attr_row[terminal->column++] = terminal->curr_attr;
1838 if (utf8.ch != terminal->last_char.ch)
1839 terminal->last_char = utf8;
1843 escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1847 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1848 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1849 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1850 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1851 else len = 1; /* Invalid, cannot happen */
1853 if (terminal->escape_length + len <= MAX_ESCAPE) {
1854 for (i = 0; i < len; i++)
1855 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1856 terminal->escape_length += len;
1857 } else if (terminal->escape_length < MAX_ESCAPE) {
1858 terminal->escape[terminal->escape_length++] = 0;
1863 terminal_data(struct terminal *terminal, const char *data, size_t length)
1866 union utf8_char utf8;
1867 enum utf8_state parser_state;
1869 for (i = 0; i < length; i++) {
1871 utf8_next_char(&terminal->state_machine, data[i]);
1872 switch(parser_state) {
1873 case utf8state_accept:
1874 utf8.ch = terminal->state_machine.s.ch;
1876 case utf8state_reject:
1877 /* the unicode replacement character */
1878 utf8.byte[0] = 0xEF;
1879 utf8.byte[1] = 0xBF;
1880 utf8.byte[2] = 0xBD;
1881 utf8.byte[3] = 0x00;
1887 /* assume escape codes never use non-ASCII characters */
1888 switch (terminal->state) {
1889 case escape_state_escape:
1890 escape_append_utf8(terminal, utf8);
1891 switch (utf8.byte[0]) {
1893 terminal->state = escape_state_dcs;
1896 terminal->state = escape_state_csi;
1899 terminal->state = escape_state_osc;
1903 case ')': /* special */
1904 terminal->state = escape_state_special;
1906 case '^': /* PM (not implemented) */
1907 case '_': /* APC (not implemented) */
1908 terminal->state = escape_state_ignore;
1911 terminal->state = escape_state_normal;
1912 handle_non_csi_escape(terminal, utf8.byte[0]);
1916 case escape_state_csi:
1917 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1919 } else if (utf8.byte[0] == '?') {
1920 terminal->escape_flags |= ESC_FLAG_WHAT;
1921 } else if (utf8.byte[0] == '>') {
1922 terminal->escape_flags |= ESC_FLAG_GT;
1923 } else if (utf8.byte[0] == '!') {
1924 terminal->escape_flags |= ESC_FLAG_BANG;
1925 } else if (utf8.byte[0] == '$') {
1926 terminal->escape_flags |= ESC_FLAG_CASH;
1927 } else if (utf8.byte[0] == '\'') {
1928 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1929 } else if (utf8.byte[0] == '"') {
1930 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1931 } else if (utf8.byte[0] == ' ') {
1932 terminal->escape_flags |= ESC_FLAG_SPACE;
1934 escape_append_utf8(terminal, utf8);
1935 if (terminal->escape_length >= MAX_ESCAPE)
1936 terminal->state = escape_state_normal;
1939 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1940 utf8.byte[0] == '`')
1942 terminal->state = escape_state_normal;
1943 handle_escape(terminal);
1947 case escape_state_inner_escape:
1948 if (utf8.byte[0] == '\\') {
1949 terminal->state = escape_state_normal;
1950 if (terminal->outer_state == escape_state_dcs) {
1951 handle_dcs(terminal);
1952 } else if (terminal->outer_state == escape_state_osc) {
1953 handle_osc(terminal);
1955 } else if (utf8.byte[0] == '\e') {
1956 terminal->state = terminal->outer_state;
1957 escape_append_utf8(terminal, utf8);
1958 if (terminal->escape_length >= MAX_ESCAPE)
1959 terminal->state = escape_state_normal;
1961 terminal->state = terminal->outer_state;
1962 if (terminal->escape_length < MAX_ESCAPE)
1963 terminal->escape[terminal->escape_length++] = '\e';
1964 escape_append_utf8(terminal, utf8);
1965 if (terminal->escape_length >= MAX_ESCAPE)
1966 terminal->state = escape_state_normal;
1969 case escape_state_dcs:
1970 case escape_state_osc:
1971 case escape_state_ignore:
1972 if (utf8.byte[0] == '\e') {
1973 terminal->outer_state = terminal->state;
1974 terminal->state = escape_state_inner_escape;
1975 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
1976 terminal->state = escape_state_normal;
1977 handle_osc(terminal);
1979 escape_append_utf8(terminal, utf8);
1980 if (terminal->escape_length >= MAX_ESCAPE)
1981 terminal->state = escape_state_normal;
1984 case escape_state_special:
1985 escape_append_utf8(terminal, utf8);
1986 terminal->state = escape_state_normal;
1987 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
1988 handle_special_escape(terminal, terminal->escape[1],
1996 /* this is valid, because ASCII characters are never used to
1997 * introduce a multibyte sequence in UTF-8 */
1998 if (utf8.byte[0] == '\e') {
1999 terminal->state = escape_state_escape;
2000 terminal->outer_state = escape_state_normal;
2001 terminal->escape[0] = '\e';
2002 terminal->escape_length = 1;
2003 terminal->escape_flags = 0;
2005 handle_char(terminal, utf8);
2009 window_schedule_redraw(terminal->window);
2013 data_source_target(void *data,
2014 struct wl_data_source *source, const char *mime_type)
2016 fprintf(stderr, "data_source_target, %s\n", mime_type);
2020 data_source_send(void *data,
2021 struct wl_data_source *source,
2022 const char *mime_type, int32_t fd)
2024 struct terminal *terminal = data;
2026 terminal_send_selection(terminal, fd);
2030 data_source_cancelled(void *data, struct wl_data_source *source)
2032 wl_data_source_destroy(source);
2035 static const struct wl_data_source_listener data_source_listener = {
2038 data_source_cancelled
2042 handle_bound_key(struct terminal *terminal,
2043 struct input *input, uint32_t sym, uint32_t time)
2047 /* Cut selection; terminal doesn't do cut, fall
2048 * through to copy. */
2050 terminal->selection =
2051 display_create_data_source(terminal->display);
2052 wl_data_source_offer(terminal->selection,
2053 "text/plain;charset=utf-8");
2054 wl_data_source_add_listener(terminal->selection,
2055 &data_source_listener, terminal);
2056 input_set_selection(input, terminal->selection, time);
2059 input_receive_selection_data_to_fd(input,
2060 "text/plain;charset=utf-8",
2070 key_handler(struct window *window, struct input *input, uint32_t time,
2071 uint32_t key, uint32_t sym, uint32_t state, void *data)
2073 struct terminal *terminal = data;
2074 char ch[MAX_RESPONSE];
2078 modifiers = input_get_modifiers(input);
2079 if ((modifiers & MOD_CONTROL_MASK) &&
2080 (modifiers & MOD_SHIFT_MASK) &&
2081 state && handle_bound_key(terminal, input, sym, time))
2088 terminal->fullscreen ^= 1;
2089 window_set_fullscreen(window, terminal->fullscreen);
2092 case XKB_KEY_BackSpace:
2094 case XKB_KEY_Linefeed:
2097 case XKB_KEY_Scroll_Lock:
2098 case XKB_KEY_Sys_Req:
2099 case XKB_KEY_Escape:
2100 ch[len++] = sym & 0x7f;
2103 case XKB_KEY_Return:
2104 if (terminal->mode & MODE_LF_NEWLINE) {
2112 case XKB_KEY_Shift_L:
2113 case XKB_KEY_Shift_R:
2114 case XKB_KEY_Control_L:
2115 case XKB_KEY_Control_R:
2120 case XKB_KEY_Insert:
2121 len = function_key_response('[', 2, modifiers, '~', ch);
2123 case XKB_KEY_Delete:
2124 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2127 len = function_key_response('[', 3, modifiers, '~', ch);
2130 case XKB_KEY_Page_Up:
2131 len = function_key_response('[', 5, modifiers, '~', ch);
2133 case XKB_KEY_Page_Down:
2134 len = function_key_response('[', 6, modifiers, '~', ch);
2137 len = function_key_response('O', 1, modifiers, 'P', ch);
2140 len = function_key_response('O', 1, modifiers, 'Q', ch);
2143 len = function_key_response('O', 1, modifiers, 'R', ch);
2146 len = function_key_response('O', 1, modifiers, 'S', ch);
2149 len = function_key_response('[', 15, modifiers, '~', ch);
2152 len = function_key_response('[', 17, modifiers, '~', ch);
2155 len = function_key_response('[', 18, modifiers, '~', ch);
2158 len = function_key_response('[', 19, modifiers, '~', ch);
2161 len = function_key_response('[', 20, modifiers, '~', ch);
2164 len = function_key_response('[', 21, modifiers, '~', ch);
2167 len = function_key_response('[', 24, modifiers, '~', ch);
2170 /* Handle special keys with alternate mappings */
2171 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2172 if (len != 0) break;
2174 if (modifiers & MOD_CONTROL_MASK) {
2175 if (sym >= '3' && sym <= '7')
2176 sym = (sym & 0x1f) + 8;
2178 if (!((sym >= '!' && sym <= '/') ||
2179 (sym >= '8' && sym <= '?') ||
2180 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2181 else if (sym == '2') sym = 0x00;
2182 else if (sym == '/') sym = 0x1F;
2183 else if (sym == '8' || sym == '?') sym = 0x7F;
2184 } else if ((terminal->mode & MODE_ALT_SENDS_ESC) &&
2185 (modifiers & MOD_ALT_MASK))
2188 } else if (modifiers & MOD_ALT_MASK) {
2197 if (state && len > 0)
2198 terminal_write(terminal, ch, len);
2202 keyboard_focus_handler(struct window *window,
2203 struct input *device, void *data)
2205 struct terminal *terminal = data;
2207 terminal->focused = (device != NULL);
2208 window_schedule_redraw(terminal->window);
2212 button_handler(struct widget *widget,
2213 struct input *input, uint32_t time,
2214 uint32_t button, uint32_t state, void *data)
2216 struct terminal *terminal = data;
2221 terminal->dragging = 1;
2222 input_get_position(input,
2223 &terminal->selection_start_x,
2224 &terminal->selection_start_y);
2225 terminal->selection_end_x = terminal->selection_start_x;
2226 terminal->selection_end_y = terminal->selection_start_y;
2227 widget_schedule_redraw(widget);
2229 terminal->dragging = 0;
2236 motion_handler(struct widget *widget,
2237 struct input *input, uint32_t time,
2238 float x, float y, void *data)
2240 struct terminal *terminal = data;
2242 if (terminal->dragging) {
2243 input_get_position(input,
2244 &terminal->selection_end_x,
2245 &terminal->selection_end_y);
2246 widget_schedule_redraw(widget);
2249 return WL_CURSOR_IBEAM;
2252 static struct terminal *
2253 terminal_create(struct display *display, int fullscreen)
2255 struct terminal *terminal;
2256 cairo_surface_t *surface;
2259 terminal = malloc(sizeof *terminal);
2260 if (terminal == NULL)
2263 memset(terminal, 0, sizeof *terminal);
2264 terminal->fullscreen = fullscreen;
2265 terminal->color_scheme = &DEFAULT_COLORS;
2266 terminal_init(terminal);
2267 terminal->margin_top = 0;
2268 terminal->margin_bottom = -1;
2269 terminal->window = window_create(display);
2270 terminal->widget = frame_create(terminal->window, terminal);
2271 window_set_title(terminal->window, "Wayland Terminal");
2272 widget_set_transparent(terminal->widget, 0);
2274 init_state_machine(&terminal->state_machine);
2275 init_color_table(terminal);
2277 terminal->display = display;
2278 terminal->margin = 5;
2280 window_set_user_data(terminal->window, terminal);
2281 window_set_key_handler(terminal->window, key_handler);
2282 window_set_keyboard_focus_handler(terminal->window,
2283 keyboard_focus_handler);
2284 widget_set_redraw_handler(terminal->widget, redraw_handler);
2285 widget_set_resize_handler(terminal->widget, resize_handler);
2286 widget_set_button_handler(terminal->widget, button_handler);
2287 widget_set_motion_handler(terminal->widget, motion_handler);
2289 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2290 cr = cairo_create(surface);
2291 cairo_set_font_size(cr, 14);
2292 cairo_select_font_face (cr, "mono",
2293 CAIRO_FONT_SLANT_NORMAL,
2294 CAIRO_FONT_WEIGHT_BOLD);
2295 terminal->font_bold = cairo_get_scaled_font (cr);
2296 cairo_scaled_font_reference(terminal->font_bold);
2298 cairo_select_font_face (cr, "mono",
2299 CAIRO_FONT_SLANT_NORMAL,
2300 CAIRO_FONT_WEIGHT_NORMAL);
2301 terminal->font_normal = cairo_get_scaled_font (cr);
2302 cairo_scaled_font_reference(terminal->font_normal);
2304 cairo_font_extents(cr, &terminal->extents);
2306 cairo_surface_destroy(surface);
2308 window_schedule_resize(terminal->window, 500, 400);
2314 io_handler(struct task *task, uint32_t events)
2316 struct terminal *terminal =
2317 container_of(task, struct terminal, io_task);
2321 if (events & EPOLLHUP)
2324 len = read(terminal->master, buffer, sizeof buffer);
2328 terminal_data(terminal, buffer, len);
2332 terminal_run(struct terminal *terminal, const char *path)
2337 pid = forkpty(&master, NULL, NULL, NULL);
2339 setenv("TERM", "xterm-256color", 1);
2340 setenv("COLORTERM", "xterm-256color", 1);
2341 if (execl(path, path, NULL)) {
2342 printf("exec failed: %m\n");
2345 } else if (pid < 0) {
2346 fprintf(stderr, "failed to fork and create pty (%m).\n");
2350 terminal->master = master;
2351 fcntl(master, F_SETFL, O_NONBLOCK);
2352 terminal->io_task.run = io_handler;
2353 display_watch_fd(terminal->display, terminal->master,
2354 EPOLLIN | EPOLLHUP, &terminal->io_task);
2356 window_set_fullscreen(terminal->window, terminal->fullscreen);
2357 if (!terminal->fullscreen)
2358 terminal_resize(terminal, 80, 24);
2363 static const struct weston_option terminal_options[] = {
2364 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
2367 int main(int argc, char *argv[])
2370 struct terminal *terminal;
2373 argc = parse_options(terminal_options,
2374 ARRAY_LENGTH(terminal_options), argc, argv);
2376 d = display_create(argc, argv);
2378 fprintf(stderr, "failed to create display: %m\n");
2382 shell = getenv("SHELL");
2384 shell = "/bin/bash";
2386 terminal = terminal_create(d, option_fullscreen);
2387 if (terminal_run(terminal, shell))