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 <wayland-client.h>
39 #include "../shared/config-parser.h"
42 static int option_fullscreen;
43 static char *option_font = "mono";
44 static int option_font_size = 14;
45 static char *option_term = "xterm";
46 static char *option_shell;
48 static struct wl_list terminal_list;
50 static struct terminal *
51 terminal_create(struct display *display, int fullscreen);
53 terminal_destroy(struct terminal *terminal);
55 terminal_run(struct terminal *terminal, const char *path);
57 #define MOD_SHIFT 0x01
61 #define ATTRMASK_BOLD 0x01
62 #define ATTRMASK_UNDERLINE 0x02
63 #define ATTRMASK_BLINK 0x04
64 #define ATTRMASK_INVERSE 0x08
65 #define ATTRMASK_CONCEALED 0x10
68 #define MAX_RESPONSE 256
69 #define MAX_ESCAPE 255
72 #define MODE_SHOW_CURSOR 0x00000001
73 #define MODE_INVERSE 0x00000002
74 #define MODE_AUTOWRAP 0x00000004
75 #define MODE_AUTOREPEAT 0x00000008
76 #define MODE_LF_NEWLINE 0x00000010
77 #define MODE_IRM 0x00000020
78 #define MODE_DELETE_SENDS_DEL 0x00000040
79 #define MODE_ALT_SENDS_ESC 0x00000080
82 unsigned char byte[4];
95 struct utf8_state_machine {
96 enum utf8_state state;
102 init_state_machine(struct utf8_state_machine *machine)
104 machine->state = utf8state_start;
109 static enum utf8_state
110 utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
112 switch(machine->state) {
113 case utf8state_start:
114 case utf8state_accept:
115 case utf8state_reject:
118 if(c == 0xC0 || c == 0xC1) {
119 /* overlong encoding, reject */
120 machine->state = utf8state_reject;
121 } else if((c & 0x80) == 0) {
122 /* single byte, accept */
123 machine->s.byte[machine->len++] = c;
124 machine->state = utf8state_accept;
125 } else if((c & 0xC0) == 0x80) {
126 /* parser out of sync, ignore byte */
127 machine->state = utf8state_start;
128 } else if((c & 0xE0) == 0xC0) {
129 /* start of two byte sequence */
130 machine->s.byte[machine->len++] = c;
131 machine->state = utf8state_expect1;
132 } else if((c & 0xF0) == 0xE0) {
133 /* start of three byte sequence */
134 machine->s.byte[machine->len++] = c;
135 machine->state = utf8state_expect2;
136 } else if((c & 0xF8) == 0xF0) {
137 /* start of four byte sequence */
138 machine->s.byte[machine->len++] = c;
139 machine->state = utf8state_expect3;
141 /* overlong encoding, reject */
142 machine->state = utf8state_reject;
145 case utf8state_expect3:
146 machine->s.byte[machine->len++] = c;
147 if((c & 0xC0) == 0x80) {
148 /* all good, continue */
149 machine->state = utf8state_expect2;
151 /* missing extra byte, reject */
152 machine->state = utf8state_reject;
155 case utf8state_expect2:
156 machine->s.byte[machine->len++] = c;
157 if((c & 0xC0) == 0x80) {
158 /* all good, continue */
159 machine->state = utf8state_expect1;
161 /* missing extra byte, reject */
162 machine->state = utf8state_reject;
165 case utf8state_expect1:
166 machine->s.byte[machine->len++] = c;
167 if((c & 0xC0) == 0x80) {
168 /* all good, accept */
169 machine->state = utf8state_accept;
171 /* missing extra byte, reject */
172 machine->state = utf8state_reject;
176 machine->state = utf8state_reject;
180 return machine->state;
184 union utf8_char match;
185 union utf8_char replace;
187 /* Set last char_sub match to NULL char */
188 typedef struct char_sub *character_set;
190 struct char_sub CS_US[] = {
193 static struct char_sub CS_UK[] = {
194 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
197 static struct char_sub CS_SPECIAL[] = {
198 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond: ♦ */
199 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell: ▒ */
200 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT: ␉ */
201 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF: ␌ */
202 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR: ␍ */
203 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF: ␊ */
204 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree: ° */
205 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus: ± */
206 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL:  */
207 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT: ␋ */
208 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB: ┘ */
209 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT: ┐ */
210 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT: ┌ */
211 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_LB: └ */
212 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS: ┼ */
213 {{{'o', 0, }}, {{0xE2, 0x8E, 0xBA, 0}}}, /* Horiz. Scan Line 1: ⎺ */
214 {{{'p', 0, }}, {{0xE2, 0x8E, 0xBB, 0}}}, /* Horiz. Scan Line 3: ⎻ */
215 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* Horiz. Scan Line 5: ─ */
216 {{{'r', 0, }}, {{0xE2, 0x8E, 0xBC, 0}}}, /* Horiz. Scan Line 7: ⎼ */
217 {{{'s', 0, }}, {{0xE2, 0x8E, 0xBD, 0}}}, /* Horiz. Scan Line 9: ⎽ */
218 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR: ├ */
219 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL: ┤ */
220 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU: ┴ */
221 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD: ┬ */
222 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V: │ */
223 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE: ≤ */
224 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE: ≥ */
225 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI: π */
226 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ: ≠ */
227 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
228 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT: ⋅ */
233 apply_char_set(character_set cs, union utf8_char *utf8)
237 while (cs[i].match.byte[0]) {
238 if ((*utf8).ch == cs[i].match.ch) {
239 *utf8 = cs[i].replace;
252 /* Set last key_sub sym to NULL */
253 typedef struct key_map *keyboard_mode;
255 static struct key_map KM_NORMAL[] = {
256 { XKB_KEY_Left, 1, '[', 'D' },
257 { XKB_KEY_Right, 1, '[', 'C' },
258 { XKB_KEY_Up, 1, '[', 'A' },
259 { XKB_KEY_Down, 1, '[', 'B' },
260 { XKB_KEY_Home, 1, '[', 'H' },
261 { XKB_KEY_End, 1, '[', 'F' },
264 static struct key_map KM_APPLICATION[] = {
265 { XKB_KEY_Left, 1, 'O', 'D' },
266 { XKB_KEY_Right, 1, 'O', 'C' },
267 { XKB_KEY_Up, 1, 'O', 'A' },
268 { XKB_KEY_Down, 1, 'O', 'B' },
269 { XKB_KEY_Home, 1, 'O', 'H' },
270 { XKB_KEY_End, 1, 'O', 'F' },
271 { XKB_KEY_KP_Enter, 1, 'O', 'M' },
272 { XKB_KEY_KP_Multiply, 1, 'O', 'j' },
273 { XKB_KEY_KP_Add, 1, 'O', 'k' },
274 { XKB_KEY_KP_Separator, 1, 'O', 'l' },
275 { XKB_KEY_KP_Subtract, 1, 'O', 'm' },
276 { XKB_KEY_KP_Divide, 1, 'O', 'o' },
281 function_key_response(char escape, int num, uint32_t modifiers,
282 char code, char *response)
287 if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
288 if (modifiers & MOD_ALT_MASK) mod_num |= 2;
289 if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
292 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
293 num, mod_num + 1, code);
294 else if (code != '~')
295 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
298 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
301 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
305 /* returns the number of bytes written into response,
306 * which must have room for MAX_RESPONSE bytes */
308 apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
314 while (mode[i].sym) {
316 if (sym == map.sym) {
317 len = function_key_response(map.escape, map.num,
327 struct terminal_color { double r, g, b, a; };
329 unsigned char fg, bg;
330 char a; /* attributes format:
333 char s; /* in selection */
335 struct color_scheme {
336 struct terminal_color palette[16];
338 struct attr default_attr;
342 attr_init(struct attr *data_attr, struct attr attr, int n)
345 for (i = 0; i < n; i++) {
351 escape_state_normal = 0,
356 escape_state_inner_escape,
361 #define ESC_FLAG_WHAT 0x01
362 #define ESC_FLAG_GT 0x02
363 #define ESC_FLAG_BANG 0x04
364 #define ESC_FLAG_CASH 0x08
365 #define ESC_FLAG_SQUOTE 0x10
366 #define ESC_FLAG_DQUOTE 0x20
367 #define ESC_FLAG_SPACE 0x40
377 struct window *window;
378 struct widget *widget;
379 struct display *display;
380 union utf8_char *data;
383 struct attr *data_attr;
384 struct attr curr_attr;
387 char saved_origin_mode;
388 struct attr saved_attr;
389 union utf8_char last_char;
390 int margin_top, margin_bottom;
391 character_set cs, g0, g1;
392 character_set saved_cs, saved_g0, saved_g1;
393 keyboard_mode key_mode;
394 int data_pitch, attr_pitch; /* The width in bytes of a line */
395 int width, height, start, row, column;
396 int saved_row, saved_column;
397 int send_cursor_position;
400 char escape[MAX_ESCAPE+1];
402 enum escape_state state;
403 enum escape_state outer_state;
405 struct utf8_state_machine state_machine;
408 struct color_scheme *color_scheme;
409 struct terminal_color color_table[256];
410 cairo_font_extents_t extents;
411 cairo_scaled_font_t *font_normal, *font_bold;
412 uint32_t hide_cursor_serial;
414 struct wl_data_source *selection;
415 uint32_t button_time;
416 int dragging, click_count;
417 int selection_start_x, selection_start_y;
418 int selection_end_x, selection_end_y;
419 int selection_start_row, selection_start_col;
420 int selection_end_row, selection_end_col;
424 /* Create default tab stops, every 8 characters */
426 terminal_init_tabs(struct terminal *terminal)
430 while (i < terminal->width) {
432 terminal->tab_ruler[i] = 1;
434 terminal->tab_ruler[i] = 0;
440 terminal_init(struct terminal *terminal)
442 terminal->curr_attr = terminal->color_scheme->default_attr;
443 terminal->origin_mode = 0;
444 terminal->mode = MODE_SHOW_CURSOR |
450 terminal->column = 0;
452 terminal->g0 = CS_US;
453 terminal->g1 = CS_US;
454 terminal->cs = terminal->g0;
455 terminal->key_mode = KM_NORMAL;
457 terminal->saved_g0 = terminal->g0;
458 terminal->saved_g1 = terminal->g1;
459 terminal->saved_cs = terminal->cs;
461 terminal->saved_attr = terminal->curr_attr;
462 terminal->saved_origin_mode = terminal->origin_mode;
463 terminal->saved_row = terminal->row;
464 terminal->saved_column = terminal->column;
466 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
470 init_color_table(struct terminal *terminal)
473 struct terminal_color *color_table = terminal->color_table;
475 for (c = 0; c < 256; c ++) {
477 color_table[c] = terminal->color_scheme->palette[c];
478 } else if (c < 232) {
480 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
481 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
482 color_table[c].r = ((double)(r % 6) / 6.0);
483 color_table[c].a = 1.0;
485 r = (c - 232) * 10 + 8;
486 color_table[c].r = ((double) r) / 256.0;
487 color_table[c].g = color_table[c].r;
488 color_table[c].b = color_table[c].r;
489 color_table[c].a = 1.0;
494 static union utf8_char *
495 terminal_get_row(struct terminal *terminal, int row)
499 index = (row + terminal->start) % terminal->height;
501 return &terminal->data[index * terminal->width];
505 terminal_get_attr_row(struct terminal *terminal, int row)
509 index = (row + terminal->start) % terminal->height;
511 return &terminal->data_attr[index * terminal->width];
520 terminal_decode_attr(struct terminal *terminal, int row, int col,
521 union decoded_attr *decoded)
524 int foreground, background, tmp;
527 if (((row == terminal->selection_start_row &&
528 col >= terminal->selection_start_col) ||
529 row > terminal->selection_start_row) &&
530 ((row == terminal->selection_end_row &&
531 col < terminal->selection_end_col) ||
532 row < terminal->selection_end_row))
535 /* get the attributes for this character cell */
536 attr = terminal_get_attr_row(terminal, row)[col];
537 if ((attr.a & ATTRMASK_INVERSE) ||
539 ((terminal->mode & MODE_SHOW_CURSOR) &&
540 window_has_focus(terminal->window) && terminal->row == row &&
541 terminal->column == col)) {
542 foreground = attr.bg;
543 background = attr.fg;
544 if (attr.a & ATTRMASK_BOLD) {
545 if (foreground <= 16) foreground |= 0x08;
546 if (background <= 16) background &= 0x07;
549 foreground = attr.fg;
550 background = attr.bg;
553 if (terminal->mode & MODE_INVERSE) {
555 foreground = background;
557 if (attr.a & ATTRMASK_BOLD) {
558 if (foreground <= 16) foreground |= 0x08;
559 if (background <= 16) background &= 0x07;
563 decoded->attr.fg = foreground;
564 decoded->attr.bg = background;
565 decoded->attr.a = attr.a;
570 terminal_scroll_buffer(struct terminal *terminal, int d)
574 d = d % (terminal->height + 1);
575 terminal->start = (terminal->start + d) % terminal->height;
576 if (terminal->start < 0) terminal->start = terminal->height + terminal->start;
579 for(i = 0; i < d; i++) {
580 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
581 attr_init(terminal_get_attr_row(terminal, i),
582 terminal->curr_attr, terminal->width);
585 for(i = terminal->height - d; i < terminal->height; i++) {
586 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
587 attr_init(terminal_get_attr_row(terminal, i),
588 terminal->curr_attr, terminal->width);
592 terminal->selection_start_row -= d;
593 terminal->selection_end_row -= d;
597 terminal_scroll_window(struct terminal *terminal, int d)
601 int from_row, to_row;
603 // scrolling range is inclusive
604 window_height = terminal->margin_bottom - terminal->margin_top + 1;
605 d = d % (window_height + 1);
608 to_row = terminal->margin_bottom;
609 from_row = terminal->margin_bottom - d;
611 for (i = 0; i < (window_height - d); i++) {
612 memcpy(terminal_get_row(terminal, to_row - i),
613 terminal_get_row(terminal, from_row - i),
614 terminal->data_pitch);
615 memcpy(terminal_get_attr_row(terminal, to_row - i),
616 terminal_get_attr_row(terminal, from_row - i),
617 terminal->attr_pitch);
619 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
620 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
621 attr_init(terminal_get_attr_row(terminal, i),
622 terminal->curr_attr, terminal->width);
625 to_row = terminal->margin_top;
626 from_row = terminal->margin_top + d;
628 for (i = 0; i < (window_height - d); i++) {
629 memcpy(terminal_get_row(terminal, to_row + i),
630 terminal_get_row(terminal, from_row + i),
631 terminal->data_pitch);
632 memcpy(terminal_get_attr_row(terminal, to_row + i),
633 terminal_get_attr_row(terminal, from_row + i),
634 terminal->attr_pitch);
636 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
637 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
638 attr_init(terminal_get_attr_row(terminal, i),
639 terminal->curr_attr, terminal->width);
645 terminal_scroll(struct terminal *terminal, int d)
647 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
648 terminal_scroll_buffer(terminal, d);
650 terminal_scroll_window(terminal, d);
654 terminal_shift_line(struct terminal *terminal, int d)
656 union utf8_char *row;
657 struct attr *attr_row;
659 row = terminal_get_row(terminal, terminal->row);
660 attr_row = terminal_get_attr_row(terminal, terminal->row);
662 if ((terminal->width + d) <= terminal->column)
663 d = terminal->column + 1 - terminal->width;
664 if ((terminal->column + d) >= terminal->width)
665 d = terminal->width - terminal->column - 1;
669 memmove(&row[terminal->column],
670 &row[terminal->column + d],
671 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
672 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
673 (terminal->width - terminal->column - d) * sizeof(struct attr));
674 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
675 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
677 memmove(&row[terminal->column + d], &row[terminal->column],
678 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
679 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
680 (terminal->width - terminal->column - d) * sizeof(struct attr));
681 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
682 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
687 terminal_resize_cells(struct terminal *terminal, int width, int height)
690 union utf8_char *data;
691 struct attr *data_attr;
693 int data_pitch, attr_pitch;
694 int i, l, total_rows;
695 struct rectangle allocation;
698 if (terminal->width == width && terminal->height == height)
701 data_pitch = width * sizeof(union utf8_char);
702 size = data_pitch * height;
704 attr_pitch = width * sizeof(struct attr);
705 data_attr = malloc(attr_pitch * height);
706 tab_ruler = malloc(width);
707 memset(data, 0, size);
708 memset(tab_ruler, 0, width);
709 attr_init(data_attr, terminal->curr_attr, width * height);
710 if (terminal->data && terminal->data_attr) {
711 if (width > terminal->width)
716 if (terminal->height > height) {
719 total_rows = terminal->height;
722 for (i = 0; i < total_rows; i++) {
723 memcpy(&data[width * i],
724 terminal_get_row(terminal, i),
725 l * sizeof(union utf8_char));
726 memcpy(&data_attr[width * i],
727 terminal_get_attr_row(terminal, i),
728 l * sizeof(struct attr));
731 free(terminal->data);
732 free(terminal->data_attr);
733 free(terminal->tab_ruler);
736 terminal->data_pitch = data_pitch;
737 terminal->attr_pitch = attr_pitch;
738 terminal->margin_bottom =
739 height - (terminal->height - terminal->margin_bottom);
740 terminal->width = width;
741 terminal->height = height;
742 terminal->data = data;
743 terminal->data_attr = data_attr;
744 terminal->tab_ruler = tab_ruler;
745 terminal_init_tabs(terminal);
747 /* Update the window size */
748 ws.ws_row = terminal->height;
749 ws.ws_col = terminal->width;
750 widget_get_allocation(terminal->widget, &allocation);
751 ws.ws_xpixel = allocation.width;
752 ws.ws_ypixel = allocation.height;
753 ioctl(terminal->master, TIOCSWINSZ, &ws);
757 resize_handler(struct widget *widget,
758 int32_t width, int32_t height, void *data)
760 struct terminal *terminal = data;
761 int32_t columns, rows, m;
763 m = 2 * terminal->margin;
764 columns = (width - m) / (int32_t) terminal->extents.max_x_advance;
765 rows = (height - m) / (int32_t) terminal->extents.height;
767 if (!terminal->fullscreen) {
768 width = columns * terminal->extents.max_x_advance + m;
769 height = rows * terminal->extents.height + m;
770 widget_set_size(terminal->widget, width, height);
773 terminal_resize_cells(terminal, columns, rows);
777 terminal_resize(struct terminal *terminal, int columns, int rows)
779 int32_t width, height, m;
781 if (terminal->fullscreen)
784 m = 2 * terminal->margin;
785 width = columns * terminal->extents.max_x_advance + m;
786 height = rows * terminal->extents.height + m;
788 frame_set_child_size(terminal->widget, width, height);
791 struct color_scheme DEFAULT_COLORS = {
793 {0, 0, 0, 1}, /* black */
794 {0.66, 0, 0, 1}, /* red */
795 {0 , 0.66, 0, 1}, /* green */
796 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
797 {0 , 0 , 0.66, 1}, /* blue */
798 {0.66, 0 , 0.66, 1}, /* magenta */
799 {0, 0.66, 0.66, 1}, /* cyan */
800 {0.66, 0.66, 0.66, 1}, /* light grey */
801 {0.22, 0.33, 0.33, 1}, /* dark grey */
802 {1, 0.33, 0.33, 1}, /* high red */
803 {0.33, 1, 0.33, 1}, /* high green */
804 {1, 1, 0.33, 1}, /* high yellow */
805 {0.33, 0.33, 1, 1}, /* high blue */
806 {1, 0.33, 1, 1}, /* high magenta */
807 {0.33, 1, 1, 1}, /* high cyan */
808 {1, 1, 1, 1} /* white */
810 0, /* black border */
811 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
815 terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
817 cairo_set_source_rgba(cr,
818 terminal->color_table[index].r,
819 terminal->color_table[index].g,
820 terminal->color_table[index].b,
821 terminal->color_table[index].a);
825 terminal_send_selection(struct terminal *terminal, int fd)
828 union utf8_char *p_row;
829 union decoded_attr attr;
833 fp = fdopen(fd, "w");
834 for (row = 0; row < terminal->height; row++) {
835 p_row = terminal_get_row(terminal, row);
836 for (col = 0; col < terminal->width; col++) {
837 /* get the attributes for this character cell */
838 terminal_decode_attr(terminal, row, col, &attr);
841 len = strnlen((char *) p_row[col].byte, 4);
843 fwrite(p_row[col].byte, 1, len, fp);
844 if (len == 0 || col == terminal->width - 1) {
845 fwrite("\n", 1, 1, fp);
854 struct terminal *terminal;
857 union decoded_attr attr;
858 cairo_glyph_t glyphs[256], *g;
862 glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
864 run->terminal = terminal;
866 run->g = run->glyphs;
872 glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
874 cairo_scaled_font_t *font;
876 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
877 (attr.key != run->attr.key)) {
878 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
879 font = run->terminal->font_bold;
881 font = run->terminal->font_normal;
882 cairo_set_scaled_font(run->cr, font);
883 terminal_set_color(run->terminal, run->cr,
886 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
887 cairo_show_glyphs (run->cr, run->glyphs, run->count);
888 run->g = run->glyphs;
895 glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
898 cairo_scaled_font_t *font;
900 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
902 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
903 font = run->terminal->font_bold;
905 font = run->terminal->font_normal;
907 cairo_move_to(run->cr, x, y);
908 cairo_scaled_font_text_to_glyphs (font, x, y,
910 &run->g, &num_glyphs,
912 run->g += num_glyphs;
913 run->count += num_glyphs;
918 redraw_handler(struct widget *widget, void *data)
920 struct terminal *terminal = data;
921 struct rectangle allocation;
923 int top_margin, side_margin;
924 int row, col, cursor_x, cursor_y;
925 union utf8_char *p_row;
926 union decoded_attr attr;
928 cairo_surface_t *surface;
930 struct glyph_run run;
931 cairo_font_extents_t extents;
933 surface = window_get_surface(terminal->window);
934 widget_get_allocation(terminal->widget, &allocation);
935 cr = cairo_create(surface);
936 cairo_rectangle(cr, allocation.x, allocation.y,
937 allocation.width, allocation.height);
939 cairo_push_group(cr);
941 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
942 terminal_set_color(terminal, cr, terminal->color_scheme->border);
945 cairo_set_scaled_font(cr, terminal->font_normal);
947 extents = terminal->extents;
948 side_margin = (allocation.width - terminal->width * extents.max_x_advance) / 2;
949 top_margin = (allocation.height - terminal->height * extents.height) / 2;
951 cairo_set_line_width(cr, 1.0);
952 cairo_translate(cr, allocation.x + side_margin,
953 allocation.y + top_margin);
954 /* paint the background */
955 for (row = 0; row < terminal->height; row++) {
956 for (col = 0; col < terminal->width; col++) {
957 /* get the attributes for this character cell */
958 terminal_decode_attr(terminal, row, col, &attr);
960 if (attr.attr.bg == terminal->color_scheme->border)
963 terminal_set_color(terminal, cr, attr.attr.bg);
964 cairo_move_to(cr, col * extents.max_x_advance,
965 row * extents.height);
966 cairo_rel_line_to(cr, extents.max_x_advance, 0);
967 cairo_rel_line_to(cr, 0, extents.height);
968 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
969 cairo_close_path(cr);
974 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
976 /* paint the foreground */
977 glyph_run_init(&run, terminal, cr);
978 for (row = 0; row < terminal->height; row++) {
979 p_row = terminal_get_row(terminal, row);
980 for (col = 0; col < terminal->width; col++) {
981 /* get the attributes for this character cell */
982 terminal_decode_attr(terminal, row, col, &attr);
984 glyph_run_flush(&run, attr);
986 text_x = col * extents.max_x_advance;
987 text_y = extents.ascent + row * extents.height;
988 if (attr.attr.a & ATTRMASK_UNDERLINE) {
989 terminal_set_color(terminal, cr, attr.attr.fg);
990 cairo_move_to(cr, text_x, (double)text_y + 1.5);
991 cairo_line_to(cr, text_x + extents.max_x_advance, (double) text_y + 1.5);
995 glyph_run_add(&run, text_x, text_y, &p_row[col]);
1000 glyph_run_flush(&run, attr);
1002 if ((terminal->mode & MODE_SHOW_CURSOR) &&
1003 !window_has_focus(terminal->window)) {
1006 cairo_set_line_width(cr, 1);
1007 cairo_move_to(cr, terminal->column * extents.max_x_advance + d,
1008 terminal->row * extents.height + d);
1009 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
1010 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
1011 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
1012 cairo_close_path(cr);
1017 cairo_pop_group_to_source(cr);
1020 cairo_surface_destroy(surface);
1022 if (terminal->send_cursor_position) {
1023 cursor_x = side_margin + allocation.x +
1024 terminal->column * extents.max_x_advance;
1025 cursor_y = top_margin + allocation.y +
1026 terminal->row * extents.height;
1027 window_set_text_cursor_position(terminal->window,
1028 cursor_x, cursor_y);
1029 terminal->send_cursor_position = 0;
1034 terminal_write(struct terminal *terminal, const char *data, size_t length)
1036 if (write(terminal->master, data, length) < 0)
1038 terminal->send_cursor_position = 1;
1042 terminal_data(struct terminal *terminal, const char *data, size_t length);
1045 handle_char(struct terminal *terminal, union utf8_char utf8);
1048 handle_sgr(struct terminal *terminal, int code);
1051 handle_term_parameter(struct terminal *terminal, int code, int sr)
1055 if (terminal->escape_flags & ESC_FLAG_WHAT) {
1057 case 1: /* DECCKM */
1058 if (sr) terminal->key_mode = KM_APPLICATION;
1059 else terminal->key_mode = KM_NORMAL;
1061 case 2: /* DECANM */
1062 /* No VT52 support yet */
1063 terminal->g0 = CS_US;
1064 terminal->g1 = CS_US;
1065 terminal->cs = terminal->g0;
1067 case 3: /* DECCOLM */
1069 terminal_resize(terminal, 132, 24);
1071 terminal_resize(terminal, 80, 24);
1073 /* set columns, but also home cursor and clear screen */
1074 terminal->row = 0; terminal->column = 0;
1075 for (i = 0; i < terminal->height; i++) {
1076 memset(terminal_get_row(terminal, i),
1077 0, terminal->data_pitch);
1078 attr_init(terminal_get_attr_row(terminal, i),
1079 terminal->curr_attr, terminal->width);
1082 case 5: /* DECSCNM */
1083 if (sr) terminal->mode |= MODE_INVERSE;
1084 else terminal->mode &= ~MODE_INVERSE;
1087 terminal->origin_mode = sr;
1088 if (terminal->origin_mode)
1089 terminal->row = terminal->margin_top;
1092 terminal->column = 0;
1094 case 7: /* DECAWM */
1095 if (sr) terminal->mode |= MODE_AUTOWRAP;
1096 else terminal->mode &= ~MODE_AUTOWRAP;
1098 case 8: /* DECARM */
1099 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1100 else terminal->mode &= ~MODE_AUTOREPEAT;
1102 case 12: /* Very visible cursor (CVVIS) */
1103 /* FIXME: What do we do here. */
1106 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1107 else terminal->mode &= ~MODE_SHOW_CURSOR;
1109 case 1034: /* smm/rmm, meta mode on/off */
1112 case 1037: /* deleteSendsDel */
1113 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1114 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1116 case 1039: /* altSendsEscape */
1117 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1118 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1120 case 1049: /* rmcup/smcup, alternate screen */
1121 /* Ignore. Should be possible to implement,
1122 * but it's kind of annoying. */
1125 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1131 if (sr) terminal->mode |= MODE_IRM;
1132 else terminal->mode &= ~MODE_IRM;
1135 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1136 else terminal->mode &= ~MODE_LF_NEWLINE;
1139 fprintf(stderr, "Unknown parameter: %d\n", code);
1146 handle_dcs(struct terminal *terminal)
1151 handle_osc(struct terminal *terminal)
1156 terminal->escape[terminal->escape_length++] = '\0';
1157 p = &terminal->escape[2];
1158 code = strtol(p, &p, 10);
1162 case 0: /* Icon name and window title */
1163 case 1: /* Icon label */
1164 case 2: /* Window title*/
1165 window_set_title(terminal->window, p);
1168 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1174 handle_escape(struct terminal *terminal)
1176 union utf8_char *row;
1177 struct attr *attr_row;
1179 int i, count, x, y, top, bottom;
1180 int args[10], set[10] = { 0, };
1181 char response[MAX_RESPONSE] = {0, };
1182 struct rectangle allocation;
1184 terminal->escape[terminal->escape_length++] = '\0';
1186 p = &terminal->escape[2];
1187 while ((isdigit(*p) || *p == ';') && i < 10) {
1196 args[i] = strtol(p, &p, 10);
1203 count = set[0] ? args[0] : 1;
1204 if (count == 0) count = 1;
1205 terminal_shift_line(terminal, count);
1208 count = set[0] ? args[0] : 1;
1209 if (count == 0) count = 1;
1210 if (terminal->row - count >= terminal->margin_top)
1211 terminal->row -= count;
1213 terminal->row = terminal->margin_top;
1216 count = set[0] ? args[0] : 1;
1217 if (count == 0) count = 1;
1218 if (terminal->row + count <= terminal->margin_bottom)
1219 terminal->row += count;
1221 terminal->row = terminal->margin_bottom;
1224 count = set[0] ? args[0] : 1;
1225 if (count == 0) count = 1;
1226 if ((terminal->column + count) < terminal->width)
1227 terminal->column += count;
1229 terminal->column = terminal->width - 1;
1232 count = set[0] ? args[0] : 1;
1233 if (count == 0) count = 1;
1234 if ((terminal->column - count) >= 0)
1235 terminal->column -= count;
1237 terminal->column = 0;
1240 count = set[0] ? args[0] : 1;
1241 if (terminal->row + count <= terminal->margin_bottom)
1242 terminal->row += count;
1244 terminal->row = terminal->margin_bottom;
1245 terminal->column = 0;
1248 count = set[0] ? args[0] : 1;
1249 if (terminal->row - count >= terminal->margin_top)
1250 terminal->row -= count;
1252 terminal->row = terminal->margin_top;
1253 terminal->column = 0;
1256 y = set[0] ? args[0] : 1;
1257 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1259 terminal->column = y - 1;
1263 x = (set[1] ? args[1] : 1) - 1;
1265 (x >= terminal->width ? terminal->width - 1 : x);
1267 y = (set[0] ? args[0] : 1) - 1;
1268 if (terminal->origin_mode) {
1269 y += terminal->margin_top;
1270 y = y < terminal->margin_top ? terminal->margin_top :
1271 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1274 (y >= terminal->height ? terminal->height - 1 : y);
1278 terminal->column = x;
1281 count = set[0] ? args[0] : 1;
1282 if (count == 0) count = 1;
1283 while (count > 0 && terminal->column < terminal->width) {
1284 if (terminal->tab_ruler[terminal->column]) count--;
1290 row = terminal_get_row(terminal, terminal->row);
1291 attr_row = terminal_get_attr_row(terminal, terminal->row);
1292 if (!set[0] || args[0] == 0 || args[0] > 2) {
1293 memset(&row[terminal->column],
1294 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1295 attr_init(&attr_row[terminal->column],
1296 terminal->curr_attr, terminal->width - terminal->column);
1297 for (i = terminal->row + 1; i < terminal->height; i++) {
1298 memset(terminal_get_row(terminal, i),
1299 0, terminal->data_pitch);
1300 attr_init(terminal_get_attr_row(terminal, i),
1301 terminal->curr_attr, terminal->width);
1303 } else if (args[0] == 1) {
1304 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1305 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1306 for (i = 0; i < terminal->row; i++) {
1307 memset(terminal_get_row(terminal, i),
1308 0, terminal->data_pitch);
1309 attr_init(terminal_get_attr_row(terminal, i),
1310 terminal->curr_attr, terminal->width);
1312 } else if (args[0] == 2) {
1313 for (i = 0; i < terminal->height; i++) {
1314 memset(terminal_get_row(terminal, i),
1315 0, terminal->data_pitch);
1316 attr_init(terminal_get_attr_row(terminal, i),
1317 terminal->curr_attr, terminal->width);
1322 row = terminal_get_row(terminal, terminal->row);
1323 attr_row = terminal_get_attr_row(terminal, terminal->row);
1324 if (!set[0] || args[0] == 0 || args[0] > 2) {
1325 memset(&row[terminal->column], 0,
1326 (terminal->width - terminal->column) * sizeof(union utf8_char));
1327 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1328 terminal->width - terminal->column);
1329 } else if (args[0] == 1) {
1330 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1331 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1332 } else if (args[0] == 2) {
1333 memset(row, 0, terminal->data_pitch);
1334 attr_init(attr_row, terminal->curr_attr, terminal->width);
1338 count = set[0] ? args[0] : 1;
1339 if (count == 0) count = 1;
1340 if (terminal->row >= terminal->margin_top &&
1341 terminal->row < terminal->margin_bottom)
1343 top = terminal->margin_top;
1344 terminal->margin_top = terminal->row;
1345 terminal_scroll(terminal, 0 - count);
1346 terminal->margin_top = top;
1347 } else if (terminal->row == terminal->margin_bottom) {
1348 memset(terminal_get_row(terminal, terminal->row),
1349 0, terminal->data_pitch);
1350 attr_init(terminal_get_attr_row(terminal, terminal->row),
1351 terminal->curr_attr, terminal->width);
1355 count = set[0] ? args[0] : 1;
1356 if (count == 0) count = 1;
1357 if (terminal->row >= terminal->margin_top &&
1358 terminal->row < terminal->margin_bottom)
1360 top = terminal->margin_top;
1361 terminal->margin_top = terminal->row;
1362 terminal_scroll(terminal, count);
1363 terminal->margin_top = top;
1364 } else if (terminal->row == terminal->margin_bottom) {
1365 memset(terminal_get_row(terminal, terminal->row),
1366 0, terminal->data_pitch);
1370 count = set[0] ? args[0] : 1;
1371 if (count == 0) count = 1;
1372 terminal_shift_line(terminal, 0 - count);
1375 terminal_scroll(terminal, set[0] ? args[0] : 1);
1378 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1381 count = set[0] ? args[0] : 1;
1382 if (count == 0) count = 1;
1383 if ((terminal->column + count) > terminal->width)
1384 count = terminal->width - terminal->column;
1385 row = terminal_get_row(terminal, terminal->row);
1386 attr_row = terminal_get_attr_row(terminal, terminal->row);
1387 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1388 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1391 count = set[0] ? args[0] : 1;
1392 if (count == 0) count = 1;
1393 while (count > 0 && terminal->column >= 0) {
1394 if (terminal->tab_ruler[terminal->column]) count--;
1400 y = set[0] ? args[0] : 1;
1401 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1403 terminal->column = y - 1;
1406 count = set[0] ? args[0] : 1;
1407 if (count == 0) count = 1;
1408 if (terminal->last_char.byte[0])
1409 for (i = 0; i < count; i++)
1410 handle_char(terminal, terminal->last_char);
1411 terminal->last_char.byte[0] = 0;
1413 case 'c': /* Primary DA */
1414 terminal_write(terminal, "\e[?6c", 5);
1417 x = set[0] ? args[0] : 1;
1418 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1420 terminal->row = x - 1;
1423 if (!set[0] || args[0] == 0) {
1424 terminal->tab_ruler[terminal->column] = 0;
1425 } else if (args[0] == 3) {
1426 memset(terminal->tab_ruler, 0, terminal->width);
1430 for(i = 0; i < 10 && set[i]; i++) {
1431 handle_term_parameter(terminal, args[i], 1);
1435 for(i = 0; i < 10 && set[i]; i++) {
1436 handle_term_parameter(terminal, args[i], 0);
1440 for(i = 0; i < 10; i++) {
1441 if (i <= 7 && set[i] && set[i + 1] &&
1442 set[i + 2] && args[i + 1] == 5)
1444 if (args[i] == 38) {
1445 handle_sgr(terminal, args[i + 2] + 256);
1447 } else if (args[i] == 48) {
1448 handle_sgr(terminal, args[i + 2] + 512);
1453 handle_sgr(terminal, args[i]);
1455 handle_sgr(terminal, 0);
1463 i = set[0] ? args[0] : 0;
1464 if (i == 0 || i == 5) {
1465 terminal_write(terminal, "\e[0n", 4);
1466 } else if (i == 6) {
1467 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1468 terminal->origin_mode ?
1469 terminal->row+terminal->margin_top : terminal->row+1,
1470 terminal->column+1);
1471 terminal_write(terminal, response, strlen(response));
1476 terminal->margin_top = 0;
1477 terminal->margin_bottom = terminal->height-1;
1479 terminal->column = 0;
1481 top = (set[0] ? args[0] : 1) - 1;
1483 (top >= terminal->height ? terminal->height - 1 : top);
1484 bottom = (set[1] ? args[1] : 1) - 1;
1485 bottom = bottom < 0 ? 0 :
1486 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1488 terminal->margin_top = top;
1489 terminal->margin_bottom = bottom;
1491 terminal->margin_top = 0;
1492 terminal->margin_bottom = terminal->height-1;
1494 if(terminal->origin_mode)
1495 terminal->row = terminal->margin_top;
1498 terminal->column = 0;
1502 terminal->saved_row = terminal->row;
1503 terminal->saved_column = terminal->column;
1505 case 't': /* windowOps */
1508 case 4: /* resize px */
1509 if (set[1] && set[2]) {
1510 widget_schedule_resize(terminal->widget,
1514 case 8: /* resize ch */
1515 if (set[1] && set[2]) {
1516 terminal_resize(terminal, args[2], args[1]);
1519 case 13: /* report position */
1520 widget_get_allocation(terminal->widget, &allocation);
1521 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1522 allocation.x, allocation.y);
1523 terminal_write(terminal, response, strlen(response));
1525 case 14: /* report px */
1526 widget_get_allocation(terminal->widget, &allocation);
1527 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1528 allocation.height, allocation.width);
1529 terminal_write(terminal, response, strlen(response));
1531 case 18: /* report ch */
1532 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1533 terminal->height, terminal->width);
1534 terminal_write(terminal, response, strlen(response));
1536 case 21: /* report title */
1537 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1538 window_get_title(terminal->window));
1539 terminal_write(terminal, response, strlen(response));
1543 terminal_resize(terminal, terminal->width, args[0]);
1545 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1549 terminal->row = terminal->saved_row;
1550 terminal->column = terminal->saved_column;
1553 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
1559 handle_non_csi_escape(struct terminal *terminal, char code)
1564 if(terminal->row < terminal->margin_top) {
1565 terminal->row = terminal->margin_top;
1566 terminal_scroll(terminal, -1);
1570 terminal->column = 0;
1574 if(terminal->row > terminal->margin_bottom) {
1575 terminal->row = terminal->margin_bottom;
1576 terminal_scroll(terminal, +1);
1580 terminal_init(terminal);
1583 terminal->tab_ruler[terminal->column] = 1;
1585 case '7': /* DECSC */
1586 terminal->saved_row = terminal->row;
1587 terminal->saved_column = terminal->column;
1588 terminal->saved_attr = terminal->curr_attr;
1589 terminal->saved_origin_mode = terminal->origin_mode;
1590 terminal->saved_cs = terminal->cs;
1591 terminal->saved_g0 = terminal->g0;
1592 terminal->saved_g1 = terminal->g1;
1594 case '8': /* DECRC */
1595 terminal->row = terminal->saved_row;
1596 terminal->column = terminal->saved_column;
1597 terminal->curr_attr = terminal->saved_attr;
1598 terminal->origin_mode = terminal->saved_origin_mode;
1599 terminal->cs = terminal->saved_cs;
1600 terminal->g0 = terminal->saved_g0;
1601 terminal->g1 = terminal->saved_g1;
1603 case '=': /* DECPAM */
1604 terminal->key_mode = KM_APPLICATION;
1606 case '>': /* DECPNM */
1607 terminal->key_mode = KM_NORMAL;
1610 fprintf(stderr, "Unknown escape code: %c\n", code);
1616 handle_special_escape(struct terminal *terminal, char special, char code)
1620 if (special == '#') {
1623 /* fill with 'E', no cheap way to do this */
1624 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1625 numChars = terminal->width * terminal->height;
1626 for(i = 0; i < numChars; i++) {
1627 terminal->data[i].byte[0] = 'E';
1631 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1634 } else if (special == '(' || special == ')') {
1638 terminal->g0 = CS_SPECIAL;
1640 terminal->g1 = CS_SPECIAL;
1644 terminal->g0 = CS_UK;
1646 terminal->g1 = CS_UK;
1650 terminal->g0 = CS_US;
1652 terminal->g1 = CS_US;
1655 fprintf(stderr, "Unknown character set %c\n", code);
1659 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1664 handle_sgr(struct terminal *terminal, int code)
1668 terminal->curr_attr = terminal->color_scheme->default_attr;
1671 terminal->curr_attr.a |= ATTRMASK_BOLD;
1672 if (terminal->curr_attr.fg < 8)
1673 terminal->curr_attr.fg += 8;
1676 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1679 terminal->curr_attr.a |= ATTRMASK_BLINK;
1682 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1687 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1688 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1689 terminal->curr_attr.fg -= 8;
1692 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1695 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1699 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1702 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1705 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1708 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1711 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1714 if(code >= 30 && code <= 37) {
1715 terminal->curr_attr.fg = code - 30;
1716 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1717 terminal->curr_attr.fg += 8;
1718 } else if(code >= 40 && code <= 47) {
1719 terminal->curr_attr.bg = code - 40;
1720 } else if (code >= 90 && code <= 97) {
1721 terminal->curr_attr.fg = code - 90 + 8;
1722 } else if (code >= 100 && code <= 107) {
1723 terminal->curr_attr.bg = code - 100 + 8;
1724 } else if(code >= 256 && code < 512) {
1725 terminal->curr_attr.fg = code - 256;
1726 } else if(code >= 512 && code < 768) {
1727 terminal->curr_attr.bg = code - 512;
1729 fprintf(stderr, "Unknown SGR code: %d\n", code);
1735 /* Returns 1 if c was special, otherwise 0 */
1737 handle_special_char(struct terminal *terminal, char c)
1739 union utf8_char *row;
1740 struct attr *attr_row;
1742 row = terminal_get_row(terminal, terminal->row);
1743 attr_row = terminal_get_attr_row(terminal, terminal->row);
1747 terminal->column = 0;
1750 if (terminal->mode & MODE_LF_NEWLINE) {
1751 terminal->column = 0;
1757 if(terminal->row > terminal->margin_bottom) {
1758 terminal->row = terminal->margin_bottom;
1759 terminal_scroll(terminal, +1);
1764 while (terminal->column < terminal->width) {
1765 if (terminal->mode & MODE_IRM)
1766 terminal_shift_line(terminal, +1);
1768 if (row[terminal->column].byte[0] == '\0') {
1769 row[terminal->column].byte[0] = ' ';
1770 row[terminal->column].byte[1] = '\0';
1771 attr_row[terminal->column] = terminal->curr_attr;
1775 if (terminal->tab_ruler[terminal->column]) break;
1777 if (terminal->column >= terminal->width) {
1778 terminal->column = terminal->width - 1;
1783 if (terminal->column >= terminal->width) {
1784 terminal->column = terminal->width - 2;
1785 } else if (terminal->column > 0) {
1787 } else if (terminal->mode & MODE_AUTOWRAP) {
1788 terminal->column = terminal->width - 1;
1790 if (terminal->row < terminal->margin_top) {
1791 terminal->row = terminal->margin_top;
1792 terminal_scroll(terminal, -1);
1800 case '\x0E': /* SO */
1801 terminal->cs = terminal->g1;
1803 case '\x0F': /* SI */
1804 terminal->cs = terminal->g0;
1816 handle_char(struct terminal *terminal, union utf8_char utf8)
1818 union utf8_char *row;
1819 struct attr *attr_row;
1821 if (handle_special_char(terminal, utf8.byte[0])) return;
1823 apply_char_set(terminal->cs, &utf8);
1825 /* There are a whole lot of non-characters, control codes,
1826 * and formatting codes that should probably be ignored,
1828 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1833 /* Some of these non-characters should be translated, e.g.: */
1834 if (utf8.byte[0] < 32) {
1835 utf8.byte[0] = utf8.byte[0] + 64;
1838 /* handle right margin effects */
1839 if (terminal->column >= terminal->width) {
1840 if (terminal->mode & MODE_AUTOWRAP) {
1841 terminal->column = 0;
1843 if (terminal->row > terminal->margin_bottom) {
1844 terminal->row = terminal->margin_bottom;
1845 terminal_scroll(terminal, +1);
1852 row = terminal_get_row(terminal, terminal->row);
1853 attr_row = terminal_get_attr_row(terminal, terminal->row);
1855 if (terminal->mode & MODE_IRM)
1856 terminal_shift_line(terminal, +1);
1857 row[terminal->column] = utf8;
1858 attr_row[terminal->column++] = terminal->curr_attr;
1860 if (utf8.ch != terminal->last_char.ch)
1861 terminal->last_char = utf8;
1865 escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1869 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1870 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1871 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1872 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1873 else len = 1; /* Invalid, cannot happen */
1875 if (terminal->escape_length + len <= MAX_ESCAPE) {
1876 for (i = 0; i < len; i++)
1877 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1878 terminal->escape_length += len;
1879 } else if (terminal->escape_length < MAX_ESCAPE) {
1880 terminal->escape[terminal->escape_length++] = 0;
1885 terminal_data(struct terminal *terminal, const char *data, size_t length)
1888 union utf8_char utf8;
1889 enum utf8_state parser_state;
1891 for (i = 0; i < length; i++) {
1893 utf8_next_char(&terminal->state_machine, data[i]);
1894 switch(parser_state) {
1895 case utf8state_accept:
1896 utf8.ch = terminal->state_machine.s.ch;
1898 case utf8state_reject:
1899 /* the unicode replacement character */
1900 utf8.byte[0] = 0xEF;
1901 utf8.byte[1] = 0xBF;
1902 utf8.byte[2] = 0xBD;
1903 utf8.byte[3] = 0x00;
1909 /* assume escape codes never use non-ASCII characters */
1910 switch (terminal->state) {
1911 case escape_state_escape:
1912 escape_append_utf8(terminal, utf8);
1913 switch (utf8.byte[0]) {
1915 terminal->state = escape_state_dcs;
1918 terminal->state = escape_state_csi;
1921 terminal->state = escape_state_osc;
1925 case ')': /* special */
1926 terminal->state = escape_state_special;
1928 case '^': /* PM (not implemented) */
1929 case '_': /* APC (not implemented) */
1930 terminal->state = escape_state_ignore;
1933 terminal->state = escape_state_normal;
1934 handle_non_csi_escape(terminal, utf8.byte[0]);
1938 case escape_state_csi:
1939 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1941 } else if (utf8.byte[0] == '?') {
1942 terminal->escape_flags |= ESC_FLAG_WHAT;
1943 } else if (utf8.byte[0] == '>') {
1944 terminal->escape_flags |= ESC_FLAG_GT;
1945 } else if (utf8.byte[0] == '!') {
1946 terminal->escape_flags |= ESC_FLAG_BANG;
1947 } else if (utf8.byte[0] == '$') {
1948 terminal->escape_flags |= ESC_FLAG_CASH;
1949 } else if (utf8.byte[0] == '\'') {
1950 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1951 } else if (utf8.byte[0] == '"') {
1952 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1953 } else if (utf8.byte[0] == ' ') {
1954 terminal->escape_flags |= ESC_FLAG_SPACE;
1956 escape_append_utf8(terminal, utf8);
1957 if (terminal->escape_length >= MAX_ESCAPE)
1958 terminal->state = escape_state_normal;
1961 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1962 utf8.byte[0] == '`')
1964 terminal->state = escape_state_normal;
1965 handle_escape(terminal);
1969 case escape_state_inner_escape:
1970 if (utf8.byte[0] == '\\') {
1971 terminal->state = escape_state_normal;
1972 if (terminal->outer_state == escape_state_dcs) {
1973 handle_dcs(terminal);
1974 } else if (terminal->outer_state == escape_state_osc) {
1975 handle_osc(terminal);
1977 } else if (utf8.byte[0] == '\e') {
1978 terminal->state = terminal->outer_state;
1979 escape_append_utf8(terminal, utf8);
1980 if (terminal->escape_length >= MAX_ESCAPE)
1981 terminal->state = escape_state_normal;
1983 terminal->state = terminal->outer_state;
1984 if (terminal->escape_length < MAX_ESCAPE)
1985 terminal->escape[terminal->escape_length++] = '\e';
1986 escape_append_utf8(terminal, utf8);
1987 if (terminal->escape_length >= MAX_ESCAPE)
1988 terminal->state = escape_state_normal;
1991 case escape_state_dcs:
1992 case escape_state_osc:
1993 case escape_state_ignore:
1994 if (utf8.byte[0] == '\e') {
1995 terminal->outer_state = terminal->state;
1996 terminal->state = escape_state_inner_escape;
1997 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
1998 terminal->state = escape_state_normal;
1999 handle_osc(terminal);
2001 escape_append_utf8(terminal, utf8);
2002 if (terminal->escape_length >= MAX_ESCAPE)
2003 terminal->state = escape_state_normal;
2006 case escape_state_special:
2007 escape_append_utf8(terminal, utf8);
2008 terminal->state = escape_state_normal;
2009 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2010 handle_special_escape(terminal, terminal->escape[1],
2018 /* this is valid, because ASCII characters are never used to
2019 * introduce a multibyte sequence in UTF-8 */
2020 if (utf8.byte[0] == '\e') {
2021 terminal->state = escape_state_escape;
2022 terminal->outer_state = escape_state_normal;
2023 terminal->escape[0] = '\e';
2024 terminal->escape_length = 1;
2025 terminal->escape_flags = 0;
2027 handle_char(terminal, utf8);
2031 window_schedule_redraw(terminal->window);
2035 data_source_target(void *data,
2036 struct wl_data_source *source, const char *mime_type)
2038 fprintf(stderr, "data_source_target, %s\n", mime_type);
2042 data_source_send(void *data,
2043 struct wl_data_source *source,
2044 const char *mime_type, int32_t fd)
2046 struct terminal *terminal = data;
2048 terminal_send_selection(terminal, fd);
2052 data_source_cancelled(void *data, struct wl_data_source *source)
2054 wl_data_source_destroy(source);
2057 static const struct wl_data_source_listener data_source_listener = {
2060 data_source_cancelled
2064 fullscreen_handler(struct window *window, void *data)
2066 struct terminal *terminal = data;
2068 terminal->fullscreen ^= 1;
2069 window_set_fullscreen(window, terminal->fullscreen);
2073 handle_bound_key(struct terminal *terminal,
2074 struct input *input, uint32_t sym, uint32_t time)
2076 struct terminal *new_terminal;
2080 /* Cut selection; terminal doesn't do cut, fall
2081 * through to copy. */
2083 terminal->selection =
2084 display_create_data_source(terminal->display);
2085 wl_data_source_offer(terminal->selection,
2086 "text/plain;charset=utf-8");
2087 wl_data_source_add_listener(terminal->selection,
2088 &data_source_listener, terminal);
2089 input_set_selection(input, terminal->selection,
2090 display_get_serial(terminal->display));
2093 input_receive_selection_data_to_fd(input,
2094 "text/plain;charset=utf-8",
2101 terminal_create(terminal->display, option_fullscreen);
2102 if (terminal_run(new_terminal, option_shell))
2103 terminal_destroy(new_terminal);
2113 key_handler(struct window *window, struct input *input, uint32_t time,
2114 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2117 struct terminal *terminal = data;
2118 char ch[MAX_RESPONSE];
2119 uint32_t modifiers, serial;
2121 bool convert_utf8 = true;
2123 modifiers = input_get_modifiers(input);
2124 if ((modifiers & MOD_CONTROL_MASK) &&
2125 (modifiers & MOD_SHIFT_MASK) &&
2126 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2127 handle_bound_key(terminal, input, sym, time))
2131 case XKB_KEY_BackSpace:
2132 if (modifiers & MOD_ALT_MASK)
2137 case XKB_KEY_Linefeed:
2140 case XKB_KEY_Scroll_Lock:
2141 case XKB_KEY_Sys_Req:
2142 case XKB_KEY_Escape:
2143 ch[len++] = sym & 0x7f;
2146 case XKB_KEY_Return:
2147 if (terminal->mode & MODE_LF_NEWLINE) {
2155 case XKB_KEY_Shift_L:
2156 case XKB_KEY_Shift_R:
2157 case XKB_KEY_Control_L:
2158 case XKB_KEY_Control_R:
2161 case XKB_KEY_Meta_L:
2162 case XKB_KEY_Meta_R:
2163 case XKB_KEY_Super_L:
2164 case XKB_KEY_Super_R:
2165 case XKB_KEY_Hyper_L:
2166 case XKB_KEY_Hyper_R:
2169 case XKB_KEY_Insert:
2170 len = function_key_response('[', 2, modifiers, '~', ch);
2172 case XKB_KEY_Delete:
2173 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2176 len = function_key_response('[', 3, modifiers, '~', ch);
2179 case XKB_KEY_Page_Up:
2180 len = function_key_response('[', 5, modifiers, '~', ch);
2182 case XKB_KEY_Page_Down:
2183 len = function_key_response('[', 6, modifiers, '~', ch);
2186 len = function_key_response('O', 1, modifiers, 'P', ch);
2189 len = function_key_response('O', 1, modifiers, 'Q', ch);
2192 len = function_key_response('O', 1, modifiers, 'R', ch);
2195 len = function_key_response('O', 1, modifiers, 'S', ch);
2198 len = function_key_response('[', 15, modifiers, '~', ch);
2201 len = function_key_response('[', 17, modifiers, '~', ch);
2204 len = function_key_response('[', 18, modifiers, '~', ch);
2207 len = function_key_response('[', 19, modifiers, '~', ch);
2210 len = function_key_response('[', 20, modifiers, '~', ch);
2213 len = function_key_response('[', 21, modifiers, '~', ch);
2216 len = function_key_response('[', 24, modifiers, '~', ch);
2219 /* Handle special keys with alternate mappings */
2220 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2221 if (len != 0) break;
2223 if (modifiers & MOD_CONTROL_MASK) {
2224 if (sym >= '3' && sym <= '7')
2225 sym = (sym & 0x1f) + 8;
2227 if (!((sym >= '!' && sym <= '/') ||
2228 (sym >= '8' && sym <= '?') ||
2229 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2230 else if (sym == '2') sym = 0x00;
2231 else if (sym == '/') sym = 0x1F;
2232 else if (sym == '8' || sym == '?') sym = 0x7F;
2234 if (modifiers & MOD_ALT_MASK) {
2235 if (terminal->mode & MODE_ALT_SENDS_ESC) {
2239 convert_utf8 = false;
2244 (!convert_utf8 && sym < 256)) {
2247 ret = xkb_keysym_to_utf8(sym, ch + len,
2248 MAX_RESPONSE - len);
2251 "Warning: buffer too small to encode "
2252 "UTF8 character\n");
2260 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
2261 terminal_write(terminal, ch, len);
2263 /* Hide cursor, except if this was coming from a
2264 * repeating key press. */
2265 serial = display_get_serial(terminal->display);
2266 if (terminal->hide_cursor_serial != serial) {
2267 input_set_pointer_image(input, CURSOR_BLANK);
2268 terminal->hide_cursor_serial = serial;
2274 keyboard_focus_handler(struct window *window,
2275 struct input *device, void *data)
2277 struct terminal *terminal = data;
2279 window_schedule_redraw(terminal->window);
2282 static int wordsep(int ch)
2284 const char extra[] = "-,./?%&#:_=+@~";
2289 return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
2293 recompute_selection(struct terminal *terminal)
2295 struct rectangle allocation;
2296 int col, x, width, height;
2297 int start_row, end_row;
2298 int word_start, eol;
2299 int side_margin, top_margin;
2302 union utf8_char *data;
2304 cw = terminal->extents.max_x_advance;
2305 ch = terminal->extents.height;
2306 widget_get_allocation(terminal->widget, &allocation);
2307 width = terminal->width * cw;
2308 height = terminal->height * ch;
2309 side_margin = allocation.x + (allocation.width - width) / 2;
2310 top_margin = allocation.y + (allocation.height - height) / 2;
2312 start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1;
2313 end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1;
2315 if (start_row < end_row ||
2316 (start_row == end_row &&
2317 terminal->selection_start_x < terminal->selection_end_x)) {
2318 terminal->selection_start_row = start_row;
2319 terminal->selection_end_row = end_row;
2320 start_x = terminal->selection_start_x;
2321 end_x = terminal->selection_end_x;
2323 terminal->selection_start_row = end_row;
2324 terminal->selection_end_row = start_row;
2325 start_x = terminal->selection_end_x;
2326 end_x = terminal->selection_start_x;
2330 if (terminal->selection_start_row < 0) {
2331 terminal->selection_start_row = 0;
2332 terminal->selection_start_col = 0;
2334 x = side_margin + cw / 2;
2335 data = terminal_get_row(terminal,
2336 terminal->selection_start_row);
2338 for (col = 0; col < terminal->width; col++, x += cw) {
2339 if (col == 0 || wordsep(data[col - 1].ch))
2341 if (data[col].ch != 0)
2347 switch (terminal->dragging) {
2349 terminal->selection_start_col = 0;
2352 terminal->selection_start_col = word_start;
2355 terminal->selection_start_col = col;
2360 if (terminal->selection_end_row >= terminal->height) {
2361 terminal->selection_end_row = terminal->height;
2362 terminal->selection_end_col = 0;
2364 x = side_margin + cw / 2;
2365 data = terminal_get_row(terminal, terminal->selection_end_row);
2366 for (col = 0; col < terminal->width; col++, x += cw) {
2367 if (terminal->dragging == SELECT_CHAR && end_x < x)
2369 if (terminal->dragging == SELECT_WORD &&
2370 end_x < x && wordsep(data[col].ch))
2373 terminal->selection_end_col = col;
2376 if (terminal->selection_end_col != terminal->selection_start_col ||
2377 terminal->selection_start_row != terminal->selection_end_row) {
2378 col = terminal->selection_end_col;
2379 if (col > 0 && data[col - 1].ch == 0)
2380 terminal->selection_end_col = terminal->width;
2381 data = terminal_get_row(terminal, terminal->selection_start_row);
2382 if (data[terminal->selection_start_col].ch == 0)
2383 terminal->selection_start_col = eol;
2390 button_handler(struct widget *widget,
2391 struct input *input, uint32_t time,
2393 enum wl_pointer_button_state state, void *data)
2395 struct terminal *terminal = data;
2399 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2401 if (time - terminal->button_time < 500)
2402 terminal->click_count++;
2404 terminal->click_count = 1;
2406 terminal->button_time = time;
2407 terminal->dragging =
2408 (terminal->click_count - 1) % 3 + SELECT_CHAR;
2410 input_get_position(input,
2411 &terminal->selection_start_x,
2412 &terminal->selection_start_y);
2413 terminal->selection_end_x = terminal->selection_start_x;
2414 terminal->selection_end_y = terminal->selection_start_y;
2415 if (recompute_selection(terminal))
2416 widget_schedule_redraw(widget);
2418 terminal->dragging = SELECT_NONE;
2425 enter_handler(struct widget *widget,
2426 struct input *input, float x, float y, void *data)
2428 return CURSOR_IBEAM;
2432 motion_handler(struct widget *widget,
2433 struct input *input, uint32_t time,
2434 float x, float y, void *data)
2436 struct terminal *terminal = data;
2438 if (terminal->dragging) {
2439 input_get_position(input,
2440 &terminal->selection_end_x,
2441 &terminal->selection_end_y);
2443 if (recompute_selection(terminal))
2444 widget_schedule_redraw(widget);
2447 return CURSOR_IBEAM;
2450 static struct terminal *
2451 terminal_create(struct display *display, int fullscreen)
2453 struct terminal *terminal;
2454 cairo_surface_t *surface;
2457 terminal = malloc(sizeof *terminal);
2458 if (terminal == NULL)
2461 memset(terminal, 0, sizeof *terminal);
2462 terminal->fullscreen = fullscreen;
2463 terminal->color_scheme = &DEFAULT_COLORS;
2464 terminal_init(terminal);
2465 terminal->margin_top = 0;
2466 terminal->margin_bottom = -1;
2467 terminal->window = window_create(display);
2468 terminal->widget = frame_create(terminal->window, terminal);
2469 window_set_title(terminal->window, "Wayland Terminal");
2470 widget_set_transparent(terminal->widget, 0);
2472 init_state_machine(&terminal->state_machine);
2473 init_color_table(terminal);
2475 terminal->display = display;
2476 terminal->margin = 5;
2478 window_set_user_data(terminal->window, terminal);
2479 window_set_key_handler(terminal->window, key_handler);
2480 window_set_keyboard_focus_handler(terminal->window,
2481 keyboard_focus_handler);
2482 window_set_fullscreen_handler(terminal->window, fullscreen_handler);
2484 widget_set_redraw_handler(terminal->widget, redraw_handler);
2485 widget_set_resize_handler(terminal->widget, resize_handler);
2486 widget_set_button_handler(terminal->widget, button_handler);
2487 widget_set_enter_handler(terminal->widget, enter_handler);
2488 widget_set_motion_handler(terminal->widget, motion_handler);
2490 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2491 cr = cairo_create(surface);
2492 cairo_set_font_size(cr, option_font_size);
2493 cairo_select_font_face (cr, option_font,
2494 CAIRO_FONT_SLANT_NORMAL,
2495 CAIRO_FONT_WEIGHT_BOLD);
2496 terminal->font_bold = cairo_get_scaled_font (cr);
2497 cairo_scaled_font_reference(terminal->font_bold);
2499 cairo_select_font_face (cr, option_font,
2500 CAIRO_FONT_SLANT_NORMAL,
2501 CAIRO_FONT_WEIGHT_NORMAL);
2502 terminal->font_normal = cairo_get_scaled_font (cr);
2503 cairo_scaled_font_reference(terminal->font_normal);
2505 cairo_font_extents(cr, &terminal->extents);
2507 cairo_surface_destroy(surface);
2509 terminal_resize(terminal, 20, 5); /* Set minimum size first */
2510 terminal_resize(terminal, 80, 25);
2512 wl_list_insert(terminal_list.prev, &terminal->link);
2518 terminal_destroy(struct terminal *terminal)
2520 window_destroy(terminal->window);
2521 close(terminal->master);
2522 wl_list_remove(&terminal->link);
2525 if (wl_list_empty(&terminal_list))
2530 io_handler(struct task *task, uint32_t events)
2532 struct terminal *terminal =
2533 container_of(task, struct terminal, io_task);
2537 if (events & EPOLLHUP) {
2538 terminal_destroy(terminal);
2542 len = read(terminal->master, buffer, sizeof buffer);
2544 terminal_destroy(terminal);
2546 terminal_data(terminal, buffer, len);
2550 terminal_run(struct terminal *terminal, const char *path)
2555 pid = forkpty(&master, NULL, NULL, NULL);
2557 setenv("TERM", option_term, 1);
2558 setenv("COLORTERM", option_term, 1);
2559 if (execl(path, path, NULL)) {
2560 printf("exec failed: %m\n");
2563 } else if (pid < 0) {
2564 fprintf(stderr, "failed to fork and create pty (%m).\n");
2568 terminal->master = master;
2569 fcntl(master, F_SETFL, O_NONBLOCK);
2570 terminal->io_task.run = io_handler;
2571 display_watch_fd(terminal->display, terminal->master,
2572 EPOLLIN | EPOLLHUP, &terminal->io_task);
2574 window_set_fullscreen(terminal->window, terminal->fullscreen);
2575 if (!terminal->fullscreen)
2576 terminal_resize(terminal, 80, 24);
2581 static const struct config_key terminal_config_keys[] = {
2582 { "font", CONFIG_KEY_STRING, &option_font },
2583 { "font-size", CONFIG_KEY_INTEGER, &option_font_size },
2584 { "term", CONFIG_KEY_STRING, &option_term },
2587 static const struct config_section config_sections[] = {
2589 terminal_config_keys, ARRAY_LENGTH(terminal_config_keys) },
2592 static const struct weston_option terminal_options[] = {
2593 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
2594 { WESTON_OPTION_STRING, "font", 0, &option_font },
2595 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
2598 int main(int argc, char *argv[])
2601 struct terminal *terminal;
2604 option_shell = getenv("SHELL");
2606 option_shell = "/bin/bash";
2608 config_file = config_file_path("weston.ini");
2609 parse_config_file(config_file,
2610 config_sections, ARRAY_LENGTH(config_sections),
2614 argc = parse_options(terminal_options,
2615 ARRAY_LENGTH(terminal_options), argc, argv);
2617 d = display_create(argc, argv);
2619 fprintf(stderr, "failed to create display: %m\n");
2623 wl_list_init(&terminal_list);
2624 terminal = terminal_create(d, option_fullscreen);
2625 if (terminal_run(terminal, option_shell))