blob: 7cacc8a6b67f4b0243788c2b835003426bec80f8 [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
Manuel Stoeckl5f592c72019-08-19 11:53:35 -040026#include <signal.h>
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +020027#include <stdbool.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050028#include <stdint.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <fcntl.h>
33#include <unistd.h>
34#include <math.h>
35#include <time.h>
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050036#include <pty.h>
Kristian Høgsbergf04e8382008-12-08 00:07:49 -050037#include <ctype.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050038#include <cairo.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040039#include <sys/epoll.h>
Peng Wucfcc1112013-08-19 10:59:21 +080040#include <wchar.h>
41#include <locale.h>
Derek Foreman88353dd2017-03-24 16:29:31 -050042#include <errno.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050043
Kristian Høgsberg67b82152013-10-23 16:52:05 -070044#include <linux/input.h>
45
Pekka Paalanen50719bc2011-11-22 14:18:50 +020046#include <wayland-client.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050047
Pekka Paalanen91b10102019-04-04 14:27:31 +030048#include <libweston/config-parser.h>
Jon Cruz35b2eaa2015-06-15 15:37:08 -070049#include "shared/helpers.h"
Bryce Harringtone99e4bf2016-03-16 14:15:18 -070050#include "shared/xalloc.h"
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050051#include "window.h"
52
Daniel Stonedd8219b2019-11-26 00:32:22 +000053static bool option_fullscreen;
54static bool option_maximize;
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -070055static char *option_font;
56static int option_font_size;
57static char *option_term;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040058static char *option_shell;
59
60static struct wl_list terminal_list;
61
62static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -040063terminal_create(struct display *display);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040064static void
65terminal_destroy(struct terminal *terminal);
66static int
67terminal_run(struct terminal *terminal, const char *path);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050068
Peng Wuf291f202013-06-06 15:32:41 +080069#define TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS \
70 " !\"#$%&'()*+,-./" \
71 "0123456789" \
72 ":;<=>?@" \
73 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
74 "[\\]^_`" \
75 "abcdefghijklmnopqrstuvwxyz" \
76 "{|}~" \
77 ""
78
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050079#define MOD_SHIFT 0x01
80#define MOD_ALT 0x02
81#define MOD_CTRL 0x04
82
Callum Lowcay30eeae52011-01-07 19:46:55 +000083#define ATTRMASK_BOLD 0x01
84#define ATTRMASK_UNDERLINE 0x02
85#define ATTRMASK_BLINK 0x04
86#define ATTRMASK_INVERSE 0x08
Callum Lowcay81179db2011-01-10 12:14:01 +130087#define ATTRMASK_CONCEALED 0x10
Callum Lowcay30eeae52011-01-07 19:46:55 +000088
Callum Lowcayb8609ad2011-01-07 19:46:57 +000089/* Buffer sizes */
Callum Lowcayef57a9b2011-01-14 20:46:23 +130090#define MAX_RESPONSE 256
91#define MAX_ESCAPE 255
Callum Lowcayb8609ad2011-01-07 19:46:57 +000092
Callum Lowcay8e57dd52011-01-07 19:46:59 +000093/* Terminal modes */
94#define MODE_SHOW_CURSOR 0x00000001
95#define MODE_INVERSE 0x00000002
96#define MODE_AUTOWRAP 0x00000004
97#define MODE_AUTOREPEAT 0x00000008
98#define MODE_LF_NEWLINE 0x00000010
Callum Lowcay69e96582011-01-07 19:47:00 +000099#define MODE_IRM 0x00000020
Callum Lowcay7e08e902011-01-07 19:47:02 +0000100#define MODE_DELETE_SENDS_DEL 0x00000040
101#define MODE_ALT_SENDS_ESC 0x00000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000102
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000103union utf8_char {
104 unsigned char byte[4];
105 uint32_t ch;
106};
107
108enum utf8_state {
109 utf8state_start,
110 utf8state_accept,
111 utf8state_reject,
112 utf8state_expect3,
113 utf8state_expect2,
114 utf8state_expect1
115};
116
117struct utf8_state_machine {
118 enum utf8_state state;
119 int len;
120 union utf8_char s;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700121 uint32_t unicode;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000122};
123
124static void
125init_state_machine(struct utf8_state_machine *machine)
126{
127 machine->state = utf8state_start;
128 machine->len = 0;
129 machine->s.ch = 0;
Quentin Glidicc0271532016-07-10 11:00:57 +0200130 machine->unicode = 0;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000131}
132
133static enum utf8_state
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400134utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000135{
Dongjin Kimba89f002024-09-30 17:49:36 +0900136 switch (machine->state) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000137 case utf8state_start:
138 case utf8state_accept:
139 case utf8state_reject:
140 machine->s.ch = 0;
141 machine->len = 0;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300142 if (c == 0xC0 || c == 0xC1) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000143 /* overlong encoding, reject */
144 machine->state = utf8state_reject;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300145 } else if ((c & 0x80) == 0) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000146 /* single byte, accept */
147 machine->s.byte[machine->len++] = c;
148 machine->state = utf8state_accept;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700149 machine->unicode = c;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300150 } else if ((c & 0xC0) == 0x80) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000151 /* parser out of sync, ignore byte */
152 machine->state = utf8state_start;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300153 } else if ((c & 0xE0) == 0xC0) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000154 /* start of two byte sequence */
155 machine->s.byte[machine->len++] = c;
156 machine->state = utf8state_expect1;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700157 machine->unicode = c & 0x1f;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300158 } else if ((c & 0xF0) == 0xE0) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000159 /* start of three byte sequence */
160 machine->s.byte[machine->len++] = c;
161 machine->state = utf8state_expect2;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700162 machine->unicode = c & 0x0f;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300163 } else if ((c & 0xF8) == 0xF0) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000164 /* start of four byte sequence */
165 machine->s.byte[machine->len++] = c;
166 machine->state = utf8state_expect3;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700167 machine->unicode = c & 0x07;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000168 } else {
169 /* overlong encoding, reject */
170 machine->state = utf8state_reject;
171 }
172 break;
173 case utf8state_expect3:
174 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700175 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300176 if ((c & 0xC0) == 0x80) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000177 /* all good, continue */
178 machine->state = utf8state_expect2;
179 } else {
180 /* missing extra byte, reject */
181 machine->state = utf8state_reject;
182 }
183 break;
184 case utf8state_expect2:
185 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700186 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300187 if ((c & 0xC0) == 0x80) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000188 /* all good, continue */
189 machine->state = utf8state_expect1;
190 } else {
191 /* missing extra byte, reject */
192 machine->state = utf8state_reject;
193 }
194 break;
195 case utf8state_expect1:
196 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700197 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300198 if ((c & 0xC0) == 0x80) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000199 /* all good, accept */
200 machine->state = utf8state_accept;
201 } else {
202 /* missing extra byte, reject */
203 machine->state = utf8state_reject;
204 }
205 break;
206 default:
207 machine->state = utf8state_reject;
208 break;
209 }
Michael Vetter2a18a522015-05-15 17:17:47 +0200210
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000211 return machine->state;
212}
213
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700214static uint32_t
215get_unicode(union utf8_char utf8)
216{
217 struct utf8_state_machine machine;
218 int i;
219
220 init_state_machine(&machine);
221 for (i = 0; i < 4; i++) {
222 utf8_next_char(&machine, utf8.byte[i]);
223 if (machine.state == utf8state_accept ||
224 machine.state == utf8state_reject)
225 break;
226 }
227
228 if (machine.state == utf8state_reject)
229 return 0xfffd;
230
231 return machine.unicode;
232}
233
Peng Wucfcc1112013-08-19 10:59:21 +0800234static bool
235is_wide(union utf8_char utf8)
236{
237 uint32_t unichar = get_unicode(utf8);
238 return wcwidth(unichar) > 1;
239}
240
Callum Lowcay256e72f2011-01-07 19:47:01 +0000241struct char_sub {
242 union utf8_char match;
243 union utf8_char replace;
244};
245/* Set last char_sub match to NULL char */
246typedef struct char_sub *character_set;
247
248struct char_sub CS_US[] = {
249 {{{0, }}, {{0, }}}
250};
251static struct char_sub CS_UK[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200252 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000253 {{{0, }}, {{0, }}}
254};
255static struct char_sub CS_SPECIAL[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200256 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond: ♦ */
257 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell: ▒ */
258 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT: ␉ */
259 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF: ␌ */
260 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR: ␍ */
261 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF: ␊ */
262 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree: ° */
263 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus: ± */
264 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL: ␤ */
265 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT: ␋ */
266 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB: ┘ */
267 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT: ┐ */
268 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT: ┌ */
269 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_LB: └ */
270 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS: ┼ */
271 {{{'o', 0, }}, {{0xE2, 0x8E, 0xBA, 0}}}, /* Horiz. Scan Line 1: ⎺ */
272 {{{'p', 0, }}, {{0xE2, 0x8E, 0xBB, 0}}}, /* Horiz. Scan Line 3: ⎻ */
273 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* Horiz. Scan Line 5: ─ */
274 {{{'r', 0, }}, {{0xE2, 0x8E, 0xBC, 0}}}, /* Horiz. Scan Line 7: ⎼ */
275 {{{'s', 0, }}, {{0xE2, 0x8E, 0xBD, 0}}}, /* Horiz. Scan Line 9: ⎽ */
276 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR: ├ */
277 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL: ┤ */
278 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU: ┴ */
279 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD: ┬ */
280 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V: │ */
281 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE: ≤ */
282 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE: ≥ */
283 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI: π */
284 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ: ≠ */
285 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
286 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT: ⋅ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000287 {{{0, }}, {{0, }}}
288};
289
290static void
291apply_char_set(character_set cs, union utf8_char *utf8)
292{
293 int i = 0;
Michael Vetter2a18a522015-05-15 17:17:47 +0200294
Callum Lowcay256e72f2011-01-07 19:47:01 +0000295 while (cs[i].match.byte[0]) {
296 if ((*utf8).ch == cs[i].match.ch) {
297 *utf8 = cs[i].replace;
298 break;
299 }
300 i++;
301 }
302}
303
Callum Lowcay7e08e902011-01-07 19:47:02 +0000304struct key_map {
305 int sym;
306 int num;
307 char escape;
308 char code;
309};
310/* Set last key_sub sym to NULL */
311typedef struct key_map *keyboard_mode;
312
313static struct key_map KM_NORMAL[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400314 { XKB_KEY_Left, 1, '[', 'D' },
315 { XKB_KEY_Right, 1, '[', 'C' },
316 { XKB_KEY_Up, 1, '[', 'A' },
317 { XKB_KEY_Down, 1, '[', 'B' },
318 { XKB_KEY_Home, 1, '[', 'H' },
319 { XKB_KEY_End, 1, '[', 'F' },
320 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000321};
322static struct key_map KM_APPLICATION[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400323 { XKB_KEY_Left, 1, 'O', 'D' },
324 { XKB_KEY_Right, 1, 'O', 'C' },
325 { XKB_KEY_Up, 1, 'O', 'A' },
326 { XKB_KEY_Down, 1, 'O', 'B' },
327 { XKB_KEY_Home, 1, 'O', 'H' },
328 { XKB_KEY_End, 1, 'O', 'F' },
329 { XKB_KEY_KP_Enter, 1, 'O', 'M' },
330 { XKB_KEY_KP_Multiply, 1, 'O', 'j' },
331 { XKB_KEY_KP_Add, 1, 'O', 'k' },
332 { XKB_KEY_KP_Separator, 1, 'O', 'l' },
333 { XKB_KEY_KP_Subtract, 1, 'O', 'm' },
334 { XKB_KEY_KP_Divide, 1, 'O', 'o' },
335 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000336};
337
338static int
339function_key_response(char escape, int num, uint32_t modifiers,
340 char code, char *response)
341{
342 int mod_num = 0;
343 int len;
344
Kristian Høgsberg70163132012-05-08 15:55:39 -0400345 if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
346 if (modifiers & MOD_ALT_MASK) mod_num |= 2;
347 if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000348
349 if (mod_num != 0)
350 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
351 num, mod_num + 1, code);
352 else if (code != '~')
353 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
354 escape, code);
355 else
356 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
357 escape, num, code);
358
359 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
360 else return len;
361}
362
363/* returns the number of bytes written into response,
364 * which must have room for MAX_RESPONSE bytes */
365static int
366apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
367{
368 struct key_map map;
369 int len = 0;
370 int i = 0;
Michael Vetter2a18a522015-05-15 17:17:47 +0200371
Callum Lowcay7e08e902011-01-07 19:47:02 +0000372 while (mode[i].sym) {
373 map = mode[i++];
374 if (sym == map.sym) {
375 len = function_key_response(map.escape, map.num,
376 modifiers, map.code,
377 response);
378 break;
379 }
380 }
Michael Vetter2a18a522015-05-15 17:17:47 +0200381
Callum Lowcay7e08e902011-01-07 19:47:02 +0000382 return len;
383}
384
Callum Lowcay30eeae52011-01-07 19:46:55 +0000385struct terminal_color { double r, g, b, a; };
386struct attr {
387 unsigned char fg, bg;
388 char a; /* attributes format:
389 * 76543210
Callum Lowcay81179db2011-01-10 12:14:01 +1300390 * cilub */
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500391 char s; /* in selection */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000392};
393struct color_scheme {
394 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500395 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000396 struct attr default_attr;
397};
398
399static void
400attr_init(struct attr *data_attr, struct attr attr, int n)
401{
402 int i;
403 for (i = 0; i < n; i++) {
404 data_attr[i] = attr;
405 }
406}
407
Callum Lowcay67a201d2011-01-12 19:23:41 +1300408enum escape_state {
409 escape_state_normal = 0,
410 escape_state_escape,
411 escape_state_dcs,
412 escape_state_csi,
413 escape_state_osc,
414 escape_state_inner_escape,
415 escape_state_ignore,
416 escape_state_special
417};
418
419#define ESC_FLAG_WHAT 0x01
420#define ESC_FLAG_GT 0x02
421#define ESC_FLAG_BANG 0x04
422#define ESC_FLAG_CASH 0x08
423#define ESC_FLAG_SQUOTE 0x10
424#define ESC_FLAG_DQUOTE 0x20
425#define ESC_FLAG_SPACE 0x40
426
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400427enum {
428 SELECT_NONE,
429 SELECT_CHAR,
430 SELECT_WORD,
431 SELECT_LINE
432};
433
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500434struct terminal {
435 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500436 struct widget *widget;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500437 struct display *display;
Kristian Høgsberga83be202013-10-23 20:47:35 -0700438 char *title;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000439 union utf8_char *data;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400440 struct task io_task;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000441 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000442 struct attr *data_attr;
443 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000444 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000445 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000446 char saved_origin_mode;
447 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000448 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000449 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000450 character_set cs, g0, g1;
451 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000452 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000453 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700454 int width, height, row, column, max_width;
455 uint32_t buffer_height;
456 uint32_t start, end, saved_start, log_size;
Magnus Hoff1046f122014-08-05 15:05:59 +0200457 wl_fixed_t smooth_scroll;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000458 int saved_row, saved_column;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700459 int scrolling;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600460 int send_cursor_position;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500461 int fd, master;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500462 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300463 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500464 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300465 enum escape_state state;
466 enum escape_state outer_state;
467 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000468 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500469 int margin;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400470 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000471 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400472 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +0800473 double average_width;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500474 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg88fd4082012-06-21 15:55:03 -0400475 uint32_t hide_cursor_serial;
Kristian Høgsbergb405a802014-02-05 14:44:04 -0800476 int size_in_title;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500477
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400478 struct wl_data_source *selection;
Xiong Zhang49c6aee2013-11-25 18:42:52 +0800479 uint32_t click_time;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400480 int dragging, click_count;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500481 int selection_start_x, selection_start_y;
482 int selection_end_x, selection_end_y;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400483 int selection_start_row, selection_start_col;
484 int selection_end_row, selection_end_col;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -0400485 struct wl_list link;
Derek Foreman88353dd2017-03-24 16:29:31 -0500486 int pace_pipe;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500487};
488
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000489/* Create default tab stops, every 8 characters */
490static void
491terminal_init_tabs(struct terminal *terminal)
492{
493 int i = 0;
Michael Vetter2a18a522015-05-15 17:17:47 +0200494
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000495 while (i < terminal->width) {
496 if (i % 8 == 0)
497 terminal->tab_ruler[i] = 1;
498 else
499 terminal->tab_ruler[i] = 0;
500 i++;
501 }
502}
503
Callum Lowcay30eeae52011-01-07 19:46:55 +0000504static void
505terminal_init(struct terminal *terminal)
506{
507 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000508 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000509 terminal->mode = MODE_SHOW_CURSOR |
510 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000511 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000512 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000513
514 terminal->row = 0;
515 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000516
Callum Lowcay256e72f2011-01-07 19:47:01 +0000517 terminal->g0 = CS_US;
518 terminal->g1 = CS_US;
519 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000520 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000521
522 terminal->saved_g0 = terminal->g0;
523 terminal->saved_g1 = terminal->g1;
524 terminal->saved_cs = terminal->cs;
525
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000526 terminal->saved_attr = terminal->curr_attr;
527 terminal->saved_origin_mode = terminal->origin_mode;
528 terminal->saved_row = terminal->row;
529 terminal->saved_column = terminal->column;
530
531 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000532}
533
534static void
535init_color_table(struct terminal *terminal)
536{
537 int c, r;
538 struct terminal_color *color_table = terminal->color_table;
539
540 for (c = 0; c < 256; c ++) {
541 if (c < 16) {
542 color_table[c] = terminal->color_scheme->palette[c];
543 } else if (c < 232) {
544 r = c - 16;
545 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
546 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
547 color_table[c].r = ((double)(r % 6) / 6.0);
548 color_table[c].a = 1.0;
549 } else {
550 r = (c - 232) * 10 + 8;
551 color_table[c].r = ((double) r) / 256.0;
552 color_table[c].g = color_table[c].r;
553 color_table[c].b = color_table[c].r;
554 color_table[c].a = 1.0;
555 }
556 }
557}
558
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000559static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500560terminal_get_row(struct terminal *terminal, int row)
561{
562 int index;
563
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700564 index = (row + terminal->start) & (terminal->buffer_height - 1);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500565
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700566 return (void *) terminal->data + index * terminal->data_pitch;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500567}
568
Callum Lowcay30eeae52011-01-07 19:46:55 +0000569static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500570terminal_get_attr_row(struct terminal *terminal, int row)
571{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000572 int index;
573
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700574 index = (row + terminal->start) & (terminal->buffer_height - 1);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000575
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700576 return (void *) terminal->data_attr + index * terminal->attr_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000577}
578
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500579union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300580 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500581 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500582};
583
584static void
585terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500586 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500587{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500588 struct attr attr;
589 int foreground, background, tmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500590
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500591 decoded->attr.s = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400592 if (((row == terminal->selection_start_row &&
593 col >= terminal->selection_start_col) ||
594 row > terminal->selection_start_row) &&
595 ((row == terminal->selection_end_row &&
596 col < terminal->selection_end_col) ||
597 row < terminal->selection_end_row))
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500598 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500599
600 /* get the attributes for this character cell */
601 attr = terminal_get_attr_row(terminal, row)[col];
602 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500603 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500604 ((terminal->mode & MODE_SHOW_CURSOR) &&
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400605 window_has_focus(terminal->window) && terminal->row == row &&
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500606 terminal->column == col)) {
607 foreground = attr.bg;
608 background = attr.fg;
609 if (attr.a & ATTRMASK_BOLD) {
610 if (foreground <= 16) foreground |= 0x08;
611 if (background <= 16) background &= 0x07;
612 }
613 } else {
614 foreground = attr.fg;
615 background = attr.bg;
616 }
617
618 if (terminal->mode & MODE_INVERSE) {
619 tmp = foreground;
620 foreground = background;
621 background = tmp;
622 if (attr.a & ATTRMASK_BOLD) {
623 if (foreground <= 16) foreground |= 0x08;
624 if (background <= 16) background &= 0x07;
625 }
626 }
627
Callum Lowcay9d708b02011-01-12 20:06:17 +1300628 decoded->attr.fg = foreground;
629 decoded->attr.bg = background;
630 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000631}
632
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500633
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500634static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000635terminal_scroll_buffer(struct terminal *terminal, int d)
636{
637 int i;
638
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700639 terminal->start += d;
640 if (d < 0) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000641 d = 0 - d;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700642 for (i = 0; i < d; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000643 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
644 attr_init(terminal_get_attr_row(terminal, i),
645 terminal->curr_attr, terminal->width);
646 }
647 } else {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700648 for (i = terminal->height - d; i < terminal->height; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000649 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
650 attr_init(terminal_get_attr_row(terminal, i),
651 terminal->curr_attr, terminal->width);
652 }
653 }
Kristian Høgsberg18e928d2012-06-27 19:29:41 -0400654
655 terminal->selection_start_row -= d;
656 terminal->selection_end_row -= d;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000657}
658
659static void
660terminal_scroll_window(struct terminal *terminal, int d)
661{
662 int i;
663 int window_height;
664 int from_row, to_row;
Michael Vetter2a18a522015-05-15 17:17:47 +0200665
Callum Lowcaybbeac602011-01-07 19:46:58 +0000666 // scrolling range is inclusive
667 window_height = terminal->margin_bottom - terminal->margin_top + 1;
668 d = d % (window_height + 1);
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300669 if (d < 0) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000670 d = 0 - d;
671 to_row = terminal->margin_bottom;
672 from_row = terminal->margin_bottom - d;
Michael Vetter2a18a522015-05-15 17:17:47 +0200673
Callum Lowcaybbeac602011-01-07 19:46:58 +0000674 for (i = 0; i < (window_height - d); i++) {
675 memcpy(terminal_get_row(terminal, to_row - i),
676 terminal_get_row(terminal, from_row - i),
677 terminal->data_pitch);
678 memcpy(terminal_get_attr_row(terminal, to_row - i),
679 terminal_get_attr_row(terminal, from_row - i),
680 terminal->attr_pitch);
681 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000682 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
683 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000684 attr_init(terminal_get_attr_row(terminal, i),
685 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000686 }
687 } else {
688 to_row = terminal->margin_top;
689 from_row = terminal->margin_top + d;
Michael Vetter2a18a522015-05-15 17:17:47 +0200690
Callum Lowcaybbeac602011-01-07 19:46:58 +0000691 for (i = 0; i < (window_height - d); i++) {
692 memcpy(terminal_get_row(terminal, to_row + i),
693 terminal_get_row(terminal, from_row + i),
694 terminal->data_pitch);
695 memcpy(terminal_get_attr_row(terminal, to_row + i),
696 terminal_get_attr_row(terminal, from_row + i),
697 terminal->attr_pitch);
698 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000699 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
700 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000701 attr_init(terminal_get_attr_row(terminal, i),
702 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000703 }
704 }
705}
706
707static void
708terminal_scroll(struct terminal *terminal, int d)
709{
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300710 if (terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
Callum Lowcaybbeac602011-01-07 19:46:58 +0000711 terminal_scroll_buffer(terminal, d);
712 else
713 terminal_scroll_window(terminal, d);
714}
715
716static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000717terminal_shift_line(struct terminal *terminal, int d)
718{
719 union utf8_char *row;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500720 struct attr *attr_row;
Michael Vetter2a18a522015-05-15 17:17:47 +0200721
Callum Lowcay69e96582011-01-07 19:47:00 +0000722 row = terminal_get_row(terminal, terminal->row);
723 attr_row = terminal_get_attr_row(terminal, terminal->row);
724
725 if ((terminal->width + d) <= terminal->column)
726 d = terminal->column + 1 - terminal->width;
727 if ((terminal->column + d) >= terminal->width)
728 d = terminal->width - terminal->column - 1;
Michael Vetter2a18a522015-05-15 17:17:47 +0200729
Callum Lowcay69e96582011-01-07 19:47:00 +0000730 if (d < 0) {
731 d = 0 - d;
732 memmove(&row[terminal->column],
733 &row[terminal->column + d],
734 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
Callum Lowcay69e96582011-01-07 19:47:00 +0000735 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
736 (terminal->width - terminal->column - d) * sizeof(struct attr));
737 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
738 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
739 } else {
740 memmove(&row[terminal->column + d], &row[terminal->column],
741 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
742 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
743 (terminal->width - terminal->column - d) * sizeof(struct attr));
744 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
745 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
746 }
747}
748
749static void
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700750terminal_resize_cells(struct terminal *terminal,
751 int width, int height)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500752{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000753 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000754 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000755 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000756 int data_pitch, attr_pitch;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500757 int i, l, total_rows;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700758 uint32_t d, uheight = height;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500759 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000760 struct winsize ws;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500761
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700762 if (uheight > terminal->buffer_height)
763 height = terminal->buffer_height;
764
Kristian Høgsberg22106762008-12-08 13:50:07 -0500765 if (terminal->width == width && terminal->height == height)
766 return;
767
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700768 if (terminal->data && width <= terminal->max_width) {
769 d = 0;
770 if (height < terminal->height && height <= terminal->row)
771 d = terminal->height - height;
772 else if (height > terminal->height &&
773 terminal->height - 1 == terminal->row) {
774 d = terminal->height - height;
775 if (terminal->log_size < uheight)
776 d = -terminal->start;
777 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500778
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700779 terminal->start += d;
780 terminal->row -= d;
781 } else {
782 terminal->max_width = width;
783 data_pitch = width * sizeof(union utf8_char);
Bryce W. Harrington66cd2c12014-04-28 18:44:08 +0000784 data = xzalloc(data_pitch * terminal->buffer_height);
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700785 attr_pitch = width * sizeof(struct attr);
Bryce W. Harrington66cd2c12014-04-28 18:44:08 +0000786 data_attr = xmalloc(attr_pitch * terminal->buffer_height);
787 tab_ruler = xzalloc(width);
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700788 attr_init(data_attr, terminal->curr_attr,
789 width * terminal->buffer_height);
790
791 if (terminal->data && terminal->data_attr) {
792 if (width > terminal->width)
793 l = terminal->width;
794 else
795 l = width;
796
797 if (terminal->height > height) {
798 total_rows = height;
799 i = 1 + terminal->row - height;
800 if (i > 0) {
801 terminal->start += i;
802 terminal->row = terminal->row - i;
803 }
804 } else {
805 total_rows = terminal->height;
José Bollo4a4704a2013-09-13 11:28:51 +0200806 }
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700807
808 for (i = 0; i < total_rows; i++) {
809 memcpy(&data[width * i],
810 terminal_get_row(terminal, i),
811 l * sizeof(union utf8_char));
812 memcpy(&data_attr[width * i],
813 terminal_get_attr_row(terminal, i),
814 l * sizeof(struct attr));
815 }
816
817 free(terminal->data);
818 free(terminal->data_attr);
819 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500820 }
821
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700822 terminal->data_pitch = data_pitch;
823 terminal->attr_pitch = attr_pitch;
824 terminal->data = data;
825 terminal->data_attr = data_attr;
826 terminal->tab_ruler = tab_ruler;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700827 terminal->start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500828 }
829
Callum Lowcay86653ed2011-01-07 19:47:03 +0000830 terminal->margin_bottom =
831 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500832 terminal->width = width;
833 terminal->height = height;
Kristian Høgsbergdcfff552013-11-22 22:43:20 -0800834 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500835
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000836 /* Update the window size */
837 ws.ws_row = terminal->height;
838 ws.ws_col = terminal->width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500839 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500840 ws.ws_xpixel = allocation.width;
841 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000842 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500843}
844
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500845static void
Jasper St. Pierrede680992014-04-10 17:23:49 -0700846update_title(struct terminal *terminal)
847{
848 if (window_is_resizing(terminal->window)) {
849 char *p;
850 if (asprintf(&p, "%s — [%dx%d]", terminal->title, terminal->width, terminal->height) > 0) {
851 window_set_title(terminal->window, p);
852 free(p);
853 }
854 } else {
855 window_set_title(terminal->window, terminal->title);
856 }
857}
858
859static void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500860resize_handler(struct widget *widget,
861 int32_t width, int32_t height, void *data)
862{
863 struct terminal *terminal = data;
864 int32_t columns, rows, m;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700865
Derek Foreman88353dd2017-03-24 16:29:31 -0500866 if (terminal->pace_pipe >= 0) {
867 close(terminal->pace_pipe);
868 terminal->pace_pipe = -1;
869 }
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500870 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800871 columns = (width - m) / (int32_t) terminal->average_width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500872 rows = (height - m) / (int32_t) terminal->extents.height;
873
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400874 if (!window_is_fullscreen(terminal->window) &&
875 !window_is_maximized(terminal->window)) {
Peng Wuf291f202013-06-06 15:32:41 +0800876 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500877 height = rows * terminal->extents.height + m;
878 widget_set_size(terminal->widget, width, height);
879 }
880
881 terminal_resize_cells(terminal, columns, rows);
Jasper St. Pierrede680992014-04-10 17:23:49 -0700882 update_title(terminal);
883}
884
885static void
886state_changed_handler(struct window *window, void *data)
887{
888 struct terminal *terminal = data;
889 update_title(terminal);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500890}
891
892static void
893terminal_resize(struct terminal *terminal, int columns, int rows)
894{
895 int32_t width, height, m;
896
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400897 if (window_is_fullscreen(terminal->window) ||
898 window_is_maximized(terminal->window))
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500899 return;
900
901 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800902 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500903 height = rows * terminal->extents.height + m;
Kristian Høgsberga1627922012-06-20 17:30:03 -0400904
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500905 window_frame_set_child_size(terminal->widget, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500906}
907
Callum Lowcay30eeae52011-01-07 19:46:55 +0000908struct color_scheme DEFAULT_COLORS = {
909 {
910 {0, 0, 0, 1}, /* black */
911 {0.66, 0, 0, 1}, /* red */
912 {0 , 0.66, 0, 1}, /* green */
913 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
914 {0 , 0 , 0.66, 1}, /* blue */
915 {0.66, 0 , 0.66, 1}, /* magenta */
916 {0, 0.66, 0.66, 1}, /* cyan */
917 {0.66, 0.66, 0.66, 1}, /* light grey */
918 {0.22, 0.33, 0.33, 1}, /* dark grey */
919 {1, 0.33, 0.33, 1}, /* high red */
920 {0.33, 1, 0.33, 1}, /* high green */
921 {1, 1, 0.33, 1}, /* high yellow */
922 {0.33, 0.33, 1, 1}, /* high blue */
923 {1, 0.33, 1, 1}, /* high magenta */
924 {0.33, 1, 1, 1}, /* high cyan */
925 {1, 1, 1, 1} /* white */
926 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500927 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000928 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
929};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400930
Kristian Høgsberg22106762008-12-08 13:50:07 -0500931static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500932terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
933{
934 cairo_set_source_rgba(cr,
935 terminal->color_table[index].r,
936 terminal->color_table[index].g,
937 terminal->color_table[index].b,
938 terminal->color_table[index].a);
939}
940
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500941static void
942terminal_send_selection(struct terminal *terminal, int fd)
943{
944 int row, col;
945 union utf8_char *p_row;
946 union decoded_attr attr;
947 FILE *fp;
948 int len;
949
950 fp = fdopen(fd, "w");
Dongjin Kimba89f002024-09-30 17:49:36 +0900951 if (fp == NULL) {
Brian Lovin1bf14812013-08-08 16:12:55 -0700952 close(fd);
953 return;
954 }
Manuel Bachmanne54cee12015-09-19 13:51:05 +0200955 for (row = terminal->selection_start_row; row < terminal->height; row++) {
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500956 p_row = terminal_get_row(terminal, row);
957 for (col = 0; col < terminal->width; col++) {
Peng Wucfcc1112013-08-19 10:59:21 +0800958 if (p_row[col].ch == 0x200B) /* space glyph */
959 continue;
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500960 /* get the attributes for this character cell */
961 terminal_decode_attr(terminal, row, col, &attr);
962 if (!attr.attr.s)
963 continue;
964 len = strnlen((char *) p_row[col].byte, 4);
Kristian Høgsberg0dee6472012-07-01 21:25:41 -0400965 if (len > 0)
966 fwrite(p_row[col].byte, 1, len, fp);
967 if (len == 0 || col == terminal->width - 1) {
968 fwrite("\n", 1, 1, fp);
969 break;
970 }
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500971 }
972 }
973 fclose(fp);
974}
975
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500976struct glyph_run {
977 struct terminal *terminal;
978 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400979 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500980 union decoded_attr attr;
981 cairo_glyph_t glyphs[256], *g;
982};
983
984static void
985glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
986{
987 run->terminal = terminal;
988 run->cr = cr;
989 run->g = run->glyphs;
990 run->count = 0;
991 run->attr.key = 0;
992}
993
994static void
995glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
996{
997 cairo_scaled_font_t *font;
998
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500999 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
1000 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +13001001 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001002 font = run->terminal->font_bold;
1003 else
1004 font = run->terminal->font_normal;
1005 cairo_set_scaled_font(run->cr, font);
1006 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +13001007 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001008
Callum Lowcay9d708b02011-01-12 20:06:17 +13001009 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
1010 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001011 run->g = run->glyphs;
1012 run->count = 0;
1013 }
Callum Lowcay9d708b02011-01-12 20:06:17 +13001014 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001015}
1016
1017static void
1018glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
1019{
1020 int num_glyphs;
1021 cairo_scaled_font_t *font;
1022
1023 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
1024
Callum Lowcay9d708b02011-01-12 20:06:17 +13001025 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001026 font = run->terminal->font_bold;
1027 else
1028 font = run->terminal->font_normal;
1029
1030 cairo_move_to(run->cr, x, y);
1031 cairo_scaled_font_text_to_glyphs (font, x, y,
1032 (char *) c->byte, 4,
1033 &run->g, &num_glyphs,
1034 NULL, NULL, NULL);
1035 run->g += num_glyphs;
1036 run->count += num_glyphs;
1037}
1038
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001039
Kristian Høgsbergf106fd52011-01-11 10:11:39 -05001040static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001041redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001042{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001043 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001044 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001045 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001046 int top_margin, side_margin;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001047 int row, col, cursor_x, cursor_y;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001048 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001049 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001050 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001051 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001052 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001053 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -05001054 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +08001055 double average_width;
Peng Wucfcc1112013-08-19 10:59:21 +08001056 double unichar_width;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001057
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001058 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001059 widget_get_allocation(terminal->widget, &allocation);
Alexander Larssonde79dd02013-05-22 14:41:34 +02001060 cr = widget_cairo_create(terminal->widget);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001061 cairo_rectangle(cr, allocation.x, allocation.y,
1062 allocation.width, allocation.height);
1063 cairo_clip(cr);
1064 cairo_push_group(cr);
1065
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001066 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -05001067 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001068 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001069
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05001070 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001071
Kristian Høgsberg59826582011-01-20 11:56:57 -05001072 extents = terminal->extents;
Peng Wuf291f202013-06-06 15:32:41 +08001073 average_width = terminal->average_width;
1074 side_margin = (allocation.width - terminal->width * average_width) / 2;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001075 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001076
Callum Lowcay30eeae52011-01-07 19:46:55 +00001077 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001078 cairo_translate(cr, allocation.x + side_margin,
1079 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001080 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001081 for (row = 0; row < terminal->height; row++) {
Peng Wucfcc1112013-08-19 10:59:21 +08001082 p_row = terminal_get_row(terminal, row);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001083 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001084 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -05001085 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001086
Callum Lowcay9d708b02011-01-12 20:06:17 +13001087 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -05001088 continue;
1089
Peng Wucfcc1112013-08-19 10:59:21 +08001090 if (is_wide(p_row[col]))
1091 unichar_width = 2 * average_width;
1092 else
1093 unichar_width = average_width;
1094
Callum Lowcay9d708b02011-01-12 20:06:17 +13001095 terminal_set_color(terminal, cr, attr.attr.bg);
Peng Wuf291f202013-06-06 15:32:41 +08001096 cairo_move_to(cr, col * average_width,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001097 row * extents.height);
Peng Wucfcc1112013-08-19 10:59:21 +08001098 cairo_rel_line_to(cr, unichar_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001099 cairo_rel_line_to(cr, 0, extents.height);
Peng Wucfcc1112013-08-19 10:59:21 +08001100 cairo_rel_line_to(cr, -unichar_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001101 cairo_close_path(cr);
1102 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001103 }
1104 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001105
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001106 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1107
1108 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001109 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001110 for (row = 0; row < terminal->height; row++) {
1111 p_row = terminal_get_row(terminal, row);
1112 for (col = 0; col < terminal->width; col++) {
1113 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -05001114 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001115
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001116 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001117
Peng Wuf291f202013-06-06 15:32:41 +08001118 text_x = col * average_width;
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001119 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +13001120 if (attr.attr.a & ATTRMASK_UNDERLINE) {
1121 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +00001122 cairo_move_to(cr, text_x, (double)text_y + 1.5);
Peng Wuf291f202013-06-06 15:32:41 +08001123 cairo_line_to(cr, text_x + average_width, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001124 cairo_stroke(cr);
1125 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -05001126
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02001127 /* skip space glyph (RLE) we use as a placeholder of
1128 the right half of a double-width character,
1129 because RLE is not available in every font. */
Daiki Ueno56d8a7a2014-04-08 18:46:18 +09001130 if (p_row[col].ch == 0x200B)
1131 continue;
1132
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001133 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001134 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001135 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001136
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001137 attr.key = ~0;
1138 glyph_run_flush(&run, attr);
1139
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001140 if ((terminal->mode & MODE_SHOW_CURSOR) &&
1141 !window_has_focus(terminal->window)) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001142 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001143
Callum Lowcay30eeae52011-01-07 19:46:55 +00001144 cairo_set_line_width(cr, 1);
Peng Wuf291f202013-06-06 15:32:41 +08001145 cairo_move_to(cr, terminal->column * average_width + d,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001146 terminal->row * extents.height + d);
Peng Wuf291f202013-06-06 15:32:41 +08001147 cairo_rel_line_to(cr, average_width - 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001148 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
Peng Wuf291f202013-06-06 15:32:41 +08001149 cairo_rel_line_to(cr, -average_width + 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001150 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001151
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001152 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001153 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001154
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001155 cairo_pop_group_to_source(cr);
1156 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001157 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001158 cairo_surface_destroy(surface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001159
1160 if (terminal->send_cursor_position) {
1161 cursor_x = side_margin + allocation.x +
Peng Wuf291f202013-06-06 15:32:41 +08001162 terminal->column * average_width;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001163 cursor_y = top_margin + allocation.y +
1164 terminal->row * extents.height;
1165 window_set_text_cursor_position(terminal->window,
1166 cursor_x, cursor_y);
1167 terminal->send_cursor_position = 0;
1168 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001169}
1170
1171static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001172terminal_write(struct terminal *terminal, const char *data, size_t length)
1173{
1174 if (write(terminal->master, data, length) < 0)
1175 abort();
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001176 terminal->send_cursor_position = 1;
Kristian Høgsberg26130862011-08-24 11:30:21 -04001177}
1178
1179static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001180terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001181
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001182static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001183handle_char(struct terminal *terminal, union utf8_char utf8);
1184
1185static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001186handle_sgr(struct terminal *terminal, int code);
1187
1188static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001189handle_term_parameter(struct terminal *terminal, int code, int sr)
1190{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001191 int i;
1192
Callum Lowcay67a201d2011-01-12 19:23:41 +13001193 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Dongjin Kimba89f002024-09-30 17:49:36 +09001194 switch (code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001195 case 1: /* DECCKM */
1196 if (sr) terminal->key_mode = KM_APPLICATION;
1197 else terminal->key_mode = KM_NORMAL;
1198 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001199 case 2: /* DECANM */
1200 /* No VT52 support yet */
1201 terminal->g0 = CS_US;
1202 terminal->g1 = CS_US;
1203 terminal->cs = terminal->g0;
1204 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001205 case 3: /* DECCOLM */
1206 if (sr)
1207 terminal_resize(terminal, 132, 24);
1208 else
1209 terminal_resize(terminal, 80, 24);
Michael Vetter2a18a522015-05-15 17:17:47 +02001210
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001211 /* set columns, but also home cursor and clear screen */
1212 terminal->row = 0; terminal->column = 0;
1213 for (i = 0; i < terminal->height; i++) {
1214 memset(terminal_get_row(terminal, i),
1215 0, terminal->data_pitch);
1216 attr_init(terminal_get_attr_row(terminal, i),
1217 terminal->curr_attr, terminal->width);
1218 }
1219 break;
1220 case 5: /* DECSCNM */
1221 if (sr) terminal->mode |= MODE_INVERSE;
1222 else terminal->mode &= ~MODE_INVERSE;
1223 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001224 case 6: /* DECOM */
1225 terminal->origin_mode = sr;
1226 if (terminal->origin_mode)
1227 terminal->row = terminal->margin_top;
1228 else
1229 terminal->row = 0;
1230 terminal->column = 0;
1231 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001232 case 7: /* DECAWM */
1233 if (sr) terminal->mode |= MODE_AUTOWRAP;
1234 else terminal->mode &= ~MODE_AUTOWRAP;
1235 break;
1236 case 8: /* DECARM */
1237 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1238 else terminal->mode &= ~MODE_AUTOREPEAT;
1239 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001240 case 12: /* Very visible cursor (CVVIS) */
1241 /* FIXME: What do we do here. */
1242 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001243 case 25:
1244 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1245 else terminal->mode &= ~MODE_SHOW_CURSOR;
1246 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001247 case 1034: /* smm/rmm, meta mode on/off */
1248 /* ignore */
1249 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001250 case 1037: /* deleteSendsDel */
1251 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1252 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1253 break;
1254 case 1039: /* altSendsEscape */
1255 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1256 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1257 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001258 case 1049: /* rmcup/smcup, alternate screen */
1259 /* Ignore. Should be possible to implement,
1260 * but it's kind of annoying. */
1261 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001262 default:
1263 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1264 break;
1265 }
1266 } else {
Dongjin Kimba89f002024-09-30 17:49:36 +09001267 switch (code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001268 case 4: /* IRM */
1269 if (sr) terminal->mode |= MODE_IRM;
1270 else terminal->mode &= ~MODE_IRM;
1271 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001272 case 20: /* LNM */
1273 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1274 else terminal->mode &= ~MODE_LF_NEWLINE;
1275 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001276 default:
1277 fprintf(stderr, "Unknown parameter: %d\n", code);
1278 break;
1279 }
1280 }
1281}
1282
1283static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001284handle_dcs(struct terminal *terminal)
1285{
1286}
1287
1288static void
1289handle_osc(struct terminal *terminal)
1290{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001291 char *p;
1292 int code;
1293
1294 terminal->escape[terminal->escape_length++] = '\0';
1295 p = &terminal->escape[2];
1296 code = strtol(p, &p, 10);
1297 if (*p == ';') p++;
1298
1299 switch (code) {
1300 case 0: /* Icon name and window title */
1301 case 1: /* Icon label */
1302 case 2: /* Window title*/
Kristian Høgsberga83be202013-10-23 20:47:35 -07001303 free(terminal->title);
1304 terminal->title = strdup(p);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001305 window_set_title(terminal->window, p);
1306 break;
Kristian Høgsberg8a205002013-09-11 11:52:56 -07001307 case 7: /* shell cwd as uri */
1308 break;
Dima Ryazanovc3b63722016-11-03 23:46:02 -07001309 case 777: /* Desktop notifications */
1310 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001311 default:
Kristian Høgsberg8a205002013-09-11 11:52:56 -07001312 fprintf(stderr, "Unknown OSC escape code %d, text %s\n",
1313 code, p);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001314 break;
1315 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001316}
1317
1318static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001319handle_escape(struct terminal *terminal)
1320{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001321 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001322 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001323 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001324 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001325 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001326 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001327 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001328
1329 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001330 i = 0;
1331 p = &terminal->escape[2];
1332 while ((isdigit(*p) || *p == ';') && i < 10) {
1333 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001334 if (!set[i]) {
1335 args[i] = 0;
1336 set[i] = 1;
1337 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001338 p++;
1339 i++;
1340 } else {
1341 args[i] = strtol(p, &p, 10);
1342 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001343 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001344 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001345
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001346 switch (*p) {
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001347 case '@': /* ICH - Insert <count> blank characters */
Callum Lowcay69e96582011-01-07 19:47:00 +00001348 count = set[0] ? args[0] : 1;
1349 if (count == 0) count = 1;
1350 terminal_shift_line(terminal, count);
1351 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001352 case 'A': /* CUU - Move cursor up <count> rows */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001353 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001354 if (count == 0) count = 1;
1355 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001356 terminal->row -= count;
1357 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001358 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001359 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001360 case 'B': /* CUD - Move cursor down <count> rows */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001361 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001362 if (count == 0) count = 1;
1363 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001364 terminal->row += count;
1365 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001366 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001367 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001368 case 'C': /* CUF - Move cursor right by <count> columns */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001369 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001370 if (count == 0) count = 1;
1371 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001372 terminal->column += count;
1373 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001374 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001375 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001376 case 'D': /* CUB - Move cursor left <count> columns */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001377 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001378 if (count == 0) count = 1;
1379 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001380 terminal->column -= count;
1381 else
1382 terminal->column = 0;
1383 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001384 case 'E': /* CNL - Move cursor down <count> rows, to column 1 */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001385 count = set[0] ? args[0] : 1;
1386 if (terminal->row + count <= terminal->margin_bottom)
1387 terminal->row += count;
1388 else
1389 terminal->row = terminal->margin_bottom;
1390 terminal->column = 0;
1391 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001392 case 'F': /* CPL - Move cursour up <count> rows, to column 1 */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001393 count = set[0] ? args[0] : 1;
1394 if (terminal->row - count >= terminal->margin_top)
1395 terminal->row -= count;
1396 else
1397 terminal->row = terminal->margin_top;
1398 terminal->column = 0;
1399 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001400 case 'G': /* CHA - Move cursor to column <y> in current row */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001401 y = set[0] ? args[0] : 1;
1402 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
Michael Vetter2a18a522015-05-15 17:17:47 +02001403
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001404 terminal->column = y - 1;
1405 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001406 case 'f': /* HVP - Move cursor to <x, y> */
1407 case 'H': /* CUP - Move cursor to <x, y> (origin at 1,1) */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001408 x = (set[1] ? args[1] : 1) - 1;
1409 x = x < 0 ? 0 :
1410 (x >= terminal->width ? terminal->width - 1 : x);
Michael Vetter2a18a522015-05-15 17:17:47 +02001411
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001412 y = (set[0] ? args[0] : 1) - 1;
1413 if (terminal->origin_mode) {
1414 y += terminal->margin_top;
1415 y = y < terminal->margin_top ? terminal->margin_top :
1416 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1417 } else {
1418 y = y < 0 ? 0 :
1419 (y >= terminal->height ? terminal->height - 1 : y);
1420 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001421
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001422 terminal->row = y;
1423 terminal->column = x;
1424 break;
1425 case 'I': /* CHT */
1426 count = set[0] ? args[0] : 1;
1427 if (count == 0) count = 1;
1428 while (count > 0 && terminal->column < terminal->width) {
1429 if (terminal->tab_ruler[terminal->column]) count--;
1430 terminal->column++;
1431 }
1432 terminal->column--;
1433 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001434 case 'J': /* ED - Erase display */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001435 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001436 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001437 if (!set[0] || args[0] == 0 || args[0] > 2) {
1438 memset(&row[terminal->column],
1439 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1440 attr_init(&attr_row[terminal->column],
1441 terminal->curr_attr, terminal->width - terminal->column);
1442 for (i = terminal->row + 1; i < terminal->height; i++) {
1443 memset(terminal_get_row(terminal, i),
1444 0, terminal->data_pitch);
1445 attr_init(terminal_get_attr_row(terminal, i),
1446 terminal->curr_attr, terminal->width);
1447 }
1448 } else if (args[0] == 1) {
1449 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1450 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1451 for (i = 0; i < terminal->row; i++) {
1452 memset(terminal_get_row(terminal, i),
1453 0, terminal->data_pitch);
1454 attr_init(terminal_get_attr_row(terminal, i),
1455 terminal->curr_attr, terminal->width);
1456 }
1457 } else if (args[0] == 2) {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07001458 /* Clear screen by scrolling contents out */
1459 terminal_scroll_buffer(terminal,
1460 terminal->end - terminal->start);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001461 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001462 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001463 case 'K': /* EL - Erase line */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001464 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001465 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001466 if (!set[0] || args[0] == 0 || args[0] > 2) {
1467 memset(&row[terminal->column], 0,
1468 (terminal->width - terminal->column) * sizeof(union utf8_char));
1469 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1470 terminal->width - terminal->column);
1471 } else if (args[0] == 1) {
1472 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1473 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1474 } else if (args[0] == 2) {
1475 memset(row, 0, terminal->data_pitch);
1476 attr_init(attr_row, terminal->curr_attr, terminal->width);
1477 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001478 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001479 case 'L': /* IL - Insert <count> blank lines */
Callum Lowcay69e96582011-01-07 19:47:00 +00001480 count = set[0] ? args[0] : 1;
1481 if (count == 0) count = 1;
1482 if (terminal->row >= terminal->margin_top &&
1483 terminal->row < terminal->margin_bottom)
1484 {
1485 top = terminal->margin_top;
1486 terminal->margin_top = terminal->row;
1487 terminal_scroll(terminal, 0 - count);
1488 terminal->margin_top = top;
1489 } else if (terminal->row == terminal->margin_bottom) {
1490 memset(terminal_get_row(terminal, terminal->row),
1491 0, terminal->data_pitch);
1492 attr_init(terminal_get_attr_row(terminal, terminal->row),
1493 terminal->curr_attr, terminal->width);
1494 }
1495 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001496 case 'M': /* DL - Delete <count> lines */
Callum Lowcay69e96582011-01-07 19:47:00 +00001497 count = set[0] ? args[0] : 1;
1498 if (count == 0) count = 1;
1499 if (terminal->row >= terminal->margin_top &&
1500 terminal->row < terminal->margin_bottom)
1501 {
1502 top = terminal->margin_top;
1503 terminal->margin_top = terminal->row;
1504 terminal_scroll(terminal, count);
1505 terminal->margin_top = top;
1506 } else if (terminal->row == terminal->margin_bottom) {
1507 memset(terminal_get_row(terminal, terminal->row),
1508 0, terminal->data_pitch);
1509 }
1510 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001511 case 'P': /* DCH - Delete <count> characters on current line */
Callum Lowcay69e96582011-01-07 19:47:00 +00001512 count = set[0] ? args[0] : 1;
1513 if (count == 0) count = 1;
1514 terminal_shift_line(terminal, 0 - count);
1515 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001516 case 'S': /* SU */
1517 terminal_scroll(terminal, set[0] ? args[0] : 1);
1518 break;
1519 case 'T': /* SD */
1520 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1521 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001522 case 'X': /* ECH - Erase <count> characters on current line */
Callum Lowcay69e96582011-01-07 19:47:00 +00001523 count = set[0] ? args[0] : 1;
1524 if (count == 0) count = 1;
1525 if ((terminal->column + count) > terminal->width)
1526 count = terminal->width - terminal->column;
1527 row = terminal_get_row(terminal, terminal->row);
1528 attr_row = terminal_get_attr_row(terminal, terminal->row);
1529 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1530 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1531 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001532 case 'Z': /* CBT */
1533 count = set[0] ? args[0] : 1;
1534 if (count == 0) count = 1;
1535 while (count > 0 && terminal->column >= 0) {
1536 if (terminal->tab_ruler[terminal->column]) count--;
1537 terminal->column--;
1538 }
1539 terminal->column++;
1540 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001541 case '`': /* HPA - Move cursor to <y> column in current row */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001542 y = set[0] ? args[0] : 1;
1543 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
Michael Vetter2a18a522015-05-15 17:17:47 +02001544
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001545 terminal->column = y - 1;
1546 break;
1547 case 'b': /* REP */
1548 count = set[0] ? args[0] : 1;
1549 if (count == 0) count = 1;
1550 if (terminal->last_char.byte[0])
1551 for (i = 0; i < count; i++)
1552 handle_char(terminal, terminal->last_char);
1553 terminal->last_char.byte[0] = 0;
1554 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001555 case 'c': /* Primary DA - Answer "I am a VT102" */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001556 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001557 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001558 case 'd': /* VPA - Move cursor to <x> row, current column */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001559 x = set[0] ? args[0] : 1;
1560 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
Michael Vetter2a18a522015-05-15 17:17:47 +02001561
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001562 terminal->row = x - 1;
1563 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001564 case 'g': /* TBC - Clear tab stop(s) */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001565 if (!set[0] || args[0] == 0) {
1566 terminal->tab_ruler[terminal->column] = 0;
1567 } else if (args[0] == 3) {
1568 memset(terminal->tab_ruler, 0, terminal->width);
1569 }
1570 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001571 case 'h': /* SM - Set mode */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001572 for (i = 0; i < 10 && set[i]; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001573 handle_term_parameter(terminal, args[i], 1);
1574 }
1575 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001576 case 'l': /* RM - Reset mode */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001577 for (i = 0; i < 10 && set[i]; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001578 handle_term_parameter(terminal, args[i], 0);
1579 }
1580 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001581 case 'm': /* SGR - Set attributes */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001582 for (i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001583 if (i <= 7 && set[i] && set[i + 1] &&
1584 set[i + 2] && args[i + 1] == 5)
1585 {
1586 if (args[i] == 38) {
1587 handle_sgr(terminal, args[i + 2] + 256);
1588 break;
1589 } else if (args[i] == 48) {
1590 handle_sgr(terminal, args[i + 2] + 512);
1591 break;
1592 }
1593 }
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001594 if (set[i]) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001595 handle_sgr(terminal, args[i]);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001596 } else if (i == 0) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001597 handle_sgr(terminal, 0);
1598 break;
1599 } else {
1600 break;
1601 }
1602 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001603 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001604 case 'n': /* DSR - Status report */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001605 i = set[0] ? args[0] : 0;
1606 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001607 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001608 } else if (i == 6) {
1609 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1610 terminal->origin_mode ?
1611 terminal->row+terminal->margin_top : terminal->row+1,
1612 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001613 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001614 }
Dongjin Kimba89f002024-09-30 17:49:36 +09001615 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001616 case 'r': /* DECSTBM - Set scrolling region */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001617 if (!set[0]) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001618 terminal->margin_top = 0;
1619 terminal->margin_bottom = terminal->height-1;
1620 terminal->row = 0;
1621 terminal->column = 0;
1622 } else {
1623 top = (set[0] ? args[0] : 1) - 1;
1624 top = top < 0 ? 0 :
1625 (top >= terminal->height ? terminal->height - 1 : top);
1626 bottom = (set[1] ? args[1] : 1) - 1;
1627 bottom = bottom < 0 ? 0 :
1628 (bottom >= terminal->height ? terminal->height - 1 : bottom);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001629 if (bottom > top) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001630 terminal->margin_top = top;
1631 terminal->margin_bottom = bottom;
1632 } else {
1633 terminal->margin_top = 0;
1634 terminal->margin_bottom = terminal->height-1;
1635 }
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001636 if (terminal->origin_mode)
Callum Lowcaybbeac602011-01-07 19:46:58 +00001637 terminal->row = terminal->margin_top;
1638 else
1639 terminal->row = 0;
1640 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001641 }
1642 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001643 case 's': /* Save cursor location */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001644 terminal->saved_row = terminal->row;
1645 terminal->saved_column = terminal->column;
1646 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001647 case 't': /* windowOps */
1648 if (!set[0]) break;
1649 switch (args[0]) {
1650 case 4: /* resize px */
1651 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001652 widget_schedule_resize(terminal->widget,
1653 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001654 }
1655 break;
1656 case 8: /* resize ch */
1657 if (set[1] && set[2]) {
1658 terminal_resize(terminal, args[2], args[1]);
1659 }
1660 break;
1661 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001662 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001663 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1664 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001665 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001666 break;
1667 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001668 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001669 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1670 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001671 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001672 break;
1673 case 18: /* report ch */
1674 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1675 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001676 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001677 break;
1678 case 21: /* report title */
1679 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1680 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001681 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001682 break;
1683 default:
1684 if (args[0] >= 24)
1685 terminal_resize(terminal, terminal->width, args[0]);
1686 else
1687 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1688 break;
1689 }
Daniel Stoneee96ce22018-04-03 19:38:09 +01001690 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001691 case 'u': /* Restore cursor location */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001692 terminal->row = terminal->saved_row;
1693 terminal->column = terminal->saved_column;
1694 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001695 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001696 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001697 break;
Michael Vetter2a18a522015-05-15 17:17:47 +02001698 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001699}
1700
1701static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001702handle_non_csi_escape(struct terminal *terminal, char code)
1703{
Dongjin Kimba89f002024-09-30 17:49:36 +09001704 switch (code) {
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001705 case 'M': /* RI - Reverse linefeed */
Callum Lowcaybbeac602011-01-07 19:46:58 +00001706 terminal->row -= 1;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001707 if (terminal->row < terminal->margin_top) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001708 terminal->row = terminal->margin_top;
1709 terminal_scroll(terminal, -1);
1710 }
1711 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001712 case 'E': /* NEL - Newline */
Callum Lowcaybbeac602011-01-07 19:46:58 +00001713 terminal->column = 0;
1714 // fallthrough
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001715 case 'D': /* IND - Linefeed */
Callum Lowcaybbeac602011-01-07 19:46:58 +00001716 terminal->row += 1;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001717 if (terminal->row > terminal->margin_bottom) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001718 terminal->row = terminal->margin_bottom;
1719 terminal_scroll(terminal, +1);
1720 }
1721 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001722 case 'c': /* RIS - Reset*/
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001723 terminal_init(terminal);
1724 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001725 case 'H': /* HTS - Set tab stop at current column */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001726 terminal->tab_ruler[terminal->column] = 1;
1727 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001728 case '7': /* DECSC - Save current state */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001729 terminal->saved_row = terminal->row;
1730 terminal->saved_column = terminal->column;
1731 terminal->saved_attr = terminal->curr_attr;
1732 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001733 terminal->saved_cs = terminal->cs;
1734 terminal->saved_g0 = terminal->g0;
1735 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001736 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001737 case '8': /* DECRC - Restore state most recently saved by ESC 7 */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001738 terminal->row = terminal->saved_row;
1739 terminal->column = terminal->saved_column;
1740 terminal->curr_attr = terminal->saved_attr;
1741 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001742 terminal->cs = terminal->saved_cs;
1743 terminal->g0 = terminal->saved_g0;
1744 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001745 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001746 case '=': /* DECPAM - Set application keypad mode */
Callum Lowcay7e08e902011-01-07 19:47:02 +00001747 terminal->key_mode = KM_APPLICATION;
1748 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001749 case '>': /* DECPNM - Set numeric keypad mode */
Callum Lowcay7e08e902011-01-07 19:47:02 +00001750 terminal->key_mode = KM_NORMAL;
1751 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001752 default:
1753 fprintf(stderr, "Unknown escape code: %c\n", code);
1754 break;
1755 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001756}
1757
1758static void
1759handle_special_escape(struct terminal *terminal, char special, char code)
1760{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001761 int i, numChars;
1762
1763 if (special == '#') {
Dongjin Kimba89f002024-09-30 17:49:36 +09001764 switch (code) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001765 case '8':
1766 /* fill with 'E', no cheap way to do this */
1767 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1768 numChars = terminal->width * terminal->height;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001769 for (i = 0; i < numChars; i++) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001770 terminal->data[i].byte[0] = 'E';
1771 }
1772 break;
1773 default:
1774 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1775 break;
1776 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001777 } else if (special == '(' || special == ')') {
Dongjin Kimba89f002024-09-30 17:49:36 +09001778 switch (code) {
Callum Lowcay256e72f2011-01-07 19:47:01 +00001779 case '0':
1780 if (special == '(')
1781 terminal->g0 = CS_SPECIAL;
1782 else
1783 terminal->g1 = CS_SPECIAL;
1784 break;
1785 case 'A':
1786 if (special == '(')
1787 terminal->g0 = CS_UK;
1788 else
1789 terminal->g1 = CS_UK;
1790 break;
1791 case 'B':
1792 if (special == '(')
1793 terminal->g0 = CS_US;
1794 else
1795 terminal->g1 = CS_US;
1796 break;
1797 default:
1798 fprintf(stderr, "Unknown character set %c\n", code);
1799 break;
1800 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001801 } else {
1802 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1803 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001804}
1805
1806static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001807handle_sgr(struct terminal *terminal, int code)
1808{
Dongjin Kimba89f002024-09-30 17:49:36 +09001809 switch (code) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001810 case 0:
1811 terminal->curr_attr = terminal->color_scheme->default_attr;
1812 break;
1813 case 1:
1814 terminal->curr_attr.a |= ATTRMASK_BOLD;
1815 if (terminal->curr_attr.fg < 8)
1816 terminal->curr_attr.fg += 8;
1817 break;
1818 case 4:
1819 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1820 break;
1821 case 5:
1822 terminal->curr_attr.a |= ATTRMASK_BLINK;
1823 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001824 case 8:
1825 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1826 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001827 case 2:
1828 case 21:
1829 case 22:
1830 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1831 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1832 terminal->curr_attr.fg -= 8;
1833 break;
1834 case 24:
1835 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1836 break;
1837 case 25:
1838 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1839 break;
1840 case 7:
1841 case 26:
1842 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1843 break;
1844 case 27:
1845 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1846 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001847 case 28:
1848 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1849 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001850 case 39:
1851 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1852 break;
1853 case 49:
1854 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1855 break;
1856 default:
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001857 if (code >= 30 && code <= 37) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001858 terminal->curr_attr.fg = code - 30;
1859 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1860 terminal->curr_attr.fg += 8;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001861 } else if (code >= 40 && code <= 47) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001862 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001863 } else if (code >= 90 && code <= 97) {
1864 terminal->curr_attr.fg = code - 90 + 8;
1865 } else if (code >= 100 && code <= 107) {
1866 terminal->curr_attr.bg = code - 100 + 8;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001867 } else if (code >= 256 && code < 512) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001868 terminal->curr_attr.fg = code - 256;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001869 } else if (code >= 512 && code < 768) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001870 terminal->curr_attr.bg = code - 512;
1871 } else {
1872 fprintf(stderr, "Unknown SGR code: %d\n", code);
1873 }
1874 break;
1875 }
1876}
1877
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001878/* Returns 1 if c was special, otherwise 0 */
1879static int
1880handle_special_char(struct terminal *terminal, char c)
1881{
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001882 union utf8_char *row;
1883 struct attr *attr_row;
1884
1885 row = terminal_get_row(terminal, terminal->row);
1886 attr_row = terminal_get_attr_row(terminal, terminal->row);
1887
Dongjin Kimba89f002024-09-30 17:49:36 +09001888 switch (c) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001889 case '\r':
1890 terminal->column = 0;
1891 break;
1892 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001893 if (terminal->mode & MODE_LF_NEWLINE) {
1894 terminal->column = 0;
1895 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001896 /* fallthrough */
1897 case '\v':
1898 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001899 terminal->row++;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07001900 if (terminal->row > terminal->margin_bottom) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001901 terminal->row = terminal->margin_bottom;
1902 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001903 }
1904
1905 break;
1906 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001907 while (terminal->column < terminal->width) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001908 if (terminal->mode & MODE_IRM)
1909 terminal_shift_line(terminal, +1);
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001910
1911 if (row[terminal->column].byte[0] == '\0') {
1912 row[terminal->column].byte[0] = ' ';
1913 row[terminal->column].byte[1] = '\0';
1914 attr_row[terminal->column] = terminal->curr_attr;
1915 }
1916
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001917 terminal->column++;
Kristian Høgsbergcca3c2f2012-06-20 15:56:13 -04001918 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001919 }
1920 if (terminal->column >= terminal->width) {
1921 terminal->column = terminal->width - 1;
1922 }
1923
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001924 break;
1925 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001926 if (terminal->column >= terminal->width) {
1927 terminal->column = terminal->width - 2;
1928 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001929 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001930 } else if (terminal->mode & MODE_AUTOWRAP) {
1931 terminal->column = terminal->width - 1;
1932 terminal->row -= 1;
1933 if (terminal->row < terminal->margin_top) {
1934 terminal->row = terminal->margin_top;
1935 terminal_scroll(terminal, -1);
1936 }
1937 }
1938
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001939 break;
1940 case '\a':
1941 /* Bell */
1942 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001943 case '\x0E': /* SO */
1944 terminal->cs = terminal->g1;
1945 break;
1946 case '\x0F': /* SI */
1947 terminal->cs = terminal->g0;
1948 break;
Kristian Høgsberg2a1aa4e2012-08-03 09:37:05 -04001949 case '\0':
1950 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001951 default:
1952 return 0;
1953 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001954
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001955 return 1;
1956}
1957
1958static void
1959handle_char(struct terminal *terminal, union utf8_char utf8)
1960{
1961 union utf8_char *row;
1962 struct attr *attr_row;
Michael Vetter2a18a522015-05-15 17:17:47 +02001963
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001964 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001965
1966 apply_char_set(terminal->cs, &utf8);
Michael Vetter2a18a522015-05-15 17:17:47 +02001967
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001968 /* There are a whole lot of non-characters, control codes,
1969 * and formatting codes that should probably be ignored,
1970 * for example: */
1971 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1972 /* BOM, ignore */
1973 return;
Michael Vetter2a18a522015-05-15 17:17:47 +02001974 }
1975
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001976 /* Some of these non-characters should be translated, e.g.: */
1977 if (utf8.byte[0] < 32) {
1978 utf8.byte[0] = utf8.byte[0] + 64;
1979 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001980
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001981 /* handle right margin effects */
1982 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001983 if (terminal->mode & MODE_AUTOWRAP) {
1984 terminal->column = 0;
1985 terminal->row += 1;
1986 if (terminal->row > terminal->margin_bottom) {
1987 terminal->row = terminal->margin_bottom;
1988 terminal_scroll(terminal, +1);
1989 }
1990 } else {
1991 terminal->column--;
Dongjin Kimba89f002024-09-30 17:49:36 +09001992 }
1993 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001994
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001995 row = terminal_get_row(terminal, terminal->row);
1996 attr_row = terminal_get_attr_row(terminal, terminal->row);
Michael Vetter2a18a522015-05-15 17:17:47 +02001997
Callum Lowcay69e96582011-01-07 19:47:00 +00001998 if (terminal->mode & MODE_IRM)
1999 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002000 row[terminal->column] = utf8;
2001 attr_row[terminal->column++] = terminal->curr_attr;
2002
Kristian Høgsberg1d781ee2013-11-24 16:54:12 -08002003 if (terminal->row + terminal->start + 1 > terminal->end)
2004 terminal->end = terminal->row + terminal->start + 1;
2005 if (terminal->end == terminal->buffer_height)
2006 terminal->log_size = terminal->buffer_height;
2007 else if (terminal->log_size < terminal->buffer_height)
2008 terminal->log_size = terminal->end;
2009
Peng Wucfcc1112013-08-19 10:59:21 +08002010 /* cursor jump for wide character. */
2011 if (is_wide(utf8))
2012 row[terminal->column++].ch = 0x200B; /* space glyph */
2013
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002014 if (utf8.ch != terminal->last_char.ch)
2015 terminal->last_char = utf8;
2016}
2017
Callum Lowcay30eeae52011-01-07 19:46:55 +00002018static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13002019escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
2020{
2021 int len, i;
2022
2023 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
2024 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
2025 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
2026 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
2027 else len = 1; /* Invalid, cannot happen */
2028
2029 if (terminal->escape_length + len <= MAX_ESCAPE) {
2030 for (i = 0; i < len; i++)
2031 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
2032 terminal->escape_length += len;
2033 } else if (terminal->escape_length < MAX_ESCAPE) {
2034 terminal->escape[terminal->escape_length++] = 0;
2035 }
2036}
2037
2038static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002039terminal_data(struct terminal *terminal, const char *data, size_t length)
2040{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002041 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002042 union utf8_char utf8;
2043 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002044
2045 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002046 parser_state =
2047 utf8_next_char(&terminal->state_machine, data[i]);
Dongjin Kimba89f002024-09-30 17:49:36 +09002048 switch (parser_state) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002049 case utf8state_accept:
2050 utf8.ch = terminal->state_machine.s.ch;
2051 break;
2052 case utf8state_reject:
2053 /* the unicode replacement character */
2054 utf8.byte[0] = 0xEF;
2055 utf8.byte[1] = 0xBF;
2056 utf8.byte[2] = 0xBD;
2057 utf8.byte[3] = 0x00;
2058 break;
2059 default:
2060 continue;
2061 }
2062
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002063 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13002064 switch (terminal->state) {
2065 case escape_state_escape:
2066 escape_append_utf8(terminal, utf8);
2067 switch (utf8.byte[0]) {
2068 case 'P': /* DCS */
2069 terminal->state = escape_state_dcs;
2070 break;
2071 case '[': /* CSI */
2072 terminal->state = escape_state_csi;
2073 break;
2074 case ']': /* OSC */
2075 terminal->state = escape_state_osc;
2076 break;
2077 case '#':
2078 case '(':
2079 case ')': /* special */
2080 terminal->state = escape_state_special;
2081 break;
2082 case '^': /* PM (not implemented) */
2083 case '_': /* APC (not implemented) */
2084 terminal->state = escape_state_ignore;
2085 break;
2086 default:
2087 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002088 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13002089 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002090 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002091 continue;
2092 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002093 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
2094 /* do nothing */
2095 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002096 terminal->escape_flags |= ESC_FLAG_WHAT;
2097 } else if (utf8.byte[0] == '>') {
2098 terminal->escape_flags |= ESC_FLAG_GT;
2099 } else if (utf8.byte[0] == '!') {
2100 terminal->escape_flags |= ESC_FLAG_BANG;
2101 } else if (utf8.byte[0] == '$') {
2102 terminal->escape_flags |= ESC_FLAG_CASH;
2103 } else if (utf8.byte[0] == '\'') {
2104 terminal->escape_flags |= ESC_FLAG_SQUOTE;
2105 } else if (utf8.byte[0] == '"') {
2106 terminal->escape_flags |= ESC_FLAG_DQUOTE;
2107 } else if (utf8.byte[0] == ' ') {
2108 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002109 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002110 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002111 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13002112 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002113 }
Michael Vetter2a18a522015-05-15 17:17:47 +02002114
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002115 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
2116 utf8.byte[0] == '`')
2117 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002118 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002119 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002120 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002121 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002122 continue;
2123 case escape_state_inner_escape:
2124 if (utf8.byte[0] == '\\') {
2125 terminal->state = escape_state_normal;
2126 if (terminal->outer_state == escape_state_dcs) {
2127 handle_dcs(terminal);
2128 } else if (terminal->outer_state == escape_state_osc) {
2129 handle_osc(terminal);
2130 }
2131 } else if (utf8.byte[0] == '\e') {
2132 terminal->state = terminal->outer_state;
2133 escape_append_utf8(terminal, utf8);
2134 if (terminal->escape_length >= MAX_ESCAPE)
2135 terminal->state = escape_state_normal;
2136 } else {
2137 terminal->state = terminal->outer_state;
2138 if (terminal->escape_length < MAX_ESCAPE)
2139 terminal->escape[terminal->escape_length++] = '\e';
2140 escape_append_utf8(terminal, utf8);
2141 if (terminal->escape_length >= MAX_ESCAPE)
2142 terminal->state = escape_state_normal;
2143 }
2144 continue;
2145 case escape_state_dcs:
2146 case escape_state_osc:
2147 case escape_state_ignore:
2148 if (utf8.byte[0] == '\e') {
2149 terminal->outer_state = terminal->state;
2150 terminal->state = escape_state_inner_escape;
2151 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
2152 terminal->state = escape_state_normal;
2153 handle_osc(terminal);
2154 } else {
2155 escape_append_utf8(terminal, utf8);
2156 if (terminal->escape_length >= MAX_ESCAPE)
2157 terminal->state = escape_state_normal;
2158 }
2159 continue;
2160 case escape_state_special:
2161 escape_append_utf8(terminal, utf8);
2162 terminal->state = escape_state_normal;
2163 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2164 handle_special_escape(terminal, terminal->escape[1],
2165 utf8.byte[0]);
2166 }
2167 continue;
2168 default:
2169 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002170 }
2171
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002172 /* this is valid, because ASCII characters are never used to
2173 * introduce a multibyte sequence in UTF-8 */
2174 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002175 terminal->state = escape_state_escape;
2176 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002177 terminal->escape[0] = '\e';
2178 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002179 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002180 } else {
2181 handle_char(terminal, utf8);
2182 } /* if */
2183 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002184
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002185 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002186}
2187
2188static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002189data_source_target(void *data,
2190 struct wl_data_source *source, const char *mime_type)
2191{
2192 fprintf(stderr, "data_source_target, %s\n", mime_type);
2193}
2194
2195static void
2196data_source_send(void *data,
2197 struct wl_data_source *source,
2198 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002199{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002200 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002201
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002202 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002203}
2204
2205static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002206data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002207{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002208 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002209}
2210
Sebastian Wick1bdf3632019-04-09 12:18:25 +02002211static void
2212data_source_dnd_drop_performed(void *data, struct wl_data_source *source)
2213{
2214}
2215
2216static void
2217data_source_dnd_finished(void *data, struct wl_data_source *source)
2218{
2219}
2220
2221static void
2222data_source_action(void *data,
2223 struct wl_data_source *source, uint32_t dnd_action)
2224{
2225}
2226
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002227static const struct wl_data_source_listener data_source_listener = {
2228 data_source_target,
2229 data_source_send,
Sebastian Wick1bdf3632019-04-09 12:18:25 +02002230 data_source_cancelled,
2231 data_source_dnd_drop_performed,
2232 data_source_dnd_finished,
2233 data_source_action
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002234};
2235
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07002236static const char text_mime_type[] = "text/plain;charset=utf-8";
2237
2238static void
2239data_handler(struct window *window,
2240 struct input *input,
2241 float x, float y, const char **types, void *data)
2242{
2243 int i, has_text = 0;
2244
2245 if (!types)
2246 return;
2247 for (i = 0; types[i]; i++)
2248 if (strcmp(types[i], text_mime_type) == 0)
2249 has_text = 1;
2250
2251 if (!has_text) {
2252 input_accept(input, NULL);
2253 } else {
2254 input_accept(input, text_mime_type);
2255 }
2256}
2257
2258static void
2259drop_handler(struct window *window, struct input *input,
2260 int32_t x, int32_t y, void *data)
2261{
2262 struct terminal *terminal = data;
2263
2264 input_receive_drag_data_to_fd(input, text_mime_type, terminal->master);
2265}
2266
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002267static void
2268fullscreen_handler(struct window *window, void *data)
2269{
2270 struct terminal *terminal = data;
2271
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002272 window_set_fullscreen(window, !window_is_fullscreen(terminal->window));
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002273}
2274
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002275static void
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002276close_handler(void *data)
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002277{
2278 struct terminal *terminal = data;
2279
2280 terminal_destroy(terminal);
2281}
2282
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002283static void
2284terminal_copy(struct terminal *terminal, struct input *input)
2285{
2286 terminal->selection =
2287 display_create_data_source(terminal->display);
Derek Foreman11b62422017-04-20 14:31:36 -05002288 if (!terminal->selection)
2289 return;
2290
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002291 wl_data_source_offer(terminal->selection,
2292 "text/plain;charset=utf-8");
2293 wl_data_source_add_listener(terminal->selection,
2294 &data_source_listener, terminal);
2295 input_set_selection(input, terminal->selection,
2296 display_get_serial(terminal->display));
2297}
2298
2299static void
2300terminal_paste(struct terminal *terminal, struct input *input)
2301{
2302 input_receive_selection_data_to_fd(input,
2303 "text/plain;charset=utf-8",
2304 terminal->master);
2305
2306}
2307
2308static void
2309terminal_new_instance(struct terminal *terminal)
2310{
2311 struct terminal *new_terminal;
2312
2313 new_terminal = terminal_create(terminal->display);
2314 if (terminal_run(new_terminal, option_shell))
2315 terminal_destroy(new_terminal);
2316}
2317
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002318static int
2319handle_bound_key(struct terminal *terminal,
2320 struct input *input, uint32_t sym, uint32_t time)
2321{
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002322 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002323 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002324 /* Cut selection; terminal doesn't do cut, fall
2325 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002326 case XKB_KEY_C:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002327 terminal_copy(terminal, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002328 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002329 case XKB_KEY_V:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002330 terminal_paste(terminal, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002331 return 1;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002332 case XKB_KEY_N:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002333 terminal_new_instance(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002334 return 1;
2335
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002336 case XKB_KEY_Up:
2337 if (!terminal->scrolling)
2338 terminal->saved_start = terminal->start;
2339 if (terminal->start == terminal->end - terminal->log_size)
2340 return 1;
2341
2342 terminal->scrolling = 1;
2343 terminal->start--;
2344 terminal->row++;
2345 terminal->selection_start_row++;
2346 terminal->selection_end_row++;
2347 widget_schedule_redraw(terminal->widget);
2348 return 1;
2349
2350 case XKB_KEY_Down:
2351 if (!terminal->scrolling)
2352 terminal->saved_start = terminal->start;
2353
2354 if (terminal->start == terminal->saved_start)
2355 return 1;
2356
2357 terminal->scrolling = 1;
2358 terminal->start++;
2359 terminal->row--;
2360 terminal->selection_start_row--;
2361 terminal->selection_end_row--;
2362 widget_schedule_redraw(terminal->widget);
2363 return 1;
2364
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002365 default:
2366 return 0;
2367 }
2368}
2369
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002370static void
2371key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002372 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2373 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002374{
2375 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002376 char ch[MAX_RESPONSE];
Kristian Høgsberg88fd4082012-06-21 15:55:03 -04002377 uint32_t modifiers, serial;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002378 int ret, len = 0, d;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002379 bool convert_utf8 = true;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002380
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002381 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002382 if ((modifiers & MOD_CONTROL_MASK) &&
2383 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002384 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2385 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002386 return;
2387
Daniel Stonea8468712012-11-07 17:51:35 +11002388 /* Map keypad symbols to 'normal' equivalents before processing */
2389 switch (sym) {
2390 case XKB_KEY_KP_Space:
2391 sym = XKB_KEY_space;
2392 break;
2393 case XKB_KEY_KP_Tab:
2394 sym = XKB_KEY_Tab;
2395 break;
2396 case XKB_KEY_KP_Enter:
2397 sym = XKB_KEY_Return;
2398 break;
2399 case XKB_KEY_KP_Left:
2400 sym = XKB_KEY_Left;
2401 break;
2402 case XKB_KEY_KP_Up:
2403 sym = XKB_KEY_Up;
2404 break;
2405 case XKB_KEY_KP_Right:
2406 sym = XKB_KEY_Right;
2407 break;
2408 case XKB_KEY_KP_Down:
2409 sym = XKB_KEY_Down;
2410 break;
2411 case XKB_KEY_KP_Equal:
2412 sym = XKB_KEY_equal;
2413 break;
2414 case XKB_KEY_KP_Multiply:
2415 sym = XKB_KEY_asterisk;
2416 break;
2417 case XKB_KEY_KP_Add:
2418 sym = XKB_KEY_plus;
2419 break;
2420 case XKB_KEY_KP_Separator:
2421 /* Note this is actually locale-dependent and should mostly be
2422 * a comma. But leave it as period until we one day start
2423 * doing the right thing. */
2424 sym = XKB_KEY_period;
2425 break;
2426 case XKB_KEY_KP_Subtract:
2427 sym = XKB_KEY_minus;
2428 break;
2429 case XKB_KEY_KP_Decimal:
2430 sym = XKB_KEY_period;
2431 break;
2432 case XKB_KEY_KP_Divide:
2433 sym = XKB_KEY_slash;
2434 break;
2435 case XKB_KEY_KP_0:
2436 case XKB_KEY_KP_1:
2437 case XKB_KEY_KP_2:
2438 case XKB_KEY_KP_3:
2439 case XKB_KEY_KP_4:
2440 case XKB_KEY_KP_5:
2441 case XKB_KEY_KP_6:
2442 case XKB_KEY_KP_7:
2443 case XKB_KEY_KP_8:
2444 case XKB_KEY_KP_9:
2445 sym = (sym - XKB_KEY_KP_0) + XKB_KEY_0;
2446 break;
2447 default:
2448 break;
2449 }
2450
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002451 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002452 case XKB_KEY_BackSpace:
Kristian Høgsbergb7f94bf2012-06-21 12:29:36 -04002453 if (modifiers & MOD_ALT_MASK)
2454 ch[len++] = 0x1b;
Kristian Høgsberg71a4cf42012-06-20 17:57:56 -04002455 ch[len++] = 0x7f;
2456 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002457 case XKB_KEY_Tab:
2458 case XKB_KEY_Linefeed:
2459 case XKB_KEY_Clear:
2460 case XKB_KEY_Pause:
2461 case XKB_KEY_Scroll_Lock:
2462 case XKB_KEY_Sys_Req:
2463 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002464 ch[len++] = sym & 0x7f;
2465 break;
2466
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002467 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002468 if (terminal->mode & MODE_LF_NEWLINE) {
2469 ch[len++] = 0x0D;
2470 ch[len++] = 0x0A;
2471 } else {
2472 ch[len++] = 0x0D;
2473 }
2474 break;
2475
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002476 case XKB_KEY_Shift_L:
2477 case XKB_KEY_Shift_R:
2478 case XKB_KEY_Control_L:
2479 case XKB_KEY_Control_R:
2480 case XKB_KEY_Alt_L:
2481 case XKB_KEY_Alt_R:
Kristian Høgsberg22fbcf72012-06-22 12:18:56 -04002482 case XKB_KEY_Meta_L:
2483 case XKB_KEY_Meta_R:
2484 case XKB_KEY_Super_L:
2485 case XKB_KEY_Super_R:
2486 case XKB_KEY_Hyper_L:
2487 case XKB_KEY_Hyper_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002488 break;
2489
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002490 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002491 len = function_key_response('[', 2, modifiers, '~', ch);
2492 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002493 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002494 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2495 ch[len++] = '\x04';
2496 } else {
2497 len = function_key_response('[', 3, modifiers, '~', ch);
2498 }
2499 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002500 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002501 len = function_key_response('[', 5, modifiers, '~', ch);
2502 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002503 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002504 len = function_key_response('[', 6, modifiers, '~', ch);
2505 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002506 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002507 len = function_key_response('O', 1, modifiers, 'P', ch);
2508 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002509 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002510 len = function_key_response('O', 1, modifiers, 'Q', ch);
2511 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002512 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002513 len = function_key_response('O', 1, modifiers, 'R', ch);
2514 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002515 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002516 len = function_key_response('O', 1, modifiers, 'S', ch);
2517 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002518 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002519 len = function_key_response('[', 15, modifiers, '~', ch);
2520 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002521 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002522 len = function_key_response('[', 17, modifiers, '~', ch);
2523 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002524 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002525 len = function_key_response('[', 18, modifiers, '~', ch);
2526 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002527 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002528 len = function_key_response('[', 19, modifiers, '~', ch);
2529 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002530 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002531 len = function_key_response('[', 20, modifiers, '~', ch);
2532 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002533 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002534 len = function_key_response('[', 21, modifiers, '~', ch);
2535 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002536 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002537 len = function_key_response('[', 24, modifiers, '~', ch);
2538 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002539 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002540 /* Handle special keys with alternate mappings */
2541 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2542 if (len != 0) break;
Michael Vetter2a18a522015-05-15 17:17:47 +02002543
Kristian Høgsberg70163132012-05-08 15:55:39 -04002544 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002545 if (sym >= '3' && sym <= '7')
2546 sym = (sym & 0x1f) + 8;
2547
2548 if (!((sym >= '!' && sym <= '/') ||
2549 (sym >= '8' && sym <= '?') ||
2550 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2551 else if (sym == '2') sym = 0x00;
2552 else if (sym == '/') sym = 0x1F;
2553 else if (sym == '8' || sym == '?') sym = 0x7F;
Kristian Høgsbergae9e0732012-06-21 12:30:15 -04002554 }
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002555 if (modifiers & MOD_ALT_MASK) {
2556 if (terminal->mode & MODE_ALT_SENDS_ESC) {
2557 ch[len++] = 0x1b;
2558 } else {
2559 sym = sym | 0x80;
2560 convert_utf8 = false;
2561 }
Callum Lowcay7e08e902011-01-07 19:47:02 +00002562 }
2563
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002564 if ((sym < 128) ||
2565 (!convert_utf8 && sym < 256)) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002566 ch[len++] = sym;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002567 } else {
2568 ret = xkb_keysym_to_utf8(sym, ch + len,
2569 MAX_RESPONSE - len);
2570 if (ret < 0)
2571 fprintf(stderr,
2572 "Warning: buffer too small to encode "
2573 "UTF8 character\n");
2574 else
2575 len += ret;
2576 }
2577
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002578 break;
2579 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002580
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002581 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002582 if (terminal->scrolling) {
2583 d = terminal->saved_start - terminal->start;
2584 terminal->row -= d;
2585 terminal->selection_start_row -= d;
2586 terminal->selection_end_row -= d;
2587 terminal->start = terminal->saved_start;
2588 terminal->scrolling = 0;
2589 widget_schedule_redraw(terminal->widget);
2590 }
2591
Kristian Høgsberg26130862011-08-24 11:30:21 -04002592 terminal_write(terminal, ch, len);
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002593
2594 /* Hide cursor, except if this was coming from a
2595 * repeating key press. */
2596 serial = display_get_serial(terminal->display);
2597 if (terminal->hide_cursor_serial != serial) {
2598 input_set_pointer_image(input, CURSOR_BLANK);
2599 terminal->hide_cursor_serial = serial;
2600 }
2601 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002602}
2603
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002604static void
2605keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002606 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002607{
2608 struct terminal *terminal = data;
2609
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002610 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002611}
2612
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002613static int wordsep(int ch)
2614{
2615 const char extra[] = "-,./?%&#:_=+@~";
2616
Derek Foreman7978bc82015-09-02 16:21:54 -05002617 if (ch > 127 || ch < 0)
Andre Heider552d12b2012-08-02 20:59:43 +02002618 return 1;
2619
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002620 return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
2621}
2622
2623static int
2624recompute_selection(struct terminal *terminal)
2625{
2626 struct rectangle allocation;
2627 int col, x, width, height;
2628 int start_row, end_row;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002629 int word_start, eol;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002630 int side_margin, top_margin;
2631 int start_x, end_x;
2632 int cw, ch;
Marius Vlad7d070ca2018-11-23 14:02:07 +02002633 union utf8_char *data = NULL;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002634
Peng Wuf291f202013-06-06 15:32:41 +08002635 cw = terminal->average_width;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002636 ch = terminal->extents.height;
2637 widget_get_allocation(terminal->widget, &allocation);
2638 width = terminal->width * cw;
2639 height = terminal->height * ch;
2640 side_margin = allocation.x + (allocation.width - width) / 2;
2641 top_margin = allocation.y + (allocation.height - height) / 2;
2642
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002643 start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1;
2644 end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1;
2645
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002646 if (start_row < end_row ||
2647 (start_row == end_row &&
2648 terminal->selection_start_x < terminal->selection_end_x)) {
2649 terminal->selection_start_row = start_row;
2650 terminal->selection_end_row = end_row;
2651 start_x = terminal->selection_start_x;
2652 end_x = terminal->selection_end_x;
2653 } else {
2654 terminal->selection_start_row = end_row;
2655 terminal->selection_end_row = start_row;
2656 start_x = terminal->selection_end_x;
2657 end_x = terminal->selection_start_x;
2658 }
2659
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002660 eol = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002661 if (terminal->selection_start_row < 0) {
2662 terminal->selection_start_row = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002663 terminal->selection_start_col = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002664 } else {
2665 x = side_margin + cw / 2;
2666 data = terminal_get_row(terminal,
2667 terminal->selection_start_row);
2668 word_start = 0;
2669 for (col = 0; col < terminal->width; col++, x += cw) {
2670 if (col == 0 || wordsep(data[col - 1].ch))
2671 word_start = col;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002672 if (data[col].ch != 0)
2673 eol = col + 1;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002674 if (start_x < x)
2675 break;
2676 }
2677
2678 switch (terminal->dragging) {
2679 case SELECT_LINE:
2680 terminal->selection_start_col = 0;
2681 break;
2682 case SELECT_WORD:
2683 terminal->selection_start_col = word_start;
2684 break;
2685 case SELECT_CHAR:
2686 terminal->selection_start_col = col;
2687 break;
2688 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002689 }
2690
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002691 if (terminal->selection_end_row >= terminal->height) {
2692 terminal->selection_end_row = terminal->height;
2693 terminal->selection_end_col = 0;
2694 } else {
2695 x = side_margin + cw / 2;
2696 data = terminal_get_row(terminal, terminal->selection_end_row);
2697 for (col = 0; col < terminal->width; col++, x += cw) {
2698 if (terminal->dragging == SELECT_CHAR && end_x < x)
2699 break;
2700 if (terminal->dragging == SELECT_WORD &&
2701 end_x < x && wordsep(data[col].ch))
2702 break;
2703 }
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002704 terminal->selection_end_col = col;
2705 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002706
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002707 if (terminal->selection_end_col != terminal->selection_start_col ||
2708 terminal->selection_start_row != terminal->selection_end_row) {
2709 col = terminal->selection_end_col;
2710 if (col > 0 && data[col - 1].ch == 0)
2711 terminal->selection_end_col = terminal->width;
2712 data = terminal_get_row(terminal, terminal->selection_start_row);
2713 if (data[terminal->selection_start_col].ch == 0)
2714 terminal->selection_start_col = eol;
2715 }
2716
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002717 return 1;
2718}
2719
Kristian Høgsberg59826582011-01-20 11:56:57 -05002720static void
Pekka Paalanen807f09c2015-03-27 15:48:54 +02002721terminal_minimize(struct terminal *terminal)
2722{
2723 window_set_minimized(terminal->window);
2724}
2725
2726static void
Jasper St. Pierredda93132014-03-13 12:06:00 -04002727menu_func(void *data, struct input *input, int index)
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002728{
Jasper St. Pierredda93132014-03-13 12:06:00 -04002729 struct window *window = data;
2730 struct terminal *terminal = window_get_user_data(window);
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002731
2732 fprintf(stderr, "picked entry %d\n", index);
2733
2734 switch (index) {
2735 case 0:
2736 terminal_new_instance(terminal);
2737 break;
2738 case 1:
2739 terminal_copy(terminal, input);
2740 break;
2741 case 2:
2742 terminal_paste(terminal, input);
2743 break;
Pekka Paalanen807f09c2015-03-27 15:48:54 +02002744 case 3:
2745 terminal_minimize(terminal);
2746 break;
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002747 }
2748}
2749
2750static void
2751show_menu(struct terminal *terminal, struct input *input, uint32_t time)
2752{
2753 int32_t x, y;
2754 static const char *entries[] = {
Pekka Paalanen807f09c2015-03-27 15:48:54 +02002755 "Open Terminal", "Copy", "Paste", "Minimize"
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002756 };
2757
2758 input_get_position(input, &x, &y);
2759 window_show_menu(terminal->display, input, time, terminal->window,
2760 x - 10, y - 10, menu_func,
2761 entries, ARRAY_LENGTH(entries));
2762}
2763
2764static void
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002765click_handler(struct widget *widget, struct terminal *terminal,
2766 struct input *input, int32_t x, int32_t y,
2767 uint32_t time)
2768{
2769 if (time - terminal->click_time < 500)
2770 terminal->click_count++;
2771 else
2772 terminal->click_count = 1;
2773
2774 terminal->click_time = time;
2775 terminal->dragging = (terminal->click_count - 1) % 3 + SELECT_CHAR;
2776
2777 terminal->selection_end_x = terminal->selection_start_x = x;
2778 terminal->selection_end_y = terminal->selection_start_y = y;
2779 if (recompute_selection(terminal))
2780 widget_schedule_redraw(widget);
2781}
2782
2783static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002784button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002785 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002786 uint32_t button,
2787 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002788{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002789 struct terminal *terminal = data;
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002790 int32_t x, y;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002791
2792 switch (button) {
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002793 case BTN_LEFT:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002794 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002795 input_get_position(input, &x, &y);
2796 click_handler(widget, terminal, input, x, y, time);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002797 } else {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002798 terminal->dragging = SELECT_NONE;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002799 }
2800 break;
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002801
2802 case BTN_RIGHT:
2803 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
2804 show_menu(terminal, input, time);
2805 break;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002806 }
2807}
2808
2809static int
Kristian Høgsberg29784402012-06-28 14:27:02 -04002810enter_handler(struct widget *widget,
2811 struct input *input, float x, float y, void *data)
2812{
2813 return CURSOR_IBEAM;
2814}
2815
2816static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002817motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002818 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002819 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002820{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002821 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002822
2823 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002824 input_get_position(input,
2825 &terminal->selection_end_x,
2826 &terminal->selection_end_y);
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002827
2828 if (recompute_selection(terminal))
2829 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002830 }
2831
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002832 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002833}
2834
Magnus Hoff1046f122014-08-05 15:05:59 +02002835/* This magnitude is chosen rather arbitrarily. Really, the scrolling
2836 * should happen on a (fractional) pixel basis, not a line basis. */
2837#define AXIS_UNITS_PER_LINE 256
2838
2839static void
2840axis_handler(struct widget *widget,
2841 struct input *input, uint32_t time,
2842 uint32_t axis,
2843 wl_fixed_t value,
2844 void *data)
2845{
2846 struct terminal *terminal = data;
2847 int lines;
2848
2849 if (axis != WL_POINTER_AXIS_VERTICAL_SCROLL)
2850 return;
2851
2852 terminal->smooth_scroll += value;
2853 lines = terminal->smooth_scroll / AXIS_UNITS_PER_LINE;
2854 terminal->smooth_scroll -= lines * AXIS_UNITS_PER_LINE;
2855
2856 if (lines > 0) {
2857 if (terminal->scrolling) {
2858 if ((uint32_t)lines > terminal->saved_start - terminal->start)
2859 lines = terminal->saved_start - terminal->start;
2860 } else {
2861 lines = 0;
2862 }
2863 } else if (lines < 0) {
2864 uint32_t neg_lines = -lines;
2865
2866 if (neg_lines > terminal->log_size + terminal->start - terminal->end)
2867 lines = terminal->end - terminal->log_size - terminal->start;
2868 }
2869
2870 if (lines) {
2871 if (!terminal->scrolling)
2872 terminal->saved_start = terminal->start;
2873 terminal->scrolling = 1;
2874
2875 terminal->start += lines;
2876 terminal->row -= lines;
2877 terminal->selection_start_row -= lines;
2878 terminal->selection_end_row -= lines;
2879
2880 widget_schedule_redraw(widget);
2881 }
2882}
2883
Alexander Larssonde79dd02013-05-22 14:41:34 +02002884static void
2885output_handler(struct window *window, struct output *output, int enter,
2886 void *data)
2887{
2888 if (enter)
2889 window_set_buffer_transform(window, output_get_transform(output));
2890 window_set_buffer_scale(window, window_get_output_scale(window));
2891 window_schedule_redraw(window);
2892}
2893
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002894static void
2895touch_down_handler(struct widget *widget, struct input *input,
2896 uint32_t serial, uint32_t time, int32_t id,
2897 float x, float y, void *data)
2898{
2899 struct terminal *terminal = data;
2900
2901 if (id == 0)
2902 click_handler(widget, terminal, input, x, y, time);
2903}
2904
2905static void
2906touch_up_handler(struct widget *widget, struct input *input,
2907 uint32_t serial, uint32_t time, int32_t id, void *data)
2908{
2909 struct terminal *terminal = data;
2910
2911 if (id == 0)
2912 terminal->dragging = SELECT_NONE;
2913}
2914
2915static void
2916touch_motion_handler(struct widget *widget, struct input *input,
2917 uint32_t time, int32_t id, float x, float y, void *data)
2918{
2919 struct terminal *terminal = data;
2920
2921 if (terminal->dragging &&
2922 id == 0) {
2923 terminal->selection_end_x = (int)x;
2924 terminal->selection_end_y = (int)y;
2925
2926 if (recompute_selection(terminal))
2927 widget_schedule_redraw(widget);
2928 }
2929}
2930
Peng Wuf291f202013-06-06 15:32:41 +08002931#ifndef howmany
2932#define howmany(x, y) (((x) + ((y) - 1)) / (y))
2933#endif
2934
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002935static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002936terminal_create(struct display *display)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002937{
2938 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002939 cairo_surface_t *surface;
2940 cairo_t *cr;
Peng Wuf291f202013-06-06 15:32:41 +08002941 cairo_text_extents_t text_extents;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002942
Peter Huttererf3d62272013-08-08 11:57:05 +10002943 terminal = xzalloc(sizeof *terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002944 terminal->color_scheme = &DEFAULT_COLORS;
2945 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002946 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002947 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002948 terminal->window = window_create(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002949 terminal->widget = window_frame_create(terminal->window, terminal);
U. Artie Eoff09827e22014-01-15 11:40:37 -08002950 terminal->title = xstrdup("Wayland Terminal");
Kristian Høgsberga83be202013-10-23 20:47:35 -07002951 window_set_title(terminal->window, terminal->title);
Marius Vladb3544c22021-09-09 13:52:18 +03002952 window_set_appid(terminal->window,
2953 "org.freedesktop.weston.wayland-terminal");
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002954 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002955
2956 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002957 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002958
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002959 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002960 terminal->margin = 5;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002961 terminal->buffer_height = 1024;
2962 terminal->end = 1;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002963
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002964 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002965 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002966 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002967 keyboard_focus_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002968 window_set_fullscreen_handler(terminal->window, fullscreen_handler);
Alexander Larssonde79dd02013-05-22 14:41:34 +02002969 window_set_output_handler(terminal->window, output_handler);
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002970 window_set_close_handler(terminal->window, close_handler);
Jasper St. Pierrede680992014-04-10 17:23:49 -07002971 window_set_state_changed_handler(terminal->window, state_changed_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002972
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07002973 window_set_data_handler(terminal->window, data_handler);
2974 window_set_drop_handler(terminal->window, drop_handler);
2975
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002976 widget_set_redraw_handler(terminal->widget, redraw_handler);
2977 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002978 widget_set_button_handler(terminal->widget, button_handler);
Kristian Høgsberg29784402012-06-28 14:27:02 -04002979 widget_set_enter_handler(terminal->widget, enter_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002980 widget_set_motion_handler(terminal->widget, motion_handler);
Magnus Hoff1046f122014-08-05 15:05:59 +02002981 widget_set_axis_handler(terminal->widget, axis_handler);
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002982 widget_set_touch_up_handler(terminal->widget, touch_up_handler);
2983 widget_set_touch_down_handler(terminal->widget, touch_down_handler);
2984 widget_set_touch_motion_handler(terminal->widget, touch_motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002985
Kristian Høgsberg09531622010-06-14 23:22:15 -04002986 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2987 cr = cairo_create(surface);
Kristian Høgsberg38912df2012-06-27 17:52:23 -04002988 cairo_set_font_size(cr, option_font_size);
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002989 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002990 CAIRO_FONT_SLANT_NORMAL,
2991 CAIRO_FONT_WEIGHT_BOLD);
2992 terminal->font_bold = cairo_get_scaled_font (cr);
2993 cairo_scaled_font_reference(terminal->font_bold);
2994
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002995 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002996 CAIRO_FONT_SLANT_NORMAL,
2997 CAIRO_FONT_WEIGHT_NORMAL);
2998 terminal->font_normal = cairo_get_scaled_font (cr);
2999 cairo_scaled_font_reference(terminal->font_normal);
3000
Kristian Høgsberg09531622010-06-14 23:22:15 -04003001 cairo_font_extents(cr, &terminal->extents);
Peng Wuf291f202013-06-06 15:32:41 +08003002
3003 /* Compute the average ascii glyph width */
3004 cairo_text_extents(cr, TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS,
3005 &text_extents);
3006 terminal->average_width = howmany
3007 (text_extents.width,
3008 strlen(TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS));
3009 terminal->average_width = ceil(terminal->average_width);
3010
Kristian Høgsberg09531622010-06-14 23:22:15 -04003011 cairo_destroy(cr);
3012 cairo_surface_destroy(surface);
3013
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04003014 terminal_resize(terminal, 20, 5); /* Set minimum size first */
Kristian Høgsberga1627922012-06-20 17:30:03 -04003015 terminal_resize(terminal, 80, 25);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05003016
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003017 wl_list_insert(terminal_list.prev, &terminal->link);
3018
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003019 return terminal;
3020}
3021
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003022static void
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003023terminal_destroy(struct terminal *terminal)
3024{
Dima Ryazanova85292e2012-11-29 00:27:09 -08003025 display_unwatch_fd(terminal->display, terminal->master);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003026 window_destroy(terminal->window);
3027 close(terminal->master);
3028 wl_list_remove(&terminal->link);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003029
3030 if (wl_list_empty(&terminal_list))
Dima Ryazanova85292e2012-11-29 00:27:09 -08003031 display_exit(terminal->display);
3032
Kristian Høgsberga83be202013-10-23 20:47:35 -07003033 free(terminal->title);
Dima Ryazanova85292e2012-11-29 00:27:09 -08003034 free(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003035}
3036
3037static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003038io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003039{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003040 struct terminal *terminal =
3041 container_of(task, struct terminal, io_task);
3042 char buffer[256];
3043 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003044
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003045 if (events & EPOLLHUP) {
3046 terminal_destroy(terminal);
3047 return;
3048 }
Tim Wiederhakef71accc2011-01-19 23:14:33 +01003049
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003050 len = read(terminal->master, buffer, sizeof buffer);
3051 if (len < 0)
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003052 terminal_destroy(terminal);
3053 else
3054 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003055}
3056
3057static int
3058terminal_run(struct terminal *terminal, const char *path)
3059{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003060 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003061 pid_t pid;
Derek Foreman88353dd2017-03-24 16:29:31 -05003062 int pipes[2];
3063
3064 /* Awkwardness: There's a sticky race condition here. If
3065 * anything prints after the forkpty() but before the window has
3066 * a size then we'll segfault. So we make a pipe and wait on
3067 * it before actually exec()ing the terminal. The resize
3068 * handler closes it in the parent process and the child continues
3069 * on to launch a shell.
3070 *
3071 * The reason we don't just do terminal_run() after the window
3072 * has a size is that we'd prefer to perform the fork() before
3073 * the process opens a wayland connection.
3074 */
3075 if (pipe(pipes) == -1) {
3076 fprintf(stderr, "Can't create pipe for pacing.\n");
3077 exit(EXIT_FAILURE);
3078 }
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003079
3080 pid = forkpty(&master, NULL, NULL, NULL);
3081 if (pid == 0) {
Derek Foreman88353dd2017-03-24 16:29:31 -05003082 int ret;
3083
3084 close(pipes[1]);
3085 do {
3086 char tmp;
3087 ret = read(pipes[0], &tmp, 1);
3088 } while (ret == -1 && errno == EINTR);
3089 close(pipes[0]);
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04003090 setenv("TERM", option_term, 1);
3091 setenv("COLORTERM", option_term, 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003092 if (execl(path, path, NULL)) {
Antonio Borneo39578632019-04-26 23:57:31 +02003093 printf("exec failed: %s\n", strerror(errno));
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003094 exit(EXIT_FAILURE);
3095 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003096 } else if (pid < 0) {
Antonio Borneo39578632019-04-26 23:57:31 +02003097 fprintf(stderr, "failed to fork and create pty (%s).\n",
3098 strerror(errno));
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003099 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003100 }
3101
Derek Foreman88353dd2017-03-24 16:29:31 -05003102 close(pipes[0]);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003103 terminal->master = master;
Derek Foreman88353dd2017-03-24 16:29:31 -05003104 terminal->pace_pipe = pipes[1];
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003105 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003106 terminal->io_task.run = io_handler;
3107 display_watch_fd(terminal->display, terminal->master,
3108 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003109
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07003110 if (option_fullscreen)
3111 window_set_fullscreen(terminal->window, 1);
Derek Foreman76091782017-03-08 11:58:20 -06003112 else if (option_maximize)
3113 window_set_maximized(terminal->window, 1);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07003114 else
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003115 terminal_resize(terminal, 80, 24);
3116
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003117 return 0;
3118}
3119
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003120static const struct weston_option terminal_options[] = {
3121 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Derek Foreman76091782017-03-08 11:58:20 -06003122 { WESTON_OPTION_BOOLEAN, "maximized", 'm', &option_maximize },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04003123 { WESTON_OPTION_STRING, "font", 0, &option_font },
Bill Spitzak5cad8432014-08-08 12:59:55 -07003124 { WESTON_OPTION_INTEGER, "font-size", 0, &option_font_size },
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003125 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003126};
3127
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003128int main(int argc, char *argv[])
3129{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003130 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003131 struct terminal *terminal;
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02003132 const char *config_file;
Manuel Stoeckl5f592c72019-08-19 11:53:35 -04003133 struct sigaction sigpipe;
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -07003134 struct weston_config *config;
3135 struct weston_config_section *s;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04003136
Peng Wucfcc1112013-08-19 10:59:21 +08003137 /* as wcwidth is locale-dependent,
3138 wcwidth needs setlocale call to function properly. */
3139 setlocale(LC_ALL, "");
3140
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003141 option_shell = getenv("SHELL");
3142 if (!option_shell)
Alyssa Ross6e2eb8f2021-11-15 23:13:14 +00003143 option_shell = "/bin/sh";
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003144
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02003145 config_file = weston_config_get_name_from_env();
3146 config = weston_config_parse(config_file);
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -07003147 s = weston_config_get_section(config, "terminal", NULL, NULL);
Alyssa Ross7c121822021-11-17 17:25:17 +00003148 weston_config_section_get_string(s, "font", &option_font, "monospace");
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -07003149 weston_config_section_get_int(s, "font-size", &option_font_size, 14);
3150 weston_config_section_get_string(s, "term", &option_term, "xterm");
3151 weston_config_destroy(config);
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003152
Bill Spitzak5cad8432014-08-08 12:59:55 -07003153 if (parse_options(terminal_options,
3154 ARRAY_LENGTH(terminal_options), &argc, argv) > 1) {
3155 printf("Usage: %s [OPTIONS]\n"
3156 " --fullscreen or -f\n"
Derek Foreman76091782017-03-08 11:58:20 -06003157 " --maximized or -m\n"
Bill Spitzak5cad8432014-08-08 12:59:55 -07003158 " --font=NAME\n"
3159 " --font-size=SIZE\n"
3160 " --shell=NAME\n", argv[0]);
3161 return 1;
3162 }
3163
Manuel Stoeckl5f592c72019-08-19 11:53:35 -04003164 /* Disable SIGPIPE so that paste operations do not crash the program
3165 * when the file descriptor provided to receive data is a pipe or
3166 * socket whose reading end has been closed */
3167 sigpipe.sa_handler = SIG_IGN;
3168 sigemptyset(&sigpipe.sa_mask);
3169 sigpipe.sa_flags = 0;
3170 sigaction(SIGPIPE, &sigpipe, NULL);
3171
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003172 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02003173 if (d == NULL) {
Antonio Borneo39578632019-04-26 23:57:31 +02003174 fprintf(stderr, "failed to create display: %s\n",
3175 strerror(errno));
Yuval Fledele9f5e362010-11-22 21:34:19 +02003176 return -1;
3177 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003178
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003179 wl_list_init(&terminal_list);
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04003180 terminal = terminal_create(d);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003181 if (terminal_run(terminal, option_shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003182 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003183
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003184 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003185
3186 return 0;
3187}