blob: 5c25fa8d2f86b39e1e19b2ce67fd0d99a78c82ac [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;
Dima Ryazanovc3b63722016-11-03 23:46:02 -07001301 case 777: /* Desktop notifications */
1302 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001303 default:
Kristian Høgsberg8a205002013-09-11 11:52:56 -07001304 fprintf(stderr, "Unknown OSC escape code %d, text %s\n",
1305 code, p);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001306 break;
1307 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001308}
1309
1310static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001311handle_escape(struct terminal *terminal)
1312{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001313 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001314 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001315 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001316 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001317 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001318 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001319 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001320
1321 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001322 i = 0;
1323 p = &terminal->escape[2];
1324 while ((isdigit(*p) || *p == ';') && i < 10) {
1325 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001326 if (!set[i]) {
1327 args[i] = 0;
1328 set[i] = 1;
1329 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001330 p++;
1331 i++;
1332 } else {
1333 args[i] = strtol(p, &p, 10);
1334 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001335 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001336 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001337
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001338 switch (*p) {
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001339 case '@': /* ICH - Insert <count> blank characters */
Callum Lowcay69e96582011-01-07 19:47:00 +00001340 count = set[0] ? args[0] : 1;
1341 if (count == 0) count = 1;
1342 terminal_shift_line(terminal, count);
1343 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001344 case 'A': /* CUU - Move cursor up <count> rows */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001345 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001346 if (count == 0) count = 1;
1347 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001348 terminal->row -= count;
1349 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001350 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001351 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001352 case 'B': /* CUD - Move cursor down <count> rows */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001353 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001354 if (count == 0) count = 1;
1355 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001356 terminal->row += count;
1357 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001358 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001359 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001360 case 'C': /* CUF - Move cursor right by <count> columns */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001361 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001362 if (count == 0) count = 1;
1363 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001364 terminal->column += count;
1365 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001366 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001367 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001368 case 'D': /* CUB - Move cursor left <count> columns */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001369 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001370 if (count == 0) count = 1;
1371 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001372 terminal->column -= count;
1373 else
1374 terminal->column = 0;
1375 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001376 case 'E': /* CNL - Move cursor down <count> rows, to column 1 */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001377 count = set[0] ? args[0] : 1;
1378 if (terminal->row + count <= terminal->margin_bottom)
1379 terminal->row += count;
1380 else
1381 terminal->row = terminal->margin_bottom;
1382 terminal->column = 0;
1383 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001384 case 'F': /* CPL - Move cursour up <count> rows, to column 1 */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001385 count = set[0] ? args[0] : 1;
1386 if (terminal->row - count >= terminal->margin_top)
1387 terminal->row -= count;
1388 else
1389 terminal->row = terminal->margin_top;
1390 terminal->column = 0;
1391 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001392 case 'G': /* CHA - Move cursor to column <y> in current row */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001393 y = set[0] ? args[0] : 1;
1394 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
Michael Vetter2a18a522015-05-15 17:17:47 +02001395
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001396 terminal->column = y - 1;
1397 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001398 case 'f': /* HVP - Move cursor to <x, y> */
1399 case 'H': /* CUP - Move cursor to <x, y> (origin at 1,1) */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001400 x = (set[1] ? args[1] : 1) - 1;
1401 x = x < 0 ? 0 :
1402 (x >= terminal->width ? terminal->width - 1 : x);
Michael Vetter2a18a522015-05-15 17:17:47 +02001403
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001404 y = (set[0] ? args[0] : 1) - 1;
1405 if (terminal->origin_mode) {
1406 y += terminal->margin_top;
1407 y = y < terminal->margin_top ? terminal->margin_top :
1408 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1409 } else {
1410 y = y < 0 ? 0 :
1411 (y >= terminal->height ? terminal->height - 1 : y);
1412 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001413
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001414 terminal->row = y;
1415 terminal->column = x;
1416 break;
1417 case 'I': /* CHT */
1418 count = set[0] ? args[0] : 1;
1419 if (count == 0) count = 1;
1420 while (count > 0 && terminal->column < terminal->width) {
1421 if (terminal->tab_ruler[terminal->column]) count--;
1422 terminal->column++;
1423 }
1424 terminal->column--;
1425 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001426 case 'J': /* ED - Erase display */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001427 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001428 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001429 if (!set[0] || args[0] == 0 || args[0] > 2) {
1430 memset(&row[terminal->column],
1431 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1432 attr_init(&attr_row[terminal->column],
1433 terminal->curr_attr, terminal->width - terminal->column);
1434 for (i = terminal->row + 1; i < terminal->height; i++) {
1435 memset(terminal_get_row(terminal, i),
1436 0, terminal->data_pitch);
1437 attr_init(terminal_get_attr_row(terminal, i),
1438 terminal->curr_attr, terminal->width);
1439 }
1440 } else if (args[0] == 1) {
1441 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1442 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1443 for (i = 0; i < terminal->row; i++) {
1444 memset(terminal_get_row(terminal, i),
1445 0, terminal->data_pitch);
1446 attr_init(terminal_get_attr_row(terminal, i),
1447 terminal->curr_attr, terminal->width);
1448 }
1449 } else if (args[0] == 2) {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07001450 /* Clear screen by scrolling contents out */
1451 terminal_scroll_buffer(terminal,
1452 terminal->end - terminal->start);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001453 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001454 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001455 case 'K': /* EL - Erase line */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001456 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001457 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001458 if (!set[0] || args[0] == 0 || args[0] > 2) {
1459 memset(&row[terminal->column], 0,
1460 (terminal->width - terminal->column) * sizeof(union utf8_char));
1461 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1462 terminal->width - terminal->column);
1463 } else if (args[0] == 1) {
1464 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1465 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1466 } else if (args[0] == 2) {
1467 memset(row, 0, terminal->data_pitch);
1468 attr_init(attr_row, terminal->curr_attr, terminal->width);
1469 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001470 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001471 case 'L': /* IL - Insert <count> blank lines */
Callum Lowcay69e96582011-01-07 19:47:00 +00001472 count = set[0] ? args[0] : 1;
1473 if (count == 0) count = 1;
1474 if (terminal->row >= terminal->margin_top &&
1475 terminal->row < terminal->margin_bottom)
1476 {
1477 top = terminal->margin_top;
1478 terminal->margin_top = terminal->row;
1479 terminal_scroll(terminal, 0 - count);
1480 terminal->margin_top = top;
1481 } else if (terminal->row == terminal->margin_bottom) {
1482 memset(terminal_get_row(terminal, terminal->row),
1483 0, terminal->data_pitch);
1484 attr_init(terminal_get_attr_row(terminal, terminal->row),
1485 terminal->curr_attr, terminal->width);
1486 }
1487 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001488 case 'M': /* DL - Delete <count> lines */
Callum Lowcay69e96582011-01-07 19:47:00 +00001489 count = set[0] ? args[0] : 1;
1490 if (count == 0) count = 1;
1491 if (terminal->row >= terminal->margin_top &&
1492 terminal->row < terminal->margin_bottom)
1493 {
1494 top = terminal->margin_top;
1495 terminal->margin_top = terminal->row;
1496 terminal_scroll(terminal, count);
1497 terminal->margin_top = top;
1498 } else if (terminal->row == terminal->margin_bottom) {
1499 memset(terminal_get_row(terminal, terminal->row),
1500 0, terminal->data_pitch);
1501 }
1502 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001503 case 'P': /* DCH - Delete <count> characters on current line */
Callum Lowcay69e96582011-01-07 19:47:00 +00001504 count = set[0] ? args[0] : 1;
1505 if (count == 0) count = 1;
1506 terminal_shift_line(terminal, 0 - count);
1507 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001508 case 'S': /* SU */
1509 terminal_scroll(terminal, set[0] ? args[0] : 1);
1510 break;
1511 case 'T': /* SD */
1512 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1513 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001514 case 'X': /* ECH - Erase <count> characters on current line */
Callum Lowcay69e96582011-01-07 19:47:00 +00001515 count = set[0] ? args[0] : 1;
1516 if (count == 0) count = 1;
1517 if ((terminal->column + count) > terminal->width)
1518 count = terminal->width - terminal->column;
1519 row = terminal_get_row(terminal, terminal->row);
1520 attr_row = terminal_get_attr_row(terminal, terminal->row);
1521 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1522 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1523 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001524 case 'Z': /* CBT */
1525 count = set[0] ? args[0] : 1;
1526 if (count == 0) count = 1;
1527 while (count > 0 && terminal->column >= 0) {
1528 if (terminal->tab_ruler[terminal->column]) count--;
1529 terminal->column--;
1530 }
1531 terminal->column++;
1532 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001533 case '`': /* HPA - Move cursor to <y> column in current row */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001534 y = set[0] ? args[0] : 1;
1535 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
Michael Vetter2a18a522015-05-15 17:17:47 +02001536
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001537 terminal->column = y - 1;
1538 break;
1539 case 'b': /* REP */
1540 count = set[0] ? args[0] : 1;
1541 if (count == 0) count = 1;
1542 if (terminal->last_char.byte[0])
1543 for (i = 0; i < count; i++)
1544 handle_char(terminal, terminal->last_char);
1545 terminal->last_char.byte[0] = 0;
1546 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001547 case 'c': /* Primary DA - Answer "I am a VT102" */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001548 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001549 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001550 case 'd': /* VPA - Move cursor to <x> row, current column */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001551 x = set[0] ? args[0] : 1;
1552 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
Michael Vetter2a18a522015-05-15 17:17:47 +02001553
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001554 terminal->row = x - 1;
1555 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001556 case 'g': /* TBC - Clear tab stop(s) */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001557 if (!set[0] || args[0] == 0) {
1558 terminal->tab_ruler[terminal->column] = 0;
1559 } else if (args[0] == 3) {
1560 memset(terminal->tab_ruler, 0, terminal->width);
1561 }
1562 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001563 case 'h': /* SM - Set mode */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001564 for (i = 0; i < 10 && set[i]; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001565 handle_term_parameter(terminal, args[i], 1);
1566 }
1567 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001568 case 'l': /* RM - Reset mode */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001569 for (i = 0; i < 10 && set[i]; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001570 handle_term_parameter(terminal, args[i], 0);
1571 }
1572 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001573 case 'm': /* SGR - Set attributes */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001574 for (i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001575 if (i <= 7 && set[i] && set[i + 1] &&
1576 set[i + 2] && args[i + 1] == 5)
1577 {
1578 if (args[i] == 38) {
1579 handle_sgr(terminal, args[i + 2] + 256);
1580 break;
1581 } else if (args[i] == 48) {
1582 handle_sgr(terminal, args[i + 2] + 512);
1583 break;
1584 }
1585 }
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001586 if (set[i]) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001587 handle_sgr(terminal, args[i]);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001588 } else if (i == 0) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001589 handle_sgr(terminal, 0);
1590 break;
1591 } else {
1592 break;
1593 }
1594 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001595 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001596 case 'n': /* DSR - Status report */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001597 i = set[0] ? args[0] : 0;
1598 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001599 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001600 } else if (i == 6) {
1601 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1602 terminal->origin_mode ?
1603 terminal->row+terminal->margin_top : terminal->row+1,
1604 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001605 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001606 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001607 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001608 case 'r': /* DECSTBM - Set scrolling region */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001609 if (!set[0]) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001610 terminal->margin_top = 0;
1611 terminal->margin_bottom = terminal->height-1;
1612 terminal->row = 0;
1613 terminal->column = 0;
1614 } else {
1615 top = (set[0] ? args[0] : 1) - 1;
1616 top = top < 0 ? 0 :
1617 (top >= terminal->height ? terminal->height - 1 : top);
1618 bottom = (set[1] ? args[1] : 1) - 1;
1619 bottom = bottom < 0 ? 0 :
1620 (bottom >= terminal->height ? terminal->height - 1 : bottom);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001621 if (bottom > top) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001622 terminal->margin_top = top;
1623 terminal->margin_bottom = bottom;
1624 } else {
1625 terminal->margin_top = 0;
1626 terminal->margin_bottom = terminal->height-1;
1627 }
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001628 if (terminal->origin_mode)
Callum Lowcaybbeac602011-01-07 19:46:58 +00001629 terminal->row = terminal->margin_top;
1630 else
1631 terminal->row = 0;
1632 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001633 }
1634 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001635 case 's': /* Save cursor location */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001636 terminal->saved_row = terminal->row;
1637 terminal->saved_column = terminal->column;
1638 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001639 case 't': /* windowOps */
1640 if (!set[0]) break;
1641 switch (args[0]) {
1642 case 4: /* resize px */
1643 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001644 widget_schedule_resize(terminal->widget,
1645 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001646 }
1647 break;
1648 case 8: /* resize ch */
1649 if (set[1] && set[2]) {
1650 terminal_resize(terminal, args[2], args[1]);
1651 }
1652 break;
1653 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001654 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001655 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1656 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001657 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001658 break;
1659 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001660 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001661 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1662 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001663 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001664 break;
1665 case 18: /* report ch */
1666 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1667 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001668 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001669 break;
1670 case 21: /* report title */
1671 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1672 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001673 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001674 break;
1675 default:
1676 if (args[0] >= 24)
1677 terminal_resize(terminal, terminal->width, args[0]);
1678 else
1679 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1680 break;
1681 }
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001682 case 'u': /* Restore cursor location */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001683 terminal->row = terminal->saved_row;
1684 terminal->column = terminal->saved_column;
1685 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001686 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001687 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001688 break;
Michael Vetter2a18a522015-05-15 17:17:47 +02001689 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001690}
1691
1692static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001693handle_non_csi_escape(struct terminal *terminal, char code)
1694{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001695 switch(code) {
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001696 case 'M': /* RI - Reverse linefeed */
Callum Lowcaybbeac602011-01-07 19:46:58 +00001697 terminal->row -= 1;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001698 if (terminal->row < terminal->margin_top) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001699 terminal->row = terminal->margin_top;
1700 terminal_scroll(terminal, -1);
1701 }
1702 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001703 case 'E': /* NEL - Newline */
Callum Lowcaybbeac602011-01-07 19:46:58 +00001704 terminal->column = 0;
1705 // fallthrough
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001706 case 'D': /* IND - Linefeed */
Callum Lowcaybbeac602011-01-07 19:46:58 +00001707 terminal->row += 1;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001708 if (terminal->row > terminal->margin_bottom) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001709 terminal->row = terminal->margin_bottom;
1710 terminal_scroll(terminal, +1);
1711 }
1712 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001713 case 'c': /* RIS - Reset*/
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001714 terminal_init(terminal);
1715 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001716 case 'H': /* HTS - Set tab stop at current column */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001717 terminal->tab_ruler[terminal->column] = 1;
1718 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001719 case '7': /* DECSC - Save current state */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001720 terminal->saved_row = terminal->row;
1721 terminal->saved_column = terminal->column;
1722 terminal->saved_attr = terminal->curr_attr;
1723 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001724 terminal->saved_cs = terminal->cs;
1725 terminal->saved_g0 = terminal->g0;
1726 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001727 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001728 case '8': /* DECRC - Restore state most recently saved by ESC 7 */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001729 terminal->row = terminal->saved_row;
1730 terminal->column = terminal->saved_column;
1731 terminal->curr_attr = terminal->saved_attr;
1732 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001733 terminal->cs = terminal->saved_cs;
1734 terminal->g0 = terminal->saved_g0;
1735 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001736 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001737 case '=': /* DECPAM - Set application keypad mode */
Callum Lowcay7e08e902011-01-07 19:47:02 +00001738 terminal->key_mode = KM_APPLICATION;
1739 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001740 case '>': /* DECPNM - Set numeric keypad mode */
Callum Lowcay7e08e902011-01-07 19:47:02 +00001741 terminal->key_mode = KM_NORMAL;
1742 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001743 default:
1744 fprintf(stderr, "Unknown escape code: %c\n", code);
1745 break;
1746 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001747}
1748
1749static void
1750handle_special_escape(struct terminal *terminal, char special, char code)
1751{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001752 int i, numChars;
1753
1754 if (special == '#') {
1755 switch(code) {
1756 case '8':
1757 /* fill with 'E', no cheap way to do this */
1758 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1759 numChars = terminal->width * terminal->height;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001760 for (i = 0; i < numChars; i++) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001761 terminal->data[i].byte[0] = 'E';
1762 }
1763 break;
1764 default:
1765 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1766 break;
1767 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001768 } else if (special == '(' || special == ')') {
1769 switch(code) {
1770 case '0':
1771 if (special == '(')
1772 terminal->g0 = CS_SPECIAL;
1773 else
1774 terminal->g1 = CS_SPECIAL;
1775 break;
1776 case 'A':
1777 if (special == '(')
1778 terminal->g0 = CS_UK;
1779 else
1780 terminal->g1 = CS_UK;
1781 break;
1782 case 'B':
1783 if (special == '(')
1784 terminal->g0 = CS_US;
1785 else
1786 terminal->g1 = CS_US;
1787 break;
1788 default:
1789 fprintf(stderr, "Unknown character set %c\n", code);
1790 break;
1791 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001792 } else {
1793 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1794 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001795}
1796
1797static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001798handle_sgr(struct terminal *terminal, int code)
1799{
1800 switch(code) {
1801 case 0:
1802 terminal->curr_attr = terminal->color_scheme->default_attr;
1803 break;
1804 case 1:
1805 terminal->curr_attr.a |= ATTRMASK_BOLD;
1806 if (terminal->curr_attr.fg < 8)
1807 terminal->curr_attr.fg += 8;
1808 break;
1809 case 4:
1810 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1811 break;
1812 case 5:
1813 terminal->curr_attr.a |= ATTRMASK_BLINK;
1814 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001815 case 8:
1816 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1817 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001818 case 2:
1819 case 21:
1820 case 22:
1821 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1822 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1823 terminal->curr_attr.fg -= 8;
1824 break;
1825 case 24:
1826 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1827 break;
1828 case 25:
1829 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1830 break;
1831 case 7:
1832 case 26:
1833 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1834 break;
1835 case 27:
1836 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1837 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001838 case 28:
1839 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1840 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001841 case 39:
1842 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1843 break;
1844 case 49:
1845 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1846 break;
1847 default:
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001848 if (code >= 30 && code <= 37) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001849 terminal->curr_attr.fg = code - 30;
1850 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1851 terminal->curr_attr.fg += 8;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001852 } else if (code >= 40 && code <= 47) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001853 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001854 } else if (code >= 90 && code <= 97) {
1855 terminal->curr_attr.fg = code - 90 + 8;
1856 } else if (code >= 100 && code <= 107) {
1857 terminal->curr_attr.bg = code - 100 + 8;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001858 } else if (code >= 256 && code < 512) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001859 terminal->curr_attr.fg = code - 256;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001860 } else if (code >= 512 && code < 768) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001861 terminal->curr_attr.bg = code - 512;
1862 } else {
1863 fprintf(stderr, "Unknown SGR code: %d\n", code);
1864 }
1865 break;
1866 }
1867}
1868
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001869/* Returns 1 if c was special, otherwise 0 */
1870static int
1871handle_special_char(struct terminal *terminal, char c)
1872{
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001873 union utf8_char *row;
1874 struct attr *attr_row;
1875
1876 row = terminal_get_row(terminal, terminal->row);
1877 attr_row = terminal_get_attr_row(terminal, terminal->row);
1878
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001879 switch(c) {
1880 case '\r':
1881 terminal->column = 0;
1882 break;
1883 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001884 if (terminal->mode & MODE_LF_NEWLINE) {
1885 terminal->column = 0;
1886 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001887 /* fallthrough */
1888 case '\v':
1889 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001890 terminal->row++;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07001891 if (terminal->row > terminal->margin_bottom) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001892 terminal->row = terminal->margin_bottom;
1893 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001894 }
1895
1896 break;
1897 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001898 while (terminal->column < terminal->width) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001899 if (terminal->mode & MODE_IRM)
1900 terminal_shift_line(terminal, +1);
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001901
1902 if (row[terminal->column].byte[0] == '\0') {
1903 row[terminal->column].byte[0] = ' ';
1904 row[terminal->column].byte[1] = '\0';
1905 attr_row[terminal->column] = terminal->curr_attr;
1906 }
1907
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001908 terminal->column++;
Kristian Høgsbergcca3c2f2012-06-20 15:56:13 -04001909 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001910 }
1911 if (terminal->column >= terminal->width) {
1912 terminal->column = terminal->width - 1;
1913 }
1914
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001915 break;
1916 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001917 if (terminal->column >= terminal->width) {
1918 terminal->column = terminal->width - 2;
1919 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001920 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001921 } else if (terminal->mode & MODE_AUTOWRAP) {
1922 terminal->column = terminal->width - 1;
1923 terminal->row -= 1;
1924 if (terminal->row < terminal->margin_top) {
1925 terminal->row = terminal->margin_top;
1926 terminal_scroll(terminal, -1);
1927 }
1928 }
1929
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001930 break;
1931 case '\a':
1932 /* Bell */
1933 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001934 case '\x0E': /* SO */
1935 terminal->cs = terminal->g1;
1936 break;
1937 case '\x0F': /* SI */
1938 terminal->cs = terminal->g0;
1939 break;
Kristian Høgsberg2a1aa4e2012-08-03 09:37:05 -04001940 case '\0':
1941 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001942 default:
1943 return 0;
1944 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001945
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001946 return 1;
1947}
1948
1949static void
1950handle_char(struct terminal *terminal, union utf8_char utf8)
1951{
1952 union utf8_char *row;
1953 struct attr *attr_row;
Michael Vetter2a18a522015-05-15 17:17:47 +02001954
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001955 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001956
1957 apply_char_set(terminal->cs, &utf8);
Michael Vetter2a18a522015-05-15 17:17:47 +02001958
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001959 /* There are a whole lot of non-characters, control codes,
1960 * and formatting codes that should probably be ignored,
1961 * for example: */
1962 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1963 /* BOM, ignore */
1964 return;
Michael Vetter2a18a522015-05-15 17:17:47 +02001965 }
1966
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001967 /* Some of these non-characters should be translated, e.g.: */
1968 if (utf8.byte[0] < 32) {
1969 utf8.byte[0] = utf8.byte[0] + 64;
1970 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001971
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001972 /* handle right margin effects */
1973 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001974 if (terminal->mode & MODE_AUTOWRAP) {
1975 terminal->column = 0;
1976 terminal->row += 1;
1977 if (terminal->row > terminal->margin_bottom) {
1978 terminal->row = terminal->margin_bottom;
1979 terminal_scroll(terminal, +1);
1980 }
1981 } else {
1982 terminal->column--;
1983 }
1984 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001985
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001986 row = terminal_get_row(terminal, terminal->row);
1987 attr_row = terminal_get_attr_row(terminal, terminal->row);
Michael Vetter2a18a522015-05-15 17:17:47 +02001988
Callum Lowcay69e96582011-01-07 19:47:00 +00001989 if (terminal->mode & MODE_IRM)
1990 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001991 row[terminal->column] = utf8;
1992 attr_row[terminal->column++] = terminal->curr_attr;
1993
Kristian Høgsberg1d781ee2013-11-24 16:54:12 -08001994 if (terminal->row + terminal->start + 1 > terminal->end)
1995 terminal->end = terminal->row + terminal->start + 1;
1996 if (terminal->end == terminal->buffer_height)
1997 terminal->log_size = terminal->buffer_height;
1998 else if (terminal->log_size < terminal->buffer_height)
1999 terminal->log_size = terminal->end;
2000
Peng Wucfcc1112013-08-19 10:59:21 +08002001 /* cursor jump for wide character. */
2002 if (is_wide(utf8))
2003 row[terminal->column++].ch = 0x200B; /* space glyph */
2004
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002005 if (utf8.ch != terminal->last_char.ch)
2006 terminal->last_char = utf8;
2007}
2008
Callum Lowcay30eeae52011-01-07 19:46:55 +00002009static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13002010escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
2011{
2012 int len, i;
2013
2014 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
2015 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
2016 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
2017 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
2018 else len = 1; /* Invalid, cannot happen */
2019
2020 if (terminal->escape_length + len <= MAX_ESCAPE) {
2021 for (i = 0; i < len; i++)
2022 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
2023 terminal->escape_length += len;
2024 } else if (terminal->escape_length < MAX_ESCAPE) {
2025 terminal->escape[terminal->escape_length++] = 0;
2026 }
2027}
2028
2029static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002030terminal_data(struct terminal *terminal, const char *data, size_t length)
2031{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002032 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002033 union utf8_char utf8;
2034 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002035
2036 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002037 parser_state =
2038 utf8_next_char(&terminal->state_machine, data[i]);
2039 switch(parser_state) {
2040 case utf8state_accept:
2041 utf8.ch = terminal->state_machine.s.ch;
2042 break;
2043 case utf8state_reject:
2044 /* the unicode replacement character */
2045 utf8.byte[0] = 0xEF;
2046 utf8.byte[1] = 0xBF;
2047 utf8.byte[2] = 0xBD;
2048 utf8.byte[3] = 0x00;
2049 break;
2050 default:
2051 continue;
2052 }
2053
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002054 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13002055 switch (terminal->state) {
2056 case escape_state_escape:
2057 escape_append_utf8(terminal, utf8);
2058 switch (utf8.byte[0]) {
2059 case 'P': /* DCS */
2060 terminal->state = escape_state_dcs;
2061 break;
2062 case '[': /* CSI */
2063 terminal->state = escape_state_csi;
2064 break;
2065 case ']': /* OSC */
2066 terminal->state = escape_state_osc;
2067 break;
2068 case '#':
2069 case '(':
2070 case ')': /* special */
2071 terminal->state = escape_state_special;
2072 break;
2073 case '^': /* PM (not implemented) */
2074 case '_': /* APC (not implemented) */
2075 terminal->state = escape_state_ignore;
2076 break;
2077 default:
2078 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002079 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13002080 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002081 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002082 continue;
2083 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002084 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
2085 /* do nothing */
2086 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002087 terminal->escape_flags |= ESC_FLAG_WHAT;
2088 } else if (utf8.byte[0] == '>') {
2089 terminal->escape_flags |= ESC_FLAG_GT;
2090 } else if (utf8.byte[0] == '!') {
2091 terminal->escape_flags |= ESC_FLAG_BANG;
2092 } else if (utf8.byte[0] == '$') {
2093 terminal->escape_flags |= ESC_FLAG_CASH;
2094 } else if (utf8.byte[0] == '\'') {
2095 terminal->escape_flags |= ESC_FLAG_SQUOTE;
2096 } else if (utf8.byte[0] == '"') {
2097 terminal->escape_flags |= ESC_FLAG_DQUOTE;
2098 } else if (utf8.byte[0] == ' ') {
2099 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002100 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002101 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002102 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13002103 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002104 }
Michael Vetter2a18a522015-05-15 17:17:47 +02002105
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002106 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
2107 utf8.byte[0] == '`')
2108 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002109 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002110 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002111 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002112 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002113 continue;
2114 case escape_state_inner_escape:
2115 if (utf8.byte[0] == '\\') {
2116 terminal->state = escape_state_normal;
2117 if (terminal->outer_state == escape_state_dcs) {
2118 handle_dcs(terminal);
2119 } else if (terminal->outer_state == escape_state_osc) {
2120 handle_osc(terminal);
2121 }
2122 } else if (utf8.byte[0] == '\e') {
2123 terminal->state = terminal->outer_state;
2124 escape_append_utf8(terminal, utf8);
2125 if (terminal->escape_length >= MAX_ESCAPE)
2126 terminal->state = escape_state_normal;
2127 } else {
2128 terminal->state = terminal->outer_state;
2129 if (terminal->escape_length < MAX_ESCAPE)
2130 terminal->escape[terminal->escape_length++] = '\e';
2131 escape_append_utf8(terminal, utf8);
2132 if (terminal->escape_length >= MAX_ESCAPE)
2133 terminal->state = escape_state_normal;
2134 }
2135 continue;
2136 case escape_state_dcs:
2137 case escape_state_osc:
2138 case escape_state_ignore:
2139 if (utf8.byte[0] == '\e') {
2140 terminal->outer_state = terminal->state;
2141 terminal->state = escape_state_inner_escape;
2142 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
2143 terminal->state = escape_state_normal;
2144 handle_osc(terminal);
2145 } else {
2146 escape_append_utf8(terminal, utf8);
2147 if (terminal->escape_length >= MAX_ESCAPE)
2148 terminal->state = escape_state_normal;
2149 }
2150 continue;
2151 case escape_state_special:
2152 escape_append_utf8(terminal, utf8);
2153 terminal->state = escape_state_normal;
2154 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2155 handle_special_escape(terminal, terminal->escape[1],
2156 utf8.byte[0]);
2157 }
2158 continue;
2159 default:
2160 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002161 }
2162
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002163 /* this is valid, because ASCII characters are never used to
2164 * introduce a multibyte sequence in UTF-8 */
2165 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002166 terminal->state = escape_state_escape;
2167 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002168 terminal->escape[0] = '\e';
2169 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002170 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002171 } else {
2172 handle_char(terminal, utf8);
2173 } /* if */
2174 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002175
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002176 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002177}
2178
2179static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002180data_source_target(void *data,
2181 struct wl_data_source *source, const char *mime_type)
2182{
2183 fprintf(stderr, "data_source_target, %s\n", mime_type);
2184}
2185
2186static void
2187data_source_send(void *data,
2188 struct wl_data_source *source,
2189 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002190{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002191 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002192
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002193 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002194}
2195
2196static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002197data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002198{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002199 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002200}
2201
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002202static const struct wl_data_source_listener data_source_listener = {
2203 data_source_target,
2204 data_source_send,
2205 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002206};
2207
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07002208static const char text_mime_type[] = "text/plain;charset=utf-8";
2209
2210static void
2211data_handler(struct window *window,
2212 struct input *input,
2213 float x, float y, const char **types, void *data)
2214{
2215 int i, has_text = 0;
2216
2217 if (!types)
2218 return;
2219 for (i = 0; types[i]; i++)
2220 if (strcmp(types[i], text_mime_type) == 0)
2221 has_text = 1;
2222
2223 if (!has_text) {
2224 input_accept(input, NULL);
2225 } else {
2226 input_accept(input, text_mime_type);
2227 }
2228}
2229
2230static void
2231drop_handler(struct window *window, struct input *input,
2232 int32_t x, int32_t y, void *data)
2233{
2234 struct terminal *terminal = data;
2235
2236 input_receive_drag_data_to_fd(input, text_mime_type, terminal->master);
2237}
2238
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002239static void
2240fullscreen_handler(struct window *window, void *data)
2241{
2242 struct terminal *terminal = data;
2243
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002244 window_set_fullscreen(window, !window_is_fullscreen(terminal->window));
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002245}
2246
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002247static void
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002248close_handler(void *data)
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002249{
2250 struct terminal *terminal = data;
2251
2252 terminal_destroy(terminal);
2253}
2254
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002255static void
2256terminal_copy(struct terminal *terminal, struct input *input)
2257{
2258 terminal->selection =
2259 display_create_data_source(terminal->display);
2260 wl_data_source_offer(terminal->selection,
2261 "text/plain;charset=utf-8");
2262 wl_data_source_add_listener(terminal->selection,
2263 &data_source_listener, terminal);
2264 input_set_selection(input, terminal->selection,
2265 display_get_serial(terminal->display));
2266}
2267
2268static void
2269terminal_paste(struct terminal *terminal, struct input *input)
2270{
2271 input_receive_selection_data_to_fd(input,
2272 "text/plain;charset=utf-8",
2273 terminal->master);
2274
2275}
2276
2277static void
2278terminal_new_instance(struct terminal *terminal)
2279{
2280 struct terminal *new_terminal;
2281
2282 new_terminal = terminal_create(terminal->display);
2283 if (terminal_run(new_terminal, option_shell))
2284 terminal_destroy(new_terminal);
2285}
2286
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002287static int
2288handle_bound_key(struct terminal *terminal,
2289 struct input *input, uint32_t sym, uint32_t time)
2290{
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002291 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002292 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002293 /* Cut selection; terminal doesn't do cut, fall
2294 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002295 case XKB_KEY_C:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002296 terminal_copy(terminal, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002297 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002298 case XKB_KEY_V:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002299 terminal_paste(terminal, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002300 return 1;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002301 case XKB_KEY_N:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002302 terminal_new_instance(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002303 return 1;
2304
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002305 case XKB_KEY_Up:
2306 if (!terminal->scrolling)
2307 terminal->saved_start = terminal->start;
2308 if (terminal->start == terminal->end - terminal->log_size)
2309 return 1;
2310
2311 terminal->scrolling = 1;
2312 terminal->start--;
2313 terminal->row++;
2314 terminal->selection_start_row++;
2315 terminal->selection_end_row++;
2316 widget_schedule_redraw(terminal->widget);
2317 return 1;
2318
2319 case XKB_KEY_Down:
2320 if (!terminal->scrolling)
2321 terminal->saved_start = terminal->start;
2322
2323 if (terminal->start == terminal->saved_start)
2324 return 1;
2325
2326 terminal->scrolling = 1;
2327 terminal->start++;
2328 terminal->row--;
2329 terminal->selection_start_row--;
2330 terminal->selection_end_row--;
2331 widget_schedule_redraw(terminal->widget);
2332 return 1;
2333
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002334 default:
2335 return 0;
2336 }
2337}
2338
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002339static void
2340key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002341 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2342 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002343{
2344 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002345 char ch[MAX_RESPONSE];
Kristian Høgsberg88fd4082012-06-21 15:55:03 -04002346 uint32_t modifiers, serial;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002347 int ret, len = 0, d;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002348 bool convert_utf8 = true;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002349
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002350 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002351 if ((modifiers & MOD_CONTROL_MASK) &&
2352 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002353 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2354 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002355 return;
2356
Daniel Stonea8468712012-11-07 17:51:35 +11002357 /* Map keypad symbols to 'normal' equivalents before processing */
2358 switch (sym) {
2359 case XKB_KEY_KP_Space:
2360 sym = XKB_KEY_space;
2361 break;
2362 case XKB_KEY_KP_Tab:
2363 sym = XKB_KEY_Tab;
2364 break;
2365 case XKB_KEY_KP_Enter:
2366 sym = XKB_KEY_Return;
2367 break;
2368 case XKB_KEY_KP_Left:
2369 sym = XKB_KEY_Left;
2370 break;
2371 case XKB_KEY_KP_Up:
2372 sym = XKB_KEY_Up;
2373 break;
2374 case XKB_KEY_KP_Right:
2375 sym = XKB_KEY_Right;
2376 break;
2377 case XKB_KEY_KP_Down:
2378 sym = XKB_KEY_Down;
2379 break;
2380 case XKB_KEY_KP_Equal:
2381 sym = XKB_KEY_equal;
2382 break;
2383 case XKB_KEY_KP_Multiply:
2384 sym = XKB_KEY_asterisk;
2385 break;
2386 case XKB_KEY_KP_Add:
2387 sym = XKB_KEY_plus;
2388 break;
2389 case XKB_KEY_KP_Separator:
2390 /* Note this is actually locale-dependent and should mostly be
2391 * a comma. But leave it as period until we one day start
2392 * doing the right thing. */
2393 sym = XKB_KEY_period;
2394 break;
2395 case XKB_KEY_KP_Subtract:
2396 sym = XKB_KEY_minus;
2397 break;
2398 case XKB_KEY_KP_Decimal:
2399 sym = XKB_KEY_period;
2400 break;
2401 case XKB_KEY_KP_Divide:
2402 sym = XKB_KEY_slash;
2403 break;
2404 case XKB_KEY_KP_0:
2405 case XKB_KEY_KP_1:
2406 case XKB_KEY_KP_2:
2407 case XKB_KEY_KP_3:
2408 case XKB_KEY_KP_4:
2409 case XKB_KEY_KP_5:
2410 case XKB_KEY_KP_6:
2411 case XKB_KEY_KP_7:
2412 case XKB_KEY_KP_8:
2413 case XKB_KEY_KP_9:
2414 sym = (sym - XKB_KEY_KP_0) + XKB_KEY_0;
2415 break;
2416 default:
2417 break;
2418 }
2419
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002420 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002421 case XKB_KEY_BackSpace:
Kristian Høgsbergb7f94bf2012-06-21 12:29:36 -04002422 if (modifiers & MOD_ALT_MASK)
2423 ch[len++] = 0x1b;
Kristian Høgsberg71a4cf42012-06-20 17:57:56 -04002424 ch[len++] = 0x7f;
2425 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002426 case XKB_KEY_Tab:
2427 case XKB_KEY_Linefeed:
2428 case XKB_KEY_Clear:
2429 case XKB_KEY_Pause:
2430 case XKB_KEY_Scroll_Lock:
2431 case XKB_KEY_Sys_Req:
2432 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002433 ch[len++] = sym & 0x7f;
2434 break;
2435
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002436 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002437 if (terminal->mode & MODE_LF_NEWLINE) {
2438 ch[len++] = 0x0D;
2439 ch[len++] = 0x0A;
2440 } else {
2441 ch[len++] = 0x0D;
2442 }
2443 break;
2444
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002445 case XKB_KEY_Shift_L:
2446 case XKB_KEY_Shift_R:
2447 case XKB_KEY_Control_L:
2448 case XKB_KEY_Control_R:
2449 case XKB_KEY_Alt_L:
2450 case XKB_KEY_Alt_R:
Kristian Høgsberg22fbcf72012-06-22 12:18:56 -04002451 case XKB_KEY_Meta_L:
2452 case XKB_KEY_Meta_R:
2453 case XKB_KEY_Super_L:
2454 case XKB_KEY_Super_R:
2455 case XKB_KEY_Hyper_L:
2456 case XKB_KEY_Hyper_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002457 break;
2458
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002459 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002460 len = function_key_response('[', 2, modifiers, '~', ch);
2461 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002462 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002463 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2464 ch[len++] = '\x04';
2465 } else {
2466 len = function_key_response('[', 3, modifiers, '~', ch);
2467 }
2468 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002469 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002470 len = function_key_response('[', 5, modifiers, '~', ch);
2471 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002472 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002473 len = function_key_response('[', 6, modifiers, '~', ch);
2474 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002475 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002476 len = function_key_response('O', 1, modifiers, 'P', ch);
2477 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002478 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002479 len = function_key_response('O', 1, modifiers, 'Q', ch);
2480 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002481 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002482 len = function_key_response('O', 1, modifiers, 'R', ch);
2483 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002484 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002485 len = function_key_response('O', 1, modifiers, 'S', ch);
2486 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002487 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002488 len = function_key_response('[', 15, modifiers, '~', ch);
2489 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002490 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002491 len = function_key_response('[', 17, modifiers, '~', ch);
2492 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002493 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002494 len = function_key_response('[', 18, modifiers, '~', ch);
2495 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002496 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002497 len = function_key_response('[', 19, modifiers, '~', ch);
2498 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002499 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002500 len = function_key_response('[', 20, modifiers, '~', ch);
2501 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002502 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002503 len = function_key_response('[', 21, modifiers, '~', ch);
2504 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002505 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002506 len = function_key_response('[', 24, modifiers, '~', ch);
2507 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002508 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002509 /* Handle special keys with alternate mappings */
2510 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2511 if (len != 0) break;
Michael Vetter2a18a522015-05-15 17:17:47 +02002512
Kristian Høgsberg70163132012-05-08 15:55:39 -04002513 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002514 if (sym >= '3' && sym <= '7')
2515 sym = (sym & 0x1f) + 8;
2516
2517 if (!((sym >= '!' && sym <= '/') ||
2518 (sym >= '8' && sym <= '?') ||
2519 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2520 else if (sym == '2') sym = 0x00;
2521 else if (sym == '/') sym = 0x1F;
2522 else if (sym == '8' || sym == '?') sym = 0x7F;
Kristian Høgsbergae9e0732012-06-21 12:30:15 -04002523 }
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002524 if (modifiers & MOD_ALT_MASK) {
2525 if (terminal->mode & MODE_ALT_SENDS_ESC) {
2526 ch[len++] = 0x1b;
2527 } else {
2528 sym = sym | 0x80;
2529 convert_utf8 = false;
2530 }
Callum Lowcay7e08e902011-01-07 19:47:02 +00002531 }
2532
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002533 if ((sym < 128) ||
2534 (!convert_utf8 && sym < 256)) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002535 ch[len++] = sym;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002536 } else {
2537 ret = xkb_keysym_to_utf8(sym, ch + len,
2538 MAX_RESPONSE - len);
2539 if (ret < 0)
2540 fprintf(stderr,
2541 "Warning: buffer too small to encode "
2542 "UTF8 character\n");
2543 else
2544 len += ret;
2545 }
2546
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002547 break;
2548 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002549
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002550 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002551 if (terminal->scrolling) {
2552 d = terminal->saved_start - terminal->start;
2553 terminal->row -= d;
2554 terminal->selection_start_row -= d;
2555 terminal->selection_end_row -= d;
2556 terminal->start = terminal->saved_start;
2557 terminal->scrolling = 0;
2558 widget_schedule_redraw(terminal->widget);
2559 }
2560
Kristian Høgsberg26130862011-08-24 11:30:21 -04002561 terminal_write(terminal, ch, len);
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002562
2563 /* Hide cursor, except if this was coming from a
2564 * repeating key press. */
2565 serial = display_get_serial(terminal->display);
2566 if (terminal->hide_cursor_serial != serial) {
2567 input_set_pointer_image(input, CURSOR_BLANK);
2568 terminal->hide_cursor_serial = serial;
2569 }
2570 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002571}
2572
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002573static void
2574keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002575 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002576{
2577 struct terminal *terminal = data;
2578
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002579 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002580}
2581
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002582static int wordsep(int ch)
2583{
2584 const char extra[] = "-,./?%&#:_=+@~";
2585
Derek Foreman7978bc82015-09-02 16:21:54 -05002586 if (ch > 127 || ch < 0)
Andre Heider552d12b2012-08-02 20:59:43 +02002587 return 1;
2588
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002589 return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
2590}
2591
2592static int
2593recompute_selection(struct terminal *terminal)
2594{
2595 struct rectangle allocation;
2596 int col, x, width, height;
2597 int start_row, end_row;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002598 int word_start, eol;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002599 int side_margin, top_margin;
2600 int start_x, end_x;
2601 int cw, ch;
2602 union utf8_char *data;
2603
Peng Wuf291f202013-06-06 15:32:41 +08002604 cw = terminal->average_width;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002605 ch = terminal->extents.height;
2606 widget_get_allocation(terminal->widget, &allocation);
2607 width = terminal->width * cw;
2608 height = terminal->height * ch;
2609 side_margin = allocation.x + (allocation.width - width) / 2;
2610 top_margin = allocation.y + (allocation.height - height) / 2;
2611
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002612 start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1;
2613 end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1;
2614
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002615 if (start_row < end_row ||
2616 (start_row == end_row &&
2617 terminal->selection_start_x < terminal->selection_end_x)) {
2618 terminal->selection_start_row = start_row;
2619 terminal->selection_end_row = end_row;
2620 start_x = terminal->selection_start_x;
2621 end_x = terminal->selection_end_x;
2622 } else {
2623 terminal->selection_start_row = end_row;
2624 terminal->selection_end_row = start_row;
2625 start_x = terminal->selection_end_x;
2626 end_x = terminal->selection_start_x;
2627 }
2628
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002629 eol = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002630 if (terminal->selection_start_row < 0) {
2631 terminal->selection_start_row = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002632 terminal->selection_start_col = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002633 } else {
2634 x = side_margin + cw / 2;
2635 data = terminal_get_row(terminal,
2636 terminal->selection_start_row);
2637 word_start = 0;
2638 for (col = 0; col < terminal->width; col++, x += cw) {
2639 if (col == 0 || wordsep(data[col - 1].ch))
2640 word_start = col;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002641 if (data[col].ch != 0)
2642 eol = col + 1;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002643 if (start_x < x)
2644 break;
2645 }
2646
2647 switch (terminal->dragging) {
2648 case SELECT_LINE:
2649 terminal->selection_start_col = 0;
2650 break;
2651 case SELECT_WORD:
2652 terminal->selection_start_col = word_start;
2653 break;
2654 case SELECT_CHAR:
2655 terminal->selection_start_col = col;
2656 break;
2657 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002658 }
2659
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002660 if (terminal->selection_end_row >= terminal->height) {
2661 terminal->selection_end_row = terminal->height;
2662 terminal->selection_end_col = 0;
2663 } else {
2664 x = side_margin + cw / 2;
2665 data = terminal_get_row(terminal, terminal->selection_end_row);
2666 for (col = 0; col < terminal->width; col++, x += cw) {
2667 if (terminal->dragging == SELECT_CHAR && end_x < x)
2668 break;
2669 if (terminal->dragging == SELECT_WORD &&
2670 end_x < x && wordsep(data[col].ch))
2671 break;
2672 }
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002673 terminal->selection_end_col = col;
2674 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002675
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002676 if (terminal->selection_end_col != terminal->selection_start_col ||
2677 terminal->selection_start_row != terminal->selection_end_row) {
2678 col = terminal->selection_end_col;
2679 if (col > 0 && data[col - 1].ch == 0)
2680 terminal->selection_end_col = terminal->width;
2681 data = terminal_get_row(terminal, terminal->selection_start_row);
2682 if (data[terminal->selection_start_col].ch == 0)
2683 terminal->selection_start_col = eol;
2684 }
2685
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002686 return 1;
2687}
2688
Kristian Høgsberg59826582011-01-20 11:56:57 -05002689static void
Pekka Paalanen807f09c2015-03-27 15:48:54 +02002690terminal_minimize(struct terminal *terminal)
2691{
2692 window_set_minimized(terminal->window);
2693}
2694
2695static void
Jasper St. Pierredda93132014-03-13 12:06:00 -04002696menu_func(void *data, struct input *input, int index)
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002697{
Jasper St. Pierredda93132014-03-13 12:06:00 -04002698 struct window *window = data;
2699 struct terminal *terminal = window_get_user_data(window);
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002700
2701 fprintf(stderr, "picked entry %d\n", index);
2702
2703 switch (index) {
2704 case 0:
2705 terminal_new_instance(terminal);
2706 break;
2707 case 1:
2708 terminal_copy(terminal, input);
2709 break;
2710 case 2:
2711 terminal_paste(terminal, input);
2712 break;
Pekka Paalanen807f09c2015-03-27 15:48:54 +02002713 case 3:
2714 terminal_minimize(terminal);
2715 break;
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002716 }
2717}
2718
2719static void
2720show_menu(struct terminal *terminal, struct input *input, uint32_t time)
2721{
2722 int32_t x, y;
2723 static const char *entries[] = {
Pekka Paalanen807f09c2015-03-27 15:48:54 +02002724 "Open Terminal", "Copy", "Paste", "Minimize"
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002725 };
2726
2727 input_get_position(input, &x, &y);
2728 window_show_menu(terminal->display, input, time, terminal->window,
2729 x - 10, y - 10, menu_func,
2730 entries, ARRAY_LENGTH(entries));
2731}
2732
2733static void
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002734click_handler(struct widget *widget, struct terminal *terminal,
2735 struct input *input, int32_t x, int32_t y,
2736 uint32_t time)
2737{
2738 if (time - terminal->click_time < 500)
2739 terminal->click_count++;
2740 else
2741 terminal->click_count = 1;
2742
2743 terminal->click_time = time;
2744 terminal->dragging = (terminal->click_count - 1) % 3 + SELECT_CHAR;
2745
2746 terminal->selection_end_x = terminal->selection_start_x = x;
2747 terminal->selection_end_y = terminal->selection_start_y = y;
2748 if (recompute_selection(terminal))
2749 widget_schedule_redraw(widget);
2750}
2751
2752static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002753button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002754 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002755 uint32_t button,
2756 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002757{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002758 struct terminal *terminal = data;
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002759 int32_t x, y;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002760
2761 switch (button) {
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002762 case BTN_LEFT:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002763 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002764 input_get_position(input, &x, &y);
2765 click_handler(widget, terminal, input, x, y, time);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002766 } else {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002767 terminal->dragging = SELECT_NONE;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002768 }
2769 break;
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002770
2771 case BTN_RIGHT:
2772 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
2773 show_menu(terminal, input, time);
2774 break;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002775 }
2776}
2777
2778static int
Kristian Høgsberg29784402012-06-28 14:27:02 -04002779enter_handler(struct widget *widget,
2780 struct input *input, float x, float y, void *data)
2781{
2782 return CURSOR_IBEAM;
2783}
2784
2785static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002786motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002787 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002788 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002789{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002790 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002791
2792 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002793 input_get_position(input,
2794 &terminal->selection_end_x,
2795 &terminal->selection_end_y);
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002796
2797 if (recompute_selection(terminal))
2798 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002799 }
2800
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002801 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002802}
2803
Magnus Hoff1046f122014-08-05 15:05:59 +02002804/* This magnitude is chosen rather arbitrarily. Really, the scrolling
2805 * should happen on a (fractional) pixel basis, not a line basis. */
2806#define AXIS_UNITS_PER_LINE 256
2807
2808static void
2809axis_handler(struct widget *widget,
2810 struct input *input, uint32_t time,
2811 uint32_t axis,
2812 wl_fixed_t value,
2813 void *data)
2814{
2815 struct terminal *terminal = data;
2816 int lines;
2817
2818 if (axis != WL_POINTER_AXIS_VERTICAL_SCROLL)
2819 return;
2820
2821 terminal->smooth_scroll += value;
2822 lines = terminal->smooth_scroll / AXIS_UNITS_PER_LINE;
2823 terminal->smooth_scroll -= lines * AXIS_UNITS_PER_LINE;
2824
2825 if (lines > 0) {
2826 if (terminal->scrolling) {
2827 if ((uint32_t)lines > terminal->saved_start - terminal->start)
2828 lines = terminal->saved_start - terminal->start;
2829 } else {
2830 lines = 0;
2831 }
2832 } else if (lines < 0) {
2833 uint32_t neg_lines = -lines;
2834
2835 if (neg_lines > terminal->log_size + terminal->start - terminal->end)
2836 lines = terminal->end - terminal->log_size - terminal->start;
2837 }
2838
2839 if (lines) {
2840 if (!terminal->scrolling)
2841 terminal->saved_start = terminal->start;
2842 terminal->scrolling = 1;
2843
2844 terminal->start += lines;
2845 terminal->row -= lines;
2846 terminal->selection_start_row -= lines;
2847 terminal->selection_end_row -= lines;
2848
2849 widget_schedule_redraw(widget);
2850 }
2851}
2852
Alexander Larssonde79dd02013-05-22 14:41:34 +02002853static void
2854output_handler(struct window *window, struct output *output, int enter,
2855 void *data)
2856{
2857 if (enter)
2858 window_set_buffer_transform(window, output_get_transform(output));
2859 window_set_buffer_scale(window, window_get_output_scale(window));
2860 window_schedule_redraw(window);
2861}
2862
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002863static void
2864touch_down_handler(struct widget *widget, struct input *input,
2865 uint32_t serial, uint32_t time, int32_t id,
2866 float x, float y, void *data)
2867{
2868 struct terminal *terminal = data;
2869
2870 if (id == 0)
2871 click_handler(widget, terminal, input, x, y, time);
2872}
2873
2874static void
2875touch_up_handler(struct widget *widget, struct input *input,
2876 uint32_t serial, uint32_t time, int32_t id, void *data)
2877{
2878 struct terminal *terminal = data;
2879
2880 if (id == 0)
2881 terminal->dragging = SELECT_NONE;
2882}
2883
2884static void
2885touch_motion_handler(struct widget *widget, struct input *input,
2886 uint32_t time, int32_t id, float x, float y, void *data)
2887{
2888 struct terminal *terminal = data;
2889
2890 if (terminal->dragging &&
2891 id == 0) {
2892 terminal->selection_end_x = (int)x;
2893 terminal->selection_end_y = (int)y;
2894
2895 if (recompute_selection(terminal))
2896 widget_schedule_redraw(widget);
2897 }
2898}
2899
Peng Wuf291f202013-06-06 15:32:41 +08002900#ifndef howmany
2901#define howmany(x, y) (((x) + ((y) - 1)) / (y))
2902#endif
2903
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002904static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002905terminal_create(struct display *display)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002906{
2907 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002908 cairo_surface_t *surface;
2909 cairo_t *cr;
Peng Wuf291f202013-06-06 15:32:41 +08002910 cairo_text_extents_t text_extents;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002911
Peter Huttererf3d62272013-08-08 11:57:05 +10002912 terminal = xzalloc(sizeof *terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002913 terminal->color_scheme = &DEFAULT_COLORS;
2914 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002915 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002916 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002917 terminal->window = window_create(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002918 terminal->widget = window_frame_create(terminal->window, terminal);
U. Artie Eoff09827e22014-01-15 11:40:37 -08002919 terminal->title = xstrdup("Wayland Terminal");
Kristian Høgsberga83be202013-10-23 20:47:35 -07002920 window_set_title(terminal->window, terminal->title);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002921 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002922
2923 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002924 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002925
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002926 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002927 terminal->margin = 5;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002928 terminal->buffer_height = 1024;
2929 terminal->end = 1;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002930
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002931 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002932 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002933 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002934 keyboard_focus_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002935 window_set_fullscreen_handler(terminal->window, fullscreen_handler);
Alexander Larssonde79dd02013-05-22 14:41:34 +02002936 window_set_output_handler(terminal->window, output_handler);
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002937 window_set_close_handler(terminal->window, close_handler);
Jasper St. Pierrede680992014-04-10 17:23:49 -07002938 window_set_state_changed_handler(terminal->window, state_changed_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002939
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07002940 window_set_data_handler(terminal->window, data_handler);
2941 window_set_drop_handler(terminal->window, drop_handler);
2942
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002943 widget_set_redraw_handler(terminal->widget, redraw_handler);
2944 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002945 widget_set_button_handler(terminal->widget, button_handler);
Kristian Høgsberg29784402012-06-28 14:27:02 -04002946 widget_set_enter_handler(terminal->widget, enter_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002947 widget_set_motion_handler(terminal->widget, motion_handler);
Magnus Hoff1046f122014-08-05 15:05:59 +02002948 widget_set_axis_handler(terminal->widget, axis_handler);
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002949 widget_set_touch_up_handler(terminal->widget, touch_up_handler);
2950 widget_set_touch_down_handler(terminal->widget, touch_down_handler);
2951 widget_set_touch_motion_handler(terminal->widget, touch_motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002952
Kristian Høgsberg09531622010-06-14 23:22:15 -04002953 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2954 cr = cairo_create(surface);
Kristian Høgsberg38912df2012-06-27 17:52:23 -04002955 cairo_set_font_size(cr, option_font_size);
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002956 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002957 CAIRO_FONT_SLANT_NORMAL,
2958 CAIRO_FONT_WEIGHT_BOLD);
2959 terminal->font_bold = cairo_get_scaled_font (cr);
2960 cairo_scaled_font_reference(terminal->font_bold);
2961
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002962 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002963 CAIRO_FONT_SLANT_NORMAL,
2964 CAIRO_FONT_WEIGHT_NORMAL);
2965 terminal->font_normal = cairo_get_scaled_font (cr);
2966 cairo_scaled_font_reference(terminal->font_normal);
2967
Kristian Høgsberg09531622010-06-14 23:22:15 -04002968 cairo_font_extents(cr, &terminal->extents);
Peng Wuf291f202013-06-06 15:32:41 +08002969
2970 /* Compute the average ascii glyph width */
2971 cairo_text_extents(cr, TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS,
2972 &text_extents);
2973 terminal->average_width = howmany
2974 (text_extents.width,
2975 strlen(TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS));
2976 terminal->average_width = ceil(terminal->average_width);
2977
Kristian Høgsberg09531622010-06-14 23:22:15 -04002978 cairo_destroy(cr);
2979 cairo_surface_destroy(surface);
2980
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002981 terminal_resize(terminal, 20, 5); /* Set minimum size first */
Kristian Høgsberga1627922012-06-20 17:30:03 -04002982 terminal_resize(terminal, 80, 25);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002983
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002984 wl_list_insert(terminal_list.prev, &terminal->link);
2985
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002986 return terminal;
2987}
2988
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002989static void
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002990terminal_destroy(struct terminal *terminal)
2991{
Dima Ryazanova85292e2012-11-29 00:27:09 -08002992 display_unwatch_fd(terminal->display, terminal->master);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002993 window_destroy(terminal->window);
2994 close(terminal->master);
2995 wl_list_remove(&terminal->link);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002996
2997 if (wl_list_empty(&terminal_list))
Dima Ryazanova85292e2012-11-29 00:27:09 -08002998 display_exit(terminal->display);
2999
Kristian Høgsberga83be202013-10-23 20:47:35 -07003000 free(terminal->title);
Dima Ryazanova85292e2012-11-29 00:27:09 -08003001 free(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003002}
3003
3004static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003005io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003006{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003007 struct terminal *terminal =
3008 container_of(task, struct terminal, io_task);
3009 char buffer[256];
3010 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003011
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003012 if (events & EPOLLHUP) {
3013 terminal_destroy(terminal);
3014 return;
3015 }
Tim Wiederhakef71accc2011-01-19 23:14:33 +01003016
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003017 len = read(terminal->master, buffer, sizeof buffer);
3018 if (len < 0)
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003019 terminal_destroy(terminal);
3020 else
3021 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003022}
3023
3024static int
3025terminal_run(struct terminal *terminal, const char *path)
3026{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003027 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003028 pid_t pid;
3029
3030 pid = forkpty(&master, NULL, NULL, NULL);
3031 if (pid == 0) {
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04003032 setenv("TERM", option_term, 1);
3033 setenv("COLORTERM", option_term, 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003034 if (execl(path, path, NULL)) {
3035 printf("exec failed: %m\n");
3036 exit(EXIT_FAILURE);
3037 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003038 } else if (pid < 0) {
3039 fprintf(stderr, "failed to fork and create pty (%m).\n");
3040 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003041 }
3042
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003043 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003044 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003045 terminal->io_task.run = io_handler;
3046 display_watch_fd(terminal->display, terminal->master,
3047 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003048
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07003049 if (option_fullscreen)
3050 window_set_fullscreen(terminal->window, 1);
3051 else
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003052 terminal_resize(terminal, 80, 24);
3053
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003054 return 0;
3055}
3056
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003057static const struct weston_option terminal_options[] = {
3058 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04003059 { WESTON_OPTION_STRING, "font", 0, &option_font },
Bill Spitzak5cad8432014-08-08 12:59:55 -07003060 { WESTON_OPTION_INTEGER, "font-size", 0, &option_font_size },
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003061 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003062};
3063
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003064int main(int argc, char *argv[])
3065{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003066 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003067 struct terminal *terminal;
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02003068 const char *config_file;
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -07003069 struct weston_config *config;
3070 struct weston_config_section *s;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04003071
Peng Wucfcc1112013-08-19 10:59:21 +08003072 /* as wcwidth is locale-dependent,
3073 wcwidth needs setlocale call to function properly. */
3074 setlocale(LC_ALL, "");
3075
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003076 option_shell = getenv("SHELL");
3077 if (!option_shell)
3078 option_shell = "/bin/bash";
3079
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02003080 config_file = weston_config_get_name_from_env();
3081 config = weston_config_parse(config_file);
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -07003082 s = weston_config_get_section(config, "terminal", NULL, NULL);
3083 weston_config_section_get_string(s, "font", &option_font, "mono");
3084 weston_config_section_get_int(s, "font-size", &option_font_size, 14);
3085 weston_config_section_get_string(s, "term", &option_term, "xterm");
3086 weston_config_destroy(config);
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003087
Bill Spitzak5cad8432014-08-08 12:59:55 -07003088 if (parse_options(terminal_options,
3089 ARRAY_LENGTH(terminal_options), &argc, argv) > 1) {
3090 printf("Usage: %s [OPTIONS]\n"
3091 " --fullscreen or -f\n"
3092 " --font=NAME\n"
3093 " --font-size=SIZE\n"
3094 " --shell=NAME\n", argv[0]);
3095 return 1;
3096 }
3097
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003098 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02003099 if (d == NULL) {
3100 fprintf(stderr, "failed to create display: %m\n");
3101 return -1;
3102 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003103
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003104 wl_list_init(&terminal_list);
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04003105 terminal = terminal_create(d);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003106 if (terminal_run(terminal, option_shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003107 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003108
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003109 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003110
3111 return 0;
3112}