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, 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 window_get_child_allocation(terminal->window, &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(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;
707 int32_t pixel_width, pixel_height;
713 if (terminal->width == width && terminal->height == height)
716 if (!terminal->fullscreen) {
717 pixel_width = width *
718 terminal->extents.max_x_advance + 2 * terminal->margin;
719 pixel_height = height *
720 terminal->extents.height + 2 * terminal->margin;
721 window_set_child_size(terminal->window,
722 pixel_width, pixel_height);
725 window_schedule_redraw (terminal->window);
727 data_pitch = width * sizeof(union utf8_char);
728 size = data_pitch * height;
730 attr_pitch = width * sizeof(struct attr);
731 data_attr = malloc(attr_pitch * height);
732 tab_ruler = malloc(width);
733 memset(data, 0, size);
734 memset(tab_ruler, 0, width);
735 attr_init(data_attr, terminal->curr_attr, width * height);
736 if (terminal->data && terminal->data_attr) {
737 if (width > terminal->width)
742 if (terminal->height > height) {
745 total_rows = terminal->height;
748 for (i = 0; i < total_rows; i++) {
749 memcpy(&data[width * i],
750 terminal_get_row(terminal, i),
751 l * sizeof(union utf8_char));
752 memcpy(&data_attr[width * i],
753 terminal_get_attr_row(terminal, i),
754 l * sizeof(struct attr));
757 free(terminal->data);
758 free(terminal->data_attr);
759 free(terminal->tab_ruler);
762 terminal->data_pitch = data_pitch;
763 terminal->attr_pitch = attr_pitch;
764 terminal->margin_bottom =
765 height - (terminal->height - terminal->margin_bottom);
766 terminal->width = width;
767 terminal->height = height;
768 terminal->data = data;
769 terminal->data_attr = data_attr;
770 terminal->tab_ruler = tab_ruler;
771 terminal_init_tabs(terminal);
773 /* Update the window size */
774 ws.ws_row = terminal->height;
775 ws.ws_col = terminal->width;
776 window_get_child_allocation(terminal->window, &allocation);
777 ws.ws_xpixel = allocation.width;
778 ws.ws_ypixel = allocation.height;
779 ioctl(terminal->master, TIOCSWINSZ, &ws);
782 struct color_scheme DEFAULT_COLORS = {
784 {0, 0, 0, 1}, /* black */
785 {0.66, 0, 0, 1}, /* red */
786 {0 , 0.66, 0, 1}, /* green */
787 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
788 {0 , 0 , 0.66, 1}, /* blue */
789 {0.66, 0 , 0.66, 1}, /* magenta */
790 {0, 0.66, 0.66, 1}, /* cyan */
791 {0.66, 0.66, 0.66, 1}, /* light grey */
792 {0.22, 0.33, 0.33, 1}, /* dark grey */
793 {1, 0.33, 0.33, 1}, /* high red */
794 {0.33, 1, 0.33, 1}, /* high green */
795 {1, 1, 0.33, 1}, /* high yellow */
796 {0.33, 0.33, 1, 1}, /* high blue */
797 {1, 0.33, 1, 1}, /* high magenta */
798 {0.33, 1, 1, 1}, /* high cyan */
799 {1, 1, 1, 1} /* white */
801 0, /* black border */
802 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
806 terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
808 cairo_set_source_rgba(cr,
809 terminal->color_table[index].r,
810 terminal->color_table[index].g,
811 terminal->color_table[index].b,
812 terminal->color_table[index].a);
816 terminal_send_selection(struct terminal *terminal, int fd)
819 union utf8_char *p_row;
820 union decoded_attr attr;
824 fp = fdopen(fd, "w");
825 for (row = 0; row < terminal->height; row++) {
826 p_row = terminal_get_row(terminal, row);
827 for (col = 0; col < terminal->width; col++) {
828 /* get the attributes for this character cell */
829 terminal_decode_attr(terminal, row, col, &attr);
832 len = strnlen((char *) p_row[col].byte, 4);
833 fwrite(p_row[col].byte, 1, len, fp);
840 struct terminal *terminal;
843 union decoded_attr attr;
844 cairo_glyph_t glyphs[256], *g;
848 glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
850 run->terminal = terminal;
852 run->g = run->glyphs;
858 glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
860 cairo_scaled_font_t *font;
862 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
863 (attr.key != run->attr.key)) {
864 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
865 font = run->terminal->font_bold;
867 font = run->terminal->font_normal;
868 cairo_set_scaled_font(run->cr, font);
869 terminal_set_color(run->terminal, run->cr,
872 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
873 cairo_show_glyphs (run->cr, run->glyphs, run->count);
874 run->g = run->glyphs;
881 glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
884 cairo_scaled_font_t *font;
886 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
888 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
889 font = run->terminal->font_bold;
891 font = run->terminal->font_normal;
893 cairo_move_to(run->cr, x, y);
894 cairo_scaled_font_text_to_glyphs (font, x, y,
896 &run->g, &num_glyphs,
898 run->g += num_glyphs;
899 run->count += num_glyphs;
903 terminal_draw_contents(struct terminal *terminal)
905 struct rectangle allocation;
907 int top_margin, side_margin;
909 union utf8_char *p_row;
910 union decoded_attr attr;
912 cairo_surface_t *surface;
914 struct glyph_run run;
915 cairo_font_extents_t extents;
917 surface = window_get_surface(terminal->window);
918 window_get_child_allocation(terminal->window, &allocation);
919 cr = cairo_create(surface);
920 cairo_rectangle(cr, allocation.x, allocation.y,
921 allocation.width, allocation.height);
923 cairo_push_group(cr);
925 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
926 terminal_set_color(terminal, cr, terminal->color_scheme->border);
929 cairo_set_scaled_font(cr, terminal->font_normal);
931 extents = terminal->extents;
932 side_margin = (allocation.width - terminal->width * extents.max_x_advance) / 2;
933 top_margin = (allocation.height - terminal->height * extents.height) / 2;
935 cairo_set_line_width(cr, 1.0);
936 cairo_translate(cr, allocation.x + side_margin,
937 allocation.y + top_margin);
938 /* paint the background */
939 for (row = 0; row < terminal->height; row++) {
940 for (col = 0; col < terminal->width; col++) {
941 /* get the attributes for this character cell */
942 terminal_decode_attr(terminal, row, col, &attr);
944 if (attr.attr.bg == terminal->color_scheme->border)
947 terminal_set_color(terminal, cr, attr.attr.bg);
948 cairo_move_to(cr, col * extents.max_x_advance,
949 row * extents.height);
950 cairo_rel_line_to(cr, extents.max_x_advance, 0);
951 cairo_rel_line_to(cr, 0, extents.height);
952 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
953 cairo_close_path(cr);
958 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
960 /* paint the foreground */
961 glyph_run_init(&run, terminal, cr);
962 for (row = 0; row < terminal->height; row++) {
963 p_row = terminal_get_row(terminal, 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 glyph_run_flush(&run, attr);
970 text_x = col * extents.max_x_advance;
971 text_y = extents.ascent + row * extents.height;
972 if (attr.attr.a & ATTRMASK_UNDERLINE) {
973 terminal_set_color(terminal, cr, attr.attr.fg);
974 cairo_move_to(cr, text_x, (double)text_y + 1.5);
975 cairo_line_to(cr, text_x + extents.max_x_advance, (double) text_y + 1.5);
979 glyph_run_add(&run, text_x, text_y, &p_row[col]);
984 glyph_run_flush(&run, attr);
986 if ((terminal->mode & MODE_SHOW_CURSOR) && !terminal->focused) {
989 cairo_set_line_width(cr, 1);
990 cairo_move_to(cr, terminal->column * extents.max_x_advance + d,
991 terminal->row * extents.height + d);
992 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
993 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
994 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
995 cairo_close_path(cr);
1000 cairo_pop_group_to_source(cr);
1003 cairo_surface_destroy(surface);
1007 terminal_write(struct terminal *terminal, const char *data, size_t length)
1009 if (write(terminal->master, data, length) < 0)
1014 resize_handler(struct window *window,
1015 int32_t pixel_width, int32_t pixel_height, void *data)
1017 struct terminal *terminal = data;
1018 int32_t width, height;
1020 width = (pixel_width - 2 * terminal->margin) /
1021 (int32_t) terminal->extents.max_x_advance;
1022 height = (pixel_height - 2 * terminal->margin) /
1023 (int32_t) terminal->extents.height;
1025 if (terminal->fullscreen)
1026 window_set_child_size(terminal->window,
1027 pixel_width, pixel_height);
1029 terminal_resize(terminal, width, height);
1033 redraw_handler(struct window *window, void *data)
1035 struct terminal *terminal = data;
1037 terminal_draw_contents(terminal);
1041 terminal_data(struct terminal *terminal, const char *data, size_t length);
1044 handle_char(struct terminal *terminal, union utf8_char utf8);
1047 handle_sgr(struct terminal *terminal, int code);
1050 handle_term_parameter(struct terminal *terminal, int code, int sr)
1054 if (terminal->escape_flags & ESC_FLAG_WHAT) {
1056 case 1: /* DECCKM */
1057 if (sr) terminal->key_mode = KM_APPLICATION;
1058 else terminal->key_mode = KM_NORMAL;
1060 case 2: /* DECANM */
1061 /* No VT52 support yet */
1062 terminal->g0 = CS_US;
1063 terminal->g1 = CS_US;
1064 terminal->cs = terminal->g0;
1066 case 3: /* DECCOLM */
1068 terminal_resize(terminal, 132, 24);
1070 terminal_resize(terminal, 80, 24);
1072 /* set columns, but also home cursor and clear screen */
1073 terminal->row = 0; terminal->column = 0;
1074 for (i = 0; i < terminal->height; i++) {
1075 memset(terminal_get_row(terminal, i),
1076 0, terminal->data_pitch);
1077 attr_init(terminal_get_attr_row(terminal, i),
1078 terminal->curr_attr, terminal->width);
1081 case 5: /* DECSCNM */
1082 if (sr) terminal->mode |= MODE_INVERSE;
1083 else terminal->mode &= ~MODE_INVERSE;
1086 terminal->origin_mode = sr;
1087 if (terminal->origin_mode)
1088 terminal->row = terminal->margin_top;
1091 terminal->column = 0;
1093 case 7: /* DECAWM */
1094 if (sr) terminal->mode |= MODE_AUTOWRAP;
1095 else terminal->mode &= ~MODE_AUTOWRAP;
1097 case 8: /* DECARM */
1098 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1099 else terminal->mode &= ~MODE_AUTOREPEAT;
1102 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1103 else terminal->mode &= ~MODE_SHOW_CURSOR;
1105 case 1037: /* deleteSendsDel */
1106 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1107 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1109 case 1039: /* altSendsEscape */
1110 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1111 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1114 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1120 if (sr) terminal->mode |= MODE_IRM;
1121 else terminal->mode &= ~MODE_IRM;
1124 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1125 else terminal->mode &= ~MODE_LF_NEWLINE;
1128 fprintf(stderr, "Unknown parameter: %d\n", code);
1135 handle_dcs(struct terminal *terminal)
1140 handle_osc(struct terminal *terminal)
1145 terminal->escape[terminal->escape_length++] = '\0';
1146 p = &terminal->escape[2];
1147 code = strtol(p, &p, 10);
1151 case 0: /* Icon name and window title */
1152 case 1: /* Icon label */
1153 case 2: /* Window title*/
1154 window_set_title(terminal->window, p);
1157 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1163 handle_escape(struct terminal *terminal)
1165 union utf8_char *row;
1166 struct attr *attr_row;
1168 int i, count, x, y, top, bottom;
1169 int args[10], set[10] = { 0, };
1170 char response[MAX_RESPONSE] = {0, };
1171 struct rectangle allocation;
1173 terminal->escape[terminal->escape_length++] = '\0';
1175 p = &terminal->escape[2];
1176 while ((isdigit(*p) || *p == ';') && i < 10) {
1185 args[i] = strtol(p, &p, 10);
1192 count = set[0] ? args[0] : 1;
1193 if (count == 0) count = 1;
1194 terminal_shift_line(terminal, count);
1197 count = set[0] ? args[0] : 1;
1198 if (count == 0) count = 1;
1199 if (terminal->row - count >= terminal->margin_top)
1200 terminal->row -= count;
1202 terminal->row = terminal->margin_top;
1205 count = set[0] ? args[0] : 1;
1206 if (count == 0) count = 1;
1207 if (terminal->row + count <= terminal->margin_bottom)
1208 terminal->row += count;
1210 terminal->row = terminal->margin_bottom;
1213 count = set[0] ? args[0] : 1;
1214 if (count == 0) count = 1;
1215 if ((terminal->column + count) < terminal->width)
1216 terminal->column += count;
1218 terminal->column = terminal->width - 1;
1221 count = set[0] ? args[0] : 1;
1222 if (count == 0) count = 1;
1223 if ((terminal->column - count) >= 0)
1224 terminal->column -= count;
1226 terminal->column = 0;
1229 count = set[0] ? args[0] : 1;
1230 if (terminal->row + count <= terminal->margin_bottom)
1231 terminal->row += count;
1233 terminal->row = terminal->margin_bottom;
1234 terminal->column = 0;
1237 count = set[0] ? args[0] : 1;
1238 if (terminal->row - count >= terminal->margin_top)
1239 terminal->row -= count;
1241 terminal->row = terminal->margin_top;
1242 terminal->column = 0;
1245 y = set[0] ? args[0] : 1;
1246 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1248 terminal->column = y - 1;
1252 x = (set[1] ? args[1] : 1) - 1;
1254 (x >= terminal->width ? terminal->width - 1 : x);
1256 y = (set[0] ? args[0] : 1) - 1;
1257 if (terminal->origin_mode) {
1258 y += terminal->margin_top;
1259 y = y < terminal->margin_top ? terminal->margin_top :
1260 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1263 (y >= terminal->height ? terminal->height - 1 : y);
1267 terminal->column = x;
1270 count = set[0] ? args[0] : 1;
1271 if (count == 0) count = 1;
1272 while (count > 0 && terminal->column < terminal->width) {
1273 if (terminal->tab_ruler[terminal->column]) count--;
1279 row = terminal_get_row(terminal, terminal->row);
1280 attr_row = terminal_get_attr_row(terminal, terminal->row);
1281 if (!set[0] || args[0] == 0 || args[0] > 2) {
1282 memset(&row[terminal->column],
1283 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1284 attr_init(&attr_row[terminal->column],
1285 terminal->curr_attr, terminal->width - terminal->column);
1286 for (i = terminal->row + 1; i < terminal->height; i++) {
1287 memset(terminal_get_row(terminal, i),
1288 0, terminal->data_pitch);
1289 attr_init(terminal_get_attr_row(terminal, i),
1290 terminal->curr_attr, terminal->width);
1292 } else if (args[0] == 1) {
1293 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1294 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1295 for (i = 0; i < terminal->row; i++) {
1296 memset(terminal_get_row(terminal, i),
1297 0, terminal->data_pitch);
1298 attr_init(terminal_get_attr_row(terminal, i),
1299 terminal->curr_attr, terminal->width);
1301 } else if (args[0] == 2) {
1302 for (i = 0; i < terminal->height; i++) {
1303 memset(terminal_get_row(terminal, i),
1304 0, terminal->data_pitch);
1305 attr_init(terminal_get_attr_row(terminal, i),
1306 terminal->curr_attr, terminal->width);
1311 row = terminal_get_row(terminal, terminal->row);
1312 attr_row = terminal_get_attr_row(terminal, terminal->row);
1313 if (!set[0] || args[0] == 0 || args[0] > 2) {
1314 memset(&row[terminal->column], 0,
1315 (terminal->width - terminal->column) * sizeof(union utf8_char));
1316 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1317 terminal->width - terminal->column);
1318 } else if (args[0] == 1) {
1319 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1320 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1321 } else if (args[0] == 2) {
1322 memset(row, 0, terminal->data_pitch);
1323 attr_init(attr_row, terminal->curr_attr, terminal->width);
1327 count = set[0] ? args[0] : 1;
1328 if (count == 0) count = 1;
1329 if (terminal->row >= terminal->margin_top &&
1330 terminal->row < terminal->margin_bottom)
1332 top = terminal->margin_top;
1333 terminal->margin_top = terminal->row;
1334 terminal_scroll(terminal, 0 - count);
1335 terminal->margin_top = top;
1336 } else if (terminal->row == terminal->margin_bottom) {
1337 memset(terminal_get_row(terminal, terminal->row),
1338 0, terminal->data_pitch);
1339 attr_init(terminal_get_attr_row(terminal, terminal->row),
1340 terminal->curr_attr, terminal->width);
1344 count = set[0] ? args[0] : 1;
1345 if (count == 0) count = 1;
1346 if (terminal->row >= terminal->margin_top &&
1347 terminal->row < terminal->margin_bottom)
1349 top = terminal->margin_top;
1350 terminal->margin_top = terminal->row;
1351 terminal_scroll(terminal, count);
1352 terminal->margin_top = top;
1353 } else if (terminal->row == terminal->margin_bottom) {
1354 memset(terminal_get_row(terminal, terminal->row),
1355 0, terminal->data_pitch);
1359 count = set[0] ? args[0] : 1;
1360 if (count == 0) count = 1;
1361 terminal_shift_line(terminal, 0 - count);
1364 terminal_scroll(terminal, set[0] ? args[0] : 1);
1367 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1370 count = set[0] ? args[0] : 1;
1371 if (count == 0) count = 1;
1372 if ((terminal->column + count) > terminal->width)
1373 count = terminal->width - terminal->column;
1374 row = terminal_get_row(terminal, terminal->row);
1375 attr_row = terminal_get_attr_row(terminal, terminal->row);
1376 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1377 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1380 count = set[0] ? args[0] : 1;
1381 if (count == 0) count = 1;
1382 while (count > 0 && terminal->column >= 0) {
1383 if (terminal->tab_ruler[terminal->column]) count--;
1389 y = set[0] ? args[0] : 1;
1390 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1392 terminal->column = y - 1;
1395 count = set[0] ? args[0] : 1;
1396 if (count == 0) count = 1;
1397 if (terminal->last_char.byte[0])
1398 for (i = 0; i < count; i++)
1399 handle_char(terminal, terminal->last_char);
1400 terminal->last_char.byte[0] = 0;
1402 case 'c': /* Primary DA */
1403 terminal_write(terminal, "\e[?6c", 5);
1406 x = set[0] ? args[0] : 1;
1407 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1409 terminal->row = x - 1;
1412 if (!set[0] || args[0] == 0) {
1413 terminal->tab_ruler[terminal->column] = 0;
1414 } else if (args[0] == 3) {
1415 memset(terminal->tab_ruler, 0, terminal->width);
1419 for(i = 0; i < 10 && set[i]; i++) {
1420 handle_term_parameter(terminal, args[i], 1);
1424 for(i = 0; i < 10 && set[i]; i++) {
1425 handle_term_parameter(terminal, args[i], 0);
1429 for(i = 0; i < 10; i++) {
1430 if (i <= 7 && set[i] && set[i + 1] &&
1431 set[i + 2] && args[i + 1] == 5)
1433 if (args[i] == 38) {
1434 handle_sgr(terminal, args[i + 2] + 256);
1436 } else if (args[i] == 48) {
1437 handle_sgr(terminal, args[i + 2] + 512);
1442 handle_sgr(terminal, args[i]);
1444 handle_sgr(terminal, 0);
1452 i = set[0] ? args[0] : 0;
1453 if (i == 0 || i == 5) {
1454 terminal_write(terminal, "\e[0n", 4);
1455 } else if (i == 6) {
1456 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1457 terminal->origin_mode ?
1458 terminal->row+terminal->margin_top : terminal->row+1,
1459 terminal->column+1);
1460 terminal_write(terminal, response, strlen(response));
1465 terminal->margin_top = 0;
1466 terminal->margin_bottom = terminal->height-1;
1468 terminal->column = 0;
1470 top = (set[0] ? args[0] : 1) - 1;
1472 (top >= terminal->height ? terminal->height - 1 : top);
1473 bottom = (set[1] ? args[1] : 1) - 1;
1474 bottom = bottom < 0 ? 0 :
1475 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1477 terminal->margin_top = top;
1478 terminal->margin_bottom = bottom;
1480 terminal->margin_top = 0;
1481 terminal->margin_bottom = terminal->height-1;
1483 if(terminal->origin_mode)
1484 terminal->row = terminal->margin_top;
1487 terminal->column = 0;
1491 terminal->saved_row = terminal->row;
1492 terminal->saved_column = terminal->column;
1494 case 't': /* windowOps */
1497 case 4: /* resize px */
1498 if (set[1] && set[2]) {
1499 window_set_child_size(terminal->window,
1501 resize_handler(terminal->window,
1502 args[2], args[1], terminal);
1505 case 8: /* resize ch */
1506 if (set[1] && set[2]) {
1507 terminal_resize(terminal, args[2], args[1]);
1510 case 13: /* report position */
1511 window_get_child_allocation(terminal->window, &allocation);
1512 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1513 allocation.x, allocation.y);
1514 terminal_write(terminal, response, strlen(response));
1516 case 14: /* report px */
1517 window_get_child_allocation(terminal->window, &allocation);
1518 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1519 allocation.height, allocation.width);
1520 terminal_write(terminal, response, strlen(response));
1522 case 18: /* report ch */
1523 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1524 terminal->height, terminal->width);
1525 terminal_write(terminal, response, strlen(response));
1527 case 21: /* report title */
1528 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1529 window_get_title(terminal->window));
1530 terminal_write(terminal, response, strlen(response));
1534 terminal_resize(terminal, terminal->width, args[0]);
1536 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1540 terminal->row = terminal->saved_row;
1541 terminal->column = terminal->saved_column;
1544 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
1550 handle_non_csi_escape(struct terminal *terminal, char code)
1555 if(terminal->row < terminal->margin_top) {
1556 terminal->row = terminal->margin_top;
1557 terminal_scroll(terminal, -1);
1561 terminal->column = 0;
1565 if(terminal->row > terminal->margin_bottom) {
1566 terminal->row = terminal->margin_bottom;
1567 terminal_scroll(terminal, +1);
1571 terminal_init(terminal);
1574 terminal->tab_ruler[terminal->column] = 1;
1576 case '7': /* DECSC */
1577 terminal->saved_row = terminal->row;
1578 terminal->saved_column = terminal->column;
1579 terminal->saved_attr = terminal->curr_attr;
1580 terminal->saved_origin_mode = terminal->origin_mode;
1581 terminal->saved_cs = terminal->cs;
1582 terminal->saved_g0 = terminal->g0;
1583 terminal->saved_g1 = terminal->g1;
1585 case '8': /* DECRC */
1586 terminal->row = terminal->saved_row;
1587 terminal->column = terminal->saved_column;
1588 terminal->curr_attr = terminal->saved_attr;
1589 terminal->origin_mode = terminal->saved_origin_mode;
1590 terminal->cs = terminal->saved_cs;
1591 terminal->g0 = terminal->saved_g0;
1592 terminal->g1 = terminal->saved_g1;
1594 case '=': /* DECPAM */
1595 terminal->key_mode = KM_APPLICATION;
1597 case '>': /* DECPNM */
1598 terminal->key_mode = KM_NORMAL;
1601 fprintf(stderr, "Unknown escape code: %c\n", code);
1607 handle_special_escape(struct terminal *terminal, char special, char code)
1611 if (special == '#') {
1614 /* fill with 'E', no cheap way to do this */
1615 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1616 numChars = terminal->width * terminal->height;
1617 for(i = 0; i < numChars; i++) {
1618 terminal->data[i].byte[0] = 'E';
1622 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1625 } else if (special == '(' || special == ')') {
1629 terminal->g0 = CS_SPECIAL;
1631 terminal->g1 = CS_SPECIAL;
1635 terminal->g0 = CS_UK;
1637 terminal->g1 = CS_UK;
1641 terminal->g0 = CS_US;
1643 terminal->g1 = CS_US;
1646 fprintf(stderr, "Unknown character set %c\n", code);
1650 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1655 handle_sgr(struct terminal *terminal, int code)
1659 terminal->curr_attr = terminal->color_scheme->default_attr;
1662 terminal->curr_attr.a |= ATTRMASK_BOLD;
1663 if (terminal->curr_attr.fg < 8)
1664 terminal->curr_attr.fg += 8;
1667 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1670 terminal->curr_attr.a |= ATTRMASK_BLINK;
1673 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1678 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1679 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1680 terminal->curr_attr.fg -= 8;
1683 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1686 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1690 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1693 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1696 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1699 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1702 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1705 if(code >= 30 && code <= 37) {
1706 terminal->curr_attr.fg = code - 30;
1707 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1708 terminal->curr_attr.fg += 8;
1709 } else if(code >= 40 && code <= 47) {
1710 terminal->curr_attr.bg = code - 40;
1711 } else if (code >= 90 && code <= 97) {
1712 terminal->curr_attr.fg = code - 90 + 8;
1713 } else if (code >= 100 && code <= 107) {
1714 terminal->curr_attr.bg = code - 100 + 8;
1715 } else if(code >= 256 && code < 512) {
1716 terminal->curr_attr.fg = code - 256;
1717 } else if(code >= 512 && code < 768) {
1718 terminal->curr_attr.bg = code - 512;
1720 fprintf(stderr, "Unknown SGR code: %d\n", code);
1726 /* Returns 1 if c was special, otherwise 0 */
1728 handle_special_char(struct terminal *terminal, char c)
1730 union utf8_char *row;
1731 struct attr *attr_row;
1733 row = terminal_get_row(terminal, terminal->row);
1734 attr_row = terminal_get_attr_row(terminal, terminal->row);
1738 terminal->column = 0;
1741 if (terminal->mode & MODE_LF_NEWLINE) {
1742 terminal->column = 0;
1748 if(terminal->row > terminal->margin_bottom) {
1749 terminal->row = terminal->margin_bottom;
1750 terminal_scroll(terminal, +1);
1755 while (terminal->column < terminal->width) {
1756 if (terminal->tab_ruler[terminal->column]) break;
1757 if (terminal->mode & MODE_IRM)
1758 terminal_shift_line(terminal, +1);
1759 row[terminal->column].byte[0] = ' ';
1760 row[terminal->column].byte[1] = '\0';
1761 attr_row[terminal->column] = terminal->curr_attr;
1764 if (terminal->column >= terminal->width) {
1765 terminal->column = terminal->width - 1;
1770 if (terminal->column >= terminal->width) {
1771 terminal->column = terminal->width - 2;
1772 } else if (terminal->column > 0) {
1774 } else if (terminal->mode & MODE_AUTOWRAP) {
1775 terminal->column = terminal->width - 1;
1777 if (terminal->row < terminal->margin_top) {
1778 terminal->row = terminal->margin_top;
1779 terminal_scroll(terminal, -1);
1787 case '\x0E': /* SO */
1788 terminal->cs = terminal->g1;
1790 case '\x0F': /* SI */
1791 terminal->cs = terminal->g0;
1801 handle_char(struct terminal *terminal, union utf8_char utf8)
1803 union utf8_char *row;
1804 struct attr *attr_row;
1806 if (handle_special_char(terminal, utf8.byte[0])) return;
1808 apply_char_set(terminal->cs, &utf8);
1810 /* There are a whole lot of non-characters, control codes,
1811 * and formatting codes that should probably be ignored,
1813 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1818 /* Some of these non-characters should be translated, e.g.: */
1819 if (utf8.byte[0] < 32) {
1820 utf8.byte[0] = utf8.byte[0] + 64;
1823 /* handle right margin effects */
1824 if (terminal->column >= terminal->width) {
1825 if (terminal->mode & MODE_AUTOWRAP) {
1826 terminal->column = 0;
1828 if (terminal->row > terminal->margin_bottom) {
1829 terminal->row = terminal->margin_bottom;
1830 terminal_scroll(terminal, +1);
1837 row = terminal_get_row(terminal, terminal->row);
1838 attr_row = terminal_get_attr_row(terminal, terminal->row);
1840 if (terminal->mode & MODE_IRM)
1841 terminal_shift_line(terminal, +1);
1842 row[terminal->column] = utf8;
1843 attr_row[terminal->column++] = terminal->curr_attr;
1845 if (utf8.ch != terminal->last_char.ch)
1846 terminal->last_char = utf8;
1850 escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1854 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1855 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1856 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1857 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1858 else len = 1; /* Invalid, cannot happen */
1860 if (terminal->escape_length + len <= MAX_ESCAPE) {
1861 for (i = 0; i < len; i++)
1862 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1863 terminal->escape_length += len;
1864 } else if (terminal->escape_length < MAX_ESCAPE) {
1865 terminal->escape[terminal->escape_length++] = 0;
1870 terminal_data(struct terminal *terminal, const char *data, size_t length)
1873 union utf8_char utf8;
1874 enum utf8_state parser_state;
1876 for (i = 0; i < length; i++) {
1878 utf8_next_char(&terminal->state_machine, data[i]);
1879 switch(parser_state) {
1880 case utf8state_accept:
1881 utf8.ch = terminal->state_machine.s.ch;
1883 case utf8state_reject:
1884 /* the unicode replacement character */
1885 utf8.byte[0] = 0xEF;
1886 utf8.byte[1] = 0xBF;
1887 utf8.byte[2] = 0xBD;
1888 utf8.byte[3] = 0x00;
1894 /* assume escape codes never use non-ASCII characters */
1895 switch (terminal->state) {
1896 case escape_state_escape:
1897 escape_append_utf8(terminal, utf8);
1898 switch (utf8.byte[0]) {
1900 terminal->state = escape_state_dcs;
1903 terminal->state = escape_state_csi;
1906 terminal->state = escape_state_osc;
1910 case ')': /* special */
1911 terminal->state = escape_state_special;
1913 case '^': /* PM (not implemented) */
1914 case '_': /* APC (not implemented) */
1915 terminal->state = escape_state_ignore;
1918 terminal->state = escape_state_normal;
1919 handle_non_csi_escape(terminal, utf8.byte[0]);
1923 case escape_state_csi:
1924 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1926 } else if (utf8.byte[0] == '?') {
1927 terminal->escape_flags |= ESC_FLAG_WHAT;
1928 } else if (utf8.byte[0] == '>') {
1929 terminal->escape_flags |= ESC_FLAG_GT;
1930 } else if (utf8.byte[0] == '!') {
1931 terminal->escape_flags |= ESC_FLAG_BANG;
1932 } else if (utf8.byte[0] == '$') {
1933 terminal->escape_flags |= ESC_FLAG_CASH;
1934 } else if (utf8.byte[0] == '\'') {
1935 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1936 } else if (utf8.byte[0] == '"') {
1937 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1938 } else if (utf8.byte[0] == ' ') {
1939 terminal->escape_flags |= ESC_FLAG_SPACE;
1941 escape_append_utf8(terminal, utf8);
1942 if (terminal->escape_length >= MAX_ESCAPE)
1943 terminal->state = escape_state_normal;
1946 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1947 utf8.byte[0] == '`')
1949 terminal->state = escape_state_normal;
1950 handle_escape(terminal);
1954 case escape_state_inner_escape:
1955 if (utf8.byte[0] == '\\') {
1956 terminal->state = escape_state_normal;
1957 if (terminal->outer_state == escape_state_dcs) {
1958 handle_dcs(terminal);
1959 } else if (terminal->outer_state == escape_state_osc) {
1960 handle_osc(terminal);
1962 } else if (utf8.byte[0] == '\e') {
1963 terminal->state = terminal->outer_state;
1964 escape_append_utf8(terminal, utf8);
1965 if (terminal->escape_length >= MAX_ESCAPE)
1966 terminal->state = escape_state_normal;
1968 terminal->state = terminal->outer_state;
1969 if (terminal->escape_length < MAX_ESCAPE)
1970 terminal->escape[terminal->escape_length++] = '\e';
1971 escape_append_utf8(terminal, utf8);
1972 if (terminal->escape_length >= MAX_ESCAPE)
1973 terminal->state = escape_state_normal;
1976 case escape_state_dcs:
1977 case escape_state_osc:
1978 case escape_state_ignore:
1979 if (utf8.byte[0] == '\e') {
1980 terminal->outer_state = terminal->state;
1981 terminal->state = escape_state_inner_escape;
1982 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
1983 terminal->state = escape_state_normal;
1984 handle_osc(terminal);
1986 escape_append_utf8(terminal, utf8);
1987 if (terminal->escape_length >= MAX_ESCAPE)
1988 terminal->state = escape_state_normal;
1991 case escape_state_special:
1992 escape_append_utf8(terminal, utf8);
1993 terminal->state = escape_state_normal;
1994 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
1995 handle_special_escape(terminal, terminal->escape[1],
2003 /* this is valid, because ASCII characters are never used to
2004 * introduce a multibyte sequence in UTF-8 */
2005 if (utf8.byte[0] == '\e') {
2006 terminal->state = escape_state_escape;
2007 terminal->outer_state = escape_state_normal;
2008 terminal->escape[0] = '\e';
2009 terminal->escape_length = 1;
2010 terminal->escape_flags = 0;
2012 handle_char(terminal, utf8);
2016 window_schedule_redraw(terminal->window);
2020 data_source_target(void *data,
2021 struct wl_data_source *source, const char *mime_type)
2023 fprintf(stderr, "data_source_target, %s\n", mime_type);
2027 data_source_send(void *data,
2028 struct wl_data_source *source,
2029 const char *mime_type, int32_t fd)
2031 struct terminal *terminal = data;
2033 terminal_send_selection(terminal, fd);
2037 data_source_cancelled(void *data, struct wl_data_source *source)
2039 wl_data_source_destroy(source);
2042 static const struct wl_data_source_listener data_source_listener = {
2045 data_source_cancelled
2049 handle_bound_key(struct terminal *terminal,
2050 struct input *input, uint32_t sym, uint32_t time)
2054 /* Cut selection; terminal doesn't do cut, fall
2055 * through to copy. */
2057 terminal->selection =
2058 display_create_data_source(terminal->display);
2059 wl_data_source_offer(terminal->selection,
2060 "text/plain;charset=utf-8");
2061 wl_data_source_add_listener(terminal->selection,
2062 &data_source_listener, terminal);
2063 input_set_selection(input, terminal->selection, time);
2066 input_receive_selection_data_to_fd(input,
2067 "text/plain;charset=utf-8",
2077 key_handler(struct window *window, struct input *input, uint32_t time,
2078 uint32_t key, uint32_t sym, uint32_t state, void *data)
2080 struct terminal *terminal = data;
2081 char ch[MAX_RESPONSE];
2085 modifiers = input_get_modifiers(input);
2086 if ((modifiers & XKB_COMMON_CONTROL_MASK) &&
2087 (modifiers & XKB_COMMON_SHIFT_MASK) &&
2088 state && handle_bound_key(terminal, input, sym, time))
2095 terminal->fullscreen ^= 1;
2096 window_set_fullscreen(window, terminal->fullscreen);
2097 window_schedule_redraw(terminal->window);
2105 case XK_Scroll_Lock:
2108 ch[len++] = sym & 0x7f;
2112 if (terminal->mode & MODE_LF_NEWLINE) {
2129 len = function_key_response('[', 2, modifiers, '~', ch);
2132 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2135 len = function_key_response('[', 3, modifiers, '~', ch);
2139 len = function_key_response('[', 5, modifiers, '~', ch);
2142 len = function_key_response('[', 6, modifiers, '~', ch);
2145 len = function_key_response('O', 1, modifiers, 'P', ch);
2148 len = function_key_response('O', 1, modifiers, 'Q', ch);
2151 len = function_key_response('O', 1, modifiers, 'R', ch);
2154 len = function_key_response('O', 1, modifiers, 'S', ch);
2157 len = function_key_response('[', 15, modifiers, '~', ch);
2160 len = function_key_response('[', 17, modifiers, '~', ch);
2163 len = function_key_response('[', 18, modifiers, '~', ch);
2166 len = function_key_response('[', 19, modifiers, '~', ch);
2169 len = function_key_response('[', 20, modifiers, '~', ch);
2172 len = function_key_response('[', 21, modifiers, '~', ch);
2175 len = function_key_response('[', 24, modifiers, '~', ch);
2178 /* Handle special keys with alternate mappings */
2179 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2180 if (len != 0) break;
2182 if (modifiers & XKB_COMMON_CONTROL_MASK) {
2183 if (sym >= '3' && sym <= '7')
2184 sym = (sym & 0x1f) + 8;
2186 if (!((sym >= '!' && sym <= '/') ||
2187 (sym >= '8' && sym <= '?') ||
2188 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2189 else if (sym == '2') sym = 0x00;
2190 else if (sym == '/') sym = 0x1F;
2191 else if (sym == '8' || sym == '?') sym = 0x7F;
2192 } else if ((terminal->mode & MODE_ALT_SENDS_ESC) &&
2193 (modifiers & XKB_COMMON_MOD1_MASK))
2196 } else if (modifiers & XKB_COMMON_MOD1_MASK) {
2205 if (state && len > 0)
2206 terminal_write(terminal, ch, len);
2210 keyboard_focus_handler(struct window *window,
2211 struct input *device, void *data)
2213 struct terminal *terminal = data;
2215 terminal->focused = (device != NULL);
2216 window_schedule_redraw(terminal->window);
2220 button_handler(struct widget *widget,
2221 struct input *input, uint32_t time,
2222 int button, int state, void *data)
2224 struct terminal *terminal = data;
2229 terminal->dragging = 1;
2230 input_get_position(input,
2231 &terminal->selection_start_x,
2232 &terminal->selection_start_y);
2233 terminal->selection_end_x = terminal->selection_start_x;
2234 terminal->selection_end_y = terminal->selection_start_y;
2235 widget_schedule_redraw(widget);
2237 terminal->dragging = 0;
2244 motion_handler(struct widget *widget,
2245 struct input *input, uint32_t time,
2246 int32_t x, int32_t y, void *data)
2248 struct terminal *terminal = data;
2250 if (terminal->dragging) {
2251 input_get_position(input,
2252 &terminal->selection_end_x,
2253 &terminal->selection_end_y);
2254 widget_schedule_redraw(widget);
2257 return POINTER_IBEAM;
2260 static struct terminal *
2261 terminal_create(struct display *display, int fullscreen)
2263 struct terminal *terminal;
2264 cairo_surface_t *surface;
2267 terminal = malloc(sizeof *terminal);
2268 if (terminal == NULL)
2271 memset(terminal, 0, sizeof *terminal);
2272 terminal->fullscreen = fullscreen;
2273 terminal->color_scheme = &DEFAULT_COLORS;
2274 terminal_init(terminal);
2275 terminal->margin_top = 0;
2276 terminal->margin_bottom = -1;
2277 terminal->window = window_create(display, 500, 400);
2278 terminal->widget = window_add_widget(terminal->window, terminal);
2279 window_set_title(terminal->window, "Wayland Terminal");
2281 init_state_machine(&terminal->state_machine);
2282 init_color_table(terminal);
2284 terminal->display = display;
2285 terminal->margin = 5;
2287 window_set_user_data(terminal->window, terminal);
2288 window_set_redraw_handler(terminal->window, redraw_handler);
2289 window_set_resize_handler(terminal->window, resize_handler);
2291 window_set_key_handler(terminal->window, key_handler);
2292 window_set_keyboard_focus_handler(terminal->window,
2293 keyboard_focus_handler);
2294 widget_set_button_handler(terminal->widget, button_handler);
2295 widget_set_motion_handler(terminal->widget, motion_handler);
2297 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2298 cr = cairo_create(surface);
2299 cairo_set_font_size(cr, 14);
2300 cairo_select_font_face (cr, "mono",
2301 CAIRO_FONT_SLANT_NORMAL,
2302 CAIRO_FONT_WEIGHT_BOLD);
2303 terminal->font_bold = cairo_get_scaled_font (cr);
2304 cairo_scaled_font_reference(terminal->font_bold);
2306 cairo_select_font_face (cr, "mono",
2307 CAIRO_FONT_SLANT_NORMAL,
2308 CAIRO_FONT_WEIGHT_NORMAL);
2309 terminal->font_normal = cairo_get_scaled_font (cr);
2310 cairo_scaled_font_reference(terminal->font_normal);
2312 cairo_font_extents(cr, &terminal->extents);
2314 cairo_surface_destroy(surface);
2320 io_handler(struct task *task, uint32_t events)
2322 struct terminal *terminal =
2323 container_of(task, struct terminal, io_task);
2327 if (events & EPOLLHUP)
2330 len = read(terminal->master, buffer, sizeof buffer);
2334 terminal_data(terminal, buffer, len);
2338 terminal_run(struct terminal *terminal, const char *path)
2343 pid = forkpty(&master, NULL, NULL, NULL);
2345 setenv("TERM", "xterm-256color", 1);
2346 setenv("COLORTERM", "xterm-256color", 1);
2347 if (execl(path, path, NULL)) {
2348 printf("exec failed: %m\n");
2351 } else if (pid < 0) {
2352 fprintf(stderr, "failed to fork and create pty (%m).\n");
2356 terminal->master = master;
2357 fcntl(master, F_SETFL, O_NONBLOCK);
2358 terminal->io_task.run = io_handler;
2359 display_watch_fd(terminal->display, terminal->master,
2360 EPOLLIN | EPOLLHUP, &terminal->io_task);
2362 window_set_fullscreen(terminal->window, terminal->fullscreen);
2363 if (!terminal->fullscreen)
2364 terminal_resize(terminal, 80, 24);
2366 window_schedule_redraw(terminal->window);
2371 static const GOptionEntry option_entries[] = {
2372 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
2373 &option_fullscreen, "Run in fullscreen mode" },
2377 int main(int argc, char *argv[])
2380 struct terminal *terminal;
2382 d = display_create(&argc, &argv, option_entries);
2384 fprintf(stderr, "failed to create display: %m\n");
2388 terminal = terminal_create(d, option_fullscreen);
2389 if (terminal_run(terminal, "/bin/bash"))