blob: 7e1c741d137610528bfa63fe16ce899f99d3db0a [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
Alexander Preisingere2b88c02012-06-18 20:59:26 +0200775 if (width < 200)
776 width = 200;
777
778 if (height < 50)
779 height = 50;
780
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500781 m = 2 * terminal->margin;
782 columns = (width - m) / (int32_t) terminal->extents.max_x_advance;
783 rows = (height - m) / (int32_t) terminal->extents.height;
784
785 if (!terminal->fullscreen) {
786 width = columns * terminal->extents.max_x_advance + m;
787 height = rows * terminal->extents.height + m;
788 widget_set_size(terminal->widget, width, height);
789 }
790
791 terminal_resize_cells(terminal, columns, rows);
792}
793
794static void
795terminal_resize(struct terminal *terminal, int columns, int rows)
796{
797 int32_t width, height, m;
798
799 if (terminal->fullscreen)
800 return;
801
802 m = 2 * terminal->margin;
803 width = columns * terminal->extents.max_x_advance + m;
804 height = rows * terminal->extents.height + m;
805 widget_schedule_resize(terminal->widget, width, height);
806}
807
Callum Lowcay30eeae52011-01-07 19:46:55 +0000808struct color_scheme DEFAULT_COLORS = {
809 {
810 {0, 0, 0, 1}, /* black */
811 {0.66, 0, 0, 1}, /* red */
812 {0 , 0.66, 0, 1}, /* green */
813 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
814 {0 , 0 , 0.66, 1}, /* blue */
815 {0.66, 0 , 0.66, 1}, /* magenta */
816 {0, 0.66, 0.66, 1}, /* cyan */
817 {0.66, 0.66, 0.66, 1}, /* light grey */
818 {0.22, 0.33, 0.33, 1}, /* dark grey */
819 {1, 0.33, 0.33, 1}, /* high red */
820 {0.33, 1, 0.33, 1}, /* high green */
821 {1, 1, 0.33, 1}, /* high yellow */
822 {0.33, 0.33, 1, 1}, /* high blue */
823 {1, 0.33, 1, 1}, /* high magenta */
824 {0.33, 1, 1, 1}, /* high cyan */
825 {1, 1, 1, 1} /* white */
826 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500827 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000828 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
829};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400830
Kristian Høgsberg22106762008-12-08 13:50:07 -0500831static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500832terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
833{
834 cairo_set_source_rgba(cr,
835 terminal->color_table[index].r,
836 terminal->color_table[index].g,
837 terminal->color_table[index].b,
838 terminal->color_table[index].a);
839}
840
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500841static void
842terminal_send_selection(struct terminal *terminal, int fd)
843{
844 int row, col;
845 union utf8_char *p_row;
846 union decoded_attr attr;
847 FILE *fp;
848 int len;
849
850 fp = fdopen(fd, "w");
851 for (row = 0; row < terminal->height; row++) {
852 p_row = terminal_get_row(terminal, row);
853 for (col = 0; col < terminal->width; col++) {
854 /* get the attributes for this character cell */
855 terminal_decode_attr(terminal, row, col, &attr);
856 if (!attr.attr.s)
857 continue;
858 len = strnlen((char *) p_row[col].byte, 4);
859 fwrite(p_row[col].byte, 1, len, fp);
860 }
861 }
862 fclose(fp);
863}
864
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500865struct glyph_run {
866 struct terminal *terminal;
867 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400868 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500869 union decoded_attr attr;
870 cairo_glyph_t glyphs[256], *g;
871};
872
873static void
874glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
875{
876 run->terminal = terminal;
877 run->cr = cr;
878 run->g = run->glyphs;
879 run->count = 0;
880 run->attr.key = 0;
881}
882
883static void
884glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
885{
886 cairo_scaled_font_t *font;
887
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500888 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
889 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300890 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500891 font = run->terminal->font_bold;
892 else
893 font = run->terminal->font_normal;
894 cairo_set_scaled_font(run->cr, font);
895 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300896 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500897
Callum Lowcay9d708b02011-01-12 20:06:17 +1300898 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
899 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500900 run->g = run->glyphs;
901 run->count = 0;
902 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300903 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500904}
905
906static void
907glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
908{
909 int num_glyphs;
910 cairo_scaled_font_t *font;
911
912 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
913
Callum Lowcay9d708b02011-01-12 20:06:17 +1300914 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500915 font = run->terminal->font_bold;
916 else
917 font = run->terminal->font_normal;
918
919 cairo_move_to(run->cr, x, y);
920 cairo_scaled_font_text_to_glyphs (font, x, y,
921 (char *) c->byte, 4,
922 &run->g, &num_glyphs,
923 NULL, NULL, NULL);
924 run->g += num_glyphs;
925 run->count += num_glyphs;
926}
927
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500928
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500929static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500930redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500931{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500932 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500933 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500934 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000935 int top_margin, side_margin;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600936 int row, col, cursor_x, cursor_y;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000937 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500938 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000939 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500940 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500941 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500942 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500943 cairo_font_extents_t extents;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500944
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500945 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500946 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500947 cr = cairo_create(surface);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500948 cairo_rectangle(cr, allocation.x, allocation.y,
949 allocation.width, allocation.height);
950 cairo_clip(cr);
951 cairo_push_group(cr);
952
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500953 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500954 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500955 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500956
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500957 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500958
Kristian Høgsberg59826582011-01-20 11:56:57 -0500959 extents = terminal->extents;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500960 side_margin = (allocation.width - terminal->width * extents.max_x_advance) / 2;
961 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500962
Callum Lowcay30eeae52011-01-07 19:46:55 +0000963 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500964 cairo_translate(cr, allocation.x + side_margin,
965 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500966 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000967 for (row = 0; row < terminal->height; row++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000968 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000969 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500970 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000971
Callum Lowcay9d708b02011-01-12 20:06:17 +1300972 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -0500973 continue;
974
Callum Lowcay9d708b02011-01-12 20:06:17 +1300975 terminal_set_color(terminal, cr, attr.attr.bg);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500976 cairo_move_to(cr, col * extents.max_x_advance,
977 row * extents.height);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000978 cairo_rel_line_to(cr, extents.max_x_advance, 0);
979 cairo_rel_line_to(cr, 0, extents.height);
980 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
981 cairo_close_path(cr);
982 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500983 }
984 }
Callum Lowcay30eeae52011-01-07 19:46:55 +0000985
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500986 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
987
988 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500989 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500990 for (row = 0; row < terminal->height; row++) {
991 p_row = terminal_get_row(terminal, row);
992 for (col = 0; col < terminal->width; col++) {
993 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500994 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500995
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500996 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000997
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500998 text_x = col * extents.max_x_advance;
999 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +13001000 if (attr.attr.a & ATTRMASK_UNDERLINE) {
1001 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +00001002 cairo_move_to(cr, text_x, (double)text_y + 1.5);
1003 cairo_line_to(cr, text_x + extents.max_x_advance, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001004 cairo_stroke(cr);
1005 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -05001006
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001007 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001008 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001009 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001010
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001011 attr.key = ~0;
1012 glyph_run_flush(&run, attr);
1013
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001014 if ((terminal->mode & MODE_SHOW_CURSOR) && !terminal->focused) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001015 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001016
Callum Lowcay30eeae52011-01-07 19:46:55 +00001017 cairo_set_line_width(cr, 1);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001018 cairo_move_to(cr, terminal->column * extents.max_x_advance + d,
1019 terminal->row * extents.height + d);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001020 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
1021 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
1022 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
1023 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001024
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001025 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001026 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001027
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001028 cairo_pop_group_to_source(cr);
1029 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001030 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001031 cairo_surface_destroy(surface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001032
1033 if (terminal->send_cursor_position) {
1034 cursor_x = side_margin + allocation.x +
1035 terminal->column * extents.max_x_advance;
1036 cursor_y = top_margin + allocation.y +
1037 terminal->row * extents.height;
1038 window_set_text_cursor_position(terminal->window,
1039 cursor_x, cursor_y);
1040 terminal->send_cursor_position = 0;
1041 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001042}
1043
1044static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001045terminal_write(struct terminal *terminal, const char *data, size_t length)
1046{
1047 if (write(terminal->master, data, length) < 0)
1048 abort();
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001049 terminal->send_cursor_position = 1;
Kristian Høgsberg26130862011-08-24 11:30:21 -04001050}
1051
1052static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001053terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001054
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001055static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001056handle_char(struct terminal *terminal, union utf8_char utf8);
1057
1058static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001059handle_sgr(struct terminal *terminal, int code);
1060
1061static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001062handle_term_parameter(struct terminal *terminal, int code, int sr)
1063{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001064 int i;
1065
Callum Lowcay67a201d2011-01-12 19:23:41 +13001066 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001067 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001068 case 1: /* DECCKM */
1069 if (sr) terminal->key_mode = KM_APPLICATION;
1070 else terminal->key_mode = KM_NORMAL;
1071 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001072 case 2: /* DECANM */
1073 /* No VT52 support yet */
1074 terminal->g0 = CS_US;
1075 terminal->g1 = CS_US;
1076 terminal->cs = terminal->g0;
1077 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001078 case 3: /* DECCOLM */
1079 if (sr)
1080 terminal_resize(terminal, 132, 24);
1081 else
1082 terminal_resize(terminal, 80, 24);
1083
1084 /* set columns, but also home cursor and clear screen */
1085 terminal->row = 0; terminal->column = 0;
1086 for (i = 0; i < terminal->height; i++) {
1087 memset(terminal_get_row(terminal, i),
1088 0, terminal->data_pitch);
1089 attr_init(terminal_get_attr_row(terminal, i),
1090 terminal->curr_attr, terminal->width);
1091 }
1092 break;
1093 case 5: /* DECSCNM */
1094 if (sr) terminal->mode |= MODE_INVERSE;
1095 else terminal->mode &= ~MODE_INVERSE;
1096 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001097 case 6: /* DECOM */
1098 terminal->origin_mode = sr;
1099 if (terminal->origin_mode)
1100 terminal->row = terminal->margin_top;
1101 else
1102 terminal->row = 0;
1103 terminal->column = 0;
1104 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001105 case 7: /* DECAWM */
1106 if (sr) terminal->mode |= MODE_AUTOWRAP;
1107 else terminal->mode &= ~MODE_AUTOWRAP;
1108 break;
1109 case 8: /* DECARM */
1110 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1111 else terminal->mode &= ~MODE_AUTOREPEAT;
1112 break;
1113 case 25:
1114 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1115 else terminal->mode &= ~MODE_SHOW_CURSOR;
1116 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001117 case 1037: /* deleteSendsDel */
1118 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1119 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1120 break;
1121 case 1039: /* altSendsEscape */
1122 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1123 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1124 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001125 default:
1126 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1127 break;
1128 }
1129 } else {
1130 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001131 case 4: /* IRM */
1132 if (sr) terminal->mode |= MODE_IRM;
1133 else terminal->mode &= ~MODE_IRM;
1134 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001135 case 20: /* LNM */
1136 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1137 else terminal->mode &= ~MODE_LF_NEWLINE;
1138 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001139 default:
1140 fprintf(stderr, "Unknown parameter: %d\n", code);
1141 break;
1142 }
1143 }
1144}
1145
1146static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001147handle_dcs(struct terminal *terminal)
1148{
1149}
1150
1151static void
1152handle_osc(struct terminal *terminal)
1153{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001154 char *p;
1155 int code;
1156
1157 terminal->escape[terminal->escape_length++] = '\0';
1158 p = &terminal->escape[2];
1159 code = strtol(p, &p, 10);
1160 if (*p == ';') p++;
1161
1162 switch (code) {
1163 case 0: /* Icon name and window title */
1164 case 1: /* Icon label */
1165 case 2: /* Window title*/
1166 window_set_title(terminal->window, p);
1167 break;
1168 default:
1169 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1170 break;
1171 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001172}
1173
1174static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001175handle_escape(struct terminal *terminal)
1176{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001177 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001178 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001179 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001180 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001181 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001182 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001183 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001184
1185 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001186 i = 0;
1187 p = &terminal->escape[2];
1188 while ((isdigit(*p) || *p == ';') && i < 10) {
1189 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001190 if (!set[i]) {
1191 args[i] = 0;
1192 set[i] = 1;
1193 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001194 p++;
1195 i++;
1196 } else {
1197 args[i] = strtol(p, &p, 10);
1198 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001199 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001200 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001201
1202 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001203 case '@': /* ICH */
1204 count = set[0] ? args[0] : 1;
1205 if (count == 0) count = 1;
1206 terminal_shift_line(terminal, count);
1207 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001208 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001209 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001210 if (count == 0) count = 1;
1211 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001212 terminal->row -= count;
1213 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001214 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001215 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001216 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001217 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001218 if (count == 0) count = 1;
1219 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001220 terminal->row += count;
1221 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001222 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001223 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001224 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001225 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001226 if (count == 0) count = 1;
1227 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001228 terminal->column += count;
1229 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001230 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001231 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001232 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001233 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001234 if (count == 0) count = 1;
1235 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001236 terminal->column -= count;
1237 else
1238 terminal->column = 0;
1239 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001240 case 'E': /* CNL */
1241 count = set[0] ? args[0] : 1;
1242 if (terminal->row + count <= terminal->margin_bottom)
1243 terminal->row += count;
1244 else
1245 terminal->row = terminal->margin_bottom;
1246 terminal->column = 0;
1247 break;
1248 case 'F': /* CPL */
1249 count = set[0] ? args[0] : 1;
1250 if (terminal->row - count >= terminal->margin_top)
1251 terminal->row -= count;
1252 else
1253 terminal->row = terminal->margin_top;
1254 terminal->column = 0;
1255 break;
1256 case 'G': /* CHA */
1257 y = set[0] ? args[0] : 1;
1258 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1259
1260 terminal->column = y - 1;
1261 break;
1262 case 'f': /* HVP */
1263 case 'H': /* CUP */
1264 x = (set[1] ? args[1] : 1) - 1;
1265 x = x < 0 ? 0 :
1266 (x >= terminal->width ? terminal->width - 1 : x);
1267
1268 y = (set[0] ? args[0] : 1) - 1;
1269 if (terminal->origin_mode) {
1270 y += terminal->margin_top;
1271 y = y < terminal->margin_top ? terminal->margin_top :
1272 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1273 } else {
1274 y = y < 0 ? 0 :
1275 (y >= terminal->height ? terminal->height - 1 : y);
1276 }
1277
1278 terminal->row = y;
1279 terminal->column = x;
1280 break;
1281 case 'I': /* CHT */
1282 count = set[0] ? args[0] : 1;
1283 if (count == 0) count = 1;
1284 while (count > 0 && terminal->column < terminal->width) {
1285 if (terminal->tab_ruler[terminal->column]) count--;
1286 terminal->column++;
1287 }
1288 terminal->column--;
1289 break;
1290 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001291 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001292 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001293 if (!set[0] || args[0] == 0 || args[0] > 2) {
1294 memset(&row[terminal->column],
1295 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1296 attr_init(&attr_row[terminal->column],
1297 terminal->curr_attr, terminal->width - terminal->column);
1298 for (i = terminal->row + 1; i < terminal->height; i++) {
1299 memset(terminal_get_row(terminal, i),
1300 0, terminal->data_pitch);
1301 attr_init(terminal_get_attr_row(terminal, i),
1302 terminal->curr_attr, terminal->width);
1303 }
1304 } else if (args[0] == 1) {
1305 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1306 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1307 for (i = 0; i < terminal->row; i++) {
1308 memset(terminal_get_row(terminal, i),
1309 0, terminal->data_pitch);
1310 attr_init(terminal_get_attr_row(terminal, i),
1311 terminal->curr_attr, terminal->width);
1312 }
1313 } else if (args[0] == 2) {
1314 for (i = 0; i < terminal->height; i++) {
1315 memset(terminal_get_row(terminal, i),
1316 0, terminal->data_pitch);
1317 attr_init(terminal_get_attr_row(terminal, i),
1318 terminal->curr_attr, terminal->width);
1319 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001320 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001321 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001322 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001323 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001324 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001325 if (!set[0] || args[0] == 0 || args[0] > 2) {
1326 memset(&row[terminal->column], 0,
1327 (terminal->width - terminal->column) * sizeof(union utf8_char));
1328 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1329 terminal->width - terminal->column);
1330 } else if (args[0] == 1) {
1331 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1332 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1333 } else if (args[0] == 2) {
1334 memset(row, 0, terminal->data_pitch);
1335 attr_init(attr_row, terminal->curr_attr, terminal->width);
1336 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001337 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001338 case 'L': /* IL */
1339 count = set[0] ? args[0] : 1;
1340 if (count == 0) count = 1;
1341 if (terminal->row >= terminal->margin_top &&
1342 terminal->row < terminal->margin_bottom)
1343 {
1344 top = terminal->margin_top;
1345 terminal->margin_top = terminal->row;
1346 terminal_scroll(terminal, 0 - count);
1347 terminal->margin_top = top;
1348 } else if (terminal->row == terminal->margin_bottom) {
1349 memset(terminal_get_row(terminal, terminal->row),
1350 0, terminal->data_pitch);
1351 attr_init(terminal_get_attr_row(terminal, terminal->row),
1352 terminal->curr_attr, terminal->width);
1353 }
1354 break;
1355 case 'M': /* DL */
1356 count = set[0] ? args[0] : 1;
1357 if (count == 0) count = 1;
1358 if (terminal->row >= terminal->margin_top &&
1359 terminal->row < terminal->margin_bottom)
1360 {
1361 top = terminal->margin_top;
1362 terminal->margin_top = terminal->row;
1363 terminal_scroll(terminal, count);
1364 terminal->margin_top = top;
1365 } else if (terminal->row == terminal->margin_bottom) {
1366 memset(terminal_get_row(terminal, terminal->row),
1367 0, terminal->data_pitch);
1368 }
1369 break;
1370 case 'P': /* DCH */
1371 count = set[0] ? args[0] : 1;
1372 if (count == 0) count = 1;
1373 terminal_shift_line(terminal, 0 - count);
1374 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001375 case 'S': /* SU */
1376 terminal_scroll(terminal, set[0] ? args[0] : 1);
1377 break;
1378 case 'T': /* SD */
1379 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1380 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001381 case 'X': /* ECH */
1382 count = set[0] ? args[0] : 1;
1383 if (count == 0) count = 1;
1384 if ((terminal->column + count) > terminal->width)
1385 count = terminal->width - terminal->column;
1386 row = terminal_get_row(terminal, terminal->row);
1387 attr_row = terminal_get_attr_row(terminal, terminal->row);
1388 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1389 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1390 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001391 case 'Z': /* CBT */
1392 count = set[0] ? args[0] : 1;
1393 if (count == 0) count = 1;
1394 while (count > 0 && terminal->column >= 0) {
1395 if (terminal->tab_ruler[terminal->column]) count--;
1396 terminal->column--;
1397 }
1398 terminal->column++;
1399 break;
1400 case '`': /* HPA */
1401 y = set[0] ? args[0] : 1;
1402 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1403
1404 terminal->column = y - 1;
1405 break;
1406 case 'b': /* REP */
1407 count = set[0] ? args[0] : 1;
1408 if (count == 0) count = 1;
1409 if (terminal->last_char.byte[0])
1410 for (i = 0; i < count; i++)
1411 handle_char(terminal, terminal->last_char);
1412 terminal->last_char.byte[0] = 0;
1413 break;
1414 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001415 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001416 break;
1417 case 'd': /* VPA */
1418 x = set[0] ? args[0] : 1;
1419 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1420
1421 terminal->row = x - 1;
1422 break;
1423 case 'g': /* TBC */
1424 if (!set[0] || args[0] == 0) {
1425 terminal->tab_ruler[terminal->column] = 0;
1426 } else if (args[0] == 3) {
1427 memset(terminal->tab_ruler, 0, terminal->width);
1428 }
1429 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001430 case 'h': /* SM */
1431 for(i = 0; i < 10 && set[i]; i++) {
1432 handle_term_parameter(terminal, args[i], 1);
1433 }
1434 break;
1435 case 'l': /* RM */
1436 for(i = 0; i < 10 && set[i]; i++) {
1437 handle_term_parameter(terminal, args[i], 0);
1438 }
1439 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001440 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001441 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001442 if (i <= 7 && set[i] && set[i + 1] &&
1443 set[i + 2] && args[i + 1] == 5)
1444 {
1445 if (args[i] == 38) {
1446 handle_sgr(terminal, args[i + 2] + 256);
1447 break;
1448 } else if (args[i] == 48) {
1449 handle_sgr(terminal, args[i + 2] + 512);
1450 break;
1451 }
1452 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001453 if(set[i]) {
1454 handle_sgr(terminal, args[i]);
1455 } else if(i == 0) {
1456 handle_sgr(terminal, 0);
1457 break;
1458 } else {
1459 break;
1460 }
1461 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001462 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001463 case 'n': /* DSR */
1464 i = set[0] ? args[0] : 0;
1465 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001466 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001467 } else if (i == 6) {
1468 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1469 terminal->origin_mode ?
1470 terminal->row+terminal->margin_top : terminal->row+1,
1471 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001472 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001473 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001474 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001475 case 'r':
1476 if(!set[0]) {
1477 terminal->margin_top = 0;
1478 terminal->margin_bottom = terminal->height-1;
1479 terminal->row = 0;
1480 terminal->column = 0;
1481 } else {
1482 top = (set[0] ? args[0] : 1) - 1;
1483 top = top < 0 ? 0 :
1484 (top >= terminal->height ? terminal->height - 1 : top);
1485 bottom = (set[1] ? args[1] : 1) - 1;
1486 bottom = bottom < 0 ? 0 :
1487 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1488 if(bottom > top) {
1489 terminal->margin_top = top;
1490 terminal->margin_bottom = bottom;
1491 } else {
1492 terminal->margin_top = 0;
1493 terminal->margin_bottom = terminal->height-1;
1494 }
1495 if(terminal->origin_mode)
1496 terminal->row = terminal->margin_top;
1497 else
1498 terminal->row = 0;
1499 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001500 }
1501 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001502 case 's':
1503 terminal->saved_row = terminal->row;
1504 terminal->saved_column = terminal->column;
1505 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001506 case 't': /* windowOps */
1507 if (!set[0]) break;
1508 switch (args[0]) {
1509 case 4: /* resize px */
1510 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001511 widget_schedule_resize(terminal->widget,
1512 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001513 }
1514 break;
1515 case 8: /* resize ch */
1516 if (set[1] && set[2]) {
1517 terminal_resize(terminal, args[2], args[1]);
1518 }
1519 break;
1520 case 13: /* report position */
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[3;%d;%dt",
1523 allocation.x, allocation.y);
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 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001527 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001528 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1529 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001530 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001531 break;
1532 case 18: /* report ch */
1533 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1534 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001535 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001536 break;
1537 case 21: /* report title */
1538 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1539 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001540 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001541 break;
1542 default:
1543 if (args[0] >= 24)
1544 terminal_resize(terminal, terminal->width, args[0]);
1545 else
1546 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1547 break;
1548 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001549 case 'u':
1550 terminal->row = terminal->saved_row;
1551 terminal->column = terminal->saved_column;
1552 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001553 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001554 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001555 break;
1556 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001557}
1558
1559static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001560handle_non_csi_escape(struct terminal *terminal, char code)
1561{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001562 switch(code) {
1563 case 'M': /* RI */
1564 terminal->row -= 1;
1565 if(terminal->row < terminal->margin_top) {
1566 terminal->row = terminal->margin_top;
1567 terminal_scroll(terminal, -1);
1568 }
1569 break;
1570 case 'E': /* NEL */
1571 terminal->column = 0;
1572 // fallthrough
1573 case 'D': /* IND */
1574 terminal->row += 1;
1575 if(terminal->row > terminal->margin_bottom) {
1576 terminal->row = terminal->margin_bottom;
1577 terminal_scroll(terminal, +1);
1578 }
1579 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001580 case 'c': /* RIS */
1581 terminal_init(terminal);
1582 break;
1583 case 'H': /* HTS */
1584 terminal->tab_ruler[terminal->column] = 1;
1585 break;
1586 case '7': /* DECSC */
1587 terminal->saved_row = terminal->row;
1588 terminal->saved_column = terminal->column;
1589 terminal->saved_attr = terminal->curr_attr;
1590 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001591 terminal->saved_cs = terminal->cs;
1592 terminal->saved_g0 = terminal->g0;
1593 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001594 break;
1595 case '8': /* DECRC */
1596 terminal->row = terminal->saved_row;
1597 terminal->column = terminal->saved_column;
1598 terminal->curr_attr = terminal->saved_attr;
1599 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001600 terminal->cs = terminal->saved_cs;
1601 terminal->g0 = terminal->saved_g0;
1602 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001603 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001604 case '=': /* DECPAM */
1605 terminal->key_mode = KM_APPLICATION;
1606 break;
1607 case '>': /* DECPNM */
1608 terminal->key_mode = KM_NORMAL;
1609 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001610 default:
1611 fprintf(stderr, "Unknown escape code: %c\n", code);
1612 break;
1613 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001614}
1615
1616static void
1617handle_special_escape(struct terminal *terminal, char special, char code)
1618{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001619 int i, numChars;
1620
1621 if (special == '#') {
1622 switch(code) {
1623 case '8':
1624 /* fill with 'E', no cheap way to do this */
1625 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1626 numChars = terminal->width * terminal->height;
1627 for(i = 0; i < numChars; i++) {
1628 terminal->data[i].byte[0] = 'E';
1629 }
1630 break;
1631 default:
1632 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1633 break;
1634 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001635 } else if (special == '(' || special == ')') {
1636 switch(code) {
1637 case '0':
1638 if (special == '(')
1639 terminal->g0 = CS_SPECIAL;
1640 else
1641 terminal->g1 = CS_SPECIAL;
1642 break;
1643 case 'A':
1644 if (special == '(')
1645 terminal->g0 = CS_UK;
1646 else
1647 terminal->g1 = CS_UK;
1648 break;
1649 case 'B':
1650 if (special == '(')
1651 terminal->g0 = CS_US;
1652 else
1653 terminal->g1 = CS_US;
1654 break;
1655 default:
1656 fprintf(stderr, "Unknown character set %c\n", code);
1657 break;
1658 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001659 } else {
1660 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1661 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001662}
1663
1664static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001665handle_sgr(struct terminal *terminal, int code)
1666{
1667 switch(code) {
1668 case 0:
1669 terminal->curr_attr = terminal->color_scheme->default_attr;
1670 break;
1671 case 1:
1672 terminal->curr_attr.a |= ATTRMASK_BOLD;
1673 if (terminal->curr_attr.fg < 8)
1674 terminal->curr_attr.fg += 8;
1675 break;
1676 case 4:
1677 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1678 break;
1679 case 5:
1680 terminal->curr_attr.a |= ATTRMASK_BLINK;
1681 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001682 case 8:
1683 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1684 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001685 case 2:
1686 case 21:
1687 case 22:
1688 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1689 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1690 terminal->curr_attr.fg -= 8;
1691 break;
1692 case 24:
1693 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1694 break;
1695 case 25:
1696 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1697 break;
1698 case 7:
1699 case 26:
1700 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1701 break;
1702 case 27:
1703 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1704 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001705 case 28:
1706 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1707 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001708 case 39:
1709 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1710 break;
1711 case 49:
1712 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1713 break;
1714 default:
1715 if(code >= 30 && code <= 37) {
1716 terminal->curr_attr.fg = code - 30;
1717 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1718 terminal->curr_attr.fg += 8;
1719 } else if(code >= 40 && code <= 47) {
1720 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001721 } else if (code >= 90 && code <= 97) {
1722 terminal->curr_attr.fg = code - 90 + 8;
1723 } else if (code >= 100 && code <= 107) {
1724 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001725 } else if(code >= 256 && code < 512) {
1726 terminal->curr_attr.fg = code - 256;
1727 } else if(code >= 512 && code < 768) {
1728 terminal->curr_attr.bg = code - 512;
1729 } else {
1730 fprintf(stderr, "Unknown SGR code: %d\n", code);
1731 }
1732 break;
1733 }
1734}
1735
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001736/* Returns 1 if c was special, otherwise 0 */
1737static int
1738handle_special_char(struct terminal *terminal, char c)
1739{
1740 union utf8_char *row;
1741 struct attr *attr_row;
1742
1743 row = terminal_get_row(terminal, terminal->row);
1744 attr_row = terminal_get_attr_row(terminal, terminal->row);
1745
1746 switch(c) {
1747 case '\r':
1748 terminal->column = 0;
1749 break;
1750 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001751 if (terminal->mode & MODE_LF_NEWLINE) {
1752 terminal->column = 0;
1753 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001754 /* fallthrough */
1755 case '\v':
1756 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001757 terminal->row++;
1758 if(terminal->row > terminal->margin_bottom) {
1759 terminal->row = terminal->margin_bottom;
1760 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001761 }
1762
1763 break;
1764 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001765 while (terminal->column < terminal->width) {
1766 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001767 if (terminal->mode & MODE_IRM)
1768 terminal_shift_line(terminal, +1);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001769 row[terminal->column].byte[0] = ' ';
1770 row[terminal->column].byte[1] = '\0';
1771 attr_row[terminal->column] = terminal->curr_attr;
1772 terminal->column++;
1773 }
1774 if (terminal->column >= terminal->width) {
1775 terminal->column = terminal->width - 1;
1776 }
1777
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001778 break;
1779 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001780 if (terminal->column >= terminal->width) {
1781 terminal->column = terminal->width - 2;
1782 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001783 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001784 } else if (terminal->mode & MODE_AUTOWRAP) {
1785 terminal->column = terminal->width - 1;
1786 terminal->row -= 1;
1787 if (terminal->row < terminal->margin_top) {
1788 terminal->row = terminal->margin_top;
1789 terminal_scroll(terminal, -1);
1790 }
1791 }
1792
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001793 break;
1794 case '\a':
1795 /* Bell */
1796 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001797 case '\x0E': /* SO */
1798 terminal->cs = terminal->g1;
1799 break;
1800 case '\x0F': /* SI */
1801 terminal->cs = terminal->g0;
1802 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001803 default:
1804 return 0;
1805 }
1806
1807 return 1;
1808}
1809
1810static void
1811handle_char(struct terminal *terminal, union utf8_char utf8)
1812{
1813 union utf8_char *row;
1814 struct attr *attr_row;
1815
1816 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001817
1818 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001819
1820 /* There are a whole lot of non-characters, control codes,
1821 * and formatting codes that should probably be ignored,
1822 * for example: */
1823 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1824 /* BOM, ignore */
1825 return;
1826 }
1827
1828 /* Some of these non-characters should be translated, e.g.: */
1829 if (utf8.byte[0] < 32) {
1830 utf8.byte[0] = utf8.byte[0] + 64;
1831 }
1832
1833 /* handle right margin effects */
1834 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001835 if (terminal->mode & MODE_AUTOWRAP) {
1836 terminal->column = 0;
1837 terminal->row += 1;
1838 if (terminal->row > terminal->margin_bottom) {
1839 terminal->row = terminal->margin_bottom;
1840 terminal_scroll(terminal, +1);
1841 }
1842 } else {
1843 terminal->column--;
1844 }
1845 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001846
1847 row = terminal_get_row(terminal, terminal->row);
1848 attr_row = terminal_get_attr_row(terminal, terminal->row);
1849
Callum Lowcay69e96582011-01-07 19:47:00 +00001850 if (terminal->mode & MODE_IRM)
1851 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001852 row[terminal->column] = utf8;
1853 attr_row[terminal->column++] = terminal->curr_attr;
1854
1855 if (utf8.ch != terminal->last_char.ch)
1856 terminal->last_char = utf8;
1857}
1858
Callum Lowcay30eeae52011-01-07 19:46:55 +00001859static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001860escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1861{
1862 int len, i;
1863
1864 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1865 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1866 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1867 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1868 else len = 1; /* Invalid, cannot happen */
1869
1870 if (terminal->escape_length + len <= MAX_ESCAPE) {
1871 for (i = 0; i < len; i++)
1872 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1873 terminal->escape_length += len;
1874 } else if (terminal->escape_length < MAX_ESCAPE) {
1875 terminal->escape[terminal->escape_length++] = 0;
1876 }
1877}
1878
1879static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001880terminal_data(struct terminal *terminal, const char *data, size_t length)
1881{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001882 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001883 union utf8_char utf8;
1884 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001885
1886 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001887 parser_state =
1888 utf8_next_char(&terminal->state_machine, data[i]);
1889 switch(parser_state) {
1890 case utf8state_accept:
1891 utf8.ch = terminal->state_machine.s.ch;
1892 break;
1893 case utf8state_reject:
1894 /* the unicode replacement character */
1895 utf8.byte[0] = 0xEF;
1896 utf8.byte[1] = 0xBF;
1897 utf8.byte[2] = 0xBD;
1898 utf8.byte[3] = 0x00;
1899 break;
1900 default:
1901 continue;
1902 }
1903
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001904 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001905 switch (terminal->state) {
1906 case escape_state_escape:
1907 escape_append_utf8(terminal, utf8);
1908 switch (utf8.byte[0]) {
1909 case 'P': /* DCS */
1910 terminal->state = escape_state_dcs;
1911 break;
1912 case '[': /* CSI */
1913 terminal->state = escape_state_csi;
1914 break;
1915 case ']': /* OSC */
1916 terminal->state = escape_state_osc;
1917 break;
1918 case '#':
1919 case '(':
1920 case ')': /* special */
1921 terminal->state = escape_state_special;
1922 break;
1923 case '^': /* PM (not implemented) */
1924 case '_': /* APC (not implemented) */
1925 terminal->state = escape_state_ignore;
1926 break;
1927 default:
1928 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001929 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13001930 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001931 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001932 continue;
1933 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001934 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1935 /* do nothing */
1936 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001937 terminal->escape_flags |= ESC_FLAG_WHAT;
1938 } else if (utf8.byte[0] == '>') {
1939 terminal->escape_flags |= ESC_FLAG_GT;
1940 } else if (utf8.byte[0] == '!') {
1941 terminal->escape_flags |= ESC_FLAG_BANG;
1942 } else if (utf8.byte[0] == '$') {
1943 terminal->escape_flags |= ESC_FLAG_CASH;
1944 } else if (utf8.byte[0] == '\'') {
1945 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1946 } else if (utf8.byte[0] == '"') {
1947 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1948 } else if (utf8.byte[0] == ' ') {
1949 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001950 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001951 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001952 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13001953 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001954 }
1955
1956 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1957 utf8.byte[0] == '`')
1958 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001959 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001960 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001961 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001962 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001963 continue;
1964 case escape_state_inner_escape:
1965 if (utf8.byte[0] == '\\') {
1966 terminal->state = escape_state_normal;
1967 if (terminal->outer_state == escape_state_dcs) {
1968 handle_dcs(terminal);
1969 } else if (terminal->outer_state == escape_state_osc) {
1970 handle_osc(terminal);
1971 }
1972 } else if (utf8.byte[0] == '\e') {
1973 terminal->state = terminal->outer_state;
1974 escape_append_utf8(terminal, utf8);
1975 if (terminal->escape_length >= MAX_ESCAPE)
1976 terminal->state = escape_state_normal;
1977 } else {
1978 terminal->state = terminal->outer_state;
1979 if (terminal->escape_length < MAX_ESCAPE)
1980 terminal->escape[terminal->escape_length++] = '\e';
1981 escape_append_utf8(terminal, utf8);
1982 if (terminal->escape_length >= MAX_ESCAPE)
1983 terminal->state = escape_state_normal;
1984 }
1985 continue;
1986 case escape_state_dcs:
1987 case escape_state_osc:
1988 case escape_state_ignore:
1989 if (utf8.byte[0] == '\e') {
1990 terminal->outer_state = terminal->state;
1991 terminal->state = escape_state_inner_escape;
1992 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
1993 terminal->state = escape_state_normal;
1994 handle_osc(terminal);
1995 } else {
1996 escape_append_utf8(terminal, utf8);
1997 if (terminal->escape_length >= MAX_ESCAPE)
1998 terminal->state = escape_state_normal;
1999 }
2000 continue;
2001 case escape_state_special:
2002 escape_append_utf8(terminal, utf8);
2003 terminal->state = escape_state_normal;
2004 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2005 handle_special_escape(terminal, terminal->escape[1],
2006 utf8.byte[0]);
2007 }
2008 continue;
2009 default:
2010 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002011 }
2012
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002013 /* this is valid, because ASCII characters are never used to
2014 * introduce a multibyte sequence in UTF-8 */
2015 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002016 terminal->state = escape_state_escape;
2017 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002018 terminal->escape[0] = '\e';
2019 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002020 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002021 } else {
2022 handle_char(terminal, utf8);
2023 } /* if */
2024 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002025
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002026 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002027}
2028
2029static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002030data_source_target(void *data,
2031 struct wl_data_source *source, const char *mime_type)
2032{
2033 fprintf(stderr, "data_source_target, %s\n", mime_type);
2034}
2035
2036static void
2037data_source_send(void *data,
2038 struct wl_data_source *source,
2039 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002040{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002041 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002042
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002043 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002044}
2045
2046static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002047data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002048{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002049 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002050}
2051
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002052static const struct wl_data_source_listener data_source_listener = {
2053 data_source_target,
2054 data_source_send,
2055 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002056};
2057
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002058static int
2059handle_bound_key(struct terminal *terminal,
2060 struct input *input, uint32_t sym, uint32_t time)
2061{
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002062 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002063 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002064 /* Cut selection; terminal doesn't do cut, fall
2065 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002066 case XKB_KEY_C:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002067 terminal->selection =
2068 display_create_data_source(terminal->display);
2069 wl_data_source_offer(terminal->selection,
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002070 "text/plain;charset=utf-8");
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002071 wl_data_source_add_listener(terminal->selection,
2072 &data_source_listener, terminal);
Kristian Høgsbergfee91be2012-06-02 21:21:41 -04002073 input_set_selection(input, terminal->selection,
2074 display_get_serial(terminal->display));
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002075 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002076 case XKB_KEY_V:
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002077 input_receive_selection_data_to_fd(input,
2078 "text/plain;charset=utf-8",
2079 terminal->master);
2080
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002081 return 1;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002082 default:
2083 return 0;
2084 }
2085}
2086
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002087static void
2088key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002089 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2090 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002091{
2092 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002093 char ch[MAX_RESPONSE];
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002094 uint32_t modifiers;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002095 int len = 0;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002096
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002097 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002098 if ((modifiers & MOD_CONTROL_MASK) &&
2099 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002100 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2101 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002102 return;
2103
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002104 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002105 case XKB_KEY_F11:
Daniel Stonec9785ea2012-05-30 16:31:52 +01002106 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002107 break;
2108 terminal->fullscreen ^= 1;
2109 window_set_fullscreen(window, terminal->fullscreen);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002110 break;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002111
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002112 case XKB_KEY_BackSpace:
2113 case XKB_KEY_Tab:
2114 case XKB_KEY_Linefeed:
2115 case XKB_KEY_Clear:
2116 case XKB_KEY_Pause:
2117 case XKB_KEY_Scroll_Lock:
2118 case XKB_KEY_Sys_Req:
2119 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002120 ch[len++] = sym & 0x7f;
2121 break;
2122
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002123 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002124 if (terminal->mode & MODE_LF_NEWLINE) {
2125 ch[len++] = 0x0D;
2126 ch[len++] = 0x0A;
2127 } else {
2128 ch[len++] = 0x0D;
2129 }
2130 break;
2131
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002132 case XKB_KEY_Shift_L:
2133 case XKB_KEY_Shift_R:
2134 case XKB_KEY_Control_L:
2135 case XKB_KEY_Control_R:
2136 case XKB_KEY_Alt_L:
2137 case XKB_KEY_Alt_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002138 break;
2139
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002140 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002141 len = function_key_response('[', 2, modifiers, '~', ch);
2142 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002143 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002144 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2145 ch[len++] = '\x04';
2146 } else {
2147 len = function_key_response('[', 3, modifiers, '~', ch);
2148 }
2149 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002150 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002151 len = function_key_response('[', 5, modifiers, '~', ch);
2152 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002153 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002154 len = function_key_response('[', 6, modifiers, '~', ch);
2155 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002156 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002157 len = function_key_response('O', 1, modifiers, 'P', ch);
2158 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002159 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002160 len = function_key_response('O', 1, modifiers, 'Q', ch);
2161 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002162 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002163 len = function_key_response('O', 1, modifiers, 'R', ch);
2164 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002165 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002166 len = function_key_response('O', 1, modifiers, 'S', ch);
2167 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002168 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002169 len = function_key_response('[', 15, modifiers, '~', ch);
2170 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002171 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002172 len = function_key_response('[', 17, modifiers, '~', ch);
2173 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002174 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002175 len = function_key_response('[', 18, modifiers, '~', ch);
2176 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002177 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002178 len = function_key_response('[', 19, modifiers, '~', ch);
2179 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002180 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002181 len = function_key_response('[', 20, modifiers, '~', ch);
2182 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002183 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002184 len = function_key_response('[', 21, modifiers, '~', ch);
2185 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002186 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002187 len = function_key_response('[', 24, modifiers, '~', ch);
2188 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002189 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002190 /* Handle special keys with alternate mappings */
2191 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2192 if (len != 0) break;
2193
Kristian Høgsberg70163132012-05-08 15:55:39 -04002194 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002195 if (sym >= '3' && sym <= '7')
2196 sym = (sym & 0x1f) + 8;
2197
2198 if (!((sym >= '!' && sym <= '/') ||
2199 (sym >= '8' && sym <= '?') ||
2200 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2201 else if (sym == '2') sym = 0x00;
2202 else if (sym == '/') sym = 0x1F;
2203 else if (sym == '8' || sym == '?') sym = 0x7F;
2204 } else if ((terminal->mode & MODE_ALT_SENDS_ESC) &&
Kristian Høgsberg70163132012-05-08 15:55:39 -04002205 (modifiers & MOD_ALT_MASK))
Callum Lowcay7e08e902011-01-07 19:47:02 +00002206 {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002207 ch[len++] = 0x1b;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002208 } else if (modifiers & MOD_ALT_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002209 sym = sym | 0x80;
2210 }
2211
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002212 if (sym < 256)
2213 ch[len++] = sym;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002214 break;
2215 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002216
Daniel Stonec9785ea2012-05-30 16:31:52 +01002217 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0)
Kristian Høgsberg26130862011-08-24 11:30:21 -04002218 terminal_write(terminal, ch, len);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002219}
2220
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002221static void
2222keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002223 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002224{
2225 struct terminal *terminal = data;
2226
2227 terminal->focused = (device != NULL);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002228 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002229}
2230
Kristian Høgsberg59826582011-01-20 11:56:57 -05002231static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002232button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002233 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002234 uint32_t button,
2235 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002236{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002237 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002238
2239 switch (button) {
2240 case 272:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002241 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002242 terminal->dragging = 1;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002243 input_get_position(input,
2244 &terminal->selection_start_x,
2245 &terminal->selection_start_y);
2246 terminal->selection_end_x = terminal->selection_start_x;
2247 terminal->selection_end_y = terminal->selection_start_y;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002248 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002249 } else {
2250 terminal->dragging = 0;
2251 }
2252 break;
2253 }
2254}
2255
2256static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002257motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002258 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002259 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002260{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002261 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002262
2263 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002264 input_get_position(input,
2265 &terminal->selection_end_x,
2266 &terminal->selection_end_y);
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002267 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002268 }
2269
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002270 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002271}
2272
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002273static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002274terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002275{
2276 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002277 cairo_surface_t *surface;
2278 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002279
2280 terminal = malloc(sizeof *terminal);
2281 if (terminal == NULL)
2282 return terminal;
2283
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002284 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002285 terminal->fullscreen = fullscreen;
Callum Lowcay30eeae52011-01-07 19:46:55 +00002286 terminal->color_scheme = &DEFAULT_COLORS;
2287 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002288 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002289 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002290 terminal->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002291 terminal->widget = frame_create(terminal->window, terminal);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002292 window_set_title(terminal->window, "Wayland Terminal");
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002293 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002294
2295 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002296 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002297
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002298 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002299 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002300
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002301 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002302 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002303 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002304 keyboard_focus_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002305 widget_set_redraw_handler(terminal->widget, redraw_handler);
2306 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002307 widget_set_button_handler(terminal->widget, button_handler);
2308 widget_set_motion_handler(terminal->widget, motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002309
Kristian Høgsberg09531622010-06-14 23:22:15 -04002310 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2311 cr = cairo_create(surface);
Kristian Høgsberg09531622010-06-14 23:22:15 -04002312 cairo_set_font_size(cr, 14);
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002313 cairo_select_font_face (cr, "mono",
2314 CAIRO_FONT_SLANT_NORMAL,
2315 CAIRO_FONT_WEIGHT_BOLD);
2316 terminal->font_bold = cairo_get_scaled_font (cr);
2317 cairo_scaled_font_reference(terminal->font_bold);
2318
2319 cairo_select_font_face (cr, "mono",
2320 CAIRO_FONT_SLANT_NORMAL,
2321 CAIRO_FONT_WEIGHT_NORMAL);
2322 terminal->font_normal = cairo_get_scaled_font (cr);
2323 cairo_scaled_font_reference(terminal->font_normal);
2324
Kristian Høgsberg09531622010-06-14 23:22:15 -04002325 cairo_font_extents(cr, &terminal->extents);
2326 cairo_destroy(cr);
2327 cairo_surface_destroy(surface);
2328
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002329 window_schedule_resize(terminal->window, 500, 400);
2330
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002331 return terminal;
2332}
2333
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002334static void
2335io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002336{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002337 struct terminal *terminal =
2338 container_of(task, struct terminal, io_task);
2339 char buffer[256];
2340 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002341
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002342 if (events & EPOLLHUP)
2343 exit(0);
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002344
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002345 len = read(terminal->master, buffer, sizeof buffer);
2346 if (len < 0)
2347 exit(0);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002348
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002349 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002350}
2351
2352static int
2353terminal_run(struct terminal *terminal, const char *path)
2354{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002355 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002356 pid_t pid;
2357
2358 pid = forkpty(&master, NULL, NULL, NULL);
2359 if (pid == 0) {
Callum Lowcay86653ed2011-01-07 19:47:03 +00002360 setenv("TERM", "xterm-256color", 1);
2361 setenv("COLORTERM", "xterm-256color", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002362 if (execl(path, path, NULL)) {
2363 printf("exec failed: %m\n");
2364 exit(EXIT_FAILURE);
2365 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002366 } else if (pid < 0) {
2367 fprintf(stderr, "failed to fork and create pty (%m).\n");
2368 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002369 }
2370
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002371 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002372 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002373 terminal->io_task.run = io_handler;
2374 display_watch_fd(terminal->display, terminal->master,
2375 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002376
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002377 window_set_fullscreen(terminal->window, terminal->fullscreen);
2378 if (!terminal->fullscreen)
2379 terminal_resize(terminal, 80, 24);
2380
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002381 return 0;
2382}
2383
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002384static const struct weston_option terminal_options[] = {
2385 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002386};
2387
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002388int main(int argc, char *argv[])
2389{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002390 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002391 struct terminal *terminal;
Peter Hutterer035ac942012-02-03 20:58:19 +10002392 const char *shell;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002393
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002394 argc = parse_options(terminal_options,
2395 ARRAY_LENGTH(terminal_options), argc, argv);
2396
2397 d = display_create(argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002398 if (d == NULL) {
2399 fprintf(stderr, "failed to create display: %m\n");
2400 return -1;
2401 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002402
Peter Hutterer035ac942012-02-03 20:58:19 +10002403 shell = getenv("SHELL");
2404 if (!shell)
2405 shell = "/bin/bash";
2406
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002407 terminal = terminal_create(d, option_fullscreen);
Peter Hutterer035ac942012-02-03 20:58:19 +10002408 if (terminal_run(terminal, shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002409 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002410
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002411 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002412
2413 return 0;
2414}