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 wl_fixed_t smooth_scroll;
451 int saved_row, saved_column;
453 int send_cursor_position;
456 char escape[MAX_ESCAPE+1];
458 enum escape_state state;
459 enum escape_state outer_state;
461 struct utf8_state_machine state_machine;
463 struct color_scheme *color_scheme;
464 struct terminal_color color_table[256];
465 cairo_font_extents_t extents;
466 double average_width;
467 cairo_scaled_font_t *font_normal, *font_bold;
468 uint32_t hide_cursor_serial;
471 struct wl_data_source *selection;
473 int dragging, click_count;
474 int selection_start_x, selection_start_y;
475 int selection_end_x, selection_end_y;
476 int selection_start_row, selection_start_col;
477 int selection_end_row, selection_end_col;
481 /* Create default tab stops, every 8 characters */
483 terminal_init_tabs(struct terminal *terminal)
487 while (i < terminal->width) {
489 terminal->tab_ruler[i] = 1;
491 terminal->tab_ruler[i] = 0;
497 terminal_init(struct terminal *terminal)
499 terminal->curr_attr = terminal->color_scheme->default_attr;
500 terminal->origin_mode = 0;
501 terminal->mode = MODE_SHOW_CURSOR |
507 terminal->column = 0;
509 terminal->g0 = CS_US;
510 terminal->g1 = CS_US;
511 terminal->cs = terminal->g0;
512 terminal->key_mode = KM_NORMAL;
514 terminal->saved_g0 = terminal->g0;
515 terminal->saved_g1 = terminal->g1;
516 terminal->saved_cs = terminal->cs;
518 terminal->saved_attr = terminal->curr_attr;
519 terminal->saved_origin_mode = terminal->origin_mode;
520 terminal->saved_row = terminal->row;
521 terminal->saved_column = terminal->column;
523 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
527 init_color_table(struct terminal *terminal)
530 struct terminal_color *color_table = terminal->color_table;
532 for (c = 0; c < 256; c ++) {
534 color_table[c] = terminal->color_scheme->palette[c];
535 } else if (c < 232) {
537 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
538 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
539 color_table[c].r = ((double)(r % 6) / 6.0);
540 color_table[c].a = 1.0;
542 r = (c - 232) * 10 + 8;
543 color_table[c].r = ((double) r) / 256.0;
544 color_table[c].g = color_table[c].r;
545 color_table[c].b = color_table[c].r;
546 color_table[c].a = 1.0;
551 static union utf8_char *
552 terminal_get_row(struct terminal *terminal, int row)
556 index = (row + terminal->start) & (terminal->buffer_height - 1);
558 return (void *) terminal->data + index * terminal->data_pitch;
562 terminal_get_attr_row(struct terminal *terminal, int row)
566 index = (row + terminal->start) & (terminal->buffer_height - 1);
568 return (void *) terminal->data_attr + index * terminal->attr_pitch;
577 terminal_decode_attr(struct terminal *terminal, int row, int col,
578 union decoded_attr *decoded)
581 int foreground, background, tmp;
584 if (((row == terminal->selection_start_row &&
585 col >= terminal->selection_start_col) ||
586 row > terminal->selection_start_row) &&
587 ((row == terminal->selection_end_row &&
588 col < terminal->selection_end_col) ||
589 row < terminal->selection_end_row))
592 /* get the attributes for this character cell */
593 attr = terminal_get_attr_row(terminal, row)[col];
594 if ((attr.a & ATTRMASK_INVERSE) ||
596 ((terminal->mode & MODE_SHOW_CURSOR) &&
597 window_has_focus(terminal->window) && terminal->row == row &&
598 terminal->column == col)) {
599 foreground = attr.bg;
600 background = attr.fg;
601 if (attr.a & ATTRMASK_BOLD) {
602 if (foreground <= 16) foreground |= 0x08;
603 if (background <= 16) background &= 0x07;
606 foreground = attr.fg;
607 background = attr.bg;
610 if (terminal->mode & MODE_INVERSE) {
612 foreground = background;
614 if (attr.a & ATTRMASK_BOLD) {
615 if (foreground <= 16) foreground |= 0x08;
616 if (background <= 16) background &= 0x07;
620 decoded->attr.fg = foreground;
621 decoded->attr.bg = background;
622 decoded->attr.a = attr.a;
627 terminal_scroll_buffer(struct terminal *terminal, int d)
631 terminal->start += d;
634 for (i = 0; i < d; 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);
640 for (i = terminal->height - d; i < terminal->height; i++) {
641 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
642 attr_init(terminal_get_attr_row(terminal, i),
643 terminal->curr_attr, terminal->width);
647 terminal->selection_start_row -= d;
648 terminal->selection_end_row -= d;
652 terminal_scroll_window(struct terminal *terminal, int d)
656 int from_row, to_row;
658 // scrolling range is inclusive
659 window_height = terminal->margin_bottom - terminal->margin_top + 1;
660 d = d % (window_height + 1);
663 to_row = terminal->margin_bottom;
664 from_row = terminal->margin_bottom - d;
666 for (i = 0; i < (window_height - d); i++) {
667 memcpy(terminal_get_row(terminal, to_row - i),
668 terminal_get_row(terminal, from_row - i),
669 terminal->data_pitch);
670 memcpy(terminal_get_attr_row(terminal, to_row - i),
671 terminal_get_attr_row(terminal, from_row - i),
672 terminal->attr_pitch);
674 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
675 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
676 attr_init(terminal_get_attr_row(terminal, i),
677 terminal->curr_attr, terminal->width);
680 to_row = terminal->margin_top;
681 from_row = terminal->margin_top + d;
683 for (i = 0; i < (window_height - d); i++) {
684 memcpy(terminal_get_row(terminal, to_row + i),
685 terminal_get_row(terminal, from_row + i),
686 terminal->data_pitch);
687 memcpy(terminal_get_attr_row(terminal, to_row + i),
688 terminal_get_attr_row(terminal, from_row + i),
689 terminal->attr_pitch);
691 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
692 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
693 attr_init(terminal_get_attr_row(terminal, i),
694 terminal->curr_attr, terminal->width);
700 terminal_scroll(struct terminal *terminal, int d)
702 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
703 terminal_scroll_buffer(terminal, d);
705 terminal_scroll_window(terminal, d);
709 terminal_shift_line(struct terminal *terminal, int d)
711 union utf8_char *row;
712 struct attr *attr_row;
714 row = terminal_get_row(terminal, terminal->row);
715 attr_row = terminal_get_attr_row(terminal, terminal->row);
717 if ((terminal->width + d) <= terminal->column)
718 d = terminal->column + 1 - terminal->width;
719 if ((terminal->column + d) >= terminal->width)
720 d = terminal->width - terminal->column - 1;
724 memmove(&row[terminal->column],
725 &row[terminal->column + d],
726 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
727 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
728 (terminal->width - terminal->column - d) * sizeof(struct attr));
729 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
730 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
732 memmove(&row[terminal->column + d], &row[terminal->column],
733 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
734 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
735 (terminal->width - terminal->column - d) * sizeof(struct attr));
736 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
737 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
742 terminal_resize_cells(struct terminal *terminal,
743 int width, int height)
745 union utf8_char *data;
746 struct attr *data_attr;
748 int data_pitch, attr_pitch;
749 int i, l, total_rows;
750 uint32_t d, uheight = height;
751 struct rectangle allocation;
754 if (uheight > terminal->buffer_height)
755 height = terminal->buffer_height;
757 if (terminal->width == width && terminal->height == height)
760 if (terminal->data && width <= terminal->max_width) {
762 if (height < terminal->height && height <= terminal->row)
763 d = terminal->height - height;
764 else if (height > terminal->height &&
765 terminal->height - 1 == terminal->row) {
766 d = terminal->height - height;
767 if (terminal->log_size < uheight)
768 d = -terminal->start;
771 terminal->start += d;
774 terminal->max_width = width;
775 data_pitch = width * sizeof(union utf8_char);
776 data = xzalloc(data_pitch * terminal->buffer_height);
777 attr_pitch = width * sizeof(struct attr);
778 data_attr = xmalloc(attr_pitch * terminal->buffer_height);
779 tab_ruler = xzalloc(width);
780 attr_init(data_attr, terminal->curr_attr,
781 width * terminal->buffer_height);
783 if (terminal->data && terminal->data_attr) {
784 if (width > terminal->width)
789 if (terminal->height > height) {
791 i = 1 + terminal->row - height;
793 terminal->start += i;
794 terminal->row = terminal->row - i;
797 total_rows = terminal->height;
800 for (i = 0; i < total_rows; i++) {
801 memcpy(&data[width * i],
802 terminal_get_row(terminal, i),
803 l * sizeof(union utf8_char));
804 memcpy(&data_attr[width * i],
805 terminal_get_attr_row(terminal, i),
806 l * sizeof(struct attr));
809 free(terminal->data);
810 free(terminal->data_attr);
811 free(terminal->tab_ruler);
814 terminal->data_pitch = data_pitch;
815 terminal->attr_pitch = attr_pitch;
816 terminal->data = data;
817 terminal->data_attr = data_attr;
818 terminal->tab_ruler = tab_ruler;
822 terminal->margin_bottom =
823 height - (terminal->height - terminal->margin_bottom);
824 terminal->width = width;
825 terminal->height = height;
826 terminal_init_tabs(terminal);
828 /* Update the window size */
829 ws.ws_row = terminal->height;
830 ws.ws_col = terminal->width;
831 widget_get_allocation(terminal->widget, &allocation);
832 ws.ws_xpixel = allocation.width;
833 ws.ws_ypixel = allocation.height;
834 ioctl(terminal->master, TIOCSWINSZ, &ws);
838 update_title(struct terminal *terminal)
840 if (window_is_resizing(terminal->window)) {
842 if (asprintf(&p, "%s — [%dx%d]", terminal->title, terminal->width, terminal->height) > 0) {
843 window_set_title(terminal->window, p);
847 window_set_title(terminal->window, terminal->title);
852 resize_handler(struct widget *widget,
853 int32_t width, int32_t height, void *data)
855 struct terminal *terminal = data;
856 int32_t columns, rows, m;
858 m = 2 * terminal->margin;
859 columns = (width - m) / (int32_t) terminal->average_width;
860 rows = (height - m) / (int32_t) terminal->extents.height;
862 if (!window_is_fullscreen(terminal->window) &&
863 !window_is_maximized(terminal->window)) {
864 width = columns * terminal->average_width + m;
865 height = rows * terminal->extents.height + m;
866 widget_set_size(terminal->widget, width, height);
869 terminal_resize_cells(terminal, columns, rows);
870 update_title(terminal);
874 state_changed_handler(struct window *window, void *data)
876 struct terminal *terminal = data;
877 update_title(terminal);
881 terminal_resize(struct terminal *terminal, int columns, int rows)
883 int32_t width, height, m;
885 if (window_is_fullscreen(terminal->window) ||
886 window_is_maximized(terminal->window))
889 m = 2 * terminal->margin;
890 width = columns * terminal->average_width + m;
891 height = rows * terminal->extents.height + m;
893 window_frame_set_child_size(terminal->widget, width, height);
896 struct color_scheme DEFAULT_COLORS = {
898 {0, 0, 0, 1}, /* black */
899 {0.66, 0, 0, 1}, /* red */
900 {0 , 0.66, 0, 1}, /* green */
901 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
902 {0 , 0 , 0.66, 1}, /* blue */
903 {0.66, 0 , 0.66, 1}, /* magenta */
904 {0, 0.66, 0.66, 1}, /* cyan */
905 {0.66, 0.66, 0.66, 1}, /* light grey */
906 {0.22, 0.33, 0.33, 1}, /* dark grey */
907 {1, 0.33, 0.33, 1}, /* high red */
908 {0.33, 1, 0.33, 1}, /* high green */
909 {1, 1, 0.33, 1}, /* high yellow */
910 {0.33, 0.33, 1, 1}, /* high blue */
911 {1, 0.33, 1, 1}, /* high magenta */
912 {0.33, 1, 1, 1}, /* high cyan */
913 {1, 1, 1, 1} /* white */
915 0, /* black border */
916 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
920 terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
922 cairo_set_source_rgba(cr,
923 terminal->color_table[index].r,
924 terminal->color_table[index].g,
925 terminal->color_table[index].b,
926 terminal->color_table[index].a);
930 terminal_send_selection(struct terminal *terminal, int fd)
933 union utf8_char *p_row;
934 union decoded_attr attr;
938 fp = fdopen(fd, "w");
943 for (row = 0; row < terminal->height; row++) {
944 p_row = terminal_get_row(terminal, row);
945 for (col = 0; col < terminal->width; col++) {
946 if (p_row[col].ch == 0x200B) /* space glyph */
948 /* get the attributes for this character cell */
949 terminal_decode_attr(terminal, row, col, &attr);
952 len = strnlen((char *) p_row[col].byte, 4);
954 fwrite(p_row[col].byte, 1, len, fp);
955 if (len == 0 || col == terminal->width - 1) {
956 fwrite("\n", 1, 1, fp);
965 struct terminal *terminal;
968 union decoded_attr attr;
969 cairo_glyph_t glyphs[256], *g;
973 glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
975 run->terminal = terminal;
977 run->g = run->glyphs;
983 glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
985 cairo_scaled_font_t *font;
987 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
988 (attr.key != run->attr.key)) {
989 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
990 font = run->terminal->font_bold;
992 font = run->terminal->font_normal;
993 cairo_set_scaled_font(run->cr, font);
994 terminal_set_color(run->terminal, run->cr,
997 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
998 cairo_show_glyphs (run->cr, run->glyphs, run->count);
999 run->g = run->glyphs;
1006 glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
1009 cairo_scaled_font_t *font;
1011 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
1013 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
1014 font = run->terminal->font_bold;
1016 font = run->terminal->font_normal;
1018 cairo_move_to(run->cr, x, y);
1019 cairo_scaled_font_text_to_glyphs (font, x, y,
1020 (char *) c->byte, 4,
1021 &run->g, &num_glyphs,
1023 run->g += num_glyphs;
1024 run->count += num_glyphs;
1029 redraw_handler(struct widget *widget, void *data)
1031 struct terminal *terminal = data;
1032 struct rectangle allocation;
1034 int top_margin, side_margin;
1035 int row, col, cursor_x, cursor_y;
1036 union utf8_char *p_row;
1037 union decoded_attr attr;
1039 cairo_surface_t *surface;
1041 struct glyph_run run;
1042 cairo_font_extents_t extents;
1043 double average_width;
1044 double unichar_width;
1046 surface = window_get_surface(terminal->window);
1047 widget_get_allocation(terminal->widget, &allocation);
1048 cr = widget_cairo_create(terminal->widget);
1049 cairo_rectangle(cr, allocation.x, allocation.y,
1050 allocation.width, allocation.height);
1052 cairo_push_group(cr);
1054 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1055 terminal_set_color(terminal, cr, terminal->color_scheme->border);
1058 cairo_set_scaled_font(cr, terminal->font_normal);
1060 extents = terminal->extents;
1061 average_width = terminal->average_width;
1062 side_margin = (allocation.width - terminal->width * average_width) / 2;
1063 top_margin = (allocation.height - terminal->height * extents.height) / 2;
1065 cairo_set_line_width(cr, 1.0);
1066 cairo_translate(cr, allocation.x + side_margin,
1067 allocation.y + top_margin);
1068 /* paint the background */
1069 for (row = 0; row < terminal->height; row++) {
1070 p_row = terminal_get_row(terminal, row);
1071 for (col = 0; col < terminal->width; col++) {
1072 /* get the attributes for this character cell */
1073 terminal_decode_attr(terminal, row, col, &attr);
1075 if (attr.attr.bg == terminal->color_scheme->border)
1078 if (is_wide(p_row[col]))
1079 unichar_width = 2 * average_width;
1081 unichar_width = average_width;
1083 terminal_set_color(terminal, cr, attr.attr.bg);
1084 cairo_move_to(cr, col * average_width,
1085 row * extents.height);
1086 cairo_rel_line_to(cr, unichar_width, 0);
1087 cairo_rel_line_to(cr, 0, extents.height);
1088 cairo_rel_line_to(cr, -unichar_width, 0);
1089 cairo_close_path(cr);
1094 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1096 /* paint the foreground */
1097 glyph_run_init(&run, terminal, cr);
1098 for (row = 0; row < terminal->height; row++) {
1099 p_row = terminal_get_row(terminal, row);
1100 for (col = 0; col < terminal->width; col++) {
1101 /* get the attributes for this character cell */
1102 terminal_decode_attr(terminal, row, col, &attr);
1104 glyph_run_flush(&run, attr);
1106 text_x = col * average_width;
1107 text_y = extents.ascent + row * extents.height;
1108 if (attr.attr.a & ATTRMASK_UNDERLINE) {
1109 terminal_set_color(terminal, cr, attr.attr.fg);
1110 cairo_move_to(cr, text_x, (double)text_y + 1.5);
1111 cairo_line_to(cr, text_x + average_width, (double) text_y + 1.5);
1115 /* skip space glyph (RLE) we use as a placeholder of
1116 the right half of a double-width character,
1117 because RLE is not available in every font. */
1118 if (p_row[col].ch == 0x200B)
1121 glyph_run_add(&run, text_x, text_y, &p_row[col]);
1126 glyph_run_flush(&run, attr);
1128 if ((terminal->mode & MODE_SHOW_CURSOR) &&
1129 !window_has_focus(terminal->window)) {
1132 cairo_set_line_width(cr, 1);
1133 cairo_move_to(cr, terminal->column * average_width + d,
1134 terminal->row * extents.height + d);
1135 cairo_rel_line_to(cr, average_width - 2 * d, 0);
1136 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
1137 cairo_rel_line_to(cr, -average_width + 2 * d, 0);
1138 cairo_close_path(cr);
1143 cairo_pop_group_to_source(cr);
1146 cairo_surface_destroy(surface);
1148 if (terminal->send_cursor_position) {
1149 cursor_x = side_margin + allocation.x +
1150 terminal->column * average_width;
1151 cursor_y = top_margin + allocation.y +
1152 terminal->row * extents.height;
1153 window_set_text_cursor_position(terminal->window,
1154 cursor_x, cursor_y);
1155 terminal->send_cursor_position = 0;
1160 terminal_write(struct terminal *terminal, const char *data, size_t length)
1162 if (write(terminal->master, data, length) < 0)
1164 terminal->send_cursor_position = 1;
1168 terminal_data(struct terminal *terminal, const char *data, size_t length);
1171 handle_char(struct terminal *terminal, union utf8_char utf8);
1174 handle_sgr(struct terminal *terminal, int code);
1177 handle_term_parameter(struct terminal *terminal, int code, int sr)
1181 if (terminal->escape_flags & ESC_FLAG_WHAT) {
1183 case 1: /* DECCKM */
1184 if (sr) terminal->key_mode = KM_APPLICATION;
1185 else terminal->key_mode = KM_NORMAL;
1187 case 2: /* DECANM */
1188 /* No VT52 support yet */
1189 terminal->g0 = CS_US;
1190 terminal->g1 = CS_US;
1191 terminal->cs = terminal->g0;
1193 case 3: /* DECCOLM */
1195 terminal_resize(terminal, 132, 24);
1197 terminal_resize(terminal, 80, 24);
1199 /* set columns, but also home cursor and clear screen */
1200 terminal->row = 0; terminal->column = 0;
1201 for (i = 0; i < terminal->height; i++) {
1202 memset(terminal_get_row(terminal, i),
1203 0, terminal->data_pitch);
1204 attr_init(terminal_get_attr_row(terminal, i),
1205 terminal->curr_attr, terminal->width);
1208 case 5: /* DECSCNM */
1209 if (sr) terminal->mode |= MODE_INVERSE;
1210 else terminal->mode &= ~MODE_INVERSE;
1213 terminal->origin_mode = sr;
1214 if (terminal->origin_mode)
1215 terminal->row = terminal->margin_top;
1218 terminal->column = 0;
1220 case 7: /* DECAWM */
1221 if (sr) terminal->mode |= MODE_AUTOWRAP;
1222 else terminal->mode &= ~MODE_AUTOWRAP;
1224 case 8: /* DECARM */
1225 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1226 else terminal->mode &= ~MODE_AUTOREPEAT;
1228 case 12: /* Very visible cursor (CVVIS) */
1229 /* FIXME: What do we do here. */
1232 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1233 else terminal->mode &= ~MODE_SHOW_CURSOR;
1235 case 1034: /* smm/rmm, meta mode on/off */
1238 case 1037: /* deleteSendsDel */
1239 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1240 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1242 case 1039: /* altSendsEscape */
1243 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1244 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1246 case 1049: /* rmcup/smcup, alternate screen */
1247 /* Ignore. Should be possible to implement,
1248 * but it's kind of annoying. */
1251 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1257 if (sr) terminal->mode |= MODE_IRM;
1258 else terminal->mode &= ~MODE_IRM;
1261 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1262 else terminal->mode &= ~MODE_LF_NEWLINE;
1265 fprintf(stderr, "Unknown parameter: %d\n", code);
1272 handle_dcs(struct terminal *terminal)
1277 handle_osc(struct terminal *terminal)
1282 terminal->escape[terminal->escape_length++] = '\0';
1283 p = &terminal->escape[2];
1284 code = strtol(p, &p, 10);
1288 case 0: /* Icon name and window title */
1289 case 1: /* Icon label */
1290 case 2: /* Window title*/
1291 free(terminal->title);
1292 terminal->title = strdup(p);
1293 window_set_title(terminal->window, p);
1295 case 7: /* shell cwd as uri */
1298 fprintf(stderr, "Unknown OSC escape code %d, text %s\n",
1305 handle_escape(struct terminal *terminal)
1307 union utf8_char *row;
1308 struct attr *attr_row;
1310 int i, count, x, y, top, bottom;
1311 int args[10], set[10] = { 0, };
1312 char response[MAX_RESPONSE] = {0, };
1313 struct rectangle allocation;
1315 terminal->escape[terminal->escape_length++] = '\0';
1317 p = &terminal->escape[2];
1318 while ((isdigit(*p) || *p == ';') && i < 10) {
1327 args[i] = strtol(p, &p, 10);
1334 count = set[0] ? args[0] : 1;
1335 if (count == 0) count = 1;
1336 terminal_shift_line(terminal, count);
1339 count = set[0] ? args[0] : 1;
1340 if (count == 0) count = 1;
1341 if (terminal->row - count >= terminal->margin_top)
1342 terminal->row -= count;
1344 terminal->row = terminal->margin_top;
1347 count = set[0] ? args[0] : 1;
1348 if (count == 0) count = 1;
1349 if (terminal->row + count <= terminal->margin_bottom)
1350 terminal->row += count;
1352 terminal->row = terminal->margin_bottom;
1355 count = set[0] ? args[0] : 1;
1356 if (count == 0) count = 1;
1357 if ((terminal->column + count) < terminal->width)
1358 terminal->column += count;
1360 terminal->column = terminal->width - 1;
1363 count = set[0] ? args[0] : 1;
1364 if (count == 0) count = 1;
1365 if ((terminal->column - count) >= 0)
1366 terminal->column -= count;
1368 terminal->column = 0;
1371 count = set[0] ? args[0] : 1;
1372 if (terminal->row + count <= terminal->margin_bottom)
1373 terminal->row += count;
1375 terminal->row = terminal->margin_bottom;
1376 terminal->column = 0;
1379 count = set[0] ? args[0] : 1;
1380 if (terminal->row - count >= terminal->margin_top)
1381 terminal->row -= count;
1383 terminal->row = terminal->margin_top;
1384 terminal->column = 0;
1387 y = set[0] ? args[0] : 1;
1388 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1390 terminal->column = y - 1;
1394 x = (set[1] ? args[1] : 1) - 1;
1396 (x >= terminal->width ? terminal->width - 1 : x);
1398 y = (set[0] ? args[0] : 1) - 1;
1399 if (terminal->origin_mode) {
1400 y += terminal->margin_top;
1401 y = y < terminal->margin_top ? terminal->margin_top :
1402 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1405 (y >= terminal->height ? terminal->height - 1 : y);
1409 terminal->column = x;
1412 count = set[0] ? args[0] : 1;
1413 if (count == 0) count = 1;
1414 while (count > 0 && terminal->column < terminal->width) {
1415 if (terminal->tab_ruler[terminal->column]) count--;
1421 row = terminal_get_row(terminal, terminal->row);
1422 attr_row = terminal_get_attr_row(terminal, terminal->row);
1423 if (!set[0] || args[0] == 0 || args[0] > 2) {
1424 memset(&row[terminal->column],
1425 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1426 attr_init(&attr_row[terminal->column],
1427 terminal->curr_attr, terminal->width - terminal->column);
1428 for (i = terminal->row + 1; i < terminal->height; i++) {
1429 memset(terminal_get_row(terminal, i),
1430 0, terminal->data_pitch);
1431 attr_init(terminal_get_attr_row(terminal, i),
1432 terminal->curr_attr, terminal->width);
1434 } else if (args[0] == 1) {
1435 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1436 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1437 for (i = 0; i < terminal->row; i++) {
1438 memset(terminal_get_row(terminal, i),
1439 0, terminal->data_pitch);
1440 attr_init(terminal_get_attr_row(terminal, i),
1441 terminal->curr_attr, terminal->width);
1443 } else if (args[0] == 2) {
1444 /* Clear screen by scrolling contents out */
1445 terminal_scroll_buffer(terminal,
1446 terminal->end - terminal->start);
1450 row = terminal_get_row(terminal, terminal->row);
1451 attr_row = terminal_get_attr_row(terminal, terminal->row);
1452 if (!set[0] || args[0] == 0 || args[0] > 2) {
1453 memset(&row[terminal->column], 0,
1454 (terminal->width - terminal->column) * sizeof(union utf8_char));
1455 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1456 terminal->width - terminal->column);
1457 } else if (args[0] == 1) {
1458 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1459 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1460 } else if (args[0] == 2) {
1461 memset(row, 0, terminal->data_pitch);
1462 attr_init(attr_row, terminal->curr_attr, terminal->width);
1466 count = set[0] ? args[0] : 1;
1467 if (count == 0) count = 1;
1468 if (terminal->row >= terminal->margin_top &&
1469 terminal->row < terminal->margin_bottom)
1471 top = terminal->margin_top;
1472 terminal->margin_top = terminal->row;
1473 terminal_scroll(terminal, 0 - count);
1474 terminal->margin_top = top;
1475 } else if (terminal->row == terminal->margin_bottom) {
1476 memset(terminal_get_row(terminal, terminal->row),
1477 0, terminal->data_pitch);
1478 attr_init(terminal_get_attr_row(terminal, terminal->row),
1479 terminal->curr_attr, terminal->width);
1483 count = set[0] ? args[0] : 1;
1484 if (count == 0) count = 1;
1485 if (terminal->row >= terminal->margin_top &&
1486 terminal->row < terminal->margin_bottom)
1488 top = terminal->margin_top;
1489 terminal->margin_top = terminal->row;
1490 terminal_scroll(terminal, count);
1491 terminal->margin_top = top;
1492 } else if (terminal->row == terminal->margin_bottom) {
1493 memset(terminal_get_row(terminal, terminal->row),
1494 0, terminal->data_pitch);
1498 count = set[0] ? args[0] : 1;
1499 if (count == 0) count = 1;
1500 terminal_shift_line(terminal, 0 - count);
1503 terminal_scroll(terminal, set[0] ? args[0] : 1);
1506 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1509 count = set[0] ? args[0] : 1;
1510 if (count == 0) count = 1;
1511 if ((terminal->column + count) > terminal->width)
1512 count = terminal->width - terminal->column;
1513 row = terminal_get_row(terminal, terminal->row);
1514 attr_row = terminal_get_attr_row(terminal, terminal->row);
1515 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1516 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1519 count = set[0] ? args[0] : 1;
1520 if (count == 0) count = 1;
1521 while (count > 0 && terminal->column >= 0) {
1522 if (terminal->tab_ruler[terminal->column]) count--;
1528 y = set[0] ? args[0] : 1;
1529 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1531 terminal->column = y - 1;
1534 count = set[0] ? args[0] : 1;
1535 if (count == 0) count = 1;
1536 if (terminal->last_char.byte[0])
1537 for (i = 0; i < count; i++)
1538 handle_char(terminal, terminal->last_char);
1539 terminal->last_char.byte[0] = 0;
1541 case 'c': /* Primary DA */
1542 terminal_write(terminal, "\e[?6c", 5);
1545 x = set[0] ? args[0] : 1;
1546 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1548 terminal->row = x - 1;
1551 if (!set[0] || args[0] == 0) {
1552 terminal->tab_ruler[terminal->column] = 0;
1553 } else if (args[0] == 3) {
1554 memset(terminal->tab_ruler, 0, terminal->width);
1558 for(i = 0; i < 10 && set[i]; i++) {
1559 handle_term_parameter(terminal, args[i], 1);
1563 for(i = 0; i < 10 && set[i]; i++) {
1564 handle_term_parameter(terminal, args[i], 0);
1568 for(i = 0; i < 10; i++) {
1569 if (i <= 7 && set[i] && set[i + 1] &&
1570 set[i + 2] && args[i + 1] == 5)
1572 if (args[i] == 38) {
1573 handle_sgr(terminal, args[i + 2] + 256);
1575 } else if (args[i] == 48) {
1576 handle_sgr(terminal, args[i + 2] + 512);
1581 handle_sgr(terminal, args[i]);
1583 handle_sgr(terminal, 0);
1591 i = set[0] ? args[0] : 0;
1592 if (i == 0 || i == 5) {
1593 terminal_write(terminal, "\e[0n", 4);
1594 } else if (i == 6) {
1595 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1596 terminal->origin_mode ?
1597 terminal->row+terminal->margin_top : terminal->row+1,
1598 terminal->column+1);
1599 terminal_write(terminal, response, strlen(response));
1604 terminal->margin_top = 0;
1605 terminal->margin_bottom = terminal->height-1;
1607 terminal->column = 0;
1609 top = (set[0] ? args[0] : 1) - 1;
1611 (top >= terminal->height ? terminal->height - 1 : top);
1612 bottom = (set[1] ? args[1] : 1) - 1;
1613 bottom = bottom < 0 ? 0 :
1614 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1616 terminal->margin_top = top;
1617 terminal->margin_bottom = bottom;
1619 terminal->margin_top = 0;
1620 terminal->margin_bottom = terminal->height-1;
1622 if(terminal->origin_mode)
1623 terminal->row = terminal->margin_top;
1626 terminal->column = 0;
1630 terminal->saved_row = terminal->row;
1631 terminal->saved_column = terminal->column;
1633 case 't': /* windowOps */
1636 case 4: /* resize px */
1637 if (set[1] && set[2]) {
1638 widget_schedule_resize(terminal->widget,
1642 case 8: /* resize ch */
1643 if (set[1] && set[2]) {
1644 terminal_resize(terminal, args[2], args[1]);
1647 case 13: /* report position */
1648 widget_get_allocation(terminal->widget, &allocation);
1649 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1650 allocation.x, allocation.y);
1651 terminal_write(terminal, response, strlen(response));
1653 case 14: /* report px */
1654 widget_get_allocation(terminal->widget, &allocation);
1655 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1656 allocation.height, allocation.width);
1657 terminal_write(terminal, response, strlen(response));
1659 case 18: /* report ch */
1660 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1661 terminal->height, terminal->width);
1662 terminal_write(terminal, response, strlen(response));
1664 case 21: /* report title */
1665 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1666 window_get_title(terminal->window));
1667 terminal_write(terminal, response, strlen(response));
1671 terminal_resize(terminal, terminal->width, args[0]);
1673 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1677 terminal->row = terminal->saved_row;
1678 terminal->column = terminal->saved_column;
1681 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
1687 handle_non_csi_escape(struct terminal *terminal, char code)
1692 if(terminal->row < terminal->margin_top) {
1693 terminal->row = terminal->margin_top;
1694 terminal_scroll(terminal, -1);
1698 terminal->column = 0;
1702 if(terminal->row > terminal->margin_bottom) {
1703 terminal->row = terminal->margin_bottom;
1704 terminal_scroll(terminal, +1);
1708 terminal_init(terminal);
1711 terminal->tab_ruler[terminal->column] = 1;
1713 case '7': /* DECSC */
1714 terminal->saved_row = terminal->row;
1715 terminal->saved_column = terminal->column;
1716 terminal->saved_attr = terminal->curr_attr;
1717 terminal->saved_origin_mode = terminal->origin_mode;
1718 terminal->saved_cs = terminal->cs;
1719 terminal->saved_g0 = terminal->g0;
1720 terminal->saved_g1 = terminal->g1;
1722 case '8': /* DECRC */
1723 terminal->row = terminal->saved_row;
1724 terminal->column = terminal->saved_column;
1725 terminal->curr_attr = terminal->saved_attr;
1726 terminal->origin_mode = terminal->saved_origin_mode;
1727 terminal->cs = terminal->saved_cs;
1728 terminal->g0 = terminal->saved_g0;
1729 terminal->g1 = terminal->saved_g1;
1731 case '=': /* DECPAM */
1732 terminal->key_mode = KM_APPLICATION;
1734 case '>': /* DECPNM */
1735 terminal->key_mode = KM_NORMAL;
1738 fprintf(stderr, "Unknown escape code: %c\n", code);
1744 handle_special_escape(struct terminal *terminal, char special, char code)
1748 if (special == '#') {
1751 /* fill with 'E', no cheap way to do this */
1752 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1753 numChars = terminal->width * terminal->height;
1754 for(i = 0; i < numChars; i++) {
1755 terminal->data[i].byte[0] = 'E';
1759 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1762 } else if (special == '(' || special == ')') {
1766 terminal->g0 = CS_SPECIAL;
1768 terminal->g1 = CS_SPECIAL;
1772 terminal->g0 = CS_UK;
1774 terminal->g1 = CS_UK;
1778 terminal->g0 = CS_US;
1780 terminal->g1 = CS_US;
1783 fprintf(stderr, "Unknown character set %c\n", code);
1787 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1792 handle_sgr(struct terminal *terminal, int code)
1796 terminal->curr_attr = terminal->color_scheme->default_attr;
1799 terminal->curr_attr.a |= ATTRMASK_BOLD;
1800 if (terminal->curr_attr.fg < 8)
1801 terminal->curr_attr.fg += 8;
1804 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1807 terminal->curr_attr.a |= ATTRMASK_BLINK;
1810 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1815 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1816 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1817 terminal->curr_attr.fg -= 8;
1820 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1823 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1827 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1830 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1833 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1836 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1839 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1842 if(code >= 30 && code <= 37) {
1843 terminal->curr_attr.fg = code - 30;
1844 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1845 terminal->curr_attr.fg += 8;
1846 } else if(code >= 40 && code <= 47) {
1847 terminal->curr_attr.bg = code - 40;
1848 } else if (code >= 90 && code <= 97) {
1849 terminal->curr_attr.fg = code - 90 + 8;
1850 } else if (code >= 100 && code <= 107) {
1851 terminal->curr_attr.bg = code - 100 + 8;
1852 } else if(code >= 256 && code < 512) {
1853 terminal->curr_attr.fg = code - 256;
1854 } else if(code >= 512 && code < 768) {
1855 terminal->curr_attr.bg = code - 512;
1857 fprintf(stderr, "Unknown SGR code: %d\n", code);
1863 /* Returns 1 if c was special, otherwise 0 */
1865 handle_special_char(struct terminal *terminal, char c)
1867 union utf8_char *row;
1868 struct attr *attr_row;
1870 row = terminal_get_row(terminal, terminal->row);
1871 attr_row = terminal_get_attr_row(terminal, terminal->row);
1875 terminal->column = 0;
1878 if (terminal->mode & MODE_LF_NEWLINE) {
1879 terminal->column = 0;
1885 if (terminal->row > terminal->margin_bottom) {
1886 terminal->row = terminal->margin_bottom;
1887 terminal_scroll(terminal, +1);
1892 while (terminal->column < terminal->width) {
1893 if (terminal->mode & MODE_IRM)
1894 terminal_shift_line(terminal, +1);
1896 if (row[terminal->column].byte[0] == '\0') {
1897 row[terminal->column].byte[0] = ' ';
1898 row[terminal->column].byte[1] = '\0';
1899 attr_row[terminal->column] = terminal->curr_attr;
1903 if (terminal->tab_ruler[terminal->column]) break;
1905 if (terminal->column >= terminal->width) {
1906 terminal->column = terminal->width - 1;
1911 if (terminal->column >= terminal->width) {
1912 terminal->column = terminal->width - 2;
1913 } else if (terminal->column > 0) {
1915 } else if (terminal->mode & MODE_AUTOWRAP) {
1916 terminal->column = terminal->width - 1;
1918 if (terminal->row < terminal->margin_top) {
1919 terminal->row = terminal->margin_top;
1920 terminal_scroll(terminal, -1);
1928 case '\x0E': /* SO */
1929 terminal->cs = terminal->g1;
1931 case '\x0F': /* SI */
1932 terminal->cs = terminal->g0;
1944 handle_char(struct terminal *terminal, union utf8_char utf8)
1946 union utf8_char *row;
1947 struct attr *attr_row;
1949 if (handle_special_char(terminal, utf8.byte[0])) return;
1951 apply_char_set(terminal->cs, &utf8);
1953 /* There are a whole lot of non-characters, control codes,
1954 * and formatting codes that should probably be ignored,
1956 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1961 /* Some of these non-characters should be translated, e.g.: */
1962 if (utf8.byte[0] < 32) {
1963 utf8.byte[0] = utf8.byte[0] + 64;
1966 /* handle right margin effects */
1967 if (terminal->column >= terminal->width) {
1968 if (terminal->mode & MODE_AUTOWRAP) {
1969 terminal->column = 0;
1971 if (terminal->row > terminal->margin_bottom) {
1972 terminal->row = terminal->margin_bottom;
1973 terminal_scroll(terminal, +1);
1980 row = terminal_get_row(terminal, terminal->row);
1981 attr_row = terminal_get_attr_row(terminal, terminal->row);
1983 if (terminal->mode & MODE_IRM)
1984 terminal_shift_line(terminal, +1);
1985 row[terminal->column] = utf8;
1986 attr_row[terminal->column++] = terminal->curr_attr;
1988 if (terminal->row + terminal->start + 1 > terminal->end)
1989 terminal->end = terminal->row + terminal->start + 1;
1990 if (terminal->end == terminal->buffer_height)
1991 terminal->log_size = terminal->buffer_height;
1992 else if (terminal->log_size < terminal->buffer_height)
1993 terminal->log_size = terminal->end;
1995 /* cursor jump for wide character. */
1997 row[terminal->column++].ch = 0x200B; /* space glyph */
1999 if (utf8.ch != terminal->last_char.ch)
2000 terminal->last_char = utf8;
2004 escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
2008 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
2009 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
2010 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
2011 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
2012 else len = 1; /* Invalid, cannot happen */
2014 if (terminal->escape_length + len <= MAX_ESCAPE) {
2015 for (i = 0; i < len; i++)
2016 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
2017 terminal->escape_length += len;
2018 } else if (terminal->escape_length < MAX_ESCAPE) {
2019 terminal->escape[terminal->escape_length++] = 0;
2024 terminal_data(struct terminal *terminal, const char *data, size_t length)
2027 union utf8_char utf8;
2028 enum utf8_state parser_state;
2030 for (i = 0; i < length; i++) {
2032 utf8_next_char(&terminal->state_machine, data[i]);
2033 switch(parser_state) {
2034 case utf8state_accept:
2035 utf8.ch = terminal->state_machine.s.ch;
2037 case utf8state_reject:
2038 /* the unicode replacement character */
2039 utf8.byte[0] = 0xEF;
2040 utf8.byte[1] = 0xBF;
2041 utf8.byte[2] = 0xBD;
2042 utf8.byte[3] = 0x00;
2048 /* assume escape codes never use non-ASCII characters */
2049 switch (terminal->state) {
2050 case escape_state_escape:
2051 escape_append_utf8(terminal, utf8);
2052 switch (utf8.byte[0]) {
2054 terminal->state = escape_state_dcs;
2057 terminal->state = escape_state_csi;
2060 terminal->state = escape_state_osc;
2064 case ')': /* special */
2065 terminal->state = escape_state_special;
2067 case '^': /* PM (not implemented) */
2068 case '_': /* APC (not implemented) */
2069 terminal->state = escape_state_ignore;
2072 terminal->state = escape_state_normal;
2073 handle_non_csi_escape(terminal, utf8.byte[0]);
2077 case escape_state_csi:
2078 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
2080 } else if (utf8.byte[0] == '?') {
2081 terminal->escape_flags |= ESC_FLAG_WHAT;
2082 } else if (utf8.byte[0] == '>') {
2083 terminal->escape_flags |= ESC_FLAG_GT;
2084 } else if (utf8.byte[0] == '!') {
2085 terminal->escape_flags |= ESC_FLAG_BANG;
2086 } else if (utf8.byte[0] == '$') {
2087 terminal->escape_flags |= ESC_FLAG_CASH;
2088 } else if (utf8.byte[0] == '\'') {
2089 terminal->escape_flags |= ESC_FLAG_SQUOTE;
2090 } else if (utf8.byte[0] == '"') {
2091 terminal->escape_flags |= ESC_FLAG_DQUOTE;
2092 } else if (utf8.byte[0] == ' ') {
2093 terminal->escape_flags |= ESC_FLAG_SPACE;
2095 escape_append_utf8(terminal, utf8);
2096 if (terminal->escape_length >= MAX_ESCAPE)
2097 terminal->state = escape_state_normal;
2100 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
2101 utf8.byte[0] == '`')
2103 terminal->state = escape_state_normal;
2104 handle_escape(terminal);
2108 case escape_state_inner_escape:
2109 if (utf8.byte[0] == '\\') {
2110 terminal->state = escape_state_normal;
2111 if (terminal->outer_state == escape_state_dcs) {
2112 handle_dcs(terminal);
2113 } else if (terminal->outer_state == escape_state_osc) {
2114 handle_osc(terminal);
2116 } else if (utf8.byte[0] == '\e') {
2117 terminal->state = terminal->outer_state;
2118 escape_append_utf8(terminal, utf8);
2119 if (terminal->escape_length >= MAX_ESCAPE)
2120 terminal->state = escape_state_normal;
2122 terminal->state = terminal->outer_state;
2123 if (terminal->escape_length < MAX_ESCAPE)
2124 terminal->escape[terminal->escape_length++] = '\e';
2125 escape_append_utf8(terminal, utf8);
2126 if (terminal->escape_length >= MAX_ESCAPE)
2127 terminal->state = escape_state_normal;
2130 case escape_state_dcs:
2131 case escape_state_osc:
2132 case escape_state_ignore:
2133 if (utf8.byte[0] == '\e') {
2134 terminal->outer_state = terminal->state;
2135 terminal->state = escape_state_inner_escape;
2136 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
2137 terminal->state = escape_state_normal;
2138 handle_osc(terminal);
2140 escape_append_utf8(terminal, utf8);
2141 if (terminal->escape_length >= MAX_ESCAPE)
2142 terminal->state = escape_state_normal;
2145 case escape_state_special:
2146 escape_append_utf8(terminal, utf8);
2147 terminal->state = escape_state_normal;
2148 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2149 handle_special_escape(terminal, terminal->escape[1],
2157 /* this is valid, because ASCII characters are never used to
2158 * introduce a multibyte sequence in UTF-8 */
2159 if (utf8.byte[0] == '\e') {
2160 terminal->state = escape_state_escape;
2161 terminal->outer_state = escape_state_normal;
2162 terminal->escape[0] = '\e';
2163 terminal->escape_length = 1;
2164 terminal->escape_flags = 0;
2166 handle_char(terminal, utf8);
2170 window_schedule_redraw(terminal->window);
2174 data_source_target(void *data,
2175 struct wl_data_source *source, const char *mime_type)
2177 fprintf(stderr, "data_source_target, %s\n", mime_type);
2181 data_source_send(void *data,
2182 struct wl_data_source *source,
2183 const char *mime_type, int32_t fd)
2185 struct terminal *terminal = data;
2187 terminal_send_selection(terminal, fd);
2191 data_source_cancelled(void *data, struct wl_data_source *source)
2193 wl_data_source_destroy(source);
2196 static const struct wl_data_source_listener data_source_listener = {
2199 data_source_cancelled
2202 static const char text_mime_type[] = "text/plain;charset=utf-8";
2205 data_handler(struct window *window,
2206 struct input *input,
2207 float x, float y, const char **types, void *data)
2209 int i, has_text = 0;
2213 for (i = 0; types[i]; i++)
2214 if (strcmp(types[i], text_mime_type) == 0)
2218 input_accept(input, NULL);
2220 input_accept(input, text_mime_type);
2225 drop_handler(struct window *window, struct input *input,
2226 int32_t x, int32_t y, void *data)
2228 struct terminal *terminal = data;
2230 input_receive_drag_data_to_fd(input, text_mime_type, terminal->master);
2234 fullscreen_handler(struct window *window, void *data)
2236 struct terminal *terminal = data;
2238 window_set_fullscreen(window, !window_is_fullscreen(terminal->window));
2242 close_handler(void *data)
2244 struct terminal *terminal = data;
2246 terminal_destroy(terminal);
2250 terminal_copy(struct terminal *terminal, struct input *input)
2252 terminal->selection =
2253 display_create_data_source(terminal->display);
2254 wl_data_source_offer(terminal->selection,
2255 "text/plain;charset=utf-8");
2256 wl_data_source_add_listener(terminal->selection,
2257 &data_source_listener, terminal);
2258 input_set_selection(input, terminal->selection,
2259 display_get_serial(terminal->display));
2263 terminal_paste(struct terminal *terminal, struct input *input)
2265 input_receive_selection_data_to_fd(input,
2266 "text/plain;charset=utf-8",
2272 terminal_new_instance(struct terminal *terminal)
2274 struct terminal *new_terminal;
2276 new_terminal = terminal_create(terminal->display);
2277 if (terminal_run(new_terminal, option_shell))
2278 terminal_destroy(new_terminal);
2282 handle_bound_key(struct terminal *terminal,
2283 struct input *input, uint32_t sym, uint32_t time)
2287 /* Cut selection; terminal doesn't do cut, fall
2288 * through to copy. */
2290 terminal_copy(terminal, input);
2293 terminal_paste(terminal, input);
2296 terminal_new_instance(terminal);
2300 if (!terminal->scrolling)
2301 terminal->saved_start = terminal->start;
2302 if (terminal->start == terminal->end - terminal->log_size)
2305 terminal->scrolling = 1;
2308 terminal->selection_start_row++;
2309 terminal->selection_end_row++;
2310 widget_schedule_redraw(terminal->widget);
2314 if (!terminal->scrolling)
2315 terminal->saved_start = terminal->start;
2317 if (terminal->start == terminal->saved_start)
2320 terminal->scrolling = 1;
2323 terminal->selection_start_row--;
2324 terminal->selection_end_row--;
2325 widget_schedule_redraw(terminal->widget);
2334 key_handler(struct window *window, struct input *input, uint32_t time,
2335 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2338 struct terminal *terminal = data;
2339 char ch[MAX_RESPONSE];
2340 uint32_t modifiers, serial;
2341 int ret, len = 0, d;
2342 bool convert_utf8 = true;
2344 modifiers = input_get_modifiers(input);
2345 if ((modifiers & MOD_CONTROL_MASK) &&
2346 (modifiers & MOD_SHIFT_MASK) &&
2347 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2348 handle_bound_key(terminal, input, sym, time))
2351 /* Map keypad symbols to 'normal' equivalents before processing */
2353 case XKB_KEY_KP_Space:
2354 sym = XKB_KEY_space;
2356 case XKB_KEY_KP_Tab:
2359 case XKB_KEY_KP_Enter:
2360 sym = XKB_KEY_Return;
2362 case XKB_KEY_KP_Left:
2368 case XKB_KEY_KP_Right:
2369 sym = XKB_KEY_Right;
2371 case XKB_KEY_KP_Down:
2374 case XKB_KEY_KP_Equal:
2375 sym = XKB_KEY_equal;
2377 case XKB_KEY_KP_Multiply:
2378 sym = XKB_KEY_asterisk;
2380 case XKB_KEY_KP_Add:
2383 case XKB_KEY_KP_Separator:
2384 /* Note this is actually locale-dependent and should mostly be
2385 * a comma. But leave it as period until we one day start
2386 * doing the right thing. */
2387 sym = XKB_KEY_period;
2389 case XKB_KEY_KP_Subtract:
2390 sym = XKB_KEY_minus;
2392 case XKB_KEY_KP_Decimal:
2393 sym = XKB_KEY_period;
2395 case XKB_KEY_KP_Divide:
2396 sym = XKB_KEY_slash;
2408 sym = (sym - XKB_KEY_KP_0) + XKB_KEY_0;
2415 case XKB_KEY_BackSpace:
2416 if (modifiers & MOD_ALT_MASK)
2421 case XKB_KEY_Linefeed:
2424 case XKB_KEY_Scroll_Lock:
2425 case XKB_KEY_Sys_Req:
2426 case XKB_KEY_Escape:
2427 ch[len++] = sym & 0x7f;
2430 case XKB_KEY_Return:
2431 if (terminal->mode & MODE_LF_NEWLINE) {
2439 case XKB_KEY_Shift_L:
2440 case XKB_KEY_Shift_R:
2441 case XKB_KEY_Control_L:
2442 case XKB_KEY_Control_R:
2445 case XKB_KEY_Meta_L:
2446 case XKB_KEY_Meta_R:
2447 case XKB_KEY_Super_L:
2448 case XKB_KEY_Super_R:
2449 case XKB_KEY_Hyper_L:
2450 case XKB_KEY_Hyper_R:
2453 case XKB_KEY_Insert:
2454 len = function_key_response('[', 2, modifiers, '~', ch);
2456 case XKB_KEY_Delete:
2457 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2460 len = function_key_response('[', 3, modifiers, '~', ch);
2463 case XKB_KEY_Page_Up:
2464 len = function_key_response('[', 5, modifiers, '~', ch);
2466 case XKB_KEY_Page_Down:
2467 len = function_key_response('[', 6, modifiers, '~', ch);
2470 len = function_key_response('O', 1, modifiers, 'P', ch);
2473 len = function_key_response('O', 1, modifiers, 'Q', ch);
2476 len = function_key_response('O', 1, modifiers, 'R', ch);
2479 len = function_key_response('O', 1, modifiers, 'S', ch);
2482 len = function_key_response('[', 15, modifiers, '~', ch);
2485 len = function_key_response('[', 17, modifiers, '~', ch);
2488 len = function_key_response('[', 18, modifiers, '~', ch);
2491 len = function_key_response('[', 19, modifiers, '~', ch);
2494 len = function_key_response('[', 20, modifiers, '~', ch);
2497 len = function_key_response('[', 21, modifiers, '~', ch);
2500 len = function_key_response('[', 24, modifiers, '~', ch);
2503 /* Handle special keys with alternate mappings */
2504 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2505 if (len != 0) break;
2507 if (modifiers & MOD_CONTROL_MASK) {
2508 if (sym >= '3' && sym <= '7')
2509 sym = (sym & 0x1f) + 8;
2511 if (!((sym >= '!' && sym <= '/') ||
2512 (sym >= '8' && sym <= '?') ||
2513 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2514 else if (sym == '2') sym = 0x00;
2515 else if (sym == '/') sym = 0x1F;
2516 else if (sym == '8' || sym == '?') sym = 0x7F;
2518 if (modifiers & MOD_ALT_MASK) {
2519 if (terminal->mode & MODE_ALT_SENDS_ESC) {
2523 convert_utf8 = false;
2528 (!convert_utf8 && sym < 256)) {
2531 ret = xkb_keysym_to_utf8(sym, ch + len,
2532 MAX_RESPONSE - len);
2535 "Warning: buffer too small to encode "
2536 "UTF8 character\n");
2544 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
2545 if (terminal->scrolling) {
2546 d = terminal->saved_start - terminal->start;
2548 terminal->selection_start_row -= d;
2549 terminal->selection_end_row -= d;
2550 terminal->start = terminal->saved_start;
2551 terminal->scrolling = 0;
2552 widget_schedule_redraw(terminal->widget);
2555 terminal_write(terminal, ch, len);
2557 /* Hide cursor, except if this was coming from a
2558 * repeating key press. */
2559 serial = display_get_serial(terminal->display);
2560 if (terminal->hide_cursor_serial != serial) {
2561 input_set_pointer_image(input, CURSOR_BLANK);
2562 terminal->hide_cursor_serial = serial;
2568 keyboard_focus_handler(struct window *window,
2569 struct input *device, void *data)
2571 struct terminal *terminal = data;
2573 window_schedule_redraw(terminal->window);
2576 static int wordsep(int ch)
2578 const char extra[] = "-,./?%&#:_=+@~";
2583 return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
2587 recompute_selection(struct terminal *terminal)
2589 struct rectangle allocation;
2590 int col, x, width, height;
2591 int start_row, end_row;
2592 int word_start, eol;
2593 int side_margin, top_margin;
2596 union utf8_char *data;
2598 cw = terminal->average_width;
2599 ch = terminal->extents.height;
2600 widget_get_allocation(terminal->widget, &allocation);
2601 width = terminal->width * cw;
2602 height = terminal->height * ch;
2603 side_margin = allocation.x + (allocation.width - width) / 2;
2604 top_margin = allocation.y + (allocation.height - height) / 2;
2606 start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1;
2607 end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1;
2609 if (start_row < end_row ||
2610 (start_row == end_row &&
2611 terminal->selection_start_x < terminal->selection_end_x)) {
2612 terminal->selection_start_row = start_row;
2613 terminal->selection_end_row = end_row;
2614 start_x = terminal->selection_start_x;
2615 end_x = terminal->selection_end_x;
2617 terminal->selection_start_row = end_row;
2618 terminal->selection_end_row = start_row;
2619 start_x = terminal->selection_end_x;
2620 end_x = terminal->selection_start_x;
2624 if (terminal->selection_start_row < 0) {
2625 terminal->selection_start_row = 0;
2626 terminal->selection_start_col = 0;
2628 x = side_margin + cw / 2;
2629 data = terminal_get_row(terminal,
2630 terminal->selection_start_row);
2632 for (col = 0; col < terminal->width; col++, x += cw) {
2633 if (col == 0 || wordsep(data[col - 1].ch))
2635 if (data[col].ch != 0)
2641 switch (terminal->dragging) {
2643 terminal->selection_start_col = 0;
2646 terminal->selection_start_col = word_start;
2649 terminal->selection_start_col = col;
2654 if (terminal->selection_end_row >= terminal->height) {
2655 terminal->selection_end_row = terminal->height;
2656 terminal->selection_end_col = 0;
2658 x = side_margin + cw / 2;
2659 data = terminal_get_row(terminal, terminal->selection_end_row);
2660 for (col = 0; col < terminal->width; col++, x += cw) {
2661 if (terminal->dragging == SELECT_CHAR && end_x < x)
2663 if (terminal->dragging == SELECT_WORD &&
2664 end_x < x && wordsep(data[col].ch))
2667 terminal->selection_end_col = col;
2670 if (terminal->selection_end_col != terminal->selection_start_col ||
2671 terminal->selection_start_row != terminal->selection_end_row) {
2672 col = terminal->selection_end_col;
2673 if (col > 0 && data[col - 1].ch == 0)
2674 terminal->selection_end_col = terminal->width;
2675 data = terminal_get_row(terminal, terminal->selection_start_row);
2676 if (data[terminal->selection_start_col].ch == 0)
2677 terminal->selection_start_col = eol;
2684 menu_func(void *data, struct input *input, int index)
2686 struct window *window = data;
2687 struct terminal *terminal = window_get_user_data(window);
2689 fprintf(stderr, "picked entry %d\n", index);
2693 terminal_new_instance(terminal);
2696 terminal_copy(terminal, input);
2699 terminal_paste(terminal, input);
2705 show_menu(struct terminal *terminal, struct input *input, uint32_t time)
2708 static const char *entries[] = {
2709 "Open Terminal", "Copy", "Paste"
2712 input_get_position(input, &x, &y);
2713 window_show_menu(terminal->display, input, time, terminal->window,
2714 x - 10, y - 10, menu_func,
2715 entries, ARRAY_LENGTH(entries));
2719 click_handler(struct widget *widget, struct terminal *terminal,
2720 struct input *input, int32_t x, int32_t y,
2723 if (time - terminal->click_time < 500)
2724 terminal->click_count++;
2726 terminal->click_count = 1;
2728 terminal->click_time = time;
2729 terminal->dragging = (terminal->click_count - 1) % 3 + SELECT_CHAR;
2731 terminal->selection_end_x = terminal->selection_start_x = x;
2732 terminal->selection_end_y = terminal->selection_start_y = y;
2733 if (recompute_selection(terminal))
2734 widget_schedule_redraw(widget);
2738 button_handler(struct widget *widget,
2739 struct input *input, uint32_t time,
2741 enum wl_pointer_button_state state, void *data)
2743 struct terminal *terminal = data;
2748 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2749 input_get_position(input, &x, &y);
2750 click_handler(widget, terminal, input, x, y, time);
2752 terminal->dragging = SELECT_NONE;
2757 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
2758 show_menu(terminal, input, time);
2764 enter_handler(struct widget *widget,
2765 struct input *input, float x, float y, void *data)
2767 return CURSOR_IBEAM;
2771 motion_handler(struct widget *widget,
2772 struct input *input, uint32_t time,
2773 float x, float y, void *data)
2775 struct terminal *terminal = data;
2777 if (terminal->dragging) {
2778 input_get_position(input,
2779 &terminal->selection_end_x,
2780 &terminal->selection_end_y);
2782 if (recompute_selection(terminal))
2783 widget_schedule_redraw(widget);
2786 return CURSOR_IBEAM;
2789 /* This magnitude is chosen rather arbitrarily. Really, the scrolling
2790 * should happen on a (fractional) pixel basis, not a line basis. */
2791 #define AXIS_UNITS_PER_LINE 256
2794 axis_handler(struct widget *widget,
2795 struct input *input, uint32_t time,
2800 struct terminal *terminal = data;
2803 if (axis != WL_POINTER_AXIS_VERTICAL_SCROLL)
2806 terminal->smooth_scroll += value;
2807 lines = terminal->smooth_scroll / AXIS_UNITS_PER_LINE;
2808 terminal->smooth_scroll -= lines * AXIS_UNITS_PER_LINE;
2811 if (terminal->scrolling) {
2812 if ((uint32_t)lines > terminal->saved_start - terminal->start)
2813 lines = terminal->saved_start - terminal->start;
2817 } else if (lines < 0) {
2818 uint32_t neg_lines = -lines;
2820 if (neg_lines > terminal->log_size + terminal->start - terminal->end)
2821 lines = terminal->end - terminal->log_size - terminal->start;
2825 if (!terminal->scrolling)
2826 terminal->saved_start = terminal->start;
2827 terminal->scrolling = 1;
2829 terminal->start += lines;
2830 terminal->row -= lines;
2831 terminal->selection_start_row -= lines;
2832 terminal->selection_end_row -= lines;
2834 widget_schedule_redraw(widget);
2839 output_handler(struct window *window, struct output *output, int enter,
2843 window_set_buffer_transform(window, output_get_transform(output));
2844 window_set_buffer_scale(window, window_get_output_scale(window));
2845 window_schedule_redraw(window);
2849 touch_down_handler(struct widget *widget, struct input *input,
2850 uint32_t serial, uint32_t time, int32_t id,
2851 float x, float y, void *data)
2853 struct terminal *terminal = data;
2856 click_handler(widget, terminal, input, x, y, time);
2860 touch_up_handler(struct widget *widget, struct input *input,
2861 uint32_t serial, uint32_t time, int32_t id, void *data)
2863 struct terminal *terminal = data;
2866 terminal->dragging = SELECT_NONE;
2870 touch_motion_handler(struct widget *widget, struct input *input,
2871 uint32_t time, int32_t id, float x, float y, void *data)
2873 struct terminal *terminal = data;
2875 if (terminal->dragging &&
2877 terminal->selection_end_x = (int)x;
2878 terminal->selection_end_y = (int)y;
2880 if (recompute_selection(terminal))
2881 widget_schedule_redraw(widget);
2886 #define howmany(x, y) (((x) + ((y) - 1)) / (y))
2889 static struct terminal *
2890 terminal_create(struct display *display)
2892 struct terminal *terminal;
2893 cairo_surface_t *surface;
2895 cairo_text_extents_t text_extents;
2897 terminal = xzalloc(sizeof *terminal);
2898 terminal->color_scheme = &DEFAULT_COLORS;
2899 terminal_init(terminal);
2900 terminal->margin_top = 0;
2901 terminal->margin_bottom = -1;
2902 terminal->window = window_create(display);
2903 terminal->widget = window_frame_create(terminal->window, terminal);
2904 terminal->title = xstrdup("Wayland Terminal");
2905 window_set_title(terminal->window, terminal->title);
2906 widget_set_transparent(terminal->widget, 0);
2908 init_state_machine(&terminal->state_machine);
2909 init_color_table(terminal);
2911 terminal->display = display;
2912 terminal->margin = 5;
2913 terminal->buffer_height = 1024;
2916 window_set_user_data(terminal->window, terminal);
2917 window_set_key_handler(terminal->window, key_handler);
2918 window_set_keyboard_focus_handler(terminal->window,
2919 keyboard_focus_handler);
2920 window_set_fullscreen_handler(terminal->window, fullscreen_handler);
2921 window_set_output_handler(terminal->window, output_handler);
2922 window_set_close_handler(terminal->window, close_handler);
2923 window_set_state_changed_handler(terminal->window, state_changed_handler);
2925 window_set_data_handler(terminal->window, data_handler);
2926 window_set_drop_handler(terminal->window, drop_handler);
2928 widget_set_redraw_handler(terminal->widget, redraw_handler);
2929 widget_set_resize_handler(terminal->widget, resize_handler);
2930 widget_set_button_handler(terminal->widget, button_handler);
2931 widget_set_enter_handler(terminal->widget, enter_handler);
2932 widget_set_motion_handler(terminal->widget, motion_handler);
2933 widget_set_axis_handler(terminal->widget, axis_handler);
2934 widget_set_touch_up_handler(terminal->widget, touch_up_handler);
2935 widget_set_touch_down_handler(terminal->widget, touch_down_handler);
2936 widget_set_touch_motion_handler(terminal->widget, touch_motion_handler);
2938 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2939 cr = cairo_create(surface);
2940 cairo_set_font_size(cr, option_font_size);
2941 cairo_select_font_face (cr, option_font,
2942 CAIRO_FONT_SLANT_NORMAL,
2943 CAIRO_FONT_WEIGHT_BOLD);
2944 terminal->font_bold = cairo_get_scaled_font (cr);
2945 cairo_scaled_font_reference(terminal->font_bold);
2947 cairo_select_font_face (cr, option_font,
2948 CAIRO_FONT_SLANT_NORMAL,
2949 CAIRO_FONT_WEIGHT_NORMAL);
2950 terminal->font_normal = cairo_get_scaled_font (cr);
2951 cairo_scaled_font_reference(terminal->font_normal);
2953 cairo_font_extents(cr, &terminal->extents);
2955 /* Compute the average ascii glyph width */
2956 cairo_text_extents(cr, TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS,
2958 terminal->average_width = howmany
2959 (text_extents.width,
2960 strlen(TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS));
2961 terminal->average_width = ceil(terminal->average_width);
2964 cairo_surface_destroy(surface);
2966 terminal_resize(terminal, 20, 5); /* Set minimum size first */
2967 terminal_resize(terminal, 80, 25);
2969 wl_list_insert(terminal_list.prev, &terminal->link);
2975 terminal_destroy(struct terminal *terminal)
2977 display_unwatch_fd(terminal->display, terminal->master);
2978 window_destroy(terminal->window);
2979 close(terminal->master);
2980 wl_list_remove(&terminal->link);
2982 if (wl_list_empty(&terminal_list))
2983 display_exit(terminal->display);
2985 free(terminal->title);
2990 io_handler(struct task *task, uint32_t events)
2992 struct terminal *terminal =
2993 container_of(task, struct terminal, io_task);
2997 if (events & EPOLLHUP) {
2998 terminal_destroy(terminal);
3002 len = read(terminal->master, buffer, sizeof buffer);
3004 terminal_destroy(terminal);
3006 terminal_data(terminal, buffer, len);
3010 terminal_run(struct terminal *terminal, const char *path)
3015 pid = forkpty(&master, NULL, NULL, NULL);
3017 setenv("TERM", option_term, 1);
3018 setenv("COLORTERM", option_term, 1);
3019 if (execl(path, path, NULL)) {
3020 printf("exec failed: %m\n");
3023 } else if (pid < 0) {
3024 fprintf(stderr, "failed to fork and create pty (%m).\n");
3028 terminal->master = master;
3029 fcntl(master, F_SETFL, O_NONBLOCK);
3030 terminal->io_task.run = io_handler;
3031 display_watch_fd(terminal->display, terminal->master,
3032 EPOLLIN | EPOLLHUP, &terminal->io_task);
3034 if (option_fullscreen)
3035 window_set_fullscreen(terminal->window, 1);
3037 terminal_resize(terminal, 80, 24);
3042 static const struct weston_option terminal_options[] = {
3043 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
3044 { WESTON_OPTION_STRING, "font", 0, &option_font },
3045 { WESTON_OPTION_INTEGER, "font-size", 0, &option_font_size },
3046 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
3049 int main(int argc, char *argv[])
3052 struct terminal *terminal;
3053 struct weston_config *config;
3054 struct weston_config_section *s;
3056 /* as wcwidth is locale-dependent,
3057 wcwidth needs setlocale call to function properly. */
3058 setlocale(LC_ALL, "");
3060 option_shell = getenv("SHELL");
3062 option_shell = "/bin/bash";
3064 config = weston_config_parse("weston.ini");
3065 s = weston_config_get_section(config, "terminal", NULL, NULL);
3066 weston_config_section_get_string(s, "font", &option_font, "mono");
3067 weston_config_section_get_int(s, "font-size", &option_font_size, 14);
3068 weston_config_section_get_string(s, "term", &option_term, "xterm");
3069 weston_config_destroy(config);
3071 if (parse_options(terminal_options,
3072 ARRAY_LENGTH(terminal_options), &argc, argv) > 1) {
3073 printf("Usage: %s [OPTIONS]\n"
3074 " --fullscreen or -f\n"
3076 " --font-size=SIZE\n"
3077 " --shell=NAME\n", argv[0]);
3081 d = display_create(&argc, argv);
3083 fprintf(stderr, "failed to create display: %m\n");
3087 wl_list_init(&terminal_list);
3088 terminal = terminal_create(d);
3089 if (terminal_run(terminal, option_shell))