blob: ad68a00fb270771bac5ca7bd72fe0778cf71f493 [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"
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050048#include "window.h"
49
Kristian Høgsberg0395f302008-12-22 12:14:50 -050050static int option_fullscreen;
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -070051static char *option_font;
52static int option_font_size;
53static char *option_term;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040054static char *option_shell;
55
56static struct wl_list terminal_list;
57
58static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -040059terminal_create(struct display *display);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040060static void
61terminal_destroy(struct terminal *terminal);
62static int
63terminal_run(struct terminal *terminal, const char *path);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050064
Peng Wuf291f202013-06-06 15:32:41 +080065#define TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS \
66 " !\"#$%&'()*+,-./" \
67 "0123456789" \
68 ":;<=>?@" \
69 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
70 "[\\]^_`" \
71 "abcdefghijklmnopqrstuvwxyz" \
72 "{|}~" \
73 ""
74
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050075#define MOD_SHIFT 0x01
76#define MOD_ALT 0x02
77#define MOD_CTRL 0x04
78
Callum Lowcay30eeae52011-01-07 19:46:55 +000079#define ATTRMASK_BOLD 0x01
80#define ATTRMASK_UNDERLINE 0x02
81#define ATTRMASK_BLINK 0x04
82#define ATTRMASK_INVERSE 0x08
Callum Lowcay81179db2011-01-10 12:14:01 +130083#define ATTRMASK_CONCEALED 0x10
Callum Lowcay30eeae52011-01-07 19:46:55 +000084
Callum Lowcayb8609ad2011-01-07 19:46:57 +000085/* Buffer sizes */
Callum Lowcayef57a9b2011-01-14 20:46:23 +130086#define MAX_RESPONSE 256
87#define MAX_ESCAPE 255
Callum Lowcayb8609ad2011-01-07 19:46:57 +000088
Callum Lowcay8e57dd52011-01-07 19:46:59 +000089/* Terminal modes */
90#define MODE_SHOW_CURSOR 0x00000001
91#define MODE_INVERSE 0x00000002
92#define MODE_AUTOWRAP 0x00000004
93#define MODE_AUTOREPEAT 0x00000008
94#define MODE_LF_NEWLINE 0x00000010
Callum Lowcay69e96582011-01-07 19:47:00 +000095#define MODE_IRM 0x00000020
Callum Lowcay7e08e902011-01-07 19:47:02 +000096#define MODE_DELETE_SENDS_DEL 0x00000040
97#define MODE_ALT_SENDS_ESC 0x00000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +000098
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000099union utf8_char {
100 unsigned char byte[4];
101 uint32_t ch;
102};
103
104enum utf8_state {
105 utf8state_start,
106 utf8state_accept,
107 utf8state_reject,
108 utf8state_expect3,
109 utf8state_expect2,
110 utf8state_expect1
111};
112
113struct utf8_state_machine {
114 enum utf8_state state;
115 int len;
116 union utf8_char s;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700117 uint32_t unicode;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000118};
119
120static void
121init_state_machine(struct utf8_state_machine *machine)
122{
123 machine->state = utf8state_start;
124 machine->len = 0;
125 machine->s.ch = 0;
126}
127
128static enum utf8_state
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400129utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000130{
131 switch(machine->state) {
132 case utf8state_start:
133 case utf8state_accept:
134 case utf8state_reject:
135 machine->s.ch = 0;
136 machine->len = 0;
137 if(c == 0xC0 || c == 0xC1) {
138 /* overlong encoding, reject */
139 machine->state = utf8state_reject;
140 } else if((c & 0x80) == 0) {
141 /* single byte, accept */
142 machine->s.byte[machine->len++] = c;
143 machine->state = utf8state_accept;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700144 machine->unicode = c;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000145 } else if((c & 0xC0) == 0x80) {
146 /* parser out of sync, ignore byte */
147 machine->state = utf8state_start;
148 } else if((c & 0xE0) == 0xC0) {
149 /* start of two byte sequence */
150 machine->s.byte[machine->len++] = c;
151 machine->state = utf8state_expect1;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700152 machine->unicode = c & 0x1f;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000153 } else if((c & 0xF0) == 0xE0) {
154 /* start of three byte sequence */
155 machine->s.byte[machine->len++] = c;
156 machine->state = utf8state_expect2;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700157 machine->unicode = c & 0x0f;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000158 } else if((c & 0xF8) == 0xF0) {
159 /* start of four byte sequence */
160 machine->s.byte[machine->len++] = c;
161 machine->state = utf8state_expect3;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700162 machine->unicode = c & 0x07;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000163 } else {
164 /* overlong encoding, reject */
165 machine->state = utf8state_reject;
166 }
167 break;
168 case utf8state_expect3:
169 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700170 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000171 if((c & 0xC0) == 0x80) {
172 /* all good, continue */
173 machine->state = utf8state_expect2;
174 } else {
175 /* missing extra byte, reject */
176 machine->state = utf8state_reject;
177 }
178 break;
179 case utf8state_expect2:
180 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700181 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000182 if((c & 0xC0) == 0x80) {
183 /* all good, continue */
184 machine->state = utf8state_expect1;
185 } else {
186 /* missing extra byte, reject */
187 machine->state = utf8state_reject;
188 }
189 break;
190 case utf8state_expect1:
191 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700192 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000193 if((c & 0xC0) == 0x80) {
194 /* all good, accept */
195 machine->state = utf8state_accept;
196 } else {
197 /* missing extra byte, reject */
198 machine->state = utf8state_reject;
199 }
200 break;
201 default:
202 machine->state = utf8state_reject;
203 break;
204 }
Michael Vetter2a18a522015-05-15 17:17:47 +0200205
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000206 return machine->state;
207}
208
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700209static uint32_t
210get_unicode(union utf8_char utf8)
211{
212 struct utf8_state_machine machine;
213 int i;
214
215 init_state_machine(&machine);
216 for (i = 0; i < 4; i++) {
217 utf8_next_char(&machine, utf8.byte[i]);
218 if (machine.state == utf8state_accept ||
219 machine.state == utf8state_reject)
220 break;
221 }
222
223 if (machine.state == utf8state_reject)
224 return 0xfffd;
225
226 return machine.unicode;
227}
228
Peng Wucfcc1112013-08-19 10:59:21 +0800229static bool
230is_wide(union utf8_char utf8)
231{
232 uint32_t unichar = get_unicode(utf8);
233 return wcwidth(unichar) > 1;
234}
235
Callum Lowcay256e72f2011-01-07 19:47:01 +0000236struct char_sub {
237 union utf8_char match;
238 union utf8_char replace;
239};
240/* Set last char_sub match to NULL char */
241typedef struct char_sub *character_set;
242
243struct char_sub CS_US[] = {
244 {{{0, }}, {{0, }}}
245};
246static struct char_sub CS_UK[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200247 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000248 {{{0, }}, {{0, }}}
249};
250static struct char_sub CS_SPECIAL[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200251 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond: ♦ */
252 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell: ▒ */
253 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT: ␉ */
254 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF: ␌ */
255 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR: ␍ */
256 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF: ␊ */
257 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree: ° */
258 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus: ± */
259 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL: ␤ */
260 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT: ␋ */
261 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB: ┘ */
262 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT: ┐ */
263 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT: ┌ */
264 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_LB: └ */
265 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS: ┼ */
266 {{{'o', 0, }}, {{0xE2, 0x8E, 0xBA, 0}}}, /* Horiz. Scan Line 1: ⎺ */
267 {{{'p', 0, }}, {{0xE2, 0x8E, 0xBB, 0}}}, /* Horiz. Scan Line 3: ⎻ */
268 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* Horiz. Scan Line 5: ─ */
269 {{{'r', 0, }}, {{0xE2, 0x8E, 0xBC, 0}}}, /* Horiz. Scan Line 7: ⎼ */
270 {{{'s', 0, }}, {{0xE2, 0x8E, 0xBD, 0}}}, /* Horiz. Scan Line 9: ⎽ */
271 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR: ├ */
272 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL: ┤ */
273 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU: ┴ */
274 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD: ┬ */
275 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V: │ */
276 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE: ≤ */
277 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE: ≥ */
278 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI: π */
279 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ: ≠ */
280 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
281 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT: ⋅ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000282 {{{0, }}, {{0, }}}
283};
284
285static void
286apply_char_set(character_set cs, union utf8_char *utf8)
287{
288 int i = 0;
Michael Vetter2a18a522015-05-15 17:17:47 +0200289
Callum Lowcay256e72f2011-01-07 19:47:01 +0000290 while (cs[i].match.byte[0]) {
291 if ((*utf8).ch == cs[i].match.ch) {
292 *utf8 = cs[i].replace;
293 break;
294 }
295 i++;
296 }
297}
298
Callum Lowcay7e08e902011-01-07 19:47:02 +0000299struct key_map {
300 int sym;
301 int num;
302 char escape;
303 char code;
304};
305/* Set last key_sub sym to NULL */
306typedef struct key_map *keyboard_mode;
307
308static struct key_map KM_NORMAL[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400309 { XKB_KEY_Left, 1, '[', 'D' },
310 { XKB_KEY_Right, 1, '[', 'C' },
311 { XKB_KEY_Up, 1, '[', 'A' },
312 { XKB_KEY_Down, 1, '[', 'B' },
313 { XKB_KEY_Home, 1, '[', 'H' },
314 { XKB_KEY_End, 1, '[', 'F' },
315 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000316};
317static struct key_map KM_APPLICATION[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400318 { XKB_KEY_Left, 1, 'O', 'D' },
319 { XKB_KEY_Right, 1, 'O', 'C' },
320 { XKB_KEY_Up, 1, 'O', 'A' },
321 { XKB_KEY_Down, 1, 'O', 'B' },
322 { XKB_KEY_Home, 1, 'O', 'H' },
323 { XKB_KEY_End, 1, 'O', 'F' },
324 { XKB_KEY_KP_Enter, 1, 'O', 'M' },
325 { XKB_KEY_KP_Multiply, 1, 'O', 'j' },
326 { XKB_KEY_KP_Add, 1, 'O', 'k' },
327 { XKB_KEY_KP_Separator, 1, 'O', 'l' },
328 { XKB_KEY_KP_Subtract, 1, 'O', 'm' },
329 { XKB_KEY_KP_Divide, 1, 'O', 'o' },
330 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000331};
332
333static int
334function_key_response(char escape, int num, uint32_t modifiers,
335 char code, char *response)
336{
337 int mod_num = 0;
338 int len;
339
Kristian Høgsberg70163132012-05-08 15:55:39 -0400340 if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
341 if (modifiers & MOD_ALT_MASK) mod_num |= 2;
342 if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000343
344 if (mod_num != 0)
345 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
346 num, mod_num + 1, code);
347 else if (code != '~')
348 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
349 escape, code);
350 else
351 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
352 escape, num, code);
353
354 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
355 else return len;
356}
357
358/* returns the number of bytes written into response,
359 * which must have room for MAX_RESPONSE bytes */
360static int
361apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
362{
363 struct key_map map;
364 int len = 0;
365 int i = 0;
Michael Vetter2a18a522015-05-15 17:17:47 +0200366
Callum Lowcay7e08e902011-01-07 19:47:02 +0000367 while (mode[i].sym) {
368 map = mode[i++];
369 if (sym == map.sym) {
370 len = function_key_response(map.escape, map.num,
371 modifiers, map.code,
372 response);
373 break;
374 }
375 }
Michael Vetter2a18a522015-05-15 17:17:47 +0200376
Callum Lowcay7e08e902011-01-07 19:47:02 +0000377 return len;
378}
379
Callum Lowcay30eeae52011-01-07 19:46:55 +0000380struct terminal_color { double r, g, b, a; };
381struct attr {
382 unsigned char fg, bg;
383 char a; /* attributes format:
384 * 76543210
Callum Lowcay81179db2011-01-10 12:14:01 +1300385 * cilub */
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500386 char s; /* in selection */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000387};
388struct color_scheme {
389 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500390 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000391 struct attr default_attr;
392};
393
394static void
395attr_init(struct attr *data_attr, struct attr attr, int n)
396{
397 int i;
398 for (i = 0; i < n; i++) {
399 data_attr[i] = attr;
400 }
401}
402
Callum Lowcay67a201d2011-01-12 19:23:41 +1300403enum escape_state {
404 escape_state_normal = 0,
405 escape_state_escape,
406 escape_state_dcs,
407 escape_state_csi,
408 escape_state_osc,
409 escape_state_inner_escape,
410 escape_state_ignore,
411 escape_state_special
412};
413
414#define ESC_FLAG_WHAT 0x01
415#define ESC_FLAG_GT 0x02
416#define ESC_FLAG_BANG 0x04
417#define ESC_FLAG_CASH 0x08
418#define ESC_FLAG_SQUOTE 0x10
419#define ESC_FLAG_DQUOTE 0x20
420#define ESC_FLAG_SPACE 0x40
421
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400422enum {
423 SELECT_NONE,
424 SELECT_CHAR,
425 SELECT_WORD,
426 SELECT_LINE
427};
428
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500429struct terminal {
430 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500431 struct widget *widget;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500432 struct display *display;
Kristian Høgsberga83be202013-10-23 20:47:35 -0700433 char *title;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000434 union utf8_char *data;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400435 struct task io_task;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000436 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000437 struct attr *data_attr;
438 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000439 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000440 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000441 char saved_origin_mode;
442 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000443 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000444 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000445 character_set cs, g0, g1;
446 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000447 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000448 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700449 int width, height, row, column, max_width;
450 uint32_t buffer_height;
451 uint32_t start, end, saved_start, log_size;
Magnus Hoff1046f122014-08-05 15:05:59 +0200452 wl_fixed_t smooth_scroll;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000453 int saved_row, saved_column;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700454 int scrolling;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600455 int send_cursor_position;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500456 int fd, master;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500457 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300458 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500459 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300460 enum escape_state state;
461 enum escape_state outer_state;
462 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000463 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500464 int margin;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400465 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000466 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400467 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +0800468 double average_width;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500469 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg88fd4082012-06-21 15:55:03 -0400470 uint32_t hide_cursor_serial;
Kristian Høgsbergb405a802014-02-05 14:44:04 -0800471 int size_in_title;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500472
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400473 struct wl_data_source *selection;
Xiong Zhang49c6aee2013-11-25 18:42:52 +0800474 uint32_t click_time;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400475 int dragging, click_count;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500476 int selection_start_x, selection_start_y;
477 int selection_end_x, selection_end_y;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400478 int selection_start_row, selection_start_col;
479 int selection_end_row, selection_end_col;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -0400480 struct wl_list link;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500481};
482
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000483/* Create default tab stops, every 8 characters */
484static void
485terminal_init_tabs(struct terminal *terminal)
486{
487 int i = 0;
Michael Vetter2a18a522015-05-15 17:17:47 +0200488
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000489 while (i < terminal->width) {
490 if (i % 8 == 0)
491 terminal->tab_ruler[i] = 1;
492 else
493 terminal->tab_ruler[i] = 0;
494 i++;
495 }
496}
497
Callum Lowcay30eeae52011-01-07 19:46:55 +0000498static void
499terminal_init(struct terminal *terminal)
500{
501 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000502 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000503 terminal->mode = MODE_SHOW_CURSOR |
504 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000505 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000506 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000507
508 terminal->row = 0;
509 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000510
Callum Lowcay256e72f2011-01-07 19:47:01 +0000511 terminal->g0 = CS_US;
512 terminal->g1 = CS_US;
513 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000514 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000515
516 terminal->saved_g0 = terminal->g0;
517 terminal->saved_g1 = terminal->g1;
518 terminal->saved_cs = terminal->cs;
519
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000520 terminal->saved_attr = terminal->curr_attr;
521 terminal->saved_origin_mode = terminal->origin_mode;
522 terminal->saved_row = terminal->row;
523 terminal->saved_column = terminal->column;
524
525 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000526}
527
528static void
529init_color_table(struct terminal *terminal)
530{
531 int c, r;
532 struct terminal_color *color_table = terminal->color_table;
533
534 for (c = 0; c < 256; c ++) {
535 if (c < 16) {
536 color_table[c] = terminal->color_scheme->palette[c];
537 } else if (c < 232) {
538 r = c - 16;
539 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
540 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
541 color_table[c].r = ((double)(r % 6) / 6.0);
542 color_table[c].a = 1.0;
543 } else {
544 r = (c - 232) * 10 + 8;
545 color_table[c].r = ((double) r) / 256.0;
546 color_table[c].g = color_table[c].r;
547 color_table[c].b = color_table[c].r;
548 color_table[c].a = 1.0;
549 }
550 }
551}
552
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000553static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500554terminal_get_row(struct terminal *terminal, int row)
555{
556 int index;
557
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700558 index = (row + terminal->start) & (terminal->buffer_height - 1);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500559
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700560 return (void *) terminal->data + index * terminal->data_pitch;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500561}
562
Callum Lowcay30eeae52011-01-07 19:46:55 +0000563static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500564terminal_get_attr_row(struct terminal *terminal, int row)
565{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000566 int index;
567
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700568 index = (row + terminal->start) & (terminal->buffer_height - 1);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000569
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700570 return (void *) terminal->data_attr + index * terminal->attr_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000571}
572
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500573union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300574 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500575 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500576};
577
578static void
579terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500580 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500581{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500582 struct attr attr;
583 int foreground, background, tmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500584
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500585 decoded->attr.s = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400586 if (((row == terminal->selection_start_row &&
587 col >= terminal->selection_start_col) ||
588 row > terminal->selection_start_row) &&
589 ((row == terminal->selection_end_row &&
590 col < terminal->selection_end_col) ||
591 row < terminal->selection_end_row))
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500592 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500593
594 /* get the attributes for this character cell */
595 attr = terminal_get_attr_row(terminal, row)[col];
596 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500597 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500598 ((terminal->mode & MODE_SHOW_CURSOR) &&
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400599 window_has_focus(terminal->window) && terminal->row == row &&
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500600 terminal->column == col)) {
601 foreground = attr.bg;
602 background = attr.fg;
603 if (attr.a & ATTRMASK_BOLD) {
604 if (foreground <= 16) foreground |= 0x08;
605 if (background <= 16) background &= 0x07;
606 }
607 } else {
608 foreground = attr.fg;
609 background = attr.bg;
610 }
611
612 if (terminal->mode & MODE_INVERSE) {
613 tmp = foreground;
614 foreground = background;
615 background = tmp;
616 if (attr.a & ATTRMASK_BOLD) {
617 if (foreground <= 16) foreground |= 0x08;
618 if (background <= 16) background &= 0x07;
619 }
620 }
621
Callum Lowcay9d708b02011-01-12 20:06:17 +1300622 decoded->attr.fg = foreground;
623 decoded->attr.bg = background;
624 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000625}
626
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500627
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500628static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000629terminal_scroll_buffer(struct terminal *terminal, int d)
630{
631 int i;
632
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700633 terminal->start += d;
634 if (d < 0) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000635 d = 0 - d;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700636 for (i = 0; i < d; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000637 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
638 attr_init(terminal_get_attr_row(terminal, i),
639 terminal->curr_attr, terminal->width);
640 }
641 } else {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700642 for (i = terminal->height - d; i < terminal->height; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000643 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
644 attr_init(terminal_get_attr_row(terminal, i),
645 terminal->curr_attr, terminal->width);
646 }
647 }
Kristian Høgsberg18e928d2012-06-27 19:29:41 -0400648
649 terminal->selection_start_row -= d;
650 terminal->selection_end_row -= d;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000651}
652
653static void
654terminal_scroll_window(struct terminal *terminal, int d)
655{
656 int i;
657 int window_height;
658 int from_row, to_row;
Michael Vetter2a18a522015-05-15 17:17:47 +0200659
Callum Lowcaybbeac602011-01-07 19:46:58 +0000660 // scrolling range is inclusive
661 window_height = terminal->margin_bottom - terminal->margin_top + 1;
662 d = d % (window_height + 1);
663 if(d < 0) {
664 d = 0 - d;
665 to_row = terminal->margin_bottom;
666 from_row = terminal->margin_bottom - d;
Michael Vetter2a18a522015-05-15 17:17:47 +0200667
Callum Lowcaybbeac602011-01-07 19:46:58 +0000668 for (i = 0; i < (window_height - d); i++) {
669 memcpy(terminal_get_row(terminal, to_row - i),
670 terminal_get_row(terminal, from_row - i),
671 terminal->data_pitch);
672 memcpy(terminal_get_attr_row(terminal, to_row - i),
673 terminal_get_attr_row(terminal, from_row - i),
674 terminal->attr_pitch);
675 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000676 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
677 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000678 attr_init(terminal_get_attr_row(terminal, i),
679 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000680 }
681 } else {
682 to_row = terminal->margin_top;
683 from_row = terminal->margin_top + d;
Michael Vetter2a18a522015-05-15 17:17:47 +0200684
Callum Lowcaybbeac602011-01-07 19:46:58 +0000685 for (i = 0; i < (window_height - d); i++) {
686 memcpy(terminal_get_row(terminal, to_row + i),
687 terminal_get_row(terminal, from_row + i),
688 terminal->data_pitch);
689 memcpy(terminal_get_attr_row(terminal, to_row + i),
690 terminal_get_attr_row(terminal, from_row + i),
691 terminal->attr_pitch);
692 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000693 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
694 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000695 attr_init(terminal_get_attr_row(terminal, i),
696 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000697 }
698 }
699}
700
701static void
702terminal_scroll(struct terminal *terminal, int d)
703{
704 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
705 terminal_scroll_buffer(terminal, d);
706 else
707 terminal_scroll_window(terminal, d);
708}
709
710static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000711terminal_shift_line(struct terminal *terminal, int d)
712{
713 union utf8_char *row;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500714 struct attr *attr_row;
Michael Vetter2a18a522015-05-15 17:17:47 +0200715
Callum Lowcay69e96582011-01-07 19:47:00 +0000716 row = terminal_get_row(terminal, terminal->row);
717 attr_row = terminal_get_attr_row(terminal, terminal->row);
718
719 if ((terminal->width + d) <= terminal->column)
720 d = terminal->column + 1 - terminal->width;
721 if ((terminal->column + d) >= terminal->width)
722 d = terminal->width - terminal->column - 1;
Michael Vetter2a18a522015-05-15 17:17:47 +0200723
Callum Lowcay69e96582011-01-07 19:47:00 +0000724 if (d < 0) {
725 d = 0 - d;
726 memmove(&row[terminal->column],
727 &row[terminal->column + d],
728 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
Callum Lowcay69e96582011-01-07 19:47:00 +0000729 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
730 (terminal->width - terminal->column - d) * sizeof(struct attr));
731 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
732 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
733 } else {
734 memmove(&row[terminal->column + d], &row[terminal->column],
735 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
736 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
737 (terminal->width - terminal->column - d) * sizeof(struct attr));
738 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
739 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
740 }
741}
742
743static void
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700744terminal_resize_cells(struct terminal *terminal,
745 int width, int height)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500746{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000747 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000748 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000749 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000750 int data_pitch, attr_pitch;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500751 int i, l, total_rows;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700752 uint32_t d, uheight = height;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500753 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000754 struct winsize ws;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500755
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700756 if (uheight > terminal->buffer_height)
757 height = terminal->buffer_height;
758
Kristian Høgsberg22106762008-12-08 13:50:07 -0500759 if (terminal->width == width && terminal->height == height)
760 return;
761
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700762 if (terminal->data && width <= terminal->max_width) {
763 d = 0;
764 if (height < terminal->height && height <= terminal->row)
765 d = terminal->height - height;
766 else if (height > terminal->height &&
767 terminal->height - 1 == terminal->row) {
768 d = terminal->height - height;
769 if (terminal->log_size < uheight)
770 d = -terminal->start;
771 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500772
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700773 terminal->start += d;
774 terminal->row -= d;
775 } else {
776 terminal->max_width = width;
777 data_pitch = width * sizeof(union utf8_char);
Bryce W. Harrington66cd2c12014-04-28 18:44:08 +0000778 data = xzalloc(data_pitch * terminal->buffer_height);
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700779 attr_pitch = width * sizeof(struct attr);
Bryce W. Harrington66cd2c12014-04-28 18:44:08 +0000780 data_attr = xmalloc(attr_pitch * terminal->buffer_height);
781 tab_ruler = xzalloc(width);
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700782 attr_init(data_attr, terminal->curr_attr,
783 width * terminal->buffer_height);
784
785 if (terminal->data && terminal->data_attr) {
786 if (width > terminal->width)
787 l = terminal->width;
788 else
789 l = width;
790
791 if (terminal->height > height) {
792 total_rows = height;
793 i = 1 + terminal->row - height;
794 if (i > 0) {
795 terminal->start += i;
796 terminal->row = terminal->row - i;
797 }
798 } else {
799 total_rows = terminal->height;
José Bollo4a4704a2013-09-13 11:28:51 +0200800 }
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700801
802 for (i = 0; i < total_rows; i++) {
803 memcpy(&data[width * i],
804 terminal_get_row(terminal, i),
805 l * sizeof(union utf8_char));
806 memcpy(&data_attr[width * i],
807 terminal_get_attr_row(terminal, i),
808 l * sizeof(struct attr));
809 }
810
811 free(terminal->data);
812 free(terminal->data_attr);
813 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500814 }
815
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700816 terminal->data_pitch = data_pitch;
817 terminal->attr_pitch = attr_pitch;
818 terminal->data = data;
819 terminal->data_attr = data_attr;
820 terminal->tab_ruler = tab_ruler;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700821 terminal->start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500822 }
823
Callum Lowcay86653ed2011-01-07 19:47:03 +0000824 terminal->margin_bottom =
825 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500826 terminal->width = width;
827 terminal->height = height;
Kristian Høgsbergdcfff552013-11-22 22:43:20 -0800828 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500829
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000830 /* Update the window size */
831 ws.ws_row = terminal->height;
832 ws.ws_col = terminal->width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500833 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500834 ws.ws_xpixel = allocation.width;
835 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000836 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500837}
838
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500839static void
Jasper St. Pierrede680992014-04-10 17:23:49 -0700840update_title(struct terminal *terminal)
841{
842 if (window_is_resizing(terminal->window)) {
843 char *p;
844 if (asprintf(&p, "%s — [%dx%d]", terminal->title, terminal->width, terminal->height) > 0) {
845 window_set_title(terminal->window, p);
846 free(p);
847 }
848 } else {
849 window_set_title(terminal->window, terminal->title);
850 }
851}
852
853static void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500854resize_handler(struct widget *widget,
855 int32_t width, int32_t height, void *data)
856{
857 struct terminal *terminal = data;
858 int32_t columns, rows, m;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700859
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500860 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800861 columns = (width - m) / (int32_t) terminal->average_width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500862 rows = (height - m) / (int32_t) terminal->extents.height;
863
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400864 if (!window_is_fullscreen(terminal->window) &&
865 !window_is_maximized(terminal->window)) {
Peng Wuf291f202013-06-06 15:32:41 +0800866 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500867 height = rows * terminal->extents.height + m;
868 widget_set_size(terminal->widget, width, height);
869 }
870
871 terminal_resize_cells(terminal, columns, rows);
Jasper St. Pierrede680992014-04-10 17:23:49 -0700872 update_title(terminal);
873}
874
875static void
876state_changed_handler(struct window *window, void *data)
877{
878 struct terminal *terminal = data;
879 update_title(terminal);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500880}
881
882static void
883terminal_resize(struct terminal *terminal, int columns, int rows)
884{
885 int32_t width, height, m;
886
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400887 if (window_is_fullscreen(terminal->window) ||
888 window_is_maximized(terminal->window))
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500889 return;
890
891 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800892 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500893 height = rows * terminal->extents.height + m;
Kristian Høgsberga1627922012-06-20 17:30:03 -0400894
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500895 window_frame_set_child_size(terminal->widget, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500896}
897
Callum Lowcay30eeae52011-01-07 19:46:55 +0000898struct color_scheme DEFAULT_COLORS = {
899 {
900 {0, 0, 0, 1}, /* black */
901 {0.66, 0, 0, 1}, /* red */
902 {0 , 0.66, 0, 1}, /* green */
903 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
904 {0 , 0 , 0.66, 1}, /* blue */
905 {0.66, 0 , 0.66, 1}, /* magenta */
906 {0, 0.66, 0.66, 1}, /* cyan */
907 {0.66, 0.66, 0.66, 1}, /* light grey */
908 {0.22, 0.33, 0.33, 1}, /* dark grey */
909 {1, 0.33, 0.33, 1}, /* high red */
910 {0.33, 1, 0.33, 1}, /* high green */
911 {1, 1, 0.33, 1}, /* high yellow */
912 {0.33, 0.33, 1, 1}, /* high blue */
913 {1, 0.33, 1, 1}, /* high magenta */
914 {0.33, 1, 1, 1}, /* high cyan */
915 {1, 1, 1, 1} /* white */
916 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500917 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000918 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
919};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400920
Kristian Høgsberg22106762008-12-08 13:50:07 -0500921static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500922terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
923{
924 cairo_set_source_rgba(cr,
925 terminal->color_table[index].r,
926 terminal->color_table[index].g,
927 terminal->color_table[index].b,
928 terminal->color_table[index].a);
929}
930
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500931static void
932terminal_send_selection(struct terminal *terminal, int fd)
933{
934 int row, col;
935 union utf8_char *p_row;
936 union decoded_attr attr;
937 FILE *fp;
938 int len;
939
940 fp = fdopen(fd, "w");
Brian Lovin1bf14812013-08-08 16:12:55 -0700941 if (fp == NULL){
942 close(fd);
943 return;
944 }
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500945 for (row = 0; row < terminal->height; row++) {
946 p_row = terminal_get_row(terminal, row);
947 for (col = 0; col < terminal->width; col++) {
Peng Wucfcc1112013-08-19 10:59:21 +0800948 if (p_row[col].ch == 0x200B) /* space glyph */
949 continue;
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500950 /* get the attributes for this character cell */
951 terminal_decode_attr(terminal, row, col, &attr);
952 if (!attr.attr.s)
953 continue;
954 len = strnlen((char *) p_row[col].byte, 4);
Kristian Høgsberg0dee6472012-07-01 21:25:41 -0400955 if (len > 0)
956 fwrite(p_row[col].byte, 1, len, fp);
957 if (len == 0 || col == terminal->width - 1) {
958 fwrite("\n", 1, 1, fp);
959 break;
960 }
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500961 }
962 }
963 fclose(fp);
964}
965
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500966struct glyph_run {
967 struct terminal *terminal;
968 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400969 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500970 union decoded_attr attr;
971 cairo_glyph_t glyphs[256], *g;
972};
973
974static void
975glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
976{
977 run->terminal = terminal;
978 run->cr = cr;
979 run->g = run->glyphs;
980 run->count = 0;
981 run->attr.key = 0;
982}
983
984static void
985glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
986{
987 cairo_scaled_font_t *font;
988
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500989 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
990 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300991 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500992 font = run->terminal->font_bold;
993 else
994 font = run->terminal->font_normal;
995 cairo_set_scaled_font(run->cr, font);
996 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300997 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500998
Callum Lowcay9d708b02011-01-12 20:06:17 +1300999 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
1000 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001001 run->g = run->glyphs;
1002 run->count = 0;
1003 }
Callum Lowcay9d708b02011-01-12 20:06:17 +13001004 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001005}
1006
1007static void
1008glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
1009{
1010 int num_glyphs;
1011 cairo_scaled_font_t *font;
1012
1013 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
1014
Callum Lowcay9d708b02011-01-12 20:06:17 +13001015 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001016 font = run->terminal->font_bold;
1017 else
1018 font = run->terminal->font_normal;
1019
1020 cairo_move_to(run->cr, x, y);
1021 cairo_scaled_font_text_to_glyphs (font, x, y,
1022 (char *) c->byte, 4,
1023 &run->g, &num_glyphs,
1024 NULL, NULL, NULL);
1025 run->g += num_glyphs;
1026 run->count += num_glyphs;
1027}
1028
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001029
Kristian Høgsbergf106fd52011-01-11 10:11:39 -05001030static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001031redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001032{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001033 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001034 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001035 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001036 int top_margin, side_margin;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001037 int row, col, cursor_x, cursor_y;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001038 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001039 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001040 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001041 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001042 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001043 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -05001044 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +08001045 double average_width;
Peng Wucfcc1112013-08-19 10:59:21 +08001046 double unichar_width;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001047
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001048 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001049 widget_get_allocation(terminal->widget, &allocation);
Alexander Larssonde79dd02013-05-22 14:41:34 +02001050 cr = widget_cairo_create(terminal->widget);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001051 cairo_rectangle(cr, allocation.x, allocation.y,
1052 allocation.width, allocation.height);
1053 cairo_clip(cr);
1054 cairo_push_group(cr);
1055
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001056 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -05001057 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001058 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001059
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05001060 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001061
Kristian Høgsberg59826582011-01-20 11:56:57 -05001062 extents = terminal->extents;
Peng Wuf291f202013-06-06 15:32:41 +08001063 average_width = terminal->average_width;
1064 side_margin = (allocation.width - terminal->width * average_width) / 2;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001065 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001066
Callum Lowcay30eeae52011-01-07 19:46:55 +00001067 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001068 cairo_translate(cr, allocation.x + side_margin,
1069 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001070 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001071 for (row = 0; row < terminal->height; row++) {
Peng Wucfcc1112013-08-19 10:59:21 +08001072 p_row = terminal_get_row(terminal, row);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001073 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001074 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -05001075 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001076
Callum Lowcay9d708b02011-01-12 20:06:17 +13001077 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -05001078 continue;
1079
Peng Wucfcc1112013-08-19 10:59:21 +08001080 if (is_wide(p_row[col]))
1081 unichar_width = 2 * average_width;
1082 else
1083 unichar_width = average_width;
1084
Callum Lowcay9d708b02011-01-12 20:06:17 +13001085 terminal_set_color(terminal, cr, attr.attr.bg);
Peng Wuf291f202013-06-06 15:32:41 +08001086 cairo_move_to(cr, col * average_width,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001087 row * extents.height);
Peng Wucfcc1112013-08-19 10:59:21 +08001088 cairo_rel_line_to(cr, unichar_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001089 cairo_rel_line_to(cr, 0, extents.height);
Peng Wucfcc1112013-08-19 10:59:21 +08001090 cairo_rel_line_to(cr, -unichar_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001091 cairo_close_path(cr);
1092 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001093 }
1094 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001095
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001096 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1097
1098 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001099 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001100 for (row = 0; row < terminal->height; row++) {
1101 p_row = terminal_get_row(terminal, row);
1102 for (col = 0; col < terminal->width; col++) {
1103 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -05001104 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001105
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001106 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001107
Peng Wuf291f202013-06-06 15:32:41 +08001108 text_x = col * average_width;
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001109 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +13001110 if (attr.attr.a & ATTRMASK_UNDERLINE) {
1111 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +00001112 cairo_move_to(cr, text_x, (double)text_y + 1.5);
Peng Wuf291f202013-06-06 15:32:41 +08001113 cairo_line_to(cr, text_x + average_width, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001114 cairo_stroke(cr);
1115 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -05001116
Daiki Ueno56d8a7a2014-04-08 18:46:18 +09001117 /* skip space glyph (RLE) we use as a placeholder of
1118 the right half of a double-width character,
1119 because RLE is not available in every font. */
1120 if (p_row[col].ch == 0x200B)
1121 continue;
1122
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001123 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001124 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001125 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001126
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001127 attr.key = ~0;
1128 glyph_run_flush(&run, attr);
1129
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001130 if ((terminal->mode & MODE_SHOW_CURSOR) &&
1131 !window_has_focus(terminal->window)) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001132 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001133
Callum Lowcay30eeae52011-01-07 19:46:55 +00001134 cairo_set_line_width(cr, 1);
Peng Wuf291f202013-06-06 15:32:41 +08001135 cairo_move_to(cr, terminal->column * average_width + d,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001136 terminal->row * extents.height + d);
Peng Wuf291f202013-06-06 15:32:41 +08001137 cairo_rel_line_to(cr, average_width - 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001138 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
Peng Wuf291f202013-06-06 15:32:41 +08001139 cairo_rel_line_to(cr, -average_width + 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001140 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001141
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001142 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001143 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001144
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001145 cairo_pop_group_to_source(cr);
1146 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001147 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001148 cairo_surface_destroy(surface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001149
1150 if (terminal->send_cursor_position) {
1151 cursor_x = side_margin + allocation.x +
Peng Wuf291f202013-06-06 15:32:41 +08001152 terminal->column * average_width;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001153 cursor_y = top_margin + allocation.y +
1154 terminal->row * extents.height;
1155 window_set_text_cursor_position(terminal->window,
1156 cursor_x, cursor_y);
1157 terminal->send_cursor_position = 0;
1158 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001159}
1160
1161static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001162terminal_write(struct terminal *terminal, const char *data, size_t length)
1163{
1164 if (write(terminal->master, data, length) < 0)
1165 abort();
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001166 terminal->send_cursor_position = 1;
Kristian Høgsberg26130862011-08-24 11:30:21 -04001167}
1168
1169static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001170terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001171
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001172static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001173handle_char(struct terminal *terminal, union utf8_char utf8);
1174
1175static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001176handle_sgr(struct terminal *terminal, int code);
1177
1178static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001179handle_term_parameter(struct terminal *terminal, int code, int sr)
1180{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001181 int i;
1182
Callum Lowcay67a201d2011-01-12 19:23:41 +13001183 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001184 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001185 case 1: /* DECCKM */
1186 if (sr) terminal->key_mode = KM_APPLICATION;
1187 else terminal->key_mode = KM_NORMAL;
1188 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001189 case 2: /* DECANM */
1190 /* No VT52 support yet */
1191 terminal->g0 = CS_US;
1192 terminal->g1 = CS_US;
1193 terminal->cs = terminal->g0;
1194 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001195 case 3: /* DECCOLM */
1196 if (sr)
1197 terminal_resize(terminal, 132, 24);
1198 else
1199 terminal_resize(terminal, 80, 24);
Michael Vetter2a18a522015-05-15 17:17:47 +02001200
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001201 /* set columns, but also home cursor and clear screen */
1202 terminal->row = 0; terminal->column = 0;
1203 for (i = 0; i < terminal->height; i++) {
1204 memset(terminal_get_row(terminal, i),
1205 0, terminal->data_pitch);
1206 attr_init(terminal_get_attr_row(terminal, i),
1207 terminal->curr_attr, terminal->width);
1208 }
1209 break;
1210 case 5: /* DECSCNM */
1211 if (sr) terminal->mode |= MODE_INVERSE;
1212 else terminal->mode &= ~MODE_INVERSE;
1213 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001214 case 6: /* DECOM */
1215 terminal->origin_mode = sr;
1216 if (terminal->origin_mode)
1217 terminal->row = terminal->margin_top;
1218 else
1219 terminal->row = 0;
1220 terminal->column = 0;
1221 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001222 case 7: /* DECAWM */
1223 if (sr) terminal->mode |= MODE_AUTOWRAP;
1224 else terminal->mode &= ~MODE_AUTOWRAP;
1225 break;
1226 case 8: /* DECARM */
1227 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1228 else terminal->mode &= ~MODE_AUTOREPEAT;
1229 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001230 case 12: /* Very visible cursor (CVVIS) */
1231 /* FIXME: What do we do here. */
1232 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001233 case 25:
1234 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1235 else terminal->mode &= ~MODE_SHOW_CURSOR;
1236 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001237 case 1034: /* smm/rmm, meta mode on/off */
1238 /* ignore */
1239 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001240 case 1037: /* deleteSendsDel */
1241 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1242 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1243 break;
1244 case 1039: /* altSendsEscape */
1245 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1246 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1247 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001248 case 1049: /* rmcup/smcup, alternate screen */
1249 /* Ignore. Should be possible to implement,
1250 * but it's kind of annoying. */
1251 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001252 default:
1253 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1254 break;
1255 }
1256 } else {
1257 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001258 case 4: /* IRM */
1259 if (sr) terminal->mode |= MODE_IRM;
1260 else terminal->mode &= ~MODE_IRM;
1261 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001262 case 20: /* LNM */
1263 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1264 else terminal->mode &= ~MODE_LF_NEWLINE;
1265 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001266 default:
1267 fprintf(stderr, "Unknown parameter: %d\n", code);
1268 break;
1269 }
1270 }
1271}
1272
1273static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001274handle_dcs(struct terminal *terminal)
1275{
1276}
1277
1278static void
1279handle_osc(struct terminal *terminal)
1280{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001281 char *p;
1282 int code;
1283
1284 terminal->escape[terminal->escape_length++] = '\0';
1285 p = &terminal->escape[2];
1286 code = strtol(p, &p, 10);
1287 if (*p == ';') p++;
1288
1289 switch (code) {
1290 case 0: /* Icon name and window title */
1291 case 1: /* Icon label */
1292 case 2: /* Window title*/
Kristian Høgsberga83be202013-10-23 20:47:35 -07001293 free(terminal->title);
1294 terminal->title = strdup(p);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001295 window_set_title(terminal->window, p);
1296 break;
Kristian Høgsberg8a205002013-09-11 11:52:56 -07001297 case 7: /* shell cwd as uri */
1298 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001299 default:
Kristian Høgsberg8a205002013-09-11 11:52:56 -07001300 fprintf(stderr, "Unknown OSC escape code %d, text %s\n",
1301 code, p);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001302 break;
1303 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001304}
1305
1306static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001307handle_escape(struct terminal *terminal)
1308{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001309 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001310 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001311 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001312 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001313 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001314 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001315 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001316
1317 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001318 i = 0;
1319 p = &terminal->escape[2];
1320 while ((isdigit(*p) || *p == ';') && i < 10) {
1321 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001322 if (!set[i]) {
1323 args[i] = 0;
1324 set[i] = 1;
1325 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001326 p++;
1327 i++;
1328 } else {
1329 args[i] = strtol(p, &p, 10);
1330 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001331 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001332 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001333
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001334 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001335 case '@': /* ICH */
1336 count = set[0] ? args[0] : 1;
1337 if (count == 0) count = 1;
1338 terminal_shift_line(terminal, count);
1339 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001340 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001341 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001342 if (count == 0) count = 1;
1343 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001344 terminal->row -= count;
1345 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001346 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001347 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001348 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001349 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001350 if (count == 0) count = 1;
1351 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001352 terminal->row += count;
1353 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001354 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001355 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001356 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001357 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001358 if (count == 0) count = 1;
1359 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001360 terminal->column += count;
1361 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001362 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001363 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001364 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001365 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001366 if (count == 0) count = 1;
1367 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001368 terminal->column -= count;
1369 else
1370 terminal->column = 0;
1371 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001372 case 'E': /* CNL */
1373 count = set[0] ? args[0] : 1;
1374 if (terminal->row + count <= terminal->margin_bottom)
1375 terminal->row += count;
1376 else
1377 terminal->row = terminal->margin_bottom;
1378 terminal->column = 0;
1379 break;
1380 case 'F': /* CPL */
1381 count = set[0] ? args[0] : 1;
1382 if (terminal->row - count >= terminal->margin_top)
1383 terminal->row -= count;
1384 else
1385 terminal->row = terminal->margin_top;
1386 terminal->column = 0;
1387 break;
1388 case 'G': /* CHA */
1389 y = set[0] ? args[0] : 1;
1390 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
Michael Vetter2a18a522015-05-15 17:17:47 +02001391
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001392 terminal->column = y - 1;
1393 break;
1394 case 'f': /* HVP */
1395 case 'H': /* CUP */
1396 x = (set[1] ? args[1] : 1) - 1;
1397 x = x < 0 ? 0 :
1398 (x >= terminal->width ? terminal->width - 1 : x);
Michael Vetter2a18a522015-05-15 17:17:47 +02001399
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001400 y = (set[0] ? args[0] : 1) - 1;
1401 if (terminal->origin_mode) {
1402 y += terminal->margin_top;
1403 y = y < terminal->margin_top ? terminal->margin_top :
1404 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1405 } else {
1406 y = y < 0 ? 0 :
1407 (y >= terminal->height ? terminal->height - 1 : y);
1408 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001409
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001410 terminal->row = y;
1411 terminal->column = x;
1412 break;
1413 case 'I': /* CHT */
1414 count = set[0] ? args[0] : 1;
1415 if (count == 0) count = 1;
1416 while (count > 0 && terminal->column < terminal->width) {
1417 if (terminal->tab_ruler[terminal->column]) count--;
1418 terminal->column++;
1419 }
1420 terminal->column--;
1421 break;
1422 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001423 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001424 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001425 if (!set[0] || args[0] == 0 || args[0] > 2) {
1426 memset(&row[terminal->column],
1427 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1428 attr_init(&attr_row[terminal->column],
1429 terminal->curr_attr, terminal->width - terminal->column);
1430 for (i = terminal->row + 1; i < terminal->height; i++) {
1431 memset(terminal_get_row(terminal, i),
1432 0, terminal->data_pitch);
1433 attr_init(terminal_get_attr_row(terminal, i),
1434 terminal->curr_attr, terminal->width);
1435 }
1436 } else if (args[0] == 1) {
1437 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1438 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1439 for (i = 0; i < terminal->row; i++) {
1440 memset(terminal_get_row(terminal, i),
1441 0, terminal->data_pitch);
1442 attr_init(terminal_get_attr_row(terminal, i),
1443 terminal->curr_attr, terminal->width);
1444 }
1445 } else if (args[0] == 2) {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07001446 /* Clear screen by scrolling contents out */
1447 terminal_scroll_buffer(terminal,
1448 terminal->end - terminal->start);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001449 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001450 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001451 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001452 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001453 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001454 if (!set[0] || args[0] == 0 || args[0] > 2) {
1455 memset(&row[terminal->column], 0,
1456 (terminal->width - terminal->column) * sizeof(union utf8_char));
1457 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1458 terminal->width - terminal->column);
1459 } else if (args[0] == 1) {
1460 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1461 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1462 } else if (args[0] == 2) {
1463 memset(row, 0, terminal->data_pitch);
1464 attr_init(attr_row, terminal->curr_attr, terminal->width);
1465 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001466 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001467 case 'L': /* IL */
1468 count = set[0] ? args[0] : 1;
1469 if (count == 0) count = 1;
1470 if (terminal->row >= terminal->margin_top &&
1471 terminal->row < terminal->margin_bottom)
1472 {
1473 top = terminal->margin_top;
1474 terminal->margin_top = terminal->row;
1475 terminal_scroll(terminal, 0 - count);
1476 terminal->margin_top = top;
1477 } else if (terminal->row == terminal->margin_bottom) {
1478 memset(terminal_get_row(terminal, terminal->row),
1479 0, terminal->data_pitch);
1480 attr_init(terminal_get_attr_row(terminal, terminal->row),
1481 terminal->curr_attr, terminal->width);
1482 }
1483 break;
1484 case 'M': /* DL */
1485 count = set[0] ? args[0] : 1;
1486 if (count == 0) count = 1;
1487 if (terminal->row >= terminal->margin_top &&
1488 terminal->row < terminal->margin_bottom)
1489 {
1490 top = terminal->margin_top;
1491 terminal->margin_top = terminal->row;
1492 terminal_scroll(terminal, count);
1493 terminal->margin_top = top;
1494 } else if (terminal->row == terminal->margin_bottom) {
1495 memset(terminal_get_row(terminal, terminal->row),
1496 0, terminal->data_pitch);
1497 }
1498 break;
1499 case 'P': /* DCH */
1500 count = set[0] ? args[0] : 1;
1501 if (count == 0) count = 1;
1502 terminal_shift_line(terminal, 0 - count);
1503 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001504 case 'S': /* SU */
1505 terminal_scroll(terminal, set[0] ? args[0] : 1);
1506 break;
1507 case 'T': /* SD */
1508 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1509 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001510 case 'X': /* ECH */
1511 count = set[0] ? args[0] : 1;
1512 if (count == 0) count = 1;
1513 if ((terminal->column + count) > terminal->width)
1514 count = terminal->width - terminal->column;
1515 row = terminal_get_row(terminal, terminal->row);
1516 attr_row = terminal_get_attr_row(terminal, terminal->row);
1517 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1518 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1519 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001520 case 'Z': /* CBT */
1521 count = set[0] ? args[0] : 1;
1522 if (count == 0) count = 1;
1523 while (count > 0 && terminal->column >= 0) {
1524 if (terminal->tab_ruler[terminal->column]) count--;
1525 terminal->column--;
1526 }
1527 terminal->column++;
1528 break;
1529 case '`': /* HPA */
1530 y = set[0] ? args[0] : 1;
1531 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
Michael Vetter2a18a522015-05-15 17:17:47 +02001532
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001533 terminal->column = y - 1;
1534 break;
1535 case 'b': /* REP */
1536 count = set[0] ? args[0] : 1;
1537 if (count == 0) count = 1;
1538 if (terminal->last_char.byte[0])
1539 for (i = 0; i < count; i++)
1540 handle_char(terminal, terminal->last_char);
1541 terminal->last_char.byte[0] = 0;
1542 break;
1543 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001544 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001545 break;
1546 case 'd': /* VPA */
1547 x = set[0] ? args[0] : 1;
1548 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
Michael Vetter2a18a522015-05-15 17:17:47 +02001549
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001550 terminal->row = x - 1;
1551 break;
1552 case 'g': /* TBC */
1553 if (!set[0] || args[0] == 0) {
1554 terminal->tab_ruler[terminal->column] = 0;
1555 } else if (args[0] == 3) {
1556 memset(terminal->tab_ruler, 0, terminal->width);
1557 }
1558 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001559 case 'h': /* SM */
1560 for(i = 0; i < 10 && set[i]; i++) {
1561 handle_term_parameter(terminal, args[i], 1);
1562 }
1563 break;
1564 case 'l': /* RM */
1565 for(i = 0; i < 10 && set[i]; i++) {
1566 handle_term_parameter(terminal, args[i], 0);
1567 }
1568 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001569 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001570 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001571 if (i <= 7 && set[i] && set[i + 1] &&
1572 set[i + 2] && args[i + 1] == 5)
1573 {
1574 if (args[i] == 38) {
1575 handle_sgr(terminal, args[i + 2] + 256);
1576 break;
1577 } else if (args[i] == 48) {
1578 handle_sgr(terminal, args[i + 2] + 512);
1579 break;
1580 }
1581 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001582 if(set[i]) {
1583 handle_sgr(terminal, args[i]);
1584 } else if(i == 0) {
1585 handle_sgr(terminal, 0);
1586 break;
1587 } else {
1588 break;
1589 }
1590 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001591 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001592 case 'n': /* DSR */
1593 i = set[0] ? args[0] : 0;
1594 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001595 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001596 } else if (i == 6) {
1597 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1598 terminal->origin_mode ?
1599 terminal->row+terminal->margin_top : terminal->row+1,
1600 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001601 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001602 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001603 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001604 case 'r':
1605 if(!set[0]) {
1606 terminal->margin_top = 0;
1607 terminal->margin_bottom = terminal->height-1;
1608 terminal->row = 0;
1609 terminal->column = 0;
1610 } else {
1611 top = (set[0] ? args[0] : 1) - 1;
1612 top = top < 0 ? 0 :
1613 (top >= terminal->height ? terminal->height - 1 : top);
1614 bottom = (set[1] ? args[1] : 1) - 1;
1615 bottom = bottom < 0 ? 0 :
1616 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1617 if(bottom > top) {
1618 terminal->margin_top = top;
1619 terminal->margin_bottom = bottom;
1620 } else {
1621 terminal->margin_top = 0;
1622 terminal->margin_bottom = terminal->height-1;
1623 }
1624 if(terminal->origin_mode)
1625 terminal->row = terminal->margin_top;
1626 else
1627 terminal->row = 0;
1628 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001629 }
1630 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001631 case 's':
1632 terminal->saved_row = terminal->row;
1633 terminal->saved_column = terminal->column;
1634 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001635 case 't': /* windowOps */
1636 if (!set[0]) break;
1637 switch (args[0]) {
1638 case 4: /* resize px */
1639 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001640 widget_schedule_resize(terminal->widget,
1641 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001642 }
1643 break;
1644 case 8: /* resize ch */
1645 if (set[1] && set[2]) {
1646 terminal_resize(terminal, args[2], args[1]);
1647 }
1648 break;
1649 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001650 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001651 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1652 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001653 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001654 break;
1655 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001656 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001657 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1658 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001659 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001660 break;
1661 case 18: /* report ch */
1662 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1663 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001664 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001665 break;
1666 case 21: /* report title */
1667 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1668 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001669 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001670 break;
1671 default:
1672 if (args[0] >= 24)
1673 terminal_resize(terminal, terminal->width, args[0]);
1674 else
1675 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1676 break;
1677 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001678 case 'u':
1679 terminal->row = terminal->saved_row;
1680 terminal->column = terminal->saved_column;
1681 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001682 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001683 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001684 break;
Michael Vetter2a18a522015-05-15 17:17:47 +02001685 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001686}
1687
1688static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001689handle_non_csi_escape(struct terminal *terminal, char code)
1690{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001691 switch(code) {
1692 case 'M': /* RI */
1693 terminal->row -= 1;
1694 if(terminal->row < terminal->margin_top) {
1695 terminal->row = terminal->margin_top;
1696 terminal_scroll(terminal, -1);
1697 }
1698 break;
1699 case 'E': /* NEL */
1700 terminal->column = 0;
1701 // fallthrough
1702 case 'D': /* IND */
1703 terminal->row += 1;
1704 if(terminal->row > terminal->margin_bottom) {
1705 terminal->row = terminal->margin_bottom;
1706 terminal_scroll(terminal, +1);
1707 }
1708 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001709 case 'c': /* RIS */
1710 terminal_init(terminal);
1711 break;
1712 case 'H': /* HTS */
1713 terminal->tab_ruler[terminal->column] = 1;
1714 break;
1715 case '7': /* DECSC */
1716 terminal->saved_row = terminal->row;
1717 terminal->saved_column = terminal->column;
1718 terminal->saved_attr = terminal->curr_attr;
1719 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001720 terminal->saved_cs = terminal->cs;
1721 terminal->saved_g0 = terminal->g0;
1722 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001723 break;
1724 case '8': /* DECRC */
1725 terminal->row = terminal->saved_row;
1726 terminal->column = terminal->saved_column;
1727 terminal->curr_attr = terminal->saved_attr;
1728 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001729 terminal->cs = terminal->saved_cs;
1730 terminal->g0 = terminal->saved_g0;
1731 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001732 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001733 case '=': /* DECPAM */
1734 terminal->key_mode = KM_APPLICATION;
1735 break;
1736 case '>': /* DECPNM */
1737 terminal->key_mode = KM_NORMAL;
1738 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001739 default:
1740 fprintf(stderr, "Unknown escape code: %c\n", code);
1741 break;
1742 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001743}
1744
1745static void
1746handle_special_escape(struct terminal *terminal, char special, char code)
1747{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001748 int i, numChars;
1749
1750 if (special == '#') {
1751 switch(code) {
1752 case '8':
1753 /* fill with 'E', no cheap way to do this */
1754 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1755 numChars = terminal->width * terminal->height;
1756 for(i = 0; i < numChars; i++) {
1757 terminal->data[i].byte[0] = 'E';
1758 }
1759 break;
1760 default:
1761 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1762 break;
1763 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001764 } else if (special == '(' || special == ')') {
1765 switch(code) {
1766 case '0':
1767 if (special == '(')
1768 terminal->g0 = CS_SPECIAL;
1769 else
1770 terminal->g1 = CS_SPECIAL;
1771 break;
1772 case 'A':
1773 if (special == '(')
1774 terminal->g0 = CS_UK;
1775 else
1776 terminal->g1 = CS_UK;
1777 break;
1778 case 'B':
1779 if (special == '(')
1780 terminal->g0 = CS_US;
1781 else
1782 terminal->g1 = CS_US;
1783 break;
1784 default:
1785 fprintf(stderr, "Unknown character set %c\n", code);
1786 break;
1787 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001788 } else {
1789 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1790 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001791}
1792
1793static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001794handle_sgr(struct terminal *terminal, int code)
1795{
1796 switch(code) {
1797 case 0:
1798 terminal->curr_attr = terminal->color_scheme->default_attr;
1799 break;
1800 case 1:
1801 terminal->curr_attr.a |= ATTRMASK_BOLD;
1802 if (terminal->curr_attr.fg < 8)
1803 terminal->curr_attr.fg += 8;
1804 break;
1805 case 4:
1806 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1807 break;
1808 case 5:
1809 terminal->curr_attr.a |= ATTRMASK_BLINK;
1810 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001811 case 8:
1812 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1813 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001814 case 2:
1815 case 21:
1816 case 22:
1817 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1818 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1819 terminal->curr_attr.fg -= 8;
1820 break;
1821 case 24:
1822 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1823 break;
1824 case 25:
1825 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1826 break;
1827 case 7:
1828 case 26:
1829 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1830 break;
1831 case 27:
1832 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1833 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001834 case 28:
1835 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1836 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001837 case 39:
1838 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1839 break;
1840 case 49:
1841 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1842 break;
1843 default:
1844 if(code >= 30 && code <= 37) {
1845 terminal->curr_attr.fg = code - 30;
1846 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1847 terminal->curr_attr.fg += 8;
1848 } else if(code >= 40 && code <= 47) {
1849 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001850 } else if (code >= 90 && code <= 97) {
1851 terminal->curr_attr.fg = code - 90 + 8;
1852 } else if (code >= 100 && code <= 107) {
1853 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001854 } else if(code >= 256 && code < 512) {
1855 terminal->curr_attr.fg = code - 256;
1856 } else if(code >= 512 && code < 768) {
1857 terminal->curr_attr.bg = code - 512;
1858 } else {
1859 fprintf(stderr, "Unknown SGR code: %d\n", code);
1860 }
1861 break;
1862 }
1863}
1864
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001865/* Returns 1 if c was special, otherwise 0 */
1866static int
1867handle_special_char(struct terminal *terminal, char c)
1868{
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001869 union utf8_char *row;
1870 struct attr *attr_row;
1871
1872 row = terminal_get_row(terminal, terminal->row);
1873 attr_row = terminal_get_attr_row(terminal, terminal->row);
1874
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001875 switch(c) {
1876 case '\r':
1877 terminal->column = 0;
1878 break;
1879 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001880 if (terminal->mode & MODE_LF_NEWLINE) {
1881 terminal->column = 0;
1882 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001883 /* fallthrough */
1884 case '\v':
1885 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001886 terminal->row++;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07001887 if (terminal->row > terminal->margin_bottom) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001888 terminal->row = terminal->margin_bottom;
1889 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001890 }
1891
1892 break;
1893 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001894 while (terminal->column < terminal->width) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001895 if (terminal->mode & MODE_IRM)
1896 terminal_shift_line(terminal, +1);
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001897
1898 if (row[terminal->column].byte[0] == '\0') {
1899 row[terminal->column].byte[0] = ' ';
1900 row[terminal->column].byte[1] = '\0';
1901 attr_row[terminal->column] = terminal->curr_attr;
1902 }
1903
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001904 terminal->column++;
Kristian Høgsbergcca3c2f2012-06-20 15:56:13 -04001905 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001906 }
1907 if (terminal->column >= terminal->width) {
1908 terminal->column = terminal->width - 1;
1909 }
1910
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001911 break;
1912 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001913 if (terminal->column >= terminal->width) {
1914 terminal->column = terminal->width - 2;
1915 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001916 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001917 } else if (terminal->mode & MODE_AUTOWRAP) {
1918 terminal->column = terminal->width - 1;
1919 terminal->row -= 1;
1920 if (terminal->row < terminal->margin_top) {
1921 terminal->row = terminal->margin_top;
1922 terminal_scroll(terminal, -1);
1923 }
1924 }
1925
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001926 break;
1927 case '\a':
1928 /* Bell */
1929 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001930 case '\x0E': /* SO */
1931 terminal->cs = terminal->g1;
1932 break;
1933 case '\x0F': /* SI */
1934 terminal->cs = terminal->g0;
1935 break;
Kristian Høgsberg2a1aa4e2012-08-03 09:37:05 -04001936 case '\0':
1937 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001938 default:
1939 return 0;
1940 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001941
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001942 return 1;
1943}
1944
1945static void
1946handle_char(struct terminal *terminal, union utf8_char utf8)
1947{
1948 union utf8_char *row;
1949 struct attr *attr_row;
Michael Vetter2a18a522015-05-15 17:17:47 +02001950
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001951 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001952
1953 apply_char_set(terminal->cs, &utf8);
Michael Vetter2a18a522015-05-15 17:17:47 +02001954
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001955 /* There are a whole lot of non-characters, control codes,
1956 * and formatting codes that should probably be ignored,
1957 * for example: */
1958 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1959 /* BOM, ignore */
1960 return;
Michael Vetter2a18a522015-05-15 17:17:47 +02001961 }
1962
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001963 /* Some of these non-characters should be translated, e.g.: */
1964 if (utf8.byte[0] < 32) {
1965 utf8.byte[0] = utf8.byte[0] + 64;
1966 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001967
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001968 /* handle right margin effects */
1969 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001970 if (terminal->mode & MODE_AUTOWRAP) {
1971 terminal->column = 0;
1972 terminal->row += 1;
1973 if (terminal->row > terminal->margin_bottom) {
1974 terminal->row = terminal->margin_bottom;
1975 terminal_scroll(terminal, +1);
1976 }
1977 } else {
1978 terminal->column--;
1979 }
1980 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001981
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001982 row = terminal_get_row(terminal, terminal->row);
1983 attr_row = terminal_get_attr_row(terminal, terminal->row);
Michael Vetter2a18a522015-05-15 17:17:47 +02001984
Callum Lowcay69e96582011-01-07 19:47:00 +00001985 if (terminal->mode & MODE_IRM)
1986 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001987 row[terminal->column] = utf8;
1988 attr_row[terminal->column++] = terminal->curr_attr;
1989
Kristian Høgsberg1d781ee2013-11-24 16:54:12 -08001990 if (terminal->row + terminal->start + 1 > terminal->end)
1991 terminal->end = terminal->row + terminal->start + 1;
1992 if (terminal->end == terminal->buffer_height)
1993 terminal->log_size = terminal->buffer_height;
1994 else if (terminal->log_size < terminal->buffer_height)
1995 terminal->log_size = terminal->end;
1996
Peng Wucfcc1112013-08-19 10:59:21 +08001997 /* cursor jump for wide character. */
1998 if (is_wide(utf8))
1999 row[terminal->column++].ch = 0x200B; /* space glyph */
2000
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002001 if (utf8.ch != terminal->last_char.ch)
2002 terminal->last_char = utf8;
2003}
2004
Callum Lowcay30eeae52011-01-07 19:46:55 +00002005static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13002006escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
2007{
2008 int len, i;
2009
2010 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
2011 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
2012 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
2013 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
2014 else len = 1; /* Invalid, cannot happen */
2015
2016 if (terminal->escape_length + len <= MAX_ESCAPE) {
2017 for (i = 0; i < len; i++)
2018 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
2019 terminal->escape_length += len;
2020 } else if (terminal->escape_length < MAX_ESCAPE) {
2021 terminal->escape[terminal->escape_length++] = 0;
2022 }
2023}
2024
2025static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002026terminal_data(struct terminal *terminal, const char *data, size_t length)
2027{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002028 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002029 union utf8_char utf8;
2030 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002031
2032 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002033 parser_state =
2034 utf8_next_char(&terminal->state_machine, data[i]);
2035 switch(parser_state) {
2036 case utf8state_accept:
2037 utf8.ch = terminal->state_machine.s.ch;
2038 break;
2039 case utf8state_reject:
2040 /* the unicode replacement character */
2041 utf8.byte[0] = 0xEF;
2042 utf8.byte[1] = 0xBF;
2043 utf8.byte[2] = 0xBD;
2044 utf8.byte[3] = 0x00;
2045 break;
2046 default:
2047 continue;
2048 }
2049
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002050 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13002051 switch (terminal->state) {
2052 case escape_state_escape:
2053 escape_append_utf8(terminal, utf8);
2054 switch (utf8.byte[0]) {
2055 case 'P': /* DCS */
2056 terminal->state = escape_state_dcs;
2057 break;
2058 case '[': /* CSI */
2059 terminal->state = escape_state_csi;
2060 break;
2061 case ']': /* OSC */
2062 terminal->state = escape_state_osc;
2063 break;
2064 case '#':
2065 case '(':
2066 case ')': /* special */
2067 terminal->state = escape_state_special;
2068 break;
2069 case '^': /* PM (not implemented) */
2070 case '_': /* APC (not implemented) */
2071 terminal->state = escape_state_ignore;
2072 break;
2073 default:
2074 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002075 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13002076 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002077 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002078 continue;
2079 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002080 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
2081 /* do nothing */
2082 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002083 terminal->escape_flags |= ESC_FLAG_WHAT;
2084 } else if (utf8.byte[0] == '>') {
2085 terminal->escape_flags |= ESC_FLAG_GT;
2086 } else if (utf8.byte[0] == '!') {
2087 terminal->escape_flags |= ESC_FLAG_BANG;
2088 } else if (utf8.byte[0] == '$') {
2089 terminal->escape_flags |= ESC_FLAG_CASH;
2090 } else if (utf8.byte[0] == '\'') {
2091 terminal->escape_flags |= ESC_FLAG_SQUOTE;
2092 } else if (utf8.byte[0] == '"') {
2093 terminal->escape_flags |= ESC_FLAG_DQUOTE;
2094 } else if (utf8.byte[0] == ' ') {
2095 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002096 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002097 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002098 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13002099 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002100 }
Michael Vetter2a18a522015-05-15 17:17:47 +02002101
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002102 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
2103 utf8.byte[0] == '`')
2104 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002105 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002106 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002107 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002108 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002109 continue;
2110 case escape_state_inner_escape:
2111 if (utf8.byte[0] == '\\') {
2112 terminal->state = escape_state_normal;
2113 if (terminal->outer_state == escape_state_dcs) {
2114 handle_dcs(terminal);
2115 } else if (terminal->outer_state == escape_state_osc) {
2116 handle_osc(terminal);
2117 }
2118 } else if (utf8.byte[0] == '\e') {
2119 terminal->state = terminal->outer_state;
2120 escape_append_utf8(terminal, utf8);
2121 if (terminal->escape_length >= MAX_ESCAPE)
2122 terminal->state = escape_state_normal;
2123 } else {
2124 terminal->state = terminal->outer_state;
2125 if (terminal->escape_length < MAX_ESCAPE)
2126 terminal->escape[terminal->escape_length++] = '\e';
2127 escape_append_utf8(terminal, utf8);
2128 if (terminal->escape_length >= MAX_ESCAPE)
2129 terminal->state = escape_state_normal;
2130 }
2131 continue;
2132 case escape_state_dcs:
2133 case escape_state_osc:
2134 case escape_state_ignore:
2135 if (utf8.byte[0] == '\e') {
2136 terminal->outer_state = terminal->state;
2137 terminal->state = escape_state_inner_escape;
2138 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
2139 terminal->state = escape_state_normal;
2140 handle_osc(terminal);
2141 } else {
2142 escape_append_utf8(terminal, utf8);
2143 if (terminal->escape_length >= MAX_ESCAPE)
2144 terminal->state = escape_state_normal;
2145 }
2146 continue;
2147 case escape_state_special:
2148 escape_append_utf8(terminal, utf8);
2149 terminal->state = escape_state_normal;
2150 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2151 handle_special_escape(terminal, terminal->escape[1],
2152 utf8.byte[0]);
2153 }
2154 continue;
2155 default:
2156 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002157 }
2158
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002159 /* this is valid, because ASCII characters are never used to
2160 * introduce a multibyte sequence in UTF-8 */
2161 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002162 terminal->state = escape_state_escape;
2163 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002164 terminal->escape[0] = '\e';
2165 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002166 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002167 } else {
2168 handle_char(terminal, utf8);
2169 } /* if */
2170 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002171
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002172 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002173}
2174
2175static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002176data_source_target(void *data,
2177 struct wl_data_source *source, const char *mime_type)
2178{
2179 fprintf(stderr, "data_source_target, %s\n", mime_type);
2180}
2181
2182static void
2183data_source_send(void *data,
2184 struct wl_data_source *source,
2185 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002186{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002187 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002188
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002189 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002190}
2191
2192static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002193data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002194{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002195 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002196}
2197
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002198static const struct wl_data_source_listener data_source_listener = {
2199 data_source_target,
2200 data_source_send,
2201 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002202};
2203
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07002204static const char text_mime_type[] = "text/plain;charset=utf-8";
2205
2206static void
2207data_handler(struct window *window,
2208 struct input *input,
2209 float x, float y, const char **types, void *data)
2210{
2211 int i, has_text = 0;
2212
2213 if (!types)
2214 return;
2215 for (i = 0; types[i]; i++)
2216 if (strcmp(types[i], text_mime_type) == 0)
2217 has_text = 1;
2218
2219 if (!has_text) {
2220 input_accept(input, NULL);
2221 } else {
2222 input_accept(input, text_mime_type);
2223 }
2224}
2225
2226static void
2227drop_handler(struct window *window, struct input *input,
2228 int32_t x, int32_t y, void *data)
2229{
2230 struct terminal *terminal = data;
2231
2232 input_receive_drag_data_to_fd(input, text_mime_type, terminal->master);
2233}
2234
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002235static void
2236fullscreen_handler(struct window *window, void *data)
2237{
2238 struct terminal *terminal = data;
2239
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002240 window_set_fullscreen(window, !window_is_fullscreen(terminal->window));
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002241}
2242
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002243static void
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002244close_handler(void *data)
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002245{
2246 struct terminal *terminal = data;
2247
2248 terminal_destroy(terminal);
2249}
2250
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002251static void
2252terminal_copy(struct terminal *terminal, struct input *input)
2253{
2254 terminal->selection =
2255 display_create_data_source(terminal->display);
2256 wl_data_source_offer(terminal->selection,
2257 "text/plain;charset=utf-8");
2258 wl_data_source_add_listener(terminal->selection,
2259 &data_source_listener, terminal);
2260 input_set_selection(input, terminal->selection,
2261 display_get_serial(terminal->display));
2262}
2263
2264static void
2265terminal_paste(struct terminal *terminal, struct input *input)
2266{
2267 input_receive_selection_data_to_fd(input,
2268 "text/plain;charset=utf-8",
2269 terminal->master);
2270
2271}
2272
2273static void
2274terminal_new_instance(struct terminal *terminal)
2275{
2276 struct terminal *new_terminal;
2277
2278 new_terminal = terminal_create(terminal->display);
2279 if (terminal_run(new_terminal, option_shell))
2280 terminal_destroy(new_terminal);
2281}
2282
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002283static int
2284handle_bound_key(struct terminal *terminal,
2285 struct input *input, uint32_t sym, uint32_t time)
2286{
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002287 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002288 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002289 /* Cut selection; terminal doesn't do cut, fall
2290 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002291 case XKB_KEY_C:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002292 terminal_copy(terminal, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002293 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002294 case XKB_KEY_V:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002295 terminal_paste(terminal, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002296 return 1;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002297 case XKB_KEY_N:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002298 terminal_new_instance(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002299 return 1;
2300
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002301 case XKB_KEY_Up:
2302 if (!terminal->scrolling)
2303 terminal->saved_start = terminal->start;
2304 if (terminal->start == terminal->end - terminal->log_size)
2305 return 1;
2306
2307 terminal->scrolling = 1;
2308 terminal->start--;
2309 terminal->row++;
2310 terminal->selection_start_row++;
2311 terminal->selection_end_row++;
2312 widget_schedule_redraw(terminal->widget);
2313 return 1;
2314
2315 case XKB_KEY_Down:
2316 if (!terminal->scrolling)
2317 terminal->saved_start = terminal->start;
2318
2319 if (terminal->start == terminal->saved_start)
2320 return 1;
2321
2322 terminal->scrolling = 1;
2323 terminal->start++;
2324 terminal->row--;
2325 terminal->selection_start_row--;
2326 terminal->selection_end_row--;
2327 widget_schedule_redraw(terminal->widget);
2328 return 1;
2329
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002330 default:
2331 return 0;
2332 }
2333}
2334
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002335static void
2336key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002337 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2338 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002339{
2340 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002341 char ch[MAX_RESPONSE];
Kristian Høgsberg88fd4082012-06-21 15:55:03 -04002342 uint32_t modifiers, serial;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002343 int ret, len = 0, d;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002344 bool convert_utf8 = true;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002345
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002346 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002347 if ((modifiers & MOD_CONTROL_MASK) &&
2348 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002349 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2350 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002351 return;
2352
Daniel Stonea8468712012-11-07 17:51:35 +11002353 /* Map keypad symbols to 'normal' equivalents before processing */
2354 switch (sym) {
2355 case XKB_KEY_KP_Space:
2356 sym = XKB_KEY_space;
2357 break;
2358 case XKB_KEY_KP_Tab:
2359 sym = XKB_KEY_Tab;
2360 break;
2361 case XKB_KEY_KP_Enter:
2362 sym = XKB_KEY_Return;
2363 break;
2364 case XKB_KEY_KP_Left:
2365 sym = XKB_KEY_Left;
2366 break;
2367 case XKB_KEY_KP_Up:
2368 sym = XKB_KEY_Up;
2369 break;
2370 case XKB_KEY_KP_Right:
2371 sym = XKB_KEY_Right;
2372 break;
2373 case XKB_KEY_KP_Down:
2374 sym = XKB_KEY_Down;
2375 break;
2376 case XKB_KEY_KP_Equal:
2377 sym = XKB_KEY_equal;
2378 break;
2379 case XKB_KEY_KP_Multiply:
2380 sym = XKB_KEY_asterisk;
2381 break;
2382 case XKB_KEY_KP_Add:
2383 sym = XKB_KEY_plus;
2384 break;
2385 case XKB_KEY_KP_Separator:
2386 /* Note this is actually locale-dependent and should mostly be
2387 * a comma. But leave it as period until we one day start
2388 * doing the right thing. */
2389 sym = XKB_KEY_period;
2390 break;
2391 case XKB_KEY_KP_Subtract:
2392 sym = XKB_KEY_minus;
2393 break;
2394 case XKB_KEY_KP_Decimal:
2395 sym = XKB_KEY_period;
2396 break;
2397 case XKB_KEY_KP_Divide:
2398 sym = XKB_KEY_slash;
2399 break;
2400 case XKB_KEY_KP_0:
2401 case XKB_KEY_KP_1:
2402 case XKB_KEY_KP_2:
2403 case XKB_KEY_KP_3:
2404 case XKB_KEY_KP_4:
2405 case XKB_KEY_KP_5:
2406 case XKB_KEY_KP_6:
2407 case XKB_KEY_KP_7:
2408 case XKB_KEY_KP_8:
2409 case XKB_KEY_KP_9:
2410 sym = (sym - XKB_KEY_KP_0) + XKB_KEY_0;
2411 break;
2412 default:
2413 break;
2414 }
2415
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002416 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002417 case XKB_KEY_BackSpace:
Kristian Høgsbergb7f94bf2012-06-21 12:29:36 -04002418 if (modifiers & MOD_ALT_MASK)
2419 ch[len++] = 0x1b;
Kristian Høgsberg71a4cf42012-06-20 17:57:56 -04002420 ch[len++] = 0x7f;
2421 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002422 case XKB_KEY_Tab:
2423 case XKB_KEY_Linefeed:
2424 case XKB_KEY_Clear:
2425 case XKB_KEY_Pause:
2426 case XKB_KEY_Scroll_Lock:
2427 case XKB_KEY_Sys_Req:
2428 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002429 ch[len++] = sym & 0x7f;
2430 break;
2431
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002432 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002433 if (terminal->mode & MODE_LF_NEWLINE) {
2434 ch[len++] = 0x0D;
2435 ch[len++] = 0x0A;
2436 } else {
2437 ch[len++] = 0x0D;
2438 }
2439 break;
2440
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002441 case XKB_KEY_Shift_L:
2442 case XKB_KEY_Shift_R:
2443 case XKB_KEY_Control_L:
2444 case XKB_KEY_Control_R:
2445 case XKB_KEY_Alt_L:
2446 case XKB_KEY_Alt_R:
Kristian Høgsberg22fbcf72012-06-22 12:18:56 -04002447 case XKB_KEY_Meta_L:
2448 case XKB_KEY_Meta_R:
2449 case XKB_KEY_Super_L:
2450 case XKB_KEY_Super_R:
2451 case XKB_KEY_Hyper_L:
2452 case XKB_KEY_Hyper_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002453 break;
2454
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002455 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002456 len = function_key_response('[', 2, modifiers, '~', ch);
2457 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002458 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002459 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2460 ch[len++] = '\x04';
2461 } else {
2462 len = function_key_response('[', 3, modifiers, '~', ch);
2463 }
2464 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002465 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002466 len = function_key_response('[', 5, modifiers, '~', ch);
2467 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002468 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002469 len = function_key_response('[', 6, modifiers, '~', ch);
2470 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002471 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002472 len = function_key_response('O', 1, modifiers, 'P', ch);
2473 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002474 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002475 len = function_key_response('O', 1, modifiers, 'Q', ch);
2476 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002477 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002478 len = function_key_response('O', 1, modifiers, 'R', ch);
2479 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002480 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002481 len = function_key_response('O', 1, modifiers, 'S', ch);
2482 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002483 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002484 len = function_key_response('[', 15, modifiers, '~', ch);
2485 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002486 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002487 len = function_key_response('[', 17, modifiers, '~', ch);
2488 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002489 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002490 len = function_key_response('[', 18, modifiers, '~', ch);
2491 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002492 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002493 len = function_key_response('[', 19, modifiers, '~', ch);
2494 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002495 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002496 len = function_key_response('[', 20, modifiers, '~', ch);
2497 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002498 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002499 len = function_key_response('[', 21, modifiers, '~', ch);
2500 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002501 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002502 len = function_key_response('[', 24, modifiers, '~', ch);
2503 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002504 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002505 /* Handle special keys with alternate mappings */
2506 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2507 if (len != 0) break;
Michael Vetter2a18a522015-05-15 17:17:47 +02002508
Kristian Høgsberg70163132012-05-08 15:55:39 -04002509 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002510 if (sym >= '3' && sym <= '7')
2511 sym = (sym & 0x1f) + 8;
2512
2513 if (!((sym >= '!' && sym <= '/') ||
2514 (sym >= '8' && sym <= '?') ||
2515 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2516 else if (sym == '2') sym = 0x00;
2517 else if (sym == '/') sym = 0x1F;
2518 else if (sym == '8' || sym == '?') sym = 0x7F;
Kristian Høgsbergae9e0732012-06-21 12:30:15 -04002519 }
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002520 if (modifiers & MOD_ALT_MASK) {
2521 if (terminal->mode & MODE_ALT_SENDS_ESC) {
2522 ch[len++] = 0x1b;
2523 } else {
2524 sym = sym | 0x80;
2525 convert_utf8 = false;
2526 }
Callum Lowcay7e08e902011-01-07 19:47:02 +00002527 }
2528
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002529 if ((sym < 128) ||
2530 (!convert_utf8 && sym < 256)) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002531 ch[len++] = sym;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002532 } else {
2533 ret = xkb_keysym_to_utf8(sym, ch + len,
2534 MAX_RESPONSE - len);
2535 if (ret < 0)
2536 fprintf(stderr,
2537 "Warning: buffer too small to encode "
2538 "UTF8 character\n");
2539 else
2540 len += ret;
2541 }
2542
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002543 break;
2544 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002545
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002546 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002547 if (terminal->scrolling) {
2548 d = terminal->saved_start - terminal->start;
2549 terminal->row -= d;
2550 terminal->selection_start_row -= d;
2551 terminal->selection_end_row -= d;
2552 terminal->start = terminal->saved_start;
2553 terminal->scrolling = 0;
2554 widget_schedule_redraw(terminal->widget);
2555 }
2556
Kristian Høgsberg26130862011-08-24 11:30:21 -04002557 terminal_write(terminal, ch, len);
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002558
2559 /* Hide cursor, except if this was coming from a
2560 * repeating key press. */
2561 serial = display_get_serial(terminal->display);
2562 if (terminal->hide_cursor_serial != serial) {
2563 input_set_pointer_image(input, CURSOR_BLANK);
2564 terminal->hide_cursor_serial = serial;
2565 }
2566 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002567}
2568
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002569static void
2570keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002571 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002572{
2573 struct terminal *terminal = data;
2574
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002575 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002576}
2577
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002578static int wordsep(int ch)
2579{
2580 const char extra[] = "-,./?%&#:_=+@~";
2581
Andre Heider552d12b2012-08-02 20:59:43 +02002582 if (ch > 127)
2583 return 1;
2584
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002585 return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
2586}
2587
2588static int
2589recompute_selection(struct terminal *terminal)
2590{
2591 struct rectangle allocation;
2592 int col, x, width, height;
2593 int start_row, end_row;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002594 int word_start, eol;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002595 int side_margin, top_margin;
2596 int start_x, end_x;
2597 int cw, ch;
2598 union utf8_char *data;
2599
Peng Wuf291f202013-06-06 15:32:41 +08002600 cw = terminal->average_width;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002601 ch = terminal->extents.height;
2602 widget_get_allocation(terminal->widget, &allocation);
2603 width = terminal->width * cw;
2604 height = terminal->height * ch;
2605 side_margin = allocation.x + (allocation.width - width) / 2;
2606 top_margin = allocation.y + (allocation.height - height) / 2;
2607
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002608 start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1;
2609 end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1;
2610
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002611 if (start_row < end_row ||
2612 (start_row == end_row &&
2613 terminal->selection_start_x < terminal->selection_end_x)) {
2614 terminal->selection_start_row = start_row;
2615 terminal->selection_end_row = end_row;
2616 start_x = terminal->selection_start_x;
2617 end_x = terminal->selection_end_x;
2618 } else {
2619 terminal->selection_start_row = end_row;
2620 terminal->selection_end_row = start_row;
2621 start_x = terminal->selection_end_x;
2622 end_x = terminal->selection_start_x;
2623 }
2624
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002625 eol = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002626 if (terminal->selection_start_row < 0) {
2627 terminal->selection_start_row = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002628 terminal->selection_start_col = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002629 } else {
2630 x = side_margin + cw / 2;
2631 data = terminal_get_row(terminal,
2632 terminal->selection_start_row);
2633 word_start = 0;
2634 for (col = 0; col < terminal->width; col++, x += cw) {
2635 if (col == 0 || wordsep(data[col - 1].ch))
2636 word_start = col;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002637 if (data[col].ch != 0)
2638 eol = col + 1;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002639 if (start_x < x)
2640 break;
2641 }
2642
2643 switch (terminal->dragging) {
2644 case SELECT_LINE:
2645 terminal->selection_start_col = 0;
2646 break;
2647 case SELECT_WORD:
2648 terminal->selection_start_col = word_start;
2649 break;
2650 case SELECT_CHAR:
2651 terminal->selection_start_col = col;
2652 break;
2653 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002654 }
2655
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002656 if (terminal->selection_end_row >= terminal->height) {
2657 terminal->selection_end_row = terminal->height;
2658 terminal->selection_end_col = 0;
2659 } else {
2660 x = side_margin + cw / 2;
2661 data = terminal_get_row(terminal, terminal->selection_end_row);
2662 for (col = 0; col < terminal->width; col++, x += cw) {
2663 if (terminal->dragging == SELECT_CHAR && end_x < x)
2664 break;
2665 if (terminal->dragging == SELECT_WORD &&
2666 end_x < x && wordsep(data[col].ch))
2667 break;
2668 }
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002669 terminal->selection_end_col = col;
2670 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002671
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002672 if (terminal->selection_end_col != terminal->selection_start_col ||
2673 terminal->selection_start_row != terminal->selection_end_row) {
2674 col = terminal->selection_end_col;
2675 if (col > 0 && data[col - 1].ch == 0)
2676 terminal->selection_end_col = terminal->width;
2677 data = terminal_get_row(terminal, terminal->selection_start_row);
2678 if (data[terminal->selection_start_col].ch == 0)
2679 terminal->selection_start_col = eol;
2680 }
2681
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002682 return 1;
2683}
2684
Kristian Høgsberg59826582011-01-20 11:56:57 -05002685static void
Pekka Paalanen807f09c2015-03-27 15:48:54 +02002686terminal_minimize(struct terminal *terminal)
2687{
2688 window_set_minimized(terminal->window);
2689}
2690
2691static void
Jasper St. Pierredda93132014-03-13 12:06:00 -04002692menu_func(void *data, struct input *input, int index)
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002693{
Jasper St. Pierredda93132014-03-13 12:06:00 -04002694 struct window *window = data;
2695 struct terminal *terminal = window_get_user_data(window);
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002696
2697 fprintf(stderr, "picked entry %d\n", index);
2698
2699 switch (index) {
2700 case 0:
2701 terminal_new_instance(terminal);
2702 break;
2703 case 1:
2704 terminal_copy(terminal, input);
2705 break;
2706 case 2:
2707 terminal_paste(terminal, input);
2708 break;
Pekka Paalanen807f09c2015-03-27 15:48:54 +02002709 case 3:
2710 terminal_minimize(terminal);
2711 break;
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002712 }
2713}
2714
2715static void
2716show_menu(struct terminal *terminal, struct input *input, uint32_t time)
2717{
2718 int32_t x, y;
2719 static const char *entries[] = {
Pekka Paalanen807f09c2015-03-27 15:48:54 +02002720 "Open Terminal", "Copy", "Paste", "Minimize"
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002721 };
2722
2723 input_get_position(input, &x, &y);
2724 window_show_menu(terminal->display, input, time, terminal->window,
2725 x - 10, y - 10, menu_func,
2726 entries, ARRAY_LENGTH(entries));
2727}
2728
2729static void
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002730click_handler(struct widget *widget, struct terminal *terminal,
2731 struct input *input, int32_t x, int32_t y,
2732 uint32_t time)
2733{
2734 if (time - terminal->click_time < 500)
2735 terminal->click_count++;
2736 else
2737 terminal->click_count = 1;
2738
2739 terminal->click_time = time;
2740 terminal->dragging = (terminal->click_count - 1) % 3 + SELECT_CHAR;
2741
2742 terminal->selection_end_x = terminal->selection_start_x = x;
2743 terminal->selection_end_y = terminal->selection_start_y = y;
2744 if (recompute_selection(terminal))
2745 widget_schedule_redraw(widget);
2746}
2747
2748static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002749button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002750 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002751 uint32_t button,
2752 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002753{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002754 struct terminal *terminal = data;
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002755 int32_t x, y;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002756
2757 switch (button) {
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002758 case BTN_LEFT:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002759 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002760 input_get_position(input, &x, &y);
2761 click_handler(widget, terminal, input, x, y, time);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002762 } else {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002763 terminal->dragging = SELECT_NONE;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002764 }
2765 break;
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002766
2767 case BTN_RIGHT:
2768 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
2769 show_menu(terminal, input, time);
2770 break;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002771 }
2772}
2773
2774static int
Kristian Høgsberg29784402012-06-28 14:27:02 -04002775enter_handler(struct widget *widget,
2776 struct input *input, float x, float y, void *data)
2777{
2778 return CURSOR_IBEAM;
2779}
2780
2781static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002782motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002783 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002784 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002785{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002786 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002787
2788 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002789 input_get_position(input,
2790 &terminal->selection_end_x,
2791 &terminal->selection_end_y);
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002792
2793 if (recompute_selection(terminal))
2794 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002795 }
2796
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002797 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002798}
2799
Magnus Hoff1046f122014-08-05 15:05:59 +02002800/* This magnitude is chosen rather arbitrarily. Really, the scrolling
2801 * should happen on a (fractional) pixel basis, not a line basis. */
2802#define AXIS_UNITS_PER_LINE 256
2803
2804static void
2805axis_handler(struct widget *widget,
2806 struct input *input, uint32_t time,
2807 uint32_t axis,
2808 wl_fixed_t value,
2809 void *data)
2810{
2811 struct terminal *terminal = data;
2812 int lines;
2813
2814 if (axis != WL_POINTER_AXIS_VERTICAL_SCROLL)
2815 return;
2816
2817 terminal->smooth_scroll += value;
2818 lines = terminal->smooth_scroll / AXIS_UNITS_PER_LINE;
2819 terminal->smooth_scroll -= lines * AXIS_UNITS_PER_LINE;
2820
2821 if (lines > 0) {
2822 if (terminal->scrolling) {
2823 if ((uint32_t)lines > terminal->saved_start - terminal->start)
2824 lines = terminal->saved_start - terminal->start;
2825 } else {
2826 lines = 0;
2827 }
2828 } else if (lines < 0) {
2829 uint32_t neg_lines = -lines;
2830
2831 if (neg_lines > terminal->log_size + terminal->start - terminal->end)
2832 lines = terminal->end - terminal->log_size - terminal->start;
2833 }
2834
2835 if (lines) {
2836 if (!terminal->scrolling)
2837 terminal->saved_start = terminal->start;
2838 terminal->scrolling = 1;
2839
2840 terminal->start += lines;
2841 terminal->row -= lines;
2842 terminal->selection_start_row -= lines;
2843 terminal->selection_end_row -= lines;
2844
2845 widget_schedule_redraw(widget);
2846 }
2847}
2848
Alexander Larssonde79dd02013-05-22 14:41:34 +02002849static void
2850output_handler(struct window *window, struct output *output, int enter,
2851 void *data)
2852{
2853 if (enter)
2854 window_set_buffer_transform(window, output_get_transform(output));
2855 window_set_buffer_scale(window, window_get_output_scale(window));
2856 window_schedule_redraw(window);
2857}
2858
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002859static void
2860touch_down_handler(struct widget *widget, struct input *input,
2861 uint32_t serial, uint32_t time, int32_t id,
2862 float x, float y, void *data)
2863{
2864 struct terminal *terminal = data;
2865
2866 if (id == 0)
2867 click_handler(widget, terminal, input, x, y, time);
2868}
2869
2870static void
2871touch_up_handler(struct widget *widget, struct input *input,
2872 uint32_t serial, uint32_t time, int32_t id, void *data)
2873{
2874 struct terminal *terminal = data;
2875
2876 if (id == 0)
2877 terminal->dragging = SELECT_NONE;
2878}
2879
2880static void
2881touch_motion_handler(struct widget *widget, struct input *input,
2882 uint32_t time, int32_t id, float x, float y, void *data)
2883{
2884 struct terminal *terminal = data;
2885
2886 if (terminal->dragging &&
2887 id == 0) {
2888 terminal->selection_end_x = (int)x;
2889 terminal->selection_end_y = (int)y;
2890
2891 if (recompute_selection(terminal))
2892 widget_schedule_redraw(widget);
2893 }
2894}
2895
Peng Wuf291f202013-06-06 15:32:41 +08002896#ifndef howmany
2897#define howmany(x, y) (((x) + ((y) - 1)) / (y))
2898#endif
2899
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002900static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002901terminal_create(struct display *display)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002902{
2903 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002904 cairo_surface_t *surface;
2905 cairo_t *cr;
Peng Wuf291f202013-06-06 15:32:41 +08002906 cairo_text_extents_t text_extents;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002907
Peter Huttererf3d62272013-08-08 11:57:05 +10002908 terminal = xzalloc(sizeof *terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002909 terminal->color_scheme = &DEFAULT_COLORS;
2910 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002911 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002912 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002913 terminal->window = window_create(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002914 terminal->widget = window_frame_create(terminal->window, terminal);
U. Artie Eoff09827e22014-01-15 11:40:37 -08002915 terminal->title = xstrdup("Wayland Terminal");
Kristian Høgsberga83be202013-10-23 20:47:35 -07002916 window_set_title(terminal->window, terminal->title);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002917 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002918
2919 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002920 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002921
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002922 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002923 terminal->margin = 5;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002924 terminal->buffer_height = 1024;
2925 terminal->end = 1;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002926
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002927 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002928 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002929 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002930 keyboard_focus_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002931 window_set_fullscreen_handler(terminal->window, fullscreen_handler);
Alexander Larssonde79dd02013-05-22 14:41:34 +02002932 window_set_output_handler(terminal->window, output_handler);
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002933 window_set_close_handler(terminal->window, close_handler);
Jasper St. Pierrede680992014-04-10 17:23:49 -07002934 window_set_state_changed_handler(terminal->window, state_changed_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002935
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07002936 window_set_data_handler(terminal->window, data_handler);
2937 window_set_drop_handler(terminal->window, drop_handler);
2938
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002939 widget_set_redraw_handler(terminal->widget, redraw_handler);
2940 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002941 widget_set_button_handler(terminal->widget, button_handler);
Kristian Høgsberg29784402012-06-28 14:27:02 -04002942 widget_set_enter_handler(terminal->widget, enter_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002943 widget_set_motion_handler(terminal->widget, motion_handler);
Magnus Hoff1046f122014-08-05 15:05:59 +02002944 widget_set_axis_handler(terminal->widget, axis_handler);
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002945 widget_set_touch_up_handler(terminal->widget, touch_up_handler);
2946 widget_set_touch_down_handler(terminal->widget, touch_down_handler);
2947 widget_set_touch_motion_handler(terminal->widget, touch_motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002948
Kristian Høgsberg09531622010-06-14 23:22:15 -04002949 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2950 cr = cairo_create(surface);
Kristian Høgsberg38912df2012-06-27 17:52:23 -04002951 cairo_set_font_size(cr, option_font_size);
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002952 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002953 CAIRO_FONT_SLANT_NORMAL,
2954 CAIRO_FONT_WEIGHT_BOLD);
2955 terminal->font_bold = cairo_get_scaled_font (cr);
2956 cairo_scaled_font_reference(terminal->font_bold);
2957
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002958 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002959 CAIRO_FONT_SLANT_NORMAL,
2960 CAIRO_FONT_WEIGHT_NORMAL);
2961 terminal->font_normal = cairo_get_scaled_font (cr);
2962 cairo_scaled_font_reference(terminal->font_normal);
2963
Kristian Høgsberg09531622010-06-14 23:22:15 -04002964 cairo_font_extents(cr, &terminal->extents);
Peng Wuf291f202013-06-06 15:32:41 +08002965
2966 /* Compute the average ascii glyph width */
2967 cairo_text_extents(cr, TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS,
2968 &text_extents);
2969 terminal->average_width = howmany
2970 (text_extents.width,
2971 strlen(TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS));
2972 terminal->average_width = ceil(terminal->average_width);
2973
Kristian Høgsberg09531622010-06-14 23:22:15 -04002974 cairo_destroy(cr);
2975 cairo_surface_destroy(surface);
2976
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002977 terminal_resize(terminal, 20, 5); /* Set minimum size first */
Kristian Høgsberga1627922012-06-20 17:30:03 -04002978 terminal_resize(terminal, 80, 25);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002979
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002980 wl_list_insert(terminal_list.prev, &terminal->link);
2981
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002982 return terminal;
2983}
2984
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002985static void
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002986terminal_destroy(struct terminal *terminal)
2987{
Dima Ryazanova85292e2012-11-29 00:27:09 -08002988 display_unwatch_fd(terminal->display, terminal->master);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002989 window_destroy(terminal->window);
2990 close(terminal->master);
2991 wl_list_remove(&terminal->link);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002992
2993 if (wl_list_empty(&terminal_list))
Dima Ryazanova85292e2012-11-29 00:27:09 -08002994 display_exit(terminal->display);
2995
Kristian Høgsberga83be202013-10-23 20:47:35 -07002996 free(terminal->title);
Dima Ryazanova85292e2012-11-29 00:27:09 -08002997 free(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002998}
2999
3000static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003001io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003002{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003003 struct terminal *terminal =
3004 container_of(task, struct terminal, io_task);
3005 char buffer[256];
3006 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003007
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003008 if (events & EPOLLHUP) {
3009 terminal_destroy(terminal);
3010 return;
3011 }
Tim Wiederhakef71accc2011-01-19 23:14:33 +01003012
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003013 len = read(terminal->master, buffer, sizeof buffer);
3014 if (len < 0)
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003015 terminal_destroy(terminal);
3016 else
3017 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003018}
3019
3020static int
3021terminal_run(struct terminal *terminal, const char *path)
3022{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003023 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003024 pid_t pid;
3025
3026 pid = forkpty(&master, NULL, NULL, NULL);
3027 if (pid == 0) {
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04003028 setenv("TERM", option_term, 1);
3029 setenv("COLORTERM", option_term, 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003030 if (execl(path, path, NULL)) {
3031 printf("exec failed: %m\n");
3032 exit(EXIT_FAILURE);
3033 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003034 } else if (pid < 0) {
3035 fprintf(stderr, "failed to fork and create pty (%m).\n");
3036 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003037 }
3038
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003039 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003040 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003041 terminal->io_task.run = io_handler;
3042 display_watch_fd(terminal->display, terminal->master,
3043 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003044
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07003045 if (option_fullscreen)
3046 window_set_fullscreen(terminal->window, 1);
3047 else
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003048 terminal_resize(terminal, 80, 24);
3049
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003050 return 0;
3051}
3052
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003053static const struct weston_option terminal_options[] = {
3054 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04003055 { WESTON_OPTION_STRING, "font", 0, &option_font },
Bill Spitzak5cad8432014-08-08 12:59:55 -07003056 { WESTON_OPTION_INTEGER, "font-size", 0, &option_font_size },
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003057 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003058};
3059
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003060int main(int argc, char *argv[])
3061{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003062 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003063 struct terminal *terminal;
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02003064 const char *config_file;
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -07003065 struct weston_config *config;
3066 struct weston_config_section *s;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04003067
Peng Wucfcc1112013-08-19 10:59:21 +08003068 /* as wcwidth is locale-dependent,
3069 wcwidth needs setlocale call to function properly. */
3070 setlocale(LC_ALL, "");
3071
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003072 option_shell = getenv("SHELL");
3073 if (!option_shell)
3074 option_shell = "/bin/bash";
3075
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02003076 config_file = weston_config_get_name_from_env();
3077 config = weston_config_parse(config_file);
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -07003078 s = weston_config_get_section(config, "terminal", NULL, NULL);
3079 weston_config_section_get_string(s, "font", &option_font, "mono");
3080 weston_config_section_get_int(s, "font-size", &option_font_size, 14);
3081 weston_config_section_get_string(s, "term", &option_term, "xterm");
3082 weston_config_destroy(config);
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003083
Bill Spitzak5cad8432014-08-08 12:59:55 -07003084 if (parse_options(terminal_options,
3085 ARRAY_LENGTH(terminal_options), &argc, argv) > 1) {
3086 printf("Usage: %s [OPTIONS]\n"
3087 " --fullscreen or -f\n"
3088 " --font=NAME\n"
3089 " --font-size=SIZE\n"
3090 " --shell=NAME\n", argv[0]);
3091 return 1;
3092 }
3093
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003094 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02003095 if (d == NULL) {
3096 fprintf(stderr, "failed to create display: %m\n");
3097 return -1;
3098 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003099
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003100 wl_list_init(&terminal_list);
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04003101 terminal = terminal_create(d);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003102 if (terminal_run(terminal, option_shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003103 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003104
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003105 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003106
3107 return 0;
3108}