blob: 66d3facb5786dbf94f29a765aadf6eb776504c9a [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øgsbergde845cf2012-06-20 22:14:31 -040043static char *option_term = "xterm";
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040044static char *option_shell;
45
46static struct wl_list terminal_list;
47
48static struct terminal *
49terminal_create(struct display *display, int fullscreen);
50static void
51terminal_destroy(struct terminal *terminal);
52static int
53terminal_run(struct terminal *terminal, const char *path);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050054
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050055#define MOD_SHIFT 0x01
56#define MOD_ALT 0x02
57#define MOD_CTRL 0x04
58
Callum Lowcay30eeae52011-01-07 19:46:55 +000059#define ATTRMASK_BOLD 0x01
60#define ATTRMASK_UNDERLINE 0x02
61#define ATTRMASK_BLINK 0x04
62#define ATTRMASK_INVERSE 0x08
Callum Lowcay81179db2011-01-10 12:14:01 +130063#define ATTRMASK_CONCEALED 0x10
Callum Lowcay30eeae52011-01-07 19:46:55 +000064
Callum Lowcayb8609ad2011-01-07 19:46:57 +000065/* Buffer sizes */
Callum Lowcayef57a9b2011-01-14 20:46:23 +130066#define MAX_RESPONSE 256
67#define MAX_ESCAPE 255
Callum Lowcayb8609ad2011-01-07 19:46:57 +000068
Callum Lowcay8e57dd52011-01-07 19:46:59 +000069/* Terminal modes */
70#define MODE_SHOW_CURSOR 0x00000001
71#define MODE_INVERSE 0x00000002
72#define MODE_AUTOWRAP 0x00000004
73#define MODE_AUTOREPEAT 0x00000008
74#define MODE_LF_NEWLINE 0x00000010
Callum Lowcay69e96582011-01-07 19:47:00 +000075#define MODE_IRM 0x00000020
Callum Lowcay7e08e902011-01-07 19:47:02 +000076#define MODE_DELETE_SENDS_DEL 0x00000040
77#define MODE_ALT_SENDS_ESC 0x00000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +000078
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000079union utf8_char {
80 unsigned char byte[4];
81 uint32_t ch;
82};
83
84enum utf8_state {
85 utf8state_start,
86 utf8state_accept,
87 utf8state_reject,
88 utf8state_expect3,
89 utf8state_expect2,
90 utf8state_expect1
91};
92
93struct utf8_state_machine {
94 enum utf8_state state;
95 int len;
96 union utf8_char s;
97};
98
99static void
100init_state_machine(struct utf8_state_machine *machine)
101{
102 machine->state = utf8state_start;
103 machine->len = 0;
104 machine->s.ch = 0;
105}
106
107static enum utf8_state
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400108utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000109{
110 switch(machine->state) {
111 case utf8state_start:
112 case utf8state_accept:
113 case utf8state_reject:
114 machine->s.ch = 0;
115 machine->len = 0;
116 if(c == 0xC0 || c == 0xC1) {
117 /* overlong encoding, reject */
118 machine->state = utf8state_reject;
119 } else if((c & 0x80) == 0) {
120 /* single byte, accept */
121 machine->s.byte[machine->len++] = c;
122 machine->state = utf8state_accept;
123 } else if((c & 0xC0) == 0x80) {
124 /* parser out of sync, ignore byte */
125 machine->state = utf8state_start;
126 } else if((c & 0xE0) == 0xC0) {
127 /* start of two byte sequence */
128 machine->s.byte[machine->len++] = c;
129 machine->state = utf8state_expect1;
130 } else if((c & 0xF0) == 0xE0) {
131 /* start of three byte sequence */
132 machine->s.byte[machine->len++] = c;
133 machine->state = utf8state_expect2;
134 } else if((c & 0xF8) == 0xF0) {
135 /* start of four byte sequence */
136 machine->s.byte[machine->len++] = c;
137 machine->state = utf8state_expect3;
138 } else {
139 /* overlong encoding, reject */
140 machine->state = utf8state_reject;
141 }
142 break;
143 case utf8state_expect3:
144 machine->s.byte[machine->len++] = c;
145 if((c & 0xC0) == 0x80) {
146 /* all good, continue */
147 machine->state = utf8state_expect2;
148 } else {
149 /* missing extra byte, reject */
150 machine->state = utf8state_reject;
151 }
152 break;
153 case utf8state_expect2:
154 machine->s.byte[machine->len++] = c;
155 if((c & 0xC0) == 0x80) {
156 /* all good, continue */
157 machine->state = utf8state_expect1;
158 } else {
159 /* missing extra byte, reject */
160 machine->state = utf8state_reject;
161 }
162 break;
163 case utf8state_expect1:
164 machine->s.byte[machine->len++] = c;
165 if((c & 0xC0) == 0x80) {
166 /* all good, accept */
167 machine->state = utf8state_accept;
168 } else {
169 /* missing extra byte, reject */
170 machine->state = utf8state_reject;
171 }
172 break;
173 default:
174 machine->state = utf8state_reject;
175 break;
176 }
177
178 return machine->state;
179}
180
Callum Lowcay256e72f2011-01-07 19:47:01 +0000181struct char_sub {
182 union utf8_char match;
183 union utf8_char replace;
184};
185/* Set last char_sub match to NULL char */
186typedef struct char_sub *character_set;
187
188struct char_sub CS_US[] = {
189 {{{0, }}, {{0, }}}
190};
191static struct char_sub CS_UK[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200192 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000193 {{{0, }}, {{0, }}}
194};
195static struct char_sub CS_SPECIAL[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200196 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond: ♦ */
197 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell: ▒ */
198 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT: ␉ */
199 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF: ␌ */
200 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR: ␍ */
201 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF: ␊ */
202 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree: ° */
203 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus: ± */
204 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL: ␤ */
205 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT: ␋ */
206 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB: ┘ */
207 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT: ┐ */
208 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT: ┌ */
209 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_LB: └ */
210 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS: ┼ */
211 {{{'o', 0, }}, {{0xE2, 0x8E, 0xBA, 0}}}, /* Horiz. Scan Line 1: ⎺ */
212 {{{'p', 0, }}, {{0xE2, 0x8E, 0xBB, 0}}}, /* Horiz. Scan Line 3: ⎻ */
213 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* Horiz. Scan Line 5: ─ */
214 {{{'r', 0, }}, {{0xE2, 0x8E, 0xBC, 0}}}, /* Horiz. Scan Line 7: ⎼ */
215 {{{'s', 0, }}, {{0xE2, 0x8E, 0xBD, 0}}}, /* Horiz. Scan Line 9: ⎽ */
216 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR: ├ */
217 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL: ┤ */
218 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU: ┴ */
219 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD: ┬ */
220 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V: │ */
221 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE: ≤ */
222 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE: ≥ */
223 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI: π */
224 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ: ≠ */
225 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
226 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT: ⋅ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000227 {{{0, }}, {{0, }}}
228};
229
230static void
231apply_char_set(character_set cs, union utf8_char *utf8)
232{
233 int i = 0;
234
235 while (cs[i].match.byte[0]) {
236 if ((*utf8).ch == cs[i].match.ch) {
237 *utf8 = cs[i].replace;
238 break;
239 }
240 i++;
241 }
242}
243
Callum Lowcay7e08e902011-01-07 19:47:02 +0000244struct key_map {
245 int sym;
246 int num;
247 char escape;
248 char code;
249};
250/* Set last key_sub sym to NULL */
251typedef struct key_map *keyboard_mode;
252
253static struct key_map KM_NORMAL[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400254 { XKB_KEY_Left, 1, '[', 'D' },
255 { XKB_KEY_Right, 1, '[', 'C' },
256 { XKB_KEY_Up, 1, '[', 'A' },
257 { XKB_KEY_Down, 1, '[', 'B' },
258 { XKB_KEY_Home, 1, '[', 'H' },
259 { XKB_KEY_End, 1, '[', 'F' },
260 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000261};
262static struct key_map KM_APPLICATION[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400263 { XKB_KEY_Left, 1, 'O', 'D' },
264 { XKB_KEY_Right, 1, 'O', 'C' },
265 { XKB_KEY_Up, 1, 'O', 'A' },
266 { XKB_KEY_Down, 1, 'O', 'B' },
267 { XKB_KEY_Home, 1, 'O', 'H' },
268 { XKB_KEY_End, 1, 'O', 'F' },
269 { XKB_KEY_KP_Enter, 1, 'O', 'M' },
270 { XKB_KEY_KP_Multiply, 1, 'O', 'j' },
271 { XKB_KEY_KP_Add, 1, 'O', 'k' },
272 { XKB_KEY_KP_Separator, 1, 'O', 'l' },
273 { XKB_KEY_KP_Subtract, 1, 'O', 'm' },
274 { XKB_KEY_KP_Divide, 1, 'O', 'o' },
275 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000276};
277
278static int
279function_key_response(char escape, int num, uint32_t modifiers,
280 char code, char *response)
281{
282 int mod_num = 0;
283 int len;
284
Kristian Høgsberg70163132012-05-08 15:55:39 -0400285 if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
286 if (modifiers & MOD_ALT_MASK) mod_num |= 2;
287 if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000288
289 if (mod_num != 0)
290 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
291 num, mod_num + 1, code);
292 else if (code != '~')
293 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
294 escape, code);
295 else
296 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
297 escape, num, code);
298
299 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
300 else return len;
301}
302
303/* returns the number of bytes written into response,
304 * which must have room for MAX_RESPONSE bytes */
305static int
306apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
307{
308 struct key_map map;
309 int len = 0;
310 int i = 0;
311
312 while (mode[i].sym) {
313 map = mode[i++];
314 if (sym == map.sym) {
315 len = function_key_response(map.escape, map.num,
316 modifiers, map.code,
317 response);
318 break;
319 }
320 }
321
322 return len;
323}
324
Callum Lowcay30eeae52011-01-07 19:46:55 +0000325struct terminal_color { double r, g, b, a; };
326struct attr {
327 unsigned char fg, bg;
328 char a; /* attributes format:
329 * 76543210
Callum Lowcay81179db2011-01-10 12:14:01 +1300330 * cilub */
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500331 char s; /* in selection */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000332};
333struct color_scheme {
334 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500335 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000336 struct attr default_attr;
337};
338
339static void
340attr_init(struct attr *data_attr, struct attr attr, int n)
341{
342 int i;
343 for (i = 0; i < n; i++) {
344 data_attr[i] = attr;
345 }
346}
347
Callum Lowcay67a201d2011-01-12 19:23:41 +1300348enum escape_state {
349 escape_state_normal = 0,
350 escape_state_escape,
351 escape_state_dcs,
352 escape_state_csi,
353 escape_state_osc,
354 escape_state_inner_escape,
355 escape_state_ignore,
356 escape_state_special
357};
358
359#define ESC_FLAG_WHAT 0x01
360#define ESC_FLAG_GT 0x02
361#define ESC_FLAG_BANG 0x04
362#define ESC_FLAG_CASH 0x08
363#define ESC_FLAG_SQUOTE 0x10
364#define ESC_FLAG_DQUOTE 0x20
365#define ESC_FLAG_SPACE 0x40
366
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400367enum {
368 SELECT_NONE,
369 SELECT_CHAR,
370 SELECT_WORD,
371 SELECT_LINE
372};
373
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500374struct terminal {
375 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500376 struct widget *widget;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500377 struct display *display;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000378 union utf8_char *data;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400379 struct task io_task;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000380 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000381 struct attr *data_attr;
382 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000383 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000384 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000385 char saved_origin_mode;
386 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000387 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000388 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000389 character_set cs, g0, g1;
390 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000391 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000392 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500393 int width, height, start, row, column;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000394 int saved_row, saved_column;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600395 int send_cursor_position;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500396 int fd, master;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500397 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300398 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500399 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300400 enum escape_state state;
401 enum escape_state outer_state;
402 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000403 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500404 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500405 int fullscreen;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500406 int focused;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400407 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000408 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400409 cairo_font_extents_t extents;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500410 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg88fd4082012-06-21 15:55:03 -0400411 uint32_t hide_cursor_serial;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500412
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400413 struct wl_data_source *selection;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400414 uint32_t button_time;
415 int dragging, click_count;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500416 int selection_start_x, selection_start_y;
417 int selection_end_x, selection_end_y;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400418 int selection_start_row, selection_start_col;
419 int selection_end_row, selection_end_col;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -0400420 struct wl_list link;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500421};
422
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000423/* Create default tab stops, every 8 characters */
424static void
425terminal_init_tabs(struct terminal *terminal)
426{
427 int i = 0;
428
429 while (i < terminal->width) {
430 if (i % 8 == 0)
431 terminal->tab_ruler[i] = 1;
432 else
433 terminal->tab_ruler[i] = 0;
434 i++;
435 }
436}
437
Callum Lowcay30eeae52011-01-07 19:46:55 +0000438static void
439terminal_init(struct terminal *terminal)
440{
441 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000442 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000443 terminal->mode = MODE_SHOW_CURSOR |
444 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000445 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000446 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000447
448 terminal->row = 0;
449 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000450
Callum Lowcay256e72f2011-01-07 19:47:01 +0000451 terminal->g0 = CS_US;
452 terminal->g1 = CS_US;
453 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000454 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000455
456 terminal->saved_g0 = terminal->g0;
457 terminal->saved_g1 = terminal->g1;
458 terminal->saved_cs = terminal->cs;
459
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000460 terminal->saved_attr = terminal->curr_attr;
461 terminal->saved_origin_mode = terminal->origin_mode;
462 terminal->saved_row = terminal->row;
463 terminal->saved_column = terminal->column;
464
465 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000466}
467
468static void
469init_color_table(struct terminal *terminal)
470{
471 int c, r;
472 struct terminal_color *color_table = terminal->color_table;
473
474 for (c = 0; c < 256; c ++) {
475 if (c < 16) {
476 color_table[c] = terminal->color_scheme->palette[c];
477 } else if (c < 232) {
478 r = c - 16;
479 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
480 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
481 color_table[c].r = ((double)(r % 6) / 6.0);
482 color_table[c].a = 1.0;
483 } else {
484 r = (c - 232) * 10 + 8;
485 color_table[c].r = ((double) r) / 256.0;
486 color_table[c].g = color_table[c].r;
487 color_table[c].b = color_table[c].r;
488 color_table[c].a = 1.0;
489 }
490 }
491}
492
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000493static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500494terminal_get_row(struct terminal *terminal, int row)
495{
496 int index;
497
498 index = (row + terminal->start) % terminal->height;
499
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000500 return &terminal->data[index * terminal->width];
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500501}
502
Callum Lowcay30eeae52011-01-07 19:46:55 +0000503static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500504terminal_get_attr_row(struct terminal *terminal, int row)
505{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000506 int index;
507
508 index = (row + terminal->start) % terminal->height;
509
510 return &terminal->data_attr[index * terminal->width];
511}
512
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500513union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300514 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500515 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500516};
517
518static void
519terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500520 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500521{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500522 struct attr attr;
523 int foreground, background, tmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500524
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500525 decoded->attr.s = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400526 if (((row == terminal->selection_start_row &&
527 col >= terminal->selection_start_col) ||
528 row > terminal->selection_start_row) &&
529 ((row == terminal->selection_end_row &&
530 col < terminal->selection_end_col) ||
531 row < terminal->selection_end_row))
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500532 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500533
534 /* get the attributes for this character cell */
535 attr = terminal_get_attr_row(terminal, row)[col];
536 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500537 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500538 ((terminal->mode & MODE_SHOW_CURSOR) &&
539 terminal->focused && terminal->row == row &&
540 terminal->column == col)) {
541 foreground = attr.bg;
542 background = attr.fg;
543 if (attr.a & ATTRMASK_BOLD) {
544 if (foreground <= 16) foreground |= 0x08;
545 if (background <= 16) background &= 0x07;
546 }
547 } else {
548 foreground = attr.fg;
549 background = attr.bg;
550 }
551
552 if (terminal->mode & MODE_INVERSE) {
553 tmp = foreground;
554 foreground = background;
555 background = tmp;
556 if (attr.a & ATTRMASK_BOLD) {
557 if (foreground <= 16) foreground |= 0x08;
558 if (background <= 16) background &= 0x07;
559 }
560 }
561
Callum Lowcay9d708b02011-01-12 20:06:17 +1300562 decoded->attr.fg = foreground;
563 decoded->attr.bg = background;
564 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000565}
566
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500567
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500568static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000569terminal_scroll_buffer(struct terminal *terminal, int d)
570{
571 int i;
572
573 d = d % (terminal->height + 1);
574 terminal->start = (terminal->start + d) % terminal->height;
575 if (terminal->start < 0) terminal->start = terminal->height + terminal->start;
576 if(d < 0) {
577 d = 0 - d;
578 for(i = 0; i < d; i++) {
579 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
580 attr_init(terminal_get_attr_row(terminal, i),
581 terminal->curr_attr, terminal->width);
582 }
583 } else {
584 for(i = terminal->height - d; i < terminal->height; i++) {
585 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
586 attr_init(terminal_get_attr_row(terminal, i),
587 terminal->curr_attr, terminal->width);
588 }
589 }
590}
591
592static void
593terminal_scroll_window(struct terminal *terminal, int d)
594{
595 int i;
596 int window_height;
597 int from_row, to_row;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000598
599 // scrolling range is inclusive
600 window_height = terminal->margin_bottom - terminal->margin_top + 1;
601 d = d % (window_height + 1);
602 if(d < 0) {
603 d = 0 - d;
604 to_row = terminal->margin_bottom;
605 from_row = terminal->margin_bottom - d;
606
607 for (i = 0; i < (window_height - d); i++) {
608 memcpy(terminal_get_row(terminal, to_row - i),
609 terminal_get_row(terminal, from_row - i),
610 terminal->data_pitch);
611 memcpy(terminal_get_attr_row(terminal, to_row - i),
612 terminal_get_attr_row(terminal, from_row - i),
613 terminal->attr_pitch);
614 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000615 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
616 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000617 attr_init(terminal_get_attr_row(terminal, i),
618 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000619 }
620 } else {
621 to_row = terminal->margin_top;
622 from_row = terminal->margin_top + d;
623
624 for (i = 0; i < (window_height - d); i++) {
625 memcpy(terminal_get_row(terminal, to_row + i),
626 terminal_get_row(terminal, from_row + i),
627 terminal->data_pitch);
628 memcpy(terminal_get_attr_row(terminal, to_row + i),
629 terminal_get_attr_row(terminal, from_row + i),
630 terminal->attr_pitch);
631 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000632 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
633 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000634 attr_init(terminal_get_attr_row(terminal, i),
635 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000636 }
637 }
638}
639
640static void
641terminal_scroll(struct terminal *terminal, int d)
642{
643 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
644 terminal_scroll_buffer(terminal, d);
645 else
646 terminal_scroll_window(terminal, d);
647}
648
649static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000650terminal_shift_line(struct terminal *terminal, int d)
651{
652 union utf8_char *row;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500653 struct attr *attr_row;
Callum Lowcay69e96582011-01-07 19:47:00 +0000654
655 row = terminal_get_row(terminal, terminal->row);
656 attr_row = terminal_get_attr_row(terminal, terminal->row);
657
658 if ((terminal->width + d) <= terminal->column)
659 d = terminal->column + 1 - terminal->width;
660 if ((terminal->column + d) >= terminal->width)
661 d = terminal->width - terminal->column - 1;
662
663 if (d < 0) {
664 d = 0 - d;
665 memmove(&row[terminal->column],
666 &row[terminal->column + d],
667 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
Callum Lowcay69e96582011-01-07 19:47:00 +0000668 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
669 (terminal->width - terminal->column - d) * sizeof(struct attr));
670 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
671 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
672 } else {
673 memmove(&row[terminal->column + d], &row[terminal->column],
674 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
675 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
676 (terminal->width - terminal->column - d) * sizeof(struct attr));
677 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
678 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
679 }
680}
681
682static void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500683terminal_resize_cells(struct terminal *terminal, int width, int height)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500684{
685 size_t size;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000686 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000687 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000688 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000689 int data_pitch, attr_pitch;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500690 int i, l, total_rows;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500691 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000692 struct winsize ws;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500693
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500694 if (width < 1)
695 width = 1;
696 if (height < 1)
697 height = 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500698 if (terminal->width == width && terminal->height == height)
699 return;
700
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000701 data_pitch = width * sizeof(union utf8_char);
702 size = data_pitch * height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500703 data = malloc(size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000704 attr_pitch = width * sizeof(struct attr);
705 data_attr = malloc(attr_pitch * height);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000706 tab_ruler = malloc(width);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500707 memset(data, 0, size);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000708 memset(tab_ruler, 0, width);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000709 attr_init(data_attr, terminal->curr_attr, width * height);
710 if (terminal->data && terminal->data_attr) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500711 if (width > terminal->width)
712 l = terminal->width;
713 else
714 l = width;
715
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500716 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500717 total_rows = height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500718 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500719 total_rows = terminal->height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500720 }
721
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000722 for (i = 0; i < total_rows; i++) {
723 memcpy(&data[width * i],
724 terminal_get_row(terminal, i),
725 l * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000726 memcpy(&data_attr[width * i],
727 terminal_get_attr_row(terminal, i),
728 l * sizeof(struct attr));
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000729 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500730
731 free(terminal->data);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000732 free(terminal->data_attr);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000733 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500734 }
735
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000736 terminal->data_pitch = data_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000737 terminal->attr_pitch = attr_pitch;
Callum Lowcay86653ed2011-01-07 19:47:03 +0000738 terminal->margin_bottom =
739 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500740 terminal->width = width;
741 terminal->height = height;
742 terminal->data = data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000743 terminal->data_attr = data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000744 terminal->tab_ruler = tab_ruler;
745 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500746
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000747 /* Update the window size */
748 ws.ws_row = terminal->height;
749 ws.ws_col = terminal->width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500750 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500751 ws.ws_xpixel = allocation.width;
752 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000753 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500754}
755
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500756static void
757resize_handler(struct widget *widget,
758 int32_t width, int32_t height, void *data)
759{
760 struct terminal *terminal = data;
761 int32_t columns, rows, m;
762
Alexander Preisingere2b88c02012-06-18 20:59:26 +0200763 if (width < 200)
764 width = 200;
765
766 if (height < 50)
767 height = 50;
768
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500769 m = 2 * terminal->margin;
770 columns = (width - m) / (int32_t) terminal->extents.max_x_advance;
771 rows = (height - m) / (int32_t) terminal->extents.height;
772
773 if (!terminal->fullscreen) {
774 width = columns * terminal->extents.max_x_advance + m;
775 height = rows * terminal->extents.height + m;
776 widget_set_size(terminal->widget, width, height);
777 }
778
779 terminal_resize_cells(terminal, columns, rows);
780}
781
782static void
783terminal_resize(struct terminal *terminal, int columns, int rows)
784{
785 int32_t width, height, m;
786
787 if (terminal->fullscreen)
788 return;
789
790 m = 2 * terminal->margin;
791 width = columns * terminal->extents.max_x_advance + m;
792 height = rows * terminal->extents.height + m;
Kristian Høgsberga1627922012-06-20 17:30:03 -0400793
794 frame_set_child_size(terminal->widget, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500795}
796
Callum Lowcay30eeae52011-01-07 19:46:55 +0000797struct color_scheme DEFAULT_COLORS = {
798 {
799 {0, 0, 0, 1}, /* black */
800 {0.66, 0, 0, 1}, /* red */
801 {0 , 0.66, 0, 1}, /* green */
802 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
803 {0 , 0 , 0.66, 1}, /* blue */
804 {0.66, 0 , 0.66, 1}, /* magenta */
805 {0, 0.66, 0.66, 1}, /* cyan */
806 {0.66, 0.66, 0.66, 1}, /* light grey */
807 {0.22, 0.33, 0.33, 1}, /* dark grey */
808 {1, 0.33, 0.33, 1}, /* high red */
809 {0.33, 1, 0.33, 1}, /* high green */
810 {1, 1, 0.33, 1}, /* high yellow */
811 {0.33, 0.33, 1, 1}, /* high blue */
812 {1, 0.33, 1, 1}, /* high magenta */
813 {0.33, 1, 1, 1}, /* high cyan */
814 {1, 1, 1, 1} /* white */
815 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500816 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000817 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
818};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400819
Kristian Høgsberg22106762008-12-08 13:50:07 -0500820static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500821terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
822{
823 cairo_set_source_rgba(cr,
824 terminal->color_table[index].r,
825 terminal->color_table[index].g,
826 terminal->color_table[index].b,
827 terminal->color_table[index].a);
828}
829
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500830static void
831terminal_send_selection(struct terminal *terminal, int fd)
832{
833 int row, col;
834 union utf8_char *p_row;
835 union decoded_attr attr;
836 FILE *fp;
837 int len;
838
839 fp = fdopen(fd, "w");
840 for (row = 0; row < terminal->height; row++) {
841 p_row = terminal_get_row(terminal, row);
842 for (col = 0; col < terminal->width; col++) {
843 /* get the attributes for this character cell */
844 terminal_decode_attr(terminal, row, col, &attr);
845 if (!attr.attr.s)
846 continue;
847 len = strnlen((char *) p_row[col].byte, 4);
848 fwrite(p_row[col].byte, 1, len, fp);
849 }
850 }
851 fclose(fp);
852}
853
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500854struct glyph_run {
855 struct terminal *terminal;
856 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400857 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500858 union decoded_attr attr;
859 cairo_glyph_t glyphs[256], *g;
860};
861
862static void
863glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
864{
865 run->terminal = terminal;
866 run->cr = cr;
867 run->g = run->glyphs;
868 run->count = 0;
869 run->attr.key = 0;
870}
871
872static void
873glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
874{
875 cairo_scaled_font_t *font;
876
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500877 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
878 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300879 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500880 font = run->terminal->font_bold;
881 else
882 font = run->terminal->font_normal;
883 cairo_set_scaled_font(run->cr, font);
884 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300885 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500886
Callum Lowcay9d708b02011-01-12 20:06:17 +1300887 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
888 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500889 run->g = run->glyphs;
890 run->count = 0;
891 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300892 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500893}
894
895static void
896glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
897{
898 int num_glyphs;
899 cairo_scaled_font_t *font;
900
901 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
902
Callum Lowcay9d708b02011-01-12 20:06:17 +1300903 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500904 font = run->terminal->font_bold;
905 else
906 font = run->terminal->font_normal;
907
908 cairo_move_to(run->cr, x, y);
909 cairo_scaled_font_text_to_glyphs (font, x, y,
910 (char *) c->byte, 4,
911 &run->g, &num_glyphs,
912 NULL, NULL, NULL);
913 run->g += num_glyphs;
914 run->count += num_glyphs;
915}
916
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500917
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500918static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500919redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500920{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500921 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500922 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500923 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000924 int top_margin, side_margin;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600925 int row, col, cursor_x, cursor_y;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000926 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500927 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000928 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500929 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500930 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500931 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500932 cairo_font_extents_t extents;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500933
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500934 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500935 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500936 cr = cairo_create(surface);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500937 cairo_rectangle(cr, allocation.x, allocation.y,
938 allocation.width, allocation.height);
939 cairo_clip(cr);
940 cairo_push_group(cr);
941
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500942 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500943 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500944 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500945
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500946 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500947
Kristian Høgsberg59826582011-01-20 11:56:57 -0500948 extents = terminal->extents;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500949 side_margin = (allocation.width - terminal->width * extents.max_x_advance) / 2;
950 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500951
Callum Lowcay30eeae52011-01-07 19:46:55 +0000952 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500953 cairo_translate(cr, allocation.x + side_margin,
954 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500955 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000956 for (row = 0; row < terminal->height; row++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000957 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000958 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500959 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000960
Callum Lowcay9d708b02011-01-12 20:06:17 +1300961 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -0500962 continue;
963
Callum Lowcay9d708b02011-01-12 20:06:17 +1300964 terminal_set_color(terminal, cr, attr.attr.bg);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500965 cairo_move_to(cr, col * extents.max_x_advance,
966 row * extents.height);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000967 cairo_rel_line_to(cr, extents.max_x_advance, 0);
968 cairo_rel_line_to(cr, 0, extents.height);
969 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
970 cairo_close_path(cr);
971 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500972 }
973 }
Callum Lowcay30eeae52011-01-07 19:46:55 +0000974
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500975 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
976
977 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500978 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500979 for (row = 0; row < terminal->height; row++) {
980 p_row = terminal_get_row(terminal, row);
981 for (col = 0; col < terminal->width; col++) {
982 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500983 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500984
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500985 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000986
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500987 text_x = col * extents.max_x_advance;
988 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +1300989 if (attr.attr.a & ATTRMASK_UNDERLINE) {
990 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000991 cairo_move_to(cr, text_x, (double)text_y + 1.5);
992 cairo_line_to(cr, text_x + extents.max_x_advance, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000993 cairo_stroke(cr);
994 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -0500995
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500996 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000997 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500998 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500999
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001000 attr.key = ~0;
1001 glyph_run_flush(&run, attr);
1002
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001003 if ((terminal->mode & MODE_SHOW_CURSOR) && !terminal->focused) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001004 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001005
Callum Lowcay30eeae52011-01-07 19:46:55 +00001006 cairo_set_line_width(cr, 1);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001007 cairo_move_to(cr, terminal->column * extents.max_x_advance + d,
1008 terminal->row * extents.height + d);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001009 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
1010 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
1011 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
1012 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001013
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001014 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001015 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001016
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001017 cairo_pop_group_to_source(cr);
1018 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001019 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001020 cairo_surface_destroy(surface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001021
1022 if (terminal->send_cursor_position) {
1023 cursor_x = side_margin + allocation.x +
1024 terminal->column * extents.max_x_advance;
1025 cursor_y = top_margin + allocation.y +
1026 terminal->row * extents.height;
1027 window_set_text_cursor_position(terminal->window,
1028 cursor_x, cursor_y);
1029 terminal->send_cursor_position = 0;
1030 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001031}
1032
1033static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001034terminal_write(struct terminal *terminal, const char *data, size_t length)
1035{
1036 if (write(terminal->master, data, length) < 0)
1037 abort();
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001038 terminal->send_cursor_position = 1;
Kristian Høgsberg26130862011-08-24 11:30:21 -04001039}
1040
1041static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001042terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001043
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001044static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001045handle_char(struct terminal *terminal, union utf8_char utf8);
1046
1047static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001048handle_sgr(struct terminal *terminal, int code);
1049
1050static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001051handle_term_parameter(struct terminal *terminal, int code, int sr)
1052{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001053 int i;
1054
Callum Lowcay67a201d2011-01-12 19:23:41 +13001055 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001056 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001057 case 1: /* DECCKM */
1058 if (sr) terminal->key_mode = KM_APPLICATION;
1059 else terminal->key_mode = KM_NORMAL;
1060 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001061 case 2: /* DECANM */
1062 /* No VT52 support yet */
1063 terminal->g0 = CS_US;
1064 terminal->g1 = CS_US;
1065 terminal->cs = terminal->g0;
1066 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001067 case 3: /* DECCOLM */
1068 if (sr)
1069 terminal_resize(terminal, 132, 24);
1070 else
1071 terminal_resize(terminal, 80, 24);
1072
1073 /* set columns, but also home cursor and clear screen */
1074 terminal->row = 0; terminal->column = 0;
1075 for (i = 0; i < terminal->height; i++) {
1076 memset(terminal_get_row(terminal, i),
1077 0, terminal->data_pitch);
1078 attr_init(terminal_get_attr_row(terminal, i),
1079 terminal->curr_attr, terminal->width);
1080 }
1081 break;
1082 case 5: /* DECSCNM */
1083 if (sr) terminal->mode |= MODE_INVERSE;
1084 else terminal->mode &= ~MODE_INVERSE;
1085 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001086 case 6: /* DECOM */
1087 terminal->origin_mode = sr;
1088 if (terminal->origin_mode)
1089 terminal->row = terminal->margin_top;
1090 else
1091 terminal->row = 0;
1092 terminal->column = 0;
1093 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001094 case 7: /* DECAWM */
1095 if (sr) terminal->mode |= MODE_AUTOWRAP;
1096 else terminal->mode &= ~MODE_AUTOWRAP;
1097 break;
1098 case 8: /* DECARM */
1099 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1100 else terminal->mode &= ~MODE_AUTOREPEAT;
1101 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001102 case 12: /* Very visible cursor (CVVIS) */
1103 /* FIXME: What do we do here. */
1104 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001105 case 25:
1106 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1107 else terminal->mode &= ~MODE_SHOW_CURSOR;
1108 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001109 case 1034: /* smm/rmm, meta mode on/off */
1110 /* ignore */
1111 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001112 case 1037: /* deleteSendsDel */
1113 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1114 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1115 break;
1116 case 1039: /* altSendsEscape */
1117 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1118 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1119 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001120 case 1049: /* rmcup/smcup, alternate screen */
1121 /* Ignore. Should be possible to implement,
1122 * but it's kind of annoying. */
1123 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001124 default:
1125 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1126 break;
1127 }
1128 } else {
1129 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001130 case 4: /* IRM */
1131 if (sr) terminal->mode |= MODE_IRM;
1132 else terminal->mode &= ~MODE_IRM;
1133 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001134 case 20: /* LNM */
1135 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1136 else terminal->mode &= ~MODE_LF_NEWLINE;
1137 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001138 default:
1139 fprintf(stderr, "Unknown parameter: %d\n", code);
1140 break;
1141 }
1142 }
1143}
1144
1145static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001146handle_dcs(struct terminal *terminal)
1147{
1148}
1149
1150static void
1151handle_osc(struct terminal *terminal)
1152{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001153 char *p;
1154 int code;
1155
1156 terminal->escape[terminal->escape_length++] = '\0';
1157 p = &terminal->escape[2];
1158 code = strtol(p, &p, 10);
1159 if (*p == ';') p++;
1160
1161 switch (code) {
1162 case 0: /* Icon name and window title */
1163 case 1: /* Icon label */
1164 case 2: /* Window title*/
1165 window_set_title(terminal->window, p);
1166 break;
1167 default:
1168 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1169 break;
1170 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001171}
1172
1173static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001174handle_escape(struct terminal *terminal)
1175{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001176 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001177 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001178 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001179 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001180 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001181 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001182 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001183
1184 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001185 i = 0;
1186 p = &terminal->escape[2];
1187 while ((isdigit(*p) || *p == ';') && i < 10) {
1188 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001189 if (!set[i]) {
1190 args[i] = 0;
1191 set[i] = 1;
1192 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001193 p++;
1194 i++;
1195 } else {
1196 args[i] = strtol(p, &p, 10);
1197 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001198 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001199 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001200
1201 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001202 case '@': /* ICH */
1203 count = set[0] ? args[0] : 1;
1204 if (count == 0) count = 1;
1205 terminal_shift_line(terminal, count);
1206 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001207 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001208 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001209 if (count == 0) count = 1;
1210 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001211 terminal->row -= count;
1212 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001213 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001214 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001215 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001216 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001217 if (count == 0) count = 1;
1218 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001219 terminal->row += count;
1220 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001221 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001222 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001223 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001224 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001225 if (count == 0) count = 1;
1226 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001227 terminal->column += count;
1228 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001229 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001230 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001231 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001232 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001233 if (count == 0) count = 1;
1234 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001235 terminal->column -= count;
1236 else
1237 terminal->column = 0;
1238 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001239 case 'E': /* CNL */
1240 count = set[0] ? args[0] : 1;
1241 if (terminal->row + count <= terminal->margin_bottom)
1242 terminal->row += count;
1243 else
1244 terminal->row = terminal->margin_bottom;
1245 terminal->column = 0;
1246 break;
1247 case 'F': /* CPL */
1248 count = set[0] ? args[0] : 1;
1249 if (terminal->row - count >= terminal->margin_top)
1250 terminal->row -= count;
1251 else
1252 terminal->row = terminal->margin_top;
1253 terminal->column = 0;
1254 break;
1255 case 'G': /* CHA */
1256 y = set[0] ? args[0] : 1;
1257 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1258
1259 terminal->column = y - 1;
1260 break;
1261 case 'f': /* HVP */
1262 case 'H': /* CUP */
1263 x = (set[1] ? args[1] : 1) - 1;
1264 x = x < 0 ? 0 :
1265 (x >= terminal->width ? terminal->width - 1 : x);
1266
1267 y = (set[0] ? args[0] : 1) - 1;
1268 if (terminal->origin_mode) {
1269 y += terminal->margin_top;
1270 y = y < terminal->margin_top ? terminal->margin_top :
1271 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1272 } else {
1273 y = y < 0 ? 0 :
1274 (y >= terminal->height ? terminal->height - 1 : y);
1275 }
1276
1277 terminal->row = y;
1278 terminal->column = x;
1279 break;
1280 case 'I': /* CHT */
1281 count = set[0] ? args[0] : 1;
1282 if (count == 0) count = 1;
1283 while (count > 0 && terminal->column < terminal->width) {
1284 if (terminal->tab_ruler[terminal->column]) count--;
1285 terminal->column++;
1286 }
1287 terminal->column--;
1288 break;
1289 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001290 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001291 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001292 if (!set[0] || args[0] == 0 || args[0] > 2) {
1293 memset(&row[terminal->column],
1294 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1295 attr_init(&attr_row[terminal->column],
1296 terminal->curr_attr, terminal->width - terminal->column);
1297 for (i = terminal->row + 1; i < terminal->height; i++) {
1298 memset(terminal_get_row(terminal, i),
1299 0, terminal->data_pitch);
1300 attr_init(terminal_get_attr_row(terminal, i),
1301 terminal->curr_attr, terminal->width);
1302 }
1303 } else if (args[0] == 1) {
1304 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1305 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1306 for (i = 0; i < terminal->row; i++) {
1307 memset(terminal_get_row(terminal, i),
1308 0, terminal->data_pitch);
1309 attr_init(terminal_get_attr_row(terminal, i),
1310 terminal->curr_attr, terminal->width);
1311 }
1312 } else if (args[0] == 2) {
1313 for (i = 0; i < terminal->height; i++) {
1314 memset(terminal_get_row(terminal, i),
1315 0, terminal->data_pitch);
1316 attr_init(terminal_get_attr_row(terminal, i),
1317 terminal->curr_attr, terminal->width);
1318 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001319 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001320 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001321 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001322 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001323 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001324 if (!set[0] || args[0] == 0 || args[0] > 2) {
1325 memset(&row[terminal->column], 0,
1326 (terminal->width - terminal->column) * sizeof(union utf8_char));
1327 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1328 terminal->width - terminal->column);
1329 } else if (args[0] == 1) {
1330 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1331 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1332 } else if (args[0] == 2) {
1333 memset(row, 0, terminal->data_pitch);
1334 attr_init(attr_row, terminal->curr_attr, terminal->width);
1335 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001336 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001337 case 'L': /* IL */
1338 count = set[0] ? args[0] : 1;
1339 if (count == 0) count = 1;
1340 if (terminal->row >= terminal->margin_top &&
1341 terminal->row < terminal->margin_bottom)
1342 {
1343 top = terminal->margin_top;
1344 terminal->margin_top = terminal->row;
1345 terminal_scroll(terminal, 0 - count);
1346 terminal->margin_top = top;
1347 } else if (terminal->row == terminal->margin_bottom) {
1348 memset(terminal_get_row(terminal, terminal->row),
1349 0, terminal->data_pitch);
1350 attr_init(terminal_get_attr_row(terminal, terminal->row),
1351 terminal->curr_attr, terminal->width);
1352 }
1353 break;
1354 case 'M': /* DL */
1355 count = set[0] ? args[0] : 1;
1356 if (count == 0) count = 1;
1357 if (terminal->row >= terminal->margin_top &&
1358 terminal->row < terminal->margin_bottom)
1359 {
1360 top = terminal->margin_top;
1361 terminal->margin_top = terminal->row;
1362 terminal_scroll(terminal, count);
1363 terminal->margin_top = top;
1364 } else if (terminal->row == terminal->margin_bottom) {
1365 memset(terminal_get_row(terminal, terminal->row),
1366 0, terminal->data_pitch);
1367 }
1368 break;
1369 case 'P': /* DCH */
1370 count = set[0] ? args[0] : 1;
1371 if (count == 0) count = 1;
1372 terminal_shift_line(terminal, 0 - count);
1373 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001374 case 'S': /* SU */
1375 terminal_scroll(terminal, set[0] ? args[0] : 1);
1376 break;
1377 case 'T': /* SD */
1378 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1379 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001380 case 'X': /* ECH */
1381 count = set[0] ? args[0] : 1;
1382 if (count == 0) count = 1;
1383 if ((terminal->column + count) > terminal->width)
1384 count = terminal->width - terminal->column;
1385 row = terminal_get_row(terminal, terminal->row);
1386 attr_row = terminal_get_attr_row(terminal, terminal->row);
1387 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1388 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1389 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001390 case 'Z': /* CBT */
1391 count = set[0] ? args[0] : 1;
1392 if (count == 0) count = 1;
1393 while (count > 0 && terminal->column >= 0) {
1394 if (terminal->tab_ruler[terminal->column]) count--;
1395 terminal->column--;
1396 }
1397 terminal->column++;
1398 break;
1399 case '`': /* HPA */
1400 y = set[0] ? args[0] : 1;
1401 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1402
1403 terminal->column = y - 1;
1404 break;
1405 case 'b': /* REP */
1406 count = set[0] ? args[0] : 1;
1407 if (count == 0) count = 1;
1408 if (terminal->last_char.byte[0])
1409 for (i = 0; i < count; i++)
1410 handle_char(terminal, terminal->last_char);
1411 terminal->last_char.byte[0] = 0;
1412 break;
1413 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001414 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001415 break;
1416 case 'd': /* VPA */
1417 x = set[0] ? args[0] : 1;
1418 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1419
1420 terminal->row = x - 1;
1421 break;
1422 case 'g': /* TBC */
1423 if (!set[0] || args[0] == 0) {
1424 terminal->tab_ruler[terminal->column] = 0;
1425 } else if (args[0] == 3) {
1426 memset(terminal->tab_ruler, 0, terminal->width);
1427 }
1428 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001429 case 'h': /* SM */
1430 for(i = 0; i < 10 && set[i]; i++) {
1431 handle_term_parameter(terminal, args[i], 1);
1432 }
1433 break;
1434 case 'l': /* RM */
1435 for(i = 0; i < 10 && set[i]; i++) {
1436 handle_term_parameter(terminal, args[i], 0);
1437 }
1438 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001439 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001440 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001441 if (i <= 7 && set[i] && set[i + 1] &&
1442 set[i + 2] && args[i + 1] == 5)
1443 {
1444 if (args[i] == 38) {
1445 handle_sgr(terminal, args[i + 2] + 256);
1446 break;
1447 } else if (args[i] == 48) {
1448 handle_sgr(terminal, args[i + 2] + 512);
1449 break;
1450 }
1451 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001452 if(set[i]) {
1453 handle_sgr(terminal, args[i]);
1454 } else if(i == 0) {
1455 handle_sgr(terminal, 0);
1456 break;
1457 } else {
1458 break;
1459 }
1460 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001461 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001462 case 'n': /* DSR */
1463 i = set[0] ? args[0] : 0;
1464 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001465 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001466 } else if (i == 6) {
1467 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1468 terminal->origin_mode ?
1469 terminal->row+terminal->margin_top : terminal->row+1,
1470 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001471 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001472 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001473 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001474 case 'r':
1475 if(!set[0]) {
1476 terminal->margin_top = 0;
1477 terminal->margin_bottom = terminal->height-1;
1478 terminal->row = 0;
1479 terminal->column = 0;
1480 } else {
1481 top = (set[0] ? args[0] : 1) - 1;
1482 top = top < 0 ? 0 :
1483 (top >= terminal->height ? terminal->height - 1 : top);
1484 bottom = (set[1] ? args[1] : 1) - 1;
1485 bottom = bottom < 0 ? 0 :
1486 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1487 if(bottom > top) {
1488 terminal->margin_top = top;
1489 terminal->margin_bottom = bottom;
1490 } else {
1491 terminal->margin_top = 0;
1492 terminal->margin_bottom = terminal->height-1;
1493 }
1494 if(terminal->origin_mode)
1495 terminal->row = terminal->margin_top;
1496 else
1497 terminal->row = 0;
1498 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001499 }
1500 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001501 case 's':
1502 terminal->saved_row = terminal->row;
1503 terminal->saved_column = terminal->column;
1504 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001505 case 't': /* windowOps */
1506 if (!set[0]) break;
1507 switch (args[0]) {
1508 case 4: /* resize px */
1509 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001510 widget_schedule_resize(terminal->widget,
1511 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001512 }
1513 break;
1514 case 8: /* resize ch */
1515 if (set[1] && set[2]) {
1516 terminal_resize(terminal, args[2], args[1]);
1517 }
1518 break;
1519 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001520 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001521 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1522 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001523 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001524 break;
1525 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001526 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001527 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1528 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001529 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001530 break;
1531 case 18: /* report ch */
1532 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1533 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001534 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001535 break;
1536 case 21: /* report title */
1537 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1538 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001539 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001540 break;
1541 default:
1542 if (args[0] >= 24)
1543 terminal_resize(terminal, terminal->width, args[0]);
1544 else
1545 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1546 break;
1547 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001548 case 'u':
1549 terminal->row = terminal->saved_row;
1550 terminal->column = terminal->saved_column;
1551 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001552 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001553 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001554 break;
1555 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001556}
1557
1558static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001559handle_non_csi_escape(struct terminal *terminal, char code)
1560{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001561 switch(code) {
1562 case 'M': /* RI */
1563 terminal->row -= 1;
1564 if(terminal->row < terminal->margin_top) {
1565 terminal->row = terminal->margin_top;
1566 terminal_scroll(terminal, -1);
1567 }
1568 break;
1569 case 'E': /* NEL */
1570 terminal->column = 0;
1571 // fallthrough
1572 case 'D': /* IND */
1573 terminal->row += 1;
1574 if(terminal->row > terminal->margin_bottom) {
1575 terminal->row = terminal->margin_bottom;
1576 terminal_scroll(terminal, +1);
1577 }
1578 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001579 case 'c': /* RIS */
1580 terminal_init(terminal);
1581 break;
1582 case 'H': /* HTS */
1583 terminal->tab_ruler[terminal->column] = 1;
1584 break;
1585 case '7': /* DECSC */
1586 terminal->saved_row = terminal->row;
1587 terminal->saved_column = terminal->column;
1588 terminal->saved_attr = terminal->curr_attr;
1589 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001590 terminal->saved_cs = terminal->cs;
1591 terminal->saved_g0 = terminal->g0;
1592 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001593 break;
1594 case '8': /* DECRC */
1595 terminal->row = terminal->saved_row;
1596 terminal->column = terminal->saved_column;
1597 terminal->curr_attr = terminal->saved_attr;
1598 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001599 terminal->cs = terminal->saved_cs;
1600 terminal->g0 = terminal->saved_g0;
1601 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001602 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001603 case '=': /* DECPAM */
1604 terminal->key_mode = KM_APPLICATION;
1605 break;
1606 case '>': /* DECPNM */
1607 terminal->key_mode = KM_NORMAL;
1608 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001609 default:
1610 fprintf(stderr, "Unknown escape code: %c\n", code);
1611 break;
1612 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001613}
1614
1615static void
1616handle_special_escape(struct terminal *terminal, char special, char code)
1617{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001618 int i, numChars;
1619
1620 if (special == '#') {
1621 switch(code) {
1622 case '8':
1623 /* fill with 'E', no cheap way to do this */
1624 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1625 numChars = terminal->width * terminal->height;
1626 for(i = 0; i < numChars; i++) {
1627 terminal->data[i].byte[0] = 'E';
1628 }
1629 break;
1630 default:
1631 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1632 break;
1633 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001634 } else if (special == '(' || special == ')') {
1635 switch(code) {
1636 case '0':
1637 if (special == '(')
1638 terminal->g0 = CS_SPECIAL;
1639 else
1640 terminal->g1 = CS_SPECIAL;
1641 break;
1642 case 'A':
1643 if (special == '(')
1644 terminal->g0 = CS_UK;
1645 else
1646 terminal->g1 = CS_UK;
1647 break;
1648 case 'B':
1649 if (special == '(')
1650 terminal->g0 = CS_US;
1651 else
1652 terminal->g1 = CS_US;
1653 break;
1654 default:
1655 fprintf(stderr, "Unknown character set %c\n", code);
1656 break;
1657 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001658 } else {
1659 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1660 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001661}
1662
1663static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001664handle_sgr(struct terminal *terminal, int code)
1665{
1666 switch(code) {
1667 case 0:
1668 terminal->curr_attr = terminal->color_scheme->default_attr;
1669 break;
1670 case 1:
1671 terminal->curr_attr.a |= ATTRMASK_BOLD;
1672 if (terminal->curr_attr.fg < 8)
1673 terminal->curr_attr.fg += 8;
1674 break;
1675 case 4:
1676 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1677 break;
1678 case 5:
1679 terminal->curr_attr.a |= ATTRMASK_BLINK;
1680 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001681 case 8:
1682 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1683 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001684 case 2:
1685 case 21:
1686 case 22:
1687 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1688 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1689 terminal->curr_attr.fg -= 8;
1690 break;
1691 case 24:
1692 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1693 break;
1694 case 25:
1695 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1696 break;
1697 case 7:
1698 case 26:
1699 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1700 break;
1701 case 27:
1702 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1703 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001704 case 28:
1705 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1706 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001707 case 39:
1708 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1709 break;
1710 case 49:
1711 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1712 break;
1713 default:
1714 if(code >= 30 && code <= 37) {
1715 terminal->curr_attr.fg = code - 30;
1716 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1717 terminal->curr_attr.fg += 8;
1718 } else if(code >= 40 && code <= 47) {
1719 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001720 } else if (code >= 90 && code <= 97) {
1721 terminal->curr_attr.fg = code - 90 + 8;
1722 } else if (code >= 100 && code <= 107) {
1723 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001724 } else if(code >= 256 && code < 512) {
1725 terminal->curr_attr.fg = code - 256;
1726 } else if(code >= 512 && code < 768) {
1727 terminal->curr_attr.bg = code - 512;
1728 } else {
1729 fprintf(stderr, "Unknown SGR code: %d\n", code);
1730 }
1731 break;
1732 }
1733}
1734
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001735/* Returns 1 if c was special, otherwise 0 */
1736static int
1737handle_special_char(struct terminal *terminal, char c)
1738{
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001739 switch(c) {
1740 case '\r':
1741 terminal->column = 0;
1742 break;
1743 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001744 if (terminal->mode & MODE_LF_NEWLINE) {
1745 terminal->column = 0;
1746 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001747 /* fallthrough */
1748 case '\v':
1749 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001750 terminal->row++;
1751 if(terminal->row > terminal->margin_bottom) {
1752 terminal->row = terminal->margin_bottom;
1753 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001754 }
1755
1756 break;
1757 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001758 while (terminal->column < terminal->width) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001759 if (terminal->mode & MODE_IRM)
1760 terminal_shift_line(terminal, +1);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001761 terminal->column++;
Kristian Høgsbergcca3c2f2012-06-20 15:56:13 -04001762 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001763 }
1764 if (terminal->column >= terminal->width) {
1765 terminal->column = terminal->width - 1;
1766 }
1767
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001768 break;
1769 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001770 if (terminal->column >= terminal->width) {
1771 terminal->column = terminal->width - 2;
1772 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001773 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001774 } else if (terminal->mode & MODE_AUTOWRAP) {
1775 terminal->column = terminal->width - 1;
1776 terminal->row -= 1;
1777 if (terminal->row < terminal->margin_top) {
1778 terminal->row = terminal->margin_top;
1779 terminal_scroll(terminal, -1);
1780 }
1781 }
1782
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001783 break;
1784 case '\a':
1785 /* Bell */
1786 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001787 case '\x0E': /* SO */
1788 terminal->cs = terminal->g1;
1789 break;
1790 case '\x0F': /* SI */
1791 terminal->cs = terminal->g0;
1792 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001793 default:
1794 return 0;
1795 }
1796
1797 return 1;
1798}
1799
1800static void
1801handle_char(struct terminal *terminal, union utf8_char utf8)
1802{
1803 union utf8_char *row;
1804 struct attr *attr_row;
1805
1806 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001807
1808 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001809
1810 /* There are a whole lot of non-characters, control codes,
1811 * and formatting codes that should probably be ignored,
1812 * for example: */
1813 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1814 /* BOM, ignore */
1815 return;
1816 }
1817
1818 /* Some of these non-characters should be translated, e.g.: */
1819 if (utf8.byte[0] < 32) {
1820 utf8.byte[0] = utf8.byte[0] + 64;
1821 }
1822
1823 /* handle right margin effects */
1824 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001825 if (terminal->mode & MODE_AUTOWRAP) {
1826 terminal->column = 0;
1827 terminal->row += 1;
1828 if (terminal->row > terminal->margin_bottom) {
1829 terminal->row = terminal->margin_bottom;
1830 terminal_scroll(terminal, +1);
1831 }
1832 } else {
1833 terminal->column--;
1834 }
1835 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001836
1837 row = terminal_get_row(terminal, terminal->row);
1838 attr_row = terminal_get_attr_row(terminal, terminal->row);
1839
Callum Lowcay69e96582011-01-07 19:47:00 +00001840 if (terminal->mode & MODE_IRM)
1841 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001842 row[terminal->column] = utf8;
1843 attr_row[terminal->column++] = terminal->curr_attr;
1844
1845 if (utf8.ch != terminal->last_char.ch)
1846 terminal->last_char = utf8;
1847}
1848
Callum Lowcay30eeae52011-01-07 19:46:55 +00001849static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001850escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1851{
1852 int len, i;
1853
1854 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1855 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1856 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1857 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1858 else len = 1; /* Invalid, cannot happen */
1859
1860 if (terminal->escape_length + len <= MAX_ESCAPE) {
1861 for (i = 0; i < len; i++)
1862 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1863 terminal->escape_length += len;
1864 } else if (terminal->escape_length < MAX_ESCAPE) {
1865 terminal->escape[terminal->escape_length++] = 0;
1866 }
1867}
1868
1869static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001870terminal_data(struct terminal *terminal, const char *data, size_t length)
1871{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001872 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001873 union utf8_char utf8;
1874 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001875
1876 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001877 parser_state =
1878 utf8_next_char(&terminal->state_machine, data[i]);
1879 switch(parser_state) {
1880 case utf8state_accept:
1881 utf8.ch = terminal->state_machine.s.ch;
1882 break;
1883 case utf8state_reject:
1884 /* the unicode replacement character */
1885 utf8.byte[0] = 0xEF;
1886 utf8.byte[1] = 0xBF;
1887 utf8.byte[2] = 0xBD;
1888 utf8.byte[3] = 0x00;
1889 break;
1890 default:
1891 continue;
1892 }
1893
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001894 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001895 switch (terminal->state) {
1896 case escape_state_escape:
1897 escape_append_utf8(terminal, utf8);
1898 switch (utf8.byte[0]) {
1899 case 'P': /* DCS */
1900 terminal->state = escape_state_dcs;
1901 break;
1902 case '[': /* CSI */
1903 terminal->state = escape_state_csi;
1904 break;
1905 case ']': /* OSC */
1906 terminal->state = escape_state_osc;
1907 break;
1908 case '#':
1909 case '(':
1910 case ')': /* special */
1911 terminal->state = escape_state_special;
1912 break;
1913 case '^': /* PM (not implemented) */
1914 case '_': /* APC (not implemented) */
1915 terminal->state = escape_state_ignore;
1916 break;
1917 default:
1918 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001919 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13001920 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001921 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001922 continue;
1923 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001924 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1925 /* do nothing */
1926 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001927 terminal->escape_flags |= ESC_FLAG_WHAT;
1928 } else if (utf8.byte[0] == '>') {
1929 terminal->escape_flags |= ESC_FLAG_GT;
1930 } else if (utf8.byte[0] == '!') {
1931 terminal->escape_flags |= ESC_FLAG_BANG;
1932 } else if (utf8.byte[0] == '$') {
1933 terminal->escape_flags |= ESC_FLAG_CASH;
1934 } else if (utf8.byte[0] == '\'') {
1935 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1936 } else if (utf8.byte[0] == '"') {
1937 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1938 } else if (utf8.byte[0] == ' ') {
1939 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001940 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001941 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001942 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13001943 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001944 }
1945
1946 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1947 utf8.byte[0] == '`')
1948 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001949 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001950 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001951 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001952 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001953 continue;
1954 case escape_state_inner_escape:
1955 if (utf8.byte[0] == '\\') {
1956 terminal->state = escape_state_normal;
1957 if (terminal->outer_state == escape_state_dcs) {
1958 handle_dcs(terminal);
1959 } else if (terminal->outer_state == escape_state_osc) {
1960 handle_osc(terminal);
1961 }
1962 } else if (utf8.byte[0] == '\e') {
1963 terminal->state = terminal->outer_state;
1964 escape_append_utf8(terminal, utf8);
1965 if (terminal->escape_length >= MAX_ESCAPE)
1966 terminal->state = escape_state_normal;
1967 } else {
1968 terminal->state = terminal->outer_state;
1969 if (terminal->escape_length < MAX_ESCAPE)
1970 terminal->escape[terminal->escape_length++] = '\e';
1971 escape_append_utf8(terminal, utf8);
1972 if (terminal->escape_length >= MAX_ESCAPE)
1973 terminal->state = escape_state_normal;
1974 }
1975 continue;
1976 case escape_state_dcs:
1977 case escape_state_osc:
1978 case escape_state_ignore:
1979 if (utf8.byte[0] == '\e') {
1980 terminal->outer_state = terminal->state;
1981 terminal->state = escape_state_inner_escape;
1982 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
1983 terminal->state = escape_state_normal;
1984 handle_osc(terminal);
1985 } else {
1986 escape_append_utf8(terminal, utf8);
1987 if (terminal->escape_length >= MAX_ESCAPE)
1988 terminal->state = escape_state_normal;
1989 }
1990 continue;
1991 case escape_state_special:
1992 escape_append_utf8(terminal, utf8);
1993 terminal->state = escape_state_normal;
1994 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
1995 handle_special_escape(terminal, terminal->escape[1],
1996 utf8.byte[0]);
1997 }
1998 continue;
1999 default:
2000 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002001 }
2002
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002003 /* this is valid, because ASCII characters are never used to
2004 * introduce a multibyte sequence in UTF-8 */
2005 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002006 terminal->state = escape_state_escape;
2007 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002008 terminal->escape[0] = '\e';
2009 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002010 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002011 } else {
2012 handle_char(terminal, utf8);
2013 } /* if */
2014 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002015
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002016 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002017}
2018
2019static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002020data_source_target(void *data,
2021 struct wl_data_source *source, const char *mime_type)
2022{
2023 fprintf(stderr, "data_source_target, %s\n", mime_type);
2024}
2025
2026static void
2027data_source_send(void *data,
2028 struct wl_data_source *source,
2029 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002030{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002031 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002032
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002033 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002034}
2035
2036static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002037data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002038{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002039 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002040}
2041
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002042static const struct wl_data_source_listener data_source_listener = {
2043 data_source_target,
2044 data_source_send,
2045 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002046};
2047
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002048static int
2049handle_bound_key(struct terminal *terminal,
2050 struct input *input, uint32_t sym, uint32_t time)
2051{
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002052 struct terminal *new_terminal;
2053
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002054 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002055 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002056 /* Cut selection; terminal doesn't do cut, fall
2057 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002058 case XKB_KEY_C:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002059 terminal->selection =
2060 display_create_data_source(terminal->display);
2061 wl_data_source_offer(terminal->selection,
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002062 "text/plain;charset=utf-8");
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002063 wl_data_source_add_listener(terminal->selection,
2064 &data_source_listener, terminal);
Kristian Høgsbergfee91be2012-06-02 21:21:41 -04002065 input_set_selection(input, terminal->selection,
2066 display_get_serial(terminal->display));
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002067 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002068 case XKB_KEY_V:
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002069 input_receive_selection_data_to_fd(input,
2070 "text/plain;charset=utf-8",
2071 terminal->master);
2072
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002073 return 1;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002074
2075 case XKB_KEY_N:
2076 new_terminal =
2077 terminal_create(terminal->display, option_fullscreen);
2078 if (terminal_run(new_terminal, option_shell))
2079 terminal_destroy(new_terminal);
2080
2081 return 1;
2082
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002083 default:
2084 return 0;
2085 }
2086}
2087
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002088static void
2089key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002090 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2091 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002092{
2093 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002094 char ch[MAX_RESPONSE];
Kristian Høgsberg88fd4082012-06-21 15:55:03 -04002095 uint32_t modifiers, serial;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002096 int len = 0;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002097
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002098 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002099 if ((modifiers & MOD_CONTROL_MASK) &&
2100 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002101 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2102 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002103 return;
2104
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002105 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002106 case XKB_KEY_F11:
Daniel Stonec9785ea2012-05-30 16:31:52 +01002107 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002108 break;
2109 terminal->fullscreen ^= 1;
2110 window_set_fullscreen(window, terminal->fullscreen);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002111 break;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002112
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002113 case XKB_KEY_BackSpace:
Kristian Høgsbergb7f94bf2012-06-21 12:29:36 -04002114 if (modifiers & MOD_ALT_MASK)
2115 ch[len++] = 0x1b;
Kristian Høgsberg71a4cf42012-06-20 17:57:56 -04002116 ch[len++] = 0x7f;
2117 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002118 case XKB_KEY_Tab:
2119 case XKB_KEY_Linefeed:
2120 case XKB_KEY_Clear:
2121 case XKB_KEY_Pause:
2122 case XKB_KEY_Scroll_Lock:
2123 case XKB_KEY_Sys_Req:
2124 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002125 ch[len++] = sym & 0x7f;
2126 break;
2127
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002128 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002129 if (terminal->mode & MODE_LF_NEWLINE) {
2130 ch[len++] = 0x0D;
2131 ch[len++] = 0x0A;
2132 } else {
2133 ch[len++] = 0x0D;
2134 }
2135 break;
2136
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002137 case XKB_KEY_Shift_L:
2138 case XKB_KEY_Shift_R:
2139 case XKB_KEY_Control_L:
2140 case XKB_KEY_Control_R:
2141 case XKB_KEY_Alt_L:
2142 case XKB_KEY_Alt_R:
Kristian Høgsberg22fbcf72012-06-22 12:18:56 -04002143 case XKB_KEY_Meta_L:
2144 case XKB_KEY_Meta_R:
2145 case XKB_KEY_Super_L:
2146 case XKB_KEY_Super_R:
2147 case XKB_KEY_Hyper_L:
2148 case XKB_KEY_Hyper_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002149 break;
2150
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002151 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002152 len = function_key_response('[', 2, modifiers, '~', ch);
2153 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002154 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002155 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2156 ch[len++] = '\x04';
2157 } else {
2158 len = function_key_response('[', 3, modifiers, '~', ch);
2159 }
2160 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002161 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002162 len = function_key_response('[', 5, modifiers, '~', ch);
2163 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002164 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002165 len = function_key_response('[', 6, modifiers, '~', ch);
2166 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002167 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002168 len = function_key_response('O', 1, modifiers, 'P', ch);
2169 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002170 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002171 len = function_key_response('O', 1, modifiers, 'Q', ch);
2172 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002173 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002174 len = function_key_response('O', 1, modifiers, 'R', ch);
2175 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002176 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002177 len = function_key_response('O', 1, modifiers, 'S', ch);
2178 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002179 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002180 len = function_key_response('[', 15, modifiers, '~', ch);
2181 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002182 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002183 len = function_key_response('[', 17, modifiers, '~', ch);
2184 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002185 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002186 len = function_key_response('[', 18, modifiers, '~', ch);
2187 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002188 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002189 len = function_key_response('[', 19, modifiers, '~', ch);
2190 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002191 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002192 len = function_key_response('[', 20, modifiers, '~', ch);
2193 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002194 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002195 len = function_key_response('[', 21, modifiers, '~', ch);
2196 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002197 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002198 len = function_key_response('[', 24, modifiers, '~', ch);
2199 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002200 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002201 /* Handle special keys with alternate mappings */
2202 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2203 if (len != 0) break;
2204
Kristian Høgsberg70163132012-05-08 15:55:39 -04002205 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002206 if (sym >= '3' && sym <= '7')
2207 sym = (sym & 0x1f) + 8;
2208
2209 if (!((sym >= '!' && sym <= '/') ||
2210 (sym >= '8' && sym <= '?') ||
2211 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2212 else if (sym == '2') sym = 0x00;
2213 else if (sym == '/') sym = 0x1F;
2214 else if (sym == '8' || sym == '?') sym = 0x7F;
Kristian Høgsbergae9e0732012-06-21 12:30:15 -04002215 }
2216 if ((terminal->mode & MODE_ALT_SENDS_ESC) &&
2217 (modifiers & MOD_ALT_MASK)) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002218 ch[len++] = 0x1b;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002219 } else if (modifiers & MOD_ALT_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002220 sym = sym | 0x80;
2221 }
2222
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002223 if (sym < 256)
2224 ch[len++] = sym;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002225 break;
2226 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002227
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002228 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04002229 terminal_write(terminal, ch, len);
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002230
2231 /* Hide cursor, except if this was coming from a
2232 * repeating key press. */
2233 serial = display_get_serial(terminal->display);
2234 if (terminal->hide_cursor_serial != serial) {
2235 input_set_pointer_image(input, CURSOR_BLANK);
2236 terminal->hide_cursor_serial = serial;
2237 }
2238 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002239}
2240
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002241static void
2242keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002243 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002244{
2245 struct terminal *terminal = data;
2246
2247 terminal->focused = (device != NULL);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002248 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002249}
2250
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002251static int wordsep(int ch)
2252{
2253 const char extra[] = "-,./?%&#:_=+@~";
2254
2255 return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
2256}
2257
2258static int
2259recompute_selection(struct terminal *terminal)
2260{
2261 struct rectangle allocation;
2262 int col, x, width, height;
2263 int start_row, end_row;
2264 int word_start;
2265 int side_margin, top_margin;
2266 int start_x, end_x;
2267 int cw, ch;
2268 union utf8_char *data;
2269
2270 cw = terminal->extents.max_x_advance;
2271 ch = terminal->extents.height;
2272 widget_get_allocation(terminal->widget, &allocation);
2273 width = terminal->width * cw;
2274 height = terminal->height * ch;
2275 side_margin = allocation.x + (allocation.width - width) / 2;
2276 top_margin = allocation.y + (allocation.height - height) / 2;
2277
2278 start_row = (terminal->selection_start_y - top_margin) / ch;
2279 end_row = (terminal->selection_end_y - top_margin) / ch;
2280 if (start_row < end_row ||
2281 (start_row == end_row &&
2282 terminal->selection_start_x < terminal->selection_end_x)) {
2283 terminal->selection_start_row = start_row;
2284 terminal->selection_end_row = end_row;
2285 start_x = terminal->selection_start_x;
2286 end_x = terminal->selection_end_x;
2287 } else {
2288 terminal->selection_start_row = end_row;
2289 terminal->selection_end_row = start_row;
2290 start_x = terminal->selection_end_x;
2291 end_x = terminal->selection_start_x;
2292 }
2293
2294 x = side_margin + cw / 2;
2295 data = terminal_get_row(terminal, terminal->selection_start_row);
2296 word_start = 0;
2297 for (col = 0; col < terminal->width; col++, x += cw) {
2298 if (col == 0 || wordsep(data[col - 1].ch))
2299 word_start = col;
2300 if (start_x < x)
2301 break;
2302 }
2303
2304 switch (terminal->dragging) {
2305 case SELECT_LINE:
2306 terminal->selection_start_col = 0;
2307 break;
2308 case SELECT_WORD:
2309 terminal->selection_start_col = word_start;
2310 break;
2311 case SELECT_CHAR:
2312 terminal->selection_start_col = col;
2313 break;
2314 }
2315
2316 x = side_margin + cw / 2;
2317 data = terminal_get_row(terminal, terminal->selection_end_row);
2318 for (col = 0; col < terminal->width; col++, x += cw) {
2319 if (terminal->dragging == SELECT_CHAR && end_x < x)
2320 break;
2321 if (terminal->dragging == SELECT_WORD &&
2322 end_x < x && wordsep(data[col].ch))
2323 break;
2324 }
2325
2326 terminal->selection_end_col = col;
2327
2328 return 1;
2329}
2330
Kristian Høgsberg59826582011-01-20 11:56:57 -05002331static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002332button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002333 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002334 uint32_t button,
2335 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002336{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002337 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002338
2339 switch (button) {
2340 case 272:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002341 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002342
2343 if (time - terminal->button_time < 500)
2344 terminal->click_count++;
2345 else
2346 terminal->click_count = 1;
2347
2348 terminal->button_time = time;
2349 terminal->dragging =
2350 (terminal->click_count - 1) % 3 + SELECT_CHAR;
2351
Kristian Høgsberg59826582011-01-20 11:56:57 -05002352 input_get_position(input,
2353 &terminal->selection_start_x,
2354 &terminal->selection_start_y);
2355 terminal->selection_end_x = terminal->selection_start_x;
2356 terminal->selection_end_y = terminal->selection_start_y;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002357 if (recompute_selection(terminal))
2358 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002359 } else {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002360 terminal->dragging = SELECT_NONE;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002361 }
2362 break;
2363 }
2364}
2365
2366static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002367motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002368 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002369 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002370{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002371 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002372
2373 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002374 input_get_position(input,
2375 &terminal->selection_end_x,
2376 &terminal->selection_end_y);
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002377
2378 if (recompute_selection(terminal))
2379 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002380 }
2381
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002382 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002383}
2384
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002385static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002386terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002387{
2388 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002389 cairo_surface_t *surface;
2390 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002391
2392 terminal = malloc(sizeof *terminal);
2393 if (terminal == NULL)
2394 return terminal;
2395
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002396 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002397 terminal->fullscreen = fullscreen;
Callum Lowcay30eeae52011-01-07 19:46:55 +00002398 terminal->color_scheme = &DEFAULT_COLORS;
2399 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002400 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002401 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002402 terminal->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002403 terminal->widget = frame_create(terminal->window, terminal);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002404 window_set_title(terminal->window, "Wayland Terminal");
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002405 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002406
2407 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002408 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002409
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002410 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002411 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002412
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002413 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002414 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002415 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002416 keyboard_focus_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002417 widget_set_redraw_handler(terminal->widget, redraw_handler);
2418 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002419 widget_set_button_handler(terminal->widget, button_handler);
2420 widget_set_motion_handler(terminal->widget, motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002421
Kristian Høgsberg09531622010-06-14 23:22:15 -04002422 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2423 cr = cairo_create(surface);
Kristian Høgsberg09531622010-06-14 23:22:15 -04002424 cairo_set_font_size(cr, 14);
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002425 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002426 CAIRO_FONT_SLANT_NORMAL,
2427 CAIRO_FONT_WEIGHT_BOLD);
2428 terminal->font_bold = cairo_get_scaled_font (cr);
2429 cairo_scaled_font_reference(terminal->font_bold);
2430
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002431 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002432 CAIRO_FONT_SLANT_NORMAL,
2433 CAIRO_FONT_WEIGHT_NORMAL);
2434 terminal->font_normal = cairo_get_scaled_font (cr);
2435 cairo_scaled_font_reference(terminal->font_normal);
2436
Kristian Høgsberg09531622010-06-14 23:22:15 -04002437 cairo_font_extents(cr, &terminal->extents);
2438 cairo_destroy(cr);
2439 cairo_surface_destroy(surface);
2440
Kristian Høgsberga1627922012-06-20 17:30:03 -04002441 terminal_resize(terminal, 80, 25);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002442
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002443 wl_list_insert(terminal_list.prev, &terminal->link);
2444
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002445 return terminal;
2446}
2447
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002448static void
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002449terminal_destroy(struct terminal *terminal)
2450{
2451 window_destroy(terminal->window);
2452 close(terminal->master);
2453 wl_list_remove(&terminal->link);
2454 free(terminal);
2455
2456 if (wl_list_empty(&terminal_list))
2457 exit(0);
2458}
2459
2460static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002461io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002462{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002463 struct terminal *terminal =
2464 container_of(task, struct terminal, io_task);
2465 char buffer[256];
2466 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002467
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002468 if (events & EPOLLHUP) {
2469 terminal_destroy(terminal);
2470 return;
2471 }
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002472
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002473 len = read(terminal->master, buffer, sizeof buffer);
2474 if (len < 0)
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002475 terminal_destroy(terminal);
2476 else
2477 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002478}
2479
2480static int
2481terminal_run(struct terminal *terminal, const char *path)
2482{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002483 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002484 pid_t pid;
2485
2486 pid = forkpty(&master, NULL, NULL, NULL);
2487 if (pid == 0) {
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04002488 setenv("TERM", option_term, 1);
2489 setenv("COLORTERM", option_term, 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002490 if (execl(path, path, NULL)) {
2491 printf("exec failed: %m\n");
2492 exit(EXIT_FAILURE);
2493 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002494 } else if (pid < 0) {
2495 fprintf(stderr, "failed to fork and create pty (%m).\n");
2496 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002497 }
2498
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002499 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002500 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002501 terminal->io_task.run = io_handler;
2502 display_watch_fd(terminal->display, terminal->master,
2503 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002504
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002505 window_set_fullscreen(terminal->window, terminal->fullscreen);
2506 if (!terminal->fullscreen)
2507 terminal_resize(terminal, 80, 24);
2508
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002509 return 0;
2510}
2511
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002512static const struct config_key terminal_config_keys[] = {
2513 { "font", CONFIG_KEY_STRING, &option_font },
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04002514 { "term", CONFIG_KEY_STRING, &option_term },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002515};
2516
2517static const struct config_section config_sections[] = {
2518 { "terminal",
2519 terminal_config_keys, ARRAY_LENGTH(terminal_config_keys) },
2520};
2521
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002522static const struct weston_option terminal_options[] = {
2523 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002524 { WESTON_OPTION_STRING, "font", 0, &option_font },
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002525 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002526};
2527
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002528int main(int argc, char *argv[])
2529{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002530 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002531 struct terminal *terminal;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002532 char *config_file;
2533
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002534 option_shell = getenv("SHELL");
2535 if (!option_shell)
2536 option_shell = "/bin/bash";
2537
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002538 config_file = config_file_path("weston.ini");
2539 parse_config_file(config_file,
2540 config_sections, ARRAY_LENGTH(config_sections),
2541 NULL);
2542 free(config_file);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002543
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002544 argc = parse_options(terminal_options,
2545 ARRAY_LENGTH(terminal_options), argc, argv);
2546
2547 d = display_create(argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002548 if (d == NULL) {
2549 fprintf(stderr, "failed to create display: %m\n");
2550 return -1;
2551 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002552
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002553 wl_list_init(&terminal_list);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002554 terminal = terminal_create(d, option_fullscreen);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002555 if (terminal_run(terminal, option_shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002556 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002557
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002558 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002559
2560 return 0;
2561}