blob: 1cc26d0e1cb3c78a1d63b8dae724d6dfad70d1d5 [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;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500713 data = malloc(size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000714 attr_pitch = width * sizeof(struct attr);
715 data_attr = malloc(attr_pitch * height);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000716 tab_ruler = malloc(width);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500717 memset(data, 0, size);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000718 memset(tab_ruler, 0, width);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000719 attr_init(data_attr, terminal->curr_attr, width * height);
720 if (terminal->data && terminal->data_attr) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500721 if (width > terminal->width)
722 l = terminal->width;
723 else
724 l = width;
725
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500726 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500727 total_rows = height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500728 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500729 total_rows = terminal->height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500730 }
731
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000732 for (i = 0; i < total_rows; i++) {
733 memcpy(&data[width * i],
734 terminal_get_row(terminal, i),
735 l * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000736 memcpy(&data_attr[width * i],
737 terminal_get_attr_row(terminal, i),
738 l * sizeof(struct attr));
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000739 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500740
741 free(terminal->data);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000742 free(terminal->data_attr);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000743 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500744 }
745
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000746 terminal->data_pitch = data_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000747 terminal->attr_pitch = attr_pitch;
Callum Lowcay86653ed2011-01-07 19:47:03 +0000748 terminal->margin_bottom =
749 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500750 terminal->width = width;
751 terminal->height = height;
752 terminal->data = data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000753 terminal->data_attr = data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000754 terminal->tab_ruler = tab_ruler;
755 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500756
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000757 /* Update the window size */
758 ws.ws_row = terminal->height;
759 ws.ws_col = terminal->width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500760 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500761 ws.ws_xpixel = allocation.width;
762 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000763 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500764}
765
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500766static void
767resize_handler(struct widget *widget,
768 int32_t width, int32_t height, void *data)
769{
770 struct terminal *terminal = data;
771 int32_t columns, rows, m;
772
773 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800774 columns = (width - m) / (int32_t) terminal->average_width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500775 rows = (height - m) / (int32_t) terminal->extents.height;
776
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400777 if (!window_is_fullscreen(terminal->window) &&
778 !window_is_maximized(terminal->window)) {
Peng Wuf291f202013-06-06 15:32:41 +0800779 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500780 height = rows * terminal->extents.height + m;
781 widget_set_size(terminal->widget, width, height);
782 }
783
784 terminal_resize_cells(terminal, columns, rows);
785}
786
787static void
788terminal_resize(struct terminal *terminal, int columns, int rows)
789{
790 int32_t width, height, m;
791
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400792 if (window_is_fullscreen(terminal->window) ||
793 window_is_maximized(terminal->window))
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500794 return;
795
796 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800797 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500798 height = rows * terminal->extents.height + m;
Kristian Høgsberga1627922012-06-20 17:30:03 -0400799
800 frame_set_child_size(terminal->widget, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500801}
802
Callum Lowcay30eeae52011-01-07 19:46:55 +0000803struct color_scheme DEFAULT_COLORS = {
804 {
805 {0, 0, 0, 1}, /* black */
806 {0.66, 0, 0, 1}, /* red */
807 {0 , 0.66, 0, 1}, /* green */
808 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
809 {0 , 0 , 0.66, 1}, /* blue */
810 {0.66, 0 , 0.66, 1}, /* magenta */
811 {0, 0.66, 0.66, 1}, /* cyan */
812 {0.66, 0.66, 0.66, 1}, /* light grey */
813 {0.22, 0.33, 0.33, 1}, /* dark grey */
814 {1, 0.33, 0.33, 1}, /* high red */
815 {0.33, 1, 0.33, 1}, /* high green */
816 {1, 1, 0.33, 1}, /* high yellow */
817 {0.33, 0.33, 1, 1}, /* high blue */
818 {1, 0.33, 1, 1}, /* high magenta */
819 {0.33, 1, 1, 1}, /* high cyan */
820 {1, 1, 1, 1} /* white */
821 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500822 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000823 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
824};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400825
Kristian Høgsberg22106762008-12-08 13:50:07 -0500826static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500827terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
828{
829 cairo_set_source_rgba(cr,
830 terminal->color_table[index].r,
831 terminal->color_table[index].g,
832 terminal->color_table[index].b,
833 terminal->color_table[index].a);
834}
835
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500836static void
837terminal_send_selection(struct terminal *terminal, int fd)
838{
839 int row, col;
840 union utf8_char *p_row;
841 union decoded_attr attr;
842 FILE *fp;
843 int len;
844
845 fp = fdopen(fd, "w");
846 for (row = 0; row < terminal->height; row++) {
847 p_row = terminal_get_row(terminal, row);
848 for (col = 0; col < terminal->width; col++) {
849 /* get the attributes for this character cell */
850 terminal_decode_attr(terminal, row, col, &attr);
851 if (!attr.attr.s)
852 continue;
853 len = strnlen((char *) p_row[col].byte, 4);
Kristian Høgsberg0dee6472012-07-01 21:25:41 -0400854 if (len > 0)
855 fwrite(p_row[col].byte, 1, len, fp);
856 if (len == 0 || col == terminal->width - 1) {
857 fwrite("\n", 1, 1, fp);
858 break;
859 }
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500860 }
861 }
862 fclose(fp);
863}
864
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500865struct glyph_run {
866 struct terminal *terminal;
867 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400868 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500869 union decoded_attr attr;
870 cairo_glyph_t glyphs[256], *g;
871};
872
873static void
874glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
875{
876 run->terminal = terminal;
877 run->cr = cr;
878 run->g = run->glyphs;
879 run->count = 0;
880 run->attr.key = 0;
881}
882
883static void
884glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
885{
886 cairo_scaled_font_t *font;
887
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500888 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
889 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300890 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500891 font = run->terminal->font_bold;
892 else
893 font = run->terminal->font_normal;
894 cairo_set_scaled_font(run->cr, font);
895 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300896 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500897
Callum Lowcay9d708b02011-01-12 20:06:17 +1300898 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
899 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500900 run->g = run->glyphs;
901 run->count = 0;
902 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300903 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500904}
905
906static void
907glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
908{
909 int num_glyphs;
910 cairo_scaled_font_t *font;
911
912 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
913
Callum Lowcay9d708b02011-01-12 20:06:17 +1300914 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500915 font = run->terminal->font_bold;
916 else
917 font = run->terminal->font_normal;
918
919 cairo_move_to(run->cr, x, y);
920 cairo_scaled_font_text_to_glyphs (font, x, y,
921 (char *) c->byte, 4,
922 &run->g, &num_glyphs,
923 NULL, NULL, NULL);
924 run->g += num_glyphs;
925 run->count += num_glyphs;
926}
927
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500928
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500929static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500930redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500931{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500932 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500933 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500934 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000935 int top_margin, side_margin;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600936 int row, col, cursor_x, cursor_y;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000937 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500938 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000939 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500940 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500941 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500942 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500943 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +0800944 double average_width;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500945
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500946 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500947 widget_get_allocation(terminal->widget, &allocation);
Alexander Larssonde79dd02013-05-22 14:41:34 +0200948 cr = widget_cairo_create(terminal->widget);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500949 cairo_rectangle(cr, allocation.x, allocation.y,
950 allocation.width, allocation.height);
951 cairo_clip(cr);
952 cairo_push_group(cr);
953
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500954 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500955 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500956 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500957
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500958 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500959
Kristian Høgsberg59826582011-01-20 11:56:57 -0500960 extents = terminal->extents;
Peng Wuf291f202013-06-06 15:32:41 +0800961 average_width = terminal->average_width;
962 side_margin = (allocation.width - terminal->width * average_width) / 2;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500963 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500964
Callum Lowcay30eeae52011-01-07 19:46:55 +0000965 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500966 cairo_translate(cr, allocation.x + side_margin,
967 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500968 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000969 for (row = 0; row < terminal->height; row++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000970 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000971 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500972 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000973
Callum Lowcay9d708b02011-01-12 20:06:17 +1300974 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -0500975 continue;
976
Callum Lowcay9d708b02011-01-12 20:06:17 +1300977 terminal_set_color(terminal, cr, attr.attr.bg);
Peng Wuf291f202013-06-06 15:32:41 +0800978 cairo_move_to(cr, col * average_width,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500979 row * extents.height);
Peng Wuf291f202013-06-06 15:32:41 +0800980 cairo_rel_line_to(cr, average_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000981 cairo_rel_line_to(cr, 0, 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_close_path(cr);
984 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500985 }
986 }
Callum Lowcay30eeae52011-01-07 19:46:55 +0000987
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500988 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
989
990 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500991 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500992 for (row = 0; row < terminal->height; row++) {
993 p_row = terminal_get_row(terminal, row);
994 for (col = 0; col < terminal->width; col++) {
995 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500996 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500997
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500998 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000999
Peng Wuf291f202013-06-06 15:32:41 +08001000 text_x = col * average_width;
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001001 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +13001002 if (attr.attr.a & ATTRMASK_UNDERLINE) {
1003 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +00001004 cairo_move_to(cr, text_x, (double)text_y + 1.5);
Peng Wuf291f202013-06-06 15:32:41 +08001005 cairo_line_to(cr, text_x + average_width, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001006 cairo_stroke(cr);
1007 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -05001008
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001009 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001010 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001011 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001012
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001013 attr.key = ~0;
1014 glyph_run_flush(&run, attr);
1015
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001016 if ((terminal->mode & MODE_SHOW_CURSOR) &&
1017 !window_has_focus(terminal->window)) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001018 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001019
Callum Lowcay30eeae52011-01-07 19:46:55 +00001020 cairo_set_line_width(cr, 1);
Peng Wuf291f202013-06-06 15:32:41 +08001021 cairo_move_to(cr, terminal->column * average_width + d,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001022 terminal->row * extents.height + d);
Peng Wuf291f202013-06-06 15:32:41 +08001023 cairo_rel_line_to(cr, average_width - 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001024 cairo_rel_line_to(cr, 0, extents.height - 2 * 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_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001027
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001028 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001029 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001030
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001031 cairo_pop_group_to_source(cr);
1032 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001033 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001034 cairo_surface_destroy(surface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001035
1036 if (terminal->send_cursor_position) {
1037 cursor_x = side_margin + allocation.x +
Peng Wuf291f202013-06-06 15:32:41 +08001038 terminal->column * average_width;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001039 cursor_y = top_margin + allocation.y +
1040 terminal->row * extents.height;
1041 window_set_text_cursor_position(terminal->window,
1042 cursor_x, cursor_y);
1043 terminal->send_cursor_position = 0;
1044 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001045}
1046
1047static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001048terminal_write(struct terminal *terminal, const char *data, size_t length)
1049{
1050 if (write(terminal->master, data, length) < 0)
1051 abort();
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001052 terminal->send_cursor_position = 1;
Kristian Høgsberg26130862011-08-24 11:30:21 -04001053}
1054
1055static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001056terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001057
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001058static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001059handle_char(struct terminal *terminal, union utf8_char utf8);
1060
1061static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001062handle_sgr(struct terminal *terminal, int code);
1063
1064static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001065handle_term_parameter(struct terminal *terminal, int code, int sr)
1066{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001067 int i;
1068
Callum Lowcay67a201d2011-01-12 19:23:41 +13001069 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001070 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001071 case 1: /* DECCKM */
1072 if (sr) terminal->key_mode = KM_APPLICATION;
1073 else terminal->key_mode = KM_NORMAL;
1074 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001075 case 2: /* DECANM */
1076 /* No VT52 support yet */
1077 terminal->g0 = CS_US;
1078 terminal->g1 = CS_US;
1079 terminal->cs = terminal->g0;
1080 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001081 case 3: /* DECCOLM */
1082 if (sr)
1083 terminal_resize(terminal, 132, 24);
1084 else
1085 terminal_resize(terminal, 80, 24);
1086
1087 /* set columns, but also home cursor and clear screen */
1088 terminal->row = 0; terminal->column = 0;
1089 for (i = 0; i < terminal->height; i++) {
1090 memset(terminal_get_row(terminal, i),
1091 0, terminal->data_pitch);
1092 attr_init(terminal_get_attr_row(terminal, i),
1093 terminal->curr_attr, terminal->width);
1094 }
1095 break;
1096 case 5: /* DECSCNM */
1097 if (sr) terminal->mode |= MODE_INVERSE;
1098 else terminal->mode &= ~MODE_INVERSE;
1099 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001100 case 6: /* DECOM */
1101 terminal->origin_mode = sr;
1102 if (terminal->origin_mode)
1103 terminal->row = terminal->margin_top;
1104 else
1105 terminal->row = 0;
1106 terminal->column = 0;
1107 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001108 case 7: /* DECAWM */
1109 if (sr) terminal->mode |= MODE_AUTOWRAP;
1110 else terminal->mode &= ~MODE_AUTOWRAP;
1111 break;
1112 case 8: /* DECARM */
1113 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1114 else terminal->mode &= ~MODE_AUTOREPEAT;
1115 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001116 case 12: /* Very visible cursor (CVVIS) */
1117 /* FIXME: What do we do here. */
1118 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001119 case 25:
1120 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1121 else terminal->mode &= ~MODE_SHOW_CURSOR;
1122 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001123 case 1034: /* smm/rmm, meta mode on/off */
1124 /* ignore */
1125 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001126 case 1037: /* deleteSendsDel */
1127 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1128 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1129 break;
1130 case 1039: /* altSendsEscape */
1131 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1132 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1133 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001134 case 1049: /* rmcup/smcup, alternate screen */
1135 /* Ignore. Should be possible to implement,
1136 * but it's kind of annoying. */
1137 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001138 default:
1139 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1140 break;
1141 }
1142 } else {
1143 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001144 case 4: /* IRM */
1145 if (sr) terminal->mode |= MODE_IRM;
1146 else terminal->mode &= ~MODE_IRM;
1147 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001148 case 20: /* LNM */
1149 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1150 else terminal->mode &= ~MODE_LF_NEWLINE;
1151 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001152 default:
1153 fprintf(stderr, "Unknown parameter: %d\n", code);
1154 break;
1155 }
1156 }
1157}
1158
1159static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001160handle_dcs(struct terminal *terminal)
1161{
1162}
1163
1164static void
1165handle_osc(struct terminal *terminal)
1166{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001167 char *p;
1168 int code;
1169
1170 terminal->escape[terminal->escape_length++] = '\0';
1171 p = &terminal->escape[2];
1172 code = strtol(p, &p, 10);
1173 if (*p == ';') p++;
1174
1175 switch (code) {
1176 case 0: /* Icon name and window title */
1177 case 1: /* Icon label */
1178 case 2: /* Window title*/
1179 window_set_title(terminal->window, p);
1180 break;
1181 default:
1182 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1183 break;
1184 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001185}
1186
1187static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001188handle_escape(struct terminal *terminal)
1189{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001190 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001191 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001192 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001193 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001194 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001195 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001196 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001197
1198 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001199 i = 0;
1200 p = &terminal->escape[2];
1201 while ((isdigit(*p) || *p == ';') && i < 10) {
1202 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001203 if (!set[i]) {
1204 args[i] = 0;
1205 set[i] = 1;
1206 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001207 p++;
1208 i++;
1209 } else {
1210 args[i] = strtol(p, &p, 10);
1211 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001212 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001213 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001214
1215 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001216 case '@': /* ICH */
1217 count = set[0] ? args[0] : 1;
1218 if (count == 0) count = 1;
1219 terminal_shift_line(terminal, count);
1220 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001221 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001222 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001223 if (count == 0) count = 1;
1224 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001225 terminal->row -= count;
1226 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001227 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001228 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001229 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001230 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001231 if (count == 0) count = 1;
1232 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001233 terminal->row += count;
1234 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001235 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001236 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001237 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001238 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001239 if (count == 0) count = 1;
1240 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001241 terminal->column += count;
1242 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001243 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001244 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001245 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001246 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001247 if (count == 0) count = 1;
1248 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001249 terminal->column -= count;
1250 else
1251 terminal->column = 0;
1252 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001253 case 'E': /* CNL */
1254 count = set[0] ? args[0] : 1;
1255 if (terminal->row + count <= terminal->margin_bottom)
1256 terminal->row += count;
1257 else
1258 terminal->row = terminal->margin_bottom;
1259 terminal->column = 0;
1260 break;
1261 case 'F': /* CPL */
1262 count = set[0] ? args[0] : 1;
1263 if (terminal->row - count >= terminal->margin_top)
1264 terminal->row -= count;
1265 else
1266 terminal->row = terminal->margin_top;
1267 terminal->column = 0;
1268 break;
1269 case 'G': /* CHA */
1270 y = set[0] ? args[0] : 1;
1271 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1272
1273 terminal->column = y - 1;
1274 break;
1275 case 'f': /* HVP */
1276 case 'H': /* CUP */
1277 x = (set[1] ? args[1] : 1) - 1;
1278 x = x < 0 ? 0 :
1279 (x >= terminal->width ? terminal->width - 1 : x);
1280
1281 y = (set[0] ? args[0] : 1) - 1;
1282 if (terminal->origin_mode) {
1283 y += terminal->margin_top;
1284 y = y < terminal->margin_top ? terminal->margin_top :
1285 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1286 } else {
1287 y = y < 0 ? 0 :
1288 (y >= terminal->height ? terminal->height - 1 : y);
1289 }
1290
1291 terminal->row = y;
1292 terminal->column = x;
1293 break;
1294 case 'I': /* CHT */
1295 count = set[0] ? args[0] : 1;
1296 if (count == 0) count = 1;
1297 while (count > 0 && terminal->column < terminal->width) {
1298 if (terminal->tab_ruler[terminal->column]) count--;
1299 terminal->column++;
1300 }
1301 terminal->column--;
1302 break;
1303 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001304 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001305 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001306 if (!set[0] || args[0] == 0 || args[0] > 2) {
1307 memset(&row[terminal->column],
1308 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1309 attr_init(&attr_row[terminal->column],
1310 terminal->curr_attr, terminal->width - terminal->column);
1311 for (i = terminal->row + 1; i < terminal->height; i++) {
1312 memset(terminal_get_row(terminal, i),
1313 0, terminal->data_pitch);
1314 attr_init(terminal_get_attr_row(terminal, i),
1315 terminal->curr_attr, terminal->width);
1316 }
1317 } else if (args[0] == 1) {
1318 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1319 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1320 for (i = 0; i < terminal->row; i++) {
1321 memset(terminal_get_row(terminal, i),
1322 0, terminal->data_pitch);
1323 attr_init(terminal_get_attr_row(terminal, i),
1324 terminal->curr_attr, terminal->width);
1325 }
1326 } else if (args[0] == 2) {
1327 for (i = 0; i < terminal->height; i++) {
1328 memset(terminal_get_row(terminal, i),
1329 0, terminal->data_pitch);
1330 attr_init(terminal_get_attr_row(terminal, i),
1331 terminal->curr_attr, terminal->width);
1332 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001333 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001334 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001335 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001336 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001337 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001338 if (!set[0] || args[0] == 0 || args[0] > 2) {
1339 memset(&row[terminal->column], 0,
1340 (terminal->width - terminal->column) * sizeof(union utf8_char));
1341 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1342 terminal->width - terminal->column);
1343 } else if (args[0] == 1) {
1344 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1345 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1346 } else if (args[0] == 2) {
1347 memset(row, 0, terminal->data_pitch);
1348 attr_init(attr_row, terminal->curr_attr, terminal->width);
1349 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001350 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001351 case 'L': /* IL */
1352 count = set[0] ? args[0] : 1;
1353 if (count == 0) count = 1;
1354 if (terminal->row >= terminal->margin_top &&
1355 terminal->row < terminal->margin_bottom)
1356 {
1357 top = terminal->margin_top;
1358 terminal->margin_top = terminal->row;
1359 terminal_scroll(terminal, 0 - count);
1360 terminal->margin_top = top;
1361 } else if (terminal->row == terminal->margin_bottom) {
1362 memset(terminal_get_row(terminal, terminal->row),
1363 0, terminal->data_pitch);
1364 attr_init(terminal_get_attr_row(terminal, terminal->row),
1365 terminal->curr_attr, terminal->width);
1366 }
1367 break;
1368 case 'M': /* DL */
1369 count = set[0] ? args[0] : 1;
1370 if (count == 0) count = 1;
1371 if (terminal->row >= terminal->margin_top &&
1372 terminal->row < terminal->margin_bottom)
1373 {
1374 top = terminal->margin_top;
1375 terminal->margin_top = terminal->row;
1376 terminal_scroll(terminal, count);
1377 terminal->margin_top = top;
1378 } else if (terminal->row == terminal->margin_bottom) {
1379 memset(terminal_get_row(terminal, terminal->row),
1380 0, terminal->data_pitch);
1381 }
1382 break;
1383 case 'P': /* DCH */
1384 count = set[0] ? args[0] : 1;
1385 if (count == 0) count = 1;
1386 terminal_shift_line(terminal, 0 - count);
1387 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001388 case 'S': /* SU */
1389 terminal_scroll(terminal, set[0] ? args[0] : 1);
1390 break;
1391 case 'T': /* SD */
1392 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1393 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001394 case 'X': /* ECH */
1395 count = set[0] ? args[0] : 1;
1396 if (count == 0) count = 1;
1397 if ((terminal->column + count) > terminal->width)
1398 count = terminal->width - terminal->column;
1399 row = terminal_get_row(terminal, terminal->row);
1400 attr_row = terminal_get_attr_row(terminal, terminal->row);
1401 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1402 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1403 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001404 case 'Z': /* CBT */
1405 count = set[0] ? args[0] : 1;
1406 if (count == 0) count = 1;
1407 while (count > 0 && terminal->column >= 0) {
1408 if (terminal->tab_ruler[terminal->column]) count--;
1409 terminal->column--;
1410 }
1411 terminal->column++;
1412 break;
1413 case '`': /* HPA */
1414 y = set[0] ? args[0] : 1;
1415 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1416
1417 terminal->column = y - 1;
1418 break;
1419 case 'b': /* REP */
1420 count = set[0] ? args[0] : 1;
1421 if (count == 0) count = 1;
1422 if (terminal->last_char.byte[0])
1423 for (i = 0; i < count; i++)
1424 handle_char(terminal, terminal->last_char);
1425 terminal->last_char.byte[0] = 0;
1426 break;
1427 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001428 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001429 break;
1430 case 'd': /* VPA */
1431 x = set[0] ? args[0] : 1;
1432 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1433
1434 terminal->row = x - 1;
1435 break;
1436 case 'g': /* TBC */
1437 if (!set[0] || args[0] == 0) {
1438 terminal->tab_ruler[terminal->column] = 0;
1439 } else if (args[0] == 3) {
1440 memset(terminal->tab_ruler, 0, terminal->width);
1441 }
1442 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001443 case 'h': /* SM */
1444 for(i = 0; i < 10 && set[i]; i++) {
1445 handle_term_parameter(terminal, args[i], 1);
1446 }
1447 break;
1448 case 'l': /* RM */
1449 for(i = 0; i < 10 && set[i]; i++) {
1450 handle_term_parameter(terminal, args[i], 0);
1451 }
1452 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001453 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001454 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001455 if (i <= 7 && set[i] && set[i + 1] &&
1456 set[i + 2] && args[i + 1] == 5)
1457 {
1458 if (args[i] == 38) {
1459 handle_sgr(terminal, args[i + 2] + 256);
1460 break;
1461 } else if (args[i] == 48) {
1462 handle_sgr(terminal, args[i + 2] + 512);
1463 break;
1464 }
1465 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001466 if(set[i]) {
1467 handle_sgr(terminal, args[i]);
1468 } else if(i == 0) {
1469 handle_sgr(terminal, 0);
1470 break;
1471 } else {
1472 break;
1473 }
1474 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001475 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001476 case 'n': /* DSR */
1477 i = set[0] ? args[0] : 0;
1478 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001479 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001480 } else if (i == 6) {
1481 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1482 terminal->origin_mode ?
1483 terminal->row+terminal->margin_top : terminal->row+1,
1484 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001485 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001486 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001487 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001488 case 'r':
1489 if(!set[0]) {
1490 terminal->margin_top = 0;
1491 terminal->margin_bottom = terminal->height-1;
1492 terminal->row = 0;
1493 terminal->column = 0;
1494 } else {
1495 top = (set[0] ? args[0] : 1) - 1;
1496 top = top < 0 ? 0 :
1497 (top >= terminal->height ? terminal->height - 1 : top);
1498 bottom = (set[1] ? args[1] : 1) - 1;
1499 bottom = bottom < 0 ? 0 :
1500 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1501 if(bottom > top) {
1502 terminal->margin_top = top;
1503 terminal->margin_bottom = bottom;
1504 } else {
1505 terminal->margin_top = 0;
1506 terminal->margin_bottom = terminal->height-1;
1507 }
1508 if(terminal->origin_mode)
1509 terminal->row = terminal->margin_top;
1510 else
1511 terminal->row = 0;
1512 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001513 }
1514 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001515 case 's':
1516 terminal->saved_row = terminal->row;
1517 terminal->saved_column = terminal->column;
1518 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001519 case 't': /* windowOps */
1520 if (!set[0]) break;
1521 switch (args[0]) {
1522 case 4: /* resize px */
1523 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001524 widget_schedule_resize(terminal->widget,
1525 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001526 }
1527 break;
1528 case 8: /* resize ch */
1529 if (set[1] && set[2]) {
1530 terminal_resize(terminal, args[2], args[1]);
1531 }
1532 break;
1533 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001534 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001535 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1536 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001537 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001538 break;
1539 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001540 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001541 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1542 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001543 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001544 break;
1545 case 18: /* report ch */
1546 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1547 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001548 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001549 break;
1550 case 21: /* report title */
1551 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1552 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001553 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001554 break;
1555 default:
1556 if (args[0] >= 24)
1557 terminal_resize(terminal, terminal->width, args[0]);
1558 else
1559 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1560 break;
1561 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001562 case 'u':
1563 terminal->row = terminal->saved_row;
1564 terminal->column = terminal->saved_column;
1565 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001566 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001567 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001568 break;
1569 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001570}
1571
1572static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001573handle_non_csi_escape(struct terminal *terminal, char code)
1574{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001575 switch(code) {
1576 case 'M': /* RI */
1577 terminal->row -= 1;
1578 if(terminal->row < terminal->margin_top) {
1579 terminal->row = terminal->margin_top;
1580 terminal_scroll(terminal, -1);
1581 }
1582 break;
1583 case 'E': /* NEL */
1584 terminal->column = 0;
1585 // fallthrough
1586 case 'D': /* IND */
1587 terminal->row += 1;
1588 if(terminal->row > terminal->margin_bottom) {
1589 terminal->row = terminal->margin_bottom;
1590 terminal_scroll(terminal, +1);
1591 }
1592 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001593 case 'c': /* RIS */
1594 terminal_init(terminal);
1595 break;
1596 case 'H': /* HTS */
1597 terminal->tab_ruler[terminal->column] = 1;
1598 break;
1599 case '7': /* DECSC */
1600 terminal->saved_row = terminal->row;
1601 terminal->saved_column = terminal->column;
1602 terminal->saved_attr = terminal->curr_attr;
1603 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001604 terminal->saved_cs = terminal->cs;
1605 terminal->saved_g0 = terminal->g0;
1606 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001607 break;
1608 case '8': /* DECRC */
1609 terminal->row = terminal->saved_row;
1610 terminal->column = terminal->saved_column;
1611 terminal->curr_attr = terminal->saved_attr;
1612 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001613 terminal->cs = terminal->saved_cs;
1614 terminal->g0 = terminal->saved_g0;
1615 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001616 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001617 case '=': /* DECPAM */
1618 terminal->key_mode = KM_APPLICATION;
1619 break;
1620 case '>': /* DECPNM */
1621 terminal->key_mode = KM_NORMAL;
1622 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001623 default:
1624 fprintf(stderr, "Unknown escape code: %c\n", code);
1625 break;
1626 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001627}
1628
1629static void
1630handle_special_escape(struct terminal *terminal, char special, char code)
1631{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001632 int i, numChars;
1633
1634 if (special == '#') {
1635 switch(code) {
1636 case '8':
1637 /* fill with 'E', no cheap way to do this */
1638 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1639 numChars = terminal->width * terminal->height;
1640 for(i = 0; i < numChars; i++) {
1641 terminal->data[i].byte[0] = 'E';
1642 }
1643 break;
1644 default:
1645 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1646 break;
1647 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001648 } else if (special == '(' || special == ')') {
1649 switch(code) {
1650 case '0':
1651 if (special == '(')
1652 terminal->g0 = CS_SPECIAL;
1653 else
1654 terminal->g1 = CS_SPECIAL;
1655 break;
1656 case 'A':
1657 if (special == '(')
1658 terminal->g0 = CS_UK;
1659 else
1660 terminal->g1 = CS_UK;
1661 break;
1662 case 'B':
1663 if (special == '(')
1664 terminal->g0 = CS_US;
1665 else
1666 terminal->g1 = CS_US;
1667 break;
1668 default:
1669 fprintf(stderr, "Unknown character set %c\n", code);
1670 break;
1671 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001672 } else {
1673 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1674 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001675}
1676
1677static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001678handle_sgr(struct terminal *terminal, int code)
1679{
1680 switch(code) {
1681 case 0:
1682 terminal->curr_attr = terminal->color_scheme->default_attr;
1683 break;
1684 case 1:
1685 terminal->curr_attr.a |= ATTRMASK_BOLD;
1686 if (terminal->curr_attr.fg < 8)
1687 terminal->curr_attr.fg += 8;
1688 break;
1689 case 4:
1690 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1691 break;
1692 case 5:
1693 terminal->curr_attr.a |= ATTRMASK_BLINK;
1694 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001695 case 8:
1696 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1697 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001698 case 2:
1699 case 21:
1700 case 22:
1701 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1702 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1703 terminal->curr_attr.fg -= 8;
1704 break;
1705 case 24:
1706 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1707 break;
1708 case 25:
1709 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1710 break;
1711 case 7:
1712 case 26:
1713 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1714 break;
1715 case 27:
1716 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1717 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001718 case 28:
1719 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1720 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001721 case 39:
1722 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1723 break;
1724 case 49:
1725 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1726 break;
1727 default:
1728 if(code >= 30 && code <= 37) {
1729 terminal->curr_attr.fg = code - 30;
1730 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1731 terminal->curr_attr.fg += 8;
1732 } else if(code >= 40 && code <= 47) {
1733 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001734 } else if (code >= 90 && code <= 97) {
1735 terminal->curr_attr.fg = code - 90 + 8;
1736 } else if (code >= 100 && code <= 107) {
1737 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001738 } else if(code >= 256 && code < 512) {
1739 terminal->curr_attr.fg = code - 256;
1740 } else if(code >= 512 && code < 768) {
1741 terminal->curr_attr.bg = code - 512;
1742 } else {
1743 fprintf(stderr, "Unknown SGR code: %d\n", code);
1744 }
1745 break;
1746 }
1747}
1748
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001749/* Returns 1 if c was special, otherwise 0 */
1750static int
1751handle_special_char(struct terminal *terminal, char c)
1752{
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001753 union utf8_char *row;
1754 struct attr *attr_row;
1755
1756 row = terminal_get_row(terminal, terminal->row);
1757 attr_row = terminal_get_attr_row(terminal, terminal->row);
1758
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001759 switch(c) {
1760 case '\r':
1761 terminal->column = 0;
1762 break;
1763 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001764 if (terminal->mode & MODE_LF_NEWLINE) {
1765 terminal->column = 0;
1766 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001767 /* fallthrough */
1768 case '\v':
1769 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001770 terminal->row++;
1771 if(terminal->row > terminal->margin_bottom) {
1772 terminal->row = terminal->margin_bottom;
1773 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001774 }
1775
1776 break;
1777 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001778 while (terminal->column < terminal->width) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001779 if (terminal->mode & MODE_IRM)
1780 terminal_shift_line(terminal, +1);
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001781
1782 if (row[terminal->column].byte[0] == '\0') {
1783 row[terminal->column].byte[0] = ' ';
1784 row[terminal->column].byte[1] = '\0';
1785 attr_row[terminal->column] = terminal->curr_attr;
1786 }
1787
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001788 terminal->column++;
Kristian Høgsbergcca3c2f2012-06-20 15:56:13 -04001789 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001790 }
1791 if (terminal->column >= terminal->width) {
1792 terminal->column = terminal->width - 1;
1793 }
1794
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001795 break;
1796 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001797 if (terminal->column >= terminal->width) {
1798 terminal->column = terminal->width - 2;
1799 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001800 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001801 } else if (terminal->mode & MODE_AUTOWRAP) {
1802 terminal->column = terminal->width - 1;
1803 terminal->row -= 1;
1804 if (terminal->row < terminal->margin_top) {
1805 terminal->row = terminal->margin_top;
1806 terminal_scroll(terminal, -1);
1807 }
1808 }
1809
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001810 break;
1811 case '\a':
1812 /* Bell */
1813 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001814 case '\x0E': /* SO */
1815 terminal->cs = terminal->g1;
1816 break;
1817 case '\x0F': /* SI */
1818 terminal->cs = terminal->g0;
1819 break;
Kristian Høgsberg2a1aa4e2012-08-03 09:37:05 -04001820 case '\0':
1821 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001822 default:
1823 return 0;
1824 }
1825
1826 return 1;
1827}
1828
1829static void
1830handle_char(struct terminal *terminal, union utf8_char utf8)
1831{
1832 union utf8_char *row;
1833 struct attr *attr_row;
1834
1835 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001836
1837 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001838
1839 /* There are a whole lot of non-characters, control codes,
1840 * and formatting codes that should probably be ignored,
1841 * for example: */
1842 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1843 /* BOM, ignore */
1844 return;
1845 }
1846
1847 /* Some of these non-characters should be translated, e.g.: */
1848 if (utf8.byte[0] < 32) {
1849 utf8.byte[0] = utf8.byte[0] + 64;
1850 }
1851
1852 /* handle right margin effects */
1853 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001854 if (terminal->mode & MODE_AUTOWRAP) {
1855 terminal->column = 0;
1856 terminal->row += 1;
1857 if (terminal->row > terminal->margin_bottom) {
1858 terminal->row = terminal->margin_bottom;
1859 terminal_scroll(terminal, +1);
1860 }
1861 } else {
1862 terminal->column--;
1863 }
1864 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001865
1866 row = terminal_get_row(terminal, terminal->row);
1867 attr_row = terminal_get_attr_row(terminal, terminal->row);
1868
Callum Lowcay69e96582011-01-07 19:47:00 +00001869 if (terminal->mode & MODE_IRM)
1870 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001871 row[terminal->column] = utf8;
1872 attr_row[terminal->column++] = terminal->curr_attr;
1873
1874 if (utf8.ch != terminal->last_char.ch)
1875 terminal->last_char = utf8;
1876}
1877
Callum Lowcay30eeae52011-01-07 19:46:55 +00001878static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001879escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1880{
1881 int len, i;
1882
1883 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1884 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1885 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1886 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1887 else len = 1; /* Invalid, cannot happen */
1888
1889 if (terminal->escape_length + len <= MAX_ESCAPE) {
1890 for (i = 0; i < len; i++)
1891 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1892 terminal->escape_length += len;
1893 } else if (terminal->escape_length < MAX_ESCAPE) {
1894 terminal->escape[terminal->escape_length++] = 0;
1895 }
1896}
1897
1898static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001899terminal_data(struct terminal *terminal, const char *data, size_t length)
1900{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001901 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001902 union utf8_char utf8;
1903 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001904
1905 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001906 parser_state =
1907 utf8_next_char(&terminal->state_machine, data[i]);
1908 switch(parser_state) {
1909 case utf8state_accept:
1910 utf8.ch = terminal->state_machine.s.ch;
1911 break;
1912 case utf8state_reject:
1913 /* the unicode replacement character */
1914 utf8.byte[0] = 0xEF;
1915 utf8.byte[1] = 0xBF;
1916 utf8.byte[2] = 0xBD;
1917 utf8.byte[3] = 0x00;
1918 break;
1919 default:
1920 continue;
1921 }
1922
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001923 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001924 switch (terminal->state) {
1925 case escape_state_escape:
1926 escape_append_utf8(terminal, utf8);
1927 switch (utf8.byte[0]) {
1928 case 'P': /* DCS */
1929 terminal->state = escape_state_dcs;
1930 break;
1931 case '[': /* CSI */
1932 terminal->state = escape_state_csi;
1933 break;
1934 case ']': /* OSC */
1935 terminal->state = escape_state_osc;
1936 break;
1937 case '#':
1938 case '(':
1939 case ')': /* special */
1940 terminal->state = escape_state_special;
1941 break;
1942 case '^': /* PM (not implemented) */
1943 case '_': /* APC (not implemented) */
1944 terminal->state = escape_state_ignore;
1945 break;
1946 default:
1947 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001948 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13001949 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001950 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001951 continue;
1952 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001953 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1954 /* do nothing */
1955 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001956 terminal->escape_flags |= ESC_FLAG_WHAT;
1957 } else if (utf8.byte[0] == '>') {
1958 terminal->escape_flags |= ESC_FLAG_GT;
1959 } else if (utf8.byte[0] == '!') {
1960 terminal->escape_flags |= ESC_FLAG_BANG;
1961 } else if (utf8.byte[0] == '$') {
1962 terminal->escape_flags |= ESC_FLAG_CASH;
1963 } else if (utf8.byte[0] == '\'') {
1964 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1965 } else if (utf8.byte[0] == '"') {
1966 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1967 } else if (utf8.byte[0] == ' ') {
1968 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001969 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001970 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001971 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13001972 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001973 }
1974
1975 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1976 utf8.byte[0] == '`')
1977 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001978 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001979 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001980 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001981 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001982 continue;
1983 case escape_state_inner_escape:
1984 if (utf8.byte[0] == '\\') {
1985 terminal->state = escape_state_normal;
1986 if (terminal->outer_state == escape_state_dcs) {
1987 handle_dcs(terminal);
1988 } else if (terminal->outer_state == escape_state_osc) {
1989 handle_osc(terminal);
1990 }
1991 } else if (utf8.byte[0] == '\e') {
1992 terminal->state = terminal->outer_state;
1993 escape_append_utf8(terminal, utf8);
1994 if (terminal->escape_length >= MAX_ESCAPE)
1995 terminal->state = escape_state_normal;
1996 } else {
1997 terminal->state = terminal->outer_state;
1998 if (terminal->escape_length < MAX_ESCAPE)
1999 terminal->escape[terminal->escape_length++] = '\e';
2000 escape_append_utf8(terminal, utf8);
2001 if (terminal->escape_length >= MAX_ESCAPE)
2002 terminal->state = escape_state_normal;
2003 }
2004 continue;
2005 case escape_state_dcs:
2006 case escape_state_osc:
2007 case escape_state_ignore:
2008 if (utf8.byte[0] == '\e') {
2009 terminal->outer_state = terminal->state;
2010 terminal->state = escape_state_inner_escape;
2011 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
2012 terminal->state = escape_state_normal;
2013 handle_osc(terminal);
2014 } else {
2015 escape_append_utf8(terminal, utf8);
2016 if (terminal->escape_length >= MAX_ESCAPE)
2017 terminal->state = escape_state_normal;
2018 }
2019 continue;
2020 case escape_state_special:
2021 escape_append_utf8(terminal, utf8);
2022 terminal->state = escape_state_normal;
2023 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2024 handle_special_escape(terminal, terminal->escape[1],
2025 utf8.byte[0]);
2026 }
2027 continue;
2028 default:
2029 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002030 }
2031
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002032 /* this is valid, because ASCII characters are never used to
2033 * introduce a multibyte sequence in UTF-8 */
2034 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002035 terminal->state = escape_state_escape;
2036 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002037 terminal->escape[0] = '\e';
2038 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002039 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002040 } else {
2041 handle_char(terminal, utf8);
2042 } /* if */
2043 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002044
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002045 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002046}
2047
2048static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002049data_source_target(void *data,
2050 struct wl_data_source *source, const char *mime_type)
2051{
2052 fprintf(stderr, "data_source_target, %s\n", mime_type);
2053}
2054
2055static void
2056data_source_send(void *data,
2057 struct wl_data_source *source,
2058 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002059{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002060 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002061
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002062 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002063}
2064
2065static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002066data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002067{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002068 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002069}
2070
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002071static const struct wl_data_source_listener data_source_listener = {
2072 data_source_target,
2073 data_source_send,
2074 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002075};
2076
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002077static void
2078fullscreen_handler(struct window *window, void *data)
2079{
2080 struct terminal *terminal = data;
2081
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002082 window_set_fullscreen(window, !window_is_fullscreen(terminal->window));
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002083}
2084
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002085static void
2086close_handler(struct window *window, void *data)
2087{
2088 struct terminal *terminal = data;
2089
2090 terminal_destroy(terminal);
2091}
2092
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002093static int
2094handle_bound_key(struct terminal *terminal,
2095 struct input *input, uint32_t sym, uint32_t time)
2096{
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002097 struct terminal *new_terminal;
2098
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002099 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002100 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002101 /* Cut selection; terminal doesn't do cut, fall
2102 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002103 case XKB_KEY_C:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002104 terminal->selection =
2105 display_create_data_source(terminal->display);
2106 wl_data_source_offer(terminal->selection,
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002107 "text/plain;charset=utf-8");
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002108 wl_data_source_add_listener(terminal->selection,
2109 &data_source_listener, terminal);
Kristian Høgsbergfee91be2012-06-02 21:21:41 -04002110 input_set_selection(input, terminal->selection,
2111 display_get_serial(terminal->display));
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002112 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002113 case XKB_KEY_V:
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002114 input_receive_selection_data_to_fd(input,
2115 "text/plain;charset=utf-8",
2116 terminal->master);
2117
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002118 return 1;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002119
2120 case XKB_KEY_N:
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002121 new_terminal = terminal_create(terminal->display);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002122 if (terminal_run(new_terminal, option_shell))
2123 terminal_destroy(new_terminal);
2124
2125 return 1;
2126
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002127 default:
2128 return 0;
2129 }
2130}
2131
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002132static void
2133key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002134 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2135 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002136{
2137 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002138 char ch[MAX_RESPONSE];
Kristian Høgsberg88fd4082012-06-21 15:55:03 -04002139 uint32_t modifiers, serial;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002140 int ret, len = 0;
2141 bool convert_utf8 = true;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002142
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002143 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002144 if ((modifiers & MOD_CONTROL_MASK) &&
2145 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002146 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2147 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002148 return;
2149
Daniel Stonea8468712012-11-07 17:51:35 +11002150 /* Map keypad symbols to 'normal' equivalents before processing */
2151 switch (sym) {
2152 case XKB_KEY_KP_Space:
2153 sym = XKB_KEY_space;
2154 break;
2155 case XKB_KEY_KP_Tab:
2156 sym = XKB_KEY_Tab;
2157 break;
2158 case XKB_KEY_KP_Enter:
2159 sym = XKB_KEY_Return;
2160 break;
2161 case XKB_KEY_KP_Left:
2162 sym = XKB_KEY_Left;
2163 break;
2164 case XKB_KEY_KP_Up:
2165 sym = XKB_KEY_Up;
2166 break;
2167 case XKB_KEY_KP_Right:
2168 sym = XKB_KEY_Right;
2169 break;
2170 case XKB_KEY_KP_Down:
2171 sym = XKB_KEY_Down;
2172 break;
2173 case XKB_KEY_KP_Equal:
2174 sym = XKB_KEY_equal;
2175 break;
2176 case XKB_KEY_KP_Multiply:
2177 sym = XKB_KEY_asterisk;
2178 break;
2179 case XKB_KEY_KP_Add:
2180 sym = XKB_KEY_plus;
2181 break;
2182 case XKB_KEY_KP_Separator:
2183 /* Note this is actually locale-dependent and should mostly be
2184 * a comma. But leave it as period until we one day start
2185 * doing the right thing. */
2186 sym = XKB_KEY_period;
2187 break;
2188 case XKB_KEY_KP_Subtract:
2189 sym = XKB_KEY_minus;
2190 break;
2191 case XKB_KEY_KP_Decimal:
2192 sym = XKB_KEY_period;
2193 break;
2194 case XKB_KEY_KP_Divide:
2195 sym = XKB_KEY_slash;
2196 break;
2197 case XKB_KEY_KP_0:
2198 case XKB_KEY_KP_1:
2199 case XKB_KEY_KP_2:
2200 case XKB_KEY_KP_3:
2201 case XKB_KEY_KP_4:
2202 case XKB_KEY_KP_5:
2203 case XKB_KEY_KP_6:
2204 case XKB_KEY_KP_7:
2205 case XKB_KEY_KP_8:
2206 case XKB_KEY_KP_9:
2207 sym = (sym - XKB_KEY_KP_0) + XKB_KEY_0;
2208 break;
2209 default:
2210 break;
2211 }
2212
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002213 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002214 case XKB_KEY_BackSpace:
Kristian Høgsbergb7f94bf2012-06-21 12:29:36 -04002215 if (modifiers & MOD_ALT_MASK)
2216 ch[len++] = 0x1b;
Kristian Høgsberg71a4cf42012-06-20 17:57:56 -04002217 ch[len++] = 0x7f;
2218 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002219 case XKB_KEY_Tab:
2220 case XKB_KEY_Linefeed:
2221 case XKB_KEY_Clear:
2222 case XKB_KEY_Pause:
2223 case XKB_KEY_Scroll_Lock:
2224 case XKB_KEY_Sys_Req:
2225 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002226 ch[len++] = sym & 0x7f;
2227 break;
2228
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002229 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002230 if (terminal->mode & MODE_LF_NEWLINE) {
2231 ch[len++] = 0x0D;
2232 ch[len++] = 0x0A;
2233 } else {
2234 ch[len++] = 0x0D;
2235 }
2236 break;
2237
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002238 case XKB_KEY_Shift_L:
2239 case XKB_KEY_Shift_R:
2240 case XKB_KEY_Control_L:
2241 case XKB_KEY_Control_R:
2242 case XKB_KEY_Alt_L:
2243 case XKB_KEY_Alt_R:
Kristian Høgsberg22fbcf72012-06-22 12:18:56 -04002244 case XKB_KEY_Meta_L:
2245 case XKB_KEY_Meta_R:
2246 case XKB_KEY_Super_L:
2247 case XKB_KEY_Super_R:
2248 case XKB_KEY_Hyper_L:
2249 case XKB_KEY_Hyper_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002250 break;
2251
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002252 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002253 len = function_key_response('[', 2, modifiers, '~', ch);
2254 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002255 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002256 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2257 ch[len++] = '\x04';
2258 } else {
2259 len = function_key_response('[', 3, modifiers, '~', ch);
2260 }
2261 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002262 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002263 len = function_key_response('[', 5, modifiers, '~', ch);
2264 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002265 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002266 len = function_key_response('[', 6, modifiers, '~', ch);
2267 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002268 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002269 len = function_key_response('O', 1, modifiers, 'P', ch);
2270 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002271 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002272 len = function_key_response('O', 1, modifiers, 'Q', ch);
2273 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002274 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002275 len = function_key_response('O', 1, modifiers, 'R', ch);
2276 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002277 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002278 len = function_key_response('O', 1, modifiers, 'S', ch);
2279 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002280 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002281 len = function_key_response('[', 15, modifiers, '~', ch);
2282 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002283 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002284 len = function_key_response('[', 17, modifiers, '~', ch);
2285 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002286 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002287 len = function_key_response('[', 18, modifiers, '~', ch);
2288 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002289 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002290 len = function_key_response('[', 19, modifiers, '~', ch);
2291 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002292 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002293 len = function_key_response('[', 20, modifiers, '~', ch);
2294 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002295 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002296 len = function_key_response('[', 21, modifiers, '~', ch);
2297 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002298 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002299 len = function_key_response('[', 24, modifiers, '~', ch);
2300 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002301 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002302 /* Handle special keys with alternate mappings */
2303 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2304 if (len != 0) break;
2305
Kristian Høgsberg70163132012-05-08 15:55:39 -04002306 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002307 if (sym >= '3' && sym <= '7')
2308 sym = (sym & 0x1f) + 8;
2309
2310 if (!((sym >= '!' && sym <= '/') ||
2311 (sym >= '8' && sym <= '?') ||
2312 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2313 else if (sym == '2') sym = 0x00;
2314 else if (sym == '/') sym = 0x1F;
2315 else if (sym == '8' || sym == '?') sym = 0x7F;
Kristian Høgsbergae9e0732012-06-21 12:30:15 -04002316 }
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002317 if (modifiers & MOD_ALT_MASK) {
2318 if (terminal->mode & MODE_ALT_SENDS_ESC) {
2319 ch[len++] = 0x1b;
2320 } else {
2321 sym = sym | 0x80;
2322 convert_utf8 = false;
2323 }
Callum Lowcay7e08e902011-01-07 19:47:02 +00002324 }
2325
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002326 if ((sym < 128) ||
2327 (!convert_utf8 && sym < 256)) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002328 ch[len++] = sym;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002329 } else {
2330 ret = xkb_keysym_to_utf8(sym, ch + len,
2331 MAX_RESPONSE - len);
2332 if (ret < 0)
2333 fprintf(stderr,
2334 "Warning: buffer too small to encode "
2335 "UTF8 character\n");
2336 else
2337 len += ret;
2338 }
2339
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002340 break;
2341 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002342
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002343 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04002344 terminal_write(terminal, ch, len);
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002345
2346 /* Hide cursor, except if this was coming from a
2347 * repeating key press. */
2348 serial = display_get_serial(terminal->display);
2349 if (terminal->hide_cursor_serial != serial) {
2350 input_set_pointer_image(input, CURSOR_BLANK);
2351 terminal->hide_cursor_serial = serial;
2352 }
2353 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002354}
2355
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002356static void
2357keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002358 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002359{
2360 struct terminal *terminal = data;
2361
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002362 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002363}
2364
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002365static int wordsep(int ch)
2366{
2367 const char extra[] = "-,./?%&#:_=+@~";
2368
Andre Heider552d12b2012-08-02 20:59:43 +02002369 if (ch > 127)
2370 return 1;
2371
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002372 return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
2373}
2374
2375static int
2376recompute_selection(struct terminal *terminal)
2377{
2378 struct rectangle allocation;
2379 int col, x, width, height;
2380 int start_row, end_row;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002381 int word_start, eol;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002382 int side_margin, top_margin;
2383 int start_x, end_x;
2384 int cw, ch;
2385 union utf8_char *data;
2386
Peng Wuf291f202013-06-06 15:32:41 +08002387 cw = terminal->average_width;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002388 ch = terminal->extents.height;
2389 widget_get_allocation(terminal->widget, &allocation);
2390 width = terminal->width * cw;
2391 height = terminal->height * ch;
2392 side_margin = allocation.x + (allocation.width - width) / 2;
2393 top_margin = allocation.y + (allocation.height - height) / 2;
2394
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002395 start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1;
2396 end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1;
2397
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002398 if (start_row < end_row ||
2399 (start_row == end_row &&
2400 terminal->selection_start_x < terminal->selection_end_x)) {
2401 terminal->selection_start_row = start_row;
2402 terminal->selection_end_row = end_row;
2403 start_x = terminal->selection_start_x;
2404 end_x = terminal->selection_end_x;
2405 } else {
2406 terminal->selection_start_row = end_row;
2407 terminal->selection_end_row = start_row;
2408 start_x = terminal->selection_end_x;
2409 end_x = terminal->selection_start_x;
2410 }
2411
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002412 eol = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002413 if (terminal->selection_start_row < 0) {
2414 terminal->selection_start_row = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002415 terminal->selection_start_col = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002416 } else {
2417 x = side_margin + cw / 2;
2418 data = terminal_get_row(terminal,
2419 terminal->selection_start_row);
2420 word_start = 0;
2421 for (col = 0; col < terminal->width; col++, x += cw) {
2422 if (col == 0 || wordsep(data[col - 1].ch))
2423 word_start = col;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002424 if (data[col].ch != 0)
2425 eol = col + 1;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002426 if (start_x < x)
2427 break;
2428 }
2429
2430 switch (terminal->dragging) {
2431 case SELECT_LINE:
2432 terminal->selection_start_col = 0;
2433 break;
2434 case SELECT_WORD:
2435 terminal->selection_start_col = word_start;
2436 break;
2437 case SELECT_CHAR:
2438 terminal->selection_start_col = col;
2439 break;
2440 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002441 }
2442
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002443 if (terminal->selection_end_row >= terminal->height) {
2444 terminal->selection_end_row = terminal->height;
2445 terminal->selection_end_col = 0;
2446 } else {
2447 x = side_margin + cw / 2;
2448 data = terminal_get_row(terminal, terminal->selection_end_row);
2449 for (col = 0; col < terminal->width; col++, x += cw) {
2450 if (terminal->dragging == SELECT_CHAR && end_x < x)
2451 break;
2452 if (terminal->dragging == SELECT_WORD &&
2453 end_x < x && wordsep(data[col].ch))
2454 break;
2455 }
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002456 terminal->selection_end_col = col;
2457 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002458
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002459 if (terminal->selection_end_col != terminal->selection_start_col ||
2460 terminal->selection_start_row != terminal->selection_end_row) {
2461 col = terminal->selection_end_col;
2462 if (col > 0 && data[col - 1].ch == 0)
2463 terminal->selection_end_col = terminal->width;
2464 data = terminal_get_row(terminal, terminal->selection_start_row);
2465 if (data[terminal->selection_start_col].ch == 0)
2466 terminal->selection_start_col = eol;
2467 }
2468
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002469 return 1;
2470}
2471
Kristian Høgsberg59826582011-01-20 11:56:57 -05002472static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002473button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002474 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002475 uint32_t button,
2476 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002477{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002478 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002479
2480 switch (button) {
2481 case 272:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002482 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002483
2484 if (time - terminal->button_time < 500)
2485 terminal->click_count++;
2486 else
2487 terminal->click_count = 1;
2488
2489 terminal->button_time = time;
2490 terminal->dragging =
2491 (terminal->click_count - 1) % 3 + SELECT_CHAR;
2492
Kristian Høgsberg59826582011-01-20 11:56:57 -05002493 input_get_position(input,
2494 &terminal->selection_start_x,
2495 &terminal->selection_start_y);
2496 terminal->selection_end_x = terminal->selection_start_x;
2497 terminal->selection_end_y = terminal->selection_start_y;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002498 if (recompute_selection(terminal))
2499 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002500 } else {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002501 terminal->dragging = SELECT_NONE;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002502 }
2503 break;
2504 }
2505}
2506
2507static int
Kristian Høgsberg29784402012-06-28 14:27:02 -04002508enter_handler(struct widget *widget,
2509 struct input *input, float x, float y, void *data)
2510{
2511 return CURSOR_IBEAM;
2512}
2513
2514static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002515motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002516 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002517 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002518{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002519 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002520
2521 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002522 input_get_position(input,
2523 &terminal->selection_end_x,
2524 &terminal->selection_end_y);
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002525
2526 if (recompute_selection(terminal))
2527 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002528 }
2529
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002530 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002531}
2532
Alexander Larssonde79dd02013-05-22 14:41:34 +02002533static void
2534output_handler(struct window *window, struct output *output, int enter,
2535 void *data)
2536{
2537 if (enter)
2538 window_set_buffer_transform(window, output_get_transform(output));
2539 window_set_buffer_scale(window, window_get_output_scale(window));
2540 window_schedule_redraw(window);
2541}
2542
Peng Wuf291f202013-06-06 15:32:41 +08002543#ifndef howmany
2544#define howmany(x, y) (((x) + ((y) - 1)) / (y))
2545#endif
2546
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002547static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002548terminal_create(struct display *display)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002549{
2550 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002551 cairo_surface_t *surface;
2552 cairo_t *cr;
Peng Wuf291f202013-06-06 15:32:41 +08002553 cairo_text_extents_t text_extents;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002554
Brian Lovinbc919262013-08-07 15:34:59 -07002555 terminal = xmalloc(sizeof *terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002556
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002557 memset(terminal, 0, 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}