blob: a726232b3628f3e364b927611b8cdb20a395ce41 [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
23#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <fcntl.h>
28#include <unistd.h>
29#include <math.h>
30#include <time.h>
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050031#include <pty.h>
Kristian Høgsbergf04e8382008-12-08 00:07:49 -050032#include <ctype.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050033#include <cairo.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040034#include <sys/epoll.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050035
Pekka Paalanen50719bc2011-11-22 14:18:50 +020036#include <wayland-client.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050037
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -040038#include "../shared/config-parser.h"
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050039#include "window.h"
40
Kristian Høgsberg0395f302008-12-22 12:14:50 -050041static int option_fullscreen;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -040042static char *option_font = "mono";
Kristian Høgsberg38912df2012-06-27 17:52:23 -040043static int option_font_size = 14;
Kristian Høgsbergde845cf2012-06-20 22:14:31 -040044static char *option_term = "xterm";
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040045static char *option_shell;
46
47static struct wl_list terminal_list;
48
49static struct terminal *
50terminal_create(struct display *display, int fullscreen);
51static void
52terminal_destroy(struct terminal *terminal);
53static int
54terminal_run(struct terminal *terminal, const char *path);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050055
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050056#define MOD_SHIFT 0x01
57#define MOD_ALT 0x02
58#define MOD_CTRL 0x04
59
Callum Lowcay30eeae52011-01-07 19:46:55 +000060#define ATTRMASK_BOLD 0x01
61#define ATTRMASK_UNDERLINE 0x02
62#define ATTRMASK_BLINK 0x04
63#define ATTRMASK_INVERSE 0x08
Callum Lowcay81179db2011-01-10 12:14:01 +130064#define ATTRMASK_CONCEALED 0x10
Callum Lowcay30eeae52011-01-07 19:46:55 +000065
Callum Lowcayb8609ad2011-01-07 19:46:57 +000066/* Buffer sizes */
Callum Lowcayef57a9b2011-01-14 20:46:23 +130067#define MAX_RESPONSE 256
68#define MAX_ESCAPE 255
Callum Lowcayb8609ad2011-01-07 19:46:57 +000069
Callum Lowcay8e57dd52011-01-07 19:46:59 +000070/* Terminal modes */
71#define MODE_SHOW_CURSOR 0x00000001
72#define MODE_INVERSE 0x00000002
73#define MODE_AUTOWRAP 0x00000004
74#define MODE_AUTOREPEAT 0x00000008
75#define MODE_LF_NEWLINE 0x00000010
Callum Lowcay69e96582011-01-07 19:47:00 +000076#define MODE_IRM 0x00000020
Callum Lowcay7e08e902011-01-07 19:47:02 +000077#define MODE_DELETE_SENDS_DEL 0x00000040
78#define MODE_ALT_SENDS_ESC 0x00000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +000079
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000080union utf8_char {
81 unsigned char byte[4];
82 uint32_t ch;
83};
84
85enum utf8_state {
86 utf8state_start,
87 utf8state_accept,
88 utf8state_reject,
89 utf8state_expect3,
90 utf8state_expect2,
91 utf8state_expect1
92};
93
94struct utf8_state_machine {
95 enum utf8_state state;
96 int len;
97 union utf8_char s;
98};
99
100static void
101init_state_machine(struct utf8_state_machine *machine)
102{
103 machine->state = utf8state_start;
104 machine->len = 0;
105 machine->s.ch = 0;
106}
107
108static enum utf8_state
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400109utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000110{
111 switch(machine->state) {
112 case utf8state_start:
113 case utf8state_accept:
114 case utf8state_reject:
115 machine->s.ch = 0;
116 machine->len = 0;
117 if(c == 0xC0 || c == 0xC1) {
118 /* overlong encoding, reject */
119 machine->state = utf8state_reject;
120 } else if((c & 0x80) == 0) {
121 /* single byte, accept */
122 machine->s.byte[machine->len++] = c;
123 machine->state = utf8state_accept;
124 } else if((c & 0xC0) == 0x80) {
125 /* parser out of sync, ignore byte */
126 machine->state = utf8state_start;
127 } else if((c & 0xE0) == 0xC0) {
128 /* start of two byte sequence */
129 machine->s.byte[machine->len++] = c;
130 machine->state = utf8state_expect1;
131 } else if((c & 0xF0) == 0xE0) {
132 /* start of three byte sequence */
133 machine->s.byte[machine->len++] = c;
134 machine->state = utf8state_expect2;
135 } else if((c & 0xF8) == 0xF0) {
136 /* start of four byte sequence */
137 machine->s.byte[machine->len++] = c;
138 machine->state = utf8state_expect3;
139 } else {
140 /* overlong encoding, reject */
141 machine->state = utf8state_reject;
142 }
143 break;
144 case utf8state_expect3:
145 machine->s.byte[machine->len++] = c;
146 if((c & 0xC0) == 0x80) {
147 /* all good, continue */
148 machine->state = utf8state_expect2;
149 } else {
150 /* missing extra byte, reject */
151 machine->state = utf8state_reject;
152 }
153 break;
154 case utf8state_expect2:
155 machine->s.byte[machine->len++] = c;
156 if((c & 0xC0) == 0x80) {
157 /* all good, continue */
158 machine->state = utf8state_expect1;
159 } else {
160 /* missing extra byte, reject */
161 machine->state = utf8state_reject;
162 }
163 break;
164 case utf8state_expect1:
165 machine->s.byte[machine->len++] = c;
166 if((c & 0xC0) == 0x80) {
167 /* all good, accept */
168 machine->state = utf8state_accept;
169 } else {
170 /* missing extra byte, reject */
171 machine->state = utf8state_reject;
172 }
173 break;
174 default:
175 machine->state = utf8state_reject;
176 break;
177 }
178
179 return machine->state;
180}
181
Callum Lowcay256e72f2011-01-07 19:47:01 +0000182struct char_sub {
183 union utf8_char match;
184 union utf8_char replace;
185};
186/* Set last char_sub match to NULL char */
187typedef struct char_sub *character_set;
188
189struct char_sub CS_US[] = {
190 {{{0, }}, {{0, }}}
191};
192static struct char_sub CS_UK[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200193 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000194 {{{0, }}, {{0, }}}
195};
196static struct char_sub CS_SPECIAL[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200197 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond: ♦ */
198 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell: ▒ */
199 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT: ␉ */
200 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF: ␌ */
201 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR: ␍ */
202 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF: ␊ */
203 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree: ° */
204 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus: ± */
205 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL: ␤ */
206 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT: ␋ */
207 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB: ┘ */
208 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT: ┐ */
209 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT: ┌ */
210 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_LB: └ */
211 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS: ┼ */
212 {{{'o', 0, }}, {{0xE2, 0x8E, 0xBA, 0}}}, /* Horiz. Scan Line 1: ⎺ */
213 {{{'p', 0, }}, {{0xE2, 0x8E, 0xBB, 0}}}, /* Horiz. Scan Line 3: ⎻ */
214 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* Horiz. Scan Line 5: ─ */
215 {{{'r', 0, }}, {{0xE2, 0x8E, 0xBC, 0}}}, /* Horiz. Scan Line 7: ⎼ */
216 {{{'s', 0, }}, {{0xE2, 0x8E, 0xBD, 0}}}, /* Horiz. Scan Line 9: ⎽ */
217 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR: ├ */
218 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL: ┤ */
219 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU: ┴ */
220 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD: ┬ */
221 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V: │ */
222 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE: ≤ */
223 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE: ≥ */
224 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI: π */
225 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ: ≠ */
226 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
227 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT: ⋅ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000228 {{{0, }}, {{0, }}}
229};
230
231static void
232apply_char_set(character_set cs, union utf8_char *utf8)
233{
234 int i = 0;
235
236 while (cs[i].match.byte[0]) {
237 if ((*utf8).ch == cs[i].match.ch) {
238 *utf8 = cs[i].replace;
239 break;
240 }
241 i++;
242 }
243}
244
Callum Lowcay7e08e902011-01-07 19:47:02 +0000245struct key_map {
246 int sym;
247 int num;
248 char escape;
249 char code;
250};
251/* Set last key_sub sym to NULL */
252typedef struct key_map *keyboard_mode;
253
254static struct key_map KM_NORMAL[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400255 { XKB_KEY_Left, 1, '[', 'D' },
256 { XKB_KEY_Right, 1, '[', 'C' },
257 { XKB_KEY_Up, 1, '[', 'A' },
258 { XKB_KEY_Down, 1, '[', 'B' },
259 { XKB_KEY_Home, 1, '[', 'H' },
260 { XKB_KEY_End, 1, '[', 'F' },
261 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000262};
263static struct key_map KM_APPLICATION[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400264 { XKB_KEY_Left, 1, 'O', 'D' },
265 { XKB_KEY_Right, 1, 'O', 'C' },
266 { XKB_KEY_Up, 1, 'O', 'A' },
267 { XKB_KEY_Down, 1, 'O', 'B' },
268 { XKB_KEY_Home, 1, 'O', 'H' },
269 { XKB_KEY_End, 1, 'O', 'F' },
270 { XKB_KEY_KP_Enter, 1, 'O', 'M' },
271 { XKB_KEY_KP_Multiply, 1, 'O', 'j' },
272 { XKB_KEY_KP_Add, 1, 'O', 'k' },
273 { XKB_KEY_KP_Separator, 1, 'O', 'l' },
274 { XKB_KEY_KP_Subtract, 1, 'O', 'm' },
275 { XKB_KEY_KP_Divide, 1, 'O', 'o' },
276 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000277};
278
279static int
280function_key_response(char escape, int num, uint32_t modifiers,
281 char code, char *response)
282{
283 int mod_num = 0;
284 int len;
285
Kristian Høgsberg70163132012-05-08 15:55:39 -0400286 if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
287 if (modifiers & MOD_ALT_MASK) mod_num |= 2;
288 if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000289
290 if (mod_num != 0)
291 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
292 num, mod_num + 1, code);
293 else if (code != '~')
294 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
295 escape, code);
296 else
297 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
298 escape, num, code);
299
300 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
301 else return len;
302}
303
304/* returns the number of bytes written into response,
305 * which must have room for MAX_RESPONSE bytes */
306static int
307apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
308{
309 struct key_map map;
310 int len = 0;
311 int i = 0;
312
313 while (mode[i].sym) {
314 map = mode[i++];
315 if (sym == map.sym) {
316 len = function_key_response(map.escape, map.num,
317 modifiers, map.code,
318 response);
319 break;
320 }
321 }
322
323 return len;
324}
325
Callum Lowcay30eeae52011-01-07 19:46:55 +0000326struct terminal_color { double r, g, b, a; };
327struct attr {
328 unsigned char fg, bg;
329 char a; /* attributes format:
330 * 76543210
Callum Lowcay81179db2011-01-10 12:14:01 +1300331 * cilub */
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500332 char s; /* in selection */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000333};
334struct color_scheme {
335 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500336 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000337 struct attr default_attr;
338};
339
340static void
341attr_init(struct attr *data_attr, struct attr attr, int n)
342{
343 int i;
344 for (i = 0; i < n; i++) {
345 data_attr[i] = attr;
346 }
347}
348
Callum Lowcay67a201d2011-01-12 19:23:41 +1300349enum escape_state {
350 escape_state_normal = 0,
351 escape_state_escape,
352 escape_state_dcs,
353 escape_state_csi,
354 escape_state_osc,
355 escape_state_inner_escape,
356 escape_state_ignore,
357 escape_state_special
358};
359
360#define ESC_FLAG_WHAT 0x01
361#define ESC_FLAG_GT 0x02
362#define ESC_FLAG_BANG 0x04
363#define ESC_FLAG_CASH 0x08
364#define ESC_FLAG_SQUOTE 0x10
365#define ESC_FLAG_DQUOTE 0x20
366#define ESC_FLAG_SPACE 0x40
367
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400368enum {
369 SELECT_NONE,
370 SELECT_CHAR,
371 SELECT_WORD,
372 SELECT_LINE
373};
374
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500375struct terminal {
376 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500377 struct widget *widget;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500378 struct display *display;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000379 union utf8_char *data;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400380 struct task io_task;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000381 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000382 struct attr *data_attr;
383 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000384 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000385 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000386 char saved_origin_mode;
387 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000388 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000389 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000390 character_set cs, g0, g1;
391 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000392 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000393 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500394 int width, height, start, row, column;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000395 int saved_row, saved_column;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600396 int send_cursor_position;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500397 int fd, master;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500398 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300399 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500400 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300401 enum escape_state state;
402 enum escape_state outer_state;
403 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000404 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500405 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500406 int fullscreen;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500407 int focused;
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) &&
540 terminal->focused && terminal->row == row &&
541 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
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500698 if (width < 1)
699 width = 1;
700 if (height < 1)
701 height = 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500702 if (terminal->width == width && terminal->height == height)
703 return;
704
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000705 data_pitch = width * sizeof(union utf8_char);
706 size = data_pitch * height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500707 data = malloc(size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000708 attr_pitch = width * sizeof(struct attr);
709 data_attr = malloc(attr_pitch * height);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000710 tab_ruler = malloc(width);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500711 memset(data, 0, size);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000712 memset(tab_ruler, 0, width);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000713 attr_init(data_attr, terminal->curr_attr, width * height);
714 if (terminal->data && terminal->data_attr) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500715 if (width > terminal->width)
716 l = terminal->width;
717 else
718 l = width;
719
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500720 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500721 total_rows = height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500722 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500723 total_rows = terminal->height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500724 }
725
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000726 for (i = 0; i < total_rows; i++) {
727 memcpy(&data[width * i],
728 terminal_get_row(terminal, i),
729 l * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000730 memcpy(&data_attr[width * i],
731 terminal_get_attr_row(terminal, i),
732 l * sizeof(struct attr));
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000733 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500734
735 free(terminal->data);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000736 free(terminal->data_attr);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000737 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500738 }
739
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000740 terminal->data_pitch = data_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000741 terminal->attr_pitch = attr_pitch;
Callum Lowcay86653ed2011-01-07 19:47:03 +0000742 terminal->margin_bottom =
743 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500744 terminal->width = width;
745 terminal->height = height;
746 terminal->data = data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000747 terminal->data_attr = data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000748 terminal->tab_ruler = tab_ruler;
749 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500750
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000751 /* Update the window size */
752 ws.ws_row = terminal->height;
753 ws.ws_col = terminal->width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500754 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500755 ws.ws_xpixel = allocation.width;
756 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000757 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500758}
759
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500760static void
761resize_handler(struct widget *widget,
762 int32_t width, int32_t height, void *data)
763{
764 struct terminal *terminal = data;
765 int32_t columns, rows, m;
766
Alexander Preisingere2b88c02012-06-18 20:59:26 +0200767 if (width < 200)
768 width = 200;
769
770 if (height < 50)
771 height = 50;
772
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500773 m = 2 * terminal->margin;
774 columns = (width - m) / (int32_t) terminal->extents.max_x_advance;
775 rows = (height - m) / (int32_t) terminal->extents.height;
776
777 if (!terminal->fullscreen) {
778 width = columns * terminal->extents.max_x_advance + m;
779 height = rows * terminal->extents.height + m;
780 widget_set_size(terminal->widget, width, height);
781 }
782
783 terminal_resize_cells(terminal, columns, rows);
784}
785
786static void
787terminal_resize(struct terminal *terminal, int columns, int rows)
788{
789 int32_t width, height, m;
790
791 if (terminal->fullscreen)
792 return;
793
794 m = 2 * terminal->margin;
795 width = columns * terminal->extents.max_x_advance + m;
796 height = rows * terminal->extents.height + m;
Kristian Høgsberga1627922012-06-20 17:30:03 -0400797
798 frame_set_child_size(terminal->widget, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500799}
800
Callum Lowcay30eeae52011-01-07 19:46:55 +0000801struct color_scheme DEFAULT_COLORS = {
802 {
803 {0, 0, 0, 1}, /* black */
804 {0.66, 0, 0, 1}, /* red */
805 {0 , 0.66, 0, 1}, /* green */
806 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
807 {0 , 0 , 0.66, 1}, /* blue */
808 {0.66, 0 , 0.66, 1}, /* magenta */
809 {0, 0.66, 0.66, 1}, /* cyan */
810 {0.66, 0.66, 0.66, 1}, /* light grey */
811 {0.22, 0.33, 0.33, 1}, /* dark grey */
812 {1, 0.33, 0.33, 1}, /* high red */
813 {0.33, 1, 0.33, 1}, /* high green */
814 {1, 1, 0.33, 1}, /* high yellow */
815 {0.33, 0.33, 1, 1}, /* high blue */
816 {1, 0.33, 1, 1}, /* high magenta */
817 {0.33, 1, 1, 1}, /* high cyan */
818 {1, 1, 1, 1} /* white */
819 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500820 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000821 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
822};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400823
Kristian Høgsberg22106762008-12-08 13:50:07 -0500824static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500825terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
826{
827 cairo_set_source_rgba(cr,
828 terminal->color_table[index].r,
829 terminal->color_table[index].g,
830 terminal->color_table[index].b,
831 terminal->color_table[index].a);
832}
833
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500834static void
835terminal_send_selection(struct terminal *terminal, int fd)
836{
837 int row, col;
838 union utf8_char *p_row;
839 union decoded_attr attr;
840 FILE *fp;
841 int len;
842
843 fp = fdopen(fd, "w");
844 for (row = 0; row < terminal->height; row++) {
845 p_row = terminal_get_row(terminal, row);
846 for (col = 0; col < terminal->width; col++) {
847 /* get the attributes for this character cell */
848 terminal_decode_attr(terminal, row, col, &attr);
849 if (!attr.attr.s)
850 continue;
851 len = strnlen((char *) p_row[col].byte, 4);
Kristian Høgsberg0dee6472012-07-01 21:25:41 -0400852 if (len > 0)
853 fwrite(p_row[col].byte, 1, len, fp);
854 if (len == 0 || col == terminal->width - 1) {
855 fwrite("\n", 1, 1, fp);
856 break;
857 }
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500858 }
859 }
860 fclose(fp);
861}
862
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500863struct glyph_run {
864 struct terminal *terminal;
865 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400866 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500867 union decoded_attr attr;
868 cairo_glyph_t glyphs[256], *g;
869};
870
871static void
872glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
873{
874 run->terminal = terminal;
875 run->cr = cr;
876 run->g = run->glyphs;
877 run->count = 0;
878 run->attr.key = 0;
879}
880
881static void
882glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
883{
884 cairo_scaled_font_t *font;
885
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500886 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
887 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300888 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500889 font = run->terminal->font_bold;
890 else
891 font = run->terminal->font_normal;
892 cairo_set_scaled_font(run->cr, font);
893 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300894 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500895
Callum Lowcay9d708b02011-01-12 20:06:17 +1300896 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
897 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500898 run->g = run->glyphs;
899 run->count = 0;
900 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300901 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500902}
903
904static void
905glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
906{
907 int num_glyphs;
908 cairo_scaled_font_t *font;
909
910 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
911
Callum Lowcay9d708b02011-01-12 20:06:17 +1300912 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500913 font = run->terminal->font_bold;
914 else
915 font = run->terminal->font_normal;
916
917 cairo_move_to(run->cr, x, y);
918 cairo_scaled_font_text_to_glyphs (font, x, y,
919 (char *) c->byte, 4,
920 &run->g, &num_glyphs,
921 NULL, NULL, NULL);
922 run->g += num_glyphs;
923 run->count += num_glyphs;
924}
925
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500926
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500927static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500928redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500929{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500930 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500931 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500932 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000933 int top_margin, side_margin;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600934 int row, col, cursor_x, cursor_y;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000935 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500936 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000937 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500938 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500939 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500940 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500941 cairo_font_extents_t extents;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500942
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500943 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500944 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500945 cr = cairo_create(surface);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500946 cairo_rectangle(cr, allocation.x, allocation.y,
947 allocation.width, allocation.height);
948 cairo_clip(cr);
949 cairo_push_group(cr);
950
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500951 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500952 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500953 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500954
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500955 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500956
Kristian Høgsberg59826582011-01-20 11:56:57 -0500957 extents = terminal->extents;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500958 side_margin = (allocation.width - terminal->width * extents.max_x_advance) / 2;
959 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500960
Callum Lowcay30eeae52011-01-07 19:46:55 +0000961 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500962 cairo_translate(cr, allocation.x + side_margin,
963 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500964 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000965 for (row = 0; row < terminal->height; row++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000966 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000967 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500968 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000969
Callum Lowcay9d708b02011-01-12 20:06:17 +1300970 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -0500971 continue;
972
Callum Lowcay9d708b02011-01-12 20:06:17 +1300973 terminal_set_color(terminal, cr, attr.attr.bg);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500974 cairo_move_to(cr, col * extents.max_x_advance,
975 row * extents.height);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000976 cairo_rel_line_to(cr, extents.max_x_advance, 0);
977 cairo_rel_line_to(cr, 0, extents.height);
978 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
979 cairo_close_path(cr);
980 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500981 }
982 }
Callum Lowcay30eeae52011-01-07 19:46:55 +0000983
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500984 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
985
986 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500987 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500988 for (row = 0; row < terminal->height; row++) {
989 p_row = terminal_get_row(terminal, row);
990 for (col = 0; col < terminal->width; col++) {
991 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500992 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500993
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500994 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000995
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500996 text_x = col * extents.max_x_advance;
997 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +1300998 if (attr.attr.a & ATTRMASK_UNDERLINE) {
999 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +00001000 cairo_move_to(cr, text_x, (double)text_y + 1.5);
1001 cairo_line_to(cr, text_x + extents.max_x_advance, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001002 cairo_stroke(cr);
1003 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -05001004
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001005 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001006 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001007 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001008
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001009 attr.key = ~0;
1010 glyph_run_flush(&run, attr);
1011
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001012 if ((terminal->mode & MODE_SHOW_CURSOR) && !terminal->focused) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001013 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001014
Callum Lowcay30eeae52011-01-07 19:46:55 +00001015 cairo_set_line_width(cr, 1);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001016 cairo_move_to(cr, terminal->column * extents.max_x_advance + d,
1017 terminal->row * extents.height + d);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001018 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
1019 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
1020 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
1021 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001022
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001023 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001024 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001025
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001026 cairo_pop_group_to_source(cr);
1027 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001028 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001029 cairo_surface_destroy(surface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001030
1031 if (terminal->send_cursor_position) {
1032 cursor_x = side_margin + allocation.x +
1033 terminal->column * extents.max_x_advance;
1034 cursor_y = top_margin + allocation.y +
1035 terminal->row * extents.height;
1036 window_set_text_cursor_position(terminal->window,
1037 cursor_x, cursor_y);
1038 terminal->send_cursor_position = 0;
1039 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001040}
1041
1042static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001043terminal_write(struct terminal *terminal, const char *data, size_t length)
1044{
1045 if (write(terminal->master, data, length) < 0)
1046 abort();
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001047 terminal->send_cursor_position = 1;
Kristian Høgsberg26130862011-08-24 11:30:21 -04001048}
1049
1050static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001051terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001052
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001053static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001054handle_char(struct terminal *terminal, union utf8_char utf8);
1055
1056static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001057handle_sgr(struct terminal *terminal, int code);
1058
1059static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001060handle_term_parameter(struct terminal *terminal, int code, int sr)
1061{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001062 int i;
1063
Callum Lowcay67a201d2011-01-12 19:23:41 +13001064 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001065 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001066 case 1: /* DECCKM */
1067 if (sr) terminal->key_mode = KM_APPLICATION;
1068 else terminal->key_mode = KM_NORMAL;
1069 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001070 case 2: /* DECANM */
1071 /* No VT52 support yet */
1072 terminal->g0 = CS_US;
1073 terminal->g1 = CS_US;
1074 terminal->cs = terminal->g0;
1075 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001076 case 3: /* DECCOLM */
1077 if (sr)
1078 terminal_resize(terminal, 132, 24);
1079 else
1080 terminal_resize(terminal, 80, 24);
1081
1082 /* set columns, but also home cursor and clear screen */
1083 terminal->row = 0; terminal->column = 0;
1084 for (i = 0; i < terminal->height; i++) {
1085 memset(terminal_get_row(terminal, i),
1086 0, terminal->data_pitch);
1087 attr_init(terminal_get_attr_row(terminal, i),
1088 terminal->curr_attr, terminal->width);
1089 }
1090 break;
1091 case 5: /* DECSCNM */
1092 if (sr) terminal->mode |= MODE_INVERSE;
1093 else terminal->mode &= ~MODE_INVERSE;
1094 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001095 case 6: /* DECOM */
1096 terminal->origin_mode = sr;
1097 if (terminal->origin_mode)
1098 terminal->row = terminal->margin_top;
1099 else
1100 terminal->row = 0;
1101 terminal->column = 0;
1102 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001103 case 7: /* DECAWM */
1104 if (sr) terminal->mode |= MODE_AUTOWRAP;
1105 else terminal->mode &= ~MODE_AUTOWRAP;
1106 break;
1107 case 8: /* DECARM */
1108 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1109 else terminal->mode &= ~MODE_AUTOREPEAT;
1110 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001111 case 12: /* Very visible cursor (CVVIS) */
1112 /* FIXME: What do we do here. */
1113 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001114 case 25:
1115 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1116 else terminal->mode &= ~MODE_SHOW_CURSOR;
1117 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001118 case 1034: /* smm/rmm, meta mode on/off */
1119 /* ignore */
1120 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001121 case 1037: /* deleteSendsDel */
1122 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1123 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1124 break;
1125 case 1039: /* altSendsEscape */
1126 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1127 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1128 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001129 case 1049: /* rmcup/smcup, alternate screen */
1130 /* Ignore. Should be possible to implement,
1131 * but it's kind of annoying. */
1132 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001133 default:
1134 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1135 break;
1136 }
1137 } else {
1138 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001139 case 4: /* IRM */
1140 if (sr) terminal->mode |= MODE_IRM;
1141 else terminal->mode &= ~MODE_IRM;
1142 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001143 case 20: /* LNM */
1144 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1145 else terminal->mode &= ~MODE_LF_NEWLINE;
1146 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001147 default:
1148 fprintf(stderr, "Unknown parameter: %d\n", code);
1149 break;
1150 }
1151 }
1152}
1153
1154static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001155handle_dcs(struct terminal *terminal)
1156{
1157}
1158
1159static void
1160handle_osc(struct terminal *terminal)
1161{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001162 char *p;
1163 int code;
1164
1165 terminal->escape[terminal->escape_length++] = '\0';
1166 p = &terminal->escape[2];
1167 code = strtol(p, &p, 10);
1168 if (*p == ';') p++;
1169
1170 switch (code) {
1171 case 0: /* Icon name and window title */
1172 case 1: /* Icon label */
1173 case 2: /* Window title*/
1174 window_set_title(terminal->window, p);
1175 break;
1176 default:
1177 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1178 break;
1179 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001180}
1181
1182static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001183handle_escape(struct terminal *terminal)
1184{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001185 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001186 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001187 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001188 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001189 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001190 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001191 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001192
1193 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001194 i = 0;
1195 p = &terminal->escape[2];
1196 while ((isdigit(*p) || *p == ';') && i < 10) {
1197 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001198 if (!set[i]) {
1199 args[i] = 0;
1200 set[i] = 1;
1201 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001202 p++;
1203 i++;
1204 } else {
1205 args[i] = strtol(p, &p, 10);
1206 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001207 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001208 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001209
1210 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001211 case '@': /* ICH */
1212 count = set[0] ? args[0] : 1;
1213 if (count == 0) count = 1;
1214 terminal_shift_line(terminal, count);
1215 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001216 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001217 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001218 if (count == 0) count = 1;
1219 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001220 terminal->row -= count;
1221 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001222 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001223 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001224 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001225 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001226 if (count == 0) count = 1;
1227 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001228 terminal->row += count;
1229 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001230 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001231 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001232 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001233 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001234 if (count == 0) count = 1;
1235 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001236 terminal->column += count;
1237 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001238 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001239 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001240 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001241 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001242 if (count == 0) count = 1;
1243 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001244 terminal->column -= count;
1245 else
1246 terminal->column = 0;
1247 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001248 case 'E': /* CNL */
1249 count = set[0] ? args[0] : 1;
1250 if (terminal->row + count <= terminal->margin_bottom)
1251 terminal->row += count;
1252 else
1253 terminal->row = terminal->margin_bottom;
1254 terminal->column = 0;
1255 break;
1256 case 'F': /* CPL */
1257 count = set[0] ? args[0] : 1;
1258 if (terminal->row - count >= terminal->margin_top)
1259 terminal->row -= count;
1260 else
1261 terminal->row = terminal->margin_top;
1262 terminal->column = 0;
1263 break;
1264 case 'G': /* CHA */
1265 y = set[0] ? args[0] : 1;
1266 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1267
1268 terminal->column = y - 1;
1269 break;
1270 case 'f': /* HVP */
1271 case 'H': /* CUP */
1272 x = (set[1] ? args[1] : 1) - 1;
1273 x = x < 0 ? 0 :
1274 (x >= terminal->width ? terminal->width - 1 : x);
1275
1276 y = (set[0] ? args[0] : 1) - 1;
1277 if (terminal->origin_mode) {
1278 y += terminal->margin_top;
1279 y = y < terminal->margin_top ? terminal->margin_top :
1280 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1281 } else {
1282 y = y < 0 ? 0 :
1283 (y >= terminal->height ? terminal->height - 1 : y);
1284 }
1285
1286 terminal->row = y;
1287 terminal->column = x;
1288 break;
1289 case 'I': /* CHT */
1290 count = set[0] ? args[0] : 1;
1291 if (count == 0) count = 1;
1292 while (count > 0 && terminal->column < terminal->width) {
1293 if (terminal->tab_ruler[terminal->column]) count--;
1294 terminal->column++;
1295 }
1296 terminal->column--;
1297 break;
1298 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001299 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001300 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001301 if (!set[0] || args[0] == 0 || args[0] > 2) {
1302 memset(&row[terminal->column],
1303 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1304 attr_init(&attr_row[terminal->column],
1305 terminal->curr_attr, terminal->width - terminal->column);
1306 for (i = terminal->row + 1; i < terminal->height; 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] == 1) {
1313 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1314 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1315 for (i = 0; i < terminal->row; i++) {
1316 memset(terminal_get_row(terminal, i),
1317 0, terminal->data_pitch);
1318 attr_init(terminal_get_attr_row(terminal, i),
1319 terminal->curr_attr, terminal->width);
1320 }
1321 } else if (args[0] == 2) {
1322 for (i = 0; i < terminal->height; i++) {
1323 memset(terminal_get_row(terminal, i),
1324 0, terminal->data_pitch);
1325 attr_init(terminal_get_attr_row(terminal, i),
1326 terminal->curr_attr, terminal->width);
1327 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001328 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001329 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001330 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001331 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001332 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001333 if (!set[0] || args[0] == 0 || args[0] > 2) {
1334 memset(&row[terminal->column], 0,
1335 (terminal->width - terminal->column) * sizeof(union utf8_char));
1336 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1337 terminal->width - terminal->column);
1338 } else if (args[0] == 1) {
1339 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1340 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1341 } else if (args[0] == 2) {
1342 memset(row, 0, terminal->data_pitch);
1343 attr_init(attr_row, terminal->curr_attr, terminal->width);
1344 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001345 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001346 case 'L': /* IL */
1347 count = set[0] ? args[0] : 1;
1348 if (count == 0) count = 1;
1349 if (terminal->row >= terminal->margin_top &&
1350 terminal->row < terminal->margin_bottom)
1351 {
1352 top = terminal->margin_top;
1353 terminal->margin_top = terminal->row;
1354 terminal_scroll(terminal, 0 - count);
1355 terminal->margin_top = top;
1356 } else if (terminal->row == terminal->margin_bottom) {
1357 memset(terminal_get_row(terminal, terminal->row),
1358 0, terminal->data_pitch);
1359 attr_init(terminal_get_attr_row(terminal, terminal->row),
1360 terminal->curr_attr, terminal->width);
1361 }
1362 break;
1363 case 'M': /* DL */
1364 count = set[0] ? args[0] : 1;
1365 if (count == 0) count = 1;
1366 if (terminal->row >= terminal->margin_top &&
1367 terminal->row < terminal->margin_bottom)
1368 {
1369 top = terminal->margin_top;
1370 terminal->margin_top = terminal->row;
1371 terminal_scroll(terminal, count);
1372 terminal->margin_top = top;
1373 } else if (terminal->row == terminal->margin_bottom) {
1374 memset(terminal_get_row(terminal, terminal->row),
1375 0, terminal->data_pitch);
1376 }
1377 break;
1378 case 'P': /* DCH */
1379 count = set[0] ? args[0] : 1;
1380 if (count == 0) count = 1;
1381 terminal_shift_line(terminal, 0 - count);
1382 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001383 case 'S': /* SU */
1384 terminal_scroll(terminal, set[0] ? args[0] : 1);
1385 break;
1386 case 'T': /* SD */
1387 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1388 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001389 case 'X': /* ECH */
1390 count = set[0] ? args[0] : 1;
1391 if (count == 0) count = 1;
1392 if ((terminal->column + count) > terminal->width)
1393 count = terminal->width - terminal->column;
1394 row = terminal_get_row(terminal, terminal->row);
1395 attr_row = terminal_get_attr_row(terminal, terminal->row);
1396 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1397 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1398 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001399 case 'Z': /* CBT */
1400 count = set[0] ? args[0] : 1;
1401 if (count == 0) count = 1;
1402 while (count > 0 && terminal->column >= 0) {
1403 if (terminal->tab_ruler[terminal->column]) count--;
1404 terminal->column--;
1405 }
1406 terminal->column++;
1407 break;
1408 case '`': /* HPA */
1409 y = set[0] ? args[0] : 1;
1410 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1411
1412 terminal->column = y - 1;
1413 break;
1414 case 'b': /* REP */
1415 count = set[0] ? args[0] : 1;
1416 if (count == 0) count = 1;
1417 if (terminal->last_char.byte[0])
1418 for (i = 0; i < count; i++)
1419 handle_char(terminal, terminal->last_char);
1420 terminal->last_char.byte[0] = 0;
1421 break;
1422 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001423 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001424 break;
1425 case 'd': /* VPA */
1426 x = set[0] ? args[0] : 1;
1427 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1428
1429 terminal->row = x - 1;
1430 break;
1431 case 'g': /* TBC */
1432 if (!set[0] || args[0] == 0) {
1433 terminal->tab_ruler[terminal->column] = 0;
1434 } else if (args[0] == 3) {
1435 memset(terminal->tab_ruler, 0, terminal->width);
1436 }
1437 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001438 case 'h': /* SM */
1439 for(i = 0; i < 10 && set[i]; i++) {
1440 handle_term_parameter(terminal, args[i], 1);
1441 }
1442 break;
1443 case 'l': /* RM */
1444 for(i = 0; i < 10 && set[i]; i++) {
1445 handle_term_parameter(terminal, args[i], 0);
1446 }
1447 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001448 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001449 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001450 if (i <= 7 && set[i] && set[i + 1] &&
1451 set[i + 2] && args[i + 1] == 5)
1452 {
1453 if (args[i] == 38) {
1454 handle_sgr(terminal, args[i + 2] + 256);
1455 break;
1456 } else if (args[i] == 48) {
1457 handle_sgr(terminal, args[i + 2] + 512);
1458 break;
1459 }
1460 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001461 if(set[i]) {
1462 handle_sgr(terminal, args[i]);
1463 } else if(i == 0) {
1464 handle_sgr(terminal, 0);
1465 break;
1466 } else {
1467 break;
1468 }
1469 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001470 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001471 case 'n': /* DSR */
1472 i = set[0] ? args[0] : 0;
1473 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001474 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001475 } else if (i == 6) {
1476 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1477 terminal->origin_mode ?
1478 terminal->row+terminal->margin_top : terminal->row+1,
1479 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001480 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001481 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001482 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001483 case 'r':
1484 if(!set[0]) {
1485 terminal->margin_top = 0;
1486 terminal->margin_bottom = terminal->height-1;
1487 terminal->row = 0;
1488 terminal->column = 0;
1489 } else {
1490 top = (set[0] ? args[0] : 1) - 1;
1491 top = top < 0 ? 0 :
1492 (top >= terminal->height ? terminal->height - 1 : top);
1493 bottom = (set[1] ? args[1] : 1) - 1;
1494 bottom = bottom < 0 ? 0 :
1495 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1496 if(bottom > top) {
1497 terminal->margin_top = top;
1498 terminal->margin_bottom = bottom;
1499 } else {
1500 terminal->margin_top = 0;
1501 terminal->margin_bottom = terminal->height-1;
1502 }
1503 if(terminal->origin_mode)
1504 terminal->row = terminal->margin_top;
1505 else
1506 terminal->row = 0;
1507 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001508 }
1509 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001510 case 's':
1511 terminal->saved_row = terminal->row;
1512 terminal->saved_column = terminal->column;
1513 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001514 case 't': /* windowOps */
1515 if (!set[0]) break;
1516 switch (args[0]) {
1517 case 4: /* resize px */
1518 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001519 widget_schedule_resize(terminal->widget,
1520 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001521 }
1522 break;
1523 case 8: /* resize ch */
1524 if (set[1] && set[2]) {
1525 terminal_resize(terminal, args[2], args[1]);
1526 }
1527 break;
1528 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001529 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001530 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1531 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001532 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001533 break;
1534 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001535 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001536 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1537 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001538 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001539 break;
1540 case 18: /* report ch */
1541 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1542 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001543 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001544 break;
1545 case 21: /* report title */
1546 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1547 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001548 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001549 break;
1550 default:
1551 if (args[0] >= 24)
1552 terminal_resize(terminal, terminal->width, args[0]);
1553 else
1554 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1555 break;
1556 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001557 case 'u':
1558 terminal->row = terminal->saved_row;
1559 terminal->column = terminal->saved_column;
1560 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001561 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001562 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001563 break;
1564 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001565}
1566
1567static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001568handle_non_csi_escape(struct terminal *terminal, char code)
1569{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001570 switch(code) {
1571 case 'M': /* RI */
1572 terminal->row -= 1;
1573 if(terminal->row < terminal->margin_top) {
1574 terminal->row = terminal->margin_top;
1575 terminal_scroll(terminal, -1);
1576 }
1577 break;
1578 case 'E': /* NEL */
1579 terminal->column = 0;
1580 // fallthrough
1581 case 'D': /* IND */
1582 terminal->row += 1;
1583 if(terminal->row > terminal->margin_bottom) {
1584 terminal->row = terminal->margin_bottom;
1585 terminal_scroll(terminal, +1);
1586 }
1587 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001588 case 'c': /* RIS */
1589 terminal_init(terminal);
1590 break;
1591 case 'H': /* HTS */
1592 terminal->tab_ruler[terminal->column] = 1;
1593 break;
1594 case '7': /* DECSC */
1595 terminal->saved_row = terminal->row;
1596 terminal->saved_column = terminal->column;
1597 terminal->saved_attr = terminal->curr_attr;
1598 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001599 terminal->saved_cs = terminal->cs;
1600 terminal->saved_g0 = terminal->g0;
1601 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001602 break;
1603 case '8': /* DECRC */
1604 terminal->row = terminal->saved_row;
1605 terminal->column = terminal->saved_column;
1606 terminal->curr_attr = terminal->saved_attr;
1607 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001608 terminal->cs = terminal->saved_cs;
1609 terminal->g0 = terminal->saved_g0;
1610 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001611 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001612 case '=': /* DECPAM */
1613 terminal->key_mode = KM_APPLICATION;
1614 break;
1615 case '>': /* DECPNM */
1616 terminal->key_mode = KM_NORMAL;
1617 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001618 default:
1619 fprintf(stderr, "Unknown escape code: %c\n", code);
1620 break;
1621 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001622}
1623
1624static void
1625handle_special_escape(struct terminal *terminal, char special, char code)
1626{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001627 int i, numChars;
1628
1629 if (special == '#') {
1630 switch(code) {
1631 case '8':
1632 /* fill with 'E', no cheap way to do this */
1633 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1634 numChars = terminal->width * terminal->height;
1635 for(i = 0; i < numChars; i++) {
1636 terminal->data[i].byte[0] = 'E';
1637 }
1638 break;
1639 default:
1640 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1641 break;
1642 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001643 } else if (special == '(' || special == ')') {
1644 switch(code) {
1645 case '0':
1646 if (special == '(')
1647 terminal->g0 = CS_SPECIAL;
1648 else
1649 terminal->g1 = CS_SPECIAL;
1650 break;
1651 case 'A':
1652 if (special == '(')
1653 terminal->g0 = CS_UK;
1654 else
1655 terminal->g1 = CS_UK;
1656 break;
1657 case 'B':
1658 if (special == '(')
1659 terminal->g0 = CS_US;
1660 else
1661 terminal->g1 = CS_US;
1662 break;
1663 default:
1664 fprintf(stderr, "Unknown character set %c\n", code);
1665 break;
1666 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001667 } else {
1668 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1669 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001670}
1671
1672static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001673handle_sgr(struct terminal *terminal, int code)
1674{
1675 switch(code) {
1676 case 0:
1677 terminal->curr_attr = terminal->color_scheme->default_attr;
1678 break;
1679 case 1:
1680 terminal->curr_attr.a |= ATTRMASK_BOLD;
1681 if (terminal->curr_attr.fg < 8)
1682 terminal->curr_attr.fg += 8;
1683 break;
1684 case 4:
1685 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1686 break;
1687 case 5:
1688 terminal->curr_attr.a |= ATTRMASK_BLINK;
1689 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001690 case 8:
1691 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1692 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001693 case 2:
1694 case 21:
1695 case 22:
1696 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1697 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1698 terminal->curr_attr.fg -= 8;
1699 break;
1700 case 24:
1701 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1702 break;
1703 case 25:
1704 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1705 break;
1706 case 7:
1707 case 26:
1708 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1709 break;
1710 case 27:
1711 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1712 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001713 case 28:
1714 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1715 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001716 case 39:
1717 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1718 break;
1719 case 49:
1720 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1721 break;
1722 default:
1723 if(code >= 30 && code <= 37) {
1724 terminal->curr_attr.fg = code - 30;
1725 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1726 terminal->curr_attr.fg += 8;
1727 } else if(code >= 40 && code <= 47) {
1728 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001729 } else if (code >= 90 && code <= 97) {
1730 terminal->curr_attr.fg = code - 90 + 8;
1731 } else if (code >= 100 && code <= 107) {
1732 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001733 } else if(code >= 256 && code < 512) {
1734 terminal->curr_attr.fg = code - 256;
1735 } else if(code >= 512 && code < 768) {
1736 terminal->curr_attr.bg = code - 512;
1737 } else {
1738 fprintf(stderr, "Unknown SGR code: %d\n", code);
1739 }
1740 break;
1741 }
1742}
1743
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001744/* Returns 1 if c was special, otherwise 0 */
1745static int
1746handle_special_char(struct terminal *terminal, char c)
1747{
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001748 union utf8_char *row;
1749 struct attr *attr_row;
1750
1751 row = terminal_get_row(terminal, terminal->row);
1752 attr_row = terminal_get_attr_row(terminal, terminal->row);
1753
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001754 switch(c) {
1755 case '\r':
1756 terminal->column = 0;
1757 break;
1758 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001759 if (terminal->mode & MODE_LF_NEWLINE) {
1760 terminal->column = 0;
1761 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001762 /* fallthrough */
1763 case '\v':
1764 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001765 terminal->row++;
1766 if(terminal->row > terminal->margin_bottom) {
1767 terminal->row = terminal->margin_bottom;
1768 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001769 }
1770
1771 break;
1772 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001773 while (terminal->column < terminal->width) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001774 if (terminal->mode & MODE_IRM)
1775 terminal_shift_line(terminal, +1);
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001776
1777 if (row[terminal->column].byte[0] == '\0') {
1778 row[terminal->column].byte[0] = ' ';
1779 row[terminal->column].byte[1] = '\0';
1780 attr_row[terminal->column] = terminal->curr_attr;
1781 }
1782
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001783 terminal->column++;
Kristian Høgsbergcca3c2f2012-06-20 15:56:13 -04001784 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001785 }
1786 if (terminal->column >= terminal->width) {
1787 terminal->column = terminal->width - 1;
1788 }
1789
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001790 break;
1791 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001792 if (terminal->column >= terminal->width) {
1793 terminal->column = terminal->width - 2;
1794 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001795 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001796 } else if (terminal->mode & MODE_AUTOWRAP) {
1797 terminal->column = terminal->width - 1;
1798 terminal->row -= 1;
1799 if (terminal->row < terminal->margin_top) {
1800 terminal->row = terminal->margin_top;
1801 terminal_scroll(terminal, -1);
1802 }
1803 }
1804
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001805 break;
1806 case '\a':
1807 /* Bell */
1808 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001809 case '\x0E': /* SO */
1810 terminal->cs = terminal->g1;
1811 break;
1812 case '\x0F': /* SI */
1813 terminal->cs = terminal->g0;
1814 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001815 default:
1816 return 0;
1817 }
1818
1819 return 1;
1820}
1821
1822static void
1823handle_char(struct terminal *terminal, union utf8_char utf8)
1824{
1825 union utf8_char *row;
1826 struct attr *attr_row;
1827
1828 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001829
1830 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001831
1832 /* There are a whole lot of non-characters, control codes,
1833 * and formatting codes that should probably be ignored,
1834 * for example: */
1835 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1836 /* BOM, ignore */
1837 return;
1838 }
1839
1840 /* Some of these non-characters should be translated, e.g.: */
1841 if (utf8.byte[0] < 32) {
1842 utf8.byte[0] = utf8.byte[0] + 64;
1843 }
1844
1845 /* handle right margin effects */
1846 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001847 if (terminal->mode & MODE_AUTOWRAP) {
1848 terminal->column = 0;
1849 terminal->row += 1;
1850 if (terminal->row > terminal->margin_bottom) {
1851 terminal->row = terminal->margin_bottom;
1852 terminal_scroll(terminal, +1);
1853 }
1854 } else {
1855 terminal->column--;
1856 }
1857 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001858
1859 row = terminal_get_row(terminal, terminal->row);
1860 attr_row = terminal_get_attr_row(terminal, terminal->row);
1861
Callum Lowcay69e96582011-01-07 19:47:00 +00001862 if (terminal->mode & MODE_IRM)
1863 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001864 row[terminal->column] = utf8;
1865 attr_row[terminal->column++] = terminal->curr_attr;
1866
1867 if (utf8.ch != terminal->last_char.ch)
1868 terminal->last_char = utf8;
1869}
1870
Callum Lowcay30eeae52011-01-07 19:46:55 +00001871static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001872escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1873{
1874 int len, i;
1875
1876 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1877 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1878 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1879 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1880 else len = 1; /* Invalid, cannot happen */
1881
1882 if (terminal->escape_length + len <= MAX_ESCAPE) {
1883 for (i = 0; i < len; i++)
1884 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1885 terminal->escape_length += len;
1886 } else if (terminal->escape_length < MAX_ESCAPE) {
1887 terminal->escape[terminal->escape_length++] = 0;
1888 }
1889}
1890
1891static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001892terminal_data(struct terminal *terminal, const char *data, size_t length)
1893{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001894 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001895 union utf8_char utf8;
1896 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001897
1898 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001899 parser_state =
1900 utf8_next_char(&terminal->state_machine, data[i]);
1901 switch(parser_state) {
1902 case utf8state_accept:
1903 utf8.ch = terminal->state_machine.s.ch;
1904 break;
1905 case utf8state_reject:
1906 /* the unicode replacement character */
1907 utf8.byte[0] = 0xEF;
1908 utf8.byte[1] = 0xBF;
1909 utf8.byte[2] = 0xBD;
1910 utf8.byte[3] = 0x00;
1911 break;
1912 default:
1913 continue;
1914 }
1915
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001916 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001917 switch (terminal->state) {
1918 case escape_state_escape:
1919 escape_append_utf8(terminal, utf8);
1920 switch (utf8.byte[0]) {
1921 case 'P': /* DCS */
1922 terminal->state = escape_state_dcs;
1923 break;
1924 case '[': /* CSI */
1925 terminal->state = escape_state_csi;
1926 break;
1927 case ']': /* OSC */
1928 terminal->state = escape_state_osc;
1929 break;
1930 case '#':
1931 case '(':
1932 case ')': /* special */
1933 terminal->state = escape_state_special;
1934 break;
1935 case '^': /* PM (not implemented) */
1936 case '_': /* APC (not implemented) */
1937 terminal->state = escape_state_ignore;
1938 break;
1939 default:
1940 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001941 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13001942 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001943 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001944 continue;
1945 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001946 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1947 /* do nothing */
1948 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001949 terminal->escape_flags |= ESC_FLAG_WHAT;
1950 } else if (utf8.byte[0] == '>') {
1951 terminal->escape_flags |= ESC_FLAG_GT;
1952 } else if (utf8.byte[0] == '!') {
1953 terminal->escape_flags |= ESC_FLAG_BANG;
1954 } else if (utf8.byte[0] == '$') {
1955 terminal->escape_flags |= ESC_FLAG_CASH;
1956 } else if (utf8.byte[0] == '\'') {
1957 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1958 } else if (utf8.byte[0] == '"') {
1959 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1960 } else if (utf8.byte[0] == ' ') {
1961 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001962 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001963 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001964 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13001965 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001966 }
1967
1968 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1969 utf8.byte[0] == '`')
1970 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001971 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001972 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001973 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001974 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001975 continue;
1976 case escape_state_inner_escape:
1977 if (utf8.byte[0] == '\\') {
1978 terminal->state = escape_state_normal;
1979 if (terminal->outer_state == escape_state_dcs) {
1980 handle_dcs(terminal);
1981 } else if (terminal->outer_state == escape_state_osc) {
1982 handle_osc(terminal);
1983 }
1984 } else if (utf8.byte[0] == '\e') {
1985 terminal->state = terminal->outer_state;
1986 escape_append_utf8(terminal, utf8);
1987 if (terminal->escape_length >= MAX_ESCAPE)
1988 terminal->state = escape_state_normal;
1989 } else {
1990 terminal->state = terminal->outer_state;
1991 if (terminal->escape_length < MAX_ESCAPE)
1992 terminal->escape[terminal->escape_length++] = '\e';
1993 escape_append_utf8(terminal, utf8);
1994 if (terminal->escape_length >= MAX_ESCAPE)
1995 terminal->state = escape_state_normal;
1996 }
1997 continue;
1998 case escape_state_dcs:
1999 case escape_state_osc:
2000 case escape_state_ignore:
2001 if (utf8.byte[0] == '\e') {
2002 terminal->outer_state = terminal->state;
2003 terminal->state = escape_state_inner_escape;
2004 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
2005 terminal->state = escape_state_normal;
2006 handle_osc(terminal);
2007 } else {
2008 escape_append_utf8(terminal, utf8);
2009 if (terminal->escape_length >= MAX_ESCAPE)
2010 terminal->state = escape_state_normal;
2011 }
2012 continue;
2013 case escape_state_special:
2014 escape_append_utf8(terminal, utf8);
2015 terminal->state = escape_state_normal;
2016 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2017 handle_special_escape(terminal, terminal->escape[1],
2018 utf8.byte[0]);
2019 }
2020 continue;
2021 default:
2022 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002023 }
2024
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002025 /* this is valid, because ASCII characters are never used to
2026 * introduce a multibyte sequence in UTF-8 */
2027 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002028 terminal->state = escape_state_escape;
2029 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002030 terminal->escape[0] = '\e';
2031 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002032 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002033 } else {
2034 handle_char(terminal, utf8);
2035 } /* if */
2036 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002037
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002038 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002039}
2040
2041static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002042data_source_target(void *data,
2043 struct wl_data_source *source, const char *mime_type)
2044{
2045 fprintf(stderr, "data_source_target, %s\n", mime_type);
2046}
2047
2048static void
2049data_source_send(void *data,
2050 struct wl_data_source *source,
2051 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002052{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002053 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002054
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002055 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002056}
2057
2058static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002059data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002060{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002061 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002062}
2063
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002064static const struct wl_data_source_listener data_source_listener = {
2065 data_source_target,
2066 data_source_send,
2067 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002068};
2069
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002070static int
2071handle_bound_key(struct terminal *terminal,
2072 struct input *input, uint32_t sym, uint32_t time)
2073{
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002074 struct terminal *new_terminal;
2075
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002076 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002077 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002078 /* Cut selection; terminal doesn't do cut, fall
2079 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002080 case XKB_KEY_C:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002081 terminal->selection =
2082 display_create_data_source(terminal->display);
2083 wl_data_source_offer(terminal->selection,
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002084 "text/plain;charset=utf-8");
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002085 wl_data_source_add_listener(terminal->selection,
2086 &data_source_listener, terminal);
Kristian Høgsbergfee91be2012-06-02 21:21:41 -04002087 input_set_selection(input, terminal->selection,
2088 display_get_serial(terminal->display));
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002089 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002090 case XKB_KEY_V:
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002091 input_receive_selection_data_to_fd(input,
2092 "text/plain;charset=utf-8",
2093 terminal->master);
2094
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002095 return 1;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002096
2097 case XKB_KEY_N:
2098 new_terminal =
2099 terminal_create(terminal->display, option_fullscreen);
2100 if (terminal_run(new_terminal, option_shell))
2101 terminal_destroy(new_terminal);
2102
2103 return 1;
2104
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002105 default:
2106 return 0;
2107 }
2108}
2109
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002110static void
2111key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002112 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2113 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002114{
2115 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002116 char ch[MAX_RESPONSE];
Kristian Høgsberg88fd4082012-06-21 15:55:03 -04002117 uint32_t modifiers, serial;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002118 int len = 0;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002119
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002120 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002121 if ((modifiers & MOD_CONTROL_MASK) &&
2122 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002123 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2124 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002125 return;
2126
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002127 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002128 case XKB_KEY_F11:
Daniel Stonec9785ea2012-05-30 16:31:52 +01002129 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002130 break;
2131 terminal->fullscreen ^= 1;
2132 window_set_fullscreen(window, terminal->fullscreen);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002133 break;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002134
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002135 case XKB_KEY_BackSpace:
Kristian Høgsbergb7f94bf2012-06-21 12:29:36 -04002136 if (modifiers & MOD_ALT_MASK)
2137 ch[len++] = 0x1b;
Kristian Høgsberg71a4cf42012-06-20 17:57:56 -04002138 ch[len++] = 0x7f;
2139 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002140 case XKB_KEY_Tab:
2141 case XKB_KEY_Linefeed:
2142 case XKB_KEY_Clear:
2143 case XKB_KEY_Pause:
2144 case XKB_KEY_Scroll_Lock:
2145 case XKB_KEY_Sys_Req:
2146 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002147 ch[len++] = sym & 0x7f;
2148 break;
2149
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002150 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002151 if (terminal->mode & MODE_LF_NEWLINE) {
2152 ch[len++] = 0x0D;
2153 ch[len++] = 0x0A;
2154 } else {
2155 ch[len++] = 0x0D;
2156 }
2157 break;
2158
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002159 case XKB_KEY_Shift_L:
2160 case XKB_KEY_Shift_R:
2161 case XKB_KEY_Control_L:
2162 case XKB_KEY_Control_R:
2163 case XKB_KEY_Alt_L:
2164 case XKB_KEY_Alt_R:
Kristian Høgsberg22fbcf72012-06-22 12:18:56 -04002165 case XKB_KEY_Meta_L:
2166 case XKB_KEY_Meta_R:
2167 case XKB_KEY_Super_L:
2168 case XKB_KEY_Super_R:
2169 case XKB_KEY_Hyper_L:
2170 case XKB_KEY_Hyper_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002171 break;
2172
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002173 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002174 len = function_key_response('[', 2, modifiers, '~', ch);
2175 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002176 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002177 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2178 ch[len++] = '\x04';
2179 } else {
2180 len = function_key_response('[', 3, modifiers, '~', ch);
2181 }
2182 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002183 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002184 len = function_key_response('[', 5, modifiers, '~', ch);
2185 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002186 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002187 len = function_key_response('[', 6, modifiers, '~', ch);
2188 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002189 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002190 len = function_key_response('O', 1, modifiers, 'P', ch);
2191 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002192 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002193 len = function_key_response('O', 1, modifiers, 'Q', ch);
2194 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002195 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002196 len = function_key_response('O', 1, modifiers, 'R', ch);
2197 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002198 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002199 len = function_key_response('O', 1, modifiers, 'S', ch);
2200 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002201 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002202 len = function_key_response('[', 15, modifiers, '~', ch);
2203 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002204 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002205 len = function_key_response('[', 17, modifiers, '~', ch);
2206 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002207 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002208 len = function_key_response('[', 18, modifiers, '~', ch);
2209 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002210 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002211 len = function_key_response('[', 19, modifiers, '~', ch);
2212 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002213 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002214 len = function_key_response('[', 20, modifiers, '~', ch);
2215 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002216 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002217 len = function_key_response('[', 21, modifiers, '~', ch);
2218 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002219 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002220 len = function_key_response('[', 24, modifiers, '~', ch);
2221 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002222 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002223 /* Handle special keys with alternate mappings */
2224 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2225 if (len != 0) break;
2226
Kristian Høgsberg70163132012-05-08 15:55:39 -04002227 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002228 if (sym >= '3' && sym <= '7')
2229 sym = (sym & 0x1f) + 8;
2230
2231 if (!((sym >= '!' && sym <= '/') ||
2232 (sym >= '8' && sym <= '?') ||
2233 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2234 else if (sym == '2') sym = 0x00;
2235 else if (sym == '/') sym = 0x1F;
2236 else if (sym == '8' || sym == '?') sym = 0x7F;
Kristian Høgsbergae9e0732012-06-21 12:30:15 -04002237 }
2238 if ((terminal->mode & MODE_ALT_SENDS_ESC) &&
2239 (modifiers & MOD_ALT_MASK)) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002240 ch[len++] = 0x1b;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002241 } else if (modifiers & MOD_ALT_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002242 sym = sym | 0x80;
2243 }
2244
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002245 if (sym < 256)
2246 ch[len++] = sym;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002247 break;
2248 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002249
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002250 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04002251 terminal_write(terminal, ch, len);
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002252
2253 /* Hide cursor, except if this was coming from a
2254 * repeating key press. */
2255 serial = display_get_serial(terminal->display);
2256 if (terminal->hide_cursor_serial != serial) {
2257 input_set_pointer_image(input, CURSOR_BLANK);
2258 terminal->hide_cursor_serial = serial;
2259 }
2260 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002261}
2262
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002263static void
2264keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002265 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002266{
2267 struct terminal *terminal = data;
2268
2269 terminal->focused = (device != NULL);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002270 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002271}
2272
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002273static int wordsep(int ch)
2274{
2275 const char extra[] = "-,./?%&#:_=+@~";
2276
2277 return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
2278}
2279
2280static int
2281recompute_selection(struct terminal *terminal)
2282{
2283 struct rectangle allocation;
2284 int col, x, width, height;
2285 int start_row, end_row;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002286 int word_start, eol;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002287 int side_margin, top_margin;
2288 int start_x, end_x;
2289 int cw, ch;
2290 union utf8_char *data;
2291
2292 cw = terminal->extents.max_x_advance;
2293 ch = terminal->extents.height;
2294 widget_get_allocation(terminal->widget, &allocation);
2295 width = terminal->width * cw;
2296 height = terminal->height * ch;
2297 side_margin = allocation.x + (allocation.width - width) / 2;
2298 top_margin = allocation.y + (allocation.height - height) / 2;
2299
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002300 start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1;
2301 end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1;
2302
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002303 if (start_row < end_row ||
2304 (start_row == end_row &&
2305 terminal->selection_start_x < terminal->selection_end_x)) {
2306 terminal->selection_start_row = start_row;
2307 terminal->selection_end_row = end_row;
2308 start_x = terminal->selection_start_x;
2309 end_x = terminal->selection_end_x;
2310 } else {
2311 terminal->selection_start_row = end_row;
2312 terminal->selection_end_row = start_row;
2313 start_x = terminal->selection_end_x;
2314 end_x = terminal->selection_start_x;
2315 }
2316
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002317 eol = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002318 if (terminal->selection_start_row < 0) {
2319 terminal->selection_start_row = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002320 terminal->selection_start_col = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002321 } else {
2322 x = side_margin + cw / 2;
2323 data = terminal_get_row(terminal,
2324 terminal->selection_start_row);
2325 word_start = 0;
2326 for (col = 0; col < terminal->width; col++, x += cw) {
2327 if (col == 0 || wordsep(data[col - 1].ch))
2328 word_start = col;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002329 if (data[col].ch != 0)
2330 eol = col + 1;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002331 if (start_x < x)
2332 break;
2333 }
2334
2335 switch (terminal->dragging) {
2336 case SELECT_LINE:
2337 terminal->selection_start_col = 0;
2338 break;
2339 case SELECT_WORD:
2340 terminal->selection_start_col = word_start;
2341 break;
2342 case SELECT_CHAR:
2343 terminal->selection_start_col = col;
2344 break;
2345 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002346 }
2347
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002348 if (terminal->selection_end_row >= terminal->height) {
2349 terminal->selection_end_row = terminal->height;
2350 terminal->selection_end_col = 0;
2351 } else {
2352 x = side_margin + cw / 2;
2353 data = terminal_get_row(terminal, terminal->selection_end_row);
2354 for (col = 0; col < terminal->width; col++, x += cw) {
2355 if (terminal->dragging == SELECT_CHAR && end_x < x)
2356 break;
2357 if (terminal->dragging == SELECT_WORD &&
2358 end_x < x && wordsep(data[col].ch))
2359 break;
2360 }
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002361 terminal->selection_end_col = col;
2362 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002363
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002364 if (terminal->selection_end_col != terminal->selection_start_col ||
2365 terminal->selection_start_row != terminal->selection_end_row) {
2366 col = terminal->selection_end_col;
2367 if (col > 0 && data[col - 1].ch == 0)
2368 terminal->selection_end_col = terminal->width;
2369 data = terminal_get_row(terminal, terminal->selection_start_row);
2370 if (data[terminal->selection_start_col].ch == 0)
2371 terminal->selection_start_col = eol;
2372 }
2373
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002374 return 1;
2375}
2376
Kristian Høgsberg59826582011-01-20 11:56:57 -05002377static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002378button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002379 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002380 uint32_t button,
2381 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002382{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002383 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002384
2385 switch (button) {
2386 case 272:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002387 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002388
2389 if (time - terminal->button_time < 500)
2390 terminal->click_count++;
2391 else
2392 terminal->click_count = 1;
2393
2394 terminal->button_time = time;
2395 terminal->dragging =
2396 (terminal->click_count - 1) % 3 + SELECT_CHAR;
2397
Kristian Høgsberg59826582011-01-20 11:56:57 -05002398 input_get_position(input,
2399 &terminal->selection_start_x,
2400 &terminal->selection_start_y);
2401 terminal->selection_end_x = terminal->selection_start_x;
2402 terminal->selection_end_y = terminal->selection_start_y;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002403 if (recompute_selection(terminal))
2404 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002405 } else {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002406 terminal->dragging = SELECT_NONE;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002407 }
2408 break;
2409 }
2410}
2411
2412static int
Kristian Høgsberg29784402012-06-28 14:27:02 -04002413enter_handler(struct widget *widget,
2414 struct input *input, float x, float y, void *data)
2415{
2416 return CURSOR_IBEAM;
2417}
2418
2419static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002420motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002421 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002422 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002423{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002424 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002425
2426 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002427 input_get_position(input,
2428 &terminal->selection_end_x,
2429 &terminal->selection_end_y);
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002430
2431 if (recompute_selection(terminal))
2432 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002433 }
2434
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002435 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002436}
2437
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002438static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002439terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002440{
2441 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002442 cairo_surface_t *surface;
2443 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002444
2445 terminal = malloc(sizeof *terminal);
2446 if (terminal == NULL)
2447 return terminal;
2448
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002449 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002450 terminal->fullscreen = fullscreen;
Callum Lowcay30eeae52011-01-07 19:46:55 +00002451 terminal->color_scheme = &DEFAULT_COLORS;
2452 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002453 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002454 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002455 terminal->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002456 terminal->widget = frame_create(terminal->window, terminal);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002457 window_set_title(terminal->window, "Wayland Terminal");
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002458 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002459
2460 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002461 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002462
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002463 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002464 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002465
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002466 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002467 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002468 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002469 keyboard_focus_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002470 widget_set_redraw_handler(terminal->widget, redraw_handler);
2471 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002472 widget_set_button_handler(terminal->widget, button_handler);
Kristian Høgsberg29784402012-06-28 14:27:02 -04002473 widget_set_enter_handler(terminal->widget, enter_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002474 widget_set_motion_handler(terminal->widget, motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002475
Kristian Høgsberg09531622010-06-14 23:22:15 -04002476 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2477 cr = cairo_create(surface);
Kristian Høgsberg38912df2012-06-27 17:52:23 -04002478 cairo_set_font_size(cr, option_font_size);
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002479 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002480 CAIRO_FONT_SLANT_NORMAL,
2481 CAIRO_FONT_WEIGHT_BOLD);
2482 terminal->font_bold = cairo_get_scaled_font (cr);
2483 cairo_scaled_font_reference(terminal->font_bold);
2484
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002485 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002486 CAIRO_FONT_SLANT_NORMAL,
2487 CAIRO_FONT_WEIGHT_NORMAL);
2488 terminal->font_normal = cairo_get_scaled_font (cr);
2489 cairo_scaled_font_reference(terminal->font_normal);
2490
Kristian Høgsberg09531622010-06-14 23:22:15 -04002491 cairo_font_extents(cr, &terminal->extents);
2492 cairo_destroy(cr);
2493 cairo_surface_destroy(surface);
2494
Kristian Høgsberga1627922012-06-20 17:30:03 -04002495 terminal_resize(terminal, 80, 25);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002496
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002497 wl_list_insert(terminal_list.prev, &terminal->link);
2498
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002499 return terminal;
2500}
2501
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002502static void
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002503terminal_destroy(struct terminal *terminal)
2504{
2505 window_destroy(terminal->window);
2506 close(terminal->master);
2507 wl_list_remove(&terminal->link);
2508 free(terminal);
2509
2510 if (wl_list_empty(&terminal_list))
2511 exit(0);
2512}
2513
2514static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002515io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002516{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002517 struct terminal *terminal =
2518 container_of(task, struct terminal, io_task);
2519 char buffer[256];
2520 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002521
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002522 if (events & EPOLLHUP) {
2523 terminal_destroy(terminal);
2524 return;
2525 }
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002526
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002527 len = read(terminal->master, buffer, sizeof buffer);
2528 if (len < 0)
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002529 terminal_destroy(terminal);
2530 else
2531 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002532}
2533
2534static int
2535terminal_run(struct terminal *terminal, const char *path)
2536{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002537 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002538 pid_t pid;
2539
2540 pid = forkpty(&master, NULL, NULL, NULL);
2541 if (pid == 0) {
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04002542 setenv("TERM", option_term, 1);
2543 setenv("COLORTERM", option_term, 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002544 if (execl(path, path, NULL)) {
2545 printf("exec failed: %m\n");
2546 exit(EXIT_FAILURE);
2547 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002548 } else if (pid < 0) {
2549 fprintf(stderr, "failed to fork and create pty (%m).\n");
2550 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002551 }
2552
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002553 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002554 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002555 terminal->io_task.run = io_handler;
2556 display_watch_fd(terminal->display, terminal->master,
2557 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002558
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002559 window_set_fullscreen(terminal->window, terminal->fullscreen);
2560 if (!terminal->fullscreen)
2561 terminal_resize(terminal, 80, 24);
2562
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002563 return 0;
2564}
2565
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002566static const struct config_key terminal_config_keys[] = {
2567 { "font", CONFIG_KEY_STRING, &option_font },
Kristian Høgsberg38912df2012-06-27 17:52:23 -04002568 { "font-size", CONFIG_KEY_INTEGER, &option_font_size },
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04002569 { "term", CONFIG_KEY_STRING, &option_term },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002570};
2571
2572static const struct config_section config_sections[] = {
2573 { "terminal",
2574 terminal_config_keys, ARRAY_LENGTH(terminal_config_keys) },
2575};
2576
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002577static const struct weston_option terminal_options[] = {
2578 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002579 { WESTON_OPTION_STRING, "font", 0, &option_font },
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002580 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002581};
2582
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002583int main(int argc, char *argv[])
2584{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002585 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002586 struct terminal *terminal;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002587 char *config_file;
2588
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002589 option_shell = getenv("SHELL");
2590 if (!option_shell)
2591 option_shell = "/bin/bash";
2592
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002593 config_file = config_file_path("weston.ini");
2594 parse_config_file(config_file,
2595 config_sections, ARRAY_LENGTH(config_sections),
2596 NULL);
2597 free(config_file);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002598
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002599 argc = parse_options(terminal_options,
2600 ARRAY_LENGTH(terminal_options), argc, argv);
2601
2602 d = display_create(argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002603 if (d == NULL) {
2604 fprintf(stderr, "failed to create display: %m\n");
2605 return -1;
2606 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002607
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002608 wl_list_init(&terminal_list);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002609 terminal = terminal_create(d, option_fullscreen);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002610 if (terminal_run(terminal, option_shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002611 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002612
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002613 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002614
2615 return 0;
2616}