blob: c5531790716adfb2c3c49143ad1440eeedf5bb37 [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;
Derek Foreman76091782017-03-08 11:58:20 -060052static int option_maximize;
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -070053static char *option_font;
54static int option_font_size;
55static char *option_term;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040056static char *option_shell;
57
58static struct wl_list terminal_list;
59
60static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -040061terminal_create(struct display *display);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040062static void
63terminal_destroy(struct terminal *terminal);
64static int
65terminal_run(struct terminal *terminal, const char *path);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050066
Peng Wuf291f202013-06-06 15:32:41 +080067#define TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS \
68 " !\"#$%&'()*+,-./" \
69 "0123456789" \
70 ":;<=>?@" \
71 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
72 "[\\]^_`" \
73 "abcdefghijklmnopqrstuvwxyz" \
74 "{|}~" \
75 ""
76
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050077#define MOD_SHIFT 0x01
78#define MOD_ALT 0x02
79#define MOD_CTRL 0x04
80
Callum Lowcay30eeae52011-01-07 19:46:55 +000081#define ATTRMASK_BOLD 0x01
82#define ATTRMASK_UNDERLINE 0x02
83#define ATTRMASK_BLINK 0x04
84#define ATTRMASK_INVERSE 0x08
Callum Lowcay81179db2011-01-10 12:14:01 +130085#define ATTRMASK_CONCEALED 0x10
Callum Lowcay30eeae52011-01-07 19:46:55 +000086
Callum Lowcayb8609ad2011-01-07 19:46:57 +000087/* Buffer sizes */
Callum Lowcayef57a9b2011-01-14 20:46:23 +130088#define MAX_RESPONSE 256
89#define MAX_ESCAPE 255
Callum Lowcayb8609ad2011-01-07 19:46:57 +000090
Callum Lowcay8e57dd52011-01-07 19:46:59 +000091/* Terminal modes */
92#define MODE_SHOW_CURSOR 0x00000001
93#define MODE_INVERSE 0x00000002
94#define MODE_AUTOWRAP 0x00000004
95#define MODE_AUTOREPEAT 0x00000008
96#define MODE_LF_NEWLINE 0x00000010
Callum Lowcay69e96582011-01-07 19:47:00 +000097#define MODE_IRM 0x00000020
Callum Lowcay7e08e902011-01-07 19:47:02 +000098#define MODE_DELETE_SENDS_DEL 0x00000040
99#define MODE_ALT_SENDS_ESC 0x00000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000100
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000101union utf8_char {
102 unsigned char byte[4];
103 uint32_t ch;
104};
105
106enum utf8_state {
107 utf8state_start,
108 utf8state_accept,
109 utf8state_reject,
110 utf8state_expect3,
111 utf8state_expect2,
112 utf8state_expect1
113};
114
115struct utf8_state_machine {
116 enum utf8_state state;
117 int len;
118 union utf8_char s;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700119 uint32_t unicode;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000120};
121
122static void
123init_state_machine(struct utf8_state_machine *machine)
124{
125 machine->state = utf8state_start;
126 machine->len = 0;
127 machine->s.ch = 0;
Quentin Glidicc0271532016-07-10 11:00:57 +0200128 machine->unicode = 0;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000129}
130
131static enum utf8_state
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400132utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000133{
134 switch(machine->state) {
135 case utf8state_start:
136 case utf8state_accept:
137 case utf8state_reject:
138 machine->s.ch = 0;
139 machine->len = 0;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300140 if (c == 0xC0 || c == 0xC1) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000141 /* overlong encoding, reject */
142 machine->state = utf8state_reject;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300143 } else if ((c & 0x80) == 0) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000144 /* single byte, accept */
145 machine->s.byte[machine->len++] = c;
146 machine->state = utf8state_accept;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700147 machine->unicode = c;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300148 } else if ((c & 0xC0) == 0x80) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000149 /* parser out of sync, ignore byte */
150 machine->state = utf8state_start;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300151 } else if ((c & 0xE0) == 0xC0) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000152 /* start of two byte sequence */
153 machine->s.byte[machine->len++] = c;
154 machine->state = utf8state_expect1;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700155 machine->unicode = c & 0x1f;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300156 } else if ((c & 0xF0) == 0xE0) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000157 /* start of three byte sequence */
158 machine->s.byte[machine->len++] = c;
159 machine->state = utf8state_expect2;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700160 machine->unicode = c & 0x0f;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300161 } else if ((c & 0xF8) == 0xF0) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000162 /* start of four byte sequence */
163 machine->s.byte[machine->len++] = c;
164 machine->state = utf8state_expect3;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700165 machine->unicode = c & 0x07;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000166 } else {
167 /* overlong encoding, reject */
168 machine->state = utf8state_reject;
169 }
170 break;
171 case utf8state_expect3:
172 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700173 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300174 if ((c & 0xC0) == 0x80) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000175 /* all good, continue */
176 machine->state = utf8state_expect2;
177 } else {
178 /* missing extra byte, reject */
179 machine->state = utf8state_reject;
180 }
181 break;
182 case utf8state_expect2:
183 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700184 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300185 if ((c & 0xC0) == 0x80) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000186 /* all good, continue */
187 machine->state = utf8state_expect1;
188 } else {
189 /* missing extra byte, reject */
190 machine->state = utf8state_reject;
191 }
192 break;
193 case utf8state_expect1:
194 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700195 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300196 if ((c & 0xC0) == 0x80) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000197 /* all good, accept */
198 machine->state = utf8state_accept;
199 } else {
200 /* missing extra byte, reject */
201 machine->state = utf8state_reject;
202 }
203 break;
204 default:
205 machine->state = utf8state_reject;
206 break;
207 }
Michael Vetter2a18a522015-05-15 17:17:47 +0200208
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000209 return machine->state;
210}
211
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700212static uint32_t
213get_unicode(union utf8_char utf8)
214{
215 struct utf8_state_machine machine;
216 int i;
217
218 init_state_machine(&machine);
219 for (i = 0; i < 4; i++) {
220 utf8_next_char(&machine, utf8.byte[i]);
221 if (machine.state == utf8state_accept ||
222 machine.state == utf8state_reject)
223 break;
224 }
225
226 if (machine.state == utf8state_reject)
227 return 0xfffd;
228
229 return machine.unicode;
230}
231
Peng Wucfcc1112013-08-19 10:59:21 +0800232static bool
233is_wide(union utf8_char utf8)
234{
235 uint32_t unichar = get_unicode(utf8);
236 return wcwidth(unichar) > 1;
237}
238
Callum Lowcay256e72f2011-01-07 19:47:01 +0000239struct char_sub {
240 union utf8_char match;
241 union utf8_char replace;
242};
243/* Set last char_sub match to NULL char */
244typedef struct char_sub *character_set;
245
246struct char_sub CS_US[] = {
247 {{{0, }}, {{0, }}}
248};
249static struct char_sub CS_UK[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200250 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000251 {{{0, }}, {{0, }}}
252};
253static struct char_sub CS_SPECIAL[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200254 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond: ♦ */
255 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell: ▒ */
256 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT: ␉ */
257 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF: ␌ */
258 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR: ␍ */
259 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF: ␊ */
260 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree: ° */
261 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus: ± */
262 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL: ␤ */
263 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT: ␋ */
264 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB: ┘ */
265 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT: ┐ */
266 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT: ┌ */
267 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_LB: └ */
268 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS: ┼ */
269 {{{'o', 0, }}, {{0xE2, 0x8E, 0xBA, 0}}}, /* Horiz. Scan Line 1: ⎺ */
270 {{{'p', 0, }}, {{0xE2, 0x8E, 0xBB, 0}}}, /* Horiz. Scan Line 3: ⎻ */
271 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* Horiz. Scan Line 5: ─ */
272 {{{'r', 0, }}, {{0xE2, 0x8E, 0xBC, 0}}}, /* Horiz. Scan Line 7: ⎼ */
273 {{{'s', 0, }}, {{0xE2, 0x8E, 0xBD, 0}}}, /* Horiz. Scan Line 9: ⎽ */
274 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR: ├ */
275 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL: ┤ */
276 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU: ┴ */
277 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD: ┬ */
278 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V: │ */
279 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE: ≤ */
280 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE: ≥ */
281 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI: π */
282 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ: ≠ */
283 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
284 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT: ⋅ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000285 {{{0, }}, {{0, }}}
286};
287
288static void
289apply_char_set(character_set cs, union utf8_char *utf8)
290{
291 int i = 0;
Michael Vetter2a18a522015-05-15 17:17:47 +0200292
Callum Lowcay256e72f2011-01-07 19:47:01 +0000293 while (cs[i].match.byte[0]) {
294 if ((*utf8).ch == cs[i].match.ch) {
295 *utf8 = cs[i].replace;
296 break;
297 }
298 i++;
299 }
300}
301
Callum Lowcay7e08e902011-01-07 19:47:02 +0000302struct key_map {
303 int sym;
304 int num;
305 char escape;
306 char code;
307};
308/* Set last key_sub sym to NULL */
309typedef struct key_map *keyboard_mode;
310
311static struct key_map KM_NORMAL[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400312 { XKB_KEY_Left, 1, '[', 'D' },
313 { XKB_KEY_Right, 1, '[', 'C' },
314 { XKB_KEY_Up, 1, '[', 'A' },
315 { XKB_KEY_Down, 1, '[', 'B' },
316 { XKB_KEY_Home, 1, '[', 'H' },
317 { XKB_KEY_End, 1, '[', 'F' },
318 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000319};
320static struct key_map KM_APPLICATION[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400321 { XKB_KEY_Left, 1, 'O', 'D' },
322 { XKB_KEY_Right, 1, 'O', 'C' },
323 { XKB_KEY_Up, 1, 'O', 'A' },
324 { XKB_KEY_Down, 1, 'O', 'B' },
325 { XKB_KEY_Home, 1, 'O', 'H' },
326 { XKB_KEY_End, 1, 'O', 'F' },
327 { XKB_KEY_KP_Enter, 1, 'O', 'M' },
328 { XKB_KEY_KP_Multiply, 1, 'O', 'j' },
329 { XKB_KEY_KP_Add, 1, 'O', 'k' },
330 { XKB_KEY_KP_Separator, 1, 'O', 'l' },
331 { XKB_KEY_KP_Subtract, 1, 'O', 'm' },
332 { XKB_KEY_KP_Divide, 1, 'O', 'o' },
333 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000334};
335
336static int
337function_key_response(char escape, int num, uint32_t modifiers,
338 char code, char *response)
339{
340 int mod_num = 0;
341 int len;
342
Kristian Høgsberg70163132012-05-08 15:55:39 -0400343 if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
344 if (modifiers & MOD_ALT_MASK) mod_num |= 2;
345 if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000346
347 if (mod_num != 0)
348 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
349 num, mod_num + 1, code);
350 else if (code != '~')
351 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
352 escape, code);
353 else
354 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
355 escape, num, code);
356
357 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
358 else return len;
359}
360
361/* returns the number of bytes written into response,
362 * which must have room for MAX_RESPONSE bytes */
363static int
364apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
365{
366 struct key_map map;
367 int len = 0;
368 int i = 0;
Michael Vetter2a18a522015-05-15 17:17:47 +0200369
Callum Lowcay7e08e902011-01-07 19:47:02 +0000370 while (mode[i].sym) {
371 map = mode[i++];
372 if (sym == map.sym) {
373 len = function_key_response(map.escape, map.num,
374 modifiers, map.code,
375 response);
376 break;
377 }
378 }
Michael Vetter2a18a522015-05-15 17:17:47 +0200379
Callum Lowcay7e08e902011-01-07 19:47:02 +0000380 return len;
381}
382
Callum Lowcay30eeae52011-01-07 19:46:55 +0000383struct terminal_color { double r, g, b, a; };
384struct attr {
385 unsigned char fg, bg;
386 char a; /* attributes format:
387 * 76543210
Callum Lowcay81179db2011-01-10 12:14:01 +1300388 * cilub */
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500389 char s; /* in selection */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000390};
391struct color_scheme {
392 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500393 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000394 struct attr default_attr;
395};
396
397static void
398attr_init(struct attr *data_attr, struct attr attr, int n)
399{
400 int i;
401 for (i = 0; i < n; i++) {
402 data_attr[i] = attr;
403 }
404}
405
Callum Lowcay67a201d2011-01-12 19:23:41 +1300406enum escape_state {
407 escape_state_normal = 0,
408 escape_state_escape,
409 escape_state_dcs,
410 escape_state_csi,
411 escape_state_osc,
412 escape_state_inner_escape,
413 escape_state_ignore,
414 escape_state_special
415};
416
417#define ESC_FLAG_WHAT 0x01
418#define ESC_FLAG_GT 0x02
419#define ESC_FLAG_BANG 0x04
420#define ESC_FLAG_CASH 0x08
421#define ESC_FLAG_SQUOTE 0x10
422#define ESC_FLAG_DQUOTE 0x20
423#define ESC_FLAG_SPACE 0x40
424
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400425enum {
426 SELECT_NONE,
427 SELECT_CHAR,
428 SELECT_WORD,
429 SELECT_LINE
430};
431
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500432struct terminal {
433 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500434 struct widget *widget;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500435 struct display *display;
Kristian Høgsberga83be202013-10-23 20:47:35 -0700436 char *title;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000437 union utf8_char *data;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400438 struct task io_task;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000439 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000440 struct attr *data_attr;
441 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000442 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000443 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000444 char saved_origin_mode;
445 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000446 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000447 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000448 character_set cs, g0, g1;
449 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000450 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000451 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700452 int width, height, row, column, max_width;
453 uint32_t buffer_height;
454 uint32_t start, end, saved_start, log_size;
Magnus Hoff1046f122014-08-05 15:05:59 +0200455 wl_fixed_t smooth_scroll;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000456 int saved_row, saved_column;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700457 int scrolling;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600458 int send_cursor_position;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500459 int fd, master;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500460 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300461 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500462 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300463 enum escape_state state;
464 enum escape_state outer_state;
465 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000466 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500467 int margin;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400468 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000469 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400470 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +0800471 double average_width;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500472 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg88fd4082012-06-21 15:55:03 -0400473 uint32_t hide_cursor_serial;
Kristian Høgsbergb405a802014-02-05 14:44:04 -0800474 int size_in_title;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500475
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400476 struct wl_data_source *selection;
Xiong Zhang49c6aee2013-11-25 18:42:52 +0800477 uint32_t click_time;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400478 int dragging, click_count;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500479 int selection_start_x, selection_start_y;
480 int selection_end_x, selection_end_y;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400481 int selection_start_row, selection_start_col;
482 int selection_end_row, selection_end_col;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -0400483 struct wl_list link;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500484};
485
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000486/* Create default tab stops, every 8 characters */
487static void
488terminal_init_tabs(struct terminal *terminal)
489{
490 int i = 0;
Michael Vetter2a18a522015-05-15 17:17:47 +0200491
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000492 while (i < terminal->width) {
493 if (i % 8 == 0)
494 terminal->tab_ruler[i] = 1;
495 else
496 terminal->tab_ruler[i] = 0;
497 i++;
498 }
499}
500
Callum Lowcay30eeae52011-01-07 19:46:55 +0000501static void
502terminal_init(struct terminal *terminal)
503{
504 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000505 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000506 terminal->mode = MODE_SHOW_CURSOR |
507 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000508 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000509 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000510
511 terminal->row = 0;
512 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000513
Callum Lowcay256e72f2011-01-07 19:47:01 +0000514 terminal->g0 = CS_US;
515 terminal->g1 = CS_US;
516 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000517 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000518
519 terminal->saved_g0 = terminal->g0;
520 terminal->saved_g1 = terminal->g1;
521 terminal->saved_cs = terminal->cs;
522
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000523 terminal->saved_attr = terminal->curr_attr;
524 terminal->saved_origin_mode = terminal->origin_mode;
525 terminal->saved_row = terminal->row;
526 terminal->saved_column = terminal->column;
527
528 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000529}
530
531static void
532init_color_table(struct terminal *terminal)
533{
534 int c, r;
535 struct terminal_color *color_table = terminal->color_table;
536
537 for (c = 0; c < 256; c ++) {
538 if (c < 16) {
539 color_table[c] = terminal->color_scheme->palette[c];
540 } else if (c < 232) {
541 r = c - 16;
542 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
543 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
544 color_table[c].r = ((double)(r % 6) / 6.0);
545 color_table[c].a = 1.0;
546 } else {
547 r = (c - 232) * 10 + 8;
548 color_table[c].r = ((double) r) / 256.0;
549 color_table[c].g = color_table[c].r;
550 color_table[c].b = color_table[c].r;
551 color_table[c].a = 1.0;
552 }
553 }
554}
555
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000556static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500557terminal_get_row(struct terminal *terminal, int row)
558{
559 int index;
560
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700561 index = (row + terminal->start) & (terminal->buffer_height - 1);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500562
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700563 return (void *) terminal->data + index * terminal->data_pitch;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500564}
565
Callum Lowcay30eeae52011-01-07 19:46:55 +0000566static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500567terminal_get_attr_row(struct terminal *terminal, int row)
568{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000569 int index;
570
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700571 index = (row + terminal->start) & (terminal->buffer_height - 1);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000572
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700573 return (void *) terminal->data_attr + index * terminal->attr_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000574}
575
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500576union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300577 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500578 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500579};
580
581static void
582terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500583 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500584{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500585 struct attr attr;
586 int foreground, background, tmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500587
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500588 decoded->attr.s = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400589 if (((row == terminal->selection_start_row &&
590 col >= terminal->selection_start_col) ||
591 row > terminal->selection_start_row) &&
592 ((row == terminal->selection_end_row &&
593 col < terminal->selection_end_col) ||
594 row < terminal->selection_end_row))
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500595 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500596
597 /* get the attributes for this character cell */
598 attr = terminal_get_attr_row(terminal, row)[col];
599 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500600 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500601 ((terminal->mode & MODE_SHOW_CURSOR) &&
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400602 window_has_focus(terminal->window) && terminal->row == row &&
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500603 terminal->column == col)) {
604 foreground = attr.bg;
605 background = attr.fg;
606 if (attr.a & ATTRMASK_BOLD) {
607 if (foreground <= 16) foreground |= 0x08;
608 if (background <= 16) background &= 0x07;
609 }
610 } else {
611 foreground = attr.fg;
612 background = attr.bg;
613 }
614
615 if (terminal->mode & MODE_INVERSE) {
616 tmp = foreground;
617 foreground = background;
618 background = tmp;
619 if (attr.a & ATTRMASK_BOLD) {
620 if (foreground <= 16) foreground |= 0x08;
621 if (background <= 16) background &= 0x07;
622 }
623 }
624
Callum Lowcay9d708b02011-01-12 20:06:17 +1300625 decoded->attr.fg = foreground;
626 decoded->attr.bg = background;
627 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000628}
629
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500630
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500631static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000632terminal_scroll_buffer(struct terminal *terminal, int d)
633{
634 int i;
635
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700636 terminal->start += d;
637 if (d < 0) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000638 d = 0 - d;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700639 for (i = 0; i < d; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000640 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
641 attr_init(terminal_get_attr_row(terminal, i),
642 terminal->curr_attr, terminal->width);
643 }
644 } else {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700645 for (i = terminal->height - d; i < terminal->height; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000646 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
647 attr_init(terminal_get_attr_row(terminal, i),
648 terminal->curr_attr, terminal->width);
649 }
650 }
Kristian Høgsberg18e928d2012-06-27 19:29:41 -0400651
652 terminal->selection_start_row -= d;
653 terminal->selection_end_row -= d;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000654}
655
656static void
657terminal_scroll_window(struct terminal *terminal, int d)
658{
659 int i;
660 int window_height;
661 int from_row, to_row;
Michael Vetter2a18a522015-05-15 17:17:47 +0200662
Callum Lowcaybbeac602011-01-07 19:46:58 +0000663 // scrolling range is inclusive
664 window_height = terminal->margin_bottom - terminal->margin_top + 1;
665 d = d % (window_height + 1);
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300666 if (d < 0) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000667 d = 0 - d;
668 to_row = terminal->margin_bottom;
669 from_row = terminal->margin_bottom - d;
Michael Vetter2a18a522015-05-15 17:17:47 +0200670
Callum Lowcaybbeac602011-01-07 19:46:58 +0000671 for (i = 0; i < (window_height - d); i++) {
672 memcpy(terminal_get_row(terminal, to_row - i),
673 terminal_get_row(terminal, from_row - i),
674 terminal->data_pitch);
675 memcpy(terminal_get_attr_row(terminal, to_row - i),
676 terminal_get_attr_row(terminal, from_row - i),
677 terminal->attr_pitch);
678 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000679 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
680 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000681 attr_init(terminal_get_attr_row(terminal, i),
682 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000683 }
684 } else {
685 to_row = terminal->margin_top;
686 from_row = terminal->margin_top + d;
Michael Vetter2a18a522015-05-15 17:17:47 +0200687
Callum Lowcaybbeac602011-01-07 19:46:58 +0000688 for (i = 0; i < (window_height - d); i++) {
689 memcpy(terminal_get_row(terminal, to_row + i),
690 terminal_get_row(terminal, from_row + i),
691 terminal->data_pitch);
692 memcpy(terminal_get_attr_row(terminal, to_row + i),
693 terminal_get_attr_row(terminal, from_row + i),
694 terminal->attr_pitch);
695 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000696 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
697 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000698 attr_init(terminal_get_attr_row(terminal, i),
699 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000700 }
701 }
702}
703
704static void
705terminal_scroll(struct terminal *terminal, int d)
706{
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300707 if (terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
Callum Lowcaybbeac602011-01-07 19:46:58 +0000708 terminal_scroll_buffer(terminal, d);
709 else
710 terminal_scroll_window(terminal, d);
711}
712
713static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000714terminal_shift_line(struct terminal *terminal, int d)
715{
716 union utf8_char *row;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500717 struct attr *attr_row;
Michael Vetter2a18a522015-05-15 17:17:47 +0200718
Callum Lowcay69e96582011-01-07 19:47:00 +0000719 row = terminal_get_row(terminal, terminal->row);
720 attr_row = terminal_get_attr_row(terminal, terminal->row);
721
722 if ((terminal->width + d) <= terminal->column)
723 d = terminal->column + 1 - terminal->width;
724 if ((terminal->column + d) >= terminal->width)
725 d = terminal->width - terminal->column - 1;
Michael Vetter2a18a522015-05-15 17:17:47 +0200726
Callum Lowcay69e96582011-01-07 19:47:00 +0000727 if (d < 0) {
728 d = 0 - d;
729 memmove(&row[terminal->column],
730 &row[terminal->column + d],
731 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
Callum Lowcay69e96582011-01-07 19:47:00 +0000732 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
733 (terminal->width - terminal->column - d) * sizeof(struct attr));
734 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
735 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
736 } else {
737 memmove(&row[terminal->column + d], &row[terminal->column],
738 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
739 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
740 (terminal->width - terminal->column - d) * sizeof(struct attr));
741 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
742 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
743 }
744}
745
746static void
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700747terminal_resize_cells(struct terminal *terminal,
748 int width, int height)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500749{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000750 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000751 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000752 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000753 int data_pitch, attr_pitch;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500754 int i, l, total_rows;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700755 uint32_t d, uheight = height;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500756 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000757 struct winsize ws;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500758
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700759 if (uheight > terminal->buffer_height)
760 height = terminal->buffer_height;
761
Kristian Høgsberg22106762008-12-08 13:50:07 -0500762 if (terminal->width == width && terminal->height == height)
763 return;
764
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700765 if (terminal->data && width <= terminal->max_width) {
766 d = 0;
767 if (height < terminal->height && height <= terminal->row)
768 d = terminal->height - height;
769 else if (height > terminal->height &&
770 terminal->height - 1 == terminal->row) {
771 d = terminal->height - height;
772 if (terminal->log_size < uheight)
773 d = -terminal->start;
774 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500775
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700776 terminal->start += d;
777 terminal->row -= d;
778 } else {
779 terminal->max_width = width;
780 data_pitch = width * sizeof(union utf8_char);
Bryce W. Harrington66cd2c12014-04-28 18:44:08 +0000781 data = xzalloc(data_pitch * terminal->buffer_height);
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700782 attr_pitch = width * sizeof(struct attr);
Bryce W. Harrington66cd2c12014-04-28 18:44:08 +0000783 data_attr = xmalloc(attr_pitch * terminal->buffer_height);
784 tab_ruler = xzalloc(width);
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700785 attr_init(data_attr, terminal->curr_attr,
786 width * terminal->buffer_height);
787
788 if (terminal->data && terminal->data_attr) {
789 if (width > terminal->width)
790 l = terminal->width;
791 else
792 l = width;
793
794 if (terminal->height > height) {
795 total_rows = height;
796 i = 1 + terminal->row - height;
797 if (i > 0) {
798 terminal->start += i;
799 terminal->row = terminal->row - i;
800 }
801 } else {
802 total_rows = terminal->height;
José Bollo4a4704a2013-09-13 11:28:51 +0200803 }
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700804
805 for (i = 0; i < total_rows; i++) {
806 memcpy(&data[width * i],
807 terminal_get_row(terminal, i),
808 l * sizeof(union utf8_char));
809 memcpy(&data_attr[width * i],
810 terminal_get_attr_row(terminal, i),
811 l * sizeof(struct attr));
812 }
813
814 free(terminal->data);
815 free(terminal->data_attr);
816 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500817 }
818
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700819 terminal->data_pitch = data_pitch;
820 terminal->attr_pitch = attr_pitch;
821 terminal->data = data;
822 terminal->data_attr = data_attr;
823 terminal->tab_ruler = tab_ruler;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700824 terminal->start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500825 }
826
Callum Lowcay86653ed2011-01-07 19:47:03 +0000827 terminal->margin_bottom =
828 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500829 terminal->width = width;
830 terminal->height = height;
Kristian Høgsbergdcfff552013-11-22 22:43:20 -0800831 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500832
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000833 /* Update the window size */
834 ws.ws_row = terminal->height;
835 ws.ws_col = terminal->width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500836 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500837 ws.ws_xpixel = allocation.width;
838 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000839 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500840}
841
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500842static void
Jasper St. Pierrede680992014-04-10 17:23:49 -0700843update_title(struct terminal *terminal)
844{
845 if (window_is_resizing(terminal->window)) {
846 char *p;
847 if (asprintf(&p, "%s — [%dx%d]", terminal->title, terminal->width, terminal->height) > 0) {
848 window_set_title(terminal->window, p);
849 free(p);
850 }
851 } else {
852 window_set_title(terminal->window, terminal->title);
853 }
854}
855
856static void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500857resize_handler(struct widget *widget,
858 int32_t width, int32_t height, void *data)
859{
860 struct terminal *terminal = data;
861 int32_t columns, rows, m;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700862
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500863 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800864 columns = (width - m) / (int32_t) terminal->average_width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500865 rows = (height - m) / (int32_t) terminal->extents.height;
866
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400867 if (!window_is_fullscreen(terminal->window) &&
868 !window_is_maximized(terminal->window)) {
Peng Wuf291f202013-06-06 15:32:41 +0800869 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500870 height = rows * terminal->extents.height + m;
871 widget_set_size(terminal->widget, width, height);
872 }
873
874 terminal_resize_cells(terminal, columns, rows);
Jasper St. Pierrede680992014-04-10 17:23:49 -0700875 update_title(terminal);
876}
877
878static void
879state_changed_handler(struct window *window, void *data)
880{
881 struct terminal *terminal = data;
882 update_title(terminal);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500883}
884
885static void
886terminal_resize(struct terminal *terminal, int columns, int rows)
887{
888 int32_t width, height, m;
889
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400890 if (window_is_fullscreen(terminal->window) ||
891 window_is_maximized(terminal->window))
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500892 return;
893
894 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800895 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500896 height = rows * terminal->extents.height + m;
Kristian Høgsberga1627922012-06-20 17:30:03 -0400897
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500898 window_frame_set_child_size(terminal->widget, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500899}
900
Callum Lowcay30eeae52011-01-07 19:46:55 +0000901struct color_scheme DEFAULT_COLORS = {
902 {
903 {0, 0, 0, 1}, /* black */
904 {0.66, 0, 0, 1}, /* red */
905 {0 , 0.66, 0, 1}, /* green */
906 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
907 {0 , 0 , 0.66, 1}, /* blue */
908 {0.66, 0 , 0.66, 1}, /* magenta */
909 {0, 0.66, 0.66, 1}, /* cyan */
910 {0.66, 0.66, 0.66, 1}, /* light grey */
911 {0.22, 0.33, 0.33, 1}, /* dark grey */
912 {1, 0.33, 0.33, 1}, /* high red */
913 {0.33, 1, 0.33, 1}, /* high green */
914 {1, 1, 0.33, 1}, /* high yellow */
915 {0.33, 0.33, 1, 1}, /* high blue */
916 {1, 0.33, 1, 1}, /* high magenta */
917 {0.33, 1, 1, 1}, /* high cyan */
918 {1, 1, 1, 1} /* white */
919 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500920 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000921 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
922};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400923
Kristian Høgsberg22106762008-12-08 13:50:07 -0500924static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500925terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
926{
927 cairo_set_source_rgba(cr,
928 terminal->color_table[index].r,
929 terminal->color_table[index].g,
930 terminal->color_table[index].b,
931 terminal->color_table[index].a);
932}
933
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500934static void
935terminal_send_selection(struct terminal *terminal, int fd)
936{
937 int row, col;
938 union utf8_char *p_row;
939 union decoded_attr attr;
940 FILE *fp;
941 int len;
942
943 fp = fdopen(fd, "w");
Brian Lovin1bf14812013-08-08 16:12:55 -0700944 if (fp == NULL){
945 close(fd);
946 return;
947 }
Manuel Bachmanne54cee12015-09-19 13:51:05 +0200948 for (row = terminal->selection_start_row; row < terminal->height; row++) {
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500949 p_row = terminal_get_row(terminal, row);
950 for (col = 0; col < terminal->width; col++) {
Peng Wucfcc1112013-08-19 10:59:21 +0800951 if (p_row[col].ch == 0x200B) /* space glyph */
952 continue;
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500953 /* get the attributes for this character cell */
954 terminal_decode_attr(terminal, row, col, &attr);
955 if (!attr.attr.s)
956 continue;
957 len = strnlen((char *) p_row[col].byte, 4);
Kristian Høgsberg0dee6472012-07-01 21:25:41 -0400958 if (len > 0)
959 fwrite(p_row[col].byte, 1, len, fp);
960 if (len == 0 || col == terminal->width - 1) {
961 fwrite("\n", 1, 1, fp);
962 break;
963 }
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500964 }
965 }
966 fclose(fp);
967}
968
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500969struct glyph_run {
970 struct terminal *terminal;
971 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400972 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500973 union decoded_attr attr;
974 cairo_glyph_t glyphs[256], *g;
975};
976
977static void
978glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
979{
980 run->terminal = terminal;
981 run->cr = cr;
982 run->g = run->glyphs;
983 run->count = 0;
984 run->attr.key = 0;
985}
986
987static void
988glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
989{
990 cairo_scaled_font_t *font;
991
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500992 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
993 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300994 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500995 font = run->terminal->font_bold;
996 else
997 font = run->terminal->font_normal;
998 cairo_set_scaled_font(run->cr, font);
999 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +13001000 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001001
Callum Lowcay9d708b02011-01-12 20:06:17 +13001002 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
1003 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001004 run->g = run->glyphs;
1005 run->count = 0;
1006 }
Callum Lowcay9d708b02011-01-12 20:06:17 +13001007 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001008}
1009
1010static void
1011glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
1012{
1013 int num_glyphs;
1014 cairo_scaled_font_t *font;
1015
1016 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
1017
Callum Lowcay9d708b02011-01-12 20:06:17 +13001018 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001019 font = run->terminal->font_bold;
1020 else
1021 font = run->terminal->font_normal;
1022
1023 cairo_move_to(run->cr, x, y);
1024 cairo_scaled_font_text_to_glyphs (font, x, y,
1025 (char *) c->byte, 4,
1026 &run->g, &num_glyphs,
1027 NULL, NULL, NULL);
1028 run->g += num_glyphs;
1029 run->count += num_glyphs;
1030}
1031
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001032
Kristian Høgsbergf106fd52011-01-11 10:11:39 -05001033static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001034redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001035{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001036 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001037 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001038 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001039 int top_margin, side_margin;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001040 int row, col, cursor_x, cursor_y;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001041 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001042 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001043 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001044 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001045 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001046 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -05001047 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +08001048 double average_width;
Peng Wucfcc1112013-08-19 10:59:21 +08001049 double unichar_width;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001050
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001051 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001052 widget_get_allocation(terminal->widget, &allocation);
Alexander Larssonde79dd02013-05-22 14:41:34 +02001053 cr = widget_cairo_create(terminal->widget);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001054 cairo_rectangle(cr, allocation.x, allocation.y,
1055 allocation.width, allocation.height);
1056 cairo_clip(cr);
1057 cairo_push_group(cr);
1058
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001059 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -05001060 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001061 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001062
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05001063 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001064
Kristian Høgsberg59826582011-01-20 11:56:57 -05001065 extents = terminal->extents;
Peng Wuf291f202013-06-06 15:32:41 +08001066 average_width = terminal->average_width;
1067 side_margin = (allocation.width - terminal->width * average_width) / 2;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001068 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001069
Callum Lowcay30eeae52011-01-07 19:46:55 +00001070 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001071 cairo_translate(cr, allocation.x + side_margin,
1072 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001073 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001074 for (row = 0; row < terminal->height; row++) {
Peng Wucfcc1112013-08-19 10:59:21 +08001075 p_row = terminal_get_row(terminal, row);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001076 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001077 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -05001078 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001079
Callum Lowcay9d708b02011-01-12 20:06:17 +13001080 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -05001081 continue;
1082
Peng Wucfcc1112013-08-19 10:59:21 +08001083 if (is_wide(p_row[col]))
1084 unichar_width = 2 * average_width;
1085 else
1086 unichar_width = average_width;
1087
Callum Lowcay9d708b02011-01-12 20:06:17 +13001088 terminal_set_color(terminal, cr, attr.attr.bg);
Peng Wuf291f202013-06-06 15:32:41 +08001089 cairo_move_to(cr, col * average_width,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001090 row * extents.height);
Peng Wucfcc1112013-08-19 10:59:21 +08001091 cairo_rel_line_to(cr, unichar_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001092 cairo_rel_line_to(cr, 0, extents.height);
Peng Wucfcc1112013-08-19 10:59:21 +08001093 cairo_rel_line_to(cr, -unichar_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001094 cairo_close_path(cr);
1095 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001096 }
1097 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001098
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001099 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1100
1101 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001102 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001103 for (row = 0; row < terminal->height; row++) {
1104 p_row = terminal_get_row(terminal, row);
1105 for (col = 0; col < terminal->width; col++) {
1106 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -05001107 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001108
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001109 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001110
Peng Wuf291f202013-06-06 15:32:41 +08001111 text_x = col * average_width;
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001112 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +13001113 if (attr.attr.a & ATTRMASK_UNDERLINE) {
1114 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +00001115 cairo_move_to(cr, text_x, (double)text_y + 1.5);
Peng Wuf291f202013-06-06 15:32:41 +08001116 cairo_line_to(cr, text_x + average_width, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001117 cairo_stroke(cr);
1118 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -05001119
Daiki Ueno56d8a7a2014-04-08 18:46:18 +09001120 /* skip space glyph (RLE) we use as a placeholder of
1121 the right half of a double-width character,
1122 because RLE is not available in every font. */
1123 if (p_row[col].ch == 0x200B)
1124 continue;
1125
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001126 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001127 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001128 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001129
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001130 attr.key = ~0;
1131 glyph_run_flush(&run, attr);
1132
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001133 if ((terminal->mode & MODE_SHOW_CURSOR) &&
1134 !window_has_focus(terminal->window)) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001135 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001136
Callum Lowcay30eeae52011-01-07 19:46:55 +00001137 cairo_set_line_width(cr, 1);
Peng Wuf291f202013-06-06 15:32:41 +08001138 cairo_move_to(cr, terminal->column * average_width + d,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001139 terminal->row * extents.height + d);
Peng Wuf291f202013-06-06 15:32:41 +08001140 cairo_rel_line_to(cr, average_width - 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001141 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
Peng Wuf291f202013-06-06 15:32:41 +08001142 cairo_rel_line_to(cr, -average_width + 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001143 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001144
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001145 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001146 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001147
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001148 cairo_pop_group_to_source(cr);
1149 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001150 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001151 cairo_surface_destroy(surface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001152
1153 if (terminal->send_cursor_position) {
1154 cursor_x = side_margin + allocation.x +
Peng Wuf291f202013-06-06 15:32:41 +08001155 terminal->column * average_width;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001156 cursor_y = top_margin + allocation.y +
1157 terminal->row * extents.height;
1158 window_set_text_cursor_position(terminal->window,
1159 cursor_x, cursor_y);
1160 terminal->send_cursor_position = 0;
1161 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001162}
1163
1164static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001165terminal_write(struct terminal *terminal, const char *data, size_t length)
1166{
1167 if (write(terminal->master, data, length) < 0)
1168 abort();
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001169 terminal->send_cursor_position = 1;
Kristian Høgsberg26130862011-08-24 11:30:21 -04001170}
1171
1172static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001173terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001174
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001175static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001176handle_char(struct terminal *terminal, union utf8_char utf8);
1177
1178static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001179handle_sgr(struct terminal *terminal, int code);
1180
1181static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001182handle_term_parameter(struct terminal *terminal, int code, int sr)
1183{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001184 int i;
1185
Callum Lowcay67a201d2011-01-12 19:23:41 +13001186 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001187 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001188 case 1: /* DECCKM */
1189 if (sr) terminal->key_mode = KM_APPLICATION;
1190 else terminal->key_mode = KM_NORMAL;
1191 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001192 case 2: /* DECANM */
1193 /* No VT52 support yet */
1194 terminal->g0 = CS_US;
1195 terminal->g1 = CS_US;
1196 terminal->cs = terminal->g0;
1197 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001198 case 3: /* DECCOLM */
1199 if (sr)
1200 terminal_resize(terminal, 132, 24);
1201 else
1202 terminal_resize(terminal, 80, 24);
Michael Vetter2a18a522015-05-15 17:17:47 +02001203
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001204 /* set columns, but also home cursor and clear screen */
1205 terminal->row = 0; terminal->column = 0;
1206 for (i = 0; i < terminal->height; i++) {
1207 memset(terminal_get_row(terminal, i),
1208 0, terminal->data_pitch);
1209 attr_init(terminal_get_attr_row(terminal, i),
1210 terminal->curr_attr, terminal->width);
1211 }
1212 break;
1213 case 5: /* DECSCNM */
1214 if (sr) terminal->mode |= MODE_INVERSE;
1215 else terminal->mode &= ~MODE_INVERSE;
1216 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001217 case 6: /* DECOM */
1218 terminal->origin_mode = sr;
1219 if (terminal->origin_mode)
1220 terminal->row = terminal->margin_top;
1221 else
1222 terminal->row = 0;
1223 terminal->column = 0;
1224 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001225 case 7: /* DECAWM */
1226 if (sr) terminal->mode |= MODE_AUTOWRAP;
1227 else terminal->mode &= ~MODE_AUTOWRAP;
1228 break;
1229 case 8: /* DECARM */
1230 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1231 else terminal->mode &= ~MODE_AUTOREPEAT;
1232 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001233 case 12: /* Very visible cursor (CVVIS) */
1234 /* FIXME: What do we do here. */
1235 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001236 case 25:
1237 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1238 else terminal->mode &= ~MODE_SHOW_CURSOR;
1239 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001240 case 1034: /* smm/rmm, meta mode on/off */
1241 /* ignore */
1242 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001243 case 1037: /* deleteSendsDel */
1244 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1245 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1246 break;
1247 case 1039: /* altSendsEscape */
1248 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1249 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1250 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001251 case 1049: /* rmcup/smcup, alternate screen */
1252 /* Ignore. Should be possible to implement,
1253 * but it's kind of annoying. */
1254 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001255 default:
1256 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1257 break;
1258 }
1259 } else {
1260 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001261 case 4: /* IRM */
1262 if (sr) terminal->mode |= MODE_IRM;
1263 else terminal->mode &= ~MODE_IRM;
1264 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001265 case 20: /* LNM */
1266 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1267 else terminal->mode &= ~MODE_LF_NEWLINE;
1268 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001269 default:
1270 fprintf(stderr, "Unknown parameter: %d\n", code);
1271 break;
1272 }
1273 }
1274}
1275
1276static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001277handle_dcs(struct terminal *terminal)
1278{
1279}
1280
1281static void
1282handle_osc(struct terminal *terminal)
1283{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001284 char *p;
1285 int code;
1286
1287 terminal->escape[terminal->escape_length++] = '\0';
1288 p = &terminal->escape[2];
1289 code = strtol(p, &p, 10);
1290 if (*p == ';') p++;
1291
1292 switch (code) {
1293 case 0: /* Icon name and window title */
1294 case 1: /* Icon label */
1295 case 2: /* Window title*/
Kristian Høgsberga83be202013-10-23 20:47:35 -07001296 free(terminal->title);
1297 terminal->title = strdup(p);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001298 window_set_title(terminal->window, p);
1299 break;
Kristian Høgsberg8a205002013-09-11 11:52:56 -07001300 case 7: /* shell cwd as uri */
1301 break;
Dima Ryazanovc3b63722016-11-03 23:46:02 -07001302 case 777: /* Desktop notifications */
1303 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001304 default:
Kristian Høgsberg8a205002013-09-11 11:52:56 -07001305 fprintf(stderr, "Unknown OSC escape code %d, text %s\n",
1306 code, p);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001307 break;
1308 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001309}
1310
1311static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001312handle_escape(struct terminal *terminal)
1313{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001314 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001315 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001316 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001317 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001318 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001319 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001320 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001321
1322 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001323 i = 0;
1324 p = &terminal->escape[2];
1325 while ((isdigit(*p) || *p == ';') && i < 10) {
1326 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001327 if (!set[i]) {
1328 args[i] = 0;
1329 set[i] = 1;
1330 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001331 p++;
1332 i++;
1333 } else {
1334 args[i] = strtol(p, &p, 10);
1335 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001336 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001337 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001338
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001339 switch (*p) {
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001340 case '@': /* ICH - Insert <count> blank characters */
Callum Lowcay69e96582011-01-07 19:47:00 +00001341 count = set[0] ? args[0] : 1;
1342 if (count == 0) count = 1;
1343 terminal_shift_line(terminal, count);
1344 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001345 case 'A': /* CUU - Move cursor up <count> rows */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001346 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001347 if (count == 0) count = 1;
1348 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001349 terminal->row -= count;
1350 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001351 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001352 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001353 case 'B': /* CUD - Move cursor down <count> rows */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001354 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001355 if (count == 0) count = 1;
1356 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001357 terminal->row += count;
1358 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001359 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001360 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001361 case 'C': /* CUF - Move cursor right by <count> columns */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001362 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001363 if (count == 0) count = 1;
1364 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001365 terminal->column += count;
1366 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001367 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001368 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001369 case 'D': /* CUB - Move cursor left <count> columns */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001370 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001371 if (count == 0) count = 1;
1372 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001373 terminal->column -= count;
1374 else
1375 terminal->column = 0;
1376 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001377 case 'E': /* CNL - Move cursor down <count> rows, to column 1 */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001378 count = set[0] ? args[0] : 1;
1379 if (terminal->row + count <= terminal->margin_bottom)
1380 terminal->row += count;
1381 else
1382 terminal->row = terminal->margin_bottom;
1383 terminal->column = 0;
1384 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001385 case 'F': /* CPL - Move cursour up <count> rows, to column 1 */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001386 count = set[0] ? args[0] : 1;
1387 if (terminal->row - count >= terminal->margin_top)
1388 terminal->row -= count;
1389 else
1390 terminal->row = terminal->margin_top;
1391 terminal->column = 0;
1392 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001393 case 'G': /* CHA - Move cursor to column <y> in current row */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001394 y = set[0] ? args[0] : 1;
1395 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
Michael Vetter2a18a522015-05-15 17:17:47 +02001396
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001397 terminal->column = y - 1;
1398 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001399 case 'f': /* HVP - Move cursor to <x, y> */
1400 case 'H': /* CUP - Move cursor to <x, y> (origin at 1,1) */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001401 x = (set[1] ? args[1] : 1) - 1;
1402 x = x < 0 ? 0 :
1403 (x >= terminal->width ? terminal->width - 1 : x);
Michael Vetter2a18a522015-05-15 17:17:47 +02001404
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001405 y = (set[0] ? args[0] : 1) - 1;
1406 if (terminal->origin_mode) {
1407 y += terminal->margin_top;
1408 y = y < terminal->margin_top ? terminal->margin_top :
1409 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1410 } else {
1411 y = y < 0 ? 0 :
1412 (y >= terminal->height ? terminal->height - 1 : y);
1413 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001414
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001415 terminal->row = y;
1416 terminal->column = x;
1417 break;
1418 case 'I': /* CHT */
1419 count = set[0] ? args[0] : 1;
1420 if (count == 0) count = 1;
1421 while (count > 0 && terminal->column < terminal->width) {
1422 if (terminal->tab_ruler[terminal->column]) count--;
1423 terminal->column++;
1424 }
1425 terminal->column--;
1426 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001427 case 'J': /* ED - Erase display */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001428 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001429 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001430 if (!set[0] || args[0] == 0 || args[0] > 2) {
1431 memset(&row[terminal->column],
1432 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1433 attr_init(&attr_row[terminal->column],
1434 terminal->curr_attr, terminal->width - terminal->column);
1435 for (i = terminal->row + 1; i < terminal->height; i++) {
1436 memset(terminal_get_row(terminal, i),
1437 0, terminal->data_pitch);
1438 attr_init(terminal_get_attr_row(terminal, i),
1439 terminal->curr_attr, terminal->width);
1440 }
1441 } else if (args[0] == 1) {
1442 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1443 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1444 for (i = 0; i < terminal->row; i++) {
1445 memset(terminal_get_row(terminal, i),
1446 0, terminal->data_pitch);
1447 attr_init(terminal_get_attr_row(terminal, i),
1448 terminal->curr_attr, terminal->width);
1449 }
1450 } else if (args[0] == 2) {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07001451 /* Clear screen by scrolling contents out */
1452 terminal_scroll_buffer(terminal,
1453 terminal->end - terminal->start);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001454 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001455 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001456 case 'K': /* EL - Erase line */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001457 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001458 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001459 if (!set[0] || args[0] == 0 || args[0] > 2) {
1460 memset(&row[terminal->column], 0,
1461 (terminal->width - terminal->column) * sizeof(union utf8_char));
1462 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1463 terminal->width - terminal->column);
1464 } else if (args[0] == 1) {
1465 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1466 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1467 } else if (args[0] == 2) {
1468 memset(row, 0, terminal->data_pitch);
1469 attr_init(attr_row, terminal->curr_attr, terminal->width);
1470 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001471 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001472 case 'L': /* IL - Insert <count> blank lines */
Callum Lowcay69e96582011-01-07 19:47:00 +00001473 count = set[0] ? args[0] : 1;
1474 if (count == 0) count = 1;
1475 if (terminal->row >= terminal->margin_top &&
1476 terminal->row < terminal->margin_bottom)
1477 {
1478 top = terminal->margin_top;
1479 terminal->margin_top = terminal->row;
1480 terminal_scroll(terminal, 0 - count);
1481 terminal->margin_top = top;
1482 } else if (terminal->row == terminal->margin_bottom) {
1483 memset(terminal_get_row(terminal, terminal->row),
1484 0, terminal->data_pitch);
1485 attr_init(terminal_get_attr_row(terminal, terminal->row),
1486 terminal->curr_attr, terminal->width);
1487 }
1488 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001489 case 'M': /* DL - Delete <count> lines */
Callum Lowcay69e96582011-01-07 19:47:00 +00001490 count = set[0] ? args[0] : 1;
1491 if (count == 0) count = 1;
1492 if (terminal->row >= terminal->margin_top &&
1493 terminal->row < terminal->margin_bottom)
1494 {
1495 top = terminal->margin_top;
1496 terminal->margin_top = terminal->row;
1497 terminal_scroll(terminal, count);
1498 terminal->margin_top = top;
1499 } else if (terminal->row == terminal->margin_bottom) {
1500 memset(terminal_get_row(terminal, terminal->row),
1501 0, terminal->data_pitch);
1502 }
1503 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001504 case 'P': /* DCH - Delete <count> characters on current line */
Callum Lowcay69e96582011-01-07 19:47:00 +00001505 count = set[0] ? args[0] : 1;
1506 if (count == 0) count = 1;
1507 terminal_shift_line(terminal, 0 - count);
1508 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001509 case 'S': /* SU */
1510 terminal_scroll(terminal, set[0] ? args[0] : 1);
1511 break;
1512 case 'T': /* SD */
1513 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1514 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001515 case 'X': /* ECH - Erase <count> characters on current line */
Callum Lowcay69e96582011-01-07 19:47:00 +00001516 count = set[0] ? args[0] : 1;
1517 if (count == 0) count = 1;
1518 if ((terminal->column + count) > terminal->width)
1519 count = terminal->width - terminal->column;
1520 row = terminal_get_row(terminal, terminal->row);
1521 attr_row = terminal_get_attr_row(terminal, terminal->row);
1522 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1523 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1524 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001525 case 'Z': /* CBT */
1526 count = set[0] ? args[0] : 1;
1527 if (count == 0) count = 1;
1528 while (count > 0 && terminal->column >= 0) {
1529 if (terminal->tab_ruler[terminal->column]) count--;
1530 terminal->column--;
1531 }
1532 terminal->column++;
1533 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001534 case '`': /* HPA - Move cursor to <y> column in current row */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001535 y = set[0] ? args[0] : 1;
1536 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
Michael Vetter2a18a522015-05-15 17:17:47 +02001537
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001538 terminal->column = y - 1;
1539 break;
1540 case 'b': /* REP */
1541 count = set[0] ? args[0] : 1;
1542 if (count == 0) count = 1;
1543 if (terminal->last_char.byte[0])
1544 for (i = 0; i < count; i++)
1545 handle_char(terminal, terminal->last_char);
1546 terminal->last_char.byte[0] = 0;
1547 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001548 case 'c': /* Primary DA - Answer "I am a VT102" */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001549 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001550 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001551 case 'd': /* VPA - Move cursor to <x> row, current column */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001552 x = set[0] ? args[0] : 1;
1553 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
Michael Vetter2a18a522015-05-15 17:17:47 +02001554
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001555 terminal->row = x - 1;
1556 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001557 case 'g': /* TBC - Clear tab stop(s) */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001558 if (!set[0] || args[0] == 0) {
1559 terminal->tab_ruler[terminal->column] = 0;
1560 } else if (args[0] == 3) {
1561 memset(terminal->tab_ruler, 0, terminal->width);
1562 }
1563 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001564 case 'h': /* SM - Set mode */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001565 for (i = 0; i < 10 && set[i]; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001566 handle_term_parameter(terminal, args[i], 1);
1567 }
1568 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001569 case 'l': /* RM - Reset mode */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001570 for (i = 0; i < 10 && set[i]; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001571 handle_term_parameter(terminal, args[i], 0);
1572 }
1573 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001574 case 'm': /* SGR - Set attributes */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001575 for (i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001576 if (i <= 7 && set[i] && set[i + 1] &&
1577 set[i + 2] && args[i + 1] == 5)
1578 {
1579 if (args[i] == 38) {
1580 handle_sgr(terminal, args[i + 2] + 256);
1581 break;
1582 } else if (args[i] == 48) {
1583 handle_sgr(terminal, args[i + 2] + 512);
1584 break;
1585 }
1586 }
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001587 if (set[i]) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001588 handle_sgr(terminal, args[i]);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001589 } else if (i == 0) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001590 handle_sgr(terminal, 0);
1591 break;
1592 } else {
1593 break;
1594 }
1595 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001596 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001597 case 'n': /* DSR - Status report */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001598 i = set[0] ? args[0] : 0;
1599 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001600 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001601 } else if (i == 6) {
1602 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1603 terminal->origin_mode ?
1604 terminal->row+terminal->margin_top : terminal->row+1,
1605 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001606 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001607 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001608 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001609 case 'r': /* DECSTBM - Set scrolling region */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001610 if (!set[0]) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001611 terminal->margin_top = 0;
1612 terminal->margin_bottom = terminal->height-1;
1613 terminal->row = 0;
1614 terminal->column = 0;
1615 } else {
1616 top = (set[0] ? args[0] : 1) - 1;
1617 top = top < 0 ? 0 :
1618 (top >= terminal->height ? terminal->height - 1 : top);
1619 bottom = (set[1] ? args[1] : 1) - 1;
1620 bottom = bottom < 0 ? 0 :
1621 (bottom >= terminal->height ? terminal->height - 1 : bottom);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001622 if (bottom > top) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001623 terminal->margin_top = top;
1624 terminal->margin_bottom = bottom;
1625 } else {
1626 terminal->margin_top = 0;
1627 terminal->margin_bottom = terminal->height-1;
1628 }
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001629 if (terminal->origin_mode)
Callum Lowcaybbeac602011-01-07 19:46:58 +00001630 terminal->row = terminal->margin_top;
1631 else
1632 terminal->row = 0;
1633 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001634 }
1635 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001636 case 's': /* Save cursor location */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001637 terminal->saved_row = terminal->row;
1638 terminal->saved_column = terminal->column;
1639 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001640 case 't': /* windowOps */
1641 if (!set[0]) break;
1642 switch (args[0]) {
1643 case 4: /* resize px */
1644 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001645 widget_schedule_resize(terminal->widget,
1646 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001647 }
1648 break;
1649 case 8: /* resize ch */
1650 if (set[1] && set[2]) {
1651 terminal_resize(terminal, args[2], args[1]);
1652 }
1653 break;
1654 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001655 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001656 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1657 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001658 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001659 break;
1660 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001661 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001662 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1663 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001664 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001665 break;
1666 case 18: /* report ch */
1667 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1668 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001669 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001670 break;
1671 case 21: /* report title */
1672 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1673 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001674 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001675 break;
1676 default:
1677 if (args[0] >= 24)
1678 terminal_resize(terminal, terminal->width, args[0]);
1679 else
1680 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1681 break;
1682 }
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001683 case 'u': /* Restore cursor location */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001684 terminal->row = terminal->saved_row;
1685 terminal->column = terminal->saved_column;
1686 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001687 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001688 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001689 break;
Michael Vetter2a18a522015-05-15 17:17:47 +02001690 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001691}
1692
1693static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001694handle_non_csi_escape(struct terminal *terminal, char code)
1695{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001696 switch(code) {
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001697 case 'M': /* RI - Reverse linefeed */
Callum Lowcaybbeac602011-01-07 19:46:58 +00001698 terminal->row -= 1;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001699 if (terminal->row < terminal->margin_top) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001700 terminal->row = terminal->margin_top;
1701 terminal_scroll(terminal, -1);
1702 }
1703 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001704 case 'E': /* NEL - Newline */
Callum Lowcaybbeac602011-01-07 19:46:58 +00001705 terminal->column = 0;
1706 // fallthrough
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001707 case 'D': /* IND - Linefeed */
Callum Lowcaybbeac602011-01-07 19:46:58 +00001708 terminal->row += 1;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001709 if (terminal->row > terminal->margin_bottom) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001710 terminal->row = terminal->margin_bottom;
1711 terminal_scroll(terminal, +1);
1712 }
1713 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001714 case 'c': /* RIS - Reset*/
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001715 terminal_init(terminal);
1716 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001717 case 'H': /* HTS - Set tab stop at current column */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001718 terminal->tab_ruler[terminal->column] = 1;
1719 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001720 case '7': /* DECSC - Save current state */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001721 terminal->saved_row = terminal->row;
1722 terminal->saved_column = terminal->column;
1723 terminal->saved_attr = terminal->curr_attr;
1724 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001725 terminal->saved_cs = terminal->cs;
1726 terminal->saved_g0 = terminal->g0;
1727 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001728 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001729 case '8': /* DECRC - Restore state most recently saved by ESC 7 */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001730 terminal->row = terminal->saved_row;
1731 terminal->column = terminal->saved_column;
1732 terminal->curr_attr = terminal->saved_attr;
1733 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001734 terminal->cs = terminal->saved_cs;
1735 terminal->g0 = terminal->saved_g0;
1736 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001737 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001738 case '=': /* DECPAM - Set application keypad mode */
Callum Lowcay7e08e902011-01-07 19:47:02 +00001739 terminal->key_mode = KM_APPLICATION;
1740 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001741 case '>': /* DECPNM - Set numeric keypad mode */
Callum Lowcay7e08e902011-01-07 19:47:02 +00001742 terminal->key_mode = KM_NORMAL;
1743 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001744 default:
1745 fprintf(stderr, "Unknown escape code: %c\n", code);
1746 break;
1747 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001748}
1749
1750static void
1751handle_special_escape(struct terminal *terminal, char special, char code)
1752{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001753 int i, numChars;
1754
1755 if (special == '#') {
1756 switch(code) {
1757 case '8':
1758 /* fill with 'E', no cheap way to do this */
1759 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1760 numChars = terminal->width * terminal->height;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001761 for (i = 0; i < numChars; i++) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001762 terminal->data[i].byte[0] = 'E';
1763 }
1764 break;
1765 default:
1766 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1767 break;
1768 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001769 } else if (special == '(' || special == ')') {
1770 switch(code) {
1771 case '0':
1772 if (special == '(')
1773 terminal->g0 = CS_SPECIAL;
1774 else
1775 terminal->g1 = CS_SPECIAL;
1776 break;
1777 case 'A':
1778 if (special == '(')
1779 terminal->g0 = CS_UK;
1780 else
1781 terminal->g1 = CS_UK;
1782 break;
1783 case 'B':
1784 if (special == '(')
1785 terminal->g0 = CS_US;
1786 else
1787 terminal->g1 = CS_US;
1788 break;
1789 default:
1790 fprintf(stderr, "Unknown character set %c\n", code);
1791 break;
1792 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001793 } else {
1794 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1795 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001796}
1797
1798static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001799handle_sgr(struct terminal *terminal, int code)
1800{
1801 switch(code) {
1802 case 0:
1803 terminal->curr_attr = terminal->color_scheme->default_attr;
1804 break;
1805 case 1:
1806 terminal->curr_attr.a |= ATTRMASK_BOLD;
1807 if (terminal->curr_attr.fg < 8)
1808 terminal->curr_attr.fg += 8;
1809 break;
1810 case 4:
1811 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1812 break;
1813 case 5:
1814 terminal->curr_attr.a |= ATTRMASK_BLINK;
1815 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001816 case 8:
1817 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1818 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001819 case 2:
1820 case 21:
1821 case 22:
1822 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1823 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1824 terminal->curr_attr.fg -= 8;
1825 break;
1826 case 24:
1827 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1828 break;
1829 case 25:
1830 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1831 break;
1832 case 7:
1833 case 26:
1834 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1835 break;
1836 case 27:
1837 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1838 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001839 case 28:
1840 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1841 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001842 case 39:
1843 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1844 break;
1845 case 49:
1846 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1847 break;
1848 default:
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001849 if (code >= 30 && code <= 37) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001850 terminal->curr_attr.fg = code - 30;
1851 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1852 terminal->curr_attr.fg += 8;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001853 } else if (code >= 40 && code <= 47) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001854 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001855 } else if (code >= 90 && code <= 97) {
1856 terminal->curr_attr.fg = code - 90 + 8;
1857 } else if (code >= 100 && code <= 107) {
1858 terminal->curr_attr.bg = code - 100 + 8;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001859 } else if (code >= 256 && code < 512) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001860 terminal->curr_attr.fg = code - 256;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001861 } else if (code >= 512 && code < 768) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001862 terminal->curr_attr.bg = code - 512;
1863 } else {
1864 fprintf(stderr, "Unknown SGR code: %d\n", code);
1865 }
1866 break;
1867 }
1868}
1869
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001870/* Returns 1 if c was special, otherwise 0 */
1871static int
1872handle_special_char(struct terminal *terminal, char c)
1873{
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001874 union utf8_char *row;
1875 struct attr *attr_row;
1876
1877 row = terminal_get_row(terminal, terminal->row);
1878 attr_row = terminal_get_attr_row(terminal, terminal->row);
1879
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001880 switch(c) {
1881 case '\r':
1882 terminal->column = 0;
1883 break;
1884 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001885 if (terminal->mode & MODE_LF_NEWLINE) {
1886 terminal->column = 0;
1887 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001888 /* fallthrough */
1889 case '\v':
1890 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001891 terminal->row++;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07001892 if (terminal->row > terminal->margin_bottom) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001893 terminal->row = terminal->margin_bottom;
1894 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001895 }
1896
1897 break;
1898 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001899 while (terminal->column < terminal->width) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001900 if (terminal->mode & MODE_IRM)
1901 terminal_shift_line(terminal, +1);
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001902
1903 if (row[terminal->column].byte[0] == '\0') {
1904 row[terminal->column].byte[0] = ' ';
1905 row[terminal->column].byte[1] = '\0';
1906 attr_row[terminal->column] = terminal->curr_attr;
1907 }
1908
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001909 terminal->column++;
Kristian Høgsbergcca3c2f2012-06-20 15:56:13 -04001910 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001911 }
1912 if (terminal->column >= terminal->width) {
1913 terminal->column = terminal->width - 1;
1914 }
1915
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001916 break;
1917 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001918 if (terminal->column >= terminal->width) {
1919 terminal->column = terminal->width - 2;
1920 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001921 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001922 } else if (terminal->mode & MODE_AUTOWRAP) {
1923 terminal->column = terminal->width - 1;
1924 terminal->row -= 1;
1925 if (terminal->row < terminal->margin_top) {
1926 terminal->row = terminal->margin_top;
1927 terminal_scroll(terminal, -1);
1928 }
1929 }
1930
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001931 break;
1932 case '\a':
1933 /* Bell */
1934 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001935 case '\x0E': /* SO */
1936 terminal->cs = terminal->g1;
1937 break;
1938 case '\x0F': /* SI */
1939 terminal->cs = terminal->g0;
1940 break;
Kristian Høgsberg2a1aa4e2012-08-03 09:37:05 -04001941 case '\0':
1942 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001943 default:
1944 return 0;
1945 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001946
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001947 return 1;
1948}
1949
1950static void
1951handle_char(struct terminal *terminal, union utf8_char utf8)
1952{
1953 union utf8_char *row;
1954 struct attr *attr_row;
Michael Vetter2a18a522015-05-15 17:17:47 +02001955
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001956 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001957
1958 apply_char_set(terminal->cs, &utf8);
Michael Vetter2a18a522015-05-15 17:17:47 +02001959
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001960 /* There are a whole lot of non-characters, control codes,
1961 * and formatting codes that should probably be ignored,
1962 * for example: */
1963 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1964 /* BOM, ignore */
1965 return;
Michael Vetter2a18a522015-05-15 17:17:47 +02001966 }
1967
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001968 /* Some of these non-characters should be translated, e.g.: */
1969 if (utf8.byte[0] < 32) {
1970 utf8.byte[0] = utf8.byte[0] + 64;
1971 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001972
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001973 /* handle right margin effects */
1974 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001975 if (terminal->mode & MODE_AUTOWRAP) {
1976 terminal->column = 0;
1977 terminal->row += 1;
1978 if (terminal->row > terminal->margin_bottom) {
1979 terminal->row = terminal->margin_bottom;
1980 terminal_scroll(terminal, +1);
1981 }
1982 } else {
1983 terminal->column--;
1984 }
1985 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001986
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001987 row = terminal_get_row(terminal, terminal->row);
1988 attr_row = terminal_get_attr_row(terminal, terminal->row);
Michael Vetter2a18a522015-05-15 17:17:47 +02001989
Callum Lowcay69e96582011-01-07 19:47:00 +00001990 if (terminal->mode & MODE_IRM)
1991 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001992 row[terminal->column] = utf8;
1993 attr_row[terminal->column++] = terminal->curr_attr;
1994
Kristian Høgsberg1d781ee2013-11-24 16:54:12 -08001995 if (terminal->row + terminal->start + 1 > terminal->end)
1996 terminal->end = terminal->row + terminal->start + 1;
1997 if (terminal->end == terminal->buffer_height)
1998 terminal->log_size = terminal->buffer_height;
1999 else if (terminal->log_size < terminal->buffer_height)
2000 terminal->log_size = terminal->end;
2001
Peng Wucfcc1112013-08-19 10:59:21 +08002002 /* cursor jump for wide character. */
2003 if (is_wide(utf8))
2004 row[terminal->column++].ch = 0x200B; /* space glyph */
2005
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002006 if (utf8.ch != terminal->last_char.ch)
2007 terminal->last_char = utf8;
2008}
2009
Callum Lowcay30eeae52011-01-07 19:46:55 +00002010static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13002011escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
2012{
2013 int len, i;
2014
2015 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
2016 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
2017 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
2018 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
2019 else len = 1; /* Invalid, cannot happen */
2020
2021 if (terminal->escape_length + len <= MAX_ESCAPE) {
2022 for (i = 0; i < len; i++)
2023 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
2024 terminal->escape_length += len;
2025 } else if (terminal->escape_length < MAX_ESCAPE) {
2026 terminal->escape[terminal->escape_length++] = 0;
2027 }
2028}
2029
2030static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002031terminal_data(struct terminal *terminal, const char *data, size_t length)
2032{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002033 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002034 union utf8_char utf8;
2035 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002036
2037 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002038 parser_state =
2039 utf8_next_char(&terminal->state_machine, data[i]);
2040 switch(parser_state) {
2041 case utf8state_accept:
2042 utf8.ch = terminal->state_machine.s.ch;
2043 break;
2044 case utf8state_reject:
2045 /* the unicode replacement character */
2046 utf8.byte[0] = 0xEF;
2047 utf8.byte[1] = 0xBF;
2048 utf8.byte[2] = 0xBD;
2049 utf8.byte[3] = 0x00;
2050 break;
2051 default:
2052 continue;
2053 }
2054
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002055 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13002056 switch (terminal->state) {
2057 case escape_state_escape:
2058 escape_append_utf8(terminal, utf8);
2059 switch (utf8.byte[0]) {
2060 case 'P': /* DCS */
2061 terminal->state = escape_state_dcs;
2062 break;
2063 case '[': /* CSI */
2064 terminal->state = escape_state_csi;
2065 break;
2066 case ']': /* OSC */
2067 terminal->state = escape_state_osc;
2068 break;
2069 case '#':
2070 case '(':
2071 case ')': /* special */
2072 terminal->state = escape_state_special;
2073 break;
2074 case '^': /* PM (not implemented) */
2075 case '_': /* APC (not implemented) */
2076 terminal->state = escape_state_ignore;
2077 break;
2078 default:
2079 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002080 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13002081 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002082 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002083 continue;
2084 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002085 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
2086 /* do nothing */
2087 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002088 terminal->escape_flags |= ESC_FLAG_WHAT;
2089 } else if (utf8.byte[0] == '>') {
2090 terminal->escape_flags |= ESC_FLAG_GT;
2091 } else if (utf8.byte[0] == '!') {
2092 terminal->escape_flags |= ESC_FLAG_BANG;
2093 } else if (utf8.byte[0] == '$') {
2094 terminal->escape_flags |= ESC_FLAG_CASH;
2095 } else if (utf8.byte[0] == '\'') {
2096 terminal->escape_flags |= ESC_FLAG_SQUOTE;
2097 } else if (utf8.byte[0] == '"') {
2098 terminal->escape_flags |= ESC_FLAG_DQUOTE;
2099 } else if (utf8.byte[0] == ' ') {
2100 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002101 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002102 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002103 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13002104 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002105 }
Michael Vetter2a18a522015-05-15 17:17:47 +02002106
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002107 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
2108 utf8.byte[0] == '`')
2109 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002110 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002111 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002112 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002113 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002114 continue;
2115 case escape_state_inner_escape:
2116 if (utf8.byte[0] == '\\') {
2117 terminal->state = escape_state_normal;
2118 if (terminal->outer_state == escape_state_dcs) {
2119 handle_dcs(terminal);
2120 } else if (terminal->outer_state == escape_state_osc) {
2121 handle_osc(terminal);
2122 }
2123 } else if (utf8.byte[0] == '\e') {
2124 terminal->state = terminal->outer_state;
2125 escape_append_utf8(terminal, utf8);
2126 if (terminal->escape_length >= MAX_ESCAPE)
2127 terminal->state = escape_state_normal;
2128 } else {
2129 terminal->state = terminal->outer_state;
2130 if (terminal->escape_length < MAX_ESCAPE)
2131 terminal->escape[terminal->escape_length++] = '\e';
2132 escape_append_utf8(terminal, utf8);
2133 if (terminal->escape_length >= MAX_ESCAPE)
2134 terminal->state = escape_state_normal;
2135 }
2136 continue;
2137 case escape_state_dcs:
2138 case escape_state_osc:
2139 case escape_state_ignore:
2140 if (utf8.byte[0] == '\e') {
2141 terminal->outer_state = terminal->state;
2142 terminal->state = escape_state_inner_escape;
2143 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
2144 terminal->state = escape_state_normal;
2145 handle_osc(terminal);
2146 } else {
2147 escape_append_utf8(terminal, utf8);
2148 if (terminal->escape_length >= MAX_ESCAPE)
2149 terminal->state = escape_state_normal;
2150 }
2151 continue;
2152 case escape_state_special:
2153 escape_append_utf8(terminal, utf8);
2154 terminal->state = escape_state_normal;
2155 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2156 handle_special_escape(terminal, terminal->escape[1],
2157 utf8.byte[0]);
2158 }
2159 continue;
2160 default:
2161 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002162 }
2163
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002164 /* this is valid, because ASCII characters are never used to
2165 * introduce a multibyte sequence in UTF-8 */
2166 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002167 terminal->state = escape_state_escape;
2168 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002169 terminal->escape[0] = '\e';
2170 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002171 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002172 } else {
2173 handle_char(terminal, utf8);
2174 } /* if */
2175 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002176
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002177 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002178}
2179
2180static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002181data_source_target(void *data,
2182 struct wl_data_source *source, const char *mime_type)
2183{
2184 fprintf(stderr, "data_source_target, %s\n", mime_type);
2185}
2186
2187static void
2188data_source_send(void *data,
2189 struct wl_data_source *source,
2190 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002191{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002192 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002193
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002194 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002195}
2196
2197static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002198data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002199{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002200 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002201}
2202
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002203static const struct wl_data_source_listener data_source_listener = {
2204 data_source_target,
2205 data_source_send,
2206 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002207};
2208
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07002209static const char text_mime_type[] = "text/plain;charset=utf-8";
2210
2211static void
2212data_handler(struct window *window,
2213 struct input *input,
2214 float x, float y, const char **types, void *data)
2215{
2216 int i, has_text = 0;
2217
2218 if (!types)
2219 return;
2220 for (i = 0; types[i]; i++)
2221 if (strcmp(types[i], text_mime_type) == 0)
2222 has_text = 1;
2223
2224 if (!has_text) {
2225 input_accept(input, NULL);
2226 } else {
2227 input_accept(input, text_mime_type);
2228 }
2229}
2230
2231static void
2232drop_handler(struct window *window, struct input *input,
2233 int32_t x, int32_t y, void *data)
2234{
2235 struct terminal *terminal = data;
2236
2237 input_receive_drag_data_to_fd(input, text_mime_type, terminal->master);
2238}
2239
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002240static void
2241fullscreen_handler(struct window *window, void *data)
2242{
2243 struct terminal *terminal = data;
2244
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002245 window_set_fullscreen(window, !window_is_fullscreen(terminal->window));
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002246}
2247
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002248static void
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002249close_handler(void *data)
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002250{
2251 struct terminal *terminal = data;
2252
2253 terminal_destroy(terminal);
2254}
2255
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002256static void
2257terminal_copy(struct terminal *terminal, struct input *input)
2258{
2259 terminal->selection =
2260 display_create_data_source(terminal->display);
2261 wl_data_source_offer(terminal->selection,
2262 "text/plain;charset=utf-8");
2263 wl_data_source_add_listener(terminal->selection,
2264 &data_source_listener, terminal);
2265 input_set_selection(input, terminal->selection,
2266 display_get_serial(terminal->display));
2267}
2268
2269static void
2270terminal_paste(struct terminal *terminal, struct input *input)
2271{
2272 input_receive_selection_data_to_fd(input,
2273 "text/plain;charset=utf-8",
2274 terminal->master);
2275
2276}
2277
2278static void
2279terminal_new_instance(struct terminal *terminal)
2280{
2281 struct terminal *new_terminal;
2282
2283 new_terminal = terminal_create(terminal->display);
2284 if (terminal_run(new_terminal, option_shell))
2285 terminal_destroy(new_terminal);
2286}
2287
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002288static int
2289handle_bound_key(struct terminal *terminal,
2290 struct input *input, uint32_t sym, uint32_t time)
2291{
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002292 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002293 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002294 /* Cut selection; terminal doesn't do cut, fall
2295 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002296 case XKB_KEY_C:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002297 terminal_copy(terminal, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002298 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002299 case XKB_KEY_V:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002300 terminal_paste(terminal, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002301 return 1;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002302 case XKB_KEY_N:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002303 terminal_new_instance(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002304 return 1;
2305
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002306 case XKB_KEY_Up:
2307 if (!terminal->scrolling)
2308 terminal->saved_start = terminal->start;
2309 if (terminal->start == terminal->end - terminal->log_size)
2310 return 1;
2311
2312 terminal->scrolling = 1;
2313 terminal->start--;
2314 terminal->row++;
2315 terminal->selection_start_row++;
2316 terminal->selection_end_row++;
2317 widget_schedule_redraw(terminal->widget);
2318 return 1;
2319
2320 case XKB_KEY_Down:
2321 if (!terminal->scrolling)
2322 terminal->saved_start = terminal->start;
2323
2324 if (terminal->start == terminal->saved_start)
2325 return 1;
2326
2327 terminal->scrolling = 1;
2328 terminal->start++;
2329 terminal->row--;
2330 terminal->selection_start_row--;
2331 terminal->selection_end_row--;
2332 widget_schedule_redraw(terminal->widget);
2333 return 1;
2334
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002335 default:
2336 return 0;
2337 }
2338}
2339
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002340static void
2341key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002342 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2343 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002344{
2345 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002346 char ch[MAX_RESPONSE];
Kristian Høgsberg88fd4082012-06-21 15:55:03 -04002347 uint32_t modifiers, serial;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002348 int ret, len = 0, d;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002349 bool convert_utf8 = true;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002350
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002351 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002352 if ((modifiers & MOD_CONTROL_MASK) &&
2353 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002354 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2355 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002356 return;
2357
Daniel Stonea8468712012-11-07 17:51:35 +11002358 /* Map keypad symbols to 'normal' equivalents before processing */
2359 switch (sym) {
2360 case XKB_KEY_KP_Space:
2361 sym = XKB_KEY_space;
2362 break;
2363 case XKB_KEY_KP_Tab:
2364 sym = XKB_KEY_Tab;
2365 break;
2366 case XKB_KEY_KP_Enter:
2367 sym = XKB_KEY_Return;
2368 break;
2369 case XKB_KEY_KP_Left:
2370 sym = XKB_KEY_Left;
2371 break;
2372 case XKB_KEY_KP_Up:
2373 sym = XKB_KEY_Up;
2374 break;
2375 case XKB_KEY_KP_Right:
2376 sym = XKB_KEY_Right;
2377 break;
2378 case XKB_KEY_KP_Down:
2379 sym = XKB_KEY_Down;
2380 break;
2381 case XKB_KEY_KP_Equal:
2382 sym = XKB_KEY_equal;
2383 break;
2384 case XKB_KEY_KP_Multiply:
2385 sym = XKB_KEY_asterisk;
2386 break;
2387 case XKB_KEY_KP_Add:
2388 sym = XKB_KEY_plus;
2389 break;
2390 case XKB_KEY_KP_Separator:
2391 /* Note this is actually locale-dependent and should mostly be
2392 * a comma. But leave it as period until we one day start
2393 * doing the right thing. */
2394 sym = XKB_KEY_period;
2395 break;
2396 case XKB_KEY_KP_Subtract:
2397 sym = XKB_KEY_minus;
2398 break;
2399 case XKB_KEY_KP_Decimal:
2400 sym = XKB_KEY_period;
2401 break;
2402 case XKB_KEY_KP_Divide:
2403 sym = XKB_KEY_slash;
2404 break;
2405 case XKB_KEY_KP_0:
2406 case XKB_KEY_KP_1:
2407 case XKB_KEY_KP_2:
2408 case XKB_KEY_KP_3:
2409 case XKB_KEY_KP_4:
2410 case XKB_KEY_KP_5:
2411 case XKB_KEY_KP_6:
2412 case XKB_KEY_KP_7:
2413 case XKB_KEY_KP_8:
2414 case XKB_KEY_KP_9:
2415 sym = (sym - XKB_KEY_KP_0) + XKB_KEY_0;
2416 break;
2417 default:
2418 break;
2419 }
2420
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002421 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002422 case XKB_KEY_BackSpace:
Kristian Høgsbergb7f94bf2012-06-21 12:29:36 -04002423 if (modifiers & MOD_ALT_MASK)
2424 ch[len++] = 0x1b;
Kristian Høgsberg71a4cf42012-06-20 17:57:56 -04002425 ch[len++] = 0x7f;
2426 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002427 case XKB_KEY_Tab:
2428 case XKB_KEY_Linefeed:
2429 case XKB_KEY_Clear:
2430 case XKB_KEY_Pause:
2431 case XKB_KEY_Scroll_Lock:
2432 case XKB_KEY_Sys_Req:
2433 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002434 ch[len++] = sym & 0x7f;
2435 break;
2436
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002437 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002438 if (terminal->mode & MODE_LF_NEWLINE) {
2439 ch[len++] = 0x0D;
2440 ch[len++] = 0x0A;
2441 } else {
2442 ch[len++] = 0x0D;
2443 }
2444 break;
2445
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002446 case XKB_KEY_Shift_L:
2447 case XKB_KEY_Shift_R:
2448 case XKB_KEY_Control_L:
2449 case XKB_KEY_Control_R:
2450 case XKB_KEY_Alt_L:
2451 case XKB_KEY_Alt_R:
Kristian Høgsberg22fbcf72012-06-22 12:18:56 -04002452 case XKB_KEY_Meta_L:
2453 case XKB_KEY_Meta_R:
2454 case XKB_KEY_Super_L:
2455 case XKB_KEY_Super_R:
2456 case XKB_KEY_Hyper_L:
2457 case XKB_KEY_Hyper_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002458 break;
2459
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002460 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002461 len = function_key_response('[', 2, modifiers, '~', ch);
2462 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002463 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002464 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2465 ch[len++] = '\x04';
2466 } else {
2467 len = function_key_response('[', 3, modifiers, '~', ch);
2468 }
2469 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002470 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002471 len = function_key_response('[', 5, modifiers, '~', ch);
2472 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002473 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002474 len = function_key_response('[', 6, modifiers, '~', ch);
2475 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002476 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002477 len = function_key_response('O', 1, modifiers, 'P', ch);
2478 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002479 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002480 len = function_key_response('O', 1, modifiers, 'Q', ch);
2481 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002482 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002483 len = function_key_response('O', 1, modifiers, 'R', ch);
2484 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002485 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002486 len = function_key_response('O', 1, modifiers, 'S', ch);
2487 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002488 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002489 len = function_key_response('[', 15, modifiers, '~', ch);
2490 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002491 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002492 len = function_key_response('[', 17, modifiers, '~', ch);
2493 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002494 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002495 len = function_key_response('[', 18, modifiers, '~', ch);
2496 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002497 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002498 len = function_key_response('[', 19, modifiers, '~', ch);
2499 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002500 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002501 len = function_key_response('[', 20, modifiers, '~', ch);
2502 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002503 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002504 len = function_key_response('[', 21, modifiers, '~', ch);
2505 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002506 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002507 len = function_key_response('[', 24, modifiers, '~', ch);
2508 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002509 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002510 /* Handle special keys with alternate mappings */
2511 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2512 if (len != 0) break;
Michael Vetter2a18a522015-05-15 17:17:47 +02002513
Kristian Høgsberg70163132012-05-08 15:55:39 -04002514 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002515 if (sym >= '3' && sym <= '7')
2516 sym = (sym & 0x1f) + 8;
2517
2518 if (!((sym >= '!' && sym <= '/') ||
2519 (sym >= '8' && sym <= '?') ||
2520 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2521 else if (sym == '2') sym = 0x00;
2522 else if (sym == '/') sym = 0x1F;
2523 else if (sym == '8' || sym == '?') sym = 0x7F;
Kristian Høgsbergae9e0732012-06-21 12:30:15 -04002524 }
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002525 if (modifiers & MOD_ALT_MASK) {
2526 if (terminal->mode & MODE_ALT_SENDS_ESC) {
2527 ch[len++] = 0x1b;
2528 } else {
2529 sym = sym | 0x80;
2530 convert_utf8 = false;
2531 }
Callum Lowcay7e08e902011-01-07 19:47:02 +00002532 }
2533
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002534 if ((sym < 128) ||
2535 (!convert_utf8 && sym < 256)) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002536 ch[len++] = sym;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002537 } else {
2538 ret = xkb_keysym_to_utf8(sym, ch + len,
2539 MAX_RESPONSE - len);
2540 if (ret < 0)
2541 fprintf(stderr,
2542 "Warning: buffer too small to encode "
2543 "UTF8 character\n");
2544 else
2545 len += ret;
2546 }
2547
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002548 break;
2549 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002550
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002551 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002552 if (terminal->scrolling) {
2553 d = terminal->saved_start - terminal->start;
2554 terminal->row -= d;
2555 terminal->selection_start_row -= d;
2556 terminal->selection_end_row -= d;
2557 terminal->start = terminal->saved_start;
2558 terminal->scrolling = 0;
2559 widget_schedule_redraw(terminal->widget);
2560 }
2561
Kristian Høgsberg26130862011-08-24 11:30:21 -04002562 terminal_write(terminal, ch, len);
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002563
2564 /* Hide cursor, except if this was coming from a
2565 * repeating key press. */
2566 serial = display_get_serial(terminal->display);
2567 if (terminal->hide_cursor_serial != serial) {
2568 input_set_pointer_image(input, CURSOR_BLANK);
2569 terminal->hide_cursor_serial = serial;
2570 }
2571 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002572}
2573
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002574static void
2575keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002576 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002577{
2578 struct terminal *terminal = data;
2579
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002580 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002581}
2582
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002583static int wordsep(int ch)
2584{
2585 const char extra[] = "-,./?%&#:_=+@~";
2586
Derek Foreman7978bc82015-09-02 16:21:54 -05002587 if (ch > 127 || ch < 0)
Andre Heider552d12b2012-08-02 20:59:43 +02002588 return 1;
2589
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002590 return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
2591}
2592
2593static int
2594recompute_selection(struct terminal *terminal)
2595{
2596 struct rectangle allocation;
2597 int col, x, width, height;
2598 int start_row, end_row;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002599 int word_start, eol;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002600 int side_margin, top_margin;
2601 int start_x, end_x;
2602 int cw, ch;
2603 union utf8_char *data;
2604
Peng Wuf291f202013-06-06 15:32:41 +08002605 cw = terminal->average_width;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002606 ch = terminal->extents.height;
2607 widget_get_allocation(terminal->widget, &allocation);
2608 width = terminal->width * cw;
2609 height = terminal->height * ch;
2610 side_margin = allocation.x + (allocation.width - width) / 2;
2611 top_margin = allocation.y + (allocation.height - height) / 2;
2612
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002613 start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1;
2614 end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1;
2615
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002616 if (start_row < end_row ||
2617 (start_row == end_row &&
2618 terminal->selection_start_x < terminal->selection_end_x)) {
2619 terminal->selection_start_row = start_row;
2620 terminal->selection_end_row = end_row;
2621 start_x = terminal->selection_start_x;
2622 end_x = terminal->selection_end_x;
2623 } else {
2624 terminal->selection_start_row = end_row;
2625 terminal->selection_end_row = start_row;
2626 start_x = terminal->selection_end_x;
2627 end_x = terminal->selection_start_x;
2628 }
2629
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002630 eol = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002631 if (terminal->selection_start_row < 0) {
2632 terminal->selection_start_row = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002633 terminal->selection_start_col = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002634 } else {
2635 x = side_margin + cw / 2;
2636 data = terminal_get_row(terminal,
2637 terminal->selection_start_row);
2638 word_start = 0;
2639 for (col = 0; col < terminal->width; col++, x += cw) {
2640 if (col == 0 || wordsep(data[col - 1].ch))
2641 word_start = col;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002642 if (data[col].ch != 0)
2643 eol = col + 1;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002644 if (start_x < x)
2645 break;
2646 }
2647
2648 switch (terminal->dragging) {
2649 case SELECT_LINE:
2650 terminal->selection_start_col = 0;
2651 break;
2652 case SELECT_WORD:
2653 terminal->selection_start_col = word_start;
2654 break;
2655 case SELECT_CHAR:
2656 terminal->selection_start_col = col;
2657 break;
2658 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002659 }
2660
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002661 if (terminal->selection_end_row >= terminal->height) {
2662 terminal->selection_end_row = terminal->height;
2663 terminal->selection_end_col = 0;
2664 } else {
2665 x = side_margin + cw / 2;
2666 data = terminal_get_row(terminal, terminal->selection_end_row);
2667 for (col = 0; col < terminal->width; col++, x += cw) {
2668 if (terminal->dragging == SELECT_CHAR && end_x < x)
2669 break;
2670 if (terminal->dragging == SELECT_WORD &&
2671 end_x < x && wordsep(data[col].ch))
2672 break;
2673 }
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002674 terminal->selection_end_col = col;
2675 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002676
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002677 if (terminal->selection_end_col != terminal->selection_start_col ||
2678 terminal->selection_start_row != terminal->selection_end_row) {
2679 col = terminal->selection_end_col;
2680 if (col > 0 && data[col - 1].ch == 0)
2681 terminal->selection_end_col = terminal->width;
2682 data = terminal_get_row(terminal, terminal->selection_start_row);
2683 if (data[terminal->selection_start_col].ch == 0)
2684 terminal->selection_start_col = eol;
2685 }
2686
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002687 return 1;
2688}
2689
Kristian Høgsberg59826582011-01-20 11:56:57 -05002690static void
Pekka Paalanen807f09c2015-03-27 15:48:54 +02002691terminal_minimize(struct terminal *terminal)
2692{
2693 window_set_minimized(terminal->window);
2694}
2695
2696static void
Jasper St. Pierredda93132014-03-13 12:06:00 -04002697menu_func(void *data, struct input *input, int index)
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002698{
Jasper St. Pierredda93132014-03-13 12:06:00 -04002699 struct window *window = data;
2700 struct terminal *terminal = window_get_user_data(window);
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002701
2702 fprintf(stderr, "picked entry %d\n", index);
2703
2704 switch (index) {
2705 case 0:
2706 terminal_new_instance(terminal);
2707 break;
2708 case 1:
2709 terminal_copy(terminal, input);
2710 break;
2711 case 2:
2712 terminal_paste(terminal, input);
2713 break;
Pekka Paalanen807f09c2015-03-27 15:48:54 +02002714 case 3:
2715 terminal_minimize(terminal);
2716 break;
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002717 }
2718}
2719
2720static void
2721show_menu(struct terminal *terminal, struct input *input, uint32_t time)
2722{
2723 int32_t x, y;
2724 static const char *entries[] = {
Pekka Paalanen807f09c2015-03-27 15:48:54 +02002725 "Open Terminal", "Copy", "Paste", "Minimize"
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002726 };
2727
2728 input_get_position(input, &x, &y);
2729 window_show_menu(terminal->display, input, time, terminal->window,
2730 x - 10, y - 10, menu_func,
2731 entries, ARRAY_LENGTH(entries));
2732}
2733
2734static void
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002735click_handler(struct widget *widget, struct terminal *terminal,
2736 struct input *input, int32_t x, int32_t y,
2737 uint32_t time)
2738{
2739 if (time - terminal->click_time < 500)
2740 terminal->click_count++;
2741 else
2742 terminal->click_count = 1;
2743
2744 terminal->click_time = time;
2745 terminal->dragging = (terminal->click_count - 1) % 3 + SELECT_CHAR;
2746
2747 terminal->selection_end_x = terminal->selection_start_x = x;
2748 terminal->selection_end_y = terminal->selection_start_y = y;
2749 if (recompute_selection(terminal))
2750 widget_schedule_redraw(widget);
2751}
2752
2753static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002754button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002755 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002756 uint32_t button,
2757 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002758{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002759 struct terminal *terminal = data;
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002760 int32_t x, y;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002761
2762 switch (button) {
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002763 case BTN_LEFT:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002764 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002765 input_get_position(input, &x, &y);
2766 click_handler(widget, terminal, input, x, y, time);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002767 } else {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002768 terminal->dragging = SELECT_NONE;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002769 }
2770 break;
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002771
2772 case BTN_RIGHT:
2773 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
2774 show_menu(terminal, input, time);
2775 break;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002776 }
2777}
2778
2779static int
Kristian Høgsberg29784402012-06-28 14:27:02 -04002780enter_handler(struct widget *widget,
2781 struct input *input, float x, float y, void *data)
2782{
2783 return CURSOR_IBEAM;
2784}
2785
2786static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002787motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002788 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002789 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002790{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002791 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002792
2793 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002794 input_get_position(input,
2795 &terminal->selection_end_x,
2796 &terminal->selection_end_y);
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002797
2798 if (recompute_selection(terminal))
2799 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002800 }
2801
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002802 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002803}
2804
Magnus Hoff1046f122014-08-05 15:05:59 +02002805/* This magnitude is chosen rather arbitrarily. Really, the scrolling
2806 * should happen on a (fractional) pixel basis, not a line basis. */
2807#define AXIS_UNITS_PER_LINE 256
2808
2809static void
2810axis_handler(struct widget *widget,
2811 struct input *input, uint32_t time,
2812 uint32_t axis,
2813 wl_fixed_t value,
2814 void *data)
2815{
2816 struct terminal *terminal = data;
2817 int lines;
2818
2819 if (axis != WL_POINTER_AXIS_VERTICAL_SCROLL)
2820 return;
2821
2822 terminal->smooth_scroll += value;
2823 lines = terminal->smooth_scroll / AXIS_UNITS_PER_LINE;
2824 terminal->smooth_scroll -= lines * AXIS_UNITS_PER_LINE;
2825
2826 if (lines > 0) {
2827 if (terminal->scrolling) {
2828 if ((uint32_t)lines > terminal->saved_start - terminal->start)
2829 lines = terminal->saved_start - terminal->start;
2830 } else {
2831 lines = 0;
2832 }
2833 } else if (lines < 0) {
2834 uint32_t neg_lines = -lines;
2835
2836 if (neg_lines > terminal->log_size + terminal->start - terminal->end)
2837 lines = terminal->end - terminal->log_size - terminal->start;
2838 }
2839
2840 if (lines) {
2841 if (!terminal->scrolling)
2842 terminal->saved_start = terminal->start;
2843 terminal->scrolling = 1;
2844
2845 terminal->start += lines;
2846 terminal->row -= lines;
2847 terminal->selection_start_row -= lines;
2848 terminal->selection_end_row -= lines;
2849
2850 widget_schedule_redraw(widget);
2851 }
2852}
2853
Alexander Larssonde79dd02013-05-22 14:41:34 +02002854static void
2855output_handler(struct window *window, struct output *output, int enter,
2856 void *data)
2857{
2858 if (enter)
2859 window_set_buffer_transform(window, output_get_transform(output));
2860 window_set_buffer_scale(window, window_get_output_scale(window));
2861 window_schedule_redraw(window);
2862}
2863
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002864static void
2865touch_down_handler(struct widget *widget, struct input *input,
2866 uint32_t serial, uint32_t time, int32_t id,
2867 float x, float y, void *data)
2868{
2869 struct terminal *terminal = data;
2870
2871 if (id == 0)
2872 click_handler(widget, terminal, input, x, y, time);
2873}
2874
2875static void
2876touch_up_handler(struct widget *widget, struct input *input,
2877 uint32_t serial, uint32_t time, int32_t id, void *data)
2878{
2879 struct terminal *terminal = data;
2880
2881 if (id == 0)
2882 terminal->dragging = SELECT_NONE;
2883}
2884
2885static void
2886touch_motion_handler(struct widget *widget, struct input *input,
2887 uint32_t time, int32_t id, float x, float y, void *data)
2888{
2889 struct terminal *terminal = data;
2890
2891 if (terminal->dragging &&
2892 id == 0) {
2893 terminal->selection_end_x = (int)x;
2894 terminal->selection_end_y = (int)y;
2895
2896 if (recompute_selection(terminal))
2897 widget_schedule_redraw(widget);
2898 }
2899}
2900
Peng Wuf291f202013-06-06 15:32:41 +08002901#ifndef howmany
2902#define howmany(x, y) (((x) + ((y) - 1)) / (y))
2903#endif
2904
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002905static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002906terminal_create(struct display *display)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002907{
2908 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002909 cairo_surface_t *surface;
2910 cairo_t *cr;
Peng Wuf291f202013-06-06 15:32:41 +08002911 cairo_text_extents_t text_extents;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002912
Peter Huttererf3d62272013-08-08 11:57:05 +10002913 terminal = xzalloc(sizeof *terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002914 terminal->color_scheme = &DEFAULT_COLORS;
2915 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002916 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002917 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002918 terminal->window = window_create(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002919 terminal->widget = window_frame_create(terminal->window, terminal);
U. Artie Eoff09827e22014-01-15 11:40:37 -08002920 terminal->title = xstrdup("Wayland Terminal");
Kristian Høgsberga83be202013-10-23 20:47:35 -07002921 window_set_title(terminal->window, terminal->title);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002922 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002923
2924 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002925 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002926
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002927 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002928 terminal->margin = 5;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002929 terminal->buffer_height = 1024;
2930 terminal->end = 1;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002931
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002932 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002933 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002934 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002935 keyboard_focus_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002936 window_set_fullscreen_handler(terminal->window, fullscreen_handler);
Alexander Larssonde79dd02013-05-22 14:41:34 +02002937 window_set_output_handler(terminal->window, output_handler);
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002938 window_set_close_handler(terminal->window, close_handler);
Jasper St. Pierrede680992014-04-10 17:23:49 -07002939 window_set_state_changed_handler(terminal->window, state_changed_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002940
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07002941 window_set_data_handler(terminal->window, data_handler);
2942 window_set_drop_handler(terminal->window, drop_handler);
2943
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002944 widget_set_redraw_handler(terminal->widget, redraw_handler);
2945 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002946 widget_set_button_handler(terminal->widget, button_handler);
Kristian Høgsberg29784402012-06-28 14:27:02 -04002947 widget_set_enter_handler(terminal->widget, enter_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002948 widget_set_motion_handler(terminal->widget, motion_handler);
Magnus Hoff1046f122014-08-05 15:05:59 +02002949 widget_set_axis_handler(terminal->widget, axis_handler);
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002950 widget_set_touch_up_handler(terminal->widget, touch_up_handler);
2951 widget_set_touch_down_handler(terminal->widget, touch_down_handler);
2952 widget_set_touch_motion_handler(terminal->widget, touch_motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002953
Kristian Høgsberg09531622010-06-14 23:22:15 -04002954 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2955 cr = cairo_create(surface);
Kristian Høgsberg38912df2012-06-27 17:52:23 -04002956 cairo_set_font_size(cr, option_font_size);
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002957 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002958 CAIRO_FONT_SLANT_NORMAL,
2959 CAIRO_FONT_WEIGHT_BOLD);
2960 terminal->font_bold = cairo_get_scaled_font (cr);
2961 cairo_scaled_font_reference(terminal->font_bold);
2962
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002963 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002964 CAIRO_FONT_SLANT_NORMAL,
2965 CAIRO_FONT_WEIGHT_NORMAL);
2966 terminal->font_normal = cairo_get_scaled_font (cr);
2967 cairo_scaled_font_reference(terminal->font_normal);
2968
Kristian Høgsberg09531622010-06-14 23:22:15 -04002969 cairo_font_extents(cr, &terminal->extents);
Peng Wuf291f202013-06-06 15:32:41 +08002970
2971 /* Compute the average ascii glyph width */
2972 cairo_text_extents(cr, TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS,
2973 &text_extents);
2974 terminal->average_width = howmany
2975 (text_extents.width,
2976 strlen(TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS));
2977 terminal->average_width = ceil(terminal->average_width);
2978
Kristian Høgsberg09531622010-06-14 23:22:15 -04002979 cairo_destroy(cr);
2980 cairo_surface_destroy(surface);
2981
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002982 terminal_resize(terminal, 20, 5); /* Set minimum size first */
Kristian Høgsberga1627922012-06-20 17:30:03 -04002983 terminal_resize(terminal, 80, 25);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002984
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002985 wl_list_insert(terminal_list.prev, &terminal->link);
2986
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002987 return terminal;
2988}
2989
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002990static void
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002991terminal_destroy(struct terminal *terminal)
2992{
Dima Ryazanova85292e2012-11-29 00:27:09 -08002993 display_unwatch_fd(terminal->display, terminal->master);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002994 window_destroy(terminal->window);
2995 close(terminal->master);
2996 wl_list_remove(&terminal->link);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002997
2998 if (wl_list_empty(&terminal_list))
Dima Ryazanova85292e2012-11-29 00:27:09 -08002999 display_exit(terminal->display);
3000
Kristian Høgsberga83be202013-10-23 20:47:35 -07003001 free(terminal->title);
Dima Ryazanova85292e2012-11-29 00:27:09 -08003002 free(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003003}
3004
3005static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003006io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003007{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003008 struct terminal *terminal =
3009 container_of(task, struct terminal, io_task);
3010 char buffer[256];
3011 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003012
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003013 if (events & EPOLLHUP) {
3014 terminal_destroy(terminal);
3015 return;
3016 }
Tim Wiederhakef71accc2011-01-19 23:14:33 +01003017
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003018 len = read(terminal->master, buffer, sizeof buffer);
3019 if (len < 0)
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003020 terminal_destroy(terminal);
3021 else
3022 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003023}
3024
3025static int
3026terminal_run(struct terminal *terminal, const char *path)
3027{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003028 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003029 pid_t pid;
3030
3031 pid = forkpty(&master, NULL, NULL, NULL);
3032 if (pid == 0) {
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04003033 setenv("TERM", option_term, 1);
3034 setenv("COLORTERM", option_term, 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003035 if (execl(path, path, NULL)) {
3036 printf("exec failed: %m\n");
3037 exit(EXIT_FAILURE);
3038 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003039 } else if (pid < 0) {
3040 fprintf(stderr, "failed to fork and create pty (%m).\n");
3041 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003042 }
3043
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003044 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003045 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003046 terminal->io_task.run = io_handler;
3047 display_watch_fd(terminal->display, terminal->master,
3048 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003049
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07003050 if (option_fullscreen)
3051 window_set_fullscreen(terminal->window, 1);
Derek Foreman76091782017-03-08 11:58:20 -06003052 else if (option_maximize)
3053 window_set_maximized(terminal->window, 1);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07003054 else
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003055 terminal_resize(terminal, 80, 24);
3056
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003057 return 0;
3058}
3059
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003060static const struct weston_option terminal_options[] = {
3061 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Derek Foreman76091782017-03-08 11:58:20 -06003062 { WESTON_OPTION_BOOLEAN, "maximized", 'm', &option_maximize },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04003063 { WESTON_OPTION_STRING, "font", 0, &option_font },
Bill Spitzak5cad8432014-08-08 12:59:55 -07003064 { WESTON_OPTION_INTEGER, "font-size", 0, &option_font_size },
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003065 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003066};
3067
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003068int main(int argc, char *argv[])
3069{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003070 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003071 struct terminal *terminal;
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02003072 const char *config_file;
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -07003073 struct weston_config *config;
3074 struct weston_config_section *s;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04003075
Peng Wucfcc1112013-08-19 10:59:21 +08003076 /* as wcwidth is locale-dependent,
3077 wcwidth needs setlocale call to function properly. */
3078 setlocale(LC_ALL, "");
3079
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003080 option_shell = getenv("SHELL");
3081 if (!option_shell)
3082 option_shell = "/bin/bash";
3083
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02003084 config_file = weston_config_get_name_from_env();
3085 config = weston_config_parse(config_file);
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -07003086 s = weston_config_get_section(config, "terminal", NULL, NULL);
3087 weston_config_section_get_string(s, "font", &option_font, "mono");
3088 weston_config_section_get_int(s, "font-size", &option_font_size, 14);
3089 weston_config_section_get_string(s, "term", &option_term, "xterm");
3090 weston_config_destroy(config);
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003091
Bill Spitzak5cad8432014-08-08 12:59:55 -07003092 if (parse_options(terminal_options,
3093 ARRAY_LENGTH(terminal_options), &argc, argv) > 1) {
3094 printf("Usage: %s [OPTIONS]\n"
3095 " --fullscreen or -f\n"
Derek Foreman76091782017-03-08 11:58:20 -06003096 " --maximized or -m\n"
Bill Spitzak5cad8432014-08-08 12:59:55 -07003097 " --font=NAME\n"
3098 " --font-size=SIZE\n"
3099 " --shell=NAME\n", argv[0]);
3100 return 1;
3101 }
3102
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003103 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02003104 if (d == NULL) {
3105 fprintf(stderr, "failed to create display: %m\n");
3106 return -1;
3107 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003108
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003109 wl_list_init(&terminal_list);
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04003110 terminal = terminal_create(d);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003111 if (terminal_run(terminal, option_shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003112 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003113
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003114 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003115
3116 return 0;
3117}