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
35 #include <sys/epoll.h>
37 #include <X11/keysym.h>
39 #include <wayland-client.h>
43 static int option_fullscreen;
45 #define MOD_SHIFT 0x01
49 #define ATTRMASK_BOLD 0x01
50 #define ATTRMASK_UNDERLINE 0x02
51 #define ATTRMASK_BLINK 0x04
52 #define ATTRMASK_INVERSE 0x08
53 #define ATTRMASK_CONCEALED 0x10
56 #define MAX_RESPONSE 256
57 #define MAX_ESCAPE 255
60 #define MODE_SHOW_CURSOR 0x00000001
61 #define MODE_INVERSE 0x00000002
62 #define MODE_AUTOWRAP 0x00000004
63 #define MODE_AUTOREPEAT 0x00000008
64 #define MODE_LF_NEWLINE 0x00000010
65 #define MODE_IRM 0x00000020
66 #define MODE_DELETE_SENDS_DEL 0x00000040
67 #define MODE_ALT_SENDS_ESC 0x00000080
70 unsigned char byte[4];
83 struct utf8_state_machine {
84 enum utf8_state state;
90 init_state_machine(struct utf8_state_machine *machine)
92 machine->state = utf8state_start;
97 static enum utf8_state
98 utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
100 switch(machine->state) {
101 case utf8state_start:
102 case utf8state_accept:
103 case utf8state_reject:
106 if(c == 0xC0 || c == 0xC1) {
107 /* overlong encoding, reject */
108 machine->state = utf8state_reject;
109 } else if((c & 0x80) == 0) {
110 /* single byte, accept */
111 machine->s.byte[machine->len++] = c;
112 machine->state = utf8state_accept;
113 } else if((c & 0xC0) == 0x80) {
114 /* parser out of sync, ignore byte */
115 machine->state = utf8state_start;
116 } else if((c & 0xE0) == 0xC0) {
117 /* start of two byte sequence */
118 machine->s.byte[machine->len++] = c;
119 machine->state = utf8state_expect1;
120 } else if((c & 0xF0) == 0xE0) {
121 /* start of three byte sequence */
122 machine->s.byte[machine->len++] = c;
123 machine->state = utf8state_expect2;
124 } else if((c & 0xF8) == 0xF0) {
125 /* start of four byte sequence */
126 machine->s.byte[machine->len++] = c;
127 machine->state = utf8state_expect3;
129 /* overlong encoding, reject */
130 machine->state = utf8state_reject;
133 case utf8state_expect3:
134 machine->s.byte[machine->len++] = c;
135 if((c & 0xC0) == 0x80) {
136 /* all good, continue */
137 machine->state = utf8state_expect2;
139 /* missing extra byte, reject */
140 machine->state = utf8state_reject;
143 case utf8state_expect2:
144 machine->s.byte[machine->len++] = c;
145 if((c & 0xC0) == 0x80) {
146 /* all good, continue */
147 machine->state = utf8state_expect1;
149 /* missing extra byte, reject */
150 machine->state = utf8state_reject;
153 case utf8state_expect1:
154 machine->s.byte[machine->len++] = c;
155 if((c & 0xC0) == 0x80) {
156 /* all good, accept */
157 machine->state = utf8state_accept;
159 /* missing extra byte, reject */
160 machine->state = utf8state_reject;
164 machine->state = utf8state_reject;
168 return machine->state;
172 union utf8_char match;
173 union utf8_char replace;
175 /* Set last char_sub match to NULL char */
176 typedef struct char_sub *character_set;
178 struct char_sub CS_US[] = {
181 static struct char_sub CS_UK[] = {
182 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}},
185 static struct char_sub CS_SPECIAL[] = {
186 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond */
187 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell */
188 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT */
189 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF */
190 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR */
191 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF */
192 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree */
193 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus */
194 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL */
195 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT */
196 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB */
197 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT */
198 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT */
199 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_RB */
200 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS */
201 {{{'o', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
202 {{{'p', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
203 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
204 {{{'r', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
205 {{{'s', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
206 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR */
207 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL */
208 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU */
209 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD */
210 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V */
211 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE */
212 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE */
213 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI */
214 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ */
215 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND */
216 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT */
221 apply_char_set(character_set cs, union utf8_char *utf8)
225 while (cs[i].match.byte[0]) {
226 if ((*utf8).ch == cs[i].match.ch) {
227 *utf8 = cs[i].replace;
240 /* Set last key_sub sym to NULL */
241 typedef struct key_map *keyboard_mode;
243 static struct key_map KM_NORMAL[] = {
244 {XK_Left, 1, '[', 'D'},
245 {XK_Right, 1, '[', 'C'},
246 {XK_Up, 1, '[', 'A'},
247 {XK_Down, 1, '[', 'B'},
248 {XK_Home, 1, '[', 'H'},
249 {XK_End, 1, '[', 'F'},
252 static struct key_map KM_APPLICATION[] = {
253 {XK_Left, 1, 'O', 'D'},
254 {XK_Right, 1, 'O', 'C'},
255 {XK_Up, 1, 'O', 'A'},
256 {XK_Down, 1, 'O', 'B'},
257 {XK_Home, 1, 'O', 'H'},
258 {XK_End, 1, 'O', 'F'},
259 {XK_KP_Enter, 1, 'O', 'M'},
260 {XK_KP_Multiply, 1, 'O', 'j'},
261 {XK_KP_Add, 1, 'O', 'k'},
262 {XK_KP_Separator, 1, 'O', 'l'},
263 {XK_KP_Subtract, 1, 'O', 'm'},
264 {XK_KP_Divide, 1, 'O', 'o'},
269 function_key_response(char escape, int num, uint32_t modifiers,
270 char code, char *response)
275 if (modifiers & XKB_COMMON_SHIFT_MASK) mod_num |= 1;
276 if (modifiers & XKB_COMMON_MOD1_MASK) mod_num |= 2;
277 if (modifiers & XKB_COMMON_CONTROL_MASK) mod_num |= 4;
280 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
281 num, mod_num + 1, code);
282 else if (code != '~')
283 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
286 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
289 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
293 /* returns the number of bytes written into response,
294 * which must have room for MAX_RESPONSE bytes */
296 apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
302 while (mode[i].sym) {
304 if (sym == map.sym) {
305 len = function_key_response(map.escape, map.num,
315 struct terminal_color { double r, g, b, a; };
317 unsigned char fg, bg;
318 char a; /* attributes format:
321 char s; /* in selection */
323 struct color_scheme {
324 struct terminal_color palette[16];
326 struct attr default_attr;
330 attr_init(struct attr *data_attr, struct attr attr, int n)
333 for (i = 0; i < n; i++) {
339 escape_state_normal = 0,
344 escape_state_inner_escape,
349 #define ESC_FLAG_WHAT 0x01
350 #define ESC_FLAG_GT 0x02
351 #define ESC_FLAG_BANG 0x04
352 #define ESC_FLAG_CASH 0x08
353 #define ESC_FLAG_SQUOTE 0x10
354 #define ESC_FLAG_DQUOTE 0x20
355 #define ESC_FLAG_SPACE 0x40
358 struct window *window;
359 struct widget *widget;
360 struct display *display;
361 union utf8_char *data;
364 struct attr *data_attr;
365 struct attr curr_attr;
368 char saved_origin_mode;
369 struct attr saved_attr;
370 union utf8_char last_char;
371 int margin_top, margin_bottom;
372 character_set cs, g0, g1;
373 character_set saved_cs, saved_g0, saved_g1;
374 keyboard_mode key_mode;
375 int data_pitch, attr_pitch; /* The width in bytes of a line */
376 int width, height, start, row, column;
377 int saved_row, saved_column;
380 char escape[MAX_ESCAPE+1];
382 enum escape_state state;
383 enum escape_state outer_state;
385 struct utf8_state_machine state_machine;
389 struct color_scheme *color_scheme;
390 struct terminal_color color_table[256];
391 cairo_font_extents_t extents;
392 cairo_scaled_font_t *font_normal, *font_bold;
394 struct wl_data_source *selection;
396 int selection_start_x, selection_start_y;
397 int selection_end_x, selection_end_y;
400 /* Create default tab stops, every 8 characters */
402 terminal_init_tabs(struct terminal *terminal)
406 while (i < terminal->width) {
408 terminal->tab_ruler[i] = 1;
410 terminal->tab_ruler[i] = 0;
416 terminal_init(struct terminal *terminal)
418 terminal->curr_attr = terminal->color_scheme->default_attr;
419 terminal->origin_mode = 0;
420 terminal->mode = MODE_SHOW_CURSOR |
426 terminal->column = 0;
428 terminal->g0 = CS_US;
429 terminal->g1 = CS_US;
430 terminal->cs = terminal->g0;
431 terminal->key_mode = KM_NORMAL;
433 terminal->saved_g0 = terminal->g0;
434 terminal->saved_g1 = terminal->g1;
435 terminal->saved_cs = terminal->cs;
437 terminal->saved_attr = terminal->curr_attr;
438 terminal->saved_origin_mode = terminal->origin_mode;
439 terminal->saved_row = terminal->row;
440 terminal->saved_column = terminal->column;
442 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
446 init_color_table(struct terminal *terminal)
449 struct terminal_color *color_table = terminal->color_table;
451 for (c = 0; c < 256; c ++) {
453 color_table[c] = terminal->color_scheme->palette[c];
454 } else if (c < 232) {
456 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
457 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
458 color_table[c].r = ((double)(r % 6) / 6.0);
459 color_table[c].a = 1.0;
461 r = (c - 232) * 10 + 8;
462 color_table[c].r = ((double) r) / 256.0;
463 color_table[c].g = color_table[c].r;
464 color_table[c].b = color_table[c].r;
465 color_table[c].a = 1.0;
470 static union utf8_char *
471 terminal_get_row(struct terminal *terminal, int row)
475 index = (row + terminal->start) % terminal->height;
477 return &terminal->data[index * terminal->width];
481 terminal_get_attr_row(struct terminal *terminal, int row)
485 index = (row + terminal->start) % terminal->height;
487 return &terminal->data_attr[index * terminal->width];
496 terminal_compare_position(struct terminal *terminal,
497 int x, int y, int32_t ref_row, int32_t ref_col)
499 struct rectangle allocation;
500 int top_margin, side_margin, col, row, ref_x;
502 widget_get_allocation(terminal->widget, &allocation);
503 side_margin = allocation.x + (allocation.width - terminal->width * terminal->extents.max_x_advance) / 2;
504 top_margin = allocation.y + (allocation.height - terminal->height * terminal->extents.height) / 2;
506 col = (x - side_margin) / terminal->extents.max_x_advance;
507 row = (y - top_margin) / terminal->extents.height;
509 ref_x = side_margin + ref_col * terminal->extents.max_x_advance +
510 terminal->extents.max_x_advance / 2;
514 if (row == ref_row) {
517 if (col == ref_col && x < ref_x)
525 terminal_decode_attr(struct terminal *terminal, int row, int col,
526 union decoded_attr *decoded)
529 int foreground, background, tmp;
530 int start_cmp, end_cmp;
533 terminal_compare_position(terminal,
534 terminal->selection_start_x,
535 terminal->selection_start_y,
538 terminal_compare_position(terminal,
539 terminal->selection_end_x,
540 terminal->selection_end_y,
543 if (start_cmp < 0 && end_cmp > 0)
545 else if (end_cmp < 0 && start_cmp > 0)
548 /* get the attributes for this character cell */
549 attr = terminal_get_attr_row(terminal, row)[col];
550 if ((attr.a & ATTRMASK_INVERSE) ||
552 ((terminal->mode & MODE_SHOW_CURSOR) &&
553 terminal->focused && terminal->row == row &&
554 terminal->column == col)) {
555 foreground = attr.bg;
556 background = attr.fg;
557 if (attr.a & ATTRMASK_BOLD) {
558 if (foreground <= 16) foreground |= 0x08;
559 if (background <= 16) background &= 0x07;
562 foreground = attr.fg;
563 background = attr.bg;
566 if (terminal->mode & MODE_INVERSE) {
568 foreground = background;
570 if (attr.a & ATTRMASK_BOLD) {
571 if (foreground <= 16) foreground |= 0x08;
572 if (background <= 16) background &= 0x07;
576 decoded->attr.fg = foreground;
577 decoded->attr.bg = background;
578 decoded->attr.a = attr.a;
583 terminal_scroll_buffer(struct terminal *terminal, int d)
587 d = d % (terminal->height + 1);
588 terminal->start = (terminal->start + d) % terminal->height;
589 if (terminal->start < 0) terminal->start = terminal->height + terminal->start;
592 for(i = 0; i < d; i++) {
593 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
594 attr_init(terminal_get_attr_row(terminal, i),
595 terminal->curr_attr, terminal->width);
598 for(i = terminal->height - d; i < terminal->height; i++) {
599 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
600 attr_init(terminal_get_attr_row(terminal, i),
601 terminal->curr_attr, terminal->width);
607 terminal_scroll_window(struct terminal *terminal, int d)
611 int from_row, to_row;
613 // scrolling range is inclusive
614 window_height = terminal->margin_bottom - terminal->margin_top + 1;
615 d = d % (window_height + 1);
618 to_row = terminal->margin_bottom;
619 from_row = terminal->margin_bottom - d;
621 for (i = 0; i < (window_height - d); i++) {
622 memcpy(terminal_get_row(terminal, to_row - i),
623 terminal_get_row(terminal, from_row - i),
624 terminal->data_pitch);
625 memcpy(terminal_get_attr_row(terminal, to_row - i),
626 terminal_get_attr_row(terminal, from_row - i),
627 terminal->attr_pitch);
629 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
630 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
631 attr_init(terminal_get_attr_row(terminal, i),
632 terminal->curr_attr, terminal->width);
635 to_row = terminal->margin_top;
636 from_row = terminal->margin_top + d;
638 for (i = 0; i < (window_height - d); i++) {
639 memcpy(terminal_get_row(terminal, to_row + i),
640 terminal_get_row(terminal, from_row + i),
641 terminal->data_pitch);
642 memcpy(terminal_get_attr_row(terminal, to_row + i),
643 terminal_get_attr_row(terminal, from_row + i),
644 terminal->attr_pitch);
646 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
647 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
648 attr_init(terminal_get_attr_row(terminal, i),
649 terminal->curr_attr, terminal->width);
655 terminal_scroll(struct terminal *terminal, int d)
657 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
658 terminal_scroll_buffer(terminal, d);
660 terminal_scroll_window(terminal, d);
664 terminal_shift_line(struct terminal *terminal, int d)
666 union utf8_char *row;
667 struct attr *attr_row;
669 row = terminal_get_row(terminal, terminal->row);
670 attr_row = terminal_get_attr_row(terminal, terminal->row);
672 if ((terminal->width + d) <= terminal->column)
673 d = terminal->column + 1 - terminal->width;
674 if ((terminal->column + d) >= terminal->width)
675 d = terminal->width - terminal->column - 1;
679 memmove(&row[terminal->column],
680 &row[terminal->column + d],
681 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
682 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
683 (terminal->width - terminal->column - d) * sizeof(struct attr));
684 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
685 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
687 memmove(&row[terminal->column + d], &row[terminal->column],
688 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
689 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
690 (terminal->width - terminal->column - d) * sizeof(struct attr));
691 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
692 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
697 terminal_resize_cells(struct terminal *terminal, int width, int height)
700 union utf8_char *data;
701 struct attr *data_attr;
703 int data_pitch, attr_pitch;
704 int i, l, total_rows;
705 struct rectangle allocation;
712 if (terminal->width == width && terminal->height == height)
715 data_pitch = width * sizeof(union utf8_char);
716 size = data_pitch * height;
718 attr_pitch = width * sizeof(struct attr);
719 data_attr = malloc(attr_pitch * height);
720 tab_ruler = malloc(width);
721 memset(data, 0, size);
722 memset(tab_ruler, 0, width);
723 attr_init(data_attr, terminal->curr_attr, width * height);
724 if (terminal->data && terminal->data_attr) {
725 if (width > terminal->width)
730 if (terminal->height > height) {
733 total_rows = terminal->height;
736 for (i = 0; i < total_rows; i++) {
737 memcpy(&data[width * i],
738 terminal_get_row(terminal, i),
739 l * sizeof(union utf8_char));
740 memcpy(&data_attr[width * i],
741 terminal_get_attr_row(terminal, i),
742 l * sizeof(struct attr));
745 free(terminal->data);
746 free(terminal->data_attr);
747 free(terminal->tab_ruler);
750 terminal->data_pitch = data_pitch;
751 terminal->attr_pitch = attr_pitch;
752 terminal->margin_bottom =
753 height - (terminal->height - terminal->margin_bottom);
754 terminal->width = width;
755 terminal->height = height;
756 terminal->data = data;
757 terminal->data_attr = data_attr;
758 terminal->tab_ruler = tab_ruler;
759 terminal_init_tabs(terminal);
761 /* Update the window size */
762 ws.ws_row = terminal->height;
763 ws.ws_col = terminal->width;
764 widget_get_allocation(terminal->widget, &allocation);
765 ws.ws_xpixel = allocation.width;
766 ws.ws_ypixel = allocation.height;
767 ioctl(terminal->master, TIOCSWINSZ, &ws);
771 resize_handler(struct widget *widget,
772 int32_t width, int32_t height, void *data)
774 struct terminal *terminal = data;
775 int32_t columns, rows, m;
777 m = 2 * terminal->margin;
778 columns = (width - m) / (int32_t) terminal->extents.max_x_advance;
779 rows = (height - m) / (int32_t) terminal->extents.height;
781 if (!terminal->fullscreen) {
782 width = columns * terminal->extents.max_x_advance + m;
783 height = rows * terminal->extents.height + m;
784 widget_set_size(terminal->widget, width, height);
787 terminal_resize_cells(terminal, columns, rows);
791 terminal_resize(struct terminal *terminal, int columns, int rows)
793 int32_t width, height, m;
795 if (terminal->fullscreen)
798 m = 2 * terminal->margin;
799 width = columns * terminal->extents.max_x_advance + m;
800 height = rows * terminal->extents.height + m;
801 widget_schedule_resize(terminal->widget, width, height);
804 struct color_scheme DEFAULT_COLORS = {
806 {0, 0, 0, 1}, /* black */
807 {0.66, 0, 0, 1}, /* red */
808 {0 , 0.66, 0, 1}, /* green */
809 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
810 {0 , 0 , 0.66, 1}, /* blue */
811 {0.66, 0 , 0.66, 1}, /* magenta */
812 {0, 0.66, 0.66, 1}, /* cyan */
813 {0.66, 0.66, 0.66, 1}, /* light grey */
814 {0.22, 0.33, 0.33, 1}, /* dark grey */
815 {1, 0.33, 0.33, 1}, /* high red */
816 {0.33, 1, 0.33, 1}, /* high green */
817 {1, 1, 0.33, 1}, /* high yellow */
818 {0.33, 0.33, 1, 1}, /* high blue */
819 {1, 0.33, 1, 1}, /* high magenta */
820 {0.33, 1, 1, 1}, /* high cyan */
821 {1, 1, 1, 1} /* white */
823 0, /* black border */
824 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
828 terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
830 cairo_set_source_rgba(cr,
831 terminal->color_table[index].r,
832 terminal->color_table[index].g,
833 terminal->color_table[index].b,
834 terminal->color_table[index].a);
838 terminal_send_selection(struct terminal *terminal, int fd)
841 union utf8_char *p_row;
842 union decoded_attr attr;
846 fp = fdopen(fd, "w");
847 for (row = 0; row < terminal->height; row++) {
848 p_row = terminal_get_row(terminal, row);
849 for (col = 0; col < terminal->width; col++) {
850 /* get the attributes for this character cell */
851 terminal_decode_attr(terminal, row, col, &attr);
854 len = strnlen((char *) p_row[col].byte, 4);
855 fwrite(p_row[col].byte, 1, len, fp);
862 struct terminal *terminal;
865 union decoded_attr attr;
866 cairo_glyph_t glyphs[256], *g;
870 glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
872 run->terminal = terminal;
874 run->g = run->glyphs;
880 glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
882 cairo_scaled_font_t *font;
884 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
885 (attr.key != run->attr.key)) {
886 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
887 font = run->terminal->font_bold;
889 font = run->terminal->font_normal;
890 cairo_set_scaled_font(run->cr, font);
891 terminal_set_color(run->terminal, run->cr,
894 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
895 cairo_show_glyphs (run->cr, run->glyphs, run->count);
896 run->g = run->glyphs;
903 glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
906 cairo_scaled_font_t *font;
908 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
910 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
911 font = run->terminal->font_bold;
913 font = run->terminal->font_normal;
915 cairo_move_to(run->cr, x, y);
916 cairo_scaled_font_text_to_glyphs (font, x, y,
918 &run->g, &num_glyphs,
920 run->g += num_glyphs;
921 run->count += num_glyphs;
926 redraw_handler(struct widget *widget, void *data)
928 struct terminal *terminal = data;
929 struct rectangle allocation;
931 int top_margin, side_margin;
933 union utf8_char *p_row;
934 union decoded_attr attr;
936 cairo_surface_t *surface;
938 struct glyph_run run;
939 cairo_font_extents_t extents;
941 surface = window_get_surface(terminal->window);
942 widget_get_allocation(terminal->widget, &allocation);
943 cr = cairo_create(surface);
944 cairo_rectangle(cr, allocation.x, allocation.y,
945 allocation.width, allocation.height);
947 cairo_push_group(cr);
949 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
950 terminal_set_color(terminal, cr, terminal->color_scheme->border);
953 cairo_set_scaled_font(cr, terminal->font_normal);
955 extents = terminal->extents;
956 side_margin = (allocation.width - terminal->width * extents.max_x_advance) / 2;
957 top_margin = (allocation.height - terminal->height * extents.height) / 2;
959 cairo_set_line_width(cr, 1.0);
960 cairo_translate(cr, allocation.x + side_margin,
961 allocation.y + top_margin);
962 /* paint the background */
963 for (row = 0; row < terminal->height; row++) {
964 for (col = 0; col < terminal->width; col++) {
965 /* get the attributes for this character cell */
966 terminal_decode_attr(terminal, row, col, &attr);
968 if (attr.attr.bg == terminal->color_scheme->border)
971 terminal_set_color(terminal, cr, attr.attr.bg);
972 cairo_move_to(cr, col * extents.max_x_advance,
973 row * extents.height);
974 cairo_rel_line_to(cr, extents.max_x_advance, 0);
975 cairo_rel_line_to(cr, 0, extents.height);
976 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
977 cairo_close_path(cr);
982 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
984 /* paint the foreground */
985 glyph_run_init(&run, terminal, cr);
986 for (row = 0; row < terminal->height; row++) {
987 p_row = terminal_get_row(terminal, row);
988 for (col = 0; col < terminal->width; col++) {
989 /* get the attributes for this character cell */
990 terminal_decode_attr(terminal, row, col, &attr);
992 glyph_run_flush(&run, attr);
994 text_x = col * extents.max_x_advance;
995 text_y = extents.ascent + row * extents.height;
996 if (attr.attr.a & ATTRMASK_UNDERLINE) {
997 terminal_set_color(terminal, cr, attr.attr.fg);
998 cairo_move_to(cr, text_x, (double)text_y + 1.5);
999 cairo_line_to(cr, text_x + extents.max_x_advance, (double) text_y + 1.5);
1003 glyph_run_add(&run, text_x, text_y, &p_row[col]);
1008 glyph_run_flush(&run, attr);
1010 if ((terminal->mode & MODE_SHOW_CURSOR) && !terminal->focused) {
1013 cairo_set_line_width(cr, 1);
1014 cairo_move_to(cr, terminal->column * extents.max_x_advance + d,
1015 terminal->row * extents.height + d);
1016 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
1017 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
1018 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
1019 cairo_close_path(cr);
1024 cairo_pop_group_to_source(cr);
1027 cairo_surface_destroy(surface);
1031 terminal_write(struct terminal *terminal, const char *data, size_t length)
1033 if (write(terminal->master, data, length) < 0)
1038 terminal_data(struct terminal *terminal, const char *data, size_t length);
1041 handle_char(struct terminal *terminal, union utf8_char utf8);
1044 handle_sgr(struct terminal *terminal, int code);
1047 handle_term_parameter(struct terminal *terminal, int code, int sr)
1051 if (terminal->escape_flags & ESC_FLAG_WHAT) {
1053 case 1: /* DECCKM */
1054 if (sr) terminal->key_mode = KM_APPLICATION;
1055 else terminal->key_mode = KM_NORMAL;
1057 case 2: /* DECANM */
1058 /* No VT52 support yet */
1059 terminal->g0 = CS_US;
1060 terminal->g1 = CS_US;
1061 terminal->cs = terminal->g0;
1063 case 3: /* DECCOLM */
1065 terminal_resize(terminal, 132, 24);
1067 terminal_resize(terminal, 80, 24);
1069 /* set columns, but also home cursor and clear screen */
1070 terminal->row = 0; terminal->column = 0;
1071 for (i = 0; i < terminal->height; i++) {
1072 memset(terminal_get_row(terminal, i),
1073 0, terminal->data_pitch);
1074 attr_init(terminal_get_attr_row(terminal, i),
1075 terminal->curr_attr, terminal->width);
1078 case 5: /* DECSCNM */
1079 if (sr) terminal->mode |= MODE_INVERSE;
1080 else terminal->mode &= ~MODE_INVERSE;
1083 terminal->origin_mode = sr;
1084 if (terminal->origin_mode)
1085 terminal->row = terminal->margin_top;
1088 terminal->column = 0;
1090 case 7: /* DECAWM */
1091 if (sr) terminal->mode |= MODE_AUTOWRAP;
1092 else terminal->mode &= ~MODE_AUTOWRAP;
1094 case 8: /* DECARM */
1095 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1096 else terminal->mode &= ~MODE_AUTOREPEAT;
1099 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1100 else terminal->mode &= ~MODE_SHOW_CURSOR;
1102 case 1037: /* deleteSendsDel */
1103 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1104 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1106 case 1039: /* altSendsEscape */
1107 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1108 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1111 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1117 if (sr) terminal->mode |= MODE_IRM;
1118 else terminal->mode &= ~MODE_IRM;
1121 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1122 else terminal->mode &= ~MODE_LF_NEWLINE;
1125 fprintf(stderr, "Unknown parameter: %d\n", code);
1132 handle_dcs(struct terminal *terminal)
1137 handle_osc(struct terminal *terminal)
1142 terminal->escape[terminal->escape_length++] = '\0';
1143 p = &terminal->escape[2];
1144 code = strtol(p, &p, 10);
1148 case 0: /* Icon name and window title */
1149 case 1: /* Icon label */
1150 case 2: /* Window title*/
1151 window_set_title(terminal->window, p);
1154 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1160 handle_escape(struct terminal *terminal)
1162 union utf8_char *row;
1163 struct attr *attr_row;
1165 int i, count, x, y, top, bottom;
1166 int args[10], set[10] = { 0, };
1167 char response[MAX_RESPONSE] = {0, };
1168 struct rectangle allocation;
1170 terminal->escape[terminal->escape_length++] = '\0';
1172 p = &terminal->escape[2];
1173 while ((isdigit(*p) || *p == ';') && i < 10) {
1182 args[i] = strtol(p, &p, 10);
1189 count = set[0] ? args[0] : 1;
1190 if (count == 0) count = 1;
1191 terminal_shift_line(terminal, count);
1194 count = set[0] ? args[0] : 1;
1195 if (count == 0) count = 1;
1196 if (terminal->row - count >= terminal->margin_top)
1197 terminal->row -= count;
1199 terminal->row = terminal->margin_top;
1202 count = set[0] ? args[0] : 1;
1203 if (count == 0) count = 1;
1204 if (terminal->row + count <= terminal->margin_bottom)
1205 terminal->row += count;
1207 terminal->row = terminal->margin_bottom;
1210 count = set[0] ? args[0] : 1;
1211 if (count == 0) count = 1;
1212 if ((terminal->column + count) < terminal->width)
1213 terminal->column += count;
1215 terminal->column = terminal->width - 1;
1218 count = set[0] ? args[0] : 1;
1219 if (count == 0) count = 1;
1220 if ((terminal->column - count) >= 0)
1221 terminal->column -= count;
1223 terminal->column = 0;
1226 count = set[0] ? args[0] : 1;
1227 if (terminal->row + count <= terminal->margin_bottom)
1228 terminal->row += count;
1230 terminal->row = terminal->margin_bottom;
1231 terminal->column = 0;
1234 count = set[0] ? args[0] : 1;
1235 if (terminal->row - count >= terminal->margin_top)
1236 terminal->row -= count;
1238 terminal->row = terminal->margin_top;
1239 terminal->column = 0;
1242 y = set[0] ? args[0] : 1;
1243 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1245 terminal->column = y - 1;
1249 x = (set[1] ? args[1] : 1) - 1;
1251 (x >= terminal->width ? terminal->width - 1 : x);
1253 y = (set[0] ? args[0] : 1) - 1;
1254 if (terminal->origin_mode) {
1255 y += terminal->margin_top;
1256 y = y < terminal->margin_top ? terminal->margin_top :
1257 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1260 (y >= terminal->height ? terminal->height - 1 : y);
1264 terminal->column = x;
1267 count = set[0] ? args[0] : 1;
1268 if (count == 0) count = 1;
1269 while (count > 0 && terminal->column < terminal->width) {
1270 if (terminal->tab_ruler[terminal->column]) count--;
1276 row = terminal_get_row(terminal, terminal->row);
1277 attr_row = terminal_get_attr_row(terminal, terminal->row);
1278 if (!set[0] || args[0] == 0 || args[0] > 2) {
1279 memset(&row[terminal->column],
1280 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1281 attr_init(&attr_row[terminal->column],
1282 terminal->curr_attr, terminal->width - terminal->column);
1283 for (i = terminal->row + 1; i < terminal->height; i++) {
1284 memset(terminal_get_row(terminal, i),
1285 0, terminal->data_pitch);
1286 attr_init(terminal_get_attr_row(terminal, i),
1287 terminal->curr_attr, terminal->width);
1289 } else if (args[0] == 1) {
1290 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1291 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1292 for (i = 0; i < terminal->row; i++) {
1293 memset(terminal_get_row(terminal, i),
1294 0, terminal->data_pitch);
1295 attr_init(terminal_get_attr_row(terminal, i),
1296 terminal->curr_attr, terminal->width);
1298 } else if (args[0] == 2) {
1299 for (i = 0; i < terminal->height; i++) {
1300 memset(terminal_get_row(terminal, i),
1301 0, terminal->data_pitch);
1302 attr_init(terminal_get_attr_row(terminal, i),
1303 terminal->curr_attr, terminal->width);
1308 row = terminal_get_row(terminal, terminal->row);
1309 attr_row = terminal_get_attr_row(terminal, terminal->row);
1310 if (!set[0] || args[0] == 0 || args[0] > 2) {
1311 memset(&row[terminal->column], 0,
1312 (terminal->width - terminal->column) * sizeof(union utf8_char));
1313 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1314 terminal->width - terminal->column);
1315 } else if (args[0] == 1) {
1316 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1317 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1318 } else if (args[0] == 2) {
1319 memset(row, 0, terminal->data_pitch);
1320 attr_init(attr_row, terminal->curr_attr, terminal->width);
1324 count = set[0] ? args[0] : 1;
1325 if (count == 0) count = 1;
1326 if (terminal->row >= terminal->margin_top &&
1327 terminal->row < terminal->margin_bottom)
1329 top = terminal->margin_top;
1330 terminal->margin_top = terminal->row;
1331 terminal_scroll(terminal, 0 - count);
1332 terminal->margin_top = top;
1333 } else if (terminal->row == terminal->margin_bottom) {
1334 memset(terminal_get_row(terminal, terminal->row),
1335 0, terminal->data_pitch);
1336 attr_init(terminal_get_attr_row(terminal, terminal->row),
1337 terminal->curr_attr, terminal->width);
1341 count = set[0] ? args[0] : 1;
1342 if (count == 0) count = 1;
1343 if (terminal->row >= terminal->margin_top &&
1344 terminal->row < terminal->margin_bottom)
1346 top = terminal->margin_top;
1347 terminal->margin_top = terminal->row;
1348 terminal_scroll(terminal, count);
1349 terminal->margin_top = top;
1350 } else if (terminal->row == terminal->margin_bottom) {
1351 memset(terminal_get_row(terminal, terminal->row),
1352 0, terminal->data_pitch);
1356 count = set[0] ? args[0] : 1;
1357 if (count == 0) count = 1;
1358 terminal_shift_line(terminal, 0 - count);
1361 terminal_scroll(terminal, set[0] ? args[0] : 1);
1364 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1367 count = set[0] ? args[0] : 1;
1368 if (count == 0) count = 1;
1369 if ((terminal->column + count) > terminal->width)
1370 count = terminal->width - terminal->column;
1371 row = terminal_get_row(terminal, terminal->row);
1372 attr_row = terminal_get_attr_row(terminal, terminal->row);
1373 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1374 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1377 count = set[0] ? args[0] : 1;
1378 if (count == 0) count = 1;
1379 while (count > 0 && terminal->column >= 0) {
1380 if (terminal->tab_ruler[terminal->column]) count--;
1386 y = set[0] ? args[0] : 1;
1387 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1389 terminal->column = y - 1;
1392 count = set[0] ? args[0] : 1;
1393 if (count == 0) count = 1;
1394 if (terminal->last_char.byte[0])
1395 for (i = 0; i < count; i++)
1396 handle_char(terminal, terminal->last_char);
1397 terminal->last_char.byte[0] = 0;
1399 case 'c': /* Primary DA */
1400 terminal_write(terminal, "\e[?6c", 5);
1403 x = set[0] ? args[0] : 1;
1404 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1406 terminal->row = x - 1;
1409 if (!set[0] || args[0] == 0) {
1410 terminal->tab_ruler[terminal->column] = 0;
1411 } else if (args[0] == 3) {
1412 memset(terminal->tab_ruler, 0, terminal->width);
1416 for(i = 0; i < 10 && set[i]; i++) {
1417 handle_term_parameter(terminal, args[i], 1);
1421 for(i = 0; i < 10 && set[i]; i++) {
1422 handle_term_parameter(terminal, args[i], 0);
1426 for(i = 0; i < 10; i++) {
1427 if (i <= 7 && set[i] && set[i + 1] &&
1428 set[i + 2] && args[i + 1] == 5)
1430 if (args[i] == 38) {
1431 handle_sgr(terminal, args[i + 2] + 256);
1433 } else if (args[i] == 48) {
1434 handle_sgr(terminal, args[i + 2] + 512);
1439 handle_sgr(terminal, args[i]);
1441 handle_sgr(terminal, 0);
1449 i = set[0] ? args[0] : 0;
1450 if (i == 0 || i == 5) {
1451 terminal_write(terminal, "\e[0n", 4);
1452 } else if (i == 6) {
1453 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1454 terminal->origin_mode ?
1455 terminal->row+terminal->margin_top : terminal->row+1,
1456 terminal->column+1);
1457 terminal_write(terminal, response, strlen(response));
1462 terminal->margin_top = 0;
1463 terminal->margin_bottom = terminal->height-1;
1465 terminal->column = 0;
1467 top = (set[0] ? args[0] : 1) - 1;
1469 (top >= terminal->height ? terminal->height - 1 : top);
1470 bottom = (set[1] ? args[1] : 1) - 1;
1471 bottom = bottom < 0 ? 0 :
1472 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1474 terminal->margin_top = top;
1475 terminal->margin_bottom = bottom;
1477 terminal->margin_top = 0;
1478 terminal->margin_bottom = terminal->height-1;
1480 if(terminal->origin_mode)
1481 terminal->row = terminal->margin_top;
1484 terminal->column = 0;
1488 terminal->saved_row = terminal->row;
1489 terminal->saved_column = terminal->column;
1491 case 't': /* windowOps */
1494 case 4: /* resize px */
1495 if (set[1] && set[2]) {
1496 widget_schedule_resize(terminal->widget,
1500 case 8: /* resize ch */
1501 if (set[1] && set[2]) {
1502 terminal_resize(terminal, args[2], args[1]);
1505 case 13: /* report position */
1506 widget_get_allocation(terminal->widget, &allocation);
1507 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1508 allocation.x, allocation.y);
1509 terminal_write(terminal, response, strlen(response));
1511 case 14: /* report px */
1512 widget_get_allocation(terminal->widget, &allocation);
1513 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1514 allocation.height, allocation.width);
1515 terminal_write(terminal, response, strlen(response));
1517 case 18: /* report ch */
1518 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1519 terminal->height, terminal->width);
1520 terminal_write(terminal, response, strlen(response));
1522 case 21: /* report title */
1523 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1524 window_get_title(terminal->window));
1525 terminal_write(terminal, response, strlen(response));
1529 terminal_resize(terminal, terminal->width, args[0]);
1531 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1535 terminal->row = terminal->saved_row;
1536 terminal->column = terminal->saved_column;
1539 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
1545 handle_non_csi_escape(struct terminal *terminal, char code)
1550 if(terminal->row < terminal->margin_top) {
1551 terminal->row = terminal->margin_top;
1552 terminal_scroll(terminal, -1);
1556 terminal->column = 0;
1560 if(terminal->row > terminal->margin_bottom) {
1561 terminal->row = terminal->margin_bottom;
1562 terminal_scroll(terminal, +1);
1566 terminal_init(terminal);
1569 terminal->tab_ruler[terminal->column] = 1;
1571 case '7': /* DECSC */
1572 terminal->saved_row = terminal->row;
1573 terminal->saved_column = terminal->column;
1574 terminal->saved_attr = terminal->curr_attr;
1575 terminal->saved_origin_mode = terminal->origin_mode;
1576 terminal->saved_cs = terminal->cs;
1577 terminal->saved_g0 = terminal->g0;
1578 terminal->saved_g1 = terminal->g1;
1580 case '8': /* DECRC */
1581 terminal->row = terminal->saved_row;
1582 terminal->column = terminal->saved_column;
1583 terminal->curr_attr = terminal->saved_attr;
1584 terminal->origin_mode = terminal->saved_origin_mode;
1585 terminal->cs = terminal->saved_cs;
1586 terminal->g0 = terminal->saved_g0;
1587 terminal->g1 = terminal->saved_g1;
1589 case '=': /* DECPAM */
1590 terminal->key_mode = KM_APPLICATION;
1592 case '>': /* DECPNM */
1593 terminal->key_mode = KM_NORMAL;
1596 fprintf(stderr, "Unknown escape code: %c\n", code);
1602 handle_special_escape(struct terminal *terminal, char special, char code)
1606 if (special == '#') {
1609 /* fill with 'E', no cheap way to do this */
1610 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1611 numChars = terminal->width * terminal->height;
1612 for(i = 0; i < numChars; i++) {
1613 terminal->data[i].byte[0] = 'E';
1617 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1620 } else if (special == '(' || special == ')') {
1624 terminal->g0 = CS_SPECIAL;
1626 terminal->g1 = CS_SPECIAL;
1630 terminal->g0 = CS_UK;
1632 terminal->g1 = CS_UK;
1636 terminal->g0 = CS_US;
1638 terminal->g1 = CS_US;
1641 fprintf(stderr, "Unknown character set %c\n", code);
1645 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1650 handle_sgr(struct terminal *terminal, int code)
1654 terminal->curr_attr = terminal->color_scheme->default_attr;
1657 terminal->curr_attr.a |= ATTRMASK_BOLD;
1658 if (terminal->curr_attr.fg < 8)
1659 terminal->curr_attr.fg += 8;
1662 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1665 terminal->curr_attr.a |= ATTRMASK_BLINK;
1668 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1673 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1674 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1675 terminal->curr_attr.fg -= 8;
1678 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1681 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1685 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1688 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1691 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1694 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1697 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1700 if(code >= 30 && code <= 37) {
1701 terminal->curr_attr.fg = code - 30;
1702 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1703 terminal->curr_attr.fg += 8;
1704 } else if(code >= 40 && code <= 47) {
1705 terminal->curr_attr.bg = code - 40;
1706 } else if (code >= 90 && code <= 97) {
1707 terminal->curr_attr.fg = code - 90 + 8;
1708 } else if (code >= 100 && code <= 107) {
1709 terminal->curr_attr.bg = code - 100 + 8;
1710 } else if(code >= 256 && code < 512) {
1711 terminal->curr_attr.fg = code - 256;
1712 } else if(code >= 512 && code < 768) {
1713 terminal->curr_attr.bg = code - 512;
1715 fprintf(stderr, "Unknown SGR code: %d\n", code);
1721 /* Returns 1 if c was special, otherwise 0 */
1723 handle_special_char(struct terminal *terminal, char c)
1725 union utf8_char *row;
1726 struct attr *attr_row;
1728 row = terminal_get_row(terminal, terminal->row);
1729 attr_row = terminal_get_attr_row(terminal, terminal->row);
1733 terminal->column = 0;
1736 if (terminal->mode & MODE_LF_NEWLINE) {
1737 terminal->column = 0;
1743 if(terminal->row > terminal->margin_bottom) {
1744 terminal->row = terminal->margin_bottom;
1745 terminal_scroll(terminal, +1);
1750 while (terminal->column < terminal->width) {
1751 if (terminal->tab_ruler[terminal->column]) break;
1752 if (terminal->mode & MODE_IRM)
1753 terminal_shift_line(terminal, +1);
1754 row[terminal->column].byte[0] = ' ';
1755 row[terminal->column].byte[1] = '\0';
1756 attr_row[terminal->column] = terminal->curr_attr;
1759 if (terminal->column >= terminal->width) {
1760 terminal->column = terminal->width - 1;
1765 if (terminal->column >= terminal->width) {
1766 terminal->column = terminal->width - 2;
1767 } else if (terminal->column > 0) {
1769 } else if (terminal->mode & MODE_AUTOWRAP) {
1770 terminal->column = terminal->width - 1;
1772 if (terminal->row < terminal->margin_top) {
1773 terminal->row = terminal->margin_top;
1774 terminal_scroll(terminal, -1);
1782 case '\x0E': /* SO */
1783 terminal->cs = terminal->g1;
1785 case '\x0F': /* SI */
1786 terminal->cs = terminal->g0;
1796 handle_char(struct terminal *terminal, union utf8_char utf8)
1798 union utf8_char *row;
1799 struct attr *attr_row;
1801 if (handle_special_char(terminal, utf8.byte[0])) return;
1803 apply_char_set(terminal->cs, &utf8);
1805 /* There are a whole lot of non-characters, control codes,
1806 * and formatting codes that should probably be ignored,
1808 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1813 /* Some of these non-characters should be translated, e.g.: */
1814 if (utf8.byte[0] < 32) {
1815 utf8.byte[0] = utf8.byte[0] + 64;
1818 /* handle right margin effects */
1819 if (terminal->column >= terminal->width) {
1820 if (terminal->mode & MODE_AUTOWRAP) {
1821 terminal->column = 0;
1823 if (terminal->row > terminal->margin_bottom) {
1824 terminal->row = terminal->margin_bottom;
1825 terminal_scroll(terminal, +1);
1832 row = terminal_get_row(terminal, terminal->row);
1833 attr_row = terminal_get_attr_row(terminal, terminal->row);
1835 if (terminal->mode & MODE_IRM)
1836 terminal_shift_line(terminal, +1);
1837 row[terminal->column] = utf8;
1838 attr_row[terminal->column++] = terminal->curr_attr;
1840 if (utf8.ch != terminal->last_char.ch)
1841 terminal->last_char = utf8;
1845 escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1849 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1850 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1851 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1852 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1853 else len = 1; /* Invalid, cannot happen */
1855 if (terminal->escape_length + len <= MAX_ESCAPE) {
1856 for (i = 0; i < len; i++)
1857 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1858 terminal->escape_length += len;
1859 } else if (terminal->escape_length < MAX_ESCAPE) {
1860 terminal->escape[terminal->escape_length++] = 0;
1865 terminal_data(struct terminal *terminal, const char *data, size_t length)
1868 union utf8_char utf8;
1869 enum utf8_state parser_state;
1871 for (i = 0; i < length; i++) {
1873 utf8_next_char(&terminal->state_machine, data[i]);
1874 switch(parser_state) {
1875 case utf8state_accept:
1876 utf8.ch = terminal->state_machine.s.ch;
1878 case utf8state_reject:
1879 /* the unicode replacement character */
1880 utf8.byte[0] = 0xEF;
1881 utf8.byte[1] = 0xBF;
1882 utf8.byte[2] = 0xBD;
1883 utf8.byte[3] = 0x00;
1889 /* assume escape codes never use non-ASCII characters */
1890 switch (terminal->state) {
1891 case escape_state_escape:
1892 escape_append_utf8(terminal, utf8);
1893 switch (utf8.byte[0]) {
1895 terminal->state = escape_state_dcs;
1898 terminal->state = escape_state_csi;
1901 terminal->state = escape_state_osc;
1905 case ')': /* special */
1906 terminal->state = escape_state_special;
1908 case '^': /* PM (not implemented) */
1909 case '_': /* APC (not implemented) */
1910 terminal->state = escape_state_ignore;
1913 terminal->state = escape_state_normal;
1914 handle_non_csi_escape(terminal, utf8.byte[0]);
1918 case escape_state_csi:
1919 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1921 } else if (utf8.byte[0] == '?') {
1922 terminal->escape_flags |= ESC_FLAG_WHAT;
1923 } else if (utf8.byte[0] == '>') {
1924 terminal->escape_flags |= ESC_FLAG_GT;
1925 } else if (utf8.byte[0] == '!') {
1926 terminal->escape_flags |= ESC_FLAG_BANG;
1927 } else if (utf8.byte[0] == '$') {
1928 terminal->escape_flags |= ESC_FLAG_CASH;
1929 } else if (utf8.byte[0] == '\'') {
1930 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1931 } else if (utf8.byte[0] == '"') {
1932 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1933 } else if (utf8.byte[0] == ' ') {
1934 terminal->escape_flags |= ESC_FLAG_SPACE;
1936 escape_append_utf8(terminal, utf8);
1937 if (terminal->escape_length >= MAX_ESCAPE)
1938 terminal->state = escape_state_normal;
1941 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1942 utf8.byte[0] == '`')
1944 terminal->state = escape_state_normal;
1945 handle_escape(terminal);
1949 case escape_state_inner_escape:
1950 if (utf8.byte[0] == '\\') {
1951 terminal->state = escape_state_normal;
1952 if (terminal->outer_state == escape_state_dcs) {
1953 handle_dcs(terminal);
1954 } else if (terminal->outer_state == escape_state_osc) {
1955 handle_osc(terminal);
1957 } else if (utf8.byte[0] == '\e') {
1958 terminal->state = terminal->outer_state;
1959 escape_append_utf8(terminal, utf8);
1960 if (terminal->escape_length >= MAX_ESCAPE)
1961 terminal->state = escape_state_normal;
1963 terminal->state = terminal->outer_state;
1964 if (terminal->escape_length < MAX_ESCAPE)
1965 terminal->escape[terminal->escape_length++] = '\e';
1966 escape_append_utf8(terminal, utf8);
1967 if (terminal->escape_length >= MAX_ESCAPE)
1968 terminal->state = escape_state_normal;
1971 case escape_state_dcs:
1972 case escape_state_osc:
1973 case escape_state_ignore:
1974 if (utf8.byte[0] == '\e') {
1975 terminal->outer_state = terminal->state;
1976 terminal->state = escape_state_inner_escape;
1977 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
1978 terminal->state = escape_state_normal;
1979 handle_osc(terminal);
1981 escape_append_utf8(terminal, utf8);
1982 if (terminal->escape_length >= MAX_ESCAPE)
1983 terminal->state = escape_state_normal;
1986 case escape_state_special:
1987 escape_append_utf8(terminal, utf8);
1988 terminal->state = escape_state_normal;
1989 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
1990 handle_special_escape(terminal, terminal->escape[1],
1998 /* this is valid, because ASCII characters are never used to
1999 * introduce a multibyte sequence in UTF-8 */
2000 if (utf8.byte[0] == '\e') {
2001 terminal->state = escape_state_escape;
2002 terminal->outer_state = escape_state_normal;
2003 terminal->escape[0] = '\e';
2004 terminal->escape_length = 1;
2005 terminal->escape_flags = 0;
2007 handle_char(terminal, utf8);
2011 window_schedule_redraw(terminal->window);
2015 data_source_target(void *data,
2016 struct wl_data_source *source, const char *mime_type)
2018 fprintf(stderr, "data_source_target, %s\n", mime_type);
2022 data_source_send(void *data,
2023 struct wl_data_source *source,
2024 const char *mime_type, int32_t fd)
2026 struct terminal *terminal = data;
2028 terminal_send_selection(terminal, fd);
2032 data_source_cancelled(void *data, struct wl_data_source *source)
2034 wl_data_source_destroy(source);
2037 static const struct wl_data_source_listener data_source_listener = {
2040 data_source_cancelled
2044 handle_bound_key(struct terminal *terminal,
2045 struct input *input, uint32_t sym, uint32_t time)
2049 /* Cut selection; terminal doesn't do cut, fall
2050 * through to copy. */
2052 terminal->selection =
2053 display_create_data_source(terminal->display);
2054 wl_data_source_offer(terminal->selection,
2055 "text/plain;charset=utf-8");
2056 wl_data_source_add_listener(terminal->selection,
2057 &data_source_listener, terminal);
2058 input_set_selection(input, terminal->selection, time);
2061 input_receive_selection_data_to_fd(input,
2062 "text/plain;charset=utf-8",
2072 key_handler(struct window *window, struct input *input, uint32_t time,
2073 uint32_t key, uint32_t sym, uint32_t state, void *data)
2075 struct terminal *terminal = data;
2076 char ch[MAX_RESPONSE];
2080 modifiers = input_get_modifiers(input);
2081 if ((modifiers & XKB_COMMON_CONTROL_MASK) &&
2082 (modifiers & XKB_COMMON_SHIFT_MASK) &&
2083 state && handle_bound_key(terminal, input, sym, time))
2090 terminal->fullscreen ^= 1;
2091 window_set_fullscreen(window, terminal->fullscreen);
2099 case XK_Scroll_Lock:
2102 ch[len++] = sym & 0x7f;
2106 if (terminal->mode & MODE_LF_NEWLINE) {
2123 len = function_key_response('[', 2, modifiers, '~', ch);
2126 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2129 len = function_key_response('[', 3, modifiers, '~', ch);
2133 len = function_key_response('[', 5, modifiers, '~', ch);
2136 len = function_key_response('[', 6, modifiers, '~', ch);
2139 len = function_key_response('O', 1, modifiers, 'P', ch);
2142 len = function_key_response('O', 1, modifiers, 'Q', ch);
2145 len = function_key_response('O', 1, modifiers, 'R', ch);
2148 len = function_key_response('O', 1, modifiers, 'S', ch);
2151 len = function_key_response('[', 15, modifiers, '~', ch);
2154 len = function_key_response('[', 17, modifiers, '~', ch);
2157 len = function_key_response('[', 18, modifiers, '~', ch);
2160 len = function_key_response('[', 19, modifiers, '~', ch);
2163 len = function_key_response('[', 20, modifiers, '~', ch);
2166 len = function_key_response('[', 21, modifiers, '~', ch);
2169 len = function_key_response('[', 24, modifiers, '~', ch);
2172 /* Handle special keys with alternate mappings */
2173 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2174 if (len != 0) break;
2176 if (modifiers & XKB_COMMON_CONTROL_MASK) {
2177 if (sym >= '3' && sym <= '7')
2178 sym = (sym & 0x1f) + 8;
2180 if (!((sym >= '!' && sym <= '/') ||
2181 (sym >= '8' && sym <= '?') ||
2182 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2183 else if (sym == '2') sym = 0x00;
2184 else if (sym == '/') sym = 0x1F;
2185 else if (sym == '8' || sym == '?') sym = 0x7F;
2186 } else if ((terminal->mode & MODE_ALT_SENDS_ESC) &&
2187 (modifiers & XKB_COMMON_MOD1_MASK))
2190 } else if (modifiers & XKB_COMMON_MOD1_MASK) {
2199 if (state && len > 0)
2200 terminal_write(terminal, ch, len);
2204 keyboard_focus_handler(struct window *window,
2205 struct input *device, void *data)
2207 struct terminal *terminal = data;
2209 terminal->focused = (device != NULL);
2210 window_schedule_redraw(terminal->window);
2214 button_handler(struct widget *widget,
2215 struct input *input, uint32_t time,
2216 uint32_t button, uint32_t state, void *data)
2218 struct terminal *terminal = data;
2223 terminal->dragging = 1;
2224 input_get_position(input,
2225 &terminal->selection_start_x,
2226 &terminal->selection_start_y);
2227 terminal->selection_end_x = terminal->selection_start_x;
2228 terminal->selection_end_y = terminal->selection_start_y;
2229 widget_schedule_redraw(widget);
2231 terminal->dragging = 0;
2238 motion_handler(struct widget *widget,
2239 struct input *input, uint32_t time,
2240 GLfloat x, GLfloat y, void *data)
2242 struct terminal *terminal = data;
2244 if (terminal->dragging) {
2245 input_get_position(input,
2246 &terminal->selection_end_x,
2247 &terminal->selection_end_y);
2248 widget_schedule_redraw(widget);
2251 return POINTER_IBEAM;
2254 static struct terminal *
2255 terminal_create(struct display *display, int fullscreen)
2257 struct terminal *terminal;
2258 cairo_surface_t *surface;
2261 terminal = malloc(sizeof *terminal);
2262 if (terminal == NULL)
2265 memset(terminal, 0, sizeof *terminal);
2266 terminal->fullscreen = fullscreen;
2267 terminal->color_scheme = &DEFAULT_COLORS;
2268 terminal_init(terminal);
2269 terminal->margin_top = 0;
2270 terminal->margin_bottom = -1;
2271 terminal->window = window_create(display);
2272 terminal->widget = frame_create(terminal->window, terminal);
2273 window_set_title(terminal->window, "Wayland Terminal");
2274 widget_set_transparent(terminal->widget, 0);
2276 init_state_machine(&terminal->state_machine);
2277 init_color_table(terminal);
2279 terminal->display = display;
2280 terminal->margin = 5;
2282 window_set_user_data(terminal->window, terminal);
2283 window_set_key_handler(terminal->window, key_handler);
2284 window_set_keyboard_focus_handler(terminal->window,
2285 keyboard_focus_handler);
2286 widget_set_redraw_handler(terminal->widget, redraw_handler);
2287 widget_set_resize_handler(terminal->widget, resize_handler);
2288 widget_set_button_handler(terminal->widget, button_handler);
2289 widget_set_motion_handler(terminal->widget, motion_handler);
2291 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2292 cr = cairo_create(surface);
2293 cairo_set_font_size(cr, 14);
2294 cairo_select_font_face (cr, "mono",
2295 CAIRO_FONT_SLANT_NORMAL,
2296 CAIRO_FONT_WEIGHT_BOLD);
2297 terminal->font_bold = cairo_get_scaled_font (cr);
2298 cairo_scaled_font_reference(terminal->font_bold);
2300 cairo_select_font_face (cr, "mono",
2301 CAIRO_FONT_SLANT_NORMAL,
2302 CAIRO_FONT_WEIGHT_NORMAL);
2303 terminal->font_normal = cairo_get_scaled_font (cr);
2304 cairo_scaled_font_reference(terminal->font_normal);
2306 cairo_font_extents(cr, &terminal->extents);
2308 cairo_surface_destroy(surface);
2310 window_schedule_resize(terminal->window, 500, 400);
2316 io_handler(struct task *task, uint32_t events)
2318 struct terminal *terminal =
2319 container_of(task, struct terminal, io_task);
2323 if (events & EPOLLHUP)
2326 len = read(terminal->master, buffer, sizeof buffer);
2330 terminal_data(terminal, buffer, len);
2334 terminal_run(struct terminal *terminal, const char *path)
2339 pid = forkpty(&master, NULL, NULL, NULL);
2341 setenv("TERM", "xterm-256color", 1);
2342 setenv("COLORTERM", "xterm-256color", 1);
2343 if (execl(path, path, NULL)) {
2344 printf("exec failed: %m\n");
2347 } else if (pid < 0) {
2348 fprintf(stderr, "failed to fork and create pty (%m).\n");
2352 terminal->master = master;
2353 fcntl(master, F_SETFL, O_NONBLOCK);
2354 terminal->io_task.run = io_handler;
2355 display_watch_fd(terminal->display, terminal->master,
2356 EPOLLIN | EPOLLHUP, &terminal->io_task);
2358 window_set_fullscreen(terminal->window, terminal->fullscreen);
2359 if (!terminal->fullscreen)
2360 terminal_resize(terminal, 80, 24);
2365 static const struct weston_option terminal_options[] = {
2366 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
2369 int main(int argc, char *argv[])
2372 struct terminal *terminal;
2375 argc = parse_options(terminal_options,
2376 ARRAY_LENGTH(terminal_options), argc, argv);
2378 d = display_create(argc, argv);
2380 fprintf(stderr, "failed to create display: %m\n");
2384 shell = getenv("SHELL");
2386 shell = "/bin/bash";
2388 terminal = terminal_create(d, option_fullscreen);
2389 if (terminal_run(terminal, shell))