blob: c45c77641f1957b24d91b828e695c53e6b8c3ff1 [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øgsberg9c3dee12013-09-21 22:23:08 -070047static char *option_font;
48static int option_font_size;
49static char *option_term;
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;
José Bollo4a4704a2013-09-13 11:28:51 +0200765 i = 1 + terminal->row - height;
766 if (i > 0) {
767 terminal->start = (terminal->start + i) % terminal->height;
768 terminal->row = terminal->row - i;
769 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500770 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500771 total_rows = terminal->height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500772 }
773
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000774 for (i = 0; i < total_rows; i++) {
775 memcpy(&data[width * i],
776 terminal_get_row(terminal, i),
777 l * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000778 memcpy(&data_attr[width * i],
779 terminal_get_attr_row(terminal, i),
780 l * sizeof(struct attr));
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000781 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500782
783 free(terminal->data);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000784 free(terminal->data_attr);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000785 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500786 }
787
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000788 terminal->data_pitch = data_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000789 terminal->attr_pitch = attr_pitch;
Callum Lowcay86653ed2011-01-07 19:47:03 +0000790 terminal->margin_bottom =
791 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500792 terminal->width = width;
793 terminal->height = height;
794 terminal->data = data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000795 terminal->data_attr = data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000796 terminal->tab_ruler = tab_ruler;
José Bollo4a4704a2013-09-13 11:28:51 +0200797 terminal->start = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000798 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500799
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000800 /* Update the window size */
801 ws.ws_row = terminal->height;
802 ws.ws_col = terminal->width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500803 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500804 ws.ws_xpixel = allocation.width;
805 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000806 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500807}
808
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500809static void
810resize_handler(struct widget *widget,
811 int32_t width, int32_t height, void *data)
812{
813 struct terminal *terminal = data;
814 int32_t columns, rows, m;
815
816 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800817 columns = (width - m) / (int32_t) terminal->average_width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500818 rows = (height - m) / (int32_t) terminal->extents.height;
819
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400820 if (!window_is_fullscreen(terminal->window) &&
821 !window_is_maximized(terminal->window)) {
Peng Wuf291f202013-06-06 15:32:41 +0800822 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500823 height = rows * terminal->extents.height + m;
824 widget_set_size(terminal->widget, width, height);
825 }
826
827 terminal_resize_cells(terminal, columns, rows);
828}
829
830static void
831terminal_resize(struct terminal *terminal, int columns, int rows)
832{
833 int32_t width, height, m;
834
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400835 if (window_is_fullscreen(terminal->window) ||
836 window_is_maximized(terminal->window))
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500837 return;
838
839 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800840 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500841 height = rows * terminal->extents.height + m;
Kristian Høgsberga1627922012-06-20 17:30:03 -0400842
843 frame_set_child_size(terminal->widget, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500844}
845
Callum Lowcay30eeae52011-01-07 19:46:55 +0000846struct color_scheme DEFAULT_COLORS = {
847 {
848 {0, 0, 0, 1}, /* black */
849 {0.66, 0, 0, 1}, /* red */
850 {0 , 0.66, 0, 1}, /* green */
851 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
852 {0 , 0 , 0.66, 1}, /* blue */
853 {0.66, 0 , 0.66, 1}, /* magenta */
854 {0, 0.66, 0.66, 1}, /* cyan */
855 {0.66, 0.66, 0.66, 1}, /* light grey */
856 {0.22, 0.33, 0.33, 1}, /* dark grey */
857 {1, 0.33, 0.33, 1}, /* high red */
858 {0.33, 1, 0.33, 1}, /* high green */
859 {1, 1, 0.33, 1}, /* high yellow */
860 {0.33, 0.33, 1, 1}, /* high blue */
861 {1, 0.33, 1, 1}, /* high magenta */
862 {0.33, 1, 1, 1}, /* high cyan */
863 {1, 1, 1, 1} /* white */
864 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500865 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000866 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
867};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400868
Kristian Høgsberg22106762008-12-08 13:50:07 -0500869static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500870terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
871{
872 cairo_set_source_rgba(cr,
873 terminal->color_table[index].r,
874 terminal->color_table[index].g,
875 terminal->color_table[index].b,
876 terminal->color_table[index].a);
877}
878
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500879static void
880terminal_send_selection(struct terminal *terminal, int fd)
881{
882 int row, col;
883 union utf8_char *p_row;
884 union decoded_attr attr;
885 FILE *fp;
886 int len;
887
888 fp = fdopen(fd, "w");
Brian Lovin1bf14812013-08-08 16:12:55 -0700889 if (fp == NULL){
890 close(fd);
891 return;
892 }
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500893 for (row = 0; row < terminal->height; row++) {
894 p_row = terminal_get_row(terminal, row);
895 for (col = 0; col < terminal->width; col++) {
Peng Wucfcc1112013-08-19 10:59:21 +0800896 if (p_row[col].ch == 0x200B) /* space glyph */
897 continue;
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500898 /* get the attributes for this character cell */
899 terminal_decode_attr(terminal, row, col, &attr);
900 if (!attr.attr.s)
901 continue;
902 len = strnlen((char *) p_row[col].byte, 4);
Kristian Høgsberg0dee6472012-07-01 21:25:41 -0400903 if (len > 0)
904 fwrite(p_row[col].byte, 1, len, fp);
905 if (len == 0 || col == terminal->width - 1) {
906 fwrite("\n", 1, 1, fp);
907 break;
908 }
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500909 }
910 }
911 fclose(fp);
912}
913
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500914struct glyph_run {
915 struct terminal *terminal;
916 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400917 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500918 union decoded_attr attr;
919 cairo_glyph_t glyphs[256], *g;
920};
921
922static void
923glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
924{
925 run->terminal = terminal;
926 run->cr = cr;
927 run->g = run->glyphs;
928 run->count = 0;
929 run->attr.key = 0;
930}
931
932static void
933glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
934{
935 cairo_scaled_font_t *font;
936
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500937 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
938 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300939 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500940 font = run->terminal->font_bold;
941 else
942 font = run->terminal->font_normal;
943 cairo_set_scaled_font(run->cr, font);
944 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300945 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500946
Callum Lowcay9d708b02011-01-12 20:06:17 +1300947 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
948 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500949 run->g = run->glyphs;
950 run->count = 0;
951 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300952 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500953}
954
955static void
956glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
957{
958 int num_glyphs;
959 cairo_scaled_font_t *font;
960
961 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
962
Callum Lowcay9d708b02011-01-12 20:06:17 +1300963 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500964 font = run->terminal->font_bold;
965 else
966 font = run->terminal->font_normal;
967
968 cairo_move_to(run->cr, x, y);
969 cairo_scaled_font_text_to_glyphs (font, x, y,
970 (char *) c->byte, 4,
971 &run->g, &num_glyphs,
972 NULL, NULL, NULL);
973 run->g += num_glyphs;
974 run->count += num_glyphs;
975}
976
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500977
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500978static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500979redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500980{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500981 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500982 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500983 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000984 int top_margin, side_margin;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600985 int row, col, cursor_x, cursor_y;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000986 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500987 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000988 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500989 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500990 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500991 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500992 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +0800993 double average_width;
Peng Wucfcc1112013-08-19 10:59:21 +0800994 double unichar_width;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500995
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500996 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500997 widget_get_allocation(terminal->widget, &allocation);
Alexander Larssonde79dd02013-05-22 14:41:34 +0200998 cr = widget_cairo_create(terminal->widget);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500999 cairo_rectangle(cr, allocation.x, allocation.y,
1000 allocation.width, allocation.height);
1001 cairo_clip(cr);
1002 cairo_push_group(cr);
1003
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001004 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -05001005 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001006 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001007
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05001008 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001009
Kristian Høgsberg59826582011-01-20 11:56:57 -05001010 extents = terminal->extents;
Peng Wuf291f202013-06-06 15:32:41 +08001011 average_width = terminal->average_width;
1012 side_margin = (allocation.width - terminal->width * average_width) / 2;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001013 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001014
Callum Lowcay30eeae52011-01-07 19:46:55 +00001015 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001016 cairo_translate(cr, allocation.x + side_margin,
1017 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001018 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001019 for (row = 0; row < terminal->height; row++) {
Peng Wucfcc1112013-08-19 10:59:21 +08001020 p_row = terminal_get_row(terminal, row);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001021 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001022 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -05001023 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001024
Callum Lowcay9d708b02011-01-12 20:06:17 +13001025 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -05001026 continue;
1027
Peng Wucfcc1112013-08-19 10:59:21 +08001028 if (is_wide(p_row[col]))
1029 unichar_width = 2 * average_width;
1030 else
1031 unichar_width = average_width;
1032
Callum Lowcay9d708b02011-01-12 20:06:17 +13001033 terminal_set_color(terminal, cr, attr.attr.bg);
Peng Wuf291f202013-06-06 15:32:41 +08001034 cairo_move_to(cr, col * average_width,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001035 row * extents.height);
Peng Wucfcc1112013-08-19 10:59:21 +08001036 cairo_rel_line_to(cr, unichar_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001037 cairo_rel_line_to(cr, 0, extents.height);
Peng Wucfcc1112013-08-19 10:59:21 +08001038 cairo_rel_line_to(cr, -unichar_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001039 cairo_close_path(cr);
1040 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001041 }
1042 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001043
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001044 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1045
1046 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001047 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001048 for (row = 0; row < terminal->height; row++) {
1049 p_row = terminal_get_row(terminal, row);
1050 for (col = 0; col < terminal->width; col++) {
1051 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -05001052 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001053
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001054 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001055
Peng Wuf291f202013-06-06 15:32:41 +08001056 text_x = col * average_width;
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001057 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +13001058 if (attr.attr.a & ATTRMASK_UNDERLINE) {
1059 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +00001060 cairo_move_to(cr, text_x, (double)text_y + 1.5);
Peng Wuf291f202013-06-06 15:32:41 +08001061 cairo_line_to(cr, text_x + average_width, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001062 cairo_stroke(cr);
1063 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -05001064
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001065 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001066 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001067 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001068
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001069 attr.key = ~0;
1070 glyph_run_flush(&run, attr);
1071
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001072 if ((terminal->mode & MODE_SHOW_CURSOR) &&
1073 !window_has_focus(terminal->window)) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001074 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001075
Callum Lowcay30eeae52011-01-07 19:46:55 +00001076 cairo_set_line_width(cr, 1);
Peng Wuf291f202013-06-06 15:32:41 +08001077 cairo_move_to(cr, terminal->column * average_width + d,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001078 terminal->row * extents.height + d);
Peng Wuf291f202013-06-06 15:32:41 +08001079 cairo_rel_line_to(cr, average_width - 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001080 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
Peng Wuf291f202013-06-06 15:32:41 +08001081 cairo_rel_line_to(cr, -average_width + 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001082 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001083
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001084 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001085 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001086
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001087 cairo_pop_group_to_source(cr);
1088 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001089 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001090 cairo_surface_destroy(surface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001091
1092 if (terminal->send_cursor_position) {
1093 cursor_x = side_margin + allocation.x +
Peng Wuf291f202013-06-06 15:32:41 +08001094 terminal->column * average_width;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001095 cursor_y = top_margin + allocation.y +
1096 terminal->row * extents.height;
1097 window_set_text_cursor_position(terminal->window,
1098 cursor_x, cursor_y);
1099 terminal->send_cursor_position = 0;
1100 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001101}
1102
1103static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001104terminal_write(struct terminal *terminal, const char *data, size_t length)
1105{
1106 if (write(terminal->master, data, length) < 0)
1107 abort();
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001108 terminal->send_cursor_position = 1;
Kristian Høgsberg26130862011-08-24 11:30:21 -04001109}
1110
1111static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001112terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001113
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001114static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001115handle_char(struct terminal *terminal, union utf8_char utf8);
1116
1117static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001118handle_sgr(struct terminal *terminal, int code);
1119
1120static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001121handle_term_parameter(struct terminal *terminal, int code, int sr)
1122{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001123 int i;
1124
Callum Lowcay67a201d2011-01-12 19:23:41 +13001125 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001126 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001127 case 1: /* DECCKM */
1128 if (sr) terminal->key_mode = KM_APPLICATION;
1129 else terminal->key_mode = KM_NORMAL;
1130 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001131 case 2: /* DECANM */
1132 /* No VT52 support yet */
1133 terminal->g0 = CS_US;
1134 terminal->g1 = CS_US;
1135 terminal->cs = terminal->g0;
1136 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001137 case 3: /* DECCOLM */
1138 if (sr)
1139 terminal_resize(terminal, 132, 24);
1140 else
1141 terminal_resize(terminal, 80, 24);
1142
1143 /* set columns, but also home cursor and clear screen */
1144 terminal->row = 0; terminal->column = 0;
1145 for (i = 0; i < terminal->height; i++) {
1146 memset(terminal_get_row(terminal, i),
1147 0, terminal->data_pitch);
1148 attr_init(terminal_get_attr_row(terminal, i),
1149 terminal->curr_attr, terminal->width);
1150 }
1151 break;
1152 case 5: /* DECSCNM */
1153 if (sr) terminal->mode |= MODE_INVERSE;
1154 else terminal->mode &= ~MODE_INVERSE;
1155 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001156 case 6: /* DECOM */
1157 terminal->origin_mode = sr;
1158 if (terminal->origin_mode)
1159 terminal->row = terminal->margin_top;
1160 else
1161 terminal->row = 0;
1162 terminal->column = 0;
1163 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001164 case 7: /* DECAWM */
1165 if (sr) terminal->mode |= MODE_AUTOWRAP;
1166 else terminal->mode &= ~MODE_AUTOWRAP;
1167 break;
1168 case 8: /* DECARM */
1169 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1170 else terminal->mode &= ~MODE_AUTOREPEAT;
1171 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001172 case 12: /* Very visible cursor (CVVIS) */
1173 /* FIXME: What do we do here. */
1174 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001175 case 25:
1176 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1177 else terminal->mode &= ~MODE_SHOW_CURSOR;
1178 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001179 case 1034: /* smm/rmm, meta mode on/off */
1180 /* ignore */
1181 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001182 case 1037: /* deleteSendsDel */
1183 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1184 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1185 break;
1186 case 1039: /* altSendsEscape */
1187 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1188 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1189 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001190 case 1049: /* rmcup/smcup, alternate screen */
1191 /* Ignore. Should be possible to implement,
1192 * but it's kind of annoying. */
1193 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001194 default:
1195 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1196 break;
1197 }
1198 } else {
1199 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001200 case 4: /* IRM */
1201 if (sr) terminal->mode |= MODE_IRM;
1202 else terminal->mode &= ~MODE_IRM;
1203 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001204 case 20: /* LNM */
1205 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1206 else terminal->mode &= ~MODE_LF_NEWLINE;
1207 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001208 default:
1209 fprintf(stderr, "Unknown parameter: %d\n", code);
1210 break;
1211 }
1212 }
1213}
1214
1215static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001216handle_dcs(struct terminal *terminal)
1217{
1218}
1219
1220static void
1221handle_osc(struct terminal *terminal)
1222{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001223 char *p;
1224 int code;
1225
1226 terminal->escape[terminal->escape_length++] = '\0';
1227 p = &terminal->escape[2];
1228 code = strtol(p, &p, 10);
1229 if (*p == ';') p++;
1230
1231 switch (code) {
1232 case 0: /* Icon name and window title */
1233 case 1: /* Icon label */
1234 case 2: /* Window title*/
1235 window_set_title(terminal->window, p);
1236 break;
Kristian Høgsberg8a205002013-09-11 11:52:56 -07001237 case 7: /* shell cwd as uri */
1238 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001239 default:
Kristian Høgsberg8a205002013-09-11 11:52:56 -07001240 fprintf(stderr, "Unknown OSC escape code %d, text %s\n",
1241 code, p);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001242 break;
1243 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001244}
1245
1246static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001247handle_escape(struct terminal *terminal)
1248{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001249 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001250 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001251 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001252 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001253 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001254 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001255 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001256
1257 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001258 i = 0;
1259 p = &terminal->escape[2];
1260 while ((isdigit(*p) || *p == ';') && i < 10) {
1261 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001262 if (!set[i]) {
1263 args[i] = 0;
1264 set[i] = 1;
1265 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001266 p++;
1267 i++;
1268 } else {
1269 args[i] = strtol(p, &p, 10);
1270 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001271 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001272 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001273
1274 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001275 case '@': /* ICH */
1276 count = set[0] ? args[0] : 1;
1277 if (count == 0) count = 1;
1278 terminal_shift_line(terminal, count);
1279 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001280 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001281 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001282 if (count == 0) count = 1;
1283 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001284 terminal->row -= count;
1285 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001286 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001287 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001288 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001289 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001290 if (count == 0) count = 1;
1291 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001292 terminal->row += count;
1293 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001294 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001295 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001296 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001297 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001298 if (count == 0) count = 1;
1299 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001300 terminal->column += count;
1301 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001302 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001303 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001304 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001305 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001306 if (count == 0) count = 1;
1307 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001308 terminal->column -= count;
1309 else
1310 terminal->column = 0;
1311 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001312 case 'E': /* CNL */
1313 count = set[0] ? args[0] : 1;
1314 if (terminal->row + count <= terminal->margin_bottom)
1315 terminal->row += count;
1316 else
1317 terminal->row = terminal->margin_bottom;
1318 terminal->column = 0;
1319 break;
1320 case 'F': /* CPL */
1321 count = set[0] ? args[0] : 1;
1322 if (terminal->row - count >= terminal->margin_top)
1323 terminal->row -= count;
1324 else
1325 terminal->row = terminal->margin_top;
1326 terminal->column = 0;
1327 break;
1328 case 'G': /* CHA */
1329 y = set[0] ? args[0] : 1;
1330 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1331
1332 terminal->column = y - 1;
1333 break;
1334 case 'f': /* HVP */
1335 case 'H': /* CUP */
1336 x = (set[1] ? args[1] : 1) - 1;
1337 x = x < 0 ? 0 :
1338 (x >= terminal->width ? terminal->width - 1 : x);
1339
1340 y = (set[0] ? args[0] : 1) - 1;
1341 if (terminal->origin_mode) {
1342 y += terminal->margin_top;
1343 y = y < terminal->margin_top ? terminal->margin_top :
1344 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1345 } else {
1346 y = y < 0 ? 0 :
1347 (y >= terminal->height ? terminal->height - 1 : y);
1348 }
1349
1350 terminal->row = y;
1351 terminal->column = x;
1352 break;
1353 case 'I': /* CHT */
1354 count = set[0] ? args[0] : 1;
1355 if (count == 0) count = 1;
1356 while (count > 0 && terminal->column < terminal->width) {
1357 if (terminal->tab_ruler[terminal->column]) count--;
1358 terminal->column++;
1359 }
1360 terminal->column--;
1361 break;
1362 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001363 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001364 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001365 if (!set[0] || args[0] == 0 || args[0] > 2) {
1366 memset(&row[terminal->column],
1367 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1368 attr_init(&attr_row[terminal->column],
1369 terminal->curr_attr, terminal->width - terminal->column);
1370 for (i = terminal->row + 1; i < terminal->height; i++) {
1371 memset(terminal_get_row(terminal, i),
1372 0, terminal->data_pitch);
1373 attr_init(terminal_get_attr_row(terminal, i),
1374 terminal->curr_attr, terminal->width);
1375 }
1376 } else if (args[0] == 1) {
1377 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1378 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1379 for (i = 0; i < terminal->row; i++) {
1380 memset(terminal_get_row(terminal, i),
1381 0, terminal->data_pitch);
1382 attr_init(terminal_get_attr_row(terminal, i),
1383 terminal->curr_attr, terminal->width);
1384 }
1385 } else if (args[0] == 2) {
1386 for (i = 0; i < terminal->height; i++) {
1387 memset(terminal_get_row(terminal, i),
1388 0, terminal->data_pitch);
1389 attr_init(terminal_get_attr_row(terminal, i),
1390 terminal->curr_attr, terminal->width);
1391 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001392 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001393 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001394 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001395 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001396 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001397 if (!set[0] || args[0] == 0 || args[0] > 2) {
1398 memset(&row[terminal->column], 0,
1399 (terminal->width - terminal->column) * sizeof(union utf8_char));
1400 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1401 terminal->width - terminal->column);
1402 } else if (args[0] == 1) {
1403 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1404 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1405 } else if (args[0] == 2) {
1406 memset(row, 0, terminal->data_pitch);
1407 attr_init(attr_row, terminal->curr_attr, terminal->width);
1408 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001409 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001410 case 'L': /* IL */
1411 count = set[0] ? args[0] : 1;
1412 if (count == 0) count = 1;
1413 if (terminal->row >= terminal->margin_top &&
1414 terminal->row < terminal->margin_bottom)
1415 {
1416 top = terminal->margin_top;
1417 terminal->margin_top = terminal->row;
1418 terminal_scroll(terminal, 0 - count);
1419 terminal->margin_top = top;
1420 } else if (terminal->row == terminal->margin_bottom) {
1421 memset(terminal_get_row(terminal, terminal->row),
1422 0, terminal->data_pitch);
1423 attr_init(terminal_get_attr_row(terminal, terminal->row),
1424 terminal->curr_attr, terminal->width);
1425 }
1426 break;
1427 case 'M': /* DL */
1428 count = set[0] ? args[0] : 1;
1429 if (count == 0) count = 1;
1430 if (terminal->row >= terminal->margin_top &&
1431 terminal->row < terminal->margin_bottom)
1432 {
1433 top = terminal->margin_top;
1434 terminal->margin_top = terminal->row;
1435 terminal_scroll(terminal, count);
1436 terminal->margin_top = top;
1437 } else if (terminal->row == terminal->margin_bottom) {
1438 memset(terminal_get_row(terminal, terminal->row),
1439 0, terminal->data_pitch);
1440 }
1441 break;
1442 case 'P': /* DCH */
1443 count = set[0] ? args[0] : 1;
1444 if (count == 0) count = 1;
1445 terminal_shift_line(terminal, 0 - count);
1446 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001447 case 'S': /* SU */
1448 terminal_scroll(terminal, set[0] ? args[0] : 1);
1449 break;
1450 case 'T': /* SD */
1451 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1452 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001453 case 'X': /* ECH */
1454 count = set[0] ? args[0] : 1;
1455 if (count == 0) count = 1;
1456 if ((terminal->column + count) > terminal->width)
1457 count = terminal->width - terminal->column;
1458 row = terminal_get_row(terminal, terminal->row);
1459 attr_row = terminal_get_attr_row(terminal, terminal->row);
1460 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1461 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1462 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001463 case 'Z': /* CBT */
1464 count = set[0] ? args[0] : 1;
1465 if (count == 0) count = 1;
1466 while (count > 0 && terminal->column >= 0) {
1467 if (terminal->tab_ruler[terminal->column]) count--;
1468 terminal->column--;
1469 }
1470 terminal->column++;
1471 break;
1472 case '`': /* HPA */
1473 y = set[0] ? args[0] : 1;
1474 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1475
1476 terminal->column = y - 1;
1477 break;
1478 case 'b': /* REP */
1479 count = set[0] ? args[0] : 1;
1480 if (count == 0) count = 1;
1481 if (terminal->last_char.byte[0])
1482 for (i = 0; i < count; i++)
1483 handle_char(terminal, terminal->last_char);
1484 terminal->last_char.byte[0] = 0;
1485 break;
1486 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001487 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001488 break;
1489 case 'd': /* VPA */
1490 x = set[0] ? args[0] : 1;
1491 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1492
1493 terminal->row = x - 1;
1494 break;
1495 case 'g': /* TBC */
1496 if (!set[0] || args[0] == 0) {
1497 terminal->tab_ruler[terminal->column] = 0;
1498 } else if (args[0] == 3) {
1499 memset(terminal->tab_ruler, 0, terminal->width);
1500 }
1501 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001502 case 'h': /* SM */
1503 for(i = 0; i < 10 && set[i]; i++) {
1504 handle_term_parameter(terminal, args[i], 1);
1505 }
1506 break;
1507 case 'l': /* RM */
1508 for(i = 0; i < 10 && set[i]; i++) {
1509 handle_term_parameter(terminal, args[i], 0);
1510 }
1511 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001512 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001513 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001514 if (i <= 7 && set[i] && set[i + 1] &&
1515 set[i + 2] && args[i + 1] == 5)
1516 {
1517 if (args[i] == 38) {
1518 handle_sgr(terminal, args[i + 2] + 256);
1519 break;
1520 } else if (args[i] == 48) {
1521 handle_sgr(terminal, args[i + 2] + 512);
1522 break;
1523 }
1524 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001525 if(set[i]) {
1526 handle_sgr(terminal, args[i]);
1527 } else if(i == 0) {
1528 handle_sgr(terminal, 0);
1529 break;
1530 } else {
1531 break;
1532 }
1533 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001534 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001535 case 'n': /* DSR */
1536 i = set[0] ? args[0] : 0;
1537 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001538 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001539 } else if (i == 6) {
1540 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1541 terminal->origin_mode ?
1542 terminal->row+terminal->margin_top : terminal->row+1,
1543 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001544 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001545 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001546 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001547 case 'r':
1548 if(!set[0]) {
1549 terminal->margin_top = 0;
1550 terminal->margin_bottom = terminal->height-1;
1551 terminal->row = 0;
1552 terminal->column = 0;
1553 } else {
1554 top = (set[0] ? args[0] : 1) - 1;
1555 top = top < 0 ? 0 :
1556 (top >= terminal->height ? terminal->height - 1 : top);
1557 bottom = (set[1] ? args[1] : 1) - 1;
1558 bottom = bottom < 0 ? 0 :
1559 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1560 if(bottom > top) {
1561 terminal->margin_top = top;
1562 terminal->margin_bottom = bottom;
1563 } else {
1564 terminal->margin_top = 0;
1565 terminal->margin_bottom = terminal->height-1;
1566 }
1567 if(terminal->origin_mode)
1568 terminal->row = terminal->margin_top;
1569 else
1570 terminal->row = 0;
1571 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001572 }
1573 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001574 case 's':
1575 terminal->saved_row = terminal->row;
1576 terminal->saved_column = terminal->column;
1577 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001578 case 't': /* windowOps */
1579 if (!set[0]) break;
1580 switch (args[0]) {
1581 case 4: /* resize px */
1582 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001583 widget_schedule_resize(terminal->widget,
1584 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001585 }
1586 break;
1587 case 8: /* resize ch */
1588 if (set[1] && set[2]) {
1589 terminal_resize(terminal, args[2], args[1]);
1590 }
1591 break;
1592 case 13: /* report position */
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[3;%d;%dt",
1595 allocation.x, allocation.y);
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 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001599 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001600 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1601 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001602 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001603 break;
1604 case 18: /* report ch */
1605 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1606 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001607 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001608 break;
1609 case 21: /* report title */
1610 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1611 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001612 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001613 break;
1614 default:
1615 if (args[0] >= 24)
1616 terminal_resize(terminal, terminal->width, args[0]);
1617 else
1618 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1619 break;
1620 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001621 case 'u':
1622 terminal->row = terminal->saved_row;
1623 terminal->column = terminal->saved_column;
1624 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001625 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001626 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001627 break;
1628 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001629}
1630
1631static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001632handle_non_csi_escape(struct terminal *terminal, char code)
1633{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001634 switch(code) {
1635 case 'M': /* RI */
1636 terminal->row -= 1;
1637 if(terminal->row < terminal->margin_top) {
1638 terminal->row = terminal->margin_top;
1639 terminal_scroll(terminal, -1);
1640 }
1641 break;
1642 case 'E': /* NEL */
1643 terminal->column = 0;
1644 // fallthrough
1645 case 'D': /* IND */
1646 terminal->row += 1;
1647 if(terminal->row > terminal->margin_bottom) {
1648 terminal->row = terminal->margin_bottom;
1649 terminal_scroll(terminal, +1);
1650 }
1651 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001652 case 'c': /* RIS */
1653 terminal_init(terminal);
1654 break;
1655 case 'H': /* HTS */
1656 terminal->tab_ruler[terminal->column] = 1;
1657 break;
1658 case '7': /* DECSC */
1659 terminal->saved_row = terminal->row;
1660 terminal->saved_column = terminal->column;
1661 terminal->saved_attr = terminal->curr_attr;
1662 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001663 terminal->saved_cs = terminal->cs;
1664 terminal->saved_g0 = terminal->g0;
1665 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001666 break;
1667 case '8': /* DECRC */
1668 terminal->row = terminal->saved_row;
1669 terminal->column = terminal->saved_column;
1670 terminal->curr_attr = terminal->saved_attr;
1671 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001672 terminal->cs = terminal->saved_cs;
1673 terminal->g0 = terminal->saved_g0;
1674 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001675 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001676 case '=': /* DECPAM */
1677 terminal->key_mode = KM_APPLICATION;
1678 break;
1679 case '>': /* DECPNM */
1680 terminal->key_mode = KM_NORMAL;
1681 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001682 default:
1683 fprintf(stderr, "Unknown escape code: %c\n", code);
1684 break;
1685 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001686}
1687
1688static void
1689handle_special_escape(struct terminal *terminal, char special, char code)
1690{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001691 int i, numChars;
1692
1693 if (special == '#') {
1694 switch(code) {
1695 case '8':
1696 /* fill with 'E', no cheap way to do this */
1697 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1698 numChars = terminal->width * terminal->height;
1699 for(i = 0; i < numChars; i++) {
1700 terminal->data[i].byte[0] = 'E';
1701 }
1702 break;
1703 default:
1704 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1705 break;
1706 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001707 } else if (special == '(' || special == ')') {
1708 switch(code) {
1709 case '0':
1710 if (special == '(')
1711 terminal->g0 = CS_SPECIAL;
1712 else
1713 terminal->g1 = CS_SPECIAL;
1714 break;
1715 case 'A':
1716 if (special == '(')
1717 terminal->g0 = CS_UK;
1718 else
1719 terminal->g1 = CS_UK;
1720 break;
1721 case 'B':
1722 if (special == '(')
1723 terminal->g0 = CS_US;
1724 else
1725 terminal->g1 = CS_US;
1726 break;
1727 default:
1728 fprintf(stderr, "Unknown character set %c\n", code);
1729 break;
1730 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001731 } else {
1732 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1733 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001734}
1735
1736static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001737handle_sgr(struct terminal *terminal, int code)
1738{
1739 switch(code) {
1740 case 0:
1741 terminal->curr_attr = terminal->color_scheme->default_attr;
1742 break;
1743 case 1:
1744 terminal->curr_attr.a |= ATTRMASK_BOLD;
1745 if (terminal->curr_attr.fg < 8)
1746 terminal->curr_attr.fg += 8;
1747 break;
1748 case 4:
1749 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1750 break;
1751 case 5:
1752 terminal->curr_attr.a |= ATTRMASK_BLINK;
1753 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001754 case 8:
1755 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1756 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001757 case 2:
1758 case 21:
1759 case 22:
1760 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1761 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1762 terminal->curr_attr.fg -= 8;
1763 break;
1764 case 24:
1765 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1766 break;
1767 case 25:
1768 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1769 break;
1770 case 7:
1771 case 26:
1772 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1773 break;
1774 case 27:
1775 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1776 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001777 case 28:
1778 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1779 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001780 case 39:
1781 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1782 break;
1783 case 49:
1784 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1785 break;
1786 default:
1787 if(code >= 30 && code <= 37) {
1788 terminal->curr_attr.fg = code - 30;
1789 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1790 terminal->curr_attr.fg += 8;
1791 } else if(code >= 40 && code <= 47) {
1792 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001793 } else if (code >= 90 && code <= 97) {
1794 terminal->curr_attr.fg = code - 90 + 8;
1795 } else if (code >= 100 && code <= 107) {
1796 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001797 } else if(code >= 256 && code < 512) {
1798 terminal->curr_attr.fg = code - 256;
1799 } else if(code >= 512 && code < 768) {
1800 terminal->curr_attr.bg = code - 512;
1801 } else {
1802 fprintf(stderr, "Unknown SGR code: %d\n", code);
1803 }
1804 break;
1805 }
1806}
1807
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001808/* Returns 1 if c was special, otherwise 0 */
1809static int
1810handle_special_char(struct terminal *terminal, char c)
1811{
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001812 union utf8_char *row;
1813 struct attr *attr_row;
1814
1815 row = terminal_get_row(terminal, terminal->row);
1816 attr_row = terminal_get_attr_row(terminal, terminal->row);
1817
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001818 switch(c) {
1819 case '\r':
1820 terminal->column = 0;
1821 break;
1822 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001823 if (terminal->mode & MODE_LF_NEWLINE) {
1824 terminal->column = 0;
1825 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001826 /* fallthrough */
1827 case '\v':
1828 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001829 terminal->row++;
1830 if(terminal->row > terminal->margin_bottom) {
1831 terminal->row = terminal->margin_bottom;
1832 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001833 }
1834
1835 break;
1836 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001837 while (terminal->column < terminal->width) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001838 if (terminal->mode & MODE_IRM)
1839 terminal_shift_line(terminal, +1);
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001840
1841 if (row[terminal->column].byte[0] == '\0') {
1842 row[terminal->column].byte[0] = ' ';
1843 row[terminal->column].byte[1] = '\0';
1844 attr_row[terminal->column] = terminal->curr_attr;
1845 }
1846
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001847 terminal->column++;
Kristian Høgsbergcca3c2f2012-06-20 15:56:13 -04001848 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001849 }
1850 if (terminal->column >= terminal->width) {
1851 terminal->column = terminal->width - 1;
1852 }
1853
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001854 break;
1855 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001856 if (terminal->column >= terminal->width) {
1857 terminal->column = terminal->width - 2;
1858 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001859 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001860 } else if (terminal->mode & MODE_AUTOWRAP) {
1861 terminal->column = terminal->width - 1;
1862 terminal->row -= 1;
1863 if (terminal->row < terminal->margin_top) {
1864 terminal->row = terminal->margin_top;
1865 terminal_scroll(terminal, -1);
1866 }
1867 }
1868
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001869 break;
1870 case '\a':
1871 /* Bell */
1872 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001873 case '\x0E': /* SO */
1874 terminal->cs = terminal->g1;
1875 break;
1876 case '\x0F': /* SI */
1877 terminal->cs = terminal->g0;
1878 break;
Kristian Høgsberg2a1aa4e2012-08-03 09:37:05 -04001879 case '\0':
1880 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001881 default:
1882 return 0;
1883 }
1884
1885 return 1;
1886}
1887
1888static void
1889handle_char(struct terminal *terminal, union utf8_char utf8)
1890{
1891 union utf8_char *row;
1892 struct attr *attr_row;
1893
1894 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001895
1896 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001897
1898 /* There are a whole lot of non-characters, control codes,
1899 * and formatting codes that should probably be ignored,
1900 * for example: */
1901 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1902 /* BOM, ignore */
1903 return;
1904 }
1905
1906 /* Some of these non-characters should be translated, e.g.: */
1907 if (utf8.byte[0] < 32) {
1908 utf8.byte[0] = utf8.byte[0] + 64;
1909 }
1910
1911 /* handle right margin effects */
1912 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001913 if (terminal->mode & MODE_AUTOWRAP) {
1914 terminal->column = 0;
1915 terminal->row += 1;
1916 if (terminal->row > terminal->margin_bottom) {
1917 terminal->row = terminal->margin_bottom;
1918 terminal_scroll(terminal, +1);
1919 }
1920 } else {
1921 terminal->column--;
1922 }
1923 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001924
1925 row = terminal_get_row(terminal, terminal->row);
1926 attr_row = terminal_get_attr_row(terminal, terminal->row);
1927
Callum Lowcay69e96582011-01-07 19:47:00 +00001928 if (terminal->mode & MODE_IRM)
1929 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001930 row[terminal->column] = utf8;
1931 attr_row[terminal->column++] = terminal->curr_attr;
1932
Peng Wucfcc1112013-08-19 10:59:21 +08001933 /* cursor jump for wide character. */
1934 if (is_wide(utf8))
1935 row[terminal->column++].ch = 0x200B; /* space glyph */
1936
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001937 if (utf8.ch != terminal->last_char.ch)
1938 terminal->last_char = utf8;
1939}
1940
Callum Lowcay30eeae52011-01-07 19:46:55 +00001941static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001942escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1943{
1944 int len, i;
1945
1946 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1947 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1948 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1949 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1950 else len = 1; /* Invalid, cannot happen */
1951
1952 if (terminal->escape_length + len <= MAX_ESCAPE) {
1953 for (i = 0; i < len; i++)
1954 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1955 terminal->escape_length += len;
1956 } else if (terminal->escape_length < MAX_ESCAPE) {
1957 terminal->escape[terminal->escape_length++] = 0;
1958 }
1959}
1960
1961static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001962terminal_data(struct terminal *terminal, const char *data, size_t length)
1963{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001964 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001965 union utf8_char utf8;
1966 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001967
1968 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001969 parser_state =
1970 utf8_next_char(&terminal->state_machine, data[i]);
1971 switch(parser_state) {
1972 case utf8state_accept:
1973 utf8.ch = terminal->state_machine.s.ch;
1974 break;
1975 case utf8state_reject:
1976 /* the unicode replacement character */
1977 utf8.byte[0] = 0xEF;
1978 utf8.byte[1] = 0xBF;
1979 utf8.byte[2] = 0xBD;
1980 utf8.byte[3] = 0x00;
1981 break;
1982 default:
1983 continue;
1984 }
1985
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001986 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001987 switch (terminal->state) {
1988 case escape_state_escape:
1989 escape_append_utf8(terminal, utf8);
1990 switch (utf8.byte[0]) {
1991 case 'P': /* DCS */
1992 terminal->state = escape_state_dcs;
1993 break;
1994 case '[': /* CSI */
1995 terminal->state = escape_state_csi;
1996 break;
1997 case ']': /* OSC */
1998 terminal->state = escape_state_osc;
1999 break;
2000 case '#':
2001 case '(':
2002 case ')': /* special */
2003 terminal->state = escape_state_special;
2004 break;
2005 case '^': /* PM (not implemented) */
2006 case '_': /* APC (not implemented) */
2007 terminal->state = escape_state_ignore;
2008 break;
2009 default:
2010 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002011 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13002012 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002013 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002014 continue;
2015 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002016 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
2017 /* do nothing */
2018 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002019 terminal->escape_flags |= ESC_FLAG_WHAT;
2020 } else if (utf8.byte[0] == '>') {
2021 terminal->escape_flags |= ESC_FLAG_GT;
2022 } else if (utf8.byte[0] == '!') {
2023 terminal->escape_flags |= ESC_FLAG_BANG;
2024 } else if (utf8.byte[0] == '$') {
2025 terminal->escape_flags |= ESC_FLAG_CASH;
2026 } else if (utf8.byte[0] == '\'') {
2027 terminal->escape_flags |= ESC_FLAG_SQUOTE;
2028 } else if (utf8.byte[0] == '"') {
2029 terminal->escape_flags |= ESC_FLAG_DQUOTE;
2030 } else if (utf8.byte[0] == ' ') {
2031 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002032 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002033 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002034 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13002035 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002036 }
2037
2038 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
2039 utf8.byte[0] == '`')
2040 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002041 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002042 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002043 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002044 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002045 continue;
2046 case escape_state_inner_escape:
2047 if (utf8.byte[0] == '\\') {
2048 terminal->state = escape_state_normal;
2049 if (terminal->outer_state == escape_state_dcs) {
2050 handle_dcs(terminal);
2051 } else if (terminal->outer_state == escape_state_osc) {
2052 handle_osc(terminal);
2053 }
2054 } else if (utf8.byte[0] == '\e') {
2055 terminal->state = terminal->outer_state;
2056 escape_append_utf8(terminal, utf8);
2057 if (terminal->escape_length >= MAX_ESCAPE)
2058 terminal->state = escape_state_normal;
2059 } else {
2060 terminal->state = terminal->outer_state;
2061 if (terminal->escape_length < MAX_ESCAPE)
2062 terminal->escape[terminal->escape_length++] = '\e';
2063 escape_append_utf8(terminal, utf8);
2064 if (terminal->escape_length >= MAX_ESCAPE)
2065 terminal->state = escape_state_normal;
2066 }
2067 continue;
2068 case escape_state_dcs:
2069 case escape_state_osc:
2070 case escape_state_ignore:
2071 if (utf8.byte[0] == '\e') {
2072 terminal->outer_state = terminal->state;
2073 terminal->state = escape_state_inner_escape;
2074 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
2075 terminal->state = escape_state_normal;
2076 handle_osc(terminal);
2077 } else {
2078 escape_append_utf8(terminal, utf8);
2079 if (terminal->escape_length >= MAX_ESCAPE)
2080 terminal->state = escape_state_normal;
2081 }
2082 continue;
2083 case escape_state_special:
2084 escape_append_utf8(terminal, utf8);
2085 terminal->state = escape_state_normal;
2086 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2087 handle_special_escape(terminal, terminal->escape[1],
2088 utf8.byte[0]);
2089 }
2090 continue;
2091 default:
2092 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002093 }
2094
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002095 /* this is valid, because ASCII characters are never used to
2096 * introduce a multibyte sequence in UTF-8 */
2097 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002098 terminal->state = escape_state_escape;
2099 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002100 terminal->escape[0] = '\e';
2101 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002102 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002103 } else {
2104 handle_char(terminal, utf8);
2105 } /* if */
2106 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002107
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002108 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002109}
2110
2111static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002112data_source_target(void *data,
2113 struct wl_data_source *source, const char *mime_type)
2114{
2115 fprintf(stderr, "data_source_target, %s\n", mime_type);
2116}
2117
2118static void
2119data_source_send(void *data,
2120 struct wl_data_source *source,
2121 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002122{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002123 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002124
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002125 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002126}
2127
2128static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002129data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002130{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002131 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002132}
2133
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002134static const struct wl_data_source_listener data_source_listener = {
2135 data_source_target,
2136 data_source_send,
2137 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002138};
2139
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07002140static const char text_mime_type[] = "text/plain;charset=utf-8";
2141
2142static void
2143data_handler(struct window *window,
2144 struct input *input,
2145 float x, float y, const char **types, void *data)
2146{
2147 int i, has_text = 0;
2148
2149 if (!types)
2150 return;
2151 for (i = 0; types[i]; i++)
2152 if (strcmp(types[i], text_mime_type) == 0)
2153 has_text = 1;
2154
2155 if (!has_text) {
2156 input_accept(input, NULL);
2157 } else {
2158 input_accept(input, text_mime_type);
2159 }
2160}
2161
2162static void
2163drop_handler(struct window *window, struct input *input,
2164 int32_t x, int32_t y, void *data)
2165{
2166 struct terminal *terminal = data;
2167
2168 input_receive_drag_data_to_fd(input, text_mime_type, terminal->master);
2169}
2170
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002171static void
2172fullscreen_handler(struct window *window, void *data)
2173{
2174 struct terminal *terminal = data;
2175
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002176 window_set_fullscreen(window, !window_is_fullscreen(terminal->window));
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002177}
2178
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002179static void
2180close_handler(struct window *window, void *data)
2181{
2182 struct terminal *terminal = data;
2183
2184 terminal_destroy(terminal);
2185}
2186
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002187static int
2188handle_bound_key(struct terminal *terminal,
2189 struct input *input, uint32_t sym, uint32_t time)
2190{
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002191 struct terminal *new_terminal;
2192
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002193 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002194 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002195 /* Cut selection; terminal doesn't do cut, fall
2196 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002197 case XKB_KEY_C:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002198 terminal->selection =
2199 display_create_data_source(terminal->display);
2200 wl_data_source_offer(terminal->selection,
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002201 "text/plain;charset=utf-8");
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002202 wl_data_source_add_listener(terminal->selection,
2203 &data_source_listener, terminal);
Kristian Høgsbergfee91be2012-06-02 21:21:41 -04002204 input_set_selection(input, terminal->selection,
2205 display_get_serial(terminal->display));
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002206 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002207 case XKB_KEY_V:
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002208 input_receive_selection_data_to_fd(input,
2209 "text/plain;charset=utf-8",
2210 terminal->master);
2211
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002212 return 1;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002213
2214 case XKB_KEY_N:
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002215 new_terminal = terminal_create(terminal->display);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002216 if (terminal_run(new_terminal, option_shell))
2217 terminal_destroy(new_terminal);
2218
2219 return 1;
2220
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002221 default:
2222 return 0;
2223 }
2224}
2225
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002226static void
2227key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002228 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2229 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002230{
2231 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002232 char ch[MAX_RESPONSE];
Kristian Høgsberg88fd4082012-06-21 15:55:03 -04002233 uint32_t modifiers, serial;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002234 int ret, len = 0;
2235 bool convert_utf8 = true;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002236
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002237 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002238 if ((modifiers & MOD_CONTROL_MASK) &&
2239 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002240 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2241 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002242 return;
2243
Daniel Stonea8468712012-11-07 17:51:35 +11002244 /* Map keypad symbols to 'normal' equivalents before processing */
2245 switch (sym) {
2246 case XKB_KEY_KP_Space:
2247 sym = XKB_KEY_space;
2248 break;
2249 case XKB_KEY_KP_Tab:
2250 sym = XKB_KEY_Tab;
2251 break;
2252 case XKB_KEY_KP_Enter:
2253 sym = XKB_KEY_Return;
2254 break;
2255 case XKB_KEY_KP_Left:
2256 sym = XKB_KEY_Left;
2257 break;
2258 case XKB_KEY_KP_Up:
2259 sym = XKB_KEY_Up;
2260 break;
2261 case XKB_KEY_KP_Right:
2262 sym = XKB_KEY_Right;
2263 break;
2264 case XKB_KEY_KP_Down:
2265 sym = XKB_KEY_Down;
2266 break;
2267 case XKB_KEY_KP_Equal:
2268 sym = XKB_KEY_equal;
2269 break;
2270 case XKB_KEY_KP_Multiply:
2271 sym = XKB_KEY_asterisk;
2272 break;
2273 case XKB_KEY_KP_Add:
2274 sym = XKB_KEY_plus;
2275 break;
2276 case XKB_KEY_KP_Separator:
2277 /* Note this is actually locale-dependent and should mostly be
2278 * a comma. But leave it as period until we one day start
2279 * doing the right thing. */
2280 sym = XKB_KEY_period;
2281 break;
2282 case XKB_KEY_KP_Subtract:
2283 sym = XKB_KEY_minus;
2284 break;
2285 case XKB_KEY_KP_Decimal:
2286 sym = XKB_KEY_period;
2287 break;
2288 case XKB_KEY_KP_Divide:
2289 sym = XKB_KEY_slash;
2290 break;
2291 case XKB_KEY_KP_0:
2292 case XKB_KEY_KP_1:
2293 case XKB_KEY_KP_2:
2294 case XKB_KEY_KP_3:
2295 case XKB_KEY_KP_4:
2296 case XKB_KEY_KP_5:
2297 case XKB_KEY_KP_6:
2298 case XKB_KEY_KP_7:
2299 case XKB_KEY_KP_8:
2300 case XKB_KEY_KP_9:
2301 sym = (sym - XKB_KEY_KP_0) + XKB_KEY_0;
2302 break;
2303 default:
2304 break;
2305 }
2306
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002307 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002308 case XKB_KEY_BackSpace:
Kristian Høgsbergb7f94bf2012-06-21 12:29:36 -04002309 if (modifiers & MOD_ALT_MASK)
2310 ch[len++] = 0x1b;
Kristian Høgsberg71a4cf42012-06-20 17:57:56 -04002311 ch[len++] = 0x7f;
2312 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002313 case XKB_KEY_Tab:
2314 case XKB_KEY_Linefeed:
2315 case XKB_KEY_Clear:
2316 case XKB_KEY_Pause:
2317 case XKB_KEY_Scroll_Lock:
2318 case XKB_KEY_Sys_Req:
2319 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002320 ch[len++] = sym & 0x7f;
2321 break;
2322
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002323 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002324 if (terminal->mode & MODE_LF_NEWLINE) {
2325 ch[len++] = 0x0D;
2326 ch[len++] = 0x0A;
2327 } else {
2328 ch[len++] = 0x0D;
2329 }
2330 break;
2331
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002332 case XKB_KEY_Shift_L:
2333 case XKB_KEY_Shift_R:
2334 case XKB_KEY_Control_L:
2335 case XKB_KEY_Control_R:
2336 case XKB_KEY_Alt_L:
2337 case XKB_KEY_Alt_R:
Kristian Høgsberg22fbcf72012-06-22 12:18:56 -04002338 case XKB_KEY_Meta_L:
2339 case XKB_KEY_Meta_R:
2340 case XKB_KEY_Super_L:
2341 case XKB_KEY_Super_R:
2342 case XKB_KEY_Hyper_L:
2343 case XKB_KEY_Hyper_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002344 break;
2345
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002346 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002347 len = function_key_response('[', 2, modifiers, '~', ch);
2348 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002349 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002350 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2351 ch[len++] = '\x04';
2352 } else {
2353 len = function_key_response('[', 3, modifiers, '~', ch);
2354 }
2355 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002356 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002357 len = function_key_response('[', 5, modifiers, '~', ch);
2358 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002359 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002360 len = function_key_response('[', 6, modifiers, '~', ch);
2361 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002362 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002363 len = function_key_response('O', 1, modifiers, 'P', ch);
2364 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002365 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002366 len = function_key_response('O', 1, modifiers, 'Q', ch);
2367 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002368 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002369 len = function_key_response('O', 1, modifiers, 'R', ch);
2370 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002371 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002372 len = function_key_response('O', 1, modifiers, 'S', ch);
2373 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002374 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002375 len = function_key_response('[', 15, modifiers, '~', ch);
2376 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002377 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002378 len = function_key_response('[', 17, modifiers, '~', ch);
2379 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002380 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002381 len = function_key_response('[', 18, modifiers, '~', ch);
2382 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002383 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002384 len = function_key_response('[', 19, modifiers, '~', ch);
2385 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002386 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002387 len = function_key_response('[', 20, modifiers, '~', ch);
2388 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002389 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002390 len = function_key_response('[', 21, modifiers, '~', ch);
2391 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002392 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002393 len = function_key_response('[', 24, modifiers, '~', ch);
2394 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002395 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002396 /* Handle special keys with alternate mappings */
2397 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2398 if (len != 0) break;
2399
Kristian Høgsberg70163132012-05-08 15:55:39 -04002400 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002401 if (sym >= '3' && sym <= '7')
2402 sym = (sym & 0x1f) + 8;
2403
2404 if (!((sym >= '!' && sym <= '/') ||
2405 (sym >= '8' && sym <= '?') ||
2406 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2407 else if (sym == '2') sym = 0x00;
2408 else if (sym == '/') sym = 0x1F;
2409 else if (sym == '8' || sym == '?') sym = 0x7F;
Kristian Høgsbergae9e0732012-06-21 12:30:15 -04002410 }
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002411 if (modifiers & MOD_ALT_MASK) {
2412 if (terminal->mode & MODE_ALT_SENDS_ESC) {
2413 ch[len++] = 0x1b;
2414 } else {
2415 sym = sym | 0x80;
2416 convert_utf8 = false;
2417 }
Callum Lowcay7e08e902011-01-07 19:47:02 +00002418 }
2419
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002420 if ((sym < 128) ||
2421 (!convert_utf8 && sym < 256)) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002422 ch[len++] = sym;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002423 } else {
2424 ret = xkb_keysym_to_utf8(sym, ch + len,
2425 MAX_RESPONSE - len);
2426 if (ret < 0)
2427 fprintf(stderr,
2428 "Warning: buffer too small to encode "
2429 "UTF8 character\n");
2430 else
2431 len += ret;
2432 }
2433
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002434 break;
2435 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002436
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002437 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04002438 terminal_write(terminal, ch, len);
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002439
2440 /* Hide cursor, except if this was coming from a
2441 * repeating key press. */
2442 serial = display_get_serial(terminal->display);
2443 if (terminal->hide_cursor_serial != serial) {
2444 input_set_pointer_image(input, CURSOR_BLANK);
2445 terminal->hide_cursor_serial = serial;
2446 }
2447 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002448}
2449
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002450static void
2451keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002452 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002453{
2454 struct terminal *terminal = data;
2455
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002456 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002457}
2458
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002459static int wordsep(int ch)
2460{
2461 const char extra[] = "-,./?%&#:_=+@~";
2462
Andre Heider552d12b2012-08-02 20:59:43 +02002463 if (ch > 127)
2464 return 1;
2465
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002466 return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
2467}
2468
2469static int
2470recompute_selection(struct terminal *terminal)
2471{
2472 struct rectangle allocation;
2473 int col, x, width, height;
2474 int start_row, end_row;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002475 int word_start, eol;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002476 int side_margin, top_margin;
2477 int start_x, end_x;
2478 int cw, ch;
2479 union utf8_char *data;
2480
Peng Wuf291f202013-06-06 15:32:41 +08002481 cw = terminal->average_width;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002482 ch = terminal->extents.height;
2483 widget_get_allocation(terminal->widget, &allocation);
2484 width = terminal->width * cw;
2485 height = terminal->height * ch;
2486 side_margin = allocation.x + (allocation.width - width) / 2;
2487 top_margin = allocation.y + (allocation.height - height) / 2;
2488
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002489 start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1;
2490 end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1;
2491
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002492 if (start_row < end_row ||
2493 (start_row == end_row &&
2494 terminal->selection_start_x < terminal->selection_end_x)) {
2495 terminal->selection_start_row = start_row;
2496 terminal->selection_end_row = end_row;
2497 start_x = terminal->selection_start_x;
2498 end_x = terminal->selection_end_x;
2499 } else {
2500 terminal->selection_start_row = end_row;
2501 terminal->selection_end_row = start_row;
2502 start_x = terminal->selection_end_x;
2503 end_x = terminal->selection_start_x;
2504 }
2505
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002506 eol = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002507 if (terminal->selection_start_row < 0) {
2508 terminal->selection_start_row = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002509 terminal->selection_start_col = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002510 } else {
2511 x = side_margin + cw / 2;
2512 data = terminal_get_row(terminal,
2513 terminal->selection_start_row);
2514 word_start = 0;
2515 for (col = 0; col < terminal->width; col++, x += cw) {
2516 if (col == 0 || wordsep(data[col - 1].ch))
2517 word_start = col;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002518 if (data[col].ch != 0)
2519 eol = col + 1;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002520 if (start_x < x)
2521 break;
2522 }
2523
2524 switch (terminal->dragging) {
2525 case SELECT_LINE:
2526 terminal->selection_start_col = 0;
2527 break;
2528 case SELECT_WORD:
2529 terminal->selection_start_col = word_start;
2530 break;
2531 case SELECT_CHAR:
2532 terminal->selection_start_col = col;
2533 break;
2534 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002535 }
2536
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002537 if (terminal->selection_end_row >= terminal->height) {
2538 terminal->selection_end_row = terminal->height;
2539 terminal->selection_end_col = 0;
2540 } else {
2541 x = side_margin + cw / 2;
2542 data = terminal_get_row(terminal, terminal->selection_end_row);
2543 for (col = 0; col < terminal->width; col++, x += cw) {
2544 if (terminal->dragging == SELECT_CHAR && end_x < x)
2545 break;
2546 if (terminal->dragging == SELECT_WORD &&
2547 end_x < x && wordsep(data[col].ch))
2548 break;
2549 }
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002550 terminal->selection_end_col = col;
2551 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002552
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002553 if (terminal->selection_end_col != terminal->selection_start_col ||
2554 terminal->selection_start_row != terminal->selection_end_row) {
2555 col = terminal->selection_end_col;
2556 if (col > 0 && data[col - 1].ch == 0)
2557 terminal->selection_end_col = terminal->width;
2558 data = terminal_get_row(terminal, terminal->selection_start_row);
2559 if (data[terminal->selection_start_col].ch == 0)
2560 terminal->selection_start_col = eol;
2561 }
2562
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002563 return 1;
2564}
2565
Kristian Høgsberg59826582011-01-20 11:56:57 -05002566static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002567button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002568 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002569 uint32_t button,
2570 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002571{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002572 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002573
2574 switch (button) {
2575 case 272:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002576 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002577
2578 if (time - terminal->button_time < 500)
2579 terminal->click_count++;
2580 else
2581 terminal->click_count = 1;
2582
2583 terminal->button_time = time;
2584 terminal->dragging =
2585 (terminal->click_count - 1) % 3 + SELECT_CHAR;
2586
Kristian Høgsberg59826582011-01-20 11:56:57 -05002587 input_get_position(input,
2588 &terminal->selection_start_x,
2589 &terminal->selection_start_y);
2590 terminal->selection_end_x = terminal->selection_start_x;
2591 terminal->selection_end_y = terminal->selection_start_y;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002592 if (recompute_selection(terminal))
2593 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002594 } else {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002595 terminal->dragging = SELECT_NONE;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002596 }
2597 break;
2598 }
2599}
2600
2601static int
Kristian Høgsberg29784402012-06-28 14:27:02 -04002602enter_handler(struct widget *widget,
2603 struct input *input, float x, float y, void *data)
2604{
2605 return CURSOR_IBEAM;
2606}
2607
2608static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002609motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002610 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002611 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002612{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002613 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002614
2615 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002616 input_get_position(input,
2617 &terminal->selection_end_x,
2618 &terminal->selection_end_y);
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002619
2620 if (recompute_selection(terminal))
2621 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002622 }
2623
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002624 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002625}
2626
Alexander Larssonde79dd02013-05-22 14:41:34 +02002627static void
2628output_handler(struct window *window, struct output *output, int enter,
2629 void *data)
2630{
2631 if (enter)
2632 window_set_buffer_transform(window, output_get_transform(output));
2633 window_set_buffer_scale(window, window_get_output_scale(window));
2634 window_schedule_redraw(window);
2635}
2636
Peng Wuf291f202013-06-06 15:32:41 +08002637#ifndef howmany
2638#define howmany(x, y) (((x) + ((y) - 1)) / (y))
2639#endif
2640
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002641static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002642terminal_create(struct display *display)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002643{
2644 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002645 cairo_surface_t *surface;
2646 cairo_t *cr;
Peng Wuf291f202013-06-06 15:32:41 +08002647 cairo_text_extents_t text_extents;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002648
Peter Huttererf3d62272013-08-08 11:57:05 +10002649 terminal = xzalloc(sizeof *terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002650 terminal->color_scheme = &DEFAULT_COLORS;
2651 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002652 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002653 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002654 terminal->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002655 terminal->widget = frame_create(terminal->window, terminal);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002656 window_set_title(terminal->window, "Wayland Terminal");
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002657 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002658
2659 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002660 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002661
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002662 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002663 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002664
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002665 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002666 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002667 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002668 keyboard_focus_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002669 window_set_fullscreen_handler(terminal->window, fullscreen_handler);
Alexander Larssonde79dd02013-05-22 14:41:34 +02002670 window_set_output_handler(terminal->window, output_handler);
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002671 window_set_close_handler(terminal->window, close_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002672
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07002673 window_set_data_handler(terminal->window, data_handler);
2674 window_set_drop_handler(terminal->window, drop_handler);
2675
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002676 widget_set_redraw_handler(terminal->widget, redraw_handler);
2677 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002678 widget_set_button_handler(terminal->widget, button_handler);
Kristian Høgsberg29784402012-06-28 14:27:02 -04002679 widget_set_enter_handler(terminal->widget, enter_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002680 widget_set_motion_handler(terminal->widget, motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002681
Kristian Høgsberg09531622010-06-14 23:22:15 -04002682 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2683 cr = cairo_create(surface);
Kristian Høgsberg38912df2012-06-27 17:52:23 -04002684 cairo_set_font_size(cr, option_font_size);
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_BOLD);
2688 terminal->font_bold = cairo_get_scaled_font (cr);
2689 cairo_scaled_font_reference(terminal->font_bold);
2690
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002691 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002692 CAIRO_FONT_SLANT_NORMAL,
2693 CAIRO_FONT_WEIGHT_NORMAL);
2694 terminal->font_normal = cairo_get_scaled_font (cr);
2695 cairo_scaled_font_reference(terminal->font_normal);
2696
Kristian Høgsberg09531622010-06-14 23:22:15 -04002697 cairo_font_extents(cr, &terminal->extents);
Peng Wuf291f202013-06-06 15:32:41 +08002698
2699 /* Compute the average ascii glyph width */
2700 cairo_text_extents(cr, TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS,
2701 &text_extents);
2702 terminal->average_width = howmany
2703 (text_extents.width,
2704 strlen(TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS));
2705 terminal->average_width = ceil(terminal->average_width);
2706
Kristian Høgsberg09531622010-06-14 23:22:15 -04002707 cairo_destroy(cr);
2708 cairo_surface_destroy(surface);
2709
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002710 terminal_resize(terminal, 20, 5); /* Set minimum size first */
Kristian Høgsberga1627922012-06-20 17:30:03 -04002711 terminal_resize(terminal, 80, 25);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002712
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002713 wl_list_insert(terminal_list.prev, &terminal->link);
2714
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002715 return terminal;
2716}
2717
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002718static void
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002719terminal_destroy(struct terminal *terminal)
2720{
Dima Ryazanova85292e2012-11-29 00:27:09 -08002721 display_unwatch_fd(terminal->display, terminal->master);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002722 window_destroy(terminal->window);
2723 close(terminal->master);
2724 wl_list_remove(&terminal->link);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002725
2726 if (wl_list_empty(&terminal_list))
Dima Ryazanova85292e2012-11-29 00:27:09 -08002727 display_exit(terminal->display);
2728
2729 free(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002730}
2731
2732static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002733io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002734{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002735 struct terminal *terminal =
2736 container_of(task, struct terminal, io_task);
2737 char buffer[256];
2738 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002739
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002740 if (events & EPOLLHUP) {
2741 terminal_destroy(terminal);
2742 return;
2743 }
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002744
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002745 len = read(terminal->master, buffer, sizeof buffer);
2746 if (len < 0)
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002747 terminal_destroy(terminal);
2748 else
2749 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002750}
2751
2752static int
2753terminal_run(struct terminal *terminal, const char *path)
2754{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002755 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002756 pid_t pid;
2757
2758 pid = forkpty(&master, NULL, NULL, NULL);
2759 if (pid == 0) {
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04002760 setenv("TERM", option_term, 1);
2761 setenv("COLORTERM", option_term, 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002762 if (execl(path, path, NULL)) {
2763 printf("exec failed: %m\n");
2764 exit(EXIT_FAILURE);
2765 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002766 } else if (pid < 0) {
2767 fprintf(stderr, "failed to fork and create pty (%m).\n");
2768 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002769 }
2770
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002771 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002772 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002773 terminal->io_task.run = io_handler;
2774 display_watch_fd(terminal->display, terminal->master,
2775 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002776
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002777 window_set_fullscreen(terminal->window, option_fullscreen);
2778 if (!window_is_fullscreen(terminal->window))
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002779 terminal_resize(terminal, 80, 24);
2780
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002781 return 0;
2782}
2783
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002784static const struct weston_option terminal_options[] = {
2785 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002786 { WESTON_OPTION_STRING, "font", 0, &option_font },
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002787 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002788};
2789
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002790int main(int argc, char *argv[])
2791{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002792 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002793 struct terminal *terminal;
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -07002794 struct weston_config *config;
2795 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07002796 int config_fd;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002797
Peng Wucfcc1112013-08-19 10:59:21 +08002798 /* as wcwidth is locale-dependent,
2799 wcwidth needs setlocale call to function properly. */
2800 setlocale(LC_ALL, "");
2801
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002802 option_shell = getenv("SHELL");
2803 if (!option_shell)
2804 option_shell = "/bin/bash";
2805
Ossama Othmana50e6e42013-05-14 09:48:26 -07002806 config_fd = open_config_file("weston.ini");
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -07002807 config = weston_config_parse(config_fd);
Ossama Othmana50e6e42013-05-14 09:48:26 -07002808 close(config_fd);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002809
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -07002810 s = weston_config_get_section(config, "terminal", NULL, NULL);
2811 weston_config_section_get_string(s, "font", &option_font, "mono");
2812 weston_config_section_get_int(s, "font-size", &option_font_size, 14);
2813 weston_config_section_get_string(s, "term", &option_term, "xterm");
2814 weston_config_destroy(config);
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002815
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002816 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002817 if (d == NULL) {
2818 fprintf(stderr, "failed to create display: %m\n");
2819 return -1;
2820 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002821
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002822 wl_list_init(&terminal_list);
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002823 terminal = terminal_create(d);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002824 if (terminal_run(terminal, option_shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002825 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002826
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002827 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002828
2829 return 0;
2830}