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
37 #include <sys/epoll.h>
41 #include <wayland-client.h>
43 #include "../shared/config-parser.h"
46 static int option_fullscreen;
47 static char *option_font;
48 static int option_font_size;
49 static char *option_term;
50 static char *option_shell;
52 static struct wl_list terminal_list;
54 static struct terminal *
55 terminal_create(struct display *display);
57 terminal_destroy(struct terminal *terminal);
59 terminal_run(struct terminal *terminal, const char *path);
61 #define TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS \
65 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
67 "abcdefghijklmnopqrstuvwxyz" \
71 #define MOD_SHIFT 0x01
75 #define ATTRMASK_BOLD 0x01
76 #define ATTRMASK_UNDERLINE 0x02
77 #define ATTRMASK_BLINK 0x04
78 #define ATTRMASK_INVERSE 0x08
79 #define ATTRMASK_CONCEALED 0x10
82 #define MAX_RESPONSE 256
83 #define MAX_ESCAPE 255
86 #define MODE_SHOW_CURSOR 0x00000001
87 #define MODE_INVERSE 0x00000002
88 #define MODE_AUTOWRAP 0x00000004
89 #define MODE_AUTOREPEAT 0x00000008
90 #define MODE_LF_NEWLINE 0x00000010
91 #define MODE_IRM 0x00000020
92 #define MODE_DELETE_SENDS_DEL 0x00000040
93 #define MODE_ALT_SENDS_ESC 0x00000080
96 unsigned char byte[4];
109 struct utf8_state_machine {
110 enum utf8_state state;
117 init_state_machine(struct utf8_state_machine *machine)
119 machine->state = utf8state_start;
124 static enum utf8_state
125 utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
127 switch(machine->state) {
128 case utf8state_start:
129 case utf8state_accept:
130 case utf8state_reject:
133 if(c == 0xC0 || c == 0xC1) {
134 /* overlong encoding, reject */
135 machine->state = utf8state_reject;
136 } else if((c & 0x80) == 0) {
137 /* single byte, accept */
138 machine->s.byte[machine->len++] = c;
139 machine->state = utf8state_accept;
140 machine->unicode = c;
141 } else if((c & 0xC0) == 0x80) {
142 /* parser out of sync, ignore byte */
143 machine->state = utf8state_start;
144 } else if((c & 0xE0) == 0xC0) {
145 /* start of two byte sequence */
146 machine->s.byte[machine->len++] = c;
147 machine->state = utf8state_expect1;
148 machine->unicode = c & 0x1f;
149 } else if((c & 0xF0) == 0xE0) {
150 /* start of three byte sequence */
151 machine->s.byte[machine->len++] = c;
152 machine->state = utf8state_expect2;
153 machine->unicode = c & 0x0f;
154 } else if((c & 0xF8) == 0xF0) {
155 /* start of four byte sequence */
156 machine->s.byte[machine->len++] = c;
157 machine->state = utf8state_expect3;
158 machine->unicode = c & 0x07;
160 /* overlong encoding, reject */
161 machine->state = utf8state_reject;
164 case utf8state_expect3:
165 machine->s.byte[machine->len++] = c;
166 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
167 if((c & 0xC0) == 0x80) {
168 /* all good, continue */
169 machine->state = utf8state_expect2;
171 /* missing extra byte, reject */
172 machine->state = utf8state_reject;
175 case utf8state_expect2:
176 machine->s.byte[machine->len++] = c;
177 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
178 if((c & 0xC0) == 0x80) {
179 /* all good, continue */
180 machine->state = utf8state_expect1;
182 /* missing extra byte, reject */
183 machine->state = utf8state_reject;
186 case utf8state_expect1:
187 machine->s.byte[machine->len++] = c;
188 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
189 if((c & 0xC0) == 0x80) {
190 /* all good, accept */
191 machine->state = utf8state_accept;
193 /* missing extra byte, reject */
194 machine->state = utf8state_reject;
198 machine->state = utf8state_reject;
202 return machine->state;
206 get_unicode(union utf8_char utf8)
208 struct utf8_state_machine machine;
211 init_state_machine(&machine);
212 for (i = 0; i < 4; i++) {
213 utf8_next_char(&machine, utf8.byte[i]);
214 if (machine.state == utf8state_accept ||
215 machine.state == utf8state_reject)
219 if (machine.state == utf8state_reject)
222 return machine.unicode;
226 is_wide(union utf8_char utf8)
228 uint32_t unichar = get_unicode(utf8);
229 return wcwidth(unichar) > 1;
233 union utf8_char match;
234 union utf8_char replace;
236 /* Set last char_sub match to NULL char */
237 typedef struct char_sub *character_set;
239 struct char_sub CS_US[] = {
242 static struct char_sub CS_UK[] = {
243 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
246 static struct char_sub CS_SPECIAL[] = {
247 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond: ♦ */
248 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell: ▒ */
249 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT: ␉ */
250 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF: ␌ */
251 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR: ␍ */
252 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF: ␊ */
253 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree: ° */
254 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus: ± */
255 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL:  */
256 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT: ␋ */
257 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB: ┘ */
258 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT: ┐ */
259 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT: ┌ */
260 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_LB: └ */
261 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS: ┼ */
262 {{{'o', 0, }}, {{0xE2, 0x8E, 0xBA, 0}}}, /* Horiz. Scan Line 1: ⎺ */
263 {{{'p', 0, }}, {{0xE2, 0x8E, 0xBB, 0}}}, /* Horiz. Scan Line 3: ⎻ */
264 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* Horiz. Scan Line 5: ─ */
265 {{{'r', 0, }}, {{0xE2, 0x8E, 0xBC, 0}}}, /* Horiz. Scan Line 7: ⎼ */
266 {{{'s', 0, }}, {{0xE2, 0x8E, 0xBD, 0}}}, /* Horiz. Scan Line 9: ⎽ */
267 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR: ├ */
268 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL: ┤ */
269 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU: ┴ */
270 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD: ┬ */
271 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V: │ */
272 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE: ≤ */
273 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE: ≥ */
274 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI: π */
275 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ: ≠ */
276 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
277 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT: ⋅ */
282 apply_char_set(character_set cs, union utf8_char *utf8)
286 while (cs[i].match.byte[0]) {
287 if ((*utf8).ch == cs[i].match.ch) {
288 *utf8 = cs[i].replace;
301 /* Set last key_sub sym to NULL */
302 typedef struct key_map *keyboard_mode;
304 static struct key_map KM_NORMAL[] = {
305 { XKB_KEY_Left, 1, '[', 'D' },
306 { XKB_KEY_Right, 1, '[', 'C' },
307 { XKB_KEY_Up, 1, '[', 'A' },
308 { XKB_KEY_Down, 1, '[', 'B' },
309 { XKB_KEY_Home, 1, '[', 'H' },
310 { XKB_KEY_End, 1, '[', 'F' },
313 static struct key_map KM_APPLICATION[] = {
314 { XKB_KEY_Left, 1, 'O', 'D' },
315 { XKB_KEY_Right, 1, 'O', 'C' },
316 { XKB_KEY_Up, 1, 'O', 'A' },
317 { XKB_KEY_Down, 1, 'O', 'B' },
318 { XKB_KEY_Home, 1, 'O', 'H' },
319 { XKB_KEY_End, 1, 'O', 'F' },
320 { XKB_KEY_KP_Enter, 1, 'O', 'M' },
321 { XKB_KEY_KP_Multiply, 1, 'O', 'j' },
322 { XKB_KEY_KP_Add, 1, 'O', 'k' },
323 { XKB_KEY_KP_Separator, 1, 'O', 'l' },
324 { XKB_KEY_KP_Subtract, 1, 'O', 'm' },
325 { XKB_KEY_KP_Divide, 1, 'O', 'o' },
330 function_key_response(char escape, int num, uint32_t modifiers,
331 char code, char *response)
336 if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
337 if (modifiers & MOD_ALT_MASK) mod_num |= 2;
338 if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
341 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
342 num, mod_num + 1, code);
343 else if (code != '~')
344 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
347 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
350 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
354 /* returns the number of bytes written into response,
355 * which must have room for MAX_RESPONSE bytes */
357 apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
363 while (mode[i].sym) {
365 if (sym == map.sym) {
366 len = function_key_response(map.escape, map.num,
376 struct terminal_color { double r, g, b, a; };
378 unsigned char fg, bg;
379 char a; /* attributes format:
382 char s; /* in selection */
384 struct color_scheme {
385 struct terminal_color palette[16];
387 struct attr default_attr;
391 attr_init(struct attr *data_attr, struct attr attr, int n)
394 for (i = 0; i < n; i++) {
400 escape_state_normal = 0,
405 escape_state_inner_escape,
410 #define ESC_FLAG_WHAT 0x01
411 #define ESC_FLAG_GT 0x02
412 #define ESC_FLAG_BANG 0x04
413 #define ESC_FLAG_CASH 0x08
414 #define ESC_FLAG_SQUOTE 0x10
415 #define ESC_FLAG_DQUOTE 0x20
416 #define ESC_FLAG_SPACE 0x40
426 struct window *window;
427 struct widget *widget;
428 struct display *display;
429 union utf8_char *data;
432 struct attr *data_attr;
433 struct attr curr_attr;
436 char saved_origin_mode;
437 struct attr saved_attr;
438 union utf8_char last_char;
439 int margin_top, margin_bottom;
440 character_set cs, g0, g1;
441 character_set saved_cs, saved_g0, saved_g1;
442 keyboard_mode key_mode;
443 int data_pitch, attr_pitch; /* The width in bytes of a line */
444 int width, height, start, row, column;
445 int saved_row, saved_column;
446 int send_cursor_position;
449 char escape[MAX_ESCAPE+1];
451 enum escape_state state;
452 enum escape_state outer_state;
454 struct utf8_state_machine state_machine;
456 struct color_scheme *color_scheme;
457 struct terminal_color color_table[256];
458 cairo_font_extents_t extents;
459 double average_width;
460 cairo_scaled_font_t *font_normal, *font_bold;
461 uint32_t hide_cursor_serial;
463 struct wl_data_source *selection;
464 uint32_t button_time;
465 int dragging, click_count;
466 int selection_start_x, selection_start_y;
467 int selection_end_x, selection_end_y;
468 int selection_start_row, selection_start_col;
469 int selection_end_row, selection_end_col;
473 /* Create default tab stops, every 8 characters */
475 terminal_init_tabs(struct terminal *terminal)
479 while (i < terminal->width) {
481 terminal->tab_ruler[i] = 1;
483 terminal->tab_ruler[i] = 0;
489 terminal_init(struct terminal *terminal)
491 terminal->curr_attr = terminal->color_scheme->default_attr;
492 terminal->origin_mode = 0;
493 terminal->mode = MODE_SHOW_CURSOR |
499 terminal->column = 0;
501 terminal->g0 = CS_US;
502 terminal->g1 = CS_US;
503 terminal->cs = terminal->g0;
504 terminal->key_mode = KM_NORMAL;
506 terminal->saved_g0 = terminal->g0;
507 terminal->saved_g1 = terminal->g1;
508 terminal->saved_cs = terminal->cs;
510 terminal->saved_attr = terminal->curr_attr;
511 terminal->saved_origin_mode = terminal->origin_mode;
512 terminal->saved_row = terminal->row;
513 terminal->saved_column = terminal->column;
515 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
519 init_color_table(struct terminal *terminal)
522 struct terminal_color *color_table = terminal->color_table;
524 for (c = 0; c < 256; c ++) {
526 color_table[c] = terminal->color_scheme->palette[c];
527 } else if (c < 232) {
529 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
530 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
531 color_table[c].r = ((double)(r % 6) / 6.0);
532 color_table[c].a = 1.0;
534 r = (c - 232) * 10 + 8;
535 color_table[c].r = ((double) r) / 256.0;
536 color_table[c].g = color_table[c].r;
537 color_table[c].b = color_table[c].r;
538 color_table[c].a = 1.0;
543 static union utf8_char *
544 terminal_get_row(struct terminal *terminal, int row)
548 index = (row + terminal->start) % terminal->height;
550 return &terminal->data[index * terminal->width];
554 terminal_get_attr_row(struct terminal *terminal, int row)
558 index = (row + terminal->start) % terminal->height;
560 return &terminal->data_attr[index * terminal->width];
569 terminal_decode_attr(struct terminal *terminal, int row, int col,
570 union decoded_attr *decoded)
573 int foreground, background, tmp;
576 if (((row == terminal->selection_start_row &&
577 col >= terminal->selection_start_col) ||
578 row > terminal->selection_start_row) &&
579 ((row == terminal->selection_end_row &&
580 col < terminal->selection_end_col) ||
581 row < terminal->selection_end_row))
584 /* get the attributes for this character cell */
585 attr = terminal_get_attr_row(terminal, row)[col];
586 if ((attr.a & ATTRMASK_INVERSE) ||
588 ((terminal->mode & MODE_SHOW_CURSOR) &&
589 window_has_focus(terminal->window) && terminal->row == row &&
590 terminal->column == col)) {
591 foreground = attr.bg;
592 background = attr.fg;
593 if (attr.a & ATTRMASK_BOLD) {
594 if (foreground <= 16) foreground |= 0x08;
595 if (background <= 16) background &= 0x07;
598 foreground = attr.fg;
599 background = attr.bg;
602 if (terminal->mode & MODE_INVERSE) {
604 foreground = background;
606 if (attr.a & ATTRMASK_BOLD) {
607 if (foreground <= 16) foreground |= 0x08;
608 if (background <= 16) background &= 0x07;
612 decoded->attr.fg = foreground;
613 decoded->attr.bg = background;
614 decoded->attr.a = attr.a;
619 terminal_scroll_buffer(struct terminal *terminal, int d)
623 d = d % (terminal->height + 1);
624 terminal->start = (terminal->start + d) % terminal->height;
625 if (terminal->start < 0) terminal->start = terminal->height + terminal->start;
628 for(i = 0; i < d; i++) {
629 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
630 attr_init(terminal_get_attr_row(terminal, i),
631 terminal->curr_attr, terminal->width);
634 for(i = terminal->height - d; i < terminal->height; i++) {
635 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
636 attr_init(terminal_get_attr_row(terminal, i),
637 terminal->curr_attr, terminal->width);
641 terminal->selection_start_row -= d;
642 terminal->selection_end_row -= d;
646 terminal_scroll_window(struct terminal *terminal, int d)
650 int from_row, to_row;
652 // scrolling range is inclusive
653 window_height = terminal->margin_bottom - terminal->margin_top + 1;
654 d = d % (window_height + 1);
657 to_row = terminal->margin_bottom;
658 from_row = terminal->margin_bottom - d;
660 for (i = 0; i < (window_height - d); i++) {
661 memcpy(terminal_get_row(terminal, to_row - i),
662 terminal_get_row(terminal, from_row - i),
663 terminal->data_pitch);
664 memcpy(terminal_get_attr_row(terminal, to_row - i),
665 terminal_get_attr_row(terminal, from_row - i),
666 terminal->attr_pitch);
668 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
669 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
670 attr_init(terminal_get_attr_row(terminal, i),
671 terminal->curr_attr, terminal->width);
674 to_row = terminal->margin_top;
675 from_row = terminal->margin_top + d;
677 for (i = 0; i < (window_height - d); i++) {
678 memcpy(terminal_get_row(terminal, to_row + i),
679 terminal_get_row(terminal, from_row + i),
680 terminal->data_pitch);
681 memcpy(terminal_get_attr_row(terminal, to_row + i),
682 terminal_get_attr_row(terminal, from_row + i),
683 terminal->attr_pitch);
685 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
686 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
687 attr_init(terminal_get_attr_row(terminal, i),
688 terminal->curr_attr, terminal->width);
694 terminal_scroll(struct terminal *terminal, int d)
696 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
697 terminal_scroll_buffer(terminal, d);
699 terminal_scroll_window(terminal, d);
703 terminal_shift_line(struct terminal *terminal, int d)
705 union utf8_char *row;
706 struct attr *attr_row;
708 row = terminal_get_row(terminal, terminal->row);
709 attr_row = terminal_get_attr_row(terminal, terminal->row);
711 if ((terminal->width + d) <= terminal->column)
712 d = terminal->column + 1 - terminal->width;
713 if ((terminal->column + d) >= terminal->width)
714 d = terminal->width - terminal->column - 1;
718 memmove(&row[terminal->column],
719 &row[terminal->column + d],
720 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
721 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
722 (terminal->width - terminal->column - d) * sizeof(struct attr));
723 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
724 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
726 memmove(&row[terminal->column + d], &row[terminal->column],
727 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
728 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
729 (terminal->width - terminal->column - d) * sizeof(struct attr));
730 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
731 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
736 terminal_resize_cells(struct terminal *terminal, int width, int height)
739 union utf8_char *data;
740 struct attr *data_attr;
742 int data_pitch, attr_pitch;
743 int i, l, total_rows;
744 struct rectangle allocation;
747 if (terminal->width == width && terminal->height == height)
750 data_pitch = width * sizeof(union utf8_char);
751 size = data_pitch * height;
753 attr_pitch = width * sizeof(struct attr);
754 data_attr = malloc(attr_pitch * height);
755 tab_ruler = zalloc(width);
756 attr_init(data_attr, terminal->curr_attr, width * height);
757 if (terminal->data && terminal->data_attr) {
758 if (width > terminal->width)
763 if (terminal->height > height) {
765 i = 1 + terminal->row - height;
767 terminal->start = (terminal->start + i) % terminal->height;
768 terminal->row = terminal->row - i;
771 total_rows = terminal->height;
774 for (i = 0; i < total_rows; i++) {
775 memcpy(&data[width * i],
776 terminal_get_row(terminal, i),
777 l * sizeof(union utf8_char));
778 memcpy(&data_attr[width * i],
779 terminal_get_attr_row(terminal, i),
780 l * sizeof(struct attr));
783 free(terminal->data);
784 free(terminal->data_attr);
785 free(terminal->tab_ruler);
788 terminal->data_pitch = data_pitch;
789 terminal->attr_pitch = attr_pitch;
790 terminal->margin_bottom =
791 height - (terminal->height - terminal->margin_bottom);
792 terminal->width = width;
793 terminal->height = height;
794 terminal->data = data;
795 terminal->data_attr = data_attr;
796 terminal->tab_ruler = tab_ruler;
798 terminal_init_tabs(terminal);
800 /* Update the window size */
801 ws.ws_row = terminal->height;
802 ws.ws_col = terminal->width;
803 widget_get_allocation(terminal->widget, &allocation);
804 ws.ws_xpixel = allocation.width;
805 ws.ws_ypixel = allocation.height;
806 ioctl(terminal->master, TIOCSWINSZ, &ws);
810 resize_handler(struct widget *widget,
811 int32_t width, int32_t height, void *data)
813 struct terminal *terminal = data;
814 int32_t columns, rows, m;
816 m = 2 * terminal->margin;
817 columns = (width - m) / (int32_t) terminal->average_width;
818 rows = (height - m) / (int32_t) terminal->extents.height;
820 if (!window_is_fullscreen(terminal->window) &&
821 !window_is_maximized(terminal->window)) {
822 width = columns * terminal->average_width + m;
823 height = rows * terminal->extents.height + m;
824 widget_set_size(terminal->widget, width, height);
827 terminal_resize_cells(terminal, columns, rows);
831 terminal_resize(struct terminal *terminal, int columns, int rows)
833 int32_t width, height, m;
835 if (window_is_fullscreen(terminal->window) ||
836 window_is_maximized(terminal->window))
839 m = 2 * terminal->margin;
840 width = columns * terminal->average_width + m;
841 height = rows * terminal->extents.height + m;
843 window_frame_set_child_size(terminal->widget, width, height);
846 struct color_scheme DEFAULT_COLORS = {
848 {0, 0, 0, 1}, /* black */
849 {0.66, 0, 0, 1}, /* red */
850 {0 , 0.66, 0, 1}, /* green */
851 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
852 {0 , 0 , 0.66, 1}, /* blue */
853 {0.66, 0 , 0.66, 1}, /* magenta */
854 {0, 0.66, 0.66, 1}, /* cyan */
855 {0.66, 0.66, 0.66, 1}, /* light grey */
856 {0.22, 0.33, 0.33, 1}, /* dark grey */
857 {1, 0.33, 0.33, 1}, /* high red */
858 {0.33, 1, 0.33, 1}, /* high green */
859 {1, 1, 0.33, 1}, /* high yellow */
860 {0.33, 0.33, 1, 1}, /* high blue */
861 {1, 0.33, 1, 1}, /* high magenta */
862 {0.33, 1, 1, 1}, /* high cyan */
863 {1, 1, 1, 1} /* white */
865 0, /* black border */
866 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
870 terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
872 cairo_set_source_rgba(cr,
873 terminal->color_table[index].r,
874 terminal->color_table[index].g,
875 terminal->color_table[index].b,
876 terminal->color_table[index].a);
880 terminal_send_selection(struct terminal *terminal, int fd)
883 union utf8_char *p_row;
884 union decoded_attr attr;
888 fp = fdopen(fd, "w");
893 for (row = 0; row < terminal->height; row++) {
894 p_row = terminal_get_row(terminal, row);
895 for (col = 0; col < terminal->width; col++) {
896 if (p_row[col].ch == 0x200B) /* space glyph */
898 /* get the attributes for this character cell */
899 terminal_decode_attr(terminal, row, col, &attr);
902 len = strnlen((char *) p_row[col].byte, 4);
904 fwrite(p_row[col].byte, 1, len, fp);
905 if (len == 0 || col == terminal->width - 1) {
906 fwrite("\n", 1, 1, fp);
915 struct terminal *terminal;
918 union decoded_attr attr;
919 cairo_glyph_t glyphs[256], *g;
923 glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
925 run->terminal = terminal;
927 run->g = run->glyphs;
933 glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
935 cairo_scaled_font_t *font;
937 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
938 (attr.key != run->attr.key)) {
939 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
940 font = run->terminal->font_bold;
942 font = run->terminal->font_normal;
943 cairo_set_scaled_font(run->cr, font);
944 terminal_set_color(run->terminal, run->cr,
947 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
948 cairo_show_glyphs (run->cr, run->glyphs, run->count);
949 run->g = run->glyphs;
956 glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
959 cairo_scaled_font_t *font;
961 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
963 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
964 font = run->terminal->font_bold;
966 font = run->terminal->font_normal;
968 cairo_move_to(run->cr, x, y);
969 cairo_scaled_font_text_to_glyphs (font, x, y,
971 &run->g, &num_glyphs,
973 run->g += num_glyphs;
974 run->count += num_glyphs;
979 redraw_handler(struct widget *widget, void *data)
981 struct terminal *terminal = data;
982 struct rectangle allocation;
984 int top_margin, side_margin;
985 int row, col, cursor_x, cursor_y;
986 union utf8_char *p_row;
987 union decoded_attr attr;
989 cairo_surface_t *surface;
991 struct glyph_run run;
992 cairo_font_extents_t extents;
993 double average_width;
994 double unichar_width;
996 surface = window_get_surface(terminal->window);
997 widget_get_allocation(terminal->widget, &allocation);
998 cr = widget_cairo_create(terminal->widget);
999 cairo_rectangle(cr, allocation.x, allocation.y,
1000 allocation.width, allocation.height);
1002 cairo_push_group(cr);
1004 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1005 terminal_set_color(terminal, cr, terminal->color_scheme->border);
1008 cairo_set_scaled_font(cr, terminal->font_normal);
1010 extents = terminal->extents;
1011 average_width = terminal->average_width;
1012 side_margin = (allocation.width - terminal->width * average_width) / 2;
1013 top_margin = (allocation.height - terminal->height * extents.height) / 2;
1015 cairo_set_line_width(cr, 1.0);
1016 cairo_translate(cr, allocation.x + side_margin,
1017 allocation.y + top_margin);
1018 /* paint the background */
1019 for (row = 0; row < terminal->height; row++) {
1020 p_row = terminal_get_row(terminal, row);
1021 for (col = 0; col < terminal->width; col++) {
1022 /* get the attributes for this character cell */
1023 terminal_decode_attr(terminal, row, col, &attr);
1025 if (attr.attr.bg == terminal->color_scheme->border)
1028 if (is_wide(p_row[col]))
1029 unichar_width = 2 * average_width;
1031 unichar_width = average_width;
1033 terminal_set_color(terminal, cr, attr.attr.bg);
1034 cairo_move_to(cr, col * average_width,
1035 row * extents.height);
1036 cairo_rel_line_to(cr, unichar_width, 0);
1037 cairo_rel_line_to(cr, 0, extents.height);
1038 cairo_rel_line_to(cr, -unichar_width, 0);
1039 cairo_close_path(cr);
1044 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1046 /* paint the foreground */
1047 glyph_run_init(&run, terminal, cr);
1048 for (row = 0; row < terminal->height; row++) {
1049 p_row = terminal_get_row(terminal, row);
1050 for (col = 0; col < terminal->width; col++) {
1051 /* get the attributes for this character cell */
1052 terminal_decode_attr(terminal, row, col, &attr);
1054 glyph_run_flush(&run, attr);
1056 text_x = col * average_width;
1057 text_y = extents.ascent + row * extents.height;
1058 if (attr.attr.a & ATTRMASK_UNDERLINE) {
1059 terminal_set_color(terminal, cr, attr.attr.fg);
1060 cairo_move_to(cr, text_x, (double)text_y + 1.5);
1061 cairo_line_to(cr, text_x + average_width, (double) text_y + 1.5);
1065 glyph_run_add(&run, text_x, text_y, &p_row[col]);
1070 glyph_run_flush(&run, attr);
1072 if ((terminal->mode & MODE_SHOW_CURSOR) &&
1073 !window_has_focus(terminal->window)) {
1076 cairo_set_line_width(cr, 1);
1077 cairo_move_to(cr, terminal->column * average_width + d,
1078 terminal->row * extents.height + d);
1079 cairo_rel_line_to(cr, average_width - 2 * d, 0);
1080 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
1081 cairo_rel_line_to(cr, -average_width + 2 * d, 0);
1082 cairo_close_path(cr);
1087 cairo_pop_group_to_source(cr);
1090 cairo_surface_destroy(surface);
1092 if (terminal->send_cursor_position) {
1093 cursor_x = side_margin + allocation.x +
1094 terminal->column * average_width;
1095 cursor_y = top_margin + allocation.y +
1096 terminal->row * extents.height;
1097 window_set_text_cursor_position(terminal->window,
1098 cursor_x, cursor_y);
1099 terminal->send_cursor_position = 0;
1104 terminal_write(struct terminal *terminal, const char *data, size_t length)
1106 if (write(terminal->master, data, length) < 0)
1108 terminal->send_cursor_position = 1;
1112 terminal_data(struct terminal *terminal, const char *data, size_t length);
1115 handle_char(struct terminal *terminal, union utf8_char utf8);
1118 handle_sgr(struct terminal *terminal, int code);
1121 handle_term_parameter(struct terminal *terminal, int code, int sr)
1125 if (terminal->escape_flags & ESC_FLAG_WHAT) {
1127 case 1: /* DECCKM */
1128 if (sr) terminal->key_mode = KM_APPLICATION;
1129 else terminal->key_mode = KM_NORMAL;
1131 case 2: /* DECANM */
1132 /* No VT52 support yet */
1133 terminal->g0 = CS_US;
1134 terminal->g1 = CS_US;
1135 terminal->cs = terminal->g0;
1137 case 3: /* DECCOLM */
1139 terminal_resize(terminal, 132, 24);
1141 terminal_resize(terminal, 80, 24);
1143 /* set columns, but also home cursor and clear screen */
1144 terminal->row = 0; terminal->column = 0;
1145 for (i = 0; i < terminal->height; i++) {
1146 memset(terminal_get_row(terminal, i),
1147 0, terminal->data_pitch);
1148 attr_init(terminal_get_attr_row(terminal, i),
1149 terminal->curr_attr, terminal->width);
1152 case 5: /* DECSCNM */
1153 if (sr) terminal->mode |= MODE_INVERSE;
1154 else terminal->mode &= ~MODE_INVERSE;
1157 terminal->origin_mode = sr;
1158 if (terminal->origin_mode)
1159 terminal->row = terminal->margin_top;
1162 terminal->column = 0;
1164 case 7: /* DECAWM */
1165 if (sr) terminal->mode |= MODE_AUTOWRAP;
1166 else terminal->mode &= ~MODE_AUTOWRAP;
1168 case 8: /* DECARM */
1169 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1170 else terminal->mode &= ~MODE_AUTOREPEAT;
1172 case 12: /* Very visible cursor (CVVIS) */
1173 /* FIXME: What do we do here. */
1176 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1177 else terminal->mode &= ~MODE_SHOW_CURSOR;
1179 case 1034: /* smm/rmm, meta mode on/off */
1182 case 1037: /* deleteSendsDel */
1183 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1184 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1186 case 1039: /* altSendsEscape */
1187 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1188 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1190 case 1049: /* rmcup/smcup, alternate screen */
1191 /* Ignore. Should be possible to implement,
1192 * but it's kind of annoying. */
1195 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1201 if (sr) terminal->mode |= MODE_IRM;
1202 else terminal->mode &= ~MODE_IRM;
1205 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1206 else terminal->mode &= ~MODE_LF_NEWLINE;
1209 fprintf(stderr, "Unknown parameter: %d\n", code);
1216 handle_dcs(struct terminal *terminal)
1221 handle_osc(struct terminal *terminal)
1226 terminal->escape[terminal->escape_length++] = '\0';
1227 p = &terminal->escape[2];
1228 code = strtol(p, &p, 10);
1232 case 0: /* Icon name and window title */
1233 case 1: /* Icon label */
1234 case 2: /* Window title*/
1235 window_set_title(terminal->window, p);
1237 case 7: /* shell cwd as uri */
1240 fprintf(stderr, "Unknown OSC escape code %d, text %s\n",
1247 handle_escape(struct terminal *terminal)
1249 union utf8_char *row;
1250 struct attr *attr_row;
1252 int i, count, x, y, top, bottom;
1253 int args[10], set[10] = { 0, };
1254 char response[MAX_RESPONSE] = {0, };
1255 struct rectangle allocation;
1257 terminal->escape[terminal->escape_length++] = '\0';
1259 p = &terminal->escape[2];
1260 while ((isdigit(*p) || *p == ';') && i < 10) {
1269 args[i] = strtol(p, &p, 10);
1276 count = set[0] ? args[0] : 1;
1277 if (count == 0) count = 1;
1278 terminal_shift_line(terminal, count);
1281 count = set[0] ? args[0] : 1;
1282 if (count == 0) count = 1;
1283 if (terminal->row - count >= terminal->margin_top)
1284 terminal->row -= count;
1286 terminal->row = terminal->margin_top;
1289 count = set[0] ? args[0] : 1;
1290 if (count == 0) count = 1;
1291 if (terminal->row + count <= terminal->margin_bottom)
1292 terminal->row += count;
1294 terminal->row = terminal->margin_bottom;
1297 count = set[0] ? args[0] : 1;
1298 if (count == 0) count = 1;
1299 if ((terminal->column + count) < terminal->width)
1300 terminal->column += count;
1302 terminal->column = terminal->width - 1;
1305 count = set[0] ? args[0] : 1;
1306 if (count == 0) count = 1;
1307 if ((terminal->column - count) >= 0)
1308 terminal->column -= count;
1310 terminal->column = 0;
1313 count = set[0] ? args[0] : 1;
1314 if (terminal->row + count <= terminal->margin_bottom)
1315 terminal->row += count;
1317 terminal->row = terminal->margin_bottom;
1318 terminal->column = 0;
1321 count = set[0] ? args[0] : 1;
1322 if (terminal->row - count >= terminal->margin_top)
1323 terminal->row -= count;
1325 terminal->row = terminal->margin_top;
1326 terminal->column = 0;
1329 y = set[0] ? args[0] : 1;
1330 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1332 terminal->column = y - 1;
1336 x = (set[1] ? args[1] : 1) - 1;
1338 (x >= terminal->width ? terminal->width - 1 : x);
1340 y = (set[0] ? args[0] : 1) - 1;
1341 if (terminal->origin_mode) {
1342 y += terminal->margin_top;
1343 y = y < terminal->margin_top ? terminal->margin_top :
1344 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1347 (y >= terminal->height ? terminal->height - 1 : y);
1351 terminal->column = x;
1354 count = set[0] ? args[0] : 1;
1355 if (count == 0) count = 1;
1356 while (count > 0 && terminal->column < terminal->width) {
1357 if (terminal->tab_ruler[terminal->column]) count--;
1363 row = terminal_get_row(terminal, terminal->row);
1364 attr_row = terminal_get_attr_row(terminal, terminal->row);
1365 if (!set[0] || args[0] == 0 || args[0] > 2) {
1366 memset(&row[terminal->column],
1367 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1368 attr_init(&attr_row[terminal->column],
1369 terminal->curr_attr, terminal->width - terminal->column);
1370 for (i = terminal->row + 1; i < terminal->height; i++) {
1371 memset(terminal_get_row(terminal, i),
1372 0, terminal->data_pitch);
1373 attr_init(terminal_get_attr_row(terminal, i),
1374 terminal->curr_attr, terminal->width);
1376 } else if (args[0] == 1) {
1377 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1378 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1379 for (i = 0; i < terminal->row; i++) {
1380 memset(terminal_get_row(terminal, i),
1381 0, terminal->data_pitch);
1382 attr_init(terminal_get_attr_row(terminal, i),
1383 terminal->curr_attr, terminal->width);
1385 } else if (args[0] == 2) {
1386 for (i = 0; i < terminal->height; i++) {
1387 memset(terminal_get_row(terminal, i),
1388 0, terminal->data_pitch);
1389 attr_init(terminal_get_attr_row(terminal, i),
1390 terminal->curr_attr, terminal->width);
1395 row = terminal_get_row(terminal, terminal->row);
1396 attr_row = terminal_get_attr_row(terminal, terminal->row);
1397 if (!set[0] || args[0] == 0 || args[0] > 2) {
1398 memset(&row[terminal->column], 0,
1399 (terminal->width - terminal->column) * sizeof(union utf8_char));
1400 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1401 terminal->width - terminal->column);
1402 } else if (args[0] == 1) {
1403 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1404 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1405 } else if (args[0] == 2) {
1406 memset(row, 0, terminal->data_pitch);
1407 attr_init(attr_row, terminal->curr_attr, terminal->width);
1411 count = set[0] ? args[0] : 1;
1412 if (count == 0) count = 1;
1413 if (terminal->row >= terminal->margin_top &&
1414 terminal->row < terminal->margin_bottom)
1416 top = terminal->margin_top;
1417 terminal->margin_top = terminal->row;
1418 terminal_scroll(terminal, 0 - count);
1419 terminal->margin_top = top;
1420 } else if (terminal->row == terminal->margin_bottom) {
1421 memset(terminal_get_row(terminal, terminal->row),
1422 0, terminal->data_pitch);
1423 attr_init(terminal_get_attr_row(terminal, terminal->row),
1424 terminal->curr_attr, terminal->width);
1428 count = set[0] ? args[0] : 1;
1429 if (count == 0) count = 1;
1430 if (terminal->row >= terminal->margin_top &&
1431 terminal->row < terminal->margin_bottom)
1433 top = terminal->margin_top;
1434 terminal->margin_top = terminal->row;
1435 terminal_scroll(terminal, count);
1436 terminal->margin_top = top;
1437 } else if (terminal->row == terminal->margin_bottom) {
1438 memset(terminal_get_row(terminal, terminal->row),
1439 0, terminal->data_pitch);
1443 count = set[0] ? args[0] : 1;
1444 if (count == 0) count = 1;
1445 terminal_shift_line(terminal, 0 - count);
1448 terminal_scroll(terminal, set[0] ? args[0] : 1);
1451 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1454 count = set[0] ? args[0] : 1;
1455 if (count == 0) count = 1;
1456 if ((terminal->column + count) > terminal->width)
1457 count = terminal->width - terminal->column;
1458 row = terminal_get_row(terminal, terminal->row);
1459 attr_row = terminal_get_attr_row(terminal, terminal->row);
1460 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1461 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1464 count = set[0] ? args[0] : 1;
1465 if (count == 0) count = 1;
1466 while (count > 0 && terminal->column >= 0) {
1467 if (terminal->tab_ruler[terminal->column]) count--;
1473 y = set[0] ? args[0] : 1;
1474 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1476 terminal->column = y - 1;
1479 count = set[0] ? args[0] : 1;
1480 if (count == 0) count = 1;
1481 if (terminal->last_char.byte[0])
1482 for (i = 0; i < count; i++)
1483 handle_char(terminal, terminal->last_char);
1484 terminal->last_char.byte[0] = 0;
1486 case 'c': /* Primary DA */
1487 terminal_write(terminal, "\e[?6c", 5);
1490 x = set[0] ? args[0] : 1;
1491 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1493 terminal->row = x - 1;
1496 if (!set[0] || args[0] == 0) {
1497 terminal->tab_ruler[terminal->column] = 0;
1498 } else if (args[0] == 3) {
1499 memset(terminal->tab_ruler, 0, terminal->width);
1503 for(i = 0; i < 10 && set[i]; i++) {
1504 handle_term_parameter(terminal, args[i], 1);
1508 for(i = 0; i < 10 && set[i]; i++) {
1509 handle_term_parameter(terminal, args[i], 0);
1513 for(i = 0; i < 10; i++) {
1514 if (i <= 7 && set[i] && set[i + 1] &&
1515 set[i + 2] && args[i + 1] == 5)
1517 if (args[i] == 38) {
1518 handle_sgr(terminal, args[i + 2] + 256);
1520 } else if (args[i] == 48) {
1521 handle_sgr(terminal, args[i + 2] + 512);
1526 handle_sgr(terminal, args[i]);
1528 handle_sgr(terminal, 0);
1536 i = set[0] ? args[0] : 0;
1537 if (i == 0 || i == 5) {
1538 terminal_write(terminal, "\e[0n", 4);
1539 } else if (i == 6) {
1540 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1541 terminal->origin_mode ?
1542 terminal->row+terminal->margin_top : terminal->row+1,
1543 terminal->column+1);
1544 terminal_write(terminal, response, strlen(response));
1549 terminal->margin_top = 0;
1550 terminal->margin_bottom = terminal->height-1;
1552 terminal->column = 0;
1554 top = (set[0] ? args[0] : 1) - 1;
1556 (top >= terminal->height ? terminal->height - 1 : top);
1557 bottom = (set[1] ? args[1] : 1) - 1;
1558 bottom = bottom < 0 ? 0 :
1559 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1561 terminal->margin_top = top;
1562 terminal->margin_bottom = bottom;
1564 terminal->margin_top = 0;
1565 terminal->margin_bottom = terminal->height-1;
1567 if(terminal->origin_mode)
1568 terminal->row = terminal->margin_top;
1571 terminal->column = 0;
1575 terminal->saved_row = terminal->row;
1576 terminal->saved_column = terminal->column;
1578 case 't': /* windowOps */
1581 case 4: /* resize px */
1582 if (set[1] && set[2]) {
1583 widget_schedule_resize(terminal->widget,
1587 case 8: /* resize ch */
1588 if (set[1] && set[2]) {
1589 terminal_resize(terminal, args[2], args[1]);
1592 case 13: /* report position */
1593 widget_get_allocation(terminal->widget, &allocation);
1594 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1595 allocation.x, allocation.y);
1596 terminal_write(terminal, response, strlen(response));
1598 case 14: /* report px */
1599 widget_get_allocation(terminal->widget, &allocation);
1600 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1601 allocation.height, allocation.width);
1602 terminal_write(terminal, response, strlen(response));
1604 case 18: /* report ch */
1605 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1606 terminal->height, terminal->width);
1607 terminal_write(terminal, response, strlen(response));
1609 case 21: /* report title */
1610 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1611 window_get_title(terminal->window));
1612 terminal_write(terminal, response, strlen(response));
1616 terminal_resize(terminal, terminal->width, args[0]);
1618 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1622 terminal->row = terminal->saved_row;
1623 terminal->column = terminal->saved_column;
1626 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
1632 handle_non_csi_escape(struct terminal *terminal, char code)
1637 if(terminal->row < terminal->margin_top) {
1638 terminal->row = terminal->margin_top;
1639 terminal_scroll(terminal, -1);
1643 terminal->column = 0;
1647 if(terminal->row > terminal->margin_bottom) {
1648 terminal->row = terminal->margin_bottom;
1649 terminal_scroll(terminal, +1);
1653 terminal_init(terminal);
1656 terminal->tab_ruler[terminal->column] = 1;
1658 case '7': /* DECSC */
1659 terminal->saved_row = terminal->row;
1660 terminal->saved_column = terminal->column;
1661 terminal->saved_attr = terminal->curr_attr;
1662 terminal->saved_origin_mode = terminal->origin_mode;
1663 terminal->saved_cs = terminal->cs;
1664 terminal->saved_g0 = terminal->g0;
1665 terminal->saved_g1 = terminal->g1;
1667 case '8': /* DECRC */
1668 terminal->row = terminal->saved_row;
1669 terminal->column = terminal->saved_column;
1670 terminal->curr_attr = terminal->saved_attr;
1671 terminal->origin_mode = terminal->saved_origin_mode;
1672 terminal->cs = terminal->saved_cs;
1673 terminal->g0 = terminal->saved_g0;
1674 terminal->g1 = terminal->saved_g1;
1676 case '=': /* DECPAM */
1677 terminal->key_mode = KM_APPLICATION;
1679 case '>': /* DECPNM */
1680 terminal->key_mode = KM_NORMAL;
1683 fprintf(stderr, "Unknown escape code: %c\n", code);
1689 handle_special_escape(struct terminal *terminal, char special, char code)
1693 if (special == '#') {
1696 /* fill with 'E', no cheap way to do this */
1697 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1698 numChars = terminal->width * terminal->height;
1699 for(i = 0; i < numChars; i++) {
1700 terminal->data[i].byte[0] = 'E';
1704 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1707 } else if (special == '(' || special == ')') {
1711 terminal->g0 = CS_SPECIAL;
1713 terminal->g1 = CS_SPECIAL;
1717 terminal->g0 = CS_UK;
1719 terminal->g1 = CS_UK;
1723 terminal->g0 = CS_US;
1725 terminal->g1 = CS_US;
1728 fprintf(stderr, "Unknown character set %c\n", code);
1732 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1737 handle_sgr(struct terminal *terminal, int code)
1741 terminal->curr_attr = terminal->color_scheme->default_attr;
1744 terminal->curr_attr.a |= ATTRMASK_BOLD;
1745 if (terminal->curr_attr.fg < 8)
1746 terminal->curr_attr.fg += 8;
1749 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1752 terminal->curr_attr.a |= ATTRMASK_BLINK;
1755 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1760 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1761 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1762 terminal->curr_attr.fg -= 8;
1765 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1768 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1772 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1775 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1778 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1781 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1784 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1787 if(code >= 30 && code <= 37) {
1788 terminal->curr_attr.fg = code - 30;
1789 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1790 terminal->curr_attr.fg += 8;
1791 } else if(code >= 40 && code <= 47) {
1792 terminal->curr_attr.bg = code - 40;
1793 } else if (code >= 90 && code <= 97) {
1794 terminal->curr_attr.fg = code - 90 + 8;
1795 } else if (code >= 100 && code <= 107) {
1796 terminal->curr_attr.bg = code - 100 + 8;
1797 } else if(code >= 256 && code < 512) {
1798 terminal->curr_attr.fg = code - 256;
1799 } else if(code >= 512 && code < 768) {
1800 terminal->curr_attr.bg = code - 512;
1802 fprintf(stderr, "Unknown SGR code: %d\n", code);
1808 /* Returns 1 if c was special, otherwise 0 */
1810 handle_special_char(struct terminal *terminal, char c)
1812 union utf8_char *row;
1813 struct attr *attr_row;
1815 row = terminal_get_row(terminal, terminal->row);
1816 attr_row = terminal_get_attr_row(terminal, terminal->row);
1820 terminal->column = 0;
1823 if (terminal->mode & MODE_LF_NEWLINE) {
1824 terminal->column = 0;
1830 if(terminal->row > terminal->margin_bottom) {
1831 terminal->row = terminal->margin_bottom;
1832 terminal_scroll(terminal, +1);
1837 while (terminal->column < terminal->width) {
1838 if (terminal->mode & MODE_IRM)
1839 terminal_shift_line(terminal, +1);
1841 if (row[terminal->column].byte[0] == '\0') {
1842 row[terminal->column].byte[0] = ' ';
1843 row[terminal->column].byte[1] = '\0';
1844 attr_row[terminal->column] = terminal->curr_attr;
1848 if (terminal->tab_ruler[terminal->column]) break;
1850 if (terminal->column >= terminal->width) {
1851 terminal->column = terminal->width - 1;
1856 if (terminal->column >= terminal->width) {
1857 terminal->column = terminal->width - 2;
1858 } else if (terminal->column > 0) {
1860 } else if (terminal->mode & MODE_AUTOWRAP) {
1861 terminal->column = terminal->width - 1;
1863 if (terminal->row < terminal->margin_top) {
1864 terminal->row = terminal->margin_top;
1865 terminal_scroll(terminal, -1);
1873 case '\x0E': /* SO */
1874 terminal->cs = terminal->g1;
1876 case '\x0F': /* SI */
1877 terminal->cs = terminal->g0;
1889 handle_char(struct terminal *terminal, union utf8_char utf8)
1891 union utf8_char *row;
1892 struct attr *attr_row;
1894 if (handle_special_char(terminal, utf8.byte[0])) return;
1896 apply_char_set(terminal->cs, &utf8);
1898 /* There are a whole lot of non-characters, control codes,
1899 * and formatting codes that should probably be ignored,
1901 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1906 /* Some of these non-characters should be translated, e.g.: */
1907 if (utf8.byte[0] < 32) {
1908 utf8.byte[0] = utf8.byte[0] + 64;
1911 /* handle right margin effects */
1912 if (terminal->column >= terminal->width) {
1913 if (terminal->mode & MODE_AUTOWRAP) {
1914 terminal->column = 0;
1916 if (terminal->row > terminal->margin_bottom) {
1917 terminal->row = terminal->margin_bottom;
1918 terminal_scroll(terminal, +1);
1925 row = terminal_get_row(terminal, terminal->row);
1926 attr_row = terminal_get_attr_row(terminal, terminal->row);
1928 if (terminal->mode & MODE_IRM)
1929 terminal_shift_line(terminal, +1);
1930 row[terminal->column] = utf8;
1931 attr_row[terminal->column++] = terminal->curr_attr;
1933 /* cursor jump for wide character. */
1935 row[terminal->column++].ch = 0x200B; /* space glyph */
1937 if (utf8.ch != terminal->last_char.ch)
1938 terminal->last_char = utf8;
1942 escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1946 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1947 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1948 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1949 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1950 else len = 1; /* Invalid, cannot happen */
1952 if (terminal->escape_length + len <= MAX_ESCAPE) {
1953 for (i = 0; i < len; i++)
1954 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1955 terminal->escape_length += len;
1956 } else if (terminal->escape_length < MAX_ESCAPE) {
1957 terminal->escape[terminal->escape_length++] = 0;
1962 terminal_data(struct terminal *terminal, const char *data, size_t length)
1965 union utf8_char utf8;
1966 enum utf8_state parser_state;
1968 for (i = 0; i < length; i++) {
1970 utf8_next_char(&terminal->state_machine, data[i]);
1971 switch(parser_state) {
1972 case utf8state_accept:
1973 utf8.ch = terminal->state_machine.s.ch;
1975 case utf8state_reject:
1976 /* the unicode replacement character */
1977 utf8.byte[0] = 0xEF;
1978 utf8.byte[1] = 0xBF;
1979 utf8.byte[2] = 0xBD;
1980 utf8.byte[3] = 0x00;
1986 /* assume escape codes never use non-ASCII characters */
1987 switch (terminal->state) {
1988 case escape_state_escape:
1989 escape_append_utf8(terminal, utf8);
1990 switch (utf8.byte[0]) {
1992 terminal->state = escape_state_dcs;
1995 terminal->state = escape_state_csi;
1998 terminal->state = escape_state_osc;
2002 case ')': /* special */
2003 terminal->state = escape_state_special;
2005 case '^': /* PM (not implemented) */
2006 case '_': /* APC (not implemented) */
2007 terminal->state = escape_state_ignore;
2010 terminal->state = escape_state_normal;
2011 handle_non_csi_escape(terminal, utf8.byte[0]);
2015 case escape_state_csi:
2016 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
2018 } else if (utf8.byte[0] == '?') {
2019 terminal->escape_flags |= ESC_FLAG_WHAT;
2020 } else if (utf8.byte[0] == '>') {
2021 terminal->escape_flags |= ESC_FLAG_GT;
2022 } else if (utf8.byte[0] == '!') {
2023 terminal->escape_flags |= ESC_FLAG_BANG;
2024 } else if (utf8.byte[0] == '$') {
2025 terminal->escape_flags |= ESC_FLAG_CASH;
2026 } else if (utf8.byte[0] == '\'') {
2027 terminal->escape_flags |= ESC_FLAG_SQUOTE;
2028 } else if (utf8.byte[0] == '"') {
2029 terminal->escape_flags |= ESC_FLAG_DQUOTE;
2030 } else if (utf8.byte[0] == ' ') {
2031 terminal->escape_flags |= ESC_FLAG_SPACE;
2033 escape_append_utf8(terminal, utf8);
2034 if (terminal->escape_length >= MAX_ESCAPE)
2035 terminal->state = escape_state_normal;
2038 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
2039 utf8.byte[0] == '`')
2041 terminal->state = escape_state_normal;
2042 handle_escape(terminal);
2046 case escape_state_inner_escape:
2047 if (utf8.byte[0] == '\\') {
2048 terminal->state = escape_state_normal;
2049 if (terminal->outer_state == escape_state_dcs) {
2050 handle_dcs(terminal);
2051 } else if (terminal->outer_state == escape_state_osc) {
2052 handle_osc(terminal);
2054 } else if (utf8.byte[0] == '\e') {
2055 terminal->state = terminal->outer_state;
2056 escape_append_utf8(terminal, utf8);
2057 if (terminal->escape_length >= MAX_ESCAPE)
2058 terminal->state = escape_state_normal;
2060 terminal->state = terminal->outer_state;
2061 if (terminal->escape_length < MAX_ESCAPE)
2062 terminal->escape[terminal->escape_length++] = '\e';
2063 escape_append_utf8(terminal, utf8);
2064 if (terminal->escape_length >= MAX_ESCAPE)
2065 terminal->state = escape_state_normal;
2068 case escape_state_dcs:
2069 case escape_state_osc:
2070 case escape_state_ignore:
2071 if (utf8.byte[0] == '\e') {
2072 terminal->outer_state = terminal->state;
2073 terminal->state = escape_state_inner_escape;
2074 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
2075 terminal->state = escape_state_normal;
2076 handle_osc(terminal);
2078 escape_append_utf8(terminal, utf8);
2079 if (terminal->escape_length >= MAX_ESCAPE)
2080 terminal->state = escape_state_normal;
2083 case escape_state_special:
2084 escape_append_utf8(terminal, utf8);
2085 terminal->state = escape_state_normal;
2086 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2087 handle_special_escape(terminal, terminal->escape[1],
2095 /* this is valid, because ASCII characters are never used to
2096 * introduce a multibyte sequence in UTF-8 */
2097 if (utf8.byte[0] == '\e') {
2098 terminal->state = escape_state_escape;
2099 terminal->outer_state = escape_state_normal;
2100 terminal->escape[0] = '\e';
2101 terminal->escape_length = 1;
2102 terminal->escape_flags = 0;
2104 handle_char(terminal, utf8);
2108 window_schedule_redraw(terminal->window);
2112 data_source_target(void *data,
2113 struct wl_data_source *source, const char *mime_type)
2115 fprintf(stderr, "data_source_target, %s\n", mime_type);
2119 data_source_send(void *data,
2120 struct wl_data_source *source,
2121 const char *mime_type, int32_t fd)
2123 struct terminal *terminal = data;
2125 terminal_send_selection(terminal, fd);
2129 data_source_cancelled(void *data, struct wl_data_source *source)
2131 wl_data_source_destroy(source);
2134 static const struct wl_data_source_listener data_source_listener = {
2137 data_source_cancelled
2140 static const char text_mime_type[] = "text/plain;charset=utf-8";
2143 data_handler(struct window *window,
2144 struct input *input,
2145 float x, float y, const char **types, void *data)
2147 int i, has_text = 0;
2151 for (i = 0; types[i]; i++)
2152 if (strcmp(types[i], text_mime_type) == 0)
2156 input_accept(input, NULL);
2158 input_accept(input, text_mime_type);
2163 drop_handler(struct window *window, struct input *input,
2164 int32_t x, int32_t y, void *data)
2166 struct terminal *terminal = data;
2168 input_receive_drag_data_to_fd(input, text_mime_type, terminal->master);
2172 fullscreen_handler(struct window *window, void *data)
2174 struct terminal *terminal = data;
2176 window_set_fullscreen(window, !window_is_fullscreen(terminal->window));
2180 close_handler(struct window *window, void *data)
2182 struct terminal *terminal = data;
2184 terminal_destroy(terminal);
2188 handle_bound_key(struct terminal *terminal,
2189 struct input *input, uint32_t sym, uint32_t time)
2191 struct terminal *new_terminal;
2195 /* Cut selection; terminal doesn't do cut, fall
2196 * through to copy. */
2198 terminal->selection =
2199 display_create_data_source(terminal->display);
2200 wl_data_source_offer(terminal->selection,
2201 "text/plain;charset=utf-8");
2202 wl_data_source_add_listener(terminal->selection,
2203 &data_source_listener, terminal);
2204 input_set_selection(input, terminal->selection,
2205 display_get_serial(terminal->display));
2208 input_receive_selection_data_to_fd(input,
2209 "text/plain;charset=utf-8",
2215 new_terminal = terminal_create(terminal->display);
2216 if (terminal_run(new_terminal, option_shell))
2217 terminal_destroy(new_terminal);
2227 key_handler(struct window *window, struct input *input, uint32_t time,
2228 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2231 struct terminal *terminal = data;
2232 char ch[MAX_RESPONSE];
2233 uint32_t modifiers, serial;
2235 bool convert_utf8 = true;
2237 modifiers = input_get_modifiers(input);
2238 if ((modifiers & MOD_CONTROL_MASK) &&
2239 (modifiers & MOD_SHIFT_MASK) &&
2240 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2241 handle_bound_key(terminal, input, sym, time))
2244 /* Map keypad symbols to 'normal' equivalents before processing */
2246 case XKB_KEY_KP_Space:
2247 sym = XKB_KEY_space;
2249 case XKB_KEY_KP_Tab:
2252 case XKB_KEY_KP_Enter:
2253 sym = XKB_KEY_Return;
2255 case XKB_KEY_KP_Left:
2261 case XKB_KEY_KP_Right:
2262 sym = XKB_KEY_Right;
2264 case XKB_KEY_KP_Down:
2267 case XKB_KEY_KP_Equal:
2268 sym = XKB_KEY_equal;
2270 case XKB_KEY_KP_Multiply:
2271 sym = XKB_KEY_asterisk;
2273 case XKB_KEY_KP_Add:
2276 case XKB_KEY_KP_Separator:
2277 /* Note this is actually locale-dependent and should mostly be
2278 * a comma. But leave it as period until we one day start
2279 * doing the right thing. */
2280 sym = XKB_KEY_period;
2282 case XKB_KEY_KP_Subtract:
2283 sym = XKB_KEY_minus;
2285 case XKB_KEY_KP_Decimal:
2286 sym = XKB_KEY_period;
2288 case XKB_KEY_KP_Divide:
2289 sym = XKB_KEY_slash;
2301 sym = (sym - XKB_KEY_KP_0) + XKB_KEY_0;
2308 case XKB_KEY_BackSpace:
2309 if (modifiers & MOD_ALT_MASK)
2314 case XKB_KEY_Linefeed:
2317 case XKB_KEY_Scroll_Lock:
2318 case XKB_KEY_Sys_Req:
2319 case XKB_KEY_Escape:
2320 ch[len++] = sym & 0x7f;
2323 case XKB_KEY_Return:
2324 if (terminal->mode & MODE_LF_NEWLINE) {
2332 case XKB_KEY_Shift_L:
2333 case XKB_KEY_Shift_R:
2334 case XKB_KEY_Control_L:
2335 case XKB_KEY_Control_R:
2338 case XKB_KEY_Meta_L:
2339 case XKB_KEY_Meta_R:
2340 case XKB_KEY_Super_L:
2341 case XKB_KEY_Super_R:
2342 case XKB_KEY_Hyper_L:
2343 case XKB_KEY_Hyper_R:
2346 case XKB_KEY_Insert:
2347 len = function_key_response('[', 2, modifiers, '~', ch);
2349 case XKB_KEY_Delete:
2350 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2353 len = function_key_response('[', 3, modifiers, '~', ch);
2356 case XKB_KEY_Page_Up:
2357 len = function_key_response('[', 5, modifiers, '~', ch);
2359 case XKB_KEY_Page_Down:
2360 len = function_key_response('[', 6, modifiers, '~', ch);
2363 len = function_key_response('O', 1, modifiers, 'P', ch);
2366 len = function_key_response('O', 1, modifiers, 'Q', ch);
2369 len = function_key_response('O', 1, modifiers, 'R', ch);
2372 len = function_key_response('O', 1, modifiers, 'S', ch);
2375 len = function_key_response('[', 15, modifiers, '~', ch);
2378 len = function_key_response('[', 17, modifiers, '~', ch);
2381 len = function_key_response('[', 18, modifiers, '~', ch);
2384 len = function_key_response('[', 19, modifiers, '~', ch);
2387 len = function_key_response('[', 20, modifiers, '~', ch);
2390 len = function_key_response('[', 21, modifiers, '~', ch);
2393 len = function_key_response('[', 24, modifiers, '~', ch);
2396 /* Handle special keys with alternate mappings */
2397 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2398 if (len != 0) break;
2400 if (modifiers & MOD_CONTROL_MASK) {
2401 if (sym >= '3' && sym <= '7')
2402 sym = (sym & 0x1f) + 8;
2404 if (!((sym >= '!' && sym <= '/') ||
2405 (sym >= '8' && sym <= '?') ||
2406 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2407 else if (sym == '2') sym = 0x00;
2408 else if (sym == '/') sym = 0x1F;
2409 else if (sym == '8' || sym == '?') sym = 0x7F;
2411 if (modifiers & MOD_ALT_MASK) {
2412 if (terminal->mode & MODE_ALT_SENDS_ESC) {
2416 convert_utf8 = false;
2421 (!convert_utf8 && sym < 256)) {
2424 ret = xkb_keysym_to_utf8(sym, ch + len,
2425 MAX_RESPONSE - len);
2428 "Warning: buffer too small to encode "
2429 "UTF8 character\n");
2437 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
2438 terminal_write(terminal, ch, len);
2440 /* Hide cursor, except if this was coming from a
2441 * repeating key press. */
2442 serial = display_get_serial(terminal->display);
2443 if (terminal->hide_cursor_serial != serial) {
2444 input_set_pointer_image(input, CURSOR_BLANK);
2445 terminal->hide_cursor_serial = serial;
2451 keyboard_focus_handler(struct window *window,
2452 struct input *device, void *data)
2454 struct terminal *terminal = data;
2456 window_schedule_redraw(terminal->window);
2459 static int wordsep(int ch)
2461 const char extra[] = "-,./?%&#:_=+@~";
2466 return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
2470 recompute_selection(struct terminal *terminal)
2472 struct rectangle allocation;
2473 int col, x, width, height;
2474 int start_row, end_row;
2475 int word_start, eol;
2476 int side_margin, top_margin;
2479 union utf8_char *data;
2481 cw = terminal->average_width;
2482 ch = terminal->extents.height;
2483 widget_get_allocation(terminal->widget, &allocation);
2484 width = terminal->width * cw;
2485 height = terminal->height * ch;
2486 side_margin = allocation.x + (allocation.width - width) / 2;
2487 top_margin = allocation.y + (allocation.height - height) / 2;
2489 start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1;
2490 end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1;
2492 if (start_row < end_row ||
2493 (start_row == end_row &&
2494 terminal->selection_start_x < terminal->selection_end_x)) {
2495 terminal->selection_start_row = start_row;
2496 terminal->selection_end_row = end_row;
2497 start_x = terminal->selection_start_x;
2498 end_x = terminal->selection_end_x;
2500 terminal->selection_start_row = end_row;
2501 terminal->selection_end_row = start_row;
2502 start_x = terminal->selection_end_x;
2503 end_x = terminal->selection_start_x;
2507 if (terminal->selection_start_row < 0) {
2508 terminal->selection_start_row = 0;
2509 terminal->selection_start_col = 0;
2511 x = side_margin + cw / 2;
2512 data = terminal_get_row(terminal,
2513 terminal->selection_start_row);
2515 for (col = 0; col < terminal->width; col++, x += cw) {
2516 if (col == 0 || wordsep(data[col - 1].ch))
2518 if (data[col].ch != 0)
2524 switch (terminal->dragging) {
2526 terminal->selection_start_col = 0;
2529 terminal->selection_start_col = word_start;
2532 terminal->selection_start_col = col;
2537 if (terminal->selection_end_row >= terminal->height) {
2538 terminal->selection_end_row = terminal->height;
2539 terminal->selection_end_col = 0;
2541 x = side_margin + cw / 2;
2542 data = terminal_get_row(terminal, terminal->selection_end_row);
2543 for (col = 0; col < terminal->width; col++, x += cw) {
2544 if (terminal->dragging == SELECT_CHAR && end_x < x)
2546 if (terminal->dragging == SELECT_WORD &&
2547 end_x < x && wordsep(data[col].ch))
2550 terminal->selection_end_col = col;
2553 if (terminal->selection_end_col != terminal->selection_start_col ||
2554 terminal->selection_start_row != terminal->selection_end_row) {
2555 col = terminal->selection_end_col;
2556 if (col > 0 && data[col - 1].ch == 0)
2557 terminal->selection_end_col = terminal->width;
2558 data = terminal_get_row(terminal, terminal->selection_start_row);
2559 if (data[terminal->selection_start_col].ch == 0)
2560 terminal->selection_start_col = eol;
2567 button_handler(struct widget *widget,
2568 struct input *input, uint32_t time,
2570 enum wl_pointer_button_state state, void *data)
2572 struct terminal *terminal = data;
2576 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2578 if (time - terminal->button_time < 500)
2579 terminal->click_count++;
2581 terminal->click_count = 1;
2583 terminal->button_time = time;
2584 terminal->dragging =
2585 (terminal->click_count - 1) % 3 + SELECT_CHAR;
2587 input_get_position(input,
2588 &terminal->selection_start_x,
2589 &terminal->selection_start_y);
2590 terminal->selection_end_x = terminal->selection_start_x;
2591 terminal->selection_end_y = terminal->selection_start_y;
2592 if (recompute_selection(terminal))
2593 widget_schedule_redraw(widget);
2595 terminal->dragging = SELECT_NONE;
2602 enter_handler(struct widget *widget,
2603 struct input *input, float x, float y, void *data)
2605 return CURSOR_IBEAM;
2609 motion_handler(struct widget *widget,
2610 struct input *input, uint32_t time,
2611 float x, float y, void *data)
2613 struct terminal *terminal = data;
2615 if (terminal->dragging) {
2616 input_get_position(input,
2617 &terminal->selection_end_x,
2618 &terminal->selection_end_y);
2620 if (recompute_selection(terminal))
2621 widget_schedule_redraw(widget);
2624 return CURSOR_IBEAM;
2628 output_handler(struct window *window, struct output *output, int enter,
2632 window_set_buffer_transform(window, output_get_transform(output));
2633 window_set_buffer_scale(window, window_get_output_scale(window));
2634 window_schedule_redraw(window);
2638 #define howmany(x, y) (((x) + ((y) - 1)) / (y))
2641 static struct terminal *
2642 terminal_create(struct display *display)
2644 struct terminal *terminal;
2645 cairo_surface_t *surface;
2647 cairo_text_extents_t text_extents;
2649 terminal = xzalloc(sizeof *terminal);
2650 terminal->color_scheme = &DEFAULT_COLORS;
2651 terminal_init(terminal);
2652 terminal->margin_top = 0;
2653 terminal->margin_bottom = -1;
2654 terminal->window = window_create(display);
2655 terminal->widget = window_frame_create(terminal->window, terminal);
2656 window_set_title(terminal->window, "Wayland Terminal");
2657 widget_set_transparent(terminal->widget, 0);
2659 init_state_machine(&terminal->state_machine);
2660 init_color_table(terminal);
2662 terminal->display = display;
2663 terminal->margin = 5;
2665 window_set_user_data(terminal->window, terminal);
2666 window_set_key_handler(terminal->window, key_handler);
2667 window_set_keyboard_focus_handler(terminal->window,
2668 keyboard_focus_handler);
2669 window_set_fullscreen_handler(terminal->window, fullscreen_handler);
2670 window_set_output_handler(terminal->window, output_handler);
2671 window_set_close_handler(terminal->window, close_handler);
2673 window_set_data_handler(terminal->window, data_handler);
2674 window_set_drop_handler(terminal->window, drop_handler);
2676 widget_set_redraw_handler(terminal->widget, redraw_handler);
2677 widget_set_resize_handler(terminal->widget, resize_handler);
2678 widget_set_button_handler(terminal->widget, button_handler);
2679 widget_set_enter_handler(terminal->widget, enter_handler);
2680 widget_set_motion_handler(terminal->widget, motion_handler);
2682 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2683 cr = cairo_create(surface);
2684 cairo_set_font_size(cr, option_font_size);
2685 cairo_select_font_face (cr, option_font,
2686 CAIRO_FONT_SLANT_NORMAL,
2687 CAIRO_FONT_WEIGHT_BOLD);
2688 terminal->font_bold = cairo_get_scaled_font (cr);
2689 cairo_scaled_font_reference(terminal->font_bold);
2691 cairo_select_font_face (cr, option_font,
2692 CAIRO_FONT_SLANT_NORMAL,
2693 CAIRO_FONT_WEIGHT_NORMAL);
2694 terminal->font_normal = cairo_get_scaled_font (cr);
2695 cairo_scaled_font_reference(terminal->font_normal);
2697 cairo_font_extents(cr, &terminal->extents);
2699 /* Compute the average ascii glyph width */
2700 cairo_text_extents(cr, TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS,
2702 terminal->average_width = howmany
2703 (text_extents.width,
2704 strlen(TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS));
2705 terminal->average_width = ceil(terminal->average_width);
2708 cairo_surface_destroy(surface);
2710 terminal_resize(terminal, 20, 5); /* Set minimum size first */
2711 terminal_resize(terminal, 80, 25);
2713 wl_list_insert(terminal_list.prev, &terminal->link);
2719 terminal_destroy(struct terminal *terminal)
2721 display_unwatch_fd(terminal->display, terminal->master);
2722 window_destroy(terminal->window);
2723 close(terminal->master);
2724 wl_list_remove(&terminal->link);
2726 if (wl_list_empty(&terminal_list))
2727 display_exit(terminal->display);
2733 io_handler(struct task *task, uint32_t events)
2735 struct terminal *terminal =
2736 container_of(task, struct terminal, io_task);
2740 if (events & EPOLLHUP) {
2741 terminal_destroy(terminal);
2745 len = read(terminal->master, buffer, sizeof buffer);
2747 terminal_destroy(terminal);
2749 terminal_data(terminal, buffer, len);
2753 terminal_run(struct terminal *terminal, const char *path)
2758 pid = forkpty(&master, NULL, NULL, NULL);
2760 setenv("TERM", option_term, 1);
2761 setenv("COLORTERM", option_term, 1);
2762 if (execl(path, path, NULL)) {
2763 printf("exec failed: %m\n");
2766 } else if (pid < 0) {
2767 fprintf(stderr, "failed to fork and create pty (%m).\n");
2771 terminal->master = master;
2772 fcntl(master, F_SETFL, O_NONBLOCK);
2773 terminal->io_task.run = io_handler;
2774 display_watch_fd(terminal->display, terminal->master,
2775 EPOLLIN | EPOLLHUP, &terminal->io_task);
2777 window_set_fullscreen(terminal->window, option_fullscreen);
2778 if (!window_is_fullscreen(terminal->window))
2779 terminal_resize(terminal, 80, 24);
2784 static const struct weston_option terminal_options[] = {
2785 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
2786 { WESTON_OPTION_STRING, "font", 0, &option_font },
2787 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
2790 int main(int argc, char *argv[])
2793 struct terminal *terminal;
2794 struct weston_config *config;
2795 struct weston_config_section *s;
2797 /* as wcwidth is locale-dependent,
2798 wcwidth needs setlocale call to function properly. */
2799 setlocale(LC_ALL, "");
2801 option_shell = getenv("SHELL");
2803 option_shell = "/bin/bash";
2805 config = weston_config_parse("weston.ini");
2806 s = weston_config_get_section(config, "terminal", NULL, NULL);
2807 weston_config_section_get_string(s, "font", &option_font, "mono");
2808 weston_config_section_get_int(s, "font-size", &option_font_size, 14);
2809 weston_config_section_get_string(s, "term", &option_term, "xterm");
2810 weston_config_destroy(config);
2812 d = display_create(&argc, argv);
2814 fprintf(stderr, "failed to create display: %m\n");
2818 wl_list_init(&terminal_list);
2819 terminal = terminal_create(d);
2820 if (terminal_run(terminal, option_shell))