2 * Copyright © 2008 Kristian Høgsberg
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
37 #include <sys/epoll.h>
41 #include <wayland-client.h>
43 #include "../shared/config-parser.h"
46 static int option_fullscreen;
47 static char *option_font;
48 static int option_font_size;
49 static char *option_term;
50 static char *option_shell;
52 static struct wl_list terminal_list;
54 static struct terminal *
55 terminal_create(struct display *display);
57 terminal_destroy(struct terminal *terminal);
59 terminal_run(struct terminal *terminal, const char *path);
61 #define TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS \
65 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
67 "abcdefghijklmnopqrstuvwxyz" \
71 #define MOD_SHIFT 0x01
75 #define ATTRMASK_BOLD 0x01
76 #define ATTRMASK_UNDERLINE 0x02
77 #define ATTRMASK_BLINK 0x04
78 #define ATTRMASK_INVERSE 0x08
79 #define ATTRMASK_CONCEALED 0x10
82 #define MAX_RESPONSE 256
83 #define MAX_ESCAPE 255
86 #define MODE_SHOW_CURSOR 0x00000001
87 #define MODE_INVERSE 0x00000002
88 #define MODE_AUTOWRAP 0x00000004
89 #define MODE_AUTOREPEAT 0x00000008
90 #define MODE_LF_NEWLINE 0x00000010
91 #define MODE_IRM 0x00000020
92 #define MODE_DELETE_SENDS_DEL 0x00000040
93 #define MODE_ALT_SENDS_ESC 0x00000080
96 unsigned char byte[4];
109 struct utf8_state_machine {
110 enum utf8_state state;
117 init_state_machine(struct utf8_state_machine *machine)
119 machine->state = utf8state_start;
124 static enum utf8_state
125 utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
127 switch(machine->state) {
128 case utf8state_start:
129 case utf8state_accept:
130 case utf8state_reject:
133 if(c == 0xC0 || c == 0xC1) {
134 /* overlong encoding, reject */
135 machine->state = utf8state_reject;
136 } else if((c & 0x80) == 0) {
137 /* single byte, accept */
138 machine->s.byte[machine->len++] = c;
139 machine->state = utf8state_accept;
140 machine->unicode = c;
141 } else if((c & 0xC0) == 0x80) {
142 /* parser out of sync, ignore byte */
143 machine->state = utf8state_start;
144 } else if((c & 0xE0) == 0xC0) {
145 /* start of two byte sequence */
146 machine->s.byte[machine->len++] = c;
147 machine->state = utf8state_expect1;
148 machine->unicode = c & 0x1f;
149 } else if((c & 0xF0) == 0xE0) {
150 /* start of three byte sequence */
151 machine->s.byte[machine->len++] = c;
152 machine->state = utf8state_expect2;
153 machine->unicode = c & 0x0f;
154 } else if((c & 0xF8) == 0xF0) {
155 /* start of four byte sequence */
156 machine->s.byte[machine->len++] = c;
157 machine->state = utf8state_expect3;
158 machine->unicode = c & 0x07;
160 /* overlong encoding, reject */
161 machine->state = utf8state_reject;
164 case utf8state_expect3:
165 machine->s.byte[machine->len++] = c;
166 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
167 if((c & 0xC0) == 0x80) {
168 /* all good, continue */
169 machine->state = utf8state_expect2;
171 /* missing extra byte, reject */
172 machine->state = utf8state_reject;
175 case utf8state_expect2:
176 machine->s.byte[machine->len++] = c;
177 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
178 if((c & 0xC0) == 0x80) {
179 /* all good, continue */
180 machine->state = utf8state_expect1;
182 /* missing extra byte, reject */
183 machine->state = utf8state_reject;
186 case utf8state_expect1:
187 machine->s.byte[machine->len++] = c;
188 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
189 if((c & 0xC0) == 0x80) {
190 /* all good, accept */
191 machine->state = utf8state_accept;
193 /* missing extra byte, reject */
194 machine->state = utf8state_reject;
198 machine->state = utf8state_reject;
202 return machine->state;
206 get_unicode(union utf8_char utf8)
208 struct utf8_state_machine machine;
211 init_state_machine(&machine);
212 for (i = 0; i < 4; i++) {
213 utf8_next_char(&machine, utf8.byte[i]);
214 if (machine.state == utf8state_accept ||
215 machine.state == utf8state_reject)
219 if (machine.state == utf8state_reject)
222 return machine.unicode;
226 is_wide(union utf8_char utf8)
228 uint32_t unichar = get_unicode(utf8);
229 return wcwidth(unichar) > 1;
233 union utf8_char match;
234 union utf8_char replace;
236 /* Set last char_sub match to NULL char */
237 typedef struct char_sub *character_set;
239 struct char_sub CS_US[] = {
242 static struct char_sub CS_UK[] = {
243 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
246 static struct char_sub CS_SPECIAL[] = {
247 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond: ♦ */
248 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell: ▒ */
249 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT: ␉ */
250 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF: ␌ */
251 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR: ␍ */
252 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF: ␊ */
253 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree: ° */
254 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus: ± */
255 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL:  */
256 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT: ␋ */
257 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB: ┘ */
258 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT: ┐ */
259 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT: ┌ */
260 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_LB: └ */
261 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS: ┼ */
262 {{{'o', 0, }}, {{0xE2, 0x8E, 0xBA, 0}}}, /* Horiz. Scan Line 1: ⎺ */
263 {{{'p', 0, }}, {{0xE2, 0x8E, 0xBB, 0}}}, /* Horiz. Scan Line 3: ⎻ */
264 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* Horiz. Scan Line 5: ─ */
265 {{{'r', 0, }}, {{0xE2, 0x8E, 0xBC, 0}}}, /* Horiz. Scan Line 7: ⎼ */
266 {{{'s', 0, }}, {{0xE2, 0x8E, 0xBD, 0}}}, /* Horiz. Scan Line 9: ⎽ */
267 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR: ├ */
268 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL: ┤ */
269 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU: ┴ */
270 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD: ┬ */
271 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V: │ */
272 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE: ≤ */
273 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE: ≥ */
274 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI: π */
275 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ: ≠ */
276 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
277 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT: ⋅ */
282 apply_char_set(character_set cs, union utf8_char *utf8)
286 while (cs[i].match.byte[0]) {
287 if ((*utf8).ch == cs[i].match.ch) {
288 *utf8 = cs[i].replace;
301 /* Set last key_sub sym to NULL */
302 typedef struct key_map *keyboard_mode;
304 static struct key_map KM_NORMAL[] = {
305 { XKB_KEY_Left, 1, '[', 'D' },
306 { XKB_KEY_Right, 1, '[', 'C' },
307 { XKB_KEY_Up, 1, '[', 'A' },
308 { XKB_KEY_Down, 1, '[', 'B' },
309 { XKB_KEY_Home, 1, '[', 'H' },
310 { XKB_KEY_End, 1, '[', 'F' },
313 static struct key_map KM_APPLICATION[] = {
314 { XKB_KEY_Left, 1, 'O', 'D' },
315 { XKB_KEY_Right, 1, 'O', 'C' },
316 { XKB_KEY_Up, 1, 'O', 'A' },
317 { XKB_KEY_Down, 1, 'O', 'B' },
318 { XKB_KEY_Home, 1, 'O', 'H' },
319 { XKB_KEY_End, 1, 'O', 'F' },
320 { XKB_KEY_KP_Enter, 1, 'O', 'M' },
321 { XKB_KEY_KP_Multiply, 1, 'O', 'j' },
322 { XKB_KEY_KP_Add, 1, 'O', 'k' },
323 { XKB_KEY_KP_Separator, 1, 'O', 'l' },
324 { XKB_KEY_KP_Subtract, 1, 'O', 'm' },
325 { XKB_KEY_KP_Divide, 1, 'O', 'o' },
330 function_key_response(char escape, int num, uint32_t modifiers,
331 char code, char *response)
336 if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
337 if (modifiers & MOD_ALT_MASK) mod_num |= 2;
338 if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
341 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
342 num, mod_num + 1, code);
343 else if (code != '~')
344 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
347 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
350 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
354 /* returns the number of bytes written into response,
355 * which must have room for MAX_RESPONSE bytes */
357 apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
363 while (mode[i].sym) {
365 if (sym == map.sym) {
366 len = function_key_response(map.escape, map.num,
376 struct terminal_color { double r, g, b, a; };
378 unsigned char fg, bg;
379 char a; /* attributes format:
382 char s; /* in selection */
384 struct color_scheme {
385 struct terminal_color palette[16];
387 struct attr default_attr;
391 attr_init(struct attr *data_attr, struct attr attr, int n)
394 for (i = 0; i < n; i++) {
400 escape_state_normal = 0,
405 escape_state_inner_escape,
410 #define ESC_FLAG_WHAT 0x01
411 #define ESC_FLAG_GT 0x02
412 #define ESC_FLAG_BANG 0x04
413 #define ESC_FLAG_CASH 0x08
414 #define ESC_FLAG_SQUOTE 0x10
415 #define ESC_FLAG_DQUOTE 0x20
416 #define ESC_FLAG_SPACE 0x40
426 struct window *window;
427 struct widget *widget;
428 struct display *display;
429 union utf8_char *data;
432 struct attr *data_attr;
433 struct attr curr_attr;
436 char saved_origin_mode;
437 struct attr saved_attr;
438 union utf8_char last_char;
439 int margin_top, margin_bottom;
440 character_set cs, g0, g1;
441 character_set saved_cs, saved_g0, saved_g1;
442 keyboard_mode key_mode;
443 int data_pitch, attr_pitch; /* The width in bytes of a line */
444 int width, height, row, column, max_width;
445 uint32_t buffer_height;
446 uint32_t start, end, saved_start, log_size;
447 int saved_row, saved_column;
449 int send_cursor_position;
452 char escape[MAX_ESCAPE+1];
454 enum escape_state state;
455 enum escape_state outer_state;
457 struct utf8_state_machine state_machine;
459 struct color_scheme *color_scheme;
460 struct terminal_color color_table[256];
461 cairo_font_extents_t extents;
462 double average_width;
463 cairo_scaled_font_t *font_normal, *font_bold;
464 uint32_t hide_cursor_serial;
466 struct wl_data_source *selection;
467 uint32_t button_time;
468 int dragging, click_count;
469 int selection_start_x, selection_start_y;
470 int selection_end_x, selection_end_y;
471 int selection_start_row, selection_start_col;
472 int selection_end_row, selection_end_col;
476 /* Create default tab stops, every 8 characters */
478 terminal_init_tabs(struct terminal *terminal)
482 while (i < terminal->width) {
484 terminal->tab_ruler[i] = 1;
486 terminal->tab_ruler[i] = 0;
492 terminal_init(struct terminal *terminal)
494 terminal->curr_attr = terminal->color_scheme->default_attr;
495 terminal->origin_mode = 0;
496 terminal->mode = MODE_SHOW_CURSOR |
502 terminal->column = 0;
504 terminal->g0 = CS_US;
505 terminal->g1 = CS_US;
506 terminal->cs = terminal->g0;
507 terminal->key_mode = KM_NORMAL;
509 terminal->saved_g0 = terminal->g0;
510 terminal->saved_g1 = terminal->g1;
511 terminal->saved_cs = terminal->cs;
513 terminal->saved_attr = terminal->curr_attr;
514 terminal->saved_origin_mode = terminal->origin_mode;
515 terminal->saved_row = terminal->row;
516 terminal->saved_column = terminal->column;
518 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
522 init_color_table(struct terminal *terminal)
525 struct terminal_color *color_table = terminal->color_table;
527 for (c = 0; c < 256; c ++) {
529 color_table[c] = terminal->color_scheme->palette[c];
530 } else if (c < 232) {
532 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
533 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
534 color_table[c].r = ((double)(r % 6) / 6.0);
535 color_table[c].a = 1.0;
537 r = (c - 232) * 10 + 8;
538 color_table[c].r = ((double) r) / 256.0;
539 color_table[c].g = color_table[c].r;
540 color_table[c].b = color_table[c].r;
541 color_table[c].a = 1.0;
546 static union utf8_char *
547 terminal_get_row(struct terminal *terminal, int row)
551 index = (row + terminal->start) & (terminal->buffer_height - 1);
553 return (void *) terminal->data + index * terminal->data_pitch;
557 terminal_get_attr_row(struct terminal *terminal, int row)
561 index = (row + terminal->start) & (terminal->buffer_height - 1);
563 return (void *) terminal->data_attr + index * terminal->attr_pitch;
572 terminal_decode_attr(struct terminal *terminal, int row, int col,
573 union decoded_attr *decoded)
576 int foreground, background, tmp;
579 if (((row == terminal->selection_start_row &&
580 col >= terminal->selection_start_col) ||
581 row > terminal->selection_start_row) &&
582 ((row == terminal->selection_end_row &&
583 col < terminal->selection_end_col) ||
584 row < terminal->selection_end_row))
587 /* get the attributes for this character cell */
588 attr = terminal_get_attr_row(terminal, row)[col];
589 if ((attr.a & ATTRMASK_INVERSE) ||
591 ((terminal->mode & MODE_SHOW_CURSOR) &&
592 window_has_focus(terminal->window) && terminal->row == row &&
593 terminal->column == col)) {
594 foreground = attr.bg;
595 background = attr.fg;
596 if (attr.a & ATTRMASK_BOLD) {
597 if (foreground <= 16) foreground |= 0x08;
598 if (background <= 16) background &= 0x07;
601 foreground = attr.fg;
602 background = attr.bg;
605 if (terminal->mode & MODE_INVERSE) {
607 foreground = background;
609 if (attr.a & ATTRMASK_BOLD) {
610 if (foreground <= 16) foreground |= 0x08;
611 if (background <= 16) background &= 0x07;
615 decoded->attr.fg = foreground;
616 decoded->attr.bg = background;
617 decoded->attr.a = attr.a;
622 terminal_scroll_buffer(struct terminal *terminal, int d)
626 terminal->start += d;
629 for (i = 0; i < d; i++) {
630 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
631 attr_init(terminal_get_attr_row(terminal, i),
632 terminal->curr_attr, terminal->width);
635 for (i = terminal->height - d; i < terminal->height; i++) {
636 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
637 attr_init(terminal_get_attr_row(terminal, i),
638 terminal->curr_attr, terminal->width);
642 terminal->selection_start_row -= d;
643 terminal->selection_end_row -= d;
647 terminal_scroll_window(struct terminal *terminal, int d)
651 int from_row, to_row;
653 // scrolling range is inclusive
654 window_height = terminal->margin_bottom - terminal->margin_top + 1;
655 d = d % (window_height + 1);
658 to_row = terminal->margin_bottom;
659 from_row = terminal->margin_bottom - d;
661 for (i = 0; i < (window_height - d); i++) {
662 memcpy(terminal_get_row(terminal, to_row - i),
663 terminal_get_row(terminal, from_row - i),
664 terminal->data_pitch);
665 memcpy(terminal_get_attr_row(terminal, to_row - i),
666 terminal_get_attr_row(terminal, from_row - i),
667 terminal->attr_pitch);
669 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
670 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
671 attr_init(terminal_get_attr_row(terminal, i),
672 terminal->curr_attr, terminal->width);
675 to_row = terminal->margin_top;
676 from_row = terminal->margin_top + d;
678 for (i = 0; i < (window_height - d); i++) {
679 memcpy(terminal_get_row(terminal, to_row + i),
680 terminal_get_row(terminal, from_row + i),
681 terminal->data_pitch);
682 memcpy(terminal_get_attr_row(terminal, to_row + i),
683 terminal_get_attr_row(terminal, from_row + i),
684 terminal->attr_pitch);
686 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
687 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
688 attr_init(terminal_get_attr_row(terminal, i),
689 terminal->curr_attr, terminal->width);
695 terminal_scroll(struct terminal *terminal, int d)
697 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
698 terminal_scroll_buffer(terminal, d);
700 terminal_scroll_window(terminal, d);
704 terminal_shift_line(struct terminal *terminal, int d)
706 union utf8_char *row;
707 struct attr *attr_row;
709 row = terminal_get_row(terminal, terminal->row);
710 attr_row = terminal_get_attr_row(terminal, terminal->row);
712 if ((terminal->width + d) <= terminal->column)
713 d = terminal->column + 1 - terminal->width;
714 if ((terminal->column + d) >= terminal->width)
715 d = terminal->width - terminal->column - 1;
719 memmove(&row[terminal->column],
720 &row[terminal->column + d],
721 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
722 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
723 (terminal->width - terminal->column - d) * sizeof(struct attr));
724 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
725 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
727 memmove(&row[terminal->column + d], &row[terminal->column],
728 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
729 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
730 (terminal->width - terminal->column - d) * sizeof(struct attr));
731 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
732 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
737 terminal_resize_cells(struct terminal *terminal,
738 int width, int height)
740 union utf8_char *data;
741 struct attr *data_attr;
743 int data_pitch, attr_pitch;
744 int i, l, total_rows;
745 uint32_t d, uheight = height;
746 struct rectangle allocation;
749 if (uheight > terminal->buffer_height)
750 height = terminal->buffer_height;
752 if (terminal->width == width && terminal->height == height)
755 if (terminal->data && width <= terminal->max_width) {
757 if (height < terminal->height && height <= terminal->row)
758 d = terminal->height - height;
759 else if (height > terminal->height &&
760 terminal->height - 1 == terminal->row) {
761 d = terminal->height - height;
762 if (terminal->log_size < uheight)
763 d = -terminal->start;
766 terminal->start += d;
769 terminal->max_width = width;
770 data_pitch = width * sizeof(union utf8_char);
771 data = zalloc(data_pitch * terminal->buffer_height);
772 attr_pitch = width * sizeof(struct attr);
773 data_attr = malloc(attr_pitch * terminal->buffer_height);
774 tab_ruler = zalloc(width);
775 attr_init(data_attr, terminal->curr_attr,
776 width * terminal->buffer_height);
778 if (terminal->data && terminal->data_attr) {
779 if (width > terminal->width)
784 if (terminal->height > height) {
786 i = 1 + terminal->row - height;
788 terminal->start += i;
789 terminal->row = terminal->row - i;
792 total_rows = terminal->height;
795 for (i = 0; i < total_rows; i++) {
796 memcpy(&data[width * i],
797 terminal_get_row(terminal, i),
798 l * sizeof(union utf8_char));
799 memcpy(&data_attr[width * i],
800 terminal_get_attr_row(terminal, i),
801 l * sizeof(struct attr));
804 free(terminal->data);
805 free(terminal->data_attr);
806 free(terminal->tab_ruler);
809 terminal->data_pitch = data_pitch;
810 terminal->attr_pitch = attr_pitch;
811 terminal->data = data;
812 terminal->data_attr = data_attr;
813 terminal->tab_ruler = tab_ruler;
814 terminal_init_tabs(terminal);
818 terminal->margin_bottom =
819 height - (terminal->height - terminal->margin_bottom);
820 terminal->width = width;
821 terminal->height = height;
823 /* Update the window size */
824 ws.ws_row = terminal->height;
825 ws.ws_col = terminal->width;
826 widget_get_allocation(terminal->widget, &allocation);
827 ws.ws_xpixel = allocation.width;
828 ws.ws_ypixel = allocation.height;
829 ioctl(terminal->master, TIOCSWINSZ, &ws);
833 resize_handler(struct widget *widget,
834 int32_t width, int32_t height, void *data)
836 struct terminal *terminal = data;
837 int32_t columns, rows, m;
839 m = 2 * terminal->margin;
840 columns = (width - m) / (int32_t) terminal->average_width;
841 rows = (height - m) / (int32_t) terminal->extents.height;
843 if (!window_is_fullscreen(terminal->window) &&
844 !window_is_maximized(terminal->window)) {
845 width = columns * terminal->average_width + m;
846 height = rows * terminal->extents.height + m;
847 widget_set_size(terminal->widget, width, height);
850 terminal_resize_cells(terminal, columns, rows);
854 terminal_resize(struct terminal *terminal, int columns, int rows)
856 int32_t width, height, m;
858 if (window_is_fullscreen(terminal->window) ||
859 window_is_maximized(terminal->window))
862 m = 2 * terminal->margin;
863 width = columns * terminal->average_width + m;
864 height = rows * terminal->extents.height + m;
866 window_frame_set_child_size(terminal->widget, width, height);
869 struct color_scheme DEFAULT_COLORS = {
871 {0, 0, 0, 1}, /* black */
872 {0.66, 0, 0, 1}, /* red */
873 {0 , 0.66, 0, 1}, /* green */
874 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
875 {0 , 0 , 0.66, 1}, /* blue */
876 {0.66, 0 , 0.66, 1}, /* magenta */
877 {0, 0.66, 0.66, 1}, /* cyan */
878 {0.66, 0.66, 0.66, 1}, /* light grey */
879 {0.22, 0.33, 0.33, 1}, /* dark grey */
880 {1, 0.33, 0.33, 1}, /* high red */
881 {0.33, 1, 0.33, 1}, /* high green */
882 {1, 1, 0.33, 1}, /* high yellow */
883 {0.33, 0.33, 1, 1}, /* high blue */
884 {1, 0.33, 1, 1}, /* high magenta */
885 {0.33, 1, 1, 1}, /* high cyan */
886 {1, 1, 1, 1} /* white */
888 0, /* black border */
889 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
893 terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
895 cairo_set_source_rgba(cr,
896 terminal->color_table[index].r,
897 terminal->color_table[index].g,
898 terminal->color_table[index].b,
899 terminal->color_table[index].a);
903 terminal_send_selection(struct terminal *terminal, int fd)
906 union utf8_char *p_row;
907 union decoded_attr attr;
911 fp = fdopen(fd, "w");
916 for (row = 0; row < terminal->height; row++) {
917 p_row = terminal_get_row(terminal, row);
918 for (col = 0; col < terminal->width; col++) {
919 if (p_row[col].ch == 0x200B) /* space glyph */
921 /* get the attributes for this character cell */
922 terminal_decode_attr(terminal, row, col, &attr);
925 len = strnlen((char *) p_row[col].byte, 4);
927 fwrite(p_row[col].byte, 1, len, fp);
928 if (len == 0 || col == terminal->width - 1) {
929 fwrite("\n", 1, 1, fp);
938 struct terminal *terminal;
941 union decoded_attr attr;
942 cairo_glyph_t glyphs[256], *g;
946 glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
948 run->terminal = terminal;
950 run->g = run->glyphs;
956 glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
958 cairo_scaled_font_t *font;
960 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
961 (attr.key != run->attr.key)) {
962 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
963 font = run->terminal->font_bold;
965 font = run->terminal->font_normal;
966 cairo_set_scaled_font(run->cr, font);
967 terminal_set_color(run->terminal, run->cr,
970 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
971 cairo_show_glyphs (run->cr, run->glyphs, run->count);
972 run->g = run->glyphs;
979 glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
982 cairo_scaled_font_t *font;
984 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
986 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
987 font = run->terminal->font_bold;
989 font = run->terminal->font_normal;
991 cairo_move_to(run->cr, x, y);
992 cairo_scaled_font_text_to_glyphs (font, x, y,
994 &run->g, &num_glyphs,
996 run->g += num_glyphs;
997 run->count += num_glyphs;
1002 redraw_handler(struct widget *widget, void *data)
1004 struct terminal *terminal = data;
1005 struct rectangle allocation;
1007 int top_margin, side_margin;
1008 int row, col, cursor_x, cursor_y;
1009 union utf8_char *p_row;
1010 union decoded_attr attr;
1012 cairo_surface_t *surface;
1014 struct glyph_run run;
1015 cairo_font_extents_t extents;
1016 double average_width;
1017 double unichar_width;
1019 surface = window_get_surface(terminal->window);
1020 widget_get_allocation(terminal->widget, &allocation);
1021 cr = widget_cairo_create(terminal->widget);
1022 cairo_rectangle(cr, allocation.x, allocation.y,
1023 allocation.width, allocation.height);
1025 cairo_push_group(cr);
1027 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1028 terminal_set_color(terminal, cr, terminal->color_scheme->border);
1031 cairo_set_scaled_font(cr, terminal->font_normal);
1033 extents = terminal->extents;
1034 average_width = terminal->average_width;
1035 side_margin = (allocation.width - terminal->width * average_width) / 2;
1036 top_margin = (allocation.height - terminal->height * extents.height) / 2;
1038 cairo_set_line_width(cr, 1.0);
1039 cairo_translate(cr, allocation.x + side_margin,
1040 allocation.y + top_margin);
1041 /* paint the background */
1042 for (row = 0; row < terminal->height; row++) {
1043 p_row = terminal_get_row(terminal, row);
1044 for (col = 0; col < terminal->width; col++) {
1045 /* get the attributes for this character cell */
1046 terminal_decode_attr(terminal, row, col, &attr);
1048 if (attr.attr.bg == terminal->color_scheme->border)
1051 if (is_wide(p_row[col]))
1052 unichar_width = 2 * average_width;
1054 unichar_width = average_width;
1056 terminal_set_color(terminal, cr, attr.attr.bg);
1057 cairo_move_to(cr, col * average_width,
1058 row * extents.height);
1059 cairo_rel_line_to(cr, unichar_width, 0);
1060 cairo_rel_line_to(cr, 0, extents.height);
1061 cairo_rel_line_to(cr, -unichar_width, 0);
1062 cairo_close_path(cr);
1067 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1069 /* paint the foreground */
1070 glyph_run_init(&run, terminal, cr);
1071 for (row = 0; row < terminal->height; row++) {
1072 p_row = terminal_get_row(terminal, row);
1073 for (col = 0; col < terminal->width; col++) {
1074 /* get the attributes for this character cell */
1075 terminal_decode_attr(terminal, row, col, &attr);
1077 glyph_run_flush(&run, attr);
1079 text_x = col * average_width;
1080 text_y = extents.ascent + row * extents.height;
1081 if (attr.attr.a & ATTRMASK_UNDERLINE) {
1082 terminal_set_color(terminal, cr, attr.attr.fg);
1083 cairo_move_to(cr, text_x, (double)text_y + 1.5);
1084 cairo_line_to(cr, text_x + average_width, (double) text_y + 1.5);
1088 glyph_run_add(&run, text_x, text_y, &p_row[col]);
1093 glyph_run_flush(&run, attr);
1095 if ((terminal->mode & MODE_SHOW_CURSOR) &&
1096 !window_has_focus(terminal->window)) {
1099 cairo_set_line_width(cr, 1);
1100 cairo_move_to(cr, terminal->column * average_width + d,
1101 terminal->row * extents.height + d);
1102 cairo_rel_line_to(cr, average_width - 2 * d, 0);
1103 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
1104 cairo_rel_line_to(cr, -average_width + 2 * d, 0);
1105 cairo_close_path(cr);
1110 cairo_pop_group_to_source(cr);
1113 cairo_surface_destroy(surface);
1115 if (terminal->send_cursor_position) {
1116 cursor_x = side_margin + allocation.x +
1117 terminal->column * average_width;
1118 cursor_y = top_margin + allocation.y +
1119 terminal->row * extents.height;
1120 window_set_text_cursor_position(terminal->window,
1121 cursor_x, cursor_y);
1122 terminal->send_cursor_position = 0;
1127 terminal_write(struct terminal *terminal, const char *data, size_t length)
1129 if (write(terminal->master, data, length) < 0)
1131 terminal->send_cursor_position = 1;
1135 terminal_data(struct terminal *terminal, const char *data, size_t length);
1138 handle_char(struct terminal *terminal, union utf8_char utf8);
1141 handle_sgr(struct terminal *terminal, int code);
1144 handle_term_parameter(struct terminal *terminal, int code, int sr)
1148 if (terminal->escape_flags & ESC_FLAG_WHAT) {
1150 case 1: /* DECCKM */
1151 if (sr) terminal->key_mode = KM_APPLICATION;
1152 else terminal->key_mode = KM_NORMAL;
1154 case 2: /* DECANM */
1155 /* No VT52 support yet */
1156 terminal->g0 = CS_US;
1157 terminal->g1 = CS_US;
1158 terminal->cs = terminal->g0;
1160 case 3: /* DECCOLM */
1162 terminal_resize(terminal, 132, 24);
1164 terminal_resize(terminal, 80, 24);
1166 /* set columns, but also home cursor and clear screen */
1167 terminal->row = 0; terminal->column = 0;
1168 for (i = 0; i < terminal->height; i++) {
1169 memset(terminal_get_row(terminal, i),
1170 0, terminal->data_pitch);
1171 attr_init(terminal_get_attr_row(terminal, i),
1172 terminal->curr_attr, terminal->width);
1175 case 5: /* DECSCNM */
1176 if (sr) terminal->mode |= MODE_INVERSE;
1177 else terminal->mode &= ~MODE_INVERSE;
1180 terminal->origin_mode = sr;
1181 if (terminal->origin_mode)
1182 terminal->row = terminal->margin_top;
1185 terminal->column = 0;
1187 case 7: /* DECAWM */
1188 if (sr) terminal->mode |= MODE_AUTOWRAP;
1189 else terminal->mode &= ~MODE_AUTOWRAP;
1191 case 8: /* DECARM */
1192 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1193 else terminal->mode &= ~MODE_AUTOREPEAT;
1195 case 12: /* Very visible cursor (CVVIS) */
1196 /* FIXME: What do we do here. */
1199 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1200 else terminal->mode &= ~MODE_SHOW_CURSOR;
1202 case 1034: /* smm/rmm, meta mode on/off */
1205 case 1037: /* deleteSendsDel */
1206 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1207 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1209 case 1039: /* altSendsEscape */
1210 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1211 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1213 case 1049: /* rmcup/smcup, alternate screen */
1214 /* Ignore. Should be possible to implement,
1215 * but it's kind of annoying. */
1218 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1224 if (sr) terminal->mode |= MODE_IRM;
1225 else terminal->mode &= ~MODE_IRM;
1228 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1229 else terminal->mode &= ~MODE_LF_NEWLINE;
1232 fprintf(stderr, "Unknown parameter: %d\n", code);
1239 handle_dcs(struct terminal *terminal)
1244 handle_osc(struct terminal *terminal)
1249 terminal->escape[terminal->escape_length++] = '\0';
1250 p = &terminal->escape[2];
1251 code = strtol(p, &p, 10);
1255 case 0: /* Icon name and window title */
1256 case 1: /* Icon label */
1257 case 2: /* Window title*/
1258 window_set_title(terminal->window, p);
1260 case 7: /* shell cwd as uri */
1263 fprintf(stderr, "Unknown OSC escape code %d, text %s\n",
1270 handle_escape(struct terminal *terminal)
1272 union utf8_char *row;
1273 struct attr *attr_row;
1275 int i, count, x, y, top, bottom;
1276 int args[10], set[10] = { 0, };
1277 char response[MAX_RESPONSE] = {0, };
1278 struct rectangle allocation;
1280 terminal->escape[terminal->escape_length++] = '\0';
1282 p = &terminal->escape[2];
1283 while ((isdigit(*p) || *p == ';') && i < 10) {
1292 args[i] = strtol(p, &p, 10);
1299 count = set[0] ? args[0] : 1;
1300 if (count == 0) count = 1;
1301 terminal_shift_line(terminal, count);
1304 count = set[0] ? args[0] : 1;
1305 if (count == 0) count = 1;
1306 if (terminal->row - count >= terminal->margin_top)
1307 terminal->row -= count;
1309 terminal->row = terminal->margin_top;
1312 count = set[0] ? args[0] : 1;
1313 if (count == 0) count = 1;
1314 if (terminal->row + count <= terminal->margin_bottom)
1315 terminal->row += count;
1317 terminal->row = terminal->margin_bottom;
1320 count = set[0] ? args[0] : 1;
1321 if (count == 0) count = 1;
1322 if ((terminal->column + count) < terminal->width)
1323 terminal->column += count;
1325 terminal->column = terminal->width - 1;
1328 count = set[0] ? args[0] : 1;
1329 if (count == 0) count = 1;
1330 if ((terminal->column - count) >= 0)
1331 terminal->column -= count;
1333 terminal->column = 0;
1336 count = set[0] ? args[0] : 1;
1337 if (terminal->row + count <= terminal->margin_bottom)
1338 terminal->row += count;
1340 terminal->row = terminal->margin_bottom;
1341 terminal->column = 0;
1344 count = set[0] ? args[0] : 1;
1345 if (terminal->row - count >= terminal->margin_top)
1346 terminal->row -= count;
1348 terminal->row = terminal->margin_top;
1349 terminal->column = 0;
1352 y = set[0] ? args[0] : 1;
1353 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1355 terminal->column = y - 1;
1359 x = (set[1] ? args[1] : 1) - 1;
1361 (x >= terminal->width ? terminal->width - 1 : x);
1363 y = (set[0] ? args[0] : 1) - 1;
1364 if (terminal->origin_mode) {
1365 y += terminal->margin_top;
1366 y = y < terminal->margin_top ? terminal->margin_top :
1367 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1370 (y >= terminal->height ? terminal->height - 1 : y);
1374 terminal->column = x;
1377 count = set[0] ? args[0] : 1;
1378 if (count == 0) count = 1;
1379 while (count > 0 && terminal->column < terminal->width) {
1380 if (terminal->tab_ruler[terminal->column]) count--;
1386 row = terminal_get_row(terminal, terminal->row);
1387 attr_row = terminal_get_attr_row(terminal, terminal->row);
1388 if (!set[0] || args[0] == 0 || args[0] > 2) {
1389 memset(&row[terminal->column],
1390 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1391 attr_init(&attr_row[terminal->column],
1392 terminal->curr_attr, terminal->width - terminal->column);
1393 for (i = terminal->row + 1; i < terminal->height; i++) {
1394 memset(terminal_get_row(terminal, i),
1395 0, terminal->data_pitch);
1396 attr_init(terminal_get_attr_row(terminal, i),
1397 terminal->curr_attr, terminal->width);
1399 } else if (args[0] == 1) {
1400 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1401 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1402 for (i = 0; i < terminal->row; i++) {
1403 memset(terminal_get_row(terminal, i),
1404 0, terminal->data_pitch);
1405 attr_init(terminal_get_attr_row(terminal, i),
1406 terminal->curr_attr, terminal->width);
1408 } else if (args[0] == 2) {
1409 /* Clear screen by scrolling contents out */
1410 terminal_scroll_buffer(terminal,
1411 terminal->end - terminal->start);
1415 row = terminal_get_row(terminal, terminal->row);
1416 attr_row = terminal_get_attr_row(terminal, terminal->row);
1417 if (!set[0] || args[0] == 0 || args[0] > 2) {
1418 memset(&row[terminal->column], 0,
1419 (terminal->width - terminal->column) * sizeof(union utf8_char));
1420 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1421 terminal->width - terminal->column);
1422 } else if (args[0] == 1) {
1423 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1424 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1425 } else if (args[0] == 2) {
1426 memset(row, 0, terminal->data_pitch);
1427 attr_init(attr_row, terminal->curr_attr, terminal->width);
1431 count = set[0] ? args[0] : 1;
1432 if (count == 0) count = 1;
1433 if (terminal->row >= terminal->margin_top &&
1434 terminal->row < terminal->margin_bottom)
1436 top = terminal->margin_top;
1437 terminal->margin_top = terminal->row;
1438 terminal_scroll(terminal, 0 - count);
1439 terminal->margin_top = top;
1440 } else if (terminal->row == terminal->margin_bottom) {
1441 memset(terminal_get_row(terminal, terminal->row),
1442 0, terminal->data_pitch);
1443 attr_init(terminal_get_attr_row(terminal, terminal->row),
1444 terminal->curr_attr, terminal->width);
1448 count = set[0] ? args[0] : 1;
1449 if (count == 0) count = 1;
1450 if (terminal->row >= terminal->margin_top &&
1451 terminal->row < terminal->margin_bottom)
1453 top = terminal->margin_top;
1454 terminal->margin_top = terminal->row;
1455 terminal_scroll(terminal, count);
1456 terminal->margin_top = top;
1457 } else if (terminal->row == terminal->margin_bottom) {
1458 memset(terminal_get_row(terminal, terminal->row),
1459 0, terminal->data_pitch);
1463 count = set[0] ? args[0] : 1;
1464 if (count == 0) count = 1;
1465 terminal_shift_line(terminal, 0 - count);
1468 terminal_scroll(terminal, set[0] ? args[0] : 1);
1471 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1474 count = set[0] ? args[0] : 1;
1475 if (count == 0) count = 1;
1476 if ((terminal->column + count) > terminal->width)
1477 count = terminal->width - terminal->column;
1478 row = terminal_get_row(terminal, terminal->row);
1479 attr_row = terminal_get_attr_row(terminal, terminal->row);
1480 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1481 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1484 count = set[0] ? args[0] : 1;
1485 if (count == 0) count = 1;
1486 while (count > 0 && terminal->column >= 0) {
1487 if (terminal->tab_ruler[terminal->column]) count--;
1493 y = set[0] ? args[0] : 1;
1494 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1496 terminal->column = y - 1;
1499 count = set[0] ? args[0] : 1;
1500 if (count == 0) count = 1;
1501 if (terminal->last_char.byte[0])
1502 for (i = 0; i < count; i++)
1503 handle_char(terminal, terminal->last_char);
1504 terminal->last_char.byte[0] = 0;
1506 case 'c': /* Primary DA */
1507 terminal_write(terminal, "\e[?6c", 5);
1510 x = set[0] ? args[0] : 1;
1511 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1513 terminal->row = x - 1;
1516 if (!set[0] || args[0] == 0) {
1517 terminal->tab_ruler[terminal->column] = 0;
1518 } else if (args[0] == 3) {
1519 memset(terminal->tab_ruler, 0, terminal->width);
1523 for(i = 0; i < 10 && set[i]; i++) {
1524 handle_term_parameter(terminal, args[i], 1);
1528 for(i = 0; i < 10 && set[i]; i++) {
1529 handle_term_parameter(terminal, args[i], 0);
1533 for(i = 0; i < 10; i++) {
1534 if (i <= 7 && set[i] && set[i + 1] &&
1535 set[i + 2] && args[i + 1] == 5)
1537 if (args[i] == 38) {
1538 handle_sgr(terminal, args[i + 2] + 256);
1540 } else if (args[i] == 48) {
1541 handle_sgr(terminal, args[i + 2] + 512);
1546 handle_sgr(terminal, args[i]);
1548 handle_sgr(terminal, 0);
1556 i = set[0] ? args[0] : 0;
1557 if (i == 0 || i == 5) {
1558 terminal_write(terminal, "\e[0n", 4);
1559 } else if (i == 6) {
1560 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1561 terminal->origin_mode ?
1562 terminal->row+terminal->margin_top : terminal->row+1,
1563 terminal->column+1);
1564 terminal_write(terminal, response, strlen(response));
1569 terminal->margin_top = 0;
1570 terminal->margin_bottom = terminal->height-1;
1572 terminal->column = 0;
1574 top = (set[0] ? args[0] : 1) - 1;
1576 (top >= terminal->height ? terminal->height - 1 : top);
1577 bottom = (set[1] ? args[1] : 1) - 1;
1578 bottom = bottom < 0 ? 0 :
1579 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1581 terminal->margin_top = top;
1582 terminal->margin_bottom = bottom;
1584 terminal->margin_top = 0;
1585 terminal->margin_bottom = terminal->height-1;
1587 if(terminal->origin_mode)
1588 terminal->row = terminal->margin_top;
1591 terminal->column = 0;
1595 terminal->saved_row = terminal->row;
1596 terminal->saved_column = terminal->column;
1598 case 't': /* windowOps */
1601 case 4: /* resize px */
1602 if (set[1] && set[2]) {
1603 widget_schedule_resize(terminal->widget,
1607 case 8: /* resize ch */
1608 if (set[1] && set[2]) {
1609 terminal_resize(terminal, args[2], args[1]);
1612 case 13: /* report position */
1613 widget_get_allocation(terminal->widget, &allocation);
1614 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1615 allocation.x, allocation.y);
1616 terminal_write(terminal, response, strlen(response));
1618 case 14: /* report px */
1619 widget_get_allocation(terminal->widget, &allocation);
1620 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1621 allocation.height, allocation.width);
1622 terminal_write(terminal, response, strlen(response));
1624 case 18: /* report ch */
1625 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1626 terminal->height, terminal->width);
1627 terminal_write(terminal, response, strlen(response));
1629 case 21: /* report title */
1630 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1631 window_get_title(terminal->window));
1632 terminal_write(terminal, response, strlen(response));
1636 terminal_resize(terminal, terminal->width, args[0]);
1638 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1642 terminal->row = terminal->saved_row;
1643 terminal->column = terminal->saved_column;
1646 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
1652 handle_non_csi_escape(struct terminal *terminal, char code)
1657 if(terminal->row < terminal->margin_top) {
1658 terminal->row = terminal->margin_top;
1659 terminal_scroll(terminal, -1);
1663 terminal->column = 0;
1667 if(terminal->row > terminal->margin_bottom) {
1668 terminal->row = terminal->margin_bottom;
1669 terminal_scroll(terminal, +1);
1673 terminal_init(terminal);
1676 terminal->tab_ruler[terminal->column] = 1;
1678 case '7': /* DECSC */
1679 terminal->saved_row = terminal->row;
1680 terminal->saved_column = terminal->column;
1681 terminal->saved_attr = terminal->curr_attr;
1682 terminal->saved_origin_mode = terminal->origin_mode;
1683 terminal->saved_cs = terminal->cs;
1684 terminal->saved_g0 = terminal->g0;
1685 terminal->saved_g1 = terminal->g1;
1687 case '8': /* DECRC */
1688 terminal->row = terminal->saved_row;
1689 terminal->column = terminal->saved_column;
1690 terminal->curr_attr = terminal->saved_attr;
1691 terminal->origin_mode = terminal->saved_origin_mode;
1692 terminal->cs = terminal->saved_cs;
1693 terminal->g0 = terminal->saved_g0;
1694 terminal->g1 = terminal->saved_g1;
1696 case '=': /* DECPAM */
1697 terminal->key_mode = KM_APPLICATION;
1699 case '>': /* DECPNM */
1700 terminal->key_mode = KM_NORMAL;
1703 fprintf(stderr, "Unknown escape code: %c\n", code);
1709 handle_special_escape(struct terminal *terminal, char special, char code)
1713 if (special == '#') {
1716 /* fill with 'E', no cheap way to do this */
1717 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1718 numChars = terminal->width * terminal->height;
1719 for(i = 0; i < numChars; i++) {
1720 terminal->data[i].byte[0] = 'E';
1724 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1727 } else if (special == '(' || special == ')') {
1731 terminal->g0 = CS_SPECIAL;
1733 terminal->g1 = CS_SPECIAL;
1737 terminal->g0 = CS_UK;
1739 terminal->g1 = CS_UK;
1743 terminal->g0 = CS_US;
1745 terminal->g1 = CS_US;
1748 fprintf(stderr, "Unknown character set %c\n", code);
1752 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1757 handle_sgr(struct terminal *terminal, int code)
1761 terminal->curr_attr = terminal->color_scheme->default_attr;
1764 terminal->curr_attr.a |= ATTRMASK_BOLD;
1765 if (terminal->curr_attr.fg < 8)
1766 terminal->curr_attr.fg += 8;
1769 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1772 terminal->curr_attr.a |= ATTRMASK_BLINK;
1775 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1780 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1781 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1782 terminal->curr_attr.fg -= 8;
1785 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1788 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1792 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1795 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1798 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1801 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1804 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1807 if(code >= 30 && code <= 37) {
1808 terminal->curr_attr.fg = code - 30;
1809 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1810 terminal->curr_attr.fg += 8;
1811 } else if(code >= 40 && code <= 47) {
1812 terminal->curr_attr.bg = code - 40;
1813 } else if (code >= 90 && code <= 97) {
1814 terminal->curr_attr.fg = code - 90 + 8;
1815 } else if (code >= 100 && code <= 107) {
1816 terminal->curr_attr.bg = code - 100 + 8;
1817 } else if(code >= 256 && code < 512) {
1818 terminal->curr_attr.fg = code - 256;
1819 } else if(code >= 512 && code < 768) {
1820 terminal->curr_attr.bg = code - 512;
1822 fprintf(stderr, "Unknown SGR code: %d\n", code);
1828 /* Returns 1 if c was special, otherwise 0 */
1830 handle_special_char(struct terminal *terminal, char c)
1832 union utf8_char *row;
1833 struct attr *attr_row;
1835 row = terminal_get_row(terminal, terminal->row);
1836 attr_row = terminal_get_attr_row(terminal, terminal->row);
1840 terminal->column = 0;
1843 if (terminal->mode & MODE_LF_NEWLINE) {
1844 terminal->column = 0;
1850 if (terminal->row + terminal->start > terminal->end)
1851 terminal->end = terminal->row + terminal->start;
1852 if (terminal->end == terminal->buffer_height)
1853 terminal->log_size = terminal->buffer_height;
1854 else if (terminal->log_size < terminal->buffer_height)
1855 terminal->log_size = terminal->end;
1857 if (terminal->row > terminal->margin_bottom) {
1858 terminal->row = terminal->margin_bottom;
1859 terminal_scroll(terminal, +1);
1864 while (terminal->column < terminal->width) {
1865 if (terminal->mode & MODE_IRM)
1866 terminal_shift_line(terminal, +1);
1868 if (row[terminal->column].byte[0] == '\0') {
1869 row[terminal->column].byte[0] = ' ';
1870 row[terminal->column].byte[1] = '\0';
1871 attr_row[terminal->column] = terminal->curr_attr;
1875 if (terminal->tab_ruler[terminal->column]) break;
1877 if (terminal->column >= terminal->width) {
1878 terminal->column = terminal->width - 1;
1883 if (terminal->column >= terminal->width) {
1884 terminal->column = terminal->width - 2;
1885 } else if (terminal->column > 0) {
1887 } else if (terminal->mode & MODE_AUTOWRAP) {
1888 terminal->column = terminal->width - 1;
1890 if (terminal->row < terminal->margin_top) {
1891 terminal->row = terminal->margin_top;
1892 terminal_scroll(terminal, -1);
1900 case '\x0E': /* SO */
1901 terminal->cs = terminal->g1;
1903 case '\x0F': /* SI */
1904 terminal->cs = terminal->g0;
1916 handle_char(struct terminal *terminal, union utf8_char utf8)
1918 union utf8_char *row;
1919 struct attr *attr_row;
1921 if (handle_special_char(terminal, utf8.byte[0])) return;
1923 apply_char_set(terminal->cs, &utf8);
1925 /* There are a whole lot of non-characters, control codes,
1926 * and formatting codes that should probably be ignored,
1928 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1933 /* Some of these non-characters should be translated, e.g.: */
1934 if (utf8.byte[0] < 32) {
1935 utf8.byte[0] = utf8.byte[0] + 64;
1938 /* handle right margin effects */
1939 if (terminal->column >= terminal->width) {
1940 if (terminal->mode & MODE_AUTOWRAP) {
1941 terminal->column = 0;
1943 if (terminal->row > terminal->margin_bottom) {
1944 terminal->row = terminal->margin_bottom;
1945 terminal_scroll(terminal, +1);
1952 row = terminal_get_row(terminal, terminal->row);
1953 attr_row = terminal_get_attr_row(terminal, terminal->row);
1955 if (terminal->mode & MODE_IRM)
1956 terminal_shift_line(terminal, +1);
1957 row[terminal->column] = utf8;
1958 attr_row[terminal->column++] = terminal->curr_attr;
1960 /* cursor jump for wide character. */
1962 row[terminal->column++].ch = 0x200B; /* space glyph */
1964 if (utf8.ch != terminal->last_char.ch)
1965 terminal->last_char = utf8;
1969 escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1973 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1974 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1975 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1976 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1977 else len = 1; /* Invalid, cannot happen */
1979 if (terminal->escape_length + len <= MAX_ESCAPE) {
1980 for (i = 0; i < len; i++)
1981 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1982 terminal->escape_length += len;
1983 } else if (terminal->escape_length < MAX_ESCAPE) {
1984 terminal->escape[terminal->escape_length++] = 0;
1989 terminal_data(struct terminal *terminal, const char *data, size_t length)
1992 union utf8_char utf8;
1993 enum utf8_state parser_state;
1995 for (i = 0; i < length; i++) {
1997 utf8_next_char(&terminal->state_machine, data[i]);
1998 switch(parser_state) {
1999 case utf8state_accept:
2000 utf8.ch = terminal->state_machine.s.ch;
2002 case utf8state_reject:
2003 /* the unicode replacement character */
2004 utf8.byte[0] = 0xEF;
2005 utf8.byte[1] = 0xBF;
2006 utf8.byte[2] = 0xBD;
2007 utf8.byte[3] = 0x00;
2013 /* assume escape codes never use non-ASCII characters */
2014 switch (terminal->state) {
2015 case escape_state_escape:
2016 escape_append_utf8(terminal, utf8);
2017 switch (utf8.byte[0]) {
2019 terminal->state = escape_state_dcs;
2022 terminal->state = escape_state_csi;
2025 terminal->state = escape_state_osc;
2029 case ')': /* special */
2030 terminal->state = escape_state_special;
2032 case '^': /* PM (not implemented) */
2033 case '_': /* APC (not implemented) */
2034 terminal->state = escape_state_ignore;
2037 terminal->state = escape_state_normal;
2038 handle_non_csi_escape(terminal, utf8.byte[0]);
2042 case escape_state_csi:
2043 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
2045 } else if (utf8.byte[0] == '?') {
2046 terminal->escape_flags |= ESC_FLAG_WHAT;
2047 } else if (utf8.byte[0] == '>') {
2048 terminal->escape_flags |= ESC_FLAG_GT;
2049 } else if (utf8.byte[0] == '!') {
2050 terminal->escape_flags |= ESC_FLAG_BANG;
2051 } else if (utf8.byte[0] == '$') {
2052 terminal->escape_flags |= ESC_FLAG_CASH;
2053 } else if (utf8.byte[0] == '\'') {
2054 terminal->escape_flags |= ESC_FLAG_SQUOTE;
2055 } else if (utf8.byte[0] == '"') {
2056 terminal->escape_flags |= ESC_FLAG_DQUOTE;
2057 } else if (utf8.byte[0] == ' ') {
2058 terminal->escape_flags |= ESC_FLAG_SPACE;
2060 escape_append_utf8(terminal, utf8);
2061 if (terminal->escape_length >= MAX_ESCAPE)
2062 terminal->state = escape_state_normal;
2065 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
2066 utf8.byte[0] == '`')
2068 terminal->state = escape_state_normal;
2069 handle_escape(terminal);
2073 case escape_state_inner_escape:
2074 if (utf8.byte[0] == '\\') {
2075 terminal->state = escape_state_normal;
2076 if (terminal->outer_state == escape_state_dcs) {
2077 handle_dcs(terminal);
2078 } else if (terminal->outer_state == escape_state_osc) {
2079 handle_osc(terminal);
2081 } else if (utf8.byte[0] == '\e') {
2082 terminal->state = terminal->outer_state;
2083 escape_append_utf8(terminal, utf8);
2084 if (terminal->escape_length >= MAX_ESCAPE)
2085 terminal->state = escape_state_normal;
2087 terminal->state = terminal->outer_state;
2088 if (terminal->escape_length < MAX_ESCAPE)
2089 terminal->escape[terminal->escape_length++] = '\e';
2090 escape_append_utf8(terminal, utf8);
2091 if (terminal->escape_length >= MAX_ESCAPE)
2092 terminal->state = escape_state_normal;
2095 case escape_state_dcs:
2096 case escape_state_osc:
2097 case escape_state_ignore:
2098 if (utf8.byte[0] == '\e') {
2099 terminal->outer_state = terminal->state;
2100 terminal->state = escape_state_inner_escape;
2101 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
2102 terminal->state = escape_state_normal;
2103 handle_osc(terminal);
2105 escape_append_utf8(terminal, utf8);
2106 if (terminal->escape_length >= MAX_ESCAPE)
2107 terminal->state = escape_state_normal;
2110 case escape_state_special:
2111 escape_append_utf8(terminal, utf8);
2112 terminal->state = escape_state_normal;
2113 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2114 handle_special_escape(terminal, terminal->escape[1],
2122 /* this is valid, because ASCII characters are never used to
2123 * introduce a multibyte sequence in UTF-8 */
2124 if (utf8.byte[0] == '\e') {
2125 terminal->state = escape_state_escape;
2126 terminal->outer_state = escape_state_normal;
2127 terminal->escape[0] = '\e';
2128 terminal->escape_length = 1;
2129 terminal->escape_flags = 0;
2131 handle_char(terminal, utf8);
2135 window_schedule_redraw(terminal->window);
2139 data_source_target(void *data,
2140 struct wl_data_source *source, const char *mime_type)
2142 fprintf(stderr, "data_source_target, %s\n", mime_type);
2146 data_source_send(void *data,
2147 struct wl_data_source *source,
2148 const char *mime_type, int32_t fd)
2150 struct terminal *terminal = data;
2152 terminal_send_selection(terminal, fd);
2156 data_source_cancelled(void *data, struct wl_data_source *source)
2158 wl_data_source_destroy(source);
2161 static const struct wl_data_source_listener data_source_listener = {
2164 data_source_cancelled
2167 static const char text_mime_type[] = "text/plain;charset=utf-8";
2170 data_handler(struct window *window,
2171 struct input *input,
2172 float x, float y, const char **types, void *data)
2174 int i, has_text = 0;
2178 for (i = 0; types[i]; i++)
2179 if (strcmp(types[i], text_mime_type) == 0)
2183 input_accept(input, NULL);
2185 input_accept(input, text_mime_type);
2190 drop_handler(struct window *window, struct input *input,
2191 int32_t x, int32_t y, void *data)
2193 struct terminal *terminal = data;
2195 input_receive_drag_data_to_fd(input, text_mime_type, terminal->master);
2199 fullscreen_handler(struct window *window, void *data)
2201 struct terminal *terminal = data;
2203 window_set_fullscreen(window, !window_is_fullscreen(terminal->window));
2207 close_handler(struct window *window, void *data)
2209 struct terminal *terminal = data;
2211 terminal_destroy(terminal);
2215 handle_bound_key(struct terminal *terminal,
2216 struct input *input, uint32_t sym, uint32_t time)
2218 struct terminal *new_terminal;
2222 /* Cut selection; terminal doesn't do cut, fall
2223 * through to copy. */
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));
2235 input_receive_selection_data_to_fd(input,
2236 "text/plain;charset=utf-8",
2242 new_terminal = terminal_create(terminal->display);
2243 if (terminal_run(new_terminal, option_shell))
2244 terminal_destroy(new_terminal);
2249 if (!terminal->scrolling)
2250 terminal->saved_start = terminal->start;
2251 if (terminal->start == terminal->end - terminal->log_size)
2254 terminal->scrolling = 1;
2257 terminal->selection_start_row++;
2258 terminal->selection_end_row++;
2259 widget_schedule_redraw(terminal->widget);
2263 if (!terminal->scrolling)
2264 terminal->saved_start = terminal->start;
2266 if (terminal->start == terminal->saved_start)
2269 terminal->scrolling = 1;
2272 terminal->selection_start_row--;
2273 terminal->selection_end_row--;
2274 widget_schedule_redraw(terminal->widget);
2283 key_handler(struct window *window, struct input *input, uint32_t time,
2284 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2287 struct terminal *terminal = data;
2288 char ch[MAX_RESPONSE];
2289 uint32_t modifiers, serial;
2290 int ret, len = 0, d;
2291 bool convert_utf8 = true;
2293 modifiers = input_get_modifiers(input);
2294 if ((modifiers & MOD_CONTROL_MASK) &&
2295 (modifiers & MOD_SHIFT_MASK) &&
2296 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2297 handle_bound_key(terminal, input, sym, time))
2300 /* Map keypad symbols to 'normal' equivalents before processing */
2302 case XKB_KEY_KP_Space:
2303 sym = XKB_KEY_space;
2305 case XKB_KEY_KP_Tab:
2308 case XKB_KEY_KP_Enter:
2309 sym = XKB_KEY_Return;
2311 case XKB_KEY_KP_Left:
2317 case XKB_KEY_KP_Right:
2318 sym = XKB_KEY_Right;
2320 case XKB_KEY_KP_Down:
2323 case XKB_KEY_KP_Equal:
2324 sym = XKB_KEY_equal;
2326 case XKB_KEY_KP_Multiply:
2327 sym = XKB_KEY_asterisk;
2329 case XKB_KEY_KP_Add:
2332 case XKB_KEY_KP_Separator:
2333 /* Note this is actually locale-dependent and should mostly be
2334 * a comma. But leave it as period until we one day start
2335 * doing the right thing. */
2336 sym = XKB_KEY_period;
2338 case XKB_KEY_KP_Subtract:
2339 sym = XKB_KEY_minus;
2341 case XKB_KEY_KP_Decimal:
2342 sym = XKB_KEY_period;
2344 case XKB_KEY_KP_Divide:
2345 sym = XKB_KEY_slash;
2357 sym = (sym - XKB_KEY_KP_0) + XKB_KEY_0;
2364 case XKB_KEY_BackSpace:
2365 if (modifiers & MOD_ALT_MASK)
2370 case XKB_KEY_Linefeed:
2373 case XKB_KEY_Scroll_Lock:
2374 case XKB_KEY_Sys_Req:
2375 case XKB_KEY_Escape:
2376 ch[len++] = sym & 0x7f;
2379 case XKB_KEY_Return:
2380 if (terminal->mode & MODE_LF_NEWLINE) {
2388 case XKB_KEY_Shift_L:
2389 case XKB_KEY_Shift_R:
2390 case XKB_KEY_Control_L:
2391 case XKB_KEY_Control_R:
2394 case XKB_KEY_Meta_L:
2395 case XKB_KEY_Meta_R:
2396 case XKB_KEY_Super_L:
2397 case XKB_KEY_Super_R:
2398 case XKB_KEY_Hyper_L:
2399 case XKB_KEY_Hyper_R:
2402 case XKB_KEY_Insert:
2403 len = function_key_response('[', 2, modifiers, '~', ch);
2405 case XKB_KEY_Delete:
2406 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2409 len = function_key_response('[', 3, modifiers, '~', ch);
2412 case XKB_KEY_Page_Up:
2413 len = function_key_response('[', 5, modifiers, '~', ch);
2415 case XKB_KEY_Page_Down:
2416 len = function_key_response('[', 6, modifiers, '~', ch);
2419 len = function_key_response('O', 1, modifiers, 'P', ch);
2422 len = function_key_response('O', 1, modifiers, 'Q', ch);
2425 len = function_key_response('O', 1, modifiers, 'R', ch);
2428 len = function_key_response('O', 1, modifiers, 'S', ch);
2431 len = function_key_response('[', 15, modifiers, '~', ch);
2434 len = function_key_response('[', 17, modifiers, '~', ch);
2437 len = function_key_response('[', 18, modifiers, '~', ch);
2440 len = function_key_response('[', 19, modifiers, '~', ch);
2443 len = function_key_response('[', 20, modifiers, '~', ch);
2446 len = function_key_response('[', 21, modifiers, '~', ch);
2449 len = function_key_response('[', 24, modifiers, '~', ch);
2452 /* Handle special keys with alternate mappings */
2453 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2454 if (len != 0) break;
2456 if (modifiers & MOD_CONTROL_MASK) {
2457 if (sym >= '3' && sym <= '7')
2458 sym = (sym & 0x1f) + 8;
2460 if (!((sym >= '!' && sym <= '/') ||
2461 (sym >= '8' && sym <= '?') ||
2462 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2463 else if (sym == '2') sym = 0x00;
2464 else if (sym == '/') sym = 0x1F;
2465 else if (sym == '8' || sym == '?') sym = 0x7F;
2467 if (modifiers & MOD_ALT_MASK) {
2468 if (terminal->mode & MODE_ALT_SENDS_ESC) {
2472 convert_utf8 = false;
2477 (!convert_utf8 && sym < 256)) {
2480 ret = xkb_keysym_to_utf8(sym, ch + len,
2481 MAX_RESPONSE - len);
2484 "Warning: buffer too small to encode "
2485 "UTF8 character\n");
2493 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
2494 if (terminal->scrolling) {
2495 d = terminal->saved_start - terminal->start;
2497 terminal->selection_start_row -= d;
2498 terminal->selection_end_row -= d;
2499 terminal->start = terminal->saved_start;
2500 terminal->scrolling = 0;
2501 widget_schedule_redraw(terminal->widget);
2504 terminal_write(terminal, ch, len);
2506 /* Hide cursor, except if this was coming from a
2507 * repeating key press. */
2508 serial = display_get_serial(terminal->display);
2509 if (terminal->hide_cursor_serial != serial) {
2510 input_set_pointer_image(input, CURSOR_BLANK);
2511 terminal->hide_cursor_serial = serial;
2517 keyboard_focus_handler(struct window *window,
2518 struct input *device, void *data)
2520 struct terminal *terminal = data;
2522 window_schedule_redraw(terminal->window);
2525 static int wordsep(int ch)
2527 const char extra[] = "-,./?%&#:_=+@~";
2532 return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
2536 recompute_selection(struct terminal *terminal)
2538 struct rectangle allocation;
2539 int col, x, width, height;
2540 int start_row, end_row;
2541 int word_start, eol;
2542 int side_margin, top_margin;
2545 union utf8_char *data;
2547 cw = terminal->average_width;
2548 ch = terminal->extents.height;
2549 widget_get_allocation(terminal->widget, &allocation);
2550 width = terminal->width * cw;
2551 height = terminal->height * ch;
2552 side_margin = allocation.x + (allocation.width - width) / 2;
2553 top_margin = allocation.y + (allocation.height - height) / 2;
2555 start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1;
2556 end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1;
2558 if (start_row < end_row ||
2559 (start_row == end_row &&
2560 terminal->selection_start_x < terminal->selection_end_x)) {
2561 terminal->selection_start_row = start_row;
2562 terminal->selection_end_row = end_row;
2563 start_x = terminal->selection_start_x;
2564 end_x = terminal->selection_end_x;
2566 terminal->selection_start_row = end_row;
2567 terminal->selection_end_row = start_row;
2568 start_x = terminal->selection_end_x;
2569 end_x = terminal->selection_start_x;
2573 if (terminal->selection_start_row < 0) {
2574 terminal->selection_start_row = 0;
2575 terminal->selection_start_col = 0;
2577 x = side_margin + cw / 2;
2578 data = terminal_get_row(terminal,
2579 terminal->selection_start_row);
2581 for (col = 0; col < terminal->width; col++, x += cw) {
2582 if (col == 0 || wordsep(data[col - 1].ch))
2584 if (data[col].ch != 0)
2590 switch (terminal->dragging) {
2592 terminal->selection_start_col = 0;
2595 terminal->selection_start_col = word_start;
2598 terminal->selection_start_col = col;
2603 if (terminal->selection_end_row >= terminal->height) {
2604 terminal->selection_end_row = terminal->height;
2605 terminal->selection_end_col = 0;
2607 x = side_margin + cw / 2;
2608 data = terminal_get_row(terminal, terminal->selection_end_row);
2609 for (col = 0; col < terminal->width; col++, x += cw) {
2610 if (terminal->dragging == SELECT_CHAR && end_x < x)
2612 if (terminal->dragging == SELECT_WORD &&
2613 end_x < x && wordsep(data[col].ch))
2616 terminal->selection_end_col = col;
2619 if (terminal->selection_end_col != terminal->selection_start_col ||
2620 terminal->selection_start_row != terminal->selection_end_row) {
2621 col = terminal->selection_end_col;
2622 if (col > 0 && data[col - 1].ch == 0)
2623 terminal->selection_end_col = terminal->width;
2624 data = terminal_get_row(terminal, terminal->selection_start_row);
2625 if (data[terminal->selection_start_col].ch == 0)
2626 terminal->selection_start_col = eol;
2633 button_handler(struct widget *widget,
2634 struct input *input, uint32_t time,
2636 enum wl_pointer_button_state state, void *data)
2638 struct terminal *terminal = data;
2642 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2644 if (time - terminal->button_time < 500)
2645 terminal->click_count++;
2647 terminal->click_count = 1;
2649 terminal->button_time = time;
2650 terminal->dragging =
2651 (terminal->click_count - 1) % 3 + SELECT_CHAR;
2653 input_get_position(input,
2654 &terminal->selection_start_x,
2655 &terminal->selection_start_y);
2656 terminal->selection_end_x = terminal->selection_start_x;
2657 terminal->selection_end_y = terminal->selection_start_y;
2658 if (recompute_selection(terminal))
2659 widget_schedule_redraw(widget);
2661 terminal->dragging = SELECT_NONE;
2668 enter_handler(struct widget *widget,
2669 struct input *input, float x, float y, void *data)
2671 return CURSOR_IBEAM;
2675 motion_handler(struct widget *widget,
2676 struct input *input, uint32_t time,
2677 float x, float y, void *data)
2679 struct terminal *terminal = data;
2681 if (terminal->dragging) {
2682 input_get_position(input,
2683 &terminal->selection_end_x,
2684 &terminal->selection_end_y);
2686 if (recompute_selection(terminal))
2687 widget_schedule_redraw(widget);
2690 return CURSOR_IBEAM;
2694 output_handler(struct window *window, struct output *output, int enter,
2698 window_set_buffer_transform(window, output_get_transform(output));
2699 window_set_buffer_scale(window, window_get_output_scale(window));
2700 window_schedule_redraw(window);
2704 #define howmany(x, y) (((x) + ((y) - 1)) / (y))
2707 static struct terminal *
2708 terminal_create(struct display *display)
2710 struct terminal *terminal;
2711 cairo_surface_t *surface;
2713 cairo_text_extents_t text_extents;
2715 terminal = xzalloc(sizeof *terminal);
2716 terminal->color_scheme = &DEFAULT_COLORS;
2717 terminal_init(terminal);
2718 terminal->margin_top = 0;
2719 terminal->margin_bottom = -1;
2720 terminal->window = window_create(display);
2721 terminal->widget = window_frame_create(terminal->window, terminal);
2722 window_set_title(terminal->window, "Wayland Terminal");
2723 widget_set_transparent(terminal->widget, 0);
2725 init_state_machine(&terminal->state_machine);
2726 init_color_table(terminal);
2728 terminal->display = display;
2729 terminal->margin = 5;
2730 terminal->buffer_height = 1024;
2733 window_set_user_data(terminal->window, terminal);
2734 window_set_key_handler(terminal->window, key_handler);
2735 window_set_keyboard_focus_handler(terminal->window,
2736 keyboard_focus_handler);
2737 window_set_fullscreen_handler(terminal->window, fullscreen_handler);
2738 window_set_output_handler(terminal->window, output_handler);
2739 window_set_close_handler(terminal->window, close_handler);
2741 window_set_data_handler(terminal->window, data_handler);
2742 window_set_drop_handler(terminal->window, drop_handler);
2744 widget_set_redraw_handler(terminal->widget, redraw_handler);
2745 widget_set_resize_handler(terminal->widget, resize_handler);
2746 widget_set_button_handler(terminal->widget, button_handler);
2747 widget_set_enter_handler(terminal->widget, enter_handler);
2748 widget_set_motion_handler(terminal->widget, motion_handler);
2750 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2751 cr = cairo_create(surface);
2752 cairo_set_font_size(cr, option_font_size);
2753 cairo_select_font_face (cr, option_font,
2754 CAIRO_FONT_SLANT_NORMAL,
2755 CAIRO_FONT_WEIGHT_BOLD);
2756 terminal->font_bold = cairo_get_scaled_font (cr);
2757 cairo_scaled_font_reference(terminal->font_bold);
2759 cairo_select_font_face (cr, option_font,
2760 CAIRO_FONT_SLANT_NORMAL,
2761 CAIRO_FONT_WEIGHT_NORMAL);
2762 terminal->font_normal = cairo_get_scaled_font (cr);
2763 cairo_scaled_font_reference(terminal->font_normal);
2765 cairo_font_extents(cr, &terminal->extents);
2767 /* Compute the average ascii glyph width */
2768 cairo_text_extents(cr, TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS,
2770 terminal->average_width = howmany
2771 (text_extents.width,
2772 strlen(TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS));
2773 terminal->average_width = ceil(terminal->average_width);
2776 cairo_surface_destroy(surface);
2778 terminal_resize(terminal, 20, 5); /* Set minimum size first */
2779 terminal_resize(terminal, 80, 25);
2781 wl_list_insert(terminal_list.prev, &terminal->link);
2787 terminal_destroy(struct terminal *terminal)
2789 display_unwatch_fd(terminal->display, terminal->master);
2790 window_destroy(terminal->window);
2791 close(terminal->master);
2792 wl_list_remove(&terminal->link);
2794 if (wl_list_empty(&terminal_list))
2795 display_exit(terminal->display);
2801 io_handler(struct task *task, uint32_t events)
2803 struct terminal *terminal =
2804 container_of(task, struct terminal, io_task);
2808 if (events & EPOLLHUP) {
2809 terminal_destroy(terminal);
2813 len = read(terminal->master, buffer, sizeof buffer);
2815 terminal_destroy(terminal);
2817 terminal_data(terminal, buffer, len);
2821 terminal_run(struct terminal *terminal, const char *path)
2826 pid = forkpty(&master, NULL, NULL, NULL);
2828 setenv("TERM", option_term, 1);
2829 setenv("COLORTERM", option_term, 1);
2830 if (execl(path, path, NULL)) {
2831 printf("exec failed: %m\n");
2834 } else if (pid < 0) {
2835 fprintf(stderr, "failed to fork and create pty (%m).\n");
2839 terminal->master = master;
2840 fcntl(master, F_SETFL, O_NONBLOCK);
2841 terminal->io_task.run = io_handler;
2842 display_watch_fd(terminal->display, terminal->master,
2843 EPOLLIN | EPOLLHUP, &terminal->io_task);
2845 window_set_fullscreen(terminal->window, option_fullscreen);
2846 if (!window_is_fullscreen(terminal->window))
2847 terminal_resize(terminal, 80, 24);
2852 static const struct weston_option terminal_options[] = {
2853 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
2854 { WESTON_OPTION_STRING, "font", 0, &option_font },
2855 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
2858 int main(int argc, char *argv[])
2861 struct terminal *terminal;
2862 struct weston_config *config;
2863 struct weston_config_section *s;
2865 /* as wcwidth is locale-dependent,
2866 wcwidth needs setlocale call to function properly. */
2867 setlocale(LC_ALL, "");
2869 option_shell = getenv("SHELL");
2871 option_shell = "/bin/bash";
2873 config = weston_config_parse("weston.ini");
2874 s = weston_config_get_section(config, "terminal", NULL, NULL);
2875 weston_config_section_get_string(s, "font", &option_font, "mono");
2876 weston_config_section_get_int(s, "font-size", &option_font_size, 14);
2877 weston_config_section_get_string(s, "term", &option_term, "xterm");
2878 weston_config_destroy(config);
2880 d = display_create(&argc, argv);
2882 fprintf(stderr, "failed to create display: %m\n");
2886 wl_list_init(&terminal_list);
2887 terminal = terminal_create(d);
2888 if (terminal_run(terminal, option_shell))