blob: 319f9c82ac48a41f0bd7206eafba97fbc4e6dc74 [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øgsberg0c4457f2008-12-07 19:59:11 -050038#include "window.h"
39
Kristian Høgsberg0395f302008-12-22 12:14:50 -050040static int option_fullscreen;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050041
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050042#define MOD_SHIFT 0x01
43#define MOD_ALT 0x02
44#define MOD_CTRL 0x04
45
Callum Lowcay30eeae52011-01-07 19:46:55 +000046#define ATTRMASK_BOLD 0x01
47#define ATTRMASK_UNDERLINE 0x02
48#define ATTRMASK_BLINK 0x04
49#define ATTRMASK_INVERSE 0x08
Callum Lowcay81179db2011-01-10 12:14:01 +130050#define ATTRMASK_CONCEALED 0x10
Callum Lowcay30eeae52011-01-07 19:46:55 +000051
Callum Lowcayb8609ad2011-01-07 19:46:57 +000052/* Buffer sizes */
Callum Lowcayef57a9b2011-01-14 20:46:23 +130053#define MAX_RESPONSE 256
54#define MAX_ESCAPE 255
Callum Lowcayb8609ad2011-01-07 19:46:57 +000055
Callum Lowcay8e57dd52011-01-07 19:46:59 +000056/* Terminal modes */
57#define MODE_SHOW_CURSOR 0x00000001
58#define MODE_INVERSE 0x00000002
59#define MODE_AUTOWRAP 0x00000004
60#define MODE_AUTOREPEAT 0x00000008
61#define MODE_LF_NEWLINE 0x00000010
Callum Lowcay69e96582011-01-07 19:47:00 +000062#define MODE_IRM 0x00000020
Callum Lowcay7e08e902011-01-07 19:47:02 +000063#define MODE_DELETE_SENDS_DEL 0x00000040
64#define MODE_ALT_SENDS_ESC 0x00000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +000065
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000066union utf8_char {
67 unsigned char byte[4];
68 uint32_t ch;
69};
70
71enum utf8_state {
72 utf8state_start,
73 utf8state_accept,
74 utf8state_reject,
75 utf8state_expect3,
76 utf8state_expect2,
77 utf8state_expect1
78};
79
80struct utf8_state_machine {
81 enum utf8_state state;
82 int len;
83 union utf8_char s;
84};
85
86static void
87init_state_machine(struct utf8_state_machine *machine)
88{
89 machine->state = utf8state_start;
90 machine->len = 0;
91 machine->s.ch = 0;
92}
93
94static enum utf8_state
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -040095utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000096{
97 switch(machine->state) {
98 case utf8state_start:
99 case utf8state_accept:
100 case utf8state_reject:
101 machine->s.ch = 0;
102 machine->len = 0;
103 if(c == 0xC0 || c == 0xC1) {
104 /* overlong encoding, reject */
105 machine->state = utf8state_reject;
106 } else if((c & 0x80) == 0) {
107 /* single byte, accept */
108 machine->s.byte[machine->len++] = c;
109 machine->state = utf8state_accept;
110 } else if((c & 0xC0) == 0x80) {
111 /* parser out of sync, ignore byte */
112 machine->state = utf8state_start;
113 } else if((c & 0xE0) == 0xC0) {
114 /* start of two byte sequence */
115 machine->s.byte[machine->len++] = c;
116 machine->state = utf8state_expect1;
117 } else if((c & 0xF0) == 0xE0) {
118 /* start of three byte sequence */
119 machine->s.byte[machine->len++] = c;
120 machine->state = utf8state_expect2;
121 } else if((c & 0xF8) == 0xF0) {
122 /* start of four byte sequence */
123 machine->s.byte[machine->len++] = c;
124 machine->state = utf8state_expect3;
125 } else {
126 /* overlong encoding, reject */
127 machine->state = utf8state_reject;
128 }
129 break;
130 case utf8state_expect3:
131 machine->s.byte[machine->len++] = c;
132 if((c & 0xC0) == 0x80) {
133 /* all good, continue */
134 machine->state = utf8state_expect2;
135 } else {
136 /* missing extra byte, reject */
137 machine->state = utf8state_reject;
138 }
139 break;
140 case utf8state_expect2:
141 machine->s.byte[machine->len++] = c;
142 if((c & 0xC0) == 0x80) {
143 /* all good, continue */
144 machine->state = utf8state_expect1;
145 } else {
146 /* missing extra byte, reject */
147 machine->state = utf8state_reject;
148 }
149 break;
150 case utf8state_expect1:
151 machine->s.byte[machine->len++] = c;
152 if((c & 0xC0) == 0x80) {
153 /* all good, accept */
154 machine->state = utf8state_accept;
155 } else {
156 /* missing extra byte, reject */
157 machine->state = utf8state_reject;
158 }
159 break;
160 default:
161 machine->state = utf8state_reject;
162 break;
163 }
164
165 return machine->state;
166}
167
Callum Lowcay256e72f2011-01-07 19:47:01 +0000168struct char_sub {
169 union utf8_char match;
170 union utf8_char replace;
171};
172/* Set last char_sub match to NULL char */
173typedef struct char_sub *character_set;
174
175struct char_sub CS_US[] = {
176 {{{0, }}, {{0, }}}
177};
178static struct char_sub CS_UK[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200179 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000180 {{{0, }}, {{0, }}}
181};
182static struct char_sub CS_SPECIAL[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200183 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond: ♦ */
184 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell: ▒ */
185 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT: ␉ */
186 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF: ␌ */
187 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR: ␍ */
188 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF: ␊ */
189 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree: ° */
190 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus: ± */
191 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL: ␤ */
192 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT: ␋ */
193 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB: ┘ */
194 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT: ┐ */
195 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT: ┌ */
196 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_LB: └ */
197 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS: ┼ */
198 {{{'o', 0, }}, {{0xE2, 0x8E, 0xBA, 0}}}, /* Horiz. Scan Line 1: ⎺ */
199 {{{'p', 0, }}, {{0xE2, 0x8E, 0xBB, 0}}}, /* Horiz. Scan Line 3: ⎻ */
200 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* Horiz. Scan Line 5: ─ */
201 {{{'r', 0, }}, {{0xE2, 0x8E, 0xBC, 0}}}, /* Horiz. Scan Line 7: ⎼ */
202 {{{'s', 0, }}, {{0xE2, 0x8E, 0xBD, 0}}}, /* Horiz. Scan Line 9: ⎽ */
203 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR: ├ */
204 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL: ┤ */
205 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU: ┴ */
206 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD: ┬ */
207 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V: │ */
208 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE: ≤ */
209 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE: ≥ */
210 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI: π */
211 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ: ≠ */
212 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
213 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT: ⋅ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000214 {{{0, }}, {{0, }}}
215};
216
217static void
218apply_char_set(character_set cs, union utf8_char *utf8)
219{
220 int i = 0;
221
222 while (cs[i].match.byte[0]) {
223 if ((*utf8).ch == cs[i].match.ch) {
224 *utf8 = cs[i].replace;
225 break;
226 }
227 i++;
228 }
229}
230
Callum Lowcay7e08e902011-01-07 19:47:02 +0000231struct key_map {
232 int sym;
233 int num;
234 char escape;
235 char code;
236};
237/* Set last key_sub sym to NULL */
238typedef struct key_map *keyboard_mode;
239
240static struct key_map KM_NORMAL[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400241 { XKB_KEY_Left, 1, '[', 'D' },
242 { XKB_KEY_Right, 1, '[', 'C' },
243 { XKB_KEY_Up, 1, '[', 'A' },
244 { XKB_KEY_Down, 1, '[', 'B' },
245 { XKB_KEY_Home, 1, '[', 'H' },
246 { XKB_KEY_End, 1, '[', 'F' },
247 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000248};
249static struct key_map KM_APPLICATION[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400250 { XKB_KEY_Left, 1, 'O', 'D' },
251 { XKB_KEY_Right, 1, 'O', 'C' },
252 { XKB_KEY_Up, 1, 'O', 'A' },
253 { XKB_KEY_Down, 1, 'O', 'B' },
254 { XKB_KEY_Home, 1, 'O', 'H' },
255 { XKB_KEY_End, 1, 'O', 'F' },
256 { XKB_KEY_KP_Enter, 1, 'O', 'M' },
257 { XKB_KEY_KP_Multiply, 1, 'O', 'j' },
258 { XKB_KEY_KP_Add, 1, 'O', 'k' },
259 { XKB_KEY_KP_Separator, 1, 'O', 'l' },
260 { XKB_KEY_KP_Subtract, 1, 'O', 'm' },
261 { XKB_KEY_KP_Divide, 1, 'O', 'o' },
262 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000263};
264
265static int
266function_key_response(char escape, int num, uint32_t modifiers,
267 char code, char *response)
268{
269 int mod_num = 0;
270 int len;
271
Kristian Høgsberg70163132012-05-08 15:55:39 -0400272 if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
273 if (modifiers & MOD_ALT_MASK) mod_num |= 2;
274 if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000275
276 if (mod_num != 0)
277 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
278 num, mod_num + 1, code);
279 else if (code != '~')
280 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
281 escape, code);
282 else
283 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
284 escape, num, code);
285
286 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
287 else return len;
288}
289
290/* returns the number of bytes written into response,
291 * which must have room for MAX_RESPONSE bytes */
292static int
293apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
294{
295 struct key_map map;
296 int len = 0;
297 int i = 0;
298
299 while (mode[i].sym) {
300 map = mode[i++];
301 if (sym == map.sym) {
302 len = function_key_response(map.escape, map.num,
303 modifiers, map.code,
304 response);
305 break;
306 }
307 }
308
309 return len;
310}
311
Callum Lowcay30eeae52011-01-07 19:46:55 +0000312struct terminal_color { double r, g, b, a; };
313struct attr {
314 unsigned char fg, bg;
315 char a; /* attributes format:
316 * 76543210
Callum Lowcay81179db2011-01-10 12:14:01 +1300317 * cilub */
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500318 char s; /* in selection */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000319};
320struct color_scheme {
321 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500322 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000323 struct attr default_attr;
324};
325
326static void
327attr_init(struct attr *data_attr, struct attr attr, int n)
328{
329 int i;
330 for (i = 0; i < n; i++) {
331 data_attr[i] = attr;
332 }
333}
334
Callum Lowcay67a201d2011-01-12 19:23:41 +1300335enum escape_state {
336 escape_state_normal = 0,
337 escape_state_escape,
338 escape_state_dcs,
339 escape_state_csi,
340 escape_state_osc,
341 escape_state_inner_escape,
342 escape_state_ignore,
343 escape_state_special
344};
345
346#define ESC_FLAG_WHAT 0x01
347#define ESC_FLAG_GT 0x02
348#define ESC_FLAG_BANG 0x04
349#define ESC_FLAG_CASH 0x08
350#define ESC_FLAG_SQUOTE 0x10
351#define ESC_FLAG_DQUOTE 0x20
352#define ESC_FLAG_SPACE 0x40
353
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500354struct terminal {
355 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500356 struct widget *widget;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500357 struct display *display;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000358 union utf8_char *data;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400359 struct task io_task;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000360 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000361 struct attr *data_attr;
362 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000363 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000364 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000365 char saved_origin_mode;
366 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000367 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000368 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000369 character_set cs, g0, g1;
370 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000371 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000372 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500373 int width, height, start, row, column;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000374 int saved_row, saved_column;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600375 int send_cursor_position;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500376 int fd, master;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500377 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300378 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500379 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300380 enum escape_state state;
381 enum escape_state outer_state;
382 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000383 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500384 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500385 int fullscreen;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500386 int focused;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400387 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000388 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400389 cairo_font_extents_t extents;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500390 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500391
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400392 struct wl_data_source *selection;
393 int32_t dragging;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500394 int selection_start_x, selection_start_y;
395 int selection_end_x, selection_end_y;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500396};
397
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000398/* Create default tab stops, every 8 characters */
399static void
400terminal_init_tabs(struct terminal *terminal)
401{
402 int i = 0;
403
404 while (i < terminal->width) {
405 if (i % 8 == 0)
406 terminal->tab_ruler[i] = 1;
407 else
408 terminal->tab_ruler[i] = 0;
409 i++;
410 }
411}
412
Callum Lowcay30eeae52011-01-07 19:46:55 +0000413static void
414terminal_init(struct terminal *terminal)
415{
416 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000417 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000418 terminal->mode = MODE_SHOW_CURSOR |
419 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000420 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000421 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000422
423 terminal->row = 0;
424 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000425
Callum Lowcay256e72f2011-01-07 19:47:01 +0000426 terminal->g0 = CS_US;
427 terminal->g1 = CS_US;
428 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000429 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000430
431 terminal->saved_g0 = terminal->g0;
432 terminal->saved_g1 = terminal->g1;
433 terminal->saved_cs = terminal->cs;
434
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000435 terminal->saved_attr = terminal->curr_attr;
436 terminal->saved_origin_mode = terminal->origin_mode;
437 terminal->saved_row = terminal->row;
438 terminal->saved_column = terminal->column;
439
440 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000441}
442
443static void
444init_color_table(struct terminal *terminal)
445{
446 int c, r;
447 struct terminal_color *color_table = terminal->color_table;
448
449 for (c = 0; c < 256; c ++) {
450 if (c < 16) {
451 color_table[c] = terminal->color_scheme->palette[c];
452 } else if (c < 232) {
453 r = c - 16;
454 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
455 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
456 color_table[c].r = ((double)(r % 6) / 6.0);
457 color_table[c].a = 1.0;
458 } else {
459 r = (c - 232) * 10 + 8;
460 color_table[c].r = ((double) r) / 256.0;
461 color_table[c].g = color_table[c].r;
462 color_table[c].b = color_table[c].r;
463 color_table[c].a = 1.0;
464 }
465 }
466}
467
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000468static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500469terminal_get_row(struct terminal *terminal, int row)
470{
471 int index;
472
473 index = (row + terminal->start) % terminal->height;
474
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000475 return &terminal->data[index * terminal->width];
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500476}
477
Callum Lowcay30eeae52011-01-07 19:46:55 +0000478static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500479terminal_get_attr_row(struct terminal *terminal, int row)
480{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000481 int index;
482
483 index = (row + terminal->start) % terminal->height;
484
485 return &terminal->data_attr[index * terminal->width];
486}
487
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500488union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300489 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500490 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500491};
492
Kristian Høgsberg59826582011-01-20 11:56:57 -0500493static int
494terminal_compare_position(struct terminal *terminal,
495 int x, int y, int32_t ref_row, int32_t ref_col)
496{
497 struct rectangle allocation;
498 int top_margin, side_margin, col, row, ref_x;
499
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500500 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsberg59826582011-01-20 11:56:57 -0500501 side_margin = allocation.x + (allocation.width - terminal->width * terminal->extents.max_x_advance) / 2;
502 top_margin = allocation.y + (allocation.height - terminal->height * terminal->extents.height) / 2;
503
504 col = (x - side_margin) / terminal->extents.max_x_advance;
505 row = (y - top_margin) / terminal->extents.height;
506
507 ref_x = side_margin + ref_col * terminal->extents.max_x_advance +
508 terminal->extents.max_x_advance / 2;
509
510 if (row < ref_row)
511 return -1;
512 if (row == ref_row) {
513 if (col < ref_col)
514 return -1;
515 if (col == ref_col && x < ref_x)
516 return -1;
517 }
518
519 return 1;
520}
521
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500522static void
523terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500524 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500525{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500526 struct attr attr;
527 int foreground, background, tmp;
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500528 int start_cmp, end_cmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500529
530 start_cmp =
531 terminal_compare_position(terminal,
532 terminal->selection_start_x,
533 terminal->selection_start_y,
534 row, col);
535 end_cmp =
536 terminal_compare_position(terminal,
537 terminal->selection_end_x,
538 terminal->selection_end_y,
539 row, col);
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500540 decoded->attr.s = 0;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500541 if (start_cmp < 0 && end_cmp > 0)
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500542 decoded->attr.s = 1;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500543 else if (end_cmp < 0 && start_cmp > 0)
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500544 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500545
546 /* get the attributes for this character cell */
547 attr = terminal_get_attr_row(terminal, row)[col];
548 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500549 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500550 ((terminal->mode & MODE_SHOW_CURSOR) &&
551 terminal->focused && terminal->row == row &&
552 terminal->column == col)) {
553 foreground = attr.bg;
554 background = attr.fg;
555 if (attr.a & ATTRMASK_BOLD) {
556 if (foreground <= 16) foreground |= 0x08;
557 if (background <= 16) background &= 0x07;
558 }
559 } else {
560 foreground = attr.fg;
561 background = attr.bg;
562 }
563
564 if (terminal->mode & MODE_INVERSE) {
565 tmp = foreground;
566 foreground = background;
567 background = tmp;
568 if (attr.a & ATTRMASK_BOLD) {
569 if (foreground <= 16) foreground |= 0x08;
570 if (background <= 16) background &= 0x07;
571 }
572 }
573
Callum Lowcay9d708b02011-01-12 20:06:17 +1300574 decoded->attr.fg = foreground;
575 decoded->attr.bg = background;
576 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000577}
578
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500579
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500580static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000581terminal_scroll_buffer(struct terminal *terminal, int d)
582{
583 int i;
584
585 d = d % (terminal->height + 1);
586 terminal->start = (terminal->start + d) % terminal->height;
587 if (terminal->start < 0) terminal->start = terminal->height + terminal->start;
588 if(d < 0) {
589 d = 0 - d;
590 for(i = 0; i < d; i++) {
591 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
592 attr_init(terminal_get_attr_row(terminal, i),
593 terminal->curr_attr, terminal->width);
594 }
595 } else {
596 for(i = terminal->height - d; i < terminal->height; i++) {
597 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
598 attr_init(terminal_get_attr_row(terminal, i),
599 terminal->curr_attr, terminal->width);
600 }
601 }
602}
603
604static void
605terminal_scroll_window(struct terminal *terminal, int d)
606{
607 int i;
608 int window_height;
609 int from_row, to_row;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000610
611 // scrolling range is inclusive
612 window_height = terminal->margin_bottom - terminal->margin_top + 1;
613 d = d % (window_height + 1);
614 if(d < 0) {
615 d = 0 - d;
616 to_row = terminal->margin_bottom;
617 from_row = terminal->margin_bottom - d;
618
619 for (i = 0; i < (window_height - d); i++) {
620 memcpy(terminal_get_row(terminal, to_row - i),
621 terminal_get_row(terminal, from_row - i),
622 terminal->data_pitch);
623 memcpy(terminal_get_attr_row(terminal, to_row - i),
624 terminal_get_attr_row(terminal, from_row - i),
625 terminal->attr_pitch);
626 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000627 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
628 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000629 attr_init(terminal_get_attr_row(terminal, i),
630 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000631 }
632 } else {
633 to_row = terminal->margin_top;
634 from_row = terminal->margin_top + d;
635
636 for (i = 0; i < (window_height - d); i++) {
637 memcpy(terminal_get_row(terminal, to_row + i),
638 terminal_get_row(terminal, from_row + i),
639 terminal->data_pitch);
640 memcpy(terminal_get_attr_row(terminal, to_row + i),
641 terminal_get_attr_row(terminal, from_row + i),
642 terminal->attr_pitch);
643 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000644 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
645 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000646 attr_init(terminal_get_attr_row(terminal, i),
647 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000648 }
649 }
650}
651
652static void
653terminal_scroll(struct terminal *terminal, int d)
654{
655 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
656 terminal_scroll_buffer(terminal, d);
657 else
658 terminal_scroll_window(terminal, d);
659}
660
661static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000662terminal_shift_line(struct terminal *terminal, int d)
663{
664 union utf8_char *row;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500665 struct attr *attr_row;
Callum Lowcay69e96582011-01-07 19:47:00 +0000666
667 row = terminal_get_row(terminal, terminal->row);
668 attr_row = terminal_get_attr_row(terminal, terminal->row);
669
670 if ((terminal->width + d) <= terminal->column)
671 d = terminal->column + 1 - terminal->width;
672 if ((terminal->column + d) >= terminal->width)
673 d = terminal->width - terminal->column - 1;
674
675 if (d < 0) {
676 d = 0 - d;
677 memmove(&row[terminal->column],
678 &row[terminal->column + d],
679 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
Callum Lowcay69e96582011-01-07 19:47:00 +0000680 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
681 (terminal->width - terminal->column - d) * sizeof(struct attr));
682 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
683 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
684 } else {
685 memmove(&row[terminal->column + d], &row[terminal->column],
686 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
687 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
688 (terminal->width - terminal->column - d) * sizeof(struct attr));
689 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
690 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
691 }
692}
693
694static void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500695terminal_resize_cells(struct terminal *terminal, int width, int height)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500696{
697 size_t size;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000698 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000699 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000700 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000701 int data_pitch, attr_pitch;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500702 int i, l, total_rows;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500703 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000704 struct winsize ws;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500705
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500706 if (width < 1)
707 width = 1;
708 if (height < 1)
709 height = 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500710 if (terminal->width == width && terminal->height == height)
711 return;
712
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000713 data_pitch = width * sizeof(union utf8_char);
714 size = data_pitch * height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500715 data = malloc(size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000716 attr_pitch = width * sizeof(struct attr);
717 data_attr = malloc(attr_pitch * height);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000718 tab_ruler = malloc(width);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500719 memset(data, 0, size);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000720 memset(tab_ruler, 0, width);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000721 attr_init(data_attr, terminal->curr_attr, width * height);
722 if (terminal->data && terminal->data_attr) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500723 if (width > terminal->width)
724 l = terminal->width;
725 else
726 l = width;
727
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500728 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500729 total_rows = height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500730 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500731 total_rows = terminal->height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500732 }
733
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000734 for (i = 0; i < total_rows; i++) {
735 memcpy(&data[width * i],
736 terminal_get_row(terminal, i),
737 l * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000738 memcpy(&data_attr[width * i],
739 terminal_get_attr_row(terminal, i),
740 l * sizeof(struct attr));
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000741 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500742
743 free(terminal->data);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000744 free(terminal->data_attr);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000745 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500746 }
747
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000748 terminal->data_pitch = data_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000749 terminal->attr_pitch = attr_pitch;
Callum Lowcay86653ed2011-01-07 19:47:03 +0000750 terminal->margin_bottom =
751 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500752 terminal->width = width;
753 terminal->height = height;
754 terminal->data = data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000755 terminal->data_attr = data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000756 terminal->tab_ruler = tab_ruler;
757 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500758
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000759 /* Update the window size */
760 ws.ws_row = terminal->height;
761 ws.ws_col = terminal->width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500762 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500763 ws.ws_xpixel = allocation.width;
764 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000765 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500766}
767
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500768static void
769resize_handler(struct widget *widget,
770 int32_t width, int32_t height, void *data)
771{
772 struct terminal *terminal = data;
773 int32_t columns, rows, m;
774
775 m = 2 * terminal->margin;
776 columns = (width - m) / (int32_t) terminal->extents.max_x_advance;
777 rows = (height - m) / (int32_t) terminal->extents.height;
778
779 if (!terminal->fullscreen) {
780 width = columns * terminal->extents.max_x_advance + m;
781 height = rows * terminal->extents.height + m;
782 widget_set_size(terminal->widget, width, height);
783 }
784
785 terminal_resize_cells(terminal, columns, rows);
786}
787
788static void
789terminal_resize(struct terminal *terminal, int columns, int rows)
790{
791 int32_t width, height, m;
792
793 if (terminal->fullscreen)
794 return;
795
796 m = 2 * terminal->margin;
797 width = columns * terminal->extents.max_x_advance + m;
798 height = rows * terminal->extents.height + m;
799 widget_schedule_resize(terminal->widget, width, height);
800}
801
Callum Lowcay30eeae52011-01-07 19:46:55 +0000802struct color_scheme DEFAULT_COLORS = {
803 {
804 {0, 0, 0, 1}, /* black */
805 {0.66, 0, 0, 1}, /* red */
806 {0 , 0.66, 0, 1}, /* green */
807 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
808 {0 , 0 , 0.66, 1}, /* blue */
809 {0.66, 0 , 0.66, 1}, /* magenta */
810 {0, 0.66, 0.66, 1}, /* cyan */
811 {0.66, 0.66, 0.66, 1}, /* light grey */
812 {0.22, 0.33, 0.33, 1}, /* dark grey */
813 {1, 0.33, 0.33, 1}, /* high red */
814 {0.33, 1, 0.33, 1}, /* high green */
815 {1, 1, 0.33, 1}, /* high yellow */
816 {0.33, 0.33, 1, 1}, /* high blue */
817 {1, 0.33, 1, 1}, /* high magenta */
818 {0.33, 1, 1, 1}, /* high cyan */
819 {1, 1, 1, 1} /* white */
820 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500821 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000822 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
823};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400824
Kristian Høgsberg22106762008-12-08 13:50:07 -0500825static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500826terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
827{
828 cairo_set_source_rgba(cr,
829 terminal->color_table[index].r,
830 terminal->color_table[index].g,
831 terminal->color_table[index].b,
832 terminal->color_table[index].a);
833}
834
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500835static void
836terminal_send_selection(struct terminal *terminal, int fd)
837{
838 int row, col;
839 union utf8_char *p_row;
840 union decoded_attr attr;
841 FILE *fp;
842 int len;
843
844 fp = fdopen(fd, "w");
845 for (row = 0; row < terminal->height; row++) {
846 p_row = terminal_get_row(terminal, row);
847 for (col = 0; col < terminal->width; col++) {
848 /* get the attributes for this character cell */
849 terminal_decode_attr(terminal, row, col, &attr);
850 if (!attr.attr.s)
851 continue;
852 len = strnlen((char *) p_row[col].byte, 4);
853 fwrite(p_row[col].byte, 1, len, fp);
854 }
855 }
856 fclose(fp);
857}
858
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500859struct glyph_run {
860 struct terminal *terminal;
861 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400862 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500863 union decoded_attr attr;
864 cairo_glyph_t glyphs[256], *g;
865};
866
867static void
868glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
869{
870 run->terminal = terminal;
871 run->cr = cr;
872 run->g = run->glyphs;
873 run->count = 0;
874 run->attr.key = 0;
875}
876
877static void
878glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
879{
880 cairo_scaled_font_t *font;
881
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500882 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
883 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300884 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500885 font = run->terminal->font_bold;
886 else
887 font = run->terminal->font_normal;
888 cairo_set_scaled_font(run->cr, font);
889 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300890 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500891
Callum Lowcay9d708b02011-01-12 20:06:17 +1300892 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
893 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500894 run->g = run->glyphs;
895 run->count = 0;
896 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300897 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500898}
899
900static void
901glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
902{
903 int num_glyphs;
904 cairo_scaled_font_t *font;
905
906 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
907
Callum Lowcay9d708b02011-01-12 20:06:17 +1300908 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500909 font = run->terminal->font_bold;
910 else
911 font = run->terminal->font_normal;
912
913 cairo_move_to(run->cr, x, y);
914 cairo_scaled_font_text_to_glyphs (font, x, y,
915 (char *) c->byte, 4,
916 &run->g, &num_glyphs,
917 NULL, NULL, NULL);
918 run->g += num_glyphs;
919 run->count += num_glyphs;
920}
921
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500922
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500923static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500924redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500925{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500926 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500927 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500928 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000929 int top_margin, side_margin;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600930 int row, col, cursor_x, cursor_y;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000931 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500932 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000933 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500934 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500935 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500936 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500937 cairo_font_extents_t extents;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500938
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500939 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500940 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500941 cr = cairo_create(surface);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500942 cairo_rectangle(cr, allocation.x, allocation.y,
943 allocation.width, allocation.height);
944 cairo_clip(cr);
945 cairo_push_group(cr);
946
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500947 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500948 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500949 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500950
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500951 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500952
Kristian Høgsberg59826582011-01-20 11:56:57 -0500953 extents = terminal->extents;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500954 side_margin = (allocation.width - terminal->width * extents.max_x_advance) / 2;
955 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500956
Callum Lowcay30eeae52011-01-07 19:46:55 +0000957 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500958 cairo_translate(cr, allocation.x + side_margin,
959 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500960 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000961 for (row = 0; row < terminal->height; row++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000962 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000963 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500964 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000965
Callum Lowcay9d708b02011-01-12 20:06:17 +1300966 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -0500967 continue;
968
Callum Lowcay9d708b02011-01-12 20:06:17 +1300969 terminal_set_color(terminal, cr, attr.attr.bg);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500970 cairo_move_to(cr, col * extents.max_x_advance,
971 row * extents.height);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000972 cairo_rel_line_to(cr, extents.max_x_advance, 0);
973 cairo_rel_line_to(cr, 0, extents.height);
974 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
975 cairo_close_path(cr);
976 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500977 }
978 }
Callum Lowcay30eeae52011-01-07 19:46:55 +0000979
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500980 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
981
982 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500983 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500984 for (row = 0; row < terminal->height; row++) {
985 p_row = terminal_get_row(terminal, row);
986 for (col = 0; col < terminal->width; col++) {
987 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500988 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500989
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500990 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000991
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500992 text_x = col * extents.max_x_advance;
993 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +1300994 if (attr.attr.a & ATTRMASK_UNDERLINE) {
995 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000996 cairo_move_to(cr, text_x, (double)text_y + 1.5);
997 cairo_line_to(cr, text_x + extents.max_x_advance, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000998 cairo_stroke(cr);
999 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -05001000
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001001 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001002 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001003 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001004
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001005 attr.key = ~0;
1006 glyph_run_flush(&run, attr);
1007
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001008 if ((terminal->mode & MODE_SHOW_CURSOR) && !terminal->focused) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001009 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001010
Callum Lowcay30eeae52011-01-07 19:46:55 +00001011 cairo_set_line_width(cr, 1);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001012 cairo_move_to(cr, terminal->column * extents.max_x_advance + d,
1013 terminal->row * extents.height + d);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001014 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
1015 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
1016 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
1017 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001018
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001019 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001020 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001021
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001022 cairo_pop_group_to_source(cr);
1023 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001024 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001025 cairo_surface_destroy(surface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001026
1027 if (terminal->send_cursor_position) {
1028 cursor_x = side_margin + allocation.x +
1029 terminal->column * extents.max_x_advance;
1030 cursor_y = top_margin + allocation.y +
1031 terminal->row * extents.height;
1032 window_set_text_cursor_position(terminal->window,
1033 cursor_x, cursor_y);
1034 terminal->send_cursor_position = 0;
1035 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001036}
1037
1038static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001039terminal_write(struct terminal *terminal, const char *data, size_t length)
1040{
1041 if (write(terminal->master, data, length) < 0)
1042 abort();
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001043 terminal->send_cursor_position = 1;
Kristian Høgsberg26130862011-08-24 11:30:21 -04001044}
1045
1046static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001047terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001048
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001049static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001050handle_char(struct terminal *terminal, union utf8_char utf8);
1051
1052static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001053handle_sgr(struct terminal *terminal, int code);
1054
1055static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001056handle_term_parameter(struct terminal *terminal, int code, int sr)
1057{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001058 int i;
1059
Callum Lowcay67a201d2011-01-12 19:23:41 +13001060 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001061 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001062 case 1: /* DECCKM */
1063 if (sr) terminal->key_mode = KM_APPLICATION;
1064 else terminal->key_mode = KM_NORMAL;
1065 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001066 case 2: /* DECANM */
1067 /* No VT52 support yet */
1068 terminal->g0 = CS_US;
1069 terminal->g1 = CS_US;
1070 terminal->cs = terminal->g0;
1071 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001072 case 3: /* DECCOLM */
1073 if (sr)
1074 terminal_resize(terminal, 132, 24);
1075 else
1076 terminal_resize(terminal, 80, 24);
1077
1078 /* set columns, but also home cursor and clear screen */
1079 terminal->row = 0; terminal->column = 0;
1080 for (i = 0; i < terminal->height; i++) {
1081 memset(terminal_get_row(terminal, i),
1082 0, terminal->data_pitch);
1083 attr_init(terminal_get_attr_row(terminal, i),
1084 terminal->curr_attr, terminal->width);
1085 }
1086 break;
1087 case 5: /* DECSCNM */
1088 if (sr) terminal->mode |= MODE_INVERSE;
1089 else terminal->mode &= ~MODE_INVERSE;
1090 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001091 case 6: /* DECOM */
1092 terminal->origin_mode = sr;
1093 if (terminal->origin_mode)
1094 terminal->row = terminal->margin_top;
1095 else
1096 terminal->row = 0;
1097 terminal->column = 0;
1098 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001099 case 7: /* DECAWM */
1100 if (sr) terminal->mode |= MODE_AUTOWRAP;
1101 else terminal->mode &= ~MODE_AUTOWRAP;
1102 break;
1103 case 8: /* DECARM */
1104 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1105 else terminal->mode &= ~MODE_AUTOREPEAT;
1106 break;
1107 case 25:
1108 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1109 else terminal->mode &= ~MODE_SHOW_CURSOR;
1110 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001111 case 1037: /* deleteSendsDel */
1112 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1113 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1114 break;
1115 case 1039: /* altSendsEscape */
1116 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1117 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1118 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001119 default:
1120 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1121 break;
1122 }
1123 } else {
1124 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001125 case 4: /* IRM */
1126 if (sr) terminal->mode |= MODE_IRM;
1127 else terminal->mode &= ~MODE_IRM;
1128 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001129 case 20: /* LNM */
1130 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1131 else terminal->mode &= ~MODE_LF_NEWLINE;
1132 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001133 default:
1134 fprintf(stderr, "Unknown parameter: %d\n", code);
1135 break;
1136 }
1137 }
1138}
1139
1140static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001141handle_dcs(struct terminal *terminal)
1142{
1143}
1144
1145static void
1146handle_osc(struct terminal *terminal)
1147{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001148 char *p;
1149 int code;
1150
1151 terminal->escape[terminal->escape_length++] = '\0';
1152 p = &terminal->escape[2];
1153 code = strtol(p, &p, 10);
1154 if (*p == ';') p++;
1155
1156 switch (code) {
1157 case 0: /* Icon name and window title */
1158 case 1: /* Icon label */
1159 case 2: /* Window title*/
1160 window_set_title(terminal->window, p);
1161 break;
1162 default:
1163 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1164 break;
1165 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001166}
1167
1168static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001169handle_escape(struct terminal *terminal)
1170{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001171 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001172 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001173 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001174 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001175 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001176 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001177 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001178
1179 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001180 i = 0;
1181 p = &terminal->escape[2];
1182 while ((isdigit(*p) || *p == ';') && i < 10) {
1183 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001184 if (!set[i]) {
1185 args[i] = 0;
1186 set[i] = 1;
1187 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001188 p++;
1189 i++;
1190 } else {
1191 args[i] = strtol(p, &p, 10);
1192 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001193 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001194 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001195
1196 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001197 case '@': /* ICH */
1198 count = set[0] ? args[0] : 1;
1199 if (count == 0) count = 1;
1200 terminal_shift_line(terminal, count);
1201 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001202 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001203 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001204 if (count == 0) count = 1;
1205 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001206 terminal->row -= count;
1207 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001208 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001209 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001210 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001211 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001212 if (count == 0) count = 1;
1213 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001214 terminal->row += count;
1215 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001216 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001217 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001218 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001219 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001220 if (count == 0) count = 1;
1221 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001222 terminal->column += count;
1223 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001224 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001225 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001226 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001227 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001228 if (count == 0) count = 1;
1229 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001230 terminal->column -= count;
1231 else
1232 terminal->column = 0;
1233 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001234 case 'E': /* CNL */
1235 count = set[0] ? args[0] : 1;
1236 if (terminal->row + count <= terminal->margin_bottom)
1237 terminal->row += count;
1238 else
1239 terminal->row = terminal->margin_bottom;
1240 terminal->column = 0;
1241 break;
1242 case 'F': /* CPL */
1243 count = set[0] ? args[0] : 1;
1244 if (terminal->row - count >= terminal->margin_top)
1245 terminal->row -= count;
1246 else
1247 terminal->row = terminal->margin_top;
1248 terminal->column = 0;
1249 break;
1250 case 'G': /* CHA */
1251 y = set[0] ? args[0] : 1;
1252 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1253
1254 terminal->column = y - 1;
1255 break;
1256 case 'f': /* HVP */
1257 case 'H': /* CUP */
1258 x = (set[1] ? args[1] : 1) - 1;
1259 x = x < 0 ? 0 :
1260 (x >= terminal->width ? terminal->width - 1 : x);
1261
1262 y = (set[0] ? args[0] : 1) - 1;
1263 if (terminal->origin_mode) {
1264 y += terminal->margin_top;
1265 y = y < terminal->margin_top ? terminal->margin_top :
1266 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1267 } else {
1268 y = y < 0 ? 0 :
1269 (y >= terminal->height ? terminal->height - 1 : y);
1270 }
1271
1272 terminal->row = y;
1273 terminal->column = x;
1274 break;
1275 case 'I': /* CHT */
1276 count = set[0] ? args[0] : 1;
1277 if (count == 0) count = 1;
1278 while (count > 0 && terminal->column < terminal->width) {
1279 if (terminal->tab_ruler[terminal->column]) count--;
1280 terminal->column++;
1281 }
1282 terminal->column--;
1283 break;
1284 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001285 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001286 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001287 if (!set[0] || args[0] == 0 || args[0] > 2) {
1288 memset(&row[terminal->column],
1289 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1290 attr_init(&attr_row[terminal->column],
1291 terminal->curr_attr, terminal->width - terminal->column);
1292 for (i = terminal->row + 1; i < terminal->height; i++) {
1293 memset(terminal_get_row(terminal, i),
1294 0, terminal->data_pitch);
1295 attr_init(terminal_get_attr_row(terminal, i),
1296 terminal->curr_attr, terminal->width);
1297 }
1298 } else if (args[0] == 1) {
1299 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1300 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1301 for (i = 0; i < terminal->row; i++) {
1302 memset(terminal_get_row(terminal, i),
1303 0, terminal->data_pitch);
1304 attr_init(terminal_get_attr_row(terminal, i),
1305 terminal->curr_attr, terminal->width);
1306 }
1307 } else if (args[0] == 2) {
1308 for (i = 0; i < terminal->height; i++) {
1309 memset(terminal_get_row(terminal, i),
1310 0, terminal->data_pitch);
1311 attr_init(terminal_get_attr_row(terminal, i),
1312 terminal->curr_attr, terminal->width);
1313 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001314 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001315 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001316 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001317 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001318 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001319 if (!set[0] || args[0] == 0 || args[0] > 2) {
1320 memset(&row[terminal->column], 0,
1321 (terminal->width - terminal->column) * sizeof(union utf8_char));
1322 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1323 terminal->width - terminal->column);
1324 } else if (args[0] == 1) {
1325 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1326 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1327 } else if (args[0] == 2) {
1328 memset(row, 0, terminal->data_pitch);
1329 attr_init(attr_row, terminal->curr_attr, terminal->width);
1330 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001331 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001332 case 'L': /* IL */
1333 count = set[0] ? args[0] : 1;
1334 if (count == 0) count = 1;
1335 if (terminal->row >= terminal->margin_top &&
1336 terminal->row < terminal->margin_bottom)
1337 {
1338 top = terminal->margin_top;
1339 terminal->margin_top = terminal->row;
1340 terminal_scroll(terminal, 0 - count);
1341 terminal->margin_top = top;
1342 } else if (terminal->row == terminal->margin_bottom) {
1343 memset(terminal_get_row(terminal, terminal->row),
1344 0, terminal->data_pitch);
1345 attr_init(terminal_get_attr_row(terminal, terminal->row),
1346 terminal->curr_attr, terminal->width);
1347 }
1348 break;
1349 case 'M': /* DL */
1350 count = set[0] ? args[0] : 1;
1351 if (count == 0) count = 1;
1352 if (terminal->row >= terminal->margin_top &&
1353 terminal->row < terminal->margin_bottom)
1354 {
1355 top = terminal->margin_top;
1356 terminal->margin_top = terminal->row;
1357 terminal_scroll(terminal, count);
1358 terminal->margin_top = top;
1359 } else if (terminal->row == terminal->margin_bottom) {
1360 memset(terminal_get_row(terminal, terminal->row),
1361 0, terminal->data_pitch);
1362 }
1363 break;
1364 case 'P': /* DCH */
1365 count = set[0] ? args[0] : 1;
1366 if (count == 0) count = 1;
1367 terminal_shift_line(terminal, 0 - count);
1368 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001369 case 'S': /* SU */
1370 terminal_scroll(terminal, set[0] ? args[0] : 1);
1371 break;
1372 case 'T': /* SD */
1373 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1374 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001375 case 'X': /* ECH */
1376 count = set[0] ? args[0] : 1;
1377 if (count == 0) count = 1;
1378 if ((terminal->column + count) > terminal->width)
1379 count = terminal->width - terminal->column;
1380 row = terminal_get_row(terminal, terminal->row);
1381 attr_row = terminal_get_attr_row(terminal, terminal->row);
1382 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1383 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1384 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001385 case 'Z': /* CBT */
1386 count = set[0] ? args[0] : 1;
1387 if (count == 0) count = 1;
1388 while (count > 0 && terminal->column >= 0) {
1389 if (terminal->tab_ruler[terminal->column]) count--;
1390 terminal->column--;
1391 }
1392 terminal->column++;
1393 break;
1394 case '`': /* HPA */
1395 y = set[0] ? args[0] : 1;
1396 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1397
1398 terminal->column = y - 1;
1399 break;
1400 case 'b': /* REP */
1401 count = set[0] ? args[0] : 1;
1402 if (count == 0) count = 1;
1403 if (terminal->last_char.byte[0])
1404 for (i = 0; i < count; i++)
1405 handle_char(terminal, terminal->last_char);
1406 terminal->last_char.byte[0] = 0;
1407 break;
1408 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001409 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001410 break;
1411 case 'd': /* VPA */
1412 x = set[0] ? args[0] : 1;
1413 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1414
1415 terminal->row = x - 1;
1416 break;
1417 case 'g': /* TBC */
1418 if (!set[0] || args[0] == 0) {
1419 terminal->tab_ruler[terminal->column] = 0;
1420 } else if (args[0] == 3) {
1421 memset(terminal->tab_ruler, 0, terminal->width);
1422 }
1423 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001424 case 'h': /* SM */
1425 for(i = 0; i < 10 && set[i]; i++) {
1426 handle_term_parameter(terminal, args[i], 1);
1427 }
1428 break;
1429 case 'l': /* RM */
1430 for(i = 0; i < 10 && set[i]; i++) {
1431 handle_term_parameter(terminal, args[i], 0);
1432 }
1433 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001434 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001435 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001436 if (i <= 7 && set[i] && set[i + 1] &&
1437 set[i + 2] && args[i + 1] == 5)
1438 {
1439 if (args[i] == 38) {
1440 handle_sgr(terminal, args[i + 2] + 256);
1441 break;
1442 } else if (args[i] == 48) {
1443 handle_sgr(terminal, args[i + 2] + 512);
1444 break;
1445 }
1446 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001447 if(set[i]) {
1448 handle_sgr(terminal, args[i]);
1449 } else if(i == 0) {
1450 handle_sgr(terminal, 0);
1451 break;
1452 } else {
1453 break;
1454 }
1455 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001456 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001457 case 'n': /* DSR */
1458 i = set[0] ? args[0] : 0;
1459 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001460 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001461 } else if (i == 6) {
1462 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1463 terminal->origin_mode ?
1464 terminal->row+terminal->margin_top : terminal->row+1,
1465 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001466 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001467 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001468 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001469 case 'r':
1470 if(!set[0]) {
1471 terminal->margin_top = 0;
1472 terminal->margin_bottom = terminal->height-1;
1473 terminal->row = 0;
1474 terminal->column = 0;
1475 } else {
1476 top = (set[0] ? args[0] : 1) - 1;
1477 top = top < 0 ? 0 :
1478 (top >= terminal->height ? terminal->height - 1 : top);
1479 bottom = (set[1] ? args[1] : 1) - 1;
1480 bottom = bottom < 0 ? 0 :
1481 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1482 if(bottom > top) {
1483 terminal->margin_top = top;
1484 terminal->margin_bottom = bottom;
1485 } else {
1486 terminal->margin_top = 0;
1487 terminal->margin_bottom = terminal->height-1;
1488 }
1489 if(terminal->origin_mode)
1490 terminal->row = terminal->margin_top;
1491 else
1492 terminal->row = 0;
1493 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001494 }
1495 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001496 case 's':
1497 terminal->saved_row = terminal->row;
1498 terminal->saved_column = terminal->column;
1499 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001500 case 't': /* windowOps */
1501 if (!set[0]) break;
1502 switch (args[0]) {
1503 case 4: /* resize px */
1504 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001505 widget_schedule_resize(terminal->widget,
1506 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001507 }
1508 break;
1509 case 8: /* resize ch */
1510 if (set[1] && set[2]) {
1511 terminal_resize(terminal, args[2], args[1]);
1512 }
1513 break;
1514 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001515 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001516 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1517 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001518 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001519 break;
1520 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001521 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001522 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1523 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001524 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001525 break;
1526 case 18: /* report ch */
1527 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1528 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001529 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001530 break;
1531 case 21: /* report title */
1532 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1533 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001534 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001535 break;
1536 default:
1537 if (args[0] >= 24)
1538 terminal_resize(terminal, terminal->width, args[0]);
1539 else
1540 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1541 break;
1542 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001543 case 'u':
1544 terminal->row = terminal->saved_row;
1545 terminal->column = terminal->saved_column;
1546 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001547 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001548 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001549 break;
1550 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001551}
1552
1553static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001554handle_non_csi_escape(struct terminal *terminal, char code)
1555{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001556 switch(code) {
1557 case 'M': /* RI */
1558 terminal->row -= 1;
1559 if(terminal->row < terminal->margin_top) {
1560 terminal->row = terminal->margin_top;
1561 terminal_scroll(terminal, -1);
1562 }
1563 break;
1564 case 'E': /* NEL */
1565 terminal->column = 0;
1566 // fallthrough
1567 case 'D': /* IND */
1568 terminal->row += 1;
1569 if(terminal->row > terminal->margin_bottom) {
1570 terminal->row = terminal->margin_bottom;
1571 terminal_scroll(terminal, +1);
1572 }
1573 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001574 case 'c': /* RIS */
1575 terminal_init(terminal);
1576 break;
1577 case 'H': /* HTS */
1578 terminal->tab_ruler[terminal->column] = 1;
1579 break;
1580 case '7': /* DECSC */
1581 terminal->saved_row = terminal->row;
1582 terminal->saved_column = terminal->column;
1583 terminal->saved_attr = terminal->curr_attr;
1584 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001585 terminal->saved_cs = terminal->cs;
1586 terminal->saved_g0 = terminal->g0;
1587 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001588 break;
1589 case '8': /* DECRC */
1590 terminal->row = terminal->saved_row;
1591 terminal->column = terminal->saved_column;
1592 terminal->curr_attr = terminal->saved_attr;
1593 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001594 terminal->cs = terminal->saved_cs;
1595 terminal->g0 = terminal->saved_g0;
1596 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001597 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001598 case '=': /* DECPAM */
1599 terminal->key_mode = KM_APPLICATION;
1600 break;
1601 case '>': /* DECPNM */
1602 terminal->key_mode = KM_NORMAL;
1603 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001604 default:
1605 fprintf(stderr, "Unknown escape code: %c\n", code);
1606 break;
1607 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001608}
1609
1610static void
1611handle_special_escape(struct terminal *terminal, char special, char code)
1612{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001613 int i, numChars;
1614
1615 if (special == '#') {
1616 switch(code) {
1617 case '8':
1618 /* fill with 'E', no cheap way to do this */
1619 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1620 numChars = terminal->width * terminal->height;
1621 for(i = 0; i < numChars; i++) {
1622 terminal->data[i].byte[0] = 'E';
1623 }
1624 break;
1625 default:
1626 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1627 break;
1628 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001629 } else if (special == '(' || special == ')') {
1630 switch(code) {
1631 case '0':
1632 if (special == '(')
1633 terminal->g0 = CS_SPECIAL;
1634 else
1635 terminal->g1 = CS_SPECIAL;
1636 break;
1637 case 'A':
1638 if (special == '(')
1639 terminal->g0 = CS_UK;
1640 else
1641 terminal->g1 = CS_UK;
1642 break;
1643 case 'B':
1644 if (special == '(')
1645 terminal->g0 = CS_US;
1646 else
1647 terminal->g1 = CS_US;
1648 break;
1649 default:
1650 fprintf(stderr, "Unknown character set %c\n", code);
1651 break;
1652 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001653 } else {
1654 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1655 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001656}
1657
1658static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001659handle_sgr(struct terminal *terminal, int code)
1660{
1661 switch(code) {
1662 case 0:
1663 terminal->curr_attr = terminal->color_scheme->default_attr;
1664 break;
1665 case 1:
1666 terminal->curr_attr.a |= ATTRMASK_BOLD;
1667 if (terminal->curr_attr.fg < 8)
1668 terminal->curr_attr.fg += 8;
1669 break;
1670 case 4:
1671 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1672 break;
1673 case 5:
1674 terminal->curr_attr.a |= ATTRMASK_BLINK;
1675 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001676 case 8:
1677 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1678 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001679 case 2:
1680 case 21:
1681 case 22:
1682 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1683 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1684 terminal->curr_attr.fg -= 8;
1685 break;
1686 case 24:
1687 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1688 break;
1689 case 25:
1690 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1691 break;
1692 case 7:
1693 case 26:
1694 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1695 break;
1696 case 27:
1697 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1698 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001699 case 28:
1700 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1701 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001702 case 39:
1703 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1704 break;
1705 case 49:
1706 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1707 break;
1708 default:
1709 if(code >= 30 && code <= 37) {
1710 terminal->curr_attr.fg = code - 30;
1711 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1712 terminal->curr_attr.fg += 8;
1713 } else if(code >= 40 && code <= 47) {
1714 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001715 } else if (code >= 90 && code <= 97) {
1716 terminal->curr_attr.fg = code - 90 + 8;
1717 } else if (code >= 100 && code <= 107) {
1718 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001719 } else if(code >= 256 && code < 512) {
1720 terminal->curr_attr.fg = code - 256;
1721 } else if(code >= 512 && code < 768) {
1722 terminal->curr_attr.bg = code - 512;
1723 } else {
1724 fprintf(stderr, "Unknown SGR code: %d\n", code);
1725 }
1726 break;
1727 }
1728}
1729
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001730/* Returns 1 if c was special, otherwise 0 */
1731static int
1732handle_special_char(struct terminal *terminal, char c)
1733{
1734 union utf8_char *row;
1735 struct attr *attr_row;
1736
1737 row = terminal_get_row(terminal, terminal->row);
1738 attr_row = terminal_get_attr_row(terminal, terminal->row);
1739
1740 switch(c) {
1741 case '\r':
1742 terminal->column = 0;
1743 break;
1744 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001745 if (terminal->mode & MODE_LF_NEWLINE) {
1746 terminal->column = 0;
1747 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001748 /* fallthrough */
1749 case '\v':
1750 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001751 terminal->row++;
1752 if(terminal->row > terminal->margin_bottom) {
1753 terminal->row = terminal->margin_bottom;
1754 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001755 }
1756
1757 break;
1758 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001759 while (terminal->column < terminal->width) {
1760 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001761 if (terminal->mode & MODE_IRM)
1762 terminal_shift_line(terminal, +1);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001763 row[terminal->column].byte[0] = ' ';
1764 row[terminal->column].byte[1] = '\0';
1765 attr_row[terminal->column] = terminal->curr_attr;
1766 terminal->column++;
1767 }
1768 if (terminal->column >= terminal->width) {
1769 terminal->column = terminal->width - 1;
1770 }
1771
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001772 break;
1773 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001774 if (terminal->column >= terminal->width) {
1775 terminal->column = terminal->width - 2;
1776 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001777 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001778 } else if (terminal->mode & MODE_AUTOWRAP) {
1779 terminal->column = terminal->width - 1;
1780 terminal->row -= 1;
1781 if (terminal->row < terminal->margin_top) {
1782 terminal->row = terminal->margin_top;
1783 terminal_scroll(terminal, -1);
1784 }
1785 }
1786
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001787 break;
1788 case '\a':
1789 /* Bell */
1790 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001791 case '\x0E': /* SO */
1792 terminal->cs = terminal->g1;
1793 break;
1794 case '\x0F': /* SI */
1795 terminal->cs = terminal->g0;
1796 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001797 default:
1798 return 0;
1799 }
1800
1801 return 1;
1802}
1803
1804static void
1805handle_char(struct terminal *terminal, union utf8_char utf8)
1806{
1807 union utf8_char *row;
1808 struct attr *attr_row;
1809
1810 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001811
1812 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001813
1814 /* There are a whole lot of non-characters, control codes,
1815 * and formatting codes that should probably be ignored,
1816 * for example: */
1817 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1818 /* BOM, ignore */
1819 return;
1820 }
1821
1822 /* Some of these non-characters should be translated, e.g.: */
1823 if (utf8.byte[0] < 32) {
1824 utf8.byte[0] = utf8.byte[0] + 64;
1825 }
1826
1827 /* handle right margin effects */
1828 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001829 if (terminal->mode & MODE_AUTOWRAP) {
1830 terminal->column = 0;
1831 terminal->row += 1;
1832 if (terminal->row > terminal->margin_bottom) {
1833 terminal->row = terminal->margin_bottom;
1834 terminal_scroll(terminal, +1);
1835 }
1836 } else {
1837 terminal->column--;
1838 }
1839 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001840
1841 row = terminal_get_row(terminal, terminal->row);
1842 attr_row = terminal_get_attr_row(terminal, terminal->row);
1843
Callum Lowcay69e96582011-01-07 19:47:00 +00001844 if (terminal->mode & MODE_IRM)
1845 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001846 row[terminal->column] = utf8;
1847 attr_row[terminal->column++] = terminal->curr_attr;
1848
1849 if (utf8.ch != terminal->last_char.ch)
1850 terminal->last_char = utf8;
1851}
1852
Callum Lowcay30eeae52011-01-07 19:46:55 +00001853static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001854escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1855{
1856 int len, i;
1857
1858 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1859 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1860 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1861 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1862 else len = 1; /* Invalid, cannot happen */
1863
1864 if (terminal->escape_length + len <= MAX_ESCAPE) {
1865 for (i = 0; i < len; i++)
1866 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1867 terminal->escape_length += len;
1868 } else if (terminal->escape_length < MAX_ESCAPE) {
1869 terminal->escape[terminal->escape_length++] = 0;
1870 }
1871}
1872
1873static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001874terminal_data(struct terminal *terminal, const char *data, size_t length)
1875{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001876 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001877 union utf8_char utf8;
1878 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001879
1880 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001881 parser_state =
1882 utf8_next_char(&terminal->state_machine, data[i]);
1883 switch(parser_state) {
1884 case utf8state_accept:
1885 utf8.ch = terminal->state_machine.s.ch;
1886 break;
1887 case utf8state_reject:
1888 /* the unicode replacement character */
1889 utf8.byte[0] = 0xEF;
1890 utf8.byte[1] = 0xBF;
1891 utf8.byte[2] = 0xBD;
1892 utf8.byte[3] = 0x00;
1893 break;
1894 default:
1895 continue;
1896 }
1897
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001898 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001899 switch (terminal->state) {
1900 case escape_state_escape:
1901 escape_append_utf8(terminal, utf8);
1902 switch (utf8.byte[0]) {
1903 case 'P': /* DCS */
1904 terminal->state = escape_state_dcs;
1905 break;
1906 case '[': /* CSI */
1907 terminal->state = escape_state_csi;
1908 break;
1909 case ']': /* OSC */
1910 terminal->state = escape_state_osc;
1911 break;
1912 case '#':
1913 case '(':
1914 case ')': /* special */
1915 terminal->state = escape_state_special;
1916 break;
1917 case '^': /* PM (not implemented) */
1918 case '_': /* APC (not implemented) */
1919 terminal->state = escape_state_ignore;
1920 break;
1921 default:
1922 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001923 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13001924 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001925 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001926 continue;
1927 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001928 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1929 /* do nothing */
1930 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001931 terminal->escape_flags |= ESC_FLAG_WHAT;
1932 } else if (utf8.byte[0] == '>') {
1933 terminal->escape_flags |= ESC_FLAG_GT;
1934 } else if (utf8.byte[0] == '!') {
1935 terminal->escape_flags |= ESC_FLAG_BANG;
1936 } else if (utf8.byte[0] == '$') {
1937 terminal->escape_flags |= ESC_FLAG_CASH;
1938 } else if (utf8.byte[0] == '\'') {
1939 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1940 } else if (utf8.byte[0] == '"') {
1941 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1942 } else if (utf8.byte[0] == ' ') {
1943 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001944 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001945 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001946 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13001947 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001948 }
1949
1950 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1951 utf8.byte[0] == '`')
1952 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001953 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001954 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001955 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001956 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001957 continue;
1958 case escape_state_inner_escape:
1959 if (utf8.byte[0] == '\\') {
1960 terminal->state = escape_state_normal;
1961 if (terminal->outer_state == escape_state_dcs) {
1962 handle_dcs(terminal);
1963 } else if (terminal->outer_state == escape_state_osc) {
1964 handle_osc(terminal);
1965 }
1966 } else if (utf8.byte[0] == '\e') {
1967 terminal->state = terminal->outer_state;
1968 escape_append_utf8(terminal, utf8);
1969 if (terminal->escape_length >= MAX_ESCAPE)
1970 terminal->state = escape_state_normal;
1971 } else {
1972 terminal->state = terminal->outer_state;
1973 if (terminal->escape_length < MAX_ESCAPE)
1974 terminal->escape[terminal->escape_length++] = '\e';
1975 escape_append_utf8(terminal, utf8);
1976 if (terminal->escape_length >= MAX_ESCAPE)
1977 terminal->state = escape_state_normal;
1978 }
1979 continue;
1980 case escape_state_dcs:
1981 case escape_state_osc:
1982 case escape_state_ignore:
1983 if (utf8.byte[0] == '\e') {
1984 terminal->outer_state = terminal->state;
1985 terminal->state = escape_state_inner_escape;
1986 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
1987 terminal->state = escape_state_normal;
1988 handle_osc(terminal);
1989 } else {
1990 escape_append_utf8(terminal, utf8);
1991 if (terminal->escape_length >= MAX_ESCAPE)
1992 terminal->state = escape_state_normal;
1993 }
1994 continue;
1995 case escape_state_special:
1996 escape_append_utf8(terminal, utf8);
1997 terminal->state = escape_state_normal;
1998 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
1999 handle_special_escape(terminal, terminal->escape[1],
2000 utf8.byte[0]);
2001 }
2002 continue;
2003 default:
2004 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002005 }
2006
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002007 /* this is valid, because ASCII characters are never used to
2008 * introduce a multibyte sequence in UTF-8 */
2009 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002010 terminal->state = escape_state_escape;
2011 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002012 terminal->escape[0] = '\e';
2013 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002014 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002015 } else {
2016 handle_char(terminal, utf8);
2017 } /* if */
2018 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002019
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002020 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002021}
2022
2023static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002024data_source_target(void *data,
2025 struct wl_data_source *source, const char *mime_type)
2026{
2027 fprintf(stderr, "data_source_target, %s\n", mime_type);
2028}
2029
2030static void
2031data_source_send(void *data,
2032 struct wl_data_source *source,
2033 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002034{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002035 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002036
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002037 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002038}
2039
2040static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002041data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002042{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002043 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002044}
2045
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002046static const struct wl_data_source_listener data_source_listener = {
2047 data_source_target,
2048 data_source_send,
2049 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002050};
2051
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002052static int
2053handle_bound_key(struct terminal *terminal,
2054 struct input *input, uint32_t sym, uint32_t time)
2055{
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002056 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002057 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002058 /* Cut selection; terminal doesn't do cut, fall
2059 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002060 case XKB_KEY_C:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002061 terminal->selection =
2062 display_create_data_source(terminal->display);
2063 wl_data_source_offer(terminal->selection,
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002064 "text/plain;charset=utf-8");
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002065 wl_data_source_add_listener(terminal->selection,
2066 &data_source_listener, terminal);
2067 input_set_selection(input, terminal->selection, time);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002068 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002069 case XKB_KEY_V:
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002070 input_receive_selection_data_to_fd(input,
2071 "text/plain;charset=utf-8",
2072 terminal->master);
2073
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002074 return 1;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002075 default:
2076 return 0;
2077 }
2078}
2079
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002080static void
2081key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002082 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2083 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002084{
2085 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002086 char ch[MAX_RESPONSE];
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002087 uint32_t modifiers;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002088 int len = 0;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002089
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002090 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002091 if ((modifiers & MOD_CONTROL_MASK) &&
2092 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002093 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2094 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002095 return;
2096
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002097 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002098 case XKB_KEY_F11:
Daniel Stonec9785ea2012-05-30 16:31:52 +01002099 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002100 break;
2101 terminal->fullscreen ^= 1;
2102 window_set_fullscreen(window, terminal->fullscreen);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002103 break;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002104
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002105 case XKB_KEY_BackSpace:
2106 case XKB_KEY_Tab:
2107 case XKB_KEY_Linefeed:
2108 case XKB_KEY_Clear:
2109 case XKB_KEY_Pause:
2110 case XKB_KEY_Scroll_Lock:
2111 case XKB_KEY_Sys_Req:
2112 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002113 ch[len++] = sym & 0x7f;
2114 break;
2115
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002116 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002117 if (terminal->mode & MODE_LF_NEWLINE) {
2118 ch[len++] = 0x0D;
2119 ch[len++] = 0x0A;
2120 } else {
2121 ch[len++] = 0x0D;
2122 }
2123 break;
2124
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002125 case XKB_KEY_Shift_L:
2126 case XKB_KEY_Shift_R:
2127 case XKB_KEY_Control_L:
2128 case XKB_KEY_Control_R:
2129 case XKB_KEY_Alt_L:
2130 case XKB_KEY_Alt_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002131 break;
2132
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002133 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002134 len = function_key_response('[', 2, modifiers, '~', ch);
2135 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002136 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002137 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2138 ch[len++] = '\x04';
2139 } else {
2140 len = function_key_response('[', 3, modifiers, '~', ch);
2141 }
2142 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002143 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002144 len = function_key_response('[', 5, modifiers, '~', ch);
2145 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002146 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002147 len = function_key_response('[', 6, modifiers, '~', ch);
2148 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002149 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002150 len = function_key_response('O', 1, modifiers, 'P', ch);
2151 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002152 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002153 len = function_key_response('O', 1, modifiers, 'Q', ch);
2154 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002155 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002156 len = function_key_response('O', 1, modifiers, 'R', ch);
2157 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002158 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002159 len = function_key_response('O', 1, modifiers, 'S', ch);
2160 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002161 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002162 len = function_key_response('[', 15, modifiers, '~', ch);
2163 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002164 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002165 len = function_key_response('[', 17, modifiers, '~', ch);
2166 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002167 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002168 len = function_key_response('[', 18, modifiers, '~', ch);
2169 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002170 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002171 len = function_key_response('[', 19, modifiers, '~', ch);
2172 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002173 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002174 len = function_key_response('[', 20, modifiers, '~', ch);
2175 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002176 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002177 len = function_key_response('[', 21, modifiers, '~', ch);
2178 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002179 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002180 len = function_key_response('[', 24, modifiers, '~', ch);
2181 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002182 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002183 /* Handle special keys with alternate mappings */
2184 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2185 if (len != 0) break;
2186
Kristian Høgsberg70163132012-05-08 15:55:39 -04002187 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002188 if (sym >= '3' && sym <= '7')
2189 sym = (sym & 0x1f) + 8;
2190
2191 if (!((sym >= '!' && sym <= '/') ||
2192 (sym >= '8' && sym <= '?') ||
2193 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2194 else if (sym == '2') sym = 0x00;
2195 else if (sym == '/') sym = 0x1F;
2196 else if (sym == '8' || sym == '?') sym = 0x7F;
2197 } else if ((terminal->mode & MODE_ALT_SENDS_ESC) &&
Kristian Høgsberg70163132012-05-08 15:55:39 -04002198 (modifiers & MOD_ALT_MASK))
Callum Lowcay7e08e902011-01-07 19:47:02 +00002199 {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002200 ch[len++] = 0x1b;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002201 } else if (modifiers & MOD_ALT_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002202 sym = sym | 0x80;
2203 }
2204
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002205 if (sym < 256)
2206 ch[len++] = sym;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002207 break;
2208 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002209
Daniel Stonec9785ea2012-05-30 16:31:52 +01002210 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0)
Kristian Høgsberg26130862011-08-24 11:30:21 -04002211 terminal_write(terminal, ch, len);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002212}
2213
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002214static void
2215keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002216 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002217{
2218 struct terminal *terminal = data;
2219
2220 terminal->focused = (device != NULL);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002221 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002222}
2223
Kristian Høgsberg59826582011-01-20 11:56:57 -05002224static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002225button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002226 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002227 uint32_t button,
2228 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002229{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002230 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002231
2232 switch (button) {
2233 case 272:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002234 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002235 terminal->dragging = 1;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002236 input_get_position(input,
2237 &terminal->selection_start_x,
2238 &terminal->selection_start_y);
2239 terminal->selection_end_x = terminal->selection_start_x;
2240 terminal->selection_end_y = terminal->selection_start_y;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002241 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002242 } else {
2243 terminal->dragging = 0;
2244 }
2245 break;
2246 }
2247}
2248
2249static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002250motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002251 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002252 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002253{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002254 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002255
2256 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002257 input_get_position(input,
2258 &terminal->selection_end_x,
2259 &terminal->selection_end_y);
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002260 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002261 }
2262
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002263 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002264}
2265
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002266static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002267terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002268{
2269 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002270 cairo_surface_t *surface;
2271 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002272
2273 terminal = malloc(sizeof *terminal);
2274 if (terminal == NULL)
2275 return terminal;
2276
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002277 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002278 terminal->fullscreen = fullscreen;
Callum Lowcay30eeae52011-01-07 19:46:55 +00002279 terminal->color_scheme = &DEFAULT_COLORS;
2280 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002281 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002282 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002283 terminal->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002284 terminal->widget = frame_create(terminal->window, terminal);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002285 window_set_title(terminal->window, "Wayland Terminal");
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002286 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002287
2288 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002289 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002290
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002291 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002292 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002293
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002294 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002295 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002296 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002297 keyboard_focus_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002298 widget_set_redraw_handler(terminal->widget, redraw_handler);
2299 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002300 widget_set_button_handler(terminal->widget, button_handler);
2301 widget_set_motion_handler(terminal->widget, motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002302
Kristian Høgsberg09531622010-06-14 23:22:15 -04002303 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2304 cr = cairo_create(surface);
Kristian Høgsberg09531622010-06-14 23:22:15 -04002305 cairo_set_font_size(cr, 14);
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002306 cairo_select_font_face (cr, "mono",
2307 CAIRO_FONT_SLANT_NORMAL,
2308 CAIRO_FONT_WEIGHT_BOLD);
2309 terminal->font_bold = cairo_get_scaled_font (cr);
2310 cairo_scaled_font_reference(terminal->font_bold);
2311
2312 cairo_select_font_face (cr, "mono",
2313 CAIRO_FONT_SLANT_NORMAL,
2314 CAIRO_FONT_WEIGHT_NORMAL);
2315 terminal->font_normal = cairo_get_scaled_font (cr);
2316 cairo_scaled_font_reference(terminal->font_normal);
2317
Kristian Høgsberg09531622010-06-14 23:22:15 -04002318 cairo_font_extents(cr, &terminal->extents);
2319 cairo_destroy(cr);
2320 cairo_surface_destroy(surface);
2321
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002322 window_schedule_resize(terminal->window, 500, 400);
2323
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002324 return terminal;
2325}
2326
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002327static void
2328io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002329{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002330 struct terminal *terminal =
2331 container_of(task, struct terminal, io_task);
2332 char buffer[256];
2333 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002334
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002335 if (events & EPOLLHUP)
2336 exit(0);
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002337
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002338 len = read(terminal->master, buffer, sizeof buffer);
2339 if (len < 0)
2340 exit(0);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002341
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002342 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002343}
2344
2345static int
2346terminal_run(struct terminal *terminal, const char *path)
2347{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002348 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002349 pid_t pid;
2350
2351 pid = forkpty(&master, NULL, NULL, NULL);
2352 if (pid == 0) {
Callum Lowcay86653ed2011-01-07 19:47:03 +00002353 setenv("TERM", "xterm-256color", 1);
2354 setenv("COLORTERM", "xterm-256color", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002355 if (execl(path, path, NULL)) {
2356 printf("exec failed: %m\n");
2357 exit(EXIT_FAILURE);
2358 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002359 } else if (pid < 0) {
2360 fprintf(stderr, "failed to fork and create pty (%m).\n");
2361 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002362 }
2363
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002364 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002365 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002366 terminal->io_task.run = io_handler;
2367 display_watch_fd(terminal->display, terminal->master,
2368 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002369
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002370 window_set_fullscreen(terminal->window, terminal->fullscreen);
2371 if (!terminal->fullscreen)
2372 terminal_resize(terminal, 80, 24);
2373
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002374 return 0;
2375}
2376
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002377static const struct weston_option terminal_options[] = {
2378 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002379};
2380
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002381int main(int argc, char *argv[])
2382{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002383 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002384 struct terminal *terminal;
Peter Hutterer035ac942012-02-03 20:58:19 +10002385 const char *shell;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002386
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002387 argc = parse_options(terminal_options,
2388 ARRAY_LENGTH(terminal_options), argc, argv);
2389
2390 d = display_create(argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002391 if (d == NULL) {
2392 fprintf(stderr, "failed to create display: %m\n");
2393 return -1;
2394 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002395
Peter Hutterer035ac942012-02-03 20:58:19 +10002396 shell = getenv("SHELL");
2397 if (!shell)
2398 shell = "/bin/bash";
2399
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002400 terminal = terminal_create(d, option_fullscreen);
Peter Hutterer035ac942012-02-03 20:58:19 +10002401 if (terminal_run(terminal, shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002402 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002403
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002404 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002405
2406 return 0;
2407}