blob: 0bacbe09f03d283d142c7216ea84e15e40cedce7 [file] [log] [blame]
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg0b36d972013-08-21 22:13:17 -070023#include <config.h>
Peng Wucfcc1112013-08-19 10:59:21 +080024
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +020025#include <stdbool.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050026#include <stdint.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <fcntl.h>
31#include <unistd.h>
32#include <math.h>
33#include <time.h>
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050034#include <pty.h>
Kristian Høgsbergf04e8382008-12-08 00:07:49 -050035#include <ctype.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050036#include <cairo.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040037#include <sys/epoll.h>
Peng Wucfcc1112013-08-19 10:59:21 +080038#include <wchar.h>
39#include <locale.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050040
Kristian Høgsberg67b82152013-10-23 16:52:05 -070041#include <linux/input.h>
42
Pekka Paalanen50719bc2011-11-22 14:18:50 +020043#include <wayland-client.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050044
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -040045#include "../shared/config-parser.h"
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050046#include "window.h"
47
Kristian Høgsberg0395f302008-12-22 12:14:50 -050048static int option_fullscreen;
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -070049static char *option_font;
50static int option_font_size;
51static char *option_term;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040052static char *option_shell;
53
54static struct wl_list terminal_list;
55
56static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -040057terminal_create(struct display *display);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040058static void
59terminal_destroy(struct terminal *terminal);
60static int
61terminal_run(struct terminal *terminal, const char *path);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050062
Peng Wuf291f202013-06-06 15:32:41 +080063#define TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS \
64 " !\"#$%&'()*+,-./" \
65 "0123456789" \
66 ":;<=>?@" \
67 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
68 "[\\]^_`" \
69 "abcdefghijklmnopqrstuvwxyz" \
70 "{|}~" \
71 ""
72
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050073#define MOD_SHIFT 0x01
74#define MOD_ALT 0x02
75#define MOD_CTRL 0x04
76
Callum Lowcay30eeae52011-01-07 19:46:55 +000077#define ATTRMASK_BOLD 0x01
78#define ATTRMASK_UNDERLINE 0x02
79#define ATTRMASK_BLINK 0x04
80#define ATTRMASK_INVERSE 0x08
Callum Lowcay81179db2011-01-10 12:14:01 +130081#define ATTRMASK_CONCEALED 0x10
Callum Lowcay30eeae52011-01-07 19:46:55 +000082
Callum Lowcayb8609ad2011-01-07 19:46:57 +000083/* Buffer sizes */
Callum Lowcayef57a9b2011-01-14 20:46:23 +130084#define MAX_RESPONSE 256
85#define MAX_ESCAPE 255
Callum Lowcayb8609ad2011-01-07 19:46:57 +000086
Callum Lowcay8e57dd52011-01-07 19:46:59 +000087/* Terminal modes */
88#define MODE_SHOW_CURSOR 0x00000001
89#define MODE_INVERSE 0x00000002
90#define MODE_AUTOWRAP 0x00000004
91#define MODE_AUTOREPEAT 0x00000008
92#define MODE_LF_NEWLINE 0x00000010
Callum Lowcay69e96582011-01-07 19:47:00 +000093#define MODE_IRM 0x00000020
Callum Lowcay7e08e902011-01-07 19:47:02 +000094#define MODE_DELETE_SENDS_DEL 0x00000040
95#define MODE_ALT_SENDS_ESC 0x00000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +000096
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000097union utf8_char {
98 unsigned char byte[4];
99 uint32_t ch;
100};
101
102enum utf8_state {
103 utf8state_start,
104 utf8state_accept,
105 utf8state_reject,
106 utf8state_expect3,
107 utf8state_expect2,
108 utf8state_expect1
109};
110
111struct utf8_state_machine {
112 enum utf8_state state;
113 int len;
114 union utf8_char s;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700115 uint32_t unicode;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000116};
117
118static void
119init_state_machine(struct utf8_state_machine *machine)
120{
121 machine->state = utf8state_start;
122 machine->len = 0;
123 machine->s.ch = 0;
124}
125
126static enum utf8_state
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400127utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000128{
129 switch(machine->state) {
130 case utf8state_start:
131 case utf8state_accept:
132 case utf8state_reject:
133 machine->s.ch = 0;
134 machine->len = 0;
135 if(c == 0xC0 || c == 0xC1) {
136 /* overlong encoding, reject */
137 machine->state = utf8state_reject;
138 } else if((c & 0x80) == 0) {
139 /* single byte, accept */
140 machine->s.byte[machine->len++] = c;
141 machine->state = utf8state_accept;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700142 machine->unicode = c;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000143 } else if((c & 0xC0) == 0x80) {
144 /* parser out of sync, ignore byte */
145 machine->state = utf8state_start;
146 } else if((c & 0xE0) == 0xC0) {
147 /* start of two byte sequence */
148 machine->s.byte[machine->len++] = c;
149 machine->state = utf8state_expect1;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700150 machine->unicode = c & 0x1f;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000151 } else if((c & 0xF0) == 0xE0) {
152 /* start of three byte sequence */
153 machine->s.byte[machine->len++] = c;
154 machine->state = utf8state_expect2;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700155 machine->unicode = c & 0x0f;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000156 } else if((c & 0xF8) == 0xF0) {
157 /* start of four byte sequence */
158 machine->s.byte[machine->len++] = c;
159 machine->state = utf8state_expect3;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700160 machine->unicode = c & 0x07;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000161 } else {
162 /* overlong encoding, reject */
163 machine->state = utf8state_reject;
164 }
165 break;
166 case utf8state_expect3:
167 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700168 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000169 if((c & 0xC0) == 0x80) {
170 /* all good, continue */
171 machine->state = utf8state_expect2;
172 } else {
173 /* missing extra byte, reject */
174 machine->state = utf8state_reject;
175 }
176 break;
177 case utf8state_expect2:
178 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700179 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000180 if((c & 0xC0) == 0x80) {
181 /* all good, continue */
182 machine->state = utf8state_expect1;
183 } else {
184 /* missing extra byte, reject */
185 machine->state = utf8state_reject;
186 }
187 break;
188 case utf8state_expect1:
189 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700190 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000191 if((c & 0xC0) == 0x80) {
192 /* all good, accept */
193 machine->state = utf8state_accept;
194 } else {
195 /* missing extra byte, reject */
196 machine->state = utf8state_reject;
197 }
198 break;
199 default:
200 machine->state = utf8state_reject;
201 break;
202 }
203
204 return machine->state;
205}
206
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700207static uint32_t
208get_unicode(union utf8_char utf8)
209{
210 struct utf8_state_machine machine;
211 int i;
212
213 init_state_machine(&machine);
214 for (i = 0; i < 4; i++) {
215 utf8_next_char(&machine, utf8.byte[i]);
216 if (machine.state == utf8state_accept ||
217 machine.state == utf8state_reject)
218 break;
219 }
220
221 if (machine.state == utf8state_reject)
222 return 0xfffd;
223
224 return machine.unicode;
225}
226
Peng Wucfcc1112013-08-19 10:59:21 +0800227static bool
228is_wide(union utf8_char utf8)
229{
230 uint32_t unichar = get_unicode(utf8);
231 return wcwidth(unichar) > 1;
232}
233
Callum Lowcay256e72f2011-01-07 19:47:01 +0000234struct char_sub {
235 union utf8_char match;
236 union utf8_char replace;
237};
238/* Set last char_sub match to NULL char */
239typedef struct char_sub *character_set;
240
241struct char_sub CS_US[] = {
242 {{{0, }}, {{0, }}}
243};
244static struct char_sub CS_UK[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200245 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000246 {{{0, }}, {{0, }}}
247};
248static struct char_sub CS_SPECIAL[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200249 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond: ♦ */
250 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell: ▒ */
251 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT: ␉ */
252 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF: ␌ */
253 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR: ␍ */
254 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF: ␊ */
255 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree: ° */
256 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus: ± */
257 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL: ␤ */
258 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT: ␋ */
259 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB: ┘ */
260 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT: ┐ */
261 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT: ┌ */
262 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_LB: └ */
263 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS: ┼ */
264 {{{'o', 0, }}, {{0xE2, 0x8E, 0xBA, 0}}}, /* Horiz. Scan Line 1: ⎺ */
265 {{{'p', 0, }}, {{0xE2, 0x8E, 0xBB, 0}}}, /* Horiz. Scan Line 3: ⎻ */
266 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* Horiz. Scan Line 5: ─ */
267 {{{'r', 0, }}, {{0xE2, 0x8E, 0xBC, 0}}}, /* Horiz. Scan Line 7: ⎼ */
268 {{{'s', 0, }}, {{0xE2, 0x8E, 0xBD, 0}}}, /* Horiz. Scan Line 9: ⎽ */
269 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR: ├ */
270 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL: ┤ */
271 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU: ┴ */
272 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD: ┬ */
273 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V: │ */
274 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE: ≤ */
275 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE: ≥ */
276 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI: π */
277 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ: ≠ */
278 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
279 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT: ⋅ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000280 {{{0, }}, {{0, }}}
281};
282
283static void
284apply_char_set(character_set cs, union utf8_char *utf8)
285{
286 int i = 0;
287
288 while (cs[i].match.byte[0]) {
289 if ((*utf8).ch == cs[i].match.ch) {
290 *utf8 = cs[i].replace;
291 break;
292 }
293 i++;
294 }
295}
296
Callum Lowcay7e08e902011-01-07 19:47:02 +0000297struct key_map {
298 int sym;
299 int num;
300 char escape;
301 char code;
302};
303/* Set last key_sub sym to NULL */
304typedef struct key_map *keyboard_mode;
305
306static struct key_map KM_NORMAL[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400307 { XKB_KEY_Left, 1, '[', 'D' },
308 { XKB_KEY_Right, 1, '[', 'C' },
309 { XKB_KEY_Up, 1, '[', 'A' },
310 { XKB_KEY_Down, 1, '[', 'B' },
311 { XKB_KEY_Home, 1, '[', 'H' },
312 { XKB_KEY_End, 1, '[', 'F' },
313 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000314};
315static struct key_map KM_APPLICATION[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400316 { XKB_KEY_Left, 1, 'O', 'D' },
317 { XKB_KEY_Right, 1, 'O', 'C' },
318 { XKB_KEY_Up, 1, 'O', 'A' },
319 { XKB_KEY_Down, 1, 'O', 'B' },
320 { XKB_KEY_Home, 1, 'O', 'H' },
321 { XKB_KEY_End, 1, 'O', 'F' },
322 { XKB_KEY_KP_Enter, 1, 'O', 'M' },
323 { XKB_KEY_KP_Multiply, 1, 'O', 'j' },
324 { XKB_KEY_KP_Add, 1, 'O', 'k' },
325 { XKB_KEY_KP_Separator, 1, 'O', 'l' },
326 { XKB_KEY_KP_Subtract, 1, 'O', 'm' },
327 { XKB_KEY_KP_Divide, 1, 'O', 'o' },
328 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000329};
330
331static int
332function_key_response(char escape, int num, uint32_t modifiers,
333 char code, char *response)
334{
335 int mod_num = 0;
336 int len;
337
Kristian Høgsberg70163132012-05-08 15:55:39 -0400338 if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
339 if (modifiers & MOD_ALT_MASK) mod_num |= 2;
340 if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000341
342 if (mod_num != 0)
343 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
344 num, mod_num + 1, code);
345 else if (code != '~')
346 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
347 escape, code);
348 else
349 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
350 escape, num, code);
351
352 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
353 else return len;
354}
355
356/* returns the number of bytes written into response,
357 * which must have room for MAX_RESPONSE bytes */
358static int
359apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
360{
361 struct key_map map;
362 int len = 0;
363 int i = 0;
364
365 while (mode[i].sym) {
366 map = mode[i++];
367 if (sym == map.sym) {
368 len = function_key_response(map.escape, map.num,
369 modifiers, map.code,
370 response);
371 break;
372 }
373 }
374
375 return len;
376}
377
Callum Lowcay30eeae52011-01-07 19:46:55 +0000378struct terminal_color { double r, g, b, a; };
379struct attr {
380 unsigned char fg, bg;
381 char a; /* attributes format:
382 * 76543210
Callum Lowcay81179db2011-01-10 12:14:01 +1300383 * cilub */
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500384 char s; /* in selection */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000385};
386struct color_scheme {
387 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500388 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000389 struct attr default_attr;
390};
391
392static void
393attr_init(struct attr *data_attr, struct attr attr, int n)
394{
395 int i;
396 for (i = 0; i < n; i++) {
397 data_attr[i] = attr;
398 }
399}
400
Callum Lowcay67a201d2011-01-12 19:23:41 +1300401enum escape_state {
402 escape_state_normal = 0,
403 escape_state_escape,
404 escape_state_dcs,
405 escape_state_csi,
406 escape_state_osc,
407 escape_state_inner_escape,
408 escape_state_ignore,
409 escape_state_special
410};
411
412#define ESC_FLAG_WHAT 0x01
413#define ESC_FLAG_GT 0x02
414#define ESC_FLAG_BANG 0x04
415#define ESC_FLAG_CASH 0x08
416#define ESC_FLAG_SQUOTE 0x10
417#define ESC_FLAG_DQUOTE 0x20
418#define ESC_FLAG_SPACE 0x40
419
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400420enum {
421 SELECT_NONE,
422 SELECT_CHAR,
423 SELECT_WORD,
424 SELECT_LINE
425};
426
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500427struct terminal {
428 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500429 struct widget *widget;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500430 struct display *display;
Kristian Høgsberga83be202013-10-23 20:47:35 -0700431 char *title;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000432 union utf8_char *data;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400433 struct task io_task;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000434 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000435 struct attr *data_attr;
436 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000437 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000438 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000439 char saved_origin_mode;
440 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000441 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000442 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000443 character_set cs, g0, g1;
444 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000445 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000446 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700447 int width, height, row, column, max_width;
448 uint32_t buffer_height;
449 uint32_t start, end, saved_start, log_size;
Magnus Hoff1046f122014-08-05 15:05:59 +0200450 wl_fixed_t smooth_scroll;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000451 int saved_row, saved_column;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700452 int scrolling;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600453 int send_cursor_position;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500454 int fd, master;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500455 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300456 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500457 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300458 enum escape_state state;
459 enum escape_state outer_state;
460 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000461 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500462 int margin;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400463 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000464 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400465 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +0800466 double average_width;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500467 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg88fd4082012-06-21 15:55:03 -0400468 uint32_t hide_cursor_serial;
Kristian Høgsbergb405a802014-02-05 14:44:04 -0800469 int size_in_title;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500470
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400471 struct wl_data_source *selection;
Xiong Zhang49c6aee2013-11-25 18:42:52 +0800472 uint32_t click_time;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400473 int dragging, click_count;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500474 int selection_start_x, selection_start_y;
475 int selection_end_x, selection_end_y;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400476 int selection_start_row, selection_start_col;
477 int selection_end_row, selection_end_col;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -0400478 struct wl_list link;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500479};
480
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000481/* Create default tab stops, every 8 characters */
482static void
483terminal_init_tabs(struct terminal *terminal)
484{
485 int i = 0;
486
487 while (i < terminal->width) {
488 if (i % 8 == 0)
489 terminal->tab_ruler[i] = 1;
490 else
491 terminal->tab_ruler[i] = 0;
492 i++;
493 }
494}
495
Callum Lowcay30eeae52011-01-07 19:46:55 +0000496static void
497terminal_init(struct terminal *terminal)
498{
499 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000500 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000501 terminal->mode = MODE_SHOW_CURSOR |
502 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000503 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000504 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000505
506 terminal->row = 0;
507 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000508
Callum Lowcay256e72f2011-01-07 19:47:01 +0000509 terminal->g0 = CS_US;
510 terminal->g1 = CS_US;
511 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000512 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000513
514 terminal->saved_g0 = terminal->g0;
515 terminal->saved_g1 = terminal->g1;
516 terminal->saved_cs = terminal->cs;
517
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000518 terminal->saved_attr = terminal->curr_attr;
519 terminal->saved_origin_mode = terminal->origin_mode;
520 terminal->saved_row = terminal->row;
521 terminal->saved_column = terminal->column;
522
523 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000524}
525
526static void
527init_color_table(struct terminal *terminal)
528{
529 int c, r;
530 struct terminal_color *color_table = terminal->color_table;
531
532 for (c = 0; c < 256; c ++) {
533 if (c < 16) {
534 color_table[c] = terminal->color_scheme->palette[c];
535 } else if (c < 232) {
536 r = c - 16;
537 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
538 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
539 color_table[c].r = ((double)(r % 6) / 6.0);
540 color_table[c].a = 1.0;
541 } else {
542 r = (c - 232) * 10 + 8;
543 color_table[c].r = ((double) r) / 256.0;
544 color_table[c].g = color_table[c].r;
545 color_table[c].b = color_table[c].r;
546 color_table[c].a = 1.0;
547 }
548 }
549}
550
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000551static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500552terminal_get_row(struct terminal *terminal, int row)
553{
554 int index;
555
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700556 index = (row + terminal->start) & (terminal->buffer_height - 1);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500557
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700558 return (void *) terminal->data + index * terminal->data_pitch;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500559}
560
Callum Lowcay30eeae52011-01-07 19:46:55 +0000561static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500562terminal_get_attr_row(struct terminal *terminal, int row)
563{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000564 int index;
565
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700566 index = (row + terminal->start) & (terminal->buffer_height - 1);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000567
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700568 return (void *) terminal->data_attr + index * terminal->attr_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000569}
570
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500571union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300572 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500573 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500574};
575
576static void
577terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500578 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500579{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500580 struct attr attr;
581 int foreground, background, tmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500582
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500583 decoded->attr.s = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400584 if (((row == terminal->selection_start_row &&
585 col >= terminal->selection_start_col) ||
586 row > terminal->selection_start_row) &&
587 ((row == terminal->selection_end_row &&
588 col < terminal->selection_end_col) ||
589 row < terminal->selection_end_row))
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500590 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500591
592 /* get the attributes for this character cell */
593 attr = terminal_get_attr_row(terminal, row)[col];
594 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500595 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500596 ((terminal->mode & MODE_SHOW_CURSOR) &&
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400597 window_has_focus(terminal->window) && terminal->row == row &&
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500598 terminal->column == col)) {
599 foreground = attr.bg;
600 background = attr.fg;
601 if (attr.a & ATTRMASK_BOLD) {
602 if (foreground <= 16) foreground |= 0x08;
603 if (background <= 16) background &= 0x07;
604 }
605 } else {
606 foreground = attr.fg;
607 background = attr.bg;
608 }
609
610 if (terminal->mode & MODE_INVERSE) {
611 tmp = foreground;
612 foreground = background;
613 background = tmp;
614 if (attr.a & ATTRMASK_BOLD) {
615 if (foreground <= 16) foreground |= 0x08;
616 if (background <= 16) background &= 0x07;
617 }
618 }
619
Callum Lowcay9d708b02011-01-12 20:06:17 +1300620 decoded->attr.fg = foreground;
621 decoded->attr.bg = background;
622 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000623}
624
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500625
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500626static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000627terminal_scroll_buffer(struct terminal *terminal, int d)
628{
629 int i;
630
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700631 terminal->start += d;
632 if (d < 0) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000633 d = 0 - d;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700634 for (i = 0; i < d; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000635 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
636 attr_init(terminal_get_attr_row(terminal, i),
637 terminal->curr_attr, terminal->width);
638 }
639 } else {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700640 for (i = terminal->height - d; i < terminal->height; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000641 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
642 attr_init(terminal_get_attr_row(terminal, i),
643 terminal->curr_attr, terminal->width);
644 }
645 }
Kristian Høgsberg18e928d2012-06-27 19:29:41 -0400646
647 terminal->selection_start_row -= d;
648 terminal->selection_end_row -= d;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000649}
650
651static void
652terminal_scroll_window(struct terminal *terminal, int d)
653{
654 int i;
655 int window_height;
656 int from_row, to_row;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000657
658 // scrolling range is inclusive
659 window_height = terminal->margin_bottom - terminal->margin_top + 1;
660 d = d % (window_height + 1);
661 if(d < 0) {
662 d = 0 - d;
663 to_row = terminal->margin_bottom;
664 from_row = terminal->margin_bottom - d;
665
666 for (i = 0; i < (window_height - d); i++) {
667 memcpy(terminal_get_row(terminal, to_row - i),
668 terminal_get_row(terminal, from_row - i),
669 terminal->data_pitch);
670 memcpy(terminal_get_attr_row(terminal, to_row - i),
671 terminal_get_attr_row(terminal, from_row - i),
672 terminal->attr_pitch);
673 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000674 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
675 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000676 attr_init(terminal_get_attr_row(terminal, i),
677 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000678 }
679 } else {
680 to_row = terminal->margin_top;
681 from_row = terminal->margin_top + d;
682
683 for (i = 0; i < (window_height - d); i++) {
684 memcpy(terminal_get_row(terminal, to_row + i),
685 terminal_get_row(terminal, from_row + i),
686 terminal->data_pitch);
687 memcpy(terminal_get_attr_row(terminal, to_row + i),
688 terminal_get_attr_row(terminal, from_row + i),
689 terminal->attr_pitch);
690 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000691 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
692 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000693 attr_init(terminal_get_attr_row(terminal, i),
694 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000695 }
696 }
697}
698
699static void
700terminal_scroll(struct terminal *terminal, int d)
701{
702 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
703 terminal_scroll_buffer(terminal, d);
704 else
705 terminal_scroll_window(terminal, d);
706}
707
708static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000709terminal_shift_line(struct terminal *terminal, int d)
710{
711 union utf8_char *row;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500712 struct attr *attr_row;
Callum Lowcay69e96582011-01-07 19:47:00 +0000713
714 row = terminal_get_row(terminal, terminal->row);
715 attr_row = terminal_get_attr_row(terminal, terminal->row);
716
717 if ((terminal->width + d) <= terminal->column)
718 d = terminal->column + 1 - terminal->width;
719 if ((terminal->column + d) >= terminal->width)
720 d = terminal->width - terminal->column - 1;
721
722 if (d < 0) {
723 d = 0 - d;
724 memmove(&row[terminal->column],
725 &row[terminal->column + d],
726 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
Callum Lowcay69e96582011-01-07 19:47:00 +0000727 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
728 (terminal->width - terminal->column - d) * sizeof(struct attr));
729 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
730 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
731 } else {
732 memmove(&row[terminal->column + d], &row[terminal->column],
733 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
734 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
735 (terminal->width - terminal->column - d) * sizeof(struct attr));
736 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
737 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
738 }
739}
740
741static void
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700742terminal_resize_cells(struct terminal *terminal,
743 int width, int height)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500744{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000745 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000746 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000747 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000748 int data_pitch, attr_pitch;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500749 int i, l, total_rows;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700750 uint32_t d, uheight = height;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500751 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000752 struct winsize ws;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500753
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700754 if (uheight > terminal->buffer_height)
755 height = terminal->buffer_height;
756
Kristian Høgsberg22106762008-12-08 13:50:07 -0500757 if (terminal->width == width && terminal->height == height)
758 return;
759
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700760 if (terminal->data && width <= terminal->max_width) {
761 d = 0;
762 if (height < terminal->height && height <= terminal->row)
763 d = terminal->height - height;
764 else if (height > terminal->height &&
765 terminal->height - 1 == terminal->row) {
766 d = terminal->height - height;
767 if (terminal->log_size < uheight)
768 d = -terminal->start;
769 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500770
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700771 terminal->start += d;
772 terminal->row -= d;
773 } else {
774 terminal->max_width = width;
775 data_pitch = width * sizeof(union utf8_char);
Bryce W. Harrington66cd2c12014-04-28 18:44:08 +0000776 data = xzalloc(data_pitch * terminal->buffer_height);
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700777 attr_pitch = width * sizeof(struct attr);
Bryce W. Harrington66cd2c12014-04-28 18:44:08 +0000778 data_attr = xmalloc(attr_pitch * terminal->buffer_height);
779 tab_ruler = xzalloc(width);
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700780 attr_init(data_attr, terminal->curr_attr,
781 width * terminal->buffer_height);
782
783 if (terminal->data && terminal->data_attr) {
784 if (width > terminal->width)
785 l = terminal->width;
786 else
787 l = width;
788
789 if (terminal->height > height) {
790 total_rows = height;
791 i = 1 + terminal->row - height;
792 if (i > 0) {
793 terminal->start += i;
794 terminal->row = terminal->row - i;
795 }
796 } else {
797 total_rows = terminal->height;
José Bollo4a4704a2013-09-13 11:28:51 +0200798 }
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700799
800 for (i = 0; i < total_rows; i++) {
801 memcpy(&data[width * i],
802 terminal_get_row(terminal, i),
803 l * sizeof(union utf8_char));
804 memcpy(&data_attr[width * i],
805 terminal_get_attr_row(terminal, i),
806 l * sizeof(struct attr));
807 }
808
809 free(terminal->data);
810 free(terminal->data_attr);
811 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500812 }
813
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700814 terminal->data_pitch = data_pitch;
815 terminal->attr_pitch = attr_pitch;
816 terminal->data = data;
817 terminal->data_attr = data_attr;
818 terminal->tab_ruler = tab_ruler;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700819 terminal->start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500820 }
821
Callum Lowcay86653ed2011-01-07 19:47:03 +0000822 terminal->margin_bottom =
823 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500824 terminal->width = width;
825 terminal->height = height;
Kristian Høgsbergdcfff552013-11-22 22:43:20 -0800826 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500827
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000828 /* Update the window size */
829 ws.ws_row = terminal->height;
830 ws.ws_col = terminal->width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500831 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500832 ws.ws_xpixel = allocation.width;
833 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000834 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500835}
836
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500837static void
Jasper St. Pierrede680992014-04-10 17:23:49 -0700838update_title(struct terminal *terminal)
839{
840 if (window_is_resizing(terminal->window)) {
841 char *p;
842 if (asprintf(&p, "%s — [%dx%d]", terminal->title, terminal->width, terminal->height) > 0) {
843 window_set_title(terminal->window, p);
844 free(p);
845 }
846 } else {
847 window_set_title(terminal->window, terminal->title);
848 }
849}
850
851static void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500852resize_handler(struct widget *widget,
853 int32_t width, int32_t height, void *data)
854{
855 struct terminal *terminal = data;
856 int32_t columns, rows, m;
Jasper St. Pierrede680992014-04-10 17:23:49 -0700857
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500858 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800859 columns = (width - m) / (int32_t) terminal->average_width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500860 rows = (height - m) / (int32_t) terminal->extents.height;
861
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400862 if (!window_is_fullscreen(terminal->window) &&
863 !window_is_maximized(terminal->window)) {
Peng Wuf291f202013-06-06 15:32:41 +0800864 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500865 height = rows * terminal->extents.height + m;
866 widget_set_size(terminal->widget, width, height);
867 }
868
869 terminal_resize_cells(terminal, columns, rows);
Jasper St. Pierrede680992014-04-10 17:23:49 -0700870 update_title(terminal);
871}
872
873static void
874state_changed_handler(struct window *window, void *data)
875{
876 struct terminal *terminal = data;
877 update_title(terminal);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500878}
879
880static void
881terminal_resize(struct terminal *terminal, int columns, int rows)
882{
883 int32_t width, height, m;
884
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400885 if (window_is_fullscreen(terminal->window) ||
886 window_is_maximized(terminal->window))
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500887 return;
888
889 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800890 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500891 height = rows * terminal->extents.height + m;
Kristian Høgsberga1627922012-06-20 17:30:03 -0400892
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500893 window_frame_set_child_size(terminal->widget, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500894}
895
Callum Lowcay30eeae52011-01-07 19:46:55 +0000896struct color_scheme DEFAULT_COLORS = {
897 {
898 {0, 0, 0, 1}, /* black */
899 {0.66, 0, 0, 1}, /* red */
900 {0 , 0.66, 0, 1}, /* green */
901 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
902 {0 , 0 , 0.66, 1}, /* blue */
903 {0.66, 0 , 0.66, 1}, /* magenta */
904 {0, 0.66, 0.66, 1}, /* cyan */
905 {0.66, 0.66, 0.66, 1}, /* light grey */
906 {0.22, 0.33, 0.33, 1}, /* dark grey */
907 {1, 0.33, 0.33, 1}, /* high red */
908 {0.33, 1, 0.33, 1}, /* high green */
909 {1, 1, 0.33, 1}, /* high yellow */
910 {0.33, 0.33, 1, 1}, /* high blue */
911 {1, 0.33, 1, 1}, /* high magenta */
912 {0.33, 1, 1, 1}, /* high cyan */
913 {1, 1, 1, 1} /* white */
914 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500915 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000916 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
917};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400918
Kristian Høgsberg22106762008-12-08 13:50:07 -0500919static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500920terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
921{
922 cairo_set_source_rgba(cr,
923 terminal->color_table[index].r,
924 terminal->color_table[index].g,
925 terminal->color_table[index].b,
926 terminal->color_table[index].a);
927}
928
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500929static void
930terminal_send_selection(struct terminal *terminal, int fd)
931{
932 int row, col;
933 union utf8_char *p_row;
934 union decoded_attr attr;
935 FILE *fp;
936 int len;
937
938 fp = fdopen(fd, "w");
Brian Lovin1bf14812013-08-08 16:12:55 -0700939 if (fp == NULL){
940 close(fd);
941 return;
942 }
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500943 for (row = 0; row < terminal->height; row++) {
944 p_row = terminal_get_row(terminal, row);
945 for (col = 0; col < terminal->width; col++) {
Peng Wucfcc1112013-08-19 10:59:21 +0800946 if (p_row[col].ch == 0x200B) /* space glyph */
947 continue;
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500948 /* get the attributes for this character cell */
949 terminal_decode_attr(terminal, row, col, &attr);
950 if (!attr.attr.s)
951 continue;
952 len = strnlen((char *) p_row[col].byte, 4);
Kristian Høgsberg0dee6472012-07-01 21:25:41 -0400953 if (len > 0)
954 fwrite(p_row[col].byte, 1, len, fp);
955 if (len == 0 || col == terminal->width - 1) {
956 fwrite("\n", 1, 1, fp);
957 break;
958 }
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500959 }
960 }
961 fclose(fp);
962}
963
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500964struct glyph_run {
965 struct terminal *terminal;
966 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400967 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500968 union decoded_attr attr;
969 cairo_glyph_t glyphs[256], *g;
970};
971
972static void
973glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
974{
975 run->terminal = terminal;
976 run->cr = cr;
977 run->g = run->glyphs;
978 run->count = 0;
979 run->attr.key = 0;
980}
981
982static void
983glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
984{
985 cairo_scaled_font_t *font;
986
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500987 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
988 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300989 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500990 font = run->terminal->font_bold;
991 else
992 font = run->terminal->font_normal;
993 cairo_set_scaled_font(run->cr, font);
994 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300995 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500996
Callum Lowcay9d708b02011-01-12 20:06:17 +1300997 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
998 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500999 run->g = run->glyphs;
1000 run->count = 0;
1001 }
Callum Lowcay9d708b02011-01-12 20:06:17 +13001002 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001003}
1004
1005static void
1006glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
1007{
1008 int num_glyphs;
1009 cairo_scaled_font_t *font;
1010
1011 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
1012
Callum Lowcay9d708b02011-01-12 20:06:17 +13001013 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001014 font = run->terminal->font_bold;
1015 else
1016 font = run->terminal->font_normal;
1017
1018 cairo_move_to(run->cr, x, y);
1019 cairo_scaled_font_text_to_glyphs (font, x, y,
1020 (char *) c->byte, 4,
1021 &run->g, &num_glyphs,
1022 NULL, NULL, NULL);
1023 run->g += num_glyphs;
1024 run->count += num_glyphs;
1025}
1026
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001027
Kristian Høgsbergf106fd52011-01-11 10:11:39 -05001028static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001029redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001030{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001031 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001032 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001033 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001034 int top_margin, side_margin;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001035 int row, col, cursor_x, cursor_y;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001036 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001037 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001038 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001039 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001040 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001041 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -05001042 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +08001043 double average_width;
Peng Wucfcc1112013-08-19 10:59:21 +08001044 double unichar_width;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001045
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001046 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001047 widget_get_allocation(terminal->widget, &allocation);
Alexander Larssonde79dd02013-05-22 14:41:34 +02001048 cr = widget_cairo_create(terminal->widget);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001049 cairo_rectangle(cr, allocation.x, allocation.y,
1050 allocation.width, allocation.height);
1051 cairo_clip(cr);
1052 cairo_push_group(cr);
1053
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001054 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -05001055 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001056 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001057
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05001058 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001059
Kristian Høgsberg59826582011-01-20 11:56:57 -05001060 extents = terminal->extents;
Peng Wuf291f202013-06-06 15:32:41 +08001061 average_width = terminal->average_width;
1062 side_margin = (allocation.width - terminal->width * average_width) / 2;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001063 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001064
Callum Lowcay30eeae52011-01-07 19:46:55 +00001065 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001066 cairo_translate(cr, allocation.x + side_margin,
1067 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001068 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001069 for (row = 0; row < terminal->height; row++) {
Peng Wucfcc1112013-08-19 10:59:21 +08001070 p_row = terminal_get_row(terminal, row);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001071 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001072 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -05001073 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001074
Callum Lowcay9d708b02011-01-12 20:06:17 +13001075 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -05001076 continue;
1077
Peng Wucfcc1112013-08-19 10:59:21 +08001078 if (is_wide(p_row[col]))
1079 unichar_width = 2 * average_width;
1080 else
1081 unichar_width = average_width;
1082
Callum Lowcay9d708b02011-01-12 20:06:17 +13001083 terminal_set_color(terminal, cr, attr.attr.bg);
Peng Wuf291f202013-06-06 15:32:41 +08001084 cairo_move_to(cr, col * average_width,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001085 row * extents.height);
Peng Wucfcc1112013-08-19 10:59:21 +08001086 cairo_rel_line_to(cr, unichar_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001087 cairo_rel_line_to(cr, 0, extents.height);
Peng Wucfcc1112013-08-19 10:59:21 +08001088 cairo_rel_line_to(cr, -unichar_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001089 cairo_close_path(cr);
1090 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001091 }
1092 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001093
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001094 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1095
1096 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001097 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001098 for (row = 0; row < terminal->height; row++) {
1099 p_row = terminal_get_row(terminal, row);
1100 for (col = 0; col < terminal->width; col++) {
1101 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -05001102 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001103
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001104 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001105
Peng Wuf291f202013-06-06 15:32:41 +08001106 text_x = col * average_width;
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001107 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +13001108 if (attr.attr.a & ATTRMASK_UNDERLINE) {
1109 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +00001110 cairo_move_to(cr, text_x, (double)text_y + 1.5);
Peng Wuf291f202013-06-06 15:32:41 +08001111 cairo_line_to(cr, text_x + average_width, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001112 cairo_stroke(cr);
1113 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -05001114
Daiki Ueno56d8a7a2014-04-08 18:46:18 +09001115 /* skip space glyph (RLE) we use as a placeholder of
1116 the right half of a double-width character,
1117 because RLE is not available in every font. */
1118 if (p_row[col].ch == 0x200B)
1119 continue;
1120
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001121 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001122 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001123 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001124
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001125 attr.key = ~0;
1126 glyph_run_flush(&run, attr);
1127
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001128 if ((terminal->mode & MODE_SHOW_CURSOR) &&
1129 !window_has_focus(terminal->window)) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001130 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001131
Callum Lowcay30eeae52011-01-07 19:46:55 +00001132 cairo_set_line_width(cr, 1);
Peng Wuf291f202013-06-06 15:32:41 +08001133 cairo_move_to(cr, terminal->column * average_width + d,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001134 terminal->row * extents.height + d);
Peng Wuf291f202013-06-06 15:32:41 +08001135 cairo_rel_line_to(cr, average_width - 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001136 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
Peng Wuf291f202013-06-06 15:32:41 +08001137 cairo_rel_line_to(cr, -average_width + 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001138 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001139
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001140 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001141 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001142
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001143 cairo_pop_group_to_source(cr);
1144 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001145 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001146 cairo_surface_destroy(surface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001147
1148 if (terminal->send_cursor_position) {
1149 cursor_x = side_margin + allocation.x +
Peng Wuf291f202013-06-06 15:32:41 +08001150 terminal->column * average_width;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001151 cursor_y = top_margin + allocation.y +
1152 terminal->row * extents.height;
1153 window_set_text_cursor_position(terminal->window,
1154 cursor_x, cursor_y);
1155 terminal->send_cursor_position = 0;
1156 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001157}
1158
1159static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001160terminal_write(struct terminal *terminal, const char *data, size_t length)
1161{
1162 if (write(terminal->master, data, length) < 0)
1163 abort();
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001164 terminal->send_cursor_position = 1;
Kristian Høgsberg26130862011-08-24 11:30:21 -04001165}
1166
1167static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001168terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001169
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001170static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001171handle_char(struct terminal *terminal, union utf8_char utf8);
1172
1173static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001174handle_sgr(struct terminal *terminal, int code);
1175
1176static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001177handle_term_parameter(struct terminal *terminal, int code, int sr)
1178{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001179 int i;
1180
Callum Lowcay67a201d2011-01-12 19:23:41 +13001181 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001182 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001183 case 1: /* DECCKM */
1184 if (sr) terminal->key_mode = KM_APPLICATION;
1185 else terminal->key_mode = KM_NORMAL;
1186 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001187 case 2: /* DECANM */
1188 /* No VT52 support yet */
1189 terminal->g0 = CS_US;
1190 terminal->g1 = CS_US;
1191 terminal->cs = terminal->g0;
1192 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001193 case 3: /* DECCOLM */
1194 if (sr)
1195 terminal_resize(terminal, 132, 24);
1196 else
1197 terminal_resize(terminal, 80, 24);
1198
1199 /* set columns, but also home cursor and clear screen */
1200 terminal->row = 0; terminal->column = 0;
1201 for (i = 0; i < terminal->height; i++) {
1202 memset(terminal_get_row(terminal, i),
1203 0, terminal->data_pitch);
1204 attr_init(terminal_get_attr_row(terminal, i),
1205 terminal->curr_attr, terminal->width);
1206 }
1207 break;
1208 case 5: /* DECSCNM */
1209 if (sr) terminal->mode |= MODE_INVERSE;
1210 else terminal->mode &= ~MODE_INVERSE;
1211 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001212 case 6: /* DECOM */
1213 terminal->origin_mode = sr;
1214 if (terminal->origin_mode)
1215 terminal->row = terminal->margin_top;
1216 else
1217 terminal->row = 0;
1218 terminal->column = 0;
1219 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001220 case 7: /* DECAWM */
1221 if (sr) terminal->mode |= MODE_AUTOWRAP;
1222 else terminal->mode &= ~MODE_AUTOWRAP;
1223 break;
1224 case 8: /* DECARM */
1225 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1226 else terminal->mode &= ~MODE_AUTOREPEAT;
1227 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001228 case 12: /* Very visible cursor (CVVIS) */
1229 /* FIXME: What do we do here. */
1230 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001231 case 25:
1232 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1233 else terminal->mode &= ~MODE_SHOW_CURSOR;
1234 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001235 case 1034: /* smm/rmm, meta mode on/off */
1236 /* ignore */
1237 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001238 case 1037: /* deleteSendsDel */
1239 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1240 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1241 break;
1242 case 1039: /* altSendsEscape */
1243 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1244 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1245 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001246 case 1049: /* rmcup/smcup, alternate screen */
1247 /* Ignore. Should be possible to implement,
1248 * but it's kind of annoying. */
1249 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001250 default:
1251 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1252 break;
1253 }
1254 } else {
1255 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001256 case 4: /* IRM */
1257 if (sr) terminal->mode |= MODE_IRM;
1258 else terminal->mode &= ~MODE_IRM;
1259 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001260 case 20: /* LNM */
1261 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1262 else terminal->mode &= ~MODE_LF_NEWLINE;
1263 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001264 default:
1265 fprintf(stderr, "Unknown parameter: %d\n", code);
1266 break;
1267 }
1268 }
1269}
1270
1271static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001272handle_dcs(struct terminal *terminal)
1273{
1274}
1275
1276static void
1277handle_osc(struct terminal *terminal)
1278{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001279 char *p;
1280 int code;
1281
1282 terminal->escape[terminal->escape_length++] = '\0';
1283 p = &terminal->escape[2];
1284 code = strtol(p, &p, 10);
1285 if (*p == ';') p++;
1286
1287 switch (code) {
1288 case 0: /* Icon name and window title */
1289 case 1: /* Icon label */
1290 case 2: /* Window title*/
Kristian Høgsberga83be202013-10-23 20:47:35 -07001291 free(terminal->title);
1292 terminal->title = strdup(p);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001293 window_set_title(terminal->window, p);
1294 break;
Kristian Høgsberg8a205002013-09-11 11:52:56 -07001295 case 7: /* shell cwd as uri */
1296 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001297 default:
Kristian Høgsberg8a205002013-09-11 11:52:56 -07001298 fprintf(stderr, "Unknown OSC escape code %d, text %s\n",
1299 code, p);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001300 break;
1301 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001302}
1303
1304static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001305handle_escape(struct terminal *terminal)
1306{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001307 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001308 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001309 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001310 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001311 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001312 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001313 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001314
1315 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001316 i = 0;
1317 p = &terminal->escape[2];
1318 while ((isdigit(*p) || *p == ';') && i < 10) {
1319 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001320 if (!set[i]) {
1321 args[i] = 0;
1322 set[i] = 1;
1323 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001324 p++;
1325 i++;
1326 } else {
1327 args[i] = strtol(p, &p, 10);
1328 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001329 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001330 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001331
1332 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001333 case '@': /* ICH */
1334 count = set[0] ? args[0] : 1;
1335 if (count == 0) count = 1;
1336 terminal_shift_line(terminal, count);
1337 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001338 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001339 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001340 if (count == 0) count = 1;
1341 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001342 terminal->row -= count;
1343 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001344 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001345 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001346 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001347 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001348 if (count == 0) count = 1;
1349 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001350 terminal->row += count;
1351 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001352 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001353 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001354 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001355 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001356 if (count == 0) count = 1;
1357 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001358 terminal->column += count;
1359 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001360 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001361 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001362 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001363 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001364 if (count == 0) count = 1;
1365 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001366 terminal->column -= count;
1367 else
1368 terminal->column = 0;
1369 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001370 case 'E': /* CNL */
1371 count = set[0] ? args[0] : 1;
1372 if (terminal->row + count <= terminal->margin_bottom)
1373 terminal->row += count;
1374 else
1375 terminal->row = terminal->margin_bottom;
1376 terminal->column = 0;
1377 break;
1378 case 'F': /* CPL */
1379 count = set[0] ? args[0] : 1;
1380 if (terminal->row - count >= terminal->margin_top)
1381 terminal->row -= count;
1382 else
1383 terminal->row = terminal->margin_top;
1384 terminal->column = 0;
1385 break;
1386 case 'G': /* CHA */
1387 y = set[0] ? args[0] : 1;
1388 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1389
1390 terminal->column = y - 1;
1391 break;
1392 case 'f': /* HVP */
1393 case 'H': /* CUP */
1394 x = (set[1] ? args[1] : 1) - 1;
1395 x = x < 0 ? 0 :
1396 (x >= terminal->width ? terminal->width - 1 : x);
1397
1398 y = (set[0] ? args[0] : 1) - 1;
1399 if (terminal->origin_mode) {
1400 y += terminal->margin_top;
1401 y = y < terminal->margin_top ? terminal->margin_top :
1402 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1403 } else {
1404 y = y < 0 ? 0 :
1405 (y >= terminal->height ? terminal->height - 1 : y);
1406 }
1407
1408 terminal->row = y;
1409 terminal->column = x;
1410 break;
1411 case 'I': /* CHT */
1412 count = set[0] ? args[0] : 1;
1413 if (count == 0) count = 1;
1414 while (count > 0 && terminal->column < terminal->width) {
1415 if (terminal->tab_ruler[terminal->column]) count--;
1416 terminal->column++;
1417 }
1418 terminal->column--;
1419 break;
1420 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001421 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001422 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001423 if (!set[0] || args[0] == 0 || args[0] > 2) {
1424 memset(&row[terminal->column],
1425 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1426 attr_init(&attr_row[terminal->column],
1427 terminal->curr_attr, terminal->width - terminal->column);
1428 for (i = terminal->row + 1; i < terminal->height; i++) {
1429 memset(terminal_get_row(terminal, i),
1430 0, terminal->data_pitch);
1431 attr_init(terminal_get_attr_row(terminal, i),
1432 terminal->curr_attr, terminal->width);
1433 }
1434 } else if (args[0] == 1) {
1435 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1436 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1437 for (i = 0; i < terminal->row; i++) {
1438 memset(terminal_get_row(terminal, i),
1439 0, terminal->data_pitch);
1440 attr_init(terminal_get_attr_row(terminal, i),
1441 terminal->curr_attr, terminal->width);
1442 }
1443 } else if (args[0] == 2) {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07001444 /* Clear screen by scrolling contents out */
1445 terminal_scroll_buffer(terminal,
1446 terminal->end - terminal->start);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001447 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001448 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001449 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001450 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001451 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001452 if (!set[0] || args[0] == 0 || args[0] > 2) {
1453 memset(&row[terminal->column], 0,
1454 (terminal->width - terminal->column) * sizeof(union utf8_char));
1455 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1456 terminal->width - terminal->column);
1457 } else if (args[0] == 1) {
1458 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1459 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1460 } else if (args[0] == 2) {
1461 memset(row, 0, terminal->data_pitch);
1462 attr_init(attr_row, terminal->curr_attr, terminal->width);
1463 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001464 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001465 case 'L': /* IL */
1466 count = set[0] ? args[0] : 1;
1467 if (count == 0) count = 1;
1468 if (terminal->row >= terminal->margin_top &&
1469 terminal->row < terminal->margin_bottom)
1470 {
1471 top = terminal->margin_top;
1472 terminal->margin_top = terminal->row;
1473 terminal_scroll(terminal, 0 - count);
1474 terminal->margin_top = top;
1475 } else if (terminal->row == terminal->margin_bottom) {
1476 memset(terminal_get_row(terminal, terminal->row),
1477 0, terminal->data_pitch);
1478 attr_init(terminal_get_attr_row(terminal, terminal->row),
1479 terminal->curr_attr, terminal->width);
1480 }
1481 break;
1482 case 'M': /* DL */
1483 count = set[0] ? args[0] : 1;
1484 if (count == 0) count = 1;
1485 if (terminal->row >= terminal->margin_top &&
1486 terminal->row < terminal->margin_bottom)
1487 {
1488 top = terminal->margin_top;
1489 terminal->margin_top = terminal->row;
1490 terminal_scroll(terminal, count);
1491 terminal->margin_top = top;
1492 } else if (terminal->row == terminal->margin_bottom) {
1493 memset(terminal_get_row(terminal, terminal->row),
1494 0, terminal->data_pitch);
1495 }
1496 break;
1497 case 'P': /* DCH */
1498 count = set[0] ? args[0] : 1;
1499 if (count == 0) count = 1;
1500 terminal_shift_line(terminal, 0 - count);
1501 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001502 case 'S': /* SU */
1503 terminal_scroll(terminal, set[0] ? args[0] : 1);
1504 break;
1505 case 'T': /* SD */
1506 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1507 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001508 case 'X': /* ECH */
1509 count = set[0] ? args[0] : 1;
1510 if (count == 0) count = 1;
1511 if ((terminal->column + count) > terminal->width)
1512 count = terminal->width - terminal->column;
1513 row = terminal_get_row(terminal, terminal->row);
1514 attr_row = terminal_get_attr_row(terminal, terminal->row);
1515 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1516 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1517 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001518 case 'Z': /* CBT */
1519 count = set[0] ? args[0] : 1;
1520 if (count == 0) count = 1;
1521 while (count > 0 && terminal->column >= 0) {
1522 if (terminal->tab_ruler[terminal->column]) count--;
1523 terminal->column--;
1524 }
1525 terminal->column++;
1526 break;
1527 case '`': /* HPA */
1528 y = set[0] ? args[0] : 1;
1529 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1530
1531 terminal->column = y - 1;
1532 break;
1533 case 'b': /* REP */
1534 count = set[0] ? args[0] : 1;
1535 if (count == 0) count = 1;
1536 if (terminal->last_char.byte[0])
1537 for (i = 0; i < count; i++)
1538 handle_char(terminal, terminal->last_char);
1539 terminal->last_char.byte[0] = 0;
1540 break;
1541 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001542 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001543 break;
1544 case 'd': /* VPA */
1545 x = set[0] ? args[0] : 1;
1546 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1547
1548 terminal->row = x - 1;
1549 break;
1550 case 'g': /* TBC */
1551 if (!set[0] || args[0] == 0) {
1552 terminal->tab_ruler[terminal->column] = 0;
1553 } else if (args[0] == 3) {
1554 memset(terminal->tab_ruler, 0, terminal->width);
1555 }
1556 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001557 case 'h': /* SM */
1558 for(i = 0; i < 10 && set[i]; i++) {
1559 handle_term_parameter(terminal, args[i], 1);
1560 }
1561 break;
1562 case 'l': /* RM */
1563 for(i = 0; i < 10 && set[i]; i++) {
1564 handle_term_parameter(terminal, args[i], 0);
1565 }
1566 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001567 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001568 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001569 if (i <= 7 && set[i] && set[i + 1] &&
1570 set[i + 2] && args[i + 1] == 5)
1571 {
1572 if (args[i] == 38) {
1573 handle_sgr(terminal, args[i + 2] + 256);
1574 break;
1575 } else if (args[i] == 48) {
1576 handle_sgr(terminal, args[i + 2] + 512);
1577 break;
1578 }
1579 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001580 if(set[i]) {
1581 handle_sgr(terminal, args[i]);
1582 } else if(i == 0) {
1583 handle_sgr(terminal, 0);
1584 break;
1585 } else {
1586 break;
1587 }
1588 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001589 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001590 case 'n': /* DSR */
1591 i = set[0] ? args[0] : 0;
1592 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001593 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001594 } else if (i == 6) {
1595 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1596 terminal->origin_mode ?
1597 terminal->row+terminal->margin_top : terminal->row+1,
1598 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001599 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001600 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001601 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001602 case 'r':
1603 if(!set[0]) {
1604 terminal->margin_top = 0;
1605 terminal->margin_bottom = terminal->height-1;
1606 terminal->row = 0;
1607 terminal->column = 0;
1608 } else {
1609 top = (set[0] ? args[0] : 1) - 1;
1610 top = top < 0 ? 0 :
1611 (top >= terminal->height ? terminal->height - 1 : top);
1612 bottom = (set[1] ? args[1] : 1) - 1;
1613 bottom = bottom < 0 ? 0 :
1614 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1615 if(bottom > top) {
1616 terminal->margin_top = top;
1617 terminal->margin_bottom = bottom;
1618 } else {
1619 terminal->margin_top = 0;
1620 terminal->margin_bottom = terminal->height-1;
1621 }
1622 if(terminal->origin_mode)
1623 terminal->row = terminal->margin_top;
1624 else
1625 terminal->row = 0;
1626 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001627 }
1628 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001629 case 's':
1630 terminal->saved_row = terminal->row;
1631 terminal->saved_column = terminal->column;
1632 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001633 case 't': /* windowOps */
1634 if (!set[0]) break;
1635 switch (args[0]) {
1636 case 4: /* resize px */
1637 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001638 widget_schedule_resize(terminal->widget,
1639 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001640 }
1641 break;
1642 case 8: /* resize ch */
1643 if (set[1] && set[2]) {
1644 terminal_resize(terminal, args[2], args[1]);
1645 }
1646 break;
1647 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001648 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001649 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1650 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001651 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001652 break;
1653 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001654 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001655 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1656 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001657 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001658 break;
1659 case 18: /* report ch */
1660 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1661 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001662 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001663 break;
1664 case 21: /* report title */
1665 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1666 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001667 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001668 break;
1669 default:
1670 if (args[0] >= 24)
1671 terminal_resize(terminal, terminal->width, args[0]);
1672 else
1673 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1674 break;
1675 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001676 case 'u':
1677 terminal->row = terminal->saved_row;
1678 terminal->column = terminal->saved_column;
1679 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001680 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001681 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001682 break;
1683 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001684}
1685
1686static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001687handle_non_csi_escape(struct terminal *terminal, char code)
1688{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001689 switch(code) {
1690 case 'M': /* RI */
1691 terminal->row -= 1;
1692 if(terminal->row < terminal->margin_top) {
1693 terminal->row = terminal->margin_top;
1694 terminal_scroll(terminal, -1);
1695 }
1696 break;
1697 case 'E': /* NEL */
1698 terminal->column = 0;
1699 // fallthrough
1700 case 'D': /* IND */
1701 terminal->row += 1;
1702 if(terminal->row > terminal->margin_bottom) {
1703 terminal->row = terminal->margin_bottom;
1704 terminal_scroll(terminal, +1);
1705 }
1706 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001707 case 'c': /* RIS */
1708 terminal_init(terminal);
1709 break;
1710 case 'H': /* HTS */
1711 terminal->tab_ruler[terminal->column] = 1;
1712 break;
1713 case '7': /* DECSC */
1714 terminal->saved_row = terminal->row;
1715 terminal->saved_column = terminal->column;
1716 terminal->saved_attr = terminal->curr_attr;
1717 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001718 terminal->saved_cs = terminal->cs;
1719 terminal->saved_g0 = terminal->g0;
1720 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001721 break;
1722 case '8': /* DECRC */
1723 terminal->row = terminal->saved_row;
1724 terminal->column = terminal->saved_column;
1725 terminal->curr_attr = terminal->saved_attr;
1726 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001727 terminal->cs = terminal->saved_cs;
1728 terminal->g0 = terminal->saved_g0;
1729 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001730 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001731 case '=': /* DECPAM */
1732 terminal->key_mode = KM_APPLICATION;
1733 break;
1734 case '>': /* DECPNM */
1735 terminal->key_mode = KM_NORMAL;
1736 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001737 default:
1738 fprintf(stderr, "Unknown escape code: %c\n", code);
1739 break;
1740 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001741}
1742
1743static void
1744handle_special_escape(struct terminal *terminal, char special, char code)
1745{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001746 int i, numChars;
1747
1748 if (special == '#') {
1749 switch(code) {
1750 case '8':
1751 /* fill with 'E', no cheap way to do this */
1752 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1753 numChars = terminal->width * terminal->height;
1754 for(i = 0; i < numChars; i++) {
1755 terminal->data[i].byte[0] = 'E';
1756 }
1757 break;
1758 default:
1759 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1760 break;
1761 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001762 } else if (special == '(' || special == ')') {
1763 switch(code) {
1764 case '0':
1765 if (special == '(')
1766 terminal->g0 = CS_SPECIAL;
1767 else
1768 terminal->g1 = CS_SPECIAL;
1769 break;
1770 case 'A':
1771 if (special == '(')
1772 terminal->g0 = CS_UK;
1773 else
1774 terminal->g1 = CS_UK;
1775 break;
1776 case 'B':
1777 if (special == '(')
1778 terminal->g0 = CS_US;
1779 else
1780 terminal->g1 = CS_US;
1781 break;
1782 default:
1783 fprintf(stderr, "Unknown character set %c\n", code);
1784 break;
1785 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001786 } else {
1787 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1788 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001789}
1790
1791static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001792handle_sgr(struct terminal *terminal, int code)
1793{
1794 switch(code) {
1795 case 0:
1796 terminal->curr_attr = terminal->color_scheme->default_attr;
1797 break;
1798 case 1:
1799 terminal->curr_attr.a |= ATTRMASK_BOLD;
1800 if (terminal->curr_attr.fg < 8)
1801 terminal->curr_attr.fg += 8;
1802 break;
1803 case 4:
1804 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1805 break;
1806 case 5:
1807 terminal->curr_attr.a |= ATTRMASK_BLINK;
1808 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001809 case 8:
1810 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1811 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001812 case 2:
1813 case 21:
1814 case 22:
1815 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1816 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1817 terminal->curr_attr.fg -= 8;
1818 break;
1819 case 24:
1820 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1821 break;
1822 case 25:
1823 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1824 break;
1825 case 7:
1826 case 26:
1827 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1828 break;
1829 case 27:
1830 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1831 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001832 case 28:
1833 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1834 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001835 case 39:
1836 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1837 break;
1838 case 49:
1839 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1840 break;
1841 default:
1842 if(code >= 30 && code <= 37) {
1843 terminal->curr_attr.fg = code - 30;
1844 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1845 terminal->curr_attr.fg += 8;
1846 } else if(code >= 40 && code <= 47) {
1847 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001848 } else if (code >= 90 && code <= 97) {
1849 terminal->curr_attr.fg = code - 90 + 8;
1850 } else if (code >= 100 && code <= 107) {
1851 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001852 } else if(code >= 256 && code < 512) {
1853 terminal->curr_attr.fg = code - 256;
1854 } else if(code >= 512 && code < 768) {
1855 terminal->curr_attr.bg = code - 512;
1856 } else {
1857 fprintf(stderr, "Unknown SGR code: %d\n", code);
1858 }
1859 break;
1860 }
1861}
1862
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001863/* Returns 1 if c was special, otherwise 0 */
1864static int
1865handle_special_char(struct terminal *terminal, char c)
1866{
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001867 union utf8_char *row;
1868 struct attr *attr_row;
1869
1870 row = terminal_get_row(terminal, terminal->row);
1871 attr_row = terminal_get_attr_row(terminal, terminal->row);
1872
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001873 switch(c) {
1874 case '\r':
1875 terminal->column = 0;
1876 break;
1877 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001878 if (terminal->mode & MODE_LF_NEWLINE) {
1879 terminal->column = 0;
1880 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001881 /* fallthrough */
1882 case '\v':
1883 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001884 terminal->row++;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07001885 if (terminal->row > terminal->margin_bottom) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001886 terminal->row = terminal->margin_bottom;
1887 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001888 }
1889
1890 break;
1891 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001892 while (terminal->column < terminal->width) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001893 if (terminal->mode & MODE_IRM)
1894 terminal_shift_line(terminal, +1);
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001895
1896 if (row[terminal->column].byte[0] == '\0') {
1897 row[terminal->column].byte[0] = ' ';
1898 row[terminal->column].byte[1] = '\0';
1899 attr_row[terminal->column] = terminal->curr_attr;
1900 }
1901
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001902 terminal->column++;
Kristian Høgsbergcca3c2f2012-06-20 15:56:13 -04001903 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001904 }
1905 if (terminal->column >= terminal->width) {
1906 terminal->column = terminal->width - 1;
1907 }
1908
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001909 break;
1910 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001911 if (terminal->column >= terminal->width) {
1912 terminal->column = terminal->width - 2;
1913 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001914 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001915 } else if (terminal->mode & MODE_AUTOWRAP) {
1916 terminal->column = terminal->width - 1;
1917 terminal->row -= 1;
1918 if (terminal->row < terminal->margin_top) {
1919 terminal->row = terminal->margin_top;
1920 terminal_scroll(terminal, -1);
1921 }
1922 }
1923
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001924 break;
1925 case '\a':
1926 /* Bell */
1927 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001928 case '\x0E': /* SO */
1929 terminal->cs = terminal->g1;
1930 break;
1931 case '\x0F': /* SI */
1932 terminal->cs = terminal->g0;
1933 break;
Kristian Høgsberg2a1aa4e2012-08-03 09:37:05 -04001934 case '\0':
1935 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001936 default:
1937 return 0;
1938 }
1939
1940 return 1;
1941}
1942
1943static void
1944handle_char(struct terminal *terminal, union utf8_char utf8)
1945{
1946 union utf8_char *row;
1947 struct attr *attr_row;
1948
1949 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001950
1951 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001952
1953 /* There are a whole lot of non-characters, control codes,
1954 * and formatting codes that should probably be ignored,
1955 * for example: */
1956 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1957 /* BOM, ignore */
1958 return;
1959 }
1960
1961 /* Some of these non-characters should be translated, e.g.: */
1962 if (utf8.byte[0] < 32) {
1963 utf8.byte[0] = utf8.byte[0] + 64;
1964 }
1965
1966 /* handle right margin effects */
1967 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001968 if (terminal->mode & MODE_AUTOWRAP) {
1969 terminal->column = 0;
1970 terminal->row += 1;
1971 if (terminal->row > terminal->margin_bottom) {
1972 terminal->row = terminal->margin_bottom;
1973 terminal_scroll(terminal, +1);
1974 }
1975 } else {
1976 terminal->column--;
1977 }
1978 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001979
1980 row = terminal_get_row(terminal, terminal->row);
1981 attr_row = terminal_get_attr_row(terminal, terminal->row);
1982
Callum Lowcay69e96582011-01-07 19:47:00 +00001983 if (terminal->mode & MODE_IRM)
1984 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001985 row[terminal->column] = utf8;
1986 attr_row[terminal->column++] = terminal->curr_attr;
1987
Kristian Høgsberg1d781ee2013-11-24 16:54:12 -08001988 if (terminal->row + terminal->start + 1 > terminal->end)
1989 terminal->end = terminal->row + terminal->start + 1;
1990 if (terminal->end == terminal->buffer_height)
1991 terminal->log_size = terminal->buffer_height;
1992 else if (terminal->log_size < terminal->buffer_height)
1993 terminal->log_size = terminal->end;
1994
Peng Wucfcc1112013-08-19 10:59:21 +08001995 /* cursor jump for wide character. */
1996 if (is_wide(utf8))
1997 row[terminal->column++].ch = 0x200B; /* space glyph */
1998
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001999 if (utf8.ch != terminal->last_char.ch)
2000 terminal->last_char = utf8;
2001}
2002
Callum Lowcay30eeae52011-01-07 19:46:55 +00002003static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13002004escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
2005{
2006 int len, i;
2007
2008 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
2009 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
2010 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
2011 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
2012 else len = 1; /* Invalid, cannot happen */
2013
2014 if (terminal->escape_length + len <= MAX_ESCAPE) {
2015 for (i = 0; i < len; i++)
2016 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
2017 terminal->escape_length += len;
2018 } else if (terminal->escape_length < MAX_ESCAPE) {
2019 terminal->escape[terminal->escape_length++] = 0;
2020 }
2021}
2022
2023static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002024terminal_data(struct terminal *terminal, const char *data, size_t length)
2025{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002026 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002027 union utf8_char utf8;
2028 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002029
2030 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002031 parser_state =
2032 utf8_next_char(&terminal->state_machine, data[i]);
2033 switch(parser_state) {
2034 case utf8state_accept:
2035 utf8.ch = terminal->state_machine.s.ch;
2036 break;
2037 case utf8state_reject:
2038 /* the unicode replacement character */
2039 utf8.byte[0] = 0xEF;
2040 utf8.byte[1] = 0xBF;
2041 utf8.byte[2] = 0xBD;
2042 utf8.byte[3] = 0x00;
2043 break;
2044 default:
2045 continue;
2046 }
2047
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002048 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13002049 switch (terminal->state) {
2050 case escape_state_escape:
2051 escape_append_utf8(terminal, utf8);
2052 switch (utf8.byte[0]) {
2053 case 'P': /* DCS */
2054 terminal->state = escape_state_dcs;
2055 break;
2056 case '[': /* CSI */
2057 terminal->state = escape_state_csi;
2058 break;
2059 case ']': /* OSC */
2060 terminal->state = escape_state_osc;
2061 break;
2062 case '#':
2063 case '(':
2064 case ')': /* special */
2065 terminal->state = escape_state_special;
2066 break;
2067 case '^': /* PM (not implemented) */
2068 case '_': /* APC (not implemented) */
2069 terminal->state = escape_state_ignore;
2070 break;
2071 default:
2072 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002073 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13002074 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002075 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002076 continue;
2077 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002078 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
2079 /* do nothing */
2080 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002081 terminal->escape_flags |= ESC_FLAG_WHAT;
2082 } else if (utf8.byte[0] == '>') {
2083 terminal->escape_flags |= ESC_FLAG_GT;
2084 } else if (utf8.byte[0] == '!') {
2085 terminal->escape_flags |= ESC_FLAG_BANG;
2086 } else if (utf8.byte[0] == '$') {
2087 terminal->escape_flags |= ESC_FLAG_CASH;
2088 } else if (utf8.byte[0] == '\'') {
2089 terminal->escape_flags |= ESC_FLAG_SQUOTE;
2090 } else if (utf8.byte[0] == '"') {
2091 terminal->escape_flags |= ESC_FLAG_DQUOTE;
2092 } else if (utf8.byte[0] == ' ') {
2093 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002094 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002095 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002096 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13002097 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002098 }
2099
2100 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
2101 utf8.byte[0] == '`')
2102 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002103 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002104 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002105 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002106 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002107 continue;
2108 case escape_state_inner_escape:
2109 if (utf8.byte[0] == '\\') {
2110 terminal->state = escape_state_normal;
2111 if (terminal->outer_state == escape_state_dcs) {
2112 handle_dcs(terminal);
2113 } else if (terminal->outer_state == escape_state_osc) {
2114 handle_osc(terminal);
2115 }
2116 } else if (utf8.byte[0] == '\e') {
2117 terminal->state = terminal->outer_state;
2118 escape_append_utf8(terminal, utf8);
2119 if (terminal->escape_length >= MAX_ESCAPE)
2120 terminal->state = escape_state_normal;
2121 } else {
2122 terminal->state = terminal->outer_state;
2123 if (terminal->escape_length < MAX_ESCAPE)
2124 terminal->escape[terminal->escape_length++] = '\e';
2125 escape_append_utf8(terminal, utf8);
2126 if (terminal->escape_length >= MAX_ESCAPE)
2127 terminal->state = escape_state_normal;
2128 }
2129 continue;
2130 case escape_state_dcs:
2131 case escape_state_osc:
2132 case escape_state_ignore:
2133 if (utf8.byte[0] == '\e') {
2134 terminal->outer_state = terminal->state;
2135 terminal->state = escape_state_inner_escape;
2136 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
2137 terminal->state = escape_state_normal;
2138 handle_osc(terminal);
2139 } else {
2140 escape_append_utf8(terminal, utf8);
2141 if (terminal->escape_length >= MAX_ESCAPE)
2142 terminal->state = escape_state_normal;
2143 }
2144 continue;
2145 case escape_state_special:
2146 escape_append_utf8(terminal, utf8);
2147 terminal->state = escape_state_normal;
2148 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2149 handle_special_escape(terminal, terminal->escape[1],
2150 utf8.byte[0]);
2151 }
2152 continue;
2153 default:
2154 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002155 }
2156
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002157 /* this is valid, because ASCII characters are never used to
2158 * introduce a multibyte sequence in UTF-8 */
2159 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002160 terminal->state = escape_state_escape;
2161 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002162 terminal->escape[0] = '\e';
2163 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002164 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002165 } else {
2166 handle_char(terminal, utf8);
2167 } /* if */
2168 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002169
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002170 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002171}
2172
2173static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002174data_source_target(void *data,
2175 struct wl_data_source *source, const char *mime_type)
2176{
2177 fprintf(stderr, "data_source_target, %s\n", mime_type);
2178}
2179
2180static void
2181data_source_send(void *data,
2182 struct wl_data_source *source,
2183 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002184{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002185 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002186
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002187 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002188}
2189
2190static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002191data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002192{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002193 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002194}
2195
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002196static const struct wl_data_source_listener data_source_listener = {
2197 data_source_target,
2198 data_source_send,
2199 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002200};
2201
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07002202static const char text_mime_type[] = "text/plain;charset=utf-8";
2203
2204static void
2205data_handler(struct window *window,
2206 struct input *input,
2207 float x, float y, const char **types, void *data)
2208{
2209 int i, has_text = 0;
2210
2211 if (!types)
2212 return;
2213 for (i = 0; types[i]; i++)
2214 if (strcmp(types[i], text_mime_type) == 0)
2215 has_text = 1;
2216
2217 if (!has_text) {
2218 input_accept(input, NULL);
2219 } else {
2220 input_accept(input, text_mime_type);
2221 }
2222}
2223
2224static void
2225drop_handler(struct window *window, struct input *input,
2226 int32_t x, int32_t y, void *data)
2227{
2228 struct terminal *terminal = data;
2229
2230 input_receive_drag_data_to_fd(input, text_mime_type, terminal->master);
2231}
2232
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002233static void
2234fullscreen_handler(struct window *window, void *data)
2235{
2236 struct terminal *terminal = data;
2237
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002238 window_set_fullscreen(window, !window_is_fullscreen(terminal->window));
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002239}
2240
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002241static void
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002242close_handler(void *data)
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002243{
2244 struct terminal *terminal = data;
2245
2246 terminal_destroy(terminal);
2247}
2248
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002249static void
2250terminal_copy(struct terminal *terminal, struct input *input)
2251{
2252 terminal->selection =
2253 display_create_data_source(terminal->display);
2254 wl_data_source_offer(terminal->selection,
2255 "text/plain;charset=utf-8");
2256 wl_data_source_add_listener(terminal->selection,
2257 &data_source_listener, terminal);
2258 input_set_selection(input, terminal->selection,
2259 display_get_serial(terminal->display));
2260}
2261
2262static void
2263terminal_paste(struct terminal *terminal, struct input *input)
2264{
2265 input_receive_selection_data_to_fd(input,
2266 "text/plain;charset=utf-8",
2267 terminal->master);
2268
2269}
2270
2271static void
2272terminal_new_instance(struct terminal *terminal)
2273{
2274 struct terminal *new_terminal;
2275
2276 new_terminal = terminal_create(terminal->display);
2277 if (terminal_run(new_terminal, option_shell))
2278 terminal_destroy(new_terminal);
2279}
2280
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002281static int
2282handle_bound_key(struct terminal *terminal,
2283 struct input *input, uint32_t sym, uint32_t time)
2284{
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002285 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002286 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002287 /* Cut selection; terminal doesn't do cut, fall
2288 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002289 case XKB_KEY_C:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002290 terminal_copy(terminal, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002291 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002292 case XKB_KEY_V:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002293 terminal_paste(terminal, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002294 return 1;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002295 case XKB_KEY_N:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002296 terminal_new_instance(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002297 return 1;
2298
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002299 case XKB_KEY_Up:
2300 if (!terminal->scrolling)
2301 terminal->saved_start = terminal->start;
2302 if (terminal->start == terminal->end - terminal->log_size)
2303 return 1;
2304
2305 terminal->scrolling = 1;
2306 terminal->start--;
2307 terminal->row++;
2308 terminal->selection_start_row++;
2309 terminal->selection_end_row++;
2310 widget_schedule_redraw(terminal->widget);
2311 return 1;
2312
2313 case XKB_KEY_Down:
2314 if (!terminal->scrolling)
2315 terminal->saved_start = terminal->start;
2316
2317 if (terminal->start == terminal->saved_start)
2318 return 1;
2319
2320 terminal->scrolling = 1;
2321 terminal->start++;
2322 terminal->row--;
2323 terminal->selection_start_row--;
2324 terminal->selection_end_row--;
2325 widget_schedule_redraw(terminal->widget);
2326 return 1;
2327
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002328 default:
2329 return 0;
2330 }
2331}
2332
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002333static void
2334key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002335 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2336 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002337{
2338 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002339 char ch[MAX_RESPONSE];
Kristian Høgsberg88fd4082012-06-21 15:55:03 -04002340 uint32_t modifiers, serial;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002341 int ret, len = 0, d;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002342 bool convert_utf8 = true;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002343
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002344 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002345 if ((modifiers & MOD_CONTROL_MASK) &&
2346 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002347 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2348 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002349 return;
2350
Daniel Stonea8468712012-11-07 17:51:35 +11002351 /* Map keypad symbols to 'normal' equivalents before processing */
2352 switch (sym) {
2353 case XKB_KEY_KP_Space:
2354 sym = XKB_KEY_space;
2355 break;
2356 case XKB_KEY_KP_Tab:
2357 sym = XKB_KEY_Tab;
2358 break;
2359 case XKB_KEY_KP_Enter:
2360 sym = XKB_KEY_Return;
2361 break;
2362 case XKB_KEY_KP_Left:
2363 sym = XKB_KEY_Left;
2364 break;
2365 case XKB_KEY_KP_Up:
2366 sym = XKB_KEY_Up;
2367 break;
2368 case XKB_KEY_KP_Right:
2369 sym = XKB_KEY_Right;
2370 break;
2371 case XKB_KEY_KP_Down:
2372 sym = XKB_KEY_Down;
2373 break;
2374 case XKB_KEY_KP_Equal:
2375 sym = XKB_KEY_equal;
2376 break;
2377 case XKB_KEY_KP_Multiply:
2378 sym = XKB_KEY_asterisk;
2379 break;
2380 case XKB_KEY_KP_Add:
2381 sym = XKB_KEY_plus;
2382 break;
2383 case XKB_KEY_KP_Separator:
2384 /* Note this is actually locale-dependent and should mostly be
2385 * a comma. But leave it as period until we one day start
2386 * doing the right thing. */
2387 sym = XKB_KEY_period;
2388 break;
2389 case XKB_KEY_KP_Subtract:
2390 sym = XKB_KEY_minus;
2391 break;
2392 case XKB_KEY_KP_Decimal:
2393 sym = XKB_KEY_period;
2394 break;
2395 case XKB_KEY_KP_Divide:
2396 sym = XKB_KEY_slash;
2397 break;
2398 case XKB_KEY_KP_0:
2399 case XKB_KEY_KP_1:
2400 case XKB_KEY_KP_2:
2401 case XKB_KEY_KP_3:
2402 case XKB_KEY_KP_4:
2403 case XKB_KEY_KP_5:
2404 case XKB_KEY_KP_6:
2405 case XKB_KEY_KP_7:
2406 case XKB_KEY_KP_8:
2407 case XKB_KEY_KP_9:
2408 sym = (sym - XKB_KEY_KP_0) + XKB_KEY_0;
2409 break;
2410 default:
2411 break;
2412 }
2413
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002414 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002415 case XKB_KEY_BackSpace:
Kristian Høgsbergb7f94bf2012-06-21 12:29:36 -04002416 if (modifiers & MOD_ALT_MASK)
2417 ch[len++] = 0x1b;
Kristian Høgsberg71a4cf42012-06-20 17:57:56 -04002418 ch[len++] = 0x7f;
2419 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002420 case XKB_KEY_Tab:
2421 case XKB_KEY_Linefeed:
2422 case XKB_KEY_Clear:
2423 case XKB_KEY_Pause:
2424 case XKB_KEY_Scroll_Lock:
2425 case XKB_KEY_Sys_Req:
2426 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002427 ch[len++] = sym & 0x7f;
2428 break;
2429
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002430 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002431 if (terminal->mode & MODE_LF_NEWLINE) {
2432 ch[len++] = 0x0D;
2433 ch[len++] = 0x0A;
2434 } else {
2435 ch[len++] = 0x0D;
2436 }
2437 break;
2438
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002439 case XKB_KEY_Shift_L:
2440 case XKB_KEY_Shift_R:
2441 case XKB_KEY_Control_L:
2442 case XKB_KEY_Control_R:
2443 case XKB_KEY_Alt_L:
2444 case XKB_KEY_Alt_R:
Kristian Høgsberg22fbcf72012-06-22 12:18:56 -04002445 case XKB_KEY_Meta_L:
2446 case XKB_KEY_Meta_R:
2447 case XKB_KEY_Super_L:
2448 case XKB_KEY_Super_R:
2449 case XKB_KEY_Hyper_L:
2450 case XKB_KEY_Hyper_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002451 break;
2452
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002453 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002454 len = function_key_response('[', 2, modifiers, '~', ch);
2455 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002456 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002457 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2458 ch[len++] = '\x04';
2459 } else {
2460 len = function_key_response('[', 3, modifiers, '~', ch);
2461 }
2462 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002463 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002464 len = function_key_response('[', 5, modifiers, '~', ch);
2465 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002466 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002467 len = function_key_response('[', 6, modifiers, '~', ch);
2468 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002469 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002470 len = function_key_response('O', 1, modifiers, 'P', ch);
2471 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002472 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002473 len = function_key_response('O', 1, modifiers, 'Q', ch);
2474 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002475 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002476 len = function_key_response('O', 1, modifiers, 'R', ch);
2477 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002478 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002479 len = function_key_response('O', 1, modifiers, 'S', ch);
2480 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002481 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002482 len = function_key_response('[', 15, modifiers, '~', ch);
2483 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002484 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002485 len = function_key_response('[', 17, modifiers, '~', ch);
2486 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002487 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002488 len = function_key_response('[', 18, modifiers, '~', ch);
2489 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002490 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002491 len = function_key_response('[', 19, modifiers, '~', ch);
2492 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002493 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002494 len = function_key_response('[', 20, modifiers, '~', ch);
2495 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002496 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002497 len = function_key_response('[', 21, modifiers, '~', ch);
2498 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002499 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002500 len = function_key_response('[', 24, modifiers, '~', ch);
2501 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002502 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002503 /* Handle special keys with alternate mappings */
2504 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2505 if (len != 0) break;
2506
Kristian Høgsberg70163132012-05-08 15:55:39 -04002507 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002508 if (sym >= '3' && sym <= '7')
2509 sym = (sym & 0x1f) + 8;
2510
2511 if (!((sym >= '!' && sym <= '/') ||
2512 (sym >= '8' && sym <= '?') ||
2513 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2514 else if (sym == '2') sym = 0x00;
2515 else if (sym == '/') sym = 0x1F;
2516 else if (sym == '8' || sym == '?') sym = 0x7F;
Kristian Høgsbergae9e0732012-06-21 12:30:15 -04002517 }
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002518 if (modifiers & MOD_ALT_MASK) {
2519 if (terminal->mode & MODE_ALT_SENDS_ESC) {
2520 ch[len++] = 0x1b;
2521 } else {
2522 sym = sym | 0x80;
2523 convert_utf8 = false;
2524 }
Callum Lowcay7e08e902011-01-07 19:47:02 +00002525 }
2526
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002527 if ((sym < 128) ||
2528 (!convert_utf8 && sym < 256)) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002529 ch[len++] = sym;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002530 } else {
2531 ret = xkb_keysym_to_utf8(sym, ch + len,
2532 MAX_RESPONSE - len);
2533 if (ret < 0)
2534 fprintf(stderr,
2535 "Warning: buffer too small to encode "
2536 "UTF8 character\n");
2537 else
2538 len += ret;
2539 }
2540
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002541 break;
2542 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002543
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002544 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002545 if (terminal->scrolling) {
2546 d = terminal->saved_start - terminal->start;
2547 terminal->row -= d;
2548 terminal->selection_start_row -= d;
2549 terminal->selection_end_row -= d;
2550 terminal->start = terminal->saved_start;
2551 terminal->scrolling = 0;
2552 widget_schedule_redraw(terminal->widget);
2553 }
2554
Kristian Høgsberg26130862011-08-24 11:30:21 -04002555 terminal_write(terminal, ch, len);
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002556
2557 /* Hide cursor, except if this was coming from a
2558 * repeating key press. */
2559 serial = display_get_serial(terminal->display);
2560 if (terminal->hide_cursor_serial != serial) {
2561 input_set_pointer_image(input, CURSOR_BLANK);
2562 terminal->hide_cursor_serial = serial;
2563 }
2564 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002565}
2566
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002567static void
2568keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002569 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002570{
2571 struct terminal *terminal = data;
2572
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002573 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002574}
2575
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002576static int wordsep(int ch)
2577{
2578 const char extra[] = "-,./?%&#:_=+@~";
2579
Andre Heider552d12b2012-08-02 20:59:43 +02002580 if (ch > 127)
2581 return 1;
2582
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002583 return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
2584}
2585
2586static int
2587recompute_selection(struct terminal *terminal)
2588{
2589 struct rectangle allocation;
2590 int col, x, width, height;
2591 int start_row, end_row;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002592 int word_start, eol;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002593 int side_margin, top_margin;
2594 int start_x, end_x;
2595 int cw, ch;
2596 union utf8_char *data;
2597
Peng Wuf291f202013-06-06 15:32:41 +08002598 cw = terminal->average_width;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002599 ch = terminal->extents.height;
2600 widget_get_allocation(terminal->widget, &allocation);
2601 width = terminal->width * cw;
2602 height = terminal->height * ch;
2603 side_margin = allocation.x + (allocation.width - width) / 2;
2604 top_margin = allocation.y + (allocation.height - height) / 2;
2605
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002606 start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1;
2607 end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1;
2608
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002609 if (start_row < end_row ||
2610 (start_row == end_row &&
2611 terminal->selection_start_x < terminal->selection_end_x)) {
2612 terminal->selection_start_row = start_row;
2613 terminal->selection_end_row = end_row;
2614 start_x = terminal->selection_start_x;
2615 end_x = terminal->selection_end_x;
2616 } else {
2617 terminal->selection_start_row = end_row;
2618 terminal->selection_end_row = start_row;
2619 start_x = terminal->selection_end_x;
2620 end_x = terminal->selection_start_x;
2621 }
2622
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002623 eol = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002624 if (terminal->selection_start_row < 0) {
2625 terminal->selection_start_row = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002626 terminal->selection_start_col = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002627 } else {
2628 x = side_margin + cw / 2;
2629 data = terminal_get_row(terminal,
2630 terminal->selection_start_row);
2631 word_start = 0;
2632 for (col = 0; col < terminal->width; col++, x += cw) {
2633 if (col == 0 || wordsep(data[col - 1].ch))
2634 word_start = col;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002635 if (data[col].ch != 0)
2636 eol = col + 1;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002637 if (start_x < x)
2638 break;
2639 }
2640
2641 switch (terminal->dragging) {
2642 case SELECT_LINE:
2643 terminal->selection_start_col = 0;
2644 break;
2645 case SELECT_WORD:
2646 terminal->selection_start_col = word_start;
2647 break;
2648 case SELECT_CHAR:
2649 terminal->selection_start_col = col;
2650 break;
2651 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002652 }
2653
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002654 if (terminal->selection_end_row >= terminal->height) {
2655 terminal->selection_end_row = terminal->height;
2656 terminal->selection_end_col = 0;
2657 } else {
2658 x = side_margin + cw / 2;
2659 data = terminal_get_row(terminal, terminal->selection_end_row);
2660 for (col = 0; col < terminal->width; col++, x += cw) {
2661 if (terminal->dragging == SELECT_CHAR && end_x < x)
2662 break;
2663 if (terminal->dragging == SELECT_WORD &&
2664 end_x < x && wordsep(data[col].ch))
2665 break;
2666 }
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002667 terminal->selection_end_col = col;
2668 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002669
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002670 if (terminal->selection_end_col != terminal->selection_start_col ||
2671 terminal->selection_start_row != terminal->selection_end_row) {
2672 col = terminal->selection_end_col;
2673 if (col > 0 && data[col - 1].ch == 0)
2674 terminal->selection_end_col = terminal->width;
2675 data = terminal_get_row(terminal, terminal->selection_start_row);
2676 if (data[terminal->selection_start_col].ch == 0)
2677 terminal->selection_start_col = eol;
2678 }
2679
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002680 return 1;
2681}
2682
Kristian Høgsberg59826582011-01-20 11:56:57 -05002683static void
Jasper St. Pierredda93132014-03-13 12:06:00 -04002684menu_func(void *data, struct input *input, int index)
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002685{
Jasper St. Pierredda93132014-03-13 12:06:00 -04002686 struct window *window = data;
2687 struct terminal *terminal = window_get_user_data(window);
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002688
2689 fprintf(stderr, "picked entry %d\n", index);
2690
2691 switch (index) {
2692 case 0:
2693 terminal_new_instance(terminal);
2694 break;
2695 case 1:
2696 terminal_copy(terminal, input);
2697 break;
2698 case 2:
2699 terminal_paste(terminal, input);
2700 break;
2701 }
2702}
2703
2704static void
2705show_menu(struct terminal *terminal, struct input *input, uint32_t time)
2706{
2707 int32_t x, y;
2708 static const char *entries[] = {
2709 "Open Terminal", "Copy", "Paste"
2710 };
2711
2712 input_get_position(input, &x, &y);
2713 window_show_menu(terminal->display, input, time, terminal->window,
2714 x - 10, y - 10, menu_func,
2715 entries, ARRAY_LENGTH(entries));
2716}
2717
2718static void
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002719click_handler(struct widget *widget, struct terminal *terminal,
2720 struct input *input, int32_t x, int32_t y,
2721 uint32_t time)
2722{
2723 if (time - terminal->click_time < 500)
2724 terminal->click_count++;
2725 else
2726 terminal->click_count = 1;
2727
2728 terminal->click_time = time;
2729 terminal->dragging = (terminal->click_count - 1) % 3 + SELECT_CHAR;
2730
2731 terminal->selection_end_x = terminal->selection_start_x = x;
2732 terminal->selection_end_y = terminal->selection_start_y = y;
2733 if (recompute_selection(terminal))
2734 widget_schedule_redraw(widget);
2735}
2736
2737static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002738button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002739 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002740 uint32_t button,
2741 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002742{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002743 struct terminal *terminal = data;
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002744 int32_t x, y;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002745
2746 switch (button) {
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002747 case BTN_LEFT:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002748 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002749 input_get_position(input, &x, &y);
2750 click_handler(widget, terminal, input, x, y, time);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002751 } else {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002752 terminal->dragging = SELECT_NONE;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002753 }
2754 break;
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002755
2756 case BTN_RIGHT:
2757 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
2758 show_menu(terminal, input, time);
2759 break;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002760 }
2761}
2762
2763static int
Kristian Høgsberg29784402012-06-28 14:27:02 -04002764enter_handler(struct widget *widget,
2765 struct input *input, float x, float y, void *data)
2766{
2767 return CURSOR_IBEAM;
2768}
2769
2770static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002771motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002772 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002773 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002774{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002775 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002776
2777 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002778 input_get_position(input,
2779 &terminal->selection_end_x,
2780 &terminal->selection_end_y);
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002781
2782 if (recompute_selection(terminal))
2783 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002784 }
2785
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002786 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002787}
2788
Magnus Hoff1046f122014-08-05 15:05:59 +02002789/* This magnitude is chosen rather arbitrarily. Really, the scrolling
2790 * should happen on a (fractional) pixel basis, not a line basis. */
2791#define AXIS_UNITS_PER_LINE 256
2792
2793static void
2794axis_handler(struct widget *widget,
2795 struct input *input, uint32_t time,
2796 uint32_t axis,
2797 wl_fixed_t value,
2798 void *data)
2799{
2800 struct terminal *terminal = data;
2801 int lines;
2802
2803 if (axis != WL_POINTER_AXIS_VERTICAL_SCROLL)
2804 return;
2805
2806 terminal->smooth_scroll += value;
2807 lines = terminal->smooth_scroll / AXIS_UNITS_PER_LINE;
2808 terminal->smooth_scroll -= lines * AXIS_UNITS_PER_LINE;
2809
2810 if (lines > 0) {
2811 if (terminal->scrolling) {
2812 if ((uint32_t)lines > terminal->saved_start - terminal->start)
2813 lines = terminal->saved_start - terminal->start;
2814 } else {
2815 lines = 0;
2816 }
2817 } else if (lines < 0) {
2818 uint32_t neg_lines = -lines;
2819
2820 if (neg_lines > terminal->log_size + terminal->start - terminal->end)
2821 lines = terminal->end - terminal->log_size - terminal->start;
2822 }
2823
2824 if (lines) {
2825 if (!terminal->scrolling)
2826 terminal->saved_start = terminal->start;
2827 terminal->scrolling = 1;
2828
2829 terminal->start += lines;
2830 terminal->row -= lines;
2831 terminal->selection_start_row -= lines;
2832 terminal->selection_end_row -= lines;
2833
2834 widget_schedule_redraw(widget);
2835 }
2836}
2837
Alexander Larssonde79dd02013-05-22 14:41:34 +02002838static void
2839output_handler(struct window *window, struct output *output, int enter,
2840 void *data)
2841{
2842 if (enter)
2843 window_set_buffer_transform(window, output_get_transform(output));
2844 window_set_buffer_scale(window, window_get_output_scale(window));
2845 window_schedule_redraw(window);
2846}
2847
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002848static void
2849touch_down_handler(struct widget *widget, struct input *input,
2850 uint32_t serial, uint32_t time, int32_t id,
2851 float x, float y, void *data)
2852{
2853 struct terminal *terminal = data;
2854
2855 if (id == 0)
2856 click_handler(widget, terminal, input, x, y, time);
2857}
2858
2859static void
2860touch_up_handler(struct widget *widget, struct input *input,
2861 uint32_t serial, uint32_t time, int32_t id, void *data)
2862{
2863 struct terminal *terminal = data;
2864
2865 if (id == 0)
2866 terminal->dragging = SELECT_NONE;
2867}
2868
2869static void
2870touch_motion_handler(struct widget *widget, struct input *input,
2871 uint32_t time, int32_t id, float x, float y, void *data)
2872{
2873 struct terminal *terminal = data;
2874
2875 if (terminal->dragging &&
2876 id == 0) {
2877 terminal->selection_end_x = (int)x;
2878 terminal->selection_end_y = (int)y;
2879
2880 if (recompute_selection(terminal))
2881 widget_schedule_redraw(widget);
2882 }
2883}
2884
Peng Wuf291f202013-06-06 15:32:41 +08002885#ifndef howmany
2886#define howmany(x, y) (((x) + ((y) - 1)) / (y))
2887#endif
2888
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002889static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002890terminal_create(struct display *display)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002891{
2892 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002893 cairo_surface_t *surface;
2894 cairo_t *cr;
Peng Wuf291f202013-06-06 15:32:41 +08002895 cairo_text_extents_t text_extents;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002896
Peter Huttererf3d62272013-08-08 11:57:05 +10002897 terminal = xzalloc(sizeof *terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002898 terminal->color_scheme = &DEFAULT_COLORS;
2899 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002900 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002901 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002902 terminal->window = window_create(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002903 terminal->widget = window_frame_create(terminal->window, terminal);
U. Artie Eoff09827e22014-01-15 11:40:37 -08002904 terminal->title = xstrdup("Wayland Terminal");
Kristian Høgsberga83be202013-10-23 20:47:35 -07002905 window_set_title(terminal->window, terminal->title);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002906 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002907
2908 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002909 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002910
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002911 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002912 terminal->margin = 5;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002913 terminal->buffer_height = 1024;
2914 terminal->end = 1;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002915
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002916 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002917 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002918 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002919 keyboard_focus_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002920 window_set_fullscreen_handler(terminal->window, fullscreen_handler);
Alexander Larssonde79dd02013-05-22 14:41:34 +02002921 window_set_output_handler(terminal->window, output_handler);
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002922 window_set_close_handler(terminal->window, close_handler);
Jasper St. Pierrede680992014-04-10 17:23:49 -07002923 window_set_state_changed_handler(terminal->window, state_changed_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002924
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07002925 window_set_data_handler(terminal->window, data_handler);
2926 window_set_drop_handler(terminal->window, drop_handler);
2927
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002928 widget_set_redraw_handler(terminal->widget, redraw_handler);
2929 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002930 widget_set_button_handler(terminal->widget, button_handler);
Kristian Høgsberg29784402012-06-28 14:27:02 -04002931 widget_set_enter_handler(terminal->widget, enter_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002932 widget_set_motion_handler(terminal->widget, motion_handler);
Magnus Hoff1046f122014-08-05 15:05:59 +02002933 widget_set_axis_handler(terminal->widget, axis_handler);
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002934 widget_set_touch_up_handler(terminal->widget, touch_up_handler);
2935 widget_set_touch_down_handler(terminal->widget, touch_down_handler);
2936 widget_set_touch_motion_handler(terminal->widget, touch_motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002937
Kristian Høgsberg09531622010-06-14 23:22:15 -04002938 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2939 cr = cairo_create(surface);
Kristian Høgsberg38912df2012-06-27 17:52:23 -04002940 cairo_set_font_size(cr, option_font_size);
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002941 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002942 CAIRO_FONT_SLANT_NORMAL,
2943 CAIRO_FONT_WEIGHT_BOLD);
2944 terminal->font_bold = cairo_get_scaled_font (cr);
2945 cairo_scaled_font_reference(terminal->font_bold);
2946
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002947 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002948 CAIRO_FONT_SLANT_NORMAL,
2949 CAIRO_FONT_WEIGHT_NORMAL);
2950 terminal->font_normal = cairo_get_scaled_font (cr);
2951 cairo_scaled_font_reference(terminal->font_normal);
2952
Kristian Høgsberg09531622010-06-14 23:22:15 -04002953 cairo_font_extents(cr, &terminal->extents);
Peng Wuf291f202013-06-06 15:32:41 +08002954
2955 /* Compute the average ascii glyph width */
2956 cairo_text_extents(cr, TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS,
2957 &text_extents);
2958 terminal->average_width = howmany
2959 (text_extents.width,
2960 strlen(TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS));
2961 terminal->average_width = ceil(terminal->average_width);
2962
Kristian Høgsberg09531622010-06-14 23:22:15 -04002963 cairo_destroy(cr);
2964 cairo_surface_destroy(surface);
2965
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002966 terminal_resize(terminal, 20, 5); /* Set minimum size first */
Kristian Høgsberga1627922012-06-20 17:30:03 -04002967 terminal_resize(terminal, 80, 25);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002968
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002969 wl_list_insert(terminal_list.prev, &terminal->link);
2970
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002971 return terminal;
2972}
2973
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002974static void
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002975terminal_destroy(struct terminal *terminal)
2976{
Dima Ryazanova85292e2012-11-29 00:27:09 -08002977 display_unwatch_fd(terminal->display, terminal->master);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002978 window_destroy(terminal->window);
2979 close(terminal->master);
2980 wl_list_remove(&terminal->link);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002981
2982 if (wl_list_empty(&terminal_list))
Dima Ryazanova85292e2012-11-29 00:27:09 -08002983 display_exit(terminal->display);
2984
Kristian Høgsberga83be202013-10-23 20:47:35 -07002985 free(terminal->title);
Dima Ryazanova85292e2012-11-29 00:27:09 -08002986 free(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002987}
2988
2989static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002990io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002991{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002992 struct terminal *terminal =
2993 container_of(task, struct terminal, io_task);
2994 char buffer[256];
2995 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002996
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002997 if (events & EPOLLHUP) {
2998 terminal_destroy(terminal);
2999 return;
3000 }
Tim Wiederhakef71accc2011-01-19 23:14:33 +01003001
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003002 len = read(terminal->master, buffer, sizeof buffer);
3003 if (len < 0)
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003004 terminal_destroy(terminal);
3005 else
3006 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003007}
3008
3009static int
3010terminal_run(struct terminal *terminal, const char *path)
3011{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003012 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003013 pid_t pid;
3014
3015 pid = forkpty(&master, NULL, NULL, NULL);
3016 if (pid == 0) {
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04003017 setenv("TERM", option_term, 1);
3018 setenv("COLORTERM", option_term, 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003019 if (execl(path, path, NULL)) {
3020 printf("exec failed: %m\n");
3021 exit(EXIT_FAILURE);
3022 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003023 } else if (pid < 0) {
3024 fprintf(stderr, "failed to fork and create pty (%m).\n");
3025 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003026 }
3027
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05003028 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003029 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04003030 terminal->io_task.run = io_handler;
3031 display_watch_fd(terminal->display, terminal->master,
3032 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003033
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07003034 if (option_fullscreen)
3035 window_set_fullscreen(terminal->window, 1);
3036 else
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04003037 terminal_resize(terminal, 80, 24);
3038
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05003039 return 0;
3040}
3041
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003042static const struct weston_option terminal_options[] = {
3043 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04003044 { WESTON_OPTION_STRING, "font", 0, &option_font },
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003045 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05003046};
3047
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003048int main(int argc, char *argv[])
3049{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05003050 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003051 struct terminal *terminal;
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -07003052 struct weston_config *config;
3053 struct weston_config_section *s;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04003054
Peng Wucfcc1112013-08-19 10:59:21 +08003055 /* as wcwidth is locale-dependent,
3056 wcwidth needs setlocale call to function properly. */
3057 setlocale(LC_ALL, "");
3058
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003059 option_shell = getenv("SHELL");
3060 if (!option_shell)
3061 option_shell = "/bin/bash";
3062
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07003063 config = weston_config_parse("weston.ini");
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -07003064 s = weston_config_get_section(config, "terminal", NULL, NULL);
3065 weston_config_section_get_string(s, "font", &option_font, "mono");
3066 weston_config_section_get_int(s, "font-size", &option_font_size, 14);
3067 weston_config_section_get_string(s, "term", &option_term, "xterm");
3068 weston_config_destroy(config);
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003069
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003070 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02003071 if (d == NULL) {
3072 fprintf(stderr, "failed to create display: %m\n");
3073 return -1;
3074 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003075
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003076 wl_list_init(&terminal_list);
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04003077 terminal = terminal_create(d);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003078 if (terminal_run(terminal, option_shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003079 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003080
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003081 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003082
3083 return 0;
3084}