blob: 67e8d1de5751496d3efc97cb40dc1b35324cb818 [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"
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050047#include "window.h"
48
Kristian Høgsberg0395f302008-12-22 12:14:50 -050049static int option_fullscreen;
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -070050static char *option_font;
51static int option_font_size;
52static char *option_term;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040053static char *option_shell;
54
55static struct wl_list terminal_list;
56
57static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -040058terminal_create(struct display *display);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040059static void
60terminal_destroy(struct terminal *terminal);
61static int
62terminal_run(struct terminal *terminal, const char *path);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050063
Peng Wuf291f202013-06-06 15:32:41 +080064#define TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS \
65 " !\"#$%&'()*+,-./" \
66 "0123456789" \
67 ":;<=>?@" \
68 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
69 "[\\]^_`" \
70 "abcdefghijklmnopqrstuvwxyz" \
71 "{|}~" \
72 ""
73
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050074#define MOD_SHIFT 0x01
75#define MOD_ALT 0x02
76#define MOD_CTRL 0x04
77
Callum Lowcay30eeae52011-01-07 19:46:55 +000078#define ATTRMASK_BOLD 0x01
79#define ATTRMASK_UNDERLINE 0x02
80#define ATTRMASK_BLINK 0x04
81#define ATTRMASK_INVERSE 0x08
Callum Lowcay81179db2011-01-10 12:14:01 +130082#define ATTRMASK_CONCEALED 0x10
Callum Lowcay30eeae52011-01-07 19:46:55 +000083
Callum Lowcayb8609ad2011-01-07 19:46:57 +000084/* Buffer sizes */
Callum Lowcayef57a9b2011-01-14 20:46:23 +130085#define MAX_RESPONSE 256
86#define MAX_ESCAPE 255
Callum Lowcayb8609ad2011-01-07 19:46:57 +000087
Callum Lowcay8e57dd52011-01-07 19:46:59 +000088/* Terminal modes */
89#define MODE_SHOW_CURSOR 0x00000001
90#define MODE_INVERSE 0x00000002
91#define MODE_AUTOWRAP 0x00000004
92#define MODE_AUTOREPEAT 0x00000008
93#define MODE_LF_NEWLINE 0x00000010
Callum Lowcay69e96582011-01-07 19:47:00 +000094#define MODE_IRM 0x00000020
Callum Lowcay7e08e902011-01-07 19:47:02 +000095#define MODE_DELETE_SENDS_DEL 0x00000040
96#define MODE_ALT_SENDS_ESC 0x00000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +000097
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000098union utf8_char {
99 unsigned char byte[4];
100 uint32_t ch;
101};
102
103enum utf8_state {
104 utf8state_start,
105 utf8state_accept,
106 utf8state_reject,
107 utf8state_expect3,
108 utf8state_expect2,
109 utf8state_expect1
110};
111
112struct utf8_state_machine {
113 enum utf8_state state;
114 int len;
115 union utf8_char s;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700116 uint32_t unicode;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000117};
118
119static void
120init_state_machine(struct utf8_state_machine *machine)
121{
122 machine->state = utf8state_start;
123 machine->len = 0;
124 machine->s.ch = 0;
125}
126
127static enum utf8_state
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400128utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000129{
130 switch(machine->state) {
131 case utf8state_start:
132 case utf8state_accept:
133 case utf8state_reject:
134 machine->s.ch = 0;
135 machine->len = 0;
136 if(c == 0xC0 || c == 0xC1) {
137 /* overlong encoding, reject */
138 machine->state = utf8state_reject;
139 } else if((c & 0x80) == 0) {
140 /* single byte, accept */
141 machine->s.byte[machine->len++] = c;
142 machine->state = utf8state_accept;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700143 machine->unicode = c;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000144 } else if((c & 0xC0) == 0x80) {
145 /* parser out of sync, ignore byte */
146 machine->state = utf8state_start;
147 } else if((c & 0xE0) == 0xC0) {
148 /* start of two byte sequence */
149 machine->s.byte[machine->len++] = c;
150 machine->state = utf8state_expect1;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700151 machine->unicode = c & 0x1f;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000152 } else if((c & 0xF0) == 0xE0) {
153 /* start of three byte sequence */
154 machine->s.byte[machine->len++] = c;
155 machine->state = utf8state_expect2;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700156 machine->unicode = c & 0x0f;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000157 } else if((c & 0xF8) == 0xF0) {
158 /* start of four byte sequence */
159 machine->s.byte[machine->len++] = c;
160 machine->state = utf8state_expect3;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700161 machine->unicode = c & 0x07;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000162 } else {
163 /* overlong encoding, reject */
164 machine->state = utf8state_reject;
165 }
166 break;
167 case utf8state_expect3:
168 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700169 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000170 if((c & 0xC0) == 0x80) {
171 /* all good, continue */
172 machine->state = utf8state_expect2;
173 } else {
174 /* missing extra byte, reject */
175 machine->state = utf8state_reject;
176 }
177 break;
178 case utf8state_expect2:
179 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700180 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000181 if((c & 0xC0) == 0x80) {
182 /* all good, continue */
183 machine->state = utf8state_expect1;
184 } else {
185 /* missing extra byte, reject */
186 machine->state = utf8state_reject;
187 }
188 break;
189 case utf8state_expect1:
190 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700191 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000192 if((c & 0xC0) == 0x80) {
193 /* all good, accept */
194 machine->state = utf8state_accept;
195 } else {
196 /* missing extra byte, reject */
197 machine->state = utf8state_reject;
198 }
199 break;
200 default:
201 machine->state = utf8state_reject;
202 break;
203 }
Michael Vetter2a18a522015-05-15 17:17:47 +0200204
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000205 return machine->state;
206}
207
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700208static uint32_t
209get_unicode(union utf8_char utf8)
210{
211 struct utf8_state_machine machine;
212 int i;
213
214 init_state_machine(&machine);
215 for (i = 0; i < 4; i++) {
216 utf8_next_char(&machine, utf8.byte[i]);
217 if (machine.state == utf8state_accept ||
218 machine.state == utf8state_reject)
219 break;
220 }
221
222 if (machine.state == utf8state_reject)
223 return 0xfffd;
224
225 return machine.unicode;
226}
227
Peng Wucfcc1112013-08-19 10:59:21 +0800228static bool
229is_wide(union utf8_char utf8)
230{
231 uint32_t unichar = get_unicode(utf8);
232 return wcwidth(unichar) > 1;
233}
234
Callum Lowcay256e72f2011-01-07 19:47:01 +0000235struct char_sub {
236 union utf8_char match;
237 union utf8_char replace;
238};
239/* Set last char_sub match to NULL char */
240typedef struct char_sub *character_set;
241
242struct char_sub CS_US[] = {
243 {{{0, }}, {{0, }}}
244};
245static struct char_sub CS_UK[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200246 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000247 {{{0, }}, {{0, }}}
248};
249static struct char_sub CS_SPECIAL[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200250 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond: ♦ */
251 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell: ▒ */
252 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT: ␉ */
253 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF: ␌ */
254 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR: ␍ */
255 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF: ␊ */
256 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree: ° */
257 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus: ± */
258 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL: ␤ */
259 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT: ␋ */
260 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB: ┘ */
261 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT: ┐ */
262 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT: ┌ */
263 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_LB: └ */
264 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS: ┼ */
265 {{{'o', 0, }}, {{0xE2, 0x8E, 0xBA, 0}}}, /* Horiz. Scan Line 1: ⎺ */
266 {{{'p', 0, }}, {{0xE2, 0x8E, 0xBB, 0}}}, /* Horiz. Scan Line 3: ⎻ */
267 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* Horiz. Scan Line 5: ─ */
268 {{{'r', 0, }}, {{0xE2, 0x8E, 0xBC, 0}}}, /* Horiz. Scan Line 7: ⎼ */
269 {{{'s', 0, }}, {{0xE2, 0x8E, 0xBD, 0}}}, /* Horiz. Scan Line 9: ⎽ */
270 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR: ├ */
271 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL: ┤ */
272 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU: ┴ */
273 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD: ┬ */
274 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V: │ */
275 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE: ≤ */
276 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE: ≥ */
277 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI: π */
278 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ: ≠ */
279 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
280 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT: ⋅ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000281 {{{0, }}, {{0, }}}
282};
283
284static void
285apply_char_set(character_set cs, union utf8_char *utf8)
286{
287 int i = 0;
Michael Vetter2a18a522015-05-15 17:17:47 +0200288
Callum Lowcay256e72f2011-01-07 19:47:01 +0000289 while (cs[i].match.byte[0]) {
290 if ((*utf8).ch == cs[i].match.ch) {
291 *utf8 = cs[i].replace;
292 break;
293 }
294 i++;
295 }
296}
297
Callum Lowcay7e08e902011-01-07 19:47:02 +0000298struct key_map {
299 int sym;
300 int num;
301 char escape;
302 char code;
303};
304/* Set last key_sub sym to NULL */
305typedef struct key_map *keyboard_mode;
306
307static struct key_map KM_NORMAL[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400308 { XKB_KEY_Left, 1, '[', 'D' },
309 { XKB_KEY_Right, 1, '[', 'C' },
310 { XKB_KEY_Up, 1, '[', 'A' },
311 { XKB_KEY_Down, 1, '[', 'B' },
312 { XKB_KEY_Home, 1, '[', 'H' },
313 { XKB_KEY_End, 1, '[', 'F' },
314 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000315};
316static struct key_map KM_APPLICATION[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400317 { XKB_KEY_Left, 1, 'O', 'D' },
318 { XKB_KEY_Right, 1, 'O', 'C' },
319 { XKB_KEY_Up, 1, 'O', 'A' },
320 { XKB_KEY_Down, 1, 'O', 'B' },
321 { XKB_KEY_Home, 1, 'O', 'H' },
322 { XKB_KEY_End, 1, 'O', 'F' },
323 { XKB_KEY_KP_Enter, 1, 'O', 'M' },
324 { XKB_KEY_KP_Multiply, 1, 'O', 'j' },
325 { XKB_KEY_KP_Add, 1, 'O', 'k' },
326 { XKB_KEY_KP_Separator, 1, 'O', 'l' },
327 { XKB_KEY_KP_Subtract, 1, 'O', 'm' },
328 { XKB_KEY_KP_Divide, 1, 'O', 'o' },
329 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000330};
331
332static int
333function_key_response(char escape, int num, uint32_t modifiers,
334 char code, char *response)
335{
336 int mod_num = 0;
337 int len;
338
Kristian Høgsberg70163132012-05-08 15:55:39 -0400339 if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
340 if (modifiers & MOD_ALT_MASK) mod_num |= 2;
341 if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000342
343 if (mod_num != 0)
344 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
345 num, mod_num + 1, code);
346 else if (code != '~')
347 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
348 escape, code);
349 else
350 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
351 escape, num, code);
352
353 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
354 else return len;
355}
356
357/* returns the number of bytes written into response,
358 * which must have room for MAX_RESPONSE bytes */
359static int
360apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
361{
362 struct key_map map;
363 int len = 0;
364 int i = 0;
Michael Vetter2a18a522015-05-15 17:17:47 +0200365
Callum Lowcay7e08e902011-01-07 19:47:02 +0000366 while (mode[i].sym) {
367 map = mode[i++];
368 if (sym == map.sym) {
369 len = function_key_response(map.escape, map.num,
370 modifiers, map.code,
371 response);
372 break;
373 }
374 }
Michael Vetter2a18a522015-05-15 17:17:47 +0200375
Callum Lowcay7e08e902011-01-07 19:47:02 +0000376 return len;
377}
378
Callum Lowcay30eeae52011-01-07 19:46:55 +0000379struct terminal_color { double r, g, b, a; };
380struct attr {
381 unsigned char fg, bg;
382 char a; /* attributes format:
383 * 76543210
Callum Lowcay81179db2011-01-10 12:14:01 +1300384 * cilub */
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500385 char s; /* in selection */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000386};
387struct color_scheme {
388 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500389 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000390 struct attr default_attr;
391};
392
393static void
394attr_init(struct attr *data_attr, struct attr attr, int n)
395{
396 int i;
397 for (i = 0; i < n; i++) {
398 data_attr[i] = attr;
399 }
400}
401
Callum Lowcay67a201d2011-01-12 19:23:41 +1300402enum escape_state {
403 escape_state_normal = 0,
404 escape_state_escape,
405 escape_state_dcs,
406 escape_state_csi,
407 escape_state_osc,
408 escape_state_inner_escape,
409 escape_state_ignore,
410 escape_state_special
411};
412
413#define ESC_FLAG_WHAT 0x01
414#define ESC_FLAG_GT 0x02
415#define ESC_FLAG_BANG 0x04
416#define ESC_FLAG_CASH 0x08
417#define ESC_FLAG_SQUOTE 0x10
418#define ESC_FLAG_DQUOTE 0x20
419#define ESC_FLAG_SPACE 0x40
420
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400421enum {
422 SELECT_NONE,
423 SELECT_CHAR,
424 SELECT_WORD,
425 SELECT_LINE
426};
427
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500428struct terminal {
429 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500430 struct widget *widget;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500431 struct display *display;
Kristian Høgsberga83be202013-10-23 20:47:35 -0700432 char *title;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000433 union utf8_char *data;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400434 struct task io_task;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000435 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000436 struct attr *data_attr;
437 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000438 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000439 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000440 char saved_origin_mode;
441 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000442 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000443 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000444 character_set cs, g0, g1;
445 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000446 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000447 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700448 int width, height, row, column, max_width;
449 uint32_t buffer_height;
450 uint32_t start, end, saved_start, log_size;
Magnus Hoff1046f122014-08-05 15:05:59 +0200451 wl_fixed_t smooth_scroll;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000452 int saved_row, saved_column;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700453 int scrolling;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600454 int send_cursor_position;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500455 int fd, master;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500456 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300457 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500458 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300459 enum escape_state state;
460 enum escape_state outer_state;
461 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000462 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500463 int margin;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400464 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000465 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400466 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +0800467 double average_width;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500468 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg88fd4082012-06-21 15:55:03 -0400469 uint32_t hide_cursor_serial;
Kristian Høgsbergb405a802014-02-05 14:44:04 -0800470 int size_in_title;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500471
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400472 struct wl_data_source *selection;
Xiong Zhang49c6aee2013-11-25 18:42:52 +0800473 uint32_t click_time;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400474 int dragging, click_count;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500475 int selection_start_x, selection_start_y;
476 int selection_end_x, selection_end_y;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400477 int selection_start_row, selection_start_col;
478 int selection_end_row, selection_end_col;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -0400479 struct wl_list link;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500480};
481
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000482/* Create default tab stops, every 8 characters */
483static void
484terminal_init_tabs(struct terminal *terminal)
485{
486 int i = 0;
Michael Vetter2a18a522015-05-15 17:17:47 +0200487
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000488 while (i < terminal->width) {
489 if (i % 8 == 0)
490 terminal->tab_ruler[i] = 1;
491 else
492 terminal->tab_ruler[i] = 0;
493 i++;
494 }
495}
496
Callum Lowcay30eeae52011-01-07 19:46:55 +0000497static void
498terminal_init(struct terminal *terminal)
499{
500 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000501 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000502 terminal->mode = MODE_SHOW_CURSOR |
503 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000504 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000505 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000506
507 terminal->row = 0;
508 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000509
Callum Lowcay256e72f2011-01-07 19:47:01 +0000510 terminal->g0 = CS_US;
511 terminal->g1 = CS_US;
512 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000513 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000514
515 terminal->saved_g0 = terminal->g0;
516 terminal->saved_g1 = terminal->g1;
517 terminal->saved_cs = terminal->cs;
518
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000519 terminal->saved_attr = terminal->curr_attr;
520 terminal->saved_origin_mode = terminal->origin_mode;
521 terminal->saved_row = terminal->row;
522 terminal->saved_column = terminal->column;
523
524 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000525}
526
527static void
528init_color_table(struct terminal *terminal)
529{
530 int c, r;
531 struct terminal_color *color_table = terminal->color_table;
532
533 for (c = 0; c < 256; c ++) {
534 if (c < 16) {
535 color_table[c] = terminal->color_scheme->palette[c];
536 } else if (c < 232) {
537 r = c - 16;
538 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
539 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
540 color_table[c].r = ((double)(r % 6) / 6.0);
541 color_table[c].a = 1.0;
542 } else {
543 r = (c - 232) * 10 + 8;
544 color_table[c].r = ((double) r) / 256.0;
545 color_table[c].g = color_table[c].r;
546 color_table[c].b = color_table[c].r;
547 color_table[c].a = 1.0;
548 }
549 }
550}
551
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000552static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500553terminal_get_row(struct terminal *terminal, int row)
554{
555 int index;
556
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700557 index = (row + terminal->start) & (terminal->buffer_height - 1);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500558
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700559 return (void *) terminal->data + index * terminal->data_pitch;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500560}
561
Callum Lowcay30eeae52011-01-07 19:46:55 +0000562static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500563terminal_get_attr_row(struct terminal *terminal, int row)
564{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000565 int index;
566
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700567 index = (row + terminal->start) & (terminal->buffer_height - 1);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000568
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700569 return (void *) terminal->data_attr + index * terminal->attr_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000570}
571
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500572union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300573 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500574 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500575};
576
577static void
578terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500579 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500580{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500581 struct attr attr;
582 int foreground, background, tmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500583
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500584 decoded->attr.s = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400585 if (((row == terminal->selection_start_row &&
586 col >= terminal->selection_start_col) ||
587 row > terminal->selection_start_row) &&
588 ((row == terminal->selection_end_row &&
589 col < terminal->selection_end_col) ||
590 row < terminal->selection_end_row))
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500591 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500592
593 /* get the attributes for this character cell */
594 attr = terminal_get_attr_row(terminal, row)[col];
595 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500596 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500597 ((terminal->mode & MODE_SHOW_CURSOR) &&
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400598 window_has_focus(terminal->window) && terminal->row == row &&
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500599 terminal->column == col)) {
600 foreground = attr.bg;
601 background = attr.fg;
602 if (attr.a & ATTRMASK_BOLD) {
603 if (foreground <= 16) foreground |= 0x08;
604 if (background <= 16) background &= 0x07;
605 }
606 } else {
607 foreground = attr.fg;
608 background = attr.bg;
609 }
610
611 if (terminal->mode & MODE_INVERSE) {
612 tmp = foreground;
613 foreground = background;
614 background = tmp;
615 if (attr.a & ATTRMASK_BOLD) {
616 if (foreground <= 16) foreground |= 0x08;
617 if (background <= 16) background &= 0x07;
618 }
619 }
620
Callum Lowcay9d708b02011-01-12 20:06:17 +1300621 decoded->attr.fg = foreground;
622 decoded->attr.bg = background;
623 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000624}
625
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500626
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500627static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000628terminal_scroll_buffer(struct terminal *terminal, int d)
629{
630 int i;
631
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700632 terminal->start += d;
633 if (d < 0) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000634 d = 0 - d;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700635 for (i = 0; i < d; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000636 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
637 attr_init(terminal_get_attr_row(terminal, i),
638 terminal->curr_attr, terminal->width);
639 }
640 } else {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700641 for (i = terminal->height - d; i < terminal->height; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000642 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
643 attr_init(terminal_get_attr_row(terminal, i),
644 terminal->curr_attr, terminal->width);
645 }
646 }
Kristian Høgsberg18e928d2012-06-27 19:29:41 -0400647
648 terminal->selection_start_row -= d;
649 terminal->selection_end_row -= d;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000650}
651
652static void
653terminal_scroll_window(struct terminal *terminal, int d)
654{
655 int i;
656 int window_height;
657 int from_row, to_row;
Michael Vetter2a18a522015-05-15 17:17:47 +0200658
Callum Lowcaybbeac602011-01-07 19:46:58 +0000659 // scrolling range is inclusive
660 window_height = terminal->margin_bottom - terminal->margin_top + 1;
661 d = d % (window_height + 1);
662 if(d < 0) {
663 d = 0 - d;
664 to_row = terminal->margin_bottom;
665 from_row = terminal->margin_bottom - d;
Michael Vetter2a18a522015-05-15 17:17:47 +0200666
Callum Lowcaybbeac602011-01-07 19:46:58 +0000667 for (i = 0; i < (window_height - d); i++) {
668 memcpy(terminal_get_row(terminal, to_row - i),
669 terminal_get_row(terminal, from_row - i),
670 terminal->data_pitch);
671 memcpy(terminal_get_attr_row(terminal, to_row - i),
672 terminal_get_attr_row(terminal, from_row - i),
673 terminal->attr_pitch);
674 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000675 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
676 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000677 attr_init(terminal_get_attr_row(terminal, i),
678 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000679 }
680 } else {
681 to_row = terminal->margin_top;
682 from_row = terminal->margin_top + d;
Michael Vetter2a18a522015-05-15 17:17:47 +0200683
Callum Lowcaybbeac602011-01-07 19:46:58 +0000684 for (i = 0; i < (window_height - d); i++) {
685 memcpy(terminal_get_row(terminal, to_row + i),
686 terminal_get_row(terminal, from_row + i),
687 terminal->data_pitch);
688 memcpy(terminal_get_attr_row(terminal, to_row + i),
689 terminal_get_attr_row(terminal, from_row + i),
690 terminal->attr_pitch);
691 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000692 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
693 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000694 attr_init(terminal_get_attr_row(terminal, i),
695 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000696 }
697 }
698}
699
700static void
701terminal_scroll(struct terminal *terminal, int d)
702{
703 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
704 terminal_scroll_buffer(terminal, d);
705 else
706 terminal_scroll_window(terminal, d);
707}
708
709static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000710terminal_shift_line(struct terminal *terminal, int d)
711{
712 union utf8_char *row;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500713 struct attr *attr_row;
Michael Vetter2a18a522015-05-15 17:17:47 +0200714
Callum Lowcay69e96582011-01-07 19:47:00 +0000715 row = terminal_get_row(terminal, terminal->row);
716 attr_row = terminal_get_attr_row(terminal, terminal->row);
717
718 if ((terminal->width + d) <= terminal->column)
719 d = terminal->column + 1 - terminal->width;
720 if ((terminal->column + d) >= terminal->width)
721 d = terminal->width - terminal->column - 1;
Michael Vetter2a18a522015-05-15 17:17:47 +0200722
Callum Lowcay69e96582011-01-07 19:47:00 +0000723 if (d < 0) {
724 d = 0 - d;
725 memmove(&row[terminal->column],
726 &row[terminal->column + d],
727 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
Callum Lowcay69e96582011-01-07 19:47:00 +0000728 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
729 (terminal->width - terminal->column - d) * sizeof(struct attr));
730 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
731 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
732 } else {
733 memmove(&row[terminal->column + d], &row[terminal->column],
734 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
735 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
736 (terminal->width - terminal->column - d) * sizeof(struct attr));
737 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
738 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
739 }
740}
741
742static void
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700743terminal_resize_cells(struct terminal *terminal,
744 int width, int height)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500745{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000746 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000747 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000748 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000749 int data_pitch, attr_pitch;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500750 int i, l, total_rows;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700751 uint32_t d, uheight = height;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500752 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000753 struct winsize ws;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500754
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700755 if (uheight > terminal->buffer_height)
756 height = terminal->buffer_height;
757
Kristian Høgsberg22106762008-12-08 13:50:07 -0500758 if (terminal->width == width && terminal->height == height)
759 return;
760
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700761 if (terminal->data && width <= terminal->max_width) {
762 d = 0;
763 if (height < terminal->height && height <= terminal->row)
764 d = terminal->height - height;
765 else if (height > terminal->height &&
766 terminal->height - 1 == terminal->row) {
767 d = terminal->height - height;
768 if (terminal->log_size < uheight)
769 d = -terminal->start;
770 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500771
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700772 terminal->start += d;
773 terminal->row -= d;
774 } else {
775 terminal->max_width = width;
776 data_pitch = width * sizeof(union utf8_char);
Bryce W. Harrington66cd2c12014-04-28 18:44:08 +0000777 data = xzalloc(data_pitch * terminal->buffer_height);
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700778 attr_pitch = width * sizeof(struct attr);
Bryce W. Harrington66cd2c12014-04-28 18:44:08 +0000779 data_attr = xmalloc(attr_pitch * terminal->buffer_height);
780 tab_ruler = xzalloc(width);
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700781 attr_init(data_attr, terminal->curr_attr,
782 width * terminal->buffer_height);
783
784 if (terminal->data && terminal->data_attr) {
785 if (width > terminal->width)
786 l = terminal->width;
787 else
788 l = width;
789
790 if (terminal->height > height) {
791 total_rows = height;
792 i = 1 + terminal->row - height;
793 if (i > 0) {
794 terminal->start += i;
795 terminal->row = terminal->row - i;
796 }
797 } else {
798 total_rows = terminal->height;
José Bollo4a4704a2013-09-13 11:28:51 +0200799 }
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700800
801 for (i = 0; i < total_rows; i++) {
802 memcpy(&data[width * i],
803 terminal_get_row(terminal, i),
804 l * sizeof(union utf8_char));
805 memcpy(&data_attr[width * i],
806 terminal_get_attr_row(terminal, i),
807 l * sizeof(struct attr));
808 }
809
810 free(terminal->data);
811 free(terminal->data_attr);
812 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500813 }
814
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700815 terminal->data_pitch = data_pitch;
816 terminal->attr_pitch = attr_pitch;
817 terminal->data = data;
818 terminal->data_attr = data_attr;
819 terminal->tab_ruler = tab_ruler;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700820 terminal->start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500821 }
822
Callum Lowcay86653ed2011-01-07 19:47:03 +0000823 terminal->margin_bottom =
824 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500825 terminal->width = width;
826 terminal->height = height;
Kristian Høgsbergdcfff552013-11-22 22:43:20 -0800827 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500828
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000829 /* Update the window size */
830 ws.ws_row = terminal->height;
831 ws.ws_col = terminal->width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500832 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500833 ws.ws_xpixel = allocation.width;
834 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000835 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500836}
837
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500838static void
Jasper St. Pierrede680992014-04-10 17:23:49 -0700839update_title(struct terminal *terminal)
840{
841 if (window_is_resizing(terminal->window)) {
842 char *p;
843 if (asprintf(&p, "%s — [%dx%d]", terminal->title, terminal->width, terminal->height) > 0) {
844 window_set_title(terminal->window, p);
845 free(p);
846 }
847 } else {
848 window_set_title(terminal->window, terminal->title);
849 }
850}
851
852static void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500853resize_handler(struct widget *widget,
854 int32_t width, int32_t height, void *data)
855{
856 struct terminal *terminal = data;
857 int32_t columns, rows, m;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700858
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500859 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800860 columns = (width - m) / (int32_t) terminal->average_width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500861 rows = (height - m) / (int32_t) terminal->extents.height;
862
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400863 if (!window_is_fullscreen(terminal->window) &&
864 !window_is_maximized(terminal->window)) {
Peng Wuf291f202013-06-06 15:32:41 +0800865 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500866 height = rows * terminal->extents.height + m;
867 widget_set_size(terminal->widget, width, height);
868 }
869
870 terminal_resize_cells(terminal, columns, rows);
Jasper St. Pierrede680992014-04-10 17:23:49 -0700871 update_title(terminal);
872}
873
874static void
875state_changed_handler(struct window *window, void *data)
876{
877 struct terminal *terminal = data;
878 update_title(terminal);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500879}
880
881static void
882terminal_resize(struct terminal *terminal, int columns, int rows)
883{
884 int32_t width, height, m;
885
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400886 if (window_is_fullscreen(terminal->window) ||
887 window_is_maximized(terminal->window))
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500888 return;
889
890 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800891 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500892 height = rows * terminal->extents.height + m;
Kristian Høgsberga1627922012-06-20 17:30:03 -0400893
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500894 window_frame_set_child_size(terminal->widget, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500895}
896
Callum Lowcay30eeae52011-01-07 19:46:55 +0000897struct color_scheme DEFAULT_COLORS = {
898 {
899 {0, 0, 0, 1}, /* black */
900 {0.66, 0, 0, 1}, /* red */
901 {0 , 0.66, 0, 1}, /* green */
902 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
903 {0 , 0 , 0.66, 1}, /* blue */
904 {0.66, 0 , 0.66, 1}, /* magenta */
905 {0, 0.66, 0.66, 1}, /* cyan */
906 {0.66, 0.66, 0.66, 1}, /* light grey */
907 {0.22, 0.33, 0.33, 1}, /* dark grey */
908 {1, 0.33, 0.33, 1}, /* high red */
909 {0.33, 1, 0.33, 1}, /* high green */
910 {1, 1, 0.33, 1}, /* high yellow */
911 {0.33, 0.33, 1, 1}, /* high blue */
912 {1, 0.33, 1, 1}, /* high magenta */
913 {0.33, 1, 1, 1}, /* high cyan */
914 {1, 1, 1, 1} /* white */
915 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500916 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000917 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
918};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400919
Kristian Høgsberg22106762008-12-08 13:50:07 -0500920static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500921terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
922{
923 cairo_set_source_rgba(cr,
924 terminal->color_table[index].r,
925 terminal->color_table[index].g,
926 terminal->color_table[index].b,
927 terminal->color_table[index].a);
928}
929
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500930static void
931terminal_send_selection(struct terminal *terminal, int fd)
932{
933 int row, col;
934 union utf8_char *p_row;
935 union decoded_attr attr;
936 FILE *fp;
937 int len;
938
939 fp = fdopen(fd, "w");
Brian Lovin1bf14812013-08-08 16:12:55 -0700940 if (fp == NULL){
941 close(fd);
942 return;
943 }
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500944 for (row = 0; row < terminal->height; row++) {
945 p_row = terminal_get_row(terminal, row);
946 for (col = 0; col < terminal->width; col++) {
Peng Wucfcc1112013-08-19 10:59:21 +0800947 if (p_row[col].ch == 0x200B) /* space glyph */
948 continue;
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500949 /* get the attributes for this character cell */
950 terminal_decode_attr(terminal, row, col, &attr);
951 if (!attr.attr.s)
952 continue;
953 len = strnlen((char *) p_row[col].byte, 4);
Kristian Høgsberg0dee6472012-07-01 21:25:41 -0400954 if (len > 0)
955 fwrite(p_row[col].byte, 1, len, fp);
956 if (len == 0 || col == terminal->width - 1) {
957 fwrite("\n", 1, 1, fp);
958 break;
959 }
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500960 }
961 }
962 fclose(fp);
963}
964
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500965struct glyph_run {
966 struct terminal *terminal;
967 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400968 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500969 union decoded_attr attr;
970 cairo_glyph_t glyphs[256], *g;
971};
972
973static void
974glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
975{
976 run->terminal = terminal;
977 run->cr = cr;
978 run->g = run->glyphs;
979 run->count = 0;
980 run->attr.key = 0;
981}
982
983static void
984glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
985{
986 cairo_scaled_font_t *font;
987
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500988 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
989 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300990 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500991 font = run->terminal->font_bold;
992 else
993 font = run->terminal->font_normal;
994 cairo_set_scaled_font(run->cr, font);
995 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300996 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500997
Callum Lowcay9d708b02011-01-12 20:06:17 +1300998 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
999 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001000 run->g = run->glyphs;
1001 run->count = 0;
1002 }
Callum Lowcay9d708b02011-01-12 20:06:17 +13001003 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001004}
1005
1006static void
1007glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
1008{
1009 int num_glyphs;
1010 cairo_scaled_font_t *font;
1011
1012 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
1013
Callum Lowcay9d708b02011-01-12 20:06:17 +13001014 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001015 font = run->terminal->font_bold;
1016 else
1017 font = run->terminal->font_normal;
1018
1019 cairo_move_to(run->cr, x, y);
1020 cairo_scaled_font_text_to_glyphs (font, x, y,
1021 (char *) c->byte, 4,
1022 &run->g, &num_glyphs,
1023 NULL, NULL, NULL);
1024 run->g += num_glyphs;
1025 run->count += num_glyphs;
1026}
1027
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001028
Kristian Høgsbergf106fd52011-01-11 10:11:39 -05001029static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001030redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001031{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001032 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001033 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001034 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001035 int top_margin, side_margin;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001036 int row, col, cursor_x, cursor_y;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001037 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001038 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001039 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001040 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001041 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001042 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -05001043 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +08001044 double average_width;
Peng Wucfcc1112013-08-19 10:59:21 +08001045 double unichar_width;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001046
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001047 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001048 widget_get_allocation(terminal->widget, &allocation);
Alexander Larssonde79dd02013-05-22 14:41:34 +02001049 cr = widget_cairo_create(terminal->widget);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001050 cairo_rectangle(cr, allocation.x, allocation.y,
1051 allocation.width, allocation.height);
1052 cairo_clip(cr);
1053 cairo_push_group(cr);
1054
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001055 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -05001056 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001057 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001058
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05001059 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001060
Kristian Høgsberg59826582011-01-20 11:56:57 -05001061 extents = terminal->extents;
Peng Wuf291f202013-06-06 15:32:41 +08001062 average_width = terminal->average_width;
1063 side_margin = (allocation.width - terminal->width * average_width) / 2;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001064 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001065
Callum Lowcay30eeae52011-01-07 19:46:55 +00001066 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001067 cairo_translate(cr, allocation.x + side_margin,
1068 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001069 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001070 for (row = 0; row < terminal->height; row++) {
Peng Wucfcc1112013-08-19 10:59:21 +08001071 p_row = terminal_get_row(terminal, row);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001072 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001073 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -05001074 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001075
Callum Lowcay9d708b02011-01-12 20:06:17 +13001076 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -05001077 continue;
1078
Peng Wucfcc1112013-08-19 10:59:21 +08001079 if (is_wide(p_row[col]))
1080 unichar_width = 2 * average_width;
1081 else
1082 unichar_width = average_width;
1083
Callum Lowcay9d708b02011-01-12 20:06:17 +13001084 terminal_set_color(terminal, cr, attr.attr.bg);
Peng Wuf291f202013-06-06 15:32:41 +08001085 cairo_move_to(cr, col * average_width,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001086 row * extents.height);
Peng Wucfcc1112013-08-19 10:59:21 +08001087 cairo_rel_line_to(cr, unichar_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001088 cairo_rel_line_to(cr, 0, extents.height);
Peng Wucfcc1112013-08-19 10:59:21 +08001089 cairo_rel_line_to(cr, -unichar_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001090 cairo_close_path(cr);
1091 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001092 }
1093 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001094
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001095 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1096
1097 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001098 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001099 for (row = 0; row < terminal->height; row++) {
1100 p_row = terminal_get_row(terminal, row);
1101 for (col = 0; col < terminal->width; col++) {
1102 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -05001103 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001104
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001105 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001106
Peng Wuf291f202013-06-06 15:32:41 +08001107 text_x = col * average_width;
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001108 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +13001109 if (attr.attr.a & ATTRMASK_UNDERLINE) {
1110 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +00001111 cairo_move_to(cr, text_x, (double)text_y + 1.5);
Peng Wuf291f202013-06-06 15:32:41 +08001112 cairo_line_to(cr, text_x + average_width, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001113 cairo_stroke(cr);
1114 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -05001115
Daiki Ueno56d8a7a2014-04-08 18:46:18 +09001116 /* skip space glyph (RLE) we use as a placeholder of
1117 the right half of a double-width character,
1118 because RLE is not available in every font. */
1119 if (p_row[col].ch == 0x200B)
1120 continue;
1121
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001122 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001123 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001124 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001125
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001126 attr.key = ~0;
1127 glyph_run_flush(&run, attr);
1128
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001129 if ((terminal->mode & MODE_SHOW_CURSOR) &&
1130 !window_has_focus(terminal->window)) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001131 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001132
Callum Lowcay30eeae52011-01-07 19:46:55 +00001133 cairo_set_line_width(cr, 1);
Peng Wuf291f202013-06-06 15:32:41 +08001134 cairo_move_to(cr, terminal->column * average_width + d,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001135 terminal->row * extents.height + d);
Peng Wuf291f202013-06-06 15:32:41 +08001136 cairo_rel_line_to(cr, average_width - 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001137 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
Peng Wuf291f202013-06-06 15:32:41 +08001138 cairo_rel_line_to(cr, -average_width + 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001139 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001140
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001141 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001142 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001143
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001144 cairo_pop_group_to_source(cr);
1145 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001146 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001147 cairo_surface_destroy(surface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001148
1149 if (terminal->send_cursor_position) {
1150 cursor_x = side_margin + allocation.x +
Peng Wuf291f202013-06-06 15:32:41 +08001151 terminal->column * average_width;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001152 cursor_y = top_margin + allocation.y +
1153 terminal->row * extents.height;
1154 window_set_text_cursor_position(terminal->window,
1155 cursor_x, cursor_y);
1156 terminal->send_cursor_position = 0;
1157 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001158}
1159
1160static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001161terminal_write(struct terminal *terminal, const char *data, size_t length)
1162{
1163 if (write(terminal->master, data, length) < 0)
1164 abort();
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001165 terminal->send_cursor_position = 1;
Kristian Høgsberg26130862011-08-24 11:30:21 -04001166}
1167
1168static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001169terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001170
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001171static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001172handle_char(struct terminal *terminal, union utf8_char utf8);
1173
1174static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001175handle_sgr(struct terminal *terminal, int code);
1176
1177static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001178handle_term_parameter(struct terminal *terminal, int code, int sr)
1179{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001180 int i;
1181
Callum Lowcay67a201d2011-01-12 19:23:41 +13001182 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001183 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001184 case 1: /* DECCKM */
1185 if (sr) terminal->key_mode = KM_APPLICATION;
1186 else terminal->key_mode = KM_NORMAL;
1187 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001188 case 2: /* DECANM */
1189 /* No VT52 support yet */
1190 terminal->g0 = CS_US;
1191 terminal->g1 = CS_US;
1192 terminal->cs = terminal->g0;
1193 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001194 case 3: /* DECCOLM */
1195 if (sr)
1196 terminal_resize(terminal, 132, 24);
1197 else
1198 terminal_resize(terminal, 80, 24);
Michael Vetter2a18a522015-05-15 17:17:47 +02001199
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001200 /* set columns, but also home cursor and clear screen */
1201 terminal->row = 0; terminal->column = 0;
1202 for (i = 0; i < terminal->height; i++) {
1203 memset(terminal_get_row(terminal, i),
1204 0, terminal->data_pitch);
1205 attr_init(terminal_get_attr_row(terminal, i),
1206 terminal->curr_attr, terminal->width);
1207 }
1208 break;
1209 case 5: /* DECSCNM */
1210 if (sr) terminal->mode |= MODE_INVERSE;
1211 else terminal->mode &= ~MODE_INVERSE;
1212 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001213 case 6: /* DECOM */
1214 terminal->origin_mode = sr;
1215 if (terminal->origin_mode)
1216 terminal->row = terminal->margin_top;
1217 else
1218 terminal->row = 0;
1219 terminal->column = 0;
1220 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001221 case 7: /* DECAWM */
1222 if (sr) terminal->mode |= MODE_AUTOWRAP;
1223 else terminal->mode &= ~MODE_AUTOWRAP;
1224 break;
1225 case 8: /* DECARM */
1226 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1227 else terminal->mode &= ~MODE_AUTOREPEAT;
1228 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001229 case 12: /* Very visible cursor (CVVIS) */
1230 /* FIXME: What do we do here. */
1231 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001232 case 25:
1233 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1234 else terminal->mode &= ~MODE_SHOW_CURSOR;
1235 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001236 case 1034: /* smm/rmm, meta mode on/off */
1237 /* ignore */
1238 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001239 case 1037: /* deleteSendsDel */
1240 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1241 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1242 break;
1243 case 1039: /* altSendsEscape */
1244 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1245 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1246 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001247 case 1049: /* rmcup/smcup, alternate screen */
1248 /* Ignore. Should be possible to implement,
1249 * but it's kind of annoying. */
1250 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001251 default:
1252 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1253 break;
1254 }
1255 } else {
1256 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001257 case 4: /* IRM */
1258 if (sr) terminal->mode |= MODE_IRM;
1259 else terminal->mode &= ~MODE_IRM;
1260 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001261 case 20: /* LNM */
1262 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1263 else terminal->mode &= ~MODE_LF_NEWLINE;
1264 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001265 default:
1266 fprintf(stderr, "Unknown parameter: %d\n", code);
1267 break;
1268 }
1269 }
1270}
1271
1272static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001273handle_dcs(struct terminal *terminal)
1274{
1275}
1276
1277static void
1278handle_osc(struct terminal *terminal)
1279{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001280 char *p;
1281 int code;
1282
1283 terminal->escape[terminal->escape_length++] = '\0';
1284 p = &terminal->escape[2];
1285 code = strtol(p, &p, 10);
1286 if (*p == ';') p++;
1287
1288 switch (code) {
1289 case 0: /* Icon name and window title */
1290 case 1: /* Icon label */
1291 case 2: /* Window title*/
Kristian Høgsberga83be202013-10-23 20:47:35 -07001292 free(terminal->title);
1293 terminal->title = strdup(p);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001294 window_set_title(terminal->window, p);
1295 break;
Kristian Høgsberg8a205002013-09-11 11:52:56 -07001296 case 7: /* shell cwd as uri */
1297 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001298 default:
Kristian Høgsberg8a205002013-09-11 11:52:56 -07001299 fprintf(stderr, "Unknown OSC escape code %d, text %s\n",
1300 code, p);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001301 break;
1302 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001303}
1304
1305static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001306handle_escape(struct terminal *terminal)
1307{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001308 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001309 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001310 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001311 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001312 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001313 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001314 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001315
1316 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001317 i = 0;
1318 p = &terminal->escape[2];
1319 while ((isdigit(*p) || *p == ';') && i < 10) {
1320 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001321 if (!set[i]) {
1322 args[i] = 0;
1323 set[i] = 1;
1324 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001325 p++;
1326 i++;
1327 } else {
1328 args[i] = strtol(p, &p, 10);
1329 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001330 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001331 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001332
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001333 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001334 case '@': /* ICH */
1335 count = set[0] ? args[0] : 1;
1336 if (count == 0) count = 1;
1337 terminal_shift_line(terminal, count);
1338 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001339 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001340 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001341 if (count == 0) count = 1;
1342 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001343 terminal->row -= count;
1344 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001345 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001346 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001347 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001348 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001349 if (count == 0) count = 1;
1350 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001351 terminal->row += count;
1352 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001353 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001354 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001355 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001356 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001357 if (count == 0) count = 1;
1358 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001359 terminal->column += count;
1360 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001361 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001362 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001363 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001364 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001365 if (count == 0) count = 1;
1366 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001367 terminal->column -= count;
1368 else
1369 terminal->column = 0;
1370 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001371 case 'E': /* CNL */
1372 count = set[0] ? args[0] : 1;
1373 if (terminal->row + count <= terminal->margin_bottom)
1374 terminal->row += count;
1375 else
1376 terminal->row = terminal->margin_bottom;
1377 terminal->column = 0;
1378 break;
1379 case 'F': /* CPL */
1380 count = set[0] ? args[0] : 1;
1381 if (terminal->row - count >= terminal->margin_top)
1382 terminal->row -= count;
1383 else
1384 terminal->row = terminal->margin_top;
1385 terminal->column = 0;
1386 break;
1387 case 'G': /* CHA */
1388 y = set[0] ? args[0] : 1;
1389 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
Michael Vetter2a18a522015-05-15 17:17:47 +02001390
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001391 terminal->column = y - 1;
1392 break;
1393 case 'f': /* HVP */
1394 case 'H': /* CUP */
1395 x = (set[1] ? args[1] : 1) - 1;
1396 x = x < 0 ? 0 :
1397 (x >= terminal->width ? terminal->width - 1 : x);
Michael Vetter2a18a522015-05-15 17:17:47 +02001398
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001399 y = (set[0] ? args[0] : 1) - 1;
1400 if (terminal->origin_mode) {
1401 y += terminal->margin_top;
1402 y = y < terminal->margin_top ? terminal->margin_top :
1403 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1404 } else {
1405 y = y < 0 ? 0 :
1406 (y >= terminal->height ? terminal->height - 1 : y);
1407 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001408
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001409 terminal->row = y;
1410 terminal->column = x;
1411 break;
1412 case 'I': /* CHT */
1413 count = set[0] ? args[0] : 1;
1414 if (count == 0) count = 1;
1415 while (count > 0 && terminal->column < terminal->width) {
1416 if (terminal->tab_ruler[terminal->column]) count--;
1417 terminal->column++;
1418 }
1419 terminal->column--;
1420 break;
1421 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001422 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001423 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001424 if (!set[0] || args[0] == 0 || args[0] > 2) {
1425 memset(&row[terminal->column],
1426 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1427 attr_init(&attr_row[terminal->column],
1428 terminal->curr_attr, terminal->width - terminal->column);
1429 for (i = terminal->row + 1; i < terminal->height; i++) {
1430 memset(terminal_get_row(terminal, i),
1431 0, terminal->data_pitch);
1432 attr_init(terminal_get_attr_row(terminal, i),
1433 terminal->curr_attr, terminal->width);
1434 }
1435 } else if (args[0] == 1) {
1436 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1437 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1438 for (i = 0; i < terminal->row; i++) {
1439 memset(terminal_get_row(terminal, i),
1440 0, terminal->data_pitch);
1441 attr_init(terminal_get_attr_row(terminal, i),
1442 terminal->curr_attr, terminal->width);
1443 }
1444 } else if (args[0] == 2) {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07001445 /* Clear screen by scrolling contents out */
1446 terminal_scroll_buffer(terminal,
1447 terminal->end - terminal->start);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001448 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001449 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001450 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001451 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001452 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001453 if (!set[0] || args[0] == 0 || args[0] > 2) {
1454 memset(&row[terminal->column], 0,
1455 (terminal->width - terminal->column) * sizeof(union utf8_char));
1456 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1457 terminal->width - terminal->column);
1458 } else if (args[0] == 1) {
1459 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1460 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1461 } else if (args[0] == 2) {
1462 memset(row, 0, terminal->data_pitch);
1463 attr_init(attr_row, terminal->curr_attr, terminal->width);
1464 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001465 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001466 case 'L': /* IL */
1467 count = set[0] ? args[0] : 1;
1468 if (count == 0) count = 1;
1469 if (terminal->row >= terminal->margin_top &&
1470 terminal->row < terminal->margin_bottom)
1471 {
1472 top = terminal->margin_top;
1473 terminal->margin_top = terminal->row;
1474 terminal_scroll(terminal, 0 - count);
1475 terminal->margin_top = top;
1476 } else if (terminal->row == terminal->margin_bottom) {
1477 memset(terminal_get_row(terminal, terminal->row),
1478 0, terminal->data_pitch);
1479 attr_init(terminal_get_attr_row(terminal, terminal->row),
1480 terminal->curr_attr, terminal->width);
1481 }
1482 break;
1483 case 'M': /* DL */
1484 count = set[0] ? args[0] : 1;
1485 if (count == 0) count = 1;
1486 if (terminal->row >= terminal->margin_top &&
1487 terminal->row < terminal->margin_bottom)
1488 {
1489 top = terminal->margin_top;
1490 terminal->margin_top = terminal->row;
1491 terminal_scroll(terminal, count);
1492 terminal->margin_top = top;
1493 } else if (terminal->row == terminal->margin_bottom) {
1494 memset(terminal_get_row(terminal, terminal->row),
1495 0, terminal->data_pitch);
1496 }
1497 break;
1498 case 'P': /* DCH */
1499 count = set[0] ? args[0] : 1;
1500 if (count == 0) count = 1;
1501 terminal_shift_line(terminal, 0 - count);
1502 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001503 case 'S': /* SU */
1504 terminal_scroll(terminal, set[0] ? args[0] : 1);
1505 break;
1506 case 'T': /* SD */
1507 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1508 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001509 case 'X': /* ECH */
1510 count = set[0] ? args[0] : 1;
1511 if (count == 0) count = 1;
1512 if ((terminal->column + count) > terminal->width)
1513 count = terminal->width - terminal->column;
1514 row = terminal_get_row(terminal, terminal->row);
1515 attr_row = terminal_get_attr_row(terminal, terminal->row);
1516 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1517 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1518 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001519 case 'Z': /* CBT */
1520 count = set[0] ? args[0] : 1;
1521 if (count == 0) count = 1;
1522 while (count > 0 && terminal->column >= 0) {
1523 if (terminal->tab_ruler[terminal->column]) count--;
1524 terminal->column--;
1525 }
1526 terminal->column++;
1527 break;
1528 case '`': /* HPA */
1529 y = set[0] ? args[0] : 1;
1530 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
Michael Vetter2a18a522015-05-15 17:17:47 +02001531
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001532 terminal->column = y - 1;
1533 break;
1534 case 'b': /* REP */
1535 count = set[0] ? args[0] : 1;
1536 if (count == 0) count = 1;
1537 if (terminal->last_char.byte[0])
1538 for (i = 0; i < count; i++)
1539 handle_char(terminal, terminal->last_char);
1540 terminal->last_char.byte[0] = 0;
1541 break;
1542 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001543 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001544 break;
1545 case 'd': /* VPA */
1546 x = set[0] ? args[0] : 1;
1547 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
Michael Vetter2a18a522015-05-15 17:17:47 +02001548
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001549 terminal->row = x - 1;
1550 break;
1551 case 'g': /* TBC */
1552 if (!set[0] || args[0] == 0) {
1553 terminal->tab_ruler[terminal->column] = 0;
1554 } else if (args[0] == 3) {
1555 memset(terminal->tab_ruler, 0, terminal->width);
1556 }
1557 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001558 case 'h': /* SM */
1559 for(i = 0; i < 10 && set[i]; i++) {
1560 handle_term_parameter(terminal, args[i], 1);
1561 }
1562 break;
1563 case 'l': /* RM */
1564 for(i = 0; i < 10 && set[i]; i++) {
1565 handle_term_parameter(terminal, args[i], 0);
1566 }
1567 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001568 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001569 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001570 if (i <= 7 && set[i] && set[i + 1] &&
1571 set[i + 2] && args[i + 1] == 5)
1572 {
1573 if (args[i] == 38) {
1574 handle_sgr(terminal, args[i + 2] + 256);
1575 break;
1576 } else if (args[i] == 48) {
1577 handle_sgr(terminal, args[i + 2] + 512);
1578 break;
1579 }
1580 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001581 if(set[i]) {
1582 handle_sgr(terminal, args[i]);
1583 } else if(i == 0) {
1584 handle_sgr(terminal, 0);
1585 break;
1586 } else {
1587 break;
1588 }
1589 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001590 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001591 case 'n': /* DSR */
1592 i = set[0] ? args[0] : 0;
1593 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001594 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001595 } else if (i == 6) {
1596 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1597 terminal->origin_mode ?
1598 terminal->row+terminal->margin_top : terminal->row+1,
1599 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001600 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001601 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001602 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001603 case 'r':
1604 if(!set[0]) {
1605 terminal->margin_top = 0;
1606 terminal->margin_bottom = terminal->height-1;
1607 terminal->row = 0;
1608 terminal->column = 0;
1609 } else {
1610 top = (set[0] ? args[0] : 1) - 1;
1611 top = top < 0 ? 0 :
1612 (top >= terminal->height ? terminal->height - 1 : top);
1613 bottom = (set[1] ? args[1] : 1) - 1;
1614 bottom = bottom < 0 ? 0 :
1615 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1616 if(bottom > top) {
1617 terminal->margin_top = top;
1618 terminal->margin_bottom = bottom;
1619 } else {
1620 terminal->margin_top = 0;
1621 terminal->margin_bottom = terminal->height-1;
1622 }
1623 if(terminal->origin_mode)
1624 terminal->row = terminal->margin_top;
1625 else
1626 terminal->row = 0;
1627 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001628 }
1629 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001630 case 's':
1631 terminal->saved_row = terminal->row;
1632 terminal->saved_column = terminal->column;
1633 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001634 case 't': /* windowOps */
1635 if (!set[0]) break;
1636 switch (args[0]) {
1637 case 4: /* resize px */
1638 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001639 widget_schedule_resize(terminal->widget,
1640 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001641 }
1642 break;
1643 case 8: /* resize ch */
1644 if (set[1] && set[2]) {
1645 terminal_resize(terminal, args[2], args[1]);
1646 }
1647 break;
1648 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001649 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001650 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1651 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001652 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001653 break;
1654 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001655 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001656 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1657 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001658 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001659 break;
1660 case 18: /* report ch */
1661 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1662 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001663 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001664 break;
1665 case 21: /* report title */
1666 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1667 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001668 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001669 break;
1670 default:
1671 if (args[0] >= 24)
1672 terminal_resize(terminal, terminal->width, args[0]);
1673 else
1674 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1675 break;
1676 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001677 case 'u':
1678 terminal->row = terminal->saved_row;
1679 terminal->column = terminal->saved_column;
1680 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001681 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001682 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001683 break;
Michael Vetter2a18a522015-05-15 17:17:47 +02001684 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001685}
1686
1687static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001688handle_non_csi_escape(struct terminal *terminal, char code)
1689{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001690 switch(code) {
1691 case 'M': /* RI */
1692 terminal->row -= 1;
1693 if(terminal->row < terminal->margin_top) {
1694 terminal->row = terminal->margin_top;
1695 terminal_scroll(terminal, -1);
1696 }
1697 break;
1698 case 'E': /* NEL */
1699 terminal->column = 0;
1700 // fallthrough
1701 case 'D': /* IND */
1702 terminal->row += 1;
1703 if(terminal->row > terminal->margin_bottom) {
1704 terminal->row = terminal->margin_bottom;
1705 terminal_scroll(terminal, +1);
1706 }
1707 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001708 case 'c': /* RIS */
1709 terminal_init(terminal);
1710 break;
1711 case 'H': /* HTS */
1712 terminal->tab_ruler[terminal->column] = 1;
1713 break;
1714 case '7': /* DECSC */
1715 terminal->saved_row = terminal->row;
1716 terminal->saved_column = terminal->column;
1717 terminal->saved_attr = terminal->curr_attr;
1718 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001719 terminal->saved_cs = terminal->cs;
1720 terminal->saved_g0 = terminal->g0;
1721 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001722 break;
1723 case '8': /* DECRC */
1724 terminal->row = terminal->saved_row;
1725 terminal->column = terminal->saved_column;
1726 terminal->curr_attr = terminal->saved_attr;
1727 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001728 terminal->cs = terminal->saved_cs;
1729 terminal->g0 = terminal->saved_g0;
1730 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001731 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001732 case '=': /* DECPAM */
1733 terminal->key_mode = KM_APPLICATION;
1734 break;
1735 case '>': /* DECPNM */
1736 terminal->key_mode = KM_NORMAL;
1737 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001738 default:
1739 fprintf(stderr, "Unknown escape code: %c\n", code);
1740 break;
1741 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001742}
1743
1744static void
1745handle_special_escape(struct terminal *terminal, char special, char code)
1746{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001747 int i, numChars;
1748
1749 if (special == '#') {
1750 switch(code) {
1751 case '8':
1752 /* fill with 'E', no cheap way to do this */
1753 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1754 numChars = terminal->width * terminal->height;
1755 for(i = 0; i < numChars; i++) {
1756 terminal->data[i].byte[0] = 'E';
1757 }
1758 break;
1759 default:
1760 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1761 break;
1762 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001763 } else if (special == '(' || special == ')') {
1764 switch(code) {
1765 case '0':
1766 if (special == '(')
1767 terminal->g0 = CS_SPECIAL;
1768 else
1769 terminal->g1 = CS_SPECIAL;
1770 break;
1771 case 'A':
1772 if (special == '(')
1773 terminal->g0 = CS_UK;
1774 else
1775 terminal->g1 = CS_UK;
1776 break;
1777 case 'B':
1778 if (special == '(')
1779 terminal->g0 = CS_US;
1780 else
1781 terminal->g1 = CS_US;
1782 break;
1783 default:
1784 fprintf(stderr, "Unknown character set %c\n", code);
1785 break;
1786 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001787 } else {
1788 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1789 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001790}
1791
1792static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001793handle_sgr(struct terminal *terminal, int code)
1794{
1795 switch(code) {
1796 case 0:
1797 terminal->curr_attr = terminal->color_scheme->default_attr;
1798 break;
1799 case 1:
1800 terminal->curr_attr.a |= ATTRMASK_BOLD;
1801 if (terminal->curr_attr.fg < 8)
1802 terminal->curr_attr.fg += 8;
1803 break;
1804 case 4:
1805 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1806 break;
1807 case 5:
1808 terminal->curr_attr.a |= ATTRMASK_BLINK;
1809 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001810 case 8:
1811 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1812 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001813 case 2:
1814 case 21:
1815 case 22:
1816 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1817 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1818 terminal->curr_attr.fg -= 8;
1819 break;
1820 case 24:
1821 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1822 break;
1823 case 25:
1824 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1825 break;
1826 case 7:
1827 case 26:
1828 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1829 break;
1830 case 27:
1831 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1832 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001833 case 28:
1834 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1835 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001836 case 39:
1837 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1838 break;
1839 case 49:
1840 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1841 break;
1842 default:
1843 if(code >= 30 && code <= 37) {
1844 terminal->curr_attr.fg = code - 30;
1845 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1846 terminal->curr_attr.fg += 8;
1847 } else if(code >= 40 && code <= 47) {
1848 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001849 } else if (code >= 90 && code <= 97) {
1850 terminal->curr_attr.fg = code - 90 + 8;
1851 } else if (code >= 100 && code <= 107) {
1852 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001853 } else if(code >= 256 && code < 512) {
1854 terminal->curr_attr.fg = code - 256;
1855 } else if(code >= 512 && code < 768) {
1856 terminal->curr_attr.bg = code - 512;
1857 } else {
1858 fprintf(stderr, "Unknown SGR code: %d\n", code);
1859 }
1860 break;
1861 }
1862}
1863
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001864/* Returns 1 if c was special, otherwise 0 */
1865static int
1866handle_special_char(struct terminal *terminal, char c)
1867{
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001868 union utf8_char *row;
1869 struct attr *attr_row;
1870
1871 row = terminal_get_row(terminal, terminal->row);
1872 attr_row = terminal_get_attr_row(terminal, terminal->row);
1873
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001874 switch(c) {
1875 case '\r':
1876 terminal->column = 0;
1877 break;
1878 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001879 if (terminal->mode & MODE_LF_NEWLINE) {
1880 terminal->column = 0;
1881 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001882 /* fallthrough */
1883 case '\v':
1884 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001885 terminal->row++;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07001886 if (terminal->row > terminal->margin_bottom) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001887 terminal->row = terminal->margin_bottom;
1888 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001889 }
1890
1891 break;
1892 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001893 while (terminal->column < terminal->width) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001894 if (terminal->mode & MODE_IRM)
1895 terminal_shift_line(terminal, +1);
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001896
1897 if (row[terminal->column].byte[0] == '\0') {
1898 row[terminal->column].byte[0] = ' ';
1899 row[terminal->column].byte[1] = '\0';
1900 attr_row[terminal->column] = terminal->curr_attr;
1901 }
1902
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001903 terminal->column++;
Kristian Høgsbergcca3c2f2012-06-20 15:56:13 -04001904 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001905 }
1906 if (terminal->column >= terminal->width) {
1907 terminal->column = terminal->width - 1;
1908 }
1909
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001910 break;
1911 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001912 if (terminal->column >= terminal->width) {
1913 terminal->column = terminal->width - 2;
1914 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001915 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001916 } else if (terminal->mode & MODE_AUTOWRAP) {
1917 terminal->column = terminal->width - 1;
1918 terminal->row -= 1;
1919 if (terminal->row < terminal->margin_top) {
1920 terminal->row = terminal->margin_top;
1921 terminal_scroll(terminal, -1);
1922 }
1923 }
1924
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001925 break;
1926 case '\a':
1927 /* Bell */
1928 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001929 case '\x0E': /* SO */
1930 terminal->cs = terminal->g1;
1931 break;
1932 case '\x0F': /* SI */
1933 terminal->cs = terminal->g0;
1934 break;
Kristian Høgsberg2a1aa4e2012-08-03 09:37:05 -04001935 case '\0':
1936 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001937 default:
1938 return 0;
1939 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001940
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001941 return 1;
1942}
1943
1944static void
1945handle_char(struct terminal *terminal, union utf8_char utf8)
1946{
1947 union utf8_char *row;
1948 struct attr *attr_row;
Michael Vetter2a18a522015-05-15 17:17:47 +02001949
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001950 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001951
1952 apply_char_set(terminal->cs, &utf8);
Michael Vetter2a18a522015-05-15 17:17:47 +02001953
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001954 /* There are a whole lot of non-characters, control codes,
1955 * and formatting codes that should probably be ignored,
1956 * for example: */
1957 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1958 /* BOM, ignore */
1959 return;
Michael Vetter2a18a522015-05-15 17:17:47 +02001960 }
1961
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001962 /* Some of these non-characters should be translated, e.g.: */
1963 if (utf8.byte[0] < 32) {
1964 utf8.byte[0] = utf8.byte[0] + 64;
1965 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001966
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001967 /* handle right margin effects */
1968 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001969 if (terminal->mode & MODE_AUTOWRAP) {
1970 terminal->column = 0;
1971 terminal->row += 1;
1972 if (terminal->row > terminal->margin_bottom) {
1973 terminal->row = terminal->margin_bottom;
1974 terminal_scroll(terminal, +1);
1975 }
1976 } else {
1977 terminal->column--;
1978 }
1979 }
Michael Vetter2a18a522015-05-15 17:17:47 +02001980
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001981 row = terminal_get_row(terminal, terminal->row);
1982 attr_row = terminal_get_attr_row(terminal, terminal->row);
Michael Vetter2a18a522015-05-15 17:17:47 +02001983
Callum Lowcay69e96582011-01-07 19:47:00 +00001984 if (terminal->mode & MODE_IRM)
1985 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001986 row[terminal->column] = utf8;
1987 attr_row[terminal->column++] = terminal->curr_attr;
1988
Kristian Høgsberg1d781ee2013-11-24 16:54:12 -08001989 if (terminal->row + terminal->start + 1 > terminal->end)
1990 terminal->end = terminal->row + terminal->start + 1;
1991 if (terminal->end == terminal->buffer_height)
1992 terminal->log_size = terminal->buffer_height;
1993 else if (terminal->log_size < terminal->buffer_height)
1994 terminal->log_size = terminal->end;
1995
Peng Wucfcc1112013-08-19 10:59:21 +08001996 /* cursor jump for wide character. */
1997 if (is_wide(utf8))
1998 row[terminal->column++].ch = 0x200B; /* space glyph */
1999
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002000 if (utf8.ch != terminal->last_char.ch)
2001 terminal->last_char = utf8;
2002}
2003
Callum Lowcay30eeae52011-01-07 19:46:55 +00002004static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13002005escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
2006{
2007 int len, i;
2008
2009 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
2010 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
2011 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
2012 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
2013 else len = 1; /* Invalid, cannot happen */
2014
2015 if (terminal->escape_length + len <= MAX_ESCAPE) {
2016 for (i = 0; i < len; i++)
2017 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
2018 terminal->escape_length += len;
2019 } else if (terminal->escape_length < MAX_ESCAPE) {
2020 terminal->escape[terminal->escape_length++] = 0;
2021 }
2022}
2023
2024static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002025terminal_data(struct terminal *terminal, const char *data, size_t length)
2026{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002027 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002028 union utf8_char utf8;
2029 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002030
2031 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002032 parser_state =
2033 utf8_next_char(&terminal->state_machine, data[i]);
2034 switch(parser_state) {
2035 case utf8state_accept:
2036 utf8.ch = terminal->state_machine.s.ch;
2037 break;
2038 case utf8state_reject:
2039 /* the unicode replacement character */
2040 utf8.byte[0] = 0xEF;
2041 utf8.byte[1] = 0xBF;
2042 utf8.byte[2] = 0xBD;
2043 utf8.byte[3] = 0x00;
2044 break;
2045 default:
2046 continue;
2047 }
2048
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002049 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13002050 switch (terminal->state) {
2051 case escape_state_escape:
2052 escape_append_utf8(terminal, utf8);
2053 switch (utf8.byte[0]) {
2054 case 'P': /* DCS */
2055 terminal->state = escape_state_dcs;
2056 break;
2057 case '[': /* CSI */
2058 terminal->state = escape_state_csi;
2059 break;
2060 case ']': /* OSC */
2061 terminal->state = escape_state_osc;
2062 break;
2063 case '#':
2064 case '(':
2065 case ')': /* special */
2066 terminal->state = escape_state_special;
2067 break;
2068 case '^': /* PM (not implemented) */
2069 case '_': /* APC (not implemented) */
2070 terminal->state = escape_state_ignore;
2071 break;
2072 default:
2073 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002074 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13002075 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002076 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002077 continue;
2078 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002079 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
2080 /* do nothing */
2081 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002082 terminal->escape_flags |= ESC_FLAG_WHAT;
2083 } else if (utf8.byte[0] == '>') {
2084 terminal->escape_flags |= ESC_FLAG_GT;
2085 } else if (utf8.byte[0] == '!') {
2086 terminal->escape_flags |= ESC_FLAG_BANG;
2087 } else if (utf8.byte[0] == '$') {
2088 terminal->escape_flags |= ESC_FLAG_CASH;
2089 } else if (utf8.byte[0] == '\'') {
2090 terminal->escape_flags |= ESC_FLAG_SQUOTE;
2091 } else if (utf8.byte[0] == '"') {
2092 terminal->escape_flags |= ESC_FLAG_DQUOTE;
2093 } else if (utf8.byte[0] == ' ') {
2094 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002095 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002096 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002097 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13002098 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002099 }
Michael Vetter2a18a522015-05-15 17:17:47 +02002100
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002101 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
2102 utf8.byte[0] == '`')
2103 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002104 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002105 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002106 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002107 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002108 continue;
2109 case escape_state_inner_escape:
2110 if (utf8.byte[0] == '\\') {
2111 terminal->state = escape_state_normal;
2112 if (terminal->outer_state == escape_state_dcs) {
2113 handle_dcs(terminal);
2114 } else if (terminal->outer_state == escape_state_osc) {
2115 handle_osc(terminal);
2116 }
2117 } else if (utf8.byte[0] == '\e') {
2118 terminal->state = terminal->outer_state;
2119 escape_append_utf8(terminal, utf8);
2120 if (terminal->escape_length >= MAX_ESCAPE)
2121 terminal->state = escape_state_normal;
2122 } else {
2123 terminal->state = terminal->outer_state;
2124 if (terminal->escape_length < MAX_ESCAPE)
2125 terminal->escape[terminal->escape_length++] = '\e';
2126 escape_append_utf8(terminal, utf8);
2127 if (terminal->escape_length >= MAX_ESCAPE)
2128 terminal->state = escape_state_normal;
2129 }
2130 continue;
2131 case escape_state_dcs:
2132 case escape_state_osc:
2133 case escape_state_ignore:
2134 if (utf8.byte[0] == '\e') {
2135 terminal->outer_state = terminal->state;
2136 terminal->state = escape_state_inner_escape;
2137 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
2138 terminal->state = escape_state_normal;
2139 handle_osc(terminal);
2140 } else {
2141 escape_append_utf8(terminal, utf8);
2142 if (terminal->escape_length >= MAX_ESCAPE)
2143 terminal->state = escape_state_normal;
2144 }
2145 continue;
2146 case escape_state_special:
2147 escape_append_utf8(terminal, utf8);
2148 terminal->state = escape_state_normal;
2149 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2150 handle_special_escape(terminal, terminal->escape[1],
2151 utf8.byte[0]);
2152 }
2153 continue;
2154 default:
2155 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002156 }
2157
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002158 /* this is valid, because ASCII characters are never used to
2159 * introduce a multibyte sequence in UTF-8 */
2160 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002161 terminal->state = escape_state_escape;
2162 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002163 terminal->escape[0] = '\e';
2164 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002165 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002166 } else {
2167 handle_char(terminal, utf8);
2168 } /* if */
2169 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002170
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002171 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002172}
2173
2174static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002175data_source_target(void *data,
2176 struct wl_data_source *source, const char *mime_type)
2177{
2178 fprintf(stderr, "data_source_target, %s\n", mime_type);
2179}
2180
2181static void
2182data_source_send(void *data,
2183 struct wl_data_source *source,
2184 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002185{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002186 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002187
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002188 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002189}
2190
2191static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002192data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002193{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002194 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002195}
2196
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002197static const struct wl_data_source_listener data_source_listener = {
2198 data_source_target,
2199 data_source_send,
2200 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002201};
2202
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07002203static const char text_mime_type[] = "text/plain;charset=utf-8";
2204
2205static void
2206data_handler(struct window *window,
2207 struct input *input,
2208 float x, float y, const char **types, void *data)
2209{
2210 int i, has_text = 0;
2211
2212 if (!types)
2213 return;
2214 for (i = 0; types[i]; i++)
2215 if (strcmp(types[i], text_mime_type) == 0)
2216 has_text = 1;
2217
2218 if (!has_text) {
2219 input_accept(input, NULL);
2220 } else {
2221 input_accept(input, text_mime_type);
2222 }
2223}
2224
2225static void
2226drop_handler(struct window *window, struct input *input,
2227 int32_t x, int32_t y, void *data)
2228{
2229 struct terminal *terminal = data;
2230
2231 input_receive_drag_data_to_fd(input, text_mime_type, terminal->master);
2232}
2233
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002234static void
2235fullscreen_handler(struct window *window, void *data)
2236{
2237 struct terminal *terminal = data;
2238
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002239 window_set_fullscreen(window, !window_is_fullscreen(terminal->window));
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002240}
2241
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002242static void
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002243close_handler(void *data)
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002244{
2245 struct terminal *terminal = data;
2246
2247 terminal_destroy(terminal);
2248}
2249
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002250static void
2251terminal_copy(struct terminal *terminal, struct input *input)
2252{
2253 terminal->selection =
2254 display_create_data_source(terminal->display);
2255 wl_data_source_offer(terminal->selection,
2256 "text/plain;charset=utf-8");
2257 wl_data_source_add_listener(terminal->selection,
2258 &data_source_listener, terminal);
2259 input_set_selection(input, terminal->selection,
2260 display_get_serial(terminal->display));
2261}
2262
2263static void
2264terminal_paste(struct terminal *terminal, struct input *input)
2265{
2266 input_receive_selection_data_to_fd(input,
2267 "text/plain;charset=utf-8",
2268 terminal->master);
2269
2270}
2271
2272static void
2273terminal_new_instance(struct terminal *terminal)
2274{
2275 struct terminal *new_terminal;
2276
2277 new_terminal = terminal_create(terminal->display);
2278 if (terminal_run(new_terminal, option_shell))
2279 terminal_destroy(new_terminal);
2280}
2281
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002282static int
2283handle_bound_key(struct terminal *terminal,
2284 struct input *input, uint32_t sym, uint32_t time)
2285{
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002286 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002287 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002288 /* Cut selection; terminal doesn't do cut, fall
2289 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002290 case XKB_KEY_C:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002291 terminal_copy(terminal, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002292 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002293 case XKB_KEY_V:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002294 terminal_paste(terminal, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002295 return 1;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002296 case XKB_KEY_N:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002297 terminal_new_instance(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002298 return 1;
2299
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002300 case XKB_KEY_Up:
2301 if (!terminal->scrolling)
2302 terminal->saved_start = terminal->start;
2303 if (terminal->start == terminal->end - terminal->log_size)
2304 return 1;
2305
2306 terminal->scrolling = 1;
2307 terminal->start--;
2308 terminal->row++;
2309 terminal->selection_start_row++;
2310 terminal->selection_end_row++;
2311 widget_schedule_redraw(terminal->widget);
2312 return 1;
2313
2314 case XKB_KEY_Down:
2315 if (!terminal->scrolling)
2316 terminal->saved_start = terminal->start;
2317
2318 if (terminal->start == terminal->saved_start)
2319 return 1;
2320
2321 terminal->scrolling = 1;
2322 terminal->start++;
2323 terminal->row--;
2324 terminal->selection_start_row--;
2325 terminal->selection_end_row--;
2326 widget_schedule_redraw(terminal->widget);
2327 return 1;
2328
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002329 default:
2330 return 0;
2331 }
2332}
2333
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002334static void
2335key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002336 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2337 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002338{
2339 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002340 char ch[MAX_RESPONSE];
Kristian Høgsberg88fd4082012-06-21 15:55:03 -04002341 uint32_t modifiers, serial;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002342 int ret, len = 0, d;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002343 bool convert_utf8 = true;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002344
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002345 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002346 if ((modifiers & MOD_CONTROL_MASK) &&
2347 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002348 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2349 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002350 return;
2351
Daniel Stonea8468712012-11-07 17:51:35 +11002352 /* Map keypad symbols to 'normal' equivalents before processing */
2353 switch (sym) {
2354 case XKB_KEY_KP_Space:
2355 sym = XKB_KEY_space;
2356 break;
2357 case XKB_KEY_KP_Tab:
2358 sym = XKB_KEY_Tab;
2359 break;
2360 case XKB_KEY_KP_Enter:
2361 sym = XKB_KEY_Return;
2362 break;
2363 case XKB_KEY_KP_Left:
2364 sym = XKB_KEY_Left;
2365 break;
2366 case XKB_KEY_KP_Up:
2367 sym = XKB_KEY_Up;
2368 break;
2369 case XKB_KEY_KP_Right:
2370 sym = XKB_KEY_Right;
2371 break;
2372 case XKB_KEY_KP_Down:
2373 sym = XKB_KEY_Down;
2374 break;
2375 case XKB_KEY_KP_Equal:
2376 sym = XKB_KEY_equal;
2377 break;
2378 case XKB_KEY_KP_Multiply:
2379 sym = XKB_KEY_asterisk;
2380 break;
2381 case XKB_KEY_KP_Add:
2382 sym = XKB_KEY_plus;
2383 break;
2384 case XKB_KEY_KP_Separator:
2385 /* Note this is actually locale-dependent and should mostly be
2386 * a comma. But leave it as period until we one day start
2387 * doing the right thing. */
2388 sym = XKB_KEY_period;
2389 break;
2390 case XKB_KEY_KP_Subtract:
2391 sym = XKB_KEY_minus;
2392 break;
2393 case XKB_KEY_KP_Decimal:
2394 sym = XKB_KEY_period;
2395 break;
2396 case XKB_KEY_KP_Divide:
2397 sym = XKB_KEY_slash;
2398 break;
2399 case XKB_KEY_KP_0:
2400 case XKB_KEY_KP_1:
2401 case XKB_KEY_KP_2:
2402 case XKB_KEY_KP_3:
2403 case XKB_KEY_KP_4:
2404 case XKB_KEY_KP_5:
2405 case XKB_KEY_KP_6:
2406 case XKB_KEY_KP_7:
2407 case XKB_KEY_KP_8:
2408 case XKB_KEY_KP_9:
2409 sym = (sym - XKB_KEY_KP_0) + XKB_KEY_0;
2410 break;
2411 default:
2412 break;
2413 }
2414
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002415 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002416 case XKB_KEY_BackSpace:
Kristian Høgsbergb7f94bf2012-06-21 12:29:36 -04002417 if (modifiers & MOD_ALT_MASK)
2418 ch[len++] = 0x1b;
Kristian Høgsberg71a4cf42012-06-20 17:57:56 -04002419 ch[len++] = 0x7f;
2420 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002421 case XKB_KEY_Tab:
2422 case XKB_KEY_Linefeed:
2423 case XKB_KEY_Clear:
2424 case XKB_KEY_Pause:
2425 case XKB_KEY_Scroll_Lock:
2426 case XKB_KEY_Sys_Req:
2427 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002428 ch[len++] = sym & 0x7f;
2429 break;
2430
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002431 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002432 if (terminal->mode & MODE_LF_NEWLINE) {
2433 ch[len++] = 0x0D;
2434 ch[len++] = 0x0A;
2435 } else {
2436 ch[len++] = 0x0D;
2437 }
2438 break;
2439
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002440 case XKB_KEY_Shift_L:
2441 case XKB_KEY_Shift_R:
2442 case XKB_KEY_Control_L:
2443 case XKB_KEY_Control_R:
2444 case XKB_KEY_Alt_L:
2445 case XKB_KEY_Alt_R:
Kristian Høgsberg22fbcf72012-06-22 12:18:56 -04002446 case XKB_KEY_Meta_L:
2447 case XKB_KEY_Meta_R:
2448 case XKB_KEY_Super_L:
2449 case XKB_KEY_Super_R:
2450 case XKB_KEY_Hyper_L:
2451 case XKB_KEY_Hyper_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002452 break;
2453
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002454 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002455 len = function_key_response('[', 2, modifiers, '~', ch);
2456 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002457 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002458 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2459 ch[len++] = '\x04';
2460 } else {
2461 len = function_key_response('[', 3, modifiers, '~', ch);
2462 }
2463 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002464 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002465 len = function_key_response('[', 5, modifiers, '~', ch);
2466 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002467 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002468 len = function_key_response('[', 6, modifiers, '~', ch);
2469 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002470 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002471 len = function_key_response('O', 1, modifiers, 'P', ch);
2472 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002473 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002474 len = function_key_response('O', 1, modifiers, 'Q', ch);
2475 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002476 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002477 len = function_key_response('O', 1, modifiers, 'R', ch);
2478 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002479 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002480 len = function_key_response('O', 1, modifiers, 'S', ch);
2481 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002482 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002483 len = function_key_response('[', 15, modifiers, '~', ch);
2484 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002485 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002486 len = function_key_response('[', 17, modifiers, '~', ch);
2487 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002488 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002489 len = function_key_response('[', 18, modifiers, '~', ch);
2490 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002491 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002492 len = function_key_response('[', 19, modifiers, '~', ch);
2493 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002494 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002495 len = function_key_response('[', 20, modifiers, '~', ch);
2496 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002497 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002498 len = function_key_response('[', 21, modifiers, '~', ch);
2499 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002500 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002501 len = function_key_response('[', 24, modifiers, '~', ch);
2502 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002503 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002504 /* Handle special keys with alternate mappings */
2505 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2506 if (len != 0) break;
Michael Vetter2a18a522015-05-15 17:17:47 +02002507
Kristian Høgsberg70163132012-05-08 15:55:39 -04002508 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002509 if (sym >= '3' && sym <= '7')
2510 sym = (sym & 0x1f) + 8;
2511
2512 if (!((sym >= '!' && sym <= '/') ||
2513 (sym >= '8' && sym <= '?') ||
2514 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2515 else if (sym == '2') sym = 0x00;
2516 else if (sym == '/') sym = 0x1F;
2517 else if (sym == '8' || sym == '?') sym = 0x7F;
Kristian Høgsbergae9e0732012-06-21 12:30:15 -04002518 }
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002519 if (modifiers & MOD_ALT_MASK) {
2520 if (terminal->mode & MODE_ALT_SENDS_ESC) {
2521 ch[len++] = 0x1b;
2522 } else {
2523 sym = sym | 0x80;
2524 convert_utf8 = false;
2525 }
Callum Lowcay7e08e902011-01-07 19:47:02 +00002526 }
2527
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002528 if ((sym < 128) ||
2529 (!convert_utf8 && sym < 256)) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002530 ch[len++] = sym;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002531 } else {
2532 ret = xkb_keysym_to_utf8(sym, ch + len,
2533 MAX_RESPONSE - len);
2534 if (ret < 0)
2535 fprintf(stderr,
2536 "Warning: buffer too small to encode "
2537 "UTF8 character\n");
2538 else
2539 len += ret;
2540 }
2541
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002542 break;
2543 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002544
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002545 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002546 if (terminal->scrolling) {
2547 d = terminal->saved_start - terminal->start;
2548 terminal->row -= d;
2549 terminal->selection_start_row -= d;
2550 terminal->selection_end_row -= d;
2551 terminal->start = terminal->saved_start;
2552 terminal->scrolling = 0;
2553 widget_schedule_redraw(terminal->widget);
2554 }
2555
Kristian Høgsberg26130862011-08-24 11:30:21 -04002556 terminal_write(terminal, ch, len);
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002557
2558 /* Hide cursor, except if this was coming from a
2559 * repeating key press. */
2560 serial = display_get_serial(terminal->display);
2561 if (terminal->hide_cursor_serial != serial) {
2562 input_set_pointer_image(input, CURSOR_BLANK);
2563 terminal->hide_cursor_serial = serial;
2564 }
2565 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002566}
2567
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002568static void
2569keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002570 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002571{
2572 struct terminal *terminal = data;
2573
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002574 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002575}
2576
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002577static int wordsep(int ch)
2578{
2579 const char extra[] = "-,./?%&#:_=+@~";
2580
Andre Heider552d12b2012-08-02 20:59:43 +02002581 if (ch > 127)
2582 return 1;
2583
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002584 return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
2585}
2586
2587static int
2588recompute_selection(struct terminal *terminal)
2589{
2590 struct rectangle allocation;
2591 int col, x, width, height;
2592 int start_row, end_row;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002593 int word_start, eol;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002594 int side_margin, top_margin;
2595 int start_x, end_x;
2596 int cw, ch;
2597 union utf8_char *data;
2598
Peng Wuf291f202013-06-06 15:32:41 +08002599 cw = terminal->average_width;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002600 ch = terminal->extents.height;
2601 widget_get_allocation(terminal->widget, &allocation);
2602 width = terminal->width * cw;
2603 height = terminal->height * ch;
2604 side_margin = allocation.x + (allocation.width - width) / 2;
2605 top_margin = allocation.y + (allocation.height - height) / 2;
2606
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002607 start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1;
2608 end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1;
2609
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002610 if (start_row < end_row ||
2611 (start_row == end_row &&
2612 terminal->selection_start_x < terminal->selection_end_x)) {
2613 terminal->selection_start_row = start_row;
2614 terminal->selection_end_row = end_row;
2615 start_x = terminal->selection_start_x;
2616 end_x = terminal->selection_end_x;
2617 } else {
2618 terminal->selection_start_row = end_row;
2619 terminal->selection_end_row = start_row;
2620 start_x = terminal->selection_end_x;
2621 end_x = terminal->selection_start_x;
2622 }
2623
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002624 eol = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002625 if (terminal->selection_start_row < 0) {
2626 terminal->selection_start_row = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002627 terminal->selection_start_col = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002628 } else {
2629 x = side_margin + cw / 2;
2630 data = terminal_get_row(terminal,
2631 terminal->selection_start_row);
2632 word_start = 0;
2633 for (col = 0; col < terminal->width; col++, x += cw) {
2634 if (col == 0 || wordsep(data[col - 1].ch))
2635 word_start = col;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002636 if (data[col].ch != 0)
2637 eol = col + 1;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002638 if (start_x < x)
2639 break;
2640 }
2641
2642 switch (terminal->dragging) {
2643 case SELECT_LINE:
2644 terminal->selection_start_col = 0;
2645 break;
2646 case SELECT_WORD:
2647 terminal->selection_start_col = word_start;
2648 break;
2649 case SELECT_CHAR:
2650 terminal->selection_start_col = col;
2651 break;
2652 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002653 }
2654
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002655 if (terminal->selection_end_row >= terminal->height) {
2656 terminal->selection_end_row = terminal->height;
2657 terminal->selection_end_col = 0;
2658 } else {
2659 x = side_margin + cw / 2;
2660 data = terminal_get_row(terminal, terminal->selection_end_row);
2661 for (col = 0; col < terminal->width; col++, x += cw) {
2662 if (terminal->dragging == SELECT_CHAR && end_x < x)
2663 break;
2664 if (terminal->dragging == SELECT_WORD &&
2665 end_x < x && wordsep(data[col].ch))
2666 break;
2667 }
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002668 terminal->selection_end_col = col;
2669 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002670
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002671 if (terminal->selection_end_col != terminal->selection_start_col ||
2672 terminal->selection_start_row != terminal->selection_end_row) {
2673 col = terminal->selection_end_col;
2674 if (col > 0 && data[col - 1].ch == 0)
2675 terminal->selection_end_col = terminal->width;
2676 data = terminal_get_row(terminal, terminal->selection_start_row);
2677 if (data[terminal->selection_start_col].ch == 0)
2678 terminal->selection_start_col = eol;
2679 }
2680
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002681 return 1;
2682}
2683
Kristian Høgsberg59826582011-01-20 11:56:57 -05002684static void
Pekka Paalanen807f09c2015-03-27 15:48:54 +02002685terminal_minimize(struct terminal *terminal)
2686{
2687 window_set_minimized(terminal->window);
2688}
2689
2690static void
Jasper St. Pierredda93132014-03-13 12:06:00 -04002691menu_func(void *data, struct input *input, int index)
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002692{
Jasper St. Pierredda93132014-03-13 12:06:00 -04002693 struct window *window = data;
2694 struct terminal *terminal = window_get_user_data(window);
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002695
2696 fprintf(stderr, "picked entry %d\n", index);
2697
2698 switch (index) {
2699 case 0:
2700 terminal_new_instance(terminal);
2701 break;
2702 case 1:
2703 terminal_copy(terminal, input);
2704 break;
2705 case 2:
2706 terminal_paste(terminal, input);
2707 break;
Pekka Paalanen807f09c2015-03-27 15:48:54 +02002708 case 3:
2709 terminal_minimize(terminal);
2710 break;
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002711 }
2712}
2713
2714static void
2715show_menu(struct terminal *terminal, struct input *input, uint32_t time)
2716{
2717 int32_t x, y;
2718 static const char *entries[] = {
Pekka Paalanen807f09c2015-03-27 15:48:54 +02002719 "Open Terminal", "Copy", "Paste", "Minimize"
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002720 };
2721
2722 input_get_position(input, &x, &y);
2723 window_show_menu(terminal->display, input, time, terminal->window,
2724 x - 10, y - 10, menu_func,
2725 entries, ARRAY_LENGTH(entries));
2726}
2727
2728static void
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002729click_handler(struct widget *widget, struct terminal *terminal,
2730 struct input *input, int32_t x, int32_t y,
2731 uint32_t time)
2732{
2733 if (time - terminal->click_time < 500)
2734 terminal->click_count++;
2735 else
2736 terminal->click_count = 1;
2737
2738 terminal->click_time = time;
2739 terminal->dragging = (terminal->click_count - 1) % 3 + SELECT_CHAR;
2740
2741 terminal->selection_end_x = terminal->selection_start_x = x;
2742 terminal->selection_end_y = terminal->selection_start_y = y;
2743 if (recompute_selection(terminal))
2744 widget_schedule_redraw(widget);
2745}
2746
2747static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002748button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002749 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002750 uint32_t button,
2751 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002752{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002753 struct terminal *terminal = data;
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002754 int32_t x, y;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002755
2756 switch (button) {
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002757 case BTN_LEFT:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002758 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002759 input_get_position(input, &x, &y);
2760 click_handler(widget, terminal, input, x, y, time);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002761 } else {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002762 terminal->dragging = SELECT_NONE;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002763 }
2764 break;
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002765
2766 case BTN_RIGHT:
2767 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
2768 show_menu(terminal, input, time);
2769 break;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002770 }
2771}
2772
2773static int
Kristian Høgsberg29784402012-06-28 14:27:02 -04002774enter_handler(struct widget *widget,
2775 struct input *input, float x, float y, void *data)
2776{
2777 return CURSOR_IBEAM;
2778}
2779
2780static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002781motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002782 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002783 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002784{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002785 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002786
2787 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002788 input_get_position(input,
2789 &terminal->selection_end_x,
2790 &terminal->selection_end_y);
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002791
2792 if (recompute_selection(terminal))
2793 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002794 }
2795
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002796 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002797}
2798
Magnus Hoff1046f122014-08-05 15:05:59 +02002799/* This magnitude is chosen rather arbitrarily. Really, the scrolling
2800 * should happen on a (fractional) pixel basis, not a line basis. */
2801#define AXIS_UNITS_PER_LINE 256
2802
2803static void
2804axis_handler(struct widget *widget,
2805 struct input *input, uint32_t time,
2806 uint32_t axis,
2807 wl_fixed_t value,
2808 void *data)
2809{
2810 struct terminal *terminal = data;
2811 int lines;
2812
2813 if (axis != WL_POINTER_AXIS_VERTICAL_SCROLL)
2814 return;
2815
2816 terminal->smooth_scroll += value;
2817 lines = terminal->smooth_scroll / AXIS_UNITS_PER_LINE;
2818 terminal->smooth_scroll -= lines * AXIS_UNITS_PER_LINE;
2819
2820 if (lines > 0) {
2821 if (terminal->scrolling) {
2822 if ((uint32_t)lines > terminal->saved_start - terminal->start)
2823 lines = terminal->saved_start - terminal->start;
2824 } else {
2825 lines = 0;
2826 }
2827 } else if (lines < 0) {
2828 uint32_t neg_lines = -lines;
2829
2830 if (neg_lines > terminal->log_size + terminal->start - terminal->end)
2831 lines = terminal->end - terminal->log_size - terminal->start;
2832 }
2833
2834 if (lines) {
2835 if (!terminal->scrolling)
2836 terminal->saved_start = terminal->start;
2837 terminal->scrolling = 1;
2838
2839 terminal->start += lines;
2840 terminal->row -= lines;
2841 terminal->selection_start_row -= lines;
2842 terminal->selection_end_row -= lines;
2843
2844 widget_schedule_redraw(widget);
2845 }
2846}
2847
Alexander Larssonde79dd02013-05-22 14:41:34 +02002848static void
2849output_handler(struct window *window, struct output *output, int enter,
2850 void *data)
2851{
2852 if (enter)
2853 window_set_buffer_transform(window, output_get_transform(output));
2854 window_set_buffer_scale(window, window_get_output_scale(window));
2855 window_schedule_redraw(window);
2856}
2857
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002858static void
2859touch_down_handler(struct widget *widget, struct input *input,
2860 uint32_t serial, uint32_t time, int32_t id,
2861 float x, float y, void *data)
2862{
2863 struct terminal *terminal = data;
2864
2865 if (id == 0)
2866 click_handler(widget, terminal, input, x, y, time);
2867}
2868
2869static void
2870touch_up_handler(struct widget *widget, struct input *input,
2871 uint32_t serial, uint32_t time, int32_t id, void *data)
2872{
2873 struct terminal *terminal = data;
2874
2875 if (id == 0)
2876 terminal->dragging = SELECT_NONE;
2877}
2878
2879static void
2880touch_motion_handler(struct widget *widget, struct input *input,
2881 uint32_t time, int32_t id, float x, float y, void *data)
2882{
2883 struct terminal *terminal = data;
2884
2885 if (terminal->dragging &&
2886 id == 0) {
2887 terminal->selection_end_x = (int)x;
2888 terminal->selection_end_y = (int)y;
2889
2890 if (recompute_selection(terminal))
2891 widget_schedule_redraw(widget);
2892 }
2893}
2894
Peng Wuf291f202013-06-06 15:32:41 +08002895#ifndef howmany
2896#define howmany(x, y) (((x) + ((y) - 1)) / (y))
2897#endif
2898
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002899static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002900terminal_create(struct display *display)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002901{
2902 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002903 cairo_surface_t *surface;
2904 cairo_t *cr;
Peng Wuf291f202013-06-06 15:32:41 +08002905 cairo_text_extents_t text_extents;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002906
Peter Huttererf3d62272013-08-08 11:57:05 +10002907 terminal = xzalloc(sizeof *terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002908 terminal->color_scheme = &DEFAULT_COLORS;
2909 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002910 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002911 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002912 terminal->window = window_create(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002913 terminal->widget = window_frame_create(terminal->window, terminal);
U. Artie Eoff09827e22014-01-15 11:40:37 -08002914 terminal->title = xstrdup("Wayland Terminal");
Kristian Høgsberga83be202013-10-23 20:47:35 -07002915 window_set_title(terminal->window, terminal->title);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002916 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002917
2918 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002919 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002920
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002921 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002922 terminal->margin = 5;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002923 terminal->buffer_height = 1024;
2924 terminal->end = 1;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002925
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002926 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002927 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002928 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002929 keyboard_focus_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002930 window_set_fullscreen_handler(terminal->window, fullscreen_handler);
Alexander Larssonde79dd02013-05-22 14:41:34 +02002931 window_set_output_handler(terminal->window, output_handler);
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002932 window_set_close_handler(terminal->window, close_handler);
Jasper St. Pierrede680992014-04-10 17:23:49 -07002933 window_set_state_changed_handler(terminal->window, state_changed_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002934
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07002935 window_set_data_handler(terminal->window, data_handler);
2936 window_set_drop_handler(terminal->window, drop_handler);
2937
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002938 widget_set_redraw_handler(terminal->widget, redraw_handler);
2939 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002940 widget_set_button_handler(terminal->widget, button_handler);
Kristian Høgsberg29784402012-06-28 14:27:02 -04002941 widget_set_enter_handler(terminal->widget, enter_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002942 widget_set_motion_handler(terminal->widget, motion_handler);
Magnus Hoff1046f122014-08-05 15:05:59 +02002943 widget_set_axis_handler(terminal->widget, axis_handler);
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002944 widget_set_touch_up_handler(terminal->widget, touch_up_handler);
2945 widget_set_touch_down_handler(terminal->widget, touch_down_handler);
2946 widget_set_touch_motion_handler(terminal->widget, touch_motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002947
Kristian Høgsberg09531622010-06-14 23:22:15 -04002948 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2949 cr = cairo_create(surface);
Kristian Høgsberg38912df2012-06-27 17:52:23 -04002950 cairo_set_font_size(cr, option_font_size);
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002951 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002952 CAIRO_FONT_SLANT_NORMAL,
2953 CAIRO_FONT_WEIGHT_BOLD);
2954 terminal->font_bold = cairo_get_scaled_font (cr);
2955 cairo_scaled_font_reference(terminal->font_bold);
2956
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002957 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002958 CAIRO_FONT_SLANT_NORMAL,
2959 CAIRO_FONT_WEIGHT_NORMAL);
2960 terminal->font_normal = cairo_get_scaled_font (cr);
2961 cairo_scaled_font_reference(terminal->font_normal);
2962
Kristian Høgsberg09531622010-06-14 23:22:15 -04002963 cairo_font_extents(cr, &terminal->extents);
Peng Wuf291f202013-06-06 15:32:41 +08002964
2965 /* Compute the average ascii glyph width */
2966 cairo_text_extents(cr, TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS,
2967 &text_extents);
2968 terminal->average_width = howmany
2969 (text_extents.width,
2970 strlen(TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS));
2971 terminal->average_width = ceil(terminal->average_width);
2972
Kristian Høgsberg09531622010-06-14 23:22:15 -04002973 cairo_destroy(cr);
2974 cairo_surface_destroy(surface);
2975
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002976 terminal_resize(terminal, 20, 5); /* Set minimum size first */
Kristian Høgsberga1627922012-06-20 17:30:03 -04002977 terminal_resize(terminal, 80, 25);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002978
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002979 wl_list_insert(terminal_list.prev, &terminal->link);
2980
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002981 return terminal;
2982}
2983
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002984static void
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002985terminal_destroy(struct terminal *terminal)
2986{
Dima Ryazanova85292e2012-11-29 00:27:09 -08002987 display_unwatch_fd(terminal->display, terminal->master);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002988 window_destroy(terminal->window);
2989 close(terminal->master);
2990 wl_list_remove(&terminal->link);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002991
2992 if (wl_list_empty(&terminal_list))
Dima Ryazanova85292e2012-11-29 00:27:09 -08002993 display_exit(terminal->display);
2994
Kristian Høgsberga83be202013-10-23 20:47:35 -07002995 free(terminal->title);
Dima Ryazanova85292e2012-11-29 00:27:09 -08002996 free(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002997}
2998
2999static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003000io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003001{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003002 struct terminal *terminal =
3003 container_of(task, struct terminal, io_task);
3004 char buffer[256];
3005 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003006
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003007 if (events & EPOLLHUP) {
3008 terminal_destroy(terminal);
3009 return;
3010 }
Tim Wiederhakef71accc2011-01-19 23:14:33 +01003011
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003012 len = read(terminal->master, buffer, sizeof buffer);
3013 if (len < 0)
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003014 terminal_destroy(terminal);
3015 else
3016 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003017}
3018
3019static int
3020terminal_run(struct terminal *terminal, const char *path)
3021{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003022 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003023 pid_t pid;
3024
3025 pid = forkpty(&master, NULL, NULL, NULL);
3026 if (pid == 0) {
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04003027 setenv("TERM", option_term, 1);
3028 setenv("COLORTERM", option_term, 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003029 if (execl(path, path, NULL)) {
3030 printf("exec failed: %m\n");
3031 exit(EXIT_FAILURE);
3032 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003033 } else if (pid < 0) {
3034 fprintf(stderr, "failed to fork and create pty (%m).\n");
3035 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003036 }
3037
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003038 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003039 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003040 terminal->io_task.run = io_handler;
3041 display_watch_fd(terminal->display, terminal->master,
3042 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003043
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07003044 if (option_fullscreen)
3045 window_set_fullscreen(terminal->window, 1);
3046 else
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003047 terminal_resize(terminal, 80, 24);
3048
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003049 return 0;
3050}
3051
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003052static const struct weston_option terminal_options[] = {
3053 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04003054 { WESTON_OPTION_STRING, "font", 0, &option_font },
Bill Spitzak5cad8432014-08-08 12:59:55 -07003055 { WESTON_OPTION_INTEGER, "font-size", 0, &option_font_size },
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003056 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003057};
3058
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003059int main(int argc, char *argv[])
3060{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003061 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003062 struct terminal *terminal;
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02003063 const char *config_file;
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -07003064 struct weston_config *config;
3065 struct weston_config_section *s;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04003066
Peng Wucfcc1112013-08-19 10:59:21 +08003067 /* as wcwidth is locale-dependent,
3068 wcwidth needs setlocale call to function properly. */
3069 setlocale(LC_ALL, "");
3070
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003071 option_shell = getenv("SHELL");
3072 if (!option_shell)
3073 option_shell = "/bin/bash";
3074
Pekka Paalanen6c71aae2015-03-24 15:56:19 +02003075 config_file = weston_config_get_name_from_env();
3076 config = weston_config_parse(config_file);
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -07003077 s = weston_config_get_section(config, "terminal", NULL, NULL);
3078 weston_config_section_get_string(s, "font", &option_font, "mono");
3079 weston_config_section_get_int(s, "font-size", &option_font_size, 14);
3080 weston_config_section_get_string(s, "term", &option_term, "xterm");
3081 weston_config_destroy(config);
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003082
Bill Spitzak5cad8432014-08-08 12:59:55 -07003083 if (parse_options(terminal_options,
3084 ARRAY_LENGTH(terminal_options), &argc, argv) > 1) {
3085 printf("Usage: %s [OPTIONS]\n"
3086 " --fullscreen or -f\n"
3087 " --font=NAME\n"
3088 " --font-size=SIZE\n"
3089 " --shell=NAME\n", argv[0]);
3090 return 1;
3091 }
3092
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003093 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02003094 if (d == NULL) {
3095 fprintf(stderr, "failed to create display: %m\n");
3096 return -1;
3097 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003098
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003099 wl_list_init(&terminal_list);
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04003100 terminal = terminal_create(d);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003101 if (terminal_run(terminal, option_shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003102 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003103
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003104 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003105
3106 return 0;
3107}