blob: beec877fceda74f5c34efb6db8a093cabfbd7f5a [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;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000450 int saved_row, saved_column;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700451 int scrolling;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600452 int send_cursor_position;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500453 int fd, master;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500454 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300455 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500456 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300457 enum escape_state state;
458 enum escape_state outer_state;
459 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000460 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500461 int margin;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400462 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000463 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400464 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +0800465 double average_width;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500466 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg88fd4082012-06-21 15:55:03 -0400467 uint32_t hide_cursor_serial;
Kristian Høgsbergb405a802014-02-05 14:44:04 -0800468 int size_in_title;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500469
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400470 struct wl_data_source *selection;
Xiong Zhang49c6aee2013-11-25 18:42:52 +0800471 uint32_t click_time;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400472 int dragging, click_count;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500473 int selection_start_x, selection_start_y;
474 int selection_end_x, selection_end_y;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400475 int selection_start_row, selection_start_col;
476 int selection_end_row, selection_end_col;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -0400477 struct wl_list link;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500478};
479
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000480/* Create default tab stops, every 8 characters */
481static void
482terminal_init_tabs(struct terminal *terminal)
483{
484 int i = 0;
485
486 while (i < terminal->width) {
487 if (i % 8 == 0)
488 terminal->tab_ruler[i] = 1;
489 else
490 terminal->tab_ruler[i] = 0;
491 i++;
492 }
493}
494
Callum Lowcay30eeae52011-01-07 19:46:55 +0000495static void
496terminal_init(struct terminal *terminal)
497{
498 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000499 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000500 terminal->mode = MODE_SHOW_CURSOR |
501 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000502 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000503 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000504
505 terminal->row = 0;
506 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000507
Callum Lowcay256e72f2011-01-07 19:47:01 +0000508 terminal->g0 = CS_US;
509 terminal->g1 = CS_US;
510 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000511 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000512
513 terminal->saved_g0 = terminal->g0;
514 terminal->saved_g1 = terminal->g1;
515 terminal->saved_cs = terminal->cs;
516
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000517 terminal->saved_attr = terminal->curr_attr;
518 terminal->saved_origin_mode = terminal->origin_mode;
519 terminal->saved_row = terminal->row;
520 terminal->saved_column = terminal->column;
521
522 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000523}
524
525static void
526init_color_table(struct terminal *terminal)
527{
528 int c, r;
529 struct terminal_color *color_table = terminal->color_table;
530
531 for (c = 0; c < 256; c ++) {
532 if (c < 16) {
533 color_table[c] = terminal->color_scheme->palette[c];
534 } else if (c < 232) {
535 r = c - 16;
536 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
537 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
538 color_table[c].r = ((double)(r % 6) / 6.0);
539 color_table[c].a = 1.0;
540 } else {
541 r = (c - 232) * 10 + 8;
542 color_table[c].r = ((double) r) / 256.0;
543 color_table[c].g = color_table[c].r;
544 color_table[c].b = color_table[c].r;
545 color_table[c].a = 1.0;
546 }
547 }
548}
549
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000550static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500551terminal_get_row(struct terminal *terminal, int row)
552{
553 int index;
554
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700555 index = (row + terminal->start) & (terminal->buffer_height - 1);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500556
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700557 return (void *) terminal->data + index * terminal->data_pitch;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500558}
559
Callum Lowcay30eeae52011-01-07 19:46:55 +0000560static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500561terminal_get_attr_row(struct terminal *terminal, int row)
562{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000563 int index;
564
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700565 index = (row + terminal->start) & (terminal->buffer_height - 1);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000566
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700567 return (void *) terminal->data_attr + index * terminal->attr_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000568}
569
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500570union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300571 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500572 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500573};
574
575static void
576terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500577 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500578{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500579 struct attr attr;
580 int foreground, background, tmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500581
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500582 decoded->attr.s = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400583 if (((row == terminal->selection_start_row &&
584 col >= terminal->selection_start_col) ||
585 row > terminal->selection_start_row) &&
586 ((row == terminal->selection_end_row &&
587 col < terminal->selection_end_col) ||
588 row < terminal->selection_end_row))
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500589 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500590
591 /* get the attributes for this character cell */
592 attr = terminal_get_attr_row(terminal, row)[col];
593 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500594 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500595 ((terminal->mode & MODE_SHOW_CURSOR) &&
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400596 window_has_focus(terminal->window) && terminal->row == row &&
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500597 terminal->column == col)) {
598 foreground = attr.bg;
599 background = attr.fg;
600 if (attr.a & ATTRMASK_BOLD) {
601 if (foreground <= 16) foreground |= 0x08;
602 if (background <= 16) background &= 0x07;
603 }
604 } else {
605 foreground = attr.fg;
606 background = attr.bg;
607 }
608
609 if (terminal->mode & MODE_INVERSE) {
610 tmp = foreground;
611 foreground = background;
612 background = tmp;
613 if (attr.a & ATTRMASK_BOLD) {
614 if (foreground <= 16) foreground |= 0x08;
615 if (background <= 16) background &= 0x07;
616 }
617 }
618
Callum Lowcay9d708b02011-01-12 20:06:17 +1300619 decoded->attr.fg = foreground;
620 decoded->attr.bg = background;
621 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000622}
623
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500624
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500625static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000626terminal_scroll_buffer(struct terminal *terminal, int d)
627{
628 int i;
629
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700630 terminal->start += d;
631 if (d < 0) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000632 d = 0 - d;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700633 for (i = 0; i < d; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000634 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
635 attr_init(terminal_get_attr_row(terminal, i),
636 terminal->curr_attr, terminal->width);
637 }
638 } else {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700639 for (i = terminal->height - d; i < terminal->height; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000640 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
641 attr_init(terminal_get_attr_row(terminal, i),
642 terminal->curr_attr, terminal->width);
643 }
644 }
Kristian Høgsberg18e928d2012-06-27 19:29:41 -0400645
646 terminal->selection_start_row -= d;
647 terminal->selection_end_row -= d;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000648}
649
650static void
651terminal_scroll_window(struct terminal *terminal, int d)
652{
653 int i;
654 int window_height;
655 int from_row, to_row;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000656
657 // scrolling range is inclusive
658 window_height = terminal->margin_bottom - terminal->margin_top + 1;
659 d = d % (window_height + 1);
660 if(d < 0) {
661 d = 0 - d;
662 to_row = terminal->margin_bottom;
663 from_row = terminal->margin_bottom - d;
664
665 for (i = 0; i < (window_height - d); i++) {
666 memcpy(terminal_get_row(terminal, to_row - i),
667 terminal_get_row(terminal, from_row - i),
668 terminal->data_pitch);
669 memcpy(terminal_get_attr_row(terminal, to_row - i),
670 terminal_get_attr_row(terminal, from_row - i),
671 terminal->attr_pitch);
672 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000673 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
674 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000675 attr_init(terminal_get_attr_row(terminal, i),
676 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000677 }
678 } else {
679 to_row = terminal->margin_top;
680 from_row = terminal->margin_top + d;
681
682 for (i = 0; i < (window_height - d); i++) {
683 memcpy(terminal_get_row(terminal, to_row + i),
684 terminal_get_row(terminal, from_row + i),
685 terminal->data_pitch);
686 memcpy(terminal_get_attr_row(terminal, to_row + i),
687 terminal_get_attr_row(terminal, from_row + i),
688 terminal->attr_pitch);
689 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000690 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
691 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000692 attr_init(terminal_get_attr_row(terminal, i),
693 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000694 }
695 }
696}
697
698static void
699terminal_scroll(struct terminal *terminal, int d)
700{
701 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
702 terminal_scroll_buffer(terminal, d);
703 else
704 terminal_scroll_window(terminal, d);
705}
706
707static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000708terminal_shift_line(struct terminal *terminal, int d)
709{
710 union utf8_char *row;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500711 struct attr *attr_row;
Callum Lowcay69e96582011-01-07 19:47:00 +0000712
713 row = terminal_get_row(terminal, terminal->row);
714 attr_row = terminal_get_attr_row(terminal, terminal->row);
715
716 if ((terminal->width + d) <= terminal->column)
717 d = terminal->column + 1 - terminal->width;
718 if ((terminal->column + d) >= terminal->width)
719 d = terminal->width - terminal->column - 1;
720
721 if (d < 0) {
722 d = 0 - d;
723 memmove(&row[terminal->column],
724 &row[terminal->column + d],
725 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
Callum Lowcay69e96582011-01-07 19:47:00 +0000726 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
727 (terminal->width - terminal->column - d) * sizeof(struct attr));
728 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
729 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
730 } else {
731 memmove(&row[terminal->column + d], &row[terminal->column],
732 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
733 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
734 (terminal->width - terminal->column - d) * sizeof(struct attr));
735 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
736 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
737 }
738}
739
740static void
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700741terminal_resize_cells(struct terminal *terminal,
742 int width, int height)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500743{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000744 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000745 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000746 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000747 int data_pitch, attr_pitch;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500748 int i, l, total_rows;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700749 uint32_t d, uheight = height;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500750 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000751 struct winsize ws;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500752
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700753 if (uheight > terminal->buffer_height)
754 height = terminal->buffer_height;
755
Kristian Høgsberg22106762008-12-08 13:50:07 -0500756 if (terminal->width == width && terminal->height == height)
757 return;
758
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700759 if (terminal->data && width <= terminal->max_width) {
760 d = 0;
761 if (height < terminal->height && height <= terminal->row)
762 d = terminal->height - height;
763 else if (height > terminal->height &&
764 terminal->height - 1 == terminal->row) {
765 d = terminal->height - height;
766 if (terminal->log_size < uheight)
767 d = -terminal->start;
768 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500769
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700770 terminal->start += d;
771 terminal->row -= d;
772 } else {
773 terminal->max_width = width;
774 data_pitch = width * sizeof(union utf8_char);
775 data = zalloc(data_pitch * terminal->buffer_height);
776 attr_pitch = width * sizeof(struct attr);
777 data_attr = malloc(attr_pitch * terminal->buffer_height);
778 tab_ruler = zalloc(width);
779 attr_init(data_attr, terminal->curr_attr,
780 width * terminal->buffer_height);
781
782 if (terminal->data && terminal->data_attr) {
783 if (width > terminal->width)
784 l = terminal->width;
785 else
786 l = width;
787
788 if (terminal->height > height) {
789 total_rows = height;
790 i = 1 + terminal->row - height;
791 if (i > 0) {
792 terminal->start += i;
793 terminal->row = terminal->row - i;
794 }
795 } else {
796 total_rows = terminal->height;
José Bollo4a4704a2013-09-13 11:28:51 +0200797 }
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700798
799 for (i = 0; i < total_rows; i++) {
800 memcpy(&data[width * i],
801 terminal_get_row(terminal, i),
802 l * sizeof(union utf8_char));
803 memcpy(&data_attr[width * i],
804 terminal_get_attr_row(terminal, i),
805 l * sizeof(struct attr));
806 }
807
808 free(terminal->data);
809 free(terminal->data_attr);
810 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500811 }
812
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700813 terminal->data_pitch = data_pitch;
814 terminal->attr_pitch = attr_pitch;
815 terminal->data = data;
816 terminal->data_attr = data_attr;
817 terminal->tab_ruler = tab_ruler;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700818 terminal->start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500819 }
820
Callum Lowcay86653ed2011-01-07 19:47:03 +0000821 terminal->margin_bottom =
822 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500823 terminal->width = width;
824 terminal->height = height;
Kristian Høgsbergdcfff552013-11-22 22:43:20 -0800825 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500826
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000827 /* Update the window size */
828 ws.ws_row = terminal->height;
829 ws.ws_col = terminal->width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500830 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500831 ws.ws_xpixel = allocation.width;
832 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000833 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500834}
835
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500836static void
837resize_handler(struct widget *widget,
838 int32_t width, int32_t height, void *data)
839{
840 struct terminal *terminal = data;
841 int32_t columns, rows, m;
Kristian Høgsberga83be202013-10-23 20:47:35 -0700842 char *p;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500843 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800844 columns = (width - m) / (int32_t) terminal->average_width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500845 rows = (height - m) / (int32_t) terminal->extents.height;
846
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400847 if (!window_is_fullscreen(terminal->window) &&
848 !window_is_maximized(terminal->window)) {
Peng Wuf291f202013-06-06 15:32:41 +0800849 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500850 height = rows * terminal->extents.height + m;
851 widget_set_size(terminal->widget, width, height);
Bryce W. Harrington3abdafd2014-01-14 21:58:32 +0000852 if (asprintf(&p, "%s — [%dx%d]", terminal->title, columns, rows) > 0) {
853 window_set_title(terminal->window, p);
Kristian Høgsbergb405a802014-02-05 14:44:04 -0800854 terminal->size_in_title = 1;
Bryce W. Harrington3abdafd2014-01-14 21:58:32 +0000855 free(p);
856 }
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500857 }
858
859 terminal_resize_cells(terminal, columns, rows);
860}
861
862static void
863terminal_resize(struct terminal *terminal, int columns, int rows)
864{
865 int32_t width, height, m;
866
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400867 if (window_is_fullscreen(terminal->window) ||
868 window_is_maximized(terminal->window))
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500869 return;
870
871 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800872 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500873 height = rows * terminal->extents.height + m;
Kristian Høgsberga1627922012-06-20 17:30:03 -0400874
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500875 window_frame_set_child_size(terminal->widget, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500876}
877
Callum Lowcay30eeae52011-01-07 19:46:55 +0000878struct color_scheme DEFAULT_COLORS = {
879 {
880 {0, 0, 0, 1}, /* black */
881 {0.66, 0, 0, 1}, /* red */
882 {0 , 0.66, 0, 1}, /* green */
883 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
884 {0 , 0 , 0.66, 1}, /* blue */
885 {0.66, 0 , 0.66, 1}, /* magenta */
886 {0, 0.66, 0.66, 1}, /* cyan */
887 {0.66, 0.66, 0.66, 1}, /* light grey */
888 {0.22, 0.33, 0.33, 1}, /* dark grey */
889 {1, 0.33, 0.33, 1}, /* high red */
890 {0.33, 1, 0.33, 1}, /* high green */
891 {1, 1, 0.33, 1}, /* high yellow */
892 {0.33, 0.33, 1, 1}, /* high blue */
893 {1, 0.33, 1, 1}, /* high magenta */
894 {0.33, 1, 1, 1}, /* high cyan */
895 {1, 1, 1, 1} /* white */
896 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500897 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000898 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
899};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400900
Kristian Høgsberg22106762008-12-08 13:50:07 -0500901static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500902terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
903{
904 cairo_set_source_rgba(cr,
905 terminal->color_table[index].r,
906 terminal->color_table[index].g,
907 terminal->color_table[index].b,
908 terminal->color_table[index].a);
909}
910
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500911static void
912terminal_send_selection(struct terminal *terminal, int fd)
913{
914 int row, col;
915 union utf8_char *p_row;
916 union decoded_attr attr;
917 FILE *fp;
918 int len;
919
920 fp = fdopen(fd, "w");
Brian Lovin1bf14812013-08-08 16:12:55 -0700921 if (fp == NULL){
922 close(fd);
923 return;
924 }
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500925 for (row = 0; row < terminal->height; row++) {
926 p_row = terminal_get_row(terminal, row);
927 for (col = 0; col < terminal->width; col++) {
Peng Wucfcc1112013-08-19 10:59:21 +0800928 if (p_row[col].ch == 0x200B) /* space glyph */
929 continue;
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500930 /* get the attributes for this character cell */
931 terminal_decode_attr(terminal, row, col, &attr);
932 if (!attr.attr.s)
933 continue;
934 len = strnlen((char *) p_row[col].byte, 4);
Kristian Høgsberg0dee6472012-07-01 21:25:41 -0400935 if (len > 0)
936 fwrite(p_row[col].byte, 1, len, fp);
937 if (len == 0 || col == terminal->width - 1) {
938 fwrite("\n", 1, 1, fp);
939 break;
940 }
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500941 }
942 }
943 fclose(fp);
944}
945
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500946struct glyph_run {
947 struct terminal *terminal;
948 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400949 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500950 union decoded_attr attr;
951 cairo_glyph_t glyphs[256], *g;
952};
953
954static void
955glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
956{
957 run->terminal = terminal;
958 run->cr = cr;
959 run->g = run->glyphs;
960 run->count = 0;
961 run->attr.key = 0;
962}
963
964static void
965glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
966{
967 cairo_scaled_font_t *font;
968
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500969 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
970 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300971 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500972 font = run->terminal->font_bold;
973 else
974 font = run->terminal->font_normal;
975 cairo_set_scaled_font(run->cr, font);
976 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300977 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500978
Callum Lowcay9d708b02011-01-12 20:06:17 +1300979 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
980 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500981 run->g = run->glyphs;
982 run->count = 0;
983 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300984 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500985}
986
987static void
988glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
989{
990 int num_glyphs;
991 cairo_scaled_font_t *font;
992
993 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
994
Callum Lowcay9d708b02011-01-12 20:06:17 +1300995 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500996 font = run->terminal->font_bold;
997 else
998 font = run->terminal->font_normal;
999
1000 cairo_move_to(run->cr, x, y);
1001 cairo_scaled_font_text_to_glyphs (font, x, y,
1002 (char *) c->byte, 4,
1003 &run->g, &num_glyphs,
1004 NULL, NULL, NULL);
1005 run->g += num_glyphs;
1006 run->count += num_glyphs;
1007}
1008
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001009
Kristian Høgsbergf106fd52011-01-11 10:11:39 -05001010static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001011redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001012{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001013 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001014 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001015 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001016 int top_margin, side_margin;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001017 int row, col, cursor_x, cursor_y;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001018 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001019 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001020 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001021 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001022 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001023 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -05001024 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +08001025 double average_width;
Peng Wucfcc1112013-08-19 10:59:21 +08001026 double unichar_width;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001027
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001028 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001029 widget_get_allocation(terminal->widget, &allocation);
Alexander Larssonde79dd02013-05-22 14:41:34 +02001030 cr = widget_cairo_create(terminal->widget);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001031 cairo_rectangle(cr, allocation.x, allocation.y,
1032 allocation.width, allocation.height);
1033 cairo_clip(cr);
1034 cairo_push_group(cr);
1035
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001036 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -05001037 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001038 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001039
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05001040 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001041
Kristian Høgsberg59826582011-01-20 11:56:57 -05001042 extents = terminal->extents;
Peng Wuf291f202013-06-06 15:32:41 +08001043 average_width = terminal->average_width;
1044 side_margin = (allocation.width - terminal->width * average_width) / 2;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001045 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001046
Callum Lowcay30eeae52011-01-07 19:46:55 +00001047 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001048 cairo_translate(cr, allocation.x + side_margin,
1049 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001050 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001051 for (row = 0; row < terminal->height; row++) {
Peng Wucfcc1112013-08-19 10:59:21 +08001052 p_row = terminal_get_row(terminal, row);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001053 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001054 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -05001055 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001056
Callum Lowcay9d708b02011-01-12 20:06:17 +13001057 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -05001058 continue;
1059
Peng Wucfcc1112013-08-19 10:59:21 +08001060 if (is_wide(p_row[col]))
1061 unichar_width = 2 * average_width;
1062 else
1063 unichar_width = average_width;
1064
Callum Lowcay9d708b02011-01-12 20:06:17 +13001065 terminal_set_color(terminal, cr, attr.attr.bg);
Peng Wuf291f202013-06-06 15:32:41 +08001066 cairo_move_to(cr, col * average_width,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001067 row * extents.height);
Peng Wucfcc1112013-08-19 10:59:21 +08001068 cairo_rel_line_to(cr, unichar_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001069 cairo_rel_line_to(cr, 0, extents.height);
Peng Wucfcc1112013-08-19 10:59:21 +08001070 cairo_rel_line_to(cr, -unichar_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001071 cairo_close_path(cr);
1072 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001073 }
1074 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001075
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001076 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1077
1078 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001079 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001080 for (row = 0; row < terminal->height; row++) {
1081 p_row = terminal_get_row(terminal, row);
1082 for (col = 0; col < terminal->width; col++) {
1083 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -05001084 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001085
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001086 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001087
Peng Wuf291f202013-06-06 15:32:41 +08001088 text_x = col * average_width;
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001089 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +13001090 if (attr.attr.a & ATTRMASK_UNDERLINE) {
1091 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +00001092 cairo_move_to(cr, text_x, (double)text_y + 1.5);
Peng Wuf291f202013-06-06 15:32:41 +08001093 cairo_line_to(cr, text_x + average_width, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001094 cairo_stroke(cr);
1095 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -05001096
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001097 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001098 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001099 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001100
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001101 attr.key = ~0;
1102 glyph_run_flush(&run, attr);
1103
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001104 if ((terminal->mode & MODE_SHOW_CURSOR) &&
1105 !window_has_focus(terminal->window)) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001106 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001107
Callum Lowcay30eeae52011-01-07 19:46:55 +00001108 cairo_set_line_width(cr, 1);
Peng Wuf291f202013-06-06 15:32:41 +08001109 cairo_move_to(cr, terminal->column * average_width + d,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001110 terminal->row * extents.height + d);
Peng Wuf291f202013-06-06 15:32:41 +08001111 cairo_rel_line_to(cr, average_width - 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001112 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
Peng Wuf291f202013-06-06 15:32:41 +08001113 cairo_rel_line_to(cr, -average_width + 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001114 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001115
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001116 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001117 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001118
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001119 cairo_pop_group_to_source(cr);
1120 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001121 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001122 cairo_surface_destroy(surface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001123
1124 if (terminal->send_cursor_position) {
1125 cursor_x = side_margin + allocation.x +
Peng Wuf291f202013-06-06 15:32:41 +08001126 terminal->column * average_width;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001127 cursor_y = top_margin + allocation.y +
1128 terminal->row * extents.height;
1129 window_set_text_cursor_position(terminal->window,
1130 cursor_x, cursor_y);
1131 terminal->send_cursor_position = 0;
1132 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001133}
1134
1135static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001136terminal_write(struct terminal *terminal, const char *data, size_t length)
1137{
1138 if (write(terminal->master, data, length) < 0)
1139 abort();
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001140 terminal->send_cursor_position = 1;
Kristian Høgsberg26130862011-08-24 11:30:21 -04001141}
1142
1143static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001144terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001145
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001146static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001147handle_char(struct terminal *terminal, union utf8_char utf8);
1148
1149static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001150handle_sgr(struct terminal *terminal, int code);
1151
1152static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001153handle_term_parameter(struct terminal *terminal, int code, int sr)
1154{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001155 int i;
1156
Callum Lowcay67a201d2011-01-12 19:23:41 +13001157 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001158 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001159 case 1: /* DECCKM */
1160 if (sr) terminal->key_mode = KM_APPLICATION;
1161 else terminal->key_mode = KM_NORMAL;
1162 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001163 case 2: /* DECANM */
1164 /* No VT52 support yet */
1165 terminal->g0 = CS_US;
1166 terminal->g1 = CS_US;
1167 terminal->cs = terminal->g0;
1168 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001169 case 3: /* DECCOLM */
1170 if (sr)
1171 terminal_resize(terminal, 132, 24);
1172 else
1173 terminal_resize(terminal, 80, 24);
1174
1175 /* set columns, but also home cursor and clear screen */
1176 terminal->row = 0; terminal->column = 0;
1177 for (i = 0; i < terminal->height; i++) {
1178 memset(terminal_get_row(terminal, i),
1179 0, terminal->data_pitch);
1180 attr_init(terminal_get_attr_row(terminal, i),
1181 terminal->curr_attr, terminal->width);
1182 }
1183 break;
1184 case 5: /* DECSCNM */
1185 if (sr) terminal->mode |= MODE_INVERSE;
1186 else terminal->mode &= ~MODE_INVERSE;
1187 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001188 case 6: /* DECOM */
1189 terminal->origin_mode = sr;
1190 if (terminal->origin_mode)
1191 terminal->row = terminal->margin_top;
1192 else
1193 terminal->row = 0;
1194 terminal->column = 0;
1195 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001196 case 7: /* DECAWM */
1197 if (sr) terminal->mode |= MODE_AUTOWRAP;
1198 else terminal->mode &= ~MODE_AUTOWRAP;
1199 break;
1200 case 8: /* DECARM */
1201 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1202 else terminal->mode &= ~MODE_AUTOREPEAT;
1203 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001204 case 12: /* Very visible cursor (CVVIS) */
1205 /* FIXME: What do we do here. */
1206 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001207 case 25:
1208 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1209 else terminal->mode &= ~MODE_SHOW_CURSOR;
1210 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001211 case 1034: /* smm/rmm, meta mode on/off */
1212 /* ignore */
1213 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001214 case 1037: /* deleteSendsDel */
1215 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1216 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1217 break;
1218 case 1039: /* altSendsEscape */
1219 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1220 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1221 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001222 case 1049: /* rmcup/smcup, alternate screen */
1223 /* Ignore. Should be possible to implement,
1224 * but it's kind of annoying. */
1225 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001226 default:
1227 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1228 break;
1229 }
1230 } else {
1231 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001232 case 4: /* IRM */
1233 if (sr) terminal->mode |= MODE_IRM;
1234 else terminal->mode &= ~MODE_IRM;
1235 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001236 case 20: /* LNM */
1237 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1238 else terminal->mode &= ~MODE_LF_NEWLINE;
1239 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001240 default:
1241 fprintf(stderr, "Unknown parameter: %d\n", code);
1242 break;
1243 }
1244 }
1245}
1246
1247static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001248handle_dcs(struct terminal *terminal)
1249{
1250}
1251
1252static void
1253handle_osc(struct terminal *terminal)
1254{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001255 char *p;
1256 int code;
1257
1258 terminal->escape[terminal->escape_length++] = '\0';
1259 p = &terminal->escape[2];
1260 code = strtol(p, &p, 10);
1261 if (*p == ';') p++;
1262
1263 switch (code) {
1264 case 0: /* Icon name and window title */
1265 case 1: /* Icon label */
1266 case 2: /* Window title*/
Kristian Høgsberga83be202013-10-23 20:47:35 -07001267 free(terminal->title);
1268 terminal->title = strdup(p);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001269 window_set_title(terminal->window, p);
1270 break;
Kristian Høgsberg8a205002013-09-11 11:52:56 -07001271 case 7: /* shell cwd as uri */
1272 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001273 default:
Kristian Høgsberg8a205002013-09-11 11:52:56 -07001274 fprintf(stderr, "Unknown OSC escape code %d, text %s\n",
1275 code, p);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001276 break;
1277 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001278}
1279
1280static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001281handle_escape(struct terminal *terminal)
1282{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001283 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001284 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001285 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001286 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001287 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001288 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001289 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001290
1291 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001292 i = 0;
1293 p = &terminal->escape[2];
1294 while ((isdigit(*p) || *p == ';') && i < 10) {
1295 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001296 if (!set[i]) {
1297 args[i] = 0;
1298 set[i] = 1;
1299 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001300 p++;
1301 i++;
1302 } else {
1303 args[i] = strtol(p, &p, 10);
1304 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001305 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001306 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001307
1308 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001309 case '@': /* ICH */
1310 count = set[0] ? args[0] : 1;
1311 if (count == 0) count = 1;
1312 terminal_shift_line(terminal, count);
1313 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001314 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001315 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001316 if (count == 0) count = 1;
1317 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001318 terminal->row -= count;
1319 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001320 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001321 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001322 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001323 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001324 if (count == 0) count = 1;
1325 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001326 terminal->row += count;
1327 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001328 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001329 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001330 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001331 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001332 if (count == 0) count = 1;
1333 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001334 terminal->column += count;
1335 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001336 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001337 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001338 case 'D': /* CUB */
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->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001342 terminal->column -= count;
1343 else
1344 terminal->column = 0;
1345 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001346 case 'E': /* CNL */
1347 count = set[0] ? args[0] : 1;
1348 if (terminal->row + count <= terminal->margin_bottom)
1349 terminal->row += count;
1350 else
1351 terminal->row = terminal->margin_bottom;
1352 terminal->column = 0;
1353 break;
1354 case 'F': /* CPL */
1355 count = set[0] ? args[0] : 1;
1356 if (terminal->row - count >= terminal->margin_top)
1357 terminal->row -= count;
1358 else
1359 terminal->row = terminal->margin_top;
1360 terminal->column = 0;
1361 break;
1362 case 'G': /* CHA */
1363 y = set[0] ? args[0] : 1;
1364 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1365
1366 terminal->column = y - 1;
1367 break;
1368 case 'f': /* HVP */
1369 case 'H': /* CUP */
1370 x = (set[1] ? args[1] : 1) - 1;
1371 x = x < 0 ? 0 :
1372 (x >= terminal->width ? terminal->width - 1 : x);
1373
1374 y = (set[0] ? args[0] : 1) - 1;
1375 if (terminal->origin_mode) {
1376 y += terminal->margin_top;
1377 y = y < terminal->margin_top ? terminal->margin_top :
1378 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1379 } else {
1380 y = y < 0 ? 0 :
1381 (y >= terminal->height ? terminal->height - 1 : y);
1382 }
1383
1384 terminal->row = y;
1385 terminal->column = x;
1386 break;
1387 case 'I': /* CHT */
1388 count = set[0] ? args[0] : 1;
1389 if (count == 0) count = 1;
1390 while (count > 0 && terminal->column < terminal->width) {
1391 if (terminal->tab_ruler[terminal->column]) count--;
1392 terminal->column++;
1393 }
1394 terminal->column--;
1395 break;
1396 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001397 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001398 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001399 if (!set[0] || args[0] == 0 || args[0] > 2) {
1400 memset(&row[terminal->column],
1401 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1402 attr_init(&attr_row[terminal->column],
1403 terminal->curr_attr, terminal->width - terminal->column);
1404 for (i = terminal->row + 1; i < terminal->height; i++) {
1405 memset(terminal_get_row(terminal, i),
1406 0, terminal->data_pitch);
1407 attr_init(terminal_get_attr_row(terminal, i),
1408 terminal->curr_attr, terminal->width);
1409 }
1410 } else if (args[0] == 1) {
1411 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1412 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1413 for (i = 0; i < terminal->row; i++) {
1414 memset(terminal_get_row(terminal, i),
1415 0, terminal->data_pitch);
1416 attr_init(terminal_get_attr_row(terminal, i),
1417 terminal->curr_attr, terminal->width);
1418 }
1419 } else if (args[0] == 2) {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07001420 /* Clear screen by scrolling contents out */
1421 terminal_scroll_buffer(terminal,
1422 terminal->end - terminal->start);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001423 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001424 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001425 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001426 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001427 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001428 if (!set[0] || args[0] == 0 || args[0] > 2) {
1429 memset(&row[terminal->column], 0,
1430 (terminal->width - terminal->column) * sizeof(union utf8_char));
1431 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1432 terminal->width - terminal->column);
1433 } else if (args[0] == 1) {
1434 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1435 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1436 } else if (args[0] == 2) {
1437 memset(row, 0, terminal->data_pitch);
1438 attr_init(attr_row, terminal->curr_attr, terminal->width);
1439 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001440 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001441 case 'L': /* IL */
1442 count = set[0] ? args[0] : 1;
1443 if (count == 0) count = 1;
1444 if (terminal->row >= terminal->margin_top &&
1445 terminal->row < terminal->margin_bottom)
1446 {
1447 top = terminal->margin_top;
1448 terminal->margin_top = terminal->row;
1449 terminal_scroll(terminal, 0 - count);
1450 terminal->margin_top = top;
1451 } else if (terminal->row == terminal->margin_bottom) {
1452 memset(terminal_get_row(terminal, terminal->row),
1453 0, terminal->data_pitch);
1454 attr_init(terminal_get_attr_row(terminal, terminal->row),
1455 terminal->curr_attr, terminal->width);
1456 }
1457 break;
1458 case 'M': /* DL */
1459 count = set[0] ? args[0] : 1;
1460 if (count == 0) count = 1;
1461 if (terminal->row >= terminal->margin_top &&
1462 terminal->row < terminal->margin_bottom)
1463 {
1464 top = terminal->margin_top;
1465 terminal->margin_top = terminal->row;
1466 terminal_scroll(terminal, count);
1467 terminal->margin_top = top;
1468 } else if (terminal->row == terminal->margin_bottom) {
1469 memset(terminal_get_row(terminal, terminal->row),
1470 0, terminal->data_pitch);
1471 }
1472 break;
1473 case 'P': /* DCH */
1474 count = set[0] ? args[0] : 1;
1475 if (count == 0) count = 1;
1476 terminal_shift_line(terminal, 0 - count);
1477 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001478 case 'S': /* SU */
1479 terminal_scroll(terminal, set[0] ? args[0] : 1);
1480 break;
1481 case 'T': /* SD */
1482 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1483 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001484 case 'X': /* ECH */
1485 count = set[0] ? args[0] : 1;
1486 if (count == 0) count = 1;
1487 if ((terminal->column + count) > terminal->width)
1488 count = terminal->width - terminal->column;
1489 row = terminal_get_row(terminal, terminal->row);
1490 attr_row = terminal_get_attr_row(terminal, terminal->row);
1491 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1492 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1493 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001494 case 'Z': /* CBT */
1495 count = set[0] ? args[0] : 1;
1496 if (count == 0) count = 1;
1497 while (count > 0 && terminal->column >= 0) {
1498 if (terminal->tab_ruler[terminal->column]) count--;
1499 terminal->column--;
1500 }
1501 terminal->column++;
1502 break;
1503 case '`': /* HPA */
1504 y = set[0] ? args[0] : 1;
1505 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1506
1507 terminal->column = y - 1;
1508 break;
1509 case 'b': /* REP */
1510 count = set[0] ? args[0] : 1;
1511 if (count == 0) count = 1;
1512 if (terminal->last_char.byte[0])
1513 for (i = 0; i < count; i++)
1514 handle_char(terminal, terminal->last_char);
1515 terminal->last_char.byte[0] = 0;
1516 break;
1517 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001518 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001519 break;
1520 case 'd': /* VPA */
1521 x = set[0] ? args[0] : 1;
1522 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1523
1524 terminal->row = x - 1;
1525 break;
1526 case 'g': /* TBC */
1527 if (!set[0] || args[0] == 0) {
1528 terminal->tab_ruler[terminal->column] = 0;
1529 } else if (args[0] == 3) {
1530 memset(terminal->tab_ruler, 0, terminal->width);
1531 }
1532 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001533 case 'h': /* SM */
1534 for(i = 0; i < 10 && set[i]; i++) {
1535 handle_term_parameter(terminal, args[i], 1);
1536 }
1537 break;
1538 case 'l': /* RM */
1539 for(i = 0; i < 10 && set[i]; i++) {
1540 handle_term_parameter(terminal, args[i], 0);
1541 }
1542 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001543 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001544 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001545 if (i <= 7 && set[i] && set[i + 1] &&
1546 set[i + 2] && args[i + 1] == 5)
1547 {
1548 if (args[i] == 38) {
1549 handle_sgr(terminal, args[i + 2] + 256);
1550 break;
1551 } else if (args[i] == 48) {
1552 handle_sgr(terminal, args[i + 2] + 512);
1553 break;
1554 }
1555 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001556 if(set[i]) {
1557 handle_sgr(terminal, args[i]);
1558 } else if(i == 0) {
1559 handle_sgr(terminal, 0);
1560 break;
1561 } else {
1562 break;
1563 }
1564 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001565 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001566 case 'n': /* DSR */
1567 i = set[0] ? args[0] : 0;
1568 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001569 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001570 } else if (i == 6) {
1571 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1572 terminal->origin_mode ?
1573 terminal->row+terminal->margin_top : terminal->row+1,
1574 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001575 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001576 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001577 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001578 case 'r':
1579 if(!set[0]) {
1580 terminal->margin_top = 0;
1581 terminal->margin_bottom = terminal->height-1;
1582 terminal->row = 0;
1583 terminal->column = 0;
1584 } else {
1585 top = (set[0] ? args[0] : 1) - 1;
1586 top = top < 0 ? 0 :
1587 (top >= terminal->height ? terminal->height - 1 : top);
1588 bottom = (set[1] ? args[1] : 1) - 1;
1589 bottom = bottom < 0 ? 0 :
1590 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1591 if(bottom > top) {
1592 terminal->margin_top = top;
1593 terminal->margin_bottom = bottom;
1594 } else {
1595 terminal->margin_top = 0;
1596 terminal->margin_bottom = terminal->height-1;
1597 }
1598 if(terminal->origin_mode)
1599 terminal->row = terminal->margin_top;
1600 else
1601 terminal->row = 0;
1602 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001603 }
1604 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001605 case 's':
1606 terminal->saved_row = terminal->row;
1607 terminal->saved_column = terminal->column;
1608 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001609 case 't': /* windowOps */
1610 if (!set[0]) break;
1611 switch (args[0]) {
1612 case 4: /* resize px */
1613 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001614 widget_schedule_resize(terminal->widget,
1615 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001616 }
1617 break;
1618 case 8: /* resize ch */
1619 if (set[1] && set[2]) {
1620 terminal_resize(terminal, args[2], args[1]);
1621 }
1622 break;
1623 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001624 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001625 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1626 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001627 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001628 break;
1629 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001630 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001631 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1632 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001633 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001634 break;
1635 case 18: /* report ch */
1636 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1637 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001638 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001639 break;
1640 case 21: /* report title */
1641 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1642 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001643 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001644 break;
1645 default:
1646 if (args[0] >= 24)
1647 terminal_resize(terminal, terminal->width, args[0]);
1648 else
1649 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1650 break;
1651 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001652 case 'u':
1653 terminal->row = terminal->saved_row;
1654 terminal->column = terminal->saved_column;
1655 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001656 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001657 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001658 break;
1659 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001660}
1661
1662static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001663handle_non_csi_escape(struct terminal *terminal, char code)
1664{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001665 switch(code) {
1666 case 'M': /* RI */
1667 terminal->row -= 1;
1668 if(terminal->row < terminal->margin_top) {
1669 terminal->row = terminal->margin_top;
1670 terminal_scroll(terminal, -1);
1671 }
1672 break;
1673 case 'E': /* NEL */
1674 terminal->column = 0;
1675 // fallthrough
1676 case 'D': /* IND */
1677 terminal->row += 1;
1678 if(terminal->row > terminal->margin_bottom) {
1679 terminal->row = terminal->margin_bottom;
1680 terminal_scroll(terminal, +1);
1681 }
1682 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001683 case 'c': /* RIS */
1684 terminal_init(terminal);
1685 break;
1686 case 'H': /* HTS */
1687 terminal->tab_ruler[terminal->column] = 1;
1688 break;
1689 case '7': /* DECSC */
1690 terminal->saved_row = terminal->row;
1691 terminal->saved_column = terminal->column;
1692 terminal->saved_attr = terminal->curr_attr;
1693 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001694 terminal->saved_cs = terminal->cs;
1695 terminal->saved_g0 = terminal->g0;
1696 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001697 break;
1698 case '8': /* DECRC */
1699 terminal->row = terminal->saved_row;
1700 terminal->column = terminal->saved_column;
1701 terminal->curr_attr = terminal->saved_attr;
1702 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001703 terminal->cs = terminal->saved_cs;
1704 terminal->g0 = terminal->saved_g0;
1705 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001706 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001707 case '=': /* DECPAM */
1708 terminal->key_mode = KM_APPLICATION;
1709 break;
1710 case '>': /* DECPNM */
1711 terminal->key_mode = KM_NORMAL;
1712 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001713 default:
1714 fprintf(stderr, "Unknown escape code: %c\n", code);
1715 break;
1716 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001717}
1718
1719static void
1720handle_special_escape(struct terminal *terminal, char special, char code)
1721{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001722 int i, numChars;
1723
1724 if (special == '#') {
1725 switch(code) {
1726 case '8':
1727 /* fill with 'E', no cheap way to do this */
1728 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1729 numChars = terminal->width * terminal->height;
1730 for(i = 0; i < numChars; i++) {
1731 terminal->data[i].byte[0] = 'E';
1732 }
1733 break;
1734 default:
1735 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1736 break;
1737 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001738 } else if (special == '(' || special == ')') {
1739 switch(code) {
1740 case '0':
1741 if (special == '(')
1742 terminal->g0 = CS_SPECIAL;
1743 else
1744 terminal->g1 = CS_SPECIAL;
1745 break;
1746 case 'A':
1747 if (special == '(')
1748 terminal->g0 = CS_UK;
1749 else
1750 terminal->g1 = CS_UK;
1751 break;
1752 case 'B':
1753 if (special == '(')
1754 terminal->g0 = CS_US;
1755 else
1756 terminal->g1 = CS_US;
1757 break;
1758 default:
1759 fprintf(stderr, "Unknown character set %c\n", code);
1760 break;
1761 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001762 } else {
1763 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1764 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001765}
1766
1767static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001768handle_sgr(struct terminal *terminal, int code)
1769{
1770 switch(code) {
1771 case 0:
1772 terminal->curr_attr = terminal->color_scheme->default_attr;
1773 break;
1774 case 1:
1775 terminal->curr_attr.a |= ATTRMASK_BOLD;
1776 if (terminal->curr_attr.fg < 8)
1777 terminal->curr_attr.fg += 8;
1778 break;
1779 case 4:
1780 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1781 break;
1782 case 5:
1783 terminal->curr_attr.a |= ATTRMASK_BLINK;
1784 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001785 case 8:
1786 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1787 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001788 case 2:
1789 case 21:
1790 case 22:
1791 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1792 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1793 terminal->curr_attr.fg -= 8;
1794 break;
1795 case 24:
1796 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1797 break;
1798 case 25:
1799 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1800 break;
1801 case 7:
1802 case 26:
1803 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1804 break;
1805 case 27:
1806 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1807 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001808 case 28:
1809 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1810 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001811 case 39:
1812 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1813 break;
1814 case 49:
1815 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1816 break;
1817 default:
1818 if(code >= 30 && code <= 37) {
1819 terminal->curr_attr.fg = code - 30;
1820 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1821 terminal->curr_attr.fg += 8;
1822 } else if(code >= 40 && code <= 47) {
1823 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001824 } else if (code >= 90 && code <= 97) {
1825 terminal->curr_attr.fg = code - 90 + 8;
1826 } else if (code >= 100 && code <= 107) {
1827 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001828 } else if(code >= 256 && code < 512) {
1829 terminal->curr_attr.fg = code - 256;
1830 } else if(code >= 512 && code < 768) {
1831 terminal->curr_attr.bg = code - 512;
1832 } else {
1833 fprintf(stderr, "Unknown SGR code: %d\n", code);
1834 }
1835 break;
1836 }
1837}
1838
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001839/* Returns 1 if c was special, otherwise 0 */
1840static int
1841handle_special_char(struct terminal *terminal, char c)
1842{
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001843 union utf8_char *row;
1844 struct attr *attr_row;
1845
1846 row = terminal_get_row(terminal, terminal->row);
1847 attr_row = terminal_get_attr_row(terminal, terminal->row);
1848
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001849 switch(c) {
1850 case '\r':
1851 terminal->column = 0;
1852 break;
1853 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001854 if (terminal->mode & MODE_LF_NEWLINE) {
1855 terminal->column = 0;
1856 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001857 /* fallthrough */
1858 case '\v':
1859 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001860 terminal->row++;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07001861 if (terminal->row > terminal->margin_bottom) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001862 terminal->row = terminal->margin_bottom;
1863 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001864 }
1865
1866 break;
1867 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001868 while (terminal->column < terminal->width) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001869 if (terminal->mode & MODE_IRM)
1870 terminal_shift_line(terminal, +1);
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001871
1872 if (row[terminal->column].byte[0] == '\0') {
1873 row[terminal->column].byte[0] = ' ';
1874 row[terminal->column].byte[1] = '\0';
1875 attr_row[terminal->column] = terminal->curr_attr;
1876 }
1877
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001878 terminal->column++;
Kristian Høgsbergcca3c2f2012-06-20 15:56:13 -04001879 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001880 }
1881 if (terminal->column >= terminal->width) {
1882 terminal->column = terminal->width - 1;
1883 }
1884
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001885 break;
1886 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001887 if (terminal->column >= terminal->width) {
1888 terminal->column = terminal->width - 2;
1889 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001890 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001891 } else if (terminal->mode & MODE_AUTOWRAP) {
1892 terminal->column = terminal->width - 1;
1893 terminal->row -= 1;
1894 if (terminal->row < terminal->margin_top) {
1895 terminal->row = terminal->margin_top;
1896 terminal_scroll(terminal, -1);
1897 }
1898 }
1899
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001900 break;
1901 case '\a':
1902 /* Bell */
1903 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001904 case '\x0E': /* SO */
1905 terminal->cs = terminal->g1;
1906 break;
1907 case '\x0F': /* SI */
1908 terminal->cs = terminal->g0;
1909 break;
Kristian Høgsberg2a1aa4e2012-08-03 09:37:05 -04001910 case '\0':
1911 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001912 default:
1913 return 0;
1914 }
1915
1916 return 1;
1917}
1918
1919static void
1920handle_char(struct terminal *terminal, union utf8_char utf8)
1921{
1922 union utf8_char *row;
1923 struct attr *attr_row;
1924
1925 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001926
1927 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001928
1929 /* There are a whole lot of non-characters, control codes,
1930 * and formatting codes that should probably be ignored,
1931 * for example: */
1932 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1933 /* BOM, ignore */
1934 return;
1935 }
1936
1937 /* Some of these non-characters should be translated, e.g.: */
1938 if (utf8.byte[0] < 32) {
1939 utf8.byte[0] = utf8.byte[0] + 64;
1940 }
1941
1942 /* handle right margin effects */
1943 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001944 if (terminal->mode & MODE_AUTOWRAP) {
1945 terminal->column = 0;
1946 terminal->row += 1;
1947 if (terminal->row > terminal->margin_bottom) {
1948 terminal->row = terminal->margin_bottom;
1949 terminal_scroll(terminal, +1);
1950 }
1951 } else {
1952 terminal->column--;
1953 }
1954 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001955
1956 row = terminal_get_row(terminal, terminal->row);
1957 attr_row = terminal_get_attr_row(terminal, terminal->row);
1958
Callum Lowcay69e96582011-01-07 19:47:00 +00001959 if (terminal->mode & MODE_IRM)
1960 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001961 row[terminal->column] = utf8;
1962 attr_row[terminal->column++] = terminal->curr_attr;
1963
Kristian Høgsberg1d781ee2013-11-24 16:54:12 -08001964 if (terminal->row + terminal->start + 1 > terminal->end)
1965 terminal->end = terminal->row + terminal->start + 1;
1966 if (terminal->end == terminal->buffer_height)
1967 terminal->log_size = terminal->buffer_height;
1968 else if (terminal->log_size < terminal->buffer_height)
1969 terminal->log_size = terminal->end;
1970
Peng Wucfcc1112013-08-19 10:59:21 +08001971 /* cursor jump for wide character. */
1972 if (is_wide(utf8))
1973 row[terminal->column++].ch = 0x200B; /* space glyph */
1974
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001975 if (utf8.ch != terminal->last_char.ch)
1976 terminal->last_char = utf8;
1977}
1978
Callum Lowcay30eeae52011-01-07 19:46:55 +00001979static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001980escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1981{
1982 int len, i;
1983
1984 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1985 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1986 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1987 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1988 else len = 1; /* Invalid, cannot happen */
1989
1990 if (terminal->escape_length + len <= MAX_ESCAPE) {
1991 for (i = 0; i < len; i++)
1992 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1993 terminal->escape_length += len;
1994 } else if (terminal->escape_length < MAX_ESCAPE) {
1995 terminal->escape[terminal->escape_length++] = 0;
1996 }
1997}
1998
1999static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002000terminal_data(struct terminal *terminal, const char *data, size_t length)
2001{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002002 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002003 union utf8_char utf8;
2004 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002005
2006 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002007 parser_state =
2008 utf8_next_char(&terminal->state_machine, data[i]);
2009 switch(parser_state) {
2010 case utf8state_accept:
2011 utf8.ch = terminal->state_machine.s.ch;
2012 break;
2013 case utf8state_reject:
2014 /* the unicode replacement character */
2015 utf8.byte[0] = 0xEF;
2016 utf8.byte[1] = 0xBF;
2017 utf8.byte[2] = 0xBD;
2018 utf8.byte[3] = 0x00;
2019 break;
2020 default:
2021 continue;
2022 }
2023
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002024 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13002025 switch (terminal->state) {
2026 case escape_state_escape:
2027 escape_append_utf8(terminal, utf8);
2028 switch (utf8.byte[0]) {
2029 case 'P': /* DCS */
2030 terminal->state = escape_state_dcs;
2031 break;
2032 case '[': /* CSI */
2033 terminal->state = escape_state_csi;
2034 break;
2035 case ']': /* OSC */
2036 terminal->state = escape_state_osc;
2037 break;
2038 case '#':
2039 case '(':
2040 case ')': /* special */
2041 terminal->state = escape_state_special;
2042 break;
2043 case '^': /* PM (not implemented) */
2044 case '_': /* APC (not implemented) */
2045 terminal->state = escape_state_ignore;
2046 break;
2047 default:
2048 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002049 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13002050 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002051 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002052 continue;
2053 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002054 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
2055 /* do nothing */
2056 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002057 terminal->escape_flags |= ESC_FLAG_WHAT;
2058 } else if (utf8.byte[0] == '>') {
2059 terminal->escape_flags |= ESC_FLAG_GT;
2060 } else if (utf8.byte[0] == '!') {
2061 terminal->escape_flags |= ESC_FLAG_BANG;
2062 } else if (utf8.byte[0] == '$') {
2063 terminal->escape_flags |= ESC_FLAG_CASH;
2064 } else if (utf8.byte[0] == '\'') {
2065 terminal->escape_flags |= ESC_FLAG_SQUOTE;
2066 } else if (utf8.byte[0] == '"') {
2067 terminal->escape_flags |= ESC_FLAG_DQUOTE;
2068 } else if (utf8.byte[0] == ' ') {
2069 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002070 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002071 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002072 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13002073 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002074 }
2075
2076 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
2077 utf8.byte[0] == '`')
2078 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002079 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002080 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002081 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002082 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002083 continue;
2084 case escape_state_inner_escape:
2085 if (utf8.byte[0] == '\\') {
2086 terminal->state = escape_state_normal;
2087 if (terminal->outer_state == escape_state_dcs) {
2088 handle_dcs(terminal);
2089 } else if (terminal->outer_state == escape_state_osc) {
2090 handle_osc(terminal);
2091 }
2092 } else if (utf8.byte[0] == '\e') {
2093 terminal->state = terminal->outer_state;
2094 escape_append_utf8(terminal, utf8);
2095 if (terminal->escape_length >= MAX_ESCAPE)
2096 terminal->state = escape_state_normal;
2097 } else {
2098 terminal->state = terminal->outer_state;
2099 if (terminal->escape_length < MAX_ESCAPE)
2100 terminal->escape[terminal->escape_length++] = '\e';
2101 escape_append_utf8(terminal, utf8);
2102 if (terminal->escape_length >= MAX_ESCAPE)
2103 terminal->state = escape_state_normal;
2104 }
2105 continue;
2106 case escape_state_dcs:
2107 case escape_state_osc:
2108 case escape_state_ignore:
2109 if (utf8.byte[0] == '\e') {
2110 terminal->outer_state = terminal->state;
2111 terminal->state = escape_state_inner_escape;
2112 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
2113 terminal->state = escape_state_normal;
2114 handle_osc(terminal);
2115 } else {
2116 escape_append_utf8(terminal, utf8);
2117 if (terminal->escape_length >= MAX_ESCAPE)
2118 terminal->state = escape_state_normal;
2119 }
2120 continue;
2121 case escape_state_special:
2122 escape_append_utf8(terminal, utf8);
2123 terminal->state = escape_state_normal;
2124 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2125 handle_special_escape(terminal, terminal->escape[1],
2126 utf8.byte[0]);
2127 }
2128 continue;
2129 default:
2130 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002131 }
2132
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002133 /* this is valid, because ASCII characters are never used to
2134 * introduce a multibyte sequence in UTF-8 */
2135 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002136 terminal->state = escape_state_escape;
2137 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002138 terminal->escape[0] = '\e';
2139 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002140 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002141 } else {
2142 handle_char(terminal, utf8);
2143 } /* if */
2144 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002145
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002146 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002147}
2148
2149static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002150data_source_target(void *data,
2151 struct wl_data_source *source, const char *mime_type)
2152{
2153 fprintf(stderr, "data_source_target, %s\n", mime_type);
2154}
2155
2156static void
2157data_source_send(void *data,
2158 struct wl_data_source *source,
2159 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002160{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002161 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002162
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002163 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002164}
2165
2166static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002167data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002168{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002169 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002170}
2171
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002172static const struct wl_data_source_listener data_source_listener = {
2173 data_source_target,
2174 data_source_send,
2175 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002176};
2177
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07002178static const char text_mime_type[] = "text/plain;charset=utf-8";
2179
2180static void
2181data_handler(struct window *window,
2182 struct input *input,
2183 float x, float y, const char **types, void *data)
2184{
2185 int i, has_text = 0;
2186
2187 if (!types)
2188 return;
2189 for (i = 0; types[i]; i++)
2190 if (strcmp(types[i], text_mime_type) == 0)
2191 has_text = 1;
2192
2193 if (!has_text) {
2194 input_accept(input, NULL);
2195 } else {
2196 input_accept(input, text_mime_type);
2197 }
2198}
2199
2200static void
2201drop_handler(struct window *window, struct input *input,
2202 int32_t x, int32_t y, void *data)
2203{
2204 struct terminal *terminal = data;
2205
2206 input_receive_drag_data_to_fd(input, text_mime_type, terminal->master);
2207}
2208
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002209static void
2210fullscreen_handler(struct window *window, void *data)
2211{
2212 struct terminal *terminal = data;
2213
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002214 window_set_fullscreen(window, !window_is_fullscreen(terminal->window));
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002215}
2216
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002217static void
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002218close_handler(void *data)
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002219{
2220 struct terminal *terminal = data;
2221
2222 terminal_destroy(terminal);
2223}
2224
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002225static void
2226terminal_copy(struct terminal *terminal, struct input *input)
2227{
2228 terminal->selection =
2229 display_create_data_source(terminal->display);
2230 wl_data_source_offer(terminal->selection,
2231 "text/plain;charset=utf-8");
2232 wl_data_source_add_listener(terminal->selection,
2233 &data_source_listener, terminal);
2234 input_set_selection(input, terminal->selection,
2235 display_get_serial(terminal->display));
2236}
2237
2238static void
2239terminal_paste(struct terminal *terminal, struct input *input)
2240{
2241 input_receive_selection_data_to_fd(input,
2242 "text/plain;charset=utf-8",
2243 terminal->master);
2244
2245}
2246
2247static void
2248terminal_new_instance(struct terminal *terminal)
2249{
2250 struct terminal *new_terminal;
2251
2252 new_terminal = terminal_create(terminal->display);
2253 if (terminal_run(new_terminal, option_shell))
2254 terminal_destroy(new_terminal);
2255}
2256
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002257static int
2258handle_bound_key(struct terminal *terminal,
2259 struct input *input, uint32_t sym, uint32_t time)
2260{
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002261 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002262 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002263 /* Cut selection; terminal doesn't do cut, fall
2264 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002265 case XKB_KEY_C:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002266 terminal_copy(terminal, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002267 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002268 case XKB_KEY_V:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002269 terminal_paste(terminal, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002270 return 1;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002271 case XKB_KEY_N:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002272 terminal_new_instance(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002273 return 1;
2274
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002275 case XKB_KEY_Up:
2276 if (!terminal->scrolling)
2277 terminal->saved_start = terminal->start;
2278 if (terminal->start == terminal->end - terminal->log_size)
2279 return 1;
2280
2281 terminal->scrolling = 1;
2282 terminal->start--;
2283 terminal->row++;
2284 terminal->selection_start_row++;
2285 terminal->selection_end_row++;
2286 widget_schedule_redraw(terminal->widget);
2287 return 1;
2288
2289 case XKB_KEY_Down:
2290 if (!terminal->scrolling)
2291 terminal->saved_start = terminal->start;
2292
2293 if (terminal->start == terminal->saved_start)
2294 return 1;
2295
2296 terminal->scrolling = 1;
2297 terminal->start++;
2298 terminal->row--;
2299 terminal->selection_start_row--;
2300 terminal->selection_end_row--;
2301 widget_schedule_redraw(terminal->widget);
2302 return 1;
2303
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002304 default:
2305 return 0;
2306 }
2307}
2308
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002309static void
2310key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002311 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2312 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002313{
2314 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002315 char ch[MAX_RESPONSE];
Kristian Høgsberg88fd4082012-06-21 15:55:03 -04002316 uint32_t modifiers, serial;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002317 int ret, len = 0, d;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002318 bool convert_utf8 = true;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002319
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002320 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002321 if ((modifiers & MOD_CONTROL_MASK) &&
2322 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002323 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2324 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002325 return;
2326
Daniel Stonea8468712012-11-07 17:51:35 +11002327 /* Map keypad symbols to 'normal' equivalents before processing */
2328 switch (sym) {
2329 case XKB_KEY_KP_Space:
2330 sym = XKB_KEY_space;
2331 break;
2332 case XKB_KEY_KP_Tab:
2333 sym = XKB_KEY_Tab;
2334 break;
2335 case XKB_KEY_KP_Enter:
2336 sym = XKB_KEY_Return;
2337 break;
2338 case XKB_KEY_KP_Left:
2339 sym = XKB_KEY_Left;
2340 break;
2341 case XKB_KEY_KP_Up:
2342 sym = XKB_KEY_Up;
2343 break;
2344 case XKB_KEY_KP_Right:
2345 sym = XKB_KEY_Right;
2346 break;
2347 case XKB_KEY_KP_Down:
2348 sym = XKB_KEY_Down;
2349 break;
2350 case XKB_KEY_KP_Equal:
2351 sym = XKB_KEY_equal;
2352 break;
2353 case XKB_KEY_KP_Multiply:
2354 sym = XKB_KEY_asterisk;
2355 break;
2356 case XKB_KEY_KP_Add:
2357 sym = XKB_KEY_plus;
2358 break;
2359 case XKB_KEY_KP_Separator:
2360 /* Note this is actually locale-dependent and should mostly be
2361 * a comma. But leave it as period until we one day start
2362 * doing the right thing. */
2363 sym = XKB_KEY_period;
2364 break;
2365 case XKB_KEY_KP_Subtract:
2366 sym = XKB_KEY_minus;
2367 break;
2368 case XKB_KEY_KP_Decimal:
2369 sym = XKB_KEY_period;
2370 break;
2371 case XKB_KEY_KP_Divide:
2372 sym = XKB_KEY_slash;
2373 break;
2374 case XKB_KEY_KP_0:
2375 case XKB_KEY_KP_1:
2376 case XKB_KEY_KP_2:
2377 case XKB_KEY_KP_3:
2378 case XKB_KEY_KP_4:
2379 case XKB_KEY_KP_5:
2380 case XKB_KEY_KP_6:
2381 case XKB_KEY_KP_7:
2382 case XKB_KEY_KP_8:
2383 case XKB_KEY_KP_9:
2384 sym = (sym - XKB_KEY_KP_0) + XKB_KEY_0;
2385 break;
2386 default:
2387 break;
2388 }
2389
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002390 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002391 case XKB_KEY_BackSpace:
Kristian Høgsbergb7f94bf2012-06-21 12:29:36 -04002392 if (modifiers & MOD_ALT_MASK)
2393 ch[len++] = 0x1b;
Kristian Høgsberg71a4cf42012-06-20 17:57:56 -04002394 ch[len++] = 0x7f;
2395 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002396 case XKB_KEY_Tab:
2397 case XKB_KEY_Linefeed:
2398 case XKB_KEY_Clear:
2399 case XKB_KEY_Pause:
2400 case XKB_KEY_Scroll_Lock:
2401 case XKB_KEY_Sys_Req:
2402 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002403 ch[len++] = sym & 0x7f;
2404 break;
2405
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002406 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002407 if (terminal->mode & MODE_LF_NEWLINE) {
2408 ch[len++] = 0x0D;
2409 ch[len++] = 0x0A;
2410 } else {
2411 ch[len++] = 0x0D;
2412 }
2413 break;
2414
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002415 case XKB_KEY_Shift_L:
2416 case XKB_KEY_Shift_R:
2417 case XKB_KEY_Control_L:
2418 case XKB_KEY_Control_R:
2419 case XKB_KEY_Alt_L:
2420 case XKB_KEY_Alt_R:
Kristian Høgsberg22fbcf72012-06-22 12:18:56 -04002421 case XKB_KEY_Meta_L:
2422 case XKB_KEY_Meta_R:
2423 case XKB_KEY_Super_L:
2424 case XKB_KEY_Super_R:
2425 case XKB_KEY_Hyper_L:
2426 case XKB_KEY_Hyper_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002427 break;
2428
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002429 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002430 len = function_key_response('[', 2, modifiers, '~', ch);
2431 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002432 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002433 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2434 ch[len++] = '\x04';
2435 } else {
2436 len = function_key_response('[', 3, modifiers, '~', ch);
2437 }
2438 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002439 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002440 len = function_key_response('[', 5, modifiers, '~', ch);
2441 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002442 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002443 len = function_key_response('[', 6, modifiers, '~', ch);
2444 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002445 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002446 len = function_key_response('O', 1, modifiers, 'P', ch);
2447 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002448 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002449 len = function_key_response('O', 1, modifiers, 'Q', ch);
2450 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002451 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002452 len = function_key_response('O', 1, modifiers, 'R', ch);
2453 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002454 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002455 len = function_key_response('O', 1, modifiers, 'S', ch);
2456 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002457 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002458 len = function_key_response('[', 15, modifiers, '~', ch);
2459 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002460 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002461 len = function_key_response('[', 17, modifiers, '~', ch);
2462 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002463 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002464 len = function_key_response('[', 18, modifiers, '~', ch);
2465 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002466 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002467 len = function_key_response('[', 19, modifiers, '~', ch);
2468 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002469 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002470 len = function_key_response('[', 20, modifiers, '~', ch);
2471 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002472 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002473 len = function_key_response('[', 21, modifiers, '~', ch);
2474 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002475 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002476 len = function_key_response('[', 24, modifiers, '~', ch);
2477 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002478 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002479 /* Handle special keys with alternate mappings */
2480 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2481 if (len != 0) break;
2482
Kristian Høgsberg70163132012-05-08 15:55:39 -04002483 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002484 if (sym >= '3' && sym <= '7')
2485 sym = (sym & 0x1f) + 8;
2486
2487 if (!((sym >= '!' && sym <= '/') ||
2488 (sym >= '8' && sym <= '?') ||
2489 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2490 else if (sym == '2') sym = 0x00;
2491 else if (sym == '/') sym = 0x1F;
2492 else if (sym == '8' || sym == '?') sym = 0x7F;
Kristian Høgsbergae9e0732012-06-21 12:30:15 -04002493 }
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002494 if (modifiers & MOD_ALT_MASK) {
2495 if (terminal->mode & MODE_ALT_SENDS_ESC) {
2496 ch[len++] = 0x1b;
2497 } else {
2498 sym = sym | 0x80;
2499 convert_utf8 = false;
2500 }
Callum Lowcay7e08e902011-01-07 19:47:02 +00002501 }
2502
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002503 if ((sym < 128) ||
2504 (!convert_utf8 && sym < 256)) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002505 ch[len++] = sym;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002506 } else {
2507 ret = xkb_keysym_to_utf8(sym, ch + len,
2508 MAX_RESPONSE - len);
2509 if (ret < 0)
2510 fprintf(stderr,
2511 "Warning: buffer too small to encode "
2512 "UTF8 character\n");
2513 else
2514 len += ret;
2515 }
2516
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002517 break;
2518 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002519
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002520 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002521 if (terminal->scrolling) {
2522 d = terminal->saved_start - terminal->start;
2523 terminal->row -= d;
2524 terminal->selection_start_row -= d;
2525 terminal->selection_end_row -= d;
2526 terminal->start = terminal->saved_start;
2527 terminal->scrolling = 0;
2528 widget_schedule_redraw(terminal->widget);
2529 }
2530
Kristian Høgsberg26130862011-08-24 11:30:21 -04002531 terminal_write(terminal, ch, len);
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002532
2533 /* Hide cursor, except if this was coming from a
2534 * repeating key press. */
2535 serial = display_get_serial(terminal->display);
2536 if (terminal->hide_cursor_serial != serial) {
2537 input_set_pointer_image(input, CURSOR_BLANK);
2538 terminal->hide_cursor_serial = serial;
2539 }
2540 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002541}
2542
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002543static void
2544keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002545 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002546{
2547 struct terminal *terminal = data;
2548
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002549 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002550}
2551
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002552static int wordsep(int ch)
2553{
2554 const char extra[] = "-,./?%&#:_=+@~";
2555
Andre Heider552d12b2012-08-02 20:59:43 +02002556 if (ch > 127)
2557 return 1;
2558
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002559 return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
2560}
2561
2562static int
2563recompute_selection(struct terminal *terminal)
2564{
2565 struct rectangle allocation;
2566 int col, x, width, height;
2567 int start_row, end_row;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002568 int word_start, eol;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002569 int side_margin, top_margin;
2570 int start_x, end_x;
2571 int cw, ch;
2572 union utf8_char *data;
2573
Peng Wuf291f202013-06-06 15:32:41 +08002574 cw = terminal->average_width;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002575 ch = terminal->extents.height;
2576 widget_get_allocation(terminal->widget, &allocation);
2577 width = terminal->width * cw;
2578 height = terminal->height * ch;
2579 side_margin = allocation.x + (allocation.width - width) / 2;
2580 top_margin = allocation.y + (allocation.height - height) / 2;
2581
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002582 start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1;
2583 end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1;
2584
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002585 if (start_row < end_row ||
2586 (start_row == end_row &&
2587 terminal->selection_start_x < terminal->selection_end_x)) {
2588 terminal->selection_start_row = start_row;
2589 terminal->selection_end_row = end_row;
2590 start_x = terminal->selection_start_x;
2591 end_x = terminal->selection_end_x;
2592 } else {
2593 terminal->selection_start_row = end_row;
2594 terminal->selection_end_row = start_row;
2595 start_x = terminal->selection_end_x;
2596 end_x = terminal->selection_start_x;
2597 }
2598
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002599 eol = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002600 if (terminal->selection_start_row < 0) {
2601 terminal->selection_start_row = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002602 terminal->selection_start_col = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002603 } else {
2604 x = side_margin + cw / 2;
2605 data = terminal_get_row(terminal,
2606 terminal->selection_start_row);
2607 word_start = 0;
2608 for (col = 0; col < terminal->width; col++, x += cw) {
2609 if (col == 0 || wordsep(data[col - 1].ch))
2610 word_start = col;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002611 if (data[col].ch != 0)
2612 eol = col + 1;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002613 if (start_x < x)
2614 break;
2615 }
2616
2617 switch (terminal->dragging) {
2618 case SELECT_LINE:
2619 terminal->selection_start_col = 0;
2620 break;
2621 case SELECT_WORD:
2622 terminal->selection_start_col = word_start;
2623 break;
2624 case SELECT_CHAR:
2625 terminal->selection_start_col = col;
2626 break;
2627 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002628 }
2629
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002630 if (terminal->selection_end_row >= terminal->height) {
2631 terminal->selection_end_row = terminal->height;
2632 terminal->selection_end_col = 0;
2633 } else {
2634 x = side_margin + cw / 2;
2635 data = terminal_get_row(terminal, terminal->selection_end_row);
2636 for (col = 0; col < terminal->width; col++, x += cw) {
2637 if (terminal->dragging == SELECT_CHAR && end_x < x)
2638 break;
2639 if (terminal->dragging == SELECT_WORD &&
2640 end_x < x && wordsep(data[col].ch))
2641 break;
2642 }
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002643 terminal->selection_end_col = col;
2644 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002645
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002646 if (terminal->selection_end_col != terminal->selection_start_col ||
2647 terminal->selection_start_row != terminal->selection_end_row) {
2648 col = terminal->selection_end_col;
2649 if (col > 0 && data[col - 1].ch == 0)
2650 terminal->selection_end_col = terminal->width;
2651 data = terminal_get_row(terminal, terminal->selection_start_row);
2652 if (data[terminal->selection_start_col].ch == 0)
2653 terminal->selection_start_col = eol;
2654 }
2655
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002656 return 1;
2657}
2658
Kristian Høgsberg59826582011-01-20 11:56:57 -05002659static void
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002660menu_func(struct window *window, struct input *input, int index, void *data)
2661{
2662 struct terminal *terminal = data;
2663
2664 fprintf(stderr, "picked entry %d\n", index);
2665
2666 switch (index) {
2667 case 0:
2668 terminal_new_instance(terminal);
2669 break;
2670 case 1:
2671 terminal_copy(terminal, input);
2672 break;
2673 case 2:
2674 terminal_paste(terminal, input);
2675 break;
2676 }
2677}
2678
2679static void
2680show_menu(struct terminal *terminal, struct input *input, uint32_t time)
2681{
2682 int32_t x, y;
2683 static const char *entries[] = {
2684 "Open Terminal", "Copy", "Paste"
2685 };
2686
2687 input_get_position(input, &x, &y);
2688 window_show_menu(terminal->display, input, time, terminal->window,
2689 x - 10, y - 10, menu_func,
2690 entries, ARRAY_LENGTH(entries));
2691}
2692
2693static void
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002694click_handler(struct widget *widget, struct terminal *terminal,
2695 struct input *input, int32_t x, int32_t y,
2696 uint32_t time)
2697{
2698 if (time - terminal->click_time < 500)
2699 terminal->click_count++;
2700 else
2701 terminal->click_count = 1;
2702
2703 terminal->click_time = time;
2704 terminal->dragging = (terminal->click_count - 1) % 3 + SELECT_CHAR;
2705
2706 terminal->selection_end_x = terminal->selection_start_x = x;
2707 terminal->selection_end_y = terminal->selection_start_y = y;
2708 if (recompute_selection(terminal))
2709 widget_schedule_redraw(widget);
2710}
2711
2712static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002713button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002714 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002715 uint32_t button,
2716 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002717{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002718 struct terminal *terminal = data;
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002719 int32_t x, y;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002720
2721 switch (button) {
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002722 case BTN_LEFT:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002723 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002724 input_get_position(input, &x, &y);
2725 click_handler(widget, terminal, input, x, y, time);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002726 } else {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002727 terminal->dragging = SELECT_NONE;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002728 }
2729 break;
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002730
2731 case BTN_RIGHT:
2732 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
2733 show_menu(terminal, input, time);
2734 break;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002735 }
2736}
2737
2738static int
Kristian Høgsberg29784402012-06-28 14:27:02 -04002739enter_handler(struct widget *widget,
2740 struct input *input, float x, float y, void *data)
2741{
Kristian Høgsberga83be202013-10-23 20:47:35 -07002742 struct terminal *terminal = data;
2743
2744 /* Reset title to get rid of resizing '[WxH]' in titlebar */
Kristian Høgsbergb405a802014-02-05 14:44:04 -08002745 if (terminal->size_in_title) {
2746 window_set_title(terminal->window, terminal->title);
2747 terminal->size_in_title = 0;
2748 }
Kristian Høgsberga83be202013-10-23 20:47:35 -07002749
Kristian Høgsberg29784402012-06-28 14:27:02 -04002750 return CURSOR_IBEAM;
2751}
2752
2753static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002754motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002755 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002756 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002757{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002758 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002759
2760 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002761 input_get_position(input,
2762 &terminal->selection_end_x,
2763 &terminal->selection_end_y);
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002764
2765 if (recompute_selection(terminal))
2766 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002767 }
2768
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002769 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002770}
2771
Alexander Larssonde79dd02013-05-22 14:41:34 +02002772static void
2773output_handler(struct window *window, struct output *output, int enter,
2774 void *data)
2775{
2776 if (enter)
2777 window_set_buffer_transform(window, output_get_transform(output));
2778 window_set_buffer_scale(window, window_get_output_scale(window));
2779 window_schedule_redraw(window);
2780}
2781
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002782static void
2783touch_down_handler(struct widget *widget, struct input *input,
2784 uint32_t serial, uint32_t time, int32_t id,
2785 float x, float y, void *data)
2786{
2787 struct terminal *terminal = data;
2788
2789 if (id == 0)
2790 click_handler(widget, terminal, input, x, y, time);
2791}
2792
2793static void
2794touch_up_handler(struct widget *widget, struct input *input,
2795 uint32_t serial, uint32_t time, int32_t id, void *data)
2796{
2797 struct terminal *terminal = data;
2798
2799 if (id == 0)
2800 terminal->dragging = SELECT_NONE;
2801}
2802
2803static void
2804touch_motion_handler(struct widget *widget, struct input *input,
2805 uint32_t time, int32_t id, float x, float y, void *data)
2806{
2807 struct terminal *terminal = data;
2808
2809 if (terminal->dragging &&
2810 id == 0) {
2811 terminal->selection_end_x = (int)x;
2812 terminal->selection_end_y = (int)y;
2813
2814 if (recompute_selection(terminal))
2815 widget_schedule_redraw(widget);
2816 }
2817}
2818
Peng Wuf291f202013-06-06 15:32:41 +08002819#ifndef howmany
2820#define howmany(x, y) (((x) + ((y) - 1)) / (y))
2821#endif
2822
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002823static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002824terminal_create(struct display *display)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002825{
2826 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002827 cairo_surface_t *surface;
2828 cairo_t *cr;
Peng Wuf291f202013-06-06 15:32:41 +08002829 cairo_text_extents_t text_extents;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002830
Peter Huttererf3d62272013-08-08 11:57:05 +10002831 terminal = xzalloc(sizeof *terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002832 terminal->color_scheme = &DEFAULT_COLORS;
2833 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002834 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002835 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002836 terminal->window = window_create(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002837 terminal->widget = window_frame_create(terminal->window, terminal);
U. Artie Eoff09827e22014-01-15 11:40:37 -08002838 terminal->title = xstrdup("Wayland Terminal");
Kristian Høgsberga83be202013-10-23 20:47:35 -07002839 window_set_title(terminal->window, terminal->title);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002840 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002841
2842 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002843 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002844
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002845 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002846 terminal->margin = 5;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002847 terminal->buffer_height = 1024;
2848 terminal->end = 1;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002849
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002850 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002851 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002852 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002853 keyboard_focus_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002854 window_set_fullscreen_handler(terminal->window, fullscreen_handler);
Alexander Larssonde79dd02013-05-22 14:41:34 +02002855 window_set_output_handler(terminal->window, output_handler);
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002856 window_set_close_handler(terminal->window, close_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002857
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07002858 window_set_data_handler(terminal->window, data_handler);
2859 window_set_drop_handler(terminal->window, drop_handler);
2860
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002861 widget_set_redraw_handler(terminal->widget, redraw_handler);
2862 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002863 widget_set_button_handler(terminal->widget, button_handler);
Kristian Høgsberg29784402012-06-28 14:27:02 -04002864 widget_set_enter_handler(terminal->widget, enter_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002865 widget_set_motion_handler(terminal->widget, motion_handler);
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002866 widget_set_touch_up_handler(terminal->widget, touch_up_handler);
2867 widget_set_touch_down_handler(terminal->widget, touch_down_handler);
2868 widget_set_touch_motion_handler(terminal->widget, touch_motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002869
Kristian Høgsberg09531622010-06-14 23:22:15 -04002870 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2871 cr = cairo_create(surface);
Kristian Høgsberg38912df2012-06-27 17:52:23 -04002872 cairo_set_font_size(cr, option_font_size);
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002873 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002874 CAIRO_FONT_SLANT_NORMAL,
2875 CAIRO_FONT_WEIGHT_BOLD);
2876 terminal->font_bold = cairo_get_scaled_font (cr);
2877 cairo_scaled_font_reference(terminal->font_bold);
2878
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002879 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002880 CAIRO_FONT_SLANT_NORMAL,
2881 CAIRO_FONT_WEIGHT_NORMAL);
2882 terminal->font_normal = cairo_get_scaled_font (cr);
2883 cairo_scaled_font_reference(terminal->font_normal);
2884
Kristian Høgsberg09531622010-06-14 23:22:15 -04002885 cairo_font_extents(cr, &terminal->extents);
Peng Wuf291f202013-06-06 15:32:41 +08002886
2887 /* Compute the average ascii glyph width */
2888 cairo_text_extents(cr, TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS,
2889 &text_extents);
2890 terminal->average_width = howmany
2891 (text_extents.width,
2892 strlen(TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS));
2893 terminal->average_width = ceil(terminal->average_width);
2894
Kristian Høgsberg09531622010-06-14 23:22:15 -04002895 cairo_destroy(cr);
2896 cairo_surface_destroy(surface);
2897
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002898 terminal_resize(terminal, 20, 5); /* Set minimum size first */
Kristian Høgsberga1627922012-06-20 17:30:03 -04002899 terminal_resize(terminal, 80, 25);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002900
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002901 wl_list_insert(terminal_list.prev, &terminal->link);
2902
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002903 return terminal;
2904}
2905
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002906static void
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002907terminal_destroy(struct terminal *terminal)
2908{
Dima Ryazanova85292e2012-11-29 00:27:09 -08002909 display_unwatch_fd(terminal->display, terminal->master);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002910 window_destroy(terminal->window);
2911 close(terminal->master);
2912 wl_list_remove(&terminal->link);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002913
2914 if (wl_list_empty(&terminal_list))
Dima Ryazanova85292e2012-11-29 00:27:09 -08002915 display_exit(terminal->display);
2916
Kristian Høgsberga83be202013-10-23 20:47:35 -07002917 free(terminal->title);
Dima Ryazanova85292e2012-11-29 00:27:09 -08002918 free(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002919}
2920
2921static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002922io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002923{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002924 struct terminal *terminal =
2925 container_of(task, struct terminal, io_task);
2926 char buffer[256];
2927 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002928
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002929 if (events & EPOLLHUP) {
2930 terminal_destroy(terminal);
2931 return;
2932 }
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002933
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002934 len = read(terminal->master, buffer, sizeof buffer);
2935 if (len < 0)
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002936 terminal_destroy(terminal);
2937 else
2938 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002939}
2940
2941static int
2942terminal_run(struct terminal *terminal, const char *path)
2943{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002944 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002945 pid_t pid;
2946
2947 pid = forkpty(&master, NULL, NULL, NULL);
2948 if (pid == 0) {
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04002949 setenv("TERM", option_term, 1);
2950 setenv("COLORTERM", option_term, 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002951 if (execl(path, path, NULL)) {
2952 printf("exec failed: %m\n");
2953 exit(EXIT_FAILURE);
2954 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002955 } else if (pid < 0) {
2956 fprintf(stderr, "failed to fork and create pty (%m).\n");
2957 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002958 }
2959
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002960 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002961 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002962 terminal->io_task.run = io_handler;
2963 display_watch_fd(terminal->display, terminal->master,
2964 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002965
Kristian Høgsbergcdbbae22014-04-07 11:28:05 -07002966 if (option_fullscreen)
2967 window_set_fullscreen(terminal->window, 1);
2968 else
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002969 terminal_resize(terminal, 80, 24);
2970
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002971 return 0;
2972}
2973
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002974static const struct weston_option terminal_options[] = {
2975 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002976 { WESTON_OPTION_STRING, "font", 0, &option_font },
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002977 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002978};
2979
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002980int main(int argc, char *argv[])
2981{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002982 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002983 struct terminal *terminal;
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -07002984 struct weston_config *config;
2985 struct weston_config_section *s;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002986
Peng Wucfcc1112013-08-19 10:59:21 +08002987 /* as wcwidth is locale-dependent,
2988 wcwidth needs setlocale call to function properly. */
2989 setlocale(LC_ALL, "");
2990
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002991 option_shell = getenv("SHELL");
2992 if (!option_shell)
2993 option_shell = "/bin/bash";
2994
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07002995 config = weston_config_parse("weston.ini");
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -07002996 s = weston_config_get_section(config, "terminal", NULL, NULL);
2997 weston_config_section_get_string(s, "font", &option_font, "mono");
2998 weston_config_section_get_int(s, "font-size", &option_font_size, 14);
2999 weston_config_section_get_string(s, "term", &option_term, "xterm");
3000 weston_config_destroy(config);
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003001
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003002 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02003003 if (d == NULL) {
3004 fprintf(stderr, "failed to create display: %m\n");
3005 return -1;
3006 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003007
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003008 wl_list_init(&terminal_list);
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04003009 terminal = terminal_create(d);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003010 if (terminal_run(terminal, option_shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003011 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003012
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003013 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003014
3015 return 0;
3016}