blob: 274ced0958abb957038b0654e5361b6d90ed90a0 [file] [log] [blame]
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -07004 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050010 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -070011 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050022 */
23
Bryce Harringtonb4dae9b2016-06-15 18:13:07 -070024#include "config.h"
Peng Wucfcc1112013-08-19 10:59:21 +080025
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +020026#include <stdbool.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
34#include <time.h>
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050035#include <pty.h>
Kristian Høgsbergf04e8382008-12-08 00:07:49 -050036#include <ctype.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050037#include <cairo.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040038#include <sys/epoll.h>
Peng Wucfcc1112013-08-19 10:59:21 +080039#include <wchar.h>
40#include <locale.h>
Derek Foreman88353dd2017-03-24 16:29:31 -050041#include <errno.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050042
Kristian Høgsberg67b82152013-10-23 16:52:05 -070043#include <linux/input.h>
44
Pekka Paalanen50719bc2011-11-22 14:18:50 +020045#include <wayland-client.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050046
Jon Cruz4678bab2015-06-15 15:37:07 -070047#include "shared/config-parser.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070048#include "shared/helpers.h"
Bryce Harringtone99e4bf2016-03-16 14:15:18 -070049#include "shared/xalloc.h"
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050050#include "window.h"
51
Kristian Høgsberg0395f302008-12-22 12:14:50 -050052static int option_fullscreen;
Derek Foreman76091782017-03-08 11:58:20 -060053static int option_maximize;
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -070054static char *option_font;
55static int option_font_size;
56static char *option_term;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040057static char *option_shell;
58
59static struct wl_list terminal_list;
60
61static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -040062terminal_create(struct display *display);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040063static void
64terminal_destroy(struct terminal *terminal);
65static int
66terminal_run(struct terminal *terminal, const char *path);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050067
Peng Wuf291f202013-06-06 15:32:41 +080068#define TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS \
69 " !\"#$%&'()*+,-./" \
70 "0123456789" \
71 ":;<=>?@" \
72 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
73 "[\\]^_`" \
74 "abcdefghijklmnopqrstuvwxyz" \
75 "{|}~" \
76 ""
77
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050078#define MOD_SHIFT 0x01
79#define MOD_ALT 0x02
80#define MOD_CTRL 0x04
81
Callum Lowcay30eeae52011-01-07 19:46:55 +000082#define ATTRMASK_BOLD 0x01
83#define ATTRMASK_UNDERLINE 0x02
84#define ATTRMASK_BLINK 0x04
85#define ATTRMASK_INVERSE 0x08
Callum Lowcay81179db2011-01-10 12:14:01 +130086#define ATTRMASK_CONCEALED 0x10
Callum Lowcay30eeae52011-01-07 19:46:55 +000087
Callum Lowcayb8609ad2011-01-07 19:46:57 +000088/* Buffer sizes */
Callum Lowcayef57a9b2011-01-14 20:46:23 +130089#define MAX_RESPONSE 256
90#define MAX_ESCAPE 255
Callum Lowcayb8609ad2011-01-07 19:46:57 +000091
Callum Lowcay8e57dd52011-01-07 19:46:59 +000092/* Terminal modes */
93#define MODE_SHOW_CURSOR 0x00000001
94#define MODE_INVERSE 0x00000002
95#define MODE_AUTOWRAP 0x00000004
96#define MODE_AUTOREPEAT 0x00000008
97#define MODE_LF_NEWLINE 0x00000010
Callum Lowcay69e96582011-01-07 19:47:00 +000098#define MODE_IRM 0x00000020
Callum Lowcay7e08e902011-01-07 19:47:02 +000099#define MODE_DELETE_SENDS_DEL 0x00000040
100#define MODE_ALT_SENDS_ESC 0x00000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000101
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000102union utf8_char {
103 unsigned char byte[4];
104 uint32_t ch;
105};
106
107enum utf8_state {
108 utf8state_start,
109 utf8state_accept,
110 utf8state_reject,
111 utf8state_expect3,
112 utf8state_expect2,
113 utf8state_expect1
114};
115
116struct utf8_state_machine {
117 enum utf8_state state;
118 int len;
119 union utf8_char s;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700120 uint32_t unicode;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000121};
122
123static void
124init_state_machine(struct utf8_state_machine *machine)
125{
126 machine->state = utf8state_start;
127 machine->len = 0;
128 machine->s.ch = 0;
Quentin Glidicc0271532016-07-10 11:00:57 +0200129 machine->unicode = 0;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000130}
131
132static enum utf8_state
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400133utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000134{
135 switch(machine->state) {
136 case utf8state_start:
137 case utf8state_accept:
138 case utf8state_reject:
139 machine->s.ch = 0;
140 machine->len = 0;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300141 if (c == 0xC0 || c == 0xC1) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000142 /* overlong encoding, reject */
143 machine->state = utf8state_reject;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300144 } else if ((c & 0x80) == 0) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000145 /* single byte, accept */
146 machine->s.byte[machine->len++] = c;
147 machine->state = utf8state_accept;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700148 machine->unicode = c;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300149 } else if ((c & 0xC0) == 0x80) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000150 /* parser out of sync, ignore byte */
151 machine->state = utf8state_start;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300152 } else if ((c & 0xE0) == 0xC0) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000153 /* start of two byte sequence */
154 machine->s.byte[machine->len++] = c;
155 machine->state = utf8state_expect1;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700156 machine->unicode = c & 0x1f;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300157 } else if ((c & 0xF0) == 0xE0) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000158 /* start of three byte sequence */
159 machine->s.byte[machine->len++] = c;
160 machine->state = utf8state_expect2;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700161 machine->unicode = c & 0x0f;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300162 } else if ((c & 0xF8) == 0xF0) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000163 /* start of four byte sequence */
164 machine->s.byte[machine->len++] = c;
165 machine->state = utf8state_expect3;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700166 machine->unicode = c & 0x07;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000167 } else {
168 /* overlong encoding, reject */
169 machine->state = utf8state_reject;
170 }
171 break;
172 case utf8state_expect3:
173 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700174 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300175 if ((c & 0xC0) == 0x80) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000176 /* all good, continue */
177 machine->state = utf8state_expect2;
178 } else {
179 /* missing extra byte, reject */
180 machine->state = utf8state_reject;
181 }
182 break;
183 case utf8state_expect2:
184 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700185 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300186 if ((c & 0xC0) == 0x80) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000187 /* all good, continue */
188 machine->state = utf8state_expect1;
189 } else {
190 /* missing extra byte, reject */
191 machine->state = utf8state_reject;
192 }
193 break;
194 case utf8state_expect1:
195 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700196 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300197 if ((c & 0xC0) == 0x80) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000198 /* all good, accept */
199 machine->state = utf8state_accept;
200 } else {
201 /* missing extra byte, reject */
202 machine->state = utf8state_reject;
203 }
204 break;
205 default:
206 machine->state = utf8state_reject;
207 break;
208 }
Michael Vetter2a18a522015-05-15 17:17:47 +0200209
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000210 return machine->state;
211}
212
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700213static uint32_t
214get_unicode(union utf8_char utf8)
215{
216 struct utf8_state_machine machine;
217 int i;
218
219 init_state_machine(&machine);
220 for (i = 0; i < 4; i++) {
221 utf8_next_char(&machine, utf8.byte[i]);
222 if (machine.state == utf8state_accept ||
223 machine.state == utf8state_reject)
224 break;
225 }
226
227 if (machine.state == utf8state_reject)
228 return 0xfffd;
229
230 return machine.unicode;
231}
232
Peng Wucfcc1112013-08-19 10:59:21 +0800233static bool
234is_wide(union utf8_char utf8)
235{
236 uint32_t unichar = get_unicode(utf8);
237 return wcwidth(unichar) > 1;
238}
239
Callum Lowcay256e72f2011-01-07 19:47:01 +0000240struct char_sub {
241 union utf8_char match;
242 union utf8_char replace;
243};
244/* Set last char_sub match to NULL char */
245typedef struct char_sub *character_set;
246
247struct char_sub CS_US[] = {
248 {{{0, }}, {{0, }}}
249};
250static struct char_sub CS_UK[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200251 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000252 {{{0, }}, {{0, }}}
253};
254static struct char_sub CS_SPECIAL[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200255 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond: ♦ */
256 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell: ▒ */
257 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT: ␉ */
258 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF: ␌ */
259 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR: ␍ */
260 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF: ␊ */
261 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree: ° */
262 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus: ± */
263 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL: ␤ */
264 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT: ␋ */
265 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB: ┘ */
266 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT: ┐ */
267 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT: ┌ */
268 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_LB: └ */
269 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS: ┼ */
270 {{{'o', 0, }}, {{0xE2, 0x8E, 0xBA, 0}}}, /* Horiz. Scan Line 1: ⎺ */
271 {{{'p', 0, }}, {{0xE2, 0x8E, 0xBB, 0}}}, /* Horiz. Scan Line 3: ⎻ */
272 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* Horiz. Scan Line 5: ─ */
273 {{{'r', 0, }}, {{0xE2, 0x8E, 0xBC, 0}}}, /* Horiz. Scan Line 7: ⎼ */
274 {{{'s', 0, }}, {{0xE2, 0x8E, 0xBD, 0}}}, /* Horiz. Scan Line 9: ⎽ */
275 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR: ├ */
276 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL: ┤ */
277 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU: ┴ */
278 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD: ┬ */
279 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V: │ */
280 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE: ≤ */
281 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE: ≥ */
282 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI: π */
283 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ: ≠ */
284 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
285 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT: ⋅ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000286 {{{0, }}, {{0, }}}
287};
288
289static void
290apply_char_set(character_set cs, union utf8_char *utf8)
291{
292 int i = 0;
Michael Vetter2a18a522015-05-15 17:17:47 +0200293
Callum Lowcay256e72f2011-01-07 19:47:01 +0000294 while (cs[i].match.byte[0]) {
295 if ((*utf8).ch == cs[i].match.ch) {
296 *utf8 = cs[i].replace;
297 break;
298 }
299 i++;
300 }
301}
302
Callum Lowcay7e08e902011-01-07 19:47:02 +0000303struct key_map {
304 int sym;
305 int num;
306 char escape;
307 char code;
308};
309/* Set last key_sub sym to NULL */
310typedef struct key_map *keyboard_mode;
311
312static struct key_map KM_NORMAL[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400313 { XKB_KEY_Left, 1, '[', 'D' },
314 { XKB_KEY_Right, 1, '[', 'C' },
315 { XKB_KEY_Up, 1, '[', 'A' },
316 { XKB_KEY_Down, 1, '[', 'B' },
317 { XKB_KEY_Home, 1, '[', 'H' },
318 { XKB_KEY_End, 1, '[', 'F' },
319 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000320};
321static struct key_map KM_APPLICATION[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400322 { XKB_KEY_Left, 1, 'O', 'D' },
323 { XKB_KEY_Right, 1, 'O', 'C' },
324 { XKB_KEY_Up, 1, 'O', 'A' },
325 { XKB_KEY_Down, 1, 'O', 'B' },
326 { XKB_KEY_Home, 1, 'O', 'H' },
327 { XKB_KEY_End, 1, 'O', 'F' },
328 { XKB_KEY_KP_Enter, 1, 'O', 'M' },
329 { XKB_KEY_KP_Multiply, 1, 'O', 'j' },
330 { XKB_KEY_KP_Add, 1, 'O', 'k' },
331 { XKB_KEY_KP_Separator, 1, 'O', 'l' },
332 { XKB_KEY_KP_Subtract, 1, 'O', 'm' },
333 { XKB_KEY_KP_Divide, 1, 'O', 'o' },
334 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000335};
336
337static int
338function_key_response(char escape, int num, uint32_t modifiers,
339 char code, char *response)
340{
341 int mod_num = 0;
342 int len;
343
Kristian Høgsberg70163132012-05-08 15:55:39 -0400344 if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
345 if (modifiers & MOD_ALT_MASK) mod_num |= 2;
346 if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000347
348 if (mod_num != 0)
349 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
350 num, mod_num + 1, code);
351 else if (code != '~')
352 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
353 escape, code);
354 else
355 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
356 escape, num, code);
357
358 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
359 else return len;
360}
361
362/* returns the number of bytes written into response,
363 * which must have room for MAX_RESPONSE bytes */
364static int
365apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
366{
367 struct key_map map;
368 int len = 0;
369 int i = 0;
Michael Vetter2a18a522015-05-15 17:17:47 +0200370
Callum Lowcay7e08e902011-01-07 19:47:02 +0000371 while (mode[i].sym) {
372 map = mode[i++];
373 if (sym == map.sym) {
374 len = function_key_response(map.escape, map.num,
375 modifiers, map.code,
376 response);
377 break;
378 }
379 }
Michael Vetter2a18a522015-05-15 17:17:47 +0200380
Callum Lowcay7e08e902011-01-07 19:47:02 +0000381 return len;
382}
383
Callum Lowcay30eeae52011-01-07 19:46:55 +0000384struct terminal_color { double r, g, b, a; };
385struct attr {
386 unsigned char fg, bg;
387 char a; /* attributes format:
388 * 76543210
Callum Lowcay81179db2011-01-10 12:14:01 +1300389 * cilub */
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500390 char s; /* in selection */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000391};
392struct color_scheme {
393 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500394 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000395 struct attr default_attr;
396};
397
398static void
399attr_init(struct attr *data_attr, struct attr attr, int n)
400{
401 int i;
402 for (i = 0; i < n; i++) {
403 data_attr[i] = attr;
404 }
405}
406
Callum Lowcay67a201d2011-01-12 19:23:41 +1300407enum escape_state {
408 escape_state_normal = 0,
409 escape_state_escape,
410 escape_state_dcs,
411 escape_state_csi,
412 escape_state_osc,
413 escape_state_inner_escape,
414 escape_state_ignore,
415 escape_state_special
416};
417
418#define ESC_FLAG_WHAT 0x01
419#define ESC_FLAG_GT 0x02
420#define ESC_FLAG_BANG 0x04
421#define ESC_FLAG_CASH 0x08
422#define ESC_FLAG_SQUOTE 0x10
423#define ESC_FLAG_DQUOTE 0x20
424#define ESC_FLAG_SPACE 0x40
425
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400426enum {
427 SELECT_NONE,
428 SELECT_CHAR,
429 SELECT_WORD,
430 SELECT_LINE
431};
432
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500433struct terminal {
434 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500435 struct widget *widget;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500436 struct display *display;
Kristian Høgsberga83be202013-10-23 20:47:35 -0700437 char *title;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000438 union utf8_char *data;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400439 struct task io_task;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000440 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000441 struct attr *data_attr;
442 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000443 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000444 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000445 char saved_origin_mode;
446 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000447 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000448 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000449 character_set cs, g0, g1;
450 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000451 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000452 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700453 int width, height, row, column, max_width;
454 uint32_t buffer_height;
455 uint32_t start, end, saved_start, log_size;
Magnus Hoff1046f122014-08-05 15:05:59 +0200456 wl_fixed_t smooth_scroll;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000457 int saved_row, saved_column;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700458 int scrolling;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600459 int send_cursor_position;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500460 int fd, master;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500461 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300462 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500463 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300464 enum escape_state state;
465 enum escape_state outer_state;
466 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000467 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500468 int margin;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400469 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000470 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400471 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +0800472 double average_width;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500473 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg88fd4082012-06-21 15:55:03 -0400474 uint32_t hide_cursor_serial;
Kristian Høgsbergb405a802014-02-05 14:44:04 -0800475 int size_in_title;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500476
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400477 struct wl_data_source *selection;
Xiong Zhang49c6aee2013-11-25 18:42:52 +0800478 uint32_t click_time;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400479 int dragging, click_count;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500480 int selection_start_x, selection_start_y;
481 int selection_end_x, selection_end_y;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400482 int selection_start_row, selection_start_col;
483 int selection_end_row, selection_end_col;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -0400484 struct wl_list link;
Derek Foreman88353dd2017-03-24 16:29:31 -0500485 int pace_pipe;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500486};
487
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000488/* Create default tab stops, every 8 characters */
489static void
490terminal_init_tabs(struct terminal *terminal)
491{
492 int i = 0;
Michael Vetter2a18a522015-05-15 17:17:47 +0200493
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000494 while (i < terminal->width) {
495 if (i % 8 == 0)
496 terminal->tab_ruler[i] = 1;
497 else
498 terminal->tab_ruler[i] = 0;
499 i++;
500 }
501}
502
Callum Lowcay30eeae52011-01-07 19:46:55 +0000503static void
504terminal_init(struct terminal *terminal)
505{
506 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000507 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000508 terminal->mode = MODE_SHOW_CURSOR |
509 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000510 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000511 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000512
513 terminal->row = 0;
514 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000515
Callum Lowcay256e72f2011-01-07 19:47:01 +0000516 terminal->g0 = CS_US;
517 terminal->g1 = CS_US;
518 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000519 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000520
521 terminal->saved_g0 = terminal->g0;
522 terminal->saved_g1 = terminal->g1;
523 terminal->saved_cs = terminal->cs;
524
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000525 terminal->saved_attr = terminal->curr_attr;
526 terminal->saved_origin_mode = terminal->origin_mode;
527 terminal->saved_row = terminal->row;
528 terminal->saved_column = terminal->column;
529
530 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000531}
532
533static void
534init_color_table(struct terminal *terminal)
535{
536 int c, r;
537 struct terminal_color *color_table = terminal->color_table;
538
539 for (c = 0; c < 256; c ++) {
540 if (c < 16) {
541 color_table[c] = terminal->color_scheme->palette[c];
542 } else if (c < 232) {
543 r = c - 16;
544 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
545 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
546 color_table[c].r = ((double)(r % 6) / 6.0);
547 color_table[c].a = 1.0;
548 } else {
549 r = (c - 232) * 10 + 8;
550 color_table[c].r = ((double) r) / 256.0;
551 color_table[c].g = color_table[c].r;
552 color_table[c].b = color_table[c].r;
553 color_table[c].a = 1.0;
554 }
555 }
556}
557
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000558static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500559terminal_get_row(struct terminal *terminal, int row)
560{
561 int index;
562
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700563 index = (row + terminal->start) & (terminal->buffer_height - 1);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500564
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700565 return (void *) terminal->data + index * terminal->data_pitch;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500566}
567
Callum Lowcay30eeae52011-01-07 19:46:55 +0000568static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500569terminal_get_attr_row(struct terminal *terminal, int row)
570{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000571 int index;
572
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700573 index = (row + terminal->start) & (terminal->buffer_height - 1);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000574
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700575 return (void *) terminal->data_attr + index * terminal->attr_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000576}
577
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500578union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300579 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500580 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500581};
582
583static void
584terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500585 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500586{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500587 struct attr attr;
588 int foreground, background, tmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500589
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500590 decoded->attr.s = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400591 if (((row == terminal->selection_start_row &&
592 col >= terminal->selection_start_col) ||
593 row > terminal->selection_start_row) &&
594 ((row == terminal->selection_end_row &&
595 col < terminal->selection_end_col) ||
596 row < terminal->selection_end_row))
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500597 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500598
599 /* get the attributes for this character cell */
600 attr = terminal_get_attr_row(terminal, row)[col];
601 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500602 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500603 ((terminal->mode & MODE_SHOW_CURSOR) &&
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400604 window_has_focus(terminal->window) && terminal->row == row &&
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500605 terminal->column == col)) {
606 foreground = attr.bg;
607 background = attr.fg;
608 if (attr.a & ATTRMASK_BOLD) {
609 if (foreground <= 16) foreground |= 0x08;
610 if (background <= 16) background &= 0x07;
611 }
612 } else {
613 foreground = attr.fg;
614 background = attr.bg;
615 }
616
617 if (terminal->mode & MODE_INVERSE) {
618 tmp = foreground;
619 foreground = background;
620 background = tmp;
621 if (attr.a & ATTRMASK_BOLD) {
622 if (foreground <= 16) foreground |= 0x08;
623 if (background <= 16) background &= 0x07;
624 }
625 }
626
Callum Lowcay9d708b02011-01-12 20:06:17 +1300627 decoded->attr.fg = foreground;
628 decoded->attr.bg = background;
629 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000630}
631
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500632
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500633static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000634terminal_scroll_buffer(struct terminal *terminal, int d)
635{
636 int i;
637
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700638 terminal->start += d;
639 if (d < 0) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000640 d = 0 - d;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700641 for (i = 0; i < d; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000642 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
643 attr_init(terminal_get_attr_row(terminal, i),
644 terminal->curr_attr, terminal->width);
645 }
646 } else {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700647 for (i = terminal->height - d; i < terminal->height; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000648 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
649 attr_init(terminal_get_attr_row(terminal, i),
650 terminal->curr_attr, terminal->width);
651 }
652 }
Kristian Høgsberg18e928d2012-06-27 19:29:41 -0400653
654 terminal->selection_start_row -= d;
655 terminal->selection_end_row -= d;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000656}
657
658static void
659terminal_scroll_window(struct terminal *terminal, int d)
660{
661 int i;
662 int window_height;
663 int from_row, to_row;
Michael Vetter2a18a522015-05-15 17:17:47 +0200664
Callum Lowcaybbeac602011-01-07 19:46:58 +0000665 // scrolling range is inclusive
666 window_height = terminal->margin_bottom - terminal->margin_top + 1;
667 d = d % (window_height + 1);
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300668 if (d < 0) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000669 d = 0 - d;
670 to_row = terminal->margin_bottom;
671 from_row = terminal->margin_bottom - d;
Michael Vetter2a18a522015-05-15 17:17:47 +0200672
Callum Lowcaybbeac602011-01-07 19:46:58 +0000673 for (i = 0; i < (window_height - d); i++) {
674 memcpy(terminal_get_row(terminal, to_row - i),
675 terminal_get_row(terminal, from_row - i),
676 terminal->data_pitch);
677 memcpy(terminal_get_attr_row(terminal, to_row - i),
678 terminal_get_attr_row(terminal, from_row - i),
679 terminal->attr_pitch);
680 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000681 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
682 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000683 attr_init(terminal_get_attr_row(terminal, i),
684 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000685 }
686 } else {
687 to_row = terminal->margin_top;
688 from_row = terminal->margin_top + d;
Michael Vetter2a18a522015-05-15 17:17:47 +0200689
Callum Lowcaybbeac602011-01-07 19:46:58 +0000690 for (i = 0; i < (window_height - d); i++) {
691 memcpy(terminal_get_row(terminal, to_row + i),
692 terminal_get_row(terminal, from_row + i),
693 terminal->data_pitch);
694 memcpy(terminal_get_attr_row(terminal, to_row + i),
695 terminal_get_attr_row(terminal, from_row + i),
696 terminal->attr_pitch);
697 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000698 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
699 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000700 attr_init(terminal_get_attr_row(terminal, i),
701 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000702 }
703 }
704}
705
706static void
707terminal_scroll(struct terminal *terminal, int d)
708{
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300709 if (terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
Callum Lowcaybbeac602011-01-07 19:46:58 +0000710 terminal_scroll_buffer(terminal, d);
711 else
712 terminal_scroll_window(terminal, d);
713}
714
715static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000716terminal_shift_line(struct terminal *terminal, int d)
717{
718 union utf8_char *row;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500719 struct attr *attr_row;
Michael Vetter2a18a522015-05-15 17:17:47 +0200720
Callum Lowcay69e96582011-01-07 19:47:00 +0000721 row = terminal_get_row(terminal, terminal->row);
722 attr_row = terminal_get_attr_row(terminal, terminal->row);
723
724 if ((terminal->width + d) <= terminal->column)
725 d = terminal->column + 1 - terminal->width;
726 if ((terminal->column + d) >= terminal->width)
727 d = terminal->width - terminal->column - 1;
Michael Vetter2a18a522015-05-15 17:17:47 +0200728
Callum Lowcay69e96582011-01-07 19:47:00 +0000729 if (d < 0) {
730 d = 0 - d;
731 memmove(&row[terminal->column],
732 &row[terminal->column + d],
733 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
Callum Lowcay69e96582011-01-07 19:47:00 +0000734 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
735 (terminal->width - terminal->column - d) * sizeof(struct attr));
736 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
737 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
738 } else {
739 memmove(&row[terminal->column + d], &row[terminal->column],
740 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
741 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
742 (terminal->width - terminal->column - d) * sizeof(struct attr));
743 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
744 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
745 }
746}
747
748static void
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700749terminal_resize_cells(struct terminal *terminal,
750 int width, int height)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500751{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000752 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000753 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000754 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000755 int data_pitch, attr_pitch;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500756 int i, l, total_rows;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700757 uint32_t d, uheight = height;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500758 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000759 struct winsize ws;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500760
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700761 if (uheight > terminal->buffer_height)
762 height = terminal->buffer_height;
763
Kristian Høgsberg22106762008-12-08 13:50:07 -0500764 if (terminal->width == width && terminal->height == height)
765 return;
766
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700767 if (terminal->data && width <= terminal->max_width) {
768 d = 0;
769 if (height < terminal->height && height <= terminal->row)
770 d = terminal->height - height;
771 else if (height > terminal->height &&
772 terminal->height - 1 == terminal->row) {
773 d = terminal->height - height;
774 if (terminal->log_size < uheight)
775 d = -terminal->start;
776 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500777
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700778 terminal->start += d;
779 terminal->row -= d;
780 } else {
781 terminal->max_width = width;
782 data_pitch = width * sizeof(union utf8_char);
Bryce W. Harrington66cd2c12014-04-28 18:44:08 +0000783 data = xzalloc(data_pitch * terminal->buffer_height);
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700784 attr_pitch = width * sizeof(struct attr);
Bryce W. Harrington66cd2c12014-04-28 18:44:08 +0000785 data_attr = xmalloc(attr_pitch * terminal->buffer_height);
786 tab_ruler = xzalloc(width);
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700787 attr_init(data_attr, terminal->curr_attr,
788 width * terminal->buffer_height);
789
790 if (terminal->data && terminal->data_attr) {
791 if (width > terminal->width)
792 l = terminal->width;
793 else
794 l = width;
795
796 if (terminal->height > height) {
797 total_rows = height;
798 i = 1 + terminal->row - height;
799 if (i > 0) {
800 terminal->start += i;
801 terminal->row = terminal->row - i;
802 }
803 } else {
804 total_rows = terminal->height;
José Bollo4a4704a2013-09-13 11:28:51 +0200805 }
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700806
807 for (i = 0; i < total_rows; i++) {
808 memcpy(&data[width * i],
809 terminal_get_row(terminal, i),
810 l * sizeof(union utf8_char));
811 memcpy(&data_attr[width * i],
812 terminal_get_attr_row(terminal, i),
813 l * sizeof(struct attr));
814 }
815
816 free(terminal->data);
817 free(terminal->data_attr);
818 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500819 }
820
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700821 terminal->data_pitch = data_pitch;
822 terminal->attr_pitch = attr_pitch;
823 terminal->data = data;
824 terminal->data_attr = data_attr;
825 terminal->tab_ruler = tab_ruler;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700826 terminal->start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500827 }
828
Callum Lowcay86653ed2011-01-07 19:47:03 +0000829 terminal->margin_bottom =
830 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500831 terminal->width = width;
832 terminal->height = height;
Kristian Høgsbergdcfff552013-11-22 22:43:20 -0800833 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500834
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000835 /* Update the window size */
836 ws.ws_row = terminal->height;
837 ws.ws_col = terminal->width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500838 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500839 ws.ws_xpixel = allocation.width;
840 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000841 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500842}
843
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500844static void
Jasper St. Pierrede680992014-04-10 17:23:49 -0700845update_title(struct terminal *terminal)
846{
847 if (window_is_resizing(terminal->window)) {
848 char *p;
849 if (asprintf(&p, "%s — [%dx%d]", terminal->title, terminal->width, terminal->height) > 0) {
850 window_set_title(terminal->window, p);
851 free(p);
852 }
853 } else {
854 window_set_title(terminal->window, terminal->title);
855 }
856}
857
858static void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500859resize_handler(struct widget *widget,
860 int32_t width, int32_t height, void *data)
861{
862 struct terminal *terminal = data;
863 int32_t columns, rows, m;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700864
Derek Foreman88353dd2017-03-24 16:29:31 -0500865 if (terminal->pace_pipe >= 0) {
866 close(terminal->pace_pipe);
867 terminal->pace_pipe = -1;
868 }
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500869 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800870 columns = (width - m) / (int32_t) terminal->average_width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500871 rows = (height - m) / (int32_t) terminal->extents.height;
872
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400873 if (!window_is_fullscreen(terminal->window) &&
874 !window_is_maximized(terminal->window)) {
Peng Wuf291f202013-06-06 15:32:41 +0800875 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500876 height = rows * terminal->extents.height + m;
877 widget_set_size(terminal->widget, width, height);
878 }
879
880 terminal_resize_cells(terminal, columns, rows);
Jasper St. Pierrede680992014-04-10 17:23:49 -0700881 update_title(terminal);
882}
883
884static void
885state_changed_handler(struct window *window, void *data)
886{
887 struct terminal *terminal = data;
888 update_title(terminal);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500889}
890
891static void
892terminal_resize(struct terminal *terminal, int columns, int rows)
893{
894 int32_t width, height, m;
895
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400896 if (window_is_fullscreen(terminal->window) ||
897 window_is_maximized(terminal->window))
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500898 return;
899
900 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800901 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500902 height = rows * terminal->extents.height + m;
Kristian Høgsberga1627922012-06-20 17:30:03 -0400903
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500904 window_frame_set_child_size(terminal->widget, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500905}
906
Callum Lowcay30eeae52011-01-07 19:46:55 +0000907struct color_scheme DEFAULT_COLORS = {
908 {
909 {0, 0, 0, 1}, /* black */
910 {0.66, 0, 0, 1}, /* red */
911 {0 , 0.66, 0, 1}, /* green */
912 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
913 {0 , 0 , 0.66, 1}, /* blue */
914 {0.66, 0 , 0.66, 1}, /* magenta */
915 {0, 0.66, 0.66, 1}, /* cyan */
916 {0.66, 0.66, 0.66, 1}, /* light grey */
917 {0.22, 0.33, 0.33, 1}, /* dark grey */
918 {1, 0.33, 0.33, 1}, /* high red */
919 {0.33, 1, 0.33, 1}, /* high green */
920 {1, 1, 0.33, 1}, /* high yellow */
921 {0.33, 0.33, 1, 1}, /* high blue */
922 {1, 0.33, 1, 1}, /* high magenta */
923 {0.33, 1, 1, 1}, /* high cyan */
924 {1, 1, 1, 1} /* white */
925 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500926 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000927 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
928};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400929
Kristian Høgsberg22106762008-12-08 13:50:07 -0500930static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500931terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
932{
933 cairo_set_source_rgba(cr,
934 terminal->color_table[index].r,
935 terminal->color_table[index].g,
936 terminal->color_table[index].b,
937 terminal->color_table[index].a);
938}
939
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500940static void
941terminal_send_selection(struct terminal *terminal, int fd)
942{
943 int row, col;
944 union utf8_char *p_row;
945 union decoded_attr attr;
946 FILE *fp;
947 int len;
948
949 fp = fdopen(fd, "w");
Brian Lovin1bf14812013-08-08 16:12:55 -0700950 if (fp == NULL){
951 close(fd);
952 return;
953 }
Manuel Bachmanne54cee12015-09-19 13:51:05 +0200954 for (row = terminal->selection_start_row; row < terminal->height; row++) {
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500955 p_row = terminal_get_row(terminal, row);
956 for (col = 0; col < terminal->width; col++) {
Peng Wucfcc1112013-08-19 10:59:21 +0800957 if (p_row[col].ch == 0x200B) /* space glyph */
958 continue;
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500959 /* get the attributes for this character cell */
960 terminal_decode_attr(terminal, row, col, &attr);
961 if (!attr.attr.s)
962 continue;
963 len = strnlen((char *) p_row[col].byte, 4);
Kristian Høgsberg0dee6472012-07-01 21:25:41 -0400964 if (len > 0)
965 fwrite(p_row[col].byte, 1, len, fp);
966 if (len == 0 || col == terminal->width - 1) {
967 fwrite("\n", 1, 1, fp);
968 break;
969 }
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500970 }
971 }
972 fclose(fp);
973}
974
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500975struct glyph_run {
976 struct terminal *terminal;
977 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400978 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500979 union decoded_attr attr;
980 cairo_glyph_t glyphs[256], *g;
981};
982
983static void
984glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
985{
986 run->terminal = terminal;
987 run->cr = cr;
988 run->g = run->glyphs;
989 run->count = 0;
990 run->attr.key = 0;
991}
992
993static void
994glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
995{
996 cairo_scaled_font_t *font;
997
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500998 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
999 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +13001000 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001001 font = run->terminal->font_bold;
1002 else
1003 font = run->terminal->font_normal;
1004 cairo_set_scaled_font(run->cr, font);
1005 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +13001006 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001007
Callum Lowcay9d708b02011-01-12 20:06:17 +13001008 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
1009 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001010 run->g = run->glyphs;
1011 run->count = 0;
1012 }
Callum Lowcay9d708b02011-01-12 20:06:17 +13001013 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001014}
1015
1016static void
1017glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
1018{
1019 int num_glyphs;
1020 cairo_scaled_font_t *font;
1021
1022 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
1023
Callum Lowcay9d708b02011-01-12 20:06:17 +13001024 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001025 font = run->terminal->font_bold;
1026 else
1027 font = run->terminal->font_normal;
1028
1029 cairo_move_to(run->cr, x, y);
1030 cairo_scaled_font_text_to_glyphs (font, x, y,
1031 (char *) c->byte, 4,
1032 &run->g, &num_glyphs,
1033 NULL, NULL, NULL);
1034 run->g += num_glyphs;
1035 run->count += num_glyphs;
1036}
1037
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001038
Kristian Høgsbergf106fd52011-01-11 10:11:39 -05001039static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001040redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001041{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001042 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001043 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001044 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001045 int top_margin, side_margin;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001046 int row, col, cursor_x, cursor_y;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001047 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001048 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001049 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001050 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001051 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001052 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -05001053 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +08001054 double average_width;
Peng Wucfcc1112013-08-19 10:59:21 +08001055 double unichar_width;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001056
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001057 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001058 widget_get_allocation(terminal->widget, &allocation);
Alexander Larssonde79dd02013-05-22 14:41:34 +02001059 cr = widget_cairo_create(terminal->widget);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001060 cairo_rectangle(cr, allocation.x, allocation.y,
1061 allocation.width, allocation.height);
1062 cairo_clip(cr);
1063 cairo_push_group(cr);
1064
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001065 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -05001066 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001067 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001068
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05001069 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001070
Kristian Høgsberg59826582011-01-20 11:56:57 -05001071 extents = terminal->extents;
Peng Wuf291f202013-06-06 15:32:41 +08001072 average_width = terminal->average_width;
1073 side_margin = (allocation.width - terminal->width * average_width) / 2;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001074 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001075
Callum Lowcay30eeae52011-01-07 19:46:55 +00001076 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001077 cairo_translate(cr, allocation.x + side_margin,
1078 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001079 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001080 for (row = 0; row < terminal->height; row++) {
Peng Wucfcc1112013-08-19 10:59:21 +08001081 p_row = terminal_get_row(terminal, row);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001082 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001083 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -05001084 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001085
Callum Lowcay9d708b02011-01-12 20:06:17 +13001086 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -05001087 continue;
1088
Peng Wucfcc1112013-08-19 10:59:21 +08001089 if (is_wide(p_row[col]))
1090 unichar_width = 2 * average_width;
1091 else
1092 unichar_width = average_width;
1093
Callum Lowcay9d708b02011-01-12 20:06:17 +13001094 terminal_set_color(terminal, cr, attr.attr.bg);
Peng Wuf291f202013-06-06 15:32:41 +08001095 cairo_move_to(cr, col * average_width,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001096 row * extents.height);
Peng Wucfcc1112013-08-19 10:59:21 +08001097 cairo_rel_line_to(cr, unichar_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001098 cairo_rel_line_to(cr, 0, extents.height);
Peng Wucfcc1112013-08-19 10:59:21 +08001099 cairo_rel_line_to(cr, -unichar_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001100 cairo_close_path(cr);
1101 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001102 }
1103 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001104
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001105 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1106
1107 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001108 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001109 for (row = 0; row < terminal->height; row++) {
1110 p_row = terminal_get_row(terminal, row);
1111 for (col = 0; col < terminal->width; col++) {
1112 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -05001113 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001114
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001115 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001116
Peng Wuf291f202013-06-06 15:32:41 +08001117 text_x = col * average_width;
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001118 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +13001119 if (attr.attr.a & ATTRMASK_UNDERLINE) {
1120 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +00001121 cairo_move_to(cr, text_x, (double)text_y + 1.5);
Peng Wuf291f202013-06-06 15:32:41 +08001122 cairo_line_to(cr, text_x + average_width, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001123 cairo_stroke(cr);
1124 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -05001125
Daiki Ueno56d8a7a2014-04-08 18:46:18 +09001126 /* skip space glyph (RLE) we use as a placeholder of
1127 the right half of a double-width character,
1128 because RLE is not available in every font. */
1129 if (p_row[col].ch == 0x200B)
1130 continue;
1131
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001132 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001133 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001134 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001135
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001136 attr.key = ~0;
1137 glyph_run_flush(&run, attr);
1138
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001139 if ((terminal->mode & MODE_SHOW_CURSOR) &&
1140 !window_has_focus(terminal->window)) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001141 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001142
Callum Lowcay30eeae52011-01-07 19:46:55 +00001143 cairo_set_line_width(cr, 1);
Peng Wuf291f202013-06-06 15:32:41 +08001144 cairo_move_to(cr, terminal->column * average_width + d,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001145 terminal->row * extents.height + d);
Peng Wuf291f202013-06-06 15:32:41 +08001146 cairo_rel_line_to(cr, average_width - 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001147 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
Peng Wuf291f202013-06-06 15:32:41 +08001148 cairo_rel_line_to(cr, -average_width + 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001149 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001150
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001151 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001152 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001153
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001154 cairo_pop_group_to_source(cr);
1155 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001156 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001157 cairo_surface_destroy(surface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001158
1159 if (terminal->send_cursor_position) {
1160 cursor_x = side_margin + allocation.x +
Peng Wuf291f202013-06-06 15:32:41 +08001161 terminal->column * average_width;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001162 cursor_y = top_margin + allocation.y +
1163 terminal->row * extents.height;
1164 window_set_text_cursor_position(terminal->window,
1165 cursor_x, cursor_y);
1166 terminal->send_cursor_position = 0;
1167 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001168}
1169
1170static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001171terminal_write(struct terminal *terminal, const char *data, size_t length)
1172{
1173 if (write(terminal->master, data, length) < 0)
1174 abort();
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001175 terminal->send_cursor_position = 1;
Kristian Høgsberg26130862011-08-24 11:30:21 -04001176}
1177
1178static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001179terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001180
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001181static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001182handle_char(struct terminal *terminal, union utf8_char utf8);
1183
1184static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001185handle_sgr(struct terminal *terminal, int code);
1186
1187static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001188handle_term_parameter(struct terminal *terminal, int code, int sr)
1189{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001190 int i;
1191
Callum Lowcay67a201d2011-01-12 19:23:41 +13001192 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001193 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001194 case 1: /* DECCKM */
1195 if (sr) terminal->key_mode = KM_APPLICATION;
1196 else terminal->key_mode = KM_NORMAL;
1197 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001198 case 2: /* DECANM */
1199 /* No VT52 support yet */
1200 terminal->g0 = CS_US;
1201 terminal->g1 = CS_US;
1202 terminal->cs = terminal->g0;
1203 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001204 case 3: /* DECCOLM */
1205 if (sr)
1206 terminal_resize(terminal, 132, 24);
1207 else
1208 terminal_resize(terminal, 80, 24);
Michael Vetter2a18a522015-05-15 17:17:47 +02001209
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001210 /* set columns, but also home cursor and clear screen */
1211 terminal->row = 0; terminal->column = 0;
1212 for (i = 0; i < terminal->height; i++) {
1213 memset(terminal_get_row(terminal, i),
1214 0, terminal->data_pitch);
1215 attr_init(terminal_get_attr_row(terminal, i),
1216 terminal->curr_attr, terminal->width);
1217 }
1218 break;
1219 case 5: /* DECSCNM */
1220 if (sr) terminal->mode |= MODE_INVERSE;
1221 else terminal->mode &= ~MODE_INVERSE;
1222 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001223 case 6: /* DECOM */
1224 terminal->origin_mode = sr;
1225 if (terminal->origin_mode)
1226 terminal->row = terminal->margin_top;
1227 else
1228 terminal->row = 0;
1229 terminal->column = 0;
1230 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001231 case 7: /* DECAWM */
1232 if (sr) terminal->mode |= MODE_AUTOWRAP;
1233 else terminal->mode &= ~MODE_AUTOWRAP;
1234 break;
1235 case 8: /* DECARM */
1236 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1237 else terminal->mode &= ~MODE_AUTOREPEAT;
1238 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001239 case 12: /* Very visible cursor (CVVIS) */
1240 /* FIXME: What do we do here. */
1241 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001242 case 25:
1243 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1244 else terminal->mode &= ~MODE_SHOW_CURSOR;
1245 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001246 case 1034: /* smm/rmm, meta mode on/off */
1247 /* ignore */
1248 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001249 case 1037: /* deleteSendsDel */
1250 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1251 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1252 break;
1253 case 1039: /* altSendsEscape */
1254 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1255 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1256 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001257 case 1049: /* rmcup/smcup, alternate screen */
1258 /* Ignore. Should be possible to implement,
1259 * but it's kind of annoying. */
1260 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001261 default:
1262 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1263 break;
1264 }
1265 } else {
1266 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001267 case 4: /* IRM */
1268 if (sr) terminal->mode |= MODE_IRM;
1269 else terminal->mode &= ~MODE_IRM;
1270 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001271 case 20: /* LNM */
1272 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1273 else terminal->mode &= ~MODE_LF_NEWLINE;
1274 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001275 default:
1276 fprintf(stderr, "Unknown parameter: %d\n", code);
1277 break;
1278 }
1279 }
1280}
1281
1282static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001283handle_dcs(struct terminal *terminal)
1284{
1285}
1286
1287static void
1288handle_osc(struct terminal *terminal)
1289{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001290 char *p;
1291 int code;
1292
1293 terminal->escape[terminal->escape_length++] = '\0';
1294 p = &terminal->escape[2];
1295 code = strtol(p, &p, 10);
1296 if (*p == ';') p++;
1297
1298 switch (code) {
1299 case 0: /* Icon name and window title */
1300 case 1: /* Icon label */
1301 case 2: /* Window title*/
Kristian Høgsberga83be202013-10-23 20:47:35 -07001302 free(terminal->title);
1303 terminal->title = strdup(p);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001304 window_set_title(terminal->window, p);
1305 break;
Kristian Høgsberg8a205002013-09-11 11:52:56 -07001306 case 7: /* shell cwd as uri */
1307 break;
Dima Ryazanovc3b63722016-11-03 23:46:02 -07001308 case 777: /* Desktop notifications */
1309 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001310 default:
Kristian Høgsberg8a205002013-09-11 11:52:56 -07001311 fprintf(stderr, "Unknown OSC escape code %d, text %s\n",
1312 code, p);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001313 break;
1314 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001315}
1316
1317static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001318handle_escape(struct terminal *terminal)
1319{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001320 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001321 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001322 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001323 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001324 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001325 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001326 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001327
1328 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001329 i = 0;
1330 p = &terminal->escape[2];
1331 while ((isdigit(*p) || *p == ';') && i < 10) {
1332 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001333 if (!set[i]) {
1334 args[i] = 0;
1335 set[i] = 1;
1336 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001337 p++;
1338 i++;
1339 } else {
1340 args[i] = strtol(p, &p, 10);
1341 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001342 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001343 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001344
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001345 switch (*p) {
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001346 case '@': /* ICH - Insert <count> blank characters */
Callum Lowcay69e96582011-01-07 19:47:00 +00001347 count = set[0] ? args[0] : 1;
1348 if (count == 0) count = 1;
1349 terminal_shift_line(terminal, count);
1350 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001351 case 'A': /* CUU - Move cursor up <count> rows */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001352 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001353 if (count == 0) count = 1;
1354 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001355 terminal->row -= count;
1356 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001357 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001358 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001359 case 'B': /* CUD - Move cursor down <count> rows */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001360 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001361 if (count == 0) count = 1;
1362 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001363 terminal->row += count;
1364 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001365 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001366 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001367 case 'C': /* CUF - Move cursor right by <count> columns */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001368 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001369 if (count == 0) count = 1;
1370 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001371 terminal->column += count;
1372 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001373 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001374 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001375 case 'D': /* CUB - Move cursor left <count> columns */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001376 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001377 if (count == 0) count = 1;
1378 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001379 terminal->column -= count;
1380 else
1381 terminal->column = 0;
1382 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001383 case 'E': /* CNL - Move cursor down <count> rows, to column 1 */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001384 count = set[0] ? args[0] : 1;
1385 if (terminal->row + count <= terminal->margin_bottom)
1386 terminal->row += count;
1387 else
1388 terminal->row = terminal->margin_bottom;
1389 terminal->column = 0;
1390 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001391 case 'F': /* CPL - Move cursour up <count> rows, to column 1 */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001392 count = set[0] ? args[0] : 1;
1393 if (terminal->row - count >= terminal->margin_top)
1394 terminal->row -= count;
1395 else
1396 terminal->row = terminal->margin_top;
1397 terminal->column = 0;
1398 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001399 case 'G': /* CHA - Move cursor to column <y> in current row */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001400 y = set[0] ? args[0] : 1;
1401 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
Michael Vetter2a18a522015-05-15 17:17:47 +02001402
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001403 terminal->column = y - 1;
1404 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001405 case 'f': /* HVP - Move cursor to <x, y> */
1406 case 'H': /* CUP - Move cursor to <x, y> (origin at 1,1) */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001407 x = (set[1] ? args[1] : 1) - 1;
1408 x = x < 0 ? 0 :
1409 (x >= terminal->width ? terminal->width - 1 : x);
Michael Vetter2a18a522015-05-15 17:17:47 +02001410
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001411 y = (set[0] ? args[0] : 1) - 1;
1412 if (terminal->origin_mode) {
1413 y += terminal->margin_top;
1414 y = y < terminal->margin_top ? terminal->margin_top :
1415 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1416 } else {
1417 y = y < 0 ? 0 :
1418 (y >= terminal->height ? terminal->height - 1 : y);
1419 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001420
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001421 terminal->row = y;
1422 terminal->column = x;
1423 break;
1424 case 'I': /* CHT */
1425 count = set[0] ? args[0] : 1;
1426 if (count == 0) count = 1;
1427 while (count > 0 && terminal->column < terminal->width) {
1428 if (terminal->tab_ruler[terminal->column]) count--;
1429 terminal->column++;
1430 }
1431 terminal->column--;
1432 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001433 case 'J': /* ED - Erase display */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001434 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001435 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001436 if (!set[0] || args[0] == 0 || args[0] > 2) {
1437 memset(&row[terminal->column],
1438 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1439 attr_init(&attr_row[terminal->column],
1440 terminal->curr_attr, terminal->width - terminal->column);
1441 for (i = terminal->row + 1; i < terminal->height; i++) {
1442 memset(terminal_get_row(terminal, i),
1443 0, terminal->data_pitch);
1444 attr_init(terminal_get_attr_row(terminal, i),
1445 terminal->curr_attr, terminal->width);
1446 }
1447 } else if (args[0] == 1) {
1448 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1449 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1450 for (i = 0; i < terminal->row; i++) {
1451 memset(terminal_get_row(terminal, i),
1452 0, terminal->data_pitch);
1453 attr_init(terminal_get_attr_row(terminal, i),
1454 terminal->curr_attr, terminal->width);
1455 }
1456 } else if (args[0] == 2) {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07001457 /* Clear screen by scrolling contents out */
1458 terminal_scroll_buffer(terminal,
1459 terminal->end - terminal->start);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001460 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001461 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001462 case 'K': /* EL - Erase line */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001463 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001464 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001465 if (!set[0] || args[0] == 0 || args[0] > 2) {
1466 memset(&row[terminal->column], 0,
1467 (terminal->width - terminal->column) * sizeof(union utf8_char));
1468 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1469 terminal->width - terminal->column);
1470 } else if (args[0] == 1) {
1471 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1472 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1473 } else if (args[0] == 2) {
1474 memset(row, 0, terminal->data_pitch);
1475 attr_init(attr_row, terminal->curr_attr, terminal->width);
1476 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001477 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001478 case 'L': /* IL - Insert <count> blank lines */
Callum Lowcay69e96582011-01-07 19:47:00 +00001479 count = set[0] ? args[0] : 1;
1480 if (count == 0) count = 1;
1481 if (terminal->row >= terminal->margin_top &&
1482 terminal->row < terminal->margin_bottom)
1483 {
1484 top = terminal->margin_top;
1485 terminal->margin_top = terminal->row;
1486 terminal_scroll(terminal, 0 - count);
1487 terminal->margin_top = top;
1488 } else if (terminal->row == terminal->margin_bottom) {
1489 memset(terminal_get_row(terminal, terminal->row),
1490 0, terminal->data_pitch);
1491 attr_init(terminal_get_attr_row(terminal, terminal->row),
1492 terminal->curr_attr, terminal->width);
1493 }
1494 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001495 case 'M': /* DL - Delete <count> lines */
Callum Lowcay69e96582011-01-07 19:47:00 +00001496 count = set[0] ? args[0] : 1;
1497 if (count == 0) count = 1;
1498 if (terminal->row >= terminal->margin_top &&
1499 terminal->row < terminal->margin_bottom)
1500 {
1501 top = terminal->margin_top;
1502 terminal->margin_top = terminal->row;
1503 terminal_scroll(terminal, count);
1504 terminal->margin_top = top;
1505 } else if (terminal->row == terminal->margin_bottom) {
1506 memset(terminal_get_row(terminal, terminal->row),
1507 0, terminal->data_pitch);
1508 }
1509 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001510 case 'P': /* DCH - Delete <count> characters on current line */
Callum Lowcay69e96582011-01-07 19:47:00 +00001511 count = set[0] ? args[0] : 1;
1512 if (count == 0) count = 1;
1513 terminal_shift_line(terminal, 0 - count);
1514 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001515 case 'S': /* SU */
1516 terminal_scroll(terminal, set[0] ? args[0] : 1);
1517 break;
1518 case 'T': /* SD */
1519 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1520 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001521 case 'X': /* ECH - Erase <count> characters on current line */
Callum Lowcay69e96582011-01-07 19:47:00 +00001522 count = set[0] ? args[0] : 1;
1523 if (count == 0) count = 1;
1524 if ((terminal->column + count) > terminal->width)
1525 count = terminal->width - terminal->column;
1526 row = terminal_get_row(terminal, terminal->row);
1527 attr_row = terminal_get_attr_row(terminal, terminal->row);
1528 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1529 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1530 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001531 case 'Z': /* CBT */
1532 count = set[0] ? args[0] : 1;
1533 if (count == 0) count = 1;
1534 while (count > 0 && terminal->column >= 0) {
1535 if (terminal->tab_ruler[terminal->column]) count--;
1536 terminal->column--;
1537 }
1538 terminal->column++;
1539 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001540 case '`': /* HPA - Move cursor to <y> column in current row */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001541 y = set[0] ? args[0] : 1;
1542 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
Michael Vetter2a18a522015-05-15 17:17:47 +02001543
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001544 terminal->column = y - 1;
1545 break;
1546 case 'b': /* REP */
1547 count = set[0] ? args[0] : 1;
1548 if (count == 0) count = 1;
1549 if (terminal->last_char.byte[0])
1550 for (i = 0; i < count; i++)
1551 handle_char(terminal, terminal->last_char);
1552 terminal->last_char.byte[0] = 0;
1553 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001554 case 'c': /* Primary DA - Answer "I am a VT102" */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001555 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001556 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001557 case 'd': /* VPA - Move cursor to <x> row, current column */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001558 x = set[0] ? args[0] : 1;
1559 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
Michael Vetter2a18a522015-05-15 17:17:47 +02001560
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001561 terminal->row = x - 1;
1562 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001563 case 'g': /* TBC - Clear tab stop(s) */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001564 if (!set[0] || args[0] == 0) {
1565 terminal->tab_ruler[terminal->column] = 0;
1566 } else if (args[0] == 3) {
1567 memset(terminal->tab_ruler, 0, terminal->width);
1568 }
1569 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001570 case 'h': /* SM - Set mode */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001571 for (i = 0; i < 10 && set[i]; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001572 handle_term_parameter(terminal, args[i], 1);
1573 }
1574 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001575 case 'l': /* RM - Reset mode */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001576 for (i = 0; i < 10 && set[i]; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001577 handle_term_parameter(terminal, args[i], 0);
1578 }
1579 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001580 case 'm': /* SGR - Set attributes */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001581 for (i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001582 if (i <= 7 && set[i] && set[i + 1] &&
1583 set[i + 2] && args[i + 1] == 5)
1584 {
1585 if (args[i] == 38) {
1586 handle_sgr(terminal, args[i + 2] + 256);
1587 break;
1588 } else if (args[i] == 48) {
1589 handle_sgr(terminal, args[i + 2] + 512);
1590 break;
1591 }
1592 }
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001593 if (set[i]) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001594 handle_sgr(terminal, args[i]);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001595 } else if (i == 0) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001596 handle_sgr(terminal, 0);
1597 break;
1598 } else {
1599 break;
1600 }
1601 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001602 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001603 case 'n': /* DSR - Status report */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001604 i = set[0] ? args[0] : 0;
1605 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001606 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001607 } else if (i == 6) {
1608 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1609 terminal->origin_mode ?
1610 terminal->row+terminal->margin_top : terminal->row+1,
1611 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001612 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001613 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001614 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001615 case 'r': /* DECSTBM - Set scrolling region */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001616 if (!set[0]) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001617 terminal->margin_top = 0;
1618 terminal->margin_bottom = terminal->height-1;
1619 terminal->row = 0;
1620 terminal->column = 0;
1621 } else {
1622 top = (set[0] ? args[0] : 1) - 1;
1623 top = top < 0 ? 0 :
1624 (top >= terminal->height ? terminal->height - 1 : top);
1625 bottom = (set[1] ? args[1] : 1) - 1;
1626 bottom = bottom < 0 ? 0 :
1627 (bottom >= terminal->height ? terminal->height - 1 : bottom);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001628 if (bottom > top) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001629 terminal->margin_top = top;
1630 terminal->margin_bottom = bottom;
1631 } else {
1632 terminal->margin_top = 0;
1633 terminal->margin_bottom = terminal->height-1;
1634 }
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001635 if (terminal->origin_mode)
Callum Lowcaybbeac602011-01-07 19:46:58 +00001636 terminal->row = terminal->margin_top;
1637 else
1638 terminal->row = 0;
1639 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001640 }
1641 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001642 case 's': /* Save cursor location */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001643 terminal->saved_row = terminal->row;
1644 terminal->saved_column = terminal->column;
1645 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001646 case 't': /* windowOps */
1647 if (!set[0]) break;
1648 switch (args[0]) {
1649 case 4: /* resize px */
1650 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001651 widget_schedule_resize(terminal->widget,
1652 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001653 }
1654 break;
1655 case 8: /* resize ch */
1656 if (set[1] && set[2]) {
1657 terminal_resize(terminal, args[2], args[1]);
1658 }
1659 break;
1660 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001661 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001662 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1663 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001664 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001665 break;
1666 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001667 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001668 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1669 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001670 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001671 break;
1672 case 18: /* report ch */
1673 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1674 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001675 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001676 break;
1677 case 21: /* report title */
1678 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1679 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001680 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001681 break;
1682 default:
1683 if (args[0] >= 24)
1684 terminal_resize(terminal, terminal->width, args[0]);
1685 else
1686 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1687 break;
1688 }
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001689 case 'u': /* Restore cursor location */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001690 terminal->row = terminal->saved_row;
1691 terminal->column = terminal->saved_column;
1692 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001693 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001694 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001695 break;
Michael Vetter2a18a522015-05-15 17:17:47 +02001696 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001697}
1698
1699static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001700handle_non_csi_escape(struct terminal *terminal, char code)
1701{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001702 switch(code) {
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001703 case 'M': /* RI - Reverse linefeed */
Callum Lowcaybbeac602011-01-07 19:46:58 +00001704 terminal->row -= 1;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001705 if (terminal->row < terminal->margin_top) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001706 terminal->row = terminal->margin_top;
1707 terminal_scroll(terminal, -1);
1708 }
1709 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001710 case 'E': /* NEL - Newline */
Callum Lowcaybbeac602011-01-07 19:46:58 +00001711 terminal->column = 0;
1712 // fallthrough
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001713 case 'D': /* IND - Linefeed */
Callum Lowcaybbeac602011-01-07 19:46:58 +00001714 terminal->row += 1;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001715 if (terminal->row > terminal->margin_bottom) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001716 terminal->row = terminal->margin_bottom;
1717 terminal_scroll(terminal, +1);
1718 }
1719 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001720 case 'c': /* RIS - Reset*/
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001721 terminal_init(terminal);
1722 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001723 case 'H': /* HTS - Set tab stop at current column */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001724 terminal->tab_ruler[terminal->column] = 1;
1725 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001726 case '7': /* DECSC - Save current state */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001727 terminal->saved_row = terminal->row;
1728 terminal->saved_column = terminal->column;
1729 terminal->saved_attr = terminal->curr_attr;
1730 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001731 terminal->saved_cs = terminal->cs;
1732 terminal->saved_g0 = terminal->g0;
1733 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001734 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001735 case '8': /* DECRC - Restore state most recently saved by ESC 7 */
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001736 terminal->row = terminal->saved_row;
1737 terminal->column = terminal->saved_column;
1738 terminal->curr_attr = terminal->saved_attr;
1739 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001740 terminal->cs = terminal->saved_cs;
1741 terminal->g0 = terminal->saved_g0;
1742 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001743 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001744 case '=': /* DECPAM - Set application keypad mode */
Callum Lowcay7e08e902011-01-07 19:47:02 +00001745 terminal->key_mode = KM_APPLICATION;
1746 break;
Bryce Harringtonf013ce92016-06-15 18:36:03 -07001747 case '>': /* DECPNM - Set numeric keypad mode */
Callum Lowcay7e08e902011-01-07 19:47:02 +00001748 terminal->key_mode = KM_NORMAL;
1749 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001750 default:
1751 fprintf(stderr, "Unknown escape code: %c\n", code);
1752 break;
1753 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001754}
1755
1756static void
1757handle_special_escape(struct terminal *terminal, char special, char code)
1758{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001759 int i, numChars;
1760
1761 if (special == '#') {
1762 switch(code) {
1763 case '8':
1764 /* fill with 'E', no cheap way to do this */
1765 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1766 numChars = terminal->width * terminal->height;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001767 for (i = 0; i < numChars; i++) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001768 terminal->data[i].byte[0] = 'E';
1769 }
1770 break;
1771 default:
1772 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1773 break;
1774 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001775 } else if (special == '(' || special == ')') {
1776 switch(code) {
1777 case '0':
1778 if (special == '(')
1779 terminal->g0 = CS_SPECIAL;
1780 else
1781 terminal->g1 = CS_SPECIAL;
1782 break;
1783 case 'A':
1784 if (special == '(')
1785 terminal->g0 = CS_UK;
1786 else
1787 terminal->g1 = CS_UK;
1788 break;
1789 case 'B':
1790 if (special == '(')
1791 terminal->g0 = CS_US;
1792 else
1793 terminal->g1 = CS_US;
1794 break;
1795 default:
1796 fprintf(stderr, "Unknown character set %c\n", code);
1797 break;
1798 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001799 } else {
1800 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1801 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001802}
1803
1804static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001805handle_sgr(struct terminal *terminal, int code)
1806{
1807 switch(code) {
1808 case 0:
1809 terminal->curr_attr = terminal->color_scheme->default_attr;
1810 break;
1811 case 1:
1812 terminal->curr_attr.a |= ATTRMASK_BOLD;
1813 if (terminal->curr_attr.fg < 8)
1814 terminal->curr_attr.fg += 8;
1815 break;
1816 case 4:
1817 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1818 break;
1819 case 5:
1820 terminal->curr_attr.a |= ATTRMASK_BLINK;
1821 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001822 case 8:
1823 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1824 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001825 case 2:
1826 case 21:
1827 case 22:
1828 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1829 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1830 terminal->curr_attr.fg -= 8;
1831 break;
1832 case 24:
1833 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1834 break;
1835 case 25:
1836 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1837 break;
1838 case 7:
1839 case 26:
1840 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1841 break;
1842 case 27:
1843 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1844 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001845 case 28:
1846 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1847 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001848 case 39:
1849 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1850 break;
1851 case 49:
1852 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1853 break;
1854 default:
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001855 if (code >= 30 && code <= 37) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001856 terminal->curr_attr.fg = code - 30;
1857 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1858 terminal->curr_attr.fg += 8;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001859 } else if (code >= 40 && code <= 47) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001860 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001861 } else if (code >= 90 && code <= 97) {
1862 terminal->curr_attr.fg = code - 90 + 8;
1863 } else if (code >= 100 && code <= 107) {
1864 terminal->curr_attr.bg = code - 100 + 8;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001865 } else if (code >= 256 && code < 512) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001866 terminal->curr_attr.fg = code - 256;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001867 } else if (code >= 512 && code < 768) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001868 terminal->curr_attr.bg = code - 512;
1869 } else {
1870 fprintf(stderr, "Unknown SGR code: %d\n", code);
1871 }
1872 break;
1873 }
1874}
1875
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001876/* Returns 1 if c was special, otherwise 0 */
1877static int
1878handle_special_char(struct terminal *terminal, char c)
1879{
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001880 union utf8_char *row;
1881 struct attr *attr_row;
1882
1883 row = terminal_get_row(terminal, terminal->row);
1884 attr_row = terminal_get_attr_row(terminal, terminal->row);
1885
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001886 switch(c) {
1887 case '\r':
1888 terminal->column = 0;
1889 break;
1890 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001891 if (terminal->mode & MODE_LF_NEWLINE) {
1892 terminal->column = 0;
1893 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001894 /* fallthrough */
1895 case '\v':
1896 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001897 terminal->row++;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07001898 if (terminal->row > terminal->margin_bottom) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001899 terminal->row = terminal->margin_bottom;
1900 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001901 }
1902
1903 break;
1904 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001905 while (terminal->column < terminal->width) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001906 if (terminal->mode & MODE_IRM)
1907 terminal_shift_line(terminal, +1);
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001908
1909 if (row[terminal->column].byte[0] == '\0') {
1910 row[terminal->column].byte[0] = ' ';
1911 row[terminal->column].byte[1] = '\0';
1912 attr_row[terminal->column] = terminal->curr_attr;
1913 }
1914
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001915 terminal->column++;
Kristian Høgsbergcca3c2f2012-06-20 15:56:13 -04001916 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001917 }
1918 if (terminal->column >= terminal->width) {
1919 terminal->column = terminal->width - 1;
1920 }
1921
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001922 break;
1923 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001924 if (terminal->column >= terminal->width) {
1925 terminal->column = terminal->width - 2;
1926 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001927 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001928 } else if (terminal->mode & MODE_AUTOWRAP) {
1929 terminal->column = terminal->width - 1;
1930 terminal->row -= 1;
1931 if (terminal->row < terminal->margin_top) {
1932 terminal->row = terminal->margin_top;
1933 terminal_scroll(terminal, -1);
1934 }
1935 }
1936
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001937 break;
1938 case '\a':
1939 /* Bell */
1940 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001941 case '\x0E': /* SO */
1942 terminal->cs = terminal->g1;
1943 break;
1944 case '\x0F': /* SI */
1945 terminal->cs = terminal->g0;
1946 break;
Kristian Høgsberg2a1aa4e2012-08-03 09:37:05 -04001947 case '\0':
1948 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001949 default:
1950 return 0;
1951 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001952
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001953 return 1;
1954}
1955
1956static void
1957handle_char(struct terminal *terminal, union utf8_char utf8)
1958{
1959 union utf8_char *row;
1960 struct attr *attr_row;
Michael Vetter2a18a522015-05-15 17:17:47 +02001961
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001962 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001963
1964 apply_char_set(terminal->cs, &utf8);
Michael Vetter2a18a522015-05-15 17:17:47 +02001965
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001966 /* There are a whole lot of non-characters, control codes,
1967 * and formatting codes that should probably be ignored,
1968 * for example: */
1969 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1970 /* BOM, ignore */
1971 return;
Michael Vetter2a18a522015-05-15 17:17:47 +02001972 }
1973
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001974 /* Some of these non-characters should be translated, e.g.: */
1975 if (utf8.byte[0] < 32) {
1976 utf8.byte[0] = utf8.byte[0] + 64;
1977 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001978
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001979 /* handle right margin effects */
1980 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001981 if (terminal->mode & MODE_AUTOWRAP) {
1982 terminal->column = 0;
1983 terminal->row += 1;
1984 if (terminal->row > terminal->margin_bottom) {
1985 terminal->row = terminal->margin_bottom;
1986 terminal_scroll(terminal, +1);
1987 }
1988 } else {
1989 terminal->column--;
1990 }
1991 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001992
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001993 row = terminal_get_row(terminal, terminal->row);
1994 attr_row = terminal_get_attr_row(terminal, terminal->row);
Michael Vetter2a18a522015-05-15 17:17:47 +02001995
Callum Lowcay69e96582011-01-07 19:47:00 +00001996 if (terminal->mode & MODE_IRM)
1997 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001998 row[terminal->column] = utf8;
1999 attr_row[terminal->column++] = terminal->curr_attr;
2000
Kristian Høgsberg1d781ee2013-11-24 16:54:12 -08002001 if (terminal->row + terminal->start + 1 > terminal->end)
2002 terminal->end = terminal->row + terminal->start + 1;
2003 if (terminal->end == terminal->buffer_height)
2004 terminal->log_size = terminal->buffer_height;
2005 else if (terminal->log_size < terminal->buffer_height)
2006 terminal->log_size = terminal->end;
2007
Peng Wucfcc1112013-08-19 10:59:21 +08002008 /* cursor jump for wide character. */
2009 if (is_wide(utf8))
2010 row[terminal->column++].ch = 0x200B; /* space glyph */
2011
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002012 if (utf8.ch != terminal->last_char.ch)
2013 terminal->last_char = utf8;
2014}
2015
Callum Lowcay30eeae52011-01-07 19:46:55 +00002016static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13002017escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
2018{
2019 int len, i;
2020
2021 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
2022 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
2023 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
2024 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
2025 else len = 1; /* Invalid, cannot happen */
2026
2027 if (terminal->escape_length + len <= MAX_ESCAPE) {
2028 for (i = 0; i < len; i++)
2029 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
2030 terminal->escape_length += len;
2031 } else if (terminal->escape_length < MAX_ESCAPE) {
2032 terminal->escape[terminal->escape_length++] = 0;
2033 }
2034}
2035
2036static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002037terminal_data(struct terminal *terminal, const char *data, size_t length)
2038{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002039 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002040 union utf8_char utf8;
2041 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002042
2043 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002044 parser_state =
2045 utf8_next_char(&terminal->state_machine, data[i]);
2046 switch(parser_state) {
2047 case utf8state_accept:
2048 utf8.ch = terminal->state_machine.s.ch;
2049 break;
2050 case utf8state_reject:
2051 /* the unicode replacement character */
2052 utf8.byte[0] = 0xEF;
2053 utf8.byte[1] = 0xBF;
2054 utf8.byte[2] = 0xBD;
2055 utf8.byte[3] = 0x00;
2056 break;
2057 default:
2058 continue;
2059 }
2060
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002061 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13002062 switch (terminal->state) {
2063 case escape_state_escape:
2064 escape_append_utf8(terminal, utf8);
2065 switch (utf8.byte[0]) {
2066 case 'P': /* DCS */
2067 terminal->state = escape_state_dcs;
2068 break;
2069 case '[': /* CSI */
2070 terminal->state = escape_state_csi;
2071 break;
2072 case ']': /* OSC */
2073 terminal->state = escape_state_osc;
2074 break;
2075 case '#':
2076 case '(':
2077 case ')': /* special */
2078 terminal->state = escape_state_special;
2079 break;
2080 case '^': /* PM (not implemented) */
2081 case '_': /* APC (not implemented) */
2082 terminal->state = escape_state_ignore;
2083 break;
2084 default:
2085 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002086 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13002087 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002088 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002089 continue;
2090 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002091 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
2092 /* do nothing */
2093 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002094 terminal->escape_flags |= ESC_FLAG_WHAT;
2095 } else if (utf8.byte[0] == '>') {
2096 terminal->escape_flags |= ESC_FLAG_GT;
2097 } else if (utf8.byte[0] == '!') {
2098 terminal->escape_flags |= ESC_FLAG_BANG;
2099 } else if (utf8.byte[0] == '$') {
2100 terminal->escape_flags |= ESC_FLAG_CASH;
2101 } else if (utf8.byte[0] == '\'') {
2102 terminal->escape_flags |= ESC_FLAG_SQUOTE;
2103 } else if (utf8.byte[0] == '"') {
2104 terminal->escape_flags |= ESC_FLAG_DQUOTE;
2105 } else if (utf8.byte[0] == ' ') {
2106 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002107 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002108 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002109 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13002110 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002111 }
Michael Vetter2a18a522015-05-15 17:17:47 +02002112
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002113 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
2114 utf8.byte[0] == '`')
2115 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002116 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002117 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002118 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002119 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002120 continue;
2121 case escape_state_inner_escape:
2122 if (utf8.byte[0] == '\\') {
2123 terminal->state = escape_state_normal;
2124 if (terminal->outer_state == escape_state_dcs) {
2125 handle_dcs(terminal);
2126 } else if (terminal->outer_state == escape_state_osc) {
2127 handle_osc(terminal);
2128 }
2129 } else if (utf8.byte[0] == '\e') {
2130 terminal->state = terminal->outer_state;
2131 escape_append_utf8(terminal, utf8);
2132 if (terminal->escape_length >= MAX_ESCAPE)
2133 terminal->state = escape_state_normal;
2134 } else {
2135 terminal->state = terminal->outer_state;
2136 if (terminal->escape_length < MAX_ESCAPE)
2137 terminal->escape[terminal->escape_length++] = '\e';
2138 escape_append_utf8(terminal, utf8);
2139 if (terminal->escape_length >= MAX_ESCAPE)
2140 terminal->state = escape_state_normal;
2141 }
2142 continue;
2143 case escape_state_dcs:
2144 case escape_state_osc:
2145 case escape_state_ignore:
2146 if (utf8.byte[0] == '\e') {
2147 terminal->outer_state = terminal->state;
2148 terminal->state = escape_state_inner_escape;
2149 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
2150 terminal->state = escape_state_normal;
2151 handle_osc(terminal);
2152 } else {
2153 escape_append_utf8(terminal, utf8);
2154 if (terminal->escape_length >= MAX_ESCAPE)
2155 terminal->state = escape_state_normal;
2156 }
2157 continue;
2158 case escape_state_special:
2159 escape_append_utf8(terminal, utf8);
2160 terminal->state = escape_state_normal;
2161 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2162 handle_special_escape(terminal, terminal->escape[1],
2163 utf8.byte[0]);
2164 }
2165 continue;
2166 default:
2167 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002168 }
2169
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002170 /* this is valid, because ASCII characters are never used to
2171 * introduce a multibyte sequence in UTF-8 */
2172 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002173 terminal->state = escape_state_escape;
2174 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002175 terminal->escape[0] = '\e';
2176 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002177 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002178 } else {
2179 handle_char(terminal, utf8);
2180 } /* if */
2181 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002182
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002183 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002184}
2185
2186static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002187data_source_target(void *data,
2188 struct wl_data_source *source, const char *mime_type)
2189{
2190 fprintf(stderr, "data_source_target, %s\n", mime_type);
2191}
2192
2193static void
2194data_source_send(void *data,
2195 struct wl_data_source *source,
2196 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002197{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002198 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002199
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002200 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002201}
2202
2203static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002204data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002205{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002206 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002207}
2208
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002209static const struct wl_data_source_listener data_source_listener = {
2210 data_source_target,
2211 data_source_send,
2212 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002213};
2214
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07002215static const char text_mime_type[] = "text/plain;charset=utf-8";
2216
2217static void
2218data_handler(struct window *window,
2219 struct input *input,
2220 float x, float y, const char **types, void *data)
2221{
2222 int i, has_text = 0;
2223
2224 if (!types)
2225 return;
2226 for (i = 0; types[i]; i++)
2227 if (strcmp(types[i], text_mime_type) == 0)
2228 has_text = 1;
2229
2230 if (!has_text) {
2231 input_accept(input, NULL);
2232 } else {
2233 input_accept(input, text_mime_type);
2234 }
2235}
2236
2237static void
2238drop_handler(struct window *window, struct input *input,
2239 int32_t x, int32_t y, void *data)
2240{
2241 struct terminal *terminal = data;
2242
2243 input_receive_drag_data_to_fd(input, text_mime_type, terminal->master);
2244}
2245
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002246static void
2247fullscreen_handler(struct window *window, void *data)
2248{
2249 struct terminal *terminal = data;
2250
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002251 window_set_fullscreen(window, !window_is_fullscreen(terminal->window));
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002252}
2253
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002254static void
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002255close_handler(void *data)
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002256{
2257 struct terminal *terminal = data;
2258
2259 terminal_destroy(terminal);
2260}
2261
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002262static void
2263terminal_copy(struct terminal *terminal, struct input *input)
2264{
2265 terminal->selection =
2266 display_create_data_source(terminal->display);
2267 wl_data_source_offer(terminal->selection,
2268 "text/plain;charset=utf-8");
2269 wl_data_source_add_listener(terminal->selection,
2270 &data_source_listener, terminal);
2271 input_set_selection(input, terminal->selection,
2272 display_get_serial(terminal->display));
2273}
2274
2275static void
2276terminal_paste(struct terminal *terminal, struct input *input)
2277{
2278 input_receive_selection_data_to_fd(input,
2279 "text/plain;charset=utf-8",
2280 terminal->master);
2281
2282}
2283
2284static void
2285terminal_new_instance(struct terminal *terminal)
2286{
2287 struct terminal *new_terminal;
2288
2289 new_terminal = terminal_create(terminal->display);
2290 if (terminal_run(new_terminal, option_shell))
2291 terminal_destroy(new_terminal);
2292}
2293
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002294static int
2295handle_bound_key(struct terminal *terminal,
2296 struct input *input, uint32_t sym, uint32_t time)
2297{
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002298 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002299 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002300 /* Cut selection; terminal doesn't do cut, fall
2301 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002302 case XKB_KEY_C:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002303 terminal_copy(terminal, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002304 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002305 case XKB_KEY_V:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002306 terminal_paste(terminal, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002307 return 1;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002308 case XKB_KEY_N:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002309 terminal_new_instance(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002310 return 1;
2311
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002312 case XKB_KEY_Up:
2313 if (!terminal->scrolling)
2314 terminal->saved_start = terminal->start;
2315 if (terminal->start == terminal->end - terminal->log_size)
2316 return 1;
2317
2318 terminal->scrolling = 1;
2319 terminal->start--;
2320 terminal->row++;
2321 terminal->selection_start_row++;
2322 terminal->selection_end_row++;
2323 widget_schedule_redraw(terminal->widget);
2324 return 1;
2325
2326 case XKB_KEY_Down:
2327 if (!terminal->scrolling)
2328 terminal->saved_start = terminal->start;
2329
2330 if (terminal->start == terminal->saved_start)
2331 return 1;
2332
2333 terminal->scrolling = 1;
2334 terminal->start++;
2335 terminal->row--;
2336 terminal->selection_start_row--;
2337 terminal->selection_end_row--;
2338 widget_schedule_redraw(terminal->widget);
2339 return 1;
2340
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002341 default:
2342 return 0;
2343 }
2344}
2345
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002346static void
2347key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002348 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2349 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002350{
2351 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002352 char ch[MAX_RESPONSE];
Kristian Høgsberg88fd4082012-06-21 15:55:03 -04002353 uint32_t modifiers, serial;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002354 int ret, len = 0, d;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002355 bool convert_utf8 = true;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002356
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002357 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002358 if ((modifiers & MOD_CONTROL_MASK) &&
2359 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002360 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2361 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002362 return;
2363
Daniel Stonea8468712012-11-07 17:51:35 +11002364 /* Map keypad symbols to 'normal' equivalents before processing */
2365 switch (sym) {
2366 case XKB_KEY_KP_Space:
2367 sym = XKB_KEY_space;
2368 break;
2369 case XKB_KEY_KP_Tab:
2370 sym = XKB_KEY_Tab;
2371 break;
2372 case XKB_KEY_KP_Enter:
2373 sym = XKB_KEY_Return;
2374 break;
2375 case XKB_KEY_KP_Left:
2376 sym = XKB_KEY_Left;
2377 break;
2378 case XKB_KEY_KP_Up:
2379 sym = XKB_KEY_Up;
2380 break;
2381 case XKB_KEY_KP_Right:
2382 sym = XKB_KEY_Right;
2383 break;
2384 case XKB_KEY_KP_Down:
2385 sym = XKB_KEY_Down;
2386 break;
2387 case XKB_KEY_KP_Equal:
2388 sym = XKB_KEY_equal;
2389 break;
2390 case XKB_KEY_KP_Multiply:
2391 sym = XKB_KEY_asterisk;
2392 break;
2393 case XKB_KEY_KP_Add:
2394 sym = XKB_KEY_plus;
2395 break;
2396 case XKB_KEY_KP_Separator:
2397 /* Note this is actually locale-dependent and should mostly be
2398 * a comma. But leave it as period until we one day start
2399 * doing the right thing. */
2400 sym = XKB_KEY_period;
2401 break;
2402 case XKB_KEY_KP_Subtract:
2403 sym = XKB_KEY_minus;
2404 break;
2405 case XKB_KEY_KP_Decimal:
2406 sym = XKB_KEY_period;
2407 break;
2408 case XKB_KEY_KP_Divide:
2409 sym = XKB_KEY_slash;
2410 break;
2411 case XKB_KEY_KP_0:
2412 case XKB_KEY_KP_1:
2413 case XKB_KEY_KP_2:
2414 case XKB_KEY_KP_3:
2415 case XKB_KEY_KP_4:
2416 case XKB_KEY_KP_5:
2417 case XKB_KEY_KP_6:
2418 case XKB_KEY_KP_7:
2419 case XKB_KEY_KP_8:
2420 case XKB_KEY_KP_9:
2421 sym = (sym - XKB_KEY_KP_0) + XKB_KEY_0;
2422 break;
2423 default:
2424 break;
2425 }
2426
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002427 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002428 case XKB_KEY_BackSpace:
Kristian Høgsbergb7f94bf2012-06-21 12:29:36 -04002429 if (modifiers & MOD_ALT_MASK)
2430 ch[len++] = 0x1b;
Kristian Høgsberg71a4cf42012-06-20 17:57:56 -04002431 ch[len++] = 0x7f;
2432 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002433 case XKB_KEY_Tab:
2434 case XKB_KEY_Linefeed:
2435 case XKB_KEY_Clear:
2436 case XKB_KEY_Pause:
2437 case XKB_KEY_Scroll_Lock:
2438 case XKB_KEY_Sys_Req:
2439 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002440 ch[len++] = sym & 0x7f;
2441 break;
2442
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002443 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002444 if (terminal->mode & MODE_LF_NEWLINE) {
2445 ch[len++] = 0x0D;
2446 ch[len++] = 0x0A;
2447 } else {
2448 ch[len++] = 0x0D;
2449 }
2450 break;
2451
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002452 case XKB_KEY_Shift_L:
2453 case XKB_KEY_Shift_R:
2454 case XKB_KEY_Control_L:
2455 case XKB_KEY_Control_R:
2456 case XKB_KEY_Alt_L:
2457 case XKB_KEY_Alt_R:
Kristian Høgsberg22fbcf72012-06-22 12:18:56 -04002458 case XKB_KEY_Meta_L:
2459 case XKB_KEY_Meta_R:
2460 case XKB_KEY_Super_L:
2461 case XKB_KEY_Super_R:
2462 case XKB_KEY_Hyper_L:
2463 case XKB_KEY_Hyper_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002464 break;
2465
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002466 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002467 len = function_key_response('[', 2, modifiers, '~', ch);
2468 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002469 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002470 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2471 ch[len++] = '\x04';
2472 } else {
2473 len = function_key_response('[', 3, modifiers, '~', ch);
2474 }
2475 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002476 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002477 len = function_key_response('[', 5, modifiers, '~', ch);
2478 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002479 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002480 len = function_key_response('[', 6, modifiers, '~', ch);
2481 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002482 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002483 len = function_key_response('O', 1, modifiers, 'P', ch);
2484 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002485 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002486 len = function_key_response('O', 1, modifiers, 'Q', ch);
2487 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002488 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002489 len = function_key_response('O', 1, modifiers, 'R', ch);
2490 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002491 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002492 len = function_key_response('O', 1, modifiers, 'S', ch);
2493 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002494 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002495 len = function_key_response('[', 15, modifiers, '~', ch);
2496 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002497 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002498 len = function_key_response('[', 17, modifiers, '~', ch);
2499 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002500 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002501 len = function_key_response('[', 18, modifiers, '~', ch);
2502 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002503 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002504 len = function_key_response('[', 19, modifiers, '~', ch);
2505 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002506 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002507 len = function_key_response('[', 20, modifiers, '~', ch);
2508 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002509 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002510 len = function_key_response('[', 21, modifiers, '~', ch);
2511 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002512 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002513 len = function_key_response('[', 24, modifiers, '~', ch);
2514 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002515 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002516 /* Handle special keys with alternate mappings */
2517 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2518 if (len != 0) break;
Michael Vetter2a18a522015-05-15 17:17:47 +02002519
Kristian Høgsberg70163132012-05-08 15:55:39 -04002520 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002521 if (sym >= '3' && sym <= '7')
2522 sym = (sym & 0x1f) + 8;
2523
2524 if (!((sym >= '!' && sym <= '/') ||
2525 (sym >= '8' && sym <= '?') ||
2526 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2527 else if (sym == '2') sym = 0x00;
2528 else if (sym == '/') sym = 0x1F;
2529 else if (sym == '8' || sym == '?') sym = 0x7F;
Kristian Høgsbergae9e0732012-06-21 12:30:15 -04002530 }
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002531 if (modifiers & MOD_ALT_MASK) {
2532 if (terminal->mode & MODE_ALT_SENDS_ESC) {
2533 ch[len++] = 0x1b;
2534 } else {
2535 sym = sym | 0x80;
2536 convert_utf8 = false;
2537 }
Callum Lowcay7e08e902011-01-07 19:47:02 +00002538 }
2539
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002540 if ((sym < 128) ||
2541 (!convert_utf8 && sym < 256)) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002542 ch[len++] = sym;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002543 } else {
2544 ret = xkb_keysym_to_utf8(sym, ch + len,
2545 MAX_RESPONSE - len);
2546 if (ret < 0)
2547 fprintf(stderr,
2548 "Warning: buffer too small to encode "
2549 "UTF8 character\n");
2550 else
2551 len += ret;
2552 }
2553
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002554 break;
2555 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002556
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002557 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002558 if (terminal->scrolling) {
2559 d = terminal->saved_start - terminal->start;
2560 terminal->row -= d;
2561 terminal->selection_start_row -= d;
2562 terminal->selection_end_row -= d;
2563 terminal->start = terminal->saved_start;
2564 terminal->scrolling = 0;
2565 widget_schedule_redraw(terminal->widget);
2566 }
2567
Kristian Høgsberg26130862011-08-24 11:30:21 -04002568 terminal_write(terminal, ch, len);
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002569
2570 /* Hide cursor, except if this was coming from a
2571 * repeating key press. */
2572 serial = display_get_serial(terminal->display);
2573 if (terminal->hide_cursor_serial != serial) {
2574 input_set_pointer_image(input, CURSOR_BLANK);
2575 terminal->hide_cursor_serial = serial;
2576 }
2577 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002578}
2579
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002580static void
2581keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002582 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002583{
2584 struct terminal *terminal = data;
2585
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002586 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002587}
2588
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002589static int wordsep(int ch)
2590{
2591 const char extra[] = "-,./?%&#:_=+@~";
2592
Derek Foreman7978bc82015-09-02 16:21:54 -05002593 if (ch > 127 || ch < 0)
Andre Heider552d12b2012-08-02 20:59:43 +02002594 return 1;
2595
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002596 return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
2597}
2598
2599static int
2600recompute_selection(struct terminal *terminal)
2601{
2602 struct rectangle allocation;
2603 int col, x, width, height;
2604 int start_row, end_row;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002605 int word_start, eol;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002606 int side_margin, top_margin;
2607 int start_x, end_x;
2608 int cw, ch;
2609 union utf8_char *data;
2610
Peng Wuf291f202013-06-06 15:32:41 +08002611 cw = terminal->average_width;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002612 ch = terminal->extents.height;
2613 widget_get_allocation(terminal->widget, &allocation);
2614 width = terminal->width * cw;
2615 height = terminal->height * ch;
2616 side_margin = allocation.x + (allocation.width - width) / 2;
2617 top_margin = allocation.y + (allocation.height - height) / 2;
2618
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002619 start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1;
2620 end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1;
2621
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002622 if (start_row < end_row ||
2623 (start_row == end_row &&
2624 terminal->selection_start_x < terminal->selection_end_x)) {
2625 terminal->selection_start_row = start_row;
2626 terminal->selection_end_row = end_row;
2627 start_x = terminal->selection_start_x;
2628 end_x = terminal->selection_end_x;
2629 } else {
2630 terminal->selection_start_row = end_row;
2631 terminal->selection_end_row = start_row;
2632 start_x = terminal->selection_end_x;
2633 end_x = terminal->selection_start_x;
2634 }
2635
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002636 eol = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002637 if (terminal->selection_start_row < 0) {
2638 terminal->selection_start_row = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002639 terminal->selection_start_col = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002640 } else {
2641 x = side_margin + cw / 2;
2642 data = terminal_get_row(terminal,
2643 terminal->selection_start_row);
2644 word_start = 0;
2645 for (col = 0; col < terminal->width; col++, x += cw) {
2646 if (col == 0 || wordsep(data[col - 1].ch))
2647 word_start = col;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002648 if (data[col].ch != 0)
2649 eol = col + 1;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002650 if (start_x < x)
2651 break;
2652 }
2653
2654 switch (terminal->dragging) {
2655 case SELECT_LINE:
2656 terminal->selection_start_col = 0;
2657 break;
2658 case SELECT_WORD:
2659 terminal->selection_start_col = word_start;
2660 break;
2661 case SELECT_CHAR:
2662 terminal->selection_start_col = col;
2663 break;
2664 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002665 }
2666
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002667 if (terminal->selection_end_row >= terminal->height) {
2668 terminal->selection_end_row = terminal->height;
2669 terminal->selection_end_col = 0;
2670 } else {
2671 x = side_margin + cw / 2;
2672 data = terminal_get_row(terminal, terminal->selection_end_row);
2673 for (col = 0; col < terminal->width; col++, x += cw) {
2674 if (terminal->dragging == SELECT_CHAR && end_x < x)
2675 break;
2676 if (terminal->dragging == SELECT_WORD &&
2677 end_x < x && wordsep(data[col].ch))
2678 break;
2679 }
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002680 terminal->selection_end_col = col;
2681 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002682
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002683 if (terminal->selection_end_col != terminal->selection_start_col ||
2684 terminal->selection_start_row != terminal->selection_end_row) {
2685 col = terminal->selection_end_col;
2686 if (col > 0 && data[col - 1].ch == 0)
2687 terminal->selection_end_col = terminal->width;
2688 data = terminal_get_row(terminal, terminal->selection_start_row);
2689 if (data[terminal->selection_start_col].ch == 0)
2690 terminal->selection_start_col = eol;
2691 }
2692
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002693 return 1;
2694}
2695
Kristian Høgsberg59826582011-01-20 11:56:57 -05002696static void
Pekka Paalanen807f09c2015-03-27 15:48:54 +02002697terminal_minimize(struct terminal *terminal)
2698{
2699 window_set_minimized(terminal->window);
2700}
2701
2702static void
Jasper St. Pierredda93132014-03-13 12:06:00 -04002703menu_func(void *data, struct input *input, int index)
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002704{
Jasper St. Pierredda93132014-03-13 12:06:00 -04002705 struct window *window = data;
2706 struct terminal *terminal = window_get_user_data(window);
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002707
2708 fprintf(stderr, "picked entry %d\n", index);
2709
2710 switch (index) {
2711 case 0:
2712 terminal_new_instance(terminal);
2713 break;
2714 case 1:
2715 terminal_copy(terminal, input);
2716 break;
2717 case 2:
2718 terminal_paste(terminal, input);
2719 break;
Pekka Paalanen807f09c2015-03-27 15:48:54 +02002720 case 3:
2721 terminal_minimize(terminal);
2722 break;
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002723 }
2724}
2725
2726static void
2727show_menu(struct terminal *terminal, struct input *input, uint32_t time)
2728{
2729 int32_t x, y;
2730 static const char *entries[] = {
Pekka Paalanen807f09c2015-03-27 15:48:54 +02002731 "Open Terminal", "Copy", "Paste", "Minimize"
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002732 };
2733
2734 input_get_position(input, &x, &y);
2735 window_show_menu(terminal->display, input, time, terminal->window,
2736 x - 10, y - 10, menu_func,
2737 entries, ARRAY_LENGTH(entries));
2738}
2739
2740static void
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002741click_handler(struct widget *widget, struct terminal *terminal,
2742 struct input *input, int32_t x, int32_t y,
2743 uint32_t time)
2744{
2745 if (time - terminal->click_time < 500)
2746 terminal->click_count++;
2747 else
2748 terminal->click_count = 1;
2749
2750 terminal->click_time = time;
2751 terminal->dragging = (terminal->click_count - 1) % 3 + SELECT_CHAR;
2752
2753 terminal->selection_end_x = terminal->selection_start_x = x;
2754 terminal->selection_end_y = terminal->selection_start_y = y;
2755 if (recompute_selection(terminal))
2756 widget_schedule_redraw(widget);
2757}
2758
2759static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002760button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002761 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002762 uint32_t button,
2763 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002764{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002765 struct terminal *terminal = data;
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002766 int32_t x, y;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002767
2768 switch (button) {
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002769 case BTN_LEFT:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002770 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002771 input_get_position(input, &x, &y);
2772 click_handler(widget, terminal, input, x, y, time);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002773 } else {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002774 terminal->dragging = SELECT_NONE;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002775 }
2776 break;
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002777
2778 case BTN_RIGHT:
2779 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
2780 show_menu(terminal, input, time);
2781 break;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002782 }
2783}
2784
2785static int
Kristian Høgsberg29784402012-06-28 14:27:02 -04002786enter_handler(struct widget *widget,
2787 struct input *input, float x, float y, void *data)
2788{
2789 return CURSOR_IBEAM;
2790}
2791
2792static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002793motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002794 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002795 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002796{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002797 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002798
2799 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002800 input_get_position(input,
2801 &terminal->selection_end_x,
2802 &terminal->selection_end_y);
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002803
2804 if (recompute_selection(terminal))
2805 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002806 }
2807
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002808 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002809}
2810
Magnus Hoff1046f122014-08-05 15:05:59 +02002811/* This magnitude is chosen rather arbitrarily. Really, the scrolling
2812 * should happen on a (fractional) pixel basis, not a line basis. */
2813#define AXIS_UNITS_PER_LINE 256
2814
2815static void
2816axis_handler(struct widget *widget,
2817 struct input *input, uint32_t time,
2818 uint32_t axis,
2819 wl_fixed_t value,
2820 void *data)
2821{
2822 struct terminal *terminal = data;
2823 int lines;
2824
2825 if (axis != WL_POINTER_AXIS_VERTICAL_SCROLL)
2826 return;
2827
2828 terminal->smooth_scroll += value;
2829 lines = terminal->smooth_scroll / AXIS_UNITS_PER_LINE;
2830 terminal->smooth_scroll -= lines * AXIS_UNITS_PER_LINE;
2831
2832 if (lines > 0) {
2833 if (terminal->scrolling) {
2834 if ((uint32_t)lines > terminal->saved_start - terminal->start)
2835 lines = terminal->saved_start - terminal->start;
2836 } else {
2837 lines = 0;
2838 }
2839 } else if (lines < 0) {
2840 uint32_t neg_lines = -lines;
2841
2842 if (neg_lines > terminal->log_size + terminal->start - terminal->end)
2843 lines = terminal->end - terminal->log_size - terminal->start;
2844 }
2845
2846 if (lines) {
2847 if (!terminal->scrolling)
2848 terminal->saved_start = terminal->start;
2849 terminal->scrolling = 1;
2850
2851 terminal->start += lines;
2852 terminal->row -= lines;
2853 terminal->selection_start_row -= lines;
2854 terminal->selection_end_row -= lines;
2855
2856 widget_schedule_redraw(widget);
2857 }
2858}
2859
Alexander Larssonde79dd02013-05-22 14:41:34 +02002860static void
2861output_handler(struct window *window, struct output *output, int enter,
2862 void *data)
2863{
2864 if (enter)
2865 window_set_buffer_transform(window, output_get_transform(output));
2866 window_set_buffer_scale(window, window_get_output_scale(window));
2867 window_schedule_redraw(window);
2868}
2869
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002870static void
2871touch_down_handler(struct widget *widget, struct input *input,
2872 uint32_t serial, uint32_t time, int32_t id,
2873 float x, float y, void *data)
2874{
2875 struct terminal *terminal = data;
2876
2877 if (id == 0)
2878 click_handler(widget, terminal, input, x, y, time);
2879}
2880
2881static void
2882touch_up_handler(struct widget *widget, struct input *input,
2883 uint32_t serial, uint32_t time, int32_t id, void *data)
2884{
2885 struct terminal *terminal = data;
2886
2887 if (id == 0)
2888 terminal->dragging = SELECT_NONE;
2889}
2890
2891static void
2892touch_motion_handler(struct widget *widget, struct input *input,
2893 uint32_t time, int32_t id, float x, float y, void *data)
2894{
2895 struct terminal *terminal = data;
2896
2897 if (terminal->dragging &&
2898 id == 0) {
2899 terminal->selection_end_x = (int)x;
2900 terminal->selection_end_y = (int)y;
2901
2902 if (recompute_selection(terminal))
2903 widget_schedule_redraw(widget);
2904 }
2905}
2906
Peng Wuf291f202013-06-06 15:32:41 +08002907#ifndef howmany
2908#define howmany(x, y) (((x) + ((y) - 1)) / (y))
2909#endif
2910
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002911static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002912terminal_create(struct display *display)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002913{
2914 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002915 cairo_surface_t *surface;
2916 cairo_t *cr;
Peng Wuf291f202013-06-06 15:32:41 +08002917 cairo_text_extents_t text_extents;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002918
Peter Huttererf3d62272013-08-08 11:57:05 +10002919 terminal = xzalloc(sizeof *terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002920 terminal->color_scheme = &DEFAULT_COLORS;
2921 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002922 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002923 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002924 terminal->window = window_create(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002925 terminal->widget = window_frame_create(terminal->window, terminal);
U. Artie Eoff09827e22014-01-15 11:40:37 -08002926 terminal->title = xstrdup("Wayland Terminal");
Kristian Høgsberga83be202013-10-23 20:47:35 -07002927 window_set_title(terminal->window, terminal->title);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002928 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002929
2930 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002931 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002932
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002933 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002934 terminal->margin = 5;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002935 terminal->buffer_height = 1024;
2936 terminal->end = 1;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002937
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002938 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002939 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002940 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002941 keyboard_focus_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002942 window_set_fullscreen_handler(terminal->window, fullscreen_handler);
Alexander Larssonde79dd02013-05-22 14:41:34 +02002943 window_set_output_handler(terminal->window, output_handler);
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002944 window_set_close_handler(terminal->window, close_handler);
Jasper St. Pierrede680992014-04-10 17:23:49 -07002945 window_set_state_changed_handler(terminal->window, state_changed_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002946
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07002947 window_set_data_handler(terminal->window, data_handler);
2948 window_set_drop_handler(terminal->window, drop_handler);
2949
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002950 widget_set_redraw_handler(terminal->widget, redraw_handler);
2951 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002952 widget_set_button_handler(terminal->widget, button_handler);
Kristian Høgsberg29784402012-06-28 14:27:02 -04002953 widget_set_enter_handler(terminal->widget, enter_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002954 widget_set_motion_handler(terminal->widget, motion_handler);
Magnus Hoff1046f122014-08-05 15:05:59 +02002955 widget_set_axis_handler(terminal->widget, axis_handler);
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002956 widget_set_touch_up_handler(terminal->widget, touch_up_handler);
2957 widget_set_touch_down_handler(terminal->widget, touch_down_handler);
2958 widget_set_touch_motion_handler(terminal->widget, touch_motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002959
Kristian Høgsberg09531622010-06-14 23:22:15 -04002960 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2961 cr = cairo_create(surface);
Kristian Høgsberg38912df2012-06-27 17:52:23 -04002962 cairo_set_font_size(cr, option_font_size);
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002963 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002964 CAIRO_FONT_SLANT_NORMAL,
2965 CAIRO_FONT_WEIGHT_BOLD);
2966 terminal->font_bold = cairo_get_scaled_font (cr);
2967 cairo_scaled_font_reference(terminal->font_bold);
2968
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002969 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002970 CAIRO_FONT_SLANT_NORMAL,
2971 CAIRO_FONT_WEIGHT_NORMAL);
2972 terminal->font_normal = cairo_get_scaled_font (cr);
2973 cairo_scaled_font_reference(terminal->font_normal);
2974
Kristian Høgsberg09531622010-06-14 23:22:15 -04002975 cairo_font_extents(cr, &terminal->extents);
Peng Wuf291f202013-06-06 15:32:41 +08002976
2977 /* Compute the average ascii glyph width */
2978 cairo_text_extents(cr, TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS,
2979 &text_extents);
2980 terminal->average_width = howmany
2981 (text_extents.width,
2982 strlen(TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS));
2983 terminal->average_width = ceil(terminal->average_width);
2984
Kristian Høgsberg09531622010-06-14 23:22:15 -04002985 cairo_destroy(cr);
2986 cairo_surface_destroy(surface);
2987
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002988 terminal_resize(terminal, 20, 5); /* Set minimum size first */
Kristian Høgsberga1627922012-06-20 17:30:03 -04002989 terminal_resize(terminal, 80, 25);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002990
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002991 wl_list_insert(terminal_list.prev, &terminal->link);
2992
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002993 return terminal;
2994}
2995
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002996static void
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002997terminal_destroy(struct terminal *terminal)
2998{
Dima Ryazanova85292e2012-11-29 00:27:09 -08002999 display_unwatch_fd(terminal->display, terminal->master);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003000 window_destroy(terminal->window);
3001 close(terminal->master);
3002 wl_list_remove(&terminal->link);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003003
3004 if (wl_list_empty(&terminal_list))
Dima Ryazanova85292e2012-11-29 00:27:09 -08003005 display_exit(terminal->display);
3006
Kristian Høgsberga83be202013-10-23 20:47:35 -07003007 free(terminal->title);
Dima Ryazanova85292e2012-11-29 00:27:09 -08003008 free(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003009}
3010
3011static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003012io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003013{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003014 struct terminal *terminal =
3015 container_of(task, struct terminal, io_task);
3016 char buffer[256];
3017 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003018
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003019 if (events & EPOLLHUP) {
3020 terminal_destroy(terminal);
3021 return;
3022 }
Tim Wiederhakef71accc2011-01-19 23:14:33 +01003023
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003024 len = read(terminal->master, buffer, sizeof buffer);
3025 if (len < 0)
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003026 terminal_destroy(terminal);
3027 else
3028 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003029}
3030
3031static int
3032terminal_run(struct terminal *terminal, const char *path)
3033{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003034 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003035 pid_t pid;
Derek Foreman88353dd2017-03-24 16:29:31 -05003036 int pipes[2];
3037
3038 /* Awkwardness: There's a sticky race condition here. If
3039 * anything prints after the forkpty() but before the window has
3040 * a size then we'll segfault. So we make a pipe and wait on
3041 * it before actually exec()ing the terminal. The resize
3042 * handler closes it in the parent process and the child continues
3043 * on to launch a shell.
3044 *
3045 * The reason we don't just do terminal_run() after the window
3046 * has a size is that we'd prefer to perform the fork() before
3047 * the process opens a wayland connection.
3048 */
3049 if (pipe(pipes) == -1) {
3050 fprintf(stderr, "Can't create pipe for pacing.\n");
3051 exit(EXIT_FAILURE);
3052 }
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003053
3054 pid = forkpty(&master, NULL, NULL, NULL);
3055 if (pid == 0) {
Derek Foreman88353dd2017-03-24 16:29:31 -05003056 int ret;
3057
3058 close(pipes[1]);
3059 do {
3060 char tmp;
3061 ret = read(pipes[0], &tmp, 1);
3062 } while (ret == -1 && errno == EINTR);
3063 close(pipes[0]);
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04003064 setenv("TERM", option_term, 1);
3065 setenv("COLORTERM", option_term, 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003066 if (execl(path, path, NULL)) {
3067 printf("exec failed: %m\n");
3068 exit(EXIT_FAILURE);
3069 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003070 } else if (pid < 0) {
3071 fprintf(stderr, "failed to fork and create pty (%m).\n");
3072 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003073 }
3074
Derek Foreman88353dd2017-03-24 16:29:31 -05003075 close(pipes[0]);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003076 terminal->master = master;
Derek Foreman88353dd2017-03-24 16:29:31 -05003077 terminal->pace_pipe = pipes[1];
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003078 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003079 terminal->io_task.run = io_handler;
3080 display_watch_fd(terminal->display, terminal->master,
3081 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003082
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07003083 if (option_fullscreen)
3084 window_set_fullscreen(terminal->window, 1);
Derek Foreman76091782017-03-08 11:58:20 -06003085 else if (option_maximize)
3086 window_set_maximized(terminal->window, 1);
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07003087 else
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003088 terminal_resize(terminal, 80, 24);
3089
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003090 return 0;
3091}
3092
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003093static const struct weston_option terminal_options[] = {
3094 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Derek Foreman76091782017-03-08 11:58:20 -06003095 { WESTON_OPTION_BOOLEAN, "maximized", 'm', &option_maximize },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04003096 { WESTON_OPTION_STRING, "font", 0, &option_font },
Bill Spitzak5cad8432014-08-08 12:59:55 -07003097 { WESTON_OPTION_INTEGER, "font-size", 0, &option_font_size },
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003098 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003099};
3100
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003101int main(int argc, char *argv[])
3102{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003103 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003104 struct terminal *terminal;
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02003105 const char *config_file;
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -07003106 struct weston_config *config;
3107 struct weston_config_section *s;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04003108
Peng Wucfcc1112013-08-19 10:59:21 +08003109 /* as wcwidth is locale-dependent,
3110 wcwidth needs setlocale call to function properly. */
3111 setlocale(LC_ALL, "");
3112
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003113 option_shell = getenv("SHELL");
3114 if (!option_shell)
3115 option_shell = "/bin/bash";
3116
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02003117 config_file = weston_config_get_name_from_env();
3118 config = weston_config_parse(config_file);
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -07003119 s = weston_config_get_section(config, "terminal", NULL, NULL);
3120 weston_config_section_get_string(s, "font", &option_font, "mono");
3121 weston_config_section_get_int(s, "font-size", &option_font_size, 14);
3122 weston_config_section_get_string(s, "term", &option_term, "xterm");
3123 weston_config_destroy(config);
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003124
Bill Spitzak5cad8432014-08-08 12:59:55 -07003125 if (parse_options(terminal_options,
3126 ARRAY_LENGTH(terminal_options), &argc, argv) > 1) {
3127 printf("Usage: %s [OPTIONS]\n"
3128 " --fullscreen or -f\n"
Derek Foreman76091782017-03-08 11:58:20 -06003129 " --maximized or -m\n"
Bill Spitzak5cad8432014-08-08 12:59:55 -07003130 " --font=NAME\n"
3131 " --font-size=SIZE\n"
3132 " --shell=NAME\n", argv[0]);
3133 return 1;
3134 }
3135
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003136 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02003137 if (d == NULL) {
3138 fprintf(stderr, "failed to create display: %m\n");
3139 return -1;
3140 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003141
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003142 wl_list_init(&terminal_list);
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04003143 terminal = terminal_create(d);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003144 if (terminal_run(terminal, option_shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003145 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003146
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003147 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003148
3149 return 0;
3150}