blob: 6701fb62c79ef38964a0aeb97e8cc4b372a2d93a [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
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +020023#include <stdbool.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050024#include <stdint.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <fcntl.h>
29#include <unistd.h>
30#include <math.h>
31#include <time.h>
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050032#include <pty.h>
Kristian Høgsbergf04e8382008-12-08 00:07:49 -050033#include <ctype.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050034#include <cairo.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040035#include <sys/epoll.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050036
Pekka Paalanen50719bc2011-11-22 14:18:50 +020037#include <wayland-client.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050038
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -040039#include "../shared/config-parser.h"
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050040#include "window.h"
41
Kristian Høgsberg0395f302008-12-22 12:14:50 -050042static int option_fullscreen;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -040043static char *option_font = "mono";
Kristian Høgsberg38912df2012-06-27 17:52:23 -040044static int option_font_size = 14;
Kristian Høgsbergde845cf2012-06-20 22:14:31 -040045static char *option_term = "xterm";
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040046static char *option_shell;
47
48static struct wl_list terminal_list;
49
50static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -040051terminal_create(struct display *display);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040052static void
53terminal_destroy(struct terminal *terminal);
54static int
55terminal_run(struct terminal *terminal, const char *path);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050056
Peng Wuf291f202013-06-06 15:32:41 +080057#define TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS \
58 " !\"#$%&'()*+,-./" \
59 "0123456789" \
60 ":;<=>?@" \
61 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
62 "[\\]^_`" \
63 "abcdefghijklmnopqrstuvwxyz" \
64 "{|}~" \
65 ""
66
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050067#define MOD_SHIFT 0x01
68#define MOD_ALT 0x02
69#define MOD_CTRL 0x04
70
Callum Lowcay30eeae52011-01-07 19:46:55 +000071#define ATTRMASK_BOLD 0x01
72#define ATTRMASK_UNDERLINE 0x02
73#define ATTRMASK_BLINK 0x04
74#define ATTRMASK_INVERSE 0x08
Callum Lowcay81179db2011-01-10 12:14:01 +130075#define ATTRMASK_CONCEALED 0x10
Callum Lowcay30eeae52011-01-07 19:46:55 +000076
Callum Lowcayb8609ad2011-01-07 19:46:57 +000077/* Buffer sizes */
Callum Lowcayef57a9b2011-01-14 20:46:23 +130078#define MAX_RESPONSE 256
79#define MAX_ESCAPE 255
Callum Lowcayb8609ad2011-01-07 19:46:57 +000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +000081/* Terminal modes */
82#define MODE_SHOW_CURSOR 0x00000001
83#define MODE_INVERSE 0x00000002
84#define MODE_AUTOWRAP 0x00000004
85#define MODE_AUTOREPEAT 0x00000008
86#define MODE_LF_NEWLINE 0x00000010
Callum Lowcay69e96582011-01-07 19:47:00 +000087#define MODE_IRM 0x00000020
Callum Lowcay7e08e902011-01-07 19:47:02 +000088#define MODE_DELETE_SENDS_DEL 0x00000040
89#define MODE_ALT_SENDS_ESC 0x00000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +000090
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000091union utf8_char {
92 unsigned char byte[4];
93 uint32_t ch;
94};
95
96enum utf8_state {
97 utf8state_start,
98 utf8state_accept,
99 utf8state_reject,
100 utf8state_expect3,
101 utf8state_expect2,
102 utf8state_expect1
103};
104
105struct utf8_state_machine {
106 enum utf8_state state;
107 int len;
108 union utf8_char s;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700109 uint32_t unicode;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000110};
111
112static void
113init_state_machine(struct utf8_state_machine *machine)
114{
115 machine->state = utf8state_start;
116 machine->len = 0;
117 machine->s.ch = 0;
118}
119
120static enum utf8_state
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400121utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000122{
123 switch(machine->state) {
124 case utf8state_start:
125 case utf8state_accept:
126 case utf8state_reject:
127 machine->s.ch = 0;
128 machine->len = 0;
129 if(c == 0xC0 || c == 0xC1) {
130 /* overlong encoding, reject */
131 machine->state = utf8state_reject;
132 } else if((c & 0x80) == 0) {
133 /* single byte, accept */
134 machine->s.byte[machine->len++] = c;
135 machine->state = utf8state_accept;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700136 machine->unicode = c;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000137 } else if((c & 0xC0) == 0x80) {
138 /* parser out of sync, ignore byte */
139 machine->state = utf8state_start;
140 } else if((c & 0xE0) == 0xC0) {
141 /* start of two byte sequence */
142 machine->s.byte[machine->len++] = c;
143 machine->state = utf8state_expect1;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700144 machine->unicode = c & 0x1f;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000145 } else if((c & 0xF0) == 0xE0) {
146 /* start of three byte sequence */
147 machine->s.byte[machine->len++] = c;
148 machine->state = utf8state_expect2;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700149 machine->unicode = c & 0x0f;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000150 } else if((c & 0xF8) == 0xF0) {
151 /* start of four byte sequence */
152 machine->s.byte[machine->len++] = c;
153 machine->state = utf8state_expect3;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700154 machine->unicode = c & 0x07;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000155 } else {
156 /* overlong encoding, reject */
157 machine->state = utf8state_reject;
158 }
159 break;
160 case utf8state_expect3:
161 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700162 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000163 if((c & 0xC0) == 0x80) {
164 /* all good, continue */
165 machine->state = utf8state_expect2;
166 } else {
167 /* missing extra byte, reject */
168 machine->state = utf8state_reject;
169 }
170 break;
171 case utf8state_expect2:
172 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700173 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000174 if((c & 0xC0) == 0x80) {
175 /* all good, continue */
176 machine->state = utf8state_expect1;
177 } else {
178 /* missing extra byte, reject */
179 machine->state = utf8state_reject;
180 }
181 break;
182 case utf8state_expect1:
183 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700184 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000185 if((c & 0xC0) == 0x80) {
186 /* all good, accept */
187 machine->state = utf8state_accept;
188 } else {
189 /* missing extra byte, reject */
190 machine->state = utf8state_reject;
191 }
192 break;
193 default:
194 machine->state = utf8state_reject;
195 break;
196 }
197
198 return machine->state;
199}
200
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700201static uint32_t
202get_unicode(union utf8_char utf8)
203{
204 struct utf8_state_machine machine;
205 int i;
206
207 init_state_machine(&machine);
208 for (i = 0; i < 4; i++) {
209 utf8_next_char(&machine, utf8.byte[i]);
210 if (machine.state == utf8state_accept ||
211 machine.state == utf8state_reject)
212 break;
213 }
214
215 if (machine.state == utf8state_reject)
216 return 0xfffd;
217
218 return machine.unicode;
219}
220
Callum Lowcay256e72f2011-01-07 19:47:01 +0000221struct char_sub {
222 union utf8_char match;
223 union utf8_char replace;
224};
225/* Set last char_sub match to NULL char */
226typedef struct char_sub *character_set;
227
228struct char_sub CS_US[] = {
229 {{{0, }}, {{0, }}}
230};
231static struct char_sub CS_UK[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200232 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000233 {{{0, }}, {{0, }}}
234};
235static struct char_sub CS_SPECIAL[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200236 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond: ♦ */
237 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell: ▒ */
238 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT: ␉ */
239 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF: ␌ */
240 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR: ␍ */
241 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF: ␊ */
242 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree: ° */
243 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus: ± */
244 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL: ␤ */
245 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT: ␋ */
246 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB: ┘ */
247 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT: ┐ */
248 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT: ┌ */
249 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_LB: └ */
250 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS: ┼ */
251 {{{'o', 0, }}, {{0xE2, 0x8E, 0xBA, 0}}}, /* Horiz. Scan Line 1: ⎺ */
252 {{{'p', 0, }}, {{0xE2, 0x8E, 0xBB, 0}}}, /* Horiz. Scan Line 3: ⎻ */
253 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* Horiz. Scan Line 5: ─ */
254 {{{'r', 0, }}, {{0xE2, 0x8E, 0xBC, 0}}}, /* Horiz. Scan Line 7: ⎼ */
255 {{{'s', 0, }}, {{0xE2, 0x8E, 0xBD, 0}}}, /* Horiz. Scan Line 9: ⎽ */
256 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR: ├ */
257 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL: ┤ */
258 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU: ┴ */
259 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD: ┬ */
260 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V: │ */
261 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE: ≤ */
262 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE: ≥ */
263 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI: π */
264 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ: ≠ */
265 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
266 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT: ⋅ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000267 {{{0, }}, {{0, }}}
268};
269
270static void
271apply_char_set(character_set cs, union utf8_char *utf8)
272{
273 int i = 0;
274
275 while (cs[i].match.byte[0]) {
276 if ((*utf8).ch == cs[i].match.ch) {
277 *utf8 = cs[i].replace;
278 break;
279 }
280 i++;
281 }
282}
283
Callum Lowcay7e08e902011-01-07 19:47:02 +0000284struct key_map {
285 int sym;
286 int num;
287 char escape;
288 char code;
289};
290/* Set last key_sub sym to NULL */
291typedef struct key_map *keyboard_mode;
292
293static struct key_map KM_NORMAL[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400294 { XKB_KEY_Left, 1, '[', 'D' },
295 { XKB_KEY_Right, 1, '[', 'C' },
296 { XKB_KEY_Up, 1, '[', 'A' },
297 { XKB_KEY_Down, 1, '[', 'B' },
298 { XKB_KEY_Home, 1, '[', 'H' },
299 { XKB_KEY_End, 1, '[', 'F' },
300 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000301};
302static struct key_map KM_APPLICATION[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400303 { XKB_KEY_Left, 1, 'O', 'D' },
304 { XKB_KEY_Right, 1, 'O', 'C' },
305 { XKB_KEY_Up, 1, 'O', 'A' },
306 { XKB_KEY_Down, 1, 'O', 'B' },
307 { XKB_KEY_Home, 1, 'O', 'H' },
308 { XKB_KEY_End, 1, 'O', 'F' },
309 { XKB_KEY_KP_Enter, 1, 'O', 'M' },
310 { XKB_KEY_KP_Multiply, 1, 'O', 'j' },
311 { XKB_KEY_KP_Add, 1, 'O', 'k' },
312 { XKB_KEY_KP_Separator, 1, 'O', 'l' },
313 { XKB_KEY_KP_Subtract, 1, 'O', 'm' },
314 { XKB_KEY_KP_Divide, 1, 'O', 'o' },
315 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000316};
317
318static int
319function_key_response(char escape, int num, uint32_t modifiers,
320 char code, char *response)
321{
322 int mod_num = 0;
323 int len;
324
Kristian Høgsberg70163132012-05-08 15:55:39 -0400325 if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
326 if (modifiers & MOD_ALT_MASK) mod_num |= 2;
327 if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000328
329 if (mod_num != 0)
330 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
331 num, mod_num + 1, code);
332 else if (code != '~')
333 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
334 escape, code);
335 else
336 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
337 escape, num, code);
338
339 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
340 else return len;
341}
342
343/* returns the number of bytes written into response,
344 * which must have room for MAX_RESPONSE bytes */
345static int
346apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
347{
348 struct key_map map;
349 int len = 0;
350 int i = 0;
351
352 while (mode[i].sym) {
353 map = mode[i++];
354 if (sym == map.sym) {
355 len = function_key_response(map.escape, map.num,
356 modifiers, map.code,
357 response);
358 break;
359 }
360 }
361
362 return len;
363}
364
Callum Lowcay30eeae52011-01-07 19:46:55 +0000365struct terminal_color { double r, g, b, a; };
366struct attr {
367 unsigned char fg, bg;
368 char a; /* attributes format:
369 * 76543210
Callum Lowcay81179db2011-01-10 12:14:01 +1300370 * cilub */
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500371 char s; /* in selection */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000372};
373struct color_scheme {
374 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500375 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000376 struct attr default_attr;
377};
378
379static void
380attr_init(struct attr *data_attr, struct attr attr, int n)
381{
382 int i;
383 for (i = 0; i < n; i++) {
384 data_attr[i] = attr;
385 }
386}
387
Callum Lowcay67a201d2011-01-12 19:23:41 +1300388enum escape_state {
389 escape_state_normal = 0,
390 escape_state_escape,
391 escape_state_dcs,
392 escape_state_csi,
393 escape_state_osc,
394 escape_state_inner_escape,
395 escape_state_ignore,
396 escape_state_special
397};
398
399#define ESC_FLAG_WHAT 0x01
400#define ESC_FLAG_GT 0x02
401#define ESC_FLAG_BANG 0x04
402#define ESC_FLAG_CASH 0x08
403#define ESC_FLAG_SQUOTE 0x10
404#define ESC_FLAG_DQUOTE 0x20
405#define ESC_FLAG_SPACE 0x40
406
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400407enum {
408 SELECT_NONE,
409 SELECT_CHAR,
410 SELECT_WORD,
411 SELECT_LINE
412};
413
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500414struct terminal {
415 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500416 struct widget *widget;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500417 struct display *display;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000418 union utf8_char *data;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400419 struct task io_task;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000420 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000421 struct attr *data_attr;
422 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000423 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000424 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000425 char saved_origin_mode;
426 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000427 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000428 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000429 character_set cs, g0, g1;
430 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000431 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000432 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500433 int width, height, start, row, column;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000434 int saved_row, saved_column;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600435 int send_cursor_position;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500436 int fd, master;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500437 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300438 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500439 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300440 enum escape_state state;
441 enum escape_state outer_state;
442 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000443 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500444 int margin;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400445 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000446 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400447 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +0800448 double average_width;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500449 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg88fd4082012-06-21 15:55:03 -0400450 uint32_t hide_cursor_serial;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500451
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400452 struct wl_data_source *selection;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400453 uint32_t button_time;
454 int dragging, click_count;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500455 int selection_start_x, selection_start_y;
456 int selection_end_x, selection_end_y;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400457 int selection_start_row, selection_start_col;
458 int selection_end_row, selection_end_col;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -0400459 struct wl_list link;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500460};
461
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000462/* Create default tab stops, every 8 characters */
463static void
464terminal_init_tabs(struct terminal *terminal)
465{
466 int i = 0;
467
468 while (i < terminal->width) {
469 if (i % 8 == 0)
470 terminal->tab_ruler[i] = 1;
471 else
472 terminal->tab_ruler[i] = 0;
473 i++;
474 }
475}
476
Callum Lowcay30eeae52011-01-07 19:46:55 +0000477static void
478terminal_init(struct terminal *terminal)
479{
480 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000481 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000482 terminal->mode = MODE_SHOW_CURSOR |
483 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000484 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000485 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000486
487 terminal->row = 0;
488 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000489
Callum Lowcay256e72f2011-01-07 19:47:01 +0000490 terminal->g0 = CS_US;
491 terminal->g1 = CS_US;
492 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000493 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000494
495 terminal->saved_g0 = terminal->g0;
496 terminal->saved_g1 = terminal->g1;
497 terminal->saved_cs = terminal->cs;
498
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000499 terminal->saved_attr = terminal->curr_attr;
500 terminal->saved_origin_mode = terminal->origin_mode;
501 terminal->saved_row = terminal->row;
502 terminal->saved_column = terminal->column;
503
504 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000505}
506
507static void
508init_color_table(struct terminal *terminal)
509{
510 int c, r;
511 struct terminal_color *color_table = terminal->color_table;
512
513 for (c = 0; c < 256; c ++) {
514 if (c < 16) {
515 color_table[c] = terminal->color_scheme->palette[c];
516 } else if (c < 232) {
517 r = c - 16;
518 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
519 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
520 color_table[c].r = ((double)(r % 6) / 6.0);
521 color_table[c].a = 1.0;
522 } else {
523 r = (c - 232) * 10 + 8;
524 color_table[c].r = ((double) r) / 256.0;
525 color_table[c].g = color_table[c].r;
526 color_table[c].b = color_table[c].r;
527 color_table[c].a = 1.0;
528 }
529 }
530}
531
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000532static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500533terminal_get_row(struct terminal *terminal, int row)
534{
535 int index;
536
537 index = (row + terminal->start) % terminal->height;
538
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000539 return &terminal->data[index * terminal->width];
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500540}
541
Callum Lowcay30eeae52011-01-07 19:46:55 +0000542static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500543terminal_get_attr_row(struct terminal *terminal, int row)
544{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000545 int index;
546
547 index = (row + terminal->start) % terminal->height;
548
549 return &terminal->data_attr[index * terminal->width];
550}
551
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500552union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300553 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500554 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500555};
556
557static void
558terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500559 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500560{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500561 struct attr attr;
562 int foreground, background, tmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500563
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500564 decoded->attr.s = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400565 if (((row == terminal->selection_start_row &&
566 col >= terminal->selection_start_col) ||
567 row > terminal->selection_start_row) &&
568 ((row == terminal->selection_end_row &&
569 col < terminal->selection_end_col) ||
570 row < terminal->selection_end_row))
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500571 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500572
573 /* get the attributes for this character cell */
574 attr = terminal_get_attr_row(terminal, row)[col];
575 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500576 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500577 ((terminal->mode & MODE_SHOW_CURSOR) &&
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400578 window_has_focus(terminal->window) && terminal->row == row &&
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500579 terminal->column == col)) {
580 foreground = attr.bg;
581 background = attr.fg;
582 if (attr.a & ATTRMASK_BOLD) {
583 if (foreground <= 16) foreground |= 0x08;
584 if (background <= 16) background &= 0x07;
585 }
586 } else {
587 foreground = attr.fg;
588 background = attr.bg;
589 }
590
591 if (terminal->mode & MODE_INVERSE) {
592 tmp = foreground;
593 foreground = background;
594 background = tmp;
595 if (attr.a & ATTRMASK_BOLD) {
596 if (foreground <= 16) foreground |= 0x08;
597 if (background <= 16) background &= 0x07;
598 }
599 }
600
Callum Lowcay9d708b02011-01-12 20:06:17 +1300601 decoded->attr.fg = foreground;
602 decoded->attr.bg = background;
603 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000604}
605
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500606
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500607static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000608terminal_scroll_buffer(struct terminal *terminal, int d)
609{
610 int i;
611
612 d = d % (terminal->height + 1);
613 terminal->start = (terminal->start + d) % terminal->height;
614 if (terminal->start < 0) terminal->start = terminal->height + terminal->start;
615 if(d < 0) {
616 d = 0 - d;
617 for(i = 0; i < d; i++) {
618 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
619 attr_init(terminal_get_attr_row(terminal, i),
620 terminal->curr_attr, terminal->width);
621 }
622 } else {
623 for(i = terminal->height - d; i < terminal->height; i++) {
624 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
625 attr_init(terminal_get_attr_row(terminal, i),
626 terminal->curr_attr, terminal->width);
627 }
628 }
Kristian Høgsberg18e928d2012-06-27 19:29:41 -0400629
630 terminal->selection_start_row -= d;
631 terminal->selection_end_row -= d;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000632}
633
634static void
635terminal_scroll_window(struct terminal *terminal, int d)
636{
637 int i;
638 int window_height;
639 int from_row, to_row;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000640
641 // scrolling range is inclusive
642 window_height = terminal->margin_bottom - terminal->margin_top + 1;
643 d = d % (window_height + 1);
644 if(d < 0) {
645 d = 0 - d;
646 to_row = terminal->margin_bottom;
647 from_row = terminal->margin_bottom - d;
648
649 for (i = 0; i < (window_height - d); i++) {
650 memcpy(terminal_get_row(terminal, to_row - i),
651 terminal_get_row(terminal, from_row - i),
652 terminal->data_pitch);
653 memcpy(terminal_get_attr_row(terminal, to_row - i),
654 terminal_get_attr_row(terminal, from_row - i),
655 terminal->attr_pitch);
656 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000657 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
658 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000659 attr_init(terminal_get_attr_row(terminal, i),
660 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000661 }
662 } else {
663 to_row = terminal->margin_top;
664 from_row = terminal->margin_top + d;
665
666 for (i = 0; i < (window_height - d); i++) {
667 memcpy(terminal_get_row(terminal, to_row + i),
668 terminal_get_row(terminal, from_row + i),
669 terminal->data_pitch);
670 memcpy(terminal_get_attr_row(terminal, to_row + i),
671 terminal_get_attr_row(terminal, from_row + i),
672 terminal->attr_pitch);
673 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000674 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
675 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000676 attr_init(terminal_get_attr_row(terminal, i),
677 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000678 }
679 }
680}
681
682static void
683terminal_scroll(struct terminal *terminal, int d)
684{
685 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
686 terminal_scroll_buffer(terminal, d);
687 else
688 terminal_scroll_window(terminal, d);
689}
690
691static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000692terminal_shift_line(struct terminal *terminal, int d)
693{
694 union utf8_char *row;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500695 struct attr *attr_row;
Callum Lowcay69e96582011-01-07 19:47:00 +0000696
697 row = terminal_get_row(terminal, terminal->row);
698 attr_row = terminal_get_attr_row(terminal, terminal->row);
699
700 if ((terminal->width + d) <= terminal->column)
701 d = terminal->column + 1 - terminal->width;
702 if ((terminal->column + d) >= terminal->width)
703 d = terminal->width - terminal->column - 1;
704
705 if (d < 0) {
706 d = 0 - d;
707 memmove(&row[terminal->column],
708 &row[terminal->column + d],
709 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
Callum Lowcay69e96582011-01-07 19:47:00 +0000710 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
711 (terminal->width - terminal->column - d) * sizeof(struct attr));
712 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
713 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
714 } else {
715 memmove(&row[terminal->column + d], &row[terminal->column],
716 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
717 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
718 (terminal->width - terminal->column - d) * sizeof(struct attr));
719 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
720 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
721 }
722}
723
724static void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500725terminal_resize_cells(struct terminal *terminal, int width, int height)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500726{
727 size_t size;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000728 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000729 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000730 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000731 int data_pitch, attr_pitch;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500732 int i, l, total_rows;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500733 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000734 struct winsize ws;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500735
736 if (terminal->width == width && terminal->height == height)
737 return;
738
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000739 data_pitch = width * sizeof(union utf8_char);
740 size = data_pitch * height;
Peter Huttererf3d62272013-08-08 11:57:05 +1000741 data = zalloc(size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000742 attr_pitch = width * sizeof(struct attr);
743 data_attr = malloc(attr_pitch * height);
Peter Huttererf3d62272013-08-08 11:57:05 +1000744 tab_ruler = zalloc(width);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000745 attr_init(data_attr, terminal->curr_attr, width * height);
746 if (terminal->data && terminal->data_attr) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500747 if (width > terminal->width)
748 l = terminal->width;
749 else
750 l = width;
751
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500752 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500753 total_rows = height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500754 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500755 total_rows = terminal->height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500756 }
757
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000758 for (i = 0; i < total_rows; i++) {
759 memcpy(&data[width * i],
760 terminal_get_row(terminal, i),
761 l * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000762 memcpy(&data_attr[width * i],
763 terminal_get_attr_row(terminal, i),
764 l * sizeof(struct attr));
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000765 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500766
767 free(terminal->data);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000768 free(terminal->data_attr);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000769 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500770 }
771
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000772 terminal->data_pitch = data_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000773 terminal->attr_pitch = attr_pitch;
Callum Lowcay86653ed2011-01-07 19:47:03 +0000774 terminal->margin_bottom =
775 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500776 terminal->width = width;
777 terminal->height = height;
778 terminal->data = data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000779 terminal->data_attr = data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000780 terminal->tab_ruler = tab_ruler;
781 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500782
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000783 /* Update the window size */
784 ws.ws_row = terminal->height;
785 ws.ws_col = terminal->width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500786 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500787 ws.ws_xpixel = allocation.width;
788 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000789 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500790}
791
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500792static void
793resize_handler(struct widget *widget,
794 int32_t width, int32_t height, void *data)
795{
796 struct terminal *terminal = data;
797 int32_t columns, rows, m;
798
799 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800800 columns = (width - m) / (int32_t) terminal->average_width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500801 rows = (height - m) / (int32_t) terminal->extents.height;
802
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400803 if (!window_is_fullscreen(terminal->window) &&
804 !window_is_maximized(terminal->window)) {
Peng Wuf291f202013-06-06 15:32:41 +0800805 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500806 height = rows * terminal->extents.height + m;
807 widget_set_size(terminal->widget, width, height);
808 }
809
810 terminal_resize_cells(terminal, columns, rows);
811}
812
813static void
814terminal_resize(struct terminal *terminal, int columns, int rows)
815{
816 int32_t width, height, m;
817
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400818 if (window_is_fullscreen(terminal->window) ||
819 window_is_maximized(terminal->window))
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500820 return;
821
822 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800823 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500824 height = rows * terminal->extents.height + m;
Kristian Høgsberga1627922012-06-20 17:30:03 -0400825
826 frame_set_child_size(terminal->widget, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500827}
828
Callum Lowcay30eeae52011-01-07 19:46:55 +0000829struct color_scheme DEFAULT_COLORS = {
830 {
831 {0, 0, 0, 1}, /* black */
832 {0.66, 0, 0, 1}, /* red */
833 {0 , 0.66, 0, 1}, /* green */
834 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
835 {0 , 0 , 0.66, 1}, /* blue */
836 {0.66, 0 , 0.66, 1}, /* magenta */
837 {0, 0.66, 0.66, 1}, /* cyan */
838 {0.66, 0.66, 0.66, 1}, /* light grey */
839 {0.22, 0.33, 0.33, 1}, /* dark grey */
840 {1, 0.33, 0.33, 1}, /* high red */
841 {0.33, 1, 0.33, 1}, /* high green */
842 {1, 1, 0.33, 1}, /* high yellow */
843 {0.33, 0.33, 1, 1}, /* high blue */
844 {1, 0.33, 1, 1}, /* high magenta */
845 {0.33, 1, 1, 1}, /* high cyan */
846 {1, 1, 1, 1} /* white */
847 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500848 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000849 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
850};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400851
Kristian Høgsberg22106762008-12-08 13:50:07 -0500852static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500853terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
854{
855 cairo_set_source_rgba(cr,
856 terminal->color_table[index].r,
857 terminal->color_table[index].g,
858 terminal->color_table[index].b,
859 terminal->color_table[index].a);
860}
861
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500862static void
863terminal_send_selection(struct terminal *terminal, int fd)
864{
865 int row, col;
866 union utf8_char *p_row;
867 union decoded_attr attr;
868 FILE *fp;
869 int len;
870
871 fp = fdopen(fd, "w");
Brian Lovin1bf14812013-08-08 16:12:55 -0700872 if (fp == NULL){
873 close(fd);
874 return;
875 }
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500876 for (row = 0; row < terminal->height; row++) {
877 p_row = terminal_get_row(terminal, row);
878 for (col = 0; col < terminal->width; col++) {
879 /* get the attributes for this character cell */
880 terminal_decode_attr(terminal, row, col, &attr);
881 if (!attr.attr.s)
882 continue;
883 len = strnlen((char *) p_row[col].byte, 4);
Kristian Høgsberg0dee6472012-07-01 21:25:41 -0400884 if (len > 0)
885 fwrite(p_row[col].byte, 1, len, fp);
886 if (len == 0 || col == terminal->width - 1) {
887 fwrite("\n", 1, 1, fp);
888 break;
889 }
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500890 }
891 }
892 fclose(fp);
893}
894
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500895struct glyph_run {
896 struct terminal *terminal;
897 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400898 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500899 union decoded_attr attr;
900 cairo_glyph_t glyphs[256], *g;
901};
902
903static void
904glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
905{
906 run->terminal = terminal;
907 run->cr = cr;
908 run->g = run->glyphs;
909 run->count = 0;
910 run->attr.key = 0;
911}
912
913static void
914glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
915{
916 cairo_scaled_font_t *font;
917
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500918 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
919 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300920 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500921 font = run->terminal->font_bold;
922 else
923 font = run->terminal->font_normal;
924 cairo_set_scaled_font(run->cr, font);
925 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300926 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500927
Callum Lowcay9d708b02011-01-12 20:06:17 +1300928 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
929 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500930 run->g = run->glyphs;
931 run->count = 0;
932 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300933 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500934}
935
936static void
937glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
938{
939 int num_glyphs;
940 cairo_scaled_font_t *font;
941
942 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
943
Callum Lowcay9d708b02011-01-12 20:06:17 +1300944 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500945 font = run->terminal->font_bold;
946 else
947 font = run->terminal->font_normal;
948
949 cairo_move_to(run->cr, x, y);
950 cairo_scaled_font_text_to_glyphs (font, x, y,
951 (char *) c->byte, 4,
952 &run->g, &num_glyphs,
953 NULL, NULL, NULL);
954 run->g += num_glyphs;
955 run->count += num_glyphs;
956}
957
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500958
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500959static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500960redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500961{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500962 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500963 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500964 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000965 int top_margin, side_margin;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600966 int row, col, cursor_x, cursor_y;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000967 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500968 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000969 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500970 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500971 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500972 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500973 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +0800974 double average_width;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500975
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500976 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500977 widget_get_allocation(terminal->widget, &allocation);
Alexander Larssonde79dd02013-05-22 14:41:34 +0200978 cr = widget_cairo_create(terminal->widget);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500979 cairo_rectangle(cr, allocation.x, allocation.y,
980 allocation.width, allocation.height);
981 cairo_clip(cr);
982 cairo_push_group(cr);
983
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500984 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500985 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500986 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500987
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500988 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500989
Kristian Høgsberg59826582011-01-20 11:56:57 -0500990 extents = terminal->extents;
Peng Wuf291f202013-06-06 15:32:41 +0800991 average_width = terminal->average_width;
992 side_margin = (allocation.width - terminal->width * average_width) / 2;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500993 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500994
Callum Lowcay30eeae52011-01-07 19:46:55 +0000995 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500996 cairo_translate(cr, allocation.x + side_margin,
997 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500998 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000999 for (row = 0; row < terminal->height; row++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001000 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001001 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -05001002 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001003
Callum Lowcay9d708b02011-01-12 20:06:17 +13001004 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -05001005 continue;
1006
Callum Lowcay9d708b02011-01-12 20:06:17 +13001007 terminal_set_color(terminal, cr, attr.attr.bg);
Peng Wuf291f202013-06-06 15:32:41 +08001008 cairo_move_to(cr, col * average_width,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001009 row * extents.height);
Peng Wuf291f202013-06-06 15:32:41 +08001010 cairo_rel_line_to(cr, average_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001011 cairo_rel_line_to(cr, 0, extents.height);
Peng Wuf291f202013-06-06 15:32:41 +08001012 cairo_rel_line_to(cr, -average_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001013 cairo_close_path(cr);
1014 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001015 }
1016 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001017
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001018 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1019
1020 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001021 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001022 for (row = 0; row < terminal->height; row++) {
1023 p_row = terminal_get_row(terminal, row);
1024 for (col = 0; col < terminal->width; col++) {
1025 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -05001026 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001027
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001028 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001029
Peng Wuf291f202013-06-06 15:32:41 +08001030 text_x = col * average_width;
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001031 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +13001032 if (attr.attr.a & ATTRMASK_UNDERLINE) {
1033 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +00001034 cairo_move_to(cr, text_x, (double)text_y + 1.5);
Peng Wuf291f202013-06-06 15:32:41 +08001035 cairo_line_to(cr, text_x + average_width, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001036 cairo_stroke(cr);
1037 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -05001038
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001039 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001040 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001041 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001042
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001043 attr.key = ~0;
1044 glyph_run_flush(&run, attr);
1045
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001046 if ((terminal->mode & MODE_SHOW_CURSOR) &&
1047 !window_has_focus(terminal->window)) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001048 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001049
Callum Lowcay30eeae52011-01-07 19:46:55 +00001050 cairo_set_line_width(cr, 1);
Peng Wuf291f202013-06-06 15:32:41 +08001051 cairo_move_to(cr, terminal->column * average_width + d,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001052 terminal->row * extents.height + d);
Peng Wuf291f202013-06-06 15:32:41 +08001053 cairo_rel_line_to(cr, average_width - 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001054 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
Peng Wuf291f202013-06-06 15:32:41 +08001055 cairo_rel_line_to(cr, -average_width + 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001056 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001057
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001058 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001059 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001060
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001061 cairo_pop_group_to_source(cr);
1062 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001063 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001064 cairo_surface_destroy(surface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001065
1066 if (terminal->send_cursor_position) {
1067 cursor_x = side_margin + allocation.x +
Peng Wuf291f202013-06-06 15:32:41 +08001068 terminal->column * average_width;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001069 cursor_y = top_margin + allocation.y +
1070 terminal->row * extents.height;
1071 window_set_text_cursor_position(terminal->window,
1072 cursor_x, cursor_y);
1073 terminal->send_cursor_position = 0;
1074 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001075}
1076
1077static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001078terminal_write(struct terminal *terminal, const char *data, size_t length)
1079{
1080 if (write(terminal->master, data, length) < 0)
1081 abort();
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001082 terminal->send_cursor_position = 1;
Kristian Høgsberg26130862011-08-24 11:30:21 -04001083}
1084
1085static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001086terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001087
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001088static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001089handle_char(struct terminal *terminal, union utf8_char utf8);
1090
1091static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001092handle_sgr(struct terminal *terminal, int code);
1093
1094static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001095handle_term_parameter(struct terminal *terminal, int code, int sr)
1096{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001097 int i;
1098
Callum Lowcay67a201d2011-01-12 19:23:41 +13001099 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001100 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001101 case 1: /* DECCKM */
1102 if (sr) terminal->key_mode = KM_APPLICATION;
1103 else terminal->key_mode = KM_NORMAL;
1104 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001105 case 2: /* DECANM */
1106 /* No VT52 support yet */
1107 terminal->g0 = CS_US;
1108 terminal->g1 = CS_US;
1109 terminal->cs = terminal->g0;
1110 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001111 case 3: /* DECCOLM */
1112 if (sr)
1113 terminal_resize(terminal, 132, 24);
1114 else
1115 terminal_resize(terminal, 80, 24);
1116
1117 /* set columns, but also home cursor and clear screen */
1118 terminal->row = 0; terminal->column = 0;
1119 for (i = 0; i < terminal->height; i++) {
1120 memset(terminal_get_row(terminal, i),
1121 0, terminal->data_pitch);
1122 attr_init(terminal_get_attr_row(terminal, i),
1123 terminal->curr_attr, terminal->width);
1124 }
1125 break;
1126 case 5: /* DECSCNM */
1127 if (sr) terminal->mode |= MODE_INVERSE;
1128 else terminal->mode &= ~MODE_INVERSE;
1129 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001130 case 6: /* DECOM */
1131 terminal->origin_mode = sr;
1132 if (terminal->origin_mode)
1133 terminal->row = terminal->margin_top;
1134 else
1135 terminal->row = 0;
1136 terminal->column = 0;
1137 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001138 case 7: /* DECAWM */
1139 if (sr) terminal->mode |= MODE_AUTOWRAP;
1140 else terminal->mode &= ~MODE_AUTOWRAP;
1141 break;
1142 case 8: /* DECARM */
1143 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1144 else terminal->mode &= ~MODE_AUTOREPEAT;
1145 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001146 case 12: /* Very visible cursor (CVVIS) */
1147 /* FIXME: What do we do here. */
1148 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001149 case 25:
1150 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1151 else terminal->mode &= ~MODE_SHOW_CURSOR;
1152 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001153 case 1034: /* smm/rmm, meta mode on/off */
1154 /* ignore */
1155 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001156 case 1037: /* deleteSendsDel */
1157 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1158 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1159 break;
1160 case 1039: /* altSendsEscape */
1161 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1162 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1163 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001164 case 1049: /* rmcup/smcup, alternate screen */
1165 /* Ignore. Should be possible to implement,
1166 * but it's kind of annoying. */
1167 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001168 default:
1169 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1170 break;
1171 }
1172 } else {
1173 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001174 case 4: /* IRM */
1175 if (sr) terminal->mode |= MODE_IRM;
1176 else terminal->mode &= ~MODE_IRM;
1177 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001178 case 20: /* LNM */
1179 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1180 else terminal->mode &= ~MODE_LF_NEWLINE;
1181 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001182 default:
1183 fprintf(stderr, "Unknown parameter: %d\n", code);
1184 break;
1185 }
1186 }
1187}
1188
1189static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001190handle_dcs(struct terminal *terminal)
1191{
1192}
1193
1194static void
1195handle_osc(struct terminal *terminal)
1196{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001197 char *p;
1198 int code;
1199
1200 terminal->escape[terminal->escape_length++] = '\0';
1201 p = &terminal->escape[2];
1202 code = strtol(p, &p, 10);
1203 if (*p == ';') p++;
1204
1205 switch (code) {
1206 case 0: /* Icon name and window title */
1207 case 1: /* Icon label */
1208 case 2: /* Window title*/
1209 window_set_title(terminal->window, p);
1210 break;
1211 default:
1212 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1213 break;
1214 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001215}
1216
1217static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001218handle_escape(struct terminal *terminal)
1219{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001220 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001221 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001222 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001223 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001224 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001225 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001226 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001227
1228 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001229 i = 0;
1230 p = &terminal->escape[2];
1231 while ((isdigit(*p) || *p == ';') && i < 10) {
1232 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001233 if (!set[i]) {
1234 args[i] = 0;
1235 set[i] = 1;
1236 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001237 p++;
1238 i++;
1239 } else {
1240 args[i] = strtol(p, &p, 10);
1241 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001242 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001243 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001244
1245 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001246 case '@': /* ICH */
1247 count = set[0] ? args[0] : 1;
1248 if (count == 0) count = 1;
1249 terminal_shift_line(terminal, count);
1250 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001251 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001252 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001253 if (count == 0) count = 1;
1254 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001255 terminal->row -= count;
1256 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001257 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001258 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001259 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001260 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001261 if (count == 0) count = 1;
1262 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001263 terminal->row += count;
1264 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001265 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001266 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001267 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001268 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001269 if (count == 0) count = 1;
1270 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001271 terminal->column += count;
1272 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001273 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001274 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001275 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001276 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001277 if (count == 0) count = 1;
1278 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001279 terminal->column -= count;
1280 else
1281 terminal->column = 0;
1282 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001283 case 'E': /* CNL */
1284 count = set[0] ? args[0] : 1;
1285 if (terminal->row + count <= terminal->margin_bottom)
1286 terminal->row += count;
1287 else
1288 terminal->row = terminal->margin_bottom;
1289 terminal->column = 0;
1290 break;
1291 case 'F': /* CPL */
1292 count = set[0] ? args[0] : 1;
1293 if (terminal->row - count >= terminal->margin_top)
1294 terminal->row -= count;
1295 else
1296 terminal->row = terminal->margin_top;
1297 terminal->column = 0;
1298 break;
1299 case 'G': /* CHA */
1300 y = set[0] ? args[0] : 1;
1301 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1302
1303 terminal->column = y - 1;
1304 break;
1305 case 'f': /* HVP */
1306 case 'H': /* CUP */
1307 x = (set[1] ? args[1] : 1) - 1;
1308 x = x < 0 ? 0 :
1309 (x >= terminal->width ? terminal->width - 1 : x);
1310
1311 y = (set[0] ? args[0] : 1) - 1;
1312 if (terminal->origin_mode) {
1313 y += terminal->margin_top;
1314 y = y < terminal->margin_top ? terminal->margin_top :
1315 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1316 } else {
1317 y = y < 0 ? 0 :
1318 (y >= terminal->height ? terminal->height - 1 : y);
1319 }
1320
1321 terminal->row = y;
1322 terminal->column = x;
1323 break;
1324 case 'I': /* CHT */
1325 count = set[0] ? args[0] : 1;
1326 if (count == 0) count = 1;
1327 while (count > 0 && terminal->column < terminal->width) {
1328 if (terminal->tab_ruler[terminal->column]) count--;
1329 terminal->column++;
1330 }
1331 terminal->column--;
1332 break;
1333 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001334 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001335 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001336 if (!set[0] || args[0] == 0 || args[0] > 2) {
1337 memset(&row[terminal->column],
1338 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1339 attr_init(&attr_row[terminal->column],
1340 terminal->curr_attr, terminal->width - terminal->column);
1341 for (i = terminal->row + 1; i < terminal->height; i++) {
1342 memset(terminal_get_row(terminal, i),
1343 0, terminal->data_pitch);
1344 attr_init(terminal_get_attr_row(terminal, i),
1345 terminal->curr_attr, terminal->width);
1346 }
1347 } else if (args[0] == 1) {
1348 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1349 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1350 for (i = 0; i < terminal->row; i++) {
1351 memset(terminal_get_row(terminal, i),
1352 0, terminal->data_pitch);
1353 attr_init(terminal_get_attr_row(terminal, i),
1354 terminal->curr_attr, terminal->width);
1355 }
1356 } else if (args[0] == 2) {
1357 for (i = 0; i < terminal->height; i++) {
1358 memset(terminal_get_row(terminal, i),
1359 0, terminal->data_pitch);
1360 attr_init(terminal_get_attr_row(terminal, i),
1361 terminal->curr_attr, terminal->width);
1362 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001363 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001364 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001365 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001366 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001367 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001368 if (!set[0] || args[0] == 0 || args[0] > 2) {
1369 memset(&row[terminal->column], 0,
1370 (terminal->width - terminal->column) * sizeof(union utf8_char));
1371 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1372 terminal->width - terminal->column);
1373 } else if (args[0] == 1) {
1374 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1375 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1376 } else if (args[0] == 2) {
1377 memset(row, 0, terminal->data_pitch);
1378 attr_init(attr_row, terminal->curr_attr, terminal->width);
1379 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001380 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001381 case 'L': /* IL */
1382 count = set[0] ? args[0] : 1;
1383 if (count == 0) count = 1;
1384 if (terminal->row >= terminal->margin_top &&
1385 terminal->row < terminal->margin_bottom)
1386 {
1387 top = terminal->margin_top;
1388 terminal->margin_top = terminal->row;
1389 terminal_scroll(terminal, 0 - count);
1390 terminal->margin_top = top;
1391 } else if (terminal->row == terminal->margin_bottom) {
1392 memset(terminal_get_row(terminal, terminal->row),
1393 0, terminal->data_pitch);
1394 attr_init(terminal_get_attr_row(terminal, terminal->row),
1395 terminal->curr_attr, terminal->width);
1396 }
1397 break;
1398 case 'M': /* DL */
1399 count = set[0] ? args[0] : 1;
1400 if (count == 0) count = 1;
1401 if (terminal->row >= terminal->margin_top &&
1402 terminal->row < terminal->margin_bottom)
1403 {
1404 top = terminal->margin_top;
1405 terminal->margin_top = terminal->row;
1406 terminal_scroll(terminal, count);
1407 terminal->margin_top = top;
1408 } else if (terminal->row == terminal->margin_bottom) {
1409 memset(terminal_get_row(terminal, terminal->row),
1410 0, terminal->data_pitch);
1411 }
1412 break;
1413 case 'P': /* DCH */
1414 count = set[0] ? args[0] : 1;
1415 if (count == 0) count = 1;
1416 terminal_shift_line(terminal, 0 - count);
1417 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001418 case 'S': /* SU */
1419 terminal_scroll(terminal, set[0] ? args[0] : 1);
1420 break;
1421 case 'T': /* SD */
1422 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1423 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001424 case 'X': /* ECH */
1425 count = set[0] ? args[0] : 1;
1426 if (count == 0) count = 1;
1427 if ((terminal->column + count) > terminal->width)
1428 count = terminal->width - terminal->column;
1429 row = terminal_get_row(terminal, terminal->row);
1430 attr_row = terminal_get_attr_row(terminal, terminal->row);
1431 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1432 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1433 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001434 case 'Z': /* CBT */
1435 count = set[0] ? args[0] : 1;
1436 if (count == 0) count = 1;
1437 while (count > 0 && terminal->column >= 0) {
1438 if (terminal->tab_ruler[terminal->column]) count--;
1439 terminal->column--;
1440 }
1441 terminal->column++;
1442 break;
1443 case '`': /* HPA */
1444 y = set[0] ? args[0] : 1;
1445 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1446
1447 terminal->column = y - 1;
1448 break;
1449 case 'b': /* REP */
1450 count = set[0] ? args[0] : 1;
1451 if (count == 0) count = 1;
1452 if (terminal->last_char.byte[0])
1453 for (i = 0; i < count; i++)
1454 handle_char(terminal, terminal->last_char);
1455 terminal->last_char.byte[0] = 0;
1456 break;
1457 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001458 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001459 break;
1460 case 'd': /* VPA */
1461 x = set[0] ? args[0] : 1;
1462 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1463
1464 terminal->row = x - 1;
1465 break;
1466 case 'g': /* TBC */
1467 if (!set[0] || args[0] == 0) {
1468 terminal->tab_ruler[terminal->column] = 0;
1469 } else if (args[0] == 3) {
1470 memset(terminal->tab_ruler, 0, terminal->width);
1471 }
1472 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001473 case 'h': /* SM */
1474 for(i = 0; i < 10 && set[i]; i++) {
1475 handle_term_parameter(terminal, args[i], 1);
1476 }
1477 break;
1478 case 'l': /* RM */
1479 for(i = 0; i < 10 && set[i]; i++) {
1480 handle_term_parameter(terminal, args[i], 0);
1481 }
1482 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001483 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001484 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001485 if (i <= 7 && set[i] && set[i + 1] &&
1486 set[i + 2] && args[i + 1] == 5)
1487 {
1488 if (args[i] == 38) {
1489 handle_sgr(terminal, args[i + 2] + 256);
1490 break;
1491 } else if (args[i] == 48) {
1492 handle_sgr(terminal, args[i + 2] + 512);
1493 break;
1494 }
1495 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001496 if(set[i]) {
1497 handle_sgr(terminal, args[i]);
1498 } else if(i == 0) {
1499 handle_sgr(terminal, 0);
1500 break;
1501 } else {
1502 break;
1503 }
1504 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001505 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001506 case 'n': /* DSR */
1507 i = set[0] ? args[0] : 0;
1508 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001509 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001510 } else if (i == 6) {
1511 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1512 terminal->origin_mode ?
1513 terminal->row+terminal->margin_top : terminal->row+1,
1514 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001515 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001516 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001517 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001518 case 'r':
1519 if(!set[0]) {
1520 terminal->margin_top = 0;
1521 terminal->margin_bottom = terminal->height-1;
1522 terminal->row = 0;
1523 terminal->column = 0;
1524 } else {
1525 top = (set[0] ? args[0] : 1) - 1;
1526 top = top < 0 ? 0 :
1527 (top >= terminal->height ? terminal->height - 1 : top);
1528 bottom = (set[1] ? args[1] : 1) - 1;
1529 bottom = bottom < 0 ? 0 :
1530 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1531 if(bottom > top) {
1532 terminal->margin_top = top;
1533 terminal->margin_bottom = bottom;
1534 } else {
1535 terminal->margin_top = 0;
1536 terminal->margin_bottom = terminal->height-1;
1537 }
1538 if(terminal->origin_mode)
1539 terminal->row = terminal->margin_top;
1540 else
1541 terminal->row = 0;
1542 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001543 }
1544 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001545 case 's':
1546 terminal->saved_row = terminal->row;
1547 terminal->saved_column = terminal->column;
1548 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001549 case 't': /* windowOps */
1550 if (!set[0]) break;
1551 switch (args[0]) {
1552 case 4: /* resize px */
1553 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001554 widget_schedule_resize(terminal->widget,
1555 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001556 }
1557 break;
1558 case 8: /* resize ch */
1559 if (set[1] && set[2]) {
1560 terminal_resize(terminal, args[2], args[1]);
1561 }
1562 break;
1563 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001564 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001565 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1566 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001567 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001568 break;
1569 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001570 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001571 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1572 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001573 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001574 break;
1575 case 18: /* report ch */
1576 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1577 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001578 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001579 break;
1580 case 21: /* report title */
1581 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1582 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001583 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001584 break;
1585 default:
1586 if (args[0] >= 24)
1587 terminal_resize(terminal, terminal->width, args[0]);
1588 else
1589 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1590 break;
1591 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001592 case 'u':
1593 terminal->row = terminal->saved_row;
1594 terminal->column = terminal->saved_column;
1595 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001596 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001597 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001598 break;
1599 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001600}
1601
1602static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001603handle_non_csi_escape(struct terminal *terminal, char code)
1604{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001605 switch(code) {
1606 case 'M': /* RI */
1607 terminal->row -= 1;
1608 if(terminal->row < terminal->margin_top) {
1609 terminal->row = terminal->margin_top;
1610 terminal_scroll(terminal, -1);
1611 }
1612 break;
1613 case 'E': /* NEL */
1614 terminal->column = 0;
1615 // fallthrough
1616 case 'D': /* IND */
1617 terminal->row += 1;
1618 if(terminal->row > terminal->margin_bottom) {
1619 terminal->row = terminal->margin_bottom;
1620 terminal_scroll(terminal, +1);
1621 }
1622 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001623 case 'c': /* RIS */
1624 terminal_init(terminal);
1625 break;
1626 case 'H': /* HTS */
1627 terminal->tab_ruler[terminal->column] = 1;
1628 break;
1629 case '7': /* DECSC */
1630 terminal->saved_row = terminal->row;
1631 terminal->saved_column = terminal->column;
1632 terminal->saved_attr = terminal->curr_attr;
1633 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001634 terminal->saved_cs = terminal->cs;
1635 terminal->saved_g0 = terminal->g0;
1636 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001637 break;
1638 case '8': /* DECRC */
1639 terminal->row = terminal->saved_row;
1640 terminal->column = terminal->saved_column;
1641 terminal->curr_attr = terminal->saved_attr;
1642 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001643 terminal->cs = terminal->saved_cs;
1644 terminal->g0 = terminal->saved_g0;
1645 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001646 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001647 case '=': /* DECPAM */
1648 terminal->key_mode = KM_APPLICATION;
1649 break;
1650 case '>': /* DECPNM */
1651 terminal->key_mode = KM_NORMAL;
1652 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001653 default:
1654 fprintf(stderr, "Unknown escape code: %c\n", code);
1655 break;
1656 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001657}
1658
1659static void
1660handle_special_escape(struct terminal *terminal, char special, char code)
1661{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001662 int i, numChars;
1663
1664 if (special == '#') {
1665 switch(code) {
1666 case '8':
1667 /* fill with 'E', no cheap way to do this */
1668 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1669 numChars = terminal->width * terminal->height;
1670 for(i = 0; i < numChars; i++) {
1671 terminal->data[i].byte[0] = 'E';
1672 }
1673 break;
1674 default:
1675 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1676 break;
1677 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001678 } else if (special == '(' || special == ')') {
1679 switch(code) {
1680 case '0':
1681 if (special == '(')
1682 terminal->g0 = CS_SPECIAL;
1683 else
1684 terminal->g1 = CS_SPECIAL;
1685 break;
1686 case 'A':
1687 if (special == '(')
1688 terminal->g0 = CS_UK;
1689 else
1690 terminal->g1 = CS_UK;
1691 break;
1692 case 'B':
1693 if (special == '(')
1694 terminal->g0 = CS_US;
1695 else
1696 terminal->g1 = CS_US;
1697 break;
1698 default:
1699 fprintf(stderr, "Unknown character set %c\n", code);
1700 break;
1701 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001702 } else {
1703 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1704 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001705}
1706
1707static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001708handle_sgr(struct terminal *terminal, int code)
1709{
1710 switch(code) {
1711 case 0:
1712 terminal->curr_attr = terminal->color_scheme->default_attr;
1713 break;
1714 case 1:
1715 terminal->curr_attr.a |= ATTRMASK_BOLD;
1716 if (terminal->curr_attr.fg < 8)
1717 terminal->curr_attr.fg += 8;
1718 break;
1719 case 4:
1720 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1721 break;
1722 case 5:
1723 terminal->curr_attr.a |= ATTRMASK_BLINK;
1724 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001725 case 8:
1726 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1727 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001728 case 2:
1729 case 21:
1730 case 22:
1731 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1732 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1733 terminal->curr_attr.fg -= 8;
1734 break;
1735 case 24:
1736 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1737 break;
1738 case 25:
1739 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1740 break;
1741 case 7:
1742 case 26:
1743 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1744 break;
1745 case 27:
1746 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1747 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001748 case 28:
1749 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1750 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001751 case 39:
1752 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1753 break;
1754 case 49:
1755 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1756 break;
1757 default:
1758 if(code >= 30 && code <= 37) {
1759 terminal->curr_attr.fg = code - 30;
1760 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1761 terminal->curr_attr.fg += 8;
1762 } else if(code >= 40 && code <= 47) {
1763 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001764 } else if (code >= 90 && code <= 97) {
1765 terminal->curr_attr.fg = code - 90 + 8;
1766 } else if (code >= 100 && code <= 107) {
1767 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001768 } else if(code >= 256 && code < 512) {
1769 terminal->curr_attr.fg = code - 256;
1770 } else if(code >= 512 && code < 768) {
1771 terminal->curr_attr.bg = code - 512;
1772 } else {
1773 fprintf(stderr, "Unknown SGR code: %d\n", code);
1774 }
1775 break;
1776 }
1777}
1778
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001779/* Returns 1 if c was special, otherwise 0 */
1780static int
1781handle_special_char(struct terminal *terminal, char c)
1782{
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001783 union utf8_char *row;
1784 struct attr *attr_row;
1785
1786 row = terminal_get_row(terminal, terminal->row);
1787 attr_row = terminal_get_attr_row(terminal, terminal->row);
1788
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001789 switch(c) {
1790 case '\r':
1791 terminal->column = 0;
1792 break;
1793 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001794 if (terminal->mode & MODE_LF_NEWLINE) {
1795 terminal->column = 0;
1796 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001797 /* fallthrough */
1798 case '\v':
1799 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001800 terminal->row++;
1801 if(terminal->row > terminal->margin_bottom) {
1802 terminal->row = terminal->margin_bottom;
1803 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001804 }
1805
1806 break;
1807 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001808 while (terminal->column < terminal->width) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001809 if (terminal->mode & MODE_IRM)
1810 terminal_shift_line(terminal, +1);
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001811
1812 if (row[terminal->column].byte[0] == '\0') {
1813 row[terminal->column].byte[0] = ' ';
1814 row[terminal->column].byte[1] = '\0';
1815 attr_row[terminal->column] = terminal->curr_attr;
1816 }
1817
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001818 terminal->column++;
Kristian Høgsbergcca3c2f2012-06-20 15:56:13 -04001819 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001820 }
1821 if (terminal->column >= terminal->width) {
1822 terminal->column = terminal->width - 1;
1823 }
1824
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001825 break;
1826 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001827 if (terminal->column >= terminal->width) {
1828 terminal->column = terminal->width - 2;
1829 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001830 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001831 } else if (terminal->mode & MODE_AUTOWRAP) {
1832 terminal->column = terminal->width - 1;
1833 terminal->row -= 1;
1834 if (terminal->row < terminal->margin_top) {
1835 terminal->row = terminal->margin_top;
1836 terminal_scroll(terminal, -1);
1837 }
1838 }
1839
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001840 break;
1841 case '\a':
1842 /* Bell */
1843 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001844 case '\x0E': /* SO */
1845 terminal->cs = terminal->g1;
1846 break;
1847 case '\x0F': /* SI */
1848 terminal->cs = terminal->g0;
1849 break;
Kristian Høgsberg2a1aa4e2012-08-03 09:37:05 -04001850 case '\0':
1851 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001852 default:
1853 return 0;
1854 }
1855
1856 return 1;
1857}
1858
1859static void
1860handle_char(struct terminal *terminal, union utf8_char utf8)
1861{
1862 union utf8_char *row;
1863 struct attr *attr_row;
1864
1865 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001866
1867 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001868
1869 /* There are a whole lot of non-characters, control codes,
1870 * and formatting codes that should probably be ignored,
1871 * for example: */
1872 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1873 /* BOM, ignore */
1874 return;
1875 }
1876
1877 /* Some of these non-characters should be translated, e.g.: */
1878 if (utf8.byte[0] < 32) {
1879 utf8.byte[0] = utf8.byte[0] + 64;
1880 }
1881
1882 /* handle right margin effects */
1883 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001884 if (terminal->mode & MODE_AUTOWRAP) {
1885 terminal->column = 0;
1886 terminal->row += 1;
1887 if (terminal->row > terminal->margin_bottom) {
1888 terminal->row = terminal->margin_bottom;
1889 terminal_scroll(terminal, +1);
1890 }
1891 } else {
1892 terminal->column--;
1893 }
1894 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001895
1896 row = terminal_get_row(terminal, terminal->row);
1897 attr_row = terminal_get_attr_row(terminal, terminal->row);
1898
Callum Lowcay69e96582011-01-07 19:47:00 +00001899 if (terminal->mode & MODE_IRM)
1900 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001901 row[terminal->column] = utf8;
1902 attr_row[terminal->column++] = terminal->curr_attr;
1903
1904 if (utf8.ch != terminal->last_char.ch)
1905 terminal->last_char = utf8;
1906}
1907
Callum Lowcay30eeae52011-01-07 19:46:55 +00001908static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001909escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1910{
1911 int len, i;
1912
1913 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1914 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1915 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1916 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1917 else len = 1; /* Invalid, cannot happen */
1918
1919 if (terminal->escape_length + len <= MAX_ESCAPE) {
1920 for (i = 0; i < len; i++)
1921 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1922 terminal->escape_length += len;
1923 } else if (terminal->escape_length < MAX_ESCAPE) {
1924 terminal->escape[terminal->escape_length++] = 0;
1925 }
1926}
1927
1928static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001929terminal_data(struct terminal *terminal, const char *data, size_t length)
1930{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001931 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001932 union utf8_char utf8;
1933 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001934
1935 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001936 parser_state =
1937 utf8_next_char(&terminal->state_machine, data[i]);
1938 switch(parser_state) {
1939 case utf8state_accept:
1940 utf8.ch = terminal->state_machine.s.ch;
1941 break;
1942 case utf8state_reject:
1943 /* the unicode replacement character */
1944 utf8.byte[0] = 0xEF;
1945 utf8.byte[1] = 0xBF;
1946 utf8.byte[2] = 0xBD;
1947 utf8.byte[3] = 0x00;
1948 break;
1949 default:
1950 continue;
1951 }
1952
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001953 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001954 switch (terminal->state) {
1955 case escape_state_escape:
1956 escape_append_utf8(terminal, utf8);
1957 switch (utf8.byte[0]) {
1958 case 'P': /* DCS */
1959 terminal->state = escape_state_dcs;
1960 break;
1961 case '[': /* CSI */
1962 terminal->state = escape_state_csi;
1963 break;
1964 case ']': /* OSC */
1965 terminal->state = escape_state_osc;
1966 break;
1967 case '#':
1968 case '(':
1969 case ')': /* special */
1970 terminal->state = escape_state_special;
1971 break;
1972 case '^': /* PM (not implemented) */
1973 case '_': /* APC (not implemented) */
1974 terminal->state = escape_state_ignore;
1975 break;
1976 default:
1977 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001978 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13001979 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001980 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001981 continue;
1982 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001983 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1984 /* do nothing */
1985 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001986 terminal->escape_flags |= ESC_FLAG_WHAT;
1987 } else if (utf8.byte[0] == '>') {
1988 terminal->escape_flags |= ESC_FLAG_GT;
1989 } else if (utf8.byte[0] == '!') {
1990 terminal->escape_flags |= ESC_FLAG_BANG;
1991 } else if (utf8.byte[0] == '$') {
1992 terminal->escape_flags |= ESC_FLAG_CASH;
1993 } else if (utf8.byte[0] == '\'') {
1994 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1995 } else if (utf8.byte[0] == '"') {
1996 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1997 } else if (utf8.byte[0] == ' ') {
1998 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001999 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002000 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002001 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13002002 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002003 }
2004
2005 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
2006 utf8.byte[0] == '`')
2007 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002008 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002009 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002010 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002011 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002012 continue;
2013 case escape_state_inner_escape:
2014 if (utf8.byte[0] == '\\') {
2015 terminal->state = escape_state_normal;
2016 if (terminal->outer_state == escape_state_dcs) {
2017 handle_dcs(terminal);
2018 } else if (terminal->outer_state == escape_state_osc) {
2019 handle_osc(terminal);
2020 }
2021 } else if (utf8.byte[0] == '\e') {
2022 terminal->state = terminal->outer_state;
2023 escape_append_utf8(terminal, utf8);
2024 if (terminal->escape_length >= MAX_ESCAPE)
2025 terminal->state = escape_state_normal;
2026 } else {
2027 terminal->state = terminal->outer_state;
2028 if (terminal->escape_length < MAX_ESCAPE)
2029 terminal->escape[terminal->escape_length++] = '\e';
2030 escape_append_utf8(terminal, utf8);
2031 if (terminal->escape_length >= MAX_ESCAPE)
2032 terminal->state = escape_state_normal;
2033 }
2034 continue;
2035 case escape_state_dcs:
2036 case escape_state_osc:
2037 case escape_state_ignore:
2038 if (utf8.byte[0] == '\e') {
2039 terminal->outer_state = terminal->state;
2040 terminal->state = escape_state_inner_escape;
2041 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
2042 terminal->state = escape_state_normal;
2043 handle_osc(terminal);
2044 } else {
2045 escape_append_utf8(terminal, utf8);
2046 if (terminal->escape_length >= MAX_ESCAPE)
2047 terminal->state = escape_state_normal;
2048 }
2049 continue;
2050 case escape_state_special:
2051 escape_append_utf8(terminal, utf8);
2052 terminal->state = escape_state_normal;
2053 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2054 handle_special_escape(terminal, terminal->escape[1],
2055 utf8.byte[0]);
2056 }
2057 continue;
2058 default:
2059 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002060 }
2061
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002062 /* this is valid, because ASCII characters are never used to
2063 * introduce a multibyte sequence in UTF-8 */
2064 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002065 terminal->state = escape_state_escape;
2066 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002067 terminal->escape[0] = '\e';
2068 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002069 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002070 } else {
2071 handle_char(terminal, utf8);
2072 } /* if */
2073 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002074
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002075 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002076}
2077
2078static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002079data_source_target(void *data,
2080 struct wl_data_source *source, const char *mime_type)
2081{
2082 fprintf(stderr, "data_source_target, %s\n", mime_type);
2083}
2084
2085static void
2086data_source_send(void *data,
2087 struct wl_data_source *source,
2088 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002089{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002090 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002091
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002092 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002093}
2094
2095static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002096data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002097{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002098 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002099}
2100
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002101static const struct wl_data_source_listener data_source_listener = {
2102 data_source_target,
2103 data_source_send,
2104 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002105};
2106
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002107static void
2108fullscreen_handler(struct window *window, void *data)
2109{
2110 struct terminal *terminal = data;
2111
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002112 window_set_fullscreen(window, !window_is_fullscreen(terminal->window));
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002113}
2114
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002115static void
2116close_handler(struct window *window, void *data)
2117{
2118 struct terminal *terminal = data;
2119
2120 terminal_destroy(terminal);
2121}
2122
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002123static int
2124handle_bound_key(struct terminal *terminal,
2125 struct input *input, uint32_t sym, uint32_t time)
2126{
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002127 struct terminal *new_terminal;
2128
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002129 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002130 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002131 /* Cut selection; terminal doesn't do cut, fall
2132 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002133 case XKB_KEY_C:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002134 terminal->selection =
2135 display_create_data_source(terminal->display);
2136 wl_data_source_offer(terminal->selection,
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002137 "text/plain;charset=utf-8");
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002138 wl_data_source_add_listener(terminal->selection,
2139 &data_source_listener, terminal);
Kristian Høgsbergfee91be2012-06-02 21:21:41 -04002140 input_set_selection(input, terminal->selection,
2141 display_get_serial(terminal->display));
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002142 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002143 case XKB_KEY_V:
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002144 input_receive_selection_data_to_fd(input,
2145 "text/plain;charset=utf-8",
2146 terminal->master);
2147
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002148 return 1;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002149
2150 case XKB_KEY_N:
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002151 new_terminal = terminal_create(terminal->display);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002152 if (terminal_run(new_terminal, option_shell))
2153 terminal_destroy(new_terminal);
2154
2155 return 1;
2156
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002157 default:
2158 return 0;
2159 }
2160}
2161
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002162static void
2163key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002164 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2165 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002166{
2167 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002168 char ch[MAX_RESPONSE];
Kristian Høgsberg88fd4082012-06-21 15:55:03 -04002169 uint32_t modifiers, serial;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002170 int ret, len = 0;
2171 bool convert_utf8 = true;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002172
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002173 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002174 if ((modifiers & MOD_CONTROL_MASK) &&
2175 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002176 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2177 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002178 return;
2179
Daniel Stonea8468712012-11-07 17:51:35 +11002180 /* Map keypad symbols to 'normal' equivalents before processing */
2181 switch (sym) {
2182 case XKB_KEY_KP_Space:
2183 sym = XKB_KEY_space;
2184 break;
2185 case XKB_KEY_KP_Tab:
2186 sym = XKB_KEY_Tab;
2187 break;
2188 case XKB_KEY_KP_Enter:
2189 sym = XKB_KEY_Return;
2190 break;
2191 case XKB_KEY_KP_Left:
2192 sym = XKB_KEY_Left;
2193 break;
2194 case XKB_KEY_KP_Up:
2195 sym = XKB_KEY_Up;
2196 break;
2197 case XKB_KEY_KP_Right:
2198 sym = XKB_KEY_Right;
2199 break;
2200 case XKB_KEY_KP_Down:
2201 sym = XKB_KEY_Down;
2202 break;
2203 case XKB_KEY_KP_Equal:
2204 sym = XKB_KEY_equal;
2205 break;
2206 case XKB_KEY_KP_Multiply:
2207 sym = XKB_KEY_asterisk;
2208 break;
2209 case XKB_KEY_KP_Add:
2210 sym = XKB_KEY_plus;
2211 break;
2212 case XKB_KEY_KP_Separator:
2213 /* Note this is actually locale-dependent and should mostly be
2214 * a comma. But leave it as period until we one day start
2215 * doing the right thing. */
2216 sym = XKB_KEY_period;
2217 break;
2218 case XKB_KEY_KP_Subtract:
2219 sym = XKB_KEY_minus;
2220 break;
2221 case XKB_KEY_KP_Decimal:
2222 sym = XKB_KEY_period;
2223 break;
2224 case XKB_KEY_KP_Divide:
2225 sym = XKB_KEY_slash;
2226 break;
2227 case XKB_KEY_KP_0:
2228 case XKB_KEY_KP_1:
2229 case XKB_KEY_KP_2:
2230 case XKB_KEY_KP_3:
2231 case XKB_KEY_KP_4:
2232 case XKB_KEY_KP_5:
2233 case XKB_KEY_KP_6:
2234 case XKB_KEY_KP_7:
2235 case XKB_KEY_KP_8:
2236 case XKB_KEY_KP_9:
2237 sym = (sym - XKB_KEY_KP_0) + XKB_KEY_0;
2238 break;
2239 default:
2240 break;
2241 }
2242
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002243 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002244 case XKB_KEY_BackSpace:
Kristian Høgsbergb7f94bf2012-06-21 12:29:36 -04002245 if (modifiers & MOD_ALT_MASK)
2246 ch[len++] = 0x1b;
Kristian Høgsberg71a4cf42012-06-20 17:57:56 -04002247 ch[len++] = 0x7f;
2248 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002249 case XKB_KEY_Tab:
2250 case XKB_KEY_Linefeed:
2251 case XKB_KEY_Clear:
2252 case XKB_KEY_Pause:
2253 case XKB_KEY_Scroll_Lock:
2254 case XKB_KEY_Sys_Req:
2255 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002256 ch[len++] = sym & 0x7f;
2257 break;
2258
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002259 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002260 if (terminal->mode & MODE_LF_NEWLINE) {
2261 ch[len++] = 0x0D;
2262 ch[len++] = 0x0A;
2263 } else {
2264 ch[len++] = 0x0D;
2265 }
2266 break;
2267
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002268 case XKB_KEY_Shift_L:
2269 case XKB_KEY_Shift_R:
2270 case XKB_KEY_Control_L:
2271 case XKB_KEY_Control_R:
2272 case XKB_KEY_Alt_L:
2273 case XKB_KEY_Alt_R:
Kristian Høgsberg22fbcf72012-06-22 12:18:56 -04002274 case XKB_KEY_Meta_L:
2275 case XKB_KEY_Meta_R:
2276 case XKB_KEY_Super_L:
2277 case XKB_KEY_Super_R:
2278 case XKB_KEY_Hyper_L:
2279 case XKB_KEY_Hyper_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002280 break;
2281
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002282 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002283 len = function_key_response('[', 2, modifiers, '~', ch);
2284 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002285 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002286 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2287 ch[len++] = '\x04';
2288 } else {
2289 len = function_key_response('[', 3, modifiers, '~', ch);
2290 }
2291 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002292 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002293 len = function_key_response('[', 5, modifiers, '~', ch);
2294 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002295 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002296 len = function_key_response('[', 6, modifiers, '~', ch);
2297 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002298 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002299 len = function_key_response('O', 1, modifiers, 'P', ch);
2300 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002301 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002302 len = function_key_response('O', 1, modifiers, 'Q', ch);
2303 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002304 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002305 len = function_key_response('O', 1, modifiers, 'R', ch);
2306 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002307 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002308 len = function_key_response('O', 1, modifiers, 'S', ch);
2309 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002310 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002311 len = function_key_response('[', 15, modifiers, '~', ch);
2312 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002313 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002314 len = function_key_response('[', 17, modifiers, '~', ch);
2315 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002316 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002317 len = function_key_response('[', 18, modifiers, '~', ch);
2318 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002319 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002320 len = function_key_response('[', 19, modifiers, '~', ch);
2321 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002322 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002323 len = function_key_response('[', 20, modifiers, '~', ch);
2324 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002325 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002326 len = function_key_response('[', 21, modifiers, '~', ch);
2327 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002328 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002329 len = function_key_response('[', 24, modifiers, '~', ch);
2330 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002331 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002332 /* Handle special keys with alternate mappings */
2333 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2334 if (len != 0) break;
2335
Kristian Høgsberg70163132012-05-08 15:55:39 -04002336 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002337 if (sym >= '3' && sym <= '7')
2338 sym = (sym & 0x1f) + 8;
2339
2340 if (!((sym >= '!' && sym <= '/') ||
2341 (sym >= '8' && sym <= '?') ||
2342 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2343 else if (sym == '2') sym = 0x00;
2344 else if (sym == '/') sym = 0x1F;
2345 else if (sym == '8' || sym == '?') sym = 0x7F;
Kristian Høgsbergae9e0732012-06-21 12:30:15 -04002346 }
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002347 if (modifiers & MOD_ALT_MASK) {
2348 if (terminal->mode & MODE_ALT_SENDS_ESC) {
2349 ch[len++] = 0x1b;
2350 } else {
2351 sym = sym | 0x80;
2352 convert_utf8 = false;
2353 }
Callum Lowcay7e08e902011-01-07 19:47:02 +00002354 }
2355
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002356 if ((sym < 128) ||
2357 (!convert_utf8 && sym < 256)) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002358 ch[len++] = sym;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002359 } else {
2360 ret = xkb_keysym_to_utf8(sym, ch + len,
2361 MAX_RESPONSE - len);
2362 if (ret < 0)
2363 fprintf(stderr,
2364 "Warning: buffer too small to encode "
2365 "UTF8 character\n");
2366 else
2367 len += ret;
2368 }
2369
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002370 break;
2371 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002372
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002373 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04002374 terminal_write(terminal, ch, len);
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002375
2376 /* Hide cursor, except if this was coming from a
2377 * repeating key press. */
2378 serial = display_get_serial(terminal->display);
2379 if (terminal->hide_cursor_serial != serial) {
2380 input_set_pointer_image(input, CURSOR_BLANK);
2381 terminal->hide_cursor_serial = serial;
2382 }
2383 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002384}
2385
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002386static void
2387keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002388 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002389{
2390 struct terminal *terminal = data;
2391
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002392 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002393}
2394
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002395static int wordsep(int ch)
2396{
2397 const char extra[] = "-,./?%&#:_=+@~";
2398
Andre Heider552d12b2012-08-02 20:59:43 +02002399 if (ch > 127)
2400 return 1;
2401
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002402 return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
2403}
2404
2405static int
2406recompute_selection(struct terminal *terminal)
2407{
2408 struct rectangle allocation;
2409 int col, x, width, height;
2410 int start_row, end_row;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002411 int word_start, eol;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002412 int side_margin, top_margin;
2413 int start_x, end_x;
2414 int cw, ch;
2415 union utf8_char *data;
2416
Peng Wuf291f202013-06-06 15:32:41 +08002417 cw = terminal->average_width;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002418 ch = terminal->extents.height;
2419 widget_get_allocation(terminal->widget, &allocation);
2420 width = terminal->width * cw;
2421 height = terminal->height * ch;
2422 side_margin = allocation.x + (allocation.width - width) / 2;
2423 top_margin = allocation.y + (allocation.height - height) / 2;
2424
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002425 start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1;
2426 end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1;
2427
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002428 if (start_row < end_row ||
2429 (start_row == end_row &&
2430 terminal->selection_start_x < terminal->selection_end_x)) {
2431 terminal->selection_start_row = start_row;
2432 terminal->selection_end_row = end_row;
2433 start_x = terminal->selection_start_x;
2434 end_x = terminal->selection_end_x;
2435 } else {
2436 terminal->selection_start_row = end_row;
2437 terminal->selection_end_row = start_row;
2438 start_x = terminal->selection_end_x;
2439 end_x = terminal->selection_start_x;
2440 }
2441
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002442 eol = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002443 if (terminal->selection_start_row < 0) {
2444 terminal->selection_start_row = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002445 terminal->selection_start_col = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002446 } else {
2447 x = side_margin + cw / 2;
2448 data = terminal_get_row(terminal,
2449 terminal->selection_start_row);
2450 word_start = 0;
2451 for (col = 0; col < terminal->width; col++, x += cw) {
2452 if (col == 0 || wordsep(data[col - 1].ch))
2453 word_start = col;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002454 if (data[col].ch != 0)
2455 eol = col + 1;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002456 if (start_x < x)
2457 break;
2458 }
2459
2460 switch (terminal->dragging) {
2461 case SELECT_LINE:
2462 terminal->selection_start_col = 0;
2463 break;
2464 case SELECT_WORD:
2465 terminal->selection_start_col = word_start;
2466 break;
2467 case SELECT_CHAR:
2468 terminal->selection_start_col = col;
2469 break;
2470 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002471 }
2472
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002473 if (terminal->selection_end_row >= terminal->height) {
2474 terminal->selection_end_row = terminal->height;
2475 terminal->selection_end_col = 0;
2476 } else {
2477 x = side_margin + cw / 2;
2478 data = terminal_get_row(terminal, terminal->selection_end_row);
2479 for (col = 0; col < terminal->width; col++, x += cw) {
2480 if (terminal->dragging == SELECT_CHAR && end_x < x)
2481 break;
2482 if (terminal->dragging == SELECT_WORD &&
2483 end_x < x && wordsep(data[col].ch))
2484 break;
2485 }
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002486 terminal->selection_end_col = col;
2487 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002488
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002489 if (terminal->selection_end_col != terminal->selection_start_col ||
2490 terminal->selection_start_row != terminal->selection_end_row) {
2491 col = terminal->selection_end_col;
2492 if (col > 0 && data[col - 1].ch == 0)
2493 terminal->selection_end_col = terminal->width;
2494 data = terminal_get_row(terminal, terminal->selection_start_row);
2495 if (data[terminal->selection_start_col].ch == 0)
2496 terminal->selection_start_col = eol;
2497 }
2498
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002499 return 1;
2500}
2501
Kristian Høgsberg59826582011-01-20 11:56:57 -05002502static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002503button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002504 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002505 uint32_t button,
2506 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002507{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002508 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002509
2510 switch (button) {
2511 case 272:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002512 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002513
2514 if (time - terminal->button_time < 500)
2515 terminal->click_count++;
2516 else
2517 terminal->click_count = 1;
2518
2519 terminal->button_time = time;
2520 terminal->dragging =
2521 (terminal->click_count - 1) % 3 + SELECT_CHAR;
2522
Kristian Høgsberg59826582011-01-20 11:56:57 -05002523 input_get_position(input,
2524 &terminal->selection_start_x,
2525 &terminal->selection_start_y);
2526 terminal->selection_end_x = terminal->selection_start_x;
2527 terminal->selection_end_y = terminal->selection_start_y;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002528 if (recompute_selection(terminal))
2529 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002530 } else {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002531 terminal->dragging = SELECT_NONE;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002532 }
2533 break;
2534 }
2535}
2536
2537static int
Kristian Høgsberg29784402012-06-28 14:27:02 -04002538enter_handler(struct widget *widget,
2539 struct input *input, float x, float y, void *data)
2540{
2541 return CURSOR_IBEAM;
2542}
2543
2544static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002545motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002546 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002547 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002548{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002549 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002550
2551 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002552 input_get_position(input,
2553 &terminal->selection_end_x,
2554 &terminal->selection_end_y);
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002555
2556 if (recompute_selection(terminal))
2557 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002558 }
2559
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002560 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002561}
2562
Alexander Larssonde79dd02013-05-22 14:41:34 +02002563static void
2564output_handler(struct window *window, struct output *output, int enter,
2565 void *data)
2566{
2567 if (enter)
2568 window_set_buffer_transform(window, output_get_transform(output));
2569 window_set_buffer_scale(window, window_get_output_scale(window));
2570 window_schedule_redraw(window);
2571}
2572
Peng Wuf291f202013-06-06 15:32:41 +08002573#ifndef howmany
2574#define howmany(x, y) (((x) + ((y) - 1)) / (y))
2575#endif
2576
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002577static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002578terminal_create(struct display *display)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002579{
2580 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002581 cairo_surface_t *surface;
2582 cairo_t *cr;
Peng Wuf291f202013-06-06 15:32:41 +08002583 cairo_text_extents_t text_extents;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002584
Peter Huttererf3d62272013-08-08 11:57:05 +10002585 terminal = xzalloc(sizeof *terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002586 terminal->color_scheme = &DEFAULT_COLORS;
2587 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002588 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002589 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002590 terminal->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002591 terminal->widget = frame_create(terminal->window, terminal);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002592 window_set_title(terminal->window, "Wayland Terminal");
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002593 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002594
2595 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002596 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002597
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002598 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002599 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002600
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002601 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002602 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002603 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002604 keyboard_focus_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002605 window_set_fullscreen_handler(terminal->window, fullscreen_handler);
Alexander Larssonde79dd02013-05-22 14:41:34 +02002606 window_set_output_handler(terminal->window, output_handler);
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002607 window_set_close_handler(terminal->window, close_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002608
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002609 widget_set_redraw_handler(terminal->widget, redraw_handler);
2610 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002611 widget_set_button_handler(terminal->widget, button_handler);
Kristian Høgsberg29784402012-06-28 14:27:02 -04002612 widget_set_enter_handler(terminal->widget, enter_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002613 widget_set_motion_handler(terminal->widget, motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002614
Kristian Høgsberg09531622010-06-14 23:22:15 -04002615 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2616 cr = cairo_create(surface);
Kristian Høgsberg38912df2012-06-27 17:52:23 -04002617 cairo_set_font_size(cr, option_font_size);
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002618 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002619 CAIRO_FONT_SLANT_NORMAL,
2620 CAIRO_FONT_WEIGHT_BOLD);
2621 terminal->font_bold = cairo_get_scaled_font (cr);
2622 cairo_scaled_font_reference(terminal->font_bold);
2623
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002624 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002625 CAIRO_FONT_SLANT_NORMAL,
2626 CAIRO_FONT_WEIGHT_NORMAL);
2627 terminal->font_normal = cairo_get_scaled_font (cr);
2628 cairo_scaled_font_reference(terminal->font_normal);
2629
Kristian Høgsberg09531622010-06-14 23:22:15 -04002630 cairo_font_extents(cr, &terminal->extents);
Peng Wuf291f202013-06-06 15:32:41 +08002631
2632 /* Compute the average ascii glyph width */
2633 cairo_text_extents(cr, TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS,
2634 &text_extents);
2635 terminal->average_width = howmany
2636 (text_extents.width,
2637 strlen(TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS));
2638 terminal->average_width = ceil(terminal->average_width);
2639
Kristian Høgsberg09531622010-06-14 23:22:15 -04002640 cairo_destroy(cr);
2641 cairo_surface_destroy(surface);
2642
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002643 terminal_resize(terminal, 20, 5); /* Set minimum size first */
Kristian Høgsberga1627922012-06-20 17:30:03 -04002644 terminal_resize(terminal, 80, 25);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002645
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002646 wl_list_insert(terminal_list.prev, &terminal->link);
2647
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002648 return terminal;
2649}
2650
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002651static void
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002652terminal_destroy(struct terminal *terminal)
2653{
Dima Ryazanova85292e2012-11-29 00:27:09 -08002654 display_unwatch_fd(terminal->display, terminal->master);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002655 window_destroy(terminal->window);
2656 close(terminal->master);
2657 wl_list_remove(&terminal->link);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002658
2659 if (wl_list_empty(&terminal_list))
Dima Ryazanova85292e2012-11-29 00:27:09 -08002660 display_exit(terminal->display);
2661
2662 free(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002663}
2664
2665static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002666io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002667{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002668 struct terminal *terminal =
2669 container_of(task, struct terminal, io_task);
2670 char buffer[256];
2671 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002672
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002673 if (events & EPOLLHUP) {
2674 terminal_destroy(terminal);
2675 return;
2676 }
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002677
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002678 len = read(terminal->master, buffer, sizeof buffer);
2679 if (len < 0)
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002680 terminal_destroy(terminal);
2681 else
2682 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002683}
2684
2685static int
2686terminal_run(struct terminal *terminal, const char *path)
2687{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002688 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002689 pid_t pid;
2690
2691 pid = forkpty(&master, NULL, NULL, NULL);
2692 if (pid == 0) {
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04002693 setenv("TERM", option_term, 1);
2694 setenv("COLORTERM", option_term, 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002695 if (execl(path, path, NULL)) {
2696 printf("exec failed: %m\n");
2697 exit(EXIT_FAILURE);
2698 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002699 } else if (pid < 0) {
2700 fprintf(stderr, "failed to fork and create pty (%m).\n");
2701 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002702 }
2703
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002704 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002705 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002706 terminal->io_task.run = io_handler;
2707 display_watch_fd(terminal->display, terminal->master,
2708 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002709
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002710 window_set_fullscreen(terminal->window, option_fullscreen);
2711 if (!window_is_fullscreen(terminal->window))
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002712 terminal_resize(terminal, 80, 24);
2713
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002714 return 0;
2715}
2716
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002717static const struct config_key terminal_config_keys[] = {
2718 { "font", CONFIG_KEY_STRING, &option_font },
Kristian Høgsberg38912df2012-06-27 17:52:23 -04002719 { "font-size", CONFIG_KEY_INTEGER, &option_font_size },
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04002720 { "term", CONFIG_KEY_STRING, &option_term },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002721};
2722
2723static const struct config_section config_sections[] = {
2724 { "terminal",
2725 terminal_config_keys, ARRAY_LENGTH(terminal_config_keys) },
2726};
2727
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002728static const struct weston_option terminal_options[] = {
2729 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002730 { WESTON_OPTION_STRING, "font", 0, &option_font },
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002731 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002732};
2733
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002734int main(int argc, char *argv[])
2735{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002736 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002737 struct terminal *terminal;
Ossama Othmana50e6e42013-05-14 09:48:26 -07002738 int config_fd;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002739
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002740 option_shell = getenv("SHELL");
2741 if (!option_shell)
2742 option_shell = "/bin/bash";
2743
Ossama Othmana50e6e42013-05-14 09:48:26 -07002744 config_fd = open_config_file("weston.ini");
2745 parse_config_file(config_fd,
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002746 config_sections, ARRAY_LENGTH(config_sections),
2747 NULL);
Ossama Othmana50e6e42013-05-14 09:48:26 -07002748 close(config_fd);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002749
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002750 parse_options(terminal_options,
2751 ARRAY_LENGTH(terminal_options), &argc, argv);
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002752
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002753 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002754 if (d == NULL) {
2755 fprintf(stderr, "failed to create display: %m\n");
2756 return -1;
2757 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002758
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002759 wl_list_init(&terminal_list);
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002760 terminal = terminal_create(d);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002761 if (terminal_run(terminal, option_shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002762 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002763
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002764 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002765
2766 return 0;
2767}