blob: a70fef310e9bfb7558ad893d601b405634bcb1f7 [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
Kristian Høgsberg0b36d972013-08-21 22:13:17 -070024#include <config.h>
Peng Wucfcc1112013-08-19 10:59:21 +080025
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +020026#include <stdbool.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
34#include <time.h>
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050035#include <pty.h>
Kristian Høgsbergf04e8382008-12-08 00:07:49 -050036#include <ctype.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050037#include <cairo.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040038#include <sys/epoll.h>
Peng Wucfcc1112013-08-19 10:59:21 +080039#include <wchar.h>
40#include <locale.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050041
Kristian Høgsberg67b82152013-10-23 16:52:05 -070042#include <linux/input.h>
43
Pekka Paalanen50719bc2011-11-22 14:18:50 +020044#include <wayland-client.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050045
Jon Cruz4678bab2015-06-15 15:37:07 -070046#include "shared/config-parser.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070047#include "shared/helpers.h"
Bryce Harringtone99e4bf2016-03-16 14:15:18 -070048#include "shared/xalloc.h"
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050049#include "window.h"
50
Kristian Høgsberg0395f302008-12-22 12:14:50 -050051static int option_fullscreen;
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -070052static char *option_font;
53static int option_font_size;
54static char *option_term;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040055static char *option_shell;
56
57static struct wl_list terminal_list;
58
59static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -040060terminal_create(struct display *display);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040061static void
62terminal_destroy(struct terminal *terminal);
63static int
64terminal_run(struct terminal *terminal, const char *path);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050065
Peng Wuf291f202013-06-06 15:32:41 +080066#define TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS \
67 " !\"#$%&'()*+,-./" \
68 "0123456789" \
69 ":;<=>?@" \
70 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
71 "[\\]^_`" \
72 "abcdefghijklmnopqrstuvwxyz" \
73 "{|}~" \
74 ""
75
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050076#define MOD_SHIFT 0x01
77#define MOD_ALT 0x02
78#define MOD_CTRL 0x04
79
Callum Lowcay30eeae52011-01-07 19:46:55 +000080#define ATTRMASK_BOLD 0x01
81#define ATTRMASK_UNDERLINE 0x02
82#define ATTRMASK_BLINK 0x04
83#define ATTRMASK_INVERSE 0x08
Callum Lowcay81179db2011-01-10 12:14:01 +130084#define ATTRMASK_CONCEALED 0x10
Callum Lowcay30eeae52011-01-07 19:46:55 +000085
Callum Lowcayb8609ad2011-01-07 19:46:57 +000086/* Buffer sizes */
Callum Lowcayef57a9b2011-01-14 20:46:23 +130087#define MAX_RESPONSE 256
88#define MAX_ESCAPE 255
Callum Lowcayb8609ad2011-01-07 19:46:57 +000089
Callum Lowcay8e57dd52011-01-07 19:46:59 +000090/* Terminal modes */
91#define MODE_SHOW_CURSOR 0x00000001
92#define MODE_INVERSE 0x00000002
93#define MODE_AUTOWRAP 0x00000004
94#define MODE_AUTOREPEAT 0x00000008
95#define MODE_LF_NEWLINE 0x00000010
Callum Lowcay69e96582011-01-07 19:47:00 +000096#define MODE_IRM 0x00000020
Callum Lowcay7e08e902011-01-07 19:47:02 +000097#define MODE_DELETE_SENDS_DEL 0x00000040
98#define MODE_ALT_SENDS_ESC 0x00000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +000099
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000100union utf8_char {
101 unsigned char byte[4];
102 uint32_t ch;
103};
104
105enum utf8_state {
106 utf8state_start,
107 utf8state_accept,
108 utf8state_reject,
109 utf8state_expect3,
110 utf8state_expect2,
111 utf8state_expect1
112};
113
114struct utf8_state_machine {
115 enum utf8_state state;
116 int len;
117 union utf8_char s;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700118 uint32_t unicode;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000119};
120
121static void
122init_state_machine(struct utf8_state_machine *machine)
123{
124 machine->state = utf8state_start;
125 machine->len = 0;
126 machine->s.ch = 0;
127}
128
129static enum utf8_state
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400130utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000131{
132 switch(machine->state) {
133 case utf8state_start:
134 case utf8state_accept:
135 case utf8state_reject:
136 machine->s.ch = 0;
137 machine->len = 0;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300138 if (c == 0xC0 || c == 0xC1) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000139 /* overlong encoding, reject */
140 machine->state = utf8state_reject;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300141 } else if ((c & 0x80) == 0) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000142 /* single byte, accept */
143 machine->s.byte[machine->len++] = c;
144 machine->state = utf8state_accept;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700145 machine->unicode = c;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300146 } else if ((c & 0xC0) == 0x80) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000147 /* parser out of sync, ignore byte */
148 machine->state = utf8state_start;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300149 } else if ((c & 0xE0) == 0xC0) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000150 /* start of two byte sequence */
151 machine->s.byte[machine->len++] = c;
152 machine->state = utf8state_expect1;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700153 machine->unicode = c & 0x1f;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300154 } else if ((c & 0xF0) == 0xE0) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000155 /* start of three byte sequence */
156 machine->s.byte[machine->len++] = c;
157 machine->state = utf8state_expect2;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700158 machine->unicode = c & 0x0f;
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300159 } else if ((c & 0xF8) == 0xF0) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000160 /* start of four byte sequence */
161 machine->s.byte[machine->len++] = c;
162 machine->state = utf8state_expect3;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700163 machine->unicode = c & 0x07;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000164 } else {
165 /* overlong encoding, reject */
166 machine->state = utf8state_reject;
167 }
168 break;
169 case utf8state_expect3:
170 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700171 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300172 if ((c & 0xC0) == 0x80) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000173 /* all good, continue */
174 machine->state = utf8state_expect2;
175 } else {
176 /* missing extra byte, reject */
177 machine->state = utf8state_reject;
178 }
179 break;
180 case utf8state_expect2:
181 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700182 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300183 if ((c & 0xC0) == 0x80) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000184 /* all good, continue */
185 machine->state = utf8state_expect1;
186 } else {
187 /* missing extra byte, reject */
188 machine->state = utf8state_reject;
189 }
190 break;
191 case utf8state_expect1:
192 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700193 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300194 if ((c & 0xC0) == 0x80) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000195 /* all good, accept */
196 machine->state = utf8state_accept;
197 } else {
198 /* missing extra byte, reject */
199 machine->state = utf8state_reject;
200 }
201 break;
202 default:
203 machine->state = utf8state_reject;
204 break;
205 }
Michael Vetter2a18a522015-05-15 17:17:47 +0200206
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000207 return machine->state;
208}
209
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700210static uint32_t
211get_unicode(union utf8_char utf8)
212{
213 struct utf8_state_machine machine;
214 int i;
215
216 init_state_machine(&machine);
217 for (i = 0; i < 4; i++) {
218 utf8_next_char(&machine, utf8.byte[i]);
219 if (machine.state == utf8state_accept ||
220 machine.state == utf8state_reject)
221 break;
222 }
223
224 if (machine.state == utf8state_reject)
225 return 0xfffd;
226
227 return machine.unicode;
228}
229
Peng Wucfcc1112013-08-19 10:59:21 +0800230static bool
231is_wide(union utf8_char utf8)
232{
233 uint32_t unichar = get_unicode(utf8);
234 return wcwidth(unichar) > 1;
235}
236
Callum Lowcay256e72f2011-01-07 19:47:01 +0000237struct char_sub {
238 union utf8_char match;
239 union utf8_char replace;
240};
241/* Set last char_sub match to NULL char */
242typedef struct char_sub *character_set;
243
244struct char_sub CS_US[] = {
245 {{{0, }}, {{0, }}}
246};
247static struct char_sub CS_UK[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200248 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000249 {{{0, }}, {{0, }}}
250};
251static struct char_sub CS_SPECIAL[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200252 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond: ♦ */
253 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell: ▒ */
254 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT: ␉ */
255 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF: ␌ */
256 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR: ␍ */
257 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF: ␊ */
258 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree: ° */
259 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus: ± */
260 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL: ␤ */
261 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT: ␋ */
262 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB: ┘ */
263 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT: ┐ */
264 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT: ┌ */
265 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_LB: └ */
266 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS: ┼ */
267 {{{'o', 0, }}, {{0xE2, 0x8E, 0xBA, 0}}}, /* Horiz. Scan Line 1: ⎺ */
268 {{{'p', 0, }}, {{0xE2, 0x8E, 0xBB, 0}}}, /* Horiz. Scan Line 3: ⎻ */
269 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* Horiz. Scan Line 5: ─ */
270 {{{'r', 0, }}, {{0xE2, 0x8E, 0xBC, 0}}}, /* Horiz. Scan Line 7: ⎼ */
271 {{{'s', 0, }}, {{0xE2, 0x8E, 0xBD, 0}}}, /* Horiz. Scan Line 9: ⎽ */
272 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR: ├ */
273 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL: ┤ */
274 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU: ┴ */
275 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD: ┬ */
276 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V: │ */
277 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE: ≤ */
278 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE: ≥ */
279 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI: π */
280 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ: ≠ */
281 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
282 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT: ⋅ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000283 {{{0, }}, {{0, }}}
284};
285
286static void
287apply_char_set(character_set cs, union utf8_char *utf8)
288{
289 int i = 0;
Michael Vetter2a18a522015-05-15 17:17:47 +0200290
Callum Lowcay256e72f2011-01-07 19:47:01 +0000291 while (cs[i].match.byte[0]) {
292 if ((*utf8).ch == cs[i].match.ch) {
293 *utf8 = cs[i].replace;
294 break;
295 }
296 i++;
297 }
298}
299
Callum Lowcay7e08e902011-01-07 19:47:02 +0000300struct key_map {
301 int sym;
302 int num;
303 char escape;
304 char code;
305};
306/* Set last key_sub sym to NULL */
307typedef struct key_map *keyboard_mode;
308
309static struct key_map KM_NORMAL[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400310 { XKB_KEY_Left, 1, '[', 'D' },
311 { XKB_KEY_Right, 1, '[', 'C' },
312 { XKB_KEY_Up, 1, '[', 'A' },
313 { XKB_KEY_Down, 1, '[', 'B' },
314 { XKB_KEY_Home, 1, '[', 'H' },
315 { XKB_KEY_End, 1, '[', 'F' },
316 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000317};
318static struct key_map KM_APPLICATION[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400319 { XKB_KEY_Left, 1, 'O', 'D' },
320 { XKB_KEY_Right, 1, 'O', 'C' },
321 { XKB_KEY_Up, 1, 'O', 'A' },
322 { XKB_KEY_Down, 1, 'O', 'B' },
323 { XKB_KEY_Home, 1, 'O', 'H' },
324 { XKB_KEY_End, 1, 'O', 'F' },
325 { XKB_KEY_KP_Enter, 1, 'O', 'M' },
326 { XKB_KEY_KP_Multiply, 1, 'O', 'j' },
327 { XKB_KEY_KP_Add, 1, 'O', 'k' },
328 { XKB_KEY_KP_Separator, 1, 'O', 'l' },
329 { XKB_KEY_KP_Subtract, 1, 'O', 'm' },
330 { XKB_KEY_KP_Divide, 1, 'O', 'o' },
331 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000332};
333
334static int
335function_key_response(char escape, int num, uint32_t modifiers,
336 char code, char *response)
337{
338 int mod_num = 0;
339 int len;
340
Kristian Høgsberg70163132012-05-08 15:55:39 -0400341 if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
342 if (modifiers & MOD_ALT_MASK) mod_num |= 2;
343 if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000344
345 if (mod_num != 0)
346 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
347 num, mod_num + 1, code);
348 else if (code != '~')
349 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
350 escape, code);
351 else
352 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
353 escape, num, code);
354
355 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
356 else return len;
357}
358
359/* returns the number of bytes written into response,
360 * which must have room for MAX_RESPONSE bytes */
361static int
362apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
363{
364 struct key_map map;
365 int len = 0;
366 int i = 0;
Michael Vetter2a18a522015-05-15 17:17:47 +0200367
Callum Lowcay7e08e902011-01-07 19:47:02 +0000368 while (mode[i].sym) {
369 map = mode[i++];
370 if (sym == map.sym) {
371 len = function_key_response(map.escape, map.num,
372 modifiers, map.code,
373 response);
374 break;
375 }
376 }
Michael Vetter2a18a522015-05-15 17:17:47 +0200377
Callum Lowcay7e08e902011-01-07 19:47:02 +0000378 return len;
379}
380
Callum Lowcay30eeae52011-01-07 19:46:55 +0000381struct terminal_color { double r, g, b, a; };
382struct attr {
383 unsigned char fg, bg;
384 char a; /* attributes format:
385 * 76543210
Callum Lowcay81179db2011-01-10 12:14:01 +1300386 * cilub */
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500387 char s; /* in selection */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000388};
389struct color_scheme {
390 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500391 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000392 struct attr default_attr;
393};
394
395static void
396attr_init(struct attr *data_attr, struct attr attr, int n)
397{
398 int i;
399 for (i = 0; i < n; i++) {
400 data_attr[i] = attr;
401 }
402}
403
Callum Lowcay67a201d2011-01-12 19:23:41 +1300404enum escape_state {
405 escape_state_normal = 0,
406 escape_state_escape,
407 escape_state_dcs,
408 escape_state_csi,
409 escape_state_osc,
410 escape_state_inner_escape,
411 escape_state_ignore,
412 escape_state_special
413};
414
415#define ESC_FLAG_WHAT 0x01
416#define ESC_FLAG_GT 0x02
417#define ESC_FLAG_BANG 0x04
418#define ESC_FLAG_CASH 0x08
419#define ESC_FLAG_SQUOTE 0x10
420#define ESC_FLAG_DQUOTE 0x20
421#define ESC_FLAG_SPACE 0x40
422
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400423enum {
424 SELECT_NONE,
425 SELECT_CHAR,
426 SELECT_WORD,
427 SELECT_LINE
428};
429
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500430struct terminal {
431 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500432 struct widget *widget;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500433 struct display *display;
Kristian Høgsberga83be202013-10-23 20:47:35 -0700434 char *title;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000435 union utf8_char *data;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400436 struct task io_task;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000437 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000438 struct attr *data_attr;
439 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000440 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000441 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000442 char saved_origin_mode;
443 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000444 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000445 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000446 character_set cs, g0, g1;
447 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000448 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000449 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700450 int width, height, row, column, max_width;
451 uint32_t buffer_height;
452 uint32_t start, end, saved_start, log_size;
Magnus Hoff1046f122014-08-05 15:05:59 +0200453 wl_fixed_t smooth_scroll;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000454 int saved_row, saved_column;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700455 int scrolling;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600456 int send_cursor_position;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500457 int fd, master;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500458 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300459 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500460 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300461 enum escape_state state;
462 enum escape_state outer_state;
463 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000464 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500465 int margin;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400466 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000467 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400468 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +0800469 double average_width;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500470 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg88fd4082012-06-21 15:55:03 -0400471 uint32_t hide_cursor_serial;
Kristian Høgsbergb405a802014-02-05 14:44:04 -0800472 int size_in_title;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500473
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400474 struct wl_data_source *selection;
Xiong Zhang49c6aee2013-11-25 18:42:52 +0800475 uint32_t click_time;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400476 int dragging, click_count;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500477 int selection_start_x, selection_start_y;
478 int selection_end_x, selection_end_y;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400479 int selection_start_row, selection_start_col;
480 int selection_end_row, selection_end_col;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -0400481 struct wl_list link;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500482};
483
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000484/* Create default tab stops, every 8 characters */
485static void
486terminal_init_tabs(struct terminal *terminal)
487{
488 int i = 0;
Michael Vetter2a18a522015-05-15 17:17:47 +0200489
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000490 while (i < terminal->width) {
491 if (i % 8 == 0)
492 terminal->tab_ruler[i] = 1;
493 else
494 terminal->tab_ruler[i] = 0;
495 i++;
496 }
497}
498
Callum Lowcay30eeae52011-01-07 19:46:55 +0000499static void
500terminal_init(struct terminal *terminal)
501{
502 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000503 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000504 terminal->mode = MODE_SHOW_CURSOR |
505 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000506 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000507 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000508
509 terminal->row = 0;
510 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000511
Callum Lowcay256e72f2011-01-07 19:47:01 +0000512 terminal->g0 = CS_US;
513 terminal->g1 = CS_US;
514 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000515 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000516
517 terminal->saved_g0 = terminal->g0;
518 terminal->saved_g1 = terminal->g1;
519 terminal->saved_cs = terminal->cs;
520
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000521 terminal->saved_attr = terminal->curr_attr;
522 terminal->saved_origin_mode = terminal->origin_mode;
523 terminal->saved_row = terminal->row;
524 terminal->saved_column = terminal->column;
525
526 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000527}
528
529static void
530init_color_table(struct terminal *terminal)
531{
532 int c, r;
533 struct terminal_color *color_table = terminal->color_table;
534
535 for (c = 0; c < 256; c ++) {
536 if (c < 16) {
537 color_table[c] = terminal->color_scheme->palette[c];
538 } else if (c < 232) {
539 r = c - 16;
540 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
541 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
542 color_table[c].r = ((double)(r % 6) / 6.0);
543 color_table[c].a = 1.0;
544 } else {
545 r = (c - 232) * 10 + 8;
546 color_table[c].r = ((double) r) / 256.0;
547 color_table[c].g = color_table[c].r;
548 color_table[c].b = color_table[c].r;
549 color_table[c].a = 1.0;
550 }
551 }
552}
553
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000554static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500555terminal_get_row(struct terminal *terminal, int row)
556{
557 int index;
558
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700559 index = (row + terminal->start) & (terminal->buffer_height - 1);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500560
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700561 return (void *) terminal->data + index * terminal->data_pitch;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500562}
563
Callum Lowcay30eeae52011-01-07 19:46:55 +0000564static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500565terminal_get_attr_row(struct terminal *terminal, int row)
566{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000567 int index;
568
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700569 index = (row + terminal->start) & (terminal->buffer_height - 1);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000570
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700571 return (void *) terminal->data_attr + index * terminal->attr_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000572}
573
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500574union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300575 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500576 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500577};
578
579static void
580terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500581 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500582{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500583 struct attr attr;
584 int foreground, background, tmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500585
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500586 decoded->attr.s = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400587 if (((row == terminal->selection_start_row &&
588 col >= terminal->selection_start_col) ||
589 row > terminal->selection_start_row) &&
590 ((row == terminal->selection_end_row &&
591 col < terminal->selection_end_col) ||
592 row < terminal->selection_end_row))
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500593 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500594
595 /* get the attributes for this character cell */
596 attr = terminal_get_attr_row(terminal, row)[col];
597 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500598 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500599 ((terminal->mode & MODE_SHOW_CURSOR) &&
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400600 window_has_focus(terminal->window) && terminal->row == row &&
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500601 terminal->column == col)) {
602 foreground = attr.bg;
603 background = attr.fg;
604 if (attr.a & ATTRMASK_BOLD) {
605 if (foreground <= 16) foreground |= 0x08;
606 if (background <= 16) background &= 0x07;
607 }
608 } else {
609 foreground = attr.fg;
610 background = attr.bg;
611 }
612
613 if (terminal->mode & MODE_INVERSE) {
614 tmp = foreground;
615 foreground = background;
616 background = tmp;
617 if (attr.a & ATTRMASK_BOLD) {
618 if (foreground <= 16) foreground |= 0x08;
619 if (background <= 16) background &= 0x07;
620 }
621 }
622
Callum Lowcay9d708b02011-01-12 20:06:17 +1300623 decoded->attr.fg = foreground;
624 decoded->attr.bg = background;
625 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000626}
627
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500628
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500629static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000630terminal_scroll_buffer(struct terminal *terminal, int d)
631{
632 int i;
633
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700634 terminal->start += d;
635 if (d < 0) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000636 d = 0 - d;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700637 for (i = 0; i < d; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000638 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
639 attr_init(terminal_get_attr_row(terminal, i),
640 terminal->curr_attr, terminal->width);
641 }
642 } else {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700643 for (i = terminal->height - d; i < terminal->height; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000644 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
645 attr_init(terminal_get_attr_row(terminal, i),
646 terminal->curr_attr, terminal->width);
647 }
648 }
Kristian Høgsberg18e928d2012-06-27 19:29:41 -0400649
650 terminal->selection_start_row -= d;
651 terminal->selection_end_row -= d;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000652}
653
654static void
655terminal_scroll_window(struct terminal *terminal, int d)
656{
657 int i;
658 int window_height;
659 int from_row, to_row;
Michael Vetter2a18a522015-05-15 17:17:47 +0200660
Callum Lowcaybbeac602011-01-07 19:46:58 +0000661 // scrolling range is inclusive
662 window_height = terminal->margin_bottom - terminal->margin_top + 1;
663 d = d % (window_height + 1);
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300664 if (d < 0) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000665 d = 0 - d;
666 to_row = terminal->margin_bottom;
667 from_row = terminal->margin_bottom - d;
Michael Vetter2a18a522015-05-15 17:17:47 +0200668
Callum Lowcaybbeac602011-01-07 19:46:58 +0000669 for (i = 0; i < (window_height - d); i++) {
670 memcpy(terminal_get_row(terminal, to_row - i),
671 terminal_get_row(terminal, from_row - i),
672 terminal->data_pitch);
673 memcpy(terminal_get_attr_row(terminal, to_row - i),
674 terminal_get_attr_row(terminal, from_row - i),
675 terminal->attr_pitch);
676 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000677 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
678 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000679 attr_init(terminal_get_attr_row(terminal, i),
680 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000681 }
682 } else {
683 to_row = terminal->margin_top;
684 from_row = terminal->margin_top + d;
Michael Vetter2a18a522015-05-15 17:17:47 +0200685
Callum Lowcaybbeac602011-01-07 19:46:58 +0000686 for (i = 0; i < (window_height - d); i++) {
687 memcpy(terminal_get_row(terminal, to_row + i),
688 terminal_get_row(terminal, from_row + i),
689 terminal->data_pitch);
690 memcpy(terminal_get_attr_row(terminal, to_row + i),
691 terminal_get_attr_row(terminal, from_row + i),
692 terminal->attr_pitch);
693 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000694 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
695 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000696 attr_init(terminal_get_attr_row(terminal, i),
697 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000698 }
699 }
700}
701
702static void
703terminal_scroll(struct terminal *terminal, int d)
704{
Dawid Gajownik74a635b2015-08-06 17:12:19 -0300705 if (terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
Callum Lowcaybbeac602011-01-07 19:46:58 +0000706 terminal_scroll_buffer(terminal, d);
707 else
708 terminal_scroll_window(terminal, d);
709}
710
711static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000712terminal_shift_line(struct terminal *terminal, int d)
713{
714 union utf8_char *row;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500715 struct attr *attr_row;
Michael Vetter2a18a522015-05-15 17:17:47 +0200716
Callum Lowcay69e96582011-01-07 19:47:00 +0000717 row = terminal_get_row(terminal, terminal->row);
718 attr_row = terminal_get_attr_row(terminal, terminal->row);
719
720 if ((terminal->width + d) <= terminal->column)
721 d = terminal->column + 1 - terminal->width;
722 if ((terminal->column + d) >= terminal->width)
723 d = terminal->width - terminal->column - 1;
Michael Vetter2a18a522015-05-15 17:17:47 +0200724
Callum Lowcay69e96582011-01-07 19:47:00 +0000725 if (d < 0) {
726 d = 0 - d;
727 memmove(&row[terminal->column],
728 &row[terminal->column + d],
729 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
Callum Lowcay69e96582011-01-07 19:47:00 +0000730 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
731 (terminal->width - terminal->column - d) * sizeof(struct attr));
732 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
733 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
734 } else {
735 memmove(&row[terminal->column + d], &row[terminal->column],
736 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
737 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
738 (terminal->width - terminal->column - d) * sizeof(struct attr));
739 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
740 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
741 }
742}
743
744static void
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700745terminal_resize_cells(struct terminal *terminal,
746 int width, int height)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500747{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000748 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000749 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000750 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000751 int data_pitch, attr_pitch;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500752 int i, l, total_rows;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700753 uint32_t d, uheight = height;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500754 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000755 struct winsize ws;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500756
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700757 if (uheight > terminal->buffer_height)
758 height = terminal->buffer_height;
759
Kristian Høgsberg22106762008-12-08 13:50:07 -0500760 if (terminal->width == width && terminal->height == height)
761 return;
762
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700763 if (terminal->data && width <= terminal->max_width) {
764 d = 0;
765 if (height < terminal->height && height <= terminal->row)
766 d = terminal->height - height;
767 else if (height > terminal->height &&
768 terminal->height - 1 == terminal->row) {
769 d = terminal->height - height;
770 if (terminal->log_size < uheight)
771 d = -terminal->start;
772 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500773
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700774 terminal->start += d;
775 terminal->row -= d;
776 } else {
777 terminal->max_width = width;
778 data_pitch = width * sizeof(union utf8_char);
Bryce W. Harrington66cd2c12014-04-28 18:44:08 +0000779 data = xzalloc(data_pitch * terminal->buffer_height);
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700780 attr_pitch = width * sizeof(struct attr);
Bryce W. Harrington66cd2c12014-04-28 18:44:08 +0000781 data_attr = xmalloc(attr_pitch * terminal->buffer_height);
782 tab_ruler = xzalloc(width);
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700783 attr_init(data_attr, terminal->curr_attr,
784 width * terminal->buffer_height);
785
786 if (terminal->data && terminal->data_attr) {
787 if (width > terminal->width)
788 l = terminal->width;
789 else
790 l = width;
791
792 if (terminal->height > height) {
793 total_rows = height;
794 i = 1 + terminal->row - height;
795 if (i > 0) {
796 terminal->start += i;
797 terminal->row = terminal->row - i;
798 }
799 } else {
800 total_rows = terminal->height;
José Bollo4a4704a2013-09-13 11:28:51 +0200801 }
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700802
803 for (i = 0; i < total_rows; i++) {
804 memcpy(&data[width * i],
805 terminal_get_row(terminal, i),
806 l * sizeof(union utf8_char));
807 memcpy(&data_attr[width * i],
808 terminal_get_attr_row(terminal, i),
809 l * sizeof(struct attr));
810 }
811
812 free(terminal->data);
813 free(terminal->data_attr);
814 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500815 }
816
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700817 terminal->data_pitch = data_pitch;
818 terminal->attr_pitch = attr_pitch;
819 terminal->data = data;
820 terminal->data_attr = data_attr;
821 terminal->tab_ruler = tab_ruler;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700822 terminal->start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500823 }
824
Callum Lowcay86653ed2011-01-07 19:47:03 +0000825 terminal->margin_bottom =
826 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500827 terminal->width = width;
828 terminal->height = height;
Kristian Høgsbergdcfff552013-11-22 22:43:20 -0800829 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500830
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000831 /* Update the window size */
832 ws.ws_row = terminal->height;
833 ws.ws_col = terminal->width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500834 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500835 ws.ws_xpixel = allocation.width;
836 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000837 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500838}
839
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500840static void
Jasper St. Pierrede680992014-04-10 17:23:49 -0700841update_title(struct terminal *terminal)
842{
843 if (window_is_resizing(terminal->window)) {
844 char *p;
845 if (asprintf(&p, "%s — [%dx%d]", terminal->title, terminal->width, terminal->height) > 0) {
846 window_set_title(terminal->window, p);
847 free(p);
848 }
849 } else {
850 window_set_title(terminal->window, terminal->title);
851 }
852}
853
854static void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500855resize_handler(struct widget *widget,
856 int32_t width, int32_t height, void *data)
857{
858 struct terminal *terminal = data;
859 int32_t columns, rows, m;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700860
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500861 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800862 columns = (width - m) / (int32_t) terminal->average_width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500863 rows = (height - m) / (int32_t) terminal->extents.height;
864
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400865 if (!window_is_fullscreen(terminal->window) &&
866 !window_is_maximized(terminal->window)) {
Peng Wuf291f202013-06-06 15:32:41 +0800867 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500868 height = rows * terminal->extents.height + m;
869 widget_set_size(terminal->widget, width, height);
870 }
871
872 terminal_resize_cells(terminal, columns, rows);
Jasper St. Pierrede680992014-04-10 17:23:49 -0700873 update_title(terminal);
874}
875
876static void
877state_changed_handler(struct window *window, void *data)
878{
879 struct terminal *terminal = data;
880 update_title(terminal);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500881}
882
883static void
884terminal_resize(struct terminal *terminal, int columns, int rows)
885{
886 int32_t width, height, m;
887
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400888 if (window_is_fullscreen(terminal->window) ||
889 window_is_maximized(terminal->window))
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500890 return;
891
892 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800893 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500894 height = rows * terminal->extents.height + m;
Kristian Høgsberga1627922012-06-20 17:30:03 -0400895
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500896 window_frame_set_child_size(terminal->widget, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500897}
898
Callum Lowcay30eeae52011-01-07 19:46:55 +0000899struct color_scheme DEFAULT_COLORS = {
900 {
901 {0, 0, 0, 1}, /* black */
902 {0.66, 0, 0, 1}, /* red */
903 {0 , 0.66, 0, 1}, /* green */
904 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
905 {0 , 0 , 0.66, 1}, /* blue */
906 {0.66, 0 , 0.66, 1}, /* magenta */
907 {0, 0.66, 0.66, 1}, /* cyan */
908 {0.66, 0.66, 0.66, 1}, /* light grey */
909 {0.22, 0.33, 0.33, 1}, /* dark grey */
910 {1, 0.33, 0.33, 1}, /* high red */
911 {0.33, 1, 0.33, 1}, /* high green */
912 {1, 1, 0.33, 1}, /* high yellow */
913 {0.33, 0.33, 1, 1}, /* high blue */
914 {1, 0.33, 1, 1}, /* high magenta */
915 {0.33, 1, 1, 1}, /* high cyan */
916 {1, 1, 1, 1} /* white */
917 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500918 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000919 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
920};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400921
Kristian Høgsberg22106762008-12-08 13:50:07 -0500922static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500923terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
924{
925 cairo_set_source_rgba(cr,
926 terminal->color_table[index].r,
927 terminal->color_table[index].g,
928 terminal->color_table[index].b,
929 terminal->color_table[index].a);
930}
931
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500932static void
933terminal_send_selection(struct terminal *terminal, int fd)
934{
935 int row, col;
936 union utf8_char *p_row;
937 union decoded_attr attr;
938 FILE *fp;
939 int len;
940
941 fp = fdopen(fd, "w");
Brian Lovin1bf14812013-08-08 16:12:55 -0700942 if (fp == NULL){
943 close(fd);
944 return;
945 }
Manuel Bachmanne54cee12015-09-19 13:51:05 +0200946 for (row = terminal->selection_start_row; row < terminal->height; row++) {
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500947 p_row = terminal_get_row(terminal, row);
948 for (col = 0; col < terminal->width; col++) {
Peng Wucfcc1112013-08-19 10:59:21 +0800949 if (p_row[col].ch == 0x200B) /* space glyph */
950 continue;
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500951 /* get the attributes for this character cell */
952 terminal_decode_attr(terminal, row, col, &attr);
953 if (!attr.attr.s)
954 continue;
955 len = strnlen((char *) p_row[col].byte, 4);
Kristian Høgsberg0dee6472012-07-01 21:25:41 -0400956 if (len > 0)
957 fwrite(p_row[col].byte, 1, len, fp);
958 if (len == 0 || col == terminal->width - 1) {
959 fwrite("\n", 1, 1, fp);
960 break;
961 }
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500962 }
963 }
964 fclose(fp);
965}
966
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500967struct glyph_run {
968 struct terminal *terminal;
969 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400970 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500971 union decoded_attr attr;
972 cairo_glyph_t glyphs[256], *g;
973};
974
975static void
976glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
977{
978 run->terminal = terminal;
979 run->cr = cr;
980 run->g = run->glyphs;
981 run->count = 0;
982 run->attr.key = 0;
983}
984
985static void
986glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
987{
988 cairo_scaled_font_t *font;
989
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500990 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
991 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300992 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500993 font = run->terminal->font_bold;
994 else
995 font = run->terminal->font_normal;
996 cairo_set_scaled_font(run->cr, font);
997 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300998 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500999
Callum Lowcay9d708b02011-01-12 20:06:17 +13001000 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
1001 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001002 run->g = run->glyphs;
1003 run->count = 0;
1004 }
Callum Lowcay9d708b02011-01-12 20:06:17 +13001005 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001006}
1007
1008static void
1009glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
1010{
1011 int num_glyphs;
1012 cairo_scaled_font_t *font;
1013
1014 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
1015
Callum Lowcay9d708b02011-01-12 20:06:17 +13001016 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001017 font = run->terminal->font_bold;
1018 else
1019 font = run->terminal->font_normal;
1020
1021 cairo_move_to(run->cr, x, y);
1022 cairo_scaled_font_text_to_glyphs (font, x, y,
1023 (char *) c->byte, 4,
1024 &run->g, &num_glyphs,
1025 NULL, NULL, NULL);
1026 run->g += num_glyphs;
1027 run->count += num_glyphs;
1028}
1029
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001030
Kristian Høgsbergf106fd52011-01-11 10:11:39 -05001031static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001032redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001033{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001034 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001035 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001036 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001037 int top_margin, side_margin;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001038 int row, col, cursor_x, cursor_y;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001039 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001040 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001041 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001042 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001043 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001044 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -05001045 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +08001046 double average_width;
Peng Wucfcc1112013-08-19 10:59:21 +08001047 double unichar_width;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001048
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001049 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001050 widget_get_allocation(terminal->widget, &allocation);
Alexander Larssonde79dd02013-05-22 14:41:34 +02001051 cr = widget_cairo_create(terminal->widget);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001052 cairo_rectangle(cr, allocation.x, allocation.y,
1053 allocation.width, allocation.height);
1054 cairo_clip(cr);
1055 cairo_push_group(cr);
1056
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001057 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -05001058 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001059 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001060
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05001061 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001062
Kristian Høgsberg59826582011-01-20 11:56:57 -05001063 extents = terminal->extents;
Peng Wuf291f202013-06-06 15:32:41 +08001064 average_width = terminal->average_width;
1065 side_margin = (allocation.width - terminal->width * average_width) / 2;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001066 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001067
Callum Lowcay30eeae52011-01-07 19:46:55 +00001068 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001069 cairo_translate(cr, allocation.x + side_margin,
1070 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001071 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001072 for (row = 0; row < terminal->height; row++) {
Peng Wucfcc1112013-08-19 10:59:21 +08001073 p_row = terminal_get_row(terminal, row);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001074 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001075 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -05001076 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001077
Callum Lowcay9d708b02011-01-12 20:06:17 +13001078 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -05001079 continue;
1080
Peng Wucfcc1112013-08-19 10:59:21 +08001081 if (is_wide(p_row[col]))
1082 unichar_width = 2 * average_width;
1083 else
1084 unichar_width = average_width;
1085
Callum Lowcay9d708b02011-01-12 20:06:17 +13001086 terminal_set_color(terminal, cr, attr.attr.bg);
Peng Wuf291f202013-06-06 15:32:41 +08001087 cairo_move_to(cr, col * average_width,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001088 row * extents.height);
Peng Wucfcc1112013-08-19 10:59:21 +08001089 cairo_rel_line_to(cr, unichar_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001090 cairo_rel_line_to(cr, 0, extents.height);
Peng Wucfcc1112013-08-19 10:59:21 +08001091 cairo_rel_line_to(cr, -unichar_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001092 cairo_close_path(cr);
1093 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001094 }
1095 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001096
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001097 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1098
1099 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001100 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001101 for (row = 0; row < terminal->height; row++) {
1102 p_row = terminal_get_row(terminal, row);
1103 for (col = 0; col < terminal->width; col++) {
1104 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -05001105 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001106
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001107 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001108
Peng Wuf291f202013-06-06 15:32:41 +08001109 text_x = col * average_width;
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001110 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +13001111 if (attr.attr.a & ATTRMASK_UNDERLINE) {
1112 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +00001113 cairo_move_to(cr, text_x, (double)text_y + 1.5);
Peng Wuf291f202013-06-06 15:32:41 +08001114 cairo_line_to(cr, text_x + average_width, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001115 cairo_stroke(cr);
1116 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -05001117
Daiki Ueno56d8a7a2014-04-08 18:46:18 +09001118 /* skip space glyph (RLE) we use as a placeholder of
1119 the right half of a double-width character,
1120 because RLE is not available in every font. */
1121 if (p_row[col].ch == 0x200B)
1122 continue;
1123
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001124 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001125 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001126 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001127
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001128 attr.key = ~0;
1129 glyph_run_flush(&run, attr);
1130
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001131 if ((terminal->mode & MODE_SHOW_CURSOR) &&
1132 !window_has_focus(terminal->window)) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001133 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001134
Callum Lowcay30eeae52011-01-07 19:46:55 +00001135 cairo_set_line_width(cr, 1);
Peng Wuf291f202013-06-06 15:32:41 +08001136 cairo_move_to(cr, terminal->column * average_width + d,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001137 terminal->row * extents.height + d);
Peng Wuf291f202013-06-06 15:32:41 +08001138 cairo_rel_line_to(cr, average_width - 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001139 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
Peng Wuf291f202013-06-06 15:32:41 +08001140 cairo_rel_line_to(cr, -average_width + 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001141 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001142
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001143 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001144 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001145
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001146 cairo_pop_group_to_source(cr);
1147 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001148 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001149 cairo_surface_destroy(surface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001150
1151 if (terminal->send_cursor_position) {
1152 cursor_x = side_margin + allocation.x +
Peng Wuf291f202013-06-06 15:32:41 +08001153 terminal->column * average_width;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001154 cursor_y = top_margin + allocation.y +
1155 terminal->row * extents.height;
1156 window_set_text_cursor_position(terminal->window,
1157 cursor_x, cursor_y);
1158 terminal->send_cursor_position = 0;
1159 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001160}
1161
1162static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001163terminal_write(struct terminal *terminal, const char *data, size_t length)
1164{
1165 if (write(terminal->master, data, length) < 0)
1166 abort();
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001167 terminal->send_cursor_position = 1;
Kristian Høgsberg26130862011-08-24 11:30:21 -04001168}
1169
1170static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001171terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001172
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001173static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001174handle_char(struct terminal *terminal, union utf8_char utf8);
1175
1176static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001177handle_sgr(struct terminal *terminal, int code);
1178
1179static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001180handle_term_parameter(struct terminal *terminal, int code, int sr)
1181{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001182 int i;
1183
Callum Lowcay67a201d2011-01-12 19:23:41 +13001184 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001185 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001186 case 1: /* DECCKM */
1187 if (sr) terminal->key_mode = KM_APPLICATION;
1188 else terminal->key_mode = KM_NORMAL;
1189 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001190 case 2: /* DECANM */
1191 /* No VT52 support yet */
1192 terminal->g0 = CS_US;
1193 terminal->g1 = CS_US;
1194 terminal->cs = terminal->g0;
1195 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001196 case 3: /* DECCOLM */
1197 if (sr)
1198 terminal_resize(terminal, 132, 24);
1199 else
1200 terminal_resize(terminal, 80, 24);
Michael Vetter2a18a522015-05-15 17:17:47 +02001201
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001202 /* set columns, but also home cursor and clear screen */
1203 terminal->row = 0; terminal->column = 0;
1204 for (i = 0; i < terminal->height; i++) {
1205 memset(terminal_get_row(terminal, i),
1206 0, terminal->data_pitch);
1207 attr_init(terminal_get_attr_row(terminal, i),
1208 terminal->curr_attr, terminal->width);
1209 }
1210 break;
1211 case 5: /* DECSCNM */
1212 if (sr) terminal->mode |= MODE_INVERSE;
1213 else terminal->mode &= ~MODE_INVERSE;
1214 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001215 case 6: /* DECOM */
1216 terminal->origin_mode = sr;
1217 if (terminal->origin_mode)
1218 terminal->row = terminal->margin_top;
1219 else
1220 terminal->row = 0;
1221 terminal->column = 0;
1222 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001223 case 7: /* DECAWM */
1224 if (sr) terminal->mode |= MODE_AUTOWRAP;
1225 else terminal->mode &= ~MODE_AUTOWRAP;
1226 break;
1227 case 8: /* DECARM */
1228 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1229 else terminal->mode &= ~MODE_AUTOREPEAT;
1230 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001231 case 12: /* Very visible cursor (CVVIS) */
1232 /* FIXME: What do we do here. */
1233 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001234 case 25:
1235 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1236 else terminal->mode &= ~MODE_SHOW_CURSOR;
1237 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001238 case 1034: /* smm/rmm, meta mode on/off */
1239 /* ignore */
1240 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001241 case 1037: /* deleteSendsDel */
1242 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1243 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1244 break;
1245 case 1039: /* altSendsEscape */
1246 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1247 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1248 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001249 case 1049: /* rmcup/smcup, alternate screen */
1250 /* Ignore. Should be possible to implement,
1251 * but it's kind of annoying. */
1252 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001253 default:
1254 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1255 break;
1256 }
1257 } else {
1258 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001259 case 4: /* IRM */
1260 if (sr) terminal->mode |= MODE_IRM;
1261 else terminal->mode &= ~MODE_IRM;
1262 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001263 case 20: /* LNM */
1264 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1265 else terminal->mode &= ~MODE_LF_NEWLINE;
1266 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001267 default:
1268 fprintf(stderr, "Unknown parameter: %d\n", code);
1269 break;
1270 }
1271 }
1272}
1273
1274static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001275handle_dcs(struct terminal *terminal)
1276{
1277}
1278
1279static void
1280handle_osc(struct terminal *terminal)
1281{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001282 char *p;
1283 int code;
1284
1285 terminal->escape[terminal->escape_length++] = '\0';
1286 p = &terminal->escape[2];
1287 code = strtol(p, &p, 10);
1288 if (*p == ';') p++;
1289
1290 switch (code) {
1291 case 0: /* Icon name and window title */
1292 case 1: /* Icon label */
1293 case 2: /* Window title*/
Kristian Høgsberga83be202013-10-23 20:47:35 -07001294 free(terminal->title);
1295 terminal->title = strdup(p);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001296 window_set_title(terminal->window, p);
1297 break;
Kristian Høgsberg8a205002013-09-11 11:52:56 -07001298 case 7: /* shell cwd as uri */
1299 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001300 default:
Kristian Høgsberg8a205002013-09-11 11:52:56 -07001301 fprintf(stderr, "Unknown OSC escape code %d, text %s\n",
1302 code, p);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001303 break;
1304 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001305}
1306
1307static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001308handle_escape(struct terminal *terminal)
1309{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001310 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001311 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001312 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001313 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001314 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001315 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001316 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001317
1318 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001319 i = 0;
1320 p = &terminal->escape[2];
1321 while ((isdigit(*p) || *p == ';') && i < 10) {
1322 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001323 if (!set[i]) {
1324 args[i] = 0;
1325 set[i] = 1;
1326 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001327 p++;
1328 i++;
1329 } else {
1330 args[i] = strtol(p, &p, 10);
1331 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001332 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001333 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001334
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001335 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001336 case '@': /* ICH */
1337 count = set[0] ? args[0] : 1;
1338 if (count == 0) count = 1;
1339 terminal_shift_line(terminal, count);
1340 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001341 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001342 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001343 if (count == 0) count = 1;
1344 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001345 terminal->row -= count;
1346 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001347 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001348 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001349 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001350 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001351 if (count == 0) count = 1;
1352 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001353 terminal->row += count;
1354 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001355 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001356 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001357 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001358 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001359 if (count == 0) count = 1;
1360 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001361 terminal->column += count;
1362 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001363 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001364 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001365 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001366 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001367 if (count == 0) count = 1;
1368 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001369 terminal->column -= count;
1370 else
1371 terminal->column = 0;
1372 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001373 case 'E': /* CNL */
1374 count = set[0] ? args[0] : 1;
1375 if (terminal->row + count <= terminal->margin_bottom)
1376 terminal->row += count;
1377 else
1378 terminal->row = terminal->margin_bottom;
1379 terminal->column = 0;
1380 break;
1381 case 'F': /* CPL */
1382 count = set[0] ? args[0] : 1;
1383 if (terminal->row - count >= terminal->margin_top)
1384 terminal->row -= count;
1385 else
1386 terminal->row = terminal->margin_top;
1387 terminal->column = 0;
1388 break;
1389 case 'G': /* CHA */
1390 y = set[0] ? args[0] : 1;
1391 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
Michael Vetter2a18a522015-05-15 17:17:47 +02001392
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001393 terminal->column = y - 1;
1394 break;
1395 case 'f': /* HVP */
1396 case 'H': /* CUP */
1397 x = (set[1] ? args[1] : 1) - 1;
1398 x = x < 0 ? 0 :
1399 (x >= terminal->width ? terminal->width - 1 : x);
Michael Vetter2a18a522015-05-15 17:17:47 +02001400
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001401 y = (set[0] ? args[0] : 1) - 1;
1402 if (terminal->origin_mode) {
1403 y += terminal->margin_top;
1404 y = y < terminal->margin_top ? terminal->margin_top :
1405 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1406 } else {
1407 y = y < 0 ? 0 :
1408 (y >= terminal->height ? terminal->height - 1 : y);
1409 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001410
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001411 terminal->row = y;
1412 terminal->column = x;
1413 break;
1414 case 'I': /* CHT */
1415 count = set[0] ? args[0] : 1;
1416 if (count == 0) count = 1;
1417 while (count > 0 && terminal->column < terminal->width) {
1418 if (terminal->tab_ruler[terminal->column]) count--;
1419 terminal->column++;
1420 }
1421 terminal->column--;
1422 break;
1423 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001424 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001425 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001426 if (!set[0] || args[0] == 0 || args[0] > 2) {
1427 memset(&row[terminal->column],
1428 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1429 attr_init(&attr_row[terminal->column],
1430 terminal->curr_attr, terminal->width - terminal->column);
1431 for (i = terminal->row + 1; i < terminal->height; i++) {
1432 memset(terminal_get_row(terminal, i),
1433 0, terminal->data_pitch);
1434 attr_init(terminal_get_attr_row(terminal, i),
1435 terminal->curr_attr, terminal->width);
1436 }
1437 } else if (args[0] == 1) {
1438 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1439 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1440 for (i = 0; i < terminal->row; i++) {
1441 memset(terminal_get_row(terminal, i),
1442 0, terminal->data_pitch);
1443 attr_init(terminal_get_attr_row(terminal, i),
1444 terminal->curr_attr, terminal->width);
1445 }
1446 } else if (args[0] == 2) {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07001447 /* Clear screen by scrolling contents out */
1448 terminal_scroll_buffer(terminal,
1449 terminal->end - terminal->start);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001450 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001451 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001452 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001453 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001454 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001455 if (!set[0] || args[0] == 0 || args[0] > 2) {
1456 memset(&row[terminal->column], 0,
1457 (terminal->width - terminal->column) * sizeof(union utf8_char));
1458 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1459 terminal->width - terminal->column);
1460 } else if (args[0] == 1) {
1461 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1462 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1463 } else if (args[0] == 2) {
1464 memset(row, 0, terminal->data_pitch);
1465 attr_init(attr_row, terminal->curr_attr, terminal->width);
1466 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001467 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001468 case 'L': /* IL */
1469 count = set[0] ? args[0] : 1;
1470 if (count == 0) count = 1;
1471 if (terminal->row >= terminal->margin_top &&
1472 terminal->row < terminal->margin_bottom)
1473 {
1474 top = terminal->margin_top;
1475 terminal->margin_top = terminal->row;
1476 terminal_scroll(terminal, 0 - count);
1477 terminal->margin_top = top;
1478 } else if (terminal->row == terminal->margin_bottom) {
1479 memset(terminal_get_row(terminal, terminal->row),
1480 0, terminal->data_pitch);
1481 attr_init(terminal_get_attr_row(terminal, terminal->row),
1482 terminal->curr_attr, terminal->width);
1483 }
1484 break;
1485 case 'M': /* DL */
1486 count = set[0] ? args[0] : 1;
1487 if (count == 0) count = 1;
1488 if (terminal->row >= terminal->margin_top &&
1489 terminal->row < terminal->margin_bottom)
1490 {
1491 top = terminal->margin_top;
1492 terminal->margin_top = terminal->row;
1493 terminal_scroll(terminal, count);
1494 terminal->margin_top = top;
1495 } else if (terminal->row == terminal->margin_bottom) {
1496 memset(terminal_get_row(terminal, terminal->row),
1497 0, terminal->data_pitch);
1498 }
1499 break;
1500 case 'P': /* DCH */
1501 count = set[0] ? args[0] : 1;
1502 if (count == 0) count = 1;
1503 terminal_shift_line(terminal, 0 - count);
1504 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001505 case 'S': /* SU */
1506 terminal_scroll(terminal, set[0] ? args[0] : 1);
1507 break;
1508 case 'T': /* SD */
1509 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1510 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001511 case 'X': /* ECH */
1512 count = set[0] ? args[0] : 1;
1513 if (count == 0) count = 1;
1514 if ((terminal->column + count) > terminal->width)
1515 count = terminal->width - terminal->column;
1516 row = terminal_get_row(terminal, terminal->row);
1517 attr_row = terminal_get_attr_row(terminal, terminal->row);
1518 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1519 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1520 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001521 case 'Z': /* CBT */
1522 count = set[0] ? args[0] : 1;
1523 if (count == 0) count = 1;
1524 while (count > 0 && terminal->column >= 0) {
1525 if (terminal->tab_ruler[terminal->column]) count--;
1526 terminal->column--;
1527 }
1528 terminal->column++;
1529 break;
1530 case '`': /* HPA */
1531 y = set[0] ? args[0] : 1;
1532 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
Michael Vetter2a18a522015-05-15 17:17:47 +02001533
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001534 terminal->column = y - 1;
1535 break;
1536 case 'b': /* REP */
1537 count = set[0] ? args[0] : 1;
1538 if (count == 0) count = 1;
1539 if (terminal->last_char.byte[0])
1540 for (i = 0; i < count; i++)
1541 handle_char(terminal, terminal->last_char);
1542 terminal->last_char.byte[0] = 0;
1543 break;
1544 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001545 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001546 break;
1547 case 'd': /* VPA */
1548 x = set[0] ? args[0] : 1;
1549 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
Michael Vetter2a18a522015-05-15 17:17:47 +02001550
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001551 terminal->row = x - 1;
1552 break;
1553 case 'g': /* TBC */
1554 if (!set[0] || args[0] == 0) {
1555 terminal->tab_ruler[terminal->column] = 0;
1556 } else if (args[0] == 3) {
1557 memset(terminal->tab_ruler, 0, terminal->width);
1558 }
1559 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001560 case 'h': /* SM */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001561 for (i = 0; i < 10 && set[i]; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001562 handle_term_parameter(terminal, args[i], 1);
1563 }
1564 break;
1565 case 'l': /* RM */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001566 for (i = 0; i < 10 && set[i]; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001567 handle_term_parameter(terminal, args[i], 0);
1568 }
1569 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001570 case 'm': /* SGR */
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001571 for (i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001572 if (i <= 7 && set[i] && set[i + 1] &&
1573 set[i + 2] && args[i + 1] == 5)
1574 {
1575 if (args[i] == 38) {
1576 handle_sgr(terminal, args[i + 2] + 256);
1577 break;
1578 } else if (args[i] == 48) {
1579 handle_sgr(terminal, args[i + 2] + 512);
1580 break;
1581 }
1582 }
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001583 if (set[i]) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001584 handle_sgr(terminal, args[i]);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001585 } else if (i == 0) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001586 handle_sgr(terminal, 0);
1587 break;
1588 } else {
1589 break;
1590 }
1591 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001592 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001593 case 'n': /* DSR */
1594 i = set[0] ? args[0] : 0;
1595 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001596 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001597 } else if (i == 6) {
1598 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1599 terminal->origin_mode ?
1600 terminal->row+terminal->margin_top : terminal->row+1,
1601 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001602 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001603 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001604 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001605 case 'r':
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001606 if (!set[0]) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001607 terminal->margin_top = 0;
1608 terminal->margin_bottom = terminal->height-1;
1609 terminal->row = 0;
1610 terminal->column = 0;
1611 } else {
1612 top = (set[0] ? args[0] : 1) - 1;
1613 top = top < 0 ? 0 :
1614 (top >= terminal->height ? terminal->height - 1 : top);
1615 bottom = (set[1] ? args[1] : 1) - 1;
1616 bottom = bottom < 0 ? 0 :
1617 (bottom >= terminal->height ? terminal->height - 1 : bottom);
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001618 if (bottom > top) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001619 terminal->margin_top = top;
1620 terminal->margin_bottom = bottom;
1621 } else {
1622 terminal->margin_top = 0;
1623 terminal->margin_bottom = terminal->height-1;
1624 }
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001625 if (terminal->origin_mode)
Callum Lowcaybbeac602011-01-07 19:46:58 +00001626 terminal->row = terminal->margin_top;
1627 else
1628 terminal->row = 0;
1629 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001630 }
1631 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001632 case 's':
1633 terminal->saved_row = terminal->row;
1634 terminal->saved_column = terminal->column;
1635 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001636 case 't': /* windowOps */
1637 if (!set[0]) break;
1638 switch (args[0]) {
1639 case 4: /* resize px */
1640 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001641 widget_schedule_resize(terminal->widget,
1642 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001643 }
1644 break;
1645 case 8: /* resize ch */
1646 if (set[1] && set[2]) {
1647 terminal_resize(terminal, args[2], args[1]);
1648 }
1649 break;
1650 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001651 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001652 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1653 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001654 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001655 break;
1656 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001657 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001658 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1659 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001660 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001661 break;
1662 case 18: /* report ch */
1663 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1664 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001665 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001666 break;
1667 case 21: /* report title */
1668 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1669 window_get_title(terminal->window));
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 default:
1673 if (args[0] >= 24)
1674 terminal_resize(terminal, terminal->width, args[0]);
1675 else
1676 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1677 break;
1678 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001679 case 'u':
1680 terminal->row = terminal->saved_row;
1681 terminal->column = terminal->saved_column;
1682 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001683 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001684 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001685 break;
Michael Vetter2a18a522015-05-15 17:17:47 +02001686 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001687}
1688
1689static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001690handle_non_csi_escape(struct terminal *terminal, char code)
1691{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001692 switch(code) {
1693 case 'M': /* RI */
1694 terminal->row -= 1;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001695 if (terminal->row < terminal->margin_top) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001696 terminal->row = terminal->margin_top;
1697 terminal_scroll(terminal, -1);
1698 }
1699 break;
1700 case 'E': /* NEL */
1701 terminal->column = 0;
1702 // fallthrough
1703 case 'D': /* IND */
1704 terminal->row += 1;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001705 if (terminal->row > terminal->margin_bottom) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001706 terminal->row = terminal->margin_bottom;
1707 terminal_scroll(terminal, +1);
1708 }
1709 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001710 case 'c': /* RIS */
1711 terminal_init(terminal);
1712 break;
1713 case 'H': /* HTS */
1714 terminal->tab_ruler[terminal->column] = 1;
1715 break;
1716 case '7': /* DECSC */
1717 terminal->saved_row = terminal->row;
1718 terminal->saved_column = terminal->column;
1719 terminal->saved_attr = terminal->curr_attr;
1720 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001721 terminal->saved_cs = terminal->cs;
1722 terminal->saved_g0 = terminal->g0;
1723 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001724 break;
1725 case '8': /* DECRC */
1726 terminal->row = terminal->saved_row;
1727 terminal->column = terminal->saved_column;
1728 terminal->curr_attr = terminal->saved_attr;
1729 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001730 terminal->cs = terminal->saved_cs;
1731 terminal->g0 = terminal->saved_g0;
1732 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001733 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001734 case '=': /* DECPAM */
1735 terminal->key_mode = KM_APPLICATION;
1736 break;
1737 case '>': /* DECPNM */
1738 terminal->key_mode = KM_NORMAL;
1739 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001740 default:
1741 fprintf(stderr, "Unknown escape code: %c\n", code);
1742 break;
1743 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001744}
1745
1746static void
1747handle_special_escape(struct terminal *terminal, char special, char code)
1748{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001749 int i, numChars;
1750
1751 if (special == '#') {
1752 switch(code) {
1753 case '8':
1754 /* fill with 'E', no cheap way to do this */
1755 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1756 numChars = terminal->width * terminal->height;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001757 for (i = 0; i < numChars; i++) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001758 terminal->data[i].byte[0] = 'E';
1759 }
1760 break;
1761 default:
1762 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1763 break;
1764 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001765 } else if (special == '(' || special == ')') {
1766 switch(code) {
1767 case '0':
1768 if (special == '(')
1769 terminal->g0 = CS_SPECIAL;
1770 else
1771 terminal->g1 = CS_SPECIAL;
1772 break;
1773 case 'A':
1774 if (special == '(')
1775 terminal->g0 = CS_UK;
1776 else
1777 terminal->g1 = CS_UK;
1778 break;
1779 case 'B':
1780 if (special == '(')
1781 terminal->g0 = CS_US;
1782 else
1783 terminal->g1 = CS_US;
1784 break;
1785 default:
1786 fprintf(stderr, "Unknown character set %c\n", code);
1787 break;
1788 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001789 } else {
1790 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1791 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001792}
1793
1794static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001795handle_sgr(struct terminal *terminal, int code)
1796{
1797 switch(code) {
1798 case 0:
1799 terminal->curr_attr = terminal->color_scheme->default_attr;
1800 break;
1801 case 1:
1802 terminal->curr_attr.a |= ATTRMASK_BOLD;
1803 if (terminal->curr_attr.fg < 8)
1804 terminal->curr_attr.fg += 8;
1805 break;
1806 case 4:
1807 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1808 break;
1809 case 5:
1810 terminal->curr_attr.a |= ATTRMASK_BLINK;
1811 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001812 case 8:
1813 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1814 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001815 case 2:
1816 case 21:
1817 case 22:
1818 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1819 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1820 terminal->curr_attr.fg -= 8;
1821 break;
1822 case 24:
1823 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1824 break;
1825 case 25:
1826 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1827 break;
1828 case 7:
1829 case 26:
1830 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1831 break;
1832 case 27:
1833 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1834 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001835 case 28:
1836 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1837 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001838 case 39:
1839 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1840 break;
1841 case 49:
1842 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1843 break;
1844 default:
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001845 if (code >= 30 && code <= 37) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001846 terminal->curr_attr.fg = code - 30;
1847 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1848 terminal->curr_attr.fg += 8;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001849 } else if (code >= 40 && code <= 47) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001850 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001851 } else if (code >= 90 && code <= 97) {
1852 terminal->curr_attr.fg = code - 90 + 8;
1853 } else if (code >= 100 && code <= 107) {
1854 terminal->curr_attr.bg = code - 100 + 8;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001855 } else if (code >= 256 && code < 512) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001856 terminal->curr_attr.fg = code - 256;
Dawid Gajownik74a635b2015-08-06 17:12:19 -03001857 } else if (code >= 512 && code < 768) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001858 terminal->curr_attr.bg = code - 512;
1859 } else {
1860 fprintf(stderr, "Unknown SGR code: %d\n", code);
1861 }
1862 break;
1863 }
1864}
1865
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001866/* Returns 1 if c was special, otherwise 0 */
1867static int
1868handle_special_char(struct terminal *terminal, char c)
1869{
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001870 union utf8_char *row;
1871 struct attr *attr_row;
1872
1873 row = terminal_get_row(terminal, terminal->row);
1874 attr_row = terminal_get_attr_row(terminal, terminal->row);
1875
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001876 switch(c) {
1877 case '\r':
1878 terminal->column = 0;
1879 break;
1880 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001881 if (terminal->mode & MODE_LF_NEWLINE) {
1882 terminal->column = 0;
1883 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001884 /* fallthrough */
1885 case '\v':
1886 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001887 terminal->row++;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07001888 if (terminal->row > terminal->margin_bottom) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001889 terminal->row = terminal->margin_bottom;
1890 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001891 }
1892
1893 break;
1894 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001895 while (terminal->column < terminal->width) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001896 if (terminal->mode & MODE_IRM)
1897 terminal_shift_line(terminal, +1);
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001898
1899 if (row[terminal->column].byte[0] == '\0') {
1900 row[terminal->column].byte[0] = ' ';
1901 row[terminal->column].byte[1] = '\0';
1902 attr_row[terminal->column] = terminal->curr_attr;
1903 }
1904
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001905 terminal->column++;
Kristian Høgsbergcca3c2f2012-06-20 15:56:13 -04001906 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001907 }
1908 if (terminal->column >= terminal->width) {
1909 terminal->column = terminal->width - 1;
1910 }
1911
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001912 break;
1913 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001914 if (terminal->column >= terminal->width) {
1915 terminal->column = terminal->width - 2;
1916 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001917 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001918 } else if (terminal->mode & MODE_AUTOWRAP) {
1919 terminal->column = terminal->width - 1;
1920 terminal->row -= 1;
1921 if (terminal->row < terminal->margin_top) {
1922 terminal->row = terminal->margin_top;
1923 terminal_scroll(terminal, -1);
1924 }
1925 }
1926
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001927 break;
1928 case '\a':
1929 /* Bell */
1930 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001931 case '\x0E': /* SO */
1932 terminal->cs = terminal->g1;
1933 break;
1934 case '\x0F': /* SI */
1935 terminal->cs = terminal->g0;
1936 break;
Kristian Høgsberg2a1aa4e2012-08-03 09:37:05 -04001937 case '\0':
1938 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001939 default:
1940 return 0;
1941 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001942
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001943 return 1;
1944}
1945
1946static void
1947handle_char(struct terminal *terminal, union utf8_char utf8)
1948{
1949 union utf8_char *row;
1950 struct attr *attr_row;
Michael Vetter2a18a522015-05-15 17:17:47 +02001951
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001952 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001953
1954 apply_char_set(terminal->cs, &utf8);
Michael Vetter2a18a522015-05-15 17:17:47 +02001955
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001956 /* There are a whole lot of non-characters, control codes,
1957 * and formatting codes that should probably be ignored,
1958 * for example: */
1959 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1960 /* BOM, ignore */
1961 return;
Michael Vetter2a18a522015-05-15 17:17:47 +02001962 }
1963
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001964 /* Some of these non-characters should be translated, e.g.: */
1965 if (utf8.byte[0] < 32) {
1966 utf8.byte[0] = utf8.byte[0] + 64;
1967 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001968
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001969 /* handle right margin effects */
1970 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001971 if (terminal->mode & MODE_AUTOWRAP) {
1972 terminal->column = 0;
1973 terminal->row += 1;
1974 if (terminal->row > terminal->margin_bottom) {
1975 terminal->row = terminal->margin_bottom;
1976 terminal_scroll(terminal, +1);
1977 }
1978 } else {
1979 terminal->column--;
1980 }
1981 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001982
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001983 row = terminal_get_row(terminal, terminal->row);
1984 attr_row = terminal_get_attr_row(terminal, terminal->row);
Michael Vetter2a18a522015-05-15 17:17:47 +02001985
Callum Lowcay69e96582011-01-07 19:47:00 +00001986 if (terminal->mode & MODE_IRM)
1987 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001988 row[terminal->column] = utf8;
1989 attr_row[terminal->column++] = terminal->curr_attr;
1990
Kristian Høgsberg1d781ee2013-11-24 16:54:12 -08001991 if (terminal->row + terminal->start + 1 > terminal->end)
1992 terminal->end = terminal->row + terminal->start + 1;
1993 if (terminal->end == terminal->buffer_height)
1994 terminal->log_size = terminal->buffer_height;
1995 else if (terminal->log_size < terminal->buffer_height)
1996 terminal->log_size = terminal->end;
1997
Peng Wucfcc1112013-08-19 10:59:21 +08001998 /* cursor jump for wide character. */
1999 if (is_wide(utf8))
2000 row[terminal->column++].ch = 0x200B; /* space glyph */
2001
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002002 if (utf8.ch != terminal->last_char.ch)
2003 terminal->last_char = utf8;
2004}
2005
Callum Lowcay30eeae52011-01-07 19:46:55 +00002006static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13002007escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
2008{
2009 int len, i;
2010
2011 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
2012 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
2013 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
2014 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
2015 else len = 1; /* Invalid, cannot happen */
2016
2017 if (terminal->escape_length + len <= MAX_ESCAPE) {
2018 for (i = 0; i < len; i++)
2019 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
2020 terminal->escape_length += len;
2021 } else if (terminal->escape_length < MAX_ESCAPE) {
2022 terminal->escape[terminal->escape_length++] = 0;
2023 }
2024}
2025
2026static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002027terminal_data(struct terminal *terminal, const char *data, size_t length)
2028{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002029 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002030 union utf8_char utf8;
2031 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002032
2033 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002034 parser_state =
2035 utf8_next_char(&terminal->state_machine, data[i]);
2036 switch(parser_state) {
2037 case utf8state_accept:
2038 utf8.ch = terminal->state_machine.s.ch;
2039 break;
2040 case utf8state_reject:
2041 /* the unicode replacement character */
2042 utf8.byte[0] = 0xEF;
2043 utf8.byte[1] = 0xBF;
2044 utf8.byte[2] = 0xBD;
2045 utf8.byte[3] = 0x00;
2046 break;
2047 default:
2048 continue;
2049 }
2050
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002051 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13002052 switch (terminal->state) {
2053 case escape_state_escape:
2054 escape_append_utf8(terminal, utf8);
2055 switch (utf8.byte[0]) {
2056 case 'P': /* DCS */
2057 terminal->state = escape_state_dcs;
2058 break;
2059 case '[': /* CSI */
2060 terminal->state = escape_state_csi;
2061 break;
2062 case ']': /* OSC */
2063 terminal->state = escape_state_osc;
2064 break;
2065 case '#':
2066 case '(':
2067 case ')': /* special */
2068 terminal->state = escape_state_special;
2069 break;
2070 case '^': /* PM (not implemented) */
2071 case '_': /* APC (not implemented) */
2072 terminal->state = escape_state_ignore;
2073 break;
2074 default:
2075 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002076 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13002077 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002078 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002079 continue;
2080 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002081 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
2082 /* do nothing */
2083 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002084 terminal->escape_flags |= ESC_FLAG_WHAT;
2085 } else if (utf8.byte[0] == '>') {
2086 terminal->escape_flags |= ESC_FLAG_GT;
2087 } else if (utf8.byte[0] == '!') {
2088 terminal->escape_flags |= ESC_FLAG_BANG;
2089 } else if (utf8.byte[0] == '$') {
2090 terminal->escape_flags |= ESC_FLAG_CASH;
2091 } else if (utf8.byte[0] == '\'') {
2092 terminal->escape_flags |= ESC_FLAG_SQUOTE;
2093 } else if (utf8.byte[0] == '"') {
2094 terminal->escape_flags |= ESC_FLAG_DQUOTE;
2095 } else if (utf8.byte[0] == ' ') {
2096 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002097 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002098 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002099 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13002100 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002101 }
Michael Vetter2a18a522015-05-15 17:17:47 +02002102
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002103 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
2104 utf8.byte[0] == '`')
2105 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002106 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002107 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002108 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002109 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002110 continue;
2111 case escape_state_inner_escape:
2112 if (utf8.byte[0] == '\\') {
2113 terminal->state = escape_state_normal;
2114 if (terminal->outer_state == escape_state_dcs) {
2115 handle_dcs(terminal);
2116 } else if (terminal->outer_state == escape_state_osc) {
2117 handle_osc(terminal);
2118 }
2119 } else if (utf8.byte[0] == '\e') {
2120 terminal->state = terminal->outer_state;
2121 escape_append_utf8(terminal, utf8);
2122 if (terminal->escape_length >= MAX_ESCAPE)
2123 terminal->state = escape_state_normal;
2124 } else {
2125 terminal->state = terminal->outer_state;
2126 if (terminal->escape_length < MAX_ESCAPE)
2127 terminal->escape[terminal->escape_length++] = '\e';
2128 escape_append_utf8(terminal, utf8);
2129 if (terminal->escape_length >= MAX_ESCAPE)
2130 terminal->state = escape_state_normal;
2131 }
2132 continue;
2133 case escape_state_dcs:
2134 case escape_state_osc:
2135 case escape_state_ignore:
2136 if (utf8.byte[0] == '\e') {
2137 terminal->outer_state = terminal->state;
2138 terminal->state = escape_state_inner_escape;
2139 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
2140 terminal->state = escape_state_normal;
2141 handle_osc(terminal);
2142 } else {
2143 escape_append_utf8(terminal, utf8);
2144 if (terminal->escape_length >= MAX_ESCAPE)
2145 terminal->state = escape_state_normal;
2146 }
2147 continue;
2148 case escape_state_special:
2149 escape_append_utf8(terminal, utf8);
2150 terminal->state = escape_state_normal;
2151 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2152 handle_special_escape(terminal, terminal->escape[1],
2153 utf8.byte[0]);
2154 }
2155 continue;
2156 default:
2157 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002158 }
2159
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002160 /* this is valid, because ASCII characters are never used to
2161 * introduce a multibyte sequence in UTF-8 */
2162 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002163 terminal->state = escape_state_escape;
2164 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002165 terminal->escape[0] = '\e';
2166 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002167 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002168 } else {
2169 handle_char(terminal, utf8);
2170 } /* if */
2171 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002172
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002173 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002174}
2175
2176static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002177data_source_target(void *data,
2178 struct wl_data_source *source, const char *mime_type)
2179{
2180 fprintf(stderr, "data_source_target, %s\n", mime_type);
2181}
2182
2183static void
2184data_source_send(void *data,
2185 struct wl_data_source *source,
2186 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002187{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002188 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002189
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002190 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002191}
2192
2193static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002194data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002195{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002196 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002197}
2198
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002199static const struct wl_data_source_listener data_source_listener = {
2200 data_source_target,
2201 data_source_send,
2202 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002203};
2204
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07002205static const char text_mime_type[] = "text/plain;charset=utf-8";
2206
2207static void
2208data_handler(struct window *window,
2209 struct input *input,
2210 float x, float y, const char **types, void *data)
2211{
2212 int i, has_text = 0;
2213
2214 if (!types)
2215 return;
2216 for (i = 0; types[i]; i++)
2217 if (strcmp(types[i], text_mime_type) == 0)
2218 has_text = 1;
2219
2220 if (!has_text) {
2221 input_accept(input, NULL);
2222 } else {
2223 input_accept(input, text_mime_type);
2224 }
2225}
2226
2227static void
2228drop_handler(struct window *window, struct input *input,
2229 int32_t x, int32_t y, void *data)
2230{
2231 struct terminal *terminal = data;
2232
2233 input_receive_drag_data_to_fd(input, text_mime_type, terminal->master);
2234}
2235
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002236static void
2237fullscreen_handler(struct window *window, void *data)
2238{
2239 struct terminal *terminal = data;
2240
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002241 window_set_fullscreen(window, !window_is_fullscreen(terminal->window));
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002242}
2243
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002244static void
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002245close_handler(void *data)
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002246{
2247 struct terminal *terminal = data;
2248
2249 terminal_destroy(terminal);
2250}
2251
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002252static void
2253terminal_copy(struct terminal *terminal, struct input *input)
2254{
2255 terminal->selection =
2256 display_create_data_source(terminal->display);
2257 wl_data_source_offer(terminal->selection,
2258 "text/plain;charset=utf-8");
2259 wl_data_source_add_listener(terminal->selection,
2260 &data_source_listener, terminal);
2261 input_set_selection(input, terminal->selection,
2262 display_get_serial(terminal->display));
2263}
2264
2265static void
2266terminal_paste(struct terminal *terminal, struct input *input)
2267{
2268 input_receive_selection_data_to_fd(input,
2269 "text/plain;charset=utf-8",
2270 terminal->master);
2271
2272}
2273
2274static void
2275terminal_new_instance(struct terminal *terminal)
2276{
2277 struct terminal *new_terminal;
2278
2279 new_terminal = terminal_create(terminal->display);
2280 if (terminal_run(new_terminal, option_shell))
2281 terminal_destroy(new_terminal);
2282}
2283
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002284static int
2285handle_bound_key(struct terminal *terminal,
2286 struct input *input, uint32_t sym, uint32_t time)
2287{
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002288 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002289 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002290 /* Cut selection; terminal doesn't do cut, fall
2291 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002292 case XKB_KEY_C:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002293 terminal_copy(terminal, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002294 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002295 case XKB_KEY_V:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002296 terminal_paste(terminal, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002297 return 1;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002298 case XKB_KEY_N:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002299 terminal_new_instance(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002300 return 1;
2301
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002302 case XKB_KEY_Up:
2303 if (!terminal->scrolling)
2304 terminal->saved_start = terminal->start;
2305 if (terminal->start == terminal->end - terminal->log_size)
2306 return 1;
2307
2308 terminal->scrolling = 1;
2309 terminal->start--;
2310 terminal->row++;
2311 terminal->selection_start_row++;
2312 terminal->selection_end_row++;
2313 widget_schedule_redraw(terminal->widget);
2314 return 1;
2315
2316 case XKB_KEY_Down:
2317 if (!terminal->scrolling)
2318 terminal->saved_start = terminal->start;
2319
2320 if (terminal->start == terminal->saved_start)
2321 return 1;
2322
2323 terminal->scrolling = 1;
2324 terminal->start++;
2325 terminal->row--;
2326 terminal->selection_start_row--;
2327 terminal->selection_end_row--;
2328 widget_schedule_redraw(terminal->widget);
2329 return 1;
2330
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002331 default:
2332 return 0;
2333 }
2334}
2335
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002336static void
2337key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002338 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2339 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002340{
2341 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002342 char ch[MAX_RESPONSE];
Kristian Høgsberg88fd4082012-06-21 15:55:03 -04002343 uint32_t modifiers, serial;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002344 int ret, len = 0, d;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002345 bool convert_utf8 = true;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002346
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002347 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002348 if ((modifiers & MOD_CONTROL_MASK) &&
2349 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002350 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2351 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002352 return;
2353
Daniel Stonea8468712012-11-07 17:51:35 +11002354 /* Map keypad symbols to 'normal' equivalents before processing */
2355 switch (sym) {
2356 case XKB_KEY_KP_Space:
2357 sym = XKB_KEY_space;
2358 break;
2359 case XKB_KEY_KP_Tab:
2360 sym = XKB_KEY_Tab;
2361 break;
2362 case XKB_KEY_KP_Enter:
2363 sym = XKB_KEY_Return;
2364 break;
2365 case XKB_KEY_KP_Left:
2366 sym = XKB_KEY_Left;
2367 break;
2368 case XKB_KEY_KP_Up:
2369 sym = XKB_KEY_Up;
2370 break;
2371 case XKB_KEY_KP_Right:
2372 sym = XKB_KEY_Right;
2373 break;
2374 case XKB_KEY_KP_Down:
2375 sym = XKB_KEY_Down;
2376 break;
2377 case XKB_KEY_KP_Equal:
2378 sym = XKB_KEY_equal;
2379 break;
2380 case XKB_KEY_KP_Multiply:
2381 sym = XKB_KEY_asterisk;
2382 break;
2383 case XKB_KEY_KP_Add:
2384 sym = XKB_KEY_plus;
2385 break;
2386 case XKB_KEY_KP_Separator:
2387 /* Note this is actually locale-dependent and should mostly be
2388 * a comma. But leave it as period until we one day start
2389 * doing the right thing. */
2390 sym = XKB_KEY_period;
2391 break;
2392 case XKB_KEY_KP_Subtract:
2393 sym = XKB_KEY_minus;
2394 break;
2395 case XKB_KEY_KP_Decimal:
2396 sym = XKB_KEY_period;
2397 break;
2398 case XKB_KEY_KP_Divide:
2399 sym = XKB_KEY_slash;
2400 break;
2401 case XKB_KEY_KP_0:
2402 case XKB_KEY_KP_1:
2403 case XKB_KEY_KP_2:
2404 case XKB_KEY_KP_3:
2405 case XKB_KEY_KP_4:
2406 case XKB_KEY_KP_5:
2407 case XKB_KEY_KP_6:
2408 case XKB_KEY_KP_7:
2409 case XKB_KEY_KP_8:
2410 case XKB_KEY_KP_9:
2411 sym = (sym - XKB_KEY_KP_0) + XKB_KEY_0;
2412 break;
2413 default:
2414 break;
2415 }
2416
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002417 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002418 case XKB_KEY_BackSpace:
Kristian Høgsbergb7f94bf2012-06-21 12:29:36 -04002419 if (modifiers & MOD_ALT_MASK)
2420 ch[len++] = 0x1b;
Kristian Høgsberg71a4cf42012-06-20 17:57:56 -04002421 ch[len++] = 0x7f;
2422 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002423 case XKB_KEY_Tab:
2424 case XKB_KEY_Linefeed:
2425 case XKB_KEY_Clear:
2426 case XKB_KEY_Pause:
2427 case XKB_KEY_Scroll_Lock:
2428 case XKB_KEY_Sys_Req:
2429 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002430 ch[len++] = sym & 0x7f;
2431 break;
2432
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002433 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002434 if (terminal->mode & MODE_LF_NEWLINE) {
2435 ch[len++] = 0x0D;
2436 ch[len++] = 0x0A;
2437 } else {
2438 ch[len++] = 0x0D;
2439 }
2440 break;
2441
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002442 case XKB_KEY_Shift_L:
2443 case XKB_KEY_Shift_R:
2444 case XKB_KEY_Control_L:
2445 case XKB_KEY_Control_R:
2446 case XKB_KEY_Alt_L:
2447 case XKB_KEY_Alt_R:
Kristian Høgsberg22fbcf72012-06-22 12:18:56 -04002448 case XKB_KEY_Meta_L:
2449 case XKB_KEY_Meta_R:
2450 case XKB_KEY_Super_L:
2451 case XKB_KEY_Super_R:
2452 case XKB_KEY_Hyper_L:
2453 case XKB_KEY_Hyper_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002454 break;
2455
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002456 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002457 len = function_key_response('[', 2, modifiers, '~', ch);
2458 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002459 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002460 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2461 ch[len++] = '\x04';
2462 } else {
2463 len = function_key_response('[', 3, modifiers, '~', ch);
2464 }
2465 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002466 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002467 len = function_key_response('[', 5, modifiers, '~', ch);
2468 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002469 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002470 len = function_key_response('[', 6, modifiers, '~', ch);
2471 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002472 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002473 len = function_key_response('O', 1, modifiers, 'P', ch);
2474 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002475 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002476 len = function_key_response('O', 1, modifiers, 'Q', ch);
2477 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002478 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002479 len = function_key_response('O', 1, modifiers, 'R', ch);
2480 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002481 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002482 len = function_key_response('O', 1, modifiers, 'S', ch);
2483 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002484 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002485 len = function_key_response('[', 15, modifiers, '~', ch);
2486 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002487 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002488 len = function_key_response('[', 17, modifiers, '~', ch);
2489 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002490 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002491 len = function_key_response('[', 18, modifiers, '~', ch);
2492 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002493 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002494 len = function_key_response('[', 19, modifiers, '~', ch);
2495 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002496 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002497 len = function_key_response('[', 20, modifiers, '~', ch);
2498 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002499 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002500 len = function_key_response('[', 21, modifiers, '~', ch);
2501 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002502 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002503 len = function_key_response('[', 24, modifiers, '~', ch);
2504 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002505 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002506 /* Handle special keys with alternate mappings */
2507 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2508 if (len != 0) break;
Michael Vetter2a18a522015-05-15 17:17:47 +02002509
Kristian Høgsberg70163132012-05-08 15:55:39 -04002510 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002511 if (sym >= '3' && sym <= '7')
2512 sym = (sym & 0x1f) + 8;
2513
2514 if (!((sym >= '!' && sym <= '/') ||
2515 (sym >= '8' && sym <= '?') ||
2516 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2517 else if (sym == '2') sym = 0x00;
2518 else if (sym == '/') sym = 0x1F;
2519 else if (sym == '8' || sym == '?') sym = 0x7F;
Kristian Høgsbergae9e0732012-06-21 12:30:15 -04002520 }
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002521 if (modifiers & MOD_ALT_MASK) {
2522 if (terminal->mode & MODE_ALT_SENDS_ESC) {
2523 ch[len++] = 0x1b;
2524 } else {
2525 sym = sym | 0x80;
2526 convert_utf8 = false;
2527 }
Callum Lowcay7e08e902011-01-07 19:47:02 +00002528 }
2529
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002530 if ((sym < 128) ||
2531 (!convert_utf8 && sym < 256)) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002532 ch[len++] = sym;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002533 } else {
2534 ret = xkb_keysym_to_utf8(sym, ch + len,
2535 MAX_RESPONSE - len);
2536 if (ret < 0)
2537 fprintf(stderr,
2538 "Warning: buffer too small to encode "
2539 "UTF8 character\n");
2540 else
2541 len += ret;
2542 }
2543
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002544 break;
2545 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002546
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002547 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002548 if (terminal->scrolling) {
2549 d = terminal->saved_start - terminal->start;
2550 terminal->row -= d;
2551 terminal->selection_start_row -= d;
2552 terminal->selection_end_row -= d;
2553 terminal->start = terminal->saved_start;
2554 terminal->scrolling = 0;
2555 widget_schedule_redraw(terminal->widget);
2556 }
2557
Kristian Høgsberg26130862011-08-24 11:30:21 -04002558 terminal_write(terminal, ch, len);
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002559
2560 /* Hide cursor, except if this was coming from a
2561 * repeating key press. */
2562 serial = display_get_serial(terminal->display);
2563 if (terminal->hide_cursor_serial != serial) {
2564 input_set_pointer_image(input, CURSOR_BLANK);
2565 terminal->hide_cursor_serial = serial;
2566 }
2567 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002568}
2569
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002570static void
2571keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002572 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002573{
2574 struct terminal *terminal = data;
2575
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002576 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002577}
2578
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002579static int wordsep(int ch)
2580{
2581 const char extra[] = "-,./?%&#:_=+@~";
2582
Derek Foreman7978bc82015-09-02 16:21:54 -05002583 if (ch > 127 || ch < 0)
Andre Heider552d12b2012-08-02 20:59:43 +02002584 return 1;
2585
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002586 return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
2587}
2588
2589static int
2590recompute_selection(struct terminal *terminal)
2591{
2592 struct rectangle allocation;
2593 int col, x, width, height;
2594 int start_row, end_row;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002595 int word_start, eol;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002596 int side_margin, top_margin;
2597 int start_x, end_x;
2598 int cw, ch;
2599 union utf8_char *data;
2600
Peng Wuf291f202013-06-06 15:32:41 +08002601 cw = terminal->average_width;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002602 ch = terminal->extents.height;
2603 widget_get_allocation(terminal->widget, &allocation);
2604 width = terminal->width * cw;
2605 height = terminal->height * ch;
2606 side_margin = allocation.x + (allocation.width - width) / 2;
2607 top_margin = allocation.y + (allocation.height - height) / 2;
2608
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002609 start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1;
2610 end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1;
2611
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002612 if (start_row < end_row ||
2613 (start_row == end_row &&
2614 terminal->selection_start_x < terminal->selection_end_x)) {
2615 terminal->selection_start_row = start_row;
2616 terminal->selection_end_row = end_row;
2617 start_x = terminal->selection_start_x;
2618 end_x = terminal->selection_end_x;
2619 } else {
2620 terminal->selection_start_row = end_row;
2621 terminal->selection_end_row = start_row;
2622 start_x = terminal->selection_end_x;
2623 end_x = terminal->selection_start_x;
2624 }
2625
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002626 eol = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002627 if (terminal->selection_start_row < 0) {
2628 terminal->selection_start_row = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002629 terminal->selection_start_col = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002630 } else {
2631 x = side_margin + cw / 2;
2632 data = terminal_get_row(terminal,
2633 terminal->selection_start_row);
2634 word_start = 0;
2635 for (col = 0; col < terminal->width; col++, x += cw) {
2636 if (col == 0 || wordsep(data[col - 1].ch))
2637 word_start = col;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002638 if (data[col].ch != 0)
2639 eol = col + 1;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002640 if (start_x < x)
2641 break;
2642 }
2643
2644 switch (terminal->dragging) {
2645 case SELECT_LINE:
2646 terminal->selection_start_col = 0;
2647 break;
2648 case SELECT_WORD:
2649 terminal->selection_start_col = word_start;
2650 break;
2651 case SELECT_CHAR:
2652 terminal->selection_start_col = col;
2653 break;
2654 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002655 }
2656
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002657 if (terminal->selection_end_row >= terminal->height) {
2658 terminal->selection_end_row = terminal->height;
2659 terminal->selection_end_col = 0;
2660 } else {
2661 x = side_margin + cw / 2;
2662 data = terminal_get_row(terminal, terminal->selection_end_row);
2663 for (col = 0; col < terminal->width; col++, x += cw) {
2664 if (terminal->dragging == SELECT_CHAR && end_x < x)
2665 break;
2666 if (terminal->dragging == SELECT_WORD &&
2667 end_x < x && wordsep(data[col].ch))
2668 break;
2669 }
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002670 terminal->selection_end_col = col;
2671 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002672
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002673 if (terminal->selection_end_col != terminal->selection_start_col ||
2674 terminal->selection_start_row != terminal->selection_end_row) {
2675 col = terminal->selection_end_col;
2676 if (col > 0 && data[col - 1].ch == 0)
2677 terminal->selection_end_col = terminal->width;
2678 data = terminal_get_row(terminal, terminal->selection_start_row);
2679 if (data[terminal->selection_start_col].ch == 0)
2680 terminal->selection_start_col = eol;
2681 }
2682
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002683 return 1;
2684}
2685
Kristian Høgsberg59826582011-01-20 11:56:57 -05002686static void
Pekka Paalanen807f09c2015-03-27 15:48:54 +02002687terminal_minimize(struct terminal *terminal)
2688{
2689 window_set_minimized(terminal->window);
2690}
2691
2692static void
Jasper St. Pierredda93132014-03-13 12:06:00 -04002693menu_func(void *data, struct input *input, int index)
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002694{
Jasper St. Pierredda93132014-03-13 12:06:00 -04002695 struct window *window = data;
2696 struct terminal *terminal = window_get_user_data(window);
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002697
2698 fprintf(stderr, "picked entry %d\n", index);
2699
2700 switch (index) {
2701 case 0:
2702 terminal_new_instance(terminal);
2703 break;
2704 case 1:
2705 terminal_copy(terminal, input);
2706 break;
2707 case 2:
2708 terminal_paste(terminal, input);
2709 break;
Pekka Paalanen807f09c2015-03-27 15:48:54 +02002710 case 3:
2711 terminal_minimize(terminal);
2712 break;
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002713 }
2714}
2715
2716static void
2717show_menu(struct terminal *terminal, struct input *input, uint32_t time)
2718{
2719 int32_t x, y;
2720 static const char *entries[] = {
Pekka Paalanen807f09c2015-03-27 15:48:54 +02002721 "Open Terminal", "Copy", "Paste", "Minimize"
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002722 };
2723
2724 input_get_position(input, &x, &y);
2725 window_show_menu(terminal->display, input, time, terminal->window,
2726 x - 10, y - 10, menu_func,
2727 entries, ARRAY_LENGTH(entries));
2728}
2729
2730static void
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002731click_handler(struct widget *widget, struct terminal *terminal,
2732 struct input *input, int32_t x, int32_t y,
2733 uint32_t time)
2734{
2735 if (time - terminal->click_time < 500)
2736 terminal->click_count++;
2737 else
2738 terminal->click_count = 1;
2739
2740 terminal->click_time = time;
2741 terminal->dragging = (terminal->click_count - 1) % 3 + SELECT_CHAR;
2742
2743 terminal->selection_end_x = terminal->selection_start_x = x;
2744 terminal->selection_end_y = terminal->selection_start_y = y;
2745 if (recompute_selection(terminal))
2746 widget_schedule_redraw(widget);
2747}
2748
2749static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002750button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002751 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002752 uint32_t button,
2753 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002754{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002755 struct terminal *terminal = data;
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002756 int32_t x, y;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002757
2758 switch (button) {
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002759 case BTN_LEFT:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002760 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002761 input_get_position(input, &x, &y);
2762 click_handler(widget, terminal, input, x, y, time);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002763 } else {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002764 terminal->dragging = SELECT_NONE;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002765 }
2766 break;
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002767
2768 case BTN_RIGHT:
2769 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
2770 show_menu(terminal, input, time);
2771 break;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002772 }
2773}
2774
2775static int
Kristian Høgsberg29784402012-06-28 14:27:02 -04002776enter_handler(struct widget *widget,
2777 struct input *input, float x, float y, void *data)
2778{
2779 return CURSOR_IBEAM;
2780}
2781
2782static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002783motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002784 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002785 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002786{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002787 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002788
2789 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002790 input_get_position(input,
2791 &terminal->selection_end_x,
2792 &terminal->selection_end_y);
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002793
2794 if (recompute_selection(terminal))
2795 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002796 }
2797
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002798 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002799}
2800
Magnus Hoff1046f122014-08-05 15:05:59 +02002801/* This magnitude is chosen rather arbitrarily. Really, the scrolling
2802 * should happen on a (fractional) pixel basis, not a line basis. */
2803#define AXIS_UNITS_PER_LINE 256
2804
2805static void
2806axis_handler(struct widget *widget,
2807 struct input *input, uint32_t time,
2808 uint32_t axis,
2809 wl_fixed_t value,
2810 void *data)
2811{
2812 struct terminal *terminal = data;
2813 int lines;
2814
2815 if (axis != WL_POINTER_AXIS_VERTICAL_SCROLL)
2816 return;
2817
2818 terminal->smooth_scroll += value;
2819 lines = terminal->smooth_scroll / AXIS_UNITS_PER_LINE;
2820 terminal->smooth_scroll -= lines * AXIS_UNITS_PER_LINE;
2821
2822 if (lines > 0) {
2823 if (terminal->scrolling) {
2824 if ((uint32_t)lines > terminal->saved_start - terminal->start)
2825 lines = terminal->saved_start - terminal->start;
2826 } else {
2827 lines = 0;
2828 }
2829 } else if (lines < 0) {
2830 uint32_t neg_lines = -lines;
2831
2832 if (neg_lines > terminal->log_size + terminal->start - terminal->end)
2833 lines = terminal->end - terminal->log_size - terminal->start;
2834 }
2835
2836 if (lines) {
2837 if (!terminal->scrolling)
2838 terminal->saved_start = terminal->start;
2839 terminal->scrolling = 1;
2840
2841 terminal->start += lines;
2842 terminal->row -= lines;
2843 terminal->selection_start_row -= lines;
2844 terminal->selection_end_row -= lines;
2845
2846 widget_schedule_redraw(widget);
2847 }
2848}
2849
Alexander Larssonde79dd02013-05-22 14:41:34 +02002850static void
2851output_handler(struct window *window, struct output *output, int enter,
2852 void *data)
2853{
2854 if (enter)
2855 window_set_buffer_transform(window, output_get_transform(output));
2856 window_set_buffer_scale(window, window_get_output_scale(window));
2857 window_schedule_redraw(window);
2858}
2859
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002860static void
2861touch_down_handler(struct widget *widget, struct input *input,
2862 uint32_t serial, uint32_t time, int32_t id,
2863 float x, float y, void *data)
2864{
2865 struct terminal *terminal = data;
2866
2867 if (id == 0)
2868 click_handler(widget, terminal, input, x, y, time);
2869}
2870
2871static void
2872touch_up_handler(struct widget *widget, struct input *input,
2873 uint32_t serial, uint32_t time, int32_t id, void *data)
2874{
2875 struct terminal *terminal = data;
2876
2877 if (id == 0)
2878 terminal->dragging = SELECT_NONE;
2879}
2880
2881static void
2882touch_motion_handler(struct widget *widget, struct input *input,
2883 uint32_t time, int32_t id, float x, float y, void *data)
2884{
2885 struct terminal *terminal = data;
2886
2887 if (terminal->dragging &&
2888 id == 0) {
2889 terminal->selection_end_x = (int)x;
2890 terminal->selection_end_y = (int)y;
2891
2892 if (recompute_selection(terminal))
2893 widget_schedule_redraw(widget);
2894 }
2895}
2896
Peng Wuf291f202013-06-06 15:32:41 +08002897#ifndef howmany
2898#define howmany(x, y) (((x) + ((y) - 1)) / (y))
2899#endif
2900
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002901static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002902terminal_create(struct display *display)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002903{
2904 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002905 cairo_surface_t *surface;
2906 cairo_t *cr;
Peng Wuf291f202013-06-06 15:32:41 +08002907 cairo_text_extents_t text_extents;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002908
Peter Huttererf3d62272013-08-08 11:57:05 +10002909 terminal = xzalloc(sizeof *terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002910 terminal->color_scheme = &DEFAULT_COLORS;
2911 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002912 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002913 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002914 terminal->window = window_create(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002915 terminal->widget = window_frame_create(terminal->window, terminal);
U. Artie Eoff09827e22014-01-15 11:40:37 -08002916 terminal->title = xstrdup("Wayland Terminal");
Kristian Høgsberga83be202013-10-23 20:47:35 -07002917 window_set_title(terminal->window, terminal->title);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002918 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002919
2920 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002921 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002922
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002923 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002924 terminal->margin = 5;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002925 terminal->buffer_height = 1024;
2926 terminal->end = 1;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002927
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002928 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002929 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002930 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002931 keyboard_focus_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002932 window_set_fullscreen_handler(terminal->window, fullscreen_handler);
Alexander Larssonde79dd02013-05-22 14:41:34 +02002933 window_set_output_handler(terminal->window, output_handler);
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002934 window_set_close_handler(terminal->window, close_handler);
Jasper St. Pierrede680992014-04-10 17:23:49 -07002935 window_set_state_changed_handler(terminal->window, state_changed_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002936
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07002937 window_set_data_handler(terminal->window, data_handler);
2938 window_set_drop_handler(terminal->window, drop_handler);
2939
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002940 widget_set_redraw_handler(terminal->widget, redraw_handler);
2941 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002942 widget_set_button_handler(terminal->widget, button_handler);
Kristian Høgsberg29784402012-06-28 14:27:02 -04002943 widget_set_enter_handler(terminal->widget, enter_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002944 widget_set_motion_handler(terminal->widget, motion_handler);
Magnus Hoff1046f122014-08-05 15:05:59 +02002945 widget_set_axis_handler(terminal->widget, axis_handler);
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002946 widget_set_touch_up_handler(terminal->widget, touch_up_handler);
2947 widget_set_touch_down_handler(terminal->widget, touch_down_handler);
2948 widget_set_touch_motion_handler(terminal->widget, touch_motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002949
Kristian Høgsberg09531622010-06-14 23:22:15 -04002950 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2951 cr = cairo_create(surface);
Kristian Høgsberg38912df2012-06-27 17:52:23 -04002952 cairo_set_font_size(cr, option_font_size);
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002953 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002954 CAIRO_FONT_SLANT_NORMAL,
2955 CAIRO_FONT_WEIGHT_BOLD);
2956 terminal->font_bold = cairo_get_scaled_font (cr);
2957 cairo_scaled_font_reference(terminal->font_bold);
2958
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002959 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002960 CAIRO_FONT_SLANT_NORMAL,
2961 CAIRO_FONT_WEIGHT_NORMAL);
2962 terminal->font_normal = cairo_get_scaled_font (cr);
2963 cairo_scaled_font_reference(terminal->font_normal);
2964
Kristian Høgsberg09531622010-06-14 23:22:15 -04002965 cairo_font_extents(cr, &terminal->extents);
Peng Wuf291f202013-06-06 15:32:41 +08002966
2967 /* Compute the average ascii glyph width */
2968 cairo_text_extents(cr, TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS,
2969 &text_extents);
2970 terminal->average_width = howmany
2971 (text_extents.width,
2972 strlen(TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS));
2973 terminal->average_width = ceil(terminal->average_width);
2974
Kristian Høgsberg09531622010-06-14 23:22:15 -04002975 cairo_destroy(cr);
2976 cairo_surface_destroy(surface);
2977
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002978 terminal_resize(terminal, 20, 5); /* Set minimum size first */
Kristian Høgsberga1627922012-06-20 17:30:03 -04002979 terminal_resize(terminal, 80, 25);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002980
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002981 wl_list_insert(terminal_list.prev, &terminal->link);
2982
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002983 return terminal;
2984}
2985
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002986static void
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002987terminal_destroy(struct terminal *terminal)
2988{
Dima Ryazanova85292e2012-11-29 00:27:09 -08002989 display_unwatch_fd(terminal->display, terminal->master);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002990 window_destroy(terminal->window);
2991 close(terminal->master);
2992 wl_list_remove(&terminal->link);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002993
2994 if (wl_list_empty(&terminal_list))
Dima Ryazanova85292e2012-11-29 00:27:09 -08002995 display_exit(terminal->display);
2996
Kristian Høgsberga83be202013-10-23 20:47:35 -07002997 free(terminal->title);
Dima Ryazanova85292e2012-11-29 00:27:09 -08002998 free(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002999}
3000
3001static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003002io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003003{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003004 struct terminal *terminal =
3005 container_of(task, struct terminal, io_task);
3006 char buffer[256];
3007 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003008
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003009 if (events & EPOLLHUP) {
3010 terminal_destroy(terminal);
3011 return;
3012 }
Tim Wiederhakef71accc2011-01-19 23:14:33 +01003013
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003014 len = read(terminal->master, buffer, sizeof buffer);
3015 if (len < 0)
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003016 terminal_destroy(terminal);
3017 else
3018 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003019}
3020
3021static int
3022terminal_run(struct terminal *terminal, const char *path)
3023{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003024 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003025 pid_t pid;
3026
3027 pid = forkpty(&master, NULL, NULL, NULL);
3028 if (pid == 0) {
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04003029 setenv("TERM", option_term, 1);
3030 setenv("COLORTERM", option_term, 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003031 if (execl(path, path, NULL)) {
3032 printf("exec failed: %m\n");
3033 exit(EXIT_FAILURE);
3034 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003035 } else if (pid < 0) {
3036 fprintf(stderr, "failed to fork and create pty (%m).\n");
3037 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003038 }
3039
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003040 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003041 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003042 terminal->io_task.run = io_handler;
3043 display_watch_fd(terminal->display, terminal->master,
3044 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003045
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07003046 if (option_fullscreen)
3047 window_set_fullscreen(terminal->window, 1);
3048 else
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003049 terminal_resize(terminal, 80, 24);
3050
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003051 return 0;
3052}
3053
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003054static const struct weston_option terminal_options[] = {
3055 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04003056 { WESTON_OPTION_STRING, "font", 0, &option_font },
Bill Spitzak5cad8432014-08-08 12:59:55 -07003057 { WESTON_OPTION_INTEGER, "font-size", 0, &option_font_size },
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003058 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003059};
3060
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003061int main(int argc, char *argv[])
3062{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003063 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003064 struct terminal *terminal;
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02003065 const char *config_file;
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -07003066 struct weston_config *config;
3067 struct weston_config_section *s;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04003068
Peng Wucfcc1112013-08-19 10:59:21 +08003069 /* as wcwidth is locale-dependent,
3070 wcwidth needs setlocale call to function properly. */
3071 setlocale(LC_ALL, "");
3072
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003073 option_shell = getenv("SHELL");
3074 if (!option_shell)
3075 option_shell = "/bin/bash";
3076
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02003077 config_file = weston_config_get_name_from_env();
3078 config = weston_config_parse(config_file);
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -07003079 s = weston_config_get_section(config, "terminal", NULL, NULL);
3080 weston_config_section_get_string(s, "font", &option_font, "mono");
3081 weston_config_section_get_int(s, "font-size", &option_font_size, 14);
3082 weston_config_section_get_string(s, "term", &option_term, "xterm");
3083 weston_config_destroy(config);
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003084
Bill Spitzak5cad8432014-08-08 12:59:55 -07003085 if (parse_options(terminal_options,
3086 ARRAY_LENGTH(terminal_options), &argc, argv) > 1) {
3087 printf("Usage: %s [OPTIONS]\n"
3088 " --fullscreen or -f\n"
3089 " --font=NAME\n"
3090 " --font-size=SIZE\n"
3091 " --shell=NAME\n", argv[0]);
3092 return 1;
3093 }
3094
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003095 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02003096 if (d == NULL) {
3097 fprintf(stderr, "failed to create display: %m\n");
3098 return -1;
3099 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003100
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003101 wl_list_init(&terminal_list);
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04003102 terminal = terminal_create(d);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003103 if (terminal_run(terminal, option_shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003104 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003105
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003106 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003107
3108 return 0;
3109}