blob: 7b2ce21399625d1bf992628db97c81adb64ae191 [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øgsberg0c4457f2008-12-07 19:59:11 -0500367struct terminal {
368 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500369 struct widget *widget;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500370 struct display *display;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000371 union utf8_char *data;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400372 struct task io_task;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000373 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000374 struct attr *data_attr;
375 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000376 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000377 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000378 char saved_origin_mode;
379 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000380 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000381 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000382 character_set cs, g0, g1;
383 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000384 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000385 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500386 int width, height, start, row, column;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000387 int saved_row, saved_column;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600388 int send_cursor_position;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500389 int fd, master;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500390 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300391 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500392 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300393 enum escape_state state;
394 enum escape_state outer_state;
395 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000396 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500397 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500398 int fullscreen;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500399 int focused;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400400 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000401 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400402 cairo_font_extents_t extents;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500403 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500404
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400405 struct wl_data_source *selection;
406 int32_t dragging;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500407 int selection_start_x, selection_start_y;
408 int selection_end_x, selection_end_y;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -0400409 struct wl_list link;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500410};
411
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000412/* Create default tab stops, every 8 characters */
413static void
414terminal_init_tabs(struct terminal *terminal)
415{
416 int i = 0;
417
418 while (i < terminal->width) {
419 if (i % 8 == 0)
420 terminal->tab_ruler[i] = 1;
421 else
422 terminal->tab_ruler[i] = 0;
423 i++;
424 }
425}
426
Callum Lowcay30eeae52011-01-07 19:46:55 +0000427static void
428terminal_init(struct terminal *terminal)
429{
430 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000431 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000432 terminal->mode = MODE_SHOW_CURSOR |
433 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000434 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000435 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000436
437 terminal->row = 0;
438 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000439
Callum Lowcay256e72f2011-01-07 19:47:01 +0000440 terminal->g0 = CS_US;
441 terminal->g1 = CS_US;
442 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000443 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000444
445 terminal->saved_g0 = terminal->g0;
446 terminal->saved_g1 = terminal->g1;
447 terminal->saved_cs = terminal->cs;
448
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000449 terminal->saved_attr = terminal->curr_attr;
450 terminal->saved_origin_mode = terminal->origin_mode;
451 terminal->saved_row = terminal->row;
452 terminal->saved_column = terminal->column;
453
454 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000455}
456
457static void
458init_color_table(struct terminal *terminal)
459{
460 int c, r;
461 struct terminal_color *color_table = terminal->color_table;
462
463 for (c = 0; c < 256; c ++) {
464 if (c < 16) {
465 color_table[c] = terminal->color_scheme->palette[c];
466 } else if (c < 232) {
467 r = c - 16;
468 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
469 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
470 color_table[c].r = ((double)(r % 6) / 6.0);
471 color_table[c].a = 1.0;
472 } else {
473 r = (c - 232) * 10 + 8;
474 color_table[c].r = ((double) r) / 256.0;
475 color_table[c].g = color_table[c].r;
476 color_table[c].b = color_table[c].r;
477 color_table[c].a = 1.0;
478 }
479 }
480}
481
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000482static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500483terminal_get_row(struct terminal *terminal, int row)
484{
485 int index;
486
487 index = (row + terminal->start) % terminal->height;
488
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000489 return &terminal->data[index * terminal->width];
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500490}
491
Callum Lowcay30eeae52011-01-07 19:46:55 +0000492static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500493terminal_get_attr_row(struct terminal *terminal, int row)
494{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000495 int index;
496
497 index = (row + terminal->start) % terminal->height;
498
499 return &terminal->data_attr[index * terminal->width];
500}
501
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500502union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300503 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500504 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500505};
506
Kristian Høgsberg59826582011-01-20 11:56:57 -0500507static int
508terminal_compare_position(struct terminal *terminal,
509 int x, int y, int32_t ref_row, int32_t ref_col)
510{
511 struct rectangle allocation;
512 int top_margin, side_margin, col, row, ref_x;
513
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500514 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsberg59826582011-01-20 11:56:57 -0500515 side_margin = allocation.x + (allocation.width - terminal->width * terminal->extents.max_x_advance) / 2;
516 top_margin = allocation.y + (allocation.height - terminal->height * terminal->extents.height) / 2;
517
518 col = (x - side_margin) / terminal->extents.max_x_advance;
519 row = (y - top_margin) / terminal->extents.height;
520
521 ref_x = side_margin + ref_col * terminal->extents.max_x_advance +
522 terminal->extents.max_x_advance / 2;
523
524 if (row < ref_row)
525 return -1;
526 if (row == ref_row) {
527 if (col < ref_col)
528 return -1;
529 if (col == ref_col && x < ref_x)
530 return -1;
531 }
532
533 return 1;
534}
535
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500536static void
537terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500538 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500539{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500540 struct attr attr;
541 int foreground, background, tmp;
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500542 int start_cmp, end_cmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500543
544 start_cmp =
545 terminal_compare_position(terminal,
546 terminal->selection_start_x,
547 terminal->selection_start_y,
548 row, col);
549 end_cmp =
550 terminal_compare_position(terminal,
551 terminal->selection_end_x,
552 terminal->selection_end_y,
553 row, col);
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500554 decoded->attr.s = 0;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500555 if (start_cmp < 0 && end_cmp > 0)
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500556 decoded->attr.s = 1;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500557 else if (end_cmp < 0 && start_cmp > 0)
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500558 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500559
560 /* get the attributes for this character cell */
561 attr = terminal_get_attr_row(terminal, row)[col];
562 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500563 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500564 ((terminal->mode & MODE_SHOW_CURSOR) &&
565 terminal->focused && terminal->row == row &&
566 terminal->column == col)) {
567 foreground = attr.bg;
568 background = attr.fg;
569 if (attr.a & ATTRMASK_BOLD) {
570 if (foreground <= 16) foreground |= 0x08;
571 if (background <= 16) background &= 0x07;
572 }
573 } else {
574 foreground = attr.fg;
575 background = attr.bg;
576 }
577
578 if (terminal->mode & MODE_INVERSE) {
579 tmp = foreground;
580 foreground = background;
581 background = tmp;
582 if (attr.a & ATTRMASK_BOLD) {
583 if (foreground <= 16) foreground |= 0x08;
584 if (background <= 16) background &= 0x07;
585 }
586 }
587
Callum Lowcay9d708b02011-01-12 20:06:17 +1300588 decoded->attr.fg = foreground;
589 decoded->attr.bg = background;
590 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000591}
592
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500593
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500594static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000595terminal_scroll_buffer(struct terminal *terminal, int d)
596{
597 int i;
598
599 d = d % (terminal->height + 1);
600 terminal->start = (terminal->start + d) % terminal->height;
601 if (terminal->start < 0) terminal->start = terminal->height + terminal->start;
602 if(d < 0) {
603 d = 0 - d;
604 for(i = 0; i < d; i++) {
605 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
606 attr_init(terminal_get_attr_row(terminal, i),
607 terminal->curr_attr, terminal->width);
608 }
609 } else {
610 for(i = terminal->height - d; i < terminal->height; i++) {
611 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
612 attr_init(terminal_get_attr_row(terminal, i),
613 terminal->curr_attr, terminal->width);
614 }
615 }
616}
617
618static void
619terminal_scroll_window(struct terminal *terminal, int d)
620{
621 int i;
622 int window_height;
623 int from_row, to_row;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000624
625 // scrolling range is inclusive
626 window_height = terminal->margin_bottom - terminal->margin_top + 1;
627 d = d % (window_height + 1);
628 if(d < 0) {
629 d = 0 - d;
630 to_row = terminal->margin_bottom;
631 from_row = terminal->margin_bottom - d;
632
633 for (i = 0; i < (window_height - d); i++) {
634 memcpy(terminal_get_row(terminal, to_row - i),
635 terminal_get_row(terminal, from_row - i),
636 terminal->data_pitch);
637 memcpy(terminal_get_attr_row(terminal, to_row - i),
638 terminal_get_attr_row(terminal, from_row - i),
639 terminal->attr_pitch);
640 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000641 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
642 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000643 attr_init(terminal_get_attr_row(terminal, i),
644 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000645 }
646 } else {
647 to_row = terminal->margin_top;
648 from_row = terminal->margin_top + d;
649
650 for (i = 0; i < (window_height - d); i++) {
651 memcpy(terminal_get_row(terminal, to_row + i),
652 terminal_get_row(terminal, from_row + i),
653 terminal->data_pitch);
654 memcpy(terminal_get_attr_row(terminal, to_row + i),
655 terminal_get_attr_row(terminal, from_row + i),
656 terminal->attr_pitch);
657 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000658 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
659 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000660 attr_init(terminal_get_attr_row(terminal, i),
661 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000662 }
663 }
664}
665
666static void
667terminal_scroll(struct terminal *terminal, int d)
668{
669 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
670 terminal_scroll_buffer(terminal, d);
671 else
672 terminal_scroll_window(terminal, d);
673}
674
675static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000676terminal_shift_line(struct terminal *terminal, int d)
677{
678 union utf8_char *row;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500679 struct attr *attr_row;
Callum Lowcay69e96582011-01-07 19:47:00 +0000680
681 row = terminal_get_row(terminal, terminal->row);
682 attr_row = terminal_get_attr_row(terminal, terminal->row);
683
684 if ((terminal->width + d) <= terminal->column)
685 d = terminal->column + 1 - terminal->width;
686 if ((terminal->column + d) >= terminal->width)
687 d = terminal->width - terminal->column - 1;
688
689 if (d < 0) {
690 d = 0 - d;
691 memmove(&row[terminal->column],
692 &row[terminal->column + d],
693 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
Callum Lowcay69e96582011-01-07 19:47:00 +0000694 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
695 (terminal->width - terminal->column - d) * sizeof(struct attr));
696 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
697 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
698 } else {
699 memmove(&row[terminal->column + d], &row[terminal->column],
700 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
701 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
702 (terminal->width - terminal->column - d) * sizeof(struct attr));
703 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
704 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
705 }
706}
707
708static void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500709terminal_resize_cells(struct terminal *terminal, int width, int height)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500710{
711 size_t size;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000712 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000713 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000714 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000715 int data_pitch, attr_pitch;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500716 int i, l, total_rows;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500717 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000718 struct winsize ws;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500719
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500720 if (width < 1)
721 width = 1;
722 if (height < 1)
723 height = 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500724 if (terminal->width == width && terminal->height == height)
725 return;
726
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000727 data_pitch = width * sizeof(union utf8_char);
728 size = data_pitch * height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500729 data = malloc(size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000730 attr_pitch = width * sizeof(struct attr);
731 data_attr = malloc(attr_pitch * height);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000732 tab_ruler = malloc(width);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500733 memset(data, 0, size);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000734 memset(tab_ruler, 0, width);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000735 attr_init(data_attr, terminal->curr_attr, width * height);
736 if (terminal->data && terminal->data_attr) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500737 if (width > terminal->width)
738 l = terminal->width;
739 else
740 l = width;
741
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500742 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500743 total_rows = height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500744 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500745 total_rows = terminal->height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500746 }
747
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000748 for (i = 0; i < total_rows; i++) {
749 memcpy(&data[width * i],
750 terminal_get_row(terminal, i),
751 l * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000752 memcpy(&data_attr[width * i],
753 terminal_get_attr_row(terminal, i),
754 l * sizeof(struct attr));
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000755 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500756
757 free(terminal->data);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000758 free(terminal->data_attr);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000759 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500760 }
761
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000762 terminal->data_pitch = data_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000763 terminal->attr_pitch = attr_pitch;
Callum Lowcay86653ed2011-01-07 19:47:03 +0000764 terminal->margin_bottom =
765 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500766 terminal->width = width;
767 terminal->height = height;
768 terminal->data = data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000769 terminal->data_attr = data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000770 terminal->tab_ruler = tab_ruler;
771 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500772
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000773 /* Update the window size */
774 ws.ws_row = terminal->height;
775 ws.ws_col = terminal->width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500776 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500777 ws.ws_xpixel = allocation.width;
778 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000779 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500780}
781
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500782static void
783resize_handler(struct widget *widget,
784 int32_t width, int32_t height, void *data)
785{
786 struct terminal *terminal = data;
787 int32_t columns, rows, m;
788
Alexander Preisingere2b88c02012-06-18 20:59:26 +0200789 if (width < 200)
790 width = 200;
791
792 if (height < 50)
793 height = 50;
794
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500795 m = 2 * terminal->margin;
796 columns = (width - m) / (int32_t) terminal->extents.max_x_advance;
797 rows = (height - m) / (int32_t) terminal->extents.height;
798
799 if (!terminal->fullscreen) {
800 width = columns * terminal->extents.max_x_advance + m;
801 height = rows * terminal->extents.height + m;
802 widget_set_size(terminal->widget, width, height);
803 }
804
805 terminal_resize_cells(terminal, columns, rows);
806}
807
808static void
809terminal_resize(struct terminal *terminal, int columns, int rows)
810{
811 int32_t width, height, m;
812
813 if (terminal->fullscreen)
814 return;
815
816 m = 2 * terminal->margin;
817 width = columns * terminal->extents.max_x_advance + m;
818 height = rows * terminal->extents.height + m;
Kristian Høgsberga1627922012-06-20 17:30:03 -0400819
820 frame_set_child_size(terminal->widget, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500821}
822
Callum Lowcay30eeae52011-01-07 19:46:55 +0000823struct color_scheme DEFAULT_COLORS = {
824 {
825 {0, 0, 0, 1}, /* black */
826 {0.66, 0, 0, 1}, /* red */
827 {0 , 0.66, 0, 1}, /* green */
828 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
829 {0 , 0 , 0.66, 1}, /* blue */
830 {0.66, 0 , 0.66, 1}, /* magenta */
831 {0, 0.66, 0.66, 1}, /* cyan */
832 {0.66, 0.66, 0.66, 1}, /* light grey */
833 {0.22, 0.33, 0.33, 1}, /* dark grey */
834 {1, 0.33, 0.33, 1}, /* high red */
835 {0.33, 1, 0.33, 1}, /* high green */
836 {1, 1, 0.33, 1}, /* high yellow */
837 {0.33, 0.33, 1, 1}, /* high blue */
838 {1, 0.33, 1, 1}, /* high magenta */
839 {0.33, 1, 1, 1}, /* high cyan */
840 {1, 1, 1, 1} /* white */
841 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500842 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000843 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
844};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400845
Kristian Høgsberg22106762008-12-08 13:50:07 -0500846static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500847terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
848{
849 cairo_set_source_rgba(cr,
850 terminal->color_table[index].r,
851 terminal->color_table[index].g,
852 terminal->color_table[index].b,
853 terminal->color_table[index].a);
854}
855
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500856static void
857terminal_send_selection(struct terminal *terminal, int fd)
858{
859 int row, col;
860 union utf8_char *p_row;
861 union decoded_attr attr;
862 FILE *fp;
863 int len;
864
865 fp = fdopen(fd, "w");
866 for (row = 0; row < terminal->height; row++) {
867 p_row = terminal_get_row(terminal, row);
868 for (col = 0; col < terminal->width; col++) {
869 /* get the attributes for this character cell */
870 terminal_decode_attr(terminal, row, col, &attr);
871 if (!attr.attr.s)
872 continue;
873 len = strnlen((char *) p_row[col].byte, 4);
874 fwrite(p_row[col].byte, 1, len, fp);
875 }
876 }
877 fclose(fp);
878}
879
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500880struct glyph_run {
881 struct terminal *terminal;
882 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400883 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500884 union decoded_attr attr;
885 cairo_glyph_t glyphs[256], *g;
886};
887
888static void
889glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
890{
891 run->terminal = terminal;
892 run->cr = cr;
893 run->g = run->glyphs;
894 run->count = 0;
895 run->attr.key = 0;
896}
897
898static void
899glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
900{
901 cairo_scaled_font_t *font;
902
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500903 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
904 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300905 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500906 font = run->terminal->font_bold;
907 else
908 font = run->terminal->font_normal;
909 cairo_set_scaled_font(run->cr, font);
910 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300911 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500912
Callum Lowcay9d708b02011-01-12 20:06:17 +1300913 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
914 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500915 run->g = run->glyphs;
916 run->count = 0;
917 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300918 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500919}
920
921static void
922glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
923{
924 int num_glyphs;
925 cairo_scaled_font_t *font;
926
927 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
928
Callum Lowcay9d708b02011-01-12 20:06:17 +1300929 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500930 font = run->terminal->font_bold;
931 else
932 font = run->terminal->font_normal;
933
934 cairo_move_to(run->cr, x, y);
935 cairo_scaled_font_text_to_glyphs (font, x, y,
936 (char *) c->byte, 4,
937 &run->g, &num_glyphs,
938 NULL, NULL, NULL);
939 run->g += num_glyphs;
940 run->count += num_glyphs;
941}
942
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500943
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500944static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500945redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500946{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500947 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500948 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500949 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000950 int top_margin, side_margin;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600951 int row, col, cursor_x, cursor_y;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000952 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500953 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000954 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500955 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500956 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500957 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500958 cairo_font_extents_t extents;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500959
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500960 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500961 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500962 cr = cairo_create(surface);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500963 cairo_rectangle(cr, allocation.x, allocation.y,
964 allocation.width, allocation.height);
965 cairo_clip(cr);
966 cairo_push_group(cr);
967
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500968 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500969 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500970 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500971
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500972 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500973
Kristian Høgsberg59826582011-01-20 11:56:57 -0500974 extents = terminal->extents;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500975 side_margin = (allocation.width - terminal->width * extents.max_x_advance) / 2;
976 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500977
Callum Lowcay30eeae52011-01-07 19:46:55 +0000978 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500979 cairo_translate(cr, allocation.x + side_margin,
980 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500981 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000982 for (row = 0; row < terminal->height; row++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000983 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000984 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500985 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000986
Callum Lowcay9d708b02011-01-12 20:06:17 +1300987 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -0500988 continue;
989
Callum Lowcay9d708b02011-01-12 20:06:17 +1300990 terminal_set_color(terminal, cr, attr.attr.bg);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500991 cairo_move_to(cr, col * extents.max_x_advance,
992 row * extents.height);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000993 cairo_rel_line_to(cr, extents.max_x_advance, 0);
994 cairo_rel_line_to(cr, 0, extents.height);
995 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
996 cairo_close_path(cr);
997 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500998 }
999 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001000
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001001 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1002
1003 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001004 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001005 for (row = 0; row < terminal->height; row++) {
1006 p_row = terminal_get_row(terminal, row);
1007 for (col = 0; col < terminal->width; col++) {
1008 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -05001009 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001010
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001011 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001012
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001013 text_x = col * extents.max_x_advance;
1014 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +13001015 if (attr.attr.a & ATTRMASK_UNDERLINE) {
1016 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +00001017 cairo_move_to(cr, text_x, (double)text_y + 1.5);
1018 cairo_line_to(cr, text_x + extents.max_x_advance, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001019 cairo_stroke(cr);
1020 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -05001021
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001022 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001023 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001024 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001025
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001026 attr.key = ~0;
1027 glyph_run_flush(&run, attr);
1028
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001029 if ((terminal->mode & MODE_SHOW_CURSOR) && !terminal->focused) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001030 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001031
Callum Lowcay30eeae52011-01-07 19:46:55 +00001032 cairo_set_line_width(cr, 1);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001033 cairo_move_to(cr, terminal->column * extents.max_x_advance + d,
1034 terminal->row * extents.height + d);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001035 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
1036 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
1037 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
1038 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001039
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001040 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001041 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001042
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001043 cairo_pop_group_to_source(cr);
1044 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001045 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001046 cairo_surface_destroy(surface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001047
1048 if (terminal->send_cursor_position) {
1049 cursor_x = side_margin + allocation.x +
1050 terminal->column * extents.max_x_advance;
1051 cursor_y = top_margin + allocation.y +
1052 terminal->row * extents.height;
1053 window_set_text_cursor_position(terminal->window,
1054 cursor_x, cursor_y);
1055 terminal->send_cursor_position = 0;
1056 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001057}
1058
1059static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001060terminal_write(struct terminal *terminal, const char *data, size_t length)
1061{
1062 if (write(terminal->master, data, length) < 0)
1063 abort();
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001064 terminal->send_cursor_position = 1;
Kristian Høgsberg26130862011-08-24 11:30:21 -04001065}
1066
1067static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001068terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001069
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001070static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001071handle_char(struct terminal *terminal, union utf8_char utf8);
1072
1073static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001074handle_sgr(struct terminal *terminal, int code);
1075
1076static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001077handle_term_parameter(struct terminal *terminal, int code, int sr)
1078{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001079 int i;
1080
Callum Lowcay67a201d2011-01-12 19:23:41 +13001081 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001082 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001083 case 1: /* DECCKM */
1084 if (sr) terminal->key_mode = KM_APPLICATION;
1085 else terminal->key_mode = KM_NORMAL;
1086 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001087 case 2: /* DECANM */
1088 /* No VT52 support yet */
1089 terminal->g0 = CS_US;
1090 terminal->g1 = CS_US;
1091 terminal->cs = terminal->g0;
1092 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001093 case 3: /* DECCOLM */
1094 if (sr)
1095 terminal_resize(terminal, 132, 24);
1096 else
1097 terminal_resize(terminal, 80, 24);
1098
1099 /* set columns, but also home cursor and clear screen */
1100 terminal->row = 0; terminal->column = 0;
1101 for (i = 0; i < terminal->height; i++) {
1102 memset(terminal_get_row(terminal, i),
1103 0, terminal->data_pitch);
1104 attr_init(terminal_get_attr_row(terminal, i),
1105 terminal->curr_attr, terminal->width);
1106 }
1107 break;
1108 case 5: /* DECSCNM */
1109 if (sr) terminal->mode |= MODE_INVERSE;
1110 else terminal->mode &= ~MODE_INVERSE;
1111 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001112 case 6: /* DECOM */
1113 terminal->origin_mode = sr;
1114 if (terminal->origin_mode)
1115 terminal->row = terminal->margin_top;
1116 else
1117 terminal->row = 0;
1118 terminal->column = 0;
1119 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001120 case 7: /* DECAWM */
1121 if (sr) terminal->mode |= MODE_AUTOWRAP;
1122 else terminal->mode &= ~MODE_AUTOWRAP;
1123 break;
1124 case 8: /* DECARM */
1125 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1126 else terminal->mode &= ~MODE_AUTOREPEAT;
1127 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001128 case 12: /* Very visible cursor (CVVIS) */
1129 /* FIXME: What do we do here. */
1130 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001131 case 25:
1132 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1133 else terminal->mode &= ~MODE_SHOW_CURSOR;
1134 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001135 case 1034: /* smm/rmm, meta mode on/off */
1136 /* ignore */
1137 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001138 case 1037: /* deleteSendsDel */
1139 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1140 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1141 break;
1142 case 1039: /* altSendsEscape */
1143 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1144 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1145 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001146 case 1049: /* rmcup/smcup, alternate screen */
1147 /* Ignore. Should be possible to implement,
1148 * but it's kind of annoying. */
1149 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001150 default:
1151 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1152 break;
1153 }
1154 } else {
1155 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001156 case 4: /* IRM */
1157 if (sr) terminal->mode |= MODE_IRM;
1158 else terminal->mode &= ~MODE_IRM;
1159 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001160 case 20: /* LNM */
1161 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1162 else terminal->mode &= ~MODE_LF_NEWLINE;
1163 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001164 default:
1165 fprintf(stderr, "Unknown parameter: %d\n", code);
1166 break;
1167 }
1168 }
1169}
1170
1171static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001172handle_dcs(struct terminal *terminal)
1173{
1174}
1175
1176static void
1177handle_osc(struct terminal *terminal)
1178{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001179 char *p;
1180 int code;
1181
1182 terminal->escape[terminal->escape_length++] = '\0';
1183 p = &terminal->escape[2];
1184 code = strtol(p, &p, 10);
1185 if (*p == ';') p++;
1186
1187 switch (code) {
1188 case 0: /* Icon name and window title */
1189 case 1: /* Icon label */
1190 case 2: /* Window title*/
1191 window_set_title(terminal->window, p);
1192 break;
1193 default:
1194 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1195 break;
1196 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001197}
1198
1199static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001200handle_escape(struct terminal *terminal)
1201{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001202 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001203 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001204 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001205 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001206 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001207 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001208 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001209
1210 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001211 i = 0;
1212 p = &terminal->escape[2];
1213 while ((isdigit(*p) || *p == ';') && i < 10) {
1214 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001215 if (!set[i]) {
1216 args[i] = 0;
1217 set[i] = 1;
1218 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001219 p++;
1220 i++;
1221 } else {
1222 args[i] = strtol(p, &p, 10);
1223 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001224 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001225 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001226
1227 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001228 case '@': /* ICH */
1229 count = set[0] ? args[0] : 1;
1230 if (count == 0) count = 1;
1231 terminal_shift_line(terminal, count);
1232 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001233 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001234 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001235 if (count == 0) count = 1;
1236 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001237 terminal->row -= count;
1238 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001239 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001240 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001241 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001242 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001243 if (count == 0) count = 1;
1244 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001245 terminal->row += count;
1246 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001247 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001248 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001249 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001250 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001251 if (count == 0) count = 1;
1252 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001253 terminal->column += count;
1254 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001255 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001256 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001257 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001258 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001259 if (count == 0) count = 1;
1260 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001261 terminal->column -= count;
1262 else
1263 terminal->column = 0;
1264 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001265 case 'E': /* CNL */
1266 count = set[0] ? args[0] : 1;
1267 if (terminal->row + count <= terminal->margin_bottom)
1268 terminal->row += count;
1269 else
1270 terminal->row = terminal->margin_bottom;
1271 terminal->column = 0;
1272 break;
1273 case 'F': /* CPL */
1274 count = set[0] ? args[0] : 1;
1275 if (terminal->row - count >= terminal->margin_top)
1276 terminal->row -= count;
1277 else
1278 terminal->row = terminal->margin_top;
1279 terminal->column = 0;
1280 break;
1281 case 'G': /* CHA */
1282 y = set[0] ? args[0] : 1;
1283 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1284
1285 terminal->column = y - 1;
1286 break;
1287 case 'f': /* HVP */
1288 case 'H': /* CUP */
1289 x = (set[1] ? args[1] : 1) - 1;
1290 x = x < 0 ? 0 :
1291 (x >= terminal->width ? terminal->width - 1 : x);
1292
1293 y = (set[0] ? args[0] : 1) - 1;
1294 if (terminal->origin_mode) {
1295 y += terminal->margin_top;
1296 y = y < terminal->margin_top ? terminal->margin_top :
1297 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1298 } else {
1299 y = y < 0 ? 0 :
1300 (y >= terminal->height ? terminal->height - 1 : y);
1301 }
1302
1303 terminal->row = y;
1304 terminal->column = x;
1305 break;
1306 case 'I': /* CHT */
1307 count = set[0] ? args[0] : 1;
1308 if (count == 0) count = 1;
1309 while (count > 0 && terminal->column < terminal->width) {
1310 if (terminal->tab_ruler[terminal->column]) count--;
1311 terminal->column++;
1312 }
1313 terminal->column--;
1314 break;
1315 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001316 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001317 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001318 if (!set[0] || args[0] == 0 || args[0] > 2) {
1319 memset(&row[terminal->column],
1320 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1321 attr_init(&attr_row[terminal->column],
1322 terminal->curr_attr, terminal->width - terminal->column);
1323 for (i = terminal->row + 1; i < terminal->height; i++) {
1324 memset(terminal_get_row(terminal, i),
1325 0, terminal->data_pitch);
1326 attr_init(terminal_get_attr_row(terminal, i),
1327 terminal->curr_attr, terminal->width);
1328 }
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 for (i = 0; i < terminal->row; i++) {
1333 memset(terminal_get_row(terminal, i),
1334 0, terminal->data_pitch);
1335 attr_init(terminal_get_attr_row(terminal, i),
1336 terminal->curr_attr, terminal->width);
1337 }
1338 } else if (args[0] == 2) {
1339 for (i = 0; i < terminal->height; i++) {
1340 memset(terminal_get_row(terminal, i),
1341 0, terminal->data_pitch);
1342 attr_init(terminal_get_attr_row(terminal, i),
1343 terminal->curr_attr, terminal->width);
1344 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001345 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001346 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001347 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001348 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001349 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001350 if (!set[0] || args[0] == 0 || args[0] > 2) {
1351 memset(&row[terminal->column], 0,
1352 (terminal->width - terminal->column) * sizeof(union utf8_char));
1353 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1354 terminal->width - terminal->column);
1355 } else if (args[0] == 1) {
1356 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1357 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1358 } else if (args[0] == 2) {
1359 memset(row, 0, terminal->data_pitch);
1360 attr_init(attr_row, terminal->curr_attr, terminal->width);
1361 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001362 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001363 case 'L': /* IL */
1364 count = set[0] ? args[0] : 1;
1365 if (count == 0) count = 1;
1366 if (terminal->row >= terminal->margin_top &&
1367 terminal->row < terminal->margin_bottom)
1368 {
1369 top = terminal->margin_top;
1370 terminal->margin_top = terminal->row;
1371 terminal_scroll(terminal, 0 - count);
1372 terminal->margin_top = top;
1373 } else if (terminal->row == terminal->margin_bottom) {
1374 memset(terminal_get_row(terminal, terminal->row),
1375 0, terminal->data_pitch);
1376 attr_init(terminal_get_attr_row(terminal, terminal->row),
1377 terminal->curr_attr, terminal->width);
1378 }
1379 break;
1380 case 'M': /* DL */
1381 count = set[0] ? args[0] : 1;
1382 if (count == 0) count = 1;
1383 if (terminal->row >= terminal->margin_top &&
1384 terminal->row < terminal->margin_bottom)
1385 {
1386 top = terminal->margin_top;
1387 terminal->margin_top = terminal->row;
1388 terminal_scroll(terminal, count);
1389 terminal->margin_top = top;
1390 } else if (terminal->row == terminal->margin_bottom) {
1391 memset(terminal_get_row(terminal, terminal->row),
1392 0, terminal->data_pitch);
1393 }
1394 break;
1395 case 'P': /* DCH */
1396 count = set[0] ? args[0] : 1;
1397 if (count == 0) count = 1;
1398 terminal_shift_line(terminal, 0 - count);
1399 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001400 case 'S': /* SU */
1401 terminal_scroll(terminal, set[0] ? args[0] : 1);
1402 break;
1403 case 'T': /* SD */
1404 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1405 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001406 case 'X': /* ECH */
1407 count = set[0] ? args[0] : 1;
1408 if (count == 0) count = 1;
1409 if ((terminal->column + count) > terminal->width)
1410 count = terminal->width - terminal->column;
1411 row = terminal_get_row(terminal, terminal->row);
1412 attr_row = terminal_get_attr_row(terminal, terminal->row);
1413 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1414 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1415 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001416 case 'Z': /* CBT */
1417 count = set[0] ? args[0] : 1;
1418 if (count == 0) count = 1;
1419 while (count > 0 && terminal->column >= 0) {
1420 if (terminal->tab_ruler[terminal->column]) count--;
1421 terminal->column--;
1422 }
1423 terminal->column++;
1424 break;
1425 case '`': /* HPA */
1426 y = set[0] ? args[0] : 1;
1427 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1428
1429 terminal->column = y - 1;
1430 break;
1431 case 'b': /* REP */
1432 count = set[0] ? args[0] : 1;
1433 if (count == 0) count = 1;
1434 if (terminal->last_char.byte[0])
1435 for (i = 0; i < count; i++)
1436 handle_char(terminal, terminal->last_char);
1437 terminal->last_char.byte[0] = 0;
1438 break;
1439 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001440 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001441 break;
1442 case 'd': /* VPA */
1443 x = set[0] ? args[0] : 1;
1444 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1445
1446 terminal->row = x - 1;
1447 break;
1448 case 'g': /* TBC */
1449 if (!set[0] || args[0] == 0) {
1450 terminal->tab_ruler[terminal->column] = 0;
1451 } else if (args[0] == 3) {
1452 memset(terminal->tab_ruler, 0, terminal->width);
1453 }
1454 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001455 case 'h': /* SM */
1456 for(i = 0; i < 10 && set[i]; i++) {
1457 handle_term_parameter(terminal, args[i], 1);
1458 }
1459 break;
1460 case 'l': /* RM */
1461 for(i = 0; i < 10 && set[i]; i++) {
1462 handle_term_parameter(terminal, args[i], 0);
1463 }
1464 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001465 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001466 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001467 if (i <= 7 && set[i] && set[i + 1] &&
1468 set[i + 2] && args[i + 1] == 5)
1469 {
1470 if (args[i] == 38) {
1471 handle_sgr(terminal, args[i + 2] + 256);
1472 break;
1473 } else if (args[i] == 48) {
1474 handle_sgr(terminal, args[i + 2] + 512);
1475 break;
1476 }
1477 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001478 if(set[i]) {
1479 handle_sgr(terminal, args[i]);
1480 } else if(i == 0) {
1481 handle_sgr(terminal, 0);
1482 break;
1483 } else {
1484 break;
1485 }
1486 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001487 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001488 case 'n': /* DSR */
1489 i = set[0] ? args[0] : 0;
1490 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001491 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001492 } else if (i == 6) {
1493 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1494 terminal->origin_mode ?
1495 terminal->row+terminal->margin_top : terminal->row+1,
1496 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001497 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001498 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001499 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001500 case 'r':
1501 if(!set[0]) {
1502 terminal->margin_top = 0;
1503 terminal->margin_bottom = terminal->height-1;
1504 terminal->row = 0;
1505 terminal->column = 0;
1506 } else {
1507 top = (set[0] ? args[0] : 1) - 1;
1508 top = top < 0 ? 0 :
1509 (top >= terminal->height ? terminal->height - 1 : top);
1510 bottom = (set[1] ? args[1] : 1) - 1;
1511 bottom = bottom < 0 ? 0 :
1512 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1513 if(bottom > top) {
1514 terminal->margin_top = top;
1515 terminal->margin_bottom = bottom;
1516 } else {
1517 terminal->margin_top = 0;
1518 terminal->margin_bottom = terminal->height-1;
1519 }
1520 if(terminal->origin_mode)
1521 terminal->row = terminal->margin_top;
1522 else
1523 terminal->row = 0;
1524 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001525 }
1526 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001527 case 's':
1528 terminal->saved_row = terminal->row;
1529 terminal->saved_column = terminal->column;
1530 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001531 case 't': /* windowOps */
1532 if (!set[0]) break;
1533 switch (args[0]) {
1534 case 4: /* resize px */
1535 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001536 widget_schedule_resize(terminal->widget,
1537 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001538 }
1539 break;
1540 case 8: /* resize ch */
1541 if (set[1] && set[2]) {
1542 terminal_resize(terminal, args[2], args[1]);
1543 }
1544 break;
1545 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001546 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001547 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1548 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001549 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001550 break;
1551 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001552 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001553 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1554 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001555 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001556 break;
1557 case 18: /* report ch */
1558 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1559 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001560 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001561 break;
1562 case 21: /* report title */
1563 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1564 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001565 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001566 break;
1567 default:
1568 if (args[0] >= 24)
1569 terminal_resize(terminal, terminal->width, args[0]);
1570 else
1571 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1572 break;
1573 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001574 case 'u':
1575 terminal->row = terminal->saved_row;
1576 terminal->column = terminal->saved_column;
1577 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001578 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001579 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001580 break;
1581 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001582}
1583
1584static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001585handle_non_csi_escape(struct terminal *terminal, char code)
1586{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001587 switch(code) {
1588 case 'M': /* RI */
1589 terminal->row -= 1;
1590 if(terminal->row < terminal->margin_top) {
1591 terminal->row = terminal->margin_top;
1592 terminal_scroll(terminal, -1);
1593 }
1594 break;
1595 case 'E': /* NEL */
1596 terminal->column = 0;
1597 // fallthrough
1598 case 'D': /* IND */
1599 terminal->row += 1;
1600 if(terminal->row > terminal->margin_bottom) {
1601 terminal->row = terminal->margin_bottom;
1602 terminal_scroll(terminal, +1);
1603 }
1604 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001605 case 'c': /* RIS */
1606 terminal_init(terminal);
1607 break;
1608 case 'H': /* HTS */
1609 terminal->tab_ruler[terminal->column] = 1;
1610 break;
1611 case '7': /* DECSC */
1612 terminal->saved_row = terminal->row;
1613 terminal->saved_column = terminal->column;
1614 terminal->saved_attr = terminal->curr_attr;
1615 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001616 terminal->saved_cs = terminal->cs;
1617 terminal->saved_g0 = terminal->g0;
1618 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001619 break;
1620 case '8': /* DECRC */
1621 terminal->row = terminal->saved_row;
1622 terminal->column = terminal->saved_column;
1623 terminal->curr_attr = terminal->saved_attr;
1624 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001625 terminal->cs = terminal->saved_cs;
1626 terminal->g0 = terminal->saved_g0;
1627 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001628 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001629 case '=': /* DECPAM */
1630 terminal->key_mode = KM_APPLICATION;
1631 break;
1632 case '>': /* DECPNM */
1633 terminal->key_mode = KM_NORMAL;
1634 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001635 default:
1636 fprintf(stderr, "Unknown escape code: %c\n", code);
1637 break;
1638 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001639}
1640
1641static void
1642handle_special_escape(struct terminal *terminal, char special, char code)
1643{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001644 int i, numChars;
1645
1646 if (special == '#') {
1647 switch(code) {
1648 case '8':
1649 /* fill with 'E', no cheap way to do this */
1650 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1651 numChars = terminal->width * terminal->height;
1652 for(i = 0; i < numChars; i++) {
1653 terminal->data[i].byte[0] = 'E';
1654 }
1655 break;
1656 default:
1657 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1658 break;
1659 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001660 } else if (special == '(' || special == ')') {
1661 switch(code) {
1662 case '0':
1663 if (special == '(')
1664 terminal->g0 = CS_SPECIAL;
1665 else
1666 terminal->g1 = CS_SPECIAL;
1667 break;
1668 case 'A':
1669 if (special == '(')
1670 terminal->g0 = CS_UK;
1671 else
1672 terminal->g1 = CS_UK;
1673 break;
1674 case 'B':
1675 if (special == '(')
1676 terminal->g0 = CS_US;
1677 else
1678 terminal->g1 = CS_US;
1679 break;
1680 default:
1681 fprintf(stderr, "Unknown character set %c\n", code);
1682 break;
1683 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001684 } else {
1685 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1686 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001687}
1688
1689static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001690handle_sgr(struct terminal *terminal, int code)
1691{
1692 switch(code) {
1693 case 0:
1694 terminal->curr_attr = terminal->color_scheme->default_attr;
1695 break;
1696 case 1:
1697 terminal->curr_attr.a |= ATTRMASK_BOLD;
1698 if (terminal->curr_attr.fg < 8)
1699 terminal->curr_attr.fg += 8;
1700 break;
1701 case 4:
1702 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1703 break;
1704 case 5:
1705 terminal->curr_attr.a |= ATTRMASK_BLINK;
1706 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001707 case 8:
1708 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1709 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001710 case 2:
1711 case 21:
1712 case 22:
1713 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1714 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1715 terminal->curr_attr.fg -= 8;
1716 break;
1717 case 24:
1718 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1719 break;
1720 case 25:
1721 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1722 break;
1723 case 7:
1724 case 26:
1725 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1726 break;
1727 case 27:
1728 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1729 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001730 case 28:
1731 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1732 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001733 case 39:
1734 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1735 break;
1736 case 49:
1737 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1738 break;
1739 default:
1740 if(code >= 30 && code <= 37) {
1741 terminal->curr_attr.fg = code - 30;
1742 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1743 terminal->curr_attr.fg += 8;
1744 } else if(code >= 40 && code <= 47) {
1745 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001746 } else if (code >= 90 && code <= 97) {
1747 terminal->curr_attr.fg = code - 90 + 8;
1748 } else if (code >= 100 && code <= 107) {
1749 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001750 } else if(code >= 256 && code < 512) {
1751 terminal->curr_attr.fg = code - 256;
1752 } else if(code >= 512 && code < 768) {
1753 terminal->curr_attr.bg = code - 512;
1754 } else {
1755 fprintf(stderr, "Unknown SGR code: %d\n", code);
1756 }
1757 break;
1758 }
1759}
1760
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001761/* Returns 1 if c was special, otherwise 0 */
1762static int
1763handle_special_char(struct terminal *terminal, char c)
1764{
1765 union utf8_char *row;
1766 struct attr *attr_row;
1767
1768 row = terminal_get_row(terminal, terminal->row);
1769 attr_row = terminal_get_attr_row(terminal, terminal->row);
1770
1771 switch(c) {
1772 case '\r':
1773 terminal->column = 0;
1774 break;
1775 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001776 if (terminal->mode & MODE_LF_NEWLINE) {
1777 terminal->column = 0;
1778 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001779 /* fallthrough */
1780 case '\v':
1781 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001782 terminal->row++;
1783 if(terminal->row > terminal->margin_bottom) {
1784 terminal->row = terminal->margin_bottom;
1785 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001786 }
1787
1788 break;
1789 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001790 while (terminal->column < terminal->width) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001791 if (terminal->mode & MODE_IRM)
1792 terminal_shift_line(terminal, +1);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001793 row[terminal->column].byte[0] = ' ';
1794 row[terminal->column].byte[1] = '\0';
1795 attr_row[terminal->column] = terminal->curr_attr;
1796 terminal->column++;
Kristian Høgsbergcca3c2f2012-06-20 15:56:13 -04001797 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001798 }
1799 if (terminal->column >= terminal->width) {
1800 terminal->column = terminal->width - 1;
1801 }
1802
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001803 break;
1804 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001805 if (terminal->column >= terminal->width) {
1806 terminal->column = terminal->width - 2;
1807 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001808 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001809 } else if (terminal->mode & MODE_AUTOWRAP) {
1810 terminal->column = terminal->width - 1;
1811 terminal->row -= 1;
1812 if (terminal->row < terminal->margin_top) {
1813 terminal->row = terminal->margin_top;
1814 terminal_scroll(terminal, -1);
1815 }
1816 }
1817
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001818 break;
1819 case '\a':
1820 /* Bell */
1821 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001822 case '\x0E': /* SO */
1823 terminal->cs = terminal->g1;
1824 break;
1825 case '\x0F': /* SI */
1826 terminal->cs = terminal->g0;
1827 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001828 default:
1829 return 0;
1830 }
1831
1832 return 1;
1833}
1834
1835static void
1836handle_char(struct terminal *terminal, union utf8_char utf8)
1837{
1838 union utf8_char *row;
1839 struct attr *attr_row;
1840
1841 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001842
1843 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001844
1845 /* There are a whole lot of non-characters, control codes,
1846 * and formatting codes that should probably be ignored,
1847 * for example: */
1848 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1849 /* BOM, ignore */
1850 return;
1851 }
1852
1853 /* Some of these non-characters should be translated, e.g.: */
1854 if (utf8.byte[0] < 32) {
1855 utf8.byte[0] = utf8.byte[0] + 64;
1856 }
1857
1858 /* handle right margin effects */
1859 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001860 if (terminal->mode & MODE_AUTOWRAP) {
1861 terminal->column = 0;
1862 terminal->row += 1;
1863 if (terminal->row > terminal->margin_bottom) {
1864 terminal->row = terminal->margin_bottom;
1865 terminal_scroll(terminal, +1);
1866 }
1867 } else {
1868 terminal->column--;
1869 }
1870 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001871
1872 row = terminal_get_row(terminal, terminal->row);
1873 attr_row = terminal_get_attr_row(terminal, terminal->row);
1874
Callum Lowcay69e96582011-01-07 19:47:00 +00001875 if (terminal->mode & MODE_IRM)
1876 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001877 row[terminal->column] = utf8;
1878 attr_row[terminal->column++] = terminal->curr_attr;
1879
1880 if (utf8.ch != terminal->last_char.ch)
1881 terminal->last_char = utf8;
1882}
1883
Callum Lowcay30eeae52011-01-07 19:46:55 +00001884static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001885escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1886{
1887 int len, i;
1888
1889 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1890 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1891 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1892 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1893 else len = 1; /* Invalid, cannot happen */
1894
1895 if (terminal->escape_length + len <= MAX_ESCAPE) {
1896 for (i = 0; i < len; i++)
1897 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1898 terminal->escape_length += len;
1899 } else if (terminal->escape_length < MAX_ESCAPE) {
1900 terminal->escape[terminal->escape_length++] = 0;
1901 }
1902}
1903
1904static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001905terminal_data(struct terminal *terminal, const char *data, size_t length)
1906{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001907 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001908 union utf8_char utf8;
1909 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001910
1911 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001912 parser_state =
1913 utf8_next_char(&terminal->state_machine, data[i]);
1914 switch(parser_state) {
1915 case utf8state_accept:
1916 utf8.ch = terminal->state_machine.s.ch;
1917 break;
1918 case utf8state_reject:
1919 /* the unicode replacement character */
1920 utf8.byte[0] = 0xEF;
1921 utf8.byte[1] = 0xBF;
1922 utf8.byte[2] = 0xBD;
1923 utf8.byte[3] = 0x00;
1924 break;
1925 default:
1926 continue;
1927 }
1928
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001929 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001930 switch (terminal->state) {
1931 case escape_state_escape:
1932 escape_append_utf8(terminal, utf8);
1933 switch (utf8.byte[0]) {
1934 case 'P': /* DCS */
1935 terminal->state = escape_state_dcs;
1936 break;
1937 case '[': /* CSI */
1938 terminal->state = escape_state_csi;
1939 break;
1940 case ']': /* OSC */
1941 terminal->state = escape_state_osc;
1942 break;
1943 case '#':
1944 case '(':
1945 case ')': /* special */
1946 terminal->state = escape_state_special;
1947 break;
1948 case '^': /* PM (not implemented) */
1949 case '_': /* APC (not implemented) */
1950 terminal->state = escape_state_ignore;
1951 break;
1952 default:
1953 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001954 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13001955 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001956 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001957 continue;
1958 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001959 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1960 /* do nothing */
1961 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001962 terminal->escape_flags |= ESC_FLAG_WHAT;
1963 } else if (utf8.byte[0] == '>') {
1964 terminal->escape_flags |= ESC_FLAG_GT;
1965 } else if (utf8.byte[0] == '!') {
1966 terminal->escape_flags |= ESC_FLAG_BANG;
1967 } else if (utf8.byte[0] == '$') {
1968 terminal->escape_flags |= ESC_FLAG_CASH;
1969 } else if (utf8.byte[0] == '\'') {
1970 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1971 } else if (utf8.byte[0] == '"') {
1972 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1973 } else if (utf8.byte[0] == ' ') {
1974 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001975 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001976 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001977 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13001978 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001979 }
1980
1981 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1982 utf8.byte[0] == '`')
1983 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001984 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001985 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001986 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001987 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001988 continue;
1989 case escape_state_inner_escape:
1990 if (utf8.byte[0] == '\\') {
1991 terminal->state = escape_state_normal;
1992 if (terminal->outer_state == escape_state_dcs) {
1993 handle_dcs(terminal);
1994 } else if (terminal->outer_state == escape_state_osc) {
1995 handle_osc(terminal);
1996 }
1997 } else if (utf8.byte[0] == '\e') {
1998 terminal->state = terminal->outer_state;
1999 escape_append_utf8(terminal, utf8);
2000 if (terminal->escape_length >= MAX_ESCAPE)
2001 terminal->state = escape_state_normal;
2002 } else {
2003 terminal->state = terminal->outer_state;
2004 if (terminal->escape_length < MAX_ESCAPE)
2005 terminal->escape[terminal->escape_length++] = '\e';
2006 escape_append_utf8(terminal, utf8);
2007 if (terminal->escape_length >= MAX_ESCAPE)
2008 terminal->state = escape_state_normal;
2009 }
2010 continue;
2011 case escape_state_dcs:
2012 case escape_state_osc:
2013 case escape_state_ignore:
2014 if (utf8.byte[0] == '\e') {
2015 terminal->outer_state = terminal->state;
2016 terminal->state = escape_state_inner_escape;
2017 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
2018 terminal->state = escape_state_normal;
2019 handle_osc(terminal);
2020 } else {
2021 escape_append_utf8(terminal, utf8);
2022 if (terminal->escape_length >= MAX_ESCAPE)
2023 terminal->state = escape_state_normal;
2024 }
2025 continue;
2026 case escape_state_special:
2027 escape_append_utf8(terminal, utf8);
2028 terminal->state = escape_state_normal;
2029 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2030 handle_special_escape(terminal, terminal->escape[1],
2031 utf8.byte[0]);
2032 }
2033 continue;
2034 default:
2035 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002036 }
2037
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002038 /* this is valid, because ASCII characters are never used to
2039 * introduce a multibyte sequence in UTF-8 */
2040 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002041 terminal->state = escape_state_escape;
2042 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002043 terminal->escape[0] = '\e';
2044 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002045 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002046 } else {
2047 handle_char(terminal, utf8);
2048 } /* if */
2049 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002050
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002051 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002052}
2053
2054static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002055data_source_target(void *data,
2056 struct wl_data_source *source, const char *mime_type)
2057{
2058 fprintf(stderr, "data_source_target, %s\n", mime_type);
2059}
2060
2061static void
2062data_source_send(void *data,
2063 struct wl_data_source *source,
2064 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002065{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002066 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002067
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002068 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002069}
2070
2071static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002072data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002073{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002074 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002075}
2076
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002077static const struct wl_data_source_listener data_source_listener = {
2078 data_source_target,
2079 data_source_send,
2080 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002081};
2082
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002083static int
2084handle_bound_key(struct terminal *terminal,
2085 struct input *input, uint32_t sym, uint32_t time)
2086{
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002087 struct terminal *new_terminal;
2088
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002089 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002090 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002091 /* Cut selection; terminal doesn't do cut, fall
2092 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002093 case XKB_KEY_C:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002094 terminal->selection =
2095 display_create_data_source(terminal->display);
2096 wl_data_source_offer(terminal->selection,
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002097 "text/plain;charset=utf-8");
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002098 wl_data_source_add_listener(terminal->selection,
2099 &data_source_listener, terminal);
Kristian Høgsbergfee91be2012-06-02 21:21:41 -04002100 input_set_selection(input, terminal->selection,
2101 display_get_serial(terminal->display));
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002102 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002103 case XKB_KEY_V:
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002104 input_receive_selection_data_to_fd(input,
2105 "text/plain;charset=utf-8",
2106 terminal->master);
2107
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002108 return 1;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002109
2110 case XKB_KEY_N:
2111 new_terminal =
2112 terminal_create(terminal->display, option_fullscreen);
2113 if (terminal_run(new_terminal, option_shell))
2114 terminal_destroy(new_terminal);
2115
2116 return 1;
2117
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002118 default:
2119 return 0;
2120 }
2121}
2122
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002123static void
2124key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002125 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2126 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002127{
2128 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002129 char ch[MAX_RESPONSE];
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002130 uint32_t modifiers;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002131 int len = 0;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002132
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002133 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002134 if ((modifiers & MOD_CONTROL_MASK) &&
2135 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002136 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2137 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002138 return;
2139
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002140 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002141 case XKB_KEY_F11:
Daniel Stonec9785ea2012-05-30 16:31:52 +01002142 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002143 break;
2144 terminal->fullscreen ^= 1;
2145 window_set_fullscreen(window, terminal->fullscreen);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002146 break;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002147
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002148 case XKB_KEY_BackSpace:
Kristian Høgsberg71a4cf42012-06-20 17:57:56 -04002149 ch[len++] = 0x7f;
2150 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002151 case XKB_KEY_Tab:
2152 case XKB_KEY_Linefeed:
2153 case XKB_KEY_Clear:
2154 case XKB_KEY_Pause:
2155 case XKB_KEY_Scroll_Lock:
2156 case XKB_KEY_Sys_Req:
2157 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002158 ch[len++] = sym & 0x7f;
2159 break;
2160
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002161 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002162 if (terminal->mode & MODE_LF_NEWLINE) {
2163 ch[len++] = 0x0D;
2164 ch[len++] = 0x0A;
2165 } else {
2166 ch[len++] = 0x0D;
2167 }
2168 break;
2169
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002170 case XKB_KEY_Shift_L:
2171 case XKB_KEY_Shift_R:
2172 case XKB_KEY_Control_L:
2173 case XKB_KEY_Control_R:
2174 case XKB_KEY_Alt_L:
2175 case XKB_KEY_Alt_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002176 break;
2177
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002178 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002179 len = function_key_response('[', 2, modifiers, '~', ch);
2180 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002181 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002182 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2183 ch[len++] = '\x04';
2184 } else {
2185 len = function_key_response('[', 3, modifiers, '~', ch);
2186 }
2187 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002188 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002189 len = function_key_response('[', 5, modifiers, '~', ch);
2190 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002191 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002192 len = function_key_response('[', 6, modifiers, '~', ch);
2193 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002194 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002195 len = function_key_response('O', 1, modifiers, 'P', ch);
2196 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002197 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002198 len = function_key_response('O', 1, modifiers, 'Q', ch);
2199 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002200 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002201 len = function_key_response('O', 1, modifiers, 'R', ch);
2202 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002203 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002204 len = function_key_response('O', 1, modifiers, 'S', ch);
2205 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002206 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002207 len = function_key_response('[', 15, modifiers, '~', ch);
2208 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002209 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002210 len = function_key_response('[', 17, modifiers, '~', ch);
2211 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002212 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002213 len = function_key_response('[', 18, modifiers, '~', ch);
2214 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002215 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002216 len = function_key_response('[', 19, modifiers, '~', ch);
2217 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002218 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002219 len = function_key_response('[', 20, modifiers, '~', ch);
2220 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002221 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002222 len = function_key_response('[', 21, modifiers, '~', ch);
2223 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002224 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002225 len = function_key_response('[', 24, modifiers, '~', ch);
2226 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002227 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002228 /* Handle special keys with alternate mappings */
2229 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2230 if (len != 0) break;
2231
Kristian Høgsberg70163132012-05-08 15:55:39 -04002232 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002233 if (sym >= '3' && sym <= '7')
2234 sym = (sym & 0x1f) + 8;
2235
2236 if (!((sym >= '!' && sym <= '/') ||
2237 (sym >= '8' && sym <= '?') ||
2238 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2239 else if (sym == '2') sym = 0x00;
2240 else if (sym == '/') sym = 0x1F;
2241 else if (sym == '8' || sym == '?') sym = 0x7F;
2242 } else if ((terminal->mode & MODE_ALT_SENDS_ESC) &&
Kristian Høgsberg70163132012-05-08 15:55:39 -04002243 (modifiers & MOD_ALT_MASK))
Callum Lowcay7e08e902011-01-07 19:47:02 +00002244 {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002245 ch[len++] = 0x1b;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002246 } else if (modifiers & MOD_ALT_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002247 sym = sym | 0x80;
2248 }
2249
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002250 if (sym < 256)
2251 ch[len++] = sym;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002252 break;
2253 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002254
Daniel Stonec9785ea2012-05-30 16:31:52 +01002255 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0)
Kristian Høgsberg26130862011-08-24 11:30:21 -04002256 terminal_write(terminal, ch, len);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002257}
2258
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002259static void
2260keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002261 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002262{
2263 struct terminal *terminal = data;
2264
2265 terminal->focused = (device != NULL);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002266 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002267}
2268
Kristian Høgsberg59826582011-01-20 11:56:57 -05002269static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002270button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002271 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002272 uint32_t button,
2273 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002274{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002275 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002276
2277 switch (button) {
2278 case 272:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002279 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002280 terminal->dragging = 1;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002281 input_get_position(input,
2282 &terminal->selection_start_x,
2283 &terminal->selection_start_y);
2284 terminal->selection_end_x = terminal->selection_start_x;
2285 terminal->selection_end_y = terminal->selection_start_y;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002286 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002287 } else {
2288 terminal->dragging = 0;
2289 }
2290 break;
2291 }
2292}
2293
2294static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002295motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002296 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002297 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002298{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002299 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002300
2301 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002302 input_get_position(input,
2303 &terminal->selection_end_x,
2304 &terminal->selection_end_y);
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002305 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002306 }
2307
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002308 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002309}
2310
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002311static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002312terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002313{
2314 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002315 cairo_surface_t *surface;
2316 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002317
2318 terminal = malloc(sizeof *terminal);
2319 if (terminal == NULL)
2320 return terminal;
2321
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002322 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002323 terminal->fullscreen = fullscreen;
Callum Lowcay30eeae52011-01-07 19:46:55 +00002324 terminal->color_scheme = &DEFAULT_COLORS;
2325 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002326 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002327 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002328 terminal->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002329 terminal->widget = frame_create(terminal->window, terminal);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002330 window_set_title(terminal->window, "Wayland Terminal");
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002331 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002332
2333 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002334 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002335
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002336 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002337 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002338
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002339 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002340 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002341 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002342 keyboard_focus_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002343 widget_set_redraw_handler(terminal->widget, redraw_handler);
2344 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002345 widget_set_button_handler(terminal->widget, button_handler);
2346 widget_set_motion_handler(terminal->widget, motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002347
Kristian Høgsberg09531622010-06-14 23:22:15 -04002348 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2349 cr = cairo_create(surface);
Kristian Høgsberg09531622010-06-14 23:22:15 -04002350 cairo_set_font_size(cr, 14);
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002351 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002352 CAIRO_FONT_SLANT_NORMAL,
2353 CAIRO_FONT_WEIGHT_BOLD);
2354 terminal->font_bold = cairo_get_scaled_font (cr);
2355 cairo_scaled_font_reference(terminal->font_bold);
2356
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002357 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002358 CAIRO_FONT_SLANT_NORMAL,
2359 CAIRO_FONT_WEIGHT_NORMAL);
2360 terminal->font_normal = cairo_get_scaled_font (cr);
2361 cairo_scaled_font_reference(terminal->font_normal);
2362
Kristian Høgsberg09531622010-06-14 23:22:15 -04002363 cairo_font_extents(cr, &terminal->extents);
2364 cairo_destroy(cr);
2365 cairo_surface_destroy(surface);
2366
Kristian Høgsberga1627922012-06-20 17:30:03 -04002367 terminal_resize(terminal, 80, 25);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002368
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002369 wl_list_insert(terminal_list.prev, &terminal->link);
2370
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002371 return terminal;
2372}
2373
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002374static void
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002375terminal_destroy(struct terminal *terminal)
2376{
2377 window_destroy(terminal->window);
2378 close(terminal->master);
2379 wl_list_remove(&terminal->link);
2380 free(terminal);
2381
2382 if (wl_list_empty(&terminal_list))
2383 exit(0);
2384}
2385
2386static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002387io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002388{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002389 struct terminal *terminal =
2390 container_of(task, struct terminal, io_task);
2391 char buffer[256];
2392 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002393
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002394 if (events & EPOLLHUP) {
2395 terminal_destroy(terminal);
2396 return;
2397 }
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002398
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002399 len = read(terminal->master, buffer, sizeof buffer);
2400 if (len < 0)
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002401 terminal_destroy(terminal);
2402 else
2403 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002404}
2405
2406static int
2407terminal_run(struct terminal *terminal, const char *path)
2408{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002409 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002410 pid_t pid;
2411
2412 pid = forkpty(&master, NULL, NULL, NULL);
2413 if (pid == 0) {
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04002414 setenv("TERM", option_term, 1);
2415 setenv("COLORTERM", option_term, 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002416 if (execl(path, path, NULL)) {
2417 printf("exec failed: %m\n");
2418 exit(EXIT_FAILURE);
2419 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002420 } else if (pid < 0) {
2421 fprintf(stderr, "failed to fork and create pty (%m).\n");
2422 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002423 }
2424
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002425 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002426 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002427 terminal->io_task.run = io_handler;
2428 display_watch_fd(terminal->display, terminal->master,
2429 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002430
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002431 window_set_fullscreen(terminal->window, terminal->fullscreen);
2432 if (!terminal->fullscreen)
2433 terminal_resize(terminal, 80, 24);
2434
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002435 return 0;
2436}
2437
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002438static const struct config_key terminal_config_keys[] = {
2439 { "font", CONFIG_KEY_STRING, &option_font },
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04002440 { "term", CONFIG_KEY_STRING, &option_term },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002441};
2442
2443static const struct config_section config_sections[] = {
2444 { "terminal",
2445 terminal_config_keys, ARRAY_LENGTH(terminal_config_keys) },
2446};
2447
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002448static const struct weston_option terminal_options[] = {
2449 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002450 { WESTON_OPTION_STRING, "font", 0, &option_font },
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002451 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002452};
2453
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002454int main(int argc, char *argv[])
2455{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002456 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002457 struct terminal *terminal;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002458 char *config_file;
2459
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002460 option_shell = getenv("SHELL");
2461 if (!option_shell)
2462 option_shell = "/bin/bash";
2463
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002464 config_file = config_file_path("weston.ini");
2465 parse_config_file(config_file,
2466 config_sections, ARRAY_LENGTH(config_sections),
2467 NULL);
2468 free(config_file);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002469
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002470 argc = parse_options(terminal_options,
2471 ARRAY_LENGTH(terminal_options), argc, argv);
2472
2473 d = display_create(argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002474 if (d == NULL) {
2475 fprintf(stderr, "failed to create display: %m\n");
2476 return -1;
2477 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002478
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002479 wl_list_init(&terminal_list);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002480 terminal = terminal_create(d, option_fullscreen);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002481 if (terminal_run(terminal, option_shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002482 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002483
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002484 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002485
2486 return 0;
2487}