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);
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;
407 struct color_scheme *color_scheme;
408 struct terminal_color color_table[256];
409 cairo_font_extents_t extents;
410 cairo_scaled_font_t *font_normal, *font_bold;
411 uint32_t hide_cursor_serial;
413 struct wl_data_source *selection;
414 uint32_t button_time;
415 int dragging, click_count;
416 int selection_start_x, selection_start_y;
417 int selection_end_x, selection_end_y;
418 int selection_start_row, selection_start_col;
419 int selection_end_row, selection_end_col;
423 /* Create default tab stops, every 8 characters */
425 terminal_init_tabs(struct terminal *terminal)
429 while (i < terminal->width) {
431 terminal->tab_ruler[i] = 1;
433 terminal->tab_ruler[i] = 0;
439 terminal_init(struct terminal *terminal)
441 terminal->curr_attr = terminal->color_scheme->default_attr;
442 terminal->origin_mode = 0;
443 terminal->mode = MODE_SHOW_CURSOR |
449 terminal->column = 0;
451 terminal->g0 = CS_US;
452 terminal->g1 = CS_US;
453 terminal->cs = terminal->g0;
454 terminal->key_mode = KM_NORMAL;
456 terminal->saved_g0 = terminal->g0;
457 terminal->saved_g1 = terminal->g1;
458 terminal->saved_cs = terminal->cs;
460 terminal->saved_attr = terminal->curr_attr;
461 terminal->saved_origin_mode = terminal->origin_mode;
462 terminal->saved_row = terminal->row;
463 terminal->saved_column = terminal->column;
465 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
469 init_color_table(struct terminal *terminal)
472 struct terminal_color *color_table = terminal->color_table;
474 for (c = 0; c < 256; c ++) {
476 color_table[c] = terminal->color_scheme->palette[c];
477 } else if (c < 232) {
479 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
480 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
481 color_table[c].r = ((double)(r % 6) / 6.0);
482 color_table[c].a = 1.0;
484 r = (c - 232) * 10 + 8;
485 color_table[c].r = ((double) r) / 256.0;
486 color_table[c].g = color_table[c].r;
487 color_table[c].b = color_table[c].r;
488 color_table[c].a = 1.0;
493 static union utf8_char *
494 terminal_get_row(struct terminal *terminal, int row)
498 index = (row + terminal->start) % terminal->height;
500 return &terminal->data[index * terminal->width];
504 terminal_get_attr_row(struct terminal *terminal, int row)
508 index = (row + terminal->start) % terminal->height;
510 return &terminal->data_attr[index * terminal->width];
519 terminal_decode_attr(struct terminal *terminal, int row, int col,
520 union decoded_attr *decoded)
523 int foreground, background, tmp;
526 if (((row == terminal->selection_start_row &&
527 col >= terminal->selection_start_col) ||
528 row > terminal->selection_start_row) &&
529 ((row == terminal->selection_end_row &&
530 col < terminal->selection_end_col) ||
531 row < terminal->selection_end_row))
534 /* get the attributes for this character cell */
535 attr = terminal_get_attr_row(terminal, row)[col];
536 if ((attr.a & ATTRMASK_INVERSE) ||
538 ((terminal->mode & MODE_SHOW_CURSOR) &&
539 window_has_focus(terminal->window) && terminal->row == row &&
540 terminal->column == col)) {
541 foreground = attr.bg;
542 background = attr.fg;
543 if (attr.a & ATTRMASK_BOLD) {
544 if (foreground <= 16) foreground |= 0x08;
545 if (background <= 16) background &= 0x07;
548 foreground = attr.fg;
549 background = attr.bg;
552 if (terminal->mode & MODE_INVERSE) {
554 foreground = background;
556 if (attr.a & ATTRMASK_BOLD) {
557 if (foreground <= 16) foreground |= 0x08;
558 if (background <= 16) background &= 0x07;
562 decoded->attr.fg = foreground;
563 decoded->attr.bg = background;
564 decoded->attr.a = attr.a;
569 terminal_scroll_buffer(struct terminal *terminal, int d)
573 d = d % (terminal->height + 1);
574 terminal->start = (terminal->start + d) % terminal->height;
575 if (terminal->start < 0) terminal->start = terminal->height + terminal->start;
578 for(i = 0; i < d; i++) {
579 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
580 attr_init(terminal_get_attr_row(terminal, i),
581 terminal->curr_attr, terminal->width);
584 for(i = terminal->height - d; i < terminal->height; i++) {
585 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
586 attr_init(terminal_get_attr_row(terminal, i),
587 terminal->curr_attr, terminal->width);
591 terminal->selection_start_row -= d;
592 terminal->selection_end_row -= d;
596 terminal_scroll_window(struct terminal *terminal, int d)
600 int from_row, to_row;
602 // scrolling range is inclusive
603 window_height = terminal->margin_bottom - terminal->margin_top + 1;
604 d = d % (window_height + 1);
607 to_row = terminal->margin_bottom;
608 from_row = terminal->margin_bottom - d;
610 for (i = 0; i < (window_height - d); i++) {
611 memcpy(terminal_get_row(terminal, to_row - i),
612 terminal_get_row(terminal, from_row - i),
613 terminal->data_pitch);
614 memcpy(terminal_get_attr_row(terminal, to_row - i),
615 terminal_get_attr_row(terminal, from_row - i),
616 terminal->attr_pitch);
618 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
619 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
620 attr_init(terminal_get_attr_row(terminal, i),
621 terminal->curr_attr, terminal->width);
624 to_row = terminal->margin_top;
625 from_row = terminal->margin_top + d;
627 for (i = 0; i < (window_height - d); i++) {
628 memcpy(terminal_get_row(terminal, to_row + i),
629 terminal_get_row(terminal, from_row + i),
630 terminal->data_pitch);
631 memcpy(terminal_get_attr_row(terminal, to_row + i),
632 terminal_get_attr_row(terminal, from_row + i),
633 terminal->attr_pitch);
635 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
636 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
637 attr_init(terminal_get_attr_row(terminal, i),
638 terminal->curr_attr, terminal->width);
644 terminal_scroll(struct terminal *terminal, int d)
646 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
647 terminal_scroll_buffer(terminal, d);
649 terminal_scroll_window(terminal, d);
653 terminal_shift_line(struct terminal *terminal, int d)
655 union utf8_char *row;
656 struct attr *attr_row;
658 row = terminal_get_row(terminal, terminal->row);
659 attr_row = terminal_get_attr_row(terminal, terminal->row);
661 if ((terminal->width + d) <= terminal->column)
662 d = terminal->column + 1 - terminal->width;
663 if ((terminal->column + d) >= terminal->width)
664 d = terminal->width - terminal->column - 1;
668 memmove(&row[terminal->column],
669 &row[terminal->column + d],
670 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
671 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
672 (terminal->width - terminal->column - d) * sizeof(struct attr));
673 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
674 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
676 memmove(&row[terminal->column + d], &row[terminal->column],
677 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
678 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
679 (terminal->width - terminal->column - d) * sizeof(struct attr));
680 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
681 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
686 terminal_resize_cells(struct terminal *terminal, int width, int height)
689 union utf8_char *data;
690 struct attr *data_attr;
692 int data_pitch, attr_pitch;
693 int i, l, total_rows;
694 struct rectangle allocation;
697 if (terminal->width == width && terminal->height == height)
700 data_pitch = width * sizeof(union utf8_char);
701 size = data_pitch * height;
703 attr_pitch = width * sizeof(struct attr);
704 data_attr = malloc(attr_pitch * height);
705 tab_ruler = malloc(width);
706 memset(data, 0, size);
707 memset(tab_ruler, 0, width);
708 attr_init(data_attr, terminal->curr_attr, width * height);
709 if (terminal->data && terminal->data_attr) {
710 if (width > terminal->width)
715 if (terminal->height > height) {
718 total_rows = terminal->height;
721 for (i = 0; i < total_rows; i++) {
722 memcpy(&data[width * i],
723 terminal_get_row(terminal, i),
724 l * sizeof(union utf8_char));
725 memcpy(&data_attr[width * i],
726 terminal_get_attr_row(terminal, i),
727 l * sizeof(struct attr));
730 free(terminal->data);
731 free(terminal->data_attr);
732 free(terminal->tab_ruler);
735 terminal->data_pitch = data_pitch;
736 terminal->attr_pitch = attr_pitch;
737 terminal->margin_bottom =
738 height - (terminal->height - terminal->margin_bottom);
739 terminal->width = width;
740 terminal->height = height;
741 terminal->data = data;
742 terminal->data_attr = data_attr;
743 terminal->tab_ruler = tab_ruler;
744 terminal_init_tabs(terminal);
746 /* Update the window size */
747 ws.ws_row = terminal->height;
748 ws.ws_col = terminal->width;
749 widget_get_allocation(terminal->widget, &allocation);
750 ws.ws_xpixel = allocation.width;
751 ws.ws_ypixel = allocation.height;
752 ioctl(terminal->master, TIOCSWINSZ, &ws);
756 resize_handler(struct widget *widget,
757 int32_t width, int32_t height, void *data)
759 struct terminal *terminal = data;
760 int32_t columns, rows, m;
762 m = 2 * terminal->margin;
763 columns = (width - m) / (int32_t) terminal->extents.max_x_advance;
764 rows = (height - m) / (int32_t) terminal->extents.height;
766 if (!window_is_fullscreen(terminal->window) &&
767 !window_is_maximized(terminal->window)) {
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 (window_is_fullscreen(terminal->window) ||
782 window_is_maximized(terminal->window))
785 m = 2 * terminal->margin;
786 width = columns * terminal->extents.max_x_advance + m;
787 height = rows * terminal->extents.height + m;
789 frame_set_child_size(terminal->widget, width, height);
792 struct color_scheme DEFAULT_COLORS = {
794 {0, 0, 0, 1}, /* black */
795 {0.66, 0, 0, 1}, /* red */
796 {0 , 0.66, 0, 1}, /* green */
797 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
798 {0 , 0 , 0.66, 1}, /* blue */
799 {0.66, 0 , 0.66, 1}, /* magenta */
800 {0, 0.66, 0.66, 1}, /* cyan */
801 {0.66, 0.66, 0.66, 1}, /* light grey */
802 {0.22, 0.33, 0.33, 1}, /* dark grey */
803 {1, 0.33, 0.33, 1}, /* high red */
804 {0.33, 1, 0.33, 1}, /* high green */
805 {1, 1, 0.33, 1}, /* high yellow */
806 {0.33, 0.33, 1, 1}, /* high blue */
807 {1, 0.33, 1, 1}, /* high magenta */
808 {0.33, 1, 1, 1}, /* high cyan */
809 {1, 1, 1, 1} /* white */
811 0, /* black border */
812 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
816 terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
818 cairo_set_source_rgba(cr,
819 terminal->color_table[index].r,
820 terminal->color_table[index].g,
821 terminal->color_table[index].b,
822 terminal->color_table[index].a);
826 terminal_send_selection(struct terminal *terminal, int fd)
829 union utf8_char *p_row;
830 union decoded_attr attr;
834 fp = fdopen(fd, "w");
835 for (row = 0; row < terminal->height; row++) {
836 p_row = terminal_get_row(terminal, row);
837 for (col = 0; col < terminal->width; col++) {
838 /* get the attributes for this character cell */
839 terminal_decode_attr(terminal, row, col, &attr);
842 len = strnlen((char *) p_row[col].byte, 4);
844 fwrite(p_row[col].byte, 1, len, fp);
845 if (len == 0 || col == terminal->width - 1) {
846 fwrite("\n", 1, 1, fp);
855 struct terminal *terminal;
858 union decoded_attr attr;
859 cairo_glyph_t glyphs[256], *g;
863 glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
865 run->terminal = terminal;
867 run->g = run->glyphs;
873 glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
875 cairo_scaled_font_t *font;
877 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
878 (attr.key != run->attr.key)) {
879 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
880 font = run->terminal->font_bold;
882 font = run->terminal->font_normal;
883 cairo_set_scaled_font(run->cr, font);
884 terminal_set_color(run->terminal, run->cr,
887 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
888 cairo_show_glyphs (run->cr, run->glyphs, run->count);
889 run->g = run->glyphs;
896 glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
899 cairo_scaled_font_t *font;
901 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
903 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
904 font = run->terminal->font_bold;
906 font = run->terminal->font_normal;
908 cairo_move_to(run->cr, x, y);
909 cairo_scaled_font_text_to_glyphs (font, x, y,
911 &run->g, &num_glyphs,
913 run->g += num_glyphs;
914 run->count += num_glyphs;
919 redraw_handler(struct widget *widget, void *data)
921 struct terminal *terminal = data;
922 struct rectangle allocation;
924 int top_margin, side_margin;
925 int row, col, cursor_x, cursor_y;
926 union utf8_char *p_row;
927 union decoded_attr attr;
929 cairo_surface_t *surface;
931 struct glyph_run run;
932 cairo_font_extents_t extents;
934 surface = window_get_surface(terminal->window);
935 widget_get_allocation(terminal->widget, &allocation);
936 cr = cairo_create(surface);
937 cairo_rectangle(cr, allocation.x, allocation.y,
938 allocation.width, allocation.height);
940 cairo_push_group(cr);
942 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
943 terminal_set_color(terminal, cr, terminal->color_scheme->border);
946 cairo_set_scaled_font(cr, terminal->font_normal);
948 extents = terminal->extents;
949 side_margin = (allocation.width - terminal->width * extents.max_x_advance) / 2;
950 top_margin = (allocation.height - terminal->height * extents.height) / 2;
952 cairo_set_line_width(cr, 1.0);
953 cairo_translate(cr, allocation.x + side_margin,
954 allocation.y + top_margin);
955 /* paint the background */
956 for (row = 0; row < terminal->height; row++) {
957 for (col = 0; col < terminal->width; col++) {
958 /* get the attributes for this character cell */
959 terminal_decode_attr(terminal, row, col, &attr);
961 if (attr.attr.bg == terminal->color_scheme->border)
964 terminal_set_color(terminal, cr, attr.attr.bg);
965 cairo_move_to(cr, col * extents.max_x_advance,
966 row * extents.height);
967 cairo_rel_line_to(cr, extents.max_x_advance, 0);
968 cairo_rel_line_to(cr, 0, extents.height);
969 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
970 cairo_close_path(cr);
975 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
977 /* paint the foreground */
978 glyph_run_init(&run, terminal, cr);
979 for (row = 0; row < terminal->height; row++) {
980 p_row = terminal_get_row(terminal, row);
981 for (col = 0; col < terminal->width; col++) {
982 /* get the attributes for this character cell */
983 terminal_decode_attr(terminal, row, col, &attr);
985 glyph_run_flush(&run, attr);
987 text_x = col * extents.max_x_advance;
988 text_y = extents.ascent + row * extents.height;
989 if (attr.attr.a & ATTRMASK_UNDERLINE) {
990 terminal_set_color(terminal, cr, attr.attr.fg);
991 cairo_move_to(cr, text_x, (double)text_y + 1.5);
992 cairo_line_to(cr, text_x + extents.max_x_advance, (double) text_y + 1.5);
996 glyph_run_add(&run, text_x, text_y, &p_row[col]);
1001 glyph_run_flush(&run, attr);
1003 if ((terminal->mode & MODE_SHOW_CURSOR) &&
1004 !window_has_focus(terminal->window)) {
1007 cairo_set_line_width(cr, 1);
1008 cairo_move_to(cr, terminal->column * extents.max_x_advance + d,
1009 terminal->row * extents.height + d);
1010 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
1011 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
1012 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
1013 cairo_close_path(cr);
1018 cairo_pop_group_to_source(cr);
1021 cairo_surface_destroy(surface);
1023 if (terminal->send_cursor_position) {
1024 cursor_x = side_margin + allocation.x +
1025 terminal->column * extents.max_x_advance;
1026 cursor_y = top_margin + allocation.y +
1027 terminal->row * extents.height;
1028 window_set_text_cursor_position(terminal->window,
1029 cursor_x, cursor_y);
1030 terminal->send_cursor_position = 0;
1035 terminal_write(struct terminal *terminal, const char *data, size_t length)
1037 if (write(terminal->master, data, length) < 0)
1039 terminal->send_cursor_position = 1;
1043 terminal_data(struct terminal *terminal, const char *data, size_t length);
1046 handle_char(struct terminal *terminal, union utf8_char utf8);
1049 handle_sgr(struct terminal *terminal, int code);
1052 handle_term_parameter(struct terminal *terminal, int code, int sr)
1056 if (terminal->escape_flags & ESC_FLAG_WHAT) {
1058 case 1: /* DECCKM */
1059 if (sr) terminal->key_mode = KM_APPLICATION;
1060 else terminal->key_mode = KM_NORMAL;
1062 case 2: /* DECANM */
1063 /* No VT52 support yet */
1064 terminal->g0 = CS_US;
1065 terminal->g1 = CS_US;
1066 terminal->cs = terminal->g0;
1068 case 3: /* DECCOLM */
1070 terminal_resize(terminal, 132, 24);
1072 terminal_resize(terminal, 80, 24);
1074 /* set columns, but also home cursor and clear screen */
1075 terminal->row = 0; terminal->column = 0;
1076 for (i = 0; i < terminal->height; i++) {
1077 memset(terminal_get_row(terminal, i),
1078 0, terminal->data_pitch);
1079 attr_init(terminal_get_attr_row(terminal, i),
1080 terminal->curr_attr, terminal->width);
1083 case 5: /* DECSCNM */
1084 if (sr) terminal->mode |= MODE_INVERSE;
1085 else terminal->mode &= ~MODE_INVERSE;
1088 terminal->origin_mode = sr;
1089 if (terminal->origin_mode)
1090 terminal->row = terminal->margin_top;
1093 terminal->column = 0;
1095 case 7: /* DECAWM */
1096 if (sr) terminal->mode |= MODE_AUTOWRAP;
1097 else terminal->mode &= ~MODE_AUTOWRAP;
1099 case 8: /* DECARM */
1100 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1101 else terminal->mode &= ~MODE_AUTOREPEAT;
1103 case 12: /* Very visible cursor (CVVIS) */
1104 /* FIXME: What do we do here. */
1107 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1108 else terminal->mode &= ~MODE_SHOW_CURSOR;
1110 case 1034: /* smm/rmm, meta mode on/off */
1113 case 1037: /* deleteSendsDel */
1114 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1115 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1117 case 1039: /* altSendsEscape */
1118 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1119 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1121 case 1049: /* rmcup/smcup, alternate screen */
1122 /* Ignore. Should be possible to implement,
1123 * but it's kind of annoying. */
1126 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1132 if (sr) terminal->mode |= MODE_IRM;
1133 else terminal->mode &= ~MODE_IRM;
1136 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1137 else terminal->mode &= ~MODE_LF_NEWLINE;
1140 fprintf(stderr, "Unknown parameter: %d\n", code);
1147 handle_dcs(struct terminal *terminal)
1152 handle_osc(struct terminal *terminal)
1157 terminal->escape[terminal->escape_length++] = '\0';
1158 p = &terminal->escape[2];
1159 code = strtol(p, &p, 10);
1163 case 0: /* Icon name and window title */
1164 case 1: /* Icon label */
1165 case 2: /* Window title*/
1166 window_set_title(terminal->window, p);
1169 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1175 handle_escape(struct terminal *terminal)
1177 union utf8_char *row;
1178 struct attr *attr_row;
1180 int i, count, x, y, top, bottom;
1181 int args[10], set[10] = { 0, };
1182 char response[MAX_RESPONSE] = {0, };
1183 struct rectangle allocation;
1185 terminal->escape[terminal->escape_length++] = '\0';
1187 p = &terminal->escape[2];
1188 while ((isdigit(*p) || *p == ';') && i < 10) {
1197 args[i] = strtol(p, &p, 10);
1204 count = set[0] ? args[0] : 1;
1205 if (count == 0) count = 1;
1206 terminal_shift_line(terminal, count);
1209 count = set[0] ? args[0] : 1;
1210 if (count == 0) count = 1;
1211 if (terminal->row - count >= terminal->margin_top)
1212 terminal->row -= count;
1214 terminal->row = terminal->margin_top;
1217 count = set[0] ? args[0] : 1;
1218 if (count == 0) count = 1;
1219 if (terminal->row + count <= terminal->margin_bottom)
1220 terminal->row += count;
1222 terminal->row = terminal->margin_bottom;
1225 count = set[0] ? args[0] : 1;
1226 if (count == 0) count = 1;
1227 if ((terminal->column + count) < terminal->width)
1228 terminal->column += count;
1230 terminal->column = terminal->width - 1;
1233 count = set[0] ? args[0] : 1;
1234 if (count == 0) count = 1;
1235 if ((terminal->column - count) >= 0)
1236 terminal->column -= count;
1238 terminal->column = 0;
1241 count = set[0] ? args[0] : 1;
1242 if (terminal->row + count <= terminal->margin_bottom)
1243 terminal->row += count;
1245 terminal->row = terminal->margin_bottom;
1246 terminal->column = 0;
1249 count = set[0] ? args[0] : 1;
1250 if (terminal->row - count >= terminal->margin_top)
1251 terminal->row -= count;
1253 terminal->row = terminal->margin_top;
1254 terminal->column = 0;
1257 y = set[0] ? args[0] : 1;
1258 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1260 terminal->column = y - 1;
1264 x = (set[1] ? args[1] : 1) - 1;
1266 (x >= terminal->width ? terminal->width - 1 : x);
1268 y = (set[0] ? args[0] : 1) - 1;
1269 if (terminal->origin_mode) {
1270 y += terminal->margin_top;
1271 y = y < terminal->margin_top ? terminal->margin_top :
1272 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1275 (y >= terminal->height ? terminal->height - 1 : y);
1279 terminal->column = x;
1282 count = set[0] ? args[0] : 1;
1283 if (count == 0) count = 1;
1284 while (count > 0 && terminal->column < terminal->width) {
1285 if (terminal->tab_ruler[terminal->column]) count--;
1291 row = terminal_get_row(terminal, terminal->row);
1292 attr_row = terminal_get_attr_row(terminal, terminal->row);
1293 if (!set[0] || args[0] == 0 || args[0] > 2) {
1294 memset(&row[terminal->column],
1295 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1296 attr_init(&attr_row[terminal->column],
1297 terminal->curr_attr, terminal->width - terminal->column);
1298 for (i = terminal->row + 1; i < terminal->height; i++) {
1299 memset(terminal_get_row(terminal, i),
1300 0, terminal->data_pitch);
1301 attr_init(terminal_get_attr_row(terminal, i),
1302 terminal->curr_attr, terminal->width);
1304 } else if (args[0] == 1) {
1305 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1306 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1307 for (i = 0; i < terminal->row; i++) {
1308 memset(terminal_get_row(terminal, i),
1309 0, terminal->data_pitch);
1310 attr_init(terminal_get_attr_row(terminal, i),
1311 terminal->curr_attr, terminal->width);
1313 } else if (args[0] == 2) {
1314 for (i = 0; i < terminal->height; i++) {
1315 memset(terminal_get_row(terminal, i),
1316 0, terminal->data_pitch);
1317 attr_init(terminal_get_attr_row(terminal, i),
1318 terminal->curr_attr, terminal->width);
1323 row = terminal_get_row(terminal, terminal->row);
1324 attr_row = terminal_get_attr_row(terminal, terminal->row);
1325 if (!set[0] || args[0] == 0 || args[0] > 2) {
1326 memset(&row[terminal->column], 0,
1327 (terminal->width - terminal->column) * sizeof(union utf8_char));
1328 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1329 terminal->width - terminal->column);
1330 } else if (args[0] == 1) {
1331 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1332 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1333 } else if (args[0] == 2) {
1334 memset(row, 0, terminal->data_pitch);
1335 attr_init(attr_row, terminal->curr_attr, terminal->width);
1339 count = set[0] ? args[0] : 1;
1340 if (count == 0) count = 1;
1341 if (terminal->row >= terminal->margin_top &&
1342 terminal->row < terminal->margin_bottom)
1344 top = terminal->margin_top;
1345 terminal->margin_top = terminal->row;
1346 terminal_scroll(terminal, 0 - count);
1347 terminal->margin_top = top;
1348 } else if (terminal->row == terminal->margin_bottom) {
1349 memset(terminal_get_row(terminal, terminal->row),
1350 0, terminal->data_pitch);
1351 attr_init(terminal_get_attr_row(terminal, terminal->row),
1352 terminal->curr_attr, terminal->width);
1356 count = set[0] ? args[0] : 1;
1357 if (count == 0) count = 1;
1358 if (terminal->row >= terminal->margin_top &&
1359 terminal->row < terminal->margin_bottom)
1361 top = terminal->margin_top;
1362 terminal->margin_top = terminal->row;
1363 terminal_scroll(terminal, count);
1364 terminal->margin_top = top;
1365 } else if (terminal->row == terminal->margin_bottom) {
1366 memset(terminal_get_row(terminal, terminal->row),
1367 0, terminal->data_pitch);
1371 count = set[0] ? args[0] : 1;
1372 if (count == 0) count = 1;
1373 terminal_shift_line(terminal, 0 - count);
1376 terminal_scroll(terminal, set[0] ? args[0] : 1);
1379 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1382 count = set[0] ? args[0] : 1;
1383 if (count == 0) count = 1;
1384 if ((terminal->column + count) > terminal->width)
1385 count = terminal->width - terminal->column;
1386 row = terminal_get_row(terminal, terminal->row);
1387 attr_row = terminal_get_attr_row(terminal, terminal->row);
1388 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1389 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1392 count = set[0] ? args[0] : 1;
1393 if (count == 0) count = 1;
1394 while (count > 0 && terminal->column >= 0) {
1395 if (terminal->tab_ruler[terminal->column]) count--;
1401 y = set[0] ? args[0] : 1;
1402 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1404 terminal->column = y - 1;
1407 count = set[0] ? args[0] : 1;
1408 if (count == 0) count = 1;
1409 if (terminal->last_char.byte[0])
1410 for (i = 0; i < count; i++)
1411 handle_char(terminal, terminal->last_char);
1412 terminal->last_char.byte[0] = 0;
1414 case 'c': /* Primary DA */
1415 terminal_write(terminal, "\e[?6c", 5);
1418 x = set[0] ? args[0] : 1;
1419 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1421 terminal->row = x - 1;
1424 if (!set[0] || args[0] == 0) {
1425 terminal->tab_ruler[terminal->column] = 0;
1426 } else if (args[0] == 3) {
1427 memset(terminal->tab_ruler, 0, terminal->width);
1431 for(i = 0; i < 10 && set[i]; i++) {
1432 handle_term_parameter(terminal, args[i], 1);
1436 for(i = 0; i < 10 && set[i]; i++) {
1437 handle_term_parameter(terminal, args[i], 0);
1441 for(i = 0; i < 10; i++) {
1442 if (i <= 7 && set[i] && set[i + 1] &&
1443 set[i + 2] && args[i + 1] == 5)
1445 if (args[i] == 38) {
1446 handle_sgr(terminal, args[i + 2] + 256);
1448 } else if (args[i] == 48) {
1449 handle_sgr(terminal, args[i + 2] + 512);
1454 handle_sgr(terminal, args[i]);
1456 handle_sgr(terminal, 0);
1464 i = set[0] ? args[0] : 0;
1465 if (i == 0 || i == 5) {
1466 terminal_write(terminal, "\e[0n", 4);
1467 } else if (i == 6) {
1468 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1469 terminal->origin_mode ?
1470 terminal->row+terminal->margin_top : terminal->row+1,
1471 terminal->column+1);
1472 terminal_write(terminal, response, strlen(response));
1477 terminal->margin_top = 0;
1478 terminal->margin_bottom = terminal->height-1;
1480 terminal->column = 0;
1482 top = (set[0] ? args[0] : 1) - 1;
1484 (top >= terminal->height ? terminal->height - 1 : top);
1485 bottom = (set[1] ? args[1] : 1) - 1;
1486 bottom = bottom < 0 ? 0 :
1487 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1489 terminal->margin_top = top;
1490 terminal->margin_bottom = bottom;
1492 terminal->margin_top = 0;
1493 terminal->margin_bottom = terminal->height-1;
1495 if(terminal->origin_mode)
1496 terminal->row = terminal->margin_top;
1499 terminal->column = 0;
1503 terminal->saved_row = terminal->row;
1504 terminal->saved_column = terminal->column;
1506 case 't': /* windowOps */
1509 case 4: /* resize px */
1510 if (set[1] && set[2]) {
1511 widget_schedule_resize(terminal->widget,
1515 case 8: /* resize ch */
1516 if (set[1] && set[2]) {
1517 terminal_resize(terminal, args[2], args[1]);
1520 case 13: /* report position */
1521 widget_get_allocation(terminal->widget, &allocation);
1522 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1523 allocation.x, allocation.y);
1524 terminal_write(terminal, response, strlen(response));
1526 case 14: /* report px */
1527 widget_get_allocation(terminal->widget, &allocation);
1528 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1529 allocation.height, allocation.width);
1530 terminal_write(terminal, response, strlen(response));
1532 case 18: /* report ch */
1533 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1534 terminal->height, terminal->width);
1535 terminal_write(terminal, response, strlen(response));
1537 case 21: /* report title */
1538 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1539 window_get_title(terminal->window));
1540 terminal_write(terminal, response, strlen(response));
1544 terminal_resize(terminal, terminal->width, args[0]);
1546 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1550 terminal->row = terminal->saved_row;
1551 terminal->column = terminal->saved_column;
1554 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
1560 handle_non_csi_escape(struct terminal *terminal, char code)
1565 if(terminal->row < terminal->margin_top) {
1566 terminal->row = terminal->margin_top;
1567 terminal_scroll(terminal, -1);
1571 terminal->column = 0;
1575 if(terminal->row > terminal->margin_bottom) {
1576 terminal->row = terminal->margin_bottom;
1577 terminal_scroll(terminal, +1);
1581 terminal_init(terminal);
1584 terminal->tab_ruler[terminal->column] = 1;
1586 case '7': /* DECSC */
1587 terminal->saved_row = terminal->row;
1588 terminal->saved_column = terminal->column;
1589 terminal->saved_attr = terminal->curr_attr;
1590 terminal->saved_origin_mode = terminal->origin_mode;
1591 terminal->saved_cs = terminal->cs;
1592 terminal->saved_g0 = terminal->g0;
1593 terminal->saved_g1 = terminal->g1;
1595 case '8': /* DECRC */
1596 terminal->row = terminal->saved_row;
1597 terminal->column = terminal->saved_column;
1598 terminal->curr_attr = terminal->saved_attr;
1599 terminal->origin_mode = terminal->saved_origin_mode;
1600 terminal->cs = terminal->saved_cs;
1601 terminal->g0 = terminal->saved_g0;
1602 terminal->g1 = terminal->saved_g1;
1604 case '=': /* DECPAM */
1605 terminal->key_mode = KM_APPLICATION;
1607 case '>': /* DECPNM */
1608 terminal->key_mode = KM_NORMAL;
1611 fprintf(stderr, "Unknown escape code: %c\n", code);
1617 handle_special_escape(struct terminal *terminal, char special, char code)
1621 if (special == '#') {
1624 /* fill with 'E', no cheap way to do this */
1625 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1626 numChars = terminal->width * terminal->height;
1627 for(i = 0; i < numChars; i++) {
1628 terminal->data[i].byte[0] = 'E';
1632 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1635 } else if (special == '(' || special == ')') {
1639 terminal->g0 = CS_SPECIAL;
1641 terminal->g1 = CS_SPECIAL;
1645 terminal->g0 = CS_UK;
1647 terminal->g1 = CS_UK;
1651 terminal->g0 = CS_US;
1653 terminal->g1 = CS_US;
1656 fprintf(stderr, "Unknown character set %c\n", code);
1660 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1665 handle_sgr(struct terminal *terminal, int code)
1669 terminal->curr_attr = terminal->color_scheme->default_attr;
1672 terminal->curr_attr.a |= ATTRMASK_BOLD;
1673 if (terminal->curr_attr.fg < 8)
1674 terminal->curr_attr.fg += 8;
1677 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1680 terminal->curr_attr.a |= ATTRMASK_BLINK;
1683 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1688 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1689 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1690 terminal->curr_attr.fg -= 8;
1693 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1696 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1700 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1703 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1706 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1709 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1712 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1715 if(code >= 30 && code <= 37) {
1716 terminal->curr_attr.fg = code - 30;
1717 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1718 terminal->curr_attr.fg += 8;
1719 } else if(code >= 40 && code <= 47) {
1720 terminal->curr_attr.bg = code - 40;
1721 } else if (code >= 90 && code <= 97) {
1722 terminal->curr_attr.fg = code - 90 + 8;
1723 } else if (code >= 100 && code <= 107) {
1724 terminal->curr_attr.bg = code - 100 + 8;
1725 } else if(code >= 256 && code < 512) {
1726 terminal->curr_attr.fg = code - 256;
1727 } else if(code >= 512 && code < 768) {
1728 terminal->curr_attr.bg = code - 512;
1730 fprintf(stderr, "Unknown SGR code: %d\n", code);
1736 /* Returns 1 if c was special, otherwise 0 */
1738 handle_special_char(struct terminal *terminal, char c)
1740 union utf8_char *row;
1741 struct attr *attr_row;
1743 row = terminal_get_row(terminal, terminal->row);
1744 attr_row = terminal_get_attr_row(terminal, terminal->row);
1748 terminal->column = 0;
1751 if (terminal->mode & MODE_LF_NEWLINE) {
1752 terminal->column = 0;
1758 if(terminal->row > terminal->margin_bottom) {
1759 terminal->row = terminal->margin_bottom;
1760 terminal_scroll(terminal, +1);
1765 while (terminal->column < terminal->width) {
1766 if (terminal->mode & MODE_IRM)
1767 terminal_shift_line(terminal, +1);
1769 if (row[terminal->column].byte[0] == '\0') {
1770 row[terminal->column].byte[0] = ' ';
1771 row[terminal->column].byte[1] = '\0';
1772 attr_row[terminal->column] = terminal->curr_attr;
1776 if (terminal->tab_ruler[terminal->column]) break;
1778 if (terminal->column >= terminal->width) {
1779 terminal->column = terminal->width - 1;
1784 if (terminal->column >= terminal->width) {
1785 terminal->column = terminal->width - 2;
1786 } else if (terminal->column > 0) {
1788 } else if (terminal->mode & MODE_AUTOWRAP) {
1789 terminal->column = terminal->width - 1;
1791 if (terminal->row < terminal->margin_top) {
1792 terminal->row = terminal->margin_top;
1793 terminal_scroll(terminal, -1);
1801 case '\x0E': /* SO */
1802 terminal->cs = terminal->g1;
1804 case '\x0F': /* SI */
1805 terminal->cs = terminal->g0;
1817 handle_char(struct terminal *terminal, union utf8_char utf8)
1819 union utf8_char *row;
1820 struct attr *attr_row;
1822 if (handle_special_char(terminal, utf8.byte[0])) return;
1824 apply_char_set(terminal->cs, &utf8);
1826 /* There are a whole lot of non-characters, control codes,
1827 * and formatting codes that should probably be ignored,
1829 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1834 /* Some of these non-characters should be translated, e.g.: */
1835 if (utf8.byte[0] < 32) {
1836 utf8.byte[0] = utf8.byte[0] + 64;
1839 /* handle right margin effects */
1840 if (terminal->column >= terminal->width) {
1841 if (terminal->mode & MODE_AUTOWRAP) {
1842 terminal->column = 0;
1844 if (terminal->row > terminal->margin_bottom) {
1845 terminal->row = terminal->margin_bottom;
1846 terminal_scroll(terminal, +1);
1853 row = terminal_get_row(terminal, terminal->row);
1854 attr_row = terminal_get_attr_row(terminal, terminal->row);
1856 if (terminal->mode & MODE_IRM)
1857 terminal_shift_line(terminal, +1);
1858 row[terminal->column] = utf8;
1859 attr_row[terminal->column++] = terminal->curr_attr;
1861 if (utf8.ch != terminal->last_char.ch)
1862 terminal->last_char = utf8;
1866 escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1870 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1871 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1872 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1873 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1874 else len = 1; /* Invalid, cannot happen */
1876 if (terminal->escape_length + len <= MAX_ESCAPE) {
1877 for (i = 0; i < len; i++)
1878 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1879 terminal->escape_length += len;
1880 } else if (terminal->escape_length < MAX_ESCAPE) {
1881 terminal->escape[terminal->escape_length++] = 0;
1886 terminal_data(struct terminal *terminal, const char *data, size_t length)
1889 union utf8_char utf8;
1890 enum utf8_state parser_state;
1892 for (i = 0; i < length; i++) {
1894 utf8_next_char(&terminal->state_machine, data[i]);
1895 switch(parser_state) {
1896 case utf8state_accept:
1897 utf8.ch = terminal->state_machine.s.ch;
1899 case utf8state_reject:
1900 /* the unicode replacement character */
1901 utf8.byte[0] = 0xEF;
1902 utf8.byte[1] = 0xBF;
1903 utf8.byte[2] = 0xBD;
1904 utf8.byte[3] = 0x00;
1910 /* assume escape codes never use non-ASCII characters */
1911 switch (terminal->state) {
1912 case escape_state_escape:
1913 escape_append_utf8(terminal, utf8);
1914 switch (utf8.byte[0]) {
1916 terminal->state = escape_state_dcs;
1919 terminal->state = escape_state_csi;
1922 terminal->state = escape_state_osc;
1926 case ')': /* special */
1927 terminal->state = escape_state_special;
1929 case '^': /* PM (not implemented) */
1930 case '_': /* APC (not implemented) */
1931 terminal->state = escape_state_ignore;
1934 terminal->state = escape_state_normal;
1935 handle_non_csi_escape(terminal, utf8.byte[0]);
1939 case escape_state_csi:
1940 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1942 } else if (utf8.byte[0] == '?') {
1943 terminal->escape_flags |= ESC_FLAG_WHAT;
1944 } else if (utf8.byte[0] == '>') {
1945 terminal->escape_flags |= ESC_FLAG_GT;
1946 } else if (utf8.byte[0] == '!') {
1947 terminal->escape_flags |= ESC_FLAG_BANG;
1948 } else if (utf8.byte[0] == '$') {
1949 terminal->escape_flags |= ESC_FLAG_CASH;
1950 } else if (utf8.byte[0] == '\'') {
1951 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1952 } else if (utf8.byte[0] == '"') {
1953 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1954 } else if (utf8.byte[0] == ' ') {
1955 terminal->escape_flags |= ESC_FLAG_SPACE;
1957 escape_append_utf8(terminal, utf8);
1958 if (terminal->escape_length >= MAX_ESCAPE)
1959 terminal->state = escape_state_normal;
1962 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1963 utf8.byte[0] == '`')
1965 terminal->state = escape_state_normal;
1966 handle_escape(terminal);
1970 case escape_state_inner_escape:
1971 if (utf8.byte[0] == '\\') {
1972 terminal->state = escape_state_normal;
1973 if (terminal->outer_state == escape_state_dcs) {
1974 handle_dcs(terminal);
1975 } else if (terminal->outer_state == escape_state_osc) {
1976 handle_osc(terminal);
1978 } else if (utf8.byte[0] == '\e') {
1979 terminal->state = terminal->outer_state;
1980 escape_append_utf8(terminal, utf8);
1981 if (terminal->escape_length >= MAX_ESCAPE)
1982 terminal->state = escape_state_normal;
1984 terminal->state = terminal->outer_state;
1985 if (terminal->escape_length < MAX_ESCAPE)
1986 terminal->escape[terminal->escape_length++] = '\e';
1987 escape_append_utf8(terminal, utf8);
1988 if (terminal->escape_length >= MAX_ESCAPE)
1989 terminal->state = escape_state_normal;
1992 case escape_state_dcs:
1993 case escape_state_osc:
1994 case escape_state_ignore:
1995 if (utf8.byte[0] == '\e') {
1996 terminal->outer_state = terminal->state;
1997 terminal->state = escape_state_inner_escape;
1998 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
1999 terminal->state = escape_state_normal;
2000 handle_osc(terminal);
2002 escape_append_utf8(terminal, utf8);
2003 if (terminal->escape_length >= MAX_ESCAPE)
2004 terminal->state = escape_state_normal;
2007 case escape_state_special:
2008 escape_append_utf8(terminal, utf8);
2009 terminal->state = escape_state_normal;
2010 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2011 handle_special_escape(terminal, terminal->escape[1],
2019 /* this is valid, because ASCII characters are never used to
2020 * introduce a multibyte sequence in UTF-8 */
2021 if (utf8.byte[0] == '\e') {
2022 terminal->state = escape_state_escape;
2023 terminal->outer_state = escape_state_normal;
2024 terminal->escape[0] = '\e';
2025 terminal->escape_length = 1;
2026 terminal->escape_flags = 0;
2028 handle_char(terminal, utf8);
2032 window_schedule_redraw(terminal->window);
2036 data_source_target(void *data,
2037 struct wl_data_source *source, const char *mime_type)
2039 fprintf(stderr, "data_source_target, %s\n", mime_type);
2043 data_source_send(void *data,
2044 struct wl_data_source *source,
2045 const char *mime_type, int32_t fd)
2047 struct terminal *terminal = data;
2049 terminal_send_selection(terminal, fd);
2053 data_source_cancelled(void *data, struct wl_data_source *source)
2055 wl_data_source_destroy(source);
2058 static const struct wl_data_source_listener data_source_listener = {
2061 data_source_cancelled
2065 fullscreen_handler(struct window *window, void *data)
2067 struct terminal *terminal = data;
2069 window_set_fullscreen(window, !window_is_fullscreen(terminal->window));
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",
2100 new_terminal = terminal_create(terminal->display);
2101 if (terminal_run(new_terminal, option_shell))
2102 terminal_destroy(new_terminal);
2112 key_handler(struct window *window, struct input *input, uint32_t time,
2113 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2116 struct terminal *terminal = data;
2117 char ch[MAX_RESPONSE];
2118 uint32_t modifiers, serial;
2120 bool convert_utf8 = true;
2122 modifiers = input_get_modifiers(input);
2123 if ((modifiers & MOD_CONTROL_MASK) &&
2124 (modifiers & MOD_SHIFT_MASK) &&
2125 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2126 handle_bound_key(terminal, input, sym, time))
2129 /* Map keypad symbols to 'normal' equivalents before processing */
2131 case XKB_KEY_KP_Space:
2132 sym = XKB_KEY_space;
2134 case XKB_KEY_KP_Tab:
2137 case XKB_KEY_KP_Enter:
2138 sym = XKB_KEY_Return;
2140 case XKB_KEY_KP_Left:
2146 case XKB_KEY_KP_Right:
2147 sym = XKB_KEY_Right;
2149 case XKB_KEY_KP_Down:
2152 case XKB_KEY_KP_Equal:
2153 sym = XKB_KEY_equal;
2155 case XKB_KEY_KP_Multiply:
2156 sym = XKB_KEY_asterisk;
2158 case XKB_KEY_KP_Add:
2161 case XKB_KEY_KP_Separator:
2162 /* Note this is actually locale-dependent and should mostly be
2163 * a comma. But leave it as period until we one day start
2164 * doing the right thing. */
2165 sym = XKB_KEY_period;
2167 case XKB_KEY_KP_Subtract:
2168 sym = XKB_KEY_minus;
2170 case XKB_KEY_KP_Decimal:
2171 sym = XKB_KEY_period;
2173 case XKB_KEY_KP_Divide:
2174 sym = XKB_KEY_slash;
2186 sym = (sym - XKB_KEY_KP_0) + XKB_KEY_0;
2193 case XKB_KEY_BackSpace:
2194 if (modifiers & MOD_ALT_MASK)
2199 case XKB_KEY_Linefeed:
2202 case XKB_KEY_Scroll_Lock:
2203 case XKB_KEY_Sys_Req:
2204 case XKB_KEY_Escape:
2205 ch[len++] = sym & 0x7f;
2208 case XKB_KEY_Return:
2209 if (terminal->mode & MODE_LF_NEWLINE) {
2217 case XKB_KEY_Shift_L:
2218 case XKB_KEY_Shift_R:
2219 case XKB_KEY_Control_L:
2220 case XKB_KEY_Control_R:
2223 case XKB_KEY_Meta_L:
2224 case XKB_KEY_Meta_R:
2225 case XKB_KEY_Super_L:
2226 case XKB_KEY_Super_R:
2227 case XKB_KEY_Hyper_L:
2228 case XKB_KEY_Hyper_R:
2231 case XKB_KEY_Insert:
2232 len = function_key_response('[', 2, modifiers, '~', ch);
2234 case XKB_KEY_Delete:
2235 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2238 len = function_key_response('[', 3, modifiers, '~', ch);
2241 case XKB_KEY_Page_Up:
2242 len = function_key_response('[', 5, modifiers, '~', ch);
2244 case XKB_KEY_Page_Down:
2245 len = function_key_response('[', 6, modifiers, '~', ch);
2248 len = function_key_response('O', 1, modifiers, 'P', ch);
2251 len = function_key_response('O', 1, modifiers, 'Q', ch);
2254 len = function_key_response('O', 1, modifiers, 'R', ch);
2257 len = function_key_response('O', 1, modifiers, 'S', ch);
2260 len = function_key_response('[', 15, modifiers, '~', ch);
2263 len = function_key_response('[', 17, modifiers, '~', ch);
2266 len = function_key_response('[', 18, modifiers, '~', ch);
2269 len = function_key_response('[', 19, modifiers, '~', ch);
2272 len = function_key_response('[', 20, modifiers, '~', ch);
2275 len = function_key_response('[', 21, modifiers, '~', ch);
2278 len = function_key_response('[', 24, modifiers, '~', ch);
2281 /* Handle special keys with alternate mappings */
2282 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2283 if (len != 0) break;
2285 if (modifiers & MOD_CONTROL_MASK) {
2286 if (sym >= '3' && sym <= '7')
2287 sym = (sym & 0x1f) + 8;
2289 if (!((sym >= '!' && sym <= '/') ||
2290 (sym >= '8' && sym <= '?') ||
2291 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2292 else if (sym == '2') sym = 0x00;
2293 else if (sym == '/') sym = 0x1F;
2294 else if (sym == '8' || sym == '?') sym = 0x7F;
2296 if (modifiers & MOD_ALT_MASK) {
2297 if (terminal->mode & MODE_ALT_SENDS_ESC) {
2301 convert_utf8 = false;
2306 (!convert_utf8 && sym < 256)) {
2309 ret = xkb_keysym_to_utf8(sym, ch + len,
2310 MAX_RESPONSE - len);
2313 "Warning: buffer too small to encode "
2314 "UTF8 character\n");
2322 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
2323 terminal_write(terminal, ch, len);
2325 /* Hide cursor, except if this was coming from a
2326 * repeating key press. */
2327 serial = display_get_serial(terminal->display);
2328 if (terminal->hide_cursor_serial != serial) {
2329 input_set_pointer_image(input, CURSOR_BLANK);
2330 terminal->hide_cursor_serial = serial;
2336 keyboard_focus_handler(struct window *window,
2337 struct input *device, void *data)
2339 struct terminal *terminal = data;
2341 window_schedule_redraw(terminal->window);
2344 static int wordsep(int ch)
2346 const char extra[] = "-,./?%&#:_=+@~";
2351 return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
2355 recompute_selection(struct terminal *terminal)
2357 struct rectangle allocation;
2358 int col, x, width, height;
2359 int start_row, end_row;
2360 int word_start, eol;
2361 int side_margin, top_margin;
2364 union utf8_char *data;
2366 cw = terminal->extents.max_x_advance;
2367 ch = terminal->extents.height;
2368 widget_get_allocation(terminal->widget, &allocation);
2369 width = terminal->width * cw;
2370 height = terminal->height * ch;
2371 side_margin = allocation.x + (allocation.width - width) / 2;
2372 top_margin = allocation.y + (allocation.height - height) / 2;
2374 start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1;
2375 end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1;
2377 if (start_row < end_row ||
2378 (start_row == end_row &&
2379 terminal->selection_start_x < terminal->selection_end_x)) {
2380 terminal->selection_start_row = start_row;
2381 terminal->selection_end_row = end_row;
2382 start_x = terminal->selection_start_x;
2383 end_x = terminal->selection_end_x;
2385 terminal->selection_start_row = end_row;
2386 terminal->selection_end_row = start_row;
2387 start_x = terminal->selection_end_x;
2388 end_x = terminal->selection_start_x;
2392 if (terminal->selection_start_row < 0) {
2393 terminal->selection_start_row = 0;
2394 terminal->selection_start_col = 0;
2396 x = side_margin + cw / 2;
2397 data = terminal_get_row(terminal,
2398 terminal->selection_start_row);
2400 for (col = 0; col < terminal->width; col++, x += cw) {
2401 if (col == 0 || wordsep(data[col - 1].ch))
2403 if (data[col].ch != 0)
2409 switch (terminal->dragging) {
2411 terminal->selection_start_col = 0;
2414 terminal->selection_start_col = word_start;
2417 terminal->selection_start_col = col;
2422 if (terminal->selection_end_row >= terminal->height) {
2423 terminal->selection_end_row = terminal->height;
2424 terminal->selection_end_col = 0;
2426 x = side_margin + cw / 2;
2427 data = terminal_get_row(terminal, terminal->selection_end_row);
2428 for (col = 0; col < terminal->width; col++, x += cw) {
2429 if (terminal->dragging == SELECT_CHAR && end_x < x)
2431 if (terminal->dragging == SELECT_WORD &&
2432 end_x < x && wordsep(data[col].ch))
2435 terminal->selection_end_col = col;
2438 if (terminal->selection_end_col != terminal->selection_start_col ||
2439 terminal->selection_start_row != terminal->selection_end_row) {
2440 col = terminal->selection_end_col;
2441 if (col > 0 && data[col - 1].ch == 0)
2442 terminal->selection_end_col = terminal->width;
2443 data = terminal_get_row(terminal, terminal->selection_start_row);
2444 if (data[terminal->selection_start_col].ch == 0)
2445 terminal->selection_start_col = eol;
2452 button_handler(struct widget *widget,
2453 struct input *input, uint32_t time,
2455 enum wl_pointer_button_state state, void *data)
2457 struct terminal *terminal = data;
2461 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2463 if (time - terminal->button_time < 500)
2464 terminal->click_count++;
2466 terminal->click_count = 1;
2468 terminal->button_time = time;
2469 terminal->dragging =
2470 (terminal->click_count - 1) % 3 + SELECT_CHAR;
2472 input_get_position(input,
2473 &terminal->selection_start_x,
2474 &terminal->selection_start_y);
2475 terminal->selection_end_x = terminal->selection_start_x;
2476 terminal->selection_end_y = terminal->selection_start_y;
2477 if (recompute_selection(terminal))
2478 widget_schedule_redraw(widget);
2480 terminal->dragging = SELECT_NONE;
2487 enter_handler(struct widget *widget,
2488 struct input *input, float x, float y, void *data)
2490 return CURSOR_IBEAM;
2494 motion_handler(struct widget *widget,
2495 struct input *input, uint32_t time,
2496 float x, float y, void *data)
2498 struct terminal *terminal = data;
2500 if (terminal->dragging) {
2501 input_get_position(input,
2502 &terminal->selection_end_x,
2503 &terminal->selection_end_y);
2505 if (recompute_selection(terminal))
2506 widget_schedule_redraw(widget);
2509 return CURSOR_IBEAM;
2512 static struct terminal *
2513 terminal_create(struct display *display)
2515 struct terminal *terminal;
2516 cairo_surface_t *surface;
2519 terminal = malloc(sizeof *terminal);
2520 if (terminal == NULL)
2523 memset(terminal, 0, sizeof *terminal);
2524 terminal->color_scheme = &DEFAULT_COLORS;
2525 terminal_init(terminal);
2526 terminal->margin_top = 0;
2527 terminal->margin_bottom = -1;
2528 terminal->window = window_create(display);
2529 terminal->widget = frame_create(terminal->window, terminal);
2530 window_set_title(terminal->window, "Wayland Terminal");
2531 widget_set_transparent(terminal->widget, 0);
2533 init_state_machine(&terminal->state_machine);
2534 init_color_table(terminal);
2536 terminal->display = display;
2537 terminal->margin = 5;
2539 window_set_user_data(terminal->window, terminal);
2540 window_set_key_handler(terminal->window, key_handler);
2541 window_set_keyboard_focus_handler(terminal->window,
2542 keyboard_focus_handler);
2543 window_set_fullscreen_handler(terminal->window, fullscreen_handler);
2545 widget_set_redraw_handler(terminal->widget, redraw_handler);
2546 widget_set_resize_handler(terminal->widget, resize_handler);
2547 widget_set_button_handler(terminal->widget, button_handler);
2548 widget_set_enter_handler(terminal->widget, enter_handler);
2549 widget_set_motion_handler(terminal->widget, motion_handler);
2551 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2552 cr = cairo_create(surface);
2553 cairo_set_font_size(cr, option_font_size);
2554 cairo_select_font_face (cr, option_font,
2555 CAIRO_FONT_SLANT_NORMAL,
2556 CAIRO_FONT_WEIGHT_BOLD);
2557 terminal->font_bold = cairo_get_scaled_font (cr);
2558 cairo_scaled_font_reference(terminal->font_bold);
2560 cairo_select_font_face (cr, option_font,
2561 CAIRO_FONT_SLANT_NORMAL,
2562 CAIRO_FONT_WEIGHT_NORMAL);
2563 terminal->font_normal = cairo_get_scaled_font (cr);
2564 cairo_scaled_font_reference(terminal->font_normal);
2566 cairo_font_extents(cr, &terminal->extents);
2568 cairo_surface_destroy(surface);
2570 terminal_resize(terminal, 20, 5); /* Set minimum size first */
2571 terminal_resize(terminal, 80, 25);
2573 wl_list_insert(terminal_list.prev, &terminal->link);
2579 terminal_destroy(struct terminal *terminal)
2581 display_unwatch_fd(terminal->display, terminal->master);
2582 window_destroy(terminal->window);
2583 close(terminal->master);
2584 wl_list_remove(&terminal->link);
2586 if (wl_list_empty(&terminal_list))
2587 display_exit(terminal->display);
2593 io_handler(struct task *task, uint32_t events)
2595 struct terminal *terminal =
2596 container_of(task, struct terminal, io_task);
2600 if (events & EPOLLHUP) {
2601 terminal_destroy(terminal);
2605 len = read(terminal->master, buffer, sizeof buffer);
2607 terminal_destroy(terminal);
2609 terminal_data(terminal, buffer, len);
2613 terminal_run(struct terminal *terminal, const char *path)
2618 pid = forkpty(&master, NULL, NULL, NULL);
2620 setenv("TERM", option_term, 1);
2621 setenv("COLORTERM", option_term, 1);
2622 if (execl(path, path, NULL)) {
2623 printf("exec failed: %m\n");
2626 } else if (pid < 0) {
2627 fprintf(stderr, "failed to fork and create pty (%m).\n");
2631 terminal->master = master;
2632 fcntl(master, F_SETFL, O_NONBLOCK);
2633 terminal->io_task.run = io_handler;
2634 display_watch_fd(terminal->display, terminal->master,
2635 EPOLLIN | EPOLLHUP, &terminal->io_task);
2637 window_set_fullscreen(terminal->window, option_fullscreen);
2638 if (!window_is_fullscreen(terminal->window))
2639 terminal_resize(terminal, 80, 24);
2644 static const struct config_key terminal_config_keys[] = {
2645 { "font", CONFIG_KEY_STRING, &option_font },
2646 { "font-size", CONFIG_KEY_INTEGER, &option_font_size },
2647 { "term", CONFIG_KEY_STRING, &option_term },
2650 static const struct config_section config_sections[] = {
2652 terminal_config_keys, ARRAY_LENGTH(terminal_config_keys) },
2655 static const struct weston_option terminal_options[] = {
2656 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
2657 { WESTON_OPTION_STRING, "font", 0, &option_font },
2658 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
2661 int main(int argc, char *argv[])
2664 struct terminal *terminal;
2667 option_shell = getenv("SHELL");
2669 option_shell = "/bin/bash";
2671 config_file = config_file_path("weston.ini");
2672 parse_config_file(config_file,
2673 config_sections, ARRAY_LENGTH(config_sections),
2677 argc = parse_options(terminal_options,
2678 ARRAY_LENGTH(terminal_options), argc, argv);
2680 d = display_create(argc, argv);
2682 fprintf(stderr, "failed to create display: %m\n");
2686 wl_list_init(&terminal_list);
2687 terminal = terminal_create(d);
2688 if (terminal_run(terminal, option_shell))