blob: e298f9e5554c48fb6964c3e2223c968de9da0e1f [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
Pekka Paalanen50719bc2011-11-22 14:18:50 +020041#include <wayland-client.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050042
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -040043#include "../shared/config-parser.h"
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050044#include "window.h"
45
Kristian Høgsberg0395f302008-12-22 12:14:50 -050046static int option_fullscreen;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -040047static char *option_font = "mono";
Kristian Høgsberg38912df2012-06-27 17:52:23 -040048static int option_font_size = 14;
Kristian Høgsbergde845cf2012-06-20 22:14:31 -040049static char *option_term = "xterm";
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040050static char *option_shell;
51
52static struct wl_list terminal_list;
53
54static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -040055terminal_create(struct display *display);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040056static void
57terminal_destroy(struct terminal *terminal);
58static int
59terminal_run(struct terminal *terminal, const char *path);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050060
Peng Wuf291f202013-06-06 15:32:41 +080061#define TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS \
62 " !\"#$%&'()*+,-./" \
63 "0123456789" \
64 ":;<=>?@" \
65 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
66 "[\\]^_`" \
67 "abcdefghijklmnopqrstuvwxyz" \
68 "{|}~" \
69 ""
70
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050071#define MOD_SHIFT 0x01
72#define MOD_ALT 0x02
73#define MOD_CTRL 0x04
74
Callum Lowcay30eeae52011-01-07 19:46:55 +000075#define ATTRMASK_BOLD 0x01
76#define ATTRMASK_UNDERLINE 0x02
77#define ATTRMASK_BLINK 0x04
78#define ATTRMASK_INVERSE 0x08
Callum Lowcay81179db2011-01-10 12:14:01 +130079#define ATTRMASK_CONCEALED 0x10
Callum Lowcay30eeae52011-01-07 19:46:55 +000080
Callum Lowcayb8609ad2011-01-07 19:46:57 +000081/* Buffer sizes */
Callum Lowcayef57a9b2011-01-14 20:46:23 +130082#define MAX_RESPONSE 256
83#define MAX_ESCAPE 255
Callum Lowcayb8609ad2011-01-07 19:46:57 +000084
Callum Lowcay8e57dd52011-01-07 19:46:59 +000085/* Terminal modes */
86#define MODE_SHOW_CURSOR 0x00000001
87#define MODE_INVERSE 0x00000002
88#define MODE_AUTOWRAP 0x00000004
89#define MODE_AUTOREPEAT 0x00000008
90#define MODE_LF_NEWLINE 0x00000010
Callum Lowcay69e96582011-01-07 19:47:00 +000091#define MODE_IRM 0x00000020
Callum Lowcay7e08e902011-01-07 19:47:02 +000092#define MODE_DELETE_SENDS_DEL 0x00000040
93#define MODE_ALT_SENDS_ESC 0x00000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +000094
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000095union utf8_char {
96 unsigned char byte[4];
97 uint32_t ch;
98};
99
100enum utf8_state {
101 utf8state_start,
102 utf8state_accept,
103 utf8state_reject,
104 utf8state_expect3,
105 utf8state_expect2,
106 utf8state_expect1
107};
108
109struct utf8_state_machine {
110 enum utf8_state state;
111 int len;
112 union utf8_char s;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700113 uint32_t unicode;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000114};
115
116static void
117init_state_machine(struct utf8_state_machine *machine)
118{
119 machine->state = utf8state_start;
120 machine->len = 0;
121 machine->s.ch = 0;
122}
123
124static enum utf8_state
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400125utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000126{
127 switch(machine->state) {
128 case utf8state_start:
129 case utf8state_accept:
130 case utf8state_reject:
131 machine->s.ch = 0;
132 machine->len = 0;
133 if(c == 0xC0 || c == 0xC1) {
134 /* overlong encoding, reject */
135 machine->state = utf8state_reject;
136 } else if((c & 0x80) == 0) {
137 /* single byte, accept */
138 machine->s.byte[machine->len++] = c;
139 machine->state = utf8state_accept;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700140 machine->unicode = c;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000141 } else if((c & 0xC0) == 0x80) {
142 /* parser out of sync, ignore byte */
143 machine->state = utf8state_start;
144 } else if((c & 0xE0) == 0xC0) {
145 /* start of two byte sequence */
146 machine->s.byte[machine->len++] = c;
147 machine->state = utf8state_expect1;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700148 machine->unicode = c & 0x1f;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000149 } else if((c & 0xF0) == 0xE0) {
150 /* start of three byte sequence */
151 machine->s.byte[machine->len++] = c;
152 machine->state = utf8state_expect2;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700153 machine->unicode = c & 0x0f;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000154 } else if((c & 0xF8) == 0xF0) {
155 /* start of four byte sequence */
156 machine->s.byte[machine->len++] = c;
157 machine->state = utf8state_expect3;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700158 machine->unicode = c & 0x07;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000159 } else {
160 /* overlong encoding, reject */
161 machine->state = utf8state_reject;
162 }
163 break;
164 case utf8state_expect3:
165 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700166 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000167 if((c & 0xC0) == 0x80) {
168 /* all good, continue */
169 machine->state = utf8state_expect2;
170 } else {
171 /* missing extra byte, reject */
172 machine->state = utf8state_reject;
173 }
174 break;
175 case utf8state_expect2:
176 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700177 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000178 if((c & 0xC0) == 0x80) {
179 /* all good, continue */
180 machine->state = utf8state_expect1;
181 } else {
182 /* missing extra byte, reject */
183 machine->state = utf8state_reject;
184 }
185 break;
186 case utf8state_expect1:
187 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700188 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000189 if((c & 0xC0) == 0x80) {
190 /* all good, accept */
191 machine->state = utf8state_accept;
192 } else {
193 /* missing extra byte, reject */
194 machine->state = utf8state_reject;
195 }
196 break;
197 default:
198 machine->state = utf8state_reject;
199 break;
200 }
201
202 return machine->state;
203}
204
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700205static uint32_t
206get_unicode(union utf8_char utf8)
207{
208 struct utf8_state_machine machine;
209 int i;
210
211 init_state_machine(&machine);
212 for (i = 0; i < 4; i++) {
213 utf8_next_char(&machine, utf8.byte[i]);
214 if (machine.state == utf8state_accept ||
215 machine.state == utf8state_reject)
216 break;
217 }
218
219 if (machine.state == utf8state_reject)
220 return 0xfffd;
221
222 return machine.unicode;
223}
224
Peng Wucfcc1112013-08-19 10:59:21 +0800225static bool
226is_wide(union utf8_char utf8)
227{
228 uint32_t unichar = get_unicode(utf8);
229 return wcwidth(unichar) > 1;
230}
231
Callum Lowcay256e72f2011-01-07 19:47:01 +0000232struct char_sub {
233 union utf8_char match;
234 union utf8_char replace;
235};
236/* Set last char_sub match to NULL char */
237typedef struct char_sub *character_set;
238
239struct char_sub CS_US[] = {
240 {{{0, }}, {{0, }}}
241};
242static struct char_sub CS_UK[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200243 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000244 {{{0, }}, {{0, }}}
245};
246static struct char_sub CS_SPECIAL[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200247 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond: ♦ */
248 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell: ▒ */
249 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT: ␉ */
250 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF: ␌ */
251 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR: ␍ */
252 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF: ␊ */
253 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree: ° */
254 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus: ± */
255 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL: ␤ */
256 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT: ␋ */
257 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB: ┘ */
258 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT: ┐ */
259 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT: ┌ */
260 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_LB: └ */
261 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS: ┼ */
262 {{{'o', 0, }}, {{0xE2, 0x8E, 0xBA, 0}}}, /* Horiz. Scan Line 1: ⎺ */
263 {{{'p', 0, }}, {{0xE2, 0x8E, 0xBB, 0}}}, /* Horiz. Scan Line 3: ⎻ */
264 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* Horiz. Scan Line 5: ─ */
265 {{{'r', 0, }}, {{0xE2, 0x8E, 0xBC, 0}}}, /* Horiz. Scan Line 7: ⎼ */
266 {{{'s', 0, }}, {{0xE2, 0x8E, 0xBD, 0}}}, /* Horiz. Scan Line 9: ⎽ */
267 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR: ├ */
268 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL: ┤ */
269 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU: ┴ */
270 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD: ┬ */
271 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V: │ */
272 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE: ≤ */
273 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE: ≥ */
274 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI: π */
275 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ: ≠ */
276 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
277 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT: ⋅ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000278 {{{0, }}, {{0, }}}
279};
280
281static void
282apply_char_set(character_set cs, union utf8_char *utf8)
283{
284 int i = 0;
285
286 while (cs[i].match.byte[0]) {
287 if ((*utf8).ch == cs[i].match.ch) {
288 *utf8 = cs[i].replace;
289 break;
290 }
291 i++;
292 }
293}
294
Callum Lowcay7e08e902011-01-07 19:47:02 +0000295struct key_map {
296 int sym;
297 int num;
298 char escape;
299 char code;
300};
301/* Set last key_sub sym to NULL */
302typedef struct key_map *keyboard_mode;
303
304static struct key_map KM_NORMAL[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400305 { XKB_KEY_Left, 1, '[', 'D' },
306 { XKB_KEY_Right, 1, '[', 'C' },
307 { XKB_KEY_Up, 1, '[', 'A' },
308 { XKB_KEY_Down, 1, '[', 'B' },
309 { XKB_KEY_Home, 1, '[', 'H' },
310 { XKB_KEY_End, 1, '[', 'F' },
311 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000312};
313static struct key_map KM_APPLICATION[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400314 { XKB_KEY_Left, 1, 'O', 'D' },
315 { XKB_KEY_Right, 1, 'O', 'C' },
316 { XKB_KEY_Up, 1, 'O', 'A' },
317 { XKB_KEY_Down, 1, 'O', 'B' },
318 { XKB_KEY_Home, 1, 'O', 'H' },
319 { XKB_KEY_End, 1, 'O', 'F' },
320 { XKB_KEY_KP_Enter, 1, 'O', 'M' },
321 { XKB_KEY_KP_Multiply, 1, 'O', 'j' },
322 { XKB_KEY_KP_Add, 1, 'O', 'k' },
323 { XKB_KEY_KP_Separator, 1, 'O', 'l' },
324 { XKB_KEY_KP_Subtract, 1, 'O', 'm' },
325 { XKB_KEY_KP_Divide, 1, 'O', 'o' },
326 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000327};
328
329static int
330function_key_response(char escape, int num, uint32_t modifiers,
331 char code, char *response)
332{
333 int mod_num = 0;
334 int len;
335
Kristian Høgsberg70163132012-05-08 15:55:39 -0400336 if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
337 if (modifiers & MOD_ALT_MASK) mod_num |= 2;
338 if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000339
340 if (mod_num != 0)
341 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
342 num, mod_num + 1, code);
343 else if (code != '~')
344 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
345 escape, code);
346 else
347 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
348 escape, num, code);
349
350 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
351 else return len;
352}
353
354/* returns the number of bytes written into response,
355 * which must have room for MAX_RESPONSE bytes */
356static int
357apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
358{
359 struct key_map map;
360 int len = 0;
361 int i = 0;
362
363 while (mode[i].sym) {
364 map = mode[i++];
365 if (sym == map.sym) {
366 len = function_key_response(map.escape, map.num,
367 modifiers, map.code,
368 response);
369 break;
370 }
371 }
372
373 return len;
374}
375
Callum Lowcay30eeae52011-01-07 19:46:55 +0000376struct terminal_color { double r, g, b, a; };
377struct attr {
378 unsigned char fg, bg;
379 char a; /* attributes format:
380 * 76543210
Callum Lowcay81179db2011-01-10 12:14:01 +1300381 * cilub */
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500382 char s; /* in selection */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000383};
384struct color_scheme {
385 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500386 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000387 struct attr default_attr;
388};
389
390static void
391attr_init(struct attr *data_attr, struct attr attr, int n)
392{
393 int i;
394 for (i = 0; i < n; i++) {
395 data_attr[i] = attr;
396 }
397}
398
Callum Lowcay67a201d2011-01-12 19:23:41 +1300399enum escape_state {
400 escape_state_normal = 0,
401 escape_state_escape,
402 escape_state_dcs,
403 escape_state_csi,
404 escape_state_osc,
405 escape_state_inner_escape,
406 escape_state_ignore,
407 escape_state_special
408};
409
410#define ESC_FLAG_WHAT 0x01
411#define ESC_FLAG_GT 0x02
412#define ESC_FLAG_BANG 0x04
413#define ESC_FLAG_CASH 0x08
414#define ESC_FLAG_SQUOTE 0x10
415#define ESC_FLAG_DQUOTE 0x20
416#define ESC_FLAG_SPACE 0x40
417
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400418enum {
419 SELECT_NONE,
420 SELECT_CHAR,
421 SELECT_WORD,
422 SELECT_LINE
423};
424
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500425struct terminal {
426 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500427 struct widget *widget;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500428 struct display *display;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000429 union utf8_char *data;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400430 struct task io_task;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000431 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000432 struct attr *data_attr;
433 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000434 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000435 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000436 char saved_origin_mode;
437 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000438 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000439 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000440 character_set cs, g0, g1;
441 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000442 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000443 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500444 int width, height, start, row, column;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000445 int saved_row, saved_column;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600446 int send_cursor_position;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500447 int fd, master;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500448 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300449 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500450 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300451 enum escape_state state;
452 enum escape_state outer_state;
453 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000454 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500455 int margin;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400456 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000457 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400458 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +0800459 double average_width;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500460 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg88fd4082012-06-21 15:55:03 -0400461 uint32_t hide_cursor_serial;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500462
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400463 struct wl_data_source *selection;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400464 uint32_t button_time;
465 int dragging, click_count;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500466 int selection_start_x, selection_start_y;
467 int selection_end_x, selection_end_y;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400468 int selection_start_row, selection_start_col;
469 int selection_end_row, selection_end_col;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -0400470 struct wl_list link;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500471};
472
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000473/* Create default tab stops, every 8 characters */
474static void
475terminal_init_tabs(struct terminal *terminal)
476{
477 int i = 0;
478
479 while (i < terminal->width) {
480 if (i % 8 == 0)
481 terminal->tab_ruler[i] = 1;
482 else
483 terminal->tab_ruler[i] = 0;
484 i++;
485 }
486}
487
Callum Lowcay30eeae52011-01-07 19:46:55 +0000488static void
489terminal_init(struct terminal *terminal)
490{
491 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000492 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000493 terminal->mode = MODE_SHOW_CURSOR |
494 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000495 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000496 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000497
498 terminal->row = 0;
499 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000500
Callum Lowcay256e72f2011-01-07 19:47:01 +0000501 terminal->g0 = CS_US;
502 terminal->g1 = CS_US;
503 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000504 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000505
506 terminal->saved_g0 = terminal->g0;
507 terminal->saved_g1 = terminal->g1;
508 terminal->saved_cs = terminal->cs;
509
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000510 terminal->saved_attr = terminal->curr_attr;
511 terminal->saved_origin_mode = terminal->origin_mode;
512 terminal->saved_row = terminal->row;
513 terminal->saved_column = terminal->column;
514
515 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000516}
517
518static void
519init_color_table(struct terminal *terminal)
520{
521 int c, r;
522 struct terminal_color *color_table = terminal->color_table;
523
524 for (c = 0; c < 256; c ++) {
525 if (c < 16) {
526 color_table[c] = terminal->color_scheme->palette[c];
527 } else if (c < 232) {
528 r = c - 16;
529 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
530 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
531 color_table[c].r = ((double)(r % 6) / 6.0);
532 color_table[c].a = 1.0;
533 } else {
534 r = (c - 232) * 10 + 8;
535 color_table[c].r = ((double) r) / 256.0;
536 color_table[c].g = color_table[c].r;
537 color_table[c].b = color_table[c].r;
538 color_table[c].a = 1.0;
539 }
540 }
541}
542
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000543static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500544terminal_get_row(struct terminal *terminal, int row)
545{
546 int index;
547
548 index = (row + terminal->start) % terminal->height;
549
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000550 return &terminal->data[index * terminal->width];
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500551}
552
Callum Lowcay30eeae52011-01-07 19:46:55 +0000553static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500554terminal_get_attr_row(struct terminal *terminal, int row)
555{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000556 int index;
557
558 index = (row + terminal->start) % terminal->height;
559
560 return &terminal->data_attr[index * terminal->width];
561}
562
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500563union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300564 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500565 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500566};
567
568static void
569terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500570 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500571{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500572 struct attr attr;
573 int foreground, background, tmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500574
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500575 decoded->attr.s = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400576 if (((row == terminal->selection_start_row &&
577 col >= terminal->selection_start_col) ||
578 row > terminal->selection_start_row) &&
579 ((row == terminal->selection_end_row &&
580 col < terminal->selection_end_col) ||
581 row < terminal->selection_end_row))
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500582 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500583
584 /* get the attributes for this character cell */
585 attr = terminal_get_attr_row(terminal, row)[col];
586 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500587 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500588 ((terminal->mode & MODE_SHOW_CURSOR) &&
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400589 window_has_focus(terminal->window) && terminal->row == row &&
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500590 terminal->column == col)) {
591 foreground = attr.bg;
592 background = attr.fg;
593 if (attr.a & ATTRMASK_BOLD) {
594 if (foreground <= 16) foreground |= 0x08;
595 if (background <= 16) background &= 0x07;
596 }
597 } else {
598 foreground = attr.fg;
599 background = attr.bg;
600 }
601
602 if (terminal->mode & MODE_INVERSE) {
603 tmp = foreground;
604 foreground = background;
605 background = tmp;
606 if (attr.a & ATTRMASK_BOLD) {
607 if (foreground <= 16) foreground |= 0x08;
608 if (background <= 16) background &= 0x07;
609 }
610 }
611
Callum Lowcay9d708b02011-01-12 20:06:17 +1300612 decoded->attr.fg = foreground;
613 decoded->attr.bg = background;
614 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000615}
616
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500617
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500618static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000619terminal_scroll_buffer(struct terminal *terminal, int d)
620{
621 int i;
622
623 d = d % (terminal->height + 1);
624 terminal->start = (terminal->start + d) % terminal->height;
625 if (terminal->start < 0) terminal->start = terminal->height + terminal->start;
626 if(d < 0) {
627 d = 0 - d;
628 for(i = 0; i < d; i++) {
629 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
630 attr_init(terminal_get_attr_row(terminal, i),
631 terminal->curr_attr, terminal->width);
632 }
633 } else {
634 for(i = terminal->height - d; i < terminal->height; i++) {
635 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
636 attr_init(terminal_get_attr_row(terminal, i),
637 terminal->curr_attr, terminal->width);
638 }
639 }
Kristian Høgsberg18e928d2012-06-27 19:29:41 -0400640
641 terminal->selection_start_row -= d;
642 terminal->selection_end_row -= d;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000643}
644
645static void
646terminal_scroll_window(struct terminal *terminal, int d)
647{
648 int i;
649 int window_height;
650 int from_row, to_row;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000651
652 // scrolling range is inclusive
653 window_height = terminal->margin_bottom - terminal->margin_top + 1;
654 d = d % (window_height + 1);
655 if(d < 0) {
656 d = 0 - d;
657 to_row = terminal->margin_bottom;
658 from_row = terminal->margin_bottom - d;
659
660 for (i = 0; i < (window_height - d); i++) {
661 memcpy(terminal_get_row(terminal, to_row - i),
662 terminal_get_row(terminal, from_row - i),
663 terminal->data_pitch);
664 memcpy(terminal_get_attr_row(terminal, to_row - i),
665 terminal_get_attr_row(terminal, from_row - i),
666 terminal->attr_pitch);
667 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000668 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
669 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000670 attr_init(terminal_get_attr_row(terminal, i),
671 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000672 }
673 } else {
674 to_row = terminal->margin_top;
675 from_row = terminal->margin_top + d;
676
677 for (i = 0; i < (window_height - d); i++) {
678 memcpy(terminal_get_row(terminal, to_row + i),
679 terminal_get_row(terminal, from_row + i),
680 terminal->data_pitch);
681 memcpy(terminal_get_attr_row(terminal, to_row + i),
682 terminal_get_attr_row(terminal, from_row + i),
683 terminal->attr_pitch);
684 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000685 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
686 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000687 attr_init(terminal_get_attr_row(terminal, i),
688 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000689 }
690 }
691}
692
693static void
694terminal_scroll(struct terminal *terminal, int d)
695{
696 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
697 terminal_scroll_buffer(terminal, d);
698 else
699 terminal_scroll_window(terminal, d);
700}
701
702static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000703terminal_shift_line(struct terminal *terminal, int d)
704{
705 union utf8_char *row;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500706 struct attr *attr_row;
Callum Lowcay69e96582011-01-07 19:47:00 +0000707
708 row = terminal_get_row(terminal, terminal->row);
709 attr_row = terminal_get_attr_row(terminal, terminal->row);
710
711 if ((terminal->width + d) <= terminal->column)
712 d = terminal->column + 1 - terminal->width;
713 if ((terminal->column + d) >= terminal->width)
714 d = terminal->width - terminal->column - 1;
715
716 if (d < 0) {
717 d = 0 - d;
718 memmove(&row[terminal->column],
719 &row[terminal->column + d],
720 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
Callum Lowcay69e96582011-01-07 19:47:00 +0000721 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
722 (terminal->width - terminal->column - d) * sizeof(struct attr));
723 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
724 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
725 } else {
726 memmove(&row[terminal->column + d], &row[terminal->column],
727 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
728 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
729 (terminal->width - terminal->column - d) * sizeof(struct attr));
730 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
731 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
732 }
733}
734
735static void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500736terminal_resize_cells(struct terminal *terminal, int width, int height)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500737{
738 size_t size;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000739 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000740 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000741 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000742 int data_pitch, attr_pitch;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500743 int i, l, total_rows;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500744 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000745 struct winsize ws;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500746
747 if (terminal->width == width && terminal->height == height)
748 return;
749
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000750 data_pitch = width * sizeof(union utf8_char);
751 size = data_pitch * height;
Peter Huttererf3d62272013-08-08 11:57:05 +1000752 data = zalloc(size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000753 attr_pitch = width * sizeof(struct attr);
754 data_attr = malloc(attr_pitch * height);
Peter Huttererf3d62272013-08-08 11:57:05 +1000755 tab_ruler = zalloc(width);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000756 attr_init(data_attr, terminal->curr_attr, width * height);
757 if (terminal->data && terminal->data_attr) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500758 if (width > terminal->width)
759 l = terminal->width;
760 else
761 l = width;
762
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500763 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500764 total_rows = height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500765 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500766 total_rows = terminal->height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500767 }
768
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000769 for (i = 0; i < total_rows; i++) {
770 memcpy(&data[width * i],
771 terminal_get_row(terminal, i),
772 l * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000773 memcpy(&data_attr[width * i],
774 terminal_get_attr_row(terminal, i),
775 l * sizeof(struct attr));
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000776 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500777
778 free(terminal->data);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000779 free(terminal->data_attr);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000780 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500781 }
782
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000783 terminal->data_pitch = data_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000784 terminal->attr_pitch = attr_pitch;
Callum Lowcay86653ed2011-01-07 19:47:03 +0000785 terminal->margin_bottom =
786 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500787 terminal->width = width;
788 terminal->height = height;
789 terminal->data = data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000790 terminal->data_attr = data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000791 terminal->tab_ruler = tab_ruler;
792 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500793
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000794 /* Update the window size */
795 ws.ws_row = terminal->height;
796 ws.ws_col = terminal->width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500797 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500798 ws.ws_xpixel = allocation.width;
799 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000800 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500801}
802
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500803static void
804resize_handler(struct widget *widget,
805 int32_t width, int32_t height, void *data)
806{
807 struct terminal *terminal = data;
808 int32_t columns, rows, m;
809
810 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800811 columns = (width - m) / (int32_t) terminal->average_width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500812 rows = (height - m) / (int32_t) terminal->extents.height;
813
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400814 if (!window_is_fullscreen(terminal->window) &&
815 !window_is_maximized(terminal->window)) {
Peng Wuf291f202013-06-06 15:32:41 +0800816 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500817 height = rows * terminal->extents.height + m;
818 widget_set_size(terminal->widget, width, height);
819 }
820
821 terminal_resize_cells(terminal, columns, rows);
822}
823
824static void
825terminal_resize(struct terminal *terminal, int columns, int rows)
826{
827 int32_t width, height, m;
828
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400829 if (window_is_fullscreen(terminal->window) ||
830 window_is_maximized(terminal->window))
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500831 return;
832
833 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800834 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500835 height = rows * terminal->extents.height + m;
Kristian Høgsberga1627922012-06-20 17:30:03 -0400836
837 frame_set_child_size(terminal->widget, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500838}
839
Callum Lowcay30eeae52011-01-07 19:46:55 +0000840struct color_scheme DEFAULT_COLORS = {
841 {
842 {0, 0, 0, 1}, /* black */
843 {0.66, 0, 0, 1}, /* red */
844 {0 , 0.66, 0, 1}, /* green */
845 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
846 {0 , 0 , 0.66, 1}, /* blue */
847 {0.66, 0 , 0.66, 1}, /* magenta */
848 {0, 0.66, 0.66, 1}, /* cyan */
849 {0.66, 0.66, 0.66, 1}, /* light grey */
850 {0.22, 0.33, 0.33, 1}, /* dark grey */
851 {1, 0.33, 0.33, 1}, /* high red */
852 {0.33, 1, 0.33, 1}, /* high green */
853 {1, 1, 0.33, 1}, /* high yellow */
854 {0.33, 0.33, 1, 1}, /* high blue */
855 {1, 0.33, 1, 1}, /* high magenta */
856 {0.33, 1, 1, 1}, /* high cyan */
857 {1, 1, 1, 1} /* white */
858 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500859 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000860 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
861};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400862
Kristian Høgsberg22106762008-12-08 13:50:07 -0500863static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500864terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
865{
866 cairo_set_source_rgba(cr,
867 terminal->color_table[index].r,
868 terminal->color_table[index].g,
869 terminal->color_table[index].b,
870 terminal->color_table[index].a);
871}
872
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500873static void
874terminal_send_selection(struct terminal *terminal, int fd)
875{
876 int row, col;
877 union utf8_char *p_row;
878 union decoded_attr attr;
879 FILE *fp;
880 int len;
881
882 fp = fdopen(fd, "w");
Brian Lovin1bf14812013-08-08 16:12:55 -0700883 if (fp == NULL){
884 close(fd);
885 return;
886 }
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500887 for (row = 0; row < terminal->height; row++) {
888 p_row = terminal_get_row(terminal, row);
889 for (col = 0; col < terminal->width; col++) {
Peng Wucfcc1112013-08-19 10:59:21 +0800890 if (p_row[col].ch == 0x200B) /* space glyph */
891 continue;
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500892 /* get the attributes for this character cell */
893 terminal_decode_attr(terminal, row, col, &attr);
894 if (!attr.attr.s)
895 continue;
896 len = strnlen((char *) p_row[col].byte, 4);
Kristian Høgsberg0dee6472012-07-01 21:25:41 -0400897 if (len > 0)
898 fwrite(p_row[col].byte, 1, len, fp);
899 if (len == 0 || col == terminal->width - 1) {
900 fwrite("\n", 1, 1, fp);
901 break;
902 }
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500903 }
904 }
905 fclose(fp);
906}
907
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500908struct glyph_run {
909 struct terminal *terminal;
910 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400911 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500912 union decoded_attr attr;
913 cairo_glyph_t glyphs[256], *g;
914};
915
916static void
917glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
918{
919 run->terminal = terminal;
920 run->cr = cr;
921 run->g = run->glyphs;
922 run->count = 0;
923 run->attr.key = 0;
924}
925
926static void
927glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
928{
929 cairo_scaled_font_t *font;
930
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500931 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
932 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300933 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500934 font = run->terminal->font_bold;
935 else
936 font = run->terminal->font_normal;
937 cairo_set_scaled_font(run->cr, font);
938 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300939 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500940
Callum Lowcay9d708b02011-01-12 20:06:17 +1300941 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
942 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500943 run->g = run->glyphs;
944 run->count = 0;
945 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300946 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500947}
948
949static void
950glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
951{
952 int num_glyphs;
953 cairo_scaled_font_t *font;
954
955 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
956
Callum Lowcay9d708b02011-01-12 20:06:17 +1300957 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500958 font = run->terminal->font_bold;
959 else
960 font = run->terminal->font_normal;
961
962 cairo_move_to(run->cr, x, y);
963 cairo_scaled_font_text_to_glyphs (font, x, y,
964 (char *) c->byte, 4,
965 &run->g, &num_glyphs,
966 NULL, NULL, NULL);
967 run->g += num_glyphs;
968 run->count += num_glyphs;
969}
970
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500971
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500972static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500973redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500974{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500975 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500976 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500977 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000978 int top_margin, side_margin;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600979 int row, col, cursor_x, cursor_y;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000980 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500981 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000982 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500983 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500984 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500985 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500986 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +0800987 double average_width;
Peng Wucfcc1112013-08-19 10:59:21 +0800988 double unichar_width;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500989
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500990 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500991 widget_get_allocation(terminal->widget, &allocation);
Alexander Larssonde79dd02013-05-22 14:41:34 +0200992 cr = widget_cairo_create(terminal->widget);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500993 cairo_rectangle(cr, allocation.x, allocation.y,
994 allocation.width, allocation.height);
995 cairo_clip(cr);
996 cairo_push_group(cr);
997
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500998 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500999 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001000 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001001
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05001002 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001003
Kristian Høgsberg59826582011-01-20 11:56:57 -05001004 extents = terminal->extents;
Peng Wuf291f202013-06-06 15:32:41 +08001005 average_width = terminal->average_width;
1006 side_margin = (allocation.width - terminal->width * average_width) / 2;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001007 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001008
Callum Lowcay30eeae52011-01-07 19:46:55 +00001009 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001010 cairo_translate(cr, allocation.x + side_margin,
1011 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001012 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001013 for (row = 0; row < terminal->height; row++) {
Peng Wucfcc1112013-08-19 10:59:21 +08001014 p_row = terminal_get_row(terminal, row);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001015 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001016 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -05001017 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001018
Callum Lowcay9d708b02011-01-12 20:06:17 +13001019 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -05001020 continue;
1021
Peng Wucfcc1112013-08-19 10:59:21 +08001022 if (is_wide(p_row[col]))
1023 unichar_width = 2 * average_width;
1024 else
1025 unichar_width = average_width;
1026
Callum Lowcay9d708b02011-01-12 20:06:17 +13001027 terminal_set_color(terminal, cr, attr.attr.bg);
Peng Wuf291f202013-06-06 15:32:41 +08001028 cairo_move_to(cr, col * average_width,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001029 row * extents.height);
Peng Wucfcc1112013-08-19 10:59:21 +08001030 cairo_rel_line_to(cr, unichar_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001031 cairo_rel_line_to(cr, 0, extents.height);
Peng Wucfcc1112013-08-19 10:59:21 +08001032 cairo_rel_line_to(cr, -unichar_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001033 cairo_close_path(cr);
1034 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001035 }
1036 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001037
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001038 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1039
1040 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001041 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001042 for (row = 0; row < terminal->height; row++) {
1043 p_row = terminal_get_row(terminal, row);
1044 for (col = 0; col < terminal->width; col++) {
1045 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -05001046 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001047
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001048 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001049
Peng Wuf291f202013-06-06 15:32:41 +08001050 text_x = col * average_width;
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001051 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +13001052 if (attr.attr.a & ATTRMASK_UNDERLINE) {
1053 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +00001054 cairo_move_to(cr, text_x, (double)text_y + 1.5);
Peng Wuf291f202013-06-06 15:32:41 +08001055 cairo_line_to(cr, text_x + average_width, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001056 cairo_stroke(cr);
1057 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -05001058
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001059 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001060 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001061 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001062
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001063 attr.key = ~0;
1064 glyph_run_flush(&run, attr);
1065
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001066 if ((terminal->mode & MODE_SHOW_CURSOR) &&
1067 !window_has_focus(terminal->window)) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001068 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001069
Callum Lowcay30eeae52011-01-07 19:46:55 +00001070 cairo_set_line_width(cr, 1);
Peng Wuf291f202013-06-06 15:32:41 +08001071 cairo_move_to(cr, terminal->column * average_width + d,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001072 terminal->row * extents.height + d);
Peng Wuf291f202013-06-06 15:32:41 +08001073 cairo_rel_line_to(cr, average_width - 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001074 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
Peng Wuf291f202013-06-06 15:32:41 +08001075 cairo_rel_line_to(cr, -average_width + 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001076 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001077
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001078 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001079 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001080
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001081 cairo_pop_group_to_source(cr);
1082 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001083 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001084 cairo_surface_destroy(surface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001085
1086 if (terminal->send_cursor_position) {
1087 cursor_x = side_margin + allocation.x +
Peng Wuf291f202013-06-06 15:32:41 +08001088 terminal->column * average_width;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001089 cursor_y = top_margin + allocation.y +
1090 terminal->row * extents.height;
1091 window_set_text_cursor_position(terminal->window,
1092 cursor_x, cursor_y);
1093 terminal->send_cursor_position = 0;
1094 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001095}
1096
1097static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001098terminal_write(struct terminal *terminal, const char *data, size_t length)
1099{
1100 if (write(terminal->master, data, length) < 0)
1101 abort();
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001102 terminal->send_cursor_position = 1;
Kristian Høgsberg26130862011-08-24 11:30:21 -04001103}
1104
1105static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001106terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001107
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001108static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001109handle_char(struct terminal *terminal, union utf8_char utf8);
1110
1111static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001112handle_sgr(struct terminal *terminal, int code);
1113
1114static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001115handle_term_parameter(struct terminal *terminal, int code, int sr)
1116{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001117 int i;
1118
Callum Lowcay67a201d2011-01-12 19:23:41 +13001119 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001120 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001121 case 1: /* DECCKM */
1122 if (sr) terminal->key_mode = KM_APPLICATION;
1123 else terminal->key_mode = KM_NORMAL;
1124 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001125 case 2: /* DECANM */
1126 /* No VT52 support yet */
1127 terminal->g0 = CS_US;
1128 terminal->g1 = CS_US;
1129 terminal->cs = terminal->g0;
1130 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001131 case 3: /* DECCOLM */
1132 if (sr)
1133 terminal_resize(terminal, 132, 24);
1134 else
1135 terminal_resize(terminal, 80, 24);
1136
1137 /* set columns, but also home cursor and clear screen */
1138 terminal->row = 0; terminal->column = 0;
1139 for (i = 0; i < terminal->height; i++) {
1140 memset(terminal_get_row(terminal, i),
1141 0, terminal->data_pitch);
1142 attr_init(terminal_get_attr_row(terminal, i),
1143 terminal->curr_attr, terminal->width);
1144 }
1145 break;
1146 case 5: /* DECSCNM */
1147 if (sr) terminal->mode |= MODE_INVERSE;
1148 else terminal->mode &= ~MODE_INVERSE;
1149 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001150 case 6: /* DECOM */
1151 terminal->origin_mode = sr;
1152 if (terminal->origin_mode)
1153 terminal->row = terminal->margin_top;
1154 else
1155 terminal->row = 0;
1156 terminal->column = 0;
1157 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001158 case 7: /* DECAWM */
1159 if (sr) terminal->mode |= MODE_AUTOWRAP;
1160 else terminal->mode &= ~MODE_AUTOWRAP;
1161 break;
1162 case 8: /* DECARM */
1163 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1164 else terminal->mode &= ~MODE_AUTOREPEAT;
1165 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001166 case 12: /* Very visible cursor (CVVIS) */
1167 /* FIXME: What do we do here. */
1168 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001169 case 25:
1170 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1171 else terminal->mode &= ~MODE_SHOW_CURSOR;
1172 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001173 case 1034: /* smm/rmm, meta mode on/off */
1174 /* ignore */
1175 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001176 case 1037: /* deleteSendsDel */
1177 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1178 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1179 break;
1180 case 1039: /* altSendsEscape */
1181 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1182 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1183 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001184 case 1049: /* rmcup/smcup, alternate screen */
1185 /* Ignore. Should be possible to implement,
1186 * but it's kind of annoying. */
1187 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001188 default:
1189 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1190 break;
1191 }
1192 } else {
1193 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001194 case 4: /* IRM */
1195 if (sr) terminal->mode |= MODE_IRM;
1196 else terminal->mode &= ~MODE_IRM;
1197 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001198 case 20: /* LNM */
1199 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1200 else terminal->mode &= ~MODE_LF_NEWLINE;
1201 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001202 default:
1203 fprintf(stderr, "Unknown parameter: %d\n", code);
1204 break;
1205 }
1206 }
1207}
1208
1209static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001210handle_dcs(struct terminal *terminal)
1211{
1212}
1213
1214static void
1215handle_osc(struct terminal *terminal)
1216{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001217 char *p;
1218 int code;
1219
1220 terminal->escape[terminal->escape_length++] = '\0';
1221 p = &terminal->escape[2];
1222 code = strtol(p, &p, 10);
1223 if (*p == ';') p++;
1224
1225 switch (code) {
1226 case 0: /* Icon name and window title */
1227 case 1: /* Icon label */
1228 case 2: /* Window title*/
1229 window_set_title(terminal->window, p);
1230 break;
1231 default:
1232 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1233 break;
1234 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001235}
1236
1237static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001238handle_escape(struct terminal *terminal)
1239{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001240 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001241 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001242 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001243 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001244 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001245 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001246 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001247
1248 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001249 i = 0;
1250 p = &terminal->escape[2];
1251 while ((isdigit(*p) || *p == ';') && i < 10) {
1252 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001253 if (!set[i]) {
1254 args[i] = 0;
1255 set[i] = 1;
1256 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001257 p++;
1258 i++;
1259 } else {
1260 args[i] = strtol(p, &p, 10);
1261 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001262 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001263 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001264
1265 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001266 case '@': /* ICH */
1267 count = set[0] ? args[0] : 1;
1268 if (count == 0) count = 1;
1269 terminal_shift_line(terminal, count);
1270 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001271 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001272 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001273 if (count == 0) count = 1;
1274 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001275 terminal->row -= count;
1276 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001277 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001278 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001279 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001280 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001281 if (count == 0) count = 1;
1282 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001283 terminal->row += count;
1284 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001285 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001286 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001287 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001288 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001289 if (count == 0) count = 1;
1290 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001291 terminal->column += count;
1292 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001293 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001294 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001295 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001296 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001297 if (count == 0) count = 1;
1298 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001299 terminal->column -= count;
1300 else
1301 terminal->column = 0;
1302 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001303 case 'E': /* CNL */
1304 count = set[0] ? args[0] : 1;
1305 if (terminal->row + count <= terminal->margin_bottom)
1306 terminal->row += count;
1307 else
1308 terminal->row = terminal->margin_bottom;
1309 terminal->column = 0;
1310 break;
1311 case 'F': /* CPL */
1312 count = set[0] ? args[0] : 1;
1313 if (terminal->row - count >= terminal->margin_top)
1314 terminal->row -= count;
1315 else
1316 terminal->row = terminal->margin_top;
1317 terminal->column = 0;
1318 break;
1319 case 'G': /* CHA */
1320 y = set[0] ? args[0] : 1;
1321 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1322
1323 terminal->column = y - 1;
1324 break;
1325 case 'f': /* HVP */
1326 case 'H': /* CUP */
1327 x = (set[1] ? args[1] : 1) - 1;
1328 x = x < 0 ? 0 :
1329 (x >= terminal->width ? terminal->width - 1 : x);
1330
1331 y = (set[0] ? args[0] : 1) - 1;
1332 if (terminal->origin_mode) {
1333 y += terminal->margin_top;
1334 y = y < terminal->margin_top ? terminal->margin_top :
1335 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1336 } else {
1337 y = y < 0 ? 0 :
1338 (y >= terminal->height ? terminal->height - 1 : y);
1339 }
1340
1341 terminal->row = y;
1342 terminal->column = x;
1343 break;
1344 case 'I': /* CHT */
1345 count = set[0] ? args[0] : 1;
1346 if (count == 0) count = 1;
1347 while (count > 0 && terminal->column < terminal->width) {
1348 if (terminal->tab_ruler[terminal->column]) count--;
1349 terminal->column++;
1350 }
1351 terminal->column--;
1352 break;
1353 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001354 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001355 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001356 if (!set[0] || args[0] == 0 || args[0] > 2) {
1357 memset(&row[terminal->column],
1358 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1359 attr_init(&attr_row[terminal->column],
1360 terminal->curr_attr, terminal->width - terminal->column);
1361 for (i = terminal->row + 1; i < terminal->height; i++) {
1362 memset(terminal_get_row(terminal, i),
1363 0, terminal->data_pitch);
1364 attr_init(terminal_get_attr_row(terminal, i),
1365 terminal->curr_attr, terminal->width);
1366 }
1367 } else if (args[0] == 1) {
1368 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1369 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1370 for (i = 0; i < terminal->row; i++) {
1371 memset(terminal_get_row(terminal, i),
1372 0, terminal->data_pitch);
1373 attr_init(terminal_get_attr_row(terminal, i),
1374 terminal->curr_attr, terminal->width);
1375 }
1376 } else if (args[0] == 2) {
1377 for (i = 0; i < terminal->height; i++) {
1378 memset(terminal_get_row(terminal, i),
1379 0, terminal->data_pitch);
1380 attr_init(terminal_get_attr_row(terminal, i),
1381 terminal->curr_attr, terminal->width);
1382 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001383 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001384 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001385 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001386 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001387 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001388 if (!set[0] || args[0] == 0 || args[0] > 2) {
1389 memset(&row[terminal->column], 0,
1390 (terminal->width - terminal->column) * sizeof(union utf8_char));
1391 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1392 terminal->width - terminal->column);
1393 } else if (args[0] == 1) {
1394 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1395 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1396 } else if (args[0] == 2) {
1397 memset(row, 0, terminal->data_pitch);
1398 attr_init(attr_row, terminal->curr_attr, terminal->width);
1399 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001400 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001401 case 'L': /* IL */
1402 count = set[0] ? args[0] : 1;
1403 if (count == 0) count = 1;
1404 if (terminal->row >= terminal->margin_top &&
1405 terminal->row < terminal->margin_bottom)
1406 {
1407 top = terminal->margin_top;
1408 terminal->margin_top = terminal->row;
1409 terminal_scroll(terminal, 0 - count);
1410 terminal->margin_top = top;
1411 } else if (terminal->row == terminal->margin_bottom) {
1412 memset(terminal_get_row(terminal, terminal->row),
1413 0, terminal->data_pitch);
1414 attr_init(terminal_get_attr_row(terminal, terminal->row),
1415 terminal->curr_attr, terminal->width);
1416 }
1417 break;
1418 case 'M': /* DL */
1419 count = set[0] ? args[0] : 1;
1420 if (count == 0) count = 1;
1421 if (terminal->row >= terminal->margin_top &&
1422 terminal->row < terminal->margin_bottom)
1423 {
1424 top = terminal->margin_top;
1425 terminal->margin_top = terminal->row;
1426 terminal_scroll(terminal, count);
1427 terminal->margin_top = top;
1428 } else if (terminal->row == terminal->margin_bottom) {
1429 memset(terminal_get_row(terminal, terminal->row),
1430 0, terminal->data_pitch);
1431 }
1432 break;
1433 case 'P': /* DCH */
1434 count = set[0] ? args[0] : 1;
1435 if (count == 0) count = 1;
1436 terminal_shift_line(terminal, 0 - count);
1437 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001438 case 'S': /* SU */
1439 terminal_scroll(terminal, set[0] ? args[0] : 1);
1440 break;
1441 case 'T': /* SD */
1442 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1443 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001444 case 'X': /* ECH */
1445 count = set[0] ? args[0] : 1;
1446 if (count == 0) count = 1;
1447 if ((terminal->column + count) > terminal->width)
1448 count = terminal->width - terminal->column;
1449 row = terminal_get_row(terminal, terminal->row);
1450 attr_row = terminal_get_attr_row(terminal, terminal->row);
1451 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1452 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1453 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001454 case 'Z': /* CBT */
1455 count = set[0] ? args[0] : 1;
1456 if (count == 0) count = 1;
1457 while (count > 0 && terminal->column >= 0) {
1458 if (terminal->tab_ruler[terminal->column]) count--;
1459 terminal->column--;
1460 }
1461 terminal->column++;
1462 break;
1463 case '`': /* HPA */
1464 y = set[0] ? args[0] : 1;
1465 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1466
1467 terminal->column = y - 1;
1468 break;
1469 case 'b': /* REP */
1470 count = set[0] ? args[0] : 1;
1471 if (count == 0) count = 1;
1472 if (terminal->last_char.byte[0])
1473 for (i = 0; i < count; i++)
1474 handle_char(terminal, terminal->last_char);
1475 terminal->last_char.byte[0] = 0;
1476 break;
1477 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001478 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001479 break;
1480 case 'd': /* VPA */
1481 x = set[0] ? args[0] : 1;
1482 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1483
1484 terminal->row = x - 1;
1485 break;
1486 case 'g': /* TBC */
1487 if (!set[0] || args[0] == 0) {
1488 terminal->tab_ruler[terminal->column] = 0;
1489 } else if (args[0] == 3) {
1490 memset(terminal->tab_ruler, 0, terminal->width);
1491 }
1492 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001493 case 'h': /* SM */
1494 for(i = 0; i < 10 && set[i]; i++) {
1495 handle_term_parameter(terminal, args[i], 1);
1496 }
1497 break;
1498 case 'l': /* RM */
1499 for(i = 0; i < 10 && set[i]; i++) {
1500 handle_term_parameter(terminal, args[i], 0);
1501 }
1502 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001503 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001504 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001505 if (i <= 7 && set[i] && set[i + 1] &&
1506 set[i + 2] && args[i + 1] == 5)
1507 {
1508 if (args[i] == 38) {
1509 handle_sgr(terminal, args[i + 2] + 256);
1510 break;
1511 } else if (args[i] == 48) {
1512 handle_sgr(terminal, args[i + 2] + 512);
1513 break;
1514 }
1515 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001516 if(set[i]) {
1517 handle_sgr(terminal, args[i]);
1518 } else if(i == 0) {
1519 handle_sgr(terminal, 0);
1520 break;
1521 } else {
1522 break;
1523 }
1524 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001525 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001526 case 'n': /* DSR */
1527 i = set[0] ? args[0] : 0;
1528 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001529 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001530 } else if (i == 6) {
1531 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1532 terminal->origin_mode ?
1533 terminal->row+terminal->margin_top : terminal->row+1,
1534 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001535 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001536 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001537 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001538 case 'r':
1539 if(!set[0]) {
1540 terminal->margin_top = 0;
1541 terminal->margin_bottom = terminal->height-1;
1542 terminal->row = 0;
1543 terminal->column = 0;
1544 } else {
1545 top = (set[0] ? args[0] : 1) - 1;
1546 top = top < 0 ? 0 :
1547 (top >= terminal->height ? terminal->height - 1 : top);
1548 bottom = (set[1] ? args[1] : 1) - 1;
1549 bottom = bottom < 0 ? 0 :
1550 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1551 if(bottom > top) {
1552 terminal->margin_top = top;
1553 terminal->margin_bottom = bottom;
1554 } else {
1555 terminal->margin_top = 0;
1556 terminal->margin_bottom = terminal->height-1;
1557 }
1558 if(terminal->origin_mode)
1559 terminal->row = terminal->margin_top;
1560 else
1561 terminal->row = 0;
1562 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001563 }
1564 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001565 case 's':
1566 terminal->saved_row = terminal->row;
1567 terminal->saved_column = terminal->column;
1568 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001569 case 't': /* windowOps */
1570 if (!set[0]) break;
1571 switch (args[0]) {
1572 case 4: /* resize px */
1573 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001574 widget_schedule_resize(terminal->widget,
1575 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001576 }
1577 break;
1578 case 8: /* resize ch */
1579 if (set[1] && set[2]) {
1580 terminal_resize(terminal, args[2], args[1]);
1581 }
1582 break;
1583 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001584 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001585 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1586 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001587 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001588 break;
1589 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001590 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001591 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1592 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001593 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001594 break;
1595 case 18: /* report ch */
1596 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1597 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001598 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001599 break;
1600 case 21: /* report title */
1601 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1602 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001603 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001604 break;
1605 default:
1606 if (args[0] >= 24)
1607 terminal_resize(terminal, terminal->width, args[0]);
1608 else
1609 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1610 break;
1611 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001612 case 'u':
1613 terminal->row = terminal->saved_row;
1614 terminal->column = terminal->saved_column;
1615 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001616 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001617 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001618 break;
1619 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001620}
1621
1622static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001623handle_non_csi_escape(struct terminal *terminal, char code)
1624{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001625 switch(code) {
1626 case 'M': /* RI */
1627 terminal->row -= 1;
1628 if(terminal->row < terminal->margin_top) {
1629 terminal->row = terminal->margin_top;
1630 terminal_scroll(terminal, -1);
1631 }
1632 break;
1633 case 'E': /* NEL */
1634 terminal->column = 0;
1635 // fallthrough
1636 case 'D': /* IND */
1637 terminal->row += 1;
1638 if(terminal->row > terminal->margin_bottom) {
1639 terminal->row = terminal->margin_bottom;
1640 terminal_scroll(terminal, +1);
1641 }
1642 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001643 case 'c': /* RIS */
1644 terminal_init(terminal);
1645 break;
1646 case 'H': /* HTS */
1647 terminal->tab_ruler[terminal->column] = 1;
1648 break;
1649 case '7': /* DECSC */
1650 terminal->saved_row = terminal->row;
1651 terminal->saved_column = terminal->column;
1652 terminal->saved_attr = terminal->curr_attr;
1653 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001654 terminal->saved_cs = terminal->cs;
1655 terminal->saved_g0 = terminal->g0;
1656 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001657 break;
1658 case '8': /* DECRC */
1659 terminal->row = terminal->saved_row;
1660 terminal->column = terminal->saved_column;
1661 terminal->curr_attr = terminal->saved_attr;
1662 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001663 terminal->cs = terminal->saved_cs;
1664 terminal->g0 = terminal->saved_g0;
1665 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001666 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001667 case '=': /* DECPAM */
1668 terminal->key_mode = KM_APPLICATION;
1669 break;
1670 case '>': /* DECPNM */
1671 terminal->key_mode = KM_NORMAL;
1672 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001673 default:
1674 fprintf(stderr, "Unknown escape code: %c\n", code);
1675 break;
1676 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001677}
1678
1679static void
1680handle_special_escape(struct terminal *terminal, char special, char code)
1681{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001682 int i, numChars;
1683
1684 if (special == '#') {
1685 switch(code) {
1686 case '8':
1687 /* fill with 'E', no cheap way to do this */
1688 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1689 numChars = terminal->width * terminal->height;
1690 for(i = 0; i < numChars; i++) {
1691 terminal->data[i].byte[0] = 'E';
1692 }
1693 break;
1694 default:
1695 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1696 break;
1697 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001698 } else if (special == '(' || special == ')') {
1699 switch(code) {
1700 case '0':
1701 if (special == '(')
1702 terminal->g0 = CS_SPECIAL;
1703 else
1704 terminal->g1 = CS_SPECIAL;
1705 break;
1706 case 'A':
1707 if (special == '(')
1708 terminal->g0 = CS_UK;
1709 else
1710 terminal->g1 = CS_UK;
1711 break;
1712 case 'B':
1713 if (special == '(')
1714 terminal->g0 = CS_US;
1715 else
1716 terminal->g1 = CS_US;
1717 break;
1718 default:
1719 fprintf(stderr, "Unknown character set %c\n", code);
1720 break;
1721 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001722 } else {
1723 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1724 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001725}
1726
1727static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001728handle_sgr(struct terminal *terminal, int code)
1729{
1730 switch(code) {
1731 case 0:
1732 terminal->curr_attr = terminal->color_scheme->default_attr;
1733 break;
1734 case 1:
1735 terminal->curr_attr.a |= ATTRMASK_BOLD;
1736 if (terminal->curr_attr.fg < 8)
1737 terminal->curr_attr.fg += 8;
1738 break;
1739 case 4:
1740 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1741 break;
1742 case 5:
1743 terminal->curr_attr.a |= ATTRMASK_BLINK;
1744 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001745 case 8:
1746 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1747 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001748 case 2:
1749 case 21:
1750 case 22:
1751 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1752 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1753 terminal->curr_attr.fg -= 8;
1754 break;
1755 case 24:
1756 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1757 break;
1758 case 25:
1759 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1760 break;
1761 case 7:
1762 case 26:
1763 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1764 break;
1765 case 27:
1766 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1767 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001768 case 28:
1769 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1770 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001771 case 39:
1772 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1773 break;
1774 case 49:
1775 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1776 break;
1777 default:
1778 if(code >= 30 && code <= 37) {
1779 terminal->curr_attr.fg = code - 30;
1780 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1781 terminal->curr_attr.fg += 8;
1782 } else if(code >= 40 && code <= 47) {
1783 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001784 } else if (code >= 90 && code <= 97) {
1785 terminal->curr_attr.fg = code - 90 + 8;
1786 } else if (code >= 100 && code <= 107) {
1787 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001788 } else if(code >= 256 && code < 512) {
1789 terminal->curr_attr.fg = code - 256;
1790 } else if(code >= 512 && code < 768) {
1791 terminal->curr_attr.bg = code - 512;
1792 } else {
1793 fprintf(stderr, "Unknown SGR code: %d\n", code);
1794 }
1795 break;
1796 }
1797}
1798
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001799/* Returns 1 if c was special, otherwise 0 */
1800static int
1801handle_special_char(struct terminal *terminal, char c)
1802{
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001803 union utf8_char *row;
1804 struct attr *attr_row;
1805
1806 row = terminal_get_row(terminal, terminal->row);
1807 attr_row = terminal_get_attr_row(terminal, terminal->row);
1808
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001809 switch(c) {
1810 case '\r':
1811 terminal->column = 0;
1812 break;
1813 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001814 if (terminal->mode & MODE_LF_NEWLINE) {
1815 terminal->column = 0;
1816 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001817 /* fallthrough */
1818 case '\v':
1819 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001820 terminal->row++;
1821 if(terminal->row > terminal->margin_bottom) {
1822 terminal->row = terminal->margin_bottom;
1823 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001824 }
1825
1826 break;
1827 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001828 while (terminal->column < terminal->width) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001829 if (terminal->mode & MODE_IRM)
1830 terminal_shift_line(terminal, +1);
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001831
1832 if (row[terminal->column].byte[0] == '\0') {
1833 row[terminal->column].byte[0] = ' ';
1834 row[terminal->column].byte[1] = '\0';
1835 attr_row[terminal->column] = terminal->curr_attr;
1836 }
1837
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001838 terminal->column++;
Kristian Høgsbergcca3c2f2012-06-20 15:56:13 -04001839 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001840 }
1841 if (terminal->column >= terminal->width) {
1842 terminal->column = terminal->width - 1;
1843 }
1844
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001845 break;
1846 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001847 if (terminal->column >= terminal->width) {
1848 terminal->column = terminal->width - 2;
1849 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001850 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001851 } else if (terminal->mode & MODE_AUTOWRAP) {
1852 terminal->column = terminal->width - 1;
1853 terminal->row -= 1;
1854 if (terminal->row < terminal->margin_top) {
1855 terminal->row = terminal->margin_top;
1856 terminal_scroll(terminal, -1);
1857 }
1858 }
1859
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001860 break;
1861 case '\a':
1862 /* Bell */
1863 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001864 case '\x0E': /* SO */
1865 terminal->cs = terminal->g1;
1866 break;
1867 case '\x0F': /* SI */
1868 terminal->cs = terminal->g0;
1869 break;
Kristian Høgsberg2a1aa4e2012-08-03 09:37:05 -04001870 case '\0':
1871 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001872 default:
1873 return 0;
1874 }
1875
1876 return 1;
1877}
1878
1879static void
1880handle_char(struct terminal *terminal, union utf8_char utf8)
1881{
1882 union utf8_char *row;
1883 struct attr *attr_row;
1884
1885 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001886
1887 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001888
1889 /* There are a whole lot of non-characters, control codes,
1890 * and formatting codes that should probably be ignored,
1891 * for example: */
1892 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1893 /* BOM, ignore */
1894 return;
1895 }
1896
1897 /* Some of these non-characters should be translated, e.g.: */
1898 if (utf8.byte[0] < 32) {
1899 utf8.byte[0] = utf8.byte[0] + 64;
1900 }
1901
1902 /* handle right margin effects */
1903 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001904 if (terminal->mode & MODE_AUTOWRAP) {
1905 terminal->column = 0;
1906 terminal->row += 1;
1907 if (terminal->row > terminal->margin_bottom) {
1908 terminal->row = terminal->margin_bottom;
1909 terminal_scroll(terminal, +1);
1910 }
1911 } else {
1912 terminal->column--;
1913 }
1914 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001915
1916 row = terminal_get_row(terminal, terminal->row);
1917 attr_row = terminal_get_attr_row(terminal, terminal->row);
1918
Callum Lowcay69e96582011-01-07 19:47:00 +00001919 if (terminal->mode & MODE_IRM)
1920 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001921 row[terminal->column] = utf8;
1922 attr_row[terminal->column++] = terminal->curr_attr;
1923
Peng Wucfcc1112013-08-19 10:59:21 +08001924 /* cursor jump for wide character. */
1925 if (is_wide(utf8))
1926 row[terminal->column++].ch = 0x200B; /* space glyph */
1927
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001928 if (utf8.ch != terminal->last_char.ch)
1929 terminal->last_char = utf8;
1930}
1931
Callum Lowcay30eeae52011-01-07 19:46:55 +00001932static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001933escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1934{
1935 int len, i;
1936
1937 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1938 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1939 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1940 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1941 else len = 1; /* Invalid, cannot happen */
1942
1943 if (terminal->escape_length + len <= MAX_ESCAPE) {
1944 for (i = 0; i < len; i++)
1945 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1946 terminal->escape_length += len;
1947 } else if (terminal->escape_length < MAX_ESCAPE) {
1948 terminal->escape[terminal->escape_length++] = 0;
1949 }
1950}
1951
1952static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001953terminal_data(struct terminal *terminal, const char *data, size_t length)
1954{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001955 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001956 union utf8_char utf8;
1957 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001958
1959 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001960 parser_state =
1961 utf8_next_char(&terminal->state_machine, data[i]);
1962 switch(parser_state) {
1963 case utf8state_accept:
1964 utf8.ch = terminal->state_machine.s.ch;
1965 break;
1966 case utf8state_reject:
1967 /* the unicode replacement character */
1968 utf8.byte[0] = 0xEF;
1969 utf8.byte[1] = 0xBF;
1970 utf8.byte[2] = 0xBD;
1971 utf8.byte[3] = 0x00;
1972 break;
1973 default:
1974 continue;
1975 }
1976
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001977 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001978 switch (terminal->state) {
1979 case escape_state_escape:
1980 escape_append_utf8(terminal, utf8);
1981 switch (utf8.byte[0]) {
1982 case 'P': /* DCS */
1983 terminal->state = escape_state_dcs;
1984 break;
1985 case '[': /* CSI */
1986 terminal->state = escape_state_csi;
1987 break;
1988 case ']': /* OSC */
1989 terminal->state = escape_state_osc;
1990 break;
1991 case '#':
1992 case '(':
1993 case ')': /* special */
1994 terminal->state = escape_state_special;
1995 break;
1996 case '^': /* PM (not implemented) */
1997 case '_': /* APC (not implemented) */
1998 terminal->state = escape_state_ignore;
1999 break;
2000 default:
2001 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002002 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13002003 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002004 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002005 continue;
2006 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002007 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
2008 /* do nothing */
2009 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002010 terminal->escape_flags |= ESC_FLAG_WHAT;
2011 } else if (utf8.byte[0] == '>') {
2012 terminal->escape_flags |= ESC_FLAG_GT;
2013 } else if (utf8.byte[0] == '!') {
2014 terminal->escape_flags |= ESC_FLAG_BANG;
2015 } else if (utf8.byte[0] == '$') {
2016 terminal->escape_flags |= ESC_FLAG_CASH;
2017 } else if (utf8.byte[0] == '\'') {
2018 terminal->escape_flags |= ESC_FLAG_SQUOTE;
2019 } else if (utf8.byte[0] == '"') {
2020 terminal->escape_flags |= ESC_FLAG_DQUOTE;
2021 } else if (utf8.byte[0] == ' ') {
2022 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002023 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002024 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002025 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13002026 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002027 }
2028
2029 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
2030 utf8.byte[0] == '`')
2031 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002032 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002033 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002034 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002035 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002036 continue;
2037 case escape_state_inner_escape:
2038 if (utf8.byte[0] == '\\') {
2039 terminal->state = escape_state_normal;
2040 if (terminal->outer_state == escape_state_dcs) {
2041 handle_dcs(terminal);
2042 } else if (terminal->outer_state == escape_state_osc) {
2043 handle_osc(terminal);
2044 }
2045 } else if (utf8.byte[0] == '\e') {
2046 terminal->state = terminal->outer_state;
2047 escape_append_utf8(terminal, utf8);
2048 if (terminal->escape_length >= MAX_ESCAPE)
2049 terminal->state = escape_state_normal;
2050 } else {
2051 terminal->state = terminal->outer_state;
2052 if (terminal->escape_length < MAX_ESCAPE)
2053 terminal->escape[terminal->escape_length++] = '\e';
2054 escape_append_utf8(terminal, utf8);
2055 if (terminal->escape_length >= MAX_ESCAPE)
2056 terminal->state = escape_state_normal;
2057 }
2058 continue;
2059 case escape_state_dcs:
2060 case escape_state_osc:
2061 case escape_state_ignore:
2062 if (utf8.byte[0] == '\e') {
2063 terminal->outer_state = terminal->state;
2064 terminal->state = escape_state_inner_escape;
2065 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
2066 terminal->state = escape_state_normal;
2067 handle_osc(terminal);
2068 } else {
2069 escape_append_utf8(terminal, utf8);
2070 if (terminal->escape_length >= MAX_ESCAPE)
2071 terminal->state = escape_state_normal;
2072 }
2073 continue;
2074 case escape_state_special:
2075 escape_append_utf8(terminal, utf8);
2076 terminal->state = escape_state_normal;
2077 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2078 handle_special_escape(terminal, terminal->escape[1],
2079 utf8.byte[0]);
2080 }
2081 continue;
2082 default:
2083 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002084 }
2085
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002086 /* this is valid, because ASCII characters are never used to
2087 * introduce a multibyte sequence in UTF-8 */
2088 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002089 terminal->state = escape_state_escape;
2090 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002091 terminal->escape[0] = '\e';
2092 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002093 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002094 } else {
2095 handle_char(terminal, utf8);
2096 } /* if */
2097 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002098
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002099 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002100}
2101
2102static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002103data_source_target(void *data,
2104 struct wl_data_source *source, const char *mime_type)
2105{
2106 fprintf(stderr, "data_source_target, %s\n", mime_type);
2107}
2108
2109static void
2110data_source_send(void *data,
2111 struct wl_data_source *source,
2112 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002113{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002114 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002115
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002116 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002117}
2118
2119static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002120data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002121{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002122 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002123}
2124
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002125static const struct wl_data_source_listener data_source_listener = {
2126 data_source_target,
2127 data_source_send,
2128 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002129};
2130
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002131static void
2132fullscreen_handler(struct window *window, void *data)
2133{
2134 struct terminal *terminal = data;
2135
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002136 window_set_fullscreen(window, !window_is_fullscreen(terminal->window));
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002137}
2138
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002139static void
2140close_handler(struct window *window, void *data)
2141{
2142 struct terminal *terminal = data;
2143
2144 terminal_destroy(terminal);
2145}
2146
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002147static int
2148handle_bound_key(struct terminal *terminal,
2149 struct input *input, uint32_t sym, uint32_t time)
2150{
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002151 struct terminal *new_terminal;
2152
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002153 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002154 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002155 /* Cut selection; terminal doesn't do cut, fall
2156 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002157 case XKB_KEY_C:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002158 terminal->selection =
2159 display_create_data_source(terminal->display);
2160 wl_data_source_offer(terminal->selection,
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002161 "text/plain;charset=utf-8");
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002162 wl_data_source_add_listener(terminal->selection,
2163 &data_source_listener, terminal);
Kristian Høgsbergfee91be2012-06-02 21:21:41 -04002164 input_set_selection(input, terminal->selection,
2165 display_get_serial(terminal->display));
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002166 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002167 case XKB_KEY_V:
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002168 input_receive_selection_data_to_fd(input,
2169 "text/plain;charset=utf-8",
2170 terminal->master);
2171
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002172 return 1;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002173
2174 case XKB_KEY_N:
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002175 new_terminal = terminal_create(terminal->display);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002176 if (terminal_run(new_terminal, option_shell))
2177 terminal_destroy(new_terminal);
2178
2179 return 1;
2180
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002181 default:
2182 return 0;
2183 }
2184}
2185
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002186static void
2187key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002188 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2189 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002190{
2191 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002192 char ch[MAX_RESPONSE];
Kristian Høgsberg88fd4082012-06-21 15:55:03 -04002193 uint32_t modifiers, serial;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002194 int ret, len = 0;
2195 bool convert_utf8 = true;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002196
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002197 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002198 if ((modifiers & MOD_CONTROL_MASK) &&
2199 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002200 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2201 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002202 return;
2203
Daniel Stonea8468712012-11-07 17:51:35 +11002204 /* Map keypad symbols to 'normal' equivalents before processing */
2205 switch (sym) {
2206 case XKB_KEY_KP_Space:
2207 sym = XKB_KEY_space;
2208 break;
2209 case XKB_KEY_KP_Tab:
2210 sym = XKB_KEY_Tab;
2211 break;
2212 case XKB_KEY_KP_Enter:
2213 sym = XKB_KEY_Return;
2214 break;
2215 case XKB_KEY_KP_Left:
2216 sym = XKB_KEY_Left;
2217 break;
2218 case XKB_KEY_KP_Up:
2219 sym = XKB_KEY_Up;
2220 break;
2221 case XKB_KEY_KP_Right:
2222 sym = XKB_KEY_Right;
2223 break;
2224 case XKB_KEY_KP_Down:
2225 sym = XKB_KEY_Down;
2226 break;
2227 case XKB_KEY_KP_Equal:
2228 sym = XKB_KEY_equal;
2229 break;
2230 case XKB_KEY_KP_Multiply:
2231 sym = XKB_KEY_asterisk;
2232 break;
2233 case XKB_KEY_KP_Add:
2234 sym = XKB_KEY_plus;
2235 break;
2236 case XKB_KEY_KP_Separator:
2237 /* Note this is actually locale-dependent and should mostly be
2238 * a comma. But leave it as period until we one day start
2239 * doing the right thing. */
2240 sym = XKB_KEY_period;
2241 break;
2242 case XKB_KEY_KP_Subtract:
2243 sym = XKB_KEY_minus;
2244 break;
2245 case XKB_KEY_KP_Decimal:
2246 sym = XKB_KEY_period;
2247 break;
2248 case XKB_KEY_KP_Divide:
2249 sym = XKB_KEY_slash;
2250 break;
2251 case XKB_KEY_KP_0:
2252 case XKB_KEY_KP_1:
2253 case XKB_KEY_KP_2:
2254 case XKB_KEY_KP_3:
2255 case XKB_KEY_KP_4:
2256 case XKB_KEY_KP_5:
2257 case XKB_KEY_KP_6:
2258 case XKB_KEY_KP_7:
2259 case XKB_KEY_KP_8:
2260 case XKB_KEY_KP_9:
2261 sym = (sym - XKB_KEY_KP_0) + XKB_KEY_0;
2262 break;
2263 default:
2264 break;
2265 }
2266
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002267 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002268 case XKB_KEY_BackSpace:
Kristian Høgsbergb7f94bf2012-06-21 12:29:36 -04002269 if (modifiers & MOD_ALT_MASK)
2270 ch[len++] = 0x1b;
Kristian Høgsberg71a4cf42012-06-20 17:57:56 -04002271 ch[len++] = 0x7f;
2272 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002273 case XKB_KEY_Tab:
2274 case XKB_KEY_Linefeed:
2275 case XKB_KEY_Clear:
2276 case XKB_KEY_Pause:
2277 case XKB_KEY_Scroll_Lock:
2278 case XKB_KEY_Sys_Req:
2279 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002280 ch[len++] = sym & 0x7f;
2281 break;
2282
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002283 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002284 if (terminal->mode & MODE_LF_NEWLINE) {
2285 ch[len++] = 0x0D;
2286 ch[len++] = 0x0A;
2287 } else {
2288 ch[len++] = 0x0D;
2289 }
2290 break;
2291
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002292 case XKB_KEY_Shift_L:
2293 case XKB_KEY_Shift_R:
2294 case XKB_KEY_Control_L:
2295 case XKB_KEY_Control_R:
2296 case XKB_KEY_Alt_L:
2297 case XKB_KEY_Alt_R:
Kristian Høgsberg22fbcf72012-06-22 12:18:56 -04002298 case XKB_KEY_Meta_L:
2299 case XKB_KEY_Meta_R:
2300 case XKB_KEY_Super_L:
2301 case XKB_KEY_Super_R:
2302 case XKB_KEY_Hyper_L:
2303 case XKB_KEY_Hyper_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002304 break;
2305
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002306 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002307 len = function_key_response('[', 2, modifiers, '~', ch);
2308 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002309 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002310 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2311 ch[len++] = '\x04';
2312 } else {
2313 len = function_key_response('[', 3, modifiers, '~', ch);
2314 }
2315 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002316 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002317 len = function_key_response('[', 5, modifiers, '~', ch);
2318 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002319 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002320 len = function_key_response('[', 6, modifiers, '~', ch);
2321 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002322 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002323 len = function_key_response('O', 1, modifiers, 'P', ch);
2324 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002325 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002326 len = function_key_response('O', 1, modifiers, 'Q', ch);
2327 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002328 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002329 len = function_key_response('O', 1, modifiers, 'R', ch);
2330 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002331 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002332 len = function_key_response('O', 1, modifiers, 'S', ch);
2333 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002334 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002335 len = function_key_response('[', 15, modifiers, '~', ch);
2336 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002337 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002338 len = function_key_response('[', 17, modifiers, '~', ch);
2339 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002340 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002341 len = function_key_response('[', 18, modifiers, '~', ch);
2342 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002343 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002344 len = function_key_response('[', 19, modifiers, '~', ch);
2345 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002346 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002347 len = function_key_response('[', 20, modifiers, '~', ch);
2348 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002349 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002350 len = function_key_response('[', 21, modifiers, '~', ch);
2351 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002352 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002353 len = function_key_response('[', 24, modifiers, '~', ch);
2354 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002355 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002356 /* Handle special keys with alternate mappings */
2357 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2358 if (len != 0) break;
2359
Kristian Høgsberg70163132012-05-08 15:55:39 -04002360 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002361 if (sym >= '3' && sym <= '7')
2362 sym = (sym & 0x1f) + 8;
2363
2364 if (!((sym >= '!' && sym <= '/') ||
2365 (sym >= '8' && sym <= '?') ||
2366 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2367 else if (sym == '2') sym = 0x00;
2368 else if (sym == '/') sym = 0x1F;
2369 else if (sym == '8' || sym == '?') sym = 0x7F;
Kristian Høgsbergae9e0732012-06-21 12:30:15 -04002370 }
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002371 if (modifiers & MOD_ALT_MASK) {
2372 if (terminal->mode & MODE_ALT_SENDS_ESC) {
2373 ch[len++] = 0x1b;
2374 } else {
2375 sym = sym | 0x80;
2376 convert_utf8 = false;
2377 }
Callum Lowcay7e08e902011-01-07 19:47:02 +00002378 }
2379
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002380 if ((sym < 128) ||
2381 (!convert_utf8 && sym < 256)) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002382 ch[len++] = sym;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002383 } else {
2384 ret = xkb_keysym_to_utf8(sym, ch + len,
2385 MAX_RESPONSE - len);
2386 if (ret < 0)
2387 fprintf(stderr,
2388 "Warning: buffer too small to encode "
2389 "UTF8 character\n");
2390 else
2391 len += ret;
2392 }
2393
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002394 break;
2395 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002396
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002397 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04002398 terminal_write(terminal, ch, len);
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002399
2400 /* Hide cursor, except if this was coming from a
2401 * repeating key press. */
2402 serial = display_get_serial(terminal->display);
2403 if (terminal->hide_cursor_serial != serial) {
2404 input_set_pointer_image(input, CURSOR_BLANK);
2405 terminal->hide_cursor_serial = serial;
2406 }
2407 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002408}
2409
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002410static void
2411keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002412 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002413{
2414 struct terminal *terminal = data;
2415
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002416 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002417}
2418
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002419static int wordsep(int ch)
2420{
2421 const char extra[] = "-,./?%&#:_=+@~";
2422
Andre Heider552d12b2012-08-02 20:59:43 +02002423 if (ch > 127)
2424 return 1;
2425
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002426 return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
2427}
2428
2429static int
2430recompute_selection(struct terminal *terminal)
2431{
2432 struct rectangle allocation;
2433 int col, x, width, height;
2434 int start_row, end_row;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002435 int word_start, eol;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002436 int side_margin, top_margin;
2437 int start_x, end_x;
2438 int cw, ch;
2439 union utf8_char *data;
2440
Peng Wuf291f202013-06-06 15:32:41 +08002441 cw = terminal->average_width;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002442 ch = terminal->extents.height;
2443 widget_get_allocation(terminal->widget, &allocation);
2444 width = terminal->width * cw;
2445 height = terminal->height * ch;
2446 side_margin = allocation.x + (allocation.width - width) / 2;
2447 top_margin = allocation.y + (allocation.height - height) / 2;
2448
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002449 start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1;
2450 end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1;
2451
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002452 if (start_row < end_row ||
2453 (start_row == end_row &&
2454 terminal->selection_start_x < terminal->selection_end_x)) {
2455 terminal->selection_start_row = start_row;
2456 terminal->selection_end_row = end_row;
2457 start_x = terminal->selection_start_x;
2458 end_x = terminal->selection_end_x;
2459 } else {
2460 terminal->selection_start_row = end_row;
2461 terminal->selection_end_row = start_row;
2462 start_x = terminal->selection_end_x;
2463 end_x = terminal->selection_start_x;
2464 }
2465
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002466 eol = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002467 if (terminal->selection_start_row < 0) {
2468 terminal->selection_start_row = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002469 terminal->selection_start_col = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002470 } else {
2471 x = side_margin + cw / 2;
2472 data = terminal_get_row(terminal,
2473 terminal->selection_start_row);
2474 word_start = 0;
2475 for (col = 0; col < terminal->width; col++, x += cw) {
2476 if (col == 0 || wordsep(data[col - 1].ch))
2477 word_start = col;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002478 if (data[col].ch != 0)
2479 eol = col + 1;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002480 if (start_x < x)
2481 break;
2482 }
2483
2484 switch (terminal->dragging) {
2485 case SELECT_LINE:
2486 terminal->selection_start_col = 0;
2487 break;
2488 case SELECT_WORD:
2489 terminal->selection_start_col = word_start;
2490 break;
2491 case SELECT_CHAR:
2492 terminal->selection_start_col = col;
2493 break;
2494 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002495 }
2496
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002497 if (terminal->selection_end_row >= terminal->height) {
2498 terminal->selection_end_row = terminal->height;
2499 terminal->selection_end_col = 0;
2500 } else {
2501 x = side_margin + cw / 2;
2502 data = terminal_get_row(terminal, terminal->selection_end_row);
2503 for (col = 0; col < terminal->width; col++, x += cw) {
2504 if (terminal->dragging == SELECT_CHAR && end_x < x)
2505 break;
2506 if (terminal->dragging == SELECT_WORD &&
2507 end_x < x && wordsep(data[col].ch))
2508 break;
2509 }
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002510 terminal->selection_end_col = col;
2511 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002512
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002513 if (terminal->selection_end_col != terminal->selection_start_col ||
2514 terminal->selection_start_row != terminal->selection_end_row) {
2515 col = terminal->selection_end_col;
2516 if (col > 0 && data[col - 1].ch == 0)
2517 terminal->selection_end_col = terminal->width;
2518 data = terminal_get_row(terminal, terminal->selection_start_row);
2519 if (data[terminal->selection_start_col].ch == 0)
2520 terminal->selection_start_col = eol;
2521 }
2522
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002523 return 1;
2524}
2525
Kristian Høgsberg59826582011-01-20 11:56:57 -05002526static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002527button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002528 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002529 uint32_t button,
2530 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002531{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002532 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002533
2534 switch (button) {
2535 case 272:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002536 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002537
2538 if (time - terminal->button_time < 500)
2539 terminal->click_count++;
2540 else
2541 terminal->click_count = 1;
2542
2543 terminal->button_time = time;
2544 terminal->dragging =
2545 (terminal->click_count - 1) % 3 + SELECT_CHAR;
2546
Kristian Høgsberg59826582011-01-20 11:56:57 -05002547 input_get_position(input,
2548 &terminal->selection_start_x,
2549 &terminal->selection_start_y);
2550 terminal->selection_end_x = terminal->selection_start_x;
2551 terminal->selection_end_y = terminal->selection_start_y;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002552 if (recompute_selection(terminal))
2553 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002554 } else {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002555 terminal->dragging = SELECT_NONE;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002556 }
2557 break;
2558 }
2559}
2560
2561static int
Kristian Høgsberg29784402012-06-28 14:27:02 -04002562enter_handler(struct widget *widget,
2563 struct input *input, float x, float y, void *data)
2564{
2565 return CURSOR_IBEAM;
2566}
2567
2568static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002569motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002570 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002571 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002572{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002573 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002574
2575 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002576 input_get_position(input,
2577 &terminal->selection_end_x,
2578 &terminal->selection_end_y);
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002579
2580 if (recompute_selection(terminal))
2581 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002582 }
2583
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002584 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002585}
2586
Alexander Larssonde79dd02013-05-22 14:41:34 +02002587static void
2588output_handler(struct window *window, struct output *output, int enter,
2589 void *data)
2590{
2591 if (enter)
2592 window_set_buffer_transform(window, output_get_transform(output));
2593 window_set_buffer_scale(window, window_get_output_scale(window));
2594 window_schedule_redraw(window);
2595}
2596
Peng Wuf291f202013-06-06 15:32:41 +08002597#ifndef howmany
2598#define howmany(x, y) (((x) + ((y) - 1)) / (y))
2599#endif
2600
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002601static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002602terminal_create(struct display *display)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002603{
2604 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002605 cairo_surface_t *surface;
2606 cairo_t *cr;
Peng Wuf291f202013-06-06 15:32:41 +08002607 cairo_text_extents_t text_extents;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002608
Peter Huttererf3d62272013-08-08 11:57:05 +10002609 terminal = xzalloc(sizeof *terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002610 terminal->color_scheme = &DEFAULT_COLORS;
2611 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002612 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002613 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002614 terminal->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002615 terminal->widget = frame_create(terminal->window, terminal);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002616 window_set_title(terminal->window, "Wayland Terminal");
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002617 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002618
2619 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002620 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002621
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002622 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002623 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002624
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002625 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002626 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002627 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002628 keyboard_focus_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002629 window_set_fullscreen_handler(terminal->window, fullscreen_handler);
Alexander Larssonde79dd02013-05-22 14:41:34 +02002630 window_set_output_handler(terminal->window, output_handler);
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002631 window_set_close_handler(terminal->window, close_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002632
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002633 widget_set_redraw_handler(terminal->widget, redraw_handler);
2634 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002635 widget_set_button_handler(terminal->widget, button_handler);
Kristian Høgsberg29784402012-06-28 14:27:02 -04002636 widget_set_enter_handler(terminal->widget, enter_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002637 widget_set_motion_handler(terminal->widget, motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002638
Kristian Høgsberg09531622010-06-14 23:22:15 -04002639 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2640 cr = cairo_create(surface);
Kristian Høgsberg38912df2012-06-27 17:52:23 -04002641 cairo_set_font_size(cr, option_font_size);
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002642 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002643 CAIRO_FONT_SLANT_NORMAL,
2644 CAIRO_FONT_WEIGHT_BOLD);
2645 terminal->font_bold = cairo_get_scaled_font (cr);
2646 cairo_scaled_font_reference(terminal->font_bold);
2647
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002648 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002649 CAIRO_FONT_SLANT_NORMAL,
2650 CAIRO_FONT_WEIGHT_NORMAL);
2651 terminal->font_normal = cairo_get_scaled_font (cr);
2652 cairo_scaled_font_reference(terminal->font_normal);
2653
Kristian Høgsberg09531622010-06-14 23:22:15 -04002654 cairo_font_extents(cr, &terminal->extents);
Peng Wuf291f202013-06-06 15:32:41 +08002655
2656 /* Compute the average ascii glyph width */
2657 cairo_text_extents(cr, TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS,
2658 &text_extents);
2659 terminal->average_width = howmany
2660 (text_extents.width,
2661 strlen(TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS));
2662 terminal->average_width = ceil(terminal->average_width);
2663
Kristian Høgsberg09531622010-06-14 23:22:15 -04002664 cairo_destroy(cr);
2665 cairo_surface_destroy(surface);
2666
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002667 terminal_resize(terminal, 20, 5); /* Set minimum size first */
Kristian Høgsberga1627922012-06-20 17:30:03 -04002668 terminal_resize(terminal, 80, 25);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002669
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002670 wl_list_insert(terminal_list.prev, &terminal->link);
2671
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002672 return terminal;
2673}
2674
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002675static void
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002676terminal_destroy(struct terminal *terminal)
2677{
Dima Ryazanova85292e2012-11-29 00:27:09 -08002678 display_unwatch_fd(terminal->display, terminal->master);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002679 window_destroy(terminal->window);
2680 close(terminal->master);
2681 wl_list_remove(&terminal->link);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002682
2683 if (wl_list_empty(&terminal_list))
Dima Ryazanova85292e2012-11-29 00:27:09 -08002684 display_exit(terminal->display);
2685
2686 free(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002687}
2688
2689static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002690io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002691{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002692 struct terminal *terminal =
2693 container_of(task, struct terminal, io_task);
2694 char buffer[256];
2695 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002696
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002697 if (events & EPOLLHUP) {
2698 terminal_destroy(terminal);
2699 return;
2700 }
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002701
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002702 len = read(terminal->master, buffer, sizeof buffer);
2703 if (len < 0)
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002704 terminal_destroy(terminal);
2705 else
2706 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002707}
2708
2709static int
2710terminal_run(struct terminal *terminal, const char *path)
2711{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002712 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002713 pid_t pid;
2714
2715 pid = forkpty(&master, NULL, NULL, NULL);
2716 if (pid == 0) {
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04002717 setenv("TERM", option_term, 1);
2718 setenv("COLORTERM", option_term, 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002719 if (execl(path, path, NULL)) {
2720 printf("exec failed: %m\n");
2721 exit(EXIT_FAILURE);
2722 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002723 } else if (pid < 0) {
2724 fprintf(stderr, "failed to fork and create pty (%m).\n");
2725 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002726 }
2727
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002728 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002729 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002730 terminal->io_task.run = io_handler;
2731 display_watch_fd(terminal->display, terminal->master,
2732 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002733
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002734 window_set_fullscreen(terminal->window, option_fullscreen);
2735 if (!window_is_fullscreen(terminal->window))
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002736 terminal_resize(terminal, 80, 24);
2737
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002738 return 0;
2739}
2740
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002741static const struct config_key terminal_config_keys[] = {
2742 { "font", CONFIG_KEY_STRING, &option_font },
Kristian Høgsberg38912df2012-06-27 17:52:23 -04002743 { "font-size", CONFIG_KEY_INTEGER, &option_font_size },
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04002744 { "term", CONFIG_KEY_STRING, &option_term },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002745};
2746
2747static const struct config_section config_sections[] = {
2748 { "terminal",
2749 terminal_config_keys, ARRAY_LENGTH(terminal_config_keys) },
2750};
2751
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002752static const struct weston_option terminal_options[] = {
2753 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002754 { WESTON_OPTION_STRING, "font", 0, &option_font },
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002755 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002756};
2757
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002758int main(int argc, char *argv[])
2759{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002760 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002761 struct terminal *terminal;
Ossama Othmana50e6e42013-05-14 09:48:26 -07002762 int config_fd;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002763
Peng Wucfcc1112013-08-19 10:59:21 +08002764 /* as wcwidth is locale-dependent,
2765 wcwidth needs setlocale call to function properly. */
2766 setlocale(LC_ALL, "");
2767
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002768 option_shell = getenv("SHELL");
2769 if (!option_shell)
2770 option_shell = "/bin/bash";
2771
Ossama Othmana50e6e42013-05-14 09:48:26 -07002772 config_fd = open_config_file("weston.ini");
2773 parse_config_file(config_fd,
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002774 config_sections, ARRAY_LENGTH(config_sections),
2775 NULL);
Ossama Othmana50e6e42013-05-14 09:48:26 -07002776 close(config_fd);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002777
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002778 parse_options(terminal_options,
2779 ARRAY_LENGTH(terminal_options), &argc, argv);
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002780
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002781 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002782 if (d == NULL) {
2783 fprintf(stderr, "failed to create display: %m\n");
2784 return -1;
2785 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002786
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002787 wl_list_init(&terminal_list);
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002788 terminal = terminal_create(d);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002789 if (terminal_run(terminal, option_shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002790 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002791
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002792 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002793
2794 return 0;
2795}