blob: c8ea239f57759db3f64d2d3042ef9f2717462488 [file] [log] [blame]
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -07004 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050010 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -070011 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050022 */
23
Bryce Harringtonb4dae9b2016-06-15 18:13:07 -070024#include "config.h"
Peng Wucfcc1112013-08-19 10:59:21 +080025
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +020026#include <stdbool.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
34#include <time.h>
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050035#include <pty.h>
Kristian Høgsbergf04e8382008-12-08 00:07:49 -050036#include <ctype.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050037#include <cairo.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040038#include <sys/epoll.h>
Peng Wucfcc1112013-08-19 10:59:21 +080039#include <wchar.h>
40#include <locale.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050041
Kristian Høgsberg67b82152013-10-23 16:52:05 -070042#include <linux/input.h>
43
Pekka Paalanen50719bc2011-11-22 14:18:50 +020044#include <wayland-client.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050045
Jon Cruz4678bab2015-06-15 15:37:07 -070046#include "shared/config-parser.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070047#include "shared/helpers.h"
Bryce Harringtone99e4bf2016-03-16 14:15:18 -070048#include "shared/xalloc.h"
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050049#include "window.h"
50
Kristian Høgsberg0395f302008-12-22 12:14:50 -050051static int option_fullscreen;
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -070052static char *option_font;
53static int option_font_size;
54static char *option_term;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040055static char *option_shell;
56
57static struct wl_list terminal_list;
58
59static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -040060terminal_create(struct display *display);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040061static void
62terminal_destroy(struct terminal *terminal);
63static int
64terminal_run(struct terminal *terminal, const char *path);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050065
Peng Wuf291f202013-06-06 15:32:41 +080066#define TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS \
67 " !\"#$%&'()*+,-./" \
68 "0123456789" \
69 ":;<=>?@" \
70 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
71 "[\\]^_`" \
72 "abcdefghijklmnopqrstuvwxyz" \
73 "{|}~" \
74 ""
75
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050076#define MOD_SHIFT 0x01
77#define MOD_ALT 0x02
78#define MOD_CTRL 0x04
79
Callum Lowcay30eeae52011-01-07 19:46:55 +000080#define ATTRMASK_BOLD 0x01
81#define ATTRMASK_UNDERLINE 0x02
82#define ATTRMASK_BLINK 0x04
83#define ATTRMASK_INVERSE 0x08
Callum Lowcay81179db2011-01-10 12:14:01 +130084#define ATTRMASK_CONCEALED 0x10
Callum Lowcay30eeae52011-01-07 19:46:55 +000085
Callum Lowcayb8609ad2011-01-07 19:46:57 +000086/* Buffer sizes */
Callum Lowcayef57a9b2011-01-14 20:46:23 +130087#define MAX_RESPONSE 256
88#define MAX_ESCAPE 255
Callum Lowcayb8609ad2011-01-07 19:46:57 +000089
Callum Lowcay8e57dd52011-01-07 19:46:59 +000090/* Terminal modes */
91#define MODE_SHOW_CURSOR 0x00000001
92#define MODE_INVERSE 0x00000002
93#define MODE_AUTOWRAP 0x00000004
94#define MODE_AUTOREPEAT 0x00000008
95#define MODE_LF_NEWLINE 0x00000010
Callum Lowcay69e96582011-01-07 19:47:00 +000096#define MODE_IRM 0x00000020
Callum Lowcay7e08e902011-01-07 19:47:02 +000097#define MODE_DELETE_SENDS_DEL 0x00000040
98#define MODE_ALT_SENDS_ESC 0x00000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +000099
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000100union utf8_char {
101 unsigned char byte[4];
102 uint32_t ch;
103};
104
105enum utf8_state {
106 utf8state_start,
107 utf8state_accept,
108 utf8state_reject,
109 utf8state_expect3,
110 utf8state_expect2,
111 utf8state_expect1
112};
113
114struct utf8_state_machine {
115 enum utf8_state state;
116 int len;
117 union utf8_char s;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700118 uint32_t unicode;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000119};
120
121static void
122init_state_machine(struct utf8_state_machine *machine)
123{
124 machine->state = utf8state_start;
125 machine->len = 0;
126 machine->s.ch = 0;
Quentin Glidicc0271532016-07-10 11:00:57 +0200127 machine->unicode = 0;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000128}
129
130static enum utf8_state
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400131utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000132{
133 switch(machine->state) {
134 case utf8state_start:
135 case utf8state_accept:
136 case utf8state_reject:
137 machine->s.ch = 0;
138 machine->len = 0;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300139 if (c == 0xC0 || c == 0xC1) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000140 /* overlong encoding, reject */
141 machine->state = utf8state_reject;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300142 } else if ((c & 0x80) == 0) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000143 /* single byte, accept */
144 machine->s.byte[machine->len++] = c;
145 machine->state = utf8state_accept;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700146 machine->unicode = c;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300147 } else if ((c & 0xC0) == 0x80) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000148 /* parser out of sync, ignore byte */
149 machine->state = utf8state_start;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300150 } else if ((c & 0xE0) == 0xC0) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000151 /* start of two byte sequence */
152 machine->s.byte[machine->len++] = c;
153 machine->state = utf8state_expect1;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700154 machine->unicode = c & 0x1f;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300155 } else if ((c & 0xF0) == 0xE0) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000156 /* start of three byte sequence */
157 machine->s.byte[machine->len++] = c;
158 machine->state = utf8state_expect2;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700159 machine->unicode = c & 0x0f;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300160 } else if ((c & 0xF8) == 0xF0) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000161 /* start of four byte sequence */
162 machine->s.byte[machine->len++] = c;
163 machine->state = utf8state_expect3;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700164 machine->unicode = c & 0x07;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000165 } else {
166 /* overlong encoding, reject */
167 machine->state = utf8state_reject;
168 }
169 break;
170 case utf8state_expect3:
171 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700172 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300173 if ((c & 0xC0) == 0x80) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000174 /* all good, continue */
175 machine->state = utf8state_expect2;
176 } else {
177 /* missing extra byte, reject */
178 machine->state = utf8state_reject;
179 }
180 break;
181 case utf8state_expect2:
182 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700183 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300184 if ((c & 0xC0) == 0x80) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000185 /* all good, continue */
186 machine->state = utf8state_expect1;
187 } else {
188 /* missing extra byte, reject */
189 machine->state = utf8state_reject;
190 }
191 break;
192 case utf8state_expect1:
193 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700194 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300195 if ((c & 0xC0) == 0x80) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000196 /* all good, accept */
197 machine->state = utf8state_accept;
198 } else {
199 /* missing extra byte, reject */
200 machine->state = utf8state_reject;
201 }
202 break;
203 default:
204 machine->state = utf8state_reject;
205 break;
206 }
Michael Vetter2a18a522015-05-15 17:17:47 +0200207
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000208 return machine->state;
209}
210
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700211static uint32_t
212get_unicode(union utf8_char utf8)
213{
214 struct utf8_state_machine machine;
215 int i;
216
217 init_state_machine(&machine);
218 for (i = 0; i < 4; i++) {
219 utf8_next_char(&machine, utf8.byte[i]);
220 if (machine.state == utf8state_accept ||
221 machine.state == utf8state_reject)
222 break;
223 }
224
225 if (machine.state == utf8state_reject)
226 return 0xfffd;
227
228 return machine.unicode;
229}
230
Peng Wucfcc1112013-08-19 10:59:21 +0800231static bool
232is_wide(union utf8_char utf8)
233{
234 uint32_t unichar = get_unicode(utf8);
235 return wcwidth(unichar) > 1;
236}
237
Callum Lowcay256e72f2011-01-07 19:47:01 +0000238struct char_sub {
239 union utf8_char match;
240 union utf8_char replace;
241};
242/* Set last char_sub match to NULL char */
243typedef struct char_sub *character_set;
244
245struct char_sub CS_US[] = {
246 {{{0, }}, {{0, }}}
247};
248static struct char_sub CS_UK[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200249 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000250 {{{0, }}, {{0, }}}
251};
252static struct char_sub CS_SPECIAL[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200253 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond: ♦ */
254 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell: ▒ */
255 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT: ␉ */
256 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF: ␌ */
257 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR: ␍ */
258 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF: ␊ */
259 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree: ° */
260 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus: ± */
261 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL: ␤ */
262 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT: ␋ */
263 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB: ┘ */
264 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT: ┐ */
265 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT: ┌ */
266 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_LB: └ */
267 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS: ┼ */
268 {{{'o', 0, }}, {{0xE2, 0x8E, 0xBA, 0}}}, /* Horiz. Scan Line 1: ⎺ */
269 {{{'p', 0, }}, {{0xE2, 0x8E, 0xBB, 0}}}, /* Horiz. Scan Line 3: ⎻ */
270 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* Horiz. Scan Line 5: ─ */
271 {{{'r', 0, }}, {{0xE2, 0x8E, 0xBC, 0}}}, /* Horiz. Scan Line 7: ⎼ */
272 {{{'s', 0, }}, {{0xE2, 0x8E, 0xBD, 0}}}, /* Horiz. Scan Line 9: ⎽ */
273 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR: ├ */
274 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL: ┤ */
275 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU: ┴ */
276 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD: ┬ */
277 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V: │ */
278 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE: ≤ */
279 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE: ≥ */
280 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI: π */
281 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ: ≠ */
282 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
283 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT: ⋅ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000284 {{{0, }}, {{0, }}}
285};
286
287static void
288apply_char_set(character_set cs, union utf8_char *utf8)
289{
290 int i = 0;
Michael Vetter2a18a522015-05-15 17:17:47 +0200291
Callum Lowcay256e72f2011-01-07 19:47:01 +0000292 while (cs[i].match.byte[0]) {
293 if ((*utf8).ch == cs[i].match.ch) {
294 *utf8 = cs[i].replace;
295 break;
296 }
297 i++;
298 }
299}
300
Callum Lowcay7e08e902011-01-07 19:47:02 +0000301struct key_map {
302 int sym;
303 int num;
304 char escape;
305 char code;
306};
307/* Set last key_sub sym to NULL */
308typedef struct key_map *keyboard_mode;
309
310static struct key_map KM_NORMAL[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400311 { XKB_KEY_Left, 1, '[', 'D' },
312 { XKB_KEY_Right, 1, '[', 'C' },
313 { XKB_KEY_Up, 1, '[', 'A' },
314 { XKB_KEY_Down, 1, '[', 'B' },
315 { XKB_KEY_Home, 1, '[', 'H' },
316 { XKB_KEY_End, 1, '[', 'F' },
317 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000318};
319static struct key_map KM_APPLICATION[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400320 { XKB_KEY_Left, 1, 'O', 'D' },
321 { XKB_KEY_Right, 1, 'O', 'C' },
322 { XKB_KEY_Up, 1, 'O', 'A' },
323 { XKB_KEY_Down, 1, 'O', 'B' },
324 { XKB_KEY_Home, 1, 'O', 'H' },
325 { XKB_KEY_End, 1, 'O', 'F' },
326 { XKB_KEY_KP_Enter, 1, 'O', 'M' },
327 { XKB_KEY_KP_Multiply, 1, 'O', 'j' },
328 { XKB_KEY_KP_Add, 1, 'O', 'k' },
329 { XKB_KEY_KP_Separator, 1, 'O', 'l' },
330 { XKB_KEY_KP_Subtract, 1, 'O', 'm' },
331 { XKB_KEY_KP_Divide, 1, 'O', 'o' },
332 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000333};
334
335static int
336function_key_response(char escape, int num, uint32_t modifiers,
337 char code, char *response)
338{
339 int mod_num = 0;
340 int len;
341
Kristian Høgsberg70163132012-05-08 15:55:39 -0400342 if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
343 if (modifiers & MOD_ALT_MASK) mod_num |= 2;
344 if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000345
346 if (mod_num != 0)
347 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
348 num, mod_num + 1, code);
349 else if (code != '~')
350 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
351 escape, code);
352 else
353 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
354 escape, num, code);
355
356 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
357 else return len;
358}
359
360/* returns the number of bytes written into response,
361 * which must have room for MAX_RESPONSE bytes */
362static int
363apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
364{
365 struct key_map map;
366 int len = 0;
367 int i = 0;
Michael Vetter2a18a522015-05-15 17:17:47 +0200368
Callum Lowcay7e08e902011-01-07 19:47:02 +0000369 while (mode[i].sym) {
370 map = mode[i++];
371 if (sym == map.sym) {
372 len = function_key_response(map.escape, map.num,
373 modifiers, map.code,
374 response);
375 break;
376 }
377 }
Michael Vetter2a18a522015-05-15 17:17:47 +0200378
Callum Lowcay7e08e902011-01-07 19:47:02 +0000379 return len;
380}
381
Callum Lowcay30eeae52011-01-07 19:46:55 +0000382struct terminal_color { double r, g, b, a; };
383struct attr {
384 unsigned char fg, bg;
385 char a; /* attributes format:
386 * 76543210
Callum Lowcay81179db2011-01-10 12:14:01 +1300387 * cilub */
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500388 char s; /* in selection */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000389};
390struct color_scheme {
391 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500392 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000393 struct attr default_attr;
394};
395
396static void
397attr_init(struct attr *data_attr, struct attr attr, int n)
398{
399 int i;
400 for (i = 0; i < n; i++) {
401 data_attr[i] = attr;
402 }
403}
404
Callum Lowcay67a201d2011-01-12 19:23:41 +1300405enum escape_state {
406 escape_state_normal = 0,
407 escape_state_escape,
408 escape_state_dcs,
409 escape_state_csi,
410 escape_state_osc,
411 escape_state_inner_escape,
412 escape_state_ignore,
413 escape_state_special
414};
415
416#define ESC_FLAG_WHAT 0x01
417#define ESC_FLAG_GT 0x02
418#define ESC_FLAG_BANG 0x04
419#define ESC_FLAG_CASH 0x08
420#define ESC_FLAG_SQUOTE 0x10
421#define ESC_FLAG_DQUOTE 0x20
422#define ESC_FLAG_SPACE 0x40
423
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400424enum {
425 SELECT_NONE,
426 SELECT_CHAR,
427 SELECT_WORD,
428 SELECT_LINE
429};
430
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500431struct terminal {
432 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500433 struct widget *widget;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500434 struct display *display;
Kristian Høgsberga83be202013-10-23 20:47:35 -0700435 char *title;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000436 union utf8_char *data;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400437 struct task io_task;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000438 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000439 struct attr *data_attr;
440 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000441 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000442 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000443 char saved_origin_mode;
444 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000445 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000446 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000447 character_set cs, g0, g1;
448 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000449 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000450 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700451 int width, height, row, column, max_width;
452 uint32_t buffer_height;
453 uint32_t start, end, saved_start, log_size;
Magnus Hoff1046f122014-08-05 15:05:59 +0200454 wl_fixed_t smooth_scroll;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000455 int saved_row, saved_column;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700456 int scrolling;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600457 int send_cursor_position;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500458 int fd, master;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500459 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300460 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500461 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300462 enum escape_state state;
463 enum escape_state outer_state;
464 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000465 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500466 int margin;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400467 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000468 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400469 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +0800470 double average_width;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500471 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg88fd4082012-06-21 15:55:03 -0400472 uint32_t hide_cursor_serial;
Kristian Høgsbergb405a802014-02-05 14:44:04 -0800473 int size_in_title;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500474
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400475 struct wl_data_source *selection;
Xiong Zhang49c6aee2013-11-25 18:42:52 +0800476 uint32_t click_time;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400477 int dragging, click_count;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500478 int selection_start_x, selection_start_y;
479 int selection_end_x, selection_end_y;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400480 int selection_start_row, selection_start_col;
481 int selection_end_row, selection_end_col;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -0400482 struct wl_list link;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500483};
484
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000485/* Create default tab stops, every 8 characters */
486static void
487terminal_init_tabs(struct terminal *terminal)
488{
489 int i = 0;
Michael Vetter2a18a522015-05-15 17:17:47 +0200490
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000491 while (i < terminal->width) {
492 if (i % 8 == 0)
493 terminal->tab_ruler[i] = 1;
494 else
495 terminal->tab_ruler[i] = 0;
496 i++;
497 }
498}
499
Callum Lowcay30eeae52011-01-07 19:46:55 +0000500static void
501terminal_init(struct terminal *terminal)
502{
503 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000504 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000505 terminal->mode = MODE_SHOW_CURSOR |
506 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000507 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000508 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000509
510 terminal->row = 0;
511 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000512
Callum Lowcay256e72f2011-01-07 19:47:01 +0000513 terminal->g0 = CS_US;
514 terminal->g1 = CS_US;
515 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000516 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000517
518 terminal->saved_g0 = terminal->g0;
519 terminal->saved_g1 = terminal->g1;
520 terminal->saved_cs = terminal->cs;
521
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000522 terminal->saved_attr = terminal->curr_attr;
523 terminal->saved_origin_mode = terminal->origin_mode;
524 terminal->saved_row = terminal->row;
525 terminal->saved_column = terminal->column;
526
527 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000528}
529
530static void
531init_color_table(struct terminal *terminal)
532{
533 int c, r;
534 struct terminal_color *color_table = terminal->color_table;
535
536 for (c = 0; c < 256; c ++) {
537 if (c < 16) {
538 color_table[c] = terminal->color_scheme->palette[c];
539 } else if (c < 232) {
540 r = c - 16;
541 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
542 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
543 color_table[c].r = ((double)(r % 6) / 6.0);
544 color_table[c].a = 1.0;
545 } else {
546 r = (c - 232) * 10 + 8;
547 color_table[c].r = ((double) r) / 256.0;
548 color_table[c].g = color_table[c].r;
549 color_table[c].b = color_table[c].r;
550 color_table[c].a = 1.0;
551 }
552 }
553}
554
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000555static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500556terminal_get_row(struct terminal *terminal, int row)
557{
558 int index;
559
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700560 index = (row + terminal->start) & (terminal->buffer_height - 1);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500561
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700562 return (void *) terminal->data + index * terminal->data_pitch;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500563}
564
Callum Lowcay30eeae52011-01-07 19:46:55 +0000565static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500566terminal_get_attr_row(struct terminal *terminal, int row)
567{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000568 int index;
569
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700570 index = (row + terminal->start) & (terminal->buffer_height - 1);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000571
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700572 return (void *) terminal->data_attr + index * terminal->attr_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000573}
574
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500575union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300576 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500577 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500578};
579
580static void
581terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500582 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500583{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500584 struct attr attr;
585 int foreground, background, tmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500586
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500587 decoded->attr.s = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400588 if (((row == terminal->selection_start_row &&
589 col >= terminal->selection_start_col) ||
590 row > terminal->selection_start_row) &&
591 ((row == terminal->selection_end_row &&
592 col < terminal->selection_end_col) ||
593 row < terminal->selection_end_row))
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500594 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500595
596 /* get the attributes for this character cell */
597 attr = terminal_get_attr_row(terminal, row)[col];
598 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500599 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500600 ((terminal->mode & MODE_SHOW_CURSOR) &&
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400601 window_has_focus(terminal->window) && terminal->row == row &&
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500602 terminal->column == col)) {
603 foreground = attr.bg;
604 background = attr.fg;
605 if (attr.a & ATTRMASK_BOLD) {
606 if (foreground <= 16) foreground |= 0x08;
607 if (background <= 16) background &= 0x07;
608 }
609 } else {
610 foreground = attr.fg;
611 background = attr.bg;
612 }
613
614 if (terminal->mode & MODE_INVERSE) {
615 tmp = foreground;
616 foreground = background;
617 background = tmp;
618 if (attr.a & ATTRMASK_BOLD) {
619 if (foreground <= 16) foreground |= 0x08;
620 if (background <= 16) background &= 0x07;
621 }
622 }
623
Callum Lowcay9d708b02011-01-12 20:06:17 +1300624 decoded->attr.fg = foreground;
625 decoded->attr.bg = background;
626 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000627}
628
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500629
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500630static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000631terminal_scroll_buffer(struct terminal *terminal, int d)
632{
633 int i;
634
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700635 terminal->start += d;
636 if (d < 0) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000637 d = 0 - d;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700638 for (i = 0; i < d; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000639 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
640 attr_init(terminal_get_attr_row(terminal, i),
641 terminal->curr_attr, terminal->width);
642 }
643 } else {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700644 for (i = terminal->height - d; i < terminal->height; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000645 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
646 attr_init(terminal_get_attr_row(terminal, i),
647 terminal->curr_attr, terminal->width);
648 }
649 }
Kristian Høgsberg18e928d2012-06-27 19:29:41 -0400650
651 terminal->selection_start_row -= d;
652 terminal->selection_end_row -= d;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000653}
654
655static void
656terminal_scroll_window(struct terminal *terminal, int d)
657{
658 int i;
659 int window_height;
660 int from_row, to_row;
Michael Vetter2a18a522015-05-15 17:17:47 +0200661
Callum Lowcaybbeac602011-01-07 19:46:58 +0000662 // scrolling range is inclusive
663 window_height = terminal->margin_bottom - terminal->margin_top + 1;
664 d = d % (window_height + 1);
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300665 if (d < 0) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000666 d = 0 - d;
667 to_row = terminal->margin_bottom;
668 from_row = terminal->margin_bottom - d;
Michael Vetter2a18a522015-05-15 17:17:47 +0200669
Callum Lowcaybbeac602011-01-07 19:46:58 +0000670 for (i = 0; i < (window_height - d); i++) {
671 memcpy(terminal_get_row(terminal, to_row - i),
672 terminal_get_row(terminal, from_row - i),
673 terminal->data_pitch);
674 memcpy(terminal_get_attr_row(terminal, to_row - i),
675 terminal_get_attr_row(terminal, from_row - i),
676 terminal->attr_pitch);
677 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000678 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
679 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000680 attr_init(terminal_get_attr_row(terminal, i),
681 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000682 }
683 } else {
684 to_row = terminal->margin_top;
685 from_row = terminal->margin_top + d;
Michael Vetter2a18a522015-05-15 17:17:47 +0200686
Callum Lowcaybbeac602011-01-07 19:46:58 +0000687 for (i = 0; i < (window_height - d); i++) {
688 memcpy(terminal_get_row(terminal, to_row + i),
689 terminal_get_row(terminal, from_row + i),
690 terminal->data_pitch);
691 memcpy(terminal_get_attr_row(terminal, to_row + i),
692 terminal_get_attr_row(terminal, from_row + i),
693 terminal->attr_pitch);
694 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000695 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
696 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000697 attr_init(terminal_get_attr_row(terminal, i),
698 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000699 }
700 }
701}
702
703static void
704terminal_scroll(struct terminal *terminal, int d)
705{
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300706 if (terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
Callum Lowcaybbeac602011-01-07 19:46:58 +0000707 terminal_scroll_buffer(terminal, d);
708 else
709 terminal_scroll_window(terminal, d);
710}
711
712static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000713terminal_shift_line(struct terminal *terminal, int d)
714{
715 union utf8_char *row;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500716 struct attr *attr_row;
Michael Vetter2a18a522015-05-15 17:17:47 +0200717
Callum Lowcay69e96582011-01-07 19:47:00 +0000718 row = terminal_get_row(terminal, terminal->row);
719 attr_row = terminal_get_attr_row(terminal, terminal->row);
720
721 if ((terminal->width + d) <= terminal->column)
722 d = terminal->column + 1 - terminal->width;
723 if ((terminal->column + d) >= terminal->width)
724 d = terminal->width - terminal->column - 1;
Michael Vetter2a18a522015-05-15 17:17:47 +0200725
Callum Lowcay69e96582011-01-07 19:47:00 +0000726 if (d < 0) {
727 d = 0 - d;
728 memmove(&row[terminal->column],
729 &row[terminal->column + d],
730 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
Callum Lowcay69e96582011-01-07 19:47:00 +0000731 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
732 (terminal->width - terminal->column - d) * sizeof(struct attr));
733 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
734 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
735 } else {
736 memmove(&row[terminal->column + d], &row[terminal->column],
737 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
738 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
739 (terminal->width - terminal->column - d) * sizeof(struct attr));
740 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
741 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
742 }
743}
744
745static void
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700746terminal_resize_cells(struct terminal *terminal,
747 int width, int height)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500748{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000749 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000750 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000751 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000752 int data_pitch, attr_pitch;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500753 int i, l, total_rows;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700754 uint32_t d, uheight = height;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500755 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000756 struct winsize ws;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500757
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700758 if (uheight > terminal->buffer_height)
759 height = terminal->buffer_height;
760
Kristian Høgsberg22106762008-12-08 13:50:07 -0500761 if (terminal->width == width && terminal->height == height)
762 return;
763
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700764 if (terminal->data && width <= terminal->max_width) {
765 d = 0;
766 if (height < terminal->height && height <= terminal->row)
767 d = terminal->height - height;
768 else if (height > terminal->height &&
769 terminal->height - 1 == terminal->row) {
770 d = terminal->height - height;
771 if (terminal->log_size < uheight)
772 d = -terminal->start;
773 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500774
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700775 terminal->start += d;
776 terminal->row -= d;
777 } else {
778 terminal->max_width = width;
779 data_pitch = width * sizeof(union utf8_char);
Bryce W. Harrington66cd2c12014-04-28 18:44:08 +0000780 data = xzalloc(data_pitch * terminal->buffer_height);
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700781 attr_pitch = width * sizeof(struct attr);
Bryce W. Harrington66cd2c12014-04-28 18:44:08 +0000782 data_attr = xmalloc(attr_pitch * terminal->buffer_height);
783 tab_ruler = xzalloc(width);
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700784 attr_init(data_attr, terminal->curr_attr,
785 width * terminal->buffer_height);
786
787 if (terminal->data && terminal->data_attr) {
788 if (width > terminal->width)
789 l = terminal->width;
790 else
791 l = width;
792
793 if (terminal->height > height) {
794 total_rows = height;
795 i = 1 + terminal->row - height;
796 if (i > 0) {
797 terminal->start += i;
798 terminal->row = terminal->row - i;
799 }
800 } else {
801 total_rows = terminal->height;
José Bollo4a4704a2013-09-13 11:28:51 +0200802 }
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700803
804 for (i = 0; i < total_rows; i++) {
805 memcpy(&data[width * i],
806 terminal_get_row(terminal, i),
807 l * sizeof(union utf8_char));
808 memcpy(&data_attr[width * i],
809 terminal_get_attr_row(terminal, i),
810 l * sizeof(struct attr));
811 }
812
813 free(terminal->data);
814 free(terminal->data_attr);
815 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500816 }
817
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700818 terminal->data_pitch = data_pitch;
819 terminal->attr_pitch = attr_pitch;
820 terminal->data = data;
821 terminal->data_attr = data_attr;
822 terminal->tab_ruler = tab_ruler;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700823 terminal->start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500824 }
825
Callum Lowcay86653ed2011-01-07 19:47:03 +0000826 terminal->margin_bottom =
827 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500828 terminal->width = width;
829 terminal->height = height;
Kristian Høgsbergdcfff552013-11-22 22:43:20 -0800830 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500831
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000832 /* Update the window size */
833 ws.ws_row = terminal->height;
834 ws.ws_col = terminal->width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500835 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500836 ws.ws_xpixel = allocation.width;
837 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000838 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500839}
840
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500841static void
Jasper St. Pierrede680992014-04-10 17:23:49 -0700842update_title(struct terminal *terminal)
843{
844 if (window_is_resizing(terminal->window)) {
845 char *p;
846 if (asprintf(&p, "%s — [%dx%d]", terminal->title, terminal->width, terminal->height) > 0) {
847 window_set_title(terminal->window, p);
848 free(p);
849 }
850 } else {
851 window_set_title(terminal->window, terminal->title);
852 }
853}
854
855static void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500856resize_handler(struct widget *widget,
857 int32_t width, int32_t height, void *data)
858{
859 struct terminal *terminal = data;
860 int32_t columns, rows, m;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700861
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500862 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800863 columns = (width - m) / (int32_t) terminal->average_width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500864 rows = (height - m) / (int32_t) terminal->extents.height;
865
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400866 if (!window_is_fullscreen(terminal->window) &&
867 !window_is_maximized(terminal->window)) {
Peng Wuf291f202013-06-06 15:32:41 +0800868 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500869 height = rows * terminal->extents.height + m;
870 widget_set_size(terminal->widget, width, height);
871 }
872
873 terminal_resize_cells(terminal, columns, rows);
Jasper St. Pierrede680992014-04-10 17:23:49 -0700874 update_title(terminal);
875}
876
877static void
878state_changed_handler(struct window *window, void *data)
879{
880 struct terminal *terminal = data;
881 update_title(terminal);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500882}
883
884static void
885terminal_resize(struct terminal *terminal, int columns, int rows)
886{
887 int32_t width, height, m;
888
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400889 if (window_is_fullscreen(terminal->window) ||
890 window_is_maximized(terminal->window))
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500891 return;
892
893 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800894 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500895 height = rows * terminal->extents.height + m;
Kristian Høgsberga1627922012-06-20 17:30:03 -0400896
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500897 window_frame_set_child_size(terminal->widget, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500898}
899
Callum Lowcay30eeae52011-01-07 19:46:55 +0000900struct color_scheme DEFAULT_COLORS = {
901 {
902 {0, 0, 0, 1}, /* black */
903 {0.66, 0, 0, 1}, /* red */
904 {0 , 0.66, 0, 1}, /* green */
905 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
906 {0 , 0 , 0.66, 1}, /* blue */
907 {0.66, 0 , 0.66, 1}, /* magenta */
908 {0, 0.66, 0.66, 1}, /* cyan */
909 {0.66, 0.66, 0.66, 1}, /* light grey */
910 {0.22, 0.33, 0.33, 1}, /* dark grey */
911 {1, 0.33, 0.33, 1}, /* high red */
912 {0.33, 1, 0.33, 1}, /* high green */
913 {1, 1, 0.33, 1}, /* high yellow */
914 {0.33, 0.33, 1, 1}, /* high blue */
915 {1, 0.33, 1, 1}, /* high magenta */
916 {0.33, 1, 1, 1}, /* high cyan */
917 {1, 1, 1, 1} /* white */
918 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500919 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000920 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
921};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400922
Kristian Høgsberg22106762008-12-08 13:50:07 -0500923static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500924terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
925{
926 cairo_set_source_rgba(cr,
927 terminal->color_table[index].r,
928 terminal->color_table[index].g,
929 terminal->color_table[index].b,
930 terminal->color_table[index].a);
931}
932
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500933static void
934terminal_send_selection(struct terminal *terminal, int fd)
935{
936 int row, col;
937 union utf8_char *p_row;
938 union decoded_attr attr;
939 FILE *fp;
940 int len;
941
942 fp = fdopen(fd, "w");
Brian Lovin1bf14812013-08-08 16:12:55 -0700943 if (fp == NULL){
944 close(fd);
945 return;
946 }
Manuel Bachmanne54cee12015-09-19 13:51:05 +0200947 for (row = terminal->selection_start_row; row < terminal->height; row++) {
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500948 p_row = terminal_get_row(terminal, row);
949 for (col = 0; col < terminal->width; col++) {
Peng Wucfcc1112013-08-19 10:59:21 +0800950 if (p_row[col].ch == 0x200B) /* space glyph */
951 continue;
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500952 /* get the attributes for this character cell */
953 terminal_decode_attr(terminal, row, col, &attr);
954 if (!attr.attr.s)
955 continue;
956 len = strnlen((char *) p_row[col].byte, 4);
Kristian Høgsberg0dee6472012-07-01 21:25:41 -0400957 if (len > 0)
958 fwrite(p_row[col].byte, 1, len, fp);
959 if (len == 0 || col == terminal->width - 1) {
960 fwrite("\n", 1, 1, fp);
961 break;
962 }
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500963 }
964 }
965 fclose(fp);
966}
967
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500968struct glyph_run {
969 struct terminal *terminal;
970 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400971 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500972 union decoded_attr attr;
973 cairo_glyph_t glyphs[256], *g;
974};
975
976static void
977glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
978{
979 run->terminal = terminal;
980 run->cr = cr;
981 run->g = run->glyphs;
982 run->count = 0;
983 run->attr.key = 0;
984}
985
986static void
987glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
988{
989 cairo_scaled_font_t *font;
990
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500991 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
992 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300993 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500994 font = run->terminal->font_bold;
995 else
996 font = run->terminal->font_normal;
997 cairo_set_scaled_font(run->cr, font);
998 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300999 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001000
Callum Lowcay9d708b02011-01-12 20:06:17 +13001001 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
1002 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001003 run->g = run->glyphs;
1004 run->count = 0;
1005 }
Callum Lowcay9d708b02011-01-12 20:06:17 +13001006 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001007}
1008
1009static void
1010glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
1011{
1012 int num_glyphs;
1013 cairo_scaled_font_t *font;
1014
1015 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
1016
Callum Lowcay9d708b02011-01-12 20:06:17 +13001017 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001018 font = run->terminal->font_bold;
1019 else
1020 font = run->terminal->font_normal;
1021
1022 cairo_move_to(run->cr, x, y);
1023 cairo_scaled_font_text_to_glyphs (font, x, y,
1024 (char *) c->byte, 4,
1025 &run->g, &num_glyphs,
1026 NULL, NULL, NULL);
1027 run->g += num_glyphs;
1028 run->count += num_glyphs;
1029}
1030
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001031
Kristian Høgsbergf106fd52011-01-11 10:11:39 -05001032static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001033redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001034{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001035 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001036 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001037 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001038 int top_margin, side_margin;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001039 int row, col, cursor_x, cursor_y;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001040 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001041 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001042 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001043 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001044 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001045 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -05001046 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +08001047 double average_width;
Peng Wucfcc1112013-08-19 10:59:21 +08001048 double unichar_width;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001049
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001050 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001051 widget_get_allocation(terminal->widget, &allocation);
Alexander Larssonde79dd02013-05-22 14:41:34 +02001052 cr = widget_cairo_create(terminal->widget);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001053 cairo_rectangle(cr, allocation.x, allocation.y,
1054 allocation.width, allocation.height);
1055 cairo_clip(cr);
1056 cairo_push_group(cr);
1057
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001058 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -05001059 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001060 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001061
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05001062 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001063
Kristian Høgsberg59826582011-01-20 11:56:57 -05001064 extents = terminal->extents;
Peng Wuf291f202013-06-06 15:32:41 +08001065 average_width = terminal->average_width;
1066 side_margin = (allocation.width - terminal->width * average_width) / 2;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001067 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001068
Callum Lowcay30eeae52011-01-07 19:46:55 +00001069 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001070 cairo_translate(cr, allocation.x + side_margin,
1071 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001072 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001073 for (row = 0; row < terminal->height; row++) {
Peng Wucfcc1112013-08-19 10:59:21 +08001074 p_row = terminal_get_row(terminal, row);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001075 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001076 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -05001077 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001078
Callum Lowcay9d708b02011-01-12 20:06:17 +13001079 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -05001080 continue;
1081
Peng Wucfcc1112013-08-19 10:59:21 +08001082 if (is_wide(p_row[col]))
1083 unichar_width = 2 * average_width;
1084 else
1085 unichar_width = average_width;
1086
Callum Lowcay9d708b02011-01-12 20:06:17 +13001087 terminal_set_color(terminal, cr, attr.attr.bg);
Peng Wuf291f202013-06-06 15:32:41 +08001088 cairo_move_to(cr, col * average_width,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001089 row * extents.height);
Peng Wucfcc1112013-08-19 10:59:21 +08001090 cairo_rel_line_to(cr, unichar_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001091 cairo_rel_line_to(cr, 0, extents.height);
Peng Wucfcc1112013-08-19 10:59:21 +08001092 cairo_rel_line_to(cr, -unichar_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001093 cairo_close_path(cr);
1094 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001095 }
1096 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001097
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001098 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1099
1100 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001101 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001102 for (row = 0; row < terminal->height; row++) {
1103 p_row = terminal_get_row(terminal, row);
1104 for (col = 0; col < terminal->width; col++) {
1105 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -05001106 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001107
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001108 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001109
Peng Wuf291f202013-06-06 15:32:41 +08001110 text_x = col * average_width;
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001111 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +13001112 if (attr.attr.a & ATTRMASK_UNDERLINE) {
1113 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +00001114 cairo_move_to(cr, text_x, (double)text_y + 1.5);
Peng Wuf291f202013-06-06 15:32:41 +08001115 cairo_line_to(cr, text_x + average_width, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001116 cairo_stroke(cr);
1117 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -05001118
Daiki Ueno56d8a7a2014-04-08 18:46:18 +09001119 /* skip space glyph (RLE) we use as a placeholder of
1120 the right half of a double-width character,
1121 because RLE is not available in every font. */
1122 if (p_row[col].ch == 0x200B)
1123 continue;
1124
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001125 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001126 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001127 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001128
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001129 attr.key = ~0;
1130 glyph_run_flush(&run, attr);
1131
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001132 if ((terminal->mode & MODE_SHOW_CURSOR) &&
1133 !window_has_focus(terminal->window)) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001134 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001135
Callum Lowcay30eeae52011-01-07 19:46:55 +00001136 cairo_set_line_width(cr, 1);
Peng Wuf291f202013-06-06 15:32:41 +08001137 cairo_move_to(cr, terminal->column * average_width + d,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001138 terminal->row * extents.height + d);
Peng Wuf291f202013-06-06 15:32:41 +08001139 cairo_rel_line_to(cr, average_width - 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001140 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
Peng Wuf291f202013-06-06 15:32:41 +08001141 cairo_rel_line_to(cr, -average_width + 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001142 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001143
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001144 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001145 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001146
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001147 cairo_pop_group_to_source(cr);
1148 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001149 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001150 cairo_surface_destroy(surface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001151
1152 if (terminal->send_cursor_position) {
1153 cursor_x = side_margin + allocation.x +
Peng Wuf291f202013-06-06 15:32:41 +08001154 terminal->column * average_width;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001155 cursor_y = top_margin + allocation.y +
1156 terminal->row * extents.height;
1157 window_set_text_cursor_position(terminal->window,
1158 cursor_x, cursor_y);
1159 terminal->send_cursor_position = 0;
1160 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001161}
1162
1163static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001164terminal_write(struct terminal *terminal, const char *data, size_t length)
1165{
1166 if (write(terminal->master, data, length) < 0)
1167 abort();
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001168 terminal->send_cursor_position = 1;
Kristian Høgsberg26130862011-08-24 11:30:21 -04001169}
1170
1171static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001172terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001173
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001174static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001175handle_char(struct terminal *terminal, union utf8_char utf8);
1176
1177static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001178handle_sgr(struct terminal *terminal, int code);
1179
1180static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001181handle_term_parameter(struct terminal *terminal, int code, int sr)
1182{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001183 int i;
1184
Callum Lowcay67a201d2011-01-12 19:23:41 +13001185 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001186 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001187 case 1: /* DECCKM */
1188 if (sr) terminal->key_mode = KM_APPLICATION;
1189 else terminal->key_mode = KM_NORMAL;
1190 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001191 case 2: /* DECANM */
1192 /* No VT52 support yet */
1193 terminal->g0 = CS_US;
1194 terminal->g1 = CS_US;
1195 terminal->cs = terminal->g0;
1196 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001197 case 3: /* DECCOLM */
1198 if (sr)
1199 terminal_resize(terminal, 132, 24);
1200 else
1201 terminal_resize(terminal, 80, 24);
Michael Vetter2a18a522015-05-15 17:17:47 +02001202
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001203 /* set columns, but also home cursor and clear screen */
1204 terminal->row = 0; terminal->column = 0;
1205 for (i = 0; i < terminal->height; i++) {
1206 memset(terminal_get_row(terminal, i),
1207 0, terminal->data_pitch);
1208 attr_init(terminal_get_attr_row(terminal, i),
1209 terminal->curr_attr, terminal->width);
1210 }
1211 break;
1212 case 5: /* DECSCNM */
1213 if (sr) terminal->mode |= MODE_INVERSE;
1214 else terminal->mode &= ~MODE_INVERSE;
1215 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001216 case 6: /* DECOM */
1217 terminal->origin_mode = sr;
1218 if (terminal->origin_mode)
1219 terminal->row = terminal->margin_top;
1220 else
1221 terminal->row = 0;
1222 terminal->column = 0;
1223 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001224 case 7: /* DECAWM */
1225 if (sr) terminal->mode |= MODE_AUTOWRAP;
1226 else terminal->mode &= ~MODE_AUTOWRAP;
1227 break;
1228 case 8: /* DECARM */
1229 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1230 else terminal->mode &= ~MODE_AUTOREPEAT;
1231 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001232 case 12: /* Very visible cursor (CVVIS) */
1233 /* FIXME: What do we do here. */
1234 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001235 case 25:
1236 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1237 else terminal->mode &= ~MODE_SHOW_CURSOR;
1238 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001239 case 1034: /* smm/rmm, meta mode on/off */
1240 /* ignore */
1241 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001242 case 1037: /* deleteSendsDel */
1243 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1244 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1245 break;
1246 case 1039: /* altSendsEscape */
1247 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1248 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1249 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001250 case 1049: /* rmcup/smcup, alternate screen */
1251 /* Ignore. Should be possible to implement,
1252 * but it's kind of annoying. */
1253 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001254 default:
1255 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1256 break;
1257 }
1258 } else {
1259 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001260 case 4: /* IRM */
1261 if (sr) terminal->mode |= MODE_IRM;
1262 else terminal->mode &= ~MODE_IRM;
1263 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001264 case 20: /* LNM */
1265 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1266 else terminal->mode &= ~MODE_LF_NEWLINE;
1267 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001268 default:
1269 fprintf(stderr, "Unknown parameter: %d\n", code);
1270 break;
1271 }
1272 }
1273}
1274
1275static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001276handle_dcs(struct terminal *terminal)
1277{
1278}
1279
1280static void
1281handle_osc(struct terminal *terminal)
1282{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001283 char *p;
1284 int code;
1285
1286 terminal->escape[terminal->escape_length++] = '\0';
1287 p = &terminal->escape[2];
1288 code = strtol(p, &p, 10);
1289 if (*p == ';') p++;
1290
1291 switch (code) {
1292 case 0: /* Icon name and window title */
1293 case 1: /* Icon label */
1294 case 2: /* Window title*/
Kristian Høgsberga83be202013-10-23 20:47:35 -07001295 free(terminal->title);
1296 terminal->title = strdup(p);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001297 window_set_title(terminal->window, p);
1298 break;
Kristian Høgsberg8a205002013-09-11 11:52:56 -07001299 case 7: /* shell cwd as uri */
1300 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001301 default:
Kristian Høgsberg8a205002013-09-11 11:52:56 -07001302 fprintf(stderr, "Unknown OSC escape code %d, text %s\n",
1303 code, p);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001304 break;
1305 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001306}
1307
1308static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001309handle_escape(struct terminal *terminal)
1310{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001311 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001312 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001313 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001314 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001315 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001316 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001317 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001318
1319 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001320 i = 0;
1321 p = &terminal->escape[2];
1322 while ((isdigit(*p) || *p == ';') && i < 10) {
1323 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001324 if (!set[i]) {
1325 args[i] = 0;
1326 set[i] = 1;
1327 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001328 p++;
1329 i++;
1330 } else {
1331 args[i] = strtol(p, &p, 10);
1332 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001333 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001334 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001335
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001336 switch (*p) {
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001337 case '@': /* ICH - Insert <count> blank characters */
Callum Lowcay69e96582011-01-07 19:47:00 +00001338 count = set[0] ? args[0] : 1;
1339 if (count == 0) count = 1;
1340 terminal_shift_line(terminal, count);
1341 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001342 case 'A': /* CUU - Move cursor up <count> rows */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001343 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001344 if (count == 0) count = 1;
1345 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001346 terminal->row -= count;
1347 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001348 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001349 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001350 case 'B': /* CUD - Move cursor down <count> rows */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001351 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001352 if (count == 0) count = 1;
1353 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001354 terminal->row += count;
1355 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001356 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001357 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001358 case 'C': /* CUF - Move cursor right by <count> columns */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001359 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001360 if (count == 0) count = 1;
1361 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001362 terminal->column += count;
1363 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001364 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001365 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001366 case 'D': /* CUB - Move cursor left <count> columns */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001367 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001368 if (count == 0) count = 1;
1369 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001370 terminal->column -= count;
1371 else
1372 terminal->column = 0;
1373 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001374 case 'E': /* CNL - Move cursor down <count> rows, to column 1 */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001375 count = set[0] ? args[0] : 1;
1376 if (terminal->row + count <= terminal->margin_bottom)
1377 terminal->row += count;
1378 else
1379 terminal->row = terminal->margin_bottom;
1380 terminal->column = 0;
1381 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001382 case 'F': /* CPL - Move cursour up <count> rows, to column 1 */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001383 count = set[0] ? args[0] : 1;
1384 if (terminal->row - count >= terminal->margin_top)
1385 terminal->row -= count;
1386 else
1387 terminal->row = terminal->margin_top;
1388 terminal->column = 0;
1389 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001390 case 'G': /* CHA - Move cursor to column <y> in current row */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001391 y = set[0] ? args[0] : 1;
1392 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
Michael Vetter2a18a522015-05-15 17:17:47 +02001393
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001394 terminal->column = y - 1;
1395 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001396 case 'f': /* HVP - Move cursor to <x, y> */
1397 case 'H': /* CUP - Move cursor to <x, y> (origin at 1,1) */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001398 x = (set[1] ? args[1] : 1) - 1;
1399 x = x < 0 ? 0 :
1400 (x >= terminal->width ? terminal->width - 1 : x);
Michael Vetter2a18a522015-05-15 17:17:47 +02001401
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001402 y = (set[0] ? args[0] : 1) - 1;
1403 if (terminal->origin_mode) {
1404 y += terminal->margin_top;
1405 y = y < terminal->margin_top ? terminal->margin_top :
1406 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1407 } else {
1408 y = y < 0 ? 0 :
1409 (y >= terminal->height ? terminal->height - 1 : y);
1410 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001411
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001412 terminal->row = y;
1413 terminal->column = x;
1414 break;
1415 case 'I': /* CHT */
1416 count = set[0] ? args[0] : 1;
1417 if (count == 0) count = 1;
1418 while (count > 0 && terminal->column < terminal->width) {
1419 if (terminal->tab_ruler[terminal->column]) count--;
1420 terminal->column++;
1421 }
1422 terminal->column--;
1423 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001424 case 'J': /* ED - Erase display */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001425 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001426 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001427 if (!set[0] || args[0] == 0 || args[0] > 2) {
1428 memset(&row[terminal->column],
1429 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1430 attr_init(&attr_row[terminal->column],
1431 terminal->curr_attr, terminal->width - terminal->column);
1432 for (i = terminal->row + 1; i < terminal->height; i++) {
1433 memset(terminal_get_row(terminal, i),
1434 0, terminal->data_pitch);
1435 attr_init(terminal_get_attr_row(terminal, i),
1436 terminal->curr_attr, terminal->width);
1437 }
1438 } else if (args[0] == 1) {
1439 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1440 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1441 for (i = 0; i < terminal->row; i++) {
1442 memset(terminal_get_row(terminal, i),
1443 0, terminal->data_pitch);
1444 attr_init(terminal_get_attr_row(terminal, i),
1445 terminal->curr_attr, terminal->width);
1446 }
1447 } else if (args[0] == 2) {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07001448 /* Clear screen by scrolling contents out */
1449 terminal_scroll_buffer(terminal,
1450 terminal->end - terminal->start);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001451 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001452 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001453 case 'K': /* EL - Erase line */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001454 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001455 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001456 if (!set[0] || args[0] == 0 || args[0] > 2) {
1457 memset(&row[terminal->column], 0,
1458 (terminal->width - terminal->column) * sizeof(union utf8_char));
1459 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1460 terminal->width - terminal->column);
1461 } else if (args[0] == 1) {
1462 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1463 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1464 } else if (args[0] == 2) {
1465 memset(row, 0, terminal->data_pitch);
1466 attr_init(attr_row, terminal->curr_attr, terminal->width);
1467 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001468 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001469 case 'L': /* IL - Insert <count> blank lines */
Callum Lowcay69e96582011-01-07 19:47:00 +00001470 count = set[0] ? args[0] : 1;
1471 if (count == 0) count = 1;
1472 if (terminal->row >= terminal->margin_top &&
1473 terminal->row < terminal->margin_bottom)
1474 {
1475 top = terminal->margin_top;
1476 terminal->margin_top = terminal->row;
1477 terminal_scroll(terminal, 0 - count);
1478 terminal->margin_top = top;
1479 } else if (terminal->row == terminal->margin_bottom) {
1480 memset(terminal_get_row(terminal, terminal->row),
1481 0, terminal->data_pitch);
1482 attr_init(terminal_get_attr_row(terminal, terminal->row),
1483 terminal->curr_attr, terminal->width);
1484 }
1485 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001486 case 'M': /* DL - Delete <count> lines */
Callum Lowcay69e96582011-01-07 19:47:00 +00001487 count = set[0] ? args[0] : 1;
1488 if (count == 0) count = 1;
1489 if (terminal->row >= terminal->margin_top &&
1490 terminal->row < terminal->margin_bottom)
1491 {
1492 top = terminal->margin_top;
1493 terminal->margin_top = terminal->row;
1494 terminal_scroll(terminal, count);
1495 terminal->margin_top = top;
1496 } else if (terminal->row == terminal->margin_bottom) {
1497 memset(terminal_get_row(terminal, terminal->row),
1498 0, terminal->data_pitch);
1499 }
1500 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001501 case 'P': /* DCH - Delete <count> characters on current line */
Callum Lowcay69e96582011-01-07 19:47:00 +00001502 count = set[0] ? args[0] : 1;
1503 if (count == 0) count = 1;
1504 terminal_shift_line(terminal, 0 - count);
1505 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001506 case 'S': /* SU */
1507 terminal_scroll(terminal, set[0] ? args[0] : 1);
1508 break;
1509 case 'T': /* SD */
1510 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1511 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001512 case 'X': /* ECH - Erase <count> characters on current line */
Callum Lowcay69e96582011-01-07 19:47:00 +00001513 count = set[0] ? args[0] : 1;
1514 if (count == 0) count = 1;
1515 if ((terminal->column + count) > terminal->width)
1516 count = terminal->width - terminal->column;
1517 row = terminal_get_row(terminal, terminal->row);
1518 attr_row = terminal_get_attr_row(terminal, terminal->row);
1519 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1520 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1521 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001522 case 'Z': /* CBT */
1523 count = set[0] ? args[0] : 1;
1524 if (count == 0) count = 1;
1525 while (count > 0 && terminal->column >= 0) {
1526 if (terminal->tab_ruler[terminal->column]) count--;
1527 terminal->column--;
1528 }
1529 terminal->column++;
1530 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001531 case '`': /* HPA - Move cursor to <y> column in current row */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001532 y = set[0] ? args[0] : 1;
1533 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
Michael Vetter2a18a522015-05-15 17:17:47 +02001534
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001535 terminal->column = y - 1;
1536 break;
1537 case 'b': /* REP */
1538 count = set[0] ? args[0] : 1;
1539 if (count == 0) count = 1;
1540 if (terminal->last_char.byte[0])
1541 for (i = 0; i < count; i++)
1542 handle_char(terminal, terminal->last_char);
1543 terminal->last_char.byte[0] = 0;
1544 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001545 case 'c': /* Primary DA - Answer "I am a VT102" */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001546 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001547 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001548 case 'd': /* VPA - Move cursor to <x> row, current column */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001549 x = set[0] ? args[0] : 1;
1550 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
Michael Vetter2a18a522015-05-15 17:17:47 +02001551
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001552 terminal->row = x - 1;
1553 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001554 case 'g': /* TBC - Clear tab stop(s) */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001555 if (!set[0] || args[0] == 0) {
1556 terminal->tab_ruler[terminal->column] = 0;
1557 } else if (args[0] == 3) {
1558 memset(terminal->tab_ruler, 0, terminal->width);
1559 }
1560 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001561 case 'h': /* SM - Set mode */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001562 for (i = 0; i < 10 && set[i]; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001563 handle_term_parameter(terminal, args[i], 1);
1564 }
1565 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001566 case 'l': /* RM - Reset mode */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001567 for (i = 0; i < 10 && set[i]; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001568 handle_term_parameter(terminal, args[i], 0);
1569 }
1570 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001571 case 'm': /* SGR - Set attributes */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001572 for (i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001573 if (i <= 7 && set[i] && set[i + 1] &&
1574 set[i + 2] && args[i + 1] == 5)
1575 {
1576 if (args[i] == 38) {
1577 handle_sgr(terminal, args[i + 2] + 256);
1578 break;
1579 } else if (args[i] == 48) {
1580 handle_sgr(terminal, args[i + 2] + 512);
1581 break;
1582 }
1583 }
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001584 if (set[i]) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001585 handle_sgr(terminal, args[i]);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001586 } else if (i == 0) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001587 handle_sgr(terminal, 0);
1588 break;
1589 } else {
1590 break;
1591 }
1592 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001593 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001594 case 'n': /* DSR - Status report */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001595 i = set[0] ? args[0] : 0;
1596 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001597 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001598 } else if (i == 6) {
1599 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1600 terminal->origin_mode ?
1601 terminal->row+terminal->margin_top : terminal->row+1,
1602 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001603 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001604 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001605 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001606 case 'r': /* DECSTBM - Set scrolling region */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001607 if (!set[0]) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001608 terminal->margin_top = 0;
1609 terminal->margin_bottom = terminal->height-1;
1610 terminal->row = 0;
1611 terminal->column = 0;
1612 } else {
1613 top = (set[0] ? args[0] : 1) - 1;
1614 top = top < 0 ? 0 :
1615 (top >= terminal->height ? terminal->height - 1 : top);
1616 bottom = (set[1] ? args[1] : 1) - 1;
1617 bottom = bottom < 0 ? 0 :
1618 (bottom >= terminal->height ? terminal->height - 1 : bottom);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001619 if (bottom > top) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001620 terminal->margin_top = top;
1621 terminal->margin_bottom = bottom;
1622 } else {
1623 terminal->margin_top = 0;
1624 terminal->margin_bottom = terminal->height-1;
1625 }
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001626 if (terminal->origin_mode)
Callum Lowcaybbeac602011-01-07 19:46:58 +00001627 terminal->row = terminal->margin_top;
1628 else
1629 terminal->row = 0;
1630 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001631 }
1632 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001633 case 's': /* Save cursor location */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001634 terminal->saved_row = terminal->row;
1635 terminal->saved_column = terminal->column;
1636 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001637 case 't': /* windowOps */
1638 if (!set[0]) break;
1639 switch (args[0]) {
1640 case 4: /* resize px */
1641 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001642 widget_schedule_resize(terminal->widget,
1643 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001644 }
1645 break;
1646 case 8: /* resize ch */
1647 if (set[1] && set[2]) {
1648 terminal_resize(terminal, args[2], args[1]);
1649 }
1650 break;
1651 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001652 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001653 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1654 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001655 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001656 break;
1657 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001658 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001659 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1660 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001661 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001662 break;
1663 case 18: /* report ch */
1664 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1665 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001666 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001667 break;
1668 case 21: /* report title */
1669 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1670 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001671 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001672 break;
1673 default:
1674 if (args[0] >= 24)
1675 terminal_resize(terminal, terminal->width, args[0]);
1676 else
1677 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1678 break;
1679 }
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001680 case 'u': /* Restore cursor location */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001681 terminal->row = terminal->saved_row;
1682 terminal->column = terminal->saved_column;
1683 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001684 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001685 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001686 break;
Michael Vetter2a18a522015-05-15 17:17:47 +02001687 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001688}
1689
1690static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001691handle_non_csi_escape(struct terminal *terminal, char code)
1692{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001693 switch(code) {
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001694 case 'M': /* RI - Reverse linefeed */
Callum Lowcaybbeac602011-01-07 19:46:58 +00001695 terminal->row -= 1;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001696 if (terminal->row < terminal->margin_top) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001697 terminal->row = terminal->margin_top;
1698 terminal_scroll(terminal, -1);
1699 }
1700 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001701 case 'E': /* NEL - Newline */
Callum Lowcaybbeac602011-01-07 19:46:58 +00001702 terminal->column = 0;
1703 // fallthrough
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001704 case 'D': /* IND - Linefeed */
Callum Lowcaybbeac602011-01-07 19:46:58 +00001705 terminal->row += 1;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001706 if (terminal->row > terminal->margin_bottom) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001707 terminal->row = terminal->margin_bottom;
1708 terminal_scroll(terminal, +1);
1709 }
1710 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001711 case 'c': /* RIS - Reset*/
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001712 terminal_init(terminal);
1713 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001714 case 'H': /* HTS - Set tab stop at current column */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001715 terminal->tab_ruler[terminal->column] = 1;
1716 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001717 case '7': /* DECSC - Save current state */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001718 terminal->saved_row = terminal->row;
1719 terminal->saved_column = terminal->column;
1720 terminal->saved_attr = terminal->curr_attr;
1721 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001722 terminal->saved_cs = terminal->cs;
1723 terminal->saved_g0 = terminal->g0;
1724 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001725 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001726 case '8': /* DECRC - Restore state most recently saved by ESC 7 */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001727 terminal->row = terminal->saved_row;
1728 terminal->column = terminal->saved_column;
1729 terminal->curr_attr = terminal->saved_attr;
1730 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001731 terminal->cs = terminal->saved_cs;
1732 terminal->g0 = terminal->saved_g0;
1733 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001734 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001735 case '=': /* DECPAM - Set application keypad mode */
Callum Lowcay7e08e902011-01-07 19:47:02 +00001736 terminal->key_mode = KM_APPLICATION;
1737 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001738 case '>': /* DECPNM - Set numeric keypad mode */
Callum Lowcay7e08e902011-01-07 19:47:02 +00001739 terminal->key_mode = KM_NORMAL;
1740 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001741 default:
1742 fprintf(stderr, "Unknown escape code: %c\n", code);
1743 break;
1744 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001745}
1746
1747static void
1748handle_special_escape(struct terminal *terminal, char special, char code)
1749{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001750 int i, numChars;
1751
1752 if (special == '#') {
1753 switch(code) {
1754 case '8':
1755 /* fill with 'E', no cheap way to do this */
1756 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1757 numChars = terminal->width * terminal->height;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001758 for (i = 0; i < numChars; i++) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001759 terminal->data[i].byte[0] = 'E';
1760 }
1761 break;
1762 default:
1763 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1764 break;
1765 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001766 } else if (special == '(' || special == ')') {
1767 switch(code) {
1768 case '0':
1769 if (special == '(')
1770 terminal->g0 = CS_SPECIAL;
1771 else
1772 terminal->g1 = CS_SPECIAL;
1773 break;
1774 case 'A':
1775 if (special == '(')
1776 terminal->g0 = CS_UK;
1777 else
1778 terminal->g1 = CS_UK;
1779 break;
1780 case 'B':
1781 if (special == '(')
1782 terminal->g0 = CS_US;
1783 else
1784 terminal->g1 = CS_US;
1785 break;
1786 default:
1787 fprintf(stderr, "Unknown character set %c\n", code);
1788 break;
1789 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001790 } else {
1791 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1792 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001793}
1794
1795static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001796handle_sgr(struct terminal *terminal, int code)
1797{
1798 switch(code) {
1799 case 0:
1800 terminal->curr_attr = terminal->color_scheme->default_attr;
1801 break;
1802 case 1:
1803 terminal->curr_attr.a |= ATTRMASK_BOLD;
1804 if (terminal->curr_attr.fg < 8)
1805 terminal->curr_attr.fg += 8;
1806 break;
1807 case 4:
1808 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1809 break;
1810 case 5:
1811 terminal->curr_attr.a |= ATTRMASK_BLINK;
1812 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001813 case 8:
1814 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1815 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001816 case 2:
1817 case 21:
1818 case 22:
1819 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1820 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1821 terminal->curr_attr.fg -= 8;
1822 break;
1823 case 24:
1824 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1825 break;
1826 case 25:
1827 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1828 break;
1829 case 7:
1830 case 26:
1831 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1832 break;
1833 case 27:
1834 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1835 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001836 case 28:
1837 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1838 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001839 case 39:
1840 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1841 break;
1842 case 49:
1843 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1844 break;
1845 default:
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001846 if (code >= 30 && code <= 37) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001847 terminal->curr_attr.fg = code - 30;
1848 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1849 terminal->curr_attr.fg += 8;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001850 } else if (code >= 40 && code <= 47) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001851 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001852 } else if (code >= 90 && code <= 97) {
1853 terminal->curr_attr.fg = code - 90 + 8;
1854 } else if (code >= 100 && code <= 107) {
1855 terminal->curr_attr.bg = code - 100 + 8;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001856 } else if (code >= 256 && code < 512) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001857 terminal->curr_attr.fg = code - 256;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001858 } else if (code >= 512 && code < 768) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001859 terminal->curr_attr.bg = code - 512;
1860 } else {
1861 fprintf(stderr, "Unknown SGR code: %d\n", code);
1862 }
1863 break;
1864 }
1865}
1866
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001867/* Returns 1 if c was special, otherwise 0 */
1868static int
1869handle_special_char(struct terminal *terminal, char c)
1870{
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001871 union utf8_char *row;
1872 struct attr *attr_row;
1873
1874 row = terminal_get_row(terminal, terminal->row);
1875 attr_row = terminal_get_attr_row(terminal, terminal->row);
1876
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001877 switch(c) {
1878 case '\r':
1879 terminal->column = 0;
1880 break;
1881 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001882 if (terminal->mode & MODE_LF_NEWLINE) {
1883 terminal->column = 0;
1884 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001885 /* fallthrough */
1886 case '\v':
1887 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001888 terminal->row++;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07001889 if (terminal->row > terminal->margin_bottom) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001890 terminal->row = terminal->margin_bottom;
1891 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001892 }
1893
1894 break;
1895 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001896 while (terminal->column < terminal->width) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001897 if (terminal->mode & MODE_IRM)
1898 terminal_shift_line(terminal, +1);
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001899
1900 if (row[terminal->column].byte[0] == '\0') {
1901 row[terminal->column].byte[0] = ' ';
1902 row[terminal->column].byte[1] = '\0';
1903 attr_row[terminal->column] = terminal->curr_attr;
1904 }
1905
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001906 terminal->column++;
Kristian Høgsbergcca3c2f2012-06-20 15:56:13 -04001907 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001908 }
1909 if (terminal->column >= terminal->width) {
1910 terminal->column = terminal->width - 1;
1911 }
1912
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001913 break;
1914 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001915 if (terminal->column >= terminal->width) {
1916 terminal->column = terminal->width - 2;
1917 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001918 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001919 } else if (terminal->mode & MODE_AUTOWRAP) {
1920 terminal->column = terminal->width - 1;
1921 terminal->row -= 1;
1922 if (terminal->row < terminal->margin_top) {
1923 terminal->row = terminal->margin_top;
1924 terminal_scroll(terminal, -1);
1925 }
1926 }
1927
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001928 break;
1929 case '\a':
1930 /* Bell */
1931 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001932 case '\x0E': /* SO */
1933 terminal->cs = terminal->g1;
1934 break;
1935 case '\x0F': /* SI */
1936 terminal->cs = terminal->g0;
1937 break;
Kristian Høgsberg2a1aa4e2012-08-03 09:37:05 -04001938 case '\0':
1939 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001940 default:
1941 return 0;
1942 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001943
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001944 return 1;
1945}
1946
1947static void
1948handle_char(struct terminal *terminal, union utf8_char utf8)
1949{
1950 union utf8_char *row;
1951 struct attr *attr_row;
Michael Vetter2a18a522015-05-15 17:17:47 +02001952
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001953 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001954
1955 apply_char_set(terminal->cs, &utf8);
Michael Vetter2a18a522015-05-15 17:17:47 +02001956
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001957 /* There are a whole lot of non-characters, control codes,
1958 * and formatting codes that should probably be ignored,
1959 * for example: */
1960 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1961 /* BOM, ignore */
1962 return;
Michael Vetter2a18a522015-05-15 17:17:47 +02001963 }
1964
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001965 /* Some of these non-characters should be translated, e.g.: */
1966 if (utf8.byte[0] < 32) {
1967 utf8.byte[0] = utf8.byte[0] + 64;
1968 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001969
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001970 /* handle right margin effects */
1971 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001972 if (terminal->mode & MODE_AUTOWRAP) {
1973 terminal->column = 0;
1974 terminal->row += 1;
1975 if (terminal->row > terminal->margin_bottom) {
1976 terminal->row = terminal->margin_bottom;
1977 terminal_scroll(terminal, +1);
1978 }
1979 } else {
1980 terminal->column--;
1981 }
1982 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001983
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001984 row = terminal_get_row(terminal, terminal->row);
1985 attr_row = terminal_get_attr_row(terminal, terminal->row);
Michael Vetter2a18a522015-05-15 17:17:47 +02001986
Callum Lowcay69e96582011-01-07 19:47:00 +00001987 if (terminal->mode & MODE_IRM)
1988 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001989 row[terminal->column] = utf8;
1990 attr_row[terminal->column++] = terminal->curr_attr;
1991
Kristian Høgsberg1d781ee2013-11-24 16:54:12 -08001992 if (terminal->row + terminal->start + 1 > terminal->end)
1993 terminal->end = terminal->row + terminal->start + 1;
1994 if (terminal->end == terminal->buffer_height)
1995 terminal->log_size = terminal->buffer_height;
1996 else if (terminal->log_size < terminal->buffer_height)
1997 terminal->log_size = terminal->end;
1998
Peng Wucfcc1112013-08-19 10:59:21 +08001999 /* cursor jump for wide character. */
2000 if (is_wide(utf8))
2001 row[terminal->column++].ch = 0x200B; /* space glyph */
2002
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002003 if (utf8.ch != terminal->last_char.ch)
2004 terminal->last_char = utf8;
2005}
2006
Callum Lowcay30eeae52011-01-07 19:46:55 +00002007static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13002008escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
2009{
2010 int len, i;
2011
2012 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
2013 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
2014 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
2015 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
2016 else len = 1; /* Invalid, cannot happen */
2017
2018 if (terminal->escape_length + len <= MAX_ESCAPE) {
2019 for (i = 0; i < len; i++)
2020 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
2021 terminal->escape_length += len;
2022 } else if (terminal->escape_length < MAX_ESCAPE) {
2023 terminal->escape[terminal->escape_length++] = 0;
2024 }
2025}
2026
2027static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002028terminal_data(struct terminal *terminal, const char *data, size_t length)
2029{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002030 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002031 union utf8_char utf8;
2032 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002033
2034 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002035 parser_state =
2036 utf8_next_char(&terminal->state_machine, data[i]);
2037 switch(parser_state) {
2038 case utf8state_accept:
2039 utf8.ch = terminal->state_machine.s.ch;
2040 break;
2041 case utf8state_reject:
2042 /* the unicode replacement character */
2043 utf8.byte[0] = 0xEF;
2044 utf8.byte[1] = 0xBF;
2045 utf8.byte[2] = 0xBD;
2046 utf8.byte[3] = 0x00;
2047 break;
2048 default:
2049 continue;
2050 }
2051
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002052 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13002053 switch (terminal->state) {
2054 case escape_state_escape:
2055 escape_append_utf8(terminal, utf8);
2056 switch (utf8.byte[0]) {
2057 case 'P': /* DCS */
2058 terminal->state = escape_state_dcs;
2059 break;
2060 case '[': /* CSI */
2061 terminal->state = escape_state_csi;
2062 break;
2063 case ']': /* OSC */
2064 terminal->state = escape_state_osc;
2065 break;
2066 case '#':
2067 case '(':
2068 case ')': /* special */
2069 terminal->state = escape_state_special;
2070 break;
2071 case '^': /* PM (not implemented) */
2072 case '_': /* APC (not implemented) */
2073 terminal->state = escape_state_ignore;
2074 break;
2075 default:
2076 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002077 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13002078 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002079 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002080 continue;
2081 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002082 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
2083 /* do nothing */
2084 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002085 terminal->escape_flags |= ESC_FLAG_WHAT;
2086 } else if (utf8.byte[0] == '>') {
2087 terminal->escape_flags |= ESC_FLAG_GT;
2088 } else if (utf8.byte[0] == '!') {
2089 terminal->escape_flags |= ESC_FLAG_BANG;
2090 } else if (utf8.byte[0] == '$') {
2091 terminal->escape_flags |= ESC_FLAG_CASH;
2092 } else if (utf8.byte[0] == '\'') {
2093 terminal->escape_flags |= ESC_FLAG_SQUOTE;
2094 } else if (utf8.byte[0] == '"') {
2095 terminal->escape_flags |= ESC_FLAG_DQUOTE;
2096 } else if (utf8.byte[0] == ' ') {
2097 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002098 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002099 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002100 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13002101 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002102 }
Michael Vetter2a18a522015-05-15 17:17:47 +02002103
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002104 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
2105 utf8.byte[0] == '`')
2106 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002107 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002108 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002109 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002110 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002111 continue;
2112 case escape_state_inner_escape:
2113 if (utf8.byte[0] == '\\') {
2114 terminal->state = escape_state_normal;
2115 if (terminal->outer_state == escape_state_dcs) {
2116 handle_dcs(terminal);
2117 } else if (terminal->outer_state == escape_state_osc) {
2118 handle_osc(terminal);
2119 }
2120 } else if (utf8.byte[0] == '\e') {
2121 terminal->state = terminal->outer_state;
2122 escape_append_utf8(terminal, utf8);
2123 if (terminal->escape_length >= MAX_ESCAPE)
2124 terminal->state = escape_state_normal;
2125 } else {
2126 terminal->state = terminal->outer_state;
2127 if (terminal->escape_length < MAX_ESCAPE)
2128 terminal->escape[terminal->escape_length++] = '\e';
2129 escape_append_utf8(terminal, utf8);
2130 if (terminal->escape_length >= MAX_ESCAPE)
2131 terminal->state = escape_state_normal;
2132 }
2133 continue;
2134 case escape_state_dcs:
2135 case escape_state_osc:
2136 case escape_state_ignore:
2137 if (utf8.byte[0] == '\e') {
2138 terminal->outer_state = terminal->state;
2139 terminal->state = escape_state_inner_escape;
2140 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
2141 terminal->state = escape_state_normal;
2142 handle_osc(terminal);
2143 } else {
2144 escape_append_utf8(terminal, utf8);
2145 if (terminal->escape_length >= MAX_ESCAPE)
2146 terminal->state = escape_state_normal;
2147 }
2148 continue;
2149 case escape_state_special:
2150 escape_append_utf8(terminal, utf8);
2151 terminal->state = escape_state_normal;
2152 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2153 handle_special_escape(terminal, terminal->escape[1],
2154 utf8.byte[0]);
2155 }
2156 continue;
2157 default:
2158 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002159 }
2160
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002161 /* this is valid, because ASCII characters are never used to
2162 * introduce a multibyte sequence in UTF-8 */
2163 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002164 terminal->state = escape_state_escape;
2165 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002166 terminal->escape[0] = '\e';
2167 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002168 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002169 } else {
2170 handle_char(terminal, utf8);
2171 } /* if */
2172 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002173
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002174 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002175}
2176
2177static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002178data_source_target(void *data,
2179 struct wl_data_source *source, const char *mime_type)
2180{
2181 fprintf(stderr, "data_source_target, %s\n", mime_type);
2182}
2183
2184static void
2185data_source_send(void *data,
2186 struct wl_data_source *source,
2187 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002188{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002189 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002190
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002191 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002192}
2193
2194static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002195data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002196{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002197 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002198}
2199
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002200static const struct wl_data_source_listener data_source_listener = {
2201 data_source_target,
2202 data_source_send,
2203 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002204};
2205
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07002206static const char text_mime_type[] = "text/plain;charset=utf-8";
2207
2208static void
2209data_handler(struct window *window,
2210 struct input *input,
2211 float x, float y, const char **types, void *data)
2212{
2213 int i, has_text = 0;
2214
2215 if (!types)
2216 return;
2217 for (i = 0; types[i]; i++)
2218 if (strcmp(types[i], text_mime_type) == 0)
2219 has_text = 1;
2220
2221 if (!has_text) {
2222 input_accept(input, NULL);
2223 } else {
2224 input_accept(input, text_mime_type);
2225 }
2226}
2227
2228static void
2229drop_handler(struct window *window, struct input *input,
2230 int32_t x, int32_t y, void *data)
2231{
2232 struct terminal *terminal = data;
2233
2234 input_receive_drag_data_to_fd(input, text_mime_type, terminal->master);
2235}
2236
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002237static void
2238fullscreen_handler(struct window *window, void *data)
2239{
2240 struct terminal *terminal = data;
2241
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002242 window_set_fullscreen(window, !window_is_fullscreen(terminal->window));
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002243}
2244
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002245static void
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002246close_handler(void *data)
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002247{
2248 struct terminal *terminal = data;
2249
2250 terminal_destroy(terminal);
2251}
2252
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002253static void
2254terminal_copy(struct terminal *terminal, struct input *input)
2255{
2256 terminal->selection =
2257 display_create_data_source(terminal->display);
2258 wl_data_source_offer(terminal->selection,
2259 "text/plain;charset=utf-8");
2260 wl_data_source_add_listener(terminal->selection,
2261 &data_source_listener, terminal);
2262 input_set_selection(input, terminal->selection,
2263 display_get_serial(terminal->display));
2264}
2265
2266static void
2267terminal_paste(struct terminal *terminal, struct input *input)
2268{
2269 input_receive_selection_data_to_fd(input,
2270 "text/plain;charset=utf-8",
2271 terminal->master);
2272
2273}
2274
2275static void
2276terminal_new_instance(struct terminal *terminal)
2277{
2278 struct terminal *new_terminal;
2279
2280 new_terminal = terminal_create(terminal->display);
2281 if (terminal_run(new_terminal, option_shell))
2282 terminal_destroy(new_terminal);
2283}
2284
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002285static int
2286handle_bound_key(struct terminal *terminal,
2287 struct input *input, uint32_t sym, uint32_t time)
2288{
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002289 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002290 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002291 /* Cut selection; terminal doesn't do cut, fall
2292 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002293 case XKB_KEY_C:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002294 terminal_copy(terminal, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002295 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002296 case XKB_KEY_V:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002297 terminal_paste(terminal, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002298 return 1;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002299 case XKB_KEY_N:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002300 terminal_new_instance(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002301 return 1;
2302
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002303 case XKB_KEY_Up:
2304 if (!terminal->scrolling)
2305 terminal->saved_start = terminal->start;
2306 if (terminal->start == terminal->end - terminal->log_size)
2307 return 1;
2308
2309 terminal->scrolling = 1;
2310 terminal->start--;
2311 terminal->row++;
2312 terminal->selection_start_row++;
2313 terminal->selection_end_row++;
2314 widget_schedule_redraw(terminal->widget);
2315 return 1;
2316
2317 case XKB_KEY_Down:
2318 if (!terminal->scrolling)
2319 terminal->saved_start = terminal->start;
2320
2321 if (terminal->start == terminal->saved_start)
2322 return 1;
2323
2324 terminal->scrolling = 1;
2325 terminal->start++;
2326 terminal->row--;
2327 terminal->selection_start_row--;
2328 terminal->selection_end_row--;
2329 widget_schedule_redraw(terminal->widget);
2330 return 1;
2331
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002332 default:
2333 return 0;
2334 }
2335}
2336
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002337static void
2338key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002339 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2340 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002341{
2342 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002343 char ch[MAX_RESPONSE];
Kristian Høgsberg88fd4082012-06-21 15:55:03 -04002344 uint32_t modifiers, serial;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002345 int ret, len = 0, d;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002346 bool convert_utf8 = true;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002347
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002348 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002349 if ((modifiers & MOD_CONTROL_MASK) &&
2350 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002351 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2352 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002353 return;
2354
Daniel Stonea8468712012-11-07 17:51:35 +11002355 /* Map keypad symbols to 'normal' equivalents before processing */
2356 switch (sym) {
2357 case XKB_KEY_KP_Space:
2358 sym = XKB_KEY_space;
2359 break;
2360 case XKB_KEY_KP_Tab:
2361 sym = XKB_KEY_Tab;
2362 break;
2363 case XKB_KEY_KP_Enter:
2364 sym = XKB_KEY_Return;
2365 break;
2366 case XKB_KEY_KP_Left:
2367 sym = XKB_KEY_Left;
2368 break;
2369 case XKB_KEY_KP_Up:
2370 sym = XKB_KEY_Up;
2371 break;
2372 case XKB_KEY_KP_Right:
2373 sym = XKB_KEY_Right;
2374 break;
2375 case XKB_KEY_KP_Down:
2376 sym = XKB_KEY_Down;
2377 break;
2378 case XKB_KEY_KP_Equal:
2379 sym = XKB_KEY_equal;
2380 break;
2381 case XKB_KEY_KP_Multiply:
2382 sym = XKB_KEY_asterisk;
2383 break;
2384 case XKB_KEY_KP_Add:
2385 sym = XKB_KEY_plus;
2386 break;
2387 case XKB_KEY_KP_Separator:
2388 /* Note this is actually locale-dependent and should mostly be
2389 * a comma. But leave it as period until we one day start
2390 * doing the right thing. */
2391 sym = XKB_KEY_period;
2392 break;
2393 case XKB_KEY_KP_Subtract:
2394 sym = XKB_KEY_minus;
2395 break;
2396 case XKB_KEY_KP_Decimal:
2397 sym = XKB_KEY_period;
2398 break;
2399 case XKB_KEY_KP_Divide:
2400 sym = XKB_KEY_slash;
2401 break;
2402 case XKB_KEY_KP_0:
2403 case XKB_KEY_KP_1:
2404 case XKB_KEY_KP_2:
2405 case XKB_KEY_KP_3:
2406 case XKB_KEY_KP_4:
2407 case XKB_KEY_KP_5:
2408 case XKB_KEY_KP_6:
2409 case XKB_KEY_KP_7:
2410 case XKB_KEY_KP_8:
2411 case XKB_KEY_KP_9:
2412 sym = (sym - XKB_KEY_KP_0) + XKB_KEY_0;
2413 break;
2414 default:
2415 break;
2416 }
2417
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002418 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002419 case XKB_KEY_BackSpace:
Kristian Høgsbergb7f94bf2012-06-21 12:29:36 -04002420 if (modifiers & MOD_ALT_MASK)
2421 ch[len++] = 0x1b;
Kristian Høgsberg71a4cf42012-06-20 17:57:56 -04002422 ch[len++] = 0x7f;
2423 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002424 case XKB_KEY_Tab:
2425 case XKB_KEY_Linefeed:
2426 case XKB_KEY_Clear:
2427 case XKB_KEY_Pause:
2428 case XKB_KEY_Scroll_Lock:
2429 case XKB_KEY_Sys_Req:
2430 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002431 ch[len++] = sym & 0x7f;
2432 break;
2433
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002434 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002435 if (terminal->mode & MODE_LF_NEWLINE) {
2436 ch[len++] = 0x0D;
2437 ch[len++] = 0x0A;
2438 } else {
2439 ch[len++] = 0x0D;
2440 }
2441 break;
2442
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002443 case XKB_KEY_Shift_L:
2444 case XKB_KEY_Shift_R:
2445 case XKB_KEY_Control_L:
2446 case XKB_KEY_Control_R:
2447 case XKB_KEY_Alt_L:
2448 case XKB_KEY_Alt_R:
Kristian Høgsberg22fbcf72012-06-22 12:18:56 -04002449 case XKB_KEY_Meta_L:
2450 case XKB_KEY_Meta_R:
2451 case XKB_KEY_Super_L:
2452 case XKB_KEY_Super_R:
2453 case XKB_KEY_Hyper_L:
2454 case XKB_KEY_Hyper_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002455 break;
2456
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002457 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002458 len = function_key_response('[', 2, modifiers, '~', ch);
2459 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002460 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002461 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2462 ch[len++] = '\x04';
2463 } else {
2464 len = function_key_response('[', 3, modifiers, '~', ch);
2465 }
2466 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002467 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002468 len = function_key_response('[', 5, modifiers, '~', ch);
2469 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002470 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002471 len = function_key_response('[', 6, modifiers, '~', ch);
2472 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002473 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002474 len = function_key_response('O', 1, modifiers, 'P', ch);
2475 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002476 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002477 len = function_key_response('O', 1, modifiers, 'Q', ch);
2478 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002479 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002480 len = function_key_response('O', 1, modifiers, 'R', ch);
2481 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002482 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002483 len = function_key_response('O', 1, modifiers, 'S', ch);
2484 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002485 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002486 len = function_key_response('[', 15, modifiers, '~', ch);
2487 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002488 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002489 len = function_key_response('[', 17, modifiers, '~', ch);
2490 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002491 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002492 len = function_key_response('[', 18, modifiers, '~', ch);
2493 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002494 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002495 len = function_key_response('[', 19, modifiers, '~', ch);
2496 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002497 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002498 len = function_key_response('[', 20, modifiers, '~', ch);
2499 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002500 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002501 len = function_key_response('[', 21, modifiers, '~', ch);
2502 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002503 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002504 len = function_key_response('[', 24, modifiers, '~', ch);
2505 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002506 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002507 /* Handle special keys with alternate mappings */
2508 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2509 if (len != 0) break;
Michael Vetter2a18a522015-05-15 17:17:47 +02002510
Kristian Høgsberg70163132012-05-08 15:55:39 -04002511 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002512 if (sym >= '3' && sym <= '7')
2513 sym = (sym & 0x1f) + 8;
2514
2515 if (!((sym >= '!' && sym <= '/') ||
2516 (sym >= '8' && sym <= '?') ||
2517 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2518 else if (sym == '2') sym = 0x00;
2519 else if (sym == '/') sym = 0x1F;
2520 else if (sym == '8' || sym == '?') sym = 0x7F;
Kristian Høgsbergae9e0732012-06-21 12:30:15 -04002521 }
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002522 if (modifiers & MOD_ALT_MASK) {
2523 if (terminal->mode & MODE_ALT_SENDS_ESC) {
2524 ch[len++] = 0x1b;
2525 } else {
2526 sym = sym | 0x80;
2527 convert_utf8 = false;
2528 }
Callum Lowcay7e08e902011-01-07 19:47:02 +00002529 }
2530
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002531 if ((sym < 128) ||
2532 (!convert_utf8 && sym < 256)) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002533 ch[len++] = sym;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002534 } else {
2535 ret = xkb_keysym_to_utf8(sym, ch + len,
2536 MAX_RESPONSE - len);
2537 if (ret < 0)
2538 fprintf(stderr,
2539 "Warning: buffer too small to encode "
2540 "UTF8 character\n");
2541 else
2542 len += ret;
2543 }
2544
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002545 break;
2546 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002547
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002548 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002549 if (terminal->scrolling) {
2550 d = terminal->saved_start - terminal->start;
2551 terminal->row -= d;
2552 terminal->selection_start_row -= d;
2553 terminal->selection_end_row -= d;
2554 terminal->start = terminal->saved_start;
2555 terminal->scrolling = 0;
2556 widget_schedule_redraw(terminal->widget);
2557 }
2558
Kristian Høgsberg26130862011-08-24 11:30:21 -04002559 terminal_write(terminal, ch, len);
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002560
2561 /* Hide cursor, except if this was coming from a
2562 * repeating key press. */
2563 serial = display_get_serial(terminal->display);
2564 if (terminal->hide_cursor_serial != serial) {
2565 input_set_pointer_image(input, CURSOR_BLANK);
2566 terminal->hide_cursor_serial = serial;
2567 }
2568 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002569}
2570
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002571static void
2572keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002573 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002574{
2575 struct terminal *terminal = data;
2576
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002577 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002578}
2579
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002580static int wordsep(int ch)
2581{
2582 const char extra[] = "-,./?%&#:_=+@~";
2583
Derek Foreman7978bc82015-09-02 16:21:54 -05002584 if (ch > 127 || ch < 0)
Andre Heider552d12b2012-08-02 20:59:43 +02002585 return 1;
2586
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002587 return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
2588}
2589
2590static int
2591recompute_selection(struct terminal *terminal)
2592{
2593 struct rectangle allocation;
2594 int col, x, width, height;
2595 int start_row, end_row;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002596 int word_start, eol;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002597 int side_margin, top_margin;
2598 int start_x, end_x;
2599 int cw, ch;
2600 union utf8_char *data;
2601
Peng Wuf291f202013-06-06 15:32:41 +08002602 cw = terminal->average_width;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002603 ch = terminal->extents.height;
2604 widget_get_allocation(terminal->widget, &allocation);
2605 width = terminal->width * cw;
2606 height = terminal->height * ch;
2607 side_margin = allocation.x + (allocation.width - width) / 2;
2608 top_margin = allocation.y + (allocation.height - height) / 2;
2609
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002610 start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1;
2611 end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1;
2612
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002613 if (start_row < end_row ||
2614 (start_row == end_row &&
2615 terminal->selection_start_x < terminal->selection_end_x)) {
2616 terminal->selection_start_row = start_row;
2617 terminal->selection_end_row = end_row;
2618 start_x = terminal->selection_start_x;
2619 end_x = terminal->selection_end_x;
2620 } else {
2621 terminal->selection_start_row = end_row;
2622 terminal->selection_end_row = start_row;
2623 start_x = terminal->selection_end_x;
2624 end_x = terminal->selection_start_x;
2625 }
2626
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002627 eol = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002628 if (terminal->selection_start_row < 0) {
2629 terminal->selection_start_row = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002630 terminal->selection_start_col = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002631 } else {
2632 x = side_margin + cw / 2;
2633 data = terminal_get_row(terminal,
2634 terminal->selection_start_row);
2635 word_start = 0;
2636 for (col = 0; col < terminal->width; col++, x += cw) {
2637 if (col == 0 || wordsep(data[col - 1].ch))
2638 word_start = col;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002639 if (data[col].ch != 0)
2640 eol = col + 1;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002641 if (start_x < x)
2642 break;
2643 }
2644
2645 switch (terminal->dragging) {
2646 case SELECT_LINE:
2647 terminal->selection_start_col = 0;
2648 break;
2649 case SELECT_WORD:
2650 terminal->selection_start_col = word_start;
2651 break;
2652 case SELECT_CHAR:
2653 terminal->selection_start_col = col;
2654 break;
2655 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002656 }
2657
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002658 if (terminal->selection_end_row >= terminal->height) {
2659 terminal->selection_end_row = terminal->height;
2660 terminal->selection_end_col = 0;
2661 } else {
2662 x = side_margin + cw / 2;
2663 data = terminal_get_row(terminal, terminal->selection_end_row);
2664 for (col = 0; col < terminal->width; col++, x += cw) {
2665 if (terminal->dragging == SELECT_CHAR && end_x < x)
2666 break;
2667 if (terminal->dragging == SELECT_WORD &&
2668 end_x < x && wordsep(data[col].ch))
2669 break;
2670 }
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002671 terminal->selection_end_col = col;
2672 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002673
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002674 if (terminal->selection_end_col != terminal->selection_start_col ||
2675 terminal->selection_start_row != terminal->selection_end_row) {
2676 col = terminal->selection_end_col;
2677 if (col > 0 && data[col - 1].ch == 0)
2678 terminal->selection_end_col = terminal->width;
2679 data = terminal_get_row(terminal, terminal->selection_start_row);
2680 if (data[terminal->selection_start_col].ch == 0)
2681 terminal->selection_start_col = eol;
2682 }
2683
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002684 return 1;
2685}
2686
Kristian Høgsberg59826582011-01-20 11:56:57 -05002687static void
Pekka Paalanen807f09c2015-03-27 15:48:54 +02002688terminal_minimize(struct terminal *terminal)
2689{
2690 window_set_minimized(terminal->window);
2691}
2692
2693static void
Jasper St. Pierredda93132014-03-13 12:06:00 -04002694menu_func(void *data, struct input *input, int index)
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002695{
Jasper St. Pierredda93132014-03-13 12:06:00 -04002696 struct window *window = data;
2697 struct terminal *terminal = window_get_user_data(window);
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002698
2699 fprintf(stderr, "picked entry %d\n", index);
2700
2701 switch (index) {
2702 case 0:
2703 terminal_new_instance(terminal);
2704 break;
2705 case 1:
2706 terminal_copy(terminal, input);
2707 break;
2708 case 2:
2709 terminal_paste(terminal, input);
2710 break;
Pekka Paalanen807f09c2015-03-27 15:48:54 +02002711 case 3:
2712 terminal_minimize(terminal);
2713 break;
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002714 }
2715}
2716
2717static void
2718show_menu(struct terminal *terminal, struct input *input, uint32_t time)
2719{
2720 int32_t x, y;
2721 static const char *entries[] = {
Pekka Paalanen807f09c2015-03-27 15:48:54 +02002722 "Open Terminal", "Copy", "Paste", "Minimize"
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002723 };
2724
2725 input_get_position(input, &x, &y);
2726 window_show_menu(terminal->display, input, time, terminal->window,
2727 x - 10, y - 10, menu_func,
2728 entries, ARRAY_LENGTH(entries));
2729}
2730
2731static void
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002732click_handler(struct widget *widget, struct terminal *terminal,
2733 struct input *input, int32_t x, int32_t y,
2734 uint32_t time)
2735{
2736 if (time - terminal->click_time < 500)
2737 terminal->click_count++;
2738 else
2739 terminal->click_count = 1;
2740
2741 terminal->click_time = time;
2742 terminal->dragging = (terminal->click_count - 1) % 3 + SELECT_CHAR;
2743
2744 terminal->selection_end_x = terminal->selection_start_x = x;
2745 terminal->selection_end_y = terminal->selection_start_y = y;
2746 if (recompute_selection(terminal))
2747 widget_schedule_redraw(widget);
2748}
2749
2750static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002751button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002752 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002753 uint32_t button,
2754 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002755{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002756 struct terminal *terminal = data;
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002757 int32_t x, y;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002758
2759 switch (button) {
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002760 case BTN_LEFT:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002761 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002762 input_get_position(input, &x, &y);
2763 click_handler(widget, terminal, input, x, y, time);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002764 } else {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002765 terminal->dragging = SELECT_NONE;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002766 }
2767 break;
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002768
2769 case BTN_RIGHT:
2770 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
2771 show_menu(terminal, input, time);
2772 break;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002773 }
2774}
2775
2776static int
Kristian Høgsberg29784402012-06-28 14:27:02 -04002777enter_handler(struct widget *widget,
2778 struct input *input, float x, float y, void *data)
2779{
2780 return CURSOR_IBEAM;
2781}
2782
2783static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002784motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002785 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002786 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002787{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002788 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002789
2790 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002791 input_get_position(input,
2792 &terminal->selection_end_x,
2793 &terminal->selection_end_y);
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002794
2795 if (recompute_selection(terminal))
2796 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002797 }
2798
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002799 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002800}
2801
Magnus Hoff1046f122014-08-05 15:05:59 +02002802/* This magnitude is chosen rather arbitrarily. Really, the scrolling
2803 * should happen on a (fractional) pixel basis, not a line basis. */
2804#define AXIS_UNITS_PER_LINE 256
2805
2806static void
2807axis_handler(struct widget *widget,
2808 struct input *input, uint32_t time,
2809 uint32_t axis,
2810 wl_fixed_t value,
2811 void *data)
2812{
2813 struct terminal *terminal = data;
2814 int lines;
2815
2816 if (axis != WL_POINTER_AXIS_VERTICAL_SCROLL)
2817 return;
2818
2819 terminal->smooth_scroll += value;
2820 lines = terminal->smooth_scroll / AXIS_UNITS_PER_LINE;
2821 terminal->smooth_scroll -= lines * AXIS_UNITS_PER_LINE;
2822
2823 if (lines > 0) {
2824 if (terminal->scrolling) {
2825 if ((uint32_t)lines > terminal->saved_start - terminal->start)
2826 lines = terminal->saved_start - terminal->start;
2827 } else {
2828 lines = 0;
2829 }
2830 } else if (lines < 0) {
2831 uint32_t neg_lines = -lines;
2832
2833 if (neg_lines > terminal->log_size + terminal->start - terminal->end)
2834 lines = terminal->end - terminal->log_size - terminal->start;
2835 }
2836
2837 if (lines) {
2838 if (!terminal->scrolling)
2839 terminal->saved_start = terminal->start;
2840 terminal->scrolling = 1;
2841
2842 terminal->start += lines;
2843 terminal->row -= lines;
2844 terminal->selection_start_row -= lines;
2845 terminal->selection_end_row -= lines;
2846
2847 widget_schedule_redraw(widget);
2848 }
2849}
2850
Alexander Larssonde79dd02013-05-22 14:41:34 +02002851static void
2852output_handler(struct window *window, struct output *output, int enter,
2853 void *data)
2854{
2855 if (enter)
2856 window_set_buffer_transform(window, output_get_transform(output));
2857 window_set_buffer_scale(window, window_get_output_scale(window));
2858 window_schedule_redraw(window);
2859}
2860
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002861static void
2862touch_down_handler(struct widget *widget, struct input *input,
2863 uint32_t serial, uint32_t time, int32_t id,
2864 float x, float y, void *data)
2865{
2866 struct terminal *terminal = data;
2867
2868 if (id == 0)
2869 click_handler(widget, terminal, input, x, y, time);
2870}
2871
2872static void
2873touch_up_handler(struct widget *widget, struct input *input,
2874 uint32_t serial, uint32_t time, int32_t id, void *data)
2875{
2876 struct terminal *terminal = data;
2877
2878 if (id == 0)
2879 terminal->dragging = SELECT_NONE;
2880}
2881
2882static void
2883touch_motion_handler(struct widget *widget, struct input *input,
2884 uint32_t time, int32_t id, float x, float y, void *data)
2885{
2886 struct terminal *terminal = data;
2887
2888 if (terminal->dragging &&
2889 id == 0) {
2890 terminal->selection_end_x = (int)x;
2891 terminal->selection_end_y = (int)y;
2892
2893 if (recompute_selection(terminal))
2894 widget_schedule_redraw(widget);
2895 }
2896}
2897
Peng Wuf291f202013-06-06 15:32:41 +08002898#ifndef howmany
2899#define howmany(x, y) (((x) + ((y) - 1)) / (y))
2900#endif
2901
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002902static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002903terminal_create(struct display *display)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002904{
2905 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002906 cairo_surface_t *surface;
2907 cairo_t *cr;
Peng Wuf291f202013-06-06 15:32:41 +08002908 cairo_text_extents_t text_extents;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002909
Peter Huttererf3d62272013-08-08 11:57:05 +10002910 terminal = xzalloc(sizeof *terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002911 terminal->color_scheme = &DEFAULT_COLORS;
2912 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002913 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002914 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002915 terminal->window = window_create(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002916 terminal->widget = window_frame_create(terminal->window, terminal);
U. Artie Eoff09827e22014-01-15 11:40:37 -08002917 terminal->title = xstrdup("Wayland Terminal");
Kristian Høgsberga83be202013-10-23 20:47:35 -07002918 window_set_title(terminal->window, terminal->title);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002919 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002920
2921 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002922 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002923
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002924 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002925 terminal->margin = 5;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002926 terminal->buffer_height = 1024;
2927 terminal->end = 1;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002928
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002929 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002930 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002931 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002932 keyboard_focus_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002933 window_set_fullscreen_handler(terminal->window, fullscreen_handler);
Alexander Larssonde79dd02013-05-22 14:41:34 +02002934 window_set_output_handler(terminal->window, output_handler);
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002935 window_set_close_handler(terminal->window, close_handler);
Jasper St. Pierrede680992014-04-10 17:23:49 -07002936 window_set_state_changed_handler(terminal->window, state_changed_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002937
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07002938 window_set_data_handler(terminal->window, data_handler);
2939 window_set_drop_handler(terminal->window, drop_handler);
2940
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002941 widget_set_redraw_handler(terminal->widget, redraw_handler);
2942 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002943 widget_set_button_handler(terminal->widget, button_handler);
Kristian Høgsberg29784402012-06-28 14:27:02 -04002944 widget_set_enter_handler(terminal->widget, enter_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002945 widget_set_motion_handler(terminal->widget, motion_handler);
Magnus Hoff1046f122014-08-05 15:05:59 +02002946 widget_set_axis_handler(terminal->widget, axis_handler);
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002947 widget_set_touch_up_handler(terminal->widget, touch_up_handler);
2948 widget_set_touch_down_handler(terminal->widget, touch_down_handler);
2949 widget_set_touch_motion_handler(terminal->widget, touch_motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002950
Kristian Høgsberg09531622010-06-14 23:22:15 -04002951 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2952 cr = cairo_create(surface);
Kristian Høgsberg38912df2012-06-27 17:52:23 -04002953 cairo_set_font_size(cr, option_font_size);
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002954 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002955 CAIRO_FONT_SLANT_NORMAL,
2956 CAIRO_FONT_WEIGHT_BOLD);
2957 terminal->font_bold = cairo_get_scaled_font (cr);
2958 cairo_scaled_font_reference(terminal->font_bold);
2959
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002960 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002961 CAIRO_FONT_SLANT_NORMAL,
2962 CAIRO_FONT_WEIGHT_NORMAL);
2963 terminal->font_normal = cairo_get_scaled_font (cr);
2964 cairo_scaled_font_reference(terminal->font_normal);
2965
Kristian Høgsberg09531622010-06-14 23:22:15 -04002966 cairo_font_extents(cr, &terminal->extents);
Peng Wuf291f202013-06-06 15:32:41 +08002967
2968 /* Compute the average ascii glyph width */
2969 cairo_text_extents(cr, TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS,
2970 &text_extents);
2971 terminal->average_width = howmany
2972 (text_extents.width,
2973 strlen(TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS));
2974 terminal->average_width = ceil(terminal->average_width);
2975
Kristian Høgsberg09531622010-06-14 23:22:15 -04002976 cairo_destroy(cr);
2977 cairo_surface_destroy(surface);
2978
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002979 terminal_resize(terminal, 20, 5); /* Set minimum size first */
Kristian Høgsberga1627922012-06-20 17:30:03 -04002980 terminal_resize(terminal, 80, 25);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002981
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002982 wl_list_insert(terminal_list.prev, &terminal->link);
2983
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002984 return terminal;
2985}
2986
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002987static void
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002988terminal_destroy(struct terminal *terminal)
2989{
Dima Ryazanova85292e2012-11-29 00:27:09 -08002990 display_unwatch_fd(terminal->display, terminal->master);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002991 window_destroy(terminal->window);
2992 close(terminal->master);
2993 wl_list_remove(&terminal->link);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002994
2995 if (wl_list_empty(&terminal_list))
Dima Ryazanova85292e2012-11-29 00:27:09 -08002996 display_exit(terminal->display);
2997
Kristian Høgsberga83be202013-10-23 20:47:35 -07002998 free(terminal->title);
Dima Ryazanova85292e2012-11-29 00:27:09 -08002999 free(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003000}
3001
3002static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003003io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003004{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003005 struct terminal *terminal =
3006 container_of(task, struct terminal, io_task);
3007 char buffer[256];
3008 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003009
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003010 if (events & EPOLLHUP) {
3011 terminal_destroy(terminal);
3012 return;
3013 }
Tim Wiederhakef71accc2011-01-19 23:14:33 +01003014
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003015 len = read(terminal->master, buffer, sizeof buffer);
3016 if (len < 0)
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003017 terminal_destroy(terminal);
3018 else
3019 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003020}
3021
3022static int
3023terminal_run(struct terminal *terminal, const char *path)
3024{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003025 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003026 pid_t pid;
3027
3028 pid = forkpty(&master, NULL, NULL, NULL);
3029 if (pid == 0) {
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04003030 setenv("TERM", option_term, 1);
3031 setenv("COLORTERM", option_term, 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003032 if (execl(path, path, NULL)) {
3033 printf("exec failed: %m\n");
3034 exit(EXIT_FAILURE);
3035 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003036 } else if (pid < 0) {
3037 fprintf(stderr, "failed to fork and create pty (%m).\n");
3038 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003039 }
3040
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003041 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003042 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003043 terminal->io_task.run = io_handler;
3044 display_watch_fd(terminal->display, terminal->master,
3045 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003046
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07003047 if (option_fullscreen)
3048 window_set_fullscreen(terminal->window, 1);
3049 else
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003050 terminal_resize(terminal, 80, 24);
3051
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003052 return 0;
3053}
3054
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003055static const struct weston_option terminal_options[] = {
3056 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04003057 { WESTON_OPTION_STRING, "font", 0, &option_font },
Bill Spitzak5cad8432014-08-08 12:59:55 -07003058 { WESTON_OPTION_INTEGER, "font-size", 0, &option_font_size },
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003059 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003060};
3061
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003062int main(int argc, char *argv[])
3063{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003064 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003065 struct terminal *terminal;
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02003066 const char *config_file;
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -07003067 struct weston_config *config;
3068 struct weston_config_section *s;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04003069
Peng Wucfcc1112013-08-19 10:59:21 +08003070 /* as wcwidth is locale-dependent,
3071 wcwidth needs setlocale call to function properly. */
3072 setlocale(LC_ALL, "");
3073
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003074 option_shell = getenv("SHELL");
3075 if (!option_shell)
3076 option_shell = "/bin/bash";
3077
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02003078 config_file = weston_config_get_name_from_env();
3079 config = weston_config_parse(config_file);
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -07003080 s = weston_config_get_section(config, "terminal", NULL, NULL);
3081 weston_config_section_get_string(s, "font", &option_font, "mono");
3082 weston_config_section_get_int(s, "font-size", &option_font_size, 14);
3083 weston_config_section_get_string(s, "term", &option_term, "xterm");
3084 weston_config_destroy(config);
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003085
Bill Spitzak5cad8432014-08-08 12:59:55 -07003086 if (parse_options(terminal_options,
3087 ARRAY_LENGTH(terminal_options), &argc, argv) > 1) {
3088 printf("Usage: %s [OPTIONS]\n"
3089 " --fullscreen or -f\n"
3090 " --font=NAME\n"
3091 " --font-size=SIZE\n"
3092 " --shell=NAME\n", argv[0]);
3093 return 1;
3094 }
3095
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003096 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02003097 if (d == NULL) {
3098 fprintf(stderr, "failed to create display: %m\n");
3099 return -1;
3100 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003101
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003102 wl_list_init(&terminal_list);
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04003103 terminal = terminal_create(d);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003104 if (terminal_run(terminal, option_shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003105 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003106
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003107 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003108
3109 return 0;
3110}