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 <linux/input.h>
43 #include <wayland-client.h>
45 #include "../shared/config-parser.h"
48 static int option_fullscreen;
49 static char *option_font;
50 static int option_font_size;
51 static char *option_term;
52 static char *option_shell;
54 static struct wl_list terminal_list;
56 static struct terminal *
57 terminal_create(struct display *display);
59 terminal_destroy(struct terminal *terminal);
61 terminal_run(struct terminal *terminal, const char *path);
63 #define TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS \
67 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
69 "abcdefghijklmnopqrstuvwxyz" \
73 #define MOD_SHIFT 0x01
77 #define ATTRMASK_BOLD 0x01
78 #define ATTRMASK_UNDERLINE 0x02
79 #define ATTRMASK_BLINK 0x04
80 #define ATTRMASK_INVERSE 0x08
81 #define ATTRMASK_CONCEALED 0x10
84 #define MAX_RESPONSE 256
85 #define MAX_ESCAPE 255
88 #define MODE_SHOW_CURSOR 0x00000001
89 #define MODE_INVERSE 0x00000002
90 #define MODE_AUTOWRAP 0x00000004
91 #define MODE_AUTOREPEAT 0x00000008
92 #define MODE_LF_NEWLINE 0x00000010
93 #define MODE_IRM 0x00000020
94 #define MODE_DELETE_SENDS_DEL 0x00000040
95 #define MODE_ALT_SENDS_ESC 0x00000080
98 unsigned char byte[4];
111 struct utf8_state_machine {
112 enum utf8_state state;
119 init_state_machine(struct utf8_state_machine *machine)
121 machine->state = utf8state_start;
126 static enum utf8_state
127 utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
129 switch(machine->state) {
130 case utf8state_start:
131 case utf8state_accept:
132 case utf8state_reject:
135 if(c == 0xC0 || c == 0xC1) {
136 /* overlong encoding, reject */
137 machine->state = utf8state_reject;
138 } else if((c & 0x80) == 0) {
139 /* single byte, accept */
140 machine->s.byte[machine->len++] = c;
141 machine->state = utf8state_accept;
142 machine->unicode = c;
143 } else if((c & 0xC0) == 0x80) {
144 /* parser out of sync, ignore byte */
145 machine->state = utf8state_start;
146 } else if((c & 0xE0) == 0xC0) {
147 /* start of two byte sequence */
148 machine->s.byte[machine->len++] = c;
149 machine->state = utf8state_expect1;
150 machine->unicode = c & 0x1f;
151 } else if((c & 0xF0) == 0xE0) {
152 /* start of three byte sequence */
153 machine->s.byte[machine->len++] = c;
154 machine->state = utf8state_expect2;
155 machine->unicode = c & 0x0f;
156 } else if((c & 0xF8) == 0xF0) {
157 /* start of four byte sequence */
158 machine->s.byte[machine->len++] = c;
159 machine->state = utf8state_expect3;
160 machine->unicode = c & 0x07;
162 /* overlong encoding, reject */
163 machine->state = utf8state_reject;
166 case utf8state_expect3:
167 machine->s.byte[machine->len++] = c;
168 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
169 if((c & 0xC0) == 0x80) {
170 /* all good, continue */
171 machine->state = utf8state_expect2;
173 /* missing extra byte, reject */
174 machine->state = utf8state_reject;
177 case utf8state_expect2:
178 machine->s.byte[machine->len++] = c;
179 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
180 if((c & 0xC0) == 0x80) {
181 /* all good, continue */
182 machine->state = utf8state_expect1;
184 /* missing extra byte, reject */
185 machine->state = utf8state_reject;
188 case utf8state_expect1:
189 machine->s.byte[machine->len++] = c;
190 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
191 if((c & 0xC0) == 0x80) {
192 /* all good, accept */
193 machine->state = utf8state_accept;
195 /* missing extra byte, reject */
196 machine->state = utf8state_reject;
200 machine->state = utf8state_reject;
204 return machine->state;
208 get_unicode(union utf8_char utf8)
210 struct utf8_state_machine machine;
213 init_state_machine(&machine);
214 for (i = 0; i < 4; i++) {
215 utf8_next_char(&machine, utf8.byte[i]);
216 if (machine.state == utf8state_accept ||
217 machine.state == utf8state_reject)
221 if (machine.state == utf8state_reject)
224 return machine.unicode;
228 is_wide(union utf8_char utf8)
230 uint32_t unichar = get_unicode(utf8);
231 return wcwidth(unichar) > 1;
235 union utf8_char match;
236 union utf8_char replace;
238 /* Set last char_sub match to NULL char */
239 typedef struct char_sub *character_set;
241 struct char_sub CS_US[] = {
244 static struct char_sub CS_UK[] = {
245 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
248 static struct char_sub CS_SPECIAL[] = {
249 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond: ♦ */
250 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell: ▒ */
251 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT: ␉ */
252 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF: ␌ */
253 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR: ␍ */
254 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF: ␊ */
255 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree: ° */
256 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus: ± */
257 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL:  */
258 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT: ␋ */
259 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB: ┘ */
260 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT: ┐ */
261 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT: ┌ */
262 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_LB: └ */
263 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS: ┼ */
264 {{{'o', 0, }}, {{0xE2, 0x8E, 0xBA, 0}}}, /* Horiz. Scan Line 1: ⎺ */
265 {{{'p', 0, }}, {{0xE2, 0x8E, 0xBB, 0}}}, /* Horiz. Scan Line 3: ⎻ */
266 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* Horiz. Scan Line 5: ─ */
267 {{{'r', 0, }}, {{0xE2, 0x8E, 0xBC, 0}}}, /* Horiz. Scan Line 7: ⎼ */
268 {{{'s', 0, }}, {{0xE2, 0x8E, 0xBD, 0}}}, /* Horiz. Scan Line 9: ⎽ */
269 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR: ├ */
270 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL: ┤ */
271 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU: ┴ */
272 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD: ┬ */
273 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V: │ */
274 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE: ≤ */
275 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE: ≥ */
276 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI: π */
277 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ: ≠ */
278 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
279 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT: ⋅ */
284 apply_char_set(character_set cs, union utf8_char *utf8)
288 while (cs[i].match.byte[0]) {
289 if ((*utf8).ch == cs[i].match.ch) {
290 *utf8 = cs[i].replace;
303 /* Set last key_sub sym to NULL */
304 typedef struct key_map *keyboard_mode;
306 static struct key_map KM_NORMAL[] = {
307 { XKB_KEY_Left, 1, '[', 'D' },
308 { XKB_KEY_Right, 1, '[', 'C' },
309 { XKB_KEY_Up, 1, '[', 'A' },
310 { XKB_KEY_Down, 1, '[', 'B' },
311 { XKB_KEY_Home, 1, '[', 'H' },
312 { XKB_KEY_End, 1, '[', 'F' },
315 static struct key_map KM_APPLICATION[] = {
316 { XKB_KEY_Left, 1, 'O', 'D' },
317 { XKB_KEY_Right, 1, 'O', 'C' },
318 { XKB_KEY_Up, 1, 'O', 'A' },
319 { XKB_KEY_Down, 1, 'O', 'B' },
320 { XKB_KEY_Home, 1, 'O', 'H' },
321 { XKB_KEY_End, 1, 'O', 'F' },
322 { XKB_KEY_KP_Enter, 1, 'O', 'M' },
323 { XKB_KEY_KP_Multiply, 1, 'O', 'j' },
324 { XKB_KEY_KP_Add, 1, 'O', 'k' },
325 { XKB_KEY_KP_Separator, 1, 'O', 'l' },
326 { XKB_KEY_KP_Subtract, 1, 'O', 'm' },
327 { XKB_KEY_KP_Divide, 1, 'O', 'o' },
332 function_key_response(char escape, int num, uint32_t modifiers,
333 char code, char *response)
338 if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
339 if (modifiers & MOD_ALT_MASK) mod_num |= 2;
340 if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
343 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
344 num, mod_num + 1, code);
345 else if (code != '~')
346 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
349 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
352 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
356 /* returns the number of bytes written into response,
357 * which must have room for MAX_RESPONSE bytes */
359 apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
365 while (mode[i].sym) {
367 if (sym == map.sym) {
368 len = function_key_response(map.escape, map.num,
378 struct terminal_color { double r, g, b, a; };
380 unsigned char fg, bg;
381 char a; /* attributes format:
384 char s; /* in selection */
386 struct color_scheme {
387 struct terminal_color palette[16];
389 struct attr default_attr;
393 attr_init(struct attr *data_attr, struct attr attr, int n)
396 for (i = 0; i < n; i++) {
402 escape_state_normal = 0,
407 escape_state_inner_escape,
412 #define ESC_FLAG_WHAT 0x01
413 #define ESC_FLAG_GT 0x02
414 #define ESC_FLAG_BANG 0x04
415 #define ESC_FLAG_CASH 0x08
416 #define ESC_FLAG_SQUOTE 0x10
417 #define ESC_FLAG_DQUOTE 0x20
418 #define ESC_FLAG_SPACE 0x40
428 struct window *window;
429 struct widget *widget;
430 struct display *display;
432 union utf8_char *data;
435 struct attr *data_attr;
436 struct attr curr_attr;
439 char saved_origin_mode;
440 struct attr saved_attr;
441 union utf8_char last_char;
442 int margin_top, margin_bottom;
443 character_set cs, g0, g1;
444 character_set saved_cs, saved_g0, saved_g1;
445 keyboard_mode key_mode;
446 int data_pitch, attr_pitch; /* The width in bytes of a line */
447 int width, height, row, column, max_width;
448 uint32_t buffer_height;
449 uint32_t start, end, saved_start, log_size;
450 int saved_row, saved_column;
452 int send_cursor_position;
455 char escape[MAX_ESCAPE+1];
457 enum escape_state state;
458 enum escape_state outer_state;
460 struct utf8_state_machine state_machine;
462 struct color_scheme *color_scheme;
463 struct terminal_color color_table[256];
464 cairo_font_extents_t extents;
465 double average_width;
466 cairo_scaled_font_t *font_normal, *font_bold;
467 uint32_t hide_cursor_serial;
469 struct wl_data_source *selection;
471 int dragging, click_count;
472 int selection_start_x, selection_start_y;
473 int selection_end_x, selection_end_y;
474 int selection_start_row, selection_start_col;
475 int selection_end_row, selection_end_col;
479 /* Create default tab stops, every 8 characters */
481 terminal_init_tabs(struct terminal *terminal)
485 while (i < terminal->width) {
487 terminal->tab_ruler[i] = 1;
489 terminal->tab_ruler[i] = 0;
495 terminal_init(struct terminal *terminal)
497 terminal->curr_attr = terminal->color_scheme->default_attr;
498 terminal->origin_mode = 0;
499 terminal->mode = MODE_SHOW_CURSOR |
505 terminal->column = 0;
507 terminal->g0 = CS_US;
508 terminal->g1 = CS_US;
509 terminal->cs = terminal->g0;
510 terminal->key_mode = KM_NORMAL;
512 terminal->saved_g0 = terminal->g0;
513 terminal->saved_g1 = terminal->g1;
514 terminal->saved_cs = terminal->cs;
516 terminal->saved_attr = terminal->curr_attr;
517 terminal->saved_origin_mode = terminal->origin_mode;
518 terminal->saved_row = terminal->row;
519 terminal->saved_column = terminal->column;
521 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
525 init_color_table(struct terminal *terminal)
528 struct terminal_color *color_table = terminal->color_table;
530 for (c = 0; c < 256; c ++) {
532 color_table[c] = terminal->color_scheme->palette[c];
533 } else if (c < 232) {
535 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
536 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
537 color_table[c].r = ((double)(r % 6) / 6.0);
538 color_table[c].a = 1.0;
540 r = (c - 232) * 10 + 8;
541 color_table[c].r = ((double) r) / 256.0;
542 color_table[c].g = color_table[c].r;
543 color_table[c].b = color_table[c].r;
544 color_table[c].a = 1.0;
549 static union utf8_char *
550 terminal_get_row(struct terminal *terminal, int row)
554 index = (row + terminal->start) & (terminal->buffer_height - 1);
556 return (void *) terminal->data + index * terminal->data_pitch;
560 terminal_get_attr_row(struct terminal *terminal, int row)
564 index = (row + terminal->start) & (terminal->buffer_height - 1);
566 return (void *) terminal->data_attr + index * terminal->attr_pitch;
575 terminal_decode_attr(struct terminal *terminal, int row, int col,
576 union decoded_attr *decoded)
579 int foreground, background, tmp;
582 if (((row == terminal->selection_start_row &&
583 col >= terminal->selection_start_col) ||
584 row > terminal->selection_start_row) &&
585 ((row == terminal->selection_end_row &&
586 col < terminal->selection_end_col) ||
587 row < terminal->selection_end_row))
590 /* get the attributes for this character cell */
591 attr = terminal_get_attr_row(terminal, row)[col];
592 if ((attr.a & ATTRMASK_INVERSE) ||
594 ((terminal->mode & MODE_SHOW_CURSOR) &&
595 window_has_focus(terminal->window) && terminal->row == row &&
596 terminal->column == col)) {
597 foreground = attr.bg;
598 background = attr.fg;
599 if (attr.a & ATTRMASK_BOLD) {
600 if (foreground <= 16) foreground |= 0x08;
601 if (background <= 16) background &= 0x07;
604 foreground = attr.fg;
605 background = attr.bg;
608 if (terminal->mode & MODE_INVERSE) {
610 foreground = background;
612 if (attr.a & ATTRMASK_BOLD) {
613 if (foreground <= 16) foreground |= 0x08;
614 if (background <= 16) background &= 0x07;
618 decoded->attr.fg = foreground;
619 decoded->attr.bg = background;
620 decoded->attr.a = attr.a;
625 terminal_scroll_buffer(struct terminal *terminal, int d)
629 terminal->start += d;
632 for (i = 0; i < d; i++) {
633 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
634 attr_init(terminal_get_attr_row(terminal, i),
635 terminal->curr_attr, terminal->width);
638 for (i = terminal->height - d; i < terminal->height; i++) {
639 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
640 attr_init(terminal_get_attr_row(terminal, i),
641 terminal->curr_attr, terminal->width);
645 terminal->selection_start_row -= d;
646 terminal->selection_end_row -= d;
650 terminal_scroll_window(struct terminal *terminal, int d)
654 int from_row, to_row;
656 // scrolling range is inclusive
657 window_height = terminal->margin_bottom - terminal->margin_top + 1;
658 d = d % (window_height + 1);
661 to_row = terminal->margin_bottom;
662 from_row = terminal->margin_bottom - d;
664 for (i = 0; i < (window_height - d); i++) {
665 memcpy(terminal_get_row(terminal, to_row - i),
666 terminal_get_row(terminal, from_row - i),
667 terminal->data_pitch);
668 memcpy(terminal_get_attr_row(terminal, to_row - i),
669 terminal_get_attr_row(terminal, from_row - i),
670 terminal->attr_pitch);
672 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
673 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
674 attr_init(terminal_get_attr_row(terminal, i),
675 terminal->curr_attr, terminal->width);
678 to_row = terminal->margin_top;
679 from_row = terminal->margin_top + d;
681 for (i = 0; i < (window_height - d); i++) {
682 memcpy(terminal_get_row(terminal, to_row + i),
683 terminal_get_row(terminal, from_row + i),
684 terminal->data_pitch);
685 memcpy(terminal_get_attr_row(terminal, to_row + i),
686 terminal_get_attr_row(terminal, from_row + i),
687 terminal->attr_pitch);
689 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
690 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
691 attr_init(terminal_get_attr_row(terminal, i),
692 terminal->curr_attr, terminal->width);
698 terminal_scroll(struct terminal *terminal, int d)
700 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
701 terminal_scroll_buffer(terminal, d);
703 terminal_scroll_window(terminal, d);
707 terminal_shift_line(struct terminal *terminal, int d)
709 union utf8_char *row;
710 struct attr *attr_row;
712 row = terminal_get_row(terminal, terminal->row);
713 attr_row = terminal_get_attr_row(terminal, terminal->row);
715 if ((terminal->width + d) <= terminal->column)
716 d = terminal->column + 1 - terminal->width;
717 if ((terminal->column + d) >= terminal->width)
718 d = terminal->width - terminal->column - 1;
722 memmove(&row[terminal->column],
723 &row[terminal->column + d],
724 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
725 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
726 (terminal->width - terminal->column - d) * sizeof(struct attr));
727 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
728 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
730 memmove(&row[terminal->column + d], &row[terminal->column],
731 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
732 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
733 (terminal->width - terminal->column - d) * sizeof(struct attr));
734 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
735 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
740 terminal_resize_cells(struct terminal *terminal,
741 int width, int height)
743 union utf8_char *data;
744 struct attr *data_attr;
746 int data_pitch, attr_pitch;
747 int i, l, total_rows;
748 uint32_t d, uheight = height;
749 struct rectangle allocation;
752 if (uheight > terminal->buffer_height)
753 height = terminal->buffer_height;
755 if (terminal->width == width && terminal->height == height)
758 if (terminal->data && width <= terminal->max_width) {
760 if (height < terminal->height && height <= terminal->row)
761 d = terminal->height - height;
762 else if (height > terminal->height &&
763 terminal->height - 1 == terminal->row) {
764 d = terminal->height - height;
765 if (terminal->log_size < uheight)
766 d = -terminal->start;
769 terminal->start += d;
772 terminal->max_width = width;
773 data_pitch = width * sizeof(union utf8_char);
774 data = zalloc(data_pitch * terminal->buffer_height);
775 attr_pitch = width * sizeof(struct attr);
776 data_attr = malloc(attr_pitch * terminal->buffer_height);
777 tab_ruler = zalloc(width);
778 attr_init(data_attr, terminal->curr_attr,
779 width * terminal->buffer_height);
781 if (terminal->data && terminal->data_attr) {
782 if (width > terminal->width)
787 if (terminal->height > height) {
789 i = 1 + terminal->row - height;
791 terminal->start += i;
792 terminal->row = terminal->row - i;
795 total_rows = terminal->height;
798 for (i = 0; i < total_rows; i++) {
799 memcpy(&data[width * i],
800 terminal_get_row(terminal, i),
801 l * sizeof(union utf8_char));
802 memcpy(&data_attr[width * i],
803 terminal_get_attr_row(terminal, i),
804 l * sizeof(struct attr));
807 free(terminal->data);
808 free(terminal->data_attr);
809 free(terminal->tab_ruler);
812 terminal->data_pitch = data_pitch;
813 terminal->attr_pitch = attr_pitch;
814 terminal->data = data;
815 terminal->data_attr = data_attr;
816 terminal->tab_ruler = tab_ruler;
820 terminal->margin_bottom =
821 height - (terminal->height - terminal->margin_bottom);
822 terminal->width = width;
823 terminal->height = height;
824 terminal_init_tabs(terminal);
826 /* Update the window size */
827 ws.ws_row = terminal->height;
828 ws.ws_col = terminal->width;
829 widget_get_allocation(terminal->widget, &allocation);
830 ws.ws_xpixel = allocation.width;
831 ws.ws_ypixel = allocation.height;
832 ioctl(terminal->master, TIOCSWINSZ, &ws);
836 resize_handler(struct widget *widget,
837 int32_t width, int32_t height, void *data)
839 struct terminal *terminal = data;
840 int32_t columns, rows, m;
842 m = 2 * terminal->margin;
843 columns = (width - m) / (int32_t) terminal->average_width;
844 rows = (height - m) / (int32_t) terminal->extents.height;
846 if (!window_is_fullscreen(terminal->window) &&
847 !window_is_maximized(terminal->window)) {
848 width = columns * terminal->average_width + m;
849 height = rows * terminal->extents.height + m;
850 widget_set_size(terminal->widget, width, height);
851 asprintf(&p, "%s — [%dx%d]", terminal->title, columns, rows);
852 window_set_title(terminal->window, p);
856 terminal_resize_cells(terminal, columns, rows);
860 terminal_resize(struct terminal *terminal, int columns, int rows)
862 int32_t width, height, m;
864 if (window_is_fullscreen(terminal->window) ||
865 window_is_maximized(terminal->window))
868 m = 2 * terminal->margin;
869 width = columns * terminal->average_width + m;
870 height = rows * terminal->extents.height + m;
872 window_frame_set_child_size(terminal->widget, width, height);
875 struct color_scheme DEFAULT_COLORS = {
877 {0, 0, 0, 1}, /* black */
878 {0.66, 0, 0, 1}, /* red */
879 {0 , 0.66, 0, 1}, /* green */
880 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
881 {0 , 0 , 0.66, 1}, /* blue */
882 {0.66, 0 , 0.66, 1}, /* magenta */
883 {0, 0.66, 0.66, 1}, /* cyan */
884 {0.66, 0.66, 0.66, 1}, /* light grey */
885 {0.22, 0.33, 0.33, 1}, /* dark grey */
886 {1, 0.33, 0.33, 1}, /* high red */
887 {0.33, 1, 0.33, 1}, /* high green */
888 {1, 1, 0.33, 1}, /* high yellow */
889 {0.33, 0.33, 1, 1}, /* high blue */
890 {1, 0.33, 1, 1}, /* high magenta */
891 {0.33, 1, 1, 1}, /* high cyan */
892 {1, 1, 1, 1} /* white */
894 0, /* black border */
895 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
899 terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
901 cairo_set_source_rgba(cr,
902 terminal->color_table[index].r,
903 terminal->color_table[index].g,
904 terminal->color_table[index].b,
905 terminal->color_table[index].a);
909 terminal_send_selection(struct terminal *terminal, int fd)
912 union utf8_char *p_row;
913 union decoded_attr attr;
917 fp = fdopen(fd, "w");
922 for (row = 0; row < terminal->height; row++) {
923 p_row = terminal_get_row(terminal, row);
924 for (col = 0; col < terminal->width; col++) {
925 if (p_row[col].ch == 0x200B) /* space glyph */
927 /* get the attributes for this character cell */
928 terminal_decode_attr(terminal, row, col, &attr);
931 len = strnlen((char *) p_row[col].byte, 4);
933 fwrite(p_row[col].byte, 1, len, fp);
934 if (len == 0 || col == terminal->width - 1) {
935 fwrite("\n", 1, 1, fp);
944 struct terminal *terminal;
947 union decoded_attr attr;
948 cairo_glyph_t glyphs[256], *g;
952 glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
954 run->terminal = terminal;
956 run->g = run->glyphs;
962 glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
964 cairo_scaled_font_t *font;
966 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
967 (attr.key != run->attr.key)) {
968 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
969 font = run->terminal->font_bold;
971 font = run->terminal->font_normal;
972 cairo_set_scaled_font(run->cr, font);
973 terminal_set_color(run->terminal, run->cr,
976 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
977 cairo_show_glyphs (run->cr, run->glyphs, run->count);
978 run->g = run->glyphs;
985 glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
988 cairo_scaled_font_t *font;
990 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
992 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
993 font = run->terminal->font_bold;
995 font = run->terminal->font_normal;
997 cairo_move_to(run->cr, x, y);
998 cairo_scaled_font_text_to_glyphs (font, x, y,
1000 &run->g, &num_glyphs,
1002 run->g += num_glyphs;
1003 run->count += num_glyphs;
1008 redraw_handler(struct widget *widget, void *data)
1010 struct terminal *terminal = data;
1011 struct rectangle allocation;
1013 int top_margin, side_margin;
1014 int row, col, cursor_x, cursor_y;
1015 union utf8_char *p_row;
1016 union decoded_attr attr;
1018 cairo_surface_t *surface;
1020 struct glyph_run run;
1021 cairo_font_extents_t extents;
1022 double average_width;
1023 double unichar_width;
1025 surface = window_get_surface(terminal->window);
1026 widget_get_allocation(terminal->widget, &allocation);
1027 cr = widget_cairo_create(terminal->widget);
1028 cairo_rectangle(cr, allocation.x, allocation.y,
1029 allocation.width, allocation.height);
1031 cairo_push_group(cr);
1033 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1034 terminal_set_color(terminal, cr, terminal->color_scheme->border);
1037 cairo_set_scaled_font(cr, terminal->font_normal);
1039 extents = terminal->extents;
1040 average_width = terminal->average_width;
1041 side_margin = (allocation.width - terminal->width * average_width) / 2;
1042 top_margin = (allocation.height - terminal->height * extents.height) / 2;
1044 cairo_set_line_width(cr, 1.0);
1045 cairo_translate(cr, allocation.x + side_margin,
1046 allocation.y + top_margin);
1047 /* paint the background */
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 if (attr.attr.bg == terminal->color_scheme->border)
1057 if (is_wide(p_row[col]))
1058 unichar_width = 2 * average_width;
1060 unichar_width = average_width;
1062 terminal_set_color(terminal, cr, attr.attr.bg);
1063 cairo_move_to(cr, col * average_width,
1064 row * extents.height);
1065 cairo_rel_line_to(cr, unichar_width, 0);
1066 cairo_rel_line_to(cr, 0, extents.height);
1067 cairo_rel_line_to(cr, -unichar_width, 0);
1068 cairo_close_path(cr);
1073 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1075 /* paint the foreground */
1076 glyph_run_init(&run, terminal, cr);
1077 for (row = 0; row < terminal->height; row++) {
1078 p_row = terminal_get_row(terminal, row);
1079 for (col = 0; col < terminal->width; col++) {
1080 /* get the attributes for this character cell */
1081 terminal_decode_attr(terminal, row, col, &attr);
1083 glyph_run_flush(&run, attr);
1085 text_x = col * average_width;
1086 text_y = extents.ascent + row * extents.height;
1087 if (attr.attr.a & ATTRMASK_UNDERLINE) {
1088 terminal_set_color(terminal, cr, attr.attr.fg);
1089 cairo_move_to(cr, text_x, (double)text_y + 1.5);
1090 cairo_line_to(cr, text_x + average_width, (double) text_y + 1.5);
1094 glyph_run_add(&run, text_x, text_y, &p_row[col]);
1099 glyph_run_flush(&run, attr);
1101 if ((terminal->mode & MODE_SHOW_CURSOR) &&
1102 !window_has_focus(terminal->window)) {
1105 cairo_set_line_width(cr, 1);
1106 cairo_move_to(cr, terminal->column * average_width + d,
1107 terminal->row * extents.height + d);
1108 cairo_rel_line_to(cr, average_width - 2 * d, 0);
1109 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
1110 cairo_rel_line_to(cr, -average_width + 2 * d, 0);
1111 cairo_close_path(cr);
1116 cairo_pop_group_to_source(cr);
1119 cairo_surface_destroy(surface);
1121 if (terminal->send_cursor_position) {
1122 cursor_x = side_margin + allocation.x +
1123 terminal->column * average_width;
1124 cursor_y = top_margin + allocation.y +
1125 terminal->row * extents.height;
1126 window_set_text_cursor_position(terminal->window,
1127 cursor_x, cursor_y);
1128 terminal->send_cursor_position = 0;
1133 terminal_write(struct terminal *terminal, const char *data, size_t length)
1135 if (write(terminal->master, data, length) < 0)
1137 terminal->send_cursor_position = 1;
1141 terminal_data(struct terminal *terminal, const char *data, size_t length);
1144 handle_char(struct terminal *terminal, union utf8_char utf8);
1147 handle_sgr(struct terminal *terminal, int code);
1150 handle_term_parameter(struct terminal *terminal, int code, int sr)
1154 if (terminal->escape_flags & ESC_FLAG_WHAT) {
1156 case 1: /* DECCKM */
1157 if (sr) terminal->key_mode = KM_APPLICATION;
1158 else terminal->key_mode = KM_NORMAL;
1160 case 2: /* DECANM */
1161 /* No VT52 support yet */
1162 terminal->g0 = CS_US;
1163 terminal->g1 = CS_US;
1164 terminal->cs = terminal->g0;
1166 case 3: /* DECCOLM */
1168 terminal_resize(terminal, 132, 24);
1170 terminal_resize(terminal, 80, 24);
1172 /* set columns, but also home cursor and clear screen */
1173 terminal->row = 0; terminal->column = 0;
1174 for (i = 0; i < terminal->height; i++) {
1175 memset(terminal_get_row(terminal, i),
1176 0, terminal->data_pitch);
1177 attr_init(terminal_get_attr_row(terminal, i),
1178 terminal->curr_attr, terminal->width);
1181 case 5: /* DECSCNM */
1182 if (sr) terminal->mode |= MODE_INVERSE;
1183 else terminal->mode &= ~MODE_INVERSE;
1186 terminal->origin_mode = sr;
1187 if (terminal->origin_mode)
1188 terminal->row = terminal->margin_top;
1191 terminal->column = 0;
1193 case 7: /* DECAWM */
1194 if (sr) terminal->mode |= MODE_AUTOWRAP;
1195 else terminal->mode &= ~MODE_AUTOWRAP;
1197 case 8: /* DECARM */
1198 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1199 else terminal->mode &= ~MODE_AUTOREPEAT;
1201 case 12: /* Very visible cursor (CVVIS) */
1202 /* FIXME: What do we do here. */
1205 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1206 else terminal->mode &= ~MODE_SHOW_CURSOR;
1208 case 1034: /* smm/rmm, meta mode on/off */
1211 case 1037: /* deleteSendsDel */
1212 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1213 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1215 case 1039: /* altSendsEscape */
1216 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1217 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1219 case 1049: /* rmcup/smcup, alternate screen */
1220 /* Ignore. Should be possible to implement,
1221 * but it's kind of annoying. */
1224 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1230 if (sr) terminal->mode |= MODE_IRM;
1231 else terminal->mode &= ~MODE_IRM;
1234 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1235 else terminal->mode &= ~MODE_LF_NEWLINE;
1238 fprintf(stderr, "Unknown parameter: %d\n", code);
1245 handle_dcs(struct terminal *terminal)
1250 handle_osc(struct terminal *terminal)
1255 terminal->escape[terminal->escape_length++] = '\0';
1256 p = &terminal->escape[2];
1257 code = strtol(p, &p, 10);
1261 case 0: /* Icon name and window title */
1262 case 1: /* Icon label */
1263 case 2: /* Window title*/
1264 free(terminal->title);
1265 terminal->title = strdup(p);
1266 window_set_title(terminal->window, p);
1268 case 7: /* shell cwd as uri */
1271 fprintf(stderr, "Unknown OSC escape code %d, text %s\n",
1278 handle_escape(struct terminal *terminal)
1280 union utf8_char *row;
1281 struct attr *attr_row;
1283 int i, count, x, y, top, bottom;
1284 int args[10], set[10] = { 0, };
1285 char response[MAX_RESPONSE] = {0, };
1286 struct rectangle allocation;
1288 terminal->escape[terminal->escape_length++] = '\0';
1290 p = &terminal->escape[2];
1291 while ((isdigit(*p) || *p == ';') && i < 10) {
1300 args[i] = strtol(p, &p, 10);
1307 count = set[0] ? args[0] : 1;
1308 if (count == 0) count = 1;
1309 terminal_shift_line(terminal, count);
1312 count = set[0] ? args[0] : 1;
1313 if (count == 0) count = 1;
1314 if (terminal->row - count >= terminal->margin_top)
1315 terminal->row -= count;
1317 terminal->row = terminal->margin_top;
1320 count = set[0] ? args[0] : 1;
1321 if (count == 0) count = 1;
1322 if (terminal->row + count <= terminal->margin_bottom)
1323 terminal->row += count;
1325 terminal->row = terminal->margin_bottom;
1328 count = set[0] ? args[0] : 1;
1329 if (count == 0) count = 1;
1330 if ((terminal->column + count) < terminal->width)
1331 terminal->column += count;
1333 terminal->column = terminal->width - 1;
1336 count = set[0] ? args[0] : 1;
1337 if (count == 0) count = 1;
1338 if ((terminal->column - count) >= 0)
1339 terminal->column -= count;
1341 terminal->column = 0;
1344 count = set[0] ? args[0] : 1;
1345 if (terminal->row + count <= terminal->margin_bottom)
1346 terminal->row += count;
1348 terminal->row = terminal->margin_bottom;
1349 terminal->column = 0;
1352 count = set[0] ? args[0] : 1;
1353 if (terminal->row - count >= terminal->margin_top)
1354 terminal->row -= count;
1356 terminal->row = terminal->margin_top;
1357 terminal->column = 0;
1360 y = set[0] ? args[0] : 1;
1361 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1363 terminal->column = y - 1;
1367 x = (set[1] ? args[1] : 1) - 1;
1369 (x >= terminal->width ? terminal->width - 1 : x);
1371 y = (set[0] ? args[0] : 1) - 1;
1372 if (terminal->origin_mode) {
1373 y += terminal->margin_top;
1374 y = y < terminal->margin_top ? terminal->margin_top :
1375 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1378 (y >= terminal->height ? terminal->height - 1 : y);
1382 terminal->column = x;
1385 count = set[0] ? args[0] : 1;
1386 if (count == 0) count = 1;
1387 while (count > 0 && terminal->column < terminal->width) {
1388 if (terminal->tab_ruler[terminal->column]) count--;
1394 row = terminal_get_row(terminal, terminal->row);
1395 attr_row = terminal_get_attr_row(terminal, terminal->row);
1396 if (!set[0] || args[0] == 0 || args[0] > 2) {
1397 memset(&row[terminal->column],
1398 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1399 attr_init(&attr_row[terminal->column],
1400 terminal->curr_attr, terminal->width - terminal->column);
1401 for (i = terminal->row + 1; i < terminal->height; i++) {
1402 memset(terminal_get_row(terminal, i),
1403 0, terminal->data_pitch);
1404 attr_init(terminal_get_attr_row(terminal, i),
1405 terminal->curr_attr, terminal->width);
1407 } else if (args[0] == 1) {
1408 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1409 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1410 for (i = 0; i < terminal->row; i++) {
1411 memset(terminal_get_row(terminal, i),
1412 0, terminal->data_pitch);
1413 attr_init(terminal_get_attr_row(terminal, i),
1414 terminal->curr_attr, terminal->width);
1416 } else if (args[0] == 2) {
1417 /* Clear screen by scrolling contents out */
1418 terminal_scroll_buffer(terminal,
1419 terminal->end - terminal->start);
1423 row = terminal_get_row(terminal, terminal->row);
1424 attr_row = terminal_get_attr_row(terminal, terminal->row);
1425 if (!set[0] || args[0] == 0 || args[0] > 2) {
1426 memset(&row[terminal->column], 0,
1427 (terminal->width - terminal->column) * sizeof(union utf8_char));
1428 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1429 terminal->width - terminal->column);
1430 } else if (args[0] == 1) {
1431 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1432 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1433 } else if (args[0] == 2) {
1434 memset(row, 0, terminal->data_pitch);
1435 attr_init(attr_row, terminal->curr_attr, terminal->width);
1439 count = set[0] ? args[0] : 1;
1440 if (count == 0) count = 1;
1441 if (terminal->row >= terminal->margin_top &&
1442 terminal->row < terminal->margin_bottom)
1444 top = terminal->margin_top;
1445 terminal->margin_top = terminal->row;
1446 terminal_scroll(terminal, 0 - count);
1447 terminal->margin_top = top;
1448 } else if (terminal->row == terminal->margin_bottom) {
1449 memset(terminal_get_row(terminal, terminal->row),
1450 0, terminal->data_pitch);
1451 attr_init(terminal_get_attr_row(terminal, terminal->row),
1452 terminal->curr_attr, terminal->width);
1456 count = set[0] ? args[0] : 1;
1457 if (count == 0) count = 1;
1458 if (terminal->row >= terminal->margin_top &&
1459 terminal->row < terminal->margin_bottom)
1461 top = terminal->margin_top;
1462 terminal->margin_top = terminal->row;
1463 terminal_scroll(terminal, count);
1464 terminal->margin_top = top;
1465 } else if (terminal->row == terminal->margin_bottom) {
1466 memset(terminal_get_row(terminal, terminal->row),
1467 0, terminal->data_pitch);
1471 count = set[0] ? args[0] : 1;
1472 if (count == 0) count = 1;
1473 terminal_shift_line(terminal, 0 - count);
1476 terminal_scroll(terminal, set[0] ? args[0] : 1);
1479 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1482 count = set[0] ? args[0] : 1;
1483 if (count == 0) count = 1;
1484 if ((terminal->column + count) > terminal->width)
1485 count = terminal->width - terminal->column;
1486 row = terminal_get_row(terminal, terminal->row);
1487 attr_row = terminal_get_attr_row(terminal, terminal->row);
1488 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1489 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1492 count = set[0] ? args[0] : 1;
1493 if (count == 0) count = 1;
1494 while (count > 0 && terminal->column >= 0) {
1495 if (terminal->tab_ruler[terminal->column]) count--;
1501 y = set[0] ? args[0] : 1;
1502 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1504 terminal->column = y - 1;
1507 count = set[0] ? args[0] : 1;
1508 if (count == 0) count = 1;
1509 if (terminal->last_char.byte[0])
1510 for (i = 0; i < count; i++)
1511 handle_char(terminal, terminal->last_char);
1512 terminal->last_char.byte[0] = 0;
1514 case 'c': /* Primary DA */
1515 terminal_write(terminal, "\e[?6c", 5);
1518 x = set[0] ? args[0] : 1;
1519 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1521 terminal->row = x - 1;
1524 if (!set[0] || args[0] == 0) {
1525 terminal->tab_ruler[terminal->column] = 0;
1526 } else if (args[0] == 3) {
1527 memset(terminal->tab_ruler, 0, terminal->width);
1531 for(i = 0; i < 10 && set[i]; i++) {
1532 handle_term_parameter(terminal, args[i], 1);
1536 for(i = 0; i < 10 && set[i]; i++) {
1537 handle_term_parameter(terminal, args[i], 0);
1541 for(i = 0; i < 10; i++) {
1542 if (i <= 7 && set[i] && set[i + 1] &&
1543 set[i + 2] && args[i + 1] == 5)
1545 if (args[i] == 38) {
1546 handle_sgr(terminal, args[i + 2] + 256);
1548 } else if (args[i] == 48) {
1549 handle_sgr(terminal, args[i + 2] + 512);
1554 handle_sgr(terminal, args[i]);
1556 handle_sgr(terminal, 0);
1564 i = set[0] ? args[0] : 0;
1565 if (i == 0 || i == 5) {
1566 terminal_write(terminal, "\e[0n", 4);
1567 } else if (i == 6) {
1568 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1569 terminal->origin_mode ?
1570 terminal->row+terminal->margin_top : terminal->row+1,
1571 terminal->column+1);
1572 terminal_write(terminal, response, strlen(response));
1577 terminal->margin_top = 0;
1578 terminal->margin_bottom = terminal->height-1;
1580 terminal->column = 0;
1582 top = (set[0] ? args[0] : 1) - 1;
1584 (top >= terminal->height ? terminal->height - 1 : top);
1585 bottom = (set[1] ? args[1] : 1) - 1;
1586 bottom = bottom < 0 ? 0 :
1587 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1589 terminal->margin_top = top;
1590 terminal->margin_bottom = bottom;
1592 terminal->margin_top = 0;
1593 terminal->margin_bottom = terminal->height-1;
1595 if(terminal->origin_mode)
1596 terminal->row = terminal->margin_top;
1599 terminal->column = 0;
1603 terminal->saved_row = terminal->row;
1604 terminal->saved_column = terminal->column;
1606 case 't': /* windowOps */
1609 case 4: /* resize px */
1610 if (set[1] && set[2]) {
1611 widget_schedule_resize(terminal->widget,
1615 case 8: /* resize ch */
1616 if (set[1] && set[2]) {
1617 terminal_resize(terminal, args[2], args[1]);
1620 case 13: /* report position */
1621 widget_get_allocation(terminal->widget, &allocation);
1622 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1623 allocation.x, allocation.y);
1624 terminal_write(terminal, response, strlen(response));
1626 case 14: /* report px */
1627 widget_get_allocation(terminal->widget, &allocation);
1628 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1629 allocation.height, allocation.width);
1630 terminal_write(terminal, response, strlen(response));
1632 case 18: /* report ch */
1633 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1634 terminal->height, terminal->width);
1635 terminal_write(terminal, response, strlen(response));
1637 case 21: /* report title */
1638 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1639 window_get_title(terminal->window));
1640 terminal_write(terminal, response, strlen(response));
1644 terminal_resize(terminal, terminal->width, args[0]);
1646 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1650 terminal->row = terminal->saved_row;
1651 terminal->column = terminal->saved_column;
1654 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
1660 handle_non_csi_escape(struct terminal *terminal, char code)
1665 if(terminal->row < terminal->margin_top) {
1666 terminal->row = terminal->margin_top;
1667 terminal_scroll(terminal, -1);
1671 terminal->column = 0;
1675 if(terminal->row > terminal->margin_bottom) {
1676 terminal->row = terminal->margin_bottom;
1677 terminal_scroll(terminal, +1);
1681 terminal_init(terminal);
1684 terminal->tab_ruler[terminal->column] = 1;
1686 case '7': /* DECSC */
1687 terminal->saved_row = terminal->row;
1688 terminal->saved_column = terminal->column;
1689 terminal->saved_attr = terminal->curr_attr;
1690 terminal->saved_origin_mode = terminal->origin_mode;
1691 terminal->saved_cs = terminal->cs;
1692 terminal->saved_g0 = terminal->g0;
1693 terminal->saved_g1 = terminal->g1;
1695 case '8': /* DECRC */
1696 terminal->row = terminal->saved_row;
1697 terminal->column = terminal->saved_column;
1698 terminal->curr_attr = terminal->saved_attr;
1699 terminal->origin_mode = terminal->saved_origin_mode;
1700 terminal->cs = terminal->saved_cs;
1701 terminal->g0 = terminal->saved_g0;
1702 terminal->g1 = terminal->saved_g1;
1704 case '=': /* DECPAM */
1705 terminal->key_mode = KM_APPLICATION;
1707 case '>': /* DECPNM */
1708 terminal->key_mode = KM_NORMAL;
1711 fprintf(stderr, "Unknown escape code: %c\n", code);
1717 handle_special_escape(struct terminal *terminal, char special, char code)
1721 if (special == '#') {
1724 /* fill with 'E', no cheap way to do this */
1725 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1726 numChars = terminal->width * terminal->height;
1727 for(i = 0; i < numChars; i++) {
1728 terminal->data[i].byte[0] = 'E';
1732 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1735 } else if (special == '(' || special == ')') {
1739 terminal->g0 = CS_SPECIAL;
1741 terminal->g1 = CS_SPECIAL;
1745 terminal->g0 = CS_UK;
1747 terminal->g1 = CS_UK;
1751 terminal->g0 = CS_US;
1753 terminal->g1 = CS_US;
1756 fprintf(stderr, "Unknown character set %c\n", code);
1760 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1765 handle_sgr(struct terminal *terminal, int code)
1769 terminal->curr_attr = terminal->color_scheme->default_attr;
1772 terminal->curr_attr.a |= ATTRMASK_BOLD;
1773 if (terminal->curr_attr.fg < 8)
1774 terminal->curr_attr.fg += 8;
1777 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1780 terminal->curr_attr.a |= ATTRMASK_BLINK;
1783 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1788 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1789 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1790 terminal->curr_attr.fg -= 8;
1793 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1796 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1800 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1803 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1806 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1809 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1812 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1815 if(code >= 30 && code <= 37) {
1816 terminal->curr_attr.fg = code - 30;
1817 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1818 terminal->curr_attr.fg += 8;
1819 } else if(code >= 40 && code <= 47) {
1820 terminal->curr_attr.bg = code - 40;
1821 } else if (code >= 90 && code <= 97) {
1822 terminal->curr_attr.fg = code - 90 + 8;
1823 } else if (code >= 100 && code <= 107) {
1824 terminal->curr_attr.bg = code - 100 + 8;
1825 } else if(code >= 256 && code < 512) {
1826 terminal->curr_attr.fg = code - 256;
1827 } else if(code >= 512 && code < 768) {
1828 terminal->curr_attr.bg = code - 512;
1830 fprintf(stderr, "Unknown SGR code: %d\n", code);
1836 /* Returns 1 if c was special, otherwise 0 */
1838 handle_special_char(struct terminal *terminal, char c)
1840 union utf8_char *row;
1841 struct attr *attr_row;
1843 row = terminal_get_row(terminal, terminal->row);
1844 attr_row = terminal_get_attr_row(terminal, terminal->row);
1848 terminal->column = 0;
1851 if (terminal->mode & MODE_LF_NEWLINE) {
1852 terminal->column = 0;
1858 if (terminal->row > terminal->margin_bottom) {
1859 terminal->row = terminal->margin_bottom;
1860 terminal_scroll(terminal, +1);
1865 while (terminal->column < terminal->width) {
1866 if (terminal->mode & MODE_IRM)
1867 terminal_shift_line(terminal, +1);
1869 if (row[terminal->column].byte[0] == '\0') {
1870 row[terminal->column].byte[0] = ' ';
1871 row[terminal->column].byte[1] = '\0';
1872 attr_row[terminal->column] = terminal->curr_attr;
1876 if (terminal->tab_ruler[terminal->column]) break;
1878 if (terminal->column >= terminal->width) {
1879 terminal->column = terminal->width - 1;
1884 if (terminal->column >= terminal->width) {
1885 terminal->column = terminal->width - 2;
1886 } else if (terminal->column > 0) {
1888 } else if (terminal->mode & MODE_AUTOWRAP) {
1889 terminal->column = terminal->width - 1;
1891 if (terminal->row < terminal->margin_top) {
1892 terminal->row = terminal->margin_top;
1893 terminal_scroll(terminal, -1);
1901 case '\x0E': /* SO */
1902 terminal->cs = terminal->g1;
1904 case '\x0F': /* SI */
1905 terminal->cs = terminal->g0;
1917 handle_char(struct terminal *terminal, union utf8_char utf8)
1919 union utf8_char *row;
1920 struct attr *attr_row;
1922 if (handle_special_char(terminal, utf8.byte[0])) return;
1924 apply_char_set(terminal->cs, &utf8);
1926 /* There are a whole lot of non-characters, control codes,
1927 * and formatting codes that should probably be ignored,
1929 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1934 /* Some of these non-characters should be translated, e.g.: */
1935 if (utf8.byte[0] < 32) {
1936 utf8.byte[0] = utf8.byte[0] + 64;
1939 /* handle right margin effects */
1940 if (terminal->column >= terminal->width) {
1941 if (terminal->mode & MODE_AUTOWRAP) {
1942 terminal->column = 0;
1944 if (terminal->row > terminal->margin_bottom) {
1945 terminal->row = terminal->margin_bottom;
1946 terminal_scroll(terminal, +1);
1953 row = terminal_get_row(terminal, terminal->row);
1954 attr_row = terminal_get_attr_row(terminal, terminal->row);
1956 if (terminal->mode & MODE_IRM)
1957 terminal_shift_line(terminal, +1);
1958 row[terminal->column] = utf8;
1959 attr_row[terminal->column++] = terminal->curr_attr;
1961 if (terminal->row + terminal->start + 1 > terminal->end)
1962 terminal->end = terminal->row + terminal->start + 1;
1963 if (terminal->end == terminal->buffer_height)
1964 terminal->log_size = terminal->buffer_height;
1965 else if (terminal->log_size < terminal->buffer_height)
1966 terminal->log_size = terminal->end;
1968 /* cursor jump for wide character. */
1970 row[terminal->column++].ch = 0x200B; /* space glyph */
1972 if (utf8.ch != terminal->last_char.ch)
1973 terminal->last_char = utf8;
1977 escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1981 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1982 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1983 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1984 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1985 else len = 1; /* Invalid, cannot happen */
1987 if (terminal->escape_length + len <= MAX_ESCAPE) {
1988 for (i = 0; i < len; i++)
1989 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1990 terminal->escape_length += len;
1991 } else if (terminal->escape_length < MAX_ESCAPE) {
1992 terminal->escape[terminal->escape_length++] = 0;
1997 terminal_data(struct terminal *terminal, const char *data, size_t length)
2000 union utf8_char utf8;
2001 enum utf8_state parser_state;
2003 for (i = 0; i < length; i++) {
2005 utf8_next_char(&terminal->state_machine, data[i]);
2006 switch(parser_state) {
2007 case utf8state_accept:
2008 utf8.ch = terminal->state_machine.s.ch;
2010 case utf8state_reject:
2011 /* the unicode replacement character */
2012 utf8.byte[0] = 0xEF;
2013 utf8.byte[1] = 0xBF;
2014 utf8.byte[2] = 0xBD;
2015 utf8.byte[3] = 0x00;
2021 /* assume escape codes never use non-ASCII characters */
2022 switch (terminal->state) {
2023 case escape_state_escape:
2024 escape_append_utf8(terminal, utf8);
2025 switch (utf8.byte[0]) {
2027 terminal->state = escape_state_dcs;
2030 terminal->state = escape_state_csi;
2033 terminal->state = escape_state_osc;
2037 case ')': /* special */
2038 terminal->state = escape_state_special;
2040 case '^': /* PM (not implemented) */
2041 case '_': /* APC (not implemented) */
2042 terminal->state = escape_state_ignore;
2045 terminal->state = escape_state_normal;
2046 handle_non_csi_escape(terminal, utf8.byte[0]);
2050 case escape_state_csi:
2051 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
2053 } else if (utf8.byte[0] == '?') {
2054 terminal->escape_flags |= ESC_FLAG_WHAT;
2055 } else if (utf8.byte[0] == '>') {
2056 terminal->escape_flags |= ESC_FLAG_GT;
2057 } else if (utf8.byte[0] == '!') {
2058 terminal->escape_flags |= ESC_FLAG_BANG;
2059 } else if (utf8.byte[0] == '$') {
2060 terminal->escape_flags |= ESC_FLAG_CASH;
2061 } else if (utf8.byte[0] == '\'') {
2062 terminal->escape_flags |= ESC_FLAG_SQUOTE;
2063 } else if (utf8.byte[0] == '"') {
2064 terminal->escape_flags |= ESC_FLAG_DQUOTE;
2065 } else if (utf8.byte[0] == ' ') {
2066 terminal->escape_flags |= ESC_FLAG_SPACE;
2068 escape_append_utf8(terminal, utf8);
2069 if (terminal->escape_length >= MAX_ESCAPE)
2070 terminal->state = escape_state_normal;
2073 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
2074 utf8.byte[0] == '`')
2076 terminal->state = escape_state_normal;
2077 handle_escape(terminal);
2081 case escape_state_inner_escape:
2082 if (utf8.byte[0] == '\\') {
2083 terminal->state = escape_state_normal;
2084 if (terminal->outer_state == escape_state_dcs) {
2085 handle_dcs(terminal);
2086 } else if (terminal->outer_state == escape_state_osc) {
2087 handle_osc(terminal);
2089 } else if (utf8.byte[0] == '\e') {
2090 terminal->state = terminal->outer_state;
2091 escape_append_utf8(terminal, utf8);
2092 if (terminal->escape_length >= MAX_ESCAPE)
2093 terminal->state = escape_state_normal;
2095 terminal->state = terminal->outer_state;
2096 if (terminal->escape_length < MAX_ESCAPE)
2097 terminal->escape[terminal->escape_length++] = '\e';
2098 escape_append_utf8(terminal, utf8);
2099 if (terminal->escape_length >= MAX_ESCAPE)
2100 terminal->state = escape_state_normal;
2103 case escape_state_dcs:
2104 case escape_state_osc:
2105 case escape_state_ignore:
2106 if (utf8.byte[0] == '\e') {
2107 terminal->outer_state = terminal->state;
2108 terminal->state = escape_state_inner_escape;
2109 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
2110 terminal->state = escape_state_normal;
2111 handle_osc(terminal);
2113 escape_append_utf8(terminal, utf8);
2114 if (terminal->escape_length >= MAX_ESCAPE)
2115 terminal->state = escape_state_normal;
2118 case escape_state_special:
2119 escape_append_utf8(terminal, utf8);
2120 terminal->state = escape_state_normal;
2121 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2122 handle_special_escape(terminal, terminal->escape[1],
2130 /* this is valid, because ASCII characters are never used to
2131 * introduce a multibyte sequence in UTF-8 */
2132 if (utf8.byte[0] == '\e') {
2133 terminal->state = escape_state_escape;
2134 terminal->outer_state = escape_state_normal;
2135 terminal->escape[0] = '\e';
2136 terminal->escape_length = 1;
2137 terminal->escape_flags = 0;
2139 handle_char(terminal, utf8);
2143 window_schedule_redraw(terminal->window);
2147 data_source_target(void *data,
2148 struct wl_data_source *source, const char *mime_type)
2150 fprintf(stderr, "data_source_target, %s\n", mime_type);
2154 data_source_send(void *data,
2155 struct wl_data_source *source,
2156 const char *mime_type, int32_t fd)
2158 struct terminal *terminal = data;
2160 terminal_send_selection(terminal, fd);
2164 data_source_cancelled(void *data, struct wl_data_source *source)
2166 wl_data_source_destroy(source);
2169 static const struct wl_data_source_listener data_source_listener = {
2172 data_source_cancelled
2175 static const char text_mime_type[] = "text/plain;charset=utf-8";
2178 data_handler(struct window *window,
2179 struct input *input,
2180 float x, float y, const char **types, void *data)
2182 int i, has_text = 0;
2186 for (i = 0; types[i]; i++)
2187 if (strcmp(types[i], text_mime_type) == 0)
2191 input_accept(input, NULL);
2193 input_accept(input, text_mime_type);
2198 drop_handler(struct window *window, struct input *input,
2199 int32_t x, int32_t y, void *data)
2201 struct terminal *terminal = data;
2203 input_receive_drag_data_to_fd(input, text_mime_type, terminal->master);
2207 fullscreen_handler(struct window *window, void *data)
2209 struct terminal *terminal = data;
2211 window_set_fullscreen(window, !window_is_fullscreen(terminal->window));
2215 close_handler(void *data)
2217 struct terminal *terminal = data;
2219 terminal_destroy(terminal);
2223 terminal_copy(struct terminal *terminal, struct input *input)
2225 terminal->selection =
2226 display_create_data_source(terminal->display);
2227 wl_data_source_offer(terminal->selection,
2228 "text/plain;charset=utf-8");
2229 wl_data_source_add_listener(terminal->selection,
2230 &data_source_listener, terminal);
2231 input_set_selection(input, terminal->selection,
2232 display_get_serial(terminal->display));
2236 terminal_paste(struct terminal *terminal, struct input *input)
2238 input_receive_selection_data_to_fd(input,
2239 "text/plain;charset=utf-8",
2245 terminal_new_instance(struct terminal *terminal)
2247 struct terminal *new_terminal;
2249 new_terminal = terminal_create(terminal->display);
2250 if (terminal_run(new_terminal, option_shell))
2251 terminal_destroy(new_terminal);
2255 handle_bound_key(struct terminal *terminal,
2256 struct input *input, uint32_t sym, uint32_t time)
2260 /* Cut selection; terminal doesn't do cut, fall
2261 * through to copy. */
2263 terminal_copy(terminal, input);
2266 terminal_paste(terminal, input);
2269 terminal_new_instance(terminal);
2273 if (!terminal->scrolling)
2274 terminal->saved_start = terminal->start;
2275 if (terminal->start == terminal->end - terminal->log_size)
2278 terminal->scrolling = 1;
2281 terminal->selection_start_row++;
2282 terminal->selection_end_row++;
2283 widget_schedule_redraw(terminal->widget);
2287 if (!terminal->scrolling)
2288 terminal->saved_start = terminal->start;
2290 if (terminal->start == terminal->saved_start)
2293 terminal->scrolling = 1;
2296 terminal->selection_start_row--;
2297 terminal->selection_end_row--;
2298 widget_schedule_redraw(terminal->widget);
2307 key_handler(struct window *window, struct input *input, uint32_t time,
2308 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2311 struct terminal *terminal = data;
2312 char ch[MAX_RESPONSE];
2313 uint32_t modifiers, serial;
2314 int ret, len = 0, d;
2315 bool convert_utf8 = true;
2317 modifiers = input_get_modifiers(input);
2318 if ((modifiers & MOD_CONTROL_MASK) &&
2319 (modifiers & MOD_SHIFT_MASK) &&
2320 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2321 handle_bound_key(terminal, input, sym, time))
2324 /* Map keypad symbols to 'normal' equivalents before processing */
2326 case XKB_KEY_KP_Space:
2327 sym = XKB_KEY_space;
2329 case XKB_KEY_KP_Tab:
2332 case XKB_KEY_KP_Enter:
2333 sym = XKB_KEY_Return;
2335 case XKB_KEY_KP_Left:
2341 case XKB_KEY_KP_Right:
2342 sym = XKB_KEY_Right;
2344 case XKB_KEY_KP_Down:
2347 case XKB_KEY_KP_Equal:
2348 sym = XKB_KEY_equal;
2350 case XKB_KEY_KP_Multiply:
2351 sym = XKB_KEY_asterisk;
2353 case XKB_KEY_KP_Add:
2356 case XKB_KEY_KP_Separator:
2357 /* Note this is actually locale-dependent and should mostly be
2358 * a comma. But leave it as period until we one day start
2359 * doing the right thing. */
2360 sym = XKB_KEY_period;
2362 case XKB_KEY_KP_Subtract:
2363 sym = XKB_KEY_minus;
2365 case XKB_KEY_KP_Decimal:
2366 sym = XKB_KEY_period;
2368 case XKB_KEY_KP_Divide:
2369 sym = XKB_KEY_slash;
2381 sym = (sym - XKB_KEY_KP_0) + XKB_KEY_0;
2388 case XKB_KEY_BackSpace:
2389 if (modifiers & MOD_ALT_MASK)
2394 case XKB_KEY_Linefeed:
2397 case XKB_KEY_Scroll_Lock:
2398 case XKB_KEY_Sys_Req:
2399 case XKB_KEY_Escape:
2400 ch[len++] = sym & 0x7f;
2403 case XKB_KEY_Return:
2404 if (terminal->mode & MODE_LF_NEWLINE) {
2412 case XKB_KEY_Shift_L:
2413 case XKB_KEY_Shift_R:
2414 case XKB_KEY_Control_L:
2415 case XKB_KEY_Control_R:
2418 case XKB_KEY_Meta_L:
2419 case XKB_KEY_Meta_R:
2420 case XKB_KEY_Super_L:
2421 case XKB_KEY_Super_R:
2422 case XKB_KEY_Hyper_L:
2423 case XKB_KEY_Hyper_R:
2426 case XKB_KEY_Insert:
2427 len = function_key_response('[', 2, modifiers, '~', ch);
2429 case XKB_KEY_Delete:
2430 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2433 len = function_key_response('[', 3, modifiers, '~', ch);
2436 case XKB_KEY_Page_Up:
2437 len = function_key_response('[', 5, modifiers, '~', ch);
2439 case XKB_KEY_Page_Down:
2440 len = function_key_response('[', 6, modifiers, '~', ch);
2443 len = function_key_response('O', 1, modifiers, 'P', ch);
2446 len = function_key_response('O', 1, modifiers, 'Q', ch);
2449 len = function_key_response('O', 1, modifiers, 'R', ch);
2452 len = function_key_response('O', 1, modifiers, 'S', ch);
2455 len = function_key_response('[', 15, modifiers, '~', ch);
2458 len = function_key_response('[', 17, modifiers, '~', ch);
2461 len = function_key_response('[', 18, modifiers, '~', ch);
2464 len = function_key_response('[', 19, modifiers, '~', ch);
2467 len = function_key_response('[', 20, modifiers, '~', ch);
2470 len = function_key_response('[', 21, modifiers, '~', ch);
2473 len = function_key_response('[', 24, modifiers, '~', ch);
2476 /* Handle special keys with alternate mappings */
2477 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2478 if (len != 0) break;
2480 if (modifiers & MOD_CONTROL_MASK) {
2481 if (sym >= '3' && sym <= '7')
2482 sym = (sym & 0x1f) + 8;
2484 if (!((sym >= '!' && sym <= '/') ||
2485 (sym >= '8' && sym <= '?') ||
2486 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2487 else if (sym == '2') sym = 0x00;
2488 else if (sym == '/') sym = 0x1F;
2489 else if (sym == '8' || sym == '?') sym = 0x7F;
2491 if (modifiers & MOD_ALT_MASK) {
2492 if (terminal->mode & MODE_ALT_SENDS_ESC) {
2496 convert_utf8 = false;
2501 (!convert_utf8 && sym < 256)) {
2504 ret = xkb_keysym_to_utf8(sym, ch + len,
2505 MAX_RESPONSE - len);
2508 "Warning: buffer too small to encode "
2509 "UTF8 character\n");
2517 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
2518 if (terminal->scrolling) {
2519 d = terminal->saved_start - terminal->start;
2521 terminal->selection_start_row -= d;
2522 terminal->selection_end_row -= d;
2523 terminal->start = terminal->saved_start;
2524 terminal->scrolling = 0;
2525 widget_schedule_redraw(terminal->widget);
2528 terminal_write(terminal, ch, len);
2530 /* Hide cursor, except if this was coming from a
2531 * repeating key press. */
2532 serial = display_get_serial(terminal->display);
2533 if (terminal->hide_cursor_serial != serial) {
2534 input_set_pointer_image(input, CURSOR_BLANK);
2535 terminal->hide_cursor_serial = serial;
2541 keyboard_focus_handler(struct window *window,
2542 struct input *device, void *data)
2544 struct terminal *terminal = data;
2546 window_schedule_redraw(terminal->window);
2549 static int wordsep(int ch)
2551 const char extra[] = "-,./?%&#:_=+@~";
2556 return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
2560 recompute_selection(struct terminal *terminal)
2562 struct rectangle allocation;
2563 int col, x, width, height;
2564 int start_row, end_row;
2565 int word_start, eol;
2566 int side_margin, top_margin;
2569 union utf8_char *data;
2571 cw = terminal->average_width;
2572 ch = terminal->extents.height;
2573 widget_get_allocation(terminal->widget, &allocation);
2574 width = terminal->width * cw;
2575 height = terminal->height * ch;
2576 side_margin = allocation.x + (allocation.width - width) / 2;
2577 top_margin = allocation.y + (allocation.height - height) / 2;
2579 start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1;
2580 end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1;
2582 if (start_row < end_row ||
2583 (start_row == end_row &&
2584 terminal->selection_start_x < terminal->selection_end_x)) {
2585 terminal->selection_start_row = start_row;
2586 terminal->selection_end_row = end_row;
2587 start_x = terminal->selection_start_x;
2588 end_x = terminal->selection_end_x;
2590 terminal->selection_start_row = end_row;
2591 terminal->selection_end_row = start_row;
2592 start_x = terminal->selection_end_x;
2593 end_x = terminal->selection_start_x;
2597 if (terminal->selection_start_row < 0) {
2598 terminal->selection_start_row = 0;
2599 terminal->selection_start_col = 0;
2601 x = side_margin + cw / 2;
2602 data = terminal_get_row(terminal,
2603 terminal->selection_start_row);
2605 for (col = 0; col < terminal->width; col++, x += cw) {
2606 if (col == 0 || wordsep(data[col - 1].ch))
2608 if (data[col].ch != 0)
2614 switch (terminal->dragging) {
2616 terminal->selection_start_col = 0;
2619 terminal->selection_start_col = word_start;
2622 terminal->selection_start_col = col;
2627 if (terminal->selection_end_row >= terminal->height) {
2628 terminal->selection_end_row = terminal->height;
2629 terminal->selection_end_col = 0;
2631 x = side_margin + cw / 2;
2632 data = terminal_get_row(terminal, terminal->selection_end_row);
2633 for (col = 0; col < terminal->width; col++, x += cw) {
2634 if (terminal->dragging == SELECT_CHAR && end_x < x)
2636 if (terminal->dragging == SELECT_WORD &&
2637 end_x < x && wordsep(data[col].ch))
2640 terminal->selection_end_col = col;
2643 if (terminal->selection_end_col != terminal->selection_start_col ||
2644 terminal->selection_start_row != terminal->selection_end_row) {
2645 col = terminal->selection_end_col;
2646 if (col > 0 && data[col - 1].ch == 0)
2647 terminal->selection_end_col = terminal->width;
2648 data = terminal_get_row(terminal, terminal->selection_start_row);
2649 if (data[terminal->selection_start_col].ch == 0)
2650 terminal->selection_start_col = eol;
2657 menu_func(struct window *window, struct input *input, int index, void *data)
2659 struct terminal *terminal = data;
2661 fprintf(stderr, "picked entry %d\n", index);
2665 terminal_new_instance(terminal);
2668 terminal_copy(terminal, input);
2671 terminal_paste(terminal, input);
2677 show_menu(struct terminal *terminal, struct input *input, uint32_t time)
2680 static const char *entries[] = {
2681 "Open Terminal", "Copy", "Paste"
2684 input_get_position(input, &x, &y);
2685 window_show_menu(terminal->display, input, time, terminal->window,
2686 x - 10, y - 10, menu_func,
2687 entries, ARRAY_LENGTH(entries));
2691 click_handler(struct widget *widget, struct terminal *terminal,
2692 struct input *input, int32_t x, int32_t y,
2695 if (time - terminal->click_time < 500)
2696 terminal->click_count++;
2698 terminal->click_count = 1;
2700 terminal->click_time = time;
2701 terminal->dragging = (terminal->click_count - 1) % 3 + SELECT_CHAR;
2703 terminal->selection_end_x = terminal->selection_start_x = x;
2704 terminal->selection_end_y = terminal->selection_start_y = y;
2705 if (recompute_selection(terminal))
2706 widget_schedule_redraw(widget);
2710 button_handler(struct widget *widget,
2711 struct input *input, uint32_t time,
2713 enum wl_pointer_button_state state, void *data)
2715 struct terminal *terminal = data;
2720 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2721 input_get_position(input, &x, &y);
2722 click_handler(widget, terminal, input, x, y, time);
2724 terminal->dragging = SELECT_NONE;
2729 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
2730 show_menu(terminal, input, time);
2736 enter_handler(struct widget *widget,
2737 struct input *input, float x, float y, void *data)
2739 struct terminal *terminal = data;
2741 /* Reset title to get rid of resizing '[WxH]' in titlebar */
2742 window_set_title(terminal->window, terminal->title);
2744 return CURSOR_IBEAM;
2748 motion_handler(struct widget *widget,
2749 struct input *input, uint32_t time,
2750 float x, float y, void *data)
2752 struct terminal *terminal = data;
2754 if (terminal->dragging) {
2755 input_get_position(input,
2756 &terminal->selection_end_x,
2757 &terminal->selection_end_y);
2759 if (recompute_selection(terminal))
2760 widget_schedule_redraw(widget);
2763 return CURSOR_IBEAM;
2767 output_handler(struct window *window, struct output *output, int enter,
2771 window_set_buffer_transform(window, output_get_transform(output));
2772 window_set_buffer_scale(window, window_get_output_scale(window));
2773 window_schedule_redraw(window);
2777 touch_down_handler(struct widget *widget, struct input *input,
2778 uint32_t serial, uint32_t time, int32_t id,
2779 float x, float y, void *data)
2781 struct terminal *terminal = data;
2784 click_handler(widget, terminal, input, x, y, time);
2788 touch_up_handler(struct widget *widget, struct input *input,
2789 uint32_t serial, uint32_t time, int32_t id, void *data)
2791 struct terminal *terminal = data;
2794 terminal->dragging = SELECT_NONE;
2798 touch_motion_handler(struct widget *widget, struct input *input,
2799 uint32_t time, int32_t id, float x, float y, void *data)
2801 struct terminal *terminal = data;
2803 if (terminal->dragging &&
2805 terminal->selection_end_x = (int)x;
2806 terminal->selection_end_y = (int)y;
2808 if (recompute_selection(terminal))
2809 widget_schedule_redraw(widget);
2814 #define howmany(x, y) (((x) + ((y) - 1)) / (y))
2817 static struct terminal *
2818 terminal_create(struct display *display)
2820 struct terminal *terminal;
2821 cairo_surface_t *surface;
2823 cairo_text_extents_t text_extents;
2825 terminal = xzalloc(sizeof *terminal);
2826 terminal->color_scheme = &DEFAULT_COLORS;
2827 terminal_init(terminal);
2828 terminal->margin_top = 0;
2829 terminal->margin_bottom = -1;
2830 terminal->window = window_create(display);
2831 terminal->widget = window_frame_create(terminal->window, terminal);
2832 terminal->title = strdup("Wayland Terminal");
2833 window_set_title(terminal->window, terminal->title);
2834 widget_set_transparent(terminal->widget, 0);
2836 init_state_machine(&terminal->state_machine);
2837 init_color_table(terminal);
2839 terminal->display = display;
2840 terminal->margin = 5;
2841 terminal->buffer_height = 1024;
2844 window_set_user_data(terminal->window, terminal);
2845 window_set_key_handler(terminal->window, key_handler);
2846 window_set_keyboard_focus_handler(terminal->window,
2847 keyboard_focus_handler);
2848 window_set_fullscreen_handler(terminal->window, fullscreen_handler);
2849 window_set_output_handler(terminal->window, output_handler);
2850 window_set_close_handler(terminal->window, close_handler);
2852 window_set_data_handler(terminal->window, data_handler);
2853 window_set_drop_handler(terminal->window, drop_handler);
2855 widget_set_redraw_handler(terminal->widget, redraw_handler);
2856 widget_set_resize_handler(terminal->widget, resize_handler);
2857 widget_set_button_handler(terminal->widget, button_handler);
2858 widget_set_enter_handler(terminal->widget, enter_handler);
2859 widget_set_motion_handler(terminal->widget, motion_handler);
2860 widget_set_touch_up_handler(terminal->widget, touch_up_handler);
2861 widget_set_touch_down_handler(terminal->widget, touch_down_handler);
2862 widget_set_touch_motion_handler(terminal->widget, touch_motion_handler);
2864 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2865 cr = cairo_create(surface);
2866 cairo_set_font_size(cr, option_font_size);
2867 cairo_select_font_face (cr, option_font,
2868 CAIRO_FONT_SLANT_NORMAL,
2869 CAIRO_FONT_WEIGHT_BOLD);
2870 terminal->font_bold = cairo_get_scaled_font (cr);
2871 cairo_scaled_font_reference(terminal->font_bold);
2873 cairo_select_font_face (cr, option_font,
2874 CAIRO_FONT_SLANT_NORMAL,
2875 CAIRO_FONT_WEIGHT_NORMAL);
2876 terminal->font_normal = cairo_get_scaled_font (cr);
2877 cairo_scaled_font_reference(terminal->font_normal);
2879 cairo_font_extents(cr, &terminal->extents);
2881 /* Compute the average ascii glyph width */
2882 cairo_text_extents(cr, TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS,
2884 terminal->average_width = howmany
2885 (text_extents.width,
2886 strlen(TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS));
2887 terminal->average_width = ceil(terminal->average_width);
2890 cairo_surface_destroy(surface);
2892 terminal_resize(terminal, 20, 5); /* Set minimum size first */
2893 terminal_resize(terminal, 80, 25);
2895 wl_list_insert(terminal_list.prev, &terminal->link);
2901 terminal_destroy(struct terminal *terminal)
2903 display_unwatch_fd(terminal->display, terminal->master);
2904 window_destroy(terminal->window);
2905 close(terminal->master);
2906 wl_list_remove(&terminal->link);
2908 if (wl_list_empty(&terminal_list))
2909 display_exit(terminal->display);
2911 free(terminal->title);
2916 io_handler(struct task *task, uint32_t events)
2918 struct terminal *terminal =
2919 container_of(task, struct terminal, io_task);
2923 if (events & EPOLLHUP) {
2924 terminal_destroy(terminal);
2928 len = read(terminal->master, buffer, sizeof buffer);
2930 terminal_destroy(terminal);
2932 terminal_data(terminal, buffer, len);
2936 terminal_run(struct terminal *terminal, const char *path)
2941 pid = forkpty(&master, NULL, NULL, NULL);
2943 setenv("TERM", option_term, 1);
2944 setenv("COLORTERM", option_term, 1);
2945 if (execl(path, path, NULL)) {
2946 printf("exec failed: %m\n");
2949 } else if (pid < 0) {
2950 fprintf(stderr, "failed to fork and create pty (%m).\n");
2954 terminal->master = master;
2955 fcntl(master, F_SETFL, O_NONBLOCK);
2956 terminal->io_task.run = io_handler;
2957 display_watch_fd(terminal->display, terminal->master,
2958 EPOLLIN | EPOLLHUP, &terminal->io_task);
2960 window_set_fullscreen(terminal->window, option_fullscreen);
2961 if (!window_is_fullscreen(terminal->window))
2962 terminal_resize(terminal, 80, 24);
2967 static const struct weston_option terminal_options[] = {
2968 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
2969 { WESTON_OPTION_STRING, "font", 0, &option_font },
2970 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
2973 int main(int argc, char *argv[])
2976 struct terminal *terminal;
2977 struct weston_config *config;
2978 struct weston_config_section *s;
2980 /* as wcwidth is locale-dependent,
2981 wcwidth needs setlocale call to function properly. */
2982 setlocale(LC_ALL, "");
2984 option_shell = getenv("SHELL");
2986 option_shell = "/bin/bash";
2988 config = weston_config_parse("weston.ini");
2989 s = weston_config_get_section(config, "terminal", NULL, NULL);
2990 weston_config_section_get_string(s, "font", &option_font, "mono");
2991 weston_config_section_get_int(s, "font-size", &option_font_size, 14);
2992 weston_config_section_get_string(s, "term", &option_term, "xterm");
2993 weston_config_destroy(config);
2995 d = display_create(&argc, argv);
2997 fprintf(stderr, "failed to create display: %m\n");
3001 wl_list_init(&terminal_list);
3002 terminal = terminal_create(d);
3003 if (terminal_run(terminal, option_shell))