blob: f97882d8bf06e523a39bf9a267240e616de99f60 [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 *
51terminal_create(struct display *display, int fullscreen);
52static void
53terminal_destroy(struct terminal *terminal);
54static int
55terminal_run(struct terminal *terminal, const char *path);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050056
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050057#define MOD_SHIFT 0x01
58#define MOD_ALT 0x02
59#define MOD_CTRL 0x04
60
Callum Lowcay30eeae52011-01-07 19:46:55 +000061#define ATTRMASK_BOLD 0x01
62#define ATTRMASK_UNDERLINE 0x02
63#define ATTRMASK_BLINK 0x04
64#define ATTRMASK_INVERSE 0x08
Callum Lowcay81179db2011-01-10 12:14:01 +130065#define ATTRMASK_CONCEALED 0x10
Callum Lowcay30eeae52011-01-07 19:46:55 +000066
Callum Lowcayb8609ad2011-01-07 19:46:57 +000067/* Buffer sizes */
Callum Lowcayef57a9b2011-01-14 20:46:23 +130068#define MAX_RESPONSE 256
69#define MAX_ESCAPE 255
Callum Lowcayb8609ad2011-01-07 19:46:57 +000070
Callum Lowcay8e57dd52011-01-07 19:46:59 +000071/* Terminal modes */
72#define MODE_SHOW_CURSOR 0x00000001
73#define MODE_INVERSE 0x00000002
74#define MODE_AUTOWRAP 0x00000004
75#define MODE_AUTOREPEAT 0x00000008
76#define MODE_LF_NEWLINE 0x00000010
Callum Lowcay69e96582011-01-07 19:47:00 +000077#define MODE_IRM 0x00000020
Callum Lowcay7e08e902011-01-07 19:47:02 +000078#define MODE_DELETE_SENDS_DEL 0x00000040
79#define MODE_ALT_SENDS_ESC 0x00000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +000080
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000081union utf8_char {
82 unsigned char byte[4];
83 uint32_t ch;
84};
85
86enum utf8_state {
87 utf8state_start,
88 utf8state_accept,
89 utf8state_reject,
90 utf8state_expect3,
91 utf8state_expect2,
92 utf8state_expect1
93};
94
95struct utf8_state_machine {
96 enum utf8_state state;
97 int len;
98 union utf8_char s;
99};
100
101static void
102init_state_machine(struct utf8_state_machine *machine)
103{
104 machine->state = utf8state_start;
105 machine->len = 0;
106 machine->s.ch = 0;
107}
108
109static enum utf8_state
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400110utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000111{
112 switch(machine->state) {
113 case utf8state_start:
114 case utf8state_accept:
115 case utf8state_reject:
116 machine->s.ch = 0;
117 machine->len = 0;
118 if(c == 0xC0 || c == 0xC1) {
119 /* overlong encoding, reject */
120 machine->state = utf8state_reject;
121 } else if((c & 0x80) == 0) {
122 /* single byte, accept */
123 machine->s.byte[machine->len++] = c;
124 machine->state = utf8state_accept;
125 } else if((c & 0xC0) == 0x80) {
126 /* parser out of sync, ignore byte */
127 machine->state = utf8state_start;
128 } else if((c & 0xE0) == 0xC0) {
129 /* start of two byte sequence */
130 machine->s.byte[machine->len++] = c;
131 machine->state = utf8state_expect1;
132 } else if((c & 0xF0) == 0xE0) {
133 /* start of three byte sequence */
134 machine->s.byte[machine->len++] = c;
135 machine->state = utf8state_expect2;
136 } else if((c & 0xF8) == 0xF0) {
137 /* start of four byte sequence */
138 machine->s.byte[machine->len++] = c;
139 machine->state = utf8state_expect3;
140 } else {
141 /* overlong encoding, reject */
142 machine->state = utf8state_reject;
143 }
144 break;
145 case utf8state_expect3:
146 machine->s.byte[machine->len++] = c;
147 if((c & 0xC0) == 0x80) {
148 /* all good, continue */
149 machine->state = utf8state_expect2;
150 } else {
151 /* missing extra byte, reject */
152 machine->state = utf8state_reject;
153 }
154 break;
155 case utf8state_expect2:
156 machine->s.byte[machine->len++] = c;
157 if((c & 0xC0) == 0x80) {
158 /* all good, continue */
159 machine->state = utf8state_expect1;
160 } else {
161 /* missing extra byte, reject */
162 machine->state = utf8state_reject;
163 }
164 break;
165 case utf8state_expect1:
166 machine->s.byte[machine->len++] = c;
167 if((c & 0xC0) == 0x80) {
168 /* all good, accept */
169 machine->state = utf8state_accept;
170 } else {
171 /* missing extra byte, reject */
172 machine->state = utf8state_reject;
173 }
174 break;
175 default:
176 machine->state = utf8state_reject;
177 break;
178 }
179
180 return machine->state;
181}
182
Callum Lowcay256e72f2011-01-07 19:47:01 +0000183struct char_sub {
184 union utf8_char match;
185 union utf8_char replace;
186};
187/* Set last char_sub match to NULL char */
188typedef struct char_sub *character_set;
189
190struct char_sub CS_US[] = {
191 {{{0, }}, {{0, }}}
192};
193static struct char_sub CS_UK[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200194 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000195 {{{0, }}, {{0, }}}
196};
197static struct char_sub CS_SPECIAL[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200198 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond: ♦ */
199 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell: ▒ */
200 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT: ␉ */
201 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF: ␌ */
202 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR: ␍ */
203 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF: ␊ */
204 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree: ° */
205 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus: ± */
206 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL: ␤ */
207 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT: ␋ */
208 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB: ┘ */
209 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT: ┐ */
210 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT: ┌ */
211 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_LB: └ */
212 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS: ┼ */
213 {{{'o', 0, }}, {{0xE2, 0x8E, 0xBA, 0}}}, /* Horiz. Scan Line 1: ⎺ */
214 {{{'p', 0, }}, {{0xE2, 0x8E, 0xBB, 0}}}, /* Horiz. Scan Line 3: ⎻ */
215 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* Horiz. Scan Line 5: ─ */
216 {{{'r', 0, }}, {{0xE2, 0x8E, 0xBC, 0}}}, /* Horiz. Scan Line 7: ⎼ */
217 {{{'s', 0, }}, {{0xE2, 0x8E, 0xBD, 0}}}, /* Horiz. Scan Line 9: ⎽ */
218 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR: ├ */
219 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL: ┤ */
220 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU: ┴ */
221 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD: ┬ */
222 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V: │ */
223 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE: ≤ */
224 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE: ≥ */
225 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI: π */
226 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ: ≠ */
227 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
228 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT: ⋅ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000229 {{{0, }}, {{0, }}}
230};
231
232static void
233apply_char_set(character_set cs, union utf8_char *utf8)
234{
235 int i = 0;
236
237 while (cs[i].match.byte[0]) {
238 if ((*utf8).ch == cs[i].match.ch) {
239 *utf8 = cs[i].replace;
240 break;
241 }
242 i++;
243 }
244}
245
Callum Lowcay7e08e902011-01-07 19:47:02 +0000246struct key_map {
247 int sym;
248 int num;
249 char escape;
250 char code;
251};
252/* Set last key_sub sym to NULL */
253typedef struct key_map *keyboard_mode;
254
255static struct key_map KM_NORMAL[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400256 { XKB_KEY_Left, 1, '[', 'D' },
257 { XKB_KEY_Right, 1, '[', 'C' },
258 { XKB_KEY_Up, 1, '[', 'A' },
259 { XKB_KEY_Down, 1, '[', 'B' },
260 { XKB_KEY_Home, 1, '[', 'H' },
261 { XKB_KEY_End, 1, '[', 'F' },
262 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000263};
264static struct key_map KM_APPLICATION[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400265 { XKB_KEY_Left, 1, 'O', 'D' },
266 { XKB_KEY_Right, 1, 'O', 'C' },
267 { XKB_KEY_Up, 1, 'O', 'A' },
268 { XKB_KEY_Down, 1, 'O', 'B' },
269 { XKB_KEY_Home, 1, 'O', 'H' },
270 { XKB_KEY_End, 1, 'O', 'F' },
271 { XKB_KEY_KP_Enter, 1, 'O', 'M' },
272 { XKB_KEY_KP_Multiply, 1, 'O', 'j' },
273 { XKB_KEY_KP_Add, 1, 'O', 'k' },
274 { XKB_KEY_KP_Separator, 1, 'O', 'l' },
275 { XKB_KEY_KP_Subtract, 1, 'O', 'm' },
276 { XKB_KEY_KP_Divide, 1, 'O', 'o' },
277 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000278};
279
280static int
281function_key_response(char escape, int num, uint32_t modifiers,
282 char code, char *response)
283{
284 int mod_num = 0;
285 int len;
286
Kristian Høgsberg70163132012-05-08 15:55:39 -0400287 if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
288 if (modifiers & MOD_ALT_MASK) mod_num |= 2;
289 if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000290
291 if (mod_num != 0)
292 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
293 num, mod_num + 1, code);
294 else if (code != '~')
295 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
296 escape, code);
297 else
298 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
299 escape, num, code);
300
301 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
302 else return len;
303}
304
305/* returns the number of bytes written into response,
306 * which must have room for MAX_RESPONSE bytes */
307static int
308apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
309{
310 struct key_map map;
311 int len = 0;
312 int i = 0;
313
314 while (mode[i].sym) {
315 map = mode[i++];
316 if (sym == map.sym) {
317 len = function_key_response(map.escape, map.num,
318 modifiers, map.code,
319 response);
320 break;
321 }
322 }
323
324 return len;
325}
326
Callum Lowcay30eeae52011-01-07 19:46:55 +0000327struct terminal_color { double r, g, b, a; };
328struct attr {
329 unsigned char fg, bg;
330 char a; /* attributes format:
331 * 76543210
Callum Lowcay81179db2011-01-10 12:14:01 +1300332 * cilub */
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500333 char s; /* in selection */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000334};
335struct color_scheme {
336 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500337 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000338 struct attr default_attr;
339};
340
341static void
342attr_init(struct attr *data_attr, struct attr attr, int n)
343{
344 int i;
345 for (i = 0; i < n; i++) {
346 data_attr[i] = attr;
347 }
348}
349
Callum Lowcay67a201d2011-01-12 19:23:41 +1300350enum escape_state {
351 escape_state_normal = 0,
352 escape_state_escape,
353 escape_state_dcs,
354 escape_state_csi,
355 escape_state_osc,
356 escape_state_inner_escape,
357 escape_state_ignore,
358 escape_state_special
359};
360
361#define ESC_FLAG_WHAT 0x01
362#define ESC_FLAG_GT 0x02
363#define ESC_FLAG_BANG 0x04
364#define ESC_FLAG_CASH 0x08
365#define ESC_FLAG_SQUOTE 0x10
366#define ESC_FLAG_DQUOTE 0x20
367#define ESC_FLAG_SPACE 0x40
368
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400369enum {
370 SELECT_NONE,
371 SELECT_CHAR,
372 SELECT_WORD,
373 SELECT_LINE
374};
375
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500376struct terminal {
377 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500378 struct widget *widget;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500379 struct display *display;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000380 union utf8_char *data;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400381 struct task io_task;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000382 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000383 struct attr *data_attr;
384 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000385 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000386 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000387 char saved_origin_mode;
388 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000389 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000390 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000391 character_set cs, g0, g1;
392 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000393 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000394 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500395 int width, height, start, row, column;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000396 int saved_row, saved_column;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600397 int send_cursor_position;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500398 int fd, master;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500399 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300400 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500401 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300402 enum escape_state state;
403 enum escape_state outer_state;
404 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000405 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500406 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500407 int fullscreen;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400408 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000409 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400410 cairo_font_extents_t extents;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500411 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg88fd4082012-06-21 15:55:03 -0400412 uint32_t hide_cursor_serial;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500413
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400414 struct wl_data_source *selection;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400415 uint32_t button_time;
416 int dragging, click_count;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500417 int selection_start_x, selection_start_y;
418 int selection_end_x, selection_end_y;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400419 int selection_start_row, selection_start_col;
420 int selection_end_row, selection_end_col;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -0400421 struct wl_list link;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500422};
423
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000424/* Create default tab stops, every 8 characters */
425static void
426terminal_init_tabs(struct terminal *terminal)
427{
428 int i = 0;
429
430 while (i < terminal->width) {
431 if (i % 8 == 0)
432 terminal->tab_ruler[i] = 1;
433 else
434 terminal->tab_ruler[i] = 0;
435 i++;
436 }
437}
438
Callum Lowcay30eeae52011-01-07 19:46:55 +0000439static void
440terminal_init(struct terminal *terminal)
441{
442 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000443 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000444 terminal->mode = MODE_SHOW_CURSOR |
445 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000446 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000447 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000448
449 terminal->row = 0;
450 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000451
Callum Lowcay256e72f2011-01-07 19:47:01 +0000452 terminal->g0 = CS_US;
453 terminal->g1 = CS_US;
454 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000455 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000456
457 terminal->saved_g0 = terminal->g0;
458 terminal->saved_g1 = terminal->g1;
459 terminal->saved_cs = terminal->cs;
460
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000461 terminal->saved_attr = terminal->curr_attr;
462 terminal->saved_origin_mode = terminal->origin_mode;
463 terminal->saved_row = terminal->row;
464 terminal->saved_column = terminal->column;
465
466 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000467}
468
469static void
470init_color_table(struct terminal *terminal)
471{
472 int c, r;
473 struct terminal_color *color_table = terminal->color_table;
474
475 for (c = 0; c < 256; c ++) {
476 if (c < 16) {
477 color_table[c] = terminal->color_scheme->palette[c];
478 } else if (c < 232) {
479 r = c - 16;
480 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
481 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
482 color_table[c].r = ((double)(r % 6) / 6.0);
483 color_table[c].a = 1.0;
484 } else {
485 r = (c - 232) * 10 + 8;
486 color_table[c].r = ((double) r) / 256.0;
487 color_table[c].g = color_table[c].r;
488 color_table[c].b = color_table[c].r;
489 color_table[c].a = 1.0;
490 }
491 }
492}
493
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000494static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500495terminal_get_row(struct terminal *terminal, int row)
496{
497 int index;
498
499 index = (row + terminal->start) % terminal->height;
500
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000501 return &terminal->data[index * terminal->width];
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500502}
503
Callum Lowcay30eeae52011-01-07 19:46:55 +0000504static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500505terminal_get_attr_row(struct terminal *terminal, int row)
506{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000507 int index;
508
509 index = (row + terminal->start) % terminal->height;
510
511 return &terminal->data_attr[index * terminal->width];
512}
513
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500514union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300515 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500516 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500517};
518
519static void
520terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500521 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500522{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500523 struct attr attr;
524 int foreground, background, tmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500525
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500526 decoded->attr.s = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400527 if (((row == terminal->selection_start_row &&
528 col >= terminal->selection_start_col) ||
529 row > terminal->selection_start_row) &&
530 ((row == terminal->selection_end_row &&
531 col < terminal->selection_end_col) ||
532 row < terminal->selection_end_row))
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500533 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500534
535 /* get the attributes for this character cell */
536 attr = terminal_get_attr_row(terminal, row)[col];
537 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500538 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500539 ((terminal->mode & MODE_SHOW_CURSOR) &&
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400540 window_has_focus(terminal->window) && terminal->row == row &&
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500541 terminal->column == col)) {
542 foreground = attr.bg;
543 background = attr.fg;
544 if (attr.a & ATTRMASK_BOLD) {
545 if (foreground <= 16) foreground |= 0x08;
546 if (background <= 16) background &= 0x07;
547 }
548 } else {
549 foreground = attr.fg;
550 background = attr.bg;
551 }
552
553 if (terminal->mode & MODE_INVERSE) {
554 tmp = foreground;
555 foreground = background;
556 background = tmp;
557 if (attr.a & ATTRMASK_BOLD) {
558 if (foreground <= 16) foreground |= 0x08;
559 if (background <= 16) background &= 0x07;
560 }
561 }
562
Callum Lowcay9d708b02011-01-12 20:06:17 +1300563 decoded->attr.fg = foreground;
564 decoded->attr.bg = background;
565 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000566}
567
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500568
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500569static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000570terminal_scroll_buffer(struct terminal *terminal, int d)
571{
572 int i;
573
574 d = d % (terminal->height + 1);
575 terminal->start = (terminal->start + d) % terminal->height;
576 if (terminal->start < 0) terminal->start = terminal->height + terminal->start;
577 if(d < 0) {
578 d = 0 - d;
579 for(i = 0; i < d; i++) {
580 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
581 attr_init(terminal_get_attr_row(terminal, i),
582 terminal->curr_attr, terminal->width);
583 }
584 } else {
585 for(i = terminal->height - d; i < terminal->height; i++) {
586 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
587 attr_init(terminal_get_attr_row(terminal, i),
588 terminal->curr_attr, terminal->width);
589 }
590 }
Kristian Høgsberg18e928d2012-06-27 19:29:41 -0400591
592 terminal->selection_start_row -= d;
593 terminal->selection_end_row -= d;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000594}
595
596static void
597terminal_scroll_window(struct terminal *terminal, int d)
598{
599 int i;
600 int window_height;
601 int from_row, to_row;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000602
603 // scrolling range is inclusive
604 window_height = terminal->margin_bottom - terminal->margin_top + 1;
605 d = d % (window_height + 1);
606 if(d < 0) {
607 d = 0 - d;
608 to_row = terminal->margin_bottom;
609 from_row = terminal->margin_bottom - d;
610
611 for (i = 0; i < (window_height - d); i++) {
612 memcpy(terminal_get_row(terminal, to_row - i),
613 terminal_get_row(terminal, from_row - i),
614 terminal->data_pitch);
615 memcpy(terminal_get_attr_row(terminal, to_row - i),
616 terminal_get_attr_row(terminal, from_row - i),
617 terminal->attr_pitch);
618 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000619 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
620 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000621 attr_init(terminal_get_attr_row(terminal, i),
622 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000623 }
624 } else {
625 to_row = terminal->margin_top;
626 from_row = terminal->margin_top + d;
627
628 for (i = 0; i < (window_height - d); i++) {
629 memcpy(terminal_get_row(terminal, to_row + i),
630 terminal_get_row(terminal, from_row + i),
631 terminal->data_pitch);
632 memcpy(terminal_get_attr_row(terminal, to_row + i),
633 terminal_get_attr_row(terminal, from_row + i),
634 terminal->attr_pitch);
635 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000636 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
637 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000638 attr_init(terminal_get_attr_row(terminal, i),
639 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000640 }
641 }
642}
643
644static void
645terminal_scroll(struct terminal *terminal, int d)
646{
647 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
648 terminal_scroll_buffer(terminal, d);
649 else
650 terminal_scroll_window(terminal, d);
651}
652
653static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000654terminal_shift_line(struct terminal *terminal, int d)
655{
656 union utf8_char *row;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500657 struct attr *attr_row;
Callum Lowcay69e96582011-01-07 19:47:00 +0000658
659 row = terminal_get_row(terminal, terminal->row);
660 attr_row = terminal_get_attr_row(terminal, terminal->row);
661
662 if ((terminal->width + d) <= terminal->column)
663 d = terminal->column + 1 - terminal->width;
664 if ((terminal->column + d) >= terminal->width)
665 d = terminal->width - terminal->column - 1;
666
667 if (d < 0) {
668 d = 0 - d;
669 memmove(&row[terminal->column],
670 &row[terminal->column + d],
671 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
Callum Lowcay69e96582011-01-07 19:47:00 +0000672 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
673 (terminal->width - terminal->column - d) * sizeof(struct attr));
674 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
675 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
676 } else {
677 memmove(&row[terminal->column + d], &row[terminal->column],
678 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
679 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
680 (terminal->width - terminal->column - d) * sizeof(struct attr));
681 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
682 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
683 }
684}
685
686static void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500687terminal_resize_cells(struct terminal *terminal, int width, int height)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500688{
689 size_t size;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000690 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000691 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000692 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000693 int data_pitch, attr_pitch;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500694 int i, l, total_rows;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500695 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000696 struct winsize ws;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500697
698 if (terminal->width == width && terminal->height == height)
699 return;
700
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000701 data_pitch = width * sizeof(union utf8_char);
702 size = data_pitch * height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500703 data = malloc(size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000704 attr_pitch = width * sizeof(struct attr);
705 data_attr = malloc(attr_pitch * height);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000706 tab_ruler = malloc(width);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500707 memset(data, 0, size);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000708 memset(tab_ruler, 0, width);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000709 attr_init(data_attr, terminal->curr_attr, width * height);
710 if (terminal->data && terminal->data_attr) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500711 if (width > terminal->width)
712 l = terminal->width;
713 else
714 l = width;
715
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500716 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500717 total_rows = height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500718 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500719 total_rows = terminal->height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500720 }
721
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000722 for (i = 0; i < total_rows; i++) {
723 memcpy(&data[width * i],
724 terminal_get_row(terminal, i),
725 l * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000726 memcpy(&data_attr[width * i],
727 terminal_get_attr_row(terminal, i),
728 l * sizeof(struct attr));
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000729 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500730
731 free(terminal->data);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000732 free(terminal->data_attr);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000733 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500734 }
735
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000736 terminal->data_pitch = data_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000737 terminal->attr_pitch = attr_pitch;
Callum Lowcay86653ed2011-01-07 19:47:03 +0000738 terminal->margin_bottom =
739 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500740 terminal->width = width;
741 terminal->height = height;
742 terminal->data = data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000743 terminal->data_attr = data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000744 terminal->tab_ruler = tab_ruler;
745 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500746
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000747 /* Update the window size */
748 ws.ws_row = terminal->height;
749 ws.ws_col = terminal->width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500750 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500751 ws.ws_xpixel = allocation.width;
752 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000753 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500754}
755
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500756static void
757resize_handler(struct widget *widget,
758 int32_t width, int32_t height, void *data)
759{
760 struct terminal *terminal = data;
761 int32_t columns, rows, m;
762
763 m = 2 * terminal->margin;
764 columns = (width - m) / (int32_t) terminal->extents.max_x_advance;
765 rows = (height - m) / (int32_t) terminal->extents.height;
766
767 if (!terminal->fullscreen) {
768 width = columns * terminal->extents.max_x_advance + m;
769 height = rows * terminal->extents.height + m;
770 widget_set_size(terminal->widget, width, height);
771 }
772
773 terminal_resize_cells(terminal, columns, rows);
774}
775
776static void
777terminal_resize(struct terminal *terminal, int columns, int rows)
778{
779 int32_t width, height, m;
780
781 if (terminal->fullscreen)
782 return;
783
784 m = 2 * terminal->margin;
785 width = columns * terminal->extents.max_x_advance + m;
786 height = rows * terminal->extents.height + m;
Kristian Høgsberga1627922012-06-20 17:30:03 -0400787
788 frame_set_child_size(terminal->widget, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500789}
790
Callum Lowcay30eeae52011-01-07 19:46:55 +0000791struct color_scheme DEFAULT_COLORS = {
792 {
793 {0, 0, 0, 1}, /* black */
794 {0.66, 0, 0, 1}, /* red */
795 {0 , 0.66, 0, 1}, /* green */
796 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
797 {0 , 0 , 0.66, 1}, /* blue */
798 {0.66, 0 , 0.66, 1}, /* magenta */
799 {0, 0.66, 0.66, 1}, /* cyan */
800 {0.66, 0.66, 0.66, 1}, /* light grey */
801 {0.22, 0.33, 0.33, 1}, /* dark grey */
802 {1, 0.33, 0.33, 1}, /* high red */
803 {0.33, 1, 0.33, 1}, /* high green */
804 {1, 1, 0.33, 1}, /* high yellow */
805 {0.33, 0.33, 1, 1}, /* high blue */
806 {1, 0.33, 1, 1}, /* high magenta */
807 {0.33, 1, 1, 1}, /* high cyan */
808 {1, 1, 1, 1} /* white */
809 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500810 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000811 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
812};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400813
Kristian Høgsberg22106762008-12-08 13:50:07 -0500814static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500815terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
816{
817 cairo_set_source_rgba(cr,
818 terminal->color_table[index].r,
819 terminal->color_table[index].g,
820 terminal->color_table[index].b,
821 terminal->color_table[index].a);
822}
823
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500824static void
825terminal_send_selection(struct terminal *terminal, int fd)
826{
827 int row, col;
828 union utf8_char *p_row;
829 union decoded_attr attr;
830 FILE *fp;
831 int len;
832
833 fp = fdopen(fd, "w");
834 for (row = 0; row < terminal->height; row++) {
835 p_row = terminal_get_row(terminal, row);
836 for (col = 0; col < terminal->width; col++) {
837 /* get the attributes for this character cell */
838 terminal_decode_attr(terminal, row, col, &attr);
839 if (!attr.attr.s)
840 continue;
841 len = strnlen((char *) p_row[col].byte, 4);
Kristian Høgsberg0dee6472012-07-01 21:25:41 -0400842 if (len > 0)
843 fwrite(p_row[col].byte, 1, len, fp);
844 if (len == 0 || col == terminal->width - 1) {
845 fwrite("\n", 1, 1, fp);
846 break;
847 }
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500848 }
849 }
850 fclose(fp);
851}
852
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500853struct glyph_run {
854 struct terminal *terminal;
855 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400856 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500857 union decoded_attr attr;
858 cairo_glyph_t glyphs[256], *g;
859};
860
861static void
862glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
863{
864 run->terminal = terminal;
865 run->cr = cr;
866 run->g = run->glyphs;
867 run->count = 0;
868 run->attr.key = 0;
869}
870
871static void
872glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
873{
874 cairo_scaled_font_t *font;
875
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500876 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
877 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300878 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500879 font = run->terminal->font_bold;
880 else
881 font = run->terminal->font_normal;
882 cairo_set_scaled_font(run->cr, font);
883 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300884 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500885
Callum Lowcay9d708b02011-01-12 20:06:17 +1300886 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
887 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500888 run->g = run->glyphs;
889 run->count = 0;
890 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300891 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500892}
893
894static void
895glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
896{
897 int num_glyphs;
898 cairo_scaled_font_t *font;
899
900 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
901
Callum Lowcay9d708b02011-01-12 20:06:17 +1300902 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500903 font = run->terminal->font_bold;
904 else
905 font = run->terminal->font_normal;
906
907 cairo_move_to(run->cr, x, y);
908 cairo_scaled_font_text_to_glyphs (font, x, y,
909 (char *) c->byte, 4,
910 &run->g, &num_glyphs,
911 NULL, NULL, NULL);
912 run->g += num_glyphs;
913 run->count += num_glyphs;
914}
915
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500916
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500917static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500918redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500919{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500920 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500921 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500922 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000923 int top_margin, side_margin;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600924 int row, col, cursor_x, cursor_y;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000925 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500926 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000927 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500928 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500929 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500930 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500931 cairo_font_extents_t extents;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500932
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500933 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500934 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500935 cr = cairo_create(surface);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500936 cairo_rectangle(cr, allocation.x, allocation.y,
937 allocation.width, allocation.height);
938 cairo_clip(cr);
939 cairo_push_group(cr);
940
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500941 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500942 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500943 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500944
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500945 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500946
Kristian Høgsberg59826582011-01-20 11:56:57 -0500947 extents = terminal->extents;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500948 side_margin = (allocation.width - terminal->width * extents.max_x_advance) / 2;
949 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500950
Callum Lowcay30eeae52011-01-07 19:46:55 +0000951 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500952 cairo_translate(cr, allocation.x + side_margin,
953 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500954 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000955 for (row = 0; row < terminal->height; row++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000956 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000957 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500958 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000959
Callum Lowcay9d708b02011-01-12 20:06:17 +1300960 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -0500961 continue;
962
Callum Lowcay9d708b02011-01-12 20:06:17 +1300963 terminal_set_color(terminal, cr, attr.attr.bg);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500964 cairo_move_to(cr, col * extents.max_x_advance,
965 row * extents.height);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000966 cairo_rel_line_to(cr, extents.max_x_advance, 0);
967 cairo_rel_line_to(cr, 0, extents.height);
968 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
969 cairo_close_path(cr);
970 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500971 }
972 }
Callum Lowcay30eeae52011-01-07 19:46:55 +0000973
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500974 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
975
976 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500977 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500978 for (row = 0; row < terminal->height; row++) {
979 p_row = terminal_get_row(terminal, row);
980 for (col = 0; col < terminal->width; col++) {
981 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500982 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500983
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500984 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000985
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500986 text_x = col * extents.max_x_advance;
987 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +1300988 if (attr.attr.a & ATTRMASK_UNDERLINE) {
989 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000990 cairo_move_to(cr, text_x, (double)text_y + 1.5);
991 cairo_line_to(cr, text_x + extents.max_x_advance, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000992 cairo_stroke(cr);
993 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -0500994
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500995 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000996 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500997 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500998
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500999 attr.key = ~0;
1000 glyph_run_flush(&run, attr);
1001
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001002 if ((terminal->mode & MODE_SHOW_CURSOR) &&
1003 !window_has_focus(terminal->window)) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001004 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001005
Callum Lowcay30eeae52011-01-07 19:46:55 +00001006 cairo_set_line_width(cr, 1);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001007 cairo_move_to(cr, terminal->column * extents.max_x_advance + d,
1008 terminal->row * extents.height + d);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001009 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
1010 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
1011 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
1012 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001013
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001014 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001015 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001016
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001017 cairo_pop_group_to_source(cr);
1018 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001019 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001020 cairo_surface_destroy(surface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001021
1022 if (terminal->send_cursor_position) {
1023 cursor_x = side_margin + allocation.x +
1024 terminal->column * extents.max_x_advance;
1025 cursor_y = top_margin + allocation.y +
1026 terminal->row * extents.height;
1027 window_set_text_cursor_position(terminal->window,
1028 cursor_x, cursor_y);
1029 terminal->send_cursor_position = 0;
1030 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001031}
1032
1033static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001034terminal_write(struct terminal *terminal, const char *data, size_t length)
1035{
1036 if (write(terminal->master, data, length) < 0)
1037 abort();
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001038 terminal->send_cursor_position = 1;
Kristian Høgsberg26130862011-08-24 11:30:21 -04001039}
1040
1041static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001042terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001043
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001044static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001045handle_char(struct terminal *terminal, union utf8_char utf8);
1046
1047static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001048handle_sgr(struct terminal *terminal, int code);
1049
1050static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001051handle_term_parameter(struct terminal *terminal, int code, int sr)
1052{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001053 int i;
1054
Callum Lowcay67a201d2011-01-12 19:23:41 +13001055 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001056 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001057 case 1: /* DECCKM */
1058 if (sr) terminal->key_mode = KM_APPLICATION;
1059 else terminal->key_mode = KM_NORMAL;
1060 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001061 case 2: /* DECANM */
1062 /* No VT52 support yet */
1063 terminal->g0 = CS_US;
1064 terminal->g1 = CS_US;
1065 terminal->cs = terminal->g0;
1066 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001067 case 3: /* DECCOLM */
1068 if (sr)
1069 terminal_resize(terminal, 132, 24);
1070 else
1071 terminal_resize(terminal, 80, 24);
1072
1073 /* set columns, but also home cursor and clear screen */
1074 terminal->row = 0; terminal->column = 0;
1075 for (i = 0; i < terminal->height; i++) {
1076 memset(terminal_get_row(terminal, i),
1077 0, terminal->data_pitch);
1078 attr_init(terminal_get_attr_row(terminal, i),
1079 terminal->curr_attr, terminal->width);
1080 }
1081 break;
1082 case 5: /* DECSCNM */
1083 if (sr) terminal->mode |= MODE_INVERSE;
1084 else terminal->mode &= ~MODE_INVERSE;
1085 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001086 case 6: /* DECOM */
1087 terminal->origin_mode = sr;
1088 if (terminal->origin_mode)
1089 terminal->row = terminal->margin_top;
1090 else
1091 terminal->row = 0;
1092 terminal->column = 0;
1093 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001094 case 7: /* DECAWM */
1095 if (sr) terminal->mode |= MODE_AUTOWRAP;
1096 else terminal->mode &= ~MODE_AUTOWRAP;
1097 break;
1098 case 8: /* DECARM */
1099 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1100 else terminal->mode &= ~MODE_AUTOREPEAT;
1101 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001102 case 12: /* Very visible cursor (CVVIS) */
1103 /* FIXME: What do we do here. */
1104 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001105 case 25:
1106 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1107 else terminal->mode &= ~MODE_SHOW_CURSOR;
1108 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001109 case 1034: /* smm/rmm, meta mode on/off */
1110 /* ignore */
1111 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001112 case 1037: /* deleteSendsDel */
1113 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1114 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1115 break;
1116 case 1039: /* altSendsEscape */
1117 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1118 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1119 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001120 case 1049: /* rmcup/smcup, alternate screen */
1121 /* Ignore. Should be possible to implement,
1122 * but it's kind of annoying. */
1123 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001124 default:
1125 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1126 break;
1127 }
1128 } else {
1129 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001130 case 4: /* IRM */
1131 if (sr) terminal->mode |= MODE_IRM;
1132 else terminal->mode &= ~MODE_IRM;
1133 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001134 case 20: /* LNM */
1135 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1136 else terminal->mode &= ~MODE_LF_NEWLINE;
1137 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001138 default:
1139 fprintf(stderr, "Unknown parameter: %d\n", code);
1140 break;
1141 }
1142 }
1143}
1144
1145static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001146handle_dcs(struct terminal *terminal)
1147{
1148}
1149
1150static void
1151handle_osc(struct terminal *terminal)
1152{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001153 char *p;
1154 int code;
1155
1156 terminal->escape[terminal->escape_length++] = '\0';
1157 p = &terminal->escape[2];
1158 code = strtol(p, &p, 10);
1159 if (*p == ';') p++;
1160
1161 switch (code) {
1162 case 0: /* Icon name and window title */
1163 case 1: /* Icon label */
1164 case 2: /* Window title*/
1165 window_set_title(terminal->window, p);
1166 break;
1167 default:
1168 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1169 break;
1170 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001171}
1172
1173static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001174handle_escape(struct terminal *terminal)
1175{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001176 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001177 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001178 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001179 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001180 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001181 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001182 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001183
1184 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001185 i = 0;
1186 p = &terminal->escape[2];
1187 while ((isdigit(*p) || *p == ';') && i < 10) {
1188 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001189 if (!set[i]) {
1190 args[i] = 0;
1191 set[i] = 1;
1192 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001193 p++;
1194 i++;
1195 } else {
1196 args[i] = strtol(p, &p, 10);
1197 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001198 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001199 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001200
1201 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001202 case '@': /* ICH */
1203 count = set[0] ? args[0] : 1;
1204 if (count == 0) count = 1;
1205 terminal_shift_line(terminal, count);
1206 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001207 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001208 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001209 if (count == 0) count = 1;
1210 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001211 terminal->row -= count;
1212 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001213 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001214 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001215 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001216 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001217 if (count == 0) count = 1;
1218 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001219 terminal->row += count;
1220 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001221 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001222 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001223 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001224 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001225 if (count == 0) count = 1;
1226 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001227 terminal->column += count;
1228 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001229 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001230 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001231 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001232 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001233 if (count == 0) count = 1;
1234 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001235 terminal->column -= count;
1236 else
1237 terminal->column = 0;
1238 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001239 case 'E': /* CNL */
1240 count = set[0] ? args[0] : 1;
1241 if (terminal->row + count <= terminal->margin_bottom)
1242 terminal->row += count;
1243 else
1244 terminal->row = terminal->margin_bottom;
1245 terminal->column = 0;
1246 break;
1247 case 'F': /* CPL */
1248 count = set[0] ? args[0] : 1;
1249 if (terminal->row - count >= terminal->margin_top)
1250 terminal->row -= count;
1251 else
1252 terminal->row = terminal->margin_top;
1253 terminal->column = 0;
1254 break;
1255 case 'G': /* CHA */
1256 y = set[0] ? args[0] : 1;
1257 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1258
1259 terminal->column = y - 1;
1260 break;
1261 case 'f': /* HVP */
1262 case 'H': /* CUP */
1263 x = (set[1] ? args[1] : 1) - 1;
1264 x = x < 0 ? 0 :
1265 (x >= terminal->width ? terminal->width - 1 : x);
1266
1267 y = (set[0] ? args[0] : 1) - 1;
1268 if (terminal->origin_mode) {
1269 y += terminal->margin_top;
1270 y = y < terminal->margin_top ? terminal->margin_top :
1271 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1272 } else {
1273 y = y < 0 ? 0 :
1274 (y >= terminal->height ? terminal->height - 1 : y);
1275 }
1276
1277 terminal->row = y;
1278 terminal->column = x;
1279 break;
1280 case 'I': /* CHT */
1281 count = set[0] ? args[0] : 1;
1282 if (count == 0) count = 1;
1283 while (count > 0 && terminal->column < terminal->width) {
1284 if (terminal->tab_ruler[terminal->column]) count--;
1285 terminal->column++;
1286 }
1287 terminal->column--;
1288 break;
1289 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001290 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001291 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001292 if (!set[0] || args[0] == 0 || args[0] > 2) {
1293 memset(&row[terminal->column],
1294 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1295 attr_init(&attr_row[terminal->column],
1296 terminal->curr_attr, terminal->width - terminal->column);
1297 for (i = terminal->row + 1; i < terminal->height; i++) {
1298 memset(terminal_get_row(terminal, i),
1299 0, terminal->data_pitch);
1300 attr_init(terminal_get_attr_row(terminal, i),
1301 terminal->curr_attr, terminal->width);
1302 }
1303 } else if (args[0] == 1) {
1304 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1305 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1306 for (i = 0; i < terminal->row; i++) {
1307 memset(terminal_get_row(terminal, i),
1308 0, terminal->data_pitch);
1309 attr_init(terminal_get_attr_row(terminal, i),
1310 terminal->curr_attr, terminal->width);
1311 }
1312 } else if (args[0] == 2) {
1313 for (i = 0; i < terminal->height; i++) {
1314 memset(terminal_get_row(terminal, i),
1315 0, terminal->data_pitch);
1316 attr_init(terminal_get_attr_row(terminal, i),
1317 terminal->curr_attr, terminal->width);
1318 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001319 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001320 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001321 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001322 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001323 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001324 if (!set[0] || args[0] == 0 || args[0] > 2) {
1325 memset(&row[terminal->column], 0,
1326 (terminal->width - terminal->column) * sizeof(union utf8_char));
1327 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1328 terminal->width - terminal->column);
1329 } else if (args[0] == 1) {
1330 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1331 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1332 } else if (args[0] == 2) {
1333 memset(row, 0, terminal->data_pitch);
1334 attr_init(attr_row, terminal->curr_attr, terminal->width);
1335 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001336 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001337 case 'L': /* IL */
1338 count = set[0] ? args[0] : 1;
1339 if (count == 0) count = 1;
1340 if (terminal->row >= terminal->margin_top &&
1341 terminal->row < terminal->margin_bottom)
1342 {
1343 top = terminal->margin_top;
1344 terminal->margin_top = terminal->row;
1345 terminal_scroll(terminal, 0 - count);
1346 terminal->margin_top = top;
1347 } else if (terminal->row == terminal->margin_bottom) {
1348 memset(terminal_get_row(terminal, terminal->row),
1349 0, terminal->data_pitch);
1350 attr_init(terminal_get_attr_row(terminal, terminal->row),
1351 terminal->curr_attr, terminal->width);
1352 }
1353 break;
1354 case 'M': /* DL */
1355 count = set[0] ? args[0] : 1;
1356 if (count == 0) count = 1;
1357 if (terminal->row >= terminal->margin_top &&
1358 terminal->row < terminal->margin_bottom)
1359 {
1360 top = terminal->margin_top;
1361 terminal->margin_top = terminal->row;
1362 terminal_scroll(terminal, count);
1363 terminal->margin_top = top;
1364 } else if (terminal->row == terminal->margin_bottom) {
1365 memset(terminal_get_row(terminal, terminal->row),
1366 0, terminal->data_pitch);
1367 }
1368 break;
1369 case 'P': /* DCH */
1370 count = set[0] ? args[0] : 1;
1371 if (count == 0) count = 1;
1372 terminal_shift_line(terminal, 0 - count);
1373 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001374 case 'S': /* SU */
1375 terminal_scroll(terminal, set[0] ? args[0] : 1);
1376 break;
1377 case 'T': /* SD */
1378 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1379 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001380 case 'X': /* ECH */
1381 count = set[0] ? args[0] : 1;
1382 if (count == 0) count = 1;
1383 if ((terminal->column + count) > terminal->width)
1384 count = terminal->width - terminal->column;
1385 row = terminal_get_row(terminal, terminal->row);
1386 attr_row = terminal_get_attr_row(terminal, terminal->row);
1387 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1388 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1389 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001390 case 'Z': /* CBT */
1391 count = set[0] ? args[0] : 1;
1392 if (count == 0) count = 1;
1393 while (count > 0 && terminal->column >= 0) {
1394 if (terminal->tab_ruler[terminal->column]) count--;
1395 terminal->column--;
1396 }
1397 terminal->column++;
1398 break;
1399 case '`': /* HPA */
1400 y = set[0] ? args[0] : 1;
1401 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1402
1403 terminal->column = y - 1;
1404 break;
1405 case 'b': /* REP */
1406 count = set[0] ? args[0] : 1;
1407 if (count == 0) count = 1;
1408 if (terminal->last_char.byte[0])
1409 for (i = 0; i < count; i++)
1410 handle_char(terminal, terminal->last_char);
1411 terminal->last_char.byte[0] = 0;
1412 break;
1413 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001414 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001415 break;
1416 case 'd': /* VPA */
1417 x = set[0] ? args[0] : 1;
1418 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1419
1420 terminal->row = x - 1;
1421 break;
1422 case 'g': /* TBC */
1423 if (!set[0] || args[0] == 0) {
1424 terminal->tab_ruler[terminal->column] = 0;
1425 } else if (args[0] == 3) {
1426 memset(terminal->tab_ruler, 0, terminal->width);
1427 }
1428 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001429 case 'h': /* SM */
1430 for(i = 0; i < 10 && set[i]; i++) {
1431 handle_term_parameter(terminal, args[i], 1);
1432 }
1433 break;
1434 case 'l': /* RM */
1435 for(i = 0; i < 10 && set[i]; i++) {
1436 handle_term_parameter(terminal, args[i], 0);
1437 }
1438 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001439 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001440 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001441 if (i <= 7 && set[i] && set[i + 1] &&
1442 set[i + 2] && args[i + 1] == 5)
1443 {
1444 if (args[i] == 38) {
1445 handle_sgr(terminal, args[i + 2] + 256);
1446 break;
1447 } else if (args[i] == 48) {
1448 handle_sgr(terminal, args[i + 2] + 512);
1449 break;
1450 }
1451 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001452 if(set[i]) {
1453 handle_sgr(terminal, args[i]);
1454 } else if(i == 0) {
1455 handle_sgr(terminal, 0);
1456 break;
1457 } else {
1458 break;
1459 }
1460 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001461 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001462 case 'n': /* DSR */
1463 i = set[0] ? args[0] : 0;
1464 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001465 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001466 } else if (i == 6) {
1467 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1468 terminal->origin_mode ?
1469 terminal->row+terminal->margin_top : terminal->row+1,
1470 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001471 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001472 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001473 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001474 case 'r':
1475 if(!set[0]) {
1476 terminal->margin_top = 0;
1477 terminal->margin_bottom = terminal->height-1;
1478 terminal->row = 0;
1479 terminal->column = 0;
1480 } else {
1481 top = (set[0] ? args[0] : 1) - 1;
1482 top = top < 0 ? 0 :
1483 (top >= terminal->height ? terminal->height - 1 : top);
1484 bottom = (set[1] ? args[1] : 1) - 1;
1485 bottom = bottom < 0 ? 0 :
1486 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1487 if(bottom > top) {
1488 terminal->margin_top = top;
1489 terminal->margin_bottom = bottom;
1490 } else {
1491 terminal->margin_top = 0;
1492 terminal->margin_bottom = terminal->height-1;
1493 }
1494 if(terminal->origin_mode)
1495 terminal->row = terminal->margin_top;
1496 else
1497 terminal->row = 0;
1498 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001499 }
1500 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001501 case 's':
1502 terminal->saved_row = terminal->row;
1503 terminal->saved_column = terminal->column;
1504 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001505 case 't': /* windowOps */
1506 if (!set[0]) break;
1507 switch (args[0]) {
1508 case 4: /* resize px */
1509 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001510 widget_schedule_resize(terminal->widget,
1511 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001512 }
1513 break;
1514 case 8: /* resize ch */
1515 if (set[1] && set[2]) {
1516 terminal_resize(terminal, args[2], args[1]);
1517 }
1518 break;
1519 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001520 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001521 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1522 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001523 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001524 break;
1525 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001526 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001527 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1528 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001529 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001530 break;
1531 case 18: /* report ch */
1532 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1533 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001534 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001535 break;
1536 case 21: /* report title */
1537 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1538 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001539 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001540 break;
1541 default:
1542 if (args[0] >= 24)
1543 terminal_resize(terminal, terminal->width, args[0]);
1544 else
1545 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1546 break;
1547 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001548 case 'u':
1549 terminal->row = terminal->saved_row;
1550 terminal->column = terminal->saved_column;
1551 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001552 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001553 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001554 break;
1555 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001556}
1557
1558static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001559handle_non_csi_escape(struct terminal *terminal, char code)
1560{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001561 switch(code) {
1562 case 'M': /* RI */
1563 terminal->row -= 1;
1564 if(terminal->row < terminal->margin_top) {
1565 terminal->row = terminal->margin_top;
1566 terminal_scroll(terminal, -1);
1567 }
1568 break;
1569 case 'E': /* NEL */
1570 terminal->column = 0;
1571 // fallthrough
1572 case 'D': /* IND */
1573 terminal->row += 1;
1574 if(terminal->row > terminal->margin_bottom) {
1575 terminal->row = terminal->margin_bottom;
1576 terminal_scroll(terminal, +1);
1577 }
1578 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001579 case 'c': /* RIS */
1580 terminal_init(terminal);
1581 break;
1582 case 'H': /* HTS */
1583 terminal->tab_ruler[terminal->column] = 1;
1584 break;
1585 case '7': /* DECSC */
1586 terminal->saved_row = terminal->row;
1587 terminal->saved_column = terminal->column;
1588 terminal->saved_attr = terminal->curr_attr;
1589 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001590 terminal->saved_cs = terminal->cs;
1591 terminal->saved_g0 = terminal->g0;
1592 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001593 break;
1594 case '8': /* DECRC */
1595 terminal->row = terminal->saved_row;
1596 terminal->column = terminal->saved_column;
1597 terminal->curr_attr = terminal->saved_attr;
1598 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001599 terminal->cs = terminal->saved_cs;
1600 terminal->g0 = terminal->saved_g0;
1601 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001602 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001603 case '=': /* DECPAM */
1604 terminal->key_mode = KM_APPLICATION;
1605 break;
1606 case '>': /* DECPNM */
1607 terminal->key_mode = KM_NORMAL;
1608 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001609 default:
1610 fprintf(stderr, "Unknown escape code: %c\n", code);
1611 break;
1612 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001613}
1614
1615static void
1616handle_special_escape(struct terminal *terminal, char special, char code)
1617{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001618 int i, numChars;
1619
1620 if (special == '#') {
1621 switch(code) {
1622 case '8':
1623 /* fill with 'E', no cheap way to do this */
1624 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1625 numChars = terminal->width * terminal->height;
1626 for(i = 0; i < numChars; i++) {
1627 terminal->data[i].byte[0] = 'E';
1628 }
1629 break;
1630 default:
1631 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1632 break;
1633 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001634 } else if (special == '(' || special == ')') {
1635 switch(code) {
1636 case '0':
1637 if (special == '(')
1638 terminal->g0 = CS_SPECIAL;
1639 else
1640 terminal->g1 = CS_SPECIAL;
1641 break;
1642 case 'A':
1643 if (special == '(')
1644 terminal->g0 = CS_UK;
1645 else
1646 terminal->g1 = CS_UK;
1647 break;
1648 case 'B':
1649 if (special == '(')
1650 terminal->g0 = CS_US;
1651 else
1652 terminal->g1 = CS_US;
1653 break;
1654 default:
1655 fprintf(stderr, "Unknown character set %c\n", code);
1656 break;
1657 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001658 } else {
1659 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1660 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001661}
1662
1663static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001664handle_sgr(struct terminal *terminal, int code)
1665{
1666 switch(code) {
1667 case 0:
1668 terminal->curr_attr = terminal->color_scheme->default_attr;
1669 break;
1670 case 1:
1671 terminal->curr_attr.a |= ATTRMASK_BOLD;
1672 if (terminal->curr_attr.fg < 8)
1673 terminal->curr_attr.fg += 8;
1674 break;
1675 case 4:
1676 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1677 break;
1678 case 5:
1679 terminal->curr_attr.a |= ATTRMASK_BLINK;
1680 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001681 case 8:
1682 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1683 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001684 case 2:
1685 case 21:
1686 case 22:
1687 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1688 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1689 terminal->curr_attr.fg -= 8;
1690 break;
1691 case 24:
1692 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1693 break;
1694 case 25:
1695 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1696 break;
1697 case 7:
1698 case 26:
1699 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1700 break;
1701 case 27:
1702 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1703 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001704 case 28:
1705 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1706 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001707 case 39:
1708 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1709 break;
1710 case 49:
1711 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1712 break;
1713 default:
1714 if(code >= 30 && code <= 37) {
1715 terminal->curr_attr.fg = code - 30;
1716 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1717 terminal->curr_attr.fg += 8;
1718 } else if(code >= 40 && code <= 47) {
1719 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001720 } else if (code >= 90 && code <= 97) {
1721 terminal->curr_attr.fg = code - 90 + 8;
1722 } else if (code >= 100 && code <= 107) {
1723 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001724 } else if(code >= 256 && code < 512) {
1725 terminal->curr_attr.fg = code - 256;
1726 } else if(code >= 512 && code < 768) {
1727 terminal->curr_attr.bg = code - 512;
1728 } else {
1729 fprintf(stderr, "Unknown SGR code: %d\n", code);
1730 }
1731 break;
1732 }
1733}
1734
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001735/* Returns 1 if c was special, otherwise 0 */
1736static int
1737handle_special_char(struct terminal *terminal, char c)
1738{
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001739 union utf8_char *row;
1740 struct attr *attr_row;
1741
1742 row = terminal_get_row(terminal, terminal->row);
1743 attr_row = terminal_get_attr_row(terminal, terminal->row);
1744
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001745 switch(c) {
1746 case '\r':
1747 terminal->column = 0;
1748 break;
1749 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001750 if (terminal->mode & MODE_LF_NEWLINE) {
1751 terminal->column = 0;
1752 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001753 /* fallthrough */
1754 case '\v':
1755 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001756 terminal->row++;
1757 if(terminal->row > terminal->margin_bottom) {
1758 terminal->row = terminal->margin_bottom;
1759 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001760 }
1761
1762 break;
1763 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001764 while (terminal->column < terminal->width) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001765 if (terminal->mode & MODE_IRM)
1766 terminal_shift_line(terminal, +1);
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001767
1768 if (row[terminal->column].byte[0] == '\0') {
1769 row[terminal->column].byte[0] = ' ';
1770 row[terminal->column].byte[1] = '\0';
1771 attr_row[terminal->column] = terminal->curr_attr;
1772 }
1773
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001774 terminal->column++;
Kristian Høgsbergcca3c2f2012-06-20 15:56:13 -04001775 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001776 }
1777 if (terminal->column >= terminal->width) {
1778 terminal->column = terminal->width - 1;
1779 }
1780
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001781 break;
1782 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001783 if (terminal->column >= terminal->width) {
1784 terminal->column = terminal->width - 2;
1785 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001786 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001787 } else if (terminal->mode & MODE_AUTOWRAP) {
1788 terminal->column = terminal->width - 1;
1789 terminal->row -= 1;
1790 if (terminal->row < terminal->margin_top) {
1791 terminal->row = terminal->margin_top;
1792 terminal_scroll(terminal, -1);
1793 }
1794 }
1795
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001796 break;
1797 case '\a':
1798 /* Bell */
1799 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001800 case '\x0E': /* SO */
1801 terminal->cs = terminal->g1;
1802 break;
1803 case '\x0F': /* SI */
1804 terminal->cs = terminal->g0;
1805 break;
Kristian Høgsberg2a1aa4e2012-08-03 09:37:05 -04001806 case '\0':
1807 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001808 default:
1809 return 0;
1810 }
1811
1812 return 1;
1813}
1814
1815static void
1816handle_char(struct terminal *terminal, union utf8_char utf8)
1817{
1818 union utf8_char *row;
1819 struct attr *attr_row;
1820
1821 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001822
1823 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001824
1825 /* There are a whole lot of non-characters, control codes,
1826 * and formatting codes that should probably be ignored,
1827 * for example: */
1828 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1829 /* BOM, ignore */
1830 return;
1831 }
1832
1833 /* Some of these non-characters should be translated, e.g.: */
1834 if (utf8.byte[0] < 32) {
1835 utf8.byte[0] = utf8.byte[0] + 64;
1836 }
1837
1838 /* handle right margin effects */
1839 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001840 if (terminal->mode & MODE_AUTOWRAP) {
1841 terminal->column = 0;
1842 terminal->row += 1;
1843 if (terminal->row > terminal->margin_bottom) {
1844 terminal->row = terminal->margin_bottom;
1845 terminal_scroll(terminal, +1);
1846 }
1847 } else {
1848 terminal->column--;
1849 }
1850 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001851
1852 row = terminal_get_row(terminal, terminal->row);
1853 attr_row = terminal_get_attr_row(terminal, terminal->row);
1854
Callum Lowcay69e96582011-01-07 19:47:00 +00001855 if (terminal->mode & MODE_IRM)
1856 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001857 row[terminal->column] = utf8;
1858 attr_row[terminal->column++] = terminal->curr_attr;
1859
1860 if (utf8.ch != terminal->last_char.ch)
1861 terminal->last_char = utf8;
1862}
1863
Callum Lowcay30eeae52011-01-07 19:46:55 +00001864static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001865escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1866{
1867 int len, i;
1868
1869 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1870 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1871 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1872 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1873 else len = 1; /* Invalid, cannot happen */
1874
1875 if (terminal->escape_length + len <= MAX_ESCAPE) {
1876 for (i = 0; i < len; i++)
1877 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1878 terminal->escape_length += len;
1879 } else if (terminal->escape_length < MAX_ESCAPE) {
1880 terminal->escape[terminal->escape_length++] = 0;
1881 }
1882}
1883
1884static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001885terminal_data(struct terminal *terminal, const char *data, size_t length)
1886{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001887 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001888 union utf8_char utf8;
1889 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001890
1891 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001892 parser_state =
1893 utf8_next_char(&terminal->state_machine, data[i]);
1894 switch(parser_state) {
1895 case utf8state_accept:
1896 utf8.ch = terminal->state_machine.s.ch;
1897 break;
1898 case utf8state_reject:
1899 /* the unicode replacement character */
1900 utf8.byte[0] = 0xEF;
1901 utf8.byte[1] = 0xBF;
1902 utf8.byte[2] = 0xBD;
1903 utf8.byte[3] = 0x00;
1904 break;
1905 default:
1906 continue;
1907 }
1908
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001909 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001910 switch (terminal->state) {
1911 case escape_state_escape:
1912 escape_append_utf8(terminal, utf8);
1913 switch (utf8.byte[0]) {
1914 case 'P': /* DCS */
1915 terminal->state = escape_state_dcs;
1916 break;
1917 case '[': /* CSI */
1918 terminal->state = escape_state_csi;
1919 break;
1920 case ']': /* OSC */
1921 terminal->state = escape_state_osc;
1922 break;
1923 case '#':
1924 case '(':
1925 case ')': /* special */
1926 terminal->state = escape_state_special;
1927 break;
1928 case '^': /* PM (not implemented) */
1929 case '_': /* APC (not implemented) */
1930 terminal->state = escape_state_ignore;
1931 break;
1932 default:
1933 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001934 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13001935 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001936 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001937 continue;
1938 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001939 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1940 /* do nothing */
1941 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001942 terminal->escape_flags |= ESC_FLAG_WHAT;
1943 } else if (utf8.byte[0] == '>') {
1944 terminal->escape_flags |= ESC_FLAG_GT;
1945 } else if (utf8.byte[0] == '!') {
1946 terminal->escape_flags |= ESC_FLAG_BANG;
1947 } else if (utf8.byte[0] == '$') {
1948 terminal->escape_flags |= ESC_FLAG_CASH;
1949 } else if (utf8.byte[0] == '\'') {
1950 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1951 } else if (utf8.byte[0] == '"') {
1952 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1953 } else if (utf8.byte[0] == ' ') {
1954 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001955 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001956 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001957 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13001958 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001959 }
1960
1961 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1962 utf8.byte[0] == '`')
1963 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001964 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001965 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001966 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001967 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001968 continue;
1969 case escape_state_inner_escape:
1970 if (utf8.byte[0] == '\\') {
1971 terminal->state = escape_state_normal;
1972 if (terminal->outer_state == escape_state_dcs) {
1973 handle_dcs(terminal);
1974 } else if (terminal->outer_state == escape_state_osc) {
1975 handle_osc(terminal);
1976 }
1977 } else if (utf8.byte[0] == '\e') {
1978 terminal->state = terminal->outer_state;
1979 escape_append_utf8(terminal, utf8);
1980 if (terminal->escape_length >= MAX_ESCAPE)
1981 terminal->state = escape_state_normal;
1982 } else {
1983 terminal->state = terminal->outer_state;
1984 if (terminal->escape_length < MAX_ESCAPE)
1985 terminal->escape[terminal->escape_length++] = '\e';
1986 escape_append_utf8(terminal, utf8);
1987 if (terminal->escape_length >= MAX_ESCAPE)
1988 terminal->state = escape_state_normal;
1989 }
1990 continue;
1991 case escape_state_dcs:
1992 case escape_state_osc:
1993 case escape_state_ignore:
1994 if (utf8.byte[0] == '\e') {
1995 terminal->outer_state = terminal->state;
1996 terminal->state = escape_state_inner_escape;
1997 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
1998 terminal->state = escape_state_normal;
1999 handle_osc(terminal);
2000 } else {
2001 escape_append_utf8(terminal, utf8);
2002 if (terminal->escape_length >= MAX_ESCAPE)
2003 terminal->state = escape_state_normal;
2004 }
2005 continue;
2006 case escape_state_special:
2007 escape_append_utf8(terminal, utf8);
2008 terminal->state = escape_state_normal;
2009 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2010 handle_special_escape(terminal, terminal->escape[1],
2011 utf8.byte[0]);
2012 }
2013 continue;
2014 default:
2015 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002016 }
2017
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002018 /* this is valid, because ASCII characters are never used to
2019 * introduce a multibyte sequence in UTF-8 */
2020 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002021 terminal->state = escape_state_escape;
2022 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002023 terminal->escape[0] = '\e';
2024 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002025 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002026 } else {
2027 handle_char(terminal, utf8);
2028 } /* if */
2029 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002030
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002031 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002032}
2033
2034static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002035data_source_target(void *data,
2036 struct wl_data_source *source, const char *mime_type)
2037{
2038 fprintf(stderr, "data_source_target, %s\n", mime_type);
2039}
2040
2041static void
2042data_source_send(void *data,
2043 struct wl_data_source *source,
2044 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002045{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002046 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002047
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002048 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002049}
2050
2051static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002052data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002053{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002054 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002055}
2056
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002057static const struct wl_data_source_listener data_source_listener = {
2058 data_source_target,
2059 data_source_send,
2060 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002061};
2062
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002063static void
2064fullscreen_handler(struct window *window, void *data)
2065{
2066 struct terminal *terminal = data;
2067
2068 terminal->fullscreen ^= 1;
2069 window_set_fullscreen(window, terminal->fullscreen);
2070}
2071
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002072static int
2073handle_bound_key(struct terminal *terminal,
2074 struct input *input, uint32_t sym, uint32_t time)
2075{
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002076 struct terminal *new_terminal;
2077
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002078 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002079 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002080 /* Cut selection; terminal doesn't do cut, fall
2081 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002082 case XKB_KEY_C:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002083 terminal->selection =
2084 display_create_data_source(terminal->display);
2085 wl_data_source_offer(terminal->selection,
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002086 "text/plain;charset=utf-8");
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002087 wl_data_source_add_listener(terminal->selection,
2088 &data_source_listener, terminal);
Kristian Høgsbergfee91be2012-06-02 21:21:41 -04002089 input_set_selection(input, terminal->selection,
2090 display_get_serial(terminal->display));
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002091 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002092 case XKB_KEY_V:
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002093 input_receive_selection_data_to_fd(input,
2094 "text/plain;charset=utf-8",
2095 terminal->master);
2096
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002097 return 1;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002098
2099 case XKB_KEY_N:
2100 new_terminal =
2101 terminal_create(terminal->display, option_fullscreen);
2102 if (terminal_run(new_terminal, option_shell))
2103 terminal_destroy(new_terminal);
2104
2105 return 1;
2106
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002107 default:
2108 return 0;
2109 }
2110}
2111
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002112static void
2113key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002114 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2115 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002116{
2117 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002118 char ch[MAX_RESPONSE];
Kristian Høgsberg88fd4082012-06-21 15:55:03 -04002119 uint32_t modifiers, serial;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002120 int ret, len = 0;
2121 bool convert_utf8 = true;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002122
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002123 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002124 if ((modifiers & MOD_CONTROL_MASK) &&
2125 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002126 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2127 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002128 return;
2129
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002130 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002131 case XKB_KEY_BackSpace:
Kristian Høgsbergb7f94bf2012-06-21 12:29:36 -04002132 if (modifiers & MOD_ALT_MASK)
2133 ch[len++] = 0x1b;
Kristian Høgsberg71a4cf42012-06-20 17:57:56 -04002134 ch[len++] = 0x7f;
2135 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002136 case XKB_KEY_Tab:
2137 case XKB_KEY_Linefeed:
2138 case XKB_KEY_Clear:
2139 case XKB_KEY_Pause:
2140 case XKB_KEY_Scroll_Lock:
2141 case XKB_KEY_Sys_Req:
2142 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002143 ch[len++] = sym & 0x7f;
2144 break;
2145
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002146 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002147 if (terminal->mode & MODE_LF_NEWLINE) {
2148 ch[len++] = 0x0D;
2149 ch[len++] = 0x0A;
2150 } else {
2151 ch[len++] = 0x0D;
2152 }
2153 break;
2154
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002155 case XKB_KEY_Shift_L:
2156 case XKB_KEY_Shift_R:
2157 case XKB_KEY_Control_L:
2158 case XKB_KEY_Control_R:
2159 case XKB_KEY_Alt_L:
2160 case XKB_KEY_Alt_R:
Kristian Høgsberg22fbcf72012-06-22 12:18:56 -04002161 case XKB_KEY_Meta_L:
2162 case XKB_KEY_Meta_R:
2163 case XKB_KEY_Super_L:
2164 case XKB_KEY_Super_R:
2165 case XKB_KEY_Hyper_L:
2166 case XKB_KEY_Hyper_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002167 break;
2168
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002169 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002170 len = function_key_response('[', 2, modifiers, '~', ch);
2171 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002172 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002173 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2174 ch[len++] = '\x04';
2175 } else {
2176 len = function_key_response('[', 3, modifiers, '~', ch);
2177 }
2178 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002179 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002180 len = function_key_response('[', 5, modifiers, '~', ch);
2181 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002182 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002183 len = function_key_response('[', 6, modifiers, '~', ch);
2184 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002185 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002186 len = function_key_response('O', 1, modifiers, 'P', ch);
2187 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002188 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002189 len = function_key_response('O', 1, modifiers, 'Q', ch);
2190 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002191 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002192 len = function_key_response('O', 1, modifiers, 'R', ch);
2193 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002194 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002195 len = function_key_response('O', 1, modifiers, 'S', ch);
2196 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002197 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002198 len = function_key_response('[', 15, modifiers, '~', ch);
2199 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002200 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002201 len = function_key_response('[', 17, modifiers, '~', ch);
2202 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002203 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002204 len = function_key_response('[', 18, modifiers, '~', ch);
2205 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002206 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002207 len = function_key_response('[', 19, modifiers, '~', ch);
2208 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002209 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002210 len = function_key_response('[', 20, modifiers, '~', ch);
2211 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002212 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002213 len = function_key_response('[', 21, modifiers, '~', ch);
2214 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002215 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002216 len = function_key_response('[', 24, modifiers, '~', ch);
2217 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002218 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002219 /* Handle special keys with alternate mappings */
2220 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2221 if (len != 0) break;
2222
Kristian Høgsberg70163132012-05-08 15:55:39 -04002223 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002224 if (sym >= '3' && sym <= '7')
2225 sym = (sym & 0x1f) + 8;
2226
2227 if (!((sym >= '!' && sym <= '/') ||
2228 (sym >= '8' && sym <= '?') ||
2229 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2230 else if (sym == '2') sym = 0x00;
2231 else if (sym == '/') sym = 0x1F;
2232 else if (sym == '8' || sym == '?') sym = 0x7F;
Kristian Høgsbergae9e0732012-06-21 12:30:15 -04002233 }
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002234 if (modifiers & MOD_ALT_MASK) {
2235 if (terminal->mode & MODE_ALT_SENDS_ESC) {
2236 ch[len++] = 0x1b;
2237 } else {
2238 sym = sym | 0x80;
2239 convert_utf8 = false;
2240 }
Callum Lowcay7e08e902011-01-07 19:47:02 +00002241 }
2242
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002243 if ((sym < 128) ||
2244 (!convert_utf8 && sym < 256)) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002245 ch[len++] = sym;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002246 } else {
2247 ret = xkb_keysym_to_utf8(sym, ch + len,
2248 MAX_RESPONSE - len);
2249 if (ret < 0)
2250 fprintf(stderr,
2251 "Warning: buffer too small to encode "
2252 "UTF8 character\n");
2253 else
2254 len += ret;
2255 }
2256
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002257 break;
2258 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002259
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002260 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04002261 terminal_write(terminal, ch, len);
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002262
2263 /* Hide cursor, except if this was coming from a
2264 * repeating key press. */
2265 serial = display_get_serial(terminal->display);
2266 if (terminal->hide_cursor_serial != serial) {
2267 input_set_pointer_image(input, CURSOR_BLANK);
2268 terminal->hide_cursor_serial = serial;
2269 }
2270 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002271}
2272
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002273static void
2274keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002275 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002276{
2277 struct terminal *terminal = data;
2278
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002279 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002280}
2281
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002282static int wordsep(int ch)
2283{
2284 const char extra[] = "-,./?%&#:_=+@~";
2285
Andre Heider552d12b2012-08-02 20:59:43 +02002286 if (ch > 127)
2287 return 1;
2288
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002289 return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
2290}
2291
2292static int
2293recompute_selection(struct terminal *terminal)
2294{
2295 struct rectangle allocation;
2296 int col, x, width, height;
2297 int start_row, end_row;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002298 int word_start, eol;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002299 int side_margin, top_margin;
2300 int start_x, end_x;
2301 int cw, ch;
2302 union utf8_char *data;
2303
2304 cw = terminal->extents.max_x_advance;
2305 ch = terminal->extents.height;
2306 widget_get_allocation(terminal->widget, &allocation);
2307 width = terminal->width * cw;
2308 height = terminal->height * ch;
2309 side_margin = allocation.x + (allocation.width - width) / 2;
2310 top_margin = allocation.y + (allocation.height - height) / 2;
2311
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002312 start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1;
2313 end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1;
2314
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002315 if (start_row < end_row ||
2316 (start_row == end_row &&
2317 terminal->selection_start_x < terminal->selection_end_x)) {
2318 terminal->selection_start_row = start_row;
2319 terminal->selection_end_row = end_row;
2320 start_x = terminal->selection_start_x;
2321 end_x = terminal->selection_end_x;
2322 } else {
2323 terminal->selection_start_row = end_row;
2324 terminal->selection_end_row = start_row;
2325 start_x = terminal->selection_end_x;
2326 end_x = terminal->selection_start_x;
2327 }
2328
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002329 eol = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002330 if (terminal->selection_start_row < 0) {
2331 terminal->selection_start_row = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002332 terminal->selection_start_col = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002333 } else {
2334 x = side_margin + cw / 2;
2335 data = terminal_get_row(terminal,
2336 terminal->selection_start_row);
2337 word_start = 0;
2338 for (col = 0; col < terminal->width; col++, x += cw) {
2339 if (col == 0 || wordsep(data[col - 1].ch))
2340 word_start = col;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002341 if (data[col].ch != 0)
2342 eol = col + 1;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002343 if (start_x < x)
2344 break;
2345 }
2346
2347 switch (terminal->dragging) {
2348 case SELECT_LINE:
2349 terminal->selection_start_col = 0;
2350 break;
2351 case SELECT_WORD:
2352 terminal->selection_start_col = word_start;
2353 break;
2354 case SELECT_CHAR:
2355 terminal->selection_start_col = col;
2356 break;
2357 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002358 }
2359
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002360 if (terminal->selection_end_row >= terminal->height) {
2361 terminal->selection_end_row = terminal->height;
2362 terminal->selection_end_col = 0;
2363 } else {
2364 x = side_margin + cw / 2;
2365 data = terminal_get_row(terminal, terminal->selection_end_row);
2366 for (col = 0; col < terminal->width; col++, x += cw) {
2367 if (terminal->dragging == SELECT_CHAR && end_x < x)
2368 break;
2369 if (terminal->dragging == SELECT_WORD &&
2370 end_x < x && wordsep(data[col].ch))
2371 break;
2372 }
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002373 terminal->selection_end_col = col;
2374 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002375
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002376 if (terminal->selection_end_col != terminal->selection_start_col ||
2377 terminal->selection_start_row != terminal->selection_end_row) {
2378 col = terminal->selection_end_col;
2379 if (col > 0 && data[col - 1].ch == 0)
2380 terminal->selection_end_col = terminal->width;
2381 data = terminal_get_row(terminal, terminal->selection_start_row);
2382 if (data[terminal->selection_start_col].ch == 0)
2383 terminal->selection_start_col = eol;
2384 }
2385
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002386 return 1;
2387}
2388
Kristian Høgsberg59826582011-01-20 11:56:57 -05002389static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002390button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002391 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002392 uint32_t button,
2393 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002394{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002395 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002396
2397 switch (button) {
2398 case 272:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002399 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002400
2401 if (time - terminal->button_time < 500)
2402 terminal->click_count++;
2403 else
2404 terminal->click_count = 1;
2405
2406 terminal->button_time = time;
2407 terminal->dragging =
2408 (terminal->click_count - 1) % 3 + SELECT_CHAR;
2409
Kristian Høgsberg59826582011-01-20 11:56:57 -05002410 input_get_position(input,
2411 &terminal->selection_start_x,
2412 &terminal->selection_start_y);
2413 terminal->selection_end_x = terminal->selection_start_x;
2414 terminal->selection_end_y = terminal->selection_start_y;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002415 if (recompute_selection(terminal))
2416 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002417 } else {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002418 terminal->dragging = SELECT_NONE;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002419 }
2420 break;
2421 }
2422}
2423
2424static int
Kristian Høgsberg29784402012-06-28 14:27:02 -04002425enter_handler(struct widget *widget,
2426 struct input *input, float x, float y, void *data)
2427{
2428 return CURSOR_IBEAM;
2429}
2430
2431static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002432motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002433 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002434 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002435{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002436 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002437
2438 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002439 input_get_position(input,
2440 &terminal->selection_end_x,
2441 &terminal->selection_end_y);
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002442
2443 if (recompute_selection(terminal))
2444 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002445 }
2446
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002447 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002448}
2449
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002450static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002451terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002452{
2453 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002454 cairo_surface_t *surface;
2455 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002456
2457 terminal = malloc(sizeof *terminal);
2458 if (terminal == NULL)
2459 return terminal;
2460
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002461 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002462 terminal->fullscreen = fullscreen;
Callum Lowcay30eeae52011-01-07 19:46:55 +00002463 terminal->color_scheme = &DEFAULT_COLORS;
2464 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002465 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002466 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002467 terminal->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002468 terminal->widget = frame_create(terminal->window, terminal);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002469 window_set_title(terminal->window, "Wayland Terminal");
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002470 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002471
2472 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002473 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002474
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002475 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002476 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002477
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002478 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002479 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002480 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002481 keyboard_focus_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002482 window_set_fullscreen_handler(terminal->window, fullscreen_handler);
2483
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002484 widget_set_redraw_handler(terminal->widget, redraw_handler);
2485 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002486 widget_set_button_handler(terminal->widget, button_handler);
Kristian Høgsberg29784402012-06-28 14:27:02 -04002487 widget_set_enter_handler(terminal->widget, enter_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002488 widget_set_motion_handler(terminal->widget, motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002489
Kristian Høgsberg09531622010-06-14 23:22:15 -04002490 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2491 cr = cairo_create(surface);
Kristian Høgsberg38912df2012-06-27 17:52:23 -04002492 cairo_set_font_size(cr, option_font_size);
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002493 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002494 CAIRO_FONT_SLANT_NORMAL,
2495 CAIRO_FONT_WEIGHT_BOLD);
2496 terminal->font_bold = cairo_get_scaled_font (cr);
2497 cairo_scaled_font_reference(terminal->font_bold);
2498
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002499 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002500 CAIRO_FONT_SLANT_NORMAL,
2501 CAIRO_FONT_WEIGHT_NORMAL);
2502 terminal->font_normal = cairo_get_scaled_font (cr);
2503 cairo_scaled_font_reference(terminal->font_normal);
2504
Kristian Høgsberg09531622010-06-14 23:22:15 -04002505 cairo_font_extents(cr, &terminal->extents);
2506 cairo_destroy(cr);
2507 cairo_surface_destroy(surface);
2508
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002509 terminal_resize(terminal, 20, 5); /* Set minimum size first */
Kristian Høgsberga1627922012-06-20 17:30:03 -04002510 terminal_resize(terminal, 80, 25);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002511
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002512 wl_list_insert(terminal_list.prev, &terminal->link);
2513
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002514 return terminal;
2515}
2516
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002517static void
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002518terminal_destroy(struct terminal *terminal)
2519{
2520 window_destroy(terminal->window);
2521 close(terminal->master);
2522 wl_list_remove(&terminal->link);
2523 free(terminal);
2524
2525 if (wl_list_empty(&terminal_list))
2526 exit(0);
2527}
2528
2529static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002530io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002531{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002532 struct terminal *terminal =
2533 container_of(task, struct terminal, io_task);
2534 char buffer[256];
2535 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002536
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002537 if (events & EPOLLHUP) {
2538 terminal_destroy(terminal);
2539 return;
2540 }
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002541
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002542 len = read(terminal->master, buffer, sizeof buffer);
2543 if (len < 0)
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002544 terminal_destroy(terminal);
2545 else
2546 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002547}
2548
2549static int
2550terminal_run(struct terminal *terminal, const char *path)
2551{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002552 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002553 pid_t pid;
2554
2555 pid = forkpty(&master, NULL, NULL, NULL);
2556 if (pid == 0) {
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04002557 setenv("TERM", option_term, 1);
2558 setenv("COLORTERM", option_term, 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002559 if (execl(path, path, NULL)) {
2560 printf("exec failed: %m\n");
2561 exit(EXIT_FAILURE);
2562 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002563 } else if (pid < 0) {
2564 fprintf(stderr, "failed to fork and create pty (%m).\n");
2565 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002566 }
2567
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002568 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002569 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002570 terminal->io_task.run = io_handler;
2571 display_watch_fd(terminal->display, terminal->master,
2572 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002573
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002574 window_set_fullscreen(terminal->window, terminal->fullscreen);
2575 if (!terminal->fullscreen)
2576 terminal_resize(terminal, 80, 24);
2577
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002578 return 0;
2579}
2580
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002581static const struct config_key terminal_config_keys[] = {
2582 { "font", CONFIG_KEY_STRING, &option_font },
Kristian Høgsberg38912df2012-06-27 17:52:23 -04002583 { "font-size", CONFIG_KEY_INTEGER, &option_font_size },
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04002584 { "term", CONFIG_KEY_STRING, &option_term },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002585};
2586
2587static const struct config_section config_sections[] = {
2588 { "terminal",
2589 terminal_config_keys, ARRAY_LENGTH(terminal_config_keys) },
2590};
2591
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002592static const struct weston_option terminal_options[] = {
2593 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002594 { WESTON_OPTION_STRING, "font", 0, &option_font },
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002595 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002596};
2597
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002598int main(int argc, char *argv[])
2599{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002600 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002601 struct terminal *terminal;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002602 char *config_file;
2603
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002604 option_shell = getenv("SHELL");
2605 if (!option_shell)
2606 option_shell = "/bin/bash";
2607
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002608 config_file = config_file_path("weston.ini");
2609 parse_config_file(config_file,
2610 config_sections, ARRAY_LENGTH(config_sections),
2611 NULL);
2612 free(config_file);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002613
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002614 argc = parse_options(terminal_options,
2615 ARRAY_LENGTH(terminal_options), argc, argv);
2616
2617 d = display_create(argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002618 if (d == NULL) {
2619 fprintf(stderr, "failed to create display: %m\n");
2620 return -1;
2621 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002622
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002623 wl_list_init(&terminal_list);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002624 terminal = terminal_create(d, option_fullscreen);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002625 if (terminal_run(terminal, option_shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002626 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002627
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002628 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002629
2630 return 0;
2631}