blob: 1c60ebaf6d2ee2dc4b9ffcf762d4ef114e2dee23 [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;
Kristian Høgsberg8a205002013-09-11 11:52:56 -07001231 case 7: /* shell cwd as uri */
1232 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001233 default:
Kristian Høgsberg8a205002013-09-11 11:52:56 -07001234 fprintf(stderr, "Unknown OSC escape code %d, text %s\n",
1235 code, p);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001236 break;
1237 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001238}
1239
1240static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001241handle_escape(struct terminal *terminal)
1242{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001243 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001244 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001245 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001246 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001247 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001248 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001249 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001250
1251 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001252 i = 0;
1253 p = &terminal->escape[2];
1254 while ((isdigit(*p) || *p == ';') && i < 10) {
1255 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001256 if (!set[i]) {
1257 args[i] = 0;
1258 set[i] = 1;
1259 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001260 p++;
1261 i++;
1262 } else {
1263 args[i] = strtol(p, &p, 10);
1264 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001265 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001266 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001267
1268 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001269 case '@': /* ICH */
1270 count = set[0] ? args[0] : 1;
1271 if (count == 0) count = 1;
1272 terminal_shift_line(terminal, count);
1273 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001274 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001275 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001276 if (count == 0) count = 1;
1277 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001278 terminal->row -= count;
1279 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001280 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001281 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001282 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001283 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001284 if (count == 0) count = 1;
1285 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001286 terminal->row += count;
1287 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001288 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001289 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001290 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001291 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001292 if (count == 0) count = 1;
1293 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001294 terminal->column += count;
1295 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001296 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001297 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001298 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001299 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001300 if (count == 0) count = 1;
1301 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001302 terminal->column -= count;
1303 else
1304 terminal->column = 0;
1305 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001306 case 'E': /* CNL */
1307 count = set[0] ? args[0] : 1;
1308 if (terminal->row + count <= terminal->margin_bottom)
1309 terminal->row += count;
1310 else
1311 terminal->row = terminal->margin_bottom;
1312 terminal->column = 0;
1313 break;
1314 case 'F': /* CPL */
1315 count = set[0] ? args[0] : 1;
1316 if (terminal->row - count >= terminal->margin_top)
1317 terminal->row -= count;
1318 else
1319 terminal->row = terminal->margin_top;
1320 terminal->column = 0;
1321 break;
1322 case 'G': /* CHA */
1323 y = set[0] ? args[0] : 1;
1324 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1325
1326 terminal->column = y - 1;
1327 break;
1328 case 'f': /* HVP */
1329 case 'H': /* CUP */
1330 x = (set[1] ? args[1] : 1) - 1;
1331 x = x < 0 ? 0 :
1332 (x >= terminal->width ? terminal->width - 1 : x);
1333
1334 y = (set[0] ? args[0] : 1) - 1;
1335 if (terminal->origin_mode) {
1336 y += terminal->margin_top;
1337 y = y < terminal->margin_top ? terminal->margin_top :
1338 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1339 } else {
1340 y = y < 0 ? 0 :
1341 (y >= terminal->height ? terminal->height - 1 : y);
1342 }
1343
1344 terminal->row = y;
1345 terminal->column = x;
1346 break;
1347 case 'I': /* CHT */
1348 count = set[0] ? args[0] : 1;
1349 if (count == 0) count = 1;
1350 while (count > 0 && terminal->column < terminal->width) {
1351 if (terminal->tab_ruler[terminal->column]) count--;
1352 terminal->column++;
1353 }
1354 terminal->column--;
1355 break;
1356 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001357 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001358 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001359 if (!set[0] || args[0] == 0 || args[0] > 2) {
1360 memset(&row[terminal->column],
1361 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1362 attr_init(&attr_row[terminal->column],
1363 terminal->curr_attr, terminal->width - terminal->column);
1364 for (i = terminal->row + 1; i < terminal->height; i++) {
1365 memset(terminal_get_row(terminal, i),
1366 0, terminal->data_pitch);
1367 attr_init(terminal_get_attr_row(terminal, i),
1368 terminal->curr_attr, terminal->width);
1369 }
1370 } else if (args[0] == 1) {
1371 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1372 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1373 for (i = 0; i < terminal->row; i++) {
1374 memset(terminal_get_row(terminal, i),
1375 0, terminal->data_pitch);
1376 attr_init(terminal_get_attr_row(terminal, i),
1377 terminal->curr_attr, terminal->width);
1378 }
1379 } else if (args[0] == 2) {
1380 for (i = 0; i < terminal->height; i++) {
1381 memset(terminal_get_row(terminal, i),
1382 0, terminal->data_pitch);
1383 attr_init(terminal_get_attr_row(terminal, i),
1384 terminal->curr_attr, terminal->width);
1385 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001386 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001387 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001388 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001389 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001390 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001391 if (!set[0] || args[0] == 0 || args[0] > 2) {
1392 memset(&row[terminal->column], 0,
1393 (terminal->width - terminal->column) * sizeof(union utf8_char));
1394 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1395 terminal->width - terminal->column);
1396 } else if (args[0] == 1) {
1397 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1398 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1399 } else if (args[0] == 2) {
1400 memset(row, 0, terminal->data_pitch);
1401 attr_init(attr_row, terminal->curr_attr, terminal->width);
1402 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001403 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001404 case 'L': /* IL */
1405 count = set[0] ? args[0] : 1;
1406 if (count == 0) count = 1;
1407 if (terminal->row >= terminal->margin_top &&
1408 terminal->row < terminal->margin_bottom)
1409 {
1410 top = terminal->margin_top;
1411 terminal->margin_top = terminal->row;
1412 terminal_scroll(terminal, 0 - count);
1413 terminal->margin_top = top;
1414 } else if (terminal->row == terminal->margin_bottom) {
1415 memset(terminal_get_row(terminal, terminal->row),
1416 0, terminal->data_pitch);
1417 attr_init(terminal_get_attr_row(terminal, terminal->row),
1418 terminal->curr_attr, terminal->width);
1419 }
1420 break;
1421 case 'M': /* DL */
1422 count = set[0] ? args[0] : 1;
1423 if (count == 0) count = 1;
1424 if (terminal->row >= terminal->margin_top &&
1425 terminal->row < terminal->margin_bottom)
1426 {
1427 top = terminal->margin_top;
1428 terminal->margin_top = terminal->row;
1429 terminal_scroll(terminal, count);
1430 terminal->margin_top = top;
1431 } else if (terminal->row == terminal->margin_bottom) {
1432 memset(terminal_get_row(terminal, terminal->row),
1433 0, terminal->data_pitch);
1434 }
1435 break;
1436 case 'P': /* DCH */
1437 count = set[0] ? args[0] : 1;
1438 if (count == 0) count = 1;
1439 terminal_shift_line(terminal, 0 - count);
1440 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001441 case 'S': /* SU */
1442 terminal_scroll(terminal, set[0] ? args[0] : 1);
1443 break;
1444 case 'T': /* SD */
1445 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1446 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001447 case 'X': /* ECH */
1448 count = set[0] ? args[0] : 1;
1449 if (count == 0) count = 1;
1450 if ((terminal->column + count) > terminal->width)
1451 count = terminal->width - terminal->column;
1452 row = terminal_get_row(terminal, terminal->row);
1453 attr_row = terminal_get_attr_row(terminal, terminal->row);
1454 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1455 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1456 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001457 case 'Z': /* CBT */
1458 count = set[0] ? args[0] : 1;
1459 if (count == 0) count = 1;
1460 while (count > 0 && terminal->column >= 0) {
1461 if (terminal->tab_ruler[terminal->column]) count--;
1462 terminal->column--;
1463 }
1464 terminal->column++;
1465 break;
1466 case '`': /* HPA */
1467 y = set[0] ? args[0] : 1;
1468 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1469
1470 terminal->column = y - 1;
1471 break;
1472 case 'b': /* REP */
1473 count = set[0] ? args[0] : 1;
1474 if (count == 0) count = 1;
1475 if (terminal->last_char.byte[0])
1476 for (i = 0; i < count; i++)
1477 handle_char(terminal, terminal->last_char);
1478 terminal->last_char.byte[0] = 0;
1479 break;
1480 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001481 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001482 break;
1483 case 'd': /* VPA */
1484 x = set[0] ? args[0] : 1;
1485 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1486
1487 terminal->row = x - 1;
1488 break;
1489 case 'g': /* TBC */
1490 if (!set[0] || args[0] == 0) {
1491 terminal->tab_ruler[terminal->column] = 0;
1492 } else if (args[0] == 3) {
1493 memset(terminal->tab_ruler, 0, terminal->width);
1494 }
1495 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001496 case 'h': /* SM */
1497 for(i = 0; i < 10 && set[i]; i++) {
1498 handle_term_parameter(terminal, args[i], 1);
1499 }
1500 break;
1501 case 'l': /* RM */
1502 for(i = 0; i < 10 && set[i]; i++) {
1503 handle_term_parameter(terminal, args[i], 0);
1504 }
1505 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001506 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001507 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001508 if (i <= 7 && set[i] && set[i + 1] &&
1509 set[i + 2] && args[i + 1] == 5)
1510 {
1511 if (args[i] == 38) {
1512 handle_sgr(terminal, args[i + 2] + 256);
1513 break;
1514 } else if (args[i] == 48) {
1515 handle_sgr(terminal, args[i + 2] + 512);
1516 break;
1517 }
1518 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001519 if(set[i]) {
1520 handle_sgr(terminal, args[i]);
1521 } else if(i == 0) {
1522 handle_sgr(terminal, 0);
1523 break;
1524 } else {
1525 break;
1526 }
1527 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001528 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001529 case 'n': /* DSR */
1530 i = set[0] ? args[0] : 0;
1531 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001532 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001533 } else if (i == 6) {
1534 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1535 terminal->origin_mode ?
1536 terminal->row+terminal->margin_top : terminal->row+1,
1537 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001538 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001539 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001540 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001541 case 'r':
1542 if(!set[0]) {
1543 terminal->margin_top = 0;
1544 terminal->margin_bottom = terminal->height-1;
1545 terminal->row = 0;
1546 terminal->column = 0;
1547 } else {
1548 top = (set[0] ? args[0] : 1) - 1;
1549 top = top < 0 ? 0 :
1550 (top >= terminal->height ? terminal->height - 1 : top);
1551 bottom = (set[1] ? args[1] : 1) - 1;
1552 bottom = bottom < 0 ? 0 :
1553 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1554 if(bottom > top) {
1555 terminal->margin_top = top;
1556 terminal->margin_bottom = bottom;
1557 } else {
1558 terminal->margin_top = 0;
1559 terminal->margin_bottom = terminal->height-1;
1560 }
1561 if(terminal->origin_mode)
1562 terminal->row = terminal->margin_top;
1563 else
1564 terminal->row = 0;
1565 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001566 }
1567 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001568 case 's':
1569 terminal->saved_row = terminal->row;
1570 terminal->saved_column = terminal->column;
1571 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001572 case 't': /* windowOps */
1573 if (!set[0]) break;
1574 switch (args[0]) {
1575 case 4: /* resize px */
1576 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001577 widget_schedule_resize(terminal->widget,
1578 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001579 }
1580 break;
1581 case 8: /* resize ch */
1582 if (set[1] && set[2]) {
1583 terminal_resize(terminal, args[2], args[1]);
1584 }
1585 break;
1586 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001587 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001588 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1589 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001590 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001591 break;
1592 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001593 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001594 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1595 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001596 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001597 break;
1598 case 18: /* report ch */
1599 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1600 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001601 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001602 break;
1603 case 21: /* report title */
1604 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1605 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001606 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001607 break;
1608 default:
1609 if (args[0] >= 24)
1610 terminal_resize(terminal, terminal->width, args[0]);
1611 else
1612 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1613 break;
1614 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001615 case 'u':
1616 terminal->row = terminal->saved_row;
1617 terminal->column = terminal->saved_column;
1618 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001619 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001620 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001621 break;
1622 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001623}
1624
1625static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001626handle_non_csi_escape(struct terminal *terminal, char code)
1627{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001628 switch(code) {
1629 case 'M': /* RI */
1630 terminal->row -= 1;
1631 if(terminal->row < terminal->margin_top) {
1632 terminal->row = terminal->margin_top;
1633 terminal_scroll(terminal, -1);
1634 }
1635 break;
1636 case 'E': /* NEL */
1637 terminal->column = 0;
1638 // fallthrough
1639 case 'D': /* IND */
1640 terminal->row += 1;
1641 if(terminal->row > terminal->margin_bottom) {
1642 terminal->row = terminal->margin_bottom;
1643 terminal_scroll(terminal, +1);
1644 }
1645 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001646 case 'c': /* RIS */
1647 terminal_init(terminal);
1648 break;
1649 case 'H': /* HTS */
1650 terminal->tab_ruler[terminal->column] = 1;
1651 break;
1652 case '7': /* DECSC */
1653 terminal->saved_row = terminal->row;
1654 terminal->saved_column = terminal->column;
1655 terminal->saved_attr = terminal->curr_attr;
1656 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001657 terminal->saved_cs = terminal->cs;
1658 terminal->saved_g0 = terminal->g0;
1659 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001660 break;
1661 case '8': /* DECRC */
1662 terminal->row = terminal->saved_row;
1663 terminal->column = terminal->saved_column;
1664 terminal->curr_attr = terminal->saved_attr;
1665 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001666 terminal->cs = terminal->saved_cs;
1667 terminal->g0 = terminal->saved_g0;
1668 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001669 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001670 case '=': /* DECPAM */
1671 terminal->key_mode = KM_APPLICATION;
1672 break;
1673 case '>': /* DECPNM */
1674 terminal->key_mode = KM_NORMAL;
1675 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001676 default:
1677 fprintf(stderr, "Unknown escape code: %c\n", code);
1678 break;
1679 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001680}
1681
1682static void
1683handle_special_escape(struct terminal *terminal, char special, char code)
1684{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001685 int i, numChars;
1686
1687 if (special == '#') {
1688 switch(code) {
1689 case '8':
1690 /* fill with 'E', no cheap way to do this */
1691 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1692 numChars = terminal->width * terminal->height;
1693 for(i = 0; i < numChars; i++) {
1694 terminal->data[i].byte[0] = 'E';
1695 }
1696 break;
1697 default:
1698 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1699 break;
1700 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001701 } else if (special == '(' || special == ')') {
1702 switch(code) {
1703 case '0':
1704 if (special == '(')
1705 terminal->g0 = CS_SPECIAL;
1706 else
1707 terminal->g1 = CS_SPECIAL;
1708 break;
1709 case 'A':
1710 if (special == '(')
1711 terminal->g0 = CS_UK;
1712 else
1713 terminal->g1 = CS_UK;
1714 break;
1715 case 'B':
1716 if (special == '(')
1717 terminal->g0 = CS_US;
1718 else
1719 terminal->g1 = CS_US;
1720 break;
1721 default:
1722 fprintf(stderr, "Unknown character set %c\n", code);
1723 break;
1724 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001725 } else {
1726 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1727 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001728}
1729
1730static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001731handle_sgr(struct terminal *terminal, int code)
1732{
1733 switch(code) {
1734 case 0:
1735 terminal->curr_attr = terminal->color_scheme->default_attr;
1736 break;
1737 case 1:
1738 terminal->curr_attr.a |= ATTRMASK_BOLD;
1739 if (terminal->curr_attr.fg < 8)
1740 terminal->curr_attr.fg += 8;
1741 break;
1742 case 4:
1743 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1744 break;
1745 case 5:
1746 terminal->curr_attr.a |= ATTRMASK_BLINK;
1747 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001748 case 8:
1749 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1750 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001751 case 2:
1752 case 21:
1753 case 22:
1754 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1755 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1756 terminal->curr_attr.fg -= 8;
1757 break;
1758 case 24:
1759 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1760 break;
1761 case 25:
1762 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1763 break;
1764 case 7:
1765 case 26:
1766 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1767 break;
1768 case 27:
1769 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1770 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001771 case 28:
1772 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1773 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001774 case 39:
1775 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1776 break;
1777 case 49:
1778 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1779 break;
1780 default:
1781 if(code >= 30 && code <= 37) {
1782 terminal->curr_attr.fg = code - 30;
1783 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1784 terminal->curr_attr.fg += 8;
1785 } else if(code >= 40 && code <= 47) {
1786 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001787 } else if (code >= 90 && code <= 97) {
1788 terminal->curr_attr.fg = code - 90 + 8;
1789 } else if (code >= 100 && code <= 107) {
1790 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001791 } else if(code >= 256 && code < 512) {
1792 terminal->curr_attr.fg = code - 256;
1793 } else if(code >= 512 && code < 768) {
1794 terminal->curr_attr.bg = code - 512;
1795 } else {
1796 fprintf(stderr, "Unknown SGR code: %d\n", code);
1797 }
1798 break;
1799 }
1800}
1801
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001802/* Returns 1 if c was special, otherwise 0 */
1803static int
1804handle_special_char(struct terminal *terminal, char c)
1805{
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001806 union utf8_char *row;
1807 struct attr *attr_row;
1808
1809 row = terminal_get_row(terminal, terminal->row);
1810 attr_row = terminal_get_attr_row(terminal, terminal->row);
1811
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001812 switch(c) {
1813 case '\r':
1814 terminal->column = 0;
1815 break;
1816 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001817 if (terminal->mode & MODE_LF_NEWLINE) {
1818 terminal->column = 0;
1819 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001820 /* fallthrough */
1821 case '\v':
1822 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001823 terminal->row++;
1824 if(terminal->row > terminal->margin_bottom) {
1825 terminal->row = terminal->margin_bottom;
1826 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001827 }
1828
1829 break;
1830 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001831 while (terminal->column < terminal->width) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001832 if (terminal->mode & MODE_IRM)
1833 terminal_shift_line(terminal, +1);
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001834
1835 if (row[terminal->column].byte[0] == '\0') {
1836 row[terminal->column].byte[0] = ' ';
1837 row[terminal->column].byte[1] = '\0';
1838 attr_row[terminal->column] = terminal->curr_attr;
1839 }
1840
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001841 terminal->column++;
Kristian Høgsbergcca3c2f2012-06-20 15:56:13 -04001842 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001843 }
1844 if (terminal->column >= terminal->width) {
1845 terminal->column = terminal->width - 1;
1846 }
1847
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001848 break;
1849 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001850 if (terminal->column >= terminal->width) {
1851 terminal->column = terminal->width - 2;
1852 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001853 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001854 } else if (terminal->mode & MODE_AUTOWRAP) {
1855 terminal->column = terminal->width - 1;
1856 terminal->row -= 1;
1857 if (terminal->row < terminal->margin_top) {
1858 terminal->row = terminal->margin_top;
1859 terminal_scroll(terminal, -1);
1860 }
1861 }
1862
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001863 break;
1864 case '\a':
1865 /* Bell */
1866 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001867 case '\x0E': /* SO */
1868 terminal->cs = terminal->g1;
1869 break;
1870 case '\x0F': /* SI */
1871 terminal->cs = terminal->g0;
1872 break;
Kristian Høgsberg2a1aa4e2012-08-03 09:37:05 -04001873 case '\0':
1874 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001875 default:
1876 return 0;
1877 }
1878
1879 return 1;
1880}
1881
1882static void
1883handle_char(struct terminal *terminal, union utf8_char utf8)
1884{
1885 union utf8_char *row;
1886 struct attr *attr_row;
1887
1888 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001889
1890 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001891
1892 /* There are a whole lot of non-characters, control codes,
1893 * and formatting codes that should probably be ignored,
1894 * for example: */
1895 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1896 /* BOM, ignore */
1897 return;
1898 }
1899
1900 /* Some of these non-characters should be translated, e.g.: */
1901 if (utf8.byte[0] < 32) {
1902 utf8.byte[0] = utf8.byte[0] + 64;
1903 }
1904
1905 /* handle right margin effects */
1906 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001907 if (terminal->mode & MODE_AUTOWRAP) {
1908 terminal->column = 0;
1909 terminal->row += 1;
1910 if (terminal->row > terminal->margin_bottom) {
1911 terminal->row = terminal->margin_bottom;
1912 terminal_scroll(terminal, +1);
1913 }
1914 } else {
1915 terminal->column--;
1916 }
1917 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001918
1919 row = terminal_get_row(terminal, terminal->row);
1920 attr_row = terminal_get_attr_row(terminal, terminal->row);
1921
Callum Lowcay69e96582011-01-07 19:47:00 +00001922 if (terminal->mode & MODE_IRM)
1923 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001924 row[terminal->column] = utf8;
1925 attr_row[terminal->column++] = terminal->curr_attr;
1926
Peng Wucfcc1112013-08-19 10:59:21 +08001927 /* cursor jump for wide character. */
1928 if (is_wide(utf8))
1929 row[terminal->column++].ch = 0x200B; /* space glyph */
1930
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001931 if (utf8.ch != terminal->last_char.ch)
1932 terminal->last_char = utf8;
1933}
1934
Callum Lowcay30eeae52011-01-07 19:46:55 +00001935static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001936escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1937{
1938 int len, i;
1939
1940 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1941 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1942 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1943 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1944 else len = 1; /* Invalid, cannot happen */
1945
1946 if (terminal->escape_length + len <= MAX_ESCAPE) {
1947 for (i = 0; i < len; i++)
1948 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1949 terminal->escape_length += len;
1950 } else if (terminal->escape_length < MAX_ESCAPE) {
1951 terminal->escape[terminal->escape_length++] = 0;
1952 }
1953}
1954
1955static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001956terminal_data(struct terminal *terminal, const char *data, size_t length)
1957{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001958 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001959 union utf8_char utf8;
1960 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001961
1962 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001963 parser_state =
1964 utf8_next_char(&terminal->state_machine, data[i]);
1965 switch(parser_state) {
1966 case utf8state_accept:
1967 utf8.ch = terminal->state_machine.s.ch;
1968 break;
1969 case utf8state_reject:
1970 /* the unicode replacement character */
1971 utf8.byte[0] = 0xEF;
1972 utf8.byte[1] = 0xBF;
1973 utf8.byte[2] = 0xBD;
1974 utf8.byte[3] = 0x00;
1975 break;
1976 default:
1977 continue;
1978 }
1979
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001980 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001981 switch (terminal->state) {
1982 case escape_state_escape:
1983 escape_append_utf8(terminal, utf8);
1984 switch (utf8.byte[0]) {
1985 case 'P': /* DCS */
1986 terminal->state = escape_state_dcs;
1987 break;
1988 case '[': /* CSI */
1989 terminal->state = escape_state_csi;
1990 break;
1991 case ']': /* OSC */
1992 terminal->state = escape_state_osc;
1993 break;
1994 case '#':
1995 case '(':
1996 case ')': /* special */
1997 terminal->state = escape_state_special;
1998 break;
1999 case '^': /* PM (not implemented) */
2000 case '_': /* APC (not implemented) */
2001 terminal->state = escape_state_ignore;
2002 break;
2003 default:
2004 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002005 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13002006 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002007 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002008 continue;
2009 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002010 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
2011 /* do nothing */
2012 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002013 terminal->escape_flags |= ESC_FLAG_WHAT;
2014 } else if (utf8.byte[0] == '>') {
2015 terminal->escape_flags |= ESC_FLAG_GT;
2016 } else if (utf8.byte[0] == '!') {
2017 terminal->escape_flags |= ESC_FLAG_BANG;
2018 } else if (utf8.byte[0] == '$') {
2019 terminal->escape_flags |= ESC_FLAG_CASH;
2020 } else if (utf8.byte[0] == '\'') {
2021 terminal->escape_flags |= ESC_FLAG_SQUOTE;
2022 } else if (utf8.byte[0] == '"') {
2023 terminal->escape_flags |= ESC_FLAG_DQUOTE;
2024 } else if (utf8.byte[0] == ' ') {
2025 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002026 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002027 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002028 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13002029 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002030 }
2031
2032 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
2033 utf8.byte[0] == '`')
2034 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002035 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002036 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002037 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002038 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002039 continue;
2040 case escape_state_inner_escape:
2041 if (utf8.byte[0] == '\\') {
2042 terminal->state = escape_state_normal;
2043 if (terminal->outer_state == escape_state_dcs) {
2044 handle_dcs(terminal);
2045 } else if (terminal->outer_state == escape_state_osc) {
2046 handle_osc(terminal);
2047 }
2048 } else if (utf8.byte[0] == '\e') {
2049 terminal->state = terminal->outer_state;
2050 escape_append_utf8(terminal, utf8);
2051 if (terminal->escape_length >= MAX_ESCAPE)
2052 terminal->state = escape_state_normal;
2053 } else {
2054 terminal->state = terminal->outer_state;
2055 if (terminal->escape_length < MAX_ESCAPE)
2056 terminal->escape[terminal->escape_length++] = '\e';
2057 escape_append_utf8(terminal, utf8);
2058 if (terminal->escape_length >= MAX_ESCAPE)
2059 terminal->state = escape_state_normal;
2060 }
2061 continue;
2062 case escape_state_dcs:
2063 case escape_state_osc:
2064 case escape_state_ignore:
2065 if (utf8.byte[0] == '\e') {
2066 terminal->outer_state = terminal->state;
2067 terminal->state = escape_state_inner_escape;
2068 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
2069 terminal->state = escape_state_normal;
2070 handle_osc(terminal);
2071 } else {
2072 escape_append_utf8(terminal, utf8);
2073 if (terminal->escape_length >= MAX_ESCAPE)
2074 terminal->state = escape_state_normal;
2075 }
2076 continue;
2077 case escape_state_special:
2078 escape_append_utf8(terminal, utf8);
2079 terminal->state = escape_state_normal;
2080 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2081 handle_special_escape(terminal, terminal->escape[1],
2082 utf8.byte[0]);
2083 }
2084 continue;
2085 default:
2086 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002087 }
2088
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002089 /* this is valid, because ASCII characters are never used to
2090 * introduce a multibyte sequence in UTF-8 */
2091 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002092 terminal->state = escape_state_escape;
2093 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002094 terminal->escape[0] = '\e';
2095 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002096 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002097 } else {
2098 handle_char(terminal, utf8);
2099 } /* if */
2100 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002101
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002102 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002103}
2104
2105static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002106data_source_target(void *data,
2107 struct wl_data_source *source, const char *mime_type)
2108{
2109 fprintf(stderr, "data_source_target, %s\n", mime_type);
2110}
2111
2112static void
2113data_source_send(void *data,
2114 struct wl_data_source *source,
2115 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002116{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002117 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002118
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002119 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002120}
2121
2122static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002123data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002124{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002125 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002126}
2127
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002128static const struct wl_data_source_listener data_source_listener = {
2129 data_source_target,
2130 data_source_send,
2131 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002132};
2133
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07002134static const char text_mime_type[] = "text/plain;charset=utf-8";
2135
2136static void
2137data_handler(struct window *window,
2138 struct input *input,
2139 float x, float y, const char **types, void *data)
2140{
2141 int i, has_text = 0;
2142
2143 if (!types)
2144 return;
2145 for (i = 0; types[i]; i++)
2146 if (strcmp(types[i], text_mime_type) == 0)
2147 has_text = 1;
2148
2149 if (!has_text) {
2150 input_accept(input, NULL);
2151 } else {
2152 input_accept(input, text_mime_type);
2153 }
2154}
2155
2156static void
2157drop_handler(struct window *window, struct input *input,
2158 int32_t x, int32_t y, void *data)
2159{
2160 struct terminal *terminal = data;
2161
2162 input_receive_drag_data_to_fd(input, text_mime_type, terminal->master);
2163}
2164
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002165static void
2166fullscreen_handler(struct window *window, void *data)
2167{
2168 struct terminal *terminal = data;
2169
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002170 window_set_fullscreen(window, !window_is_fullscreen(terminal->window));
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002171}
2172
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002173static void
2174close_handler(struct window *window, void *data)
2175{
2176 struct terminal *terminal = data;
2177
2178 terminal_destroy(terminal);
2179}
2180
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002181static int
2182handle_bound_key(struct terminal *terminal,
2183 struct input *input, uint32_t sym, uint32_t time)
2184{
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002185 struct terminal *new_terminal;
2186
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002187 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002188 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002189 /* Cut selection; terminal doesn't do cut, fall
2190 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002191 case XKB_KEY_C:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002192 terminal->selection =
2193 display_create_data_source(terminal->display);
2194 wl_data_source_offer(terminal->selection,
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002195 "text/plain;charset=utf-8");
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002196 wl_data_source_add_listener(terminal->selection,
2197 &data_source_listener, terminal);
Kristian Høgsbergfee91be2012-06-02 21:21:41 -04002198 input_set_selection(input, terminal->selection,
2199 display_get_serial(terminal->display));
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002200 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002201 case XKB_KEY_V:
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002202 input_receive_selection_data_to_fd(input,
2203 "text/plain;charset=utf-8",
2204 terminal->master);
2205
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002206 return 1;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002207
2208 case XKB_KEY_N:
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002209 new_terminal = terminal_create(terminal->display);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002210 if (terminal_run(new_terminal, option_shell))
2211 terminal_destroy(new_terminal);
2212
2213 return 1;
2214
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002215 default:
2216 return 0;
2217 }
2218}
2219
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002220static void
2221key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002222 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2223 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002224{
2225 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002226 char ch[MAX_RESPONSE];
Kristian Høgsberg88fd4082012-06-21 15:55:03 -04002227 uint32_t modifiers, serial;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002228 int ret, len = 0;
2229 bool convert_utf8 = true;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002230
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002231 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002232 if ((modifiers & MOD_CONTROL_MASK) &&
2233 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002234 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2235 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002236 return;
2237
Daniel Stonea8468712012-11-07 17:51:35 +11002238 /* Map keypad symbols to 'normal' equivalents before processing */
2239 switch (sym) {
2240 case XKB_KEY_KP_Space:
2241 sym = XKB_KEY_space;
2242 break;
2243 case XKB_KEY_KP_Tab:
2244 sym = XKB_KEY_Tab;
2245 break;
2246 case XKB_KEY_KP_Enter:
2247 sym = XKB_KEY_Return;
2248 break;
2249 case XKB_KEY_KP_Left:
2250 sym = XKB_KEY_Left;
2251 break;
2252 case XKB_KEY_KP_Up:
2253 sym = XKB_KEY_Up;
2254 break;
2255 case XKB_KEY_KP_Right:
2256 sym = XKB_KEY_Right;
2257 break;
2258 case XKB_KEY_KP_Down:
2259 sym = XKB_KEY_Down;
2260 break;
2261 case XKB_KEY_KP_Equal:
2262 sym = XKB_KEY_equal;
2263 break;
2264 case XKB_KEY_KP_Multiply:
2265 sym = XKB_KEY_asterisk;
2266 break;
2267 case XKB_KEY_KP_Add:
2268 sym = XKB_KEY_plus;
2269 break;
2270 case XKB_KEY_KP_Separator:
2271 /* Note this is actually locale-dependent and should mostly be
2272 * a comma. But leave it as period until we one day start
2273 * doing the right thing. */
2274 sym = XKB_KEY_period;
2275 break;
2276 case XKB_KEY_KP_Subtract:
2277 sym = XKB_KEY_minus;
2278 break;
2279 case XKB_KEY_KP_Decimal:
2280 sym = XKB_KEY_period;
2281 break;
2282 case XKB_KEY_KP_Divide:
2283 sym = XKB_KEY_slash;
2284 break;
2285 case XKB_KEY_KP_0:
2286 case XKB_KEY_KP_1:
2287 case XKB_KEY_KP_2:
2288 case XKB_KEY_KP_3:
2289 case XKB_KEY_KP_4:
2290 case XKB_KEY_KP_5:
2291 case XKB_KEY_KP_6:
2292 case XKB_KEY_KP_7:
2293 case XKB_KEY_KP_8:
2294 case XKB_KEY_KP_9:
2295 sym = (sym - XKB_KEY_KP_0) + XKB_KEY_0;
2296 break;
2297 default:
2298 break;
2299 }
2300
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002301 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002302 case XKB_KEY_BackSpace:
Kristian Høgsbergb7f94bf2012-06-21 12:29:36 -04002303 if (modifiers & MOD_ALT_MASK)
2304 ch[len++] = 0x1b;
Kristian Høgsberg71a4cf42012-06-20 17:57:56 -04002305 ch[len++] = 0x7f;
2306 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002307 case XKB_KEY_Tab:
2308 case XKB_KEY_Linefeed:
2309 case XKB_KEY_Clear:
2310 case XKB_KEY_Pause:
2311 case XKB_KEY_Scroll_Lock:
2312 case XKB_KEY_Sys_Req:
2313 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002314 ch[len++] = sym & 0x7f;
2315 break;
2316
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002317 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002318 if (terminal->mode & MODE_LF_NEWLINE) {
2319 ch[len++] = 0x0D;
2320 ch[len++] = 0x0A;
2321 } else {
2322 ch[len++] = 0x0D;
2323 }
2324 break;
2325
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002326 case XKB_KEY_Shift_L:
2327 case XKB_KEY_Shift_R:
2328 case XKB_KEY_Control_L:
2329 case XKB_KEY_Control_R:
2330 case XKB_KEY_Alt_L:
2331 case XKB_KEY_Alt_R:
Kristian Høgsberg22fbcf72012-06-22 12:18:56 -04002332 case XKB_KEY_Meta_L:
2333 case XKB_KEY_Meta_R:
2334 case XKB_KEY_Super_L:
2335 case XKB_KEY_Super_R:
2336 case XKB_KEY_Hyper_L:
2337 case XKB_KEY_Hyper_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002338 break;
2339
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002340 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002341 len = function_key_response('[', 2, modifiers, '~', ch);
2342 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002343 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002344 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2345 ch[len++] = '\x04';
2346 } else {
2347 len = function_key_response('[', 3, modifiers, '~', ch);
2348 }
2349 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002350 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002351 len = function_key_response('[', 5, modifiers, '~', ch);
2352 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002353 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002354 len = function_key_response('[', 6, modifiers, '~', ch);
2355 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002356 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002357 len = function_key_response('O', 1, modifiers, 'P', ch);
2358 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002359 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002360 len = function_key_response('O', 1, modifiers, 'Q', ch);
2361 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002362 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002363 len = function_key_response('O', 1, modifiers, 'R', ch);
2364 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002365 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002366 len = function_key_response('O', 1, modifiers, 'S', ch);
2367 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002368 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002369 len = function_key_response('[', 15, modifiers, '~', ch);
2370 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002371 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002372 len = function_key_response('[', 17, modifiers, '~', ch);
2373 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002374 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002375 len = function_key_response('[', 18, modifiers, '~', ch);
2376 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002377 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002378 len = function_key_response('[', 19, modifiers, '~', ch);
2379 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002380 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002381 len = function_key_response('[', 20, modifiers, '~', ch);
2382 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002383 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002384 len = function_key_response('[', 21, modifiers, '~', ch);
2385 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002386 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002387 len = function_key_response('[', 24, modifiers, '~', ch);
2388 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002389 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002390 /* Handle special keys with alternate mappings */
2391 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2392 if (len != 0) break;
2393
Kristian Høgsberg70163132012-05-08 15:55:39 -04002394 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002395 if (sym >= '3' && sym <= '7')
2396 sym = (sym & 0x1f) + 8;
2397
2398 if (!((sym >= '!' && sym <= '/') ||
2399 (sym >= '8' && sym <= '?') ||
2400 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2401 else if (sym == '2') sym = 0x00;
2402 else if (sym == '/') sym = 0x1F;
2403 else if (sym == '8' || sym == '?') sym = 0x7F;
Kristian Høgsbergae9e0732012-06-21 12:30:15 -04002404 }
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002405 if (modifiers & MOD_ALT_MASK) {
2406 if (terminal->mode & MODE_ALT_SENDS_ESC) {
2407 ch[len++] = 0x1b;
2408 } else {
2409 sym = sym | 0x80;
2410 convert_utf8 = false;
2411 }
Callum Lowcay7e08e902011-01-07 19:47:02 +00002412 }
2413
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002414 if ((sym < 128) ||
2415 (!convert_utf8 && sym < 256)) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002416 ch[len++] = sym;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002417 } else {
2418 ret = xkb_keysym_to_utf8(sym, ch + len,
2419 MAX_RESPONSE - len);
2420 if (ret < 0)
2421 fprintf(stderr,
2422 "Warning: buffer too small to encode "
2423 "UTF8 character\n");
2424 else
2425 len += ret;
2426 }
2427
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002428 break;
2429 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002430
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002431 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04002432 terminal_write(terminal, ch, len);
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002433
2434 /* Hide cursor, except if this was coming from a
2435 * repeating key press. */
2436 serial = display_get_serial(terminal->display);
2437 if (terminal->hide_cursor_serial != serial) {
2438 input_set_pointer_image(input, CURSOR_BLANK);
2439 terminal->hide_cursor_serial = serial;
2440 }
2441 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002442}
2443
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002444static void
2445keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002446 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002447{
2448 struct terminal *terminal = data;
2449
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002450 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002451}
2452
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002453static int wordsep(int ch)
2454{
2455 const char extra[] = "-,./?%&#:_=+@~";
2456
Andre Heider552d12b2012-08-02 20:59:43 +02002457 if (ch > 127)
2458 return 1;
2459
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002460 return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
2461}
2462
2463static int
2464recompute_selection(struct terminal *terminal)
2465{
2466 struct rectangle allocation;
2467 int col, x, width, height;
2468 int start_row, end_row;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002469 int word_start, eol;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002470 int side_margin, top_margin;
2471 int start_x, end_x;
2472 int cw, ch;
2473 union utf8_char *data;
2474
Peng Wuf291f202013-06-06 15:32:41 +08002475 cw = terminal->average_width;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002476 ch = terminal->extents.height;
2477 widget_get_allocation(terminal->widget, &allocation);
2478 width = terminal->width * cw;
2479 height = terminal->height * ch;
2480 side_margin = allocation.x + (allocation.width - width) / 2;
2481 top_margin = allocation.y + (allocation.height - height) / 2;
2482
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002483 start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1;
2484 end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1;
2485
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002486 if (start_row < end_row ||
2487 (start_row == end_row &&
2488 terminal->selection_start_x < terminal->selection_end_x)) {
2489 terminal->selection_start_row = start_row;
2490 terminal->selection_end_row = end_row;
2491 start_x = terminal->selection_start_x;
2492 end_x = terminal->selection_end_x;
2493 } else {
2494 terminal->selection_start_row = end_row;
2495 terminal->selection_end_row = start_row;
2496 start_x = terminal->selection_end_x;
2497 end_x = terminal->selection_start_x;
2498 }
2499
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002500 eol = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002501 if (terminal->selection_start_row < 0) {
2502 terminal->selection_start_row = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002503 terminal->selection_start_col = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002504 } else {
2505 x = side_margin + cw / 2;
2506 data = terminal_get_row(terminal,
2507 terminal->selection_start_row);
2508 word_start = 0;
2509 for (col = 0; col < terminal->width; col++, x += cw) {
2510 if (col == 0 || wordsep(data[col - 1].ch))
2511 word_start = col;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002512 if (data[col].ch != 0)
2513 eol = col + 1;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002514 if (start_x < x)
2515 break;
2516 }
2517
2518 switch (terminal->dragging) {
2519 case SELECT_LINE:
2520 terminal->selection_start_col = 0;
2521 break;
2522 case SELECT_WORD:
2523 terminal->selection_start_col = word_start;
2524 break;
2525 case SELECT_CHAR:
2526 terminal->selection_start_col = col;
2527 break;
2528 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002529 }
2530
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002531 if (terminal->selection_end_row >= terminal->height) {
2532 terminal->selection_end_row = terminal->height;
2533 terminal->selection_end_col = 0;
2534 } else {
2535 x = side_margin + cw / 2;
2536 data = terminal_get_row(terminal, terminal->selection_end_row);
2537 for (col = 0; col < terminal->width; col++, x += cw) {
2538 if (terminal->dragging == SELECT_CHAR && end_x < x)
2539 break;
2540 if (terminal->dragging == SELECT_WORD &&
2541 end_x < x && wordsep(data[col].ch))
2542 break;
2543 }
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002544 terminal->selection_end_col = col;
2545 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002546
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002547 if (terminal->selection_end_col != terminal->selection_start_col ||
2548 terminal->selection_start_row != terminal->selection_end_row) {
2549 col = terminal->selection_end_col;
2550 if (col > 0 && data[col - 1].ch == 0)
2551 terminal->selection_end_col = terminal->width;
2552 data = terminal_get_row(terminal, terminal->selection_start_row);
2553 if (data[terminal->selection_start_col].ch == 0)
2554 terminal->selection_start_col = eol;
2555 }
2556
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002557 return 1;
2558}
2559
Kristian Høgsberg59826582011-01-20 11:56:57 -05002560static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002561button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002562 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002563 uint32_t button,
2564 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002565{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002566 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002567
2568 switch (button) {
2569 case 272:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002570 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002571
2572 if (time - terminal->button_time < 500)
2573 terminal->click_count++;
2574 else
2575 terminal->click_count = 1;
2576
2577 terminal->button_time = time;
2578 terminal->dragging =
2579 (terminal->click_count - 1) % 3 + SELECT_CHAR;
2580
Kristian Høgsberg59826582011-01-20 11:56:57 -05002581 input_get_position(input,
2582 &terminal->selection_start_x,
2583 &terminal->selection_start_y);
2584 terminal->selection_end_x = terminal->selection_start_x;
2585 terminal->selection_end_y = terminal->selection_start_y;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002586 if (recompute_selection(terminal))
2587 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002588 } else {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002589 terminal->dragging = SELECT_NONE;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002590 }
2591 break;
2592 }
2593}
2594
2595static int
Kristian Høgsberg29784402012-06-28 14:27:02 -04002596enter_handler(struct widget *widget,
2597 struct input *input, float x, float y, void *data)
2598{
2599 return CURSOR_IBEAM;
2600}
2601
2602static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002603motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002604 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002605 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002606{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002607 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002608
2609 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002610 input_get_position(input,
2611 &terminal->selection_end_x,
2612 &terminal->selection_end_y);
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002613
2614 if (recompute_selection(terminal))
2615 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002616 }
2617
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002618 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002619}
2620
Alexander Larssonde79dd02013-05-22 14:41:34 +02002621static void
2622output_handler(struct window *window, struct output *output, int enter,
2623 void *data)
2624{
2625 if (enter)
2626 window_set_buffer_transform(window, output_get_transform(output));
2627 window_set_buffer_scale(window, window_get_output_scale(window));
2628 window_schedule_redraw(window);
2629}
2630
Peng Wuf291f202013-06-06 15:32:41 +08002631#ifndef howmany
2632#define howmany(x, y) (((x) + ((y) - 1)) / (y))
2633#endif
2634
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002635static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002636terminal_create(struct display *display)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002637{
2638 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002639 cairo_surface_t *surface;
2640 cairo_t *cr;
Peng Wuf291f202013-06-06 15:32:41 +08002641 cairo_text_extents_t text_extents;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002642
Peter Huttererf3d62272013-08-08 11:57:05 +10002643 terminal = xzalloc(sizeof *terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002644 terminal->color_scheme = &DEFAULT_COLORS;
2645 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002646 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002647 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002648 terminal->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002649 terminal->widget = frame_create(terminal->window, terminal);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002650 window_set_title(terminal->window, "Wayland Terminal");
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002651 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002652
2653 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002654 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002655
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002656 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002657 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002658
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002659 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002660 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002661 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002662 keyboard_focus_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002663 window_set_fullscreen_handler(terminal->window, fullscreen_handler);
Alexander Larssonde79dd02013-05-22 14:41:34 +02002664 window_set_output_handler(terminal->window, output_handler);
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002665 window_set_close_handler(terminal->window, close_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002666
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07002667 window_set_data_handler(terminal->window, data_handler);
2668 window_set_drop_handler(terminal->window, drop_handler);
2669
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002670 widget_set_redraw_handler(terminal->widget, redraw_handler);
2671 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002672 widget_set_button_handler(terminal->widget, button_handler);
Kristian Høgsberg29784402012-06-28 14:27:02 -04002673 widget_set_enter_handler(terminal->widget, enter_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002674 widget_set_motion_handler(terminal->widget, motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002675
Kristian Høgsberg09531622010-06-14 23:22:15 -04002676 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2677 cr = cairo_create(surface);
Kristian Høgsberg38912df2012-06-27 17:52:23 -04002678 cairo_set_font_size(cr, option_font_size);
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002679 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002680 CAIRO_FONT_SLANT_NORMAL,
2681 CAIRO_FONT_WEIGHT_BOLD);
2682 terminal->font_bold = cairo_get_scaled_font (cr);
2683 cairo_scaled_font_reference(terminal->font_bold);
2684
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002685 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002686 CAIRO_FONT_SLANT_NORMAL,
2687 CAIRO_FONT_WEIGHT_NORMAL);
2688 terminal->font_normal = cairo_get_scaled_font (cr);
2689 cairo_scaled_font_reference(terminal->font_normal);
2690
Kristian Høgsberg09531622010-06-14 23:22:15 -04002691 cairo_font_extents(cr, &terminal->extents);
Peng Wuf291f202013-06-06 15:32:41 +08002692
2693 /* Compute the average ascii glyph width */
2694 cairo_text_extents(cr, TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS,
2695 &text_extents);
2696 terminal->average_width = howmany
2697 (text_extents.width,
2698 strlen(TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS));
2699 terminal->average_width = ceil(terminal->average_width);
2700
Kristian Høgsberg09531622010-06-14 23:22:15 -04002701 cairo_destroy(cr);
2702 cairo_surface_destroy(surface);
2703
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002704 terminal_resize(terminal, 20, 5); /* Set minimum size first */
Kristian Høgsberga1627922012-06-20 17:30:03 -04002705 terminal_resize(terminal, 80, 25);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002706
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002707 wl_list_insert(terminal_list.prev, &terminal->link);
2708
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002709 return terminal;
2710}
2711
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002712static void
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002713terminal_destroy(struct terminal *terminal)
2714{
Dima Ryazanova85292e2012-11-29 00:27:09 -08002715 display_unwatch_fd(terminal->display, terminal->master);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002716 window_destroy(terminal->window);
2717 close(terminal->master);
2718 wl_list_remove(&terminal->link);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002719
2720 if (wl_list_empty(&terminal_list))
Dima Ryazanova85292e2012-11-29 00:27:09 -08002721 display_exit(terminal->display);
2722
2723 free(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002724}
2725
2726static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002727io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002728{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002729 struct terminal *terminal =
2730 container_of(task, struct terminal, io_task);
2731 char buffer[256];
2732 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002733
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002734 if (events & EPOLLHUP) {
2735 terminal_destroy(terminal);
2736 return;
2737 }
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002738
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002739 len = read(terminal->master, buffer, sizeof buffer);
2740 if (len < 0)
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002741 terminal_destroy(terminal);
2742 else
2743 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002744}
2745
2746static int
2747terminal_run(struct terminal *terminal, const char *path)
2748{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002749 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002750 pid_t pid;
2751
2752 pid = forkpty(&master, NULL, NULL, NULL);
2753 if (pid == 0) {
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04002754 setenv("TERM", option_term, 1);
2755 setenv("COLORTERM", option_term, 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002756 if (execl(path, path, NULL)) {
2757 printf("exec failed: %m\n");
2758 exit(EXIT_FAILURE);
2759 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002760 } else if (pid < 0) {
2761 fprintf(stderr, "failed to fork and create pty (%m).\n");
2762 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002763 }
2764
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002765 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002766 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002767 terminal->io_task.run = io_handler;
2768 display_watch_fd(terminal->display, terminal->master,
2769 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002770
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002771 window_set_fullscreen(terminal->window, option_fullscreen);
2772 if (!window_is_fullscreen(terminal->window))
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002773 terminal_resize(terminal, 80, 24);
2774
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002775 return 0;
2776}
2777
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002778static const struct config_key terminal_config_keys[] = {
2779 { "font", CONFIG_KEY_STRING, &option_font },
Kristian Høgsberg38912df2012-06-27 17:52:23 -04002780 { "font-size", CONFIG_KEY_INTEGER, &option_font_size },
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04002781 { "term", CONFIG_KEY_STRING, &option_term },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002782};
2783
2784static const struct config_section config_sections[] = {
2785 { "terminal",
2786 terminal_config_keys, ARRAY_LENGTH(terminal_config_keys) },
2787};
2788
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002789static const struct weston_option terminal_options[] = {
2790 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002791 { WESTON_OPTION_STRING, "font", 0, &option_font },
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002792 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002793};
2794
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002795int main(int argc, char *argv[])
2796{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002797 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002798 struct terminal *terminal;
Ossama Othmana50e6e42013-05-14 09:48:26 -07002799 int config_fd;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002800
Peng Wucfcc1112013-08-19 10:59:21 +08002801 /* as wcwidth is locale-dependent,
2802 wcwidth needs setlocale call to function properly. */
2803 setlocale(LC_ALL, "");
2804
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002805 option_shell = getenv("SHELL");
2806 if (!option_shell)
2807 option_shell = "/bin/bash";
2808
Ossama Othmana50e6e42013-05-14 09:48:26 -07002809 config_fd = open_config_file("weston.ini");
2810 parse_config_file(config_fd,
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002811 config_sections, ARRAY_LENGTH(config_sections),
2812 NULL);
Ossama Othmana50e6e42013-05-14 09:48:26 -07002813 close(config_fd);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002814
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002815 parse_options(terminal_options,
2816 ARRAY_LENGTH(terminal_options), &argc, argv);
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002817
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002818 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002819 if (d == NULL) {
2820 fprintf(stderr, "failed to create display: %m\n");
2821 return -1;
2822 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002823
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002824 wl_list_init(&terminal_list);
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002825 terminal = terminal_create(d);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002826 if (terminal_run(terminal, option_shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002827 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002828
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002829 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002830
2831 return 0;
2832}