blob: 31bcedd427273645c28c157cb12bd93d067cf3fe [file] [log] [blame]
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +020023#include <stdbool.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050024#include <stdint.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <fcntl.h>
29#include <unistd.h>
30#include <math.h>
31#include <time.h>
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050032#include <pty.h>
Kristian Høgsbergf04e8382008-12-08 00:07:49 -050033#include <ctype.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050034#include <cairo.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040035#include <sys/epoll.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050036
Pekka Paalanen50719bc2011-11-22 14:18:50 +020037#include <wayland-client.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050038
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -040039#include "../shared/config-parser.h"
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050040#include "window.h"
41
Kristian Høgsberg0395f302008-12-22 12:14:50 -050042static int option_fullscreen;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -040043static char *option_font = "mono";
Kristian Høgsberg38912df2012-06-27 17:52:23 -040044static int option_font_size = 14;
Kristian Høgsbergde845cf2012-06-20 22:14:31 -040045static char *option_term = "xterm";
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040046static char *option_shell;
47
48static struct wl_list terminal_list;
49
50static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -040051terminal_create(struct display *display);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040052static void
53terminal_destroy(struct terminal *terminal);
54static int
55terminal_run(struct terminal *terminal, const char *path);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050056
Peng Wuf291f202013-06-06 15:32:41 +080057#define TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS \
58 " !\"#$%&'()*+,-./" \
59 "0123456789" \
60 ":;<=>?@" \
61 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
62 "[\\]^_`" \
63 "abcdefghijklmnopqrstuvwxyz" \
64 "{|}~" \
65 ""
66
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050067#define MOD_SHIFT 0x01
68#define MOD_ALT 0x02
69#define MOD_CTRL 0x04
70
Callum Lowcay30eeae52011-01-07 19:46:55 +000071#define ATTRMASK_BOLD 0x01
72#define ATTRMASK_UNDERLINE 0x02
73#define ATTRMASK_BLINK 0x04
74#define ATTRMASK_INVERSE 0x08
Callum Lowcay81179db2011-01-10 12:14:01 +130075#define ATTRMASK_CONCEALED 0x10
Callum Lowcay30eeae52011-01-07 19:46:55 +000076
Callum Lowcayb8609ad2011-01-07 19:46:57 +000077/* Buffer sizes */
Callum Lowcayef57a9b2011-01-14 20:46:23 +130078#define MAX_RESPONSE 256
79#define MAX_ESCAPE 255
Callum Lowcayb8609ad2011-01-07 19:46:57 +000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +000081/* Terminal modes */
82#define MODE_SHOW_CURSOR 0x00000001
83#define MODE_INVERSE 0x00000002
84#define MODE_AUTOWRAP 0x00000004
85#define MODE_AUTOREPEAT 0x00000008
86#define MODE_LF_NEWLINE 0x00000010
Callum Lowcay69e96582011-01-07 19:47:00 +000087#define MODE_IRM 0x00000020
Callum Lowcay7e08e902011-01-07 19:47:02 +000088#define MODE_DELETE_SENDS_DEL 0x00000040
89#define MODE_ALT_SENDS_ESC 0x00000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +000090
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000091union utf8_char {
92 unsigned char byte[4];
93 uint32_t ch;
94};
95
96enum utf8_state {
97 utf8state_start,
98 utf8state_accept,
99 utf8state_reject,
100 utf8state_expect3,
101 utf8state_expect2,
102 utf8state_expect1
103};
104
105struct utf8_state_machine {
106 enum utf8_state state;
107 int len;
108 union utf8_char s;
109};
110
111static void
112init_state_machine(struct utf8_state_machine *machine)
113{
114 machine->state = utf8state_start;
115 machine->len = 0;
116 machine->s.ch = 0;
117}
118
119static enum utf8_state
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400120utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000121{
122 switch(machine->state) {
123 case utf8state_start:
124 case utf8state_accept:
125 case utf8state_reject:
126 machine->s.ch = 0;
127 machine->len = 0;
128 if(c == 0xC0 || c == 0xC1) {
129 /* overlong encoding, reject */
130 machine->state = utf8state_reject;
131 } else if((c & 0x80) == 0) {
132 /* single byte, accept */
133 machine->s.byte[machine->len++] = c;
134 machine->state = utf8state_accept;
135 } else if((c & 0xC0) == 0x80) {
136 /* parser out of sync, ignore byte */
137 machine->state = utf8state_start;
138 } else if((c & 0xE0) == 0xC0) {
139 /* start of two byte sequence */
140 machine->s.byte[machine->len++] = c;
141 machine->state = utf8state_expect1;
142 } else if((c & 0xF0) == 0xE0) {
143 /* start of three byte sequence */
144 machine->s.byte[machine->len++] = c;
145 machine->state = utf8state_expect2;
146 } else if((c & 0xF8) == 0xF0) {
147 /* start of four byte sequence */
148 machine->s.byte[machine->len++] = c;
149 machine->state = utf8state_expect3;
150 } else {
151 /* overlong encoding, reject */
152 machine->state = utf8state_reject;
153 }
154 break;
155 case utf8state_expect3:
156 machine->s.byte[machine->len++] = c;
157 if((c & 0xC0) == 0x80) {
158 /* all good, continue */
159 machine->state = utf8state_expect2;
160 } else {
161 /* missing extra byte, reject */
162 machine->state = utf8state_reject;
163 }
164 break;
165 case utf8state_expect2:
166 machine->s.byte[machine->len++] = c;
167 if((c & 0xC0) == 0x80) {
168 /* all good, continue */
169 machine->state = utf8state_expect1;
170 } else {
171 /* missing extra byte, reject */
172 machine->state = utf8state_reject;
173 }
174 break;
175 case utf8state_expect1:
176 machine->s.byte[machine->len++] = c;
177 if((c & 0xC0) == 0x80) {
178 /* all good, accept */
179 machine->state = utf8state_accept;
180 } else {
181 /* missing extra byte, reject */
182 machine->state = utf8state_reject;
183 }
184 break;
185 default:
186 machine->state = utf8state_reject;
187 break;
188 }
189
190 return machine->state;
191}
192
Callum Lowcay256e72f2011-01-07 19:47:01 +0000193struct char_sub {
194 union utf8_char match;
195 union utf8_char replace;
196};
197/* Set last char_sub match to NULL char */
198typedef struct char_sub *character_set;
199
200struct char_sub CS_US[] = {
201 {{{0, }}, {{0, }}}
202};
203static struct char_sub CS_UK[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200204 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000205 {{{0, }}, {{0, }}}
206};
207static struct char_sub CS_SPECIAL[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200208 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond: ♦ */
209 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell: ▒ */
210 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT: ␉ */
211 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF: ␌ */
212 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR: ␍ */
213 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF: ␊ */
214 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree: ° */
215 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus: ± */
216 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL: ␤ */
217 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT: ␋ */
218 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB: ┘ */
219 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT: ┐ */
220 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT: ┌ */
221 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_LB: └ */
222 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS: ┼ */
223 {{{'o', 0, }}, {{0xE2, 0x8E, 0xBA, 0}}}, /* Horiz. Scan Line 1: ⎺ */
224 {{{'p', 0, }}, {{0xE2, 0x8E, 0xBB, 0}}}, /* Horiz. Scan Line 3: ⎻ */
225 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* Horiz. Scan Line 5: ─ */
226 {{{'r', 0, }}, {{0xE2, 0x8E, 0xBC, 0}}}, /* Horiz. Scan Line 7: ⎼ */
227 {{{'s', 0, }}, {{0xE2, 0x8E, 0xBD, 0}}}, /* Horiz. Scan Line 9: ⎽ */
228 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR: ├ */
229 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL: ┤ */
230 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU: ┴ */
231 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD: ┬ */
232 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V: │ */
233 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE: ≤ */
234 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE: ≥ */
235 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI: π */
236 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ: ≠ */
237 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
238 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT: ⋅ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000239 {{{0, }}, {{0, }}}
240};
241
242static void
243apply_char_set(character_set cs, union utf8_char *utf8)
244{
245 int i = 0;
246
247 while (cs[i].match.byte[0]) {
248 if ((*utf8).ch == cs[i].match.ch) {
249 *utf8 = cs[i].replace;
250 break;
251 }
252 i++;
253 }
254}
255
Callum Lowcay7e08e902011-01-07 19:47:02 +0000256struct key_map {
257 int sym;
258 int num;
259 char escape;
260 char code;
261};
262/* Set last key_sub sym to NULL */
263typedef struct key_map *keyboard_mode;
264
265static struct key_map KM_NORMAL[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400266 { XKB_KEY_Left, 1, '[', 'D' },
267 { XKB_KEY_Right, 1, '[', 'C' },
268 { XKB_KEY_Up, 1, '[', 'A' },
269 { XKB_KEY_Down, 1, '[', 'B' },
270 { XKB_KEY_Home, 1, '[', 'H' },
271 { XKB_KEY_End, 1, '[', 'F' },
272 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000273};
274static struct key_map KM_APPLICATION[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400275 { XKB_KEY_Left, 1, 'O', 'D' },
276 { XKB_KEY_Right, 1, 'O', 'C' },
277 { XKB_KEY_Up, 1, 'O', 'A' },
278 { XKB_KEY_Down, 1, 'O', 'B' },
279 { XKB_KEY_Home, 1, 'O', 'H' },
280 { XKB_KEY_End, 1, 'O', 'F' },
281 { XKB_KEY_KP_Enter, 1, 'O', 'M' },
282 { XKB_KEY_KP_Multiply, 1, 'O', 'j' },
283 { XKB_KEY_KP_Add, 1, 'O', 'k' },
284 { XKB_KEY_KP_Separator, 1, 'O', 'l' },
285 { XKB_KEY_KP_Subtract, 1, 'O', 'm' },
286 { XKB_KEY_KP_Divide, 1, 'O', 'o' },
287 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000288};
289
290static int
291function_key_response(char escape, int num, uint32_t modifiers,
292 char code, char *response)
293{
294 int mod_num = 0;
295 int len;
296
Kristian Høgsberg70163132012-05-08 15:55:39 -0400297 if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
298 if (modifiers & MOD_ALT_MASK) mod_num |= 2;
299 if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000300
301 if (mod_num != 0)
302 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
303 num, mod_num + 1, code);
304 else if (code != '~')
305 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
306 escape, code);
307 else
308 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
309 escape, num, code);
310
311 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
312 else return len;
313}
314
315/* returns the number of bytes written into response,
316 * which must have room for MAX_RESPONSE bytes */
317static int
318apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
319{
320 struct key_map map;
321 int len = 0;
322 int i = 0;
323
324 while (mode[i].sym) {
325 map = mode[i++];
326 if (sym == map.sym) {
327 len = function_key_response(map.escape, map.num,
328 modifiers, map.code,
329 response);
330 break;
331 }
332 }
333
334 return len;
335}
336
Callum Lowcay30eeae52011-01-07 19:46:55 +0000337struct terminal_color { double r, g, b, a; };
338struct attr {
339 unsigned char fg, bg;
340 char a; /* attributes format:
341 * 76543210
Callum Lowcay81179db2011-01-10 12:14:01 +1300342 * cilub */
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500343 char s; /* in selection */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000344};
345struct color_scheme {
346 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500347 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000348 struct attr default_attr;
349};
350
351static void
352attr_init(struct attr *data_attr, struct attr attr, int n)
353{
354 int i;
355 for (i = 0; i < n; i++) {
356 data_attr[i] = attr;
357 }
358}
359
Callum Lowcay67a201d2011-01-12 19:23:41 +1300360enum escape_state {
361 escape_state_normal = 0,
362 escape_state_escape,
363 escape_state_dcs,
364 escape_state_csi,
365 escape_state_osc,
366 escape_state_inner_escape,
367 escape_state_ignore,
368 escape_state_special
369};
370
371#define ESC_FLAG_WHAT 0x01
372#define ESC_FLAG_GT 0x02
373#define ESC_FLAG_BANG 0x04
374#define ESC_FLAG_CASH 0x08
375#define ESC_FLAG_SQUOTE 0x10
376#define ESC_FLAG_DQUOTE 0x20
377#define ESC_FLAG_SPACE 0x40
378
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400379enum {
380 SELECT_NONE,
381 SELECT_CHAR,
382 SELECT_WORD,
383 SELECT_LINE
384};
385
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500386struct terminal {
387 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500388 struct widget *widget;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500389 struct display *display;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000390 union utf8_char *data;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400391 struct task io_task;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000392 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000393 struct attr *data_attr;
394 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000395 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000396 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000397 char saved_origin_mode;
398 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000399 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000400 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000401 character_set cs, g0, g1;
402 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000403 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000404 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500405 int width, height, start, row, column;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000406 int saved_row, saved_column;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600407 int send_cursor_position;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500408 int fd, master;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500409 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300410 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500411 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300412 enum escape_state state;
413 enum escape_state outer_state;
414 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000415 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500416 int margin;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400417 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000418 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400419 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +0800420 double average_width;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500421 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg88fd4082012-06-21 15:55:03 -0400422 uint32_t hide_cursor_serial;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500423
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400424 struct wl_data_source *selection;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400425 uint32_t button_time;
426 int dragging, click_count;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500427 int selection_start_x, selection_start_y;
428 int selection_end_x, selection_end_y;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400429 int selection_start_row, selection_start_col;
430 int selection_end_row, selection_end_col;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -0400431 struct wl_list link;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500432};
433
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000434/* Create default tab stops, every 8 characters */
435static void
436terminal_init_tabs(struct terminal *terminal)
437{
438 int i = 0;
439
440 while (i < terminal->width) {
441 if (i % 8 == 0)
442 terminal->tab_ruler[i] = 1;
443 else
444 terminal->tab_ruler[i] = 0;
445 i++;
446 }
447}
448
Callum Lowcay30eeae52011-01-07 19:46:55 +0000449static void
450terminal_init(struct terminal *terminal)
451{
452 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000453 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000454 terminal->mode = MODE_SHOW_CURSOR |
455 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000456 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000457 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000458
459 terminal->row = 0;
460 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000461
Callum Lowcay256e72f2011-01-07 19:47:01 +0000462 terminal->g0 = CS_US;
463 terminal->g1 = CS_US;
464 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000465 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000466
467 terminal->saved_g0 = terminal->g0;
468 terminal->saved_g1 = terminal->g1;
469 terminal->saved_cs = terminal->cs;
470
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000471 terminal->saved_attr = terminal->curr_attr;
472 terminal->saved_origin_mode = terminal->origin_mode;
473 terminal->saved_row = terminal->row;
474 terminal->saved_column = terminal->column;
475
476 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000477}
478
479static void
480init_color_table(struct terminal *terminal)
481{
482 int c, r;
483 struct terminal_color *color_table = terminal->color_table;
484
485 for (c = 0; c < 256; c ++) {
486 if (c < 16) {
487 color_table[c] = terminal->color_scheme->palette[c];
488 } else if (c < 232) {
489 r = c - 16;
490 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
491 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
492 color_table[c].r = ((double)(r % 6) / 6.0);
493 color_table[c].a = 1.0;
494 } else {
495 r = (c - 232) * 10 + 8;
496 color_table[c].r = ((double) r) / 256.0;
497 color_table[c].g = color_table[c].r;
498 color_table[c].b = color_table[c].r;
499 color_table[c].a = 1.0;
500 }
501 }
502}
503
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000504static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500505terminal_get_row(struct terminal *terminal, int row)
506{
507 int index;
508
509 index = (row + terminal->start) % terminal->height;
510
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000511 return &terminal->data[index * terminal->width];
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500512}
513
Callum Lowcay30eeae52011-01-07 19:46:55 +0000514static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500515terminal_get_attr_row(struct terminal *terminal, int row)
516{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000517 int index;
518
519 index = (row + terminal->start) % terminal->height;
520
521 return &terminal->data_attr[index * terminal->width];
522}
523
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500524union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300525 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500526 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500527};
528
529static void
530terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500531 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500532{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500533 struct attr attr;
534 int foreground, background, tmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500535
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500536 decoded->attr.s = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400537 if (((row == terminal->selection_start_row &&
538 col >= terminal->selection_start_col) ||
539 row > terminal->selection_start_row) &&
540 ((row == terminal->selection_end_row &&
541 col < terminal->selection_end_col) ||
542 row < terminal->selection_end_row))
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500543 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500544
545 /* get the attributes for this character cell */
546 attr = terminal_get_attr_row(terminal, row)[col];
547 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500548 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500549 ((terminal->mode & MODE_SHOW_CURSOR) &&
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400550 window_has_focus(terminal->window) && terminal->row == row &&
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500551 terminal->column == col)) {
552 foreground = attr.bg;
553 background = attr.fg;
554 if (attr.a & ATTRMASK_BOLD) {
555 if (foreground <= 16) foreground |= 0x08;
556 if (background <= 16) background &= 0x07;
557 }
558 } else {
559 foreground = attr.fg;
560 background = attr.bg;
561 }
562
563 if (terminal->mode & MODE_INVERSE) {
564 tmp = foreground;
565 foreground = background;
566 background = tmp;
567 if (attr.a & ATTRMASK_BOLD) {
568 if (foreground <= 16) foreground |= 0x08;
569 if (background <= 16) background &= 0x07;
570 }
571 }
572
Callum Lowcay9d708b02011-01-12 20:06:17 +1300573 decoded->attr.fg = foreground;
574 decoded->attr.bg = background;
575 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000576}
577
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500578
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500579static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000580terminal_scroll_buffer(struct terminal *terminal, int d)
581{
582 int i;
583
584 d = d % (terminal->height + 1);
585 terminal->start = (terminal->start + d) % terminal->height;
586 if (terminal->start < 0) terminal->start = terminal->height + terminal->start;
587 if(d < 0) {
588 d = 0 - d;
589 for(i = 0; i < d; i++) {
590 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
591 attr_init(terminal_get_attr_row(terminal, i),
592 terminal->curr_attr, terminal->width);
593 }
594 } else {
595 for(i = terminal->height - d; i < terminal->height; i++) {
596 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
597 attr_init(terminal_get_attr_row(terminal, i),
598 terminal->curr_attr, terminal->width);
599 }
600 }
Kristian Høgsberg18e928d2012-06-27 19:29:41 -0400601
602 terminal->selection_start_row -= d;
603 terminal->selection_end_row -= d;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000604}
605
606static void
607terminal_scroll_window(struct terminal *terminal, int d)
608{
609 int i;
610 int window_height;
611 int from_row, to_row;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000612
613 // scrolling range is inclusive
614 window_height = terminal->margin_bottom - terminal->margin_top + 1;
615 d = d % (window_height + 1);
616 if(d < 0) {
617 d = 0 - d;
618 to_row = terminal->margin_bottom;
619 from_row = terminal->margin_bottom - d;
620
621 for (i = 0; i < (window_height - d); i++) {
622 memcpy(terminal_get_row(terminal, to_row - i),
623 terminal_get_row(terminal, from_row - i),
624 terminal->data_pitch);
625 memcpy(terminal_get_attr_row(terminal, to_row - i),
626 terminal_get_attr_row(terminal, from_row - i),
627 terminal->attr_pitch);
628 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000629 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
630 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000631 attr_init(terminal_get_attr_row(terminal, i),
632 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000633 }
634 } else {
635 to_row = terminal->margin_top;
636 from_row = terminal->margin_top + d;
637
638 for (i = 0; i < (window_height - d); i++) {
639 memcpy(terminal_get_row(terminal, to_row + i),
640 terminal_get_row(terminal, from_row + i),
641 terminal->data_pitch);
642 memcpy(terminal_get_attr_row(terminal, to_row + i),
643 terminal_get_attr_row(terminal, from_row + i),
644 terminal->attr_pitch);
645 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000646 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
647 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000648 attr_init(terminal_get_attr_row(terminal, i),
649 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000650 }
651 }
652}
653
654static void
655terminal_scroll(struct terminal *terminal, int d)
656{
657 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
658 terminal_scroll_buffer(terminal, d);
659 else
660 terminal_scroll_window(terminal, d);
661}
662
663static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000664terminal_shift_line(struct terminal *terminal, int d)
665{
666 union utf8_char *row;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500667 struct attr *attr_row;
Callum Lowcay69e96582011-01-07 19:47:00 +0000668
669 row = terminal_get_row(terminal, terminal->row);
670 attr_row = terminal_get_attr_row(terminal, terminal->row);
671
672 if ((terminal->width + d) <= terminal->column)
673 d = terminal->column + 1 - terminal->width;
674 if ((terminal->column + d) >= terminal->width)
675 d = terminal->width - terminal->column - 1;
676
677 if (d < 0) {
678 d = 0 - d;
679 memmove(&row[terminal->column],
680 &row[terminal->column + d],
681 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
Callum Lowcay69e96582011-01-07 19:47:00 +0000682 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
683 (terminal->width - terminal->column - d) * sizeof(struct attr));
684 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
685 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
686 } else {
687 memmove(&row[terminal->column + d], &row[terminal->column],
688 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
689 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
690 (terminal->width - terminal->column - d) * sizeof(struct attr));
691 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
692 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
693 }
694}
695
696static void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500697terminal_resize_cells(struct terminal *terminal, int width, int height)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500698{
699 size_t size;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000700 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000701 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000702 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000703 int data_pitch, attr_pitch;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500704 int i, l, total_rows;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500705 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000706 struct winsize ws;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500707
708 if (terminal->width == width && terminal->height == height)
709 return;
710
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000711 data_pitch = width * sizeof(union utf8_char);
712 size = data_pitch * height;
Peter Huttererf3d62272013-08-08 11:57:05 +1000713 data = zalloc(size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000714 attr_pitch = width * sizeof(struct attr);
715 data_attr = malloc(attr_pitch * height);
Peter Huttererf3d62272013-08-08 11:57:05 +1000716 tab_ruler = zalloc(width);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000717 attr_init(data_attr, terminal->curr_attr, width * height);
718 if (terminal->data && terminal->data_attr) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500719 if (width > terminal->width)
720 l = terminal->width;
721 else
722 l = width;
723
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500724 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500725 total_rows = height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500726 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500727 total_rows = terminal->height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500728 }
729
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000730 for (i = 0; i < total_rows; i++) {
731 memcpy(&data[width * i],
732 terminal_get_row(terminal, i),
733 l * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000734 memcpy(&data_attr[width * i],
735 terminal_get_attr_row(terminal, i),
736 l * sizeof(struct attr));
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000737 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500738
739 free(terminal->data);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000740 free(terminal->data_attr);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000741 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500742 }
743
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000744 terminal->data_pitch = data_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000745 terminal->attr_pitch = attr_pitch;
Callum Lowcay86653ed2011-01-07 19:47:03 +0000746 terminal->margin_bottom =
747 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500748 terminal->width = width;
749 terminal->height = height;
750 terminal->data = data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000751 terminal->data_attr = data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000752 terminal->tab_ruler = tab_ruler;
753 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500754
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000755 /* Update the window size */
756 ws.ws_row = terminal->height;
757 ws.ws_col = terminal->width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500758 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500759 ws.ws_xpixel = allocation.width;
760 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000761 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500762}
763
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500764static void
765resize_handler(struct widget *widget,
766 int32_t width, int32_t height, void *data)
767{
768 struct terminal *terminal = data;
769 int32_t columns, rows, m;
770
771 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800772 columns = (width - m) / (int32_t) terminal->average_width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500773 rows = (height - m) / (int32_t) terminal->extents.height;
774
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400775 if (!window_is_fullscreen(terminal->window) &&
776 !window_is_maximized(terminal->window)) {
Peng Wuf291f202013-06-06 15:32:41 +0800777 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500778 height = rows * terminal->extents.height + m;
779 widget_set_size(terminal->widget, width, height);
780 }
781
782 terminal_resize_cells(terminal, columns, rows);
783}
784
785static void
786terminal_resize(struct terminal *terminal, int columns, int rows)
787{
788 int32_t width, height, m;
789
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400790 if (window_is_fullscreen(terminal->window) ||
791 window_is_maximized(terminal->window))
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500792 return;
793
794 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800795 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500796 height = rows * terminal->extents.height + m;
Kristian Høgsberga1627922012-06-20 17:30:03 -0400797
798 frame_set_child_size(terminal->widget, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500799}
800
Callum Lowcay30eeae52011-01-07 19:46:55 +0000801struct color_scheme DEFAULT_COLORS = {
802 {
803 {0, 0, 0, 1}, /* black */
804 {0.66, 0, 0, 1}, /* red */
805 {0 , 0.66, 0, 1}, /* green */
806 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
807 {0 , 0 , 0.66, 1}, /* blue */
808 {0.66, 0 , 0.66, 1}, /* magenta */
809 {0, 0.66, 0.66, 1}, /* cyan */
810 {0.66, 0.66, 0.66, 1}, /* light grey */
811 {0.22, 0.33, 0.33, 1}, /* dark grey */
812 {1, 0.33, 0.33, 1}, /* high red */
813 {0.33, 1, 0.33, 1}, /* high green */
814 {1, 1, 0.33, 1}, /* high yellow */
815 {0.33, 0.33, 1, 1}, /* high blue */
816 {1, 0.33, 1, 1}, /* high magenta */
817 {0.33, 1, 1, 1}, /* high cyan */
818 {1, 1, 1, 1} /* white */
819 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500820 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000821 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
822};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400823
Kristian Høgsberg22106762008-12-08 13:50:07 -0500824static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500825terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
826{
827 cairo_set_source_rgba(cr,
828 terminal->color_table[index].r,
829 terminal->color_table[index].g,
830 terminal->color_table[index].b,
831 terminal->color_table[index].a);
832}
833
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500834static void
835terminal_send_selection(struct terminal *terminal, int fd)
836{
837 int row, col;
838 union utf8_char *p_row;
839 union decoded_attr attr;
840 FILE *fp;
841 int len;
842
843 fp = fdopen(fd, "w");
Brian Lovin1bf14812013-08-08 16:12:55 -0700844 if (fp == NULL){
845 close(fd);
846 return;
847 }
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500848 for (row = 0; row < terminal->height; row++) {
849 p_row = terminal_get_row(terminal, row);
850 for (col = 0; col < terminal->width; col++) {
851 /* get the attributes for this character cell */
852 terminal_decode_attr(terminal, row, col, &attr);
853 if (!attr.attr.s)
854 continue;
855 len = strnlen((char *) p_row[col].byte, 4);
Kristian Høgsberg0dee6472012-07-01 21:25:41 -0400856 if (len > 0)
857 fwrite(p_row[col].byte, 1, len, fp);
858 if (len == 0 || col == terminal->width - 1) {
859 fwrite("\n", 1, 1, fp);
860 break;
861 }
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500862 }
863 }
864 fclose(fp);
865}
866
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500867struct glyph_run {
868 struct terminal *terminal;
869 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400870 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500871 union decoded_attr attr;
872 cairo_glyph_t glyphs[256], *g;
873};
874
875static void
876glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
877{
878 run->terminal = terminal;
879 run->cr = cr;
880 run->g = run->glyphs;
881 run->count = 0;
882 run->attr.key = 0;
883}
884
885static void
886glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
887{
888 cairo_scaled_font_t *font;
889
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500890 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
891 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300892 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500893 font = run->terminal->font_bold;
894 else
895 font = run->terminal->font_normal;
896 cairo_set_scaled_font(run->cr, font);
897 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300898 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500899
Callum Lowcay9d708b02011-01-12 20:06:17 +1300900 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
901 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500902 run->g = run->glyphs;
903 run->count = 0;
904 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300905 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500906}
907
908static void
909glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
910{
911 int num_glyphs;
912 cairo_scaled_font_t *font;
913
914 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
915
Callum Lowcay9d708b02011-01-12 20:06:17 +1300916 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500917 font = run->terminal->font_bold;
918 else
919 font = run->terminal->font_normal;
920
921 cairo_move_to(run->cr, x, y);
922 cairo_scaled_font_text_to_glyphs (font, x, y,
923 (char *) c->byte, 4,
924 &run->g, &num_glyphs,
925 NULL, NULL, NULL);
926 run->g += num_glyphs;
927 run->count += num_glyphs;
928}
929
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500930
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500931static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500932redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500933{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500934 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500935 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500936 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000937 int top_margin, side_margin;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600938 int row, col, cursor_x, cursor_y;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000939 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500940 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000941 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500942 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500943 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500944 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500945 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +0800946 double average_width;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500947
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500948 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500949 widget_get_allocation(terminal->widget, &allocation);
Alexander Larssonde79dd02013-05-22 14:41:34 +0200950 cr = widget_cairo_create(terminal->widget);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500951 cairo_rectangle(cr, allocation.x, allocation.y,
952 allocation.width, allocation.height);
953 cairo_clip(cr);
954 cairo_push_group(cr);
955
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500956 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500957 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500958 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500959
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500960 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500961
Kristian Høgsberg59826582011-01-20 11:56:57 -0500962 extents = terminal->extents;
Peng Wuf291f202013-06-06 15:32:41 +0800963 average_width = terminal->average_width;
964 side_margin = (allocation.width - terminal->width * average_width) / 2;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500965 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500966
Callum Lowcay30eeae52011-01-07 19:46:55 +0000967 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500968 cairo_translate(cr, allocation.x + side_margin,
969 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500970 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000971 for (row = 0; row < terminal->height; row++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000972 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000973 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500974 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000975
Callum Lowcay9d708b02011-01-12 20:06:17 +1300976 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -0500977 continue;
978
Callum Lowcay9d708b02011-01-12 20:06:17 +1300979 terminal_set_color(terminal, cr, attr.attr.bg);
Peng Wuf291f202013-06-06 15:32:41 +0800980 cairo_move_to(cr, col * average_width,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500981 row * extents.height);
Peng Wuf291f202013-06-06 15:32:41 +0800982 cairo_rel_line_to(cr, average_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000983 cairo_rel_line_to(cr, 0, extents.height);
Peng Wuf291f202013-06-06 15:32:41 +0800984 cairo_rel_line_to(cr, -average_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000985 cairo_close_path(cr);
986 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500987 }
988 }
Callum Lowcay30eeae52011-01-07 19:46:55 +0000989
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500990 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
991
992 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500993 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500994 for (row = 0; row < terminal->height; row++) {
995 p_row = terminal_get_row(terminal, row);
996 for (col = 0; col < terminal->width; col++) {
997 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500998 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500999
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001000 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001001
Peng Wuf291f202013-06-06 15:32:41 +08001002 text_x = col * average_width;
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001003 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +13001004 if (attr.attr.a & ATTRMASK_UNDERLINE) {
1005 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +00001006 cairo_move_to(cr, text_x, (double)text_y + 1.5);
Peng Wuf291f202013-06-06 15:32:41 +08001007 cairo_line_to(cr, text_x + average_width, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001008 cairo_stroke(cr);
1009 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -05001010
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001011 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001012 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001013 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001014
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001015 attr.key = ~0;
1016 glyph_run_flush(&run, attr);
1017
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001018 if ((terminal->mode & MODE_SHOW_CURSOR) &&
1019 !window_has_focus(terminal->window)) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001020 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001021
Callum Lowcay30eeae52011-01-07 19:46:55 +00001022 cairo_set_line_width(cr, 1);
Peng Wuf291f202013-06-06 15:32:41 +08001023 cairo_move_to(cr, terminal->column * average_width + d,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001024 terminal->row * extents.height + d);
Peng Wuf291f202013-06-06 15:32:41 +08001025 cairo_rel_line_to(cr, average_width - 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001026 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
Peng Wuf291f202013-06-06 15:32:41 +08001027 cairo_rel_line_to(cr, -average_width + 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001028 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001029
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001030 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001031 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001032
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001033 cairo_pop_group_to_source(cr);
1034 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001035 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001036 cairo_surface_destroy(surface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001037
1038 if (terminal->send_cursor_position) {
1039 cursor_x = side_margin + allocation.x +
Peng Wuf291f202013-06-06 15:32:41 +08001040 terminal->column * average_width;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001041 cursor_y = top_margin + allocation.y +
1042 terminal->row * extents.height;
1043 window_set_text_cursor_position(terminal->window,
1044 cursor_x, cursor_y);
1045 terminal->send_cursor_position = 0;
1046 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001047}
1048
1049static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001050terminal_write(struct terminal *terminal, const char *data, size_t length)
1051{
1052 if (write(terminal->master, data, length) < 0)
1053 abort();
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001054 terminal->send_cursor_position = 1;
Kristian Høgsberg26130862011-08-24 11:30:21 -04001055}
1056
1057static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001058terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001059
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001060static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001061handle_char(struct terminal *terminal, union utf8_char utf8);
1062
1063static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001064handle_sgr(struct terminal *terminal, int code);
1065
1066static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001067handle_term_parameter(struct terminal *terminal, int code, int sr)
1068{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001069 int i;
1070
Callum Lowcay67a201d2011-01-12 19:23:41 +13001071 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001072 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001073 case 1: /* DECCKM */
1074 if (sr) terminal->key_mode = KM_APPLICATION;
1075 else terminal->key_mode = KM_NORMAL;
1076 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001077 case 2: /* DECANM */
1078 /* No VT52 support yet */
1079 terminal->g0 = CS_US;
1080 terminal->g1 = CS_US;
1081 terminal->cs = terminal->g0;
1082 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001083 case 3: /* DECCOLM */
1084 if (sr)
1085 terminal_resize(terminal, 132, 24);
1086 else
1087 terminal_resize(terminal, 80, 24);
1088
1089 /* set columns, but also home cursor and clear screen */
1090 terminal->row = 0; terminal->column = 0;
1091 for (i = 0; i < terminal->height; i++) {
1092 memset(terminal_get_row(terminal, i),
1093 0, terminal->data_pitch);
1094 attr_init(terminal_get_attr_row(terminal, i),
1095 terminal->curr_attr, terminal->width);
1096 }
1097 break;
1098 case 5: /* DECSCNM */
1099 if (sr) terminal->mode |= MODE_INVERSE;
1100 else terminal->mode &= ~MODE_INVERSE;
1101 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001102 case 6: /* DECOM */
1103 terminal->origin_mode = sr;
1104 if (terminal->origin_mode)
1105 terminal->row = terminal->margin_top;
1106 else
1107 terminal->row = 0;
1108 terminal->column = 0;
1109 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001110 case 7: /* DECAWM */
1111 if (sr) terminal->mode |= MODE_AUTOWRAP;
1112 else terminal->mode &= ~MODE_AUTOWRAP;
1113 break;
1114 case 8: /* DECARM */
1115 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1116 else terminal->mode &= ~MODE_AUTOREPEAT;
1117 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001118 case 12: /* Very visible cursor (CVVIS) */
1119 /* FIXME: What do we do here. */
1120 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001121 case 25:
1122 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1123 else terminal->mode &= ~MODE_SHOW_CURSOR;
1124 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001125 case 1034: /* smm/rmm, meta mode on/off */
1126 /* ignore */
1127 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001128 case 1037: /* deleteSendsDel */
1129 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1130 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1131 break;
1132 case 1039: /* altSendsEscape */
1133 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1134 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1135 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001136 case 1049: /* rmcup/smcup, alternate screen */
1137 /* Ignore. Should be possible to implement,
1138 * but it's kind of annoying. */
1139 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001140 default:
1141 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1142 break;
1143 }
1144 } else {
1145 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001146 case 4: /* IRM */
1147 if (sr) terminal->mode |= MODE_IRM;
1148 else terminal->mode &= ~MODE_IRM;
1149 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001150 case 20: /* LNM */
1151 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1152 else terminal->mode &= ~MODE_LF_NEWLINE;
1153 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001154 default:
1155 fprintf(stderr, "Unknown parameter: %d\n", code);
1156 break;
1157 }
1158 }
1159}
1160
1161static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001162handle_dcs(struct terminal *terminal)
1163{
1164}
1165
1166static void
1167handle_osc(struct terminal *terminal)
1168{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001169 char *p;
1170 int code;
1171
1172 terminal->escape[terminal->escape_length++] = '\0';
1173 p = &terminal->escape[2];
1174 code = strtol(p, &p, 10);
1175 if (*p == ';') p++;
1176
1177 switch (code) {
1178 case 0: /* Icon name and window title */
1179 case 1: /* Icon label */
1180 case 2: /* Window title*/
1181 window_set_title(terminal->window, p);
1182 break;
1183 default:
1184 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1185 break;
1186 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001187}
1188
1189static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001190handle_escape(struct terminal *terminal)
1191{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001192 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001193 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001194 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001195 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001196 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001197 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001198 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001199
1200 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001201 i = 0;
1202 p = &terminal->escape[2];
1203 while ((isdigit(*p) || *p == ';') && i < 10) {
1204 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001205 if (!set[i]) {
1206 args[i] = 0;
1207 set[i] = 1;
1208 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001209 p++;
1210 i++;
1211 } else {
1212 args[i] = strtol(p, &p, 10);
1213 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001214 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001215 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001216
1217 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001218 case '@': /* ICH */
1219 count = set[0] ? args[0] : 1;
1220 if (count == 0) count = 1;
1221 terminal_shift_line(terminal, count);
1222 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001223 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001224 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001225 if (count == 0) count = 1;
1226 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001227 terminal->row -= count;
1228 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001229 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001230 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001231 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001232 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001233 if (count == 0) count = 1;
1234 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001235 terminal->row += count;
1236 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001237 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001238 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001239 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001240 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001241 if (count == 0) count = 1;
1242 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001243 terminal->column += count;
1244 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001245 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001246 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001247 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001248 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001249 if (count == 0) count = 1;
1250 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001251 terminal->column -= count;
1252 else
1253 terminal->column = 0;
1254 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001255 case 'E': /* CNL */
1256 count = set[0] ? args[0] : 1;
1257 if (terminal->row + count <= terminal->margin_bottom)
1258 terminal->row += count;
1259 else
1260 terminal->row = terminal->margin_bottom;
1261 terminal->column = 0;
1262 break;
1263 case 'F': /* CPL */
1264 count = set[0] ? args[0] : 1;
1265 if (terminal->row - count >= terminal->margin_top)
1266 terminal->row -= count;
1267 else
1268 terminal->row = terminal->margin_top;
1269 terminal->column = 0;
1270 break;
1271 case 'G': /* CHA */
1272 y = set[0] ? args[0] : 1;
1273 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1274
1275 terminal->column = y - 1;
1276 break;
1277 case 'f': /* HVP */
1278 case 'H': /* CUP */
1279 x = (set[1] ? args[1] : 1) - 1;
1280 x = x < 0 ? 0 :
1281 (x >= terminal->width ? terminal->width - 1 : x);
1282
1283 y = (set[0] ? args[0] : 1) - 1;
1284 if (terminal->origin_mode) {
1285 y += terminal->margin_top;
1286 y = y < terminal->margin_top ? terminal->margin_top :
1287 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1288 } else {
1289 y = y < 0 ? 0 :
1290 (y >= terminal->height ? terminal->height - 1 : y);
1291 }
1292
1293 terminal->row = y;
1294 terminal->column = x;
1295 break;
1296 case 'I': /* CHT */
1297 count = set[0] ? args[0] : 1;
1298 if (count == 0) count = 1;
1299 while (count > 0 && terminal->column < terminal->width) {
1300 if (terminal->tab_ruler[terminal->column]) count--;
1301 terminal->column++;
1302 }
1303 terminal->column--;
1304 break;
1305 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001306 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001307 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001308 if (!set[0] || args[0] == 0 || args[0] > 2) {
1309 memset(&row[terminal->column],
1310 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1311 attr_init(&attr_row[terminal->column],
1312 terminal->curr_attr, terminal->width - terminal->column);
1313 for (i = terminal->row + 1; i < terminal->height; i++) {
1314 memset(terminal_get_row(terminal, i),
1315 0, terminal->data_pitch);
1316 attr_init(terminal_get_attr_row(terminal, i),
1317 terminal->curr_attr, terminal->width);
1318 }
1319 } else if (args[0] == 1) {
1320 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1321 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1322 for (i = 0; i < terminal->row; i++) {
1323 memset(terminal_get_row(terminal, i),
1324 0, terminal->data_pitch);
1325 attr_init(terminal_get_attr_row(terminal, i),
1326 terminal->curr_attr, terminal->width);
1327 }
1328 } else if (args[0] == 2) {
1329 for (i = 0; i < terminal->height; i++) {
1330 memset(terminal_get_row(terminal, i),
1331 0, terminal->data_pitch);
1332 attr_init(terminal_get_attr_row(terminal, i),
1333 terminal->curr_attr, terminal->width);
1334 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001335 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001336 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001337 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001338 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001339 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001340 if (!set[0] || args[0] == 0 || args[0] > 2) {
1341 memset(&row[terminal->column], 0,
1342 (terminal->width - terminal->column) * sizeof(union utf8_char));
1343 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1344 terminal->width - terminal->column);
1345 } else if (args[0] == 1) {
1346 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1347 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1348 } else if (args[0] == 2) {
1349 memset(row, 0, terminal->data_pitch);
1350 attr_init(attr_row, terminal->curr_attr, terminal->width);
1351 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001352 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001353 case 'L': /* IL */
1354 count = set[0] ? args[0] : 1;
1355 if (count == 0) count = 1;
1356 if (terminal->row >= terminal->margin_top &&
1357 terminal->row < terminal->margin_bottom)
1358 {
1359 top = terminal->margin_top;
1360 terminal->margin_top = terminal->row;
1361 terminal_scroll(terminal, 0 - count);
1362 terminal->margin_top = top;
1363 } else if (terminal->row == terminal->margin_bottom) {
1364 memset(terminal_get_row(terminal, terminal->row),
1365 0, terminal->data_pitch);
1366 attr_init(terminal_get_attr_row(terminal, terminal->row),
1367 terminal->curr_attr, terminal->width);
1368 }
1369 break;
1370 case 'M': /* DL */
1371 count = set[0] ? args[0] : 1;
1372 if (count == 0) count = 1;
1373 if (terminal->row >= terminal->margin_top &&
1374 terminal->row < terminal->margin_bottom)
1375 {
1376 top = terminal->margin_top;
1377 terminal->margin_top = terminal->row;
1378 terminal_scroll(terminal, count);
1379 terminal->margin_top = top;
1380 } else if (terminal->row == terminal->margin_bottom) {
1381 memset(terminal_get_row(terminal, terminal->row),
1382 0, terminal->data_pitch);
1383 }
1384 break;
1385 case 'P': /* DCH */
1386 count = set[0] ? args[0] : 1;
1387 if (count == 0) count = 1;
1388 terminal_shift_line(terminal, 0 - count);
1389 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001390 case 'S': /* SU */
1391 terminal_scroll(terminal, set[0] ? args[0] : 1);
1392 break;
1393 case 'T': /* SD */
1394 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1395 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001396 case 'X': /* ECH */
1397 count = set[0] ? args[0] : 1;
1398 if (count == 0) count = 1;
1399 if ((terminal->column + count) > terminal->width)
1400 count = terminal->width - terminal->column;
1401 row = terminal_get_row(terminal, terminal->row);
1402 attr_row = terminal_get_attr_row(terminal, terminal->row);
1403 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1404 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1405 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001406 case 'Z': /* CBT */
1407 count = set[0] ? args[0] : 1;
1408 if (count == 0) count = 1;
1409 while (count > 0 && terminal->column >= 0) {
1410 if (terminal->tab_ruler[terminal->column]) count--;
1411 terminal->column--;
1412 }
1413 terminal->column++;
1414 break;
1415 case '`': /* HPA */
1416 y = set[0] ? args[0] : 1;
1417 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1418
1419 terminal->column = y - 1;
1420 break;
1421 case 'b': /* REP */
1422 count = set[0] ? args[0] : 1;
1423 if (count == 0) count = 1;
1424 if (terminal->last_char.byte[0])
1425 for (i = 0; i < count; i++)
1426 handle_char(terminal, terminal->last_char);
1427 terminal->last_char.byte[0] = 0;
1428 break;
1429 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001430 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001431 break;
1432 case 'd': /* VPA */
1433 x = set[0] ? args[0] : 1;
1434 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1435
1436 terminal->row = x - 1;
1437 break;
1438 case 'g': /* TBC */
1439 if (!set[0] || args[0] == 0) {
1440 terminal->tab_ruler[terminal->column] = 0;
1441 } else if (args[0] == 3) {
1442 memset(terminal->tab_ruler, 0, terminal->width);
1443 }
1444 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001445 case 'h': /* SM */
1446 for(i = 0; i < 10 && set[i]; i++) {
1447 handle_term_parameter(terminal, args[i], 1);
1448 }
1449 break;
1450 case 'l': /* RM */
1451 for(i = 0; i < 10 && set[i]; i++) {
1452 handle_term_parameter(terminal, args[i], 0);
1453 }
1454 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001455 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001456 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001457 if (i <= 7 && set[i] && set[i + 1] &&
1458 set[i + 2] && args[i + 1] == 5)
1459 {
1460 if (args[i] == 38) {
1461 handle_sgr(terminal, args[i + 2] + 256);
1462 break;
1463 } else if (args[i] == 48) {
1464 handle_sgr(terminal, args[i + 2] + 512);
1465 break;
1466 }
1467 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001468 if(set[i]) {
1469 handle_sgr(terminal, args[i]);
1470 } else if(i == 0) {
1471 handle_sgr(terminal, 0);
1472 break;
1473 } else {
1474 break;
1475 }
1476 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001477 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001478 case 'n': /* DSR */
1479 i = set[0] ? args[0] : 0;
1480 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001481 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001482 } else if (i == 6) {
1483 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1484 terminal->origin_mode ?
1485 terminal->row+terminal->margin_top : terminal->row+1,
1486 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001487 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001488 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001489 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001490 case 'r':
1491 if(!set[0]) {
1492 terminal->margin_top = 0;
1493 terminal->margin_bottom = terminal->height-1;
1494 terminal->row = 0;
1495 terminal->column = 0;
1496 } else {
1497 top = (set[0] ? args[0] : 1) - 1;
1498 top = top < 0 ? 0 :
1499 (top >= terminal->height ? terminal->height - 1 : top);
1500 bottom = (set[1] ? args[1] : 1) - 1;
1501 bottom = bottom < 0 ? 0 :
1502 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1503 if(bottom > top) {
1504 terminal->margin_top = top;
1505 terminal->margin_bottom = bottom;
1506 } else {
1507 terminal->margin_top = 0;
1508 terminal->margin_bottom = terminal->height-1;
1509 }
1510 if(terminal->origin_mode)
1511 terminal->row = terminal->margin_top;
1512 else
1513 terminal->row = 0;
1514 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001515 }
1516 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001517 case 's':
1518 terminal->saved_row = terminal->row;
1519 terminal->saved_column = terminal->column;
1520 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001521 case 't': /* windowOps */
1522 if (!set[0]) break;
1523 switch (args[0]) {
1524 case 4: /* resize px */
1525 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001526 widget_schedule_resize(terminal->widget,
1527 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001528 }
1529 break;
1530 case 8: /* resize ch */
1531 if (set[1] && set[2]) {
1532 terminal_resize(terminal, args[2], args[1]);
1533 }
1534 break;
1535 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001536 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001537 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1538 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001539 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001540 break;
1541 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001542 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001543 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1544 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001545 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001546 break;
1547 case 18: /* report ch */
1548 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1549 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001550 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001551 break;
1552 case 21: /* report title */
1553 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1554 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001555 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001556 break;
1557 default:
1558 if (args[0] >= 24)
1559 terminal_resize(terminal, terminal->width, args[0]);
1560 else
1561 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1562 break;
1563 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001564 case 'u':
1565 terminal->row = terminal->saved_row;
1566 terminal->column = terminal->saved_column;
1567 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001568 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001569 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001570 break;
1571 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001572}
1573
1574static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001575handle_non_csi_escape(struct terminal *terminal, char code)
1576{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001577 switch(code) {
1578 case 'M': /* RI */
1579 terminal->row -= 1;
1580 if(terminal->row < terminal->margin_top) {
1581 terminal->row = terminal->margin_top;
1582 terminal_scroll(terminal, -1);
1583 }
1584 break;
1585 case 'E': /* NEL */
1586 terminal->column = 0;
1587 // fallthrough
1588 case 'D': /* IND */
1589 terminal->row += 1;
1590 if(terminal->row > terminal->margin_bottom) {
1591 terminal->row = terminal->margin_bottom;
1592 terminal_scroll(terminal, +1);
1593 }
1594 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001595 case 'c': /* RIS */
1596 terminal_init(terminal);
1597 break;
1598 case 'H': /* HTS */
1599 terminal->tab_ruler[terminal->column] = 1;
1600 break;
1601 case '7': /* DECSC */
1602 terminal->saved_row = terminal->row;
1603 terminal->saved_column = terminal->column;
1604 terminal->saved_attr = terminal->curr_attr;
1605 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001606 terminal->saved_cs = terminal->cs;
1607 terminal->saved_g0 = terminal->g0;
1608 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001609 break;
1610 case '8': /* DECRC */
1611 terminal->row = terminal->saved_row;
1612 terminal->column = terminal->saved_column;
1613 terminal->curr_attr = terminal->saved_attr;
1614 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001615 terminal->cs = terminal->saved_cs;
1616 terminal->g0 = terminal->saved_g0;
1617 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001618 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001619 case '=': /* DECPAM */
1620 terminal->key_mode = KM_APPLICATION;
1621 break;
1622 case '>': /* DECPNM */
1623 terminal->key_mode = KM_NORMAL;
1624 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001625 default:
1626 fprintf(stderr, "Unknown escape code: %c\n", code);
1627 break;
1628 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001629}
1630
1631static void
1632handle_special_escape(struct terminal *terminal, char special, char code)
1633{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001634 int i, numChars;
1635
1636 if (special == '#') {
1637 switch(code) {
1638 case '8':
1639 /* fill with 'E', no cheap way to do this */
1640 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1641 numChars = terminal->width * terminal->height;
1642 for(i = 0; i < numChars; i++) {
1643 terminal->data[i].byte[0] = 'E';
1644 }
1645 break;
1646 default:
1647 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1648 break;
1649 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001650 } else if (special == '(' || special == ')') {
1651 switch(code) {
1652 case '0':
1653 if (special == '(')
1654 terminal->g0 = CS_SPECIAL;
1655 else
1656 terminal->g1 = CS_SPECIAL;
1657 break;
1658 case 'A':
1659 if (special == '(')
1660 terminal->g0 = CS_UK;
1661 else
1662 terminal->g1 = CS_UK;
1663 break;
1664 case 'B':
1665 if (special == '(')
1666 terminal->g0 = CS_US;
1667 else
1668 terminal->g1 = CS_US;
1669 break;
1670 default:
1671 fprintf(stderr, "Unknown character set %c\n", code);
1672 break;
1673 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001674 } else {
1675 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1676 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001677}
1678
1679static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001680handle_sgr(struct terminal *terminal, int code)
1681{
1682 switch(code) {
1683 case 0:
1684 terminal->curr_attr = terminal->color_scheme->default_attr;
1685 break;
1686 case 1:
1687 terminal->curr_attr.a |= ATTRMASK_BOLD;
1688 if (terminal->curr_attr.fg < 8)
1689 terminal->curr_attr.fg += 8;
1690 break;
1691 case 4:
1692 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1693 break;
1694 case 5:
1695 terminal->curr_attr.a |= ATTRMASK_BLINK;
1696 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001697 case 8:
1698 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1699 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001700 case 2:
1701 case 21:
1702 case 22:
1703 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1704 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1705 terminal->curr_attr.fg -= 8;
1706 break;
1707 case 24:
1708 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1709 break;
1710 case 25:
1711 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1712 break;
1713 case 7:
1714 case 26:
1715 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1716 break;
1717 case 27:
1718 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1719 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001720 case 28:
1721 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1722 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001723 case 39:
1724 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1725 break;
1726 case 49:
1727 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1728 break;
1729 default:
1730 if(code >= 30 && code <= 37) {
1731 terminal->curr_attr.fg = code - 30;
1732 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1733 terminal->curr_attr.fg += 8;
1734 } else if(code >= 40 && code <= 47) {
1735 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001736 } else if (code >= 90 && code <= 97) {
1737 terminal->curr_attr.fg = code - 90 + 8;
1738 } else if (code >= 100 && code <= 107) {
1739 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001740 } else if(code >= 256 && code < 512) {
1741 terminal->curr_attr.fg = code - 256;
1742 } else if(code >= 512 && code < 768) {
1743 terminal->curr_attr.bg = code - 512;
1744 } else {
1745 fprintf(stderr, "Unknown SGR code: %d\n", code);
1746 }
1747 break;
1748 }
1749}
1750
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001751/* Returns 1 if c was special, otherwise 0 */
1752static int
1753handle_special_char(struct terminal *terminal, char c)
1754{
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001755 union utf8_char *row;
1756 struct attr *attr_row;
1757
1758 row = terminal_get_row(terminal, terminal->row);
1759 attr_row = terminal_get_attr_row(terminal, terminal->row);
1760
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001761 switch(c) {
1762 case '\r':
1763 terminal->column = 0;
1764 break;
1765 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001766 if (terminal->mode & MODE_LF_NEWLINE) {
1767 terminal->column = 0;
1768 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001769 /* fallthrough */
1770 case '\v':
1771 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001772 terminal->row++;
1773 if(terminal->row > terminal->margin_bottom) {
1774 terminal->row = terminal->margin_bottom;
1775 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001776 }
1777
1778 break;
1779 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001780 while (terminal->column < terminal->width) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001781 if (terminal->mode & MODE_IRM)
1782 terminal_shift_line(terminal, +1);
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001783
1784 if (row[terminal->column].byte[0] == '\0') {
1785 row[terminal->column].byte[0] = ' ';
1786 row[terminal->column].byte[1] = '\0';
1787 attr_row[terminal->column] = terminal->curr_attr;
1788 }
1789
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001790 terminal->column++;
Kristian Høgsbergcca3c2f2012-06-20 15:56:13 -04001791 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001792 }
1793 if (terminal->column >= terminal->width) {
1794 terminal->column = terminal->width - 1;
1795 }
1796
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001797 break;
1798 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001799 if (terminal->column >= terminal->width) {
1800 terminal->column = terminal->width - 2;
1801 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001802 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001803 } else if (terminal->mode & MODE_AUTOWRAP) {
1804 terminal->column = terminal->width - 1;
1805 terminal->row -= 1;
1806 if (terminal->row < terminal->margin_top) {
1807 terminal->row = terminal->margin_top;
1808 terminal_scroll(terminal, -1);
1809 }
1810 }
1811
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001812 break;
1813 case '\a':
1814 /* Bell */
1815 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001816 case '\x0E': /* SO */
1817 terminal->cs = terminal->g1;
1818 break;
1819 case '\x0F': /* SI */
1820 terminal->cs = terminal->g0;
1821 break;
Kristian Høgsberg2a1aa4e2012-08-03 09:37:05 -04001822 case '\0':
1823 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001824 default:
1825 return 0;
1826 }
1827
1828 return 1;
1829}
1830
1831static void
1832handle_char(struct terminal *terminal, union utf8_char utf8)
1833{
1834 union utf8_char *row;
1835 struct attr *attr_row;
1836
1837 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001838
1839 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001840
1841 /* There are a whole lot of non-characters, control codes,
1842 * and formatting codes that should probably be ignored,
1843 * for example: */
1844 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1845 /* BOM, ignore */
1846 return;
1847 }
1848
1849 /* Some of these non-characters should be translated, e.g.: */
1850 if (utf8.byte[0] < 32) {
1851 utf8.byte[0] = utf8.byte[0] + 64;
1852 }
1853
1854 /* handle right margin effects */
1855 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001856 if (terminal->mode & MODE_AUTOWRAP) {
1857 terminal->column = 0;
1858 terminal->row += 1;
1859 if (terminal->row > terminal->margin_bottom) {
1860 terminal->row = terminal->margin_bottom;
1861 terminal_scroll(terminal, +1);
1862 }
1863 } else {
1864 terminal->column--;
1865 }
1866 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001867
1868 row = terminal_get_row(terminal, terminal->row);
1869 attr_row = terminal_get_attr_row(terminal, terminal->row);
1870
Callum Lowcay69e96582011-01-07 19:47:00 +00001871 if (terminal->mode & MODE_IRM)
1872 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001873 row[terminal->column] = utf8;
1874 attr_row[terminal->column++] = terminal->curr_attr;
1875
1876 if (utf8.ch != terminal->last_char.ch)
1877 terminal->last_char = utf8;
1878}
1879
Callum Lowcay30eeae52011-01-07 19:46:55 +00001880static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001881escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1882{
1883 int len, i;
1884
1885 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1886 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1887 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1888 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1889 else len = 1; /* Invalid, cannot happen */
1890
1891 if (terminal->escape_length + len <= MAX_ESCAPE) {
1892 for (i = 0; i < len; i++)
1893 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1894 terminal->escape_length += len;
1895 } else if (terminal->escape_length < MAX_ESCAPE) {
1896 terminal->escape[terminal->escape_length++] = 0;
1897 }
1898}
1899
1900static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001901terminal_data(struct terminal *terminal, const char *data, size_t length)
1902{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001903 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001904 union utf8_char utf8;
1905 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001906
1907 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001908 parser_state =
1909 utf8_next_char(&terminal->state_machine, data[i]);
1910 switch(parser_state) {
1911 case utf8state_accept:
1912 utf8.ch = terminal->state_machine.s.ch;
1913 break;
1914 case utf8state_reject:
1915 /* the unicode replacement character */
1916 utf8.byte[0] = 0xEF;
1917 utf8.byte[1] = 0xBF;
1918 utf8.byte[2] = 0xBD;
1919 utf8.byte[3] = 0x00;
1920 break;
1921 default:
1922 continue;
1923 }
1924
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001925 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001926 switch (terminal->state) {
1927 case escape_state_escape:
1928 escape_append_utf8(terminal, utf8);
1929 switch (utf8.byte[0]) {
1930 case 'P': /* DCS */
1931 terminal->state = escape_state_dcs;
1932 break;
1933 case '[': /* CSI */
1934 terminal->state = escape_state_csi;
1935 break;
1936 case ']': /* OSC */
1937 terminal->state = escape_state_osc;
1938 break;
1939 case '#':
1940 case '(':
1941 case ')': /* special */
1942 terminal->state = escape_state_special;
1943 break;
1944 case '^': /* PM (not implemented) */
1945 case '_': /* APC (not implemented) */
1946 terminal->state = escape_state_ignore;
1947 break;
1948 default:
1949 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001950 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13001951 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001952 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001953 continue;
1954 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001955 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1956 /* do nothing */
1957 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001958 terminal->escape_flags |= ESC_FLAG_WHAT;
1959 } else if (utf8.byte[0] == '>') {
1960 terminal->escape_flags |= ESC_FLAG_GT;
1961 } else if (utf8.byte[0] == '!') {
1962 terminal->escape_flags |= ESC_FLAG_BANG;
1963 } else if (utf8.byte[0] == '$') {
1964 terminal->escape_flags |= ESC_FLAG_CASH;
1965 } else if (utf8.byte[0] == '\'') {
1966 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1967 } else if (utf8.byte[0] == '"') {
1968 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1969 } else if (utf8.byte[0] == ' ') {
1970 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001971 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001972 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001973 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13001974 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001975 }
1976
1977 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1978 utf8.byte[0] == '`')
1979 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001980 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001981 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001982 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001983 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001984 continue;
1985 case escape_state_inner_escape:
1986 if (utf8.byte[0] == '\\') {
1987 terminal->state = escape_state_normal;
1988 if (terminal->outer_state == escape_state_dcs) {
1989 handle_dcs(terminal);
1990 } else if (terminal->outer_state == escape_state_osc) {
1991 handle_osc(terminal);
1992 }
1993 } else if (utf8.byte[0] == '\e') {
1994 terminal->state = terminal->outer_state;
1995 escape_append_utf8(terminal, utf8);
1996 if (terminal->escape_length >= MAX_ESCAPE)
1997 terminal->state = escape_state_normal;
1998 } else {
1999 terminal->state = terminal->outer_state;
2000 if (terminal->escape_length < MAX_ESCAPE)
2001 terminal->escape[terminal->escape_length++] = '\e';
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_dcs:
2008 case escape_state_osc:
2009 case escape_state_ignore:
2010 if (utf8.byte[0] == '\e') {
2011 terminal->outer_state = terminal->state;
2012 terminal->state = escape_state_inner_escape;
2013 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
2014 terminal->state = escape_state_normal;
2015 handle_osc(terminal);
2016 } else {
2017 escape_append_utf8(terminal, utf8);
2018 if (terminal->escape_length >= MAX_ESCAPE)
2019 terminal->state = escape_state_normal;
2020 }
2021 continue;
2022 case escape_state_special:
2023 escape_append_utf8(terminal, utf8);
2024 terminal->state = escape_state_normal;
2025 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2026 handle_special_escape(terminal, terminal->escape[1],
2027 utf8.byte[0]);
2028 }
2029 continue;
2030 default:
2031 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002032 }
2033
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002034 /* this is valid, because ASCII characters are never used to
2035 * introduce a multibyte sequence in UTF-8 */
2036 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002037 terminal->state = escape_state_escape;
2038 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002039 terminal->escape[0] = '\e';
2040 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002041 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002042 } else {
2043 handle_char(terminal, utf8);
2044 } /* if */
2045 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002046
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002047 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002048}
2049
2050static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002051data_source_target(void *data,
2052 struct wl_data_source *source, const char *mime_type)
2053{
2054 fprintf(stderr, "data_source_target, %s\n", mime_type);
2055}
2056
2057static void
2058data_source_send(void *data,
2059 struct wl_data_source *source,
2060 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002061{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002062 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002063
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002064 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002065}
2066
2067static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002068data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002069{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002070 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002071}
2072
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002073static const struct wl_data_source_listener data_source_listener = {
2074 data_source_target,
2075 data_source_send,
2076 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002077};
2078
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002079static void
2080fullscreen_handler(struct window *window, void *data)
2081{
2082 struct terminal *terminal = data;
2083
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002084 window_set_fullscreen(window, !window_is_fullscreen(terminal->window));
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002085}
2086
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002087static void
2088close_handler(struct window *window, void *data)
2089{
2090 struct terminal *terminal = data;
2091
2092 terminal_destroy(terminal);
2093}
2094
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002095static int
2096handle_bound_key(struct terminal *terminal,
2097 struct input *input, uint32_t sym, uint32_t time)
2098{
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002099 struct terminal *new_terminal;
2100
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002101 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002102 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002103 /* Cut selection; terminal doesn't do cut, fall
2104 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002105 case XKB_KEY_C:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002106 terminal->selection =
2107 display_create_data_source(terminal->display);
2108 wl_data_source_offer(terminal->selection,
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002109 "text/plain;charset=utf-8");
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002110 wl_data_source_add_listener(terminal->selection,
2111 &data_source_listener, terminal);
Kristian Høgsbergfee91be2012-06-02 21:21:41 -04002112 input_set_selection(input, terminal->selection,
2113 display_get_serial(terminal->display));
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002114 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002115 case XKB_KEY_V:
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002116 input_receive_selection_data_to_fd(input,
2117 "text/plain;charset=utf-8",
2118 terminal->master);
2119
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002120 return 1;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002121
2122 case XKB_KEY_N:
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002123 new_terminal = terminal_create(terminal->display);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002124 if (terminal_run(new_terminal, option_shell))
2125 terminal_destroy(new_terminal);
2126
2127 return 1;
2128
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002129 default:
2130 return 0;
2131 }
2132}
2133
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002134static void
2135key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002136 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2137 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002138{
2139 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002140 char ch[MAX_RESPONSE];
Kristian Høgsberg88fd4082012-06-21 15:55:03 -04002141 uint32_t modifiers, serial;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002142 int ret, len = 0;
2143 bool convert_utf8 = true;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002144
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002145 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002146 if ((modifiers & MOD_CONTROL_MASK) &&
2147 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002148 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2149 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002150 return;
2151
Daniel Stonea8468712012-11-07 17:51:35 +11002152 /* Map keypad symbols to 'normal' equivalents before processing */
2153 switch (sym) {
2154 case XKB_KEY_KP_Space:
2155 sym = XKB_KEY_space;
2156 break;
2157 case XKB_KEY_KP_Tab:
2158 sym = XKB_KEY_Tab;
2159 break;
2160 case XKB_KEY_KP_Enter:
2161 sym = XKB_KEY_Return;
2162 break;
2163 case XKB_KEY_KP_Left:
2164 sym = XKB_KEY_Left;
2165 break;
2166 case XKB_KEY_KP_Up:
2167 sym = XKB_KEY_Up;
2168 break;
2169 case XKB_KEY_KP_Right:
2170 sym = XKB_KEY_Right;
2171 break;
2172 case XKB_KEY_KP_Down:
2173 sym = XKB_KEY_Down;
2174 break;
2175 case XKB_KEY_KP_Equal:
2176 sym = XKB_KEY_equal;
2177 break;
2178 case XKB_KEY_KP_Multiply:
2179 sym = XKB_KEY_asterisk;
2180 break;
2181 case XKB_KEY_KP_Add:
2182 sym = XKB_KEY_plus;
2183 break;
2184 case XKB_KEY_KP_Separator:
2185 /* Note this is actually locale-dependent and should mostly be
2186 * a comma. But leave it as period until we one day start
2187 * doing the right thing. */
2188 sym = XKB_KEY_period;
2189 break;
2190 case XKB_KEY_KP_Subtract:
2191 sym = XKB_KEY_minus;
2192 break;
2193 case XKB_KEY_KP_Decimal:
2194 sym = XKB_KEY_period;
2195 break;
2196 case XKB_KEY_KP_Divide:
2197 sym = XKB_KEY_slash;
2198 break;
2199 case XKB_KEY_KP_0:
2200 case XKB_KEY_KP_1:
2201 case XKB_KEY_KP_2:
2202 case XKB_KEY_KP_3:
2203 case XKB_KEY_KP_4:
2204 case XKB_KEY_KP_5:
2205 case XKB_KEY_KP_6:
2206 case XKB_KEY_KP_7:
2207 case XKB_KEY_KP_8:
2208 case XKB_KEY_KP_9:
2209 sym = (sym - XKB_KEY_KP_0) + XKB_KEY_0;
2210 break;
2211 default:
2212 break;
2213 }
2214
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002215 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002216 case XKB_KEY_BackSpace:
Kristian Høgsbergb7f94bf2012-06-21 12:29:36 -04002217 if (modifiers & MOD_ALT_MASK)
2218 ch[len++] = 0x1b;
Kristian Høgsberg71a4cf42012-06-20 17:57:56 -04002219 ch[len++] = 0x7f;
2220 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002221 case XKB_KEY_Tab:
2222 case XKB_KEY_Linefeed:
2223 case XKB_KEY_Clear:
2224 case XKB_KEY_Pause:
2225 case XKB_KEY_Scroll_Lock:
2226 case XKB_KEY_Sys_Req:
2227 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002228 ch[len++] = sym & 0x7f;
2229 break;
2230
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002231 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002232 if (terminal->mode & MODE_LF_NEWLINE) {
2233 ch[len++] = 0x0D;
2234 ch[len++] = 0x0A;
2235 } else {
2236 ch[len++] = 0x0D;
2237 }
2238 break;
2239
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002240 case XKB_KEY_Shift_L:
2241 case XKB_KEY_Shift_R:
2242 case XKB_KEY_Control_L:
2243 case XKB_KEY_Control_R:
2244 case XKB_KEY_Alt_L:
2245 case XKB_KEY_Alt_R:
Kristian Høgsberg22fbcf72012-06-22 12:18:56 -04002246 case XKB_KEY_Meta_L:
2247 case XKB_KEY_Meta_R:
2248 case XKB_KEY_Super_L:
2249 case XKB_KEY_Super_R:
2250 case XKB_KEY_Hyper_L:
2251 case XKB_KEY_Hyper_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002252 break;
2253
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002254 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002255 len = function_key_response('[', 2, modifiers, '~', ch);
2256 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002257 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002258 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2259 ch[len++] = '\x04';
2260 } else {
2261 len = function_key_response('[', 3, modifiers, '~', ch);
2262 }
2263 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002264 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002265 len = function_key_response('[', 5, modifiers, '~', ch);
2266 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002267 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002268 len = function_key_response('[', 6, modifiers, '~', ch);
2269 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002270 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002271 len = function_key_response('O', 1, modifiers, 'P', ch);
2272 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002273 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002274 len = function_key_response('O', 1, modifiers, 'Q', ch);
2275 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002276 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002277 len = function_key_response('O', 1, modifiers, 'R', ch);
2278 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002279 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002280 len = function_key_response('O', 1, modifiers, 'S', ch);
2281 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002282 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002283 len = function_key_response('[', 15, modifiers, '~', ch);
2284 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002285 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002286 len = function_key_response('[', 17, modifiers, '~', ch);
2287 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002288 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002289 len = function_key_response('[', 18, modifiers, '~', ch);
2290 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002291 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002292 len = function_key_response('[', 19, modifiers, '~', ch);
2293 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002294 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002295 len = function_key_response('[', 20, modifiers, '~', ch);
2296 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002297 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002298 len = function_key_response('[', 21, modifiers, '~', ch);
2299 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002300 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002301 len = function_key_response('[', 24, modifiers, '~', ch);
2302 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002303 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002304 /* Handle special keys with alternate mappings */
2305 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2306 if (len != 0) break;
2307
Kristian Høgsberg70163132012-05-08 15:55:39 -04002308 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002309 if (sym >= '3' && sym <= '7')
2310 sym = (sym & 0x1f) + 8;
2311
2312 if (!((sym >= '!' && sym <= '/') ||
2313 (sym >= '8' && sym <= '?') ||
2314 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2315 else if (sym == '2') sym = 0x00;
2316 else if (sym == '/') sym = 0x1F;
2317 else if (sym == '8' || sym == '?') sym = 0x7F;
Kristian Høgsbergae9e0732012-06-21 12:30:15 -04002318 }
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002319 if (modifiers & MOD_ALT_MASK) {
2320 if (terminal->mode & MODE_ALT_SENDS_ESC) {
2321 ch[len++] = 0x1b;
2322 } else {
2323 sym = sym | 0x80;
2324 convert_utf8 = false;
2325 }
Callum Lowcay7e08e902011-01-07 19:47:02 +00002326 }
2327
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002328 if ((sym < 128) ||
2329 (!convert_utf8 && sym < 256)) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002330 ch[len++] = sym;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002331 } else {
2332 ret = xkb_keysym_to_utf8(sym, ch + len,
2333 MAX_RESPONSE - len);
2334 if (ret < 0)
2335 fprintf(stderr,
2336 "Warning: buffer too small to encode "
2337 "UTF8 character\n");
2338 else
2339 len += ret;
2340 }
2341
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002342 break;
2343 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002344
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002345 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04002346 terminal_write(terminal, ch, len);
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002347
2348 /* Hide cursor, except if this was coming from a
2349 * repeating key press. */
2350 serial = display_get_serial(terminal->display);
2351 if (terminal->hide_cursor_serial != serial) {
2352 input_set_pointer_image(input, CURSOR_BLANK);
2353 terminal->hide_cursor_serial = serial;
2354 }
2355 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002356}
2357
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002358static void
2359keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002360 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002361{
2362 struct terminal *terminal = data;
2363
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002364 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002365}
2366
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002367static int wordsep(int ch)
2368{
2369 const char extra[] = "-,./?%&#:_=+@~";
2370
Andre Heider552d12b2012-08-02 20:59:43 +02002371 if (ch > 127)
2372 return 1;
2373
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002374 return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
2375}
2376
2377static int
2378recompute_selection(struct terminal *terminal)
2379{
2380 struct rectangle allocation;
2381 int col, x, width, height;
2382 int start_row, end_row;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002383 int word_start, eol;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002384 int side_margin, top_margin;
2385 int start_x, end_x;
2386 int cw, ch;
2387 union utf8_char *data;
2388
Peng Wuf291f202013-06-06 15:32:41 +08002389 cw = terminal->average_width;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002390 ch = terminal->extents.height;
2391 widget_get_allocation(terminal->widget, &allocation);
2392 width = terminal->width * cw;
2393 height = terminal->height * ch;
2394 side_margin = allocation.x + (allocation.width - width) / 2;
2395 top_margin = allocation.y + (allocation.height - height) / 2;
2396
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002397 start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1;
2398 end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1;
2399
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002400 if (start_row < end_row ||
2401 (start_row == end_row &&
2402 terminal->selection_start_x < terminal->selection_end_x)) {
2403 terminal->selection_start_row = start_row;
2404 terminal->selection_end_row = end_row;
2405 start_x = terminal->selection_start_x;
2406 end_x = terminal->selection_end_x;
2407 } else {
2408 terminal->selection_start_row = end_row;
2409 terminal->selection_end_row = start_row;
2410 start_x = terminal->selection_end_x;
2411 end_x = terminal->selection_start_x;
2412 }
2413
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002414 eol = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002415 if (terminal->selection_start_row < 0) {
2416 terminal->selection_start_row = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002417 terminal->selection_start_col = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002418 } else {
2419 x = side_margin + cw / 2;
2420 data = terminal_get_row(terminal,
2421 terminal->selection_start_row);
2422 word_start = 0;
2423 for (col = 0; col < terminal->width; col++, x += cw) {
2424 if (col == 0 || wordsep(data[col - 1].ch))
2425 word_start = col;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002426 if (data[col].ch != 0)
2427 eol = col + 1;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002428 if (start_x < x)
2429 break;
2430 }
2431
2432 switch (terminal->dragging) {
2433 case SELECT_LINE:
2434 terminal->selection_start_col = 0;
2435 break;
2436 case SELECT_WORD:
2437 terminal->selection_start_col = word_start;
2438 break;
2439 case SELECT_CHAR:
2440 terminal->selection_start_col = col;
2441 break;
2442 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002443 }
2444
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002445 if (terminal->selection_end_row >= terminal->height) {
2446 terminal->selection_end_row = terminal->height;
2447 terminal->selection_end_col = 0;
2448 } else {
2449 x = side_margin + cw / 2;
2450 data = terminal_get_row(terminal, terminal->selection_end_row);
2451 for (col = 0; col < terminal->width; col++, x += cw) {
2452 if (terminal->dragging == SELECT_CHAR && end_x < x)
2453 break;
2454 if (terminal->dragging == SELECT_WORD &&
2455 end_x < x && wordsep(data[col].ch))
2456 break;
2457 }
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002458 terminal->selection_end_col = col;
2459 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002460
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002461 if (terminal->selection_end_col != terminal->selection_start_col ||
2462 terminal->selection_start_row != terminal->selection_end_row) {
2463 col = terminal->selection_end_col;
2464 if (col > 0 && data[col - 1].ch == 0)
2465 terminal->selection_end_col = terminal->width;
2466 data = terminal_get_row(terminal, terminal->selection_start_row);
2467 if (data[terminal->selection_start_col].ch == 0)
2468 terminal->selection_start_col = eol;
2469 }
2470
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002471 return 1;
2472}
2473
Kristian Høgsberg59826582011-01-20 11:56:57 -05002474static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002475button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002476 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002477 uint32_t button,
2478 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002479{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002480 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002481
2482 switch (button) {
2483 case 272:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002484 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002485
2486 if (time - terminal->button_time < 500)
2487 terminal->click_count++;
2488 else
2489 terminal->click_count = 1;
2490
2491 terminal->button_time = time;
2492 terminal->dragging =
2493 (terminal->click_count - 1) % 3 + SELECT_CHAR;
2494
Kristian Høgsberg59826582011-01-20 11:56:57 -05002495 input_get_position(input,
2496 &terminal->selection_start_x,
2497 &terminal->selection_start_y);
2498 terminal->selection_end_x = terminal->selection_start_x;
2499 terminal->selection_end_y = terminal->selection_start_y;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002500 if (recompute_selection(terminal))
2501 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002502 } else {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002503 terminal->dragging = SELECT_NONE;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002504 }
2505 break;
2506 }
2507}
2508
2509static int
Kristian Høgsberg29784402012-06-28 14:27:02 -04002510enter_handler(struct widget *widget,
2511 struct input *input, float x, float y, void *data)
2512{
2513 return CURSOR_IBEAM;
2514}
2515
2516static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002517motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002518 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002519 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002520{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002521 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002522
2523 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002524 input_get_position(input,
2525 &terminal->selection_end_x,
2526 &terminal->selection_end_y);
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002527
2528 if (recompute_selection(terminal))
2529 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002530 }
2531
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002532 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002533}
2534
Alexander Larssonde79dd02013-05-22 14:41:34 +02002535static void
2536output_handler(struct window *window, struct output *output, int enter,
2537 void *data)
2538{
2539 if (enter)
2540 window_set_buffer_transform(window, output_get_transform(output));
2541 window_set_buffer_scale(window, window_get_output_scale(window));
2542 window_schedule_redraw(window);
2543}
2544
Peng Wuf291f202013-06-06 15:32:41 +08002545#ifndef howmany
2546#define howmany(x, y) (((x) + ((y) - 1)) / (y))
2547#endif
2548
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002549static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002550terminal_create(struct display *display)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002551{
2552 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002553 cairo_surface_t *surface;
2554 cairo_t *cr;
Peng Wuf291f202013-06-06 15:32:41 +08002555 cairo_text_extents_t text_extents;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002556
Peter Huttererf3d62272013-08-08 11:57:05 +10002557 terminal = xzalloc(sizeof *terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002558 terminal->color_scheme = &DEFAULT_COLORS;
2559 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002560 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002561 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002562 terminal->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002563 terminal->widget = frame_create(terminal->window, terminal);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002564 window_set_title(terminal->window, "Wayland Terminal");
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002565 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002566
2567 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002568 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002569
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002570 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002571 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002572
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002573 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002574 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002575 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002576 keyboard_focus_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002577 window_set_fullscreen_handler(terminal->window, fullscreen_handler);
Alexander Larssonde79dd02013-05-22 14:41:34 +02002578 window_set_output_handler(terminal->window, output_handler);
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002579 window_set_close_handler(terminal->window, close_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002580
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002581 widget_set_redraw_handler(terminal->widget, redraw_handler);
2582 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002583 widget_set_button_handler(terminal->widget, button_handler);
Kristian Høgsberg29784402012-06-28 14:27:02 -04002584 widget_set_enter_handler(terminal->widget, enter_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002585 widget_set_motion_handler(terminal->widget, motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002586
Kristian Høgsberg09531622010-06-14 23:22:15 -04002587 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2588 cr = cairo_create(surface);
Kristian Høgsberg38912df2012-06-27 17:52:23 -04002589 cairo_set_font_size(cr, option_font_size);
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002590 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002591 CAIRO_FONT_SLANT_NORMAL,
2592 CAIRO_FONT_WEIGHT_BOLD);
2593 terminal->font_bold = cairo_get_scaled_font (cr);
2594 cairo_scaled_font_reference(terminal->font_bold);
2595
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002596 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002597 CAIRO_FONT_SLANT_NORMAL,
2598 CAIRO_FONT_WEIGHT_NORMAL);
2599 terminal->font_normal = cairo_get_scaled_font (cr);
2600 cairo_scaled_font_reference(terminal->font_normal);
2601
Kristian Høgsberg09531622010-06-14 23:22:15 -04002602 cairo_font_extents(cr, &terminal->extents);
Peng Wuf291f202013-06-06 15:32:41 +08002603
2604 /* Compute the average ascii glyph width */
2605 cairo_text_extents(cr, TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS,
2606 &text_extents);
2607 terminal->average_width = howmany
2608 (text_extents.width,
2609 strlen(TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS));
2610 terminal->average_width = ceil(terminal->average_width);
2611
Kristian Høgsberg09531622010-06-14 23:22:15 -04002612 cairo_destroy(cr);
2613 cairo_surface_destroy(surface);
2614
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002615 terminal_resize(terminal, 20, 5); /* Set minimum size first */
Kristian Høgsberga1627922012-06-20 17:30:03 -04002616 terminal_resize(terminal, 80, 25);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002617
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002618 wl_list_insert(terminal_list.prev, &terminal->link);
2619
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002620 return terminal;
2621}
2622
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002623static void
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002624terminal_destroy(struct terminal *terminal)
2625{
Dima Ryazanova85292e2012-11-29 00:27:09 -08002626 display_unwatch_fd(terminal->display, terminal->master);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002627 window_destroy(terminal->window);
2628 close(terminal->master);
2629 wl_list_remove(&terminal->link);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002630
2631 if (wl_list_empty(&terminal_list))
Dima Ryazanova85292e2012-11-29 00:27:09 -08002632 display_exit(terminal->display);
2633
2634 free(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002635}
2636
2637static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002638io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002639{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002640 struct terminal *terminal =
2641 container_of(task, struct terminal, io_task);
2642 char buffer[256];
2643 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002644
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002645 if (events & EPOLLHUP) {
2646 terminal_destroy(terminal);
2647 return;
2648 }
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002649
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002650 len = read(terminal->master, buffer, sizeof buffer);
2651 if (len < 0)
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002652 terminal_destroy(terminal);
2653 else
2654 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002655}
2656
2657static int
2658terminal_run(struct terminal *terminal, const char *path)
2659{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002660 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002661 pid_t pid;
2662
2663 pid = forkpty(&master, NULL, NULL, NULL);
2664 if (pid == 0) {
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04002665 setenv("TERM", option_term, 1);
2666 setenv("COLORTERM", option_term, 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002667 if (execl(path, path, NULL)) {
2668 printf("exec failed: %m\n");
2669 exit(EXIT_FAILURE);
2670 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002671 } else if (pid < 0) {
2672 fprintf(stderr, "failed to fork and create pty (%m).\n");
2673 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002674 }
2675
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002676 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002677 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002678 terminal->io_task.run = io_handler;
2679 display_watch_fd(terminal->display, terminal->master,
2680 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002681
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002682 window_set_fullscreen(terminal->window, option_fullscreen);
2683 if (!window_is_fullscreen(terminal->window))
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002684 terminal_resize(terminal, 80, 24);
2685
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002686 return 0;
2687}
2688
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002689static const struct config_key terminal_config_keys[] = {
2690 { "font", CONFIG_KEY_STRING, &option_font },
Kristian Høgsberg38912df2012-06-27 17:52:23 -04002691 { "font-size", CONFIG_KEY_INTEGER, &option_font_size },
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04002692 { "term", CONFIG_KEY_STRING, &option_term },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002693};
2694
2695static const struct config_section config_sections[] = {
2696 { "terminal",
2697 terminal_config_keys, ARRAY_LENGTH(terminal_config_keys) },
2698};
2699
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002700static const struct weston_option terminal_options[] = {
2701 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002702 { WESTON_OPTION_STRING, "font", 0, &option_font },
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002703 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002704};
2705
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002706int main(int argc, char *argv[])
2707{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002708 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002709 struct terminal *terminal;
Ossama Othmana50e6e42013-05-14 09:48:26 -07002710 int config_fd;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002711
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002712 option_shell = getenv("SHELL");
2713 if (!option_shell)
2714 option_shell = "/bin/bash";
2715
Ossama Othmana50e6e42013-05-14 09:48:26 -07002716 config_fd = open_config_file("weston.ini");
2717 parse_config_file(config_fd,
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002718 config_sections, ARRAY_LENGTH(config_sections),
2719 NULL);
Ossama Othmana50e6e42013-05-14 09:48:26 -07002720 close(config_fd);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002721
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002722 parse_options(terminal_options,
2723 ARRAY_LENGTH(terminal_options), &argc, argv);
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002724
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002725 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002726 if (d == NULL) {
2727 fprintf(stderr, "failed to create display: %m\n");
2728 return -1;
2729 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002730
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002731 wl_list_init(&terminal_list);
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002732 terminal = terminal_create(d);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002733 if (terminal_run(terminal, option_shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002734 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002735
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002736 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002737
2738 return 0;
2739}