blob: 32d738c8059634867f5d4dda7c3ae249129bacf5 [file] [log] [blame]
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +020023#include <stdbool.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050024#include <stdint.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <fcntl.h>
29#include <unistd.h>
30#include <math.h>
31#include <time.h>
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050032#include <pty.h>
Kristian Høgsbergf04e8382008-12-08 00:07:49 -050033#include <ctype.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050034#include <cairo.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040035#include <sys/epoll.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050036
Pekka Paalanen50719bc2011-11-22 14:18:50 +020037#include <wayland-client.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050038
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -040039#include "../shared/config-parser.h"
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050040#include "window.h"
41
Kristian Høgsberg0395f302008-12-22 12:14:50 -050042static int option_fullscreen;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -040043static char *option_font = "mono";
Kristian Høgsberg38912df2012-06-27 17:52:23 -040044static int option_font_size = 14;
Kristian Høgsbergde845cf2012-06-20 22:14:31 -040045static char *option_term = "xterm";
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040046static char *option_shell;
47
48static struct wl_list terminal_list;
49
50static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -040051terminal_create(struct display *display);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040052static void
53terminal_destroy(struct terminal *terminal);
54static int
55terminal_run(struct terminal *terminal, const char *path);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050056
Peng Wuf291f202013-06-06 15:32:41 +080057#define TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS \
58 " !\"#$%&'()*+,-./" \
59 "0123456789" \
60 ":;<=>?@" \
61 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
62 "[\\]^_`" \
63 "abcdefghijklmnopqrstuvwxyz" \
64 "{|}~" \
65 ""
66
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050067#define MOD_SHIFT 0x01
68#define MOD_ALT 0x02
69#define MOD_CTRL 0x04
70
Callum Lowcay30eeae52011-01-07 19:46:55 +000071#define ATTRMASK_BOLD 0x01
72#define ATTRMASK_UNDERLINE 0x02
73#define ATTRMASK_BLINK 0x04
74#define ATTRMASK_INVERSE 0x08
Callum Lowcay81179db2011-01-10 12:14:01 +130075#define ATTRMASK_CONCEALED 0x10
Callum Lowcay30eeae52011-01-07 19:46:55 +000076
Callum Lowcayb8609ad2011-01-07 19:46:57 +000077/* Buffer sizes */
Callum Lowcayef57a9b2011-01-14 20:46:23 +130078#define MAX_RESPONSE 256
79#define MAX_ESCAPE 255
Callum Lowcayb8609ad2011-01-07 19:46:57 +000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +000081/* Terminal modes */
82#define MODE_SHOW_CURSOR 0x00000001
83#define MODE_INVERSE 0x00000002
84#define MODE_AUTOWRAP 0x00000004
85#define MODE_AUTOREPEAT 0x00000008
86#define MODE_LF_NEWLINE 0x00000010
Callum Lowcay69e96582011-01-07 19:47:00 +000087#define MODE_IRM 0x00000020
Callum Lowcay7e08e902011-01-07 19:47:02 +000088#define MODE_DELETE_SENDS_DEL 0x00000040
89#define MODE_ALT_SENDS_ESC 0x00000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +000090
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000091union utf8_char {
92 unsigned char byte[4];
93 uint32_t ch;
94};
95
96enum utf8_state {
97 utf8state_start,
98 utf8state_accept,
99 utf8state_reject,
100 utf8state_expect3,
101 utf8state_expect2,
102 utf8state_expect1
103};
104
105struct utf8_state_machine {
106 enum utf8_state state;
107 int len;
108 union utf8_char s;
109};
110
111static void
112init_state_machine(struct utf8_state_machine *machine)
113{
114 machine->state = utf8state_start;
115 machine->len = 0;
116 machine->s.ch = 0;
117}
118
119static enum utf8_state
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400120utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000121{
122 switch(machine->state) {
123 case utf8state_start:
124 case utf8state_accept:
125 case utf8state_reject:
126 machine->s.ch = 0;
127 machine->len = 0;
128 if(c == 0xC0 || c == 0xC1) {
129 /* overlong encoding, reject */
130 machine->state = utf8state_reject;
131 } else if((c & 0x80) == 0) {
132 /* single byte, accept */
133 machine->s.byte[machine->len++] = c;
134 machine->state = utf8state_accept;
135 } else if((c & 0xC0) == 0x80) {
136 /* parser out of sync, ignore byte */
137 machine->state = utf8state_start;
138 } else if((c & 0xE0) == 0xC0) {
139 /* start of two byte sequence */
140 machine->s.byte[machine->len++] = c;
141 machine->state = utf8state_expect1;
142 } else if((c & 0xF0) == 0xE0) {
143 /* start of three byte sequence */
144 machine->s.byte[machine->len++] = c;
145 machine->state = utf8state_expect2;
146 } else if((c & 0xF8) == 0xF0) {
147 /* start of four byte sequence */
148 machine->s.byte[machine->len++] = c;
149 machine->state = utf8state_expect3;
150 } else {
151 /* overlong encoding, reject */
152 machine->state = utf8state_reject;
153 }
154 break;
155 case utf8state_expect3:
156 machine->s.byte[machine->len++] = c;
157 if((c & 0xC0) == 0x80) {
158 /* all good, continue */
159 machine->state = utf8state_expect2;
160 } else {
161 /* missing extra byte, reject */
162 machine->state = utf8state_reject;
163 }
164 break;
165 case utf8state_expect2:
166 machine->s.byte[machine->len++] = c;
167 if((c & 0xC0) == 0x80) {
168 /* all good, continue */
169 machine->state = utf8state_expect1;
170 } else {
171 /* missing extra byte, reject */
172 machine->state = utf8state_reject;
173 }
174 break;
175 case utf8state_expect1:
176 machine->s.byte[machine->len++] = c;
177 if((c & 0xC0) == 0x80) {
178 /* all good, accept */
179 machine->state = utf8state_accept;
180 } else {
181 /* missing extra byte, reject */
182 machine->state = utf8state_reject;
183 }
184 break;
185 default:
186 machine->state = utf8state_reject;
187 break;
188 }
189
190 return machine->state;
191}
192
Callum Lowcay256e72f2011-01-07 19:47:01 +0000193struct char_sub {
194 union utf8_char match;
195 union utf8_char replace;
196};
197/* Set last char_sub match to NULL char */
198typedef struct char_sub *character_set;
199
200struct char_sub CS_US[] = {
201 {{{0, }}, {{0, }}}
202};
203static struct char_sub CS_UK[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200204 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000205 {{{0, }}, {{0, }}}
206};
207static struct char_sub CS_SPECIAL[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200208 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond: ♦ */
209 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell: ▒ */
210 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT: ␉ */
211 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF: ␌ */
212 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR: ␍ */
213 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF: ␊ */
214 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree: ° */
215 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus: ± */
216 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL: ␤ */
217 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT: ␋ */
218 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB: ┘ */
219 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT: ┐ */
220 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT: ┌ */
221 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_LB: └ */
222 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS: ┼ */
223 {{{'o', 0, }}, {{0xE2, 0x8E, 0xBA, 0}}}, /* Horiz. Scan Line 1: ⎺ */
224 {{{'p', 0, }}, {{0xE2, 0x8E, 0xBB, 0}}}, /* Horiz. Scan Line 3: ⎻ */
225 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* Horiz. Scan Line 5: ─ */
226 {{{'r', 0, }}, {{0xE2, 0x8E, 0xBC, 0}}}, /* Horiz. Scan Line 7: ⎼ */
227 {{{'s', 0, }}, {{0xE2, 0x8E, 0xBD, 0}}}, /* Horiz. Scan Line 9: ⎽ */
228 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR: ├ */
229 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL: ┤ */
230 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU: ┴ */
231 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD: ┬ */
232 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V: │ */
233 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE: ≤ */
234 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE: ≥ */
235 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI: π */
236 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ: ≠ */
237 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
238 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT: ⋅ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000239 {{{0, }}, {{0, }}}
240};
241
242static void
243apply_char_set(character_set cs, union utf8_char *utf8)
244{
245 int i = 0;
246
247 while (cs[i].match.byte[0]) {
248 if ((*utf8).ch == cs[i].match.ch) {
249 *utf8 = cs[i].replace;
250 break;
251 }
252 i++;
253 }
254}
255
Callum Lowcay7e08e902011-01-07 19:47:02 +0000256struct key_map {
257 int sym;
258 int num;
259 char escape;
260 char code;
261};
262/* Set last key_sub sym to NULL */
263typedef struct key_map *keyboard_mode;
264
265static struct key_map KM_NORMAL[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400266 { XKB_KEY_Left, 1, '[', 'D' },
267 { XKB_KEY_Right, 1, '[', 'C' },
268 { XKB_KEY_Up, 1, '[', 'A' },
269 { XKB_KEY_Down, 1, '[', 'B' },
270 { XKB_KEY_Home, 1, '[', 'H' },
271 { XKB_KEY_End, 1, '[', 'F' },
272 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000273};
274static struct key_map KM_APPLICATION[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400275 { XKB_KEY_Left, 1, 'O', 'D' },
276 { XKB_KEY_Right, 1, 'O', 'C' },
277 { XKB_KEY_Up, 1, 'O', 'A' },
278 { XKB_KEY_Down, 1, 'O', 'B' },
279 { XKB_KEY_Home, 1, 'O', 'H' },
280 { XKB_KEY_End, 1, 'O', 'F' },
281 { XKB_KEY_KP_Enter, 1, 'O', 'M' },
282 { XKB_KEY_KP_Multiply, 1, 'O', 'j' },
283 { XKB_KEY_KP_Add, 1, 'O', 'k' },
284 { XKB_KEY_KP_Separator, 1, 'O', 'l' },
285 { XKB_KEY_KP_Subtract, 1, 'O', 'm' },
286 { XKB_KEY_KP_Divide, 1, 'O', 'o' },
287 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000288};
289
290static int
291function_key_response(char escape, int num, uint32_t modifiers,
292 char code, char *response)
293{
294 int mod_num = 0;
295 int len;
296
Kristian Høgsberg70163132012-05-08 15:55:39 -0400297 if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
298 if (modifiers & MOD_ALT_MASK) mod_num |= 2;
299 if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000300
301 if (mod_num != 0)
302 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
303 num, mod_num + 1, code);
304 else if (code != '~')
305 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
306 escape, code);
307 else
308 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
309 escape, num, code);
310
311 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
312 else return len;
313}
314
315/* returns the number of bytes written into response,
316 * which must have room for MAX_RESPONSE bytes */
317static int
318apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
319{
320 struct key_map map;
321 int len = 0;
322 int i = 0;
323
324 while (mode[i].sym) {
325 map = mode[i++];
326 if (sym == map.sym) {
327 len = function_key_response(map.escape, map.num,
328 modifiers, map.code,
329 response);
330 break;
331 }
332 }
333
334 return len;
335}
336
Callum Lowcay30eeae52011-01-07 19:46:55 +0000337struct terminal_color { double r, g, b, a; };
338struct attr {
339 unsigned char fg, bg;
340 char a; /* attributes format:
341 * 76543210
Callum Lowcay81179db2011-01-10 12:14:01 +1300342 * cilub */
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500343 char s; /* in selection */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000344};
345struct color_scheme {
346 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500347 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000348 struct attr default_attr;
349};
350
351static void
352attr_init(struct attr *data_attr, struct attr attr, int n)
353{
354 int i;
355 for (i = 0; i < n; i++) {
356 data_attr[i] = attr;
357 }
358}
359
Callum Lowcay67a201d2011-01-12 19:23:41 +1300360enum escape_state {
361 escape_state_normal = 0,
362 escape_state_escape,
363 escape_state_dcs,
364 escape_state_csi,
365 escape_state_osc,
366 escape_state_inner_escape,
367 escape_state_ignore,
368 escape_state_special
369};
370
371#define ESC_FLAG_WHAT 0x01
372#define ESC_FLAG_GT 0x02
373#define ESC_FLAG_BANG 0x04
374#define ESC_FLAG_CASH 0x08
375#define ESC_FLAG_SQUOTE 0x10
376#define ESC_FLAG_DQUOTE 0x20
377#define ESC_FLAG_SPACE 0x40
378
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400379enum {
380 SELECT_NONE,
381 SELECT_CHAR,
382 SELECT_WORD,
383 SELECT_LINE
384};
385
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500386struct terminal {
387 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500388 struct widget *widget;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500389 struct display *display;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000390 union utf8_char *data;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400391 struct task io_task;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000392 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000393 struct attr *data_attr;
394 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000395 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000396 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000397 char saved_origin_mode;
398 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000399 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000400 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000401 character_set cs, g0, g1;
402 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000403 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000404 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500405 int width, height, start, row, column;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000406 int saved_row, saved_column;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600407 int send_cursor_position;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500408 int fd, master;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500409 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300410 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500411 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300412 enum escape_state state;
413 enum escape_state outer_state;
414 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000415 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500416 int margin;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400417 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000418 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400419 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +0800420 double average_width;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500421 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg88fd4082012-06-21 15:55:03 -0400422 uint32_t hide_cursor_serial;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500423
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400424 struct wl_data_source *selection;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400425 uint32_t button_time;
426 int dragging, click_count;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500427 int selection_start_x, selection_start_y;
428 int selection_end_x, selection_end_y;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400429 int selection_start_row, selection_start_col;
430 int selection_end_row, selection_end_col;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -0400431 struct wl_list link;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500432};
433
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000434/* Create default tab stops, every 8 characters */
435static void
436terminal_init_tabs(struct terminal *terminal)
437{
438 int i = 0;
439
440 while (i < terminal->width) {
441 if (i % 8 == 0)
442 terminal->tab_ruler[i] = 1;
443 else
444 terminal->tab_ruler[i] = 0;
445 i++;
446 }
447}
448
Callum Lowcay30eeae52011-01-07 19:46:55 +0000449static void
450terminal_init(struct terminal *terminal)
451{
452 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000453 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000454 terminal->mode = MODE_SHOW_CURSOR |
455 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000456 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000457 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000458
459 terminal->row = 0;
460 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000461
Callum Lowcay256e72f2011-01-07 19:47:01 +0000462 terminal->g0 = CS_US;
463 terminal->g1 = CS_US;
464 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000465 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000466
467 terminal->saved_g0 = terminal->g0;
468 terminal->saved_g1 = terminal->g1;
469 terminal->saved_cs = terminal->cs;
470
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000471 terminal->saved_attr = terminal->curr_attr;
472 terminal->saved_origin_mode = terminal->origin_mode;
473 terminal->saved_row = terminal->row;
474 terminal->saved_column = terminal->column;
475
476 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000477}
478
479static void
480init_color_table(struct terminal *terminal)
481{
482 int c, r;
483 struct terminal_color *color_table = terminal->color_table;
484
485 for (c = 0; c < 256; c ++) {
486 if (c < 16) {
487 color_table[c] = terminal->color_scheme->palette[c];
488 } else if (c < 232) {
489 r = c - 16;
490 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
491 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
492 color_table[c].r = ((double)(r % 6) / 6.0);
493 color_table[c].a = 1.0;
494 } else {
495 r = (c - 232) * 10 + 8;
496 color_table[c].r = ((double) r) / 256.0;
497 color_table[c].g = color_table[c].r;
498 color_table[c].b = color_table[c].r;
499 color_table[c].a = 1.0;
500 }
501 }
502}
503
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000504static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500505terminal_get_row(struct terminal *terminal, int row)
506{
507 int index;
508
509 index = (row + terminal->start) % terminal->height;
510
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000511 return &terminal->data[index * terminal->width];
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500512}
513
Callum Lowcay30eeae52011-01-07 19:46:55 +0000514static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500515terminal_get_attr_row(struct terminal *terminal, int row)
516{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000517 int index;
518
519 index = (row + terminal->start) % terminal->height;
520
521 return &terminal->data_attr[index * terminal->width];
522}
523
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500524union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300525 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500526 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500527};
528
529static void
530terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500531 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500532{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500533 struct attr attr;
534 int foreground, background, tmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500535
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500536 decoded->attr.s = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400537 if (((row == terminal->selection_start_row &&
538 col >= terminal->selection_start_col) ||
539 row > terminal->selection_start_row) &&
540 ((row == terminal->selection_end_row &&
541 col < terminal->selection_end_col) ||
542 row < terminal->selection_end_row))
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500543 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500544
545 /* get the attributes for this character cell */
546 attr = terminal_get_attr_row(terminal, row)[col];
547 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500548 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500549 ((terminal->mode & MODE_SHOW_CURSOR) &&
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400550 window_has_focus(terminal->window) && terminal->row == row &&
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500551 terminal->column == col)) {
552 foreground = attr.bg;
553 background = attr.fg;
554 if (attr.a & ATTRMASK_BOLD) {
555 if (foreground <= 16) foreground |= 0x08;
556 if (background <= 16) background &= 0x07;
557 }
558 } else {
559 foreground = attr.fg;
560 background = attr.bg;
561 }
562
563 if (terminal->mode & MODE_INVERSE) {
564 tmp = foreground;
565 foreground = background;
566 background = tmp;
567 if (attr.a & ATTRMASK_BOLD) {
568 if (foreground <= 16) foreground |= 0x08;
569 if (background <= 16) background &= 0x07;
570 }
571 }
572
Callum Lowcay9d708b02011-01-12 20:06:17 +1300573 decoded->attr.fg = foreground;
574 decoded->attr.bg = background;
575 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000576}
577
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500578
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500579static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000580terminal_scroll_buffer(struct terminal *terminal, int d)
581{
582 int i;
583
584 d = d % (terminal->height + 1);
585 terminal->start = (terminal->start + d) % terminal->height;
586 if (terminal->start < 0) terminal->start = terminal->height + terminal->start;
587 if(d < 0) {
588 d = 0 - d;
589 for(i = 0; i < d; i++) {
590 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
591 attr_init(terminal_get_attr_row(terminal, i),
592 terminal->curr_attr, terminal->width);
593 }
594 } else {
595 for(i = terminal->height - d; i < terminal->height; i++) {
596 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
597 attr_init(terminal_get_attr_row(terminal, i),
598 terminal->curr_attr, terminal->width);
599 }
600 }
Kristian Høgsberg18e928d2012-06-27 19:29:41 -0400601
602 terminal->selection_start_row -= d;
603 terminal->selection_end_row -= d;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000604}
605
606static void
607terminal_scroll_window(struct terminal *terminal, int d)
608{
609 int i;
610 int window_height;
611 int from_row, to_row;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000612
613 // scrolling range is inclusive
614 window_height = terminal->margin_bottom - terminal->margin_top + 1;
615 d = d % (window_height + 1);
616 if(d < 0) {
617 d = 0 - d;
618 to_row = terminal->margin_bottom;
619 from_row = terminal->margin_bottom - d;
620
621 for (i = 0; i < (window_height - d); i++) {
622 memcpy(terminal_get_row(terminal, to_row - i),
623 terminal_get_row(terminal, from_row - i),
624 terminal->data_pitch);
625 memcpy(terminal_get_attr_row(terminal, to_row - i),
626 terminal_get_attr_row(terminal, from_row - i),
627 terminal->attr_pitch);
628 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000629 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
630 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000631 attr_init(terminal_get_attr_row(terminal, i),
632 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000633 }
634 } else {
635 to_row = terminal->margin_top;
636 from_row = terminal->margin_top + d;
637
638 for (i = 0; i < (window_height - d); i++) {
639 memcpy(terminal_get_row(terminal, to_row + i),
640 terminal_get_row(terminal, from_row + i),
641 terminal->data_pitch);
642 memcpy(terminal_get_attr_row(terminal, to_row + i),
643 terminal_get_attr_row(terminal, from_row + i),
644 terminal->attr_pitch);
645 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000646 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
647 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000648 attr_init(terminal_get_attr_row(terminal, i),
649 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000650 }
651 }
652}
653
654static void
655terminal_scroll(struct terminal *terminal, int d)
656{
657 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
658 terminal_scroll_buffer(terminal, d);
659 else
660 terminal_scroll_window(terminal, d);
661}
662
663static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000664terminal_shift_line(struct terminal *terminal, int d)
665{
666 union utf8_char *row;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500667 struct attr *attr_row;
Callum Lowcay69e96582011-01-07 19:47:00 +0000668
669 row = terminal_get_row(terminal, terminal->row);
670 attr_row = terminal_get_attr_row(terminal, terminal->row);
671
672 if ((terminal->width + d) <= terminal->column)
673 d = terminal->column + 1 - terminal->width;
674 if ((terminal->column + d) >= terminal->width)
675 d = terminal->width - terminal->column - 1;
676
677 if (d < 0) {
678 d = 0 - d;
679 memmove(&row[terminal->column],
680 &row[terminal->column + d],
681 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
Callum Lowcay69e96582011-01-07 19:47:00 +0000682 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
683 (terminal->width - terminal->column - d) * sizeof(struct attr));
684 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
685 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
686 } else {
687 memmove(&row[terminal->column + d], &row[terminal->column],
688 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
689 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
690 (terminal->width - terminal->column - d) * sizeof(struct attr));
691 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
692 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
693 }
694}
695
696static void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500697terminal_resize_cells(struct terminal *terminal, int width, int height)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500698{
699 size_t size;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000700 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000701 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000702 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000703 int data_pitch, attr_pitch;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500704 int i, l, total_rows;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500705 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000706 struct winsize ws;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500707
708 if (terminal->width == width && terminal->height == height)
709 return;
710
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000711 data_pitch = width * sizeof(union utf8_char);
712 size = data_pitch * height;
Peter Huttererf3d62272013-08-08 11:57:05 +1000713 data = zalloc(size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000714 attr_pitch = width * sizeof(struct attr);
715 data_attr = malloc(attr_pitch * height);
Peter Huttererf3d62272013-08-08 11:57:05 +1000716 tab_ruler = zalloc(width);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000717 attr_init(data_attr, terminal->curr_attr, width * height);
718 if (terminal->data && terminal->data_attr) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500719 if (width > terminal->width)
720 l = terminal->width;
721 else
722 l = width;
723
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500724 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500725 total_rows = height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500726 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500727 total_rows = terminal->height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500728 }
729
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000730 for (i = 0; i < total_rows; i++) {
731 memcpy(&data[width * i],
732 terminal_get_row(terminal, i),
733 l * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000734 memcpy(&data_attr[width * i],
735 terminal_get_attr_row(terminal, i),
736 l * sizeof(struct attr));
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000737 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500738
739 free(terminal->data);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000740 free(terminal->data_attr);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000741 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500742 }
743
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000744 terminal->data_pitch = data_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000745 terminal->attr_pitch = attr_pitch;
Callum Lowcay86653ed2011-01-07 19:47:03 +0000746 terminal->margin_bottom =
747 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500748 terminal->width = width;
749 terminal->height = height;
750 terminal->data = data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000751 terminal->data_attr = data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000752 terminal->tab_ruler = tab_ruler;
753 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500754
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000755 /* Update the window size */
756 ws.ws_row = terminal->height;
757 ws.ws_col = terminal->width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500758 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500759 ws.ws_xpixel = allocation.width;
760 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000761 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500762}
763
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500764static void
765resize_handler(struct widget *widget,
766 int32_t width, int32_t height, void *data)
767{
768 struct terminal *terminal = data;
769 int32_t columns, rows, m;
770
771 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800772 columns = (width - m) / (int32_t) terminal->average_width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500773 rows = (height - m) / (int32_t) terminal->extents.height;
774
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400775 if (!window_is_fullscreen(terminal->window) &&
776 !window_is_maximized(terminal->window)) {
Peng Wuf291f202013-06-06 15:32:41 +0800777 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500778 height = rows * terminal->extents.height + m;
779 widget_set_size(terminal->widget, width, height);
780 }
781
782 terminal_resize_cells(terminal, columns, rows);
783}
784
785static void
786terminal_resize(struct terminal *terminal, int columns, int rows)
787{
788 int32_t width, height, m;
789
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400790 if (window_is_fullscreen(terminal->window) ||
791 window_is_maximized(terminal->window))
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500792 return;
793
794 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800795 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500796 height = rows * terminal->extents.height + m;
Kristian Høgsberga1627922012-06-20 17:30:03 -0400797
798 frame_set_child_size(terminal->widget, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500799}
800
Callum Lowcay30eeae52011-01-07 19:46:55 +0000801struct color_scheme DEFAULT_COLORS = {
802 {
803 {0, 0, 0, 1}, /* black */
804 {0.66, 0, 0, 1}, /* red */
805 {0 , 0.66, 0, 1}, /* green */
806 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
807 {0 , 0 , 0.66, 1}, /* blue */
808 {0.66, 0 , 0.66, 1}, /* magenta */
809 {0, 0.66, 0.66, 1}, /* cyan */
810 {0.66, 0.66, 0.66, 1}, /* light grey */
811 {0.22, 0.33, 0.33, 1}, /* dark grey */
812 {1, 0.33, 0.33, 1}, /* high red */
813 {0.33, 1, 0.33, 1}, /* high green */
814 {1, 1, 0.33, 1}, /* high yellow */
815 {0.33, 0.33, 1, 1}, /* high blue */
816 {1, 0.33, 1, 1}, /* high magenta */
817 {0.33, 1, 1, 1}, /* high cyan */
818 {1, 1, 1, 1} /* white */
819 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500820 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000821 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
822};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400823
Kristian Høgsberg22106762008-12-08 13:50:07 -0500824static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500825terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
826{
827 cairo_set_source_rgba(cr,
828 terminal->color_table[index].r,
829 terminal->color_table[index].g,
830 terminal->color_table[index].b,
831 terminal->color_table[index].a);
832}
833
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500834static void
835terminal_send_selection(struct terminal *terminal, int fd)
836{
837 int row, col;
838 union utf8_char *p_row;
839 union decoded_attr attr;
840 FILE *fp;
841 int len;
842
843 fp = fdopen(fd, "w");
844 for (row = 0; row < terminal->height; row++) {
845 p_row = terminal_get_row(terminal, row);
846 for (col = 0; col < terminal->width; col++) {
847 /* get the attributes for this character cell */
848 terminal_decode_attr(terminal, row, col, &attr);
849 if (!attr.attr.s)
850 continue;
851 len = strnlen((char *) p_row[col].byte, 4);
Kristian Høgsberg0dee6472012-07-01 21:25:41 -0400852 if (len > 0)
853 fwrite(p_row[col].byte, 1, len, fp);
854 if (len == 0 || col == terminal->width - 1) {
855 fwrite("\n", 1, 1, fp);
856 break;
857 }
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500858 }
859 }
860 fclose(fp);
861}
862
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500863struct glyph_run {
864 struct terminal *terminal;
865 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400866 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500867 union decoded_attr attr;
868 cairo_glyph_t glyphs[256], *g;
869};
870
871static void
872glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
873{
874 run->terminal = terminal;
875 run->cr = cr;
876 run->g = run->glyphs;
877 run->count = 0;
878 run->attr.key = 0;
879}
880
881static void
882glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
883{
884 cairo_scaled_font_t *font;
885
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500886 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
887 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300888 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500889 font = run->terminal->font_bold;
890 else
891 font = run->terminal->font_normal;
892 cairo_set_scaled_font(run->cr, font);
893 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300894 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500895
Callum Lowcay9d708b02011-01-12 20:06:17 +1300896 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
897 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500898 run->g = run->glyphs;
899 run->count = 0;
900 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300901 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500902}
903
904static void
905glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
906{
907 int num_glyphs;
908 cairo_scaled_font_t *font;
909
910 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
911
Callum Lowcay9d708b02011-01-12 20:06:17 +1300912 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500913 font = run->terminal->font_bold;
914 else
915 font = run->terminal->font_normal;
916
917 cairo_move_to(run->cr, x, y);
918 cairo_scaled_font_text_to_glyphs (font, x, y,
919 (char *) c->byte, 4,
920 &run->g, &num_glyphs,
921 NULL, NULL, NULL);
922 run->g += num_glyphs;
923 run->count += num_glyphs;
924}
925
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500926
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500927static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500928redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500929{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500930 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500931 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500932 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000933 int top_margin, side_margin;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600934 int row, col, cursor_x, cursor_y;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000935 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500936 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000937 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500938 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500939 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500940 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500941 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +0800942 double average_width;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500943
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500944 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500945 widget_get_allocation(terminal->widget, &allocation);
Alexander Larssonde79dd02013-05-22 14:41:34 +0200946 cr = widget_cairo_create(terminal->widget);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500947 cairo_rectangle(cr, allocation.x, allocation.y,
948 allocation.width, allocation.height);
949 cairo_clip(cr);
950 cairo_push_group(cr);
951
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500952 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500953 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500954 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500955
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500956 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500957
Kristian Høgsberg59826582011-01-20 11:56:57 -0500958 extents = terminal->extents;
Peng Wuf291f202013-06-06 15:32:41 +0800959 average_width = terminal->average_width;
960 side_margin = (allocation.width - terminal->width * average_width) / 2;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500961 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500962
Callum Lowcay30eeae52011-01-07 19:46:55 +0000963 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500964 cairo_translate(cr, allocation.x + side_margin,
965 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500966 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000967 for (row = 0; row < terminal->height; row++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000968 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000969 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500970 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000971
Callum Lowcay9d708b02011-01-12 20:06:17 +1300972 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -0500973 continue;
974
Callum Lowcay9d708b02011-01-12 20:06:17 +1300975 terminal_set_color(terminal, cr, attr.attr.bg);
Peng Wuf291f202013-06-06 15:32:41 +0800976 cairo_move_to(cr, col * average_width,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500977 row * extents.height);
Peng Wuf291f202013-06-06 15:32:41 +0800978 cairo_rel_line_to(cr, average_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000979 cairo_rel_line_to(cr, 0, 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_close_path(cr);
982 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500983 }
984 }
Callum Lowcay30eeae52011-01-07 19:46:55 +0000985
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500986 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
987
988 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500989 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500990 for (row = 0; row < terminal->height; row++) {
991 p_row = terminal_get_row(terminal, row);
992 for (col = 0; col < terminal->width; col++) {
993 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500994 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500995
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500996 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000997
Peng Wuf291f202013-06-06 15:32:41 +0800998 text_x = col * average_width;
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500999 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +13001000 if (attr.attr.a & ATTRMASK_UNDERLINE) {
1001 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +00001002 cairo_move_to(cr, text_x, (double)text_y + 1.5);
Peng Wuf291f202013-06-06 15:32:41 +08001003 cairo_line_to(cr, text_x + average_width, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001004 cairo_stroke(cr);
1005 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -05001006
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001007 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001008 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001009 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001010
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001011 attr.key = ~0;
1012 glyph_run_flush(&run, attr);
1013
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001014 if ((terminal->mode & MODE_SHOW_CURSOR) &&
1015 !window_has_focus(terminal->window)) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001016 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001017
Callum Lowcay30eeae52011-01-07 19:46:55 +00001018 cairo_set_line_width(cr, 1);
Peng Wuf291f202013-06-06 15:32:41 +08001019 cairo_move_to(cr, terminal->column * average_width + d,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001020 terminal->row * extents.height + d);
Peng Wuf291f202013-06-06 15:32:41 +08001021 cairo_rel_line_to(cr, average_width - 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001022 cairo_rel_line_to(cr, 0, extents.height - 2 * 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_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001025
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001026 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001027 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001028
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001029 cairo_pop_group_to_source(cr);
1030 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001031 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001032 cairo_surface_destroy(surface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001033
1034 if (terminal->send_cursor_position) {
1035 cursor_x = side_margin + allocation.x +
Peng Wuf291f202013-06-06 15:32:41 +08001036 terminal->column * average_width;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001037 cursor_y = top_margin + allocation.y +
1038 terminal->row * extents.height;
1039 window_set_text_cursor_position(terminal->window,
1040 cursor_x, cursor_y);
1041 terminal->send_cursor_position = 0;
1042 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001043}
1044
1045static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001046terminal_write(struct terminal *terminal, const char *data, size_t length)
1047{
1048 if (write(terminal->master, data, length) < 0)
1049 abort();
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001050 terminal->send_cursor_position = 1;
Kristian Høgsberg26130862011-08-24 11:30:21 -04001051}
1052
1053static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001054terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001055
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001056static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001057handle_char(struct terminal *terminal, union utf8_char utf8);
1058
1059static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001060handle_sgr(struct terminal *terminal, int code);
1061
1062static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001063handle_term_parameter(struct terminal *terminal, int code, int sr)
1064{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001065 int i;
1066
Callum Lowcay67a201d2011-01-12 19:23:41 +13001067 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001068 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001069 case 1: /* DECCKM */
1070 if (sr) terminal->key_mode = KM_APPLICATION;
1071 else terminal->key_mode = KM_NORMAL;
1072 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001073 case 2: /* DECANM */
1074 /* No VT52 support yet */
1075 terminal->g0 = CS_US;
1076 terminal->g1 = CS_US;
1077 terminal->cs = terminal->g0;
1078 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001079 case 3: /* DECCOLM */
1080 if (sr)
1081 terminal_resize(terminal, 132, 24);
1082 else
1083 terminal_resize(terminal, 80, 24);
1084
1085 /* set columns, but also home cursor and clear screen */
1086 terminal->row = 0; terminal->column = 0;
1087 for (i = 0; i < terminal->height; i++) {
1088 memset(terminal_get_row(terminal, i),
1089 0, terminal->data_pitch);
1090 attr_init(terminal_get_attr_row(terminal, i),
1091 terminal->curr_attr, terminal->width);
1092 }
1093 break;
1094 case 5: /* DECSCNM */
1095 if (sr) terminal->mode |= MODE_INVERSE;
1096 else terminal->mode &= ~MODE_INVERSE;
1097 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001098 case 6: /* DECOM */
1099 terminal->origin_mode = sr;
1100 if (terminal->origin_mode)
1101 terminal->row = terminal->margin_top;
1102 else
1103 terminal->row = 0;
1104 terminal->column = 0;
1105 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001106 case 7: /* DECAWM */
1107 if (sr) terminal->mode |= MODE_AUTOWRAP;
1108 else terminal->mode &= ~MODE_AUTOWRAP;
1109 break;
1110 case 8: /* DECARM */
1111 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1112 else terminal->mode &= ~MODE_AUTOREPEAT;
1113 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001114 case 12: /* Very visible cursor (CVVIS) */
1115 /* FIXME: What do we do here. */
1116 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001117 case 25:
1118 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1119 else terminal->mode &= ~MODE_SHOW_CURSOR;
1120 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001121 case 1034: /* smm/rmm, meta mode on/off */
1122 /* ignore */
1123 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001124 case 1037: /* deleteSendsDel */
1125 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1126 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1127 break;
1128 case 1039: /* altSendsEscape */
1129 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1130 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1131 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001132 case 1049: /* rmcup/smcup, alternate screen */
1133 /* Ignore. Should be possible to implement,
1134 * but it's kind of annoying. */
1135 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001136 default:
1137 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1138 break;
1139 }
1140 } else {
1141 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001142 case 4: /* IRM */
1143 if (sr) terminal->mode |= MODE_IRM;
1144 else terminal->mode &= ~MODE_IRM;
1145 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001146 case 20: /* LNM */
1147 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1148 else terminal->mode &= ~MODE_LF_NEWLINE;
1149 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001150 default:
1151 fprintf(stderr, "Unknown parameter: %d\n", code);
1152 break;
1153 }
1154 }
1155}
1156
1157static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001158handle_dcs(struct terminal *terminal)
1159{
1160}
1161
1162static void
1163handle_osc(struct terminal *terminal)
1164{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001165 char *p;
1166 int code;
1167
1168 terminal->escape[terminal->escape_length++] = '\0';
1169 p = &terminal->escape[2];
1170 code = strtol(p, &p, 10);
1171 if (*p == ';') p++;
1172
1173 switch (code) {
1174 case 0: /* Icon name and window title */
1175 case 1: /* Icon label */
1176 case 2: /* Window title*/
1177 window_set_title(terminal->window, p);
1178 break;
1179 default:
1180 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1181 break;
1182 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001183}
1184
1185static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001186handle_escape(struct terminal *terminal)
1187{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001188 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001189 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001190 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001191 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001192 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001193 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001194 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001195
1196 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001197 i = 0;
1198 p = &terminal->escape[2];
1199 while ((isdigit(*p) || *p == ';') && i < 10) {
1200 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001201 if (!set[i]) {
1202 args[i] = 0;
1203 set[i] = 1;
1204 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001205 p++;
1206 i++;
1207 } else {
1208 args[i] = strtol(p, &p, 10);
1209 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001210 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001211 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001212
1213 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001214 case '@': /* ICH */
1215 count = set[0] ? args[0] : 1;
1216 if (count == 0) count = 1;
1217 terminal_shift_line(terminal, count);
1218 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001219 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001220 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001221 if (count == 0) count = 1;
1222 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001223 terminal->row -= count;
1224 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001225 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001226 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001227 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001228 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001229 if (count == 0) count = 1;
1230 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001231 terminal->row += count;
1232 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001233 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001234 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001235 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001236 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001237 if (count == 0) count = 1;
1238 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001239 terminal->column += count;
1240 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001241 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001242 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001243 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001244 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001245 if (count == 0) count = 1;
1246 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001247 terminal->column -= count;
1248 else
1249 terminal->column = 0;
1250 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001251 case 'E': /* CNL */
1252 count = set[0] ? args[0] : 1;
1253 if (terminal->row + count <= terminal->margin_bottom)
1254 terminal->row += count;
1255 else
1256 terminal->row = terminal->margin_bottom;
1257 terminal->column = 0;
1258 break;
1259 case 'F': /* CPL */
1260 count = set[0] ? args[0] : 1;
1261 if (terminal->row - count >= terminal->margin_top)
1262 terminal->row -= count;
1263 else
1264 terminal->row = terminal->margin_top;
1265 terminal->column = 0;
1266 break;
1267 case 'G': /* CHA */
1268 y = set[0] ? args[0] : 1;
1269 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1270
1271 terminal->column = y - 1;
1272 break;
1273 case 'f': /* HVP */
1274 case 'H': /* CUP */
1275 x = (set[1] ? args[1] : 1) - 1;
1276 x = x < 0 ? 0 :
1277 (x >= terminal->width ? terminal->width - 1 : x);
1278
1279 y = (set[0] ? args[0] : 1) - 1;
1280 if (terminal->origin_mode) {
1281 y += terminal->margin_top;
1282 y = y < terminal->margin_top ? terminal->margin_top :
1283 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1284 } else {
1285 y = y < 0 ? 0 :
1286 (y >= terminal->height ? terminal->height - 1 : y);
1287 }
1288
1289 terminal->row = y;
1290 terminal->column = x;
1291 break;
1292 case 'I': /* CHT */
1293 count = set[0] ? args[0] : 1;
1294 if (count == 0) count = 1;
1295 while (count > 0 && terminal->column < terminal->width) {
1296 if (terminal->tab_ruler[terminal->column]) count--;
1297 terminal->column++;
1298 }
1299 terminal->column--;
1300 break;
1301 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001302 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001303 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001304 if (!set[0] || args[0] == 0 || args[0] > 2) {
1305 memset(&row[terminal->column],
1306 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1307 attr_init(&attr_row[terminal->column],
1308 terminal->curr_attr, terminal->width - terminal->column);
1309 for (i = terminal->row + 1; i < terminal->height; i++) {
1310 memset(terminal_get_row(terminal, i),
1311 0, terminal->data_pitch);
1312 attr_init(terminal_get_attr_row(terminal, i),
1313 terminal->curr_attr, terminal->width);
1314 }
1315 } else if (args[0] == 1) {
1316 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1317 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1318 for (i = 0; i < terminal->row; i++) {
1319 memset(terminal_get_row(terminal, i),
1320 0, terminal->data_pitch);
1321 attr_init(terminal_get_attr_row(terminal, i),
1322 terminal->curr_attr, terminal->width);
1323 }
1324 } else if (args[0] == 2) {
1325 for (i = 0; i < terminal->height; i++) {
1326 memset(terminal_get_row(terminal, i),
1327 0, terminal->data_pitch);
1328 attr_init(terminal_get_attr_row(terminal, i),
1329 terminal->curr_attr, terminal->width);
1330 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001331 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001332 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001333 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001334 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001335 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001336 if (!set[0] || args[0] == 0 || args[0] > 2) {
1337 memset(&row[terminal->column], 0,
1338 (terminal->width - terminal->column) * sizeof(union utf8_char));
1339 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1340 terminal->width - terminal->column);
1341 } else if (args[0] == 1) {
1342 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1343 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1344 } else if (args[0] == 2) {
1345 memset(row, 0, terminal->data_pitch);
1346 attr_init(attr_row, terminal->curr_attr, terminal->width);
1347 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001348 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001349 case 'L': /* IL */
1350 count = set[0] ? args[0] : 1;
1351 if (count == 0) count = 1;
1352 if (terminal->row >= terminal->margin_top &&
1353 terminal->row < terminal->margin_bottom)
1354 {
1355 top = terminal->margin_top;
1356 terminal->margin_top = terminal->row;
1357 terminal_scroll(terminal, 0 - count);
1358 terminal->margin_top = top;
1359 } else if (terminal->row == terminal->margin_bottom) {
1360 memset(terminal_get_row(terminal, terminal->row),
1361 0, terminal->data_pitch);
1362 attr_init(terminal_get_attr_row(terminal, terminal->row),
1363 terminal->curr_attr, terminal->width);
1364 }
1365 break;
1366 case 'M': /* DL */
1367 count = set[0] ? args[0] : 1;
1368 if (count == 0) count = 1;
1369 if (terminal->row >= terminal->margin_top &&
1370 terminal->row < terminal->margin_bottom)
1371 {
1372 top = terminal->margin_top;
1373 terminal->margin_top = terminal->row;
1374 terminal_scroll(terminal, count);
1375 terminal->margin_top = top;
1376 } else if (terminal->row == terminal->margin_bottom) {
1377 memset(terminal_get_row(terminal, terminal->row),
1378 0, terminal->data_pitch);
1379 }
1380 break;
1381 case 'P': /* DCH */
1382 count = set[0] ? args[0] : 1;
1383 if (count == 0) count = 1;
1384 terminal_shift_line(terminal, 0 - count);
1385 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001386 case 'S': /* SU */
1387 terminal_scroll(terminal, set[0] ? args[0] : 1);
1388 break;
1389 case 'T': /* SD */
1390 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1391 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001392 case 'X': /* ECH */
1393 count = set[0] ? args[0] : 1;
1394 if (count == 0) count = 1;
1395 if ((terminal->column + count) > terminal->width)
1396 count = terminal->width - terminal->column;
1397 row = terminal_get_row(terminal, terminal->row);
1398 attr_row = terminal_get_attr_row(terminal, terminal->row);
1399 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1400 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1401 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001402 case 'Z': /* CBT */
1403 count = set[0] ? args[0] : 1;
1404 if (count == 0) count = 1;
1405 while (count > 0 && terminal->column >= 0) {
1406 if (terminal->tab_ruler[terminal->column]) count--;
1407 terminal->column--;
1408 }
1409 terminal->column++;
1410 break;
1411 case '`': /* HPA */
1412 y = set[0] ? args[0] : 1;
1413 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1414
1415 terminal->column = y - 1;
1416 break;
1417 case 'b': /* REP */
1418 count = set[0] ? args[0] : 1;
1419 if (count == 0) count = 1;
1420 if (terminal->last_char.byte[0])
1421 for (i = 0; i < count; i++)
1422 handle_char(terminal, terminal->last_char);
1423 terminal->last_char.byte[0] = 0;
1424 break;
1425 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001426 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001427 break;
1428 case 'd': /* VPA */
1429 x = set[0] ? args[0] : 1;
1430 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1431
1432 terminal->row = x - 1;
1433 break;
1434 case 'g': /* TBC */
1435 if (!set[0] || args[0] == 0) {
1436 terminal->tab_ruler[terminal->column] = 0;
1437 } else if (args[0] == 3) {
1438 memset(terminal->tab_ruler, 0, terminal->width);
1439 }
1440 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001441 case 'h': /* SM */
1442 for(i = 0; i < 10 && set[i]; i++) {
1443 handle_term_parameter(terminal, args[i], 1);
1444 }
1445 break;
1446 case 'l': /* RM */
1447 for(i = 0; i < 10 && set[i]; i++) {
1448 handle_term_parameter(terminal, args[i], 0);
1449 }
1450 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001451 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001452 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001453 if (i <= 7 && set[i] && set[i + 1] &&
1454 set[i + 2] && args[i + 1] == 5)
1455 {
1456 if (args[i] == 38) {
1457 handle_sgr(terminal, args[i + 2] + 256);
1458 break;
1459 } else if (args[i] == 48) {
1460 handle_sgr(terminal, args[i + 2] + 512);
1461 break;
1462 }
1463 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001464 if(set[i]) {
1465 handle_sgr(terminal, args[i]);
1466 } else if(i == 0) {
1467 handle_sgr(terminal, 0);
1468 break;
1469 } else {
1470 break;
1471 }
1472 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001473 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001474 case 'n': /* DSR */
1475 i = set[0] ? args[0] : 0;
1476 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001477 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001478 } else if (i == 6) {
1479 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1480 terminal->origin_mode ?
1481 terminal->row+terminal->margin_top : terminal->row+1,
1482 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001483 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001484 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001485 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001486 case 'r':
1487 if(!set[0]) {
1488 terminal->margin_top = 0;
1489 terminal->margin_bottom = terminal->height-1;
1490 terminal->row = 0;
1491 terminal->column = 0;
1492 } else {
1493 top = (set[0] ? args[0] : 1) - 1;
1494 top = top < 0 ? 0 :
1495 (top >= terminal->height ? terminal->height - 1 : top);
1496 bottom = (set[1] ? args[1] : 1) - 1;
1497 bottom = bottom < 0 ? 0 :
1498 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1499 if(bottom > top) {
1500 terminal->margin_top = top;
1501 terminal->margin_bottom = bottom;
1502 } else {
1503 terminal->margin_top = 0;
1504 terminal->margin_bottom = terminal->height-1;
1505 }
1506 if(terminal->origin_mode)
1507 terminal->row = terminal->margin_top;
1508 else
1509 terminal->row = 0;
1510 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001511 }
1512 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001513 case 's':
1514 terminal->saved_row = terminal->row;
1515 terminal->saved_column = terminal->column;
1516 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001517 case 't': /* windowOps */
1518 if (!set[0]) break;
1519 switch (args[0]) {
1520 case 4: /* resize px */
1521 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001522 widget_schedule_resize(terminal->widget,
1523 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001524 }
1525 break;
1526 case 8: /* resize ch */
1527 if (set[1] && set[2]) {
1528 terminal_resize(terminal, args[2], args[1]);
1529 }
1530 break;
1531 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001532 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001533 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1534 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001535 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001536 break;
1537 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001538 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001539 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1540 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001541 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001542 break;
1543 case 18: /* report ch */
1544 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1545 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001546 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001547 break;
1548 case 21: /* report title */
1549 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1550 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001551 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001552 break;
1553 default:
1554 if (args[0] >= 24)
1555 terminal_resize(terminal, terminal->width, args[0]);
1556 else
1557 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1558 break;
1559 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001560 case 'u':
1561 terminal->row = terminal->saved_row;
1562 terminal->column = terminal->saved_column;
1563 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001564 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001565 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001566 break;
1567 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001568}
1569
1570static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001571handle_non_csi_escape(struct terminal *terminal, char code)
1572{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001573 switch(code) {
1574 case 'M': /* RI */
1575 terminal->row -= 1;
1576 if(terminal->row < terminal->margin_top) {
1577 terminal->row = terminal->margin_top;
1578 terminal_scroll(terminal, -1);
1579 }
1580 break;
1581 case 'E': /* NEL */
1582 terminal->column = 0;
1583 // fallthrough
1584 case 'D': /* IND */
1585 terminal->row += 1;
1586 if(terminal->row > terminal->margin_bottom) {
1587 terminal->row = terminal->margin_bottom;
1588 terminal_scroll(terminal, +1);
1589 }
1590 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001591 case 'c': /* RIS */
1592 terminal_init(terminal);
1593 break;
1594 case 'H': /* HTS */
1595 terminal->tab_ruler[terminal->column] = 1;
1596 break;
1597 case '7': /* DECSC */
1598 terminal->saved_row = terminal->row;
1599 terminal->saved_column = terminal->column;
1600 terminal->saved_attr = terminal->curr_attr;
1601 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001602 terminal->saved_cs = terminal->cs;
1603 terminal->saved_g0 = terminal->g0;
1604 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001605 break;
1606 case '8': /* DECRC */
1607 terminal->row = terminal->saved_row;
1608 terminal->column = terminal->saved_column;
1609 terminal->curr_attr = terminal->saved_attr;
1610 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001611 terminal->cs = terminal->saved_cs;
1612 terminal->g0 = terminal->saved_g0;
1613 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001614 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001615 case '=': /* DECPAM */
1616 terminal->key_mode = KM_APPLICATION;
1617 break;
1618 case '>': /* DECPNM */
1619 terminal->key_mode = KM_NORMAL;
1620 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001621 default:
1622 fprintf(stderr, "Unknown escape code: %c\n", code);
1623 break;
1624 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001625}
1626
1627static void
1628handle_special_escape(struct terminal *terminal, char special, char code)
1629{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001630 int i, numChars;
1631
1632 if (special == '#') {
1633 switch(code) {
1634 case '8':
1635 /* fill with 'E', no cheap way to do this */
1636 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1637 numChars = terminal->width * terminal->height;
1638 for(i = 0; i < numChars; i++) {
1639 terminal->data[i].byte[0] = 'E';
1640 }
1641 break;
1642 default:
1643 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1644 break;
1645 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001646 } else if (special == '(' || special == ')') {
1647 switch(code) {
1648 case '0':
1649 if (special == '(')
1650 terminal->g0 = CS_SPECIAL;
1651 else
1652 terminal->g1 = CS_SPECIAL;
1653 break;
1654 case 'A':
1655 if (special == '(')
1656 terminal->g0 = CS_UK;
1657 else
1658 terminal->g1 = CS_UK;
1659 break;
1660 case 'B':
1661 if (special == '(')
1662 terminal->g0 = CS_US;
1663 else
1664 terminal->g1 = CS_US;
1665 break;
1666 default:
1667 fprintf(stderr, "Unknown character set %c\n", code);
1668 break;
1669 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001670 } else {
1671 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1672 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001673}
1674
1675static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001676handle_sgr(struct terminal *terminal, int code)
1677{
1678 switch(code) {
1679 case 0:
1680 terminal->curr_attr = terminal->color_scheme->default_attr;
1681 break;
1682 case 1:
1683 terminal->curr_attr.a |= ATTRMASK_BOLD;
1684 if (terminal->curr_attr.fg < 8)
1685 terminal->curr_attr.fg += 8;
1686 break;
1687 case 4:
1688 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1689 break;
1690 case 5:
1691 terminal->curr_attr.a |= ATTRMASK_BLINK;
1692 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001693 case 8:
1694 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1695 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001696 case 2:
1697 case 21:
1698 case 22:
1699 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1700 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1701 terminal->curr_attr.fg -= 8;
1702 break;
1703 case 24:
1704 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1705 break;
1706 case 25:
1707 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1708 break;
1709 case 7:
1710 case 26:
1711 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1712 break;
1713 case 27:
1714 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1715 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001716 case 28:
1717 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1718 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001719 case 39:
1720 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1721 break;
1722 case 49:
1723 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1724 break;
1725 default:
1726 if(code >= 30 && code <= 37) {
1727 terminal->curr_attr.fg = code - 30;
1728 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1729 terminal->curr_attr.fg += 8;
1730 } else if(code >= 40 && code <= 47) {
1731 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001732 } else if (code >= 90 && code <= 97) {
1733 terminal->curr_attr.fg = code - 90 + 8;
1734 } else if (code >= 100 && code <= 107) {
1735 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001736 } else if(code >= 256 && code < 512) {
1737 terminal->curr_attr.fg = code - 256;
1738 } else if(code >= 512 && code < 768) {
1739 terminal->curr_attr.bg = code - 512;
1740 } else {
1741 fprintf(stderr, "Unknown SGR code: %d\n", code);
1742 }
1743 break;
1744 }
1745}
1746
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001747/* Returns 1 if c was special, otherwise 0 */
1748static int
1749handle_special_char(struct terminal *terminal, char c)
1750{
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001751 union utf8_char *row;
1752 struct attr *attr_row;
1753
1754 row = terminal_get_row(terminal, terminal->row);
1755 attr_row = terminal_get_attr_row(terminal, terminal->row);
1756
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001757 switch(c) {
1758 case '\r':
1759 terminal->column = 0;
1760 break;
1761 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001762 if (terminal->mode & MODE_LF_NEWLINE) {
1763 terminal->column = 0;
1764 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001765 /* fallthrough */
1766 case '\v':
1767 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001768 terminal->row++;
1769 if(terminal->row > terminal->margin_bottom) {
1770 terminal->row = terminal->margin_bottom;
1771 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001772 }
1773
1774 break;
1775 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001776 while (terminal->column < terminal->width) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001777 if (terminal->mode & MODE_IRM)
1778 terminal_shift_line(terminal, +1);
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001779
1780 if (row[terminal->column].byte[0] == '\0') {
1781 row[terminal->column].byte[0] = ' ';
1782 row[terminal->column].byte[1] = '\0';
1783 attr_row[terminal->column] = terminal->curr_attr;
1784 }
1785
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001786 terminal->column++;
Kristian Høgsbergcca3c2f2012-06-20 15:56:13 -04001787 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001788 }
1789 if (terminal->column >= terminal->width) {
1790 terminal->column = terminal->width - 1;
1791 }
1792
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001793 break;
1794 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001795 if (terminal->column >= terminal->width) {
1796 terminal->column = terminal->width - 2;
1797 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001798 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001799 } else if (terminal->mode & MODE_AUTOWRAP) {
1800 terminal->column = terminal->width - 1;
1801 terminal->row -= 1;
1802 if (terminal->row < terminal->margin_top) {
1803 terminal->row = terminal->margin_top;
1804 terminal_scroll(terminal, -1);
1805 }
1806 }
1807
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001808 break;
1809 case '\a':
1810 /* Bell */
1811 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001812 case '\x0E': /* SO */
1813 terminal->cs = terminal->g1;
1814 break;
1815 case '\x0F': /* SI */
1816 terminal->cs = terminal->g0;
1817 break;
Kristian Høgsberg2a1aa4e2012-08-03 09:37:05 -04001818 case '\0':
1819 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001820 default:
1821 return 0;
1822 }
1823
1824 return 1;
1825}
1826
1827static void
1828handle_char(struct terminal *terminal, union utf8_char utf8)
1829{
1830 union utf8_char *row;
1831 struct attr *attr_row;
1832
1833 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001834
1835 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001836
1837 /* There are a whole lot of non-characters, control codes,
1838 * and formatting codes that should probably be ignored,
1839 * for example: */
1840 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1841 /* BOM, ignore */
1842 return;
1843 }
1844
1845 /* Some of these non-characters should be translated, e.g.: */
1846 if (utf8.byte[0] < 32) {
1847 utf8.byte[0] = utf8.byte[0] + 64;
1848 }
1849
1850 /* handle right margin effects */
1851 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001852 if (terminal->mode & MODE_AUTOWRAP) {
1853 terminal->column = 0;
1854 terminal->row += 1;
1855 if (terminal->row > terminal->margin_bottom) {
1856 terminal->row = terminal->margin_bottom;
1857 terminal_scroll(terminal, +1);
1858 }
1859 } else {
1860 terminal->column--;
1861 }
1862 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001863
1864 row = terminal_get_row(terminal, terminal->row);
1865 attr_row = terminal_get_attr_row(terminal, terminal->row);
1866
Callum Lowcay69e96582011-01-07 19:47:00 +00001867 if (terminal->mode & MODE_IRM)
1868 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001869 row[terminal->column] = utf8;
1870 attr_row[terminal->column++] = terminal->curr_attr;
1871
1872 if (utf8.ch != terminal->last_char.ch)
1873 terminal->last_char = utf8;
1874}
1875
Callum Lowcay30eeae52011-01-07 19:46:55 +00001876static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001877escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1878{
1879 int len, i;
1880
1881 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1882 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1883 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1884 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1885 else len = 1; /* Invalid, cannot happen */
1886
1887 if (terminal->escape_length + len <= MAX_ESCAPE) {
1888 for (i = 0; i < len; i++)
1889 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1890 terminal->escape_length += len;
1891 } else if (terminal->escape_length < MAX_ESCAPE) {
1892 terminal->escape[terminal->escape_length++] = 0;
1893 }
1894}
1895
1896static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001897terminal_data(struct terminal *terminal, const char *data, size_t length)
1898{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001899 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001900 union utf8_char utf8;
1901 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001902
1903 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001904 parser_state =
1905 utf8_next_char(&terminal->state_machine, data[i]);
1906 switch(parser_state) {
1907 case utf8state_accept:
1908 utf8.ch = terminal->state_machine.s.ch;
1909 break;
1910 case utf8state_reject:
1911 /* the unicode replacement character */
1912 utf8.byte[0] = 0xEF;
1913 utf8.byte[1] = 0xBF;
1914 utf8.byte[2] = 0xBD;
1915 utf8.byte[3] = 0x00;
1916 break;
1917 default:
1918 continue;
1919 }
1920
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001921 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001922 switch (terminal->state) {
1923 case escape_state_escape:
1924 escape_append_utf8(terminal, utf8);
1925 switch (utf8.byte[0]) {
1926 case 'P': /* DCS */
1927 terminal->state = escape_state_dcs;
1928 break;
1929 case '[': /* CSI */
1930 terminal->state = escape_state_csi;
1931 break;
1932 case ']': /* OSC */
1933 terminal->state = escape_state_osc;
1934 break;
1935 case '#':
1936 case '(':
1937 case ')': /* special */
1938 terminal->state = escape_state_special;
1939 break;
1940 case '^': /* PM (not implemented) */
1941 case '_': /* APC (not implemented) */
1942 terminal->state = escape_state_ignore;
1943 break;
1944 default:
1945 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001946 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13001947 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001948 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001949 continue;
1950 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001951 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1952 /* do nothing */
1953 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001954 terminal->escape_flags |= ESC_FLAG_WHAT;
1955 } else if (utf8.byte[0] == '>') {
1956 terminal->escape_flags |= ESC_FLAG_GT;
1957 } else if (utf8.byte[0] == '!') {
1958 terminal->escape_flags |= ESC_FLAG_BANG;
1959 } else if (utf8.byte[0] == '$') {
1960 terminal->escape_flags |= ESC_FLAG_CASH;
1961 } else if (utf8.byte[0] == '\'') {
1962 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1963 } else if (utf8.byte[0] == '"') {
1964 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1965 } else if (utf8.byte[0] == ' ') {
1966 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001967 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001968 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001969 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13001970 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001971 }
1972
1973 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1974 utf8.byte[0] == '`')
1975 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001976 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001977 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001978 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001979 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001980 continue;
1981 case escape_state_inner_escape:
1982 if (utf8.byte[0] == '\\') {
1983 terminal->state = escape_state_normal;
1984 if (terminal->outer_state == escape_state_dcs) {
1985 handle_dcs(terminal);
1986 } else if (terminal->outer_state == escape_state_osc) {
1987 handle_osc(terminal);
1988 }
1989 } else if (utf8.byte[0] == '\e') {
1990 terminal->state = terminal->outer_state;
1991 escape_append_utf8(terminal, utf8);
1992 if (terminal->escape_length >= MAX_ESCAPE)
1993 terminal->state = escape_state_normal;
1994 } else {
1995 terminal->state = terminal->outer_state;
1996 if (terminal->escape_length < MAX_ESCAPE)
1997 terminal->escape[terminal->escape_length++] = '\e';
1998 escape_append_utf8(terminal, utf8);
1999 if (terminal->escape_length >= MAX_ESCAPE)
2000 terminal->state = escape_state_normal;
2001 }
2002 continue;
2003 case escape_state_dcs:
2004 case escape_state_osc:
2005 case escape_state_ignore:
2006 if (utf8.byte[0] == '\e') {
2007 terminal->outer_state = terminal->state;
2008 terminal->state = escape_state_inner_escape;
2009 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
2010 terminal->state = escape_state_normal;
2011 handle_osc(terminal);
2012 } else {
2013 escape_append_utf8(terminal, utf8);
2014 if (terminal->escape_length >= MAX_ESCAPE)
2015 terminal->state = escape_state_normal;
2016 }
2017 continue;
2018 case escape_state_special:
2019 escape_append_utf8(terminal, utf8);
2020 terminal->state = escape_state_normal;
2021 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2022 handle_special_escape(terminal, terminal->escape[1],
2023 utf8.byte[0]);
2024 }
2025 continue;
2026 default:
2027 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002028 }
2029
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002030 /* this is valid, because ASCII characters are never used to
2031 * introduce a multibyte sequence in UTF-8 */
2032 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002033 terminal->state = escape_state_escape;
2034 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002035 terminal->escape[0] = '\e';
2036 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002037 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002038 } else {
2039 handle_char(terminal, utf8);
2040 } /* if */
2041 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002042
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002043 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002044}
2045
2046static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002047data_source_target(void *data,
2048 struct wl_data_source *source, const char *mime_type)
2049{
2050 fprintf(stderr, "data_source_target, %s\n", mime_type);
2051}
2052
2053static void
2054data_source_send(void *data,
2055 struct wl_data_source *source,
2056 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002057{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002058 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002059
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002060 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002061}
2062
2063static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002064data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002065{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002066 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002067}
2068
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002069static const struct wl_data_source_listener data_source_listener = {
2070 data_source_target,
2071 data_source_send,
2072 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002073};
2074
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002075static void
2076fullscreen_handler(struct window *window, void *data)
2077{
2078 struct terminal *terminal = data;
2079
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002080 window_set_fullscreen(window, !window_is_fullscreen(terminal->window));
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002081}
2082
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002083static void
2084close_handler(struct window *window, void *data)
2085{
2086 struct terminal *terminal = data;
2087
2088 terminal_destroy(terminal);
2089}
2090
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002091static int
2092handle_bound_key(struct terminal *terminal,
2093 struct input *input, uint32_t sym, uint32_t time)
2094{
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002095 struct terminal *new_terminal;
2096
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002097 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002098 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002099 /* Cut selection; terminal doesn't do cut, fall
2100 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002101 case XKB_KEY_C:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002102 terminal->selection =
2103 display_create_data_source(terminal->display);
2104 wl_data_source_offer(terminal->selection,
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002105 "text/plain;charset=utf-8");
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002106 wl_data_source_add_listener(terminal->selection,
2107 &data_source_listener, terminal);
Kristian Høgsbergfee91be2012-06-02 21:21:41 -04002108 input_set_selection(input, terminal->selection,
2109 display_get_serial(terminal->display));
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002110 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002111 case XKB_KEY_V:
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002112 input_receive_selection_data_to_fd(input,
2113 "text/plain;charset=utf-8",
2114 terminal->master);
2115
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002116 return 1;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002117
2118 case XKB_KEY_N:
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002119 new_terminal = terminal_create(terminal->display);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002120 if (terminal_run(new_terminal, option_shell))
2121 terminal_destroy(new_terminal);
2122
2123 return 1;
2124
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002125 default:
2126 return 0;
2127 }
2128}
2129
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002130static void
2131key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002132 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2133 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002134{
2135 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002136 char ch[MAX_RESPONSE];
Kristian Høgsberg88fd4082012-06-21 15:55:03 -04002137 uint32_t modifiers, serial;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002138 int ret, len = 0;
2139 bool convert_utf8 = true;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002140
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002141 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002142 if ((modifiers & MOD_CONTROL_MASK) &&
2143 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002144 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2145 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002146 return;
2147
Daniel Stonea8468712012-11-07 17:51:35 +11002148 /* Map keypad symbols to 'normal' equivalents before processing */
2149 switch (sym) {
2150 case XKB_KEY_KP_Space:
2151 sym = XKB_KEY_space;
2152 break;
2153 case XKB_KEY_KP_Tab:
2154 sym = XKB_KEY_Tab;
2155 break;
2156 case XKB_KEY_KP_Enter:
2157 sym = XKB_KEY_Return;
2158 break;
2159 case XKB_KEY_KP_Left:
2160 sym = XKB_KEY_Left;
2161 break;
2162 case XKB_KEY_KP_Up:
2163 sym = XKB_KEY_Up;
2164 break;
2165 case XKB_KEY_KP_Right:
2166 sym = XKB_KEY_Right;
2167 break;
2168 case XKB_KEY_KP_Down:
2169 sym = XKB_KEY_Down;
2170 break;
2171 case XKB_KEY_KP_Equal:
2172 sym = XKB_KEY_equal;
2173 break;
2174 case XKB_KEY_KP_Multiply:
2175 sym = XKB_KEY_asterisk;
2176 break;
2177 case XKB_KEY_KP_Add:
2178 sym = XKB_KEY_plus;
2179 break;
2180 case XKB_KEY_KP_Separator:
2181 /* Note this is actually locale-dependent and should mostly be
2182 * a comma. But leave it as period until we one day start
2183 * doing the right thing. */
2184 sym = XKB_KEY_period;
2185 break;
2186 case XKB_KEY_KP_Subtract:
2187 sym = XKB_KEY_minus;
2188 break;
2189 case XKB_KEY_KP_Decimal:
2190 sym = XKB_KEY_period;
2191 break;
2192 case XKB_KEY_KP_Divide:
2193 sym = XKB_KEY_slash;
2194 break;
2195 case XKB_KEY_KP_0:
2196 case XKB_KEY_KP_1:
2197 case XKB_KEY_KP_2:
2198 case XKB_KEY_KP_3:
2199 case XKB_KEY_KP_4:
2200 case XKB_KEY_KP_5:
2201 case XKB_KEY_KP_6:
2202 case XKB_KEY_KP_7:
2203 case XKB_KEY_KP_8:
2204 case XKB_KEY_KP_9:
2205 sym = (sym - XKB_KEY_KP_0) + XKB_KEY_0;
2206 break;
2207 default:
2208 break;
2209 }
2210
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002211 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002212 case XKB_KEY_BackSpace:
Kristian Høgsbergb7f94bf2012-06-21 12:29:36 -04002213 if (modifiers & MOD_ALT_MASK)
2214 ch[len++] = 0x1b;
Kristian Høgsberg71a4cf42012-06-20 17:57:56 -04002215 ch[len++] = 0x7f;
2216 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002217 case XKB_KEY_Tab:
2218 case XKB_KEY_Linefeed:
2219 case XKB_KEY_Clear:
2220 case XKB_KEY_Pause:
2221 case XKB_KEY_Scroll_Lock:
2222 case XKB_KEY_Sys_Req:
2223 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002224 ch[len++] = sym & 0x7f;
2225 break;
2226
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002227 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002228 if (terminal->mode & MODE_LF_NEWLINE) {
2229 ch[len++] = 0x0D;
2230 ch[len++] = 0x0A;
2231 } else {
2232 ch[len++] = 0x0D;
2233 }
2234 break;
2235
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002236 case XKB_KEY_Shift_L:
2237 case XKB_KEY_Shift_R:
2238 case XKB_KEY_Control_L:
2239 case XKB_KEY_Control_R:
2240 case XKB_KEY_Alt_L:
2241 case XKB_KEY_Alt_R:
Kristian Høgsberg22fbcf72012-06-22 12:18:56 -04002242 case XKB_KEY_Meta_L:
2243 case XKB_KEY_Meta_R:
2244 case XKB_KEY_Super_L:
2245 case XKB_KEY_Super_R:
2246 case XKB_KEY_Hyper_L:
2247 case XKB_KEY_Hyper_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002248 break;
2249
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002250 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002251 len = function_key_response('[', 2, modifiers, '~', ch);
2252 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002253 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002254 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2255 ch[len++] = '\x04';
2256 } else {
2257 len = function_key_response('[', 3, modifiers, '~', ch);
2258 }
2259 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002260 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002261 len = function_key_response('[', 5, modifiers, '~', ch);
2262 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002263 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002264 len = function_key_response('[', 6, modifiers, '~', ch);
2265 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002266 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002267 len = function_key_response('O', 1, modifiers, 'P', ch);
2268 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002269 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002270 len = function_key_response('O', 1, modifiers, 'Q', ch);
2271 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002272 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002273 len = function_key_response('O', 1, modifiers, 'R', ch);
2274 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002275 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002276 len = function_key_response('O', 1, modifiers, 'S', ch);
2277 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002278 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002279 len = function_key_response('[', 15, modifiers, '~', ch);
2280 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002281 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002282 len = function_key_response('[', 17, modifiers, '~', ch);
2283 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002284 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002285 len = function_key_response('[', 18, modifiers, '~', ch);
2286 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002287 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002288 len = function_key_response('[', 19, modifiers, '~', ch);
2289 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002290 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002291 len = function_key_response('[', 20, modifiers, '~', ch);
2292 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002293 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002294 len = function_key_response('[', 21, modifiers, '~', ch);
2295 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002296 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002297 len = function_key_response('[', 24, modifiers, '~', ch);
2298 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002299 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002300 /* Handle special keys with alternate mappings */
2301 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2302 if (len != 0) break;
2303
Kristian Høgsberg70163132012-05-08 15:55:39 -04002304 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002305 if (sym >= '3' && sym <= '7')
2306 sym = (sym & 0x1f) + 8;
2307
2308 if (!((sym >= '!' && sym <= '/') ||
2309 (sym >= '8' && sym <= '?') ||
2310 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2311 else if (sym == '2') sym = 0x00;
2312 else if (sym == '/') sym = 0x1F;
2313 else if (sym == '8' || sym == '?') sym = 0x7F;
Kristian Høgsbergae9e0732012-06-21 12:30:15 -04002314 }
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002315 if (modifiers & MOD_ALT_MASK) {
2316 if (terminal->mode & MODE_ALT_SENDS_ESC) {
2317 ch[len++] = 0x1b;
2318 } else {
2319 sym = sym | 0x80;
2320 convert_utf8 = false;
2321 }
Callum Lowcay7e08e902011-01-07 19:47:02 +00002322 }
2323
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002324 if ((sym < 128) ||
2325 (!convert_utf8 && sym < 256)) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002326 ch[len++] = sym;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002327 } else {
2328 ret = xkb_keysym_to_utf8(sym, ch + len,
2329 MAX_RESPONSE - len);
2330 if (ret < 0)
2331 fprintf(stderr,
2332 "Warning: buffer too small to encode "
2333 "UTF8 character\n");
2334 else
2335 len += ret;
2336 }
2337
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002338 break;
2339 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002340
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002341 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04002342 terminal_write(terminal, ch, len);
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002343
2344 /* Hide cursor, except if this was coming from a
2345 * repeating key press. */
2346 serial = display_get_serial(terminal->display);
2347 if (terminal->hide_cursor_serial != serial) {
2348 input_set_pointer_image(input, CURSOR_BLANK);
2349 terminal->hide_cursor_serial = serial;
2350 }
2351 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002352}
2353
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002354static void
2355keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002356 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002357{
2358 struct terminal *terminal = data;
2359
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002360 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002361}
2362
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002363static int wordsep(int ch)
2364{
2365 const char extra[] = "-,./?%&#:_=+@~";
2366
Andre Heider552d12b2012-08-02 20:59:43 +02002367 if (ch > 127)
2368 return 1;
2369
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002370 return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
2371}
2372
2373static int
2374recompute_selection(struct terminal *terminal)
2375{
2376 struct rectangle allocation;
2377 int col, x, width, height;
2378 int start_row, end_row;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002379 int word_start, eol;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002380 int side_margin, top_margin;
2381 int start_x, end_x;
2382 int cw, ch;
2383 union utf8_char *data;
2384
Peng Wuf291f202013-06-06 15:32:41 +08002385 cw = terminal->average_width;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002386 ch = terminal->extents.height;
2387 widget_get_allocation(terminal->widget, &allocation);
2388 width = terminal->width * cw;
2389 height = terminal->height * ch;
2390 side_margin = allocation.x + (allocation.width - width) / 2;
2391 top_margin = allocation.y + (allocation.height - height) / 2;
2392
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002393 start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1;
2394 end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1;
2395
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002396 if (start_row < end_row ||
2397 (start_row == end_row &&
2398 terminal->selection_start_x < terminal->selection_end_x)) {
2399 terminal->selection_start_row = start_row;
2400 terminal->selection_end_row = end_row;
2401 start_x = terminal->selection_start_x;
2402 end_x = terminal->selection_end_x;
2403 } else {
2404 terminal->selection_start_row = end_row;
2405 terminal->selection_end_row = start_row;
2406 start_x = terminal->selection_end_x;
2407 end_x = terminal->selection_start_x;
2408 }
2409
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002410 eol = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002411 if (terminal->selection_start_row < 0) {
2412 terminal->selection_start_row = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002413 terminal->selection_start_col = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002414 } else {
2415 x = side_margin + cw / 2;
2416 data = terminal_get_row(terminal,
2417 terminal->selection_start_row);
2418 word_start = 0;
2419 for (col = 0; col < terminal->width; col++, x += cw) {
2420 if (col == 0 || wordsep(data[col - 1].ch))
2421 word_start = col;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002422 if (data[col].ch != 0)
2423 eol = col + 1;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002424 if (start_x < x)
2425 break;
2426 }
2427
2428 switch (terminal->dragging) {
2429 case SELECT_LINE:
2430 terminal->selection_start_col = 0;
2431 break;
2432 case SELECT_WORD:
2433 terminal->selection_start_col = word_start;
2434 break;
2435 case SELECT_CHAR:
2436 terminal->selection_start_col = col;
2437 break;
2438 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002439 }
2440
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002441 if (terminal->selection_end_row >= terminal->height) {
2442 terminal->selection_end_row = terminal->height;
2443 terminal->selection_end_col = 0;
2444 } else {
2445 x = side_margin + cw / 2;
2446 data = terminal_get_row(terminal, terminal->selection_end_row);
2447 for (col = 0; col < terminal->width; col++, x += cw) {
2448 if (terminal->dragging == SELECT_CHAR && end_x < x)
2449 break;
2450 if (terminal->dragging == SELECT_WORD &&
2451 end_x < x && wordsep(data[col].ch))
2452 break;
2453 }
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002454 terminal->selection_end_col = col;
2455 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002456
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002457 if (terminal->selection_end_col != terminal->selection_start_col ||
2458 terminal->selection_start_row != terminal->selection_end_row) {
2459 col = terminal->selection_end_col;
2460 if (col > 0 && data[col - 1].ch == 0)
2461 terminal->selection_end_col = terminal->width;
2462 data = terminal_get_row(terminal, terminal->selection_start_row);
2463 if (data[terminal->selection_start_col].ch == 0)
2464 terminal->selection_start_col = eol;
2465 }
2466
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002467 return 1;
2468}
2469
Kristian Høgsberg59826582011-01-20 11:56:57 -05002470static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002471button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002472 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002473 uint32_t button,
2474 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002475{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002476 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002477
2478 switch (button) {
2479 case 272:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002480 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002481
2482 if (time - terminal->button_time < 500)
2483 terminal->click_count++;
2484 else
2485 terminal->click_count = 1;
2486
2487 terminal->button_time = time;
2488 terminal->dragging =
2489 (terminal->click_count - 1) % 3 + SELECT_CHAR;
2490
Kristian Høgsberg59826582011-01-20 11:56:57 -05002491 input_get_position(input,
2492 &terminal->selection_start_x,
2493 &terminal->selection_start_y);
2494 terminal->selection_end_x = terminal->selection_start_x;
2495 terminal->selection_end_y = terminal->selection_start_y;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002496 if (recompute_selection(terminal))
2497 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002498 } else {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002499 terminal->dragging = SELECT_NONE;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002500 }
2501 break;
2502 }
2503}
2504
2505static int
Kristian Høgsberg29784402012-06-28 14:27:02 -04002506enter_handler(struct widget *widget,
2507 struct input *input, float x, float y, void *data)
2508{
2509 return CURSOR_IBEAM;
2510}
2511
2512static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002513motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002514 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002515 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002516{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002517 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002518
2519 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002520 input_get_position(input,
2521 &terminal->selection_end_x,
2522 &terminal->selection_end_y);
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002523
2524 if (recompute_selection(terminal))
2525 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002526 }
2527
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002528 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002529}
2530
Alexander Larssonde79dd02013-05-22 14:41:34 +02002531static void
2532output_handler(struct window *window, struct output *output, int enter,
2533 void *data)
2534{
2535 if (enter)
2536 window_set_buffer_transform(window, output_get_transform(output));
2537 window_set_buffer_scale(window, window_get_output_scale(window));
2538 window_schedule_redraw(window);
2539}
2540
Peng Wuf291f202013-06-06 15:32:41 +08002541#ifndef howmany
2542#define howmany(x, y) (((x) + ((y) - 1)) / (y))
2543#endif
2544
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002545static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002546terminal_create(struct display *display)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002547{
2548 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002549 cairo_surface_t *surface;
2550 cairo_t *cr;
Peng Wuf291f202013-06-06 15:32:41 +08002551 cairo_text_extents_t text_extents;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002552
Peter Huttererf3d62272013-08-08 11:57:05 +10002553 terminal = xzalloc(sizeof *terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002554 terminal->color_scheme = &DEFAULT_COLORS;
2555 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002556 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002557 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002558 terminal->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002559 terminal->widget = frame_create(terminal->window, terminal);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002560 window_set_title(terminal->window, "Wayland Terminal");
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002561 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002562
2563 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002564 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002565
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002566 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002567 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002568
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002569 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002570 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002571 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002572 keyboard_focus_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002573 window_set_fullscreen_handler(terminal->window, fullscreen_handler);
Alexander Larssonde79dd02013-05-22 14:41:34 +02002574 window_set_output_handler(terminal->window, output_handler);
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002575 window_set_close_handler(terminal->window, close_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002576
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002577 widget_set_redraw_handler(terminal->widget, redraw_handler);
2578 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002579 widget_set_button_handler(terminal->widget, button_handler);
Kristian Høgsberg29784402012-06-28 14:27:02 -04002580 widget_set_enter_handler(terminal->widget, enter_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002581 widget_set_motion_handler(terminal->widget, motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002582
Kristian Høgsberg09531622010-06-14 23:22:15 -04002583 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2584 cr = cairo_create(surface);
Kristian Høgsberg38912df2012-06-27 17:52:23 -04002585 cairo_set_font_size(cr, option_font_size);
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002586 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002587 CAIRO_FONT_SLANT_NORMAL,
2588 CAIRO_FONT_WEIGHT_BOLD);
2589 terminal->font_bold = cairo_get_scaled_font (cr);
2590 cairo_scaled_font_reference(terminal->font_bold);
2591
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002592 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002593 CAIRO_FONT_SLANT_NORMAL,
2594 CAIRO_FONT_WEIGHT_NORMAL);
2595 terminal->font_normal = cairo_get_scaled_font (cr);
2596 cairo_scaled_font_reference(terminal->font_normal);
2597
Kristian Høgsberg09531622010-06-14 23:22:15 -04002598 cairo_font_extents(cr, &terminal->extents);
Peng Wuf291f202013-06-06 15:32:41 +08002599
2600 /* Compute the average ascii glyph width */
2601 cairo_text_extents(cr, TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS,
2602 &text_extents);
2603 terminal->average_width = howmany
2604 (text_extents.width,
2605 strlen(TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS));
2606 terminal->average_width = ceil(terminal->average_width);
2607
Kristian Høgsberg09531622010-06-14 23:22:15 -04002608 cairo_destroy(cr);
2609 cairo_surface_destroy(surface);
2610
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002611 terminal_resize(terminal, 20, 5); /* Set minimum size first */
Kristian Høgsberga1627922012-06-20 17:30:03 -04002612 terminal_resize(terminal, 80, 25);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002613
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002614 wl_list_insert(terminal_list.prev, &terminal->link);
2615
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002616 return terminal;
2617}
2618
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002619static void
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002620terminal_destroy(struct terminal *terminal)
2621{
Dima Ryazanova85292e2012-11-29 00:27:09 -08002622 display_unwatch_fd(terminal->display, terminal->master);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002623 window_destroy(terminal->window);
2624 close(terminal->master);
2625 wl_list_remove(&terminal->link);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002626
2627 if (wl_list_empty(&terminal_list))
Dima Ryazanova85292e2012-11-29 00:27:09 -08002628 display_exit(terminal->display);
2629
2630 free(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002631}
2632
2633static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002634io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002635{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002636 struct terminal *terminal =
2637 container_of(task, struct terminal, io_task);
2638 char buffer[256];
2639 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002640
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002641 if (events & EPOLLHUP) {
2642 terminal_destroy(terminal);
2643 return;
2644 }
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002645
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002646 len = read(terminal->master, buffer, sizeof buffer);
2647 if (len < 0)
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002648 terminal_destroy(terminal);
2649 else
2650 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002651}
2652
2653static int
2654terminal_run(struct terminal *terminal, const char *path)
2655{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002656 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002657 pid_t pid;
2658
2659 pid = forkpty(&master, NULL, NULL, NULL);
2660 if (pid == 0) {
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04002661 setenv("TERM", option_term, 1);
2662 setenv("COLORTERM", option_term, 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002663 if (execl(path, path, NULL)) {
2664 printf("exec failed: %m\n");
2665 exit(EXIT_FAILURE);
2666 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002667 } else if (pid < 0) {
2668 fprintf(stderr, "failed to fork and create pty (%m).\n");
2669 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002670 }
2671
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002672 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002673 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002674 terminal->io_task.run = io_handler;
2675 display_watch_fd(terminal->display, terminal->master,
2676 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002677
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002678 window_set_fullscreen(terminal->window, option_fullscreen);
2679 if (!window_is_fullscreen(terminal->window))
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002680 terminal_resize(terminal, 80, 24);
2681
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002682 return 0;
2683}
2684
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002685static const struct config_key terminal_config_keys[] = {
2686 { "font", CONFIG_KEY_STRING, &option_font },
Kristian Høgsberg38912df2012-06-27 17:52:23 -04002687 { "font-size", CONFIG_KEY_INTEGER, &option_font_size },
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04002688 { "term", CONFIG_KEY_STRING, &option_term },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002689};
2690
2691static const struct config_section config_sections[] = {
2692 { "terminal",
2693 terminal_config_keys, ARRAY_LENGTH(terminal_config_keys) },
2694};
2695
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002696static const struct weston_option terminal_options[] = {
2697 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002698 { WESTON_OPTION_STRING, "font", 0, &option_font },
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002699 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002700};
2701
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002702int main(int argc, char *argv[])
2703{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002704 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002705 struct terminal *terminal;
Ossama Othmana50e6e42013-05-14 09:48:26 -07002706 int config_fd;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002707
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002708 option_shell = getenv("SHELL");
2709 if (!option_shell)
2710 option_shell = "/bin/bash";
2711
Ossama Othmana50e6e42013-05-14 09:48:26 -07002712 config_fd = open_config_file("weston.ini");
2713 parse_config_file(config_fd,
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002714 config_sections, ARRAY_LENGTH(config_sections),
2715 NULL);
Ossama Othmana50e6e42013-05-14 09:48:26 -07002716 close(config_fd);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002717
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002718 parse_options(terminal_options,
2719 ARRAY_LENGTH(terminal_options), &argc, argv);
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002720
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002721 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002722 if (d == NULL) {
2723 fprintf(stderr, "failed to create display: %m\n");
2724 return -1;
2725 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002726
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002727 wl_list_init(&terminal_list);
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002728 terminal = terminal_create(d);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002729 if (terminal_run(terminal, option_shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002730 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002731
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002732 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002733
2734 return 0;
2735}