blob: 298572631905656de843f5a8e7200367d0573788 [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
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050057#define MOD_SHIFT 0x01
58#define MOD_ALT 0x02
59#define MOD_CTRL 0x04
60
Callum Lowcay30eeae52011-01-07 19:46:55 +000061#define ATTRMASK_BOLD 0x01
62#define ATTRMASK_UNDERLINE 0x02
63#define ATTRMASK_BLINK 0x04
64#define ATTRMASK_INVERSE 0x08
Callum Lowcay81179db2011-01-10 12:14:01 +130065#define ATTRMASK_CONCEALED 0x10
Callum Lowcay30eeae52011-01-07 19:46:55 +000066
Callum Lowcayb8609ad2011-01-07 19:46:57 +000067/* Buffer sizes */
Callum Lowcayef57a9b2011-01-14 20:46:23 +130068#define MAX_RESPONSE 256
69#define MAX_ESCAPE 255
Callum Lowcayb8609ad2011-01-07 19:46:57 +000070
Callum Lowcay8e57dd52011-01-07 19:46:59 +000071/* Terminal modes */
72#define MODE_SHOW_CURSOR 0x00000001
73#define MODE_INVERSE 0x00000002
74#define MODE_AUTOWRAP 0x00000004
75#define MODE_AUTOREPEAT 0x00000008
76#define MODE_LF_NEWLINE 0x00000010
Callum Lowcay69e96582011-01-07 19:47:00 +000077#define MODE_IRM 0x00000020
Callum Lowcay7e08e902011-01-07 19:47:02 +000078#define MODE_DELETE_SENDS_DEL 0x00000040
79#define MODE_ALT_SENDS_ESC 0x00000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +000080
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000081union utf8_char {
82 unsigned char byte[4];
83 uint32_t ch;
84};
85
86enum utf8_state {
87 utf8state_start,
88 utf8state_accept,
89 utf8state_reject,
90 utf8state_expect3,
91 utf8state_expect2,
92 utf8state_expect1
93};
94
95struct utf8_state_machine {
96 enum utf8_state state;
97 int len;
98 union utf8_char s;
99};
100
101static void
102init_state_machine(struct utf8_state_machine *machine)
103{
104 machine->state = utf8state_start;
105 machine->len = 0;
106 machine->s.ch = 0;
107}
108
109static enum utf8_state
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400110utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000111{
112 switch(machine->state) {
113 case utf8state_start:
114 case utf8state_accept:
115 case utf8state_reject:
116 machine->s.ch = 0;
117 machine->len = 0;
118 if(c == 0xC0 || c == 0xC1) {
119 /* overlong encoding, reject */
120 machine->state = utf8state_reject;
121 } else if((c & 0x80) == 0) {
122 /* single byte, accept */
123 machine->s.byte[machine->len++] = c;
124 machine->state = utf8state_accept;
125 } else if((c & 0xC0) == 0x80) {
126 /* parser out of sync, ignore byte */
127 machine->state = utf8state_start;
128 } else if((c & 0xE0) == 0xC0) {
129 /* start of two byte sequence */
130 machine->s.byte[machine->len++] = c;
131 machine->state = utf8state_expect1;
132 } else if((c & 0xF0) == 0xE0) {
133 /* start of three byte sequence */
134 machine->s.byte[machine->len++] = c;
135 machine->state = utf8state_expect2;
136 } else if((c & 0xF8) == 0xF0) {
137 /* start of four byte sequence */
138 machine->s.byte[machine->len++] = c;
139 machine->state = utf8state_expect3;
140 } else {
141 /* overlong encoding, reject */
142 machine->state = utf8state_reject;
143 }
144 break;
145 case utf8state_expect3:
146 machine->s.byte[machine->len++] = c;
147 if((c & 0xC0) == 0x80) {
148 /* all good, continue */
149 machine->state = utf8state_expect2;
150 } else {
151 /* missing extra byte, reject */
152 machine->state = utf8state_reject;
153 }
154 break;
155 case utf8state_expect2:
156 machine->s.byte[machine->len++] = c;
157 if((c & 0xC0) == 0x80) {
158 /* all good, continue */
159 machine->state = utf8state_expect1;
160 } else {
161 /* missing extra byte, reject */
162 machine->state = utf8state_reject;
163 }
164 break;
165 case utf8state_expect1:
166 machine->s.byte[machine->len++] = c;
167 if((c & 0xC0) == 0x80) {
168 /* all good, accept */
169 machine->state = utf8state_accept;
170 } else {
171 /* missing extra byte, reject */
172 machine->state = utf8state_reject;
173 }
174 break;
175 default:
176 machine->state = utf8state_reject;
177 break;
178 }
179
180 return machine->state;
181}
182
Callum Lowcay256e72f2011-01-07 19:47:01 +0000183struct char_sub {
184 union utf8_char match;
185 union utf8_char replace;
186};
187/* Set last char_sub match to NULL char */
188typedef struct char_sub *character_set;
189
190struct char_sub CS_US[] = {
191 {{{0, }}, {{0, }}}
192};
193static struct char_sub CS_UK[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200194 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000195 {{{0, }}, {{0, }}}
196};
197static struct char_sub CS_SPECIAL[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200198 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond: ♦ */
199 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell: ▒ */
200 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT: ␉ */
201 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF: ␌ */
202 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR: ␍ */
203 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF: ␊ */
204 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree: ° */
205 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus: ± */
206 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL: ␤ */
207 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT: ␋ */
208 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB: ┘ */
209 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT: ┐ */
210 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT: ┌ */
211 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_LB: └ */
212 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS: ┼ */
213 {{{'o', 0, }}, {{0xE2, 0x8E, 0xBA, 0}}}, /* Horiz. Scan Line 1: ⎺ */
214 {{{'p', 0, }}, {{0xE2, 0x8E, 0xBB, 0}}}, /* Horiz. Scan Line 3: ⎻ */
215 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* Horiz. Scan Line 5: ─ */
216 {{{'r', 0, }}, {{0xE2, 0x8E, 0xBC, 0}}}, /* Horiz. Scan Line 7: ⎼ */
217 {{{'s', 0, }}, {{0xE2, 0x8E, 0xBD, 0}}}, /* Horiz. Scan Line 9: ⎽ */
218 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR: ├ */
219 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL: ┤ */
220 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU: ┴ */
221 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD: ┬ */
222 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V: │ */
223 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE: ≤ */
224 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE: ≥ */
225 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI: π */
226 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ: ≠ */
227 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
228 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT: ⋅ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000229 {{{0, }}, {{0, }}}
230};
231
232static void
233apply_char_set(character_set cs, union utf8_char *utf8)
234{
235 int i = 0;
236
237 while (cs[i].match.byte[0]) {
238 if ((*utf8).ch == cs[i].match.ch) {
239 *utf8 = cs[i].replace;
240 break;
241 }
242 i++;
243 }
244}
245
Callum Lowcay7e08e902011-01-07 19:47:02 +0000246struct key_map {
247 int sym;
248 int num;
249 char escape;
250 char code;
251};
252/* Set last key_sub sym to NULL */
253typedef struct key_map *keyboard_mode;
254
255static struct key_map KM_NORMAL[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400256 { XKB_KEY_Left, 1, '[', 'D' },
257 { XKB_KEY_Right, 1, '[', 'C' },
258 { XKB_KEY_Up, 1, '[', 'A' },
259 { XKB_KEY_Down, 1, '[', 'B' },
260 { XKB_KEY_Home, 1, '[', 'H' },
261 { XKB_KEY_End, 1, '[', 'F' },
262 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000263};
264static struct key_map KM_APPLICATION[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400265 { XKB_KEY_Left, 1, 'O', 'D' },
266 { XKB_KEY_Right, 1, 'O', 'C' },
267 { XKB_KEY_Up, 1, 'O', 'A' },
268 { XKB_KEY_Down, 1, 'O', 'B' },
269 { XKB_KEY_Home, 1, 'O', 'H' },
270 { XKB_KEY_End, 1, 'O', 'F' },
271 { XKB_KEY_KP_Enter, 1, 'O', 'M' },
272 { XKB_KEY_KP_Multiply, 1, 'O', 'j' },
273 { XKB_KEY_KP_Add, 1, 'O', 'k' },
274 { XKB_KEY_KP_Separator, 1, 'O', 'l' },
275 { XKB_KEY_KP_Subtract, 1, 'O', 'm' },
276 { XKB_KEY_KP_Divide, 1, 'O', 'o' },
277 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000278};
279
280static int
281function_key_response(char escape, int num, uint32_t modifiers,
282 char code, char *response)
283{
284 int mod_num = 0;
285 int len;
286
Kristian Høgsberg70163132012-05-08 15:55:39 -0400287 if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
288 if (modifiers & MOD_ALT_MASK) mod_num |= 2;
289 if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000290
291 if (mod_num != 0)
292 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
293 num, mod_num + 1, code);
294 else if (code != '~')
295 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
296 escape, code);
297 else
298 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
299 escape, num, code);
300
301 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
302 else return len;
303}
304
305/* returns the number of bytes written into response,
306 * which must have room for MAX_RESPONSE bytes */
307static int
308apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
309{
310 struct key_map map;
311 int len = 0;
312 int i = 0;
313
314 while (mode[i].sym) {
315 map = mode[i++];
316 if (sym == map.sym) {
317 len = function_key_response(map.escape, map.num,
318 modifiers, map.code,
319 response);
320 break;
321 }
322 }
323
324 return len;
325}
326
Callum Lowcay30eeae52011-01-07 19:46:55 +0000327struct terminal_color { double r, g, b, a; };
328struct attr {
329 unsigned char fg, bg;
330 char a; /* attributes format:
331 * 76543210
Callum Lowcay81179db2011-01-10 12:14:01 +1300332 * cilub */
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500333 char s; /* in selection */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000334};
335struct color_scheme {
336 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500337 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000338 struct attr default_attr;
339};
340
341static void
342attr_init(struct attr *data_attr, struct attr attr, int n)
343{
344 int i;
345 for (i = 0; i < n; i++) {
346 data_attr[i] = attr;
347 }
348}
349
Callum Lowcay67a201d2011-01-12 19:23:41 +1300350enum escape_state {
351 escape_state_normal = 0,
352 escape_state_escape,
353 escape_state_dcs,
354 escape_state_csi,
355 escape_state_osc,
356 escape_state_inner_escape,
357 escape_state_ignore,
358 escape_state_special
359};
360
361#define ESC_FLAG_WHAT 0x01
362#define ESC_FLAG_GT 0x02
363#define ESC_FLAG_BANG 0x04
364#define ESC_FLAG_CASH 0x08
365#define ESC_FLAG_SQUOTE 0x10
366#define ESC_FLAG_DQUOTE 0x20
367#define ESC_FLAG_SPACE 0x40
368
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400369enum {
370 SELECT_NONE,
371 SELECT_CHAR,
372 SELECT_WORD,
373 SELECT_LINE
374};
375
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500376struct terminal {
377 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500378 struct widget *widget;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500379 struct display *display;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000380 union utf8_char *data;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400381 struct task io_task;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000382 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000383 struct attr *data_attr;
384 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000385 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000386 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000387 char saved_origin_mode;
388 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000389 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000390 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000391 character_set cs, g0, g1;
392 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000393 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000394 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500395 int width, height, start, row, column;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000396 int saved_row, saved_column;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600397 int send_cursor_position;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500398 int fd, master;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500399 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300400 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500401 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300402 enum escape_state state;
403 enum escape_state outer_state;
404 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000405 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500406 int margin;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400407 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000408 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400409 cairo_font_extents_t extents;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500410 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg88fd4082012-06-21 15:55:03 -0400411 uint32_t hide_cursor_serial;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500412
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400413 struct wl_data_source *selection;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400414 uint32_t button_time;
415 int dragging, click_count;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500416 int selection_start_x, selection_start_y;
417 int selection_end_x, selection_end_y;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400418 int selection_start_row, selection_start_col;
419 int selection_end_row, selection_end_col;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -0400420 struct wl_list link;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500421};
422
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000423/* Create default tab stops, every 8 characters */
424static void
425terminal_init_tabs(struct terminal *terminal)
426{
427 int i = 0;
428
429 while (i < terminal->width) {
430 if (i % 8 == 0)
431 terminal->tab_ruler[i] = 1;
432 else
433 terminal->tab_ruler[i] = 0;
434 i++;
435 }
436}
437
Callum Lowcay30eeae52011-01-07 19:46:55 +0000438static void
439terminal_init(struct terminal *terminal)
440{
441 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000442 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000443 terminal->mode = MODE_SHOW_CURSOR |
444 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000445 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000446 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000447
448 terminal->row = 0;
449 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000450
Callum Lowcay256e72f2011-01-07 19:47:01 +0000451 terminal->g0 = CS_US;
452 terminal->g1 = CS_US;
453 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000454 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000455
456 terminal->saved_g0 = terminal->g0;
457 terminal->saved_g1 = terminal->g1;
458 terminal->saved_cs = terminal->cs;
459
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000460 terminal->saved_attr = terminal->curr_attr;
461 terminal->saved_origin_mode = terminal->origin_mode;
462 terminal->saved_row = terminal->row;
463 terminal->saved_column = terminal->column;
464
465 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000466}
467
468static void
469init_color_table(struct terminal *terminal)
470{
471 int c, r;
472 struct terminal_color *color_table = terminal->color_table;
473
474 for (c = 0; c < 256; c ++) {
475 if (c < 16) {
476 color_table[c] = terminal->color_scheme->palette[c];
477 } else if (c < 232) {
478 r = c - 16;
479 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
480 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
481 color_table[c].r = ((double)(r % 6) / 6.0);
482 color_table[c].a = 1.0;
483 } else {
484 r = (c - 232) * 10 + 8;
485 color_table[c].r = ((double) r) / 256.0;
486 color_table[c].g = color_table[c].r;
487 color_table[c].b = color_table[c].r;
488 color_table[c].a = 1.0;
489 }
490 }
491}
492
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000493static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500494terminal_get_row(struct terminal *terminal, int row)
495{
496 int index;
497
498 index = (row + terminal->start) % terminal->height;
499
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000500 return &terminal->data[index * terminal->width];
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500501}
502
Callum Lowcay30eeae52011-01-07 19:46:55 +0000503static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500504terminal_get_attr_row(struct terminal *terminal, int row)
505{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000506 int index;
507
508 index = (row + terminal->start) % terminal->height;
509
510 return &terminal->data_attr[index * terminal->width];
511}
512
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500513union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300514 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500515 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500516};
517
518static void
519terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500520 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500521{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500522 struct attr attr;
523 int foreground, background, tmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500524
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500525 decoded->attr.s = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400526 if (((row == terminal->selection_start_row &&
527 col >= terminal->selection_start_col) ||
528 row > terminal->selection_start_row) &&
529 ((row == terminal->selection_end_row &&
530 col < terminal->selection_end_col) ||
531 row < terminal->selection_end_row))
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500532 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500533
534 /* get the attributes for this character cell */
535 attr = terminal_get_attr_row(terminal, row)[col];
536 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500537 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500538 ((terminal->mode & MODE_SHOW_CURSOR) &&
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400539 window_has_focus(terminal->window) && terminal->row == row &&
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500540 terminal->column == col)) {
541 foreground = attr.bg;
542 background = attr.fg;
543 if (attr.a & ATTRMASK_BOLD) {
544 if (foreground <= 16) foreground |= 0x08;
545 if (background <= 16) background &= 0x07;
546 }
547 } else {
548 foreground = attr.fg;
549 background = attr.bg;
550 }
551
552 if (terminal->mode & MODE_INVERSE) {
553 tmp = foreground;
554 foreground = background;
555 background = tmp;
556 if (attr.a & ATTRMASK_BOLD) {
557 if (foreground <= 16) foreground |= 0x08;
558 if (background <= 16) background &= 0x07;
559 }
560 }
561
Callum Lowcay9d708b02011-01-12 20:06:17 +1300562 decoded->attr.fg = foreground;
563 decoded->attr.bg = background;
564 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000565}
566
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500567
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500568static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000569terminal_scroll_buffer(struct terminal *terminal, int d)
570{
571 int i;
572
573 d = d % (terminal->height + 1);
574 terminal->start = (terminal->start + d) % terminal->height;
575 if (terminal->start < 0) terminal->start = terminal->height + terminal->start;
576 if(d < 0) {
577 d = 0 - d;
578 for(i = 0; i < d; i++) {
579 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
580 attr_init(terminal_get_attr_row(terminal, i),
581 terminal->curr_attr, terminal->width);
582 }
583 } else {
584 for(i = terminal->height - d; i < terminal->height; i++) {
585 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
586 attr_init(terminal_get_attr_row(terminal, i),
587 terminal->curr_attr, terminal->width);
588 }
589 }
Kristian Høgsberg18e928d2012-06-27 19:29:41 -0400590
591 terminal->selection_start_row -= d;
592 terminal->selection_end_row -= d;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000593}
594
595static void
596terminal_scroll_window(struct terminal *terminal, int d)
597{
598 int i;
599 int window_height;
600 int from_row, to_row;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000601
602 // scrolling range is inclusive
603 window_height = terminal->margin_bottom - terminal->margin_top + 1;
604 d = d % (window_height + 1);
605 if(d < 0) {
606 d = 0 - d;
607 to_row = terminal->margin_bottom;
608 from_row = terminal->margin_bottom - d;
609
610 for (i = 0; i < (window_height - d); i++) {
611 memcpy(terminal_get_row(terminal, to_row - i),
612 terminal_get_row(terminal, from_row - i),
613 terminal->data_pitch);
614 memcpy(terminal_get_attr_row(terminal, to_row - i),
615 terminal_get_attr_row(terminal, from_row - i),
616 terminal->attr_pitch);
617 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000618 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
619 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000620 attr_init(terminal_get_attr_row(terminal, i),
621 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000622 }
623 } else {
624 to_row = terminal->margin_top;
625 from_row = terminal->margin_top + d;
626
627 for (i = 0; i < (window_height - d); i++) {
628 memcpy(terminal_get_row(terminal, to_row + i),
629 terminal_get_row(terminal, from_row + i),
630 terminal->data_pitch);
631 memcpy(terminal_get_attr_row(terminal, to_row + i),
632 terminal_get_attr_row(terminal, from_row + i),
633 terminal->attr_pitch);
634 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000635 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
636 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000637 attr_init(terminal_get_attr_row(terminal, i),
638 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000639 }
640 }
641}
642
643static void
644terminal_scroll(struct terminal *terminal, int d)
645{
646 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
647 terminal_scroll_buffer(terminal, d);
648 else
649 terminal_scroll_window(terminal, d);
650}
651
652static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000653terminal_shift_line(struct terminal *terminal, int d)
654{
655 union utf8_char *row;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500656 struct attr *attr_row;
Callum Lowcay69e96582011-01-07 19:47:00 +0000657
658 row = terminal_get_row(terminal, terminal->row);
659 attr_row = terminal_get_attr_row(terminal, terminal->row);
660
661 if ((terminal->width + d) <= terminal->column)
662 d = terminal->column + 1 - terminal->width;
663 if ((terminal->column + d) >= terminal->width)
664 d = terminal->width - terminal->column - 1;
665
666 if (d < 0) {
667 d = 0 - d;
668 memmove(&row[terminal->column],
669 &row[terminal->column + d],
670 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
Callum Lowcay69e96582011-01-07 19:47:00 +0000671 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
672 (terminal->width - terminal->column - d) * sizeof(struct attr));
673 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
674 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
675 } else {
676 memmove(&row[terminal->column + d], &row[terminal->column],
677 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
678 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
679 (terminal->width - terminal->column - d) * sizeof(struct attr));
680 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
681 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
682 }
683}
684
685static void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500686terminal_resize_cells(struct terminal *terminal, int width, int height)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500687{
688 size_t size;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000689 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000690 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000691 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000692 int data_pitch, attr_pitch;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500693 int i, l, total_rows;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500694 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000695 struct winsize ws;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500696
697 if (terminal->width == width && terminal->height == height)
698 return;
699
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000700 data_pitch = width * sizeof(union utf8_char);
701 size = data_pitch * height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500702 data = malloc(size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000703 attr_pitch = width * sizeof(struct attr);
704 data_attr = malloc(attr_pitch * height);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000705 tab_ruler = malloc(width);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500706 memset(data, 0, size);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000707 memset(tab_ruler, 0, width);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000708 attr_init(data_attr, terminal->curr_attr, width * height);
709 if (terminal->data && terminal->data_attr) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500710 if (width > terminal->width)
711 l = terminal->width;
712 else
713 l = width;
714
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500715 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500716 total_rows = height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500717 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500718 total_rows = terminal->height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500719 }
720
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000721 for (i = 0; i < total_rows; i++) {
722 memcpy(&data[width * i],
723 terminal_get_row(terminal, i),
724 l * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000725 memcpy(&data_attr[width * i],
726 terminal_get_attr_row(terminal, i),
727 l * sizeof(struct attr));
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000728 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500729
730 free(terminal->data);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000731 free(terminal->data_attr);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000732 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500733 }
734
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000735 terminal->data_pitch = data_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000736 terminal->attr_pitch = attr_pitch;
Callum Lowcay86653ed2011-01-07 19:47:03 +0000737 terminal->margin_bottom =
738 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500739 terminal->width = width;
740 terminal->height = height;
741 terminal->data = data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000742 terminal->data_attr = data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000743 terminal->tab_ruler = tab_ruler;
744 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500745
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000746 /* Update the window size */
747 ws.ws_row = terminal->height;
748 ws.ws_col = terminal->width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500749 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500750 ws.ws_xpixel = allocation.width;
751 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000752 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500753}
754
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500755static void
756resize_handler(struct widget *widget,
757 int32_t width, int32_t height, void *data)
758{
759 struct terminal *terminal = data;
760 int32_t columns, rows, m;
761
762 m = 2 * terminal->margin;
763 columns = (width - m) / (int32_t) terminal->extents.max_x_advance;
764 rows = (height - m) / (int32_t) terminal->extents.height;
765
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400766 if (!window_is_fullscreen(terminal->window) &&
767 !window_is_maximized(terminal->window)) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500768 width = columns * terminal->extents.max_x_advance + m;
769 height = rows * terminal->extents.height + m;
770 widget_set_size(terminal->widget, width, height);
771 }
772
773 terminal_resize_cells(terminal, columns, rows);
774}
775
776static void
777terminal_resize(struct terminal *terminal, int columns, int rows)
778{
779 int32_t width, height, m;
780
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400781 if (window_is_fullscreen(terminal->window) ||
782 window_is_maximized(terminal->window))
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500783 return;
784
785 m = 2 * terminal->margin;
786 width = columns * terminal->extents.max_x_advance + m;
787 height = rows * terminal->extents.height + m;
Kristian Høgsberga1627922012-06-20 17:30:03 -0400788
789 frame_set_child_size(terminal->widget, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500790}
791
Callum Lowcay30eeae52011-01-07 19:46:55 +0000792struct color_scheme DEFAULT_COLORS = {
793 {
794 {0, 0, 0, 1}, /* black */
795 {0.66, 0, 0, 1}, /* red */
796 {0 , 0.66, 0, 1}, /* green */
797 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
798 {0 , 0 , 0.66, 1}, /* blue */
799 {0.66, 0 , 0.66, 1}, /* magenta */
800 {0, 0.66, 0.66, 1}, /* cyan */
801 {0.66, 0.66, 0.66, 1}, /* light grey */
802 {0.22, 0.33, 0.33, 1}, /* dark grey */
803 {1, 0.33, 0.33, 1}, /* high red */
804 {0.33, 1, 0.33, 1}, /* high green */
805 {1, 1, 0.33, 1}, /* high yellow */
806 {0.33, 0.33, 1, 1}, /* high blue */
807 {1, 0.33, 1, 1}, /* high magenta */
808 {0.33, 1, 1, 1}, /* high cyan */
809 {1, 1, 1, 1} /* white */
810 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500811 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000812 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
813};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400814
Kristian Høgsberg22106762008-12-08 13:50:07 -0500815static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500816terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
817{
818 cairo_set_source_rgba(cr,
819 terminal->color_table[index].r,
820 terminal->color_table[index].g,
821 terminal->color_table[index].b,
822 terminal->color_table[index].a);
823}
824
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500825static void
826terminal_send_selection(struct terminal *terminal, int fd)
827{
828 int row, col;
829 union utf8_char *p_row;
830 union decoded_attr attr;
831 FILE *fp;
832 int len;
833
834 fp = fdopen(fd, "w");
835 for (row = 0; row < terminal->height; row++) {
836 p_row = terminal_get_row(terminal, row);
837 for (col = 0; col < terminal->width; col++) {
838 /* get the attributes for this character cell */
839 terminal_decode_attr(terminal, row, col, &attr);
840 if (!attr.attr.s)
841 continue;
842 len = strnlen((char *) p_row[col].byte, 4);
Kristian Høgsberg0dee6472012-07-01 21:25:41 -0400843 if (len > 0)
844 fwrite(p_row[col].byte, 1, len, fp);
845 if (len == 0 || col == terminal->width - 1) {
846 fwrite("\n", 1, 1, fp);
847 break;
848 }
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500849 }
850 }
851 fclose(fp);
852}
853
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500854struct glyph_run {
855 struct terminal *terminal;
856 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400857 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500858 union decoded_attr attr;
859 cairo_glyph_t glyphs[256], *g;
860};
861
862static void
863glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
864{
865 run->terminal = terminal;
866 run->cr = cr;
867 run->g = run->glyphs;
868 run->count = 0;
869 run->attr.key = 0;
870}
871
872static void
873glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
874{
875 cairo_scaled_font_t *font;
876
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500877 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
878 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300879 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500880 font = run->terminal->font_bold;
881 else
882 font = run->terminal->font_normal;
883 cairo_set_scaled_font(run->cr, font);
884 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300885 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500886
Callum Lowcay9d708b02011-01-12 20:06:17 +1300887 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
888 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500889 run->g = run->glyphs;
890 run->count = 0;
891 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300892 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500893}
894
895static void
896glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
897{
898 int num_glyphs;
899 cairo_scaled_font_t *font;
900
901 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
902
Callum Lowcay9d708b02011-01-12 20:06:17 +1300903 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500904 font = run->terminal->font_bold;
905 else
906 font = run->terminal->font_normal;
907
908 cairo_move_to(run->cr, x, y);
909 cairo_scaled_font_text_to_glyphs (font, x, y,
910 (char *) c->byte, 4,
911 &run->g, &num_glyphs,
912 NULL, NULL, NULL);
913 run->g += num_glyphs;
914 run->count += num_glyphs;
915}
916
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500917
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500918static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500919redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500920{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500921 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500922 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500923 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000924 int top_margin, side_margin;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600925 int row, col, cursor_x, cursor_y;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000926 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500927 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000928 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500929 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500930 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500931 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500932 cairo_font_extents_t extents;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500933
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500934 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500935 widget_get_allocation(terminal->widget, &allocation);
Alexander Larssonde79dd02013-05-22 14:41:34 +0200936 cr = widget_cairo_create(terminal->widget);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500937 cairo_rectangle(cr, allocation.x, allocation.y,
938 allocation.width, allocation.height);
939 cairo_clip(cr);
940 cairo_push_group(cr);
941
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500942 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500943 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500944 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500945
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500946 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500947
Kristian Høgsberg59826582011-01-20 11:56:57 -0500948 extents = terminal->extents;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500949 side_margin = (allocation.width - terminal->width * extents.max_x_advance) / 2;
950 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500951
Callum Lowcay30eeae52011-01-07 19:46:55 +0000952 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500953 cairo_translate(cr, allocation.x + side_margin,
954 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500955 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000956 for (row = 0; row < terminal->height; row++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000957 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000958 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500959 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000960
Callum Lowcay9d708b02011-01-12 20:06:17 +1300961 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -0500962 continue;
963
Callum Lowcay9d708b02011-01-12 20:06:17 +1300964 terminal_set_color(terminal, cr, attr.attr.bg);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500965 cairo_move_to(cr, col * extents.max_x_advance,
966 row * extents.height);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000967 cairo_rel_line_to(cr, extents.max_x_advance, 0);
968 cairo_rel_line_to(cr, 0, extents.height);
969 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
970 cairo_close_path(cr);
971 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500972 }
973 }
Callum Lowcay30eeae52011-01-07 19:46:55 +0000974
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500975 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
976
977 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500978 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500979 for (row = 0; row < terminal->height; row++) {
980 p_row = terminal_get_row(terminal, row);
981 for (col = 0; col < terminal->width; col++) {
982 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500983 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500984
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500985 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000986
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500987 text_x = col * extents.max_x_advance;
988 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +1300989 if (attr.attr.a & ATTRMASK_UNDERLINE) {
990 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000991 cairo_move_to(cr, text_x, (double)text_y + 1.5);
992 cairo_line_to(cr, text_x + extents.max_x_advance, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000993 cairo_stroke(cr);
994 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -0500995
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500996 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000997 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500998 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500999
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001000 attr.key = ~0;
1001 glyph_run_flush(&run, attr);
1002
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001003 if ((terminal->mode & MODE_SHOW_CURSOR) &&
1004 !window_has_focus(terminal->window)) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001005 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001006
Callum Lowcay30eeae52011-01-07 19:46:55 +00001007 cairo_set_line_width(cr, 1);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001008 cairo_move_to(cr, terminal->column * extents.max_x_advance + d,
1009 terminal->row * extents.height + d);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001010 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
1011 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
1012 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
1013 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001014
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001015 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001016 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001017
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001018 cairo_pop_group_to_source(cr);
1019 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001020 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001021 cairo_surface_destroy(surface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001022
1023 if (terminal->send_cursor_position) {
1024 cursor_x = side_margin + allocation.x +
1025 terminal->column * extents.max_x_advance;
1026 cursor_y = top_margin + allocation.y +
1027 terminal->row * extents.height;
1028 window_set_text_cursor_position(terminal->window,
1029 cursor_x, cursor_y);
1030 terminal->send_cursor_position = 0;
1031 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001032}
1033
1034static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001035terminal_write(struct terminal *terminal, const char *data, size_t length)
1036{
1037 if (write(terminal->master, data, length) < 0)
1038 abort();
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001039 terminal->send_cursor_position = 1;
Kristian Høgsberg26130862011-08-24 11:30:21 -04001040}
1041
1042static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001043terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001044
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001045static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001046handle_char(struct terminal *terminal, union utf8_char utf8);
1047
1048static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001049handle_sgr(struct terminal *terminal, int code);
1050
1051static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001052handle_term_parameter(struct terminal *terminal, int code, int sr)
1053{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001054 int i;
1055
Callum Lowcay67a201d2011-01-12 19:23:41 +13001056 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001057 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001058 case 1: /* DECCKM */
1059 if (sr) terminal->key_mode = KM_APPLICATION;
1060 else terminal->key_mode = KM_NORMAL;
1061 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001062 case 2: /* DECANM */
1063 /* No VT52 support yet */
1064 terminal->g0 = CS_US;
1065 terminal->g1 = CS_US;
1066 terminal->cs = terminal->g0;
1067 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001068 case 3: /* DECCOLM */
1069 if (sr)
1070 terminal_resize(terminal, 132, 24);
1071 else
1072 terminal_resize(terminal, 80, 24);
1073
1074 /* set columns, but also home cursor and clear screen */
1075 terminal->row = 0; terminal->column = 0;
1076 for (i = 0; i < terminal->height; i++) {
1077 memset(terminal_get_row(terminal, i),
1078 0, terminal->data_pitch);
1079 attr_init(terminal_get_attr_row(terminal, i),
1080 terminal->curr_attr, terminal->width);
1081 }
1082 break;
1083 case 5: /* DECSCNM */
1084 if (sr) terminal->mode |= MODE_INVERSE;
1085 else terminal->mode &= ~MODE_INVERSE;
1086 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001087 case 6: /* DECOM */
1088 terminal->origin_mode = sr;
1089 if (terminal->origin_mode)
1090 terminal->row = terminal->margin_top;
1091 else
1092 terminal->row = 0;
1093 terminal->column = 0;
1094 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001095 case 7: /* DECAWM */
1096 if (sr) terminal->mode |= MODE_AUTOWRAP;
1097 else terminal->mode &= ~MODE_AUTOWRAP;
1098 break;
1099 case 8: /* DECARM */
1100 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1101 else terminal->mode &= ~MODE_AUTOREPEAT;
1102 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001103 case 12: /* Very visible cursor (CVVIS) */
1104 /* FIXME: What do we do here. */
1105 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001106 case 25:
1107 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1108 else terminal->mode &= ~MODE_SHOW_CURSOR;
1109 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001110 case 1034: /* smm/rmm, meta mode on/off */
1111 /* ignore */
1112 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001113 case 1037: /* deleteSendsDel */
1114 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1115 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1116 break;
1117 case 1039: /* altSendsEscape */
1118 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1119 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1120 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001121 case 1049: /* rmcup/smcup, alternate screen */
1122 /* Ignore. Should be possible to implement,
1123 * but it's kind of annoying. */
1124 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001125 default:
1126 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1127 break;
1128 }
1129 } else {
1130 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001131 case 4: /* IRM */
1132 if (sr) terminal->mode |= MODE_IRM;
1133 else terminal->mode &= ~MODE_IRM;
1134 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001135 case 20: /* LNM */
1136 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1137 else terminal->mode &= ~MODE_LF_NEWLINE;
1138 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001139 default:
1140 fprintf(stderr, "Unknown parameter: %d\n", code);
1141 break;
1142 }
1143 }
1144}
1145
1146static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001147handle_dcs(struct terminal *terminal)
1148{
1149}
1150
1151static void
1152handle_osc(struct terminal *terminal)
1153{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001154 char *p;
1155 int code;
1156
1157 terminal->escape[terminal->escape_length++] = '\0';
1158 p = &terminal->escape[2];
1159 code = strtol(p, &p, 10);
1160 if (*p == ';') p++;
1161
1162 switch (code) {
1163 case 0: /* Icon name and window title */
1164 case 1: /* Icon label */
1165 case 2: /* Window title*/
1166 window_set_title(terminal->window, p);
1167 break;
1168 default:
1169 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1170 break;
1171 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001172}
1173
1174static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001175handle_escape(struct terminal *terminal)
1176{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001177 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001178 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001179 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001180 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001181 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001182 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001183 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001184
1185 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001186 i = 0;
1187 p = &terminal->escape[2];
1188 while ((isdigit(*p) || *p == ';') && i < 10) {
1189 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001190 if (!set[i]) {
1191 args[i] = 0;
1192 set[i] = 1;
1193 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001194 p++;
1195 i++;
1196 } else {
1197 args[i] = strtol(p, &p, 10);
1198 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001199 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001200 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001201
1202 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001203 case '@': /* ICH */
1204 count = set[0] ? args[0] : 1;
1205 if (count == 0) count = 1;
1206 terminal_shift_line(terminal, count);
1207 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001208 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001209 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001210 if (count == 0) count = 1;
1211 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001212 terminal->row -= count;
1213 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001214 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001215 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001216 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001217 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001218 if (count == 0) count = 1;
1219 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001220 terminal->row += count;
1221 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001222 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001223 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001224 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001225 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001226 if (count == 0) count = 1;
1227 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001228 terminal->column += count;
1229 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001230 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001231 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001232 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001233 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001234 if (count == 0) count = 1;
1235 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001236 terminal->column -= count;
1237 else
1238 terminal->column = 0;
1239 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001240 case 'E': /* CNL */
1241 count = set[0] ? args[0] : 1;
1242 if (terminal->row + count <= terminal->margin_bottom)
1243 terminal->row += count;
1244 else
1245 terminal->row = terminal->margin_bottom;
1246 terminal->column = 0;
1247 break;
1248 case 'F': /* CPL */
1249 count = set[0] ? args[0] : 1;
1250 if (terminal->row - count >= terminal->margin_top)
1251 terminal->row -= count;
1252 else
1253 terminal->row = terminal->margin_top;
1254 terminal->column = 0;
1255 break;
1256 case 'G': /* CHA */
1257 y = set[0] ? args[0] : 1;
1258 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1259
1260 terminal->column = y - 1;
1261 break;
1262 case 'f': /* HVP */
1263 case 'H': /* CUP */
1264 x = (set[1] ? args[1] : 1) - 1;
1265 x = x < 0 ? 0 :
1266 (x >= terminal->width ? terminal->width - 1 : x);
1267
1268 y = (set[0] ? args[0] : 1) - 1;
1269 if (terminal->origin_mode) {
1270 y += terminal->margin_top;
1271 y = y < terminal->margin_top ? terminal->margin_top :
1272 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1273 } else {
1274 y = y < 0 ? 0 :
1275 (y >= terminal->height ? terminal->height - 1 : y);
1276 }
1277
1278 terminal->row = y;
1279 terminal->column = x;
1280 break;
1281 case 'I': /* CHT */
1282 count = set[0] ? args[0] : 1;
1283 if (count == 0) count = 1;
1284 while (count > 0 && terminal->column < terminal->width) {
1285 if (terminal->tab_ruler[terminal->column]) count--;
1286 terminal->column++;
1287 }
1288 terminal->column--;
1289 break;
1290 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001291 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001292 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001293 if (!set[0] || args[0] == 0 || args[0] > 2) {
1294 memset(&row[terminal->column],
1295 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1296 attr_init(&attr_row[terminal->column],
1297 terminal->curr_attr, terminal->width - terminal->column);
1298 for (i = terminal->row + 1; i < terminal->height; i++) {
1299 memset(terminal_get_row(terminal, i),
1300 0, terminal->data_pitch);
1301 attr_init(terminal_get_attr_row(terminal, i),
1302 terminal->curr_attr, terminal->width);
1303 }
1304 } else if (args[0] == 1) {
1305 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1306 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1307 for (i = 0; i < terminal->row; i++) {
1308 memset(terminal_get_row(terminal, i),
1309 0, terminal->data_pitch);
1310 attr_init(terminal_get_attr_row(terminal, i),
1311 terminal->curr_attr, terminal->width);
1312 }
1313 } else if (args[0] == 2) {
1314 for (i = 0; i < terminal->height; i++) {
1315 memset(terminal_get_row(terminal, i),
1316 0, terminal->data_pitch);
1317 attr_init(terminal_get_attr_row(terminal, i),
1318 terminal->curr_attr, terminal->width);
1319 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001320 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001321 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001322 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001323 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001324 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001325 if (!set[0] || args[0] == 0 || args[0] > 2) {
1326 memset(&row[terminal->column], 0,
1327 (terminal->width - terminal->column) * sizeof(union utf8_char));
1328 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1329 terminal->width - terminal->column);
1330 } else if (args[0] == 1) {
1331 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1332 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1333 } else if (args[0] == 2) {
1334 memset(row, 0, terminal->data_pitch);
1335 attr_init(attr_row, terminal->curr_attr, terminal->width);
1336 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001337 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001338 case 'L': /* IL */
1339 count = set[0] ? args[0] : 1;
1340 if (count == 0) count = 1;
1341 if (terminal->row >= terminal->margin_top &&
1342 terminal->row < terminal->margin_bottom)
1343 {
1344 top = terminal->margin_top;
1345 terminal->margin_top = terminal->row;
1346 terminal_scroll(terminal, 0 - count);
1347 terminal->margin_top = top;
1348 } else if (terminal->row == terminal->margin_bottom) {
1349 memset(terminal_get_row(terminal, terminal->row),
1350 0, terminal->data_pitch);
1351 attr_init(terminal_get_attr_row(terminal, terminal->row),
1352 terminal->curr_attr, terminal->width);
1353 }
1354 break;
1355 case 'M': /* DL */
1356 count = set[0] ? args[0] : 1;
1357 if (count == 0) count = 1;
1358 if (terminal->row >= terminal->margin_top &&
1359 terminal->row < terminal->margin_bottom)
1360 {
1361 top = terminal->margin_top;
1362 terminal->margin_top = terminal->row;
1363 terminal_scroll(terminal, count);
1364 terminal->margin_top = top;
1365 } else if (terminal->row == terminal->margin_bottom) {
1366 memset(terminal_get_row(terminal, terminal->row),
1367 0, terminal->data_pitch);
1368 }
1369 break;
1370 case 'P': /* DCH */
1371 count = set[0] ? args[0] : 1;
1372 if (count == 0) count = 1;
1373 terminal_shift_line(terminal, 0 - count);
1374 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001375 case 'S': /* SU */
1376 terminal_scroll(terminal, set[0] ? args[0] : 1);
1377 break;
1378 case 'T': /* SD */
1379 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1380 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001381 case 'X': /* ECH */
1382 count = set[0] ? args[0] : 1;
1383 if (count == 0) count = 1;
1384 if ((terminal->column + count) > terminal->width)
1385 count = terminal->width - terminal->column;
1386 row = terminal_get_row(terminal, terminal->row);
1387 attr_row = terminal_get_attr_row(terminal, terminal->row);
1388 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1389 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1390 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001391 case 'Z': /* CBT */
1392 count = set[0] ? args[0] : 1;
1393 if (count == 0) count = 1;
1394 while (count > 0 && terminal->column >= 0) {
1395 if (terminal->tab_ruler[terminal->column]) count--;
1396 terminal->column--;
1397 }
1398 terminal->column++;
1399 break;
1400 case '`': /* HPA */
1401 y = set[0] ? args[0] : 1;
1402 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1403
1404 terminal->column = y - 1;
1405 break;
1406 case 'b': /* REP */
1407 count = set[0] ? args[0] : 1;
1408 if (count == 0) count = 1;
1409 if (terminal->last_char.byte[0])
1410 for (i = 0; i < count; i++)
1411 handle_char(terminal, terminal->last_char);
1412 terminal->last_char.byte[0] = 0;
1413 break;
1414 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001415 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001416 break;
1417 case 'd': /* VPA */
1418 x = set[0] ? args[0] : 1;
1419 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1420
1421 terminal->row = x - 1;
1422 break;
1423 case 'g': /* TBC */
1424 if (!set[0] || args[0] == 0) {
1425 terminal->tab_ruler[terminal->column] = 0;
1426 } else if (args[0] == 3) {
1427 memset(terminal->tab_ruler, 0, terminal->width);
1428 }
1429 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001430 case 'h': /* SM */
1431 for(i = 0; i < 10 && set[i]; i++) {
1432 handle_term_parameter(terminal, args[i], 1);
1433 }
1434 break;
1435 case 'l': /* RM */
1436 for(i = 0; i < 10 && set[i]; i++) {
1437 handle_term_parameter(terminal, args[i], 0);
1438 }
1439 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001440 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001441 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001442 if (i <= 7 && set[i] && set[i + 1] &&
1443 set[i + 2] && args[i + 1] == 5)
1444 {
1445 if (args[i] == 38) {
1446 handle_sgr(terminal, args[i + 2] + 256);
1447 break;
1448 } else if (args[i] == 48) {
1449 handle_sgr(terminal, args[i + 2] + 512);
1450 break;
1451 }
1452 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001453 if(set[i]) {
1454 handle_sgr(terminal, args[i]);
1455 } else if(i == 0) {
1456 handle_sgr(terminal, 0);
1457 break;
1458 } else {
1459 break;
1460 }
1461 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001462 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001463 case 'n': /* DSR */
1464 i = set[0] ? args[0] : 0;
1465 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001466 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001467 } else if (i == 6) {
1468 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1469 terminal->origin_mode ?
1470 terminal->row+terminal->margin_top : terminal->row+1,
1471 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001472 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001473 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001474 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001475 case 'r':
1476 if(!set[0]) {
1477 terminal->margin_top = 0;
1478 terminal->margin_bottom = terminal->height-1;
1479 terminal->row = 0;
1480 terminal->column = 0;
1481 } else {
1482 top = (set[0] ? args[0] : 1) - 1;
1483 top = top < 0 ? 0 :
1484 (top >= terminal->height ? terminal->height - 1 : top);
1485 bottom = (set[1] ? args[1] : 1) - 1;
1486 bottom = bottom < 0 ? 0 :
1487 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1488 if(bottom > top) {
1489 terminal->margin_top = top;
1490 terminal->margin_bottom = bottom;
1491 } else {
1492 terminal->margin_top = 0;
1493 terminal->margin_bottom = terminal->height-1;
1494 }
1495 if(terminal->origin_mode)
1496 terminal->row = terminal->margin_top;
1497 else
1498 terminal->row = 0;
1499 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001500 }
1501 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001502 case 's':
1503 terminal->saved_row = terminal->row;
1504 terminal->saved_column = terminal->column;
1505 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001506 case 't': /* windowOps */
1507 if (!set[0]) break;
1508 switch (args[0]) {
1509 case 4: /* resize px */
1510 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001511 widget_schedule_resize(terminal->widget,
1512 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001513 }
1514 break;
1515 case 8: /* resize ch */
1516 if (set[1] && set[2]) {
1517 terminal_resize(terminal, args[2], args[1]);
1518 }
1519 break;
1520 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001521 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001522 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1523 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001524 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001525 break;
1526 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001527 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001528 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1529 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001530 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001531 break;
1532 case 18: /* report ch */
1533 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1534 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001535 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001536 break;
1537 case 21: /* report title */
1538 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1539 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001540 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001541 break;
1542 default:
1543 if (args[0] >= 24)
1544 terminal_resize(terminal, terminal->width, args[0]);
1545 else
1546 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1547 break;
1548 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001549 case 'u':
1550 terminal->row = terminal->saved_row;
1551 terminal->column = terminal->saved_column;
1552 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001553 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001554 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001555 break;
1556 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001557}
1558
1559static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001560handle_non_csi_escape(struct terminal *terminal, char code)
1561{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001562 switch(code) {
1563 case 'M': /* RI */
1564 terminal->row -= 1;
1565 if(terminal->row < terminal->margin_top) {
1566 terminal->row = terminal->margin_top;
1567 terminal_scroll(terminal, -1);
1568 }
1569 break;
1570 case 'E': /* NEL */
1571 terminal->column = 0;
1572 // fallthrough
1573 case 'D': /* IND */
1574 terminal->row += 1;
1575 if(terminal->row > terminal->margin_bottom) {
1576 terminal->row = terminal->margin_bottom;
1577 terminal_scroll(terminal, +1);
1578 }
1579 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001580 case 'c': /* RIS */
1581 terminal_init(terminal);
1582 break;
1583 case 'H': /* HTS */
1584 terminal->tab_ruler[terminal->column] = 1;
1585 break;
1586 case '7': /* DECSC */
1587 terminal->saved_row = terminal->row;
1588 terminal->saved_column = terminal->column;
1589 terminal->saved_attr = terminal->curr_attr;
1590 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001591 terminal->saved_cs = terminal->cs;
1592 terminal->saved_g0 = terminal->g0;
1593 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001594 break;
1595 case '8': /* DECRC */
1596 terminal->row = terminal->saved_row;
1597 terminal->column = terminal->saved_column;
1598 terminal->curr_attr = terminal->saved_attr;
1599 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001600 terminal->cs = terminal->saved_cs;
1601 terminal->g0 = terminal->saved_g0;
1602 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001603 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001604 case '=': /* DECPAM */
1605 terminal->key_mode = KM_APPLICATION;
1606 break;
1607 case '>': /* DECPNM */
1608 terminal->key_mode = KM_NORMAL;
1609 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001610 default:
1611 fprintf(stderr, "Unknown escape code: %c\n", code);
1612 break;
1613 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001614}
1615
1616static void
1617handle_special_escape(struct terminal *terminal, char special, char code)
1618{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001619 int i, numChars;
1620
1621 if (special == '#') {
1622 switch(code) {
1623 case '8':
1624 /* fill with 'E', no cheap way to do this */
1625 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1626 numChars = terminal->width * terminal->height;
1627 for(i = 0; i < numChars; i++) {
1628 terminal->data[i].byte[0] = 'E';
1629 }
1630 break;
1631 default:
1632 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1633 break;
1634 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001635 } else if (special == '(' || special == ')') {
1636 switch(code) {
1637 case '0':
1638 if (special == '(')
1639 terminal->g0 = CS_SPECIAL;
1640 else
1641 terminal->g1 = CS_SPECIAL;
1642 break;
1643 case 'A':
1644 if (special == '(')
1645 terminal->g0 = CS_UK;
1646 else
1647 terminal->g1 = CS_UK;
1648 break;
1649 case 'B':
1650 if (special == '(')
1651 terminal->g0 = CS_US;
1652 else
1653 terminal->g1 = CS_US;
1654 break;
1655 default:
1656 fprintf(stderr, "Unknown character set %c\n", code);
1657 break;
1658 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001659 } else {
1660 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1661 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001662}
1663
1664static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001665handle_sgr(struct terminal *terminal, int code)
1666{
1667 switch(code) {
1668 case 0:
1669 terminal->curr_attr = terminal->color_scheme->default_attr;
1670 break;
1671 case 1:
1672 terminal->curr_attr.a |= ATTRMASK_BOLD;
1673 if (terminal->curr_attr.fg < 8)
1674 terminal->curr_attr.fg += 8;
1675 break;
1676 case 4:
1677 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1678 break;
1679 case 5:
1680 terminal->curr_attr.a |= ATTRMASK_BLINK;
1681 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001682 case 8:
1683 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1684 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001685 case 2:
1686 case 21:
1687 case 22:
1688 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1689 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1690 terminal->curr_attr.fg -= 8;
1691 break;
1692 case 24:
1693 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1694 break;
1695 case 25:
1696 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1697 break;
1698 case 7:
1699 case 26:
1700 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1701 break;
1702 case 27:
1703 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1704 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001705 case 28:
1706 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1707 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001708 case 39:
1709 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1710 break;
1711 case 49:
1712 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1713 break;
1714 default:
1715 if(code >= 30 && code <= 37) {
1716 terminal->curr_attr.fg = code - 30;
1717 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1718 terminal->curr_attr.fg += 8;
1719 } else if(code >= 40 && code <= 47) {
1720 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001721 } else if (code >= 90 && code <= 97) {
1722 terminal->curr_attr.fg = code - 90 + 8;
1723 } else if (code >= 100 && code <= 107) {
1724 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001725 } else if(code >= 256 && code < 512) {
1726 terminal->curr_attr.fg = code - 256;
1727 } else if(code >= 512 && code < 768) {
1728 terminal->curr_attr.bg = code - 512;
1729 } else {
1730 fprintf(stderr, "Unknown SGR code: %d\n", code);
1731 }
1732 break;
1733 }
1734}
1735
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001736/* Returns 1 if c was special, otherwise 0 */
1737static int
1738handle_special_char(struct terminal *terminal, char c)
1739{
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001740 union utf8_char *row;
1741 struct attr *attr_row;
1742
1743 row = terminal_get_row(terminal, terminal->row);
1744 attr_row = terminal_get_attr_row(terminal, terminal->row);
1745
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001746 switch(c) {
1747 case '\r':
1748 terminal->column = 0;
1749 break;
1750 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001751 if (terminal->mode & MODE_LF_NEWLINE) {
1752 terminal->column = 0;
1753 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001754 /* fallthrough */
1755 case '\v':
1756 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001757 terminal->row++;
1758 if(terminal->row > terminal->margin_bottom) {
1759 terminal->row = terminal->margin_bottom;
1760 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001761 }
1762
1763 break;
1764 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001765 while (terminal->column < terminal->width) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001766 if (terminal->mode & MODE_IRM)
1767 terminal_shift_line(terminal, +1);
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001768
1769 if (row[terminal->column].byte[0] == '\0') {
1770 row[terminal->column].byte[0] = ' ';
1771 row[terminal->column].byte[1] = '\0';
1772 attr_row[terminal->column] = terminal->curr_attr;
1773 }
1774
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001775 terminal->column++;
Kristian Høgsbergcca3c2f2012-06-20 15:56:13 -04001776 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001777 }
1778 if (terminal->column >= terminal->width) {
1779 terminal->column = terminal->width - 1;
1780 }
1781
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001782 break;
1783 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001784 if (terminal->column >= terminal->width) {
1785 terminal->column = terminal->width - 2;
1786 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001787 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001788 } else if (terminal->mode & MODE_AUTOWRAP) {
1789 terminal->column = terminal->width - 1;
1790 terminal->row -= 1;
1791 if (terminal->row < terminal->margin_top) {
1792 terminal->row = terminal->margin_top;
1793 terminal_scroll(terminal, -1);
1794 }
1795 }
1796
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001797 break;
1798 case '\a':
1799 /* Bell */
1800 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001801 case '\x0E': /* SO */
1802 terminal->cs = terminal->g1;
1803 break;
1804 case '\x0F': /* SI */
1805 terminal->cs = terminal->g0;
1806 break;
Kristian Høgsberg2a1aa4e2012-08-03 09:37:05 -04001807 case '\0':
1808 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001809 default:
1810 return 0;
1811 }
1812
1813 return 1;
1814}
1815
1816static void
1817handle_char(struct terminal *terminal, union utf8_char utf8)
1818{
1819 union utf8_char *row;
1820 struct attr *attr_row;
1821
1822 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001823
1824 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001825
1826 /* There are a whole lot of non-characters, control codes,
1827 * and formatting codes that should probably be ignored,
1828 * for example: */
1829 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1830 /* BOM, ignore */
1831 return;
1832 }
1833
1834 /* Some of these non-characters should be translated, e.g.: */
1835 if (utf8.byte[0] < 32) {
1836 utf8.byte[0] = utf8.byte[0] + 64;
1837 }
1838
1839 /* handle right margin effects */
1840 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001841 if (terminal->mode & MODE_AUTOWRAP) {
1842 terminal->column = 0;
1843 terminal->row += 1;
1844 if (terminal->row > terminal->margin_bottom) {
1845 terminal->row = terminal->margin_bottom;
1846 terminal_scroll(terminal, +1);
1847 }
1848 } else {
1849 terminal->column--;
1850 }
1851 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001852
1853 row = terminal_get_row(terminal, terminal->row);
1854 attr_row = terminal_get_attr_row(terminal, terminal->row);
1855
Callum Lowcay69e96582011-01-07 19:47:00 +00001856 if (terminal->mode & MODE_IRM)
1857 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001858 row[terminal->column] = utf8;
1859 attr_row[terminal->column++] = terminal->curr_attr;
1860
1861 if (utf8.ch != terminal->last_char.ch)
1862 terminal->last_char = utf8;
1863}
1864
Callum Lowcay30eeae52011-01-07 19:46:55 +00001865static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001866escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1867{
1868 int len, i;
1869
1870 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1871 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1872 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1873 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1874 else len = 1; /* Invalid, cannot happen */
1875
1876 if (terminal->escape_length + len <= MAX_ESCAPE) {
1877 for (i = 0; i < len; i++)
1878 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1879 terminal->escape_length += len;
1880 } else if (terminal->escape_length < MAX_ESCAPE) {
1881 terminal->escape[terminal->escape_length++] = 0;
1882 }
1883}
1884
1885static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001886terminal_data(struct terminal *terminal, const char *data, size_t length)
1887{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001888 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001889 union utf8_char utf8;
1890 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001891
1892 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001893 parser_state =
1894 utf8_next_char(&terminal->state_machine, data[i]);
1895 switch(parser_state) {
1896 case utf8state_accept:
1897 utf8.ch = terminal->state_machine.s.ch;
1898 break;
1899 case utf8state_reject:
1900 /* the unicode replacement character */
1901 utf8.byte[0] = 0xEF;
1902 utf8.byte[1] = 0xBF;
1903 utf8.byte[2] = 0xBD;
1904 utf8.byte[3] = 0x00;
1905 break;
1906 default:
1907 continue;
1908 }
1909
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001910 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001911 switch (terminal->state) {
1912 case escape_state_escape:
1913 escape_append_utf8(terminal, utf8);
1914 switch (utf8.byte[0]) {
1915 case 'P': /* DCS */
1916 terminal->state = escape_state_dcs;
1917 break;
1918 case '[': /* CSI */
1919 terminal->state = escape_state_csi;
1920 break;
1921 case ']': /* OSC */
1922 terminal->state = escape_state_osc;
1923 break;
1924 case '#':
1925 case '(':
1926 case ')': /* special */
1927 terminal->state = escape_state_special;
1928 break;
1929 case '^': /* PM (not implemented) */
1930 case '_': /* APC (not implemented) */
1931 terminal->state = escape_state_ignore;
1932 break;
1933 default:
1934 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001935 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13001936 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001937 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001938 continue;
1939 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001940 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1941 /* do nothing */
1942 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001943 terminal->escape_flags |= ESC_FLAG_WHAT;
1944 } else if (utf8.byte[0] == '>') {
1945 terminal->escape_flags |= ESC_FLAG_GT;
1946 } else if (utf8.byte[0] == '!') {
1947 terminal->escape_flags |= ESC_FLAG_BANG;
1948 } else if (utf8.byte[0] == '$') {
1949 terminal->escape_flags |= ESC_FLAG_CASH;
1950 } else if (utf8.byte[0] == '\'') {
1951 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1952 } else if (utf8.byte[0] == '"') {
1953 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1954 } else if (utf8.byte[0] == ' ') {
1955 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001956 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001957 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001958 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13001959 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001960 }
1961
1962 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1963 utf8.byte[0] == '`')
1964 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001965 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001966 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001967 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001968 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001969 continue;
1970 case escape_state_inner_escape:
1971 if (utf8.byte[0] == '\\') {
1972 terminal->state = escape_state_normal;
1973 if (terminal->outer_state == escape_state_dcs) {
1974 handle_dcs(terminal);
1975 } else if (terminal->outer_state == escape_state_osc) {
1976 handle_osc(terminal);
1977 }
1978 } else if (utf8.byte[0] == '\e') {
1979 terminal->state = terminal->outer_state;
1980 escape_append_utf8(terminal, utf8);
1981 if (terminal->escape_length >= MAX_ESCAPE)
1982 terminal->state = escape_state_normal;
1983 } else {
1984 terminal->state = terminal->outer_state;
1985 if (terminal->escape_length < MAX_ESCAPE)
1986 terminal->escape[terminal->escape_length++] = '\e';
1987 escape_append_utf8(terminal, utf8);
1988 if (terminal->escape_length >= MAX_ESCAPE)
1989 terminal->state = escape_state_normal;
1990 }
1991 continue;
1992 case escape_state_dcs:
1993 case escape_state_osc:
1994 case escape_state_ignore:
1995 if (utf8.byte[0] == '\e') {
1996 terminal->outer_state = terminal->state;
1997 terminal->state = escape_state_inner_escape;
1998 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
1999 terminal->state = escape_state_normal;
2000 handle_osc(terminal);
2001 } else {
2002 escape_append_utf8(terminal, utf8);
2003 if (terminal->escape_length >= MAX_ESCAPE)
2004 terminal->state = escape_state_normal;
2005 }
2006 continue;
2007 case escape_state_special:
2008 escape_append_utf8(terminal, utf8);
2009 terminal->state = escape_state_normal;
2010 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2011 handle_special_escape(terminal, terminal->escape[1],
2012 utf8.byte[0]);
2013 }
2014 continue;
2015 default:
2016 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002017 }
2018
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002019 /* this is valid, because ASCII characters are never used to
2020 * introduce a multibyte sequence in UTF-8 */
2021 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002022 terminal->state = escape_state_escape;
2023 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002024 terminal->escape[0] = '\e';
2025 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002026 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002027 } else {
2028 handle_char(terminal, utf8);
2029 } /* if */
2030 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002031
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002032 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002033}
2034
2035static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002036data_source_target(void *data,
2037 struct wl_data_source *source, const char *mime_type)
2038{
2039 fprintf(stderr, "data_source_target, %s\n", mime_type);
2040}
2041
2042static void
2043data_source_send(void *data,
2044 struct wl_data_source *source,
2045 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002046{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002047 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002048
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002049 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002050}
2051
2052static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002053data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002054{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002055 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002056}
2057
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002058static const struct wl_data_source_listener data_source_listener = {
2059 data_source_target,
2060 data_source_send,
2061 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002062};
2063
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002064static void
2065fullscreen_handler(struct window *window, void *data)
2066{
2067 struct terminal *terminal = data;
2068
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002069 window_set_fullscreen(window, !window_is_fullscreen(terminal->window));
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002070}
2071
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002072static void
2073close_handler(struct window *window, void *data)
2074{
2075 struct terminal *terminal = data;
2076
2077 terminal_destroy(terminal);
2078}
2079
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002080static int
2081handle_bound_key(struct terminal *terminal,
2082 struct input *input, uint32_t sym, uint32_t time)
2083{
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002084 struct terminal *new_terminal;
2085
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002086 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002087 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002088 /* Cut selection; terminal doesn't do cut, fall
2089 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002090 case XKB_KEY_C:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002091 terminal->selection =
2092 display_create_data_source(terminal->display);
2093 wl_data_source_offer(terminal->selection,
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002094 "text/plain;charset=utf-8");
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002095 wl_data_source_add_listener(terminal->selection,
2096 &data_source_listener, terminal);
Kristian Høgsbergfee91be2012-06-02 21:21:41 -04002097 input_set_selection(input, terminal->selection,
2098 display_get_serial(terminal->display));
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002099 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002100 case XKB_KEY_V:
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002101 input_receive_selection_data_to_fd(input,
2102 "text/plain;charset=utf-8",
2103 terminal->master);
2104
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002105 return 1;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002106
2107 case XKB_KEY_N:
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002108 new_terminal = terminal_create(terminal->display);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002109 if (terminal_run(new_terminal, option_shell))
2110 terminal_destroy(new_terminal);
2111
2112 return 1;
2113
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002114 default:
2115 return 0;
2116 }
2117}
2118
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002119static void
2120key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002121 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2122 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002123{
2124 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002125 char ch[MAX_RESPONSE];
Kristian Høgsberg88fd4082012-06-21 15:55:03 -04002126 uint32_t modifiers, serial;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002127 int ret, len = 0;
2128 bool convert_utf8 = true;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002129
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002130 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002131 if ((modifiers & MOD_CONTROL_MASK) &&
2132 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002133 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2134 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002135 return;
2136
Daniel Stonea8468712012-11-07 17:51:35 +11002137 /* Map keypad symbols to 'normal' equivalents before processing */
2138 switch (sym) {
2139 case XKB_KEY_KP_Space:
2140 sym = XKB_KEY_space;
2141 break;
2142 case XKB_KEY_KP_Tab:
2143 sym = XKB_KEY_Tab;
2144 break;
2145 case XKB_KEY_KP_Enter:
2146 sym = XKB_KEY_Return;
2147 break;
2148 case XKB_KEY_KP_Left:
2149 sym = XKB_KEY_Left;
2150 break;
2151 case XKB_KEY_KP_Up:
2152 sym = XKB_KEY_Up;
2153 break;
2154 case XKB_KEY_KP_Right:
2155 sym = XKB_KEY_Right;
2156 break;
2157 case XKB_KEY_KP_Down:
2158 sym = XKB_KEY_Down;
2159 break;
2160 case XKB_KEY_KP_Equal:
2161 sym = XKB_KEY_equal;
2162 break;
2163 case XKB_KEY_KP_Multiply:
2164 sym = XKB_KEY_asterisk;
2165 break;
2166 case XKB_KEY_KP_Add:
2167 sym = XKB_KEY_plus;
2168 break;
2169 case XKB_KEY_KP_Separator:
2170 /* Note this is actually locale-dependent and should mostly be
2171 * a comma. But leave it as period until we one day start
2172 * doing the right thing. */
2173 sym = XKB_KEY_period;
2174 break;
2175 case XKB_KEY_KP_Subtract:
2176 sym = XKB_KEY_minus;
2177 break;
2178 case XKB_KEY_KP_Decimal:
2179 sym = XKB_KEY_period;
2180 break;
2181 case XKB_KEY_KP_Divide:
2182 sym = XKB_KEY_slash;
2183 break;
2184 case XKB_KEY_KP_0:
2185 case XKB_KEY_KP_1:
2186 case XKB_KEY_KP_2:
2187 case XKB_KEY_KP_3:
2188 case XKB_KEY_KP_4:
2189 case XKB_KEY_KP_5:
2190 case XKB_KEY_KP_6:
2191 case XKB_KEY_KP_7:
2192 case XKB_KEY_KP_8:
2193 case XKB_KEY_KP_9:
2194 sym = (sym - XKB_KEY_KP_0) + XKB_KEY_0;
2195 break;
2196 default:
2197 break;
2198 }
2199
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002200 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002201 case XKB_KEY_BackSpace:
Kristian Høgsbergb7f94bf2012-06-21 12:29:36 -04002202 if (modifiers & MOD_ALT_MASK)
2203 ch[len++] = 0x1b;
Kristian Høgsberg71a4cf42012-06-20 17:57:56 -04002204 ch[len++] = 0x7f;
2205 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002206 case XKB_KEY_Tab:
2207 case XKB_KEY_Linefeed:
2208 case XKB_KEY_Clear:
2209 case XKB_KEY_Pause:
2210 case XKB_KEY_Scroll_Lock:
2211 case XKB_KEY_Sys_Req:
2212 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002213 ch[len++] = sym & 0x7f;
2214 break;
2215
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002216 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002217 if (terminal->mode & MODE_LF_NEWLINE) {
2218 ch[len++] = 0x0D;
2219 ch[len++] = 0x0A;
2220 } else {
2221 ch[len++] = 0x0D;
2222 }
2223 break;
2224
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002225 case XKB_KEY_Shift_L:
2226 case XKB_KEY_Shift_R:
2227 case XKB_KEY_Control_L:
2228 case XKB_KEY_Control_R:
2229 case XKB_KEY_Alt_L:
2230 case XKB_KEY_Alt_R:
Kristian Høgsberg22fbcf72012-06-22 12:18:56 -04002231 case XKB_KEY_Meta_L:
2232 case XKB_KEY_Meta_R:
2233 case XKB_KEY_Super_L:
2234 case XKB_KEY_Super_R:
2235 case XKB_KEY_Hyper_L:
2236 case XKB_KEY_Hyper_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002237 break;
2238
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002239 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002240 len = function_key_response('[', 2, modifiers, '~', ch);
2241 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002242 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002243 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2244 ch[len++] = '\x04';
2245 } else {
2246 len = function_key_response('[', 3, modifiers, '~', ch);
2247 }
2248 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002249 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002250 len = function_key_response('[', 5, modifiers, '~', ch);
2251 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002252 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002253 len = function_key_response('[', 6, modifiers, '~', ch);
2254 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002255 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002256 len = function_key_response('O', 1, modifiers, 'P', ch);
2257 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002258 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002259 len = function_key_response('O', 1, modifiers, 'Q', ch);
2260 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002261 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002262 len = function_key_response('O', 1, modifiers, 'R', ch);
2263 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002264 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002265 len = function_key_response('O', 1, modifiers, 'S', ch);
2266 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002267 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002268 len = function_key_response('[', 15, modifiers, '~', ch);
2269 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002270 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002271 len = function_key_response('[', 17, modifiers, '~', ch);
2272 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002273 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002274 len = function_key_response('[', 18, modifiers, '~', ch);
2275 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002276 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002277 len = function_key_response('[', 19, modifiers, '~', ch);
2278 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002279 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002280 len = function_key_response('[', 20, modifiers, '~', ch);
2281 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002282 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002283 len = function_key_response('[', 21, modifiers, '~', ch);
2284 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002285 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002286 len = function_key_response('[', 24, modifiers, '~', ch);
2287 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002288 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002289 /* Handle special keys with alternate mappings */
2290 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2291 if (len != 0) break;
2292
Kristian Høgsberg70163132012-05-08 15:55:39 -04002293 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002294 if (sym >= '3' && sym <= '7')
2295 sym = (sym & 0x1f) + 8;
2296
2297 if (!((sym >= '!' && sym <= '/') ||
2298 (sym >= '8' && sym <= '?') ||
2299 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2300 else if (sym == '2') sym = 0x00;
2301 else if (sym == '/') sym = 0x1F;
2302 else if (sym == '8' || sym == '?') sym = 0x7F;
Kristian Høgsbergae9e0732012-06-21 12:30:15 -04002303 }
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002304 if (modifiers & MOD_ALT_MASK) {
2305 if (terminal->mode & MODE_ALT_SENDS_ESC) {
2306 ch[len++] = 0x1b;
2307 } else {
2308 sym = sym | 0x80;
2309 convert_utf8 = false;
2310 }
Callum Lowcay7e08e902011-01-07 19:47:02 +00002311 }
2312
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002313 if ((sym < 128) ||
2314 (!convert_utf8 && sym < 256)) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002315 ch[len++] = sym;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002316 } else {
2317 ret = xkb_keysym_to_utf8(sym, ch + len,
2318 MAX_RESPONSE - len);
2319 if (ret < 0)
2320 fprintf(stderr,
2321 "Warning: buffer too small to encode "
2322 "UTF8 character\n");
2323 else
2324 len += ret;
2325 }
2326
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002327 break;
2328 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002329
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002330 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04002331 terminal_write(terminal, ch, len);
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002332
2333 /* Hide cursor, except if this was coming from a
2334 * repeating key press. */
2335 serial = display_get_serial(terminal->display);
2336 if (terminal->hide_cursor_serial != serial) {
2337 input_set_pointer_image(input, CURSOR_BLANK);
2338 terminal->hide_cursor_serial = serial;
2339 }
2340 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002341}
2342
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002343static void
2344keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002345 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002346{
2347 struct terminal *terminal = data;
2348
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002349 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002350}
2351
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002352static int wordsep(int ch)
2353{
2354 const char extra[] = "-,./?%&#:_=+@~";
2355
Andre Heider552d12b2012-08-02 20:59:43 +02002356 if (ch > 127)
2357 return 1;
2358
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002359 return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
2360}
2361
2362static int
2363recompute_selection(struct terminal *terminal)
2364{
2365 struct rectangle allocation;
2366 int col, x, width, height;
2367 int start_row, end_row;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002368 int word_start, eol;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002369 int side_margin, top_margin;
2370 int start_x, end_x;
2371 int cw, ch;
2372 union utf8_char *data;
2373
2374 cw = terminal->extents.max_x_advance;
2375 ch = terminal->extents.height;
2376 widget_get_allocation(terminal->widget, &allocation);
2377 width = terminal->width * cw;
2378 height = terminal->height * ch;
2379 side_margin = allocation.x + (allocation.width - width) / 2;
2380 top_margin = allocation.y + (allocation.height - height) / 2;
2381
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002382 start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1;
2383 end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1;
2384
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002385 if (start_row < end_row ||
2386 (start_row == end_row &&
2387 terminal->selection_start_x < terminal->selection_end_x)) {
2388 terminal->selection_start_row = start_row;
2389 terminal->selection_end_row = end_row;
2390 start_x = terminal->selection_start_x;
2391 end_x = terminal->selection_end_x;
2392 } else {
2393 terminal->selection_start_row = end_row;
2394 terminal->selection_end_row = start_row;
2395 start_x = terminal->selection_end_x;
2396 end_x = terminal->selection_start_x;
2397 }
2398
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002399 eol = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002400 if (terminal->selection_start_row < 0) {
2401 terminal->selection_start_row = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002402 terminal->selection_start_col = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002403 } else {
2404 x = side_margin + cw / 2;
2405 data = terminal_get_row(terminal,
2406 terminal->selection_start_row);
2407 word_start = 0;
2408 for (col = 0; col < terminal->width; col++, x += cw) {
2409 if (col == 0 || wordsep(data[col - 1].ch))
2410 word_start = col;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002411 if (data[col].ch != 0)
2412 eol = col + 1;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002413 if (start_x < x)
2414 break;
2415 }
2416
2417 switch (terminal->dragging) {
2418 case SELECT_LINE:
2419 terminal->selection_start_col = 0;
2420 break;
2421 case SELECT_WORD:
2422 terminal->selection_start_col = word_start;
2423 break;
2424 case SELECT_CHAR:
2425 terminal->selection_start_col = col;
2426 break;
2427 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002428 }
2429
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002430 if (terminal->selection_end_row >= terminal->height) {
2431 terminal->selection_end_row = terminal->height;
2432 terminal->selection_end_col = 0;
2433 } else {
2434 x = side_margin + cw / 2;
2435 data = terminal_get_row(terminal, terminal->selection_end_row);
2436 for (col = 0; col < terminal->width; col++, x += cw) {
2437 if (terminal->dragging == SELECT_CHAR && end_x < x)
2438 break;
2439 if (terminal->dragging == SELECT_WORD &&
2440 end_x < x && wordsep(data[col].ch))
2441 break;
2442 }
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002443 terminal->selection_end_col = col;
2444 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002445
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002446 if (terminal->selection_end_col != terminal->selection_start_col ||
2447 terminal->selection_start_row != terminal->selection_end_row) {
2448 col = terminal->selection_end_col;
2449 if (col > 0 && data[col - 1].ch == 0)
2450 terminal->selection_end_col = terminal->width;
2451 data = terminal_get_row(terminal, terminal->selection_start_row);
2452 if (data[terminal->selection_start_col].ch == 0)
2453 terminal->selection_start_col = eol;
2454 }
2455
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002456 return 1;
2457}
2458
Kristian Høgsberg59826582011-01-20 11:56:57 -05002459static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002460button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002461 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002462 uint32_t button,
2463 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002464{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002465 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002466
2467 switch (button) {
2468 case 272:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002469 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002470
2471 if (time - terminal->button_time < 500)
2472 terminal->click_count++;
2473 else
2474 terminal->click_count = 1;
2475
2476 terminal->button_time = time;
2477 terminal->dragging =
2478 (terminal->click_count - 1) % 3 + SELECT_CHAR;
2479
Kristian Høgsberg59826582011-01-20 11:56:57 -05002480 input_get_position(input,
2481 &terminal->selection_start_x,
2482 &terminal->selection_start_y);
2483 terminal->selection_end_x = terminal->selection_start_x;
2484 terminal->selection_end_y = terminal->selection_start_y;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002485 if (recompute_selection(terminal))
2486 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002487 } else {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002488 terminal->dragging = SELECT_NONE;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002489 }
2490 break;
2491 }
2492}
2493
2494static int
Kristian Høgsberg29784402012-06-28 14:27:02 -04002495enter_handler(struct widget *widget,
2496 struct input *input, float x, float y, void *data)
2497{
2498 return CURSOR_IBEAM;
2499}
2500
2501static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002502motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002503 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002504 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002505{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002506 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002507
2508 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002509 input_get_position(input,
2510 &terminal->selection_end_x,
2511 &terminal->selection_end_y);
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002512
2513 if (recompute_selection(terminal))
2514 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002515 }
2516
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002517 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002518}
2519
Alexander Larssonde79dd02013-05-22 14:41:34 +02002520static void
2521output_handler(struct window *window, struct output *output, int enter,
2522 void *data)
2523{
2524 if (enter)
2525 window_set_buffer_transform(window, output_get_transform(output));
2526 window_set_buffer_scale(window, window_get_output_scale(window));
2527 window_schedule_redraw(window);
2528}
2529
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002530static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002531terminal_create(struct display *display)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002532{
2533 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002534 cairo_surface_t *surface;
2535 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002536
2537 terminal = malloc(sizeof *terminal);
2538 if (terminal == NULL)
2539 return terminal;
2540
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002541 memset(terminal, 0, sizeof *terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002542 terminal->color_scheme = &DEFAULT_COLORS;
2543 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002544 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002545 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002546 terminal->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002547 terminal->widget = frame_create(terminal->window, terminal);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002548 window_set_title(terminal->window, "Wayland Terminal");
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002549 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002550
2551 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002552 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002553
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002554 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002555 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002556
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002557 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002558 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002559 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002560 keyboard_focus_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002561 window_set_fullscreen_handler(terminal->window, fullscreen_handler);
Alexander Larssonde79dd02013-05-22 14:41:34 +02002562 window_set_output_handler(terminal->window, output_handler);
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002563 window_set_close_handler(terminal->window, close_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002564
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002565 widget_set_redraw_handler(terminal->widget, redraw_handler);
2566 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002567 widget_set_button_handler(terminal->widget, button_handler);
Kristian Høgsberg29784402012-06-28 14:27:02 -04002568 widget_set_enter_handler(terminal->widget, enter_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002569 widget_set_motion_handler(terminal->widget, motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002570
Kristian Høgsberg09531622010-06-14 23:22:15 -04002571 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2572 cr = cairo_create(surface);
Kristian Høgsberg38912df2012-06-27 17:52:23 -04002573 cairo_set_font_size(cr, option_font_size);
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002574 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002575 CAIRO_FONT_SLANT_NORMAL,
2576 CAIRO_FONT_WEIGHT_BOLD);
2577 terminal->font_bold = cairo_get_scaled_font (cr);
2578 cairo_scaled_font_reference(terminal->font_bold);
2579
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002580 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002581 CAIRO_FONT_SLANT_NORMAL,
2582 CAIRO_FONT_WEIGHT_NORMAL);
2583 terminal->font_normal = cairo_get_scaled_font (cr);
2584 cairo_scaled_font_reference(terminal->font_normal);
2585
Kristian Høgsberg09531622010-06-14 23:22:15 -04002586 cairo_font_extents(cr, &terminal->extents);
2587 cairo_destroy(cr);
2588 cairo_surface_destroy(surface);
2589
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002590 terminal_resize(terminal, 20, 5); /* Set minimum size first */
Kristian Høgsberga1627922012-06-20 17:30:03 -04002591 terminal_resize(terminal, 80, 25);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002592
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002593 wl_list_insert(terminal_list.prev, &terminal->link);
2594
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002595 return terminal;
2596}
2597
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002598static void
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002599terminal_destroy(struct terminal *terminal)
2600{
Dima Ryazanova85292e2012-11-29 00:27:09 -08002601 display_unwatch_fd(terminal->display, terminal->master);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002602 window_destroy(terminal->window);
2603 close(terminal->master);
2604 wl_list_remove(&terminal->link);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002605
2606 if (wl_list_empty(&terminal_list))
Dima Ryazanova85292e2012-11-29 00:27:09 -08002607 display_exit(terminal->display);
2608
2609 free(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002610}
2611
2612static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002613io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002614{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002615 struct terminal *terminal =
2616 container_of(task, struct terminal, io_task);
2617 char buffer[256];
2618 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002619
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002620 if (events & EPOLLHUP) {
2621 terminal_destroy(terminal);
2622 return;
2623 }
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002624
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002625 len = read(terminal->master, buffer, sizeof buffer);
2626 if (len < 0)
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002627 terminal_destroy(terminal);
2628 else
2629 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002630}
2631
2632static int
2633terminal_run(struct terminal *terminal, const char *path)
2634{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002635 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002636 pid_t pid;
2637
2638 pid = forkpty(&master, NULL, NULL, NULL);
2639 if (pid == 0) {
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04002640 setenv("TERM", option_term, 1);
2641 setenv("COLORTERM", option_term, 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002642 if (execl(path, path, NULL)) {
2643 printf("exec failed: %m\n");
2644 exit(EXIT_FAILURE);
2645 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002646 } else if (pid < 0) {
2647 fprintf(stderr, "failed to fork and create pty (%m).\n");
2648 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002649 }
2650
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002651 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002652 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002653 terminal->io_task.run = io_handler;
2654 display_watch_fd(terminal->display, terminal->master,
2655 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002656
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002657 window_set_fullscreen(terminal->window, option_fullscreen);
2658 if (!window_is_fullscreen(terminal->window))
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002659 terminal_resize(terminal, 80, 24);
2660
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002661 return 0;
2662}
2663
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002664static const struct config_key terminal_config_keys[] = {
2665 { "font", CONFIG_KEY_STRING, &option_font },
Kristian Høgsberg38912df2012-06-27 17:52:23 -04002666 { "font-size", CONFIG_KEY_INTEGER, &option_font_size },
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04002667 { "term", CONFIG_KEY_STRING, &option_term },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002668};
2669
2670static const struct config_section config_sections[] = {
2671 { "terminal",
2672 terminal_config_keys, ARRAY_LENGTH(terminal_config_keys) },
2673};
2674
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002675static const struct weston_option terminal_options[] = {
2676 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002677 { WESTON_OPTION_STRING, "font", 0, &option_font },
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002678 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002679};
2680
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002681int main(int argc, char *argv[])
2682{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002683 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002684 struct terminal *terminal;
Ossama Othmana50e6e42013-05-14 09:48:26 -07002685 int config_fd;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002686
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002687 option_shell = getenv("SHELL");
2688 if (!option_shell)
2689 option_shell = "/bin/bash";
2690
Ossama Othmana50e6e42013-05-14 09:48:26 -07002691 config_fd = open_config_file("weston.ini");
2692 parse_config_file(config_fd,
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002693 config_sections, ARRAY_LENGTH(config_sections),
2694 NULL);
Ossama Othmana50e6e42013-05-14 09:48:26 -07002695 close(config_fd);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002696
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002697 parse_options(terminal_options,
2698 ARRAY_LENGTH(terminal_options), &argc, argv);
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002699
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002700 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002701 if (d == NULL) {
2702 fprintf(stderr, "failed to create display: %m\n");
2703 return -1;
2704 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002705
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002706 wl_list_init(&terminal_list);
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002707 terminal = terminal_create(d);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002708 if (terminal_run(terminal, option_shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002709 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002710
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002711 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002712
2713 return 0;
2714}