blob: e0f54bdaabe5a831e827d03561b1521a59f0a6b0 [file] [log] [blame]
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
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.
13 *
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
20 * OF THIS SOFTWARE.
21 */
22
23#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <fcntl.h>
28#include <unistd.h>
29#include <math.h>
30#include <time.h>
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050031#include <pty.h>
Kristian Høgsbergf04e8382008-12-08 00:07:49 -050032#include <ctype.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050033#include <cairo.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040034#include <sys/epoll.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050035
Pekka Paalanen50719bc2011-11-22 14:18:50 +020036#include <wayland-client.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050037
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -040038#include "../shared/config-parser.h"
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050039#include "window.h"
40
Kristian Høgsberg0395f302008-12-22 12:14:50 -050041static int option_fullscreen;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -040042static char *option_font = "mono";
Kristian Høgsbergde845cf2012-06-20 22:14:31 -040043static char *option_term = "xterm";
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040044static char *option_shell;
45
46static struct wl_list terminal_list;
47
48static struct terminal *
49terminal_create(struct display *display, int fullscreen);
50static void
51terminal_destroy(struct terminal *terminal);
52static int
53terminal_run(struct terminal *terminal, const char *path);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050054
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050055#define MOD_SHIFT 0x01
56#define MOD_ALT 0x02
57#define MOD_CTRL 0x04
58
Callum Lowcay30eeae52011-01-07 19:46:55 +000059#define ATTRMASK_BOLD 0x01
60#define ATTRMASK_UNDERLINE 0x02
61#define ATTRMASK_BLINK 0x04
62#define ATTRMASK_INVERSE 0x08
Callum Lowcay81179db2011-01-10 12:14:01 +130063#define ATTRMASK_CONCEALED 0x10
Callum Lowcay30eeae52011-01-07 19:46:55 +000064
Callum Lowcayb8609ad2011-01-07 19:46:57 +000065/* Buffer sizes */
Callum Lowcayef57a9b2011-01-14 20:46:23 +130066#define MAX_RESPONSE 256
67#define MAX_ESCAPE 255
Callum Lowcayb8609ad2011-01-07 19:46:57 +000068
Callum Lowcay8e57dd52011-01-07 19:46:59 +000069/* Terminal modes */
70#define MODE_SHOW_CURSOR 0x00000001
71#define MODE_INVERSE 0x00000002
72#define MODE_AUTOWRAP 0x00000004
73#define MODE_AUTOREPEAT 0x00000008
74#define MODE_LF_NEWLINE 0x00000010
Callum Lowcay69e96582011-01-07 19:47:00 +000075#define MODE_IRM 0x00000020
Callum Lowcay7e08e902011-01-07 19:47:02 +000076#define MODE_DELETE_SENDS_DEL 0x00000040
77#define MODE_ALT_SENDS_ESC 0x00000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +000078
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000079union utf8_char {
80 unsigned char byte[4];
81 uint32_t ch;
82};
83
84enum utf8_state {
85 utf8state_start,
86 utf8state_accept,
87 utf8state_reject,
88 utf8state_expect3,
89 utf8state_expect2,
90 utf8state_expect1
91};
92
93struct utf8_state_machine {
94 enum utf8_state state;
95 int len;
96 union utf8_char s;
97};
98
99static void
100init_state_machine(struct utf8_state_machine *machine)
101{
102 machine->state = utf8state_start;
103 machine->len = 0;
104 machine->s.ch = 0;
105}
106
107static enum utf8_state
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400108utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000109{
110 switch(machine->state) {
111 case utf8state_start:
112 case utf8state_accept:
113 case utf8state_reject:
114 machine->s.ch = 0;
115 machine->len = 0;
116 if(c == 0xC0 || c == 0xC1) {
117 /* overlong encoding, reject */
118 machine->state = utf8state_reject;
119 } else if((c & 0x80) == 0) {
120 /* single byte, accept */
121 machine->s.byte[machine->len++] = c;
122 machine->state = utf8state_accept;
123 } else if((c & 0xC0) == 0x80) {
124 /* parser out of sync, ignore byte */
125 machine->state = utf8state_start;
126 } else if((c & 0xE0) == 0xC0) {
127 /* start of two byte sequence */
128 machine->s.byte[machine->len++] = c;
129 machine->state = utf8state_expect1;
130 } else if((c & 0xF0) == 0xE0) {
131 /* start of three byte sequence */
132 machine->s.byte[machine->len++] = c;
133 machine->state = utf8state_expect2;
134 } else if((c & 0xF8) == 0xF0) {
135 /* start of four byte sequence */
136 machine->s.byte[machine->len++] = c;
137 machine->state = utf8state_expect3;
138 } else {
139 /* overlong encoding, reject */
140 machine->state = utf8state_reject;
141 }
142 break;
143 case utf8state_expect3:
144 machine->s.byte[machine->len++] = c;
145 if((c & 0xC0) == 0x80) {
146 /* all good, continue */
147 machine->state = utf8state_expect2;
148 } else {
149 /* missing extra byte, reject */
150 machine->state = utf8state_reject;
151 }
152 break;
153 case utf8state_expect2:
154 machine->s.byte[machine->len++] = c;
155 if((c & 0xC0) == 0x80) {
156 /* all good, continue */
157 machine->state = utf8state_expect1;
158 } else {
159 /* missing extra byte, reject */
160 machine->state = utf8state_reject;
161 }
162 break;
163 case utf8state_expect1:
164 machine->s.byte[machine->len++] = c;
165 if((c & 0xC0) == 0x80) {
166 /* all good, accept */
167 machine->state = utf8state_accept;
168 } else {
169 /* missing extra byte, reject */
170 machine->state = utf8state_reject;
171 }
172 break;
173 default:
174 machine->state = utf8state_reject;
175 break;
176 }
177
178 return machine->state;
179}
180
Callum Lowcay256e72f2011-01-07 19:47:01 +0000181struct char_sub {
182 union utf8_char match;
183 union utf8_char replace;
184};
185/* Set last char_sub match to NULL char */
186typedef struct char_sub *character_set;
187
188struct char_sub CS_US[] = {
189 {{{0, }}, {{0, }}}
190};
191static struct char_sub CS_UK[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200192 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000193 {{{0, }}, {{0, }}}
194};
195static struct char_sub CS_SPECIAL[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200196 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond: ♦ */
197 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell: ▒ */
198 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT: ␉ */
199 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF: ␌ */
200 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR: ␍ */
201 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF: ␊ */
202 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree: ° */
203 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus: ± */
204 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL: ␤ */
205 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT: ␋ */
206 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB: ┘ */
207 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT: ┐ */
208 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT: ┌ */
209 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_LB: └ */
210 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS: ┼ */
211 {{{'o', 0, }}, {{0xE2, 0x8E, 0xBA, 0}}}, /* Horiz. Scan Line 1: ⎺ */
212 {{{'p', 0, }}, {{0xE2, 0x8E, 0xBB, 0}}}, /* Horiz. Scan Line 3: ⎻ */
213 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* Horiz. Scan Line 5: ─ */
214 {{{'r', 0, }}, {{0xE2, 0x8E, 0xBC, 0}}}, /* Horiz. Scan Line 7: ⎼ */
215 {{{'s', 0, }}, {{0xE2, 0x8E, 0xBD, 0}}}, /* Horiz. Scan Line 9: ⎽ */
216 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR: ├ */
217 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL: ┤ */
218 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU: ┴ */
219 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD: ┬ */
220 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V: │ */
221 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE: ≤ */
222 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE: ≥ */
223 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI: π */
224 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ: ≠ */
225 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
226 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT: ⋅ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000227 {{{0, }}, {{0, }}}
228};
229
230static void
231apply_char_set(character_set cs, union utf8_char *utf8)
232{
233 int i = 0;
234
235 while (cs[i].match.byte[0]) {
236 if ((*utf8).ch == cs[i].match.ch) {
237 *utf8 = cs[i].replace;
238 break;
239 }
240 i++;
241 }
242}
243
Callum Lowcay7e08e902011-01-07 19:47:02 +0000244struct key_map {
245 int sym;
246 int num;
247 char escape;
248 char code;
249};
250/* Set last key_sub sym to NULL */
251typedef struct key_map *keyboard_mode;
252
253static struct key_map KM_NORMAL[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400254 { XKB_KEY_Left, 1, '[', 'D' },
255 { XKB_KEY_Right, 1, '[', 'C' },
256 { XKB_KEY_Up, 1, '[', 'A' },
257 { XKB_KEY_Down, 1, '[', 'B' },
258 { XKB_KEY_Home, 1, '[', 'H' },
259 { XKB_KEY_End, 1, '[', 'F' },
260 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000261};
262static struct key_map KM_APPLICATION[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400263 { XKB_KEY_Left, 1, 'O', 'D' },
264 { XKB_KEY_Right, 1, 'O', 'C' },
265 { XKB_KEY_Up, 1, 'O', 'A' },
266 { XKB_KEY_Down, 1, 'O', 'B' },
267 { XKB_KEY_Home, 1, 'O', 'H' },
268 { XKB_KEY_End, 1, 'O', 'F' },
269 { XKB_KEY_KP_Enter, 1, 'O', 'M' },
270 { XKB_KEY_KP_Multiply, 1, 'O', 'j' },
271 { XKB_KEY_KP_Add, 1, 'O', 'k' },
272 { XKB_KEY_KP_Separator, 1, 'O', 'l' },
273 { XKB_KEY_KP_Subtract, 1, 'O', 'm' },
274 { XKB_KEY_KP_Divide, 1, 'O', 'o' },
275 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000276};
277
278static int
279function_key_response(char escape, int num, uint32_t modifiers,
280 char code, char *response)
281{
282 int mod_num = 0;
283 int len;
284
Kristian Høgsberg70163132012-05-08 15:55:39 -0400285 if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
286 if (modifiers & MOD_ALT_MASK) mod_num |= 2;
287 if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000288
289 if (mod_num != 0)
290 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
291 num, mod_num + 1, code);
292 else if (code != '~')
293 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
294 escape, code);
295 else
296 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
297 escape, num, code);
298
299 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
300 else return len;
301}
302
303/* returns the number of bytes written into response,
304 * which must have room for MAX_RESPONSE bytes */
305static int
306apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
307{
308 struct key_map map;
309 int len = 0;
310 int i = 0;
311
312 while (mode[i].sym) {
313 map = mode[i++];
314 if (sym == map.sym) {
315 len = function_key_response(map.escape, map.num,
316 modifiers, map.code,
317 response);
318 break;
319 }
320 }
321
322 return len;
323}
324
Callum Lowcay30eeae52011-01-07 19:46:55 +0000325struct terminal_color { double r, g, b, a; };
326struct attr {
327 unsigned char fg, bg;
328 char a; /* attributes format:
329 * 76543210
Callum Lowcay81179db2011-01-10 12:14:01 +1300330 * cilub */
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500331 char s; /* in selection */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000332};
333struct color_scheme {
334 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500335 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000336 struct attr default_attr;
337};
338
339static void
340attr_init(struct attr *data_attr, struct attr attr, int n)
341{
342 int i;
343 for (i = 0; i < n; i++) {
344 data_attr[i] = attr;
345 }
346}
347
Callum Lowcay67a201d2011-01-12 19:23:41 +1300348enum escape_state {
349 escape_state_normal = 0,
350 escape_state_escape,
351 escape_state_dcs,
352 escape_state_csi,
353 escape_state_osc,
354 escape_state_inner_escape,
355 escape_state_ignore,
356 escape_state_special
357};
358
359#define ESC_FLAG_WHAT 0x01
360#define ESC_FLAG_GT 0x02
361#define ESC_FLAG_BANG 0x04
362#define ESC_FLAG_CASH 0x08
363#define ESC_FLAG_SQUOTE 0x10
364#define ESC_FLAG_DQUOTE 0x20
365#define ESC_FLAG_SPACE 0x40
366
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500367struct terminal {
368 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500369 struct widget *widget;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500370 struct display *display;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000371 union utf8_char *data;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400372 struct task io_task;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000373 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000374 struct attr *data_attr;
375 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000376 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000377 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000378 char saved_origin_mode;
379 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000380 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000381 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000382 character_set cs, g0, g1;
383 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000384 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000385 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500386 int width, height, start, row, column;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000387 int saved_row, saved_column;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600388 int send_cursor_position;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500389 int fd, master;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500390 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300391 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500392 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300393 enum escape_state state;
394 enum escape_state outer_state;
395 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000396 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500397 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500398 int fullscreen;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500399 int focused;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400400 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000401 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400402 cairo_font_extents_t extents;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500403 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg88fd4082012-06-21 15:55:03 -0400404 uint32_t hide_cursor_serial;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500405
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400406 struct wl_data_source *selection;
407 int32_t dragging;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500408 int selection_start_x, selection_start_y;
409 int selection_end_x, selection_end_y;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -0400410 struct wl_list link;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500411};
412
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000413/* Create default tab stops, every 8 characters */
414static void
415terminal_init_tabs(struct terminal *terminal)
416{
417 int i = 0;
418
419 while (i < terminal->width) {
420 if (i % 8 == 0)
421 terminal->tab_ruler[i] = 1;
422 else
423 terminal->tab_ruler[i] = 0;
424 i++;
425 }
426}
427
Callum Lowcay30eeae52011-01-07 19:46:55 +0000428static void
429terminal_init(struct terminal *terminal)
430{
431 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000432 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000433 terminal->mode = MODE_SHOW_CURSOR |
434 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000435 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000436 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000437
438 terminal->row = 0;
439 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000440
Callum Lowcay256e72f2011-01-07 19:47:01 +0000441 terminal->g0 = CS_US;
442 terminal->g1 = CS_US;
443 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000444 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000445
446 terminal->saved_g0 = terminal->g0;
447 terminal->saved_g1 = terminal->g1;
448 terminal->saved_cs = terminal->cs;
449
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000450 terminal->saved_attr = terminal->curr_attr;
451 terminal->saved_origin_mode = terminal->origin_mode;
452 terminal->saved_row = terminal->row;
453 terminal->saved_column = terminal->column;
454
455 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000456}
457
458static void
459init_color_table(struct terminal *terminal)
460{
461 int c, r;
462 struct terminal_color *color_table = terminal->color_table;
463
464 for (c = 0; c < 256; c ++) {
465 if (c < 16) {
466 color_table[c] = terminal->color_scheme->palette[c];
467 } else if (c < 232) {
468 r = c - 16;
469 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
470 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
471 color_table[c].r = ((double)(r % 6) / 6.0);
472 color_table[c].a = 1.0;
473 } else {
474 r = (c - 232) * 10 + 8;
475 color_table[c].r = ((double) r) / 256.0;
476 color_table[c].g = color_table[c].r;
477 color_table[c].b = color_table[c].r;
478 color_table[c].a = 1.0;
479 }
480 }
481}
482
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000483static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500484terminal_get_row(struct terminal *terminal, int row)
485{
486 int index;
487
488 index = (row + terminal->start) % terminal->height;
489
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000490 return &terminal->data[index * terminal->width];
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500491}
492
Callum Lowcay30eeae52011-01-07 19:46:55 +0000493static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500494terminal_get_attr_row(struct terminal *terminal, int row)
495{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000496 int index;
497
498 index = (row + terminal->start) % terminal->height;
499
500 return &terminal->data_attr[index * terminal->width];
501}
502
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500503union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300504 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500505 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500506};
507
Kristian Høgsberg59826582011-01-20 11:56:57 -0500508static int
509terminal_compare_position(struct terminal *terminal,
510 int x, int y, int32_t ref_row, int32_t ref_col)
511{
512 struct rectangle allocation;
513 int top_margin, side_margin, col, row, ref_x;
514
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500515 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsberg59826582011-01-20 11:56:57 -0500516 side_margin = allocation.x + (allocation.width - terminal->width * terminal->extents.max_x_advance) / 2;
517 top_margin = allocation.y + (allocation.height - terminal->height * terminal->extents.height) / 2;
518
519 col = (x - side_margin) / terminal->extents.max_x_advance;
520 row = (y - top_margin) / terminal->extents.height;
521
522 ref_x = side_margin + ref_col * terminal->extents.max_x_advance +
523 terminal->extents.max_x_advance / 2;
524
525 if (row < ref_row)
526 return -1;
527 if (row == ref_row) {
528 if (col < ref_col)
529 return -1;
530 if (col == ref_col && x < ref_x)
531 return -1;
532 }
533
534 return 1;
535}
536
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500537static void
538terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500539 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500540{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500541 struct attr attr;
542 int foreground, background, tmp;
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500543 int start_cmp, end_cmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500544
545 start_cmp =
546 terminal_compare_position(terminal,
547 terminal->selection_start_x,
548 terminal->selection_start_y,
549 row, col);
550 end_cmp =
551 terminal_compare_position(terminal,
552 terminal->selection_end_x,
553 terminal->selection_end_y,
554 row, col);
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500555 decoded->attr.s = 0;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500556 if (start_cmp < 0 && end_cmp > 0)
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500557 decoded->attr.s = 1;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500558 else if (end_cmp < 0 && start_cmp > 0)
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500559 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500560
561 /* get the attributes for this character cell */
562 attr = terminal_get_attr_row(terminal, row)[col];
563 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500564 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500565 ((terminal->mode & MODE_SHOW_CURSOR) &&
566 terminal->focused && terminal->row == row &&
567 terminal->column == col)) {
568 foreground = attr.bg;
569 background = attr.fg;
570 if (attr.a & ATTRMASK_BOLD) {
571 if (foreground <= 16) foreground |= 0x08;
572 if (background <= 16) background &= 0x07;
573 }
574 } else {
575 foreground = attr.fg;
576 background = attr.bg;
577 }
578
579 if (terminal->mode & MODE_INVERSE) {
580 tmp = foreground;
581 foreground = background;
582 background = tmp;
583 if (attr.a & ATTRMASK_BOLD) {
584 if (foreground <= 16) foreground |= 0x08;
585 if (background <= 16) background &= 0x07;
586 }
587 }
588
Callum Lowcay9d708b02011-01-12 20:06:17 +1300589 decoded->attr.fg = foreground;
590 decoded->attr.bg = background;
591 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000592}
593
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500594
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500595static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000596terminal_scroll_buffer(struct terminal *terminal, int d)
597{
598 int i;
599
600 d = d % (terminal->height + 1);
601 terminal->start = (terminal->start + d) % terminal->height;
602 if (terminal->start < 0) terminal->start = terminal->height + terminal->start;
603 if(d < 0) {
604 d = 0 - d;
605 for(i = 0; i < d; i++) {
606 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
607 attr_init(terminal_get_attr_row(terminal, i),
608 terminal->curr_attr, terminal->width);
609 }
610 } else {
611 for(i = terminal->height - d; i < terminal->height; i++) {
612 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
613 attr_init(terminal_get_attr_row(terminal, i),
614 terminal->curr_attr, terminal->width);
615 }
616 }
617}
618
619static void
620terminal_scroll_window(struct terminal *terminal, int d)
621{
622 int i;
623 int window_height;
624 int from_row, to_row;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000625
626 // scrolling range is inclusive
627 window_height = terminal->margin_bottom - terminal->margin_top + 1;
628 d = d % (window_height + 1);
629 if(d < 0) {
630 d = 0 - d;
631 to_row = terminal->margin_bottom;
632 from_row = terminal->margin_bottom - d;
633
634 for (i = 0; i < (window_height - d); i++) {
635 memcpy(terminal_get_row(terminal, to_row - i),
636 terminal_get_row(terminal, from_row - i),
637 terminal->data_pitch);
638 memcpy(terminal_get_attr_row(terminal, to_row - i),
639 terminal_get_attr_row(terminal, from_row - i),
640 terminal->attr_pitch);
641 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000642 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
643 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000644 attr_init(terminal_get_attr_row(terminal, i),
645 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000646 }
647 } else {
648 to_row = terminal->margin_top;
649 from_row = terminal->margin_top + d;
650
651 for (i = 0; i < (window_height - d); i++) {
652 memcpy(terminal_get_row(terminal, to_row + i),
653 terminal_get_row(terminal, from_row + i),
654 terminal->data_pitch);
655 memcpy(terminal_get_attr_row(terminal, to_row + i),
656 terminal_get_attr_row(terminal, from_row + i),
657 terminal->attr_pitch);
658 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000659 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
660 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000661 attr_init(terminal_get_attr_row(terminal, i),
662 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000663 }
664 }
665}
666
667static void
668terminal_scroll(struct terminal *terminal, int d)
669{
670 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
671 terminal_scroll_buffer(terminal, d);
672 else
673 terminal_scroll_window(terminal, d);
674}
675
676static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000677terminal_shift_line(struct terminal *terminal, int d)
678{
679 union utf8_char *row;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500680 struct attr *attr_row;
Callum Lowcay69e96582011-01-07 19:47:00 +0000681
682 row = terminal_get_row(terminal, terminal->row);
683 attr_row = terminal_get_attr_row(terminal, terminal->row);
684
685 if ((terminal->width + d) <= terminal->column)
686 d = terminal->column + 1 - terminal->width;
687 if ((terminal->column + d) >= terminal->width)
688 d = terminal->width - terminal->column - 1;
689
690 if (d < 0) {
691 d = 0 - d;
692 memmove(&row[terminal->column],
693 &row[terminal->column + d],
694 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
Callum Lowcay69e96582011-01-07 19:47:00 +0000695 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
696 (terminal->width - terminal->column - d) * sizeof(struct attr));
697 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
698 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
699 } else {
700 memmove(&row[terminal->column + d], &row[terminal->column],
701 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
702 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
703 (terminal->width - terminal->column - d) * sizeof(struct attr));
704 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
705 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
706 }
707}
708
709static void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500710terminal_resize_cells(struct terminal *terminal, int width, int height)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500711{
712 size_t size;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000713 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000714 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000715 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000716 int data_pitch, attr_pitch;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500717 int i, l, total_rows;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500718 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000719 struct winsize ws;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500720
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500721 if (width < 1)
722 width = 1;
723 if (height < 1)
724 height = 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500725 if (terminal->width == width && terminal->height == height)
726 return;
727
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000728 data_pitch = width * sizeof(union utf8_char);
729 size = data_pitch * height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500730 data = malloc(size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000731 attr_pitch = width * sizeof(struct attr);
732 data_attr = malloc(attr_pitch * height);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000733 tab_ruler = malloc(width);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500734 memset(data, 0, size);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000735 memset(tab_ruler, 0, width);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000736 attr_init(data_attr, terminal->curr_attr, width * height);
737 if (terminal->data && terminal->data_attr) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500738 if (width > terminal->width)
739 l = terminal->width;
740 else
741 l = width;
742
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500743 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500744 total_rows = height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500745 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500746 total_rows = terminal->height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500747 }
748
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000749 for (i = 0; i < total_rows; i++) {
750 memcpy(&data[width * i],
751 terminal_get_row(terminal, i),
752 l * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000753 memcpy(&data_attr[width * i],
754 terminal_get_attr_row(terminal, i),
755 l * sizeof(struct attr));
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000756 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500757
758 free(terminal->data);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000759 free(terminal->data_attr);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000760 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500761 }
762
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000763 terminal->data_pitch = data_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000764 terminal->attr_pitch = attr_pitch;
Callum Lowcay86653ed2011-01-07 19:47:03 +0000765 terminal->margin_bottom =
766 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500767 terminal->width = width;
768 terminal->height = height;
769 terminal->data = data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000770 terminal->data_attr = data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000771 terminal->tab_ruler = tab_ruler;
772 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500773
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000774 /* Update the window size */
775 ws.ws_row = terminal->height;
776 ws.ws_col = terminal->width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500777 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500778 ws.ws_xpixel = allocation.width;
779 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000780 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500781}
782
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500783static void
784resize_handler(struct widget *widget,
785 int32_t width, int32_t height, void *data)
786{
787 struct terminal *terminal = data;
788 int32_t columns, rows, m;
789
Alexander Preisingere2b88c02012-06-18 20:59:26 +0200790 if (width < 200)
791 width = 200;
792
793 if (height < 50)
794 height = 50;
795
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500796 m = 2 * terminal->margin;
797 columns = (width - m) / (int32_t) terminal->extents.max_x_advance;
798 rows = (height - m) / (int32_t) terminal->extents.height;
799
800 if (!terminal->fullscreen) {
801 width = columns * terminal->extents.max_x_advance + m;
802 height = rows * terminal->extents.height + m;
803 widget_set_size(terminal->widget, width, height);
804 }
805
806 terminal_resize_cells(terminal, columns, rows);
807}
808
809static void
810terminal_resize(struct terminal *terminal, int columns, int rows)
811{
812 int32_t width, height, m;
813
814 if (terminal->fullscreen)
815 return;
816
817 m = 2 * terminal->margin;
818 width = columns * terminal->extents.max_x_advance + m;
819 height = rows * terminal->extents.height + m;
Kristian Høgsberga1627922012-06-20 17:30:03 -0400820
821 frame_set_child_size(terminal->widget, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500822}
823
Callum Lowcay30eeae52011-01-07 19:46:55 +0000824struct color_scheme DEFAULT_COLORS = {
825 {
826 {0, 0, 0, 1}, /* black */
827 {0.66, 0, 0, 1}, /* red */
828 {0 , 0.66, 0, 1}, /* green */
829 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
830 {0 , 0 , 0.66, 1}, /* blue */
831 {0.66, 0 , 0.66, 1}, /* magenta */
832 {0, 0.66, 0.66, 1}, /* cyan */
833 {0.66, 0.66, 0.66, 1}, /* light grey */
834 {0.22, 0.33, 0.33, 1}, /* dark grey */
835 {1, 0.33, 0.33, 1}, /* high red */
836 {0.33, 1, 0.33, 1}, /* high green */
837 {1, 1, 0.33, 1}, /* high yellow */
838 {0.33, 0.33, 1, 1}, /* high blue */
839 {1, 0.33, 1, 1}, /* high magenta */
840 {0.33, 1, 1, 1}, /* high cyan */
841 {1, 1, 1, 1} /* white */
842 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500843 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000844 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
845};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400846
Kristian Høgsberg22106762008-12-08 13:50:07 -0500847static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500848terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
849{
850 cairo_set_source_rgba(cr,
851 terminal->color_table[index].r,
852 terminal->color_table[index].g,
853 terminal->color_table[index].b,
854 terminal->color_table[index].a);
855}
856
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500857static void
858terminal_send_selection(struct terminal *terminal, int fd)
859{
860 int row, col;
861 union utf8_char *p_row;
862 union decoded_attr attr;
863 FILE *fp;
864 int len;
865
866 fp = fdopen(fd, "w");
867 for (row = 0; row < terminal->height; row++) {
868 p_row = terminal_get_row(terminal, row);
869 for (col = 0; col < terminal->width; col++) {
870 /* get the attributes for this character cell */
871 terminal_decode_attr(terminal, row, col, &attr);
872 if (!attr.attr.s)
873 continue;
874 len = strnlen((char *) p_row[col].byte, 4);
875 fwrite(p_row[col].byte, 1, len, fp);
876 }
877 }
878 fclose(fp);
879}
880
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500881struct glyph_run {
882 struct terminal *terminal;
883 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400884 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500885 union decoded_attr attr;
886 cairo_glyph_t glyphs[256], *g;
887};
888
889static void
890glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
891{
892 run->terminal = terminal;
893 run->cr = cr;
894 run->g = run->glyphs;
895 run->count = 0;
896 run->attr.key = 0;
897}
898
899static void
900glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
901{
902 cairo_scaled_font_t *font;
903
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500904 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
905 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300906 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500907 font = run->terminal->font_bold;
908 else
909 font = run->terminal->font_normal;
910 cairo_set_scaled_font(run->cr, font);
911 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300912 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500913
Callum Lowcay9d708b02011-01-12 20:06:17 +1300914 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
915 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500916 run->g = run->glyphs;
917 run->count = 0;
918 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300919 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500920}
921
922static void
923glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
924{
925 int num_glyphs;
926 cairo_scaled_font_t *font;
927
928 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
929
Callum Lowcay9d708b02011-01-12 20:06:17 +1300930 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500931 font = run->terminal->font_bold;
932 else
933 font = run->terminal->font_normal;
934
935 cairo_move_to(run->cr, x, y);
936 cairo_scaled_font_text_to_glyphs (font, x, y,
937 (char *) c->byte, 4,
938 &run->g, &num_glyphs,
939 NULL, NULL, NULL);
940 run->g += num_glyphs;
941 run->count += num_glyphs;
942}
943
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500944
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500945static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500946redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500947{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500948 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500949 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500950 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000951 int top_margin, side_margin;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600952 int row, col, cursor_x, cursor_y;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000953 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500954 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000955 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500956 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500957 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500958 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500959 cairo_font_extents_t extents;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500960
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500961 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500962 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500963 cr = cairo_create(surface);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500964 cairo_rectangle(cr, allocation.x, allocation.y,
965 allocation.width, allocation.height);
966 cairo_clip(cr);
967 cairo_push_group(cr);
968
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500969 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500970 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500971 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500972
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500973 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500974
Kristian Høgsberg59826582011-01-20 11:56:57 -0500975 extents = terminal->extents;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500976 side_margin = (allocation.width - terminal->width * extents.max_x_advance) / 2;
977 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500978
Callum Lowcay30eeae52011-01-07 19:46:55 +0000979 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500980 cairo_translate(cr, allocation.x + side_margin,
981 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500982 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000983 for (row = 0; row < terminal->height; row++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000984 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000985 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500986 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000987
Callum Lowcay9d708b02011-01-12 20:06:17 +1300988 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -0500989 continue;
990
Callum Lowcay9d708b02011-01-12 20:06:17 +1300991 terminal_set_color(terminal, cr, attr.attr.bg);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500992 cairo_move_to(cr, col * extents.max_x_advance,
993 row * extents.height);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000994 cairo_rel_line_to(cr, extents.max_x_advance, 0);
995 cairo_rel_line_to(cr, 0, extents.height);
996 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
997 cairo_close_path(cr);
998 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500999 }
1000 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001001
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001002 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1003
1004 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001005 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001006 for (row = 0; row < terminal->height; row++) {
1007 p_row = terminal_get_row(terminal, row);
1008 for (col = 0; col < terminal->width; col++) {
1009 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -05001010 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001011
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001012 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001013
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001014 text_x = col * extents.max_x_advance;
1015 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +13001016 if (attr.attr.a & ATTRMASK_UNDERLINE) {
1017 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +00001018 cairo_move_to(cr, text_x, (double)text_y + 1.5);
1019 cairo_line_to(cr, text_x + extents.max_x_advance, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001020 cairo_stroke(cr);
1021 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -05001022
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001023 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001024 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001025 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001026
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001027 attr.key = ~0;
1028 glyph_run_flush(&run, attr);
1029
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001030 if ((terminal->mode & MODE_SHOW_CURSOR) && !terminal->focused) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001031 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001032
Callum Lowcay30eeae52011-01-07 19:46:55 +00001033 cairo_set_line_width(cr, 1);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001034 cairo_move_to(cr, terminal->column * extents.max_x_advance + d,
1035 terminal->row * extents.height + d);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001036 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
1037 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
1038 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
1039 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001040
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001041 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001042 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001043
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001044 cairo_pop_group_to_source(cr);
1045 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001046 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001047 cairo_surface_destroy(surface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001048
1049 if (terminal->send_cursor_position) {
1050 cursor_x = side_margin + allocation.x +
1051 terminal->column * extents.max_x_advance;
1052 cursor_y = top_margin + allocation.y +
1053 terminal->row * extents.height;
1054 window_set_text_cursor_position(terminal->window,
1055 cursor_x, cursor_y);
1056 terminal->send_cursor_position = 0;
1057 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001058}
1059
1060static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001061terminal_write(struct terminal *terminal, const char *data, size_t length)
1062{
1063 if (write(terminal->master, data, length) < 0)
1064 abort();
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001065 terminal->send_cursor_position = 1;
Kristian Høgsberg26130862011-08-24 11:30:21 -04001066}
1067
1068static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001069terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001070
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001071static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001072handle_char(struct terminal *terminal, union utf8_char utf8);
1073
1074static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001075handle_sgr(struct terminal *terminal, int code);
1076
1077static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001078handle_term_parameter(struct terminal *terminal, int code, int sr)
1079{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001080 int i;
1081
Callum Lowcay67a201d2011-01-12 19:23:41 +13001082 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001083 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001084 case 1: /* DECCKM */
1085 if (sr) terminal->key_mode = KM_APPLICATION;
1086 else terminal->key_mode = KM_NORMAL;
1087 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001088 case 2: /* DECANM */
1089 /* No VT52 support yet */
1090 terminal->g0 = CS_US;
1091 terminal->g1 = CS_US;
1092 terminal->cs = terminal->g0;
1093 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001094 case 3: /* DECCOLM */
1095 if (sr)
1096 terminal_resize(terminal, 132, 24);
1097 else
1098 terminal_resize(terminal, 80, 24);
1099
1100 /* set columns, but also home cursor and clear screen */
1101 terminal->row = 0; terminal->column = 0;
1102 for (i = 0; i < terminal->height; i++) {
1103 memset(terminal_get_row(terminal, i),
1104 0, terminal->data_pitch);
1105 attr_init(terminal_get_attr_row(terminal, i),
1106 terminal->curr_attr, terminal->width);
1107 }
1108 break;
1109 case 5: /* DECSCNM */
1110 if (sr) terminal->mode |= MODE_INVERSE;
1111 else terminal->mode &= ~MODE_INVERSE;
1112 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001113 case 6: /* DECOM */
1114 terminal->origin_mode = sr;
1115 if (terminal->origin_mode)
1116 terminal->row = terminal->margin_top;
1117 else
1118 terminal->row = 0;
1119 terminal->column = 0;
1120 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001121 case 7: /* DECAWM */
1122 if (sr) terminal->mode |= MODE_AUTOWRAP;
1123 else terminal->mode &= ~MODE_AUTOWRAP;
1124 break;
1125 case 8: /* DECARM */
1126 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1127 else terminal->mode &= ~MODE_AUTOREPEAT;
1128 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001129 case 12: /* Very visible cursor (CVVIS) */
1130 /* FIXME: What do we do here. */
1131 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001132 case 25:
1133 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1134 else terminal->mode &= ~MODE_SHOW_CURSOR;
1135 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001136 case 1034: /* smm/rmm, meta mode on/off */
1137 /* ignore */
1138 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001139 case 1037: /* deleteSendsDel */
1140 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1141 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1142 break;
1143 case 1039: /* altSendsEscape */
1144 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1145 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1146 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001147 case 1049: /* rmcup/smcup, alternate screen */
1148 /* Ignore. Should be possible to implement,
1149 * but it's kind of annoying. */
1150 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001151 default:
1152 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1153 break;
1154 }
1155 } else {
1156 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001157 case 4: /* IRM */
1158 if (sr) terminal->mode |= MODE_IRM;
1159 else terminal->mode &= ~MODE_IRM;
1160 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001161 case 20: /* LNM */
1162 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1163 else terminal->mode &= ~MODE_LF_NEWLINE;
1164 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001165 default:
1166 fprintf(stderr, "Unknown parameter: %d\n", code);
1167 break;
1168 }
1169 }
1170}
1171
1172static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001173handle_dcs(struct terminal *terminal)
1174{
1175}
1176
1177static void
1178handle_osc(struct terminal *terminal)
1179{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001180 char *p;
1181 int code;
1182
1183 terminal->escape[terminal->escape_length++] = '\0';
1184 p = &terminal->escape[2];
1185 code = strtol(p, &p, 10);
1186 if (*p == ';') p++;
1187
1188 switch (code) {
1189 case 0: /* Icon name and window title */
1190 case 1: /* Icon label */
1191 case 2: /* Window title*/
1192 window_set_title(terminal->window, p);
1193 break;
1194 default:
1195 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1196 break;
1197 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001198}
1199
1200static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001201handle_escape(struct terminal *terminal)
1202{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001203 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001204 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001205 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001206 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001207 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001208 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001209 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001210
1211 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001212 i = 0;
1213 p = &terminal->escape[2];
1214 while ((isdigit(*p) || *p == ';') && i < 10) {
1215 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001216 if (!set[i]) {
1217 args[i] = 0;
1218 set[i] = 1;
1219 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001220 p++;
1221 i++;
1222 } else {
1223 args[i] = strtol(p, &p, 10);
1224 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001225 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001226 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001227
1228 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001229 case '@': /* ICH */
1230 count = set[0] ? args[0] : 1;
1231 if (count == 0) count = 1;
1232 terminal_shift_line(terminal, count);
1233 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001234 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001235 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001236 if (count == 0) count = 1;
1237 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001238 terminal->row -= count;
1239 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001240 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001241 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001242 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001243 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001244 if (count == 0) count = 1;
1245 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001246 terminal->row += count;
1247 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001248 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001249 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001250 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001251 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001252 if (count == 0) count = 1;
1253 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001254 terminal->column += count;
1255 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001256 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001257 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001258 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001259 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001260 if (count == 0) count = 1;
1261 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001262 terminal->column -= count;
1263 else
1264 terminal->column = 0;
1265 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001266 case 'E': /* CNL */
1267 count = set[0] ? args[0] : 1;
1268 if (terminal->row + count <= terminal->margin_bottom)
1269 terminal->row += count;
1270 else
1271 terminal->row = terminal->margin_bottom;
1272 terminal->column = 0;
1273 break;
1274 case 'F': /* CPL */
1275 count = set[0] ? args[0] : 1;
1276 if (terminal->row - count >= terminal->margin_top)
1277 terminal->row -= count;
1278 else
1279 terminal->row = terminal->margin_top;
1280 terminal->column = 0;
1281 break;
1282 case 'G': /* CHA */
1283 y = set[0] ? args[0] : 1;
1284 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1285
1286 terminal->column = y - 1;
1287 break;
1288 case 'f': /* HVP */
1289 case 'H': /* CUP */
1290 x = (set[1] ? args[1] : 1) - 1;
1291 x = x < 0 ? 0 :
1292 (x >= terminal->width ? terminal->width - 1 : x);
1293
1294 y = (set[0] ? args[0] : 1) - 1;
1295 if (terminal->origin_mode) {
1296 y += terminal->margin_top;
1297 y = y < terminal->margin_top ? terminal->margin_top :
1298 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1299 } else {
1300 y = y < 0 ? 0 :
1301 (y >= terminal->height ? terminal->height - 1 : y);
1302 }
1303
1304 terminal->row = y;
1305 terminal->column = x;
1306 break;
1307 case 'I': /* CHT */
1308 count = set[0] ? args[0] : 1;
1309 if (count == 0) count = 1;
1310 while (count > 0 && terminal->column < terminal->width) {
1311 if (terminal->tab_ruler[terminal->column]) count--;
1312 terminal->column++;
1313 }
1314 terminal->column--;
1315 break;
1316 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001317 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001318 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001319 if (!set[0] || args[0] == 0 || args[0] > 2) {
1320 memset(&row[terminal->column],
1321 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1322 attr_init(&attr_row[terminal->column],
1323 terminal->curr_attr, terminal->width - terminal->column);
1324 for (i = terminal->row + 1; i < terminal->height; i++) {
1325 memset(terminal_get_row(terminal, i),
1326 0, terminal->data_pitch);
1327 attr_init(terminal_get_attr_row(terminal, i),
1328 terminal->curr_attr, terminal->width);
1329 }
1330 } else if (args[0] == 1) {
1331 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1332 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1333 for (i = 0; i < terminal->row; i++) {
1334 memset(terminal_get_row(terminal, i),
1335 0, terminal->data_pitch);
1336 attr_init(terminal_get_attr_row(terminal, i),
1337 terminal->curr_attr, terminal->width);
1338 }
1339 } else if (args[0] == 2) {
1340 for (i = 0; i < terminal->height; i++) {
1341 memset(terminal_get_row(terminal, i),
1342 0, terminal->data_pitch);
1343 attr_init(terminal_get_attr_row(terminal, i),
1344 terminal->curr_attr, terminal->width);
1345 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001346 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001347 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001348 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001349 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001350 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001351 if (!set[0] || args[0] == 0 || args[0] > 2) {
1352 memset(&row[terminal->column], 0,
1353 (terminal->width - terminal->column) * sizeof(union utf8_char));
1354 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1355 terminal->width - terminal->column);
1356 } else if (args[0] == 1) {
1357 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1358 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1359 } else if (args[0] == 2) {
1360 memset(row, 0, terminal->data_pitch);
1361 attr_init(attr_row, terminal->curr_attr, terminal->width);
1362 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001363 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001364 case 'L': /* IL */
1365 count = set[0] ? args[0] : 1;
1366 if (count == 0) count = 1;
1367 if (terminal->row >= terminal->margin_top &&
1368 terminal->row < terminal->margin_bottom)
1369 {
1370 top = terminal->margin_top;
1371 terminal->margin_top = terminal->row;
1372 terminal_scroll(terminal, 0 - count);
1373 terminal->margin_top = top;
1374 } else if (terminal->row == terminal->margin_bottom) {
1375 memset(terminal_get_row(terminal, terminal->row),
1376 0, terminal->data_pitch);
1377 attr_init(terminal_get_attr_row(terminal, terminal->row),
1378 terminal->curr_attr, terminal->width);
1379 }
1380 break;
1381 case 'M': /* DL */
1382 count = set[0] ? args[0] : 1;
1383 if (count == 0) count = 1;
1384 if (terminal->row >= terminal->margin_top &&
1385 terminal->row < terminal->margin_bottom)
1386 {
1387 top = terminal->margin_top;
1388 terminal->margin_top = terminal->row;
1389 terminal_scroll(terminal, count);
1390 terminal->margin_top = top;
1391 } else if (terminal->row == terminal->margin_bottom) {
1392 memset(terminal_get_row(terminal, terminal->row),
1393 0, terminal->data_pitch);
1394 }
1395 break;
1396 case 'P': /* DCH */
1397 count = set[0] ? args[0] : 1;
1398 if (count == 0) count = 1;
1399 terminal_shift_line(terminal, 0 - count);
1400 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001401 case 'S': /* SU */
1402 terminal_scroll(terminal, set[0] ? args[0] : 1);
1403 break;
1404 case 'T': /* SD */
1405 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1406 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001407 case 'X': /* ECH */
1408 count = set[0] ? args[0] : 1;
1409 if (count == 0) count = 1;
1410 if ((terminal->column + count) > terminal->width)
1411 count = terminal->width - terminal->column;
1412 row = terminal_get_row(terminal, terminal->row);
1413 attr_row = terminal_get_attr_row(terminal, terminal->row);
1414 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1415 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1416 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001417 case 'Z': /* CBT */
1418 count = set[0] ? args[0] : 1;
1419 if (count == 0) count = 1;
1420 while (count > 0 && terminal->column >= 0) {
1421 if (terminal->tab_ruler[terminal->column]) count--;
1422 terminal->column--;
1423 }
1424 terminal->column++;
1425 break;
1426 case '`': /* HPA */
1427 y = set[0] ? args[0] : 1;
1428 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1429
1430 terminal->column = y - 1;
1431 break;
1432 case 'b': /* REP */
1433 count = set[0] ? args[0] : 1;
1434 if (count == 0) count = 1;
1435 if (terminal->last_char.byte[0])
1436 for (i = 0; i < count; i++)
1437 handle_char(terminal, terminal->last_char);
1438 terminal->last_char.byte[0] = 0;
1439 break;
1440 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001441 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001442 break;
1443 case 'd': /* VPA */
1444 x = set[0] ? args[0] : 1;
1445 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1446
1447 terminal->row = x - 1;
1448 break;
1449 case 'g': /* TBC */
1450 if (!set[0] || args[0] == 0) {
1451 terminal->tab_ruler[terminal->column] = 0;
1452 } else if (args[0] == 3) {
1453 memset(terminal->tab_ruler, 0, terminal->width);
1454 }
1455 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001456 case 'h': /* SM */
1457 for(i = 0; i < 10 && set[i]; i++) {
1458 handle_term_parameter(terminal, args[i], 1);
1459 }
1460 break;
1461 case 'l': /* RM */
1462 for(i = 0; i < 10 && set[i]; i++) {
1463 handle_term_parameter(terminal, args[i], 0);
1464 }
1465 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001466 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001467 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001468 if (i <= 7 && set[i] && set[i + 1] &&
1469 set[i + 2] && args[i + 1] == 5)
1470 {
1471 if (args[i] == 38) {
1472 handle_sgr(terminal, args[i + 2] + 256);
1473 break;
1474 } else if (args[i] == 48) {
1475 handle_sgr(terminal, args[i + 2] + 512);
1476 break;
1477 }
1478 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001479 if(set[i]) {
1480 handle_sgr(terminal, args[i]);
1481 } else if(i == 0) {
1482 handle_sgr(terminal, 0);
1483 break;
1484 } else {
1485 break;
1486 }
1487 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001488 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001489 case 'n': /* DSR */
1490 i = set[0] ? args[0] : 0;
1491 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001492 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001493 } else if (i == 6) {
1494 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1495 terminal->origin_mode ?
1496 terminal->row+terminal->margin_top : terminal->row+1,
1497 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001498 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001499 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001500 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001501 case 'r':
1502 if(!set[0]) {
1503 terminal->margin_top = 0;
1504 terminal->margin_bottom = terminal->height-1;
1505 terminal->row = 0;
1506 terminal->column = 0;
1507 } else {
1508 top = (set[0] ? args[0] : 1) - 1;
1509 top = top < 0 ? 0 :
1510 (top >= terminal->height ? terminal->height - 1 : top);
1511 bottom = (set[1] ? args[1] : 1) - 1;
1512 bottom = bottom < 0 ? 0 :
1513 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1514 if(bottom > top) {
1515 terminal->margin_top = top;
1516 terminal->margin_bottom = bottom;
1517 } else {
1518 terminal->margin_top = 0;
1519 terminal->margin_bottom = terminal->height-1;
1520 }
1521 if(terminal->origin_mode)
1522 terminal->row = terminal->margin_top;
1523 else
1524 terminal->row = 0;
1525 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001526 }
1527 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001528 case 's':
1529 terminal->saved_row = terminal->row;
1530 terminal->saved_column = terminal->column;
1531 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001532 case 't': /* windowOps */
1533 if (!set[0]) break;
1534 switch (args[0]) {
1535 case 4: /* resize px */
1536 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001537 widget_schedule_resize(terminal->widget,
1538 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001539 }
1540 break;
1541 case 8: /* resize ch */
1542 if (set[1] && set[2]) {
1543 terminal_resize(terminal, args[2], args[1]);
1544 }
1545 break;
1546 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001547 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001548 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1549 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001550 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001551 break;
1552 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001553 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001554 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1555 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001556 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001557 break;
1558 case 18: /* report ch */
1559 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1560 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001561 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001562 break;
1563 case 21: /* report title */
1564 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1565 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001566 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001567 break;
1568 default:
1569 if (args[0] >= 24)
1570 terminal_resize(terminal, terminal->width, args[0]);
1571 else
1572 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1573 break;
1574 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001575 case 'u':
1576 terminal->row = terminal->saved_row;
1577 terminal->column = terminal->saved_column;
1578 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001579 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001580 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001581 break;
1582 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001583}
1584
1585static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001586handle_non_csi_escape(struct terminal *terminal, char code)
1587{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001588 switch(code) {
1589 case 'M': /* RI */
1590 terminal->row -= 1;
1591 if(terminal->row < terminal->margin_top) {
1592 terminal->row = terminal->margin_top;
1593 terminal_scroll(terminal, -1);
1594 }
1595 break;
1596 case 'E': /* NEL */
1597 terminal->column = 0;
1598 // fallthrough
1599 case 'D': /* IND */
1600 terminal->row += 1;
1601 if(terminal->row > terminal->margin_bottom) {
1602 terminal->row = terminal->margin_bottom;
1603 terminal_scroll(terminal, +1);
1604 }
1605 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001606 case 'c': /* RIS */
1607 terminal_init(terminal);
1608 break;
1609 case 'H': /* HTS */
1610 terminal->tab_ruler[terminal->column] = 1;
1611 break;
1612 case '7': /* DECSC */
1613 terminal->saved_row = terminal->row;
1614 terminal->saved_column = terminal->column;
1615 terminal->saved_attr = terminal->curr_attr;
1616 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001617 terminal->saved_cs = terminal->cs;
1618 terminal->saved_g0 = terminal->g0;
1619 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001620 break;
1621 case '8': /* DECRC */
1622 terminal->row = terminal->saved_row;
1623 terminal->column = terminal->saved_column;
1624 terminal->curr_attr = terminal->saved_attr;
1625 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001626 terminal->cs = terminal->saved_cs;
1627 terminal->g0 = terminal->saved_g0;
1628 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001629 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001630 case '=': /* DECPAM */
1631 terminal->key_mode = KM_APPLICATION;
1632 break;
1633 case '>': /* DECPNM */
1634 terminal->key_mode = KM_NORMAL;
1635 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001636 default:
1637 fprintf(stderr, "Unknown escape code: %c\n", code);
1638 break;
1639 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001640}
1641
1642static void
1643handle_special_escape(struct terminal *terminal, char special, char code)
1644{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001645 int i, numChars;
1646
1647 if (special == '#') {
1648 switch(code) {
1649 case '8':
1650 /* fill with 'E', no cheap way to do this */
1651 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1652 numChars = terminal->width * terminal->height;
1653 for(i = 0; i < numChars; i++) {
1654 terminal->data[i].byte[0] = 'E';
1655 }
1656 break;
1657 default:
1658 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1659 break;
1660 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001661 } else if (special == '(' || special == ')') {
1662 switch(code) {
1663 case '0':
1664 if (special == '(')
1665 terminal->g0 = CS_SPECIAL;
1666 else
1667 terminal->g1 = CS_SPECIAL;
1668 break;
1669 case 'A':
1670 if (special == '(')
1671 terminal->g0 = CS_UK;
1672 else
1673 terminal->g1 = CS_UK;
1674 break;
1675 case 'B':
1676 if (special == '(')
1677 terminal->g0 = CS_US;
1678 else
1679 terminal->g1 = CS_US;
1680 break;
1681 default:
1682 fprintf(stderr, "Unknown character set %c\n", code);
1683 break;
1684 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001685 } else {
1686 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1687 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001688}
1689
1690static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001691handle_sgr(struct terminal *terminal, int code)
1692{
1693 switch(code) {
1694 case 0:
1695 terminal->curr_attr = terminal->color_scheme->default_attr;
1696 break;
1697 case 1:
1698 terminal->curr_attr.a |= ATTRMASK_BOLD;
1699 if (terminal->curr_attr.fg < 8)
1700 terminal->curr_attr.fg += 8;
1701 break;
1702 case 4:
1703 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1704 break;
1705 case 5:
1706 terminal->curr_attr.a |= ATTRMASK_BLINK;
1707 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001708 case 8:
1709 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1710 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001711 case 2:
1712 case 21:
1713 case 22:
1714 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1715 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1716 terminal->curr_attr.fg -= 8;
1717 break;
1718 case 24:
1719 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1720 break;
1721 case 25:
1722 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1723 break;
1724 case 7:
1725 case 26:
1726 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1727 break;
1728 case 27:
1729 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1730 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001731 case 28:
1732 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1733 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001734 case 39:
1735 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1736 break;
1737 case 49:
1738 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1739 break;
1740 default:
1741 if(code >= 30 && code <= 37) {
1742 terminal->curr_attr.fg = code - 30;
1743 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1744 terminal->curr_attr.fg += 8;
1745 } else if(code >= 40 && code <= 47) {
1746 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001747 } else if (code >= 90 && code <= 97) {
1748 terminal->curr_attr.fg = code - 90 + 8;
1749 } else if (code >= 100 && code <= 107) {
1750 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001751 } else if(code >= 256 && code < 512) {
1752 terminal->curr_attr.fg = code - 256;
1753 } else if(code >= 512 && code < 768) {
1754 terminal->curr_attr.bg = code - 512;
1755 } else {
1756 fprintf(stderr, "Unknown SGR code: %d\n", code);
1757 }
1758 break;
1759 }
1760}
1761
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001762/* Returns 1 if c was special, otherwise 0 */
1763static int
1764handle_special_char(struct terminal *terminal, char c)
1765{
1766 union utf8_char *row;
1767 struct attr *attr_row;
1768
1769 row = terminal_get_row(terminal, terminal->row);
1770 attr_row = terminal_get_attr_row(terminal, terminal->row);
1771
1772 switch(c) {
1773 case '\r':
1774 terminal->column = 0;
1775 break;
1776 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001777 if (terminal->mode & MODE_LF_NEWLINE) {
1778 terminal->column = 0;
1779 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001780 /* fallthrough */
1781 case '\v':
1782 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001783 terminal->row++;
1784 if(terminal->row > terminal->margin_bottom) {
1785 terminal->row = terminal->margin_bottom;
1786 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001787 }
1788
1789 break;
1790 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001791 while (terminal->column < terminal->width) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001792 if (terminal->mode & MODE_IRM)
1793 terminal_shift_line(terminal, +1);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001794 row[terminal->column].byte[0] = ' ';
1795 row[terminal->column].byte[1] = '\0';
1796 attr_row[terminal->column] = terminal->curr_attr;
1797 terminal->column++;
Kristian Høgsbergcca3c2f2012-06-20 15:56:13 -04001798 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001799 }
1800 if (terminal->column >= terminal->width) {
1801 terminal->column = terminal->width - 1;
1802 }
1803
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001804 break;
1805 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001806 if (terminal->column >= terminal->width) {
1807 terminal->column = terminal->width - 2;
1808 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001809 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001810 } else if (terminal->mode & MODE_AUTOWRAP) {
1811 terminal->column = terminal->width - 1;
1812 terminal->row -= 1;
1813 if (terminal->row < terminal->margin_top) {
1814 terminal->row = terminal->margin_top;
1815 terminal_scroll(terminal, -1);
1816 }
1817 }
1818
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001819 break;
1820 case '\a':
1821 /* Bell */
1822 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001823 case '\x0E': /* SO */
1824 terminal->cs = terminal->g1;
1825 break;
1826 case '\x0F': /* SI */
1827 terminal->cs = terminal->g0;
1828 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001829 default:
1830 return 0;
1831 }
1832
1833 return 1;
1834}
1835
1836static void
1837handle_char(struct terminal *terminal, union utf8_char utf8)
1838{
1839 union utf8_char *row;
1840 struct attr *attr_row;
1841
1842 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001843
1844 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001845
1846 /* There are a whole lot of non-characters, control codes,
1847 * and formatting codes that should probably be ignored,
1848 * for example: */
1849 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1850 /* BOM, ignore */
1851 return;
1852 }
1853
1854 /* Some of these non-characters should be translated, e.g.: */
1855 if (utf8.byte[0] < 32) {
1856 utf8.byte[0] = utf8.byte[0] + 64;
1857 }
1858
1859 /* handle right margin effects */
1860 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001861 if (terminal->mode & MODE_AUTOWRAP) {
1862 terminal->column = 0;
1863 terminal->row += 1;
1864 if (terminal->row > terminal->margin_bottom) {
1865 terminal->row = terminal->margin_bottom;
1866 terminal_scroll(terminal, +1);
1867 }
1868 } else {
1869 terminal->column--;
1870 }
1871 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001872
1873 row = terminal_get_row(terminal, terminal->row);
1874 attr_row = terminal_get_attr_row(terminal, terminal->row);
1875
Callum Lowcay69e96582011-01-07 19:47:00 +00001876 if (terminal->mode & MODE_IRM)
1877 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001878 row[terminal->column] = utf8;
1879 attr_row[terminal->column++] = terminal->curr_attr;
1880
1881 if (utf8.ch != terminal->last_char.ch)
1882 terminal->last_char = utf8;
1883}
1884
Callum Lowcay30eeae52011-01-07 19:46:55 +00001885static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001886escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1887{
1888 int len, i;
1889
1890 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1891 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1892 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1893 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1894 else len = 1; /* Invalid, cannot happen */
1895
1896 if (terminal->escape_length + len <= MAX_ESCAPE) {
1897 for (i = 0; i < len; i++)
1898 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1899 terminal->escape_length += len;
1900 } else if (terminal->escape_length < MAX_ESCAPE) {
1901 terminal->escape[terminal->escape_length++] = 0;
1902 }
1903}
1904
1905static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001906terminal_data(struct terminal *terminal, const char *data, size_t length)
1907{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001908 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001909 union utf8_char utf8;
1910 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001911
1912 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001913 parser_state =
1914 utf8_next_char(&terminal->state_machine, data[i]);
1915 switch(parser_state) {
1916 case utf8state_accept:
1917 utf8.ch = terminal->state_machine.s.ch;
1918 break;
1919 case utf8state_reject:
1920 /* the unicode replacement character */
1921 utf8.byte[0] = 0xEF;
1922 utf8.byte[1] = 0xBF;
1923 utf8.byte[2] = 0xBD;
1924 utf8.byte[3] = 0x00;
1925 break;
1926 default:
1927 continue;
1928 }
1929
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001930 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001931 switch (terminal->state) {
1932 case escape_state_escape:
1933 escape_append_utf8(terminal, utf8);
1934 switch (utf8.byte[0]) {
1935 case 'P': /* DCS */
1936 terminal->state = escape_state_dcs;
1937 break;
1938 case '[': /* CSI */
1939 terminal->state = escape_state_csi;
1940 break;
1941 case ']': /* OSC */
1942 terminal->state = escape_state_osc;
1943 break;
1944 case '#':
1945 case '(':
1946 case ')': /* special */
1947 terminal->state = escape_state_special;
1948 break;
1949 case '^': /* PM (not implemented) */
1950 case '_': /* APC (not implemented) */
1951 terminal->state = escape_state_ignore;
1952 break;
1953 default:
1954 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001955 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13001956 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001957 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001958 continue;
1959 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001960 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1961 /* do nothing */
1962 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001963 terminal->escape_flags |= ESC_FLAG_WHAT;
1964 } else if (utf8.byte[0] == '>') {
1965 terminal->escape_flags |= ESC_FLAG_GT;
1966 } else if (utf8.byte[0] == '!') {
1967 terminal->escape_flags |= ESC_FLAG_BANG;
1968 } else if (utf8.byte[0] == '$') {
1969 terminal->escape_flags |= ESC_FLAG_CASH;
1970 } else if (utf8.byte[0] == '\'') {
1971 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1972 } else if (utf8.byte[0] == '"') {
1973 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1974 } else if (utf8.byte[0] == ' ') {
1975 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001976 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001977 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001978 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13001979 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001980 }
1981
1982 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1983 utf8.byte[0] == '`')
1984 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001985 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001986 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001987 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001988 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001989 continue;
1990 case escape_state_inner_escape:
1991 if (utf8.byte[0] == '\\') {
1992 terminal->state = escape_state_normal;
1993 if (terminal->outer_state == escape_state_dcs) {
1994 handle_dcs(terminal);
1995 } else if (terminal->outer_state == escape_state_osc) {
1996 handle_osc(terminal);
1997 }
1998 } else if (utf8.byte[0] == '\e') {
1999 terminal->state = terminal->outer_state;
2000 escape_append_utf8(terminal, utf8);
2001 if (terminal->escape_length >= MAX_ESCAPE)
2002 terminal->state = escape_state_normal;
2003 } else {
2004 terminal->state = terminal->outer_state;
2005 if (terminal->escape_length < MAX_ESCAPE)
2006 terminal->escape[terminal->escape_length++] = '\e';
2007 escape_append_utf8(terminal, utf8);
2008 if (terminal->escape_length >= MAX_ESCAPE)
2009 terminal->state = escape_state_normal;
2010 }
2011 continue;
2012 case escape_state_dcs:
2013 case escape_state_osc:
2014 case escape_state_ignore:
2015 if (utf8.byte[0] == '\e') {
2016 terminal->outer_state = terminal->state;
2017 terminal->state = escape_state_inner_escape;
2018 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
2019 terminal->state = escape_state_normal;
2020 handle_osc(terminal);
2021 } else {
2022 escape_append_utf8(terminal, utf8);
2023 if (terminal->escape_length >= MAX_ESCAPE)
2024 terminal->state = escape_state_normal;
2025 }
2026 continue;
2027 case escape_state_special:
2028 escape_append_utf8(terminal, utf8);
2029 terminal->state = escape_state_normal;
2030 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2031 handle_special_escape(terminal, terminal->escape[1],
2032 utf8.byte[0]);
2033 }
2034 continue;
2035 default:
2036 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002037 }
2038
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002039 /* this is valid, because ASCII characters are never used to
2040 * introduce a multibyte sequence in UTF-8 */
2041 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002042 terminal->state = escape_state_escape;
2043 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002044 terminal->escape[0] = '\e';
2045 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002046 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002047 } else {
2048 handle_char(terminal, utf8);
2049 } /* if */
2050 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002051
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002052 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002053}
2054
2055static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002056data_source_target(void *data,
2057 struct wl_data_source *source, const char *mime_type)
2058{
2059 fprintf(stderr, "data_source_target, %s\n", mime_type);
2060}
2061
2062static void
2063data_source_send(void *data,
2064 struct wl_data_source *source,
2065 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002066{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002067 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002068
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002069 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002070}
2071
2072static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002073data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002074{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002075 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002076}
2077
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002078static const struct wl_data_source_listener data_source_listener = {
2079 data_source_target,
2080 data_source_send,
2081 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002082};
2083
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002084static int
2085handle_bound_key(struct terminal *terminal,
2086 struct input *input, uint32_t sym, uint32_t time)
2087{
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002088 struct terminal *new_terminal;
2089
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002090 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002091 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002092 /* Cut selection; terminal doesn't do cut, fall
2093 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002094 case XKB_KEY_C:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002095 terminal->selection =
2096 display_create_data_source(terminal->display);
2097 wl_data_source_offer(terminal->selection,
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002098 "text/plain;charset=utf-8");
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002099 wl_data_source_add_listener(terminal->selection,
2100 &data_source_listener, terminal);
Kristian Høgsbergfee91be2012-06-02 21:21:41 -04002101 input_set_selection(input, terminal->selection,
2102 display_get_serial(terminal->display));
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002103 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002104 case XKB_KEY_V:
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002105 input_receive_selection_data_to_fd(input,
2106 "text/plain;charset=utf-8",
2107 terminal->master);
2108
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002109 return 1;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002110
2111 case XKB_KEY_N:
2112 new_terminal =
2113 terminal_create(terminal->display, option_fullscreen);
2114 if (terminal_run(new_terminal, option_shell))
2115 terminal_destroy(new_terminal);
2116
2117 return 1;
2118
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002119 default:
2120 return 0;
2121 }
2122}
2123
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002124static void
2125key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002126 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2127 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002128{
2129 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002130 char ch[MAX_RESPONSE];
Kristian Høgsberg88fd4082012-06-21 15:55:03 -04002131 uint32_t modifiers, serial;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002132 int len = 0;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002133
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002134 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002135 if ((modifiers & MOD_CONTROL_MASK) &&
2136 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002137 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2138 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002139 return;
2140
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002141 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002142 case XKB_KEY_F11:
Daniel Stonec9785ea2012-05-30 16:31:52 +01002143 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002144 break;
2145 terminal->fullscreen ^= 1;
2146 window_set_fullscreen(window, terminal->fullscreen);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002147 break;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002148
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002149 case XKB_KEY_BackSpace:
Kristian Høgsbergb7f94bf2012-06-21 12:29:36 -04002150 if (modifiers & MOD_ALT_MASK)
2151 ch[len++] = 0x1b;
Kristian Høgsberg71a4cf42012-06-20 17:57:56 -04002152 ch[len++] = 0x7f;
2153 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002154 case XKB_KEY_Tab:
2155 case XKB_KEY_Linefeed:
2156 case XKB_KEY_Clear:
2157 case XKB_KEY_Pause:
2158 case XKB_KEY_Scroll_Lock:
2159 case XKB_KEY_Sys_Req:
2160 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002161 ch[len++] = sym & 0x7f;
2162 break;
2163
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002164 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002165 if (terminal->mode & MODE_LF_NEWLINE) {
2166 ch[len++] = 0x0D;
2167 ch[len++] = 0x0A;
2168 } else {
2169 ch[len++] = 0x0D;
2170 }
2171 break;
2172
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002173 case XKB_KEY_Shift_L:
2174 case XKB_KEY_Shift_R:
2175 case XKB_KEY_Control_L:
2176 case XKB_KEY_Control_R:
2177 case XKB_KEY_Alt_L:
2178 case XKB_KEY_Alt_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002179 break;
2180
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002181 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002182 len = function_key_response('[', 2, modifiers, '~', ch);
2183 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002184 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002185 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2186 ch[len++] = '\x04';
2187 } else {
2188 len = function_key_response('[', 3, modifiers, '~', ch);
2189 }
2190 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002191 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002192 len = function_key_response('[', 5, modifiers, '~', ch);
2193 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002194 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002195 len = function_key_response('[', 6, modifiers, '~', ch);
2196 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002197 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002198 len = function_key_response('O', 1, modifiers, 'P', ch);
2199 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002200 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002201 len = function_key_response('O', 1, modifiers, 'Q', ch);
2202 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002203 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002204 len = function_key_response('O', 1, modifiers, 'R', ch);
2205 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002206 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002207 len = function_key_response('O', 1, modifiers, 'S', ch);
2208 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002209 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002210 len = function_key_response('[', 15, modifiers, '~', ch);
2211 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002212 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002213 len = function_key_response('[', 17, modifiers, '~', ch);
2214 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002215 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002216 len = function_key_response('[', 18, modifiers, '~', ch);
2217 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002218 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002219 len = function_key_response('[', 19, modifiers, '~', ch);
2220 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002221 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002222 len = function_key_response('[', 20, modifiers, '~', ch);
2223 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002224 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002225 len = function_key_response('[', 21, modifiers, '~', ch);
2226 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002227 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002228 len = function_key_response('[', 24, modifiers, '~', ch);
2229 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002230 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002231 /* Handle special keys with alternate mappings */
2232 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2233 if (len != 0) break;
2234
Kristian Høgsberg70163132012-05-08 15:55:39 -04002235 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002236 if (sym >= '3' && sym <= '7')
2237 sym = (sym & 0x1f) + 8;
2238
2239 if (!((sym >= '!' && sym <= '/') ||
2240 (sym >= '8' && sym <= '?') ||
2241 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2242 else if (sym == '2') sym = 0x00;
2243 else if (sym == '/') sym = 0x1F;
2244 else if (sym == '8' || sym == '?') sym = 0x7F;
Kristian Høgsbergae9e0732012-06-21 12:30:15 -04002245 }
2246 if ((terminal->mode & MODE_ALT_SENDS_ESC) &&
2247 (modifiers & MOD_ALT_MASK)) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002248 ch[len++] = 0x1b;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002249 } else if (modifiers & MOD_ALT_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002250 sym = sym | 0x80;
2251 }
2252
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002253 if (sym < 256)
2254 ch[len++] = sym;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002255 break;
2256 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002257
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002258 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04002259 terminal_write(terminal, ch, len);
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002260
2261 /* Hide cursor, except if this was coming from a
2262 * repeating key press. */
2263 serial = display_get_serial(terminal->display);
2264 if (terminal->hide_cursor_serial != serial) {
2265 input_set_pointer_image(input, CURSOR_BLANK);
2266 terminal->hide_cursor_serial = serial;
2267 }
2268 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002269}
2270
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002271static void
2272keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002273 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002274{
2275 struct terminal *terminal = data;
2276
2277 terminal->focused = (device != NULL);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002278 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002279}
2280
Kristian Høgsberg59826582011-01-20 11:56:57 -05002281static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002282button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002283 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002284 uint32_t button,
2285 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002286{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002287 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002288
2289 switch (button) {
2290 case 272:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002291 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002292 terminal->dragging = 1;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002293 input_get_position(input,
2294 &terminal->selection_start_x,
2295 &terminal->selection_start_y);
2296 terminal->selection_end_x = terminal->selection_start_x;
2297 terminal->selection_end_y = terminal->selection_start_y;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002298 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002299 } else {
2300 terminal->dragging = 0;
2301 }
2302 break;
2303 }
2304}
2305
2306static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002307motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002308 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002309 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002310{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002311 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002312
2313 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002314 input_get_position(input,
2315 &terminal->selection_end_x,
2316 &terminal->selection_end_y);
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002317 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002318 }
2319
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002320 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002321}
2322
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002323static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002324terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002325{
2326 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002327 cairo_surface_t *surface;
2328 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002329
2330 terminal = malloc(sizeof *terminal);
2331 if (terminal == NULL)
2332 return terminal;
2333
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002334 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002335 terminal->fullscreen = fullscreen;
Callum Lowcay30eeae52011-01-07 19:46:55 +00002336 terminal->color_scheme = &DEFAULT_COLORS;
2337 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002338 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002339 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002340 terminal->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002341 terminal->widget = frame_create(terminal->window, terminal);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002342 window_set_title(terminal->window, "Wayland Terminal");
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002343 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002344
2345 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002346 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002347
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002348 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002349 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002350
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002351 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002352 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002353 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002354 keyboard_focus_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002355 widget_set_redraw_handler(terminal->widget, redraw_handler);
2356 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002357 widget_set_button_handler(terminal->widget, button_handler);
2358 widget_set_motion_handler(terminal->widget, motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002359
Kristian Høgsberg09531622010-06-14 23:22:15 -04002360 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2361 cr = cairo_create(surface);
Kristian Høgsberg09531622010-06-14 23:22:15 -04002362 cairo_set_font_size(cr, 14);
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002363 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002364 CAIRO_FONT_SLANT_NORMAL,
2365 CAIRO_FONT_WEIGHT_BOLD);
2366 terminal->font_bold = cairo_get_scaled_font (cr);
2367 cairo_scaled_font_reference(terminal->font_bold);
2368
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002369 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002370 CAIRO_FONT_SLANT_NORMAL,
2371 CAIRO_FONT_WEIGHT_NORMAL);
2372 terminal->font_normal = cairo_get_scaled_font (cr);
2373 cairo_scaled_font_reference(terminal->font_normal);
2374
Kristian Høgsberg09531622010-06-14 23:22:15 -04002375 cairo_font_extents(cr, &terminal->extents);
2376 cairo_destroy(cr);
2377 cairo_surface_destroy(surface);
2378
Kristian Høgsberga1627922012-06-20 17:30:03 -04002379 terminal_resize(terminal, 80, 25);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002380
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002381 wl_list_insert(terminal_list.prev, &terminal->link);
2382
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002383 return terminal;
2384}
2385
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002386static void
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002387terminal_destroy(struct terminal *terminal)
2388{
2389 window_destroy(terminal->window);
2390 close(terminal->master);
2391 wl_list_remove(&terminal->link);
2392 free(terminal);
2393
2394 if (wl_list_empty(&terminal_list))
2395 exit(0);
2396}
2397
2398static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002399io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002400{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002401 struct terminal *terminal =
2402 container_of(task, struct terminal, io_task);
2403 char buffer[256];
2404 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002405
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002406 if (events & EPOLLHUP) {
2407 terminal_destroy(terminal);
2408 return;
2409 }
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002410
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002411 len = read(terminal->master, buffer, sizeof buffer);
2412 if (len < 0)
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002413 terminal_destroy(terminal);
2414 else
2415 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002416}
2417
2418static int
2419terminal_run(struct terminal *terminal, const char *path)
2420{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002421 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002422 pid_t pid;
2423
2424 pid = forkpty(&master, NULL, NULL, NULL);
2425 if (pid == 0) {
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04002426 setenv("TERM", option_term, 1);
2427 setenv("COLORTERM", option_term, 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002428 if (execl(path, path, NULL)) {
2429 printf("exec failed: %m\n");
2430 exit(EXIT_FAILURE);
2431 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002432 } else if (pid < 0) {
2433 fprintf(stderr, "failed to fork and create pty (%m).\n");
2434 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002435 }
2436
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002437 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002438 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002439 terminal->io_task.run = io_handler;
2440 display_watch_fd(terminal->display, terminal->master,
2441 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002442
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002443 window_set_fullscreen(terminal->window, terminal->fullscreen);
2444 if (!terminal->fullscreen)
2445 terminal_resize(terminal, 80, 24);
2446
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002447 return 0;
2448}
2449
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002450static const struct config_key terminal_config_keys[] = {
2451 { "font", CONFIG_KEY_STRING, &option_font },
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04002452 { "term", CONFIG_KEY_STRING, &option_term },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002453};
2454
2455static const struct config_section config_sections[] = {
2456 { "terminal",
2457 terminal_config_keys, ARRAY_LENGTH(terminal_config_keys) },
2458};
2459
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002460static const struct weston_option terminal_options[] = {
2461 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002462 { WESTON_OPTION_STRING, "font", 0, &option_font },
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002463 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002464};
2465
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002466int main(int argc, char *argv[])
2467{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002468 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002469 struct terminal *terminal;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002470 char *config_file;
2471
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002472 option_shell = getenv("SHELL");
2473 if (!option_shell)
2474 option_shell = "/bin/bash";
2475
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002476 config_file = config_file_path("weston.ini");
2477 parse_config_file(config_file,
2478 config_sections, ARRAY_LENGTH(config_sections),
2479 NULL);
2480 free(config_file);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002481
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002482 argc = parse_options(terminal_options,
2483 ARRAY_LENGTH(terminal_options), argc, argv);
2484
2485 d = display_create(argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002486 if (d == NULL) {
2487 fprintf(stderr, "failed to create display: %m\n");
2488 return -1;
2489 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002490
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002491 wl_list_init(&terminal_list);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002492 terminal = terminal_create(d, option_fullscreen);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002493 if (terminal_run(terminal, option_shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002494 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002495
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002496 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002497
2498 return 0;
2499}