blob: b507ab6071cd08908b1d71f9486007485f8f76b3 [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>
34#include <glib.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040035#include <sys/epoll.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050036
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040037#include <X11/keysym.h>
38
Pekka Paalanen50719bc2011-11-22 14:18:50 +020039#include <wayland-client.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050040
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050041#include "window.h"
42
Kristian Høgsberg0395f302008-12-22 12:14:50 -050043static int option_fullscreen;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050044
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050045#define MOD_SHIFT 0x01
46#define MOD_ALT 0x02
47#define MOD_CTRL 0x04
48
Callum Lowcay30eeae52011-01-07 19:46:55 +000049#define ATTRMASK_BOLD 0x01
50#define ATTRMASK_UNDERLINE 0x02
51#define ATTRMASK_BLINK 0x04
52#define ATTRMASK_INVERSE 0x08
Callum Lowcay81179db2011-01-10 12:14:01 +130053#define ATTRMASK_CONCEALED 0x10
Callum Lowcay30eeae52011-01-07 19:46:55 +000054
Callum Lowcayb8609ad2011-01-07 19:46:57 +000055/* Buffer sizes */
Callum Lowcayef57a9b2011-01-14 20:46:23 +130056#define MAX_RESPONSE 256
57#define MAX_ESCAPE 255
Callum Lowcayb8609ad2011-01-07 19:46:57 +000058
Callum Lowcay8e57dd52011-01-07 19:46:59 +000059/* Terminal modes */
60#define MODE_SHOW_CURSOR 0x00000001
61#define MODE_INVERSE 0x00000002
62#define MODE_AUTOWRAP 0x00000004
63#define MODE_AUTOREPEAT 0x00000008
64#define MODE_LF_NEWLINE 0x00000010
Callum Lowcay69e96582011-01-07 19:47:00 +000065#define MODE_IRM 0x00000020
Callum Lowcay7e08e902011-01-07 19:47:02 +000066#define MODE_DELETE_SENDS_DEL 0x00000040
67#define MODE_ALT_SENDS_ESC 0x00000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +000068
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000069union utf8_char {
70 unsigned char byte[4];
71 uint32_t ch;
72};
73
74enum utf8_state {
75 utf8state_start,
76 utf8state_accept,
77 utf8state_reject,
78 utf8state_expect3,
79 utf8state_expect2,
80 utf8state_expect1
81};
82
83struct utf8_state_machine {
84 enum utf8_state state;
85 int len;
86 union utf8_char s;
87};
88
89static void
90init_state_machine(struct utf8_state_machine *machine)
91{
92 machine->state = utf8state_start;
93 machine->len = 0;
94 machine->s.ch = 0;
95}
96
97static enum utf8_state
98utf8_next_char(struct utf8_state_machine *machine, char c)
99{
100 switch(machine->state) {
101 case utf8state_start:
102 case utf8state_accept:
103 case utf8state_reject:
104 machine->s.ch = 0;
105 machine->len = 0;
106 if(c == 0xC0 || c == 0xC1) {
107 /* overlong encoding, reject */
108 machine->state = utf8state_reject;
109 } else if((c & 0x80) == 0) {
110 /* single byte, accept */
111 machine->s.byte[machine->len++] = c;
112 machine->state = utf8state_accept;
113 } else if((c & 0xC0) == 0x80) {
114 /* parser out of sync, ignore byte */
115 machine->state = utf8state_start;
116 } else if((c & 0xE0) == 0xC0) {
117 /* start of two byte sequence */
118 machine->s.byte[machine->len++] = c;
119 machine->state = utf8state_expect1;
120 } else if((c & 0xF0) == 0xE0) {
121 /* start of three byte sequence */
122 machine->s.byte[machine->len++] = c;
123 machine->state = utf8state_expect2;
124 } else if((c & 0xF8) == 0xF0) {
125 /* start of four byte sequence */
126 machine->s.byte[machine->len++] = c;
127 machine->state = utf8state_expect3;
128 } else {
129 /* overlong encoding, reject */
130 machine->state = utf8state_reject;
131 }
132 break;
133 case utf8state_expect3:
134 machine->s.byte[machine->len++] = c;
135 if((c & 0xC0) == 0x80) {
136 /* all good, continue */
137 machine->state = utf8state_expect2;
138 } else {
139 /* missing extra byte, reject */
140 machine->state = utf8state_reject;
141 }
142 break;
143 case utf8state_expect2:
144 machine->s.byte[machine->len++] = c;
145 if((c & 0xC0) == 0x80) {
146 /* all good, continue */
147 machine->state = utf8state_expect1;
148 } else {
149 /* missing extra byte, reject */
150 machine->state = utf8state_reject;
151 }
152 break;
153 case utf8state_expect1:
154 machine->s.byte[machine->len++] = c;
155 if((c & 0xC0) == 0x80) {
156 /* all good, accept */
157 machine->state = utf8state_accept;
158 } else {
159 /* missing extra byte, reject */
160 machine->state = utf8state_reject;
161 }
162 break;
163 default:
164 machine->state = utf8state_reject;
165 break;
166 }
167
168 return machine->state;
169}
170
Callum Lowcay256e72f2011-01-07 19:47:01 +0000171struct char_sub {
172 union utf8_char match;
173 union utf8_char replace;
174};
175/* Set last char_sub match to NULL char */
176typedef struct char_sub *character_set;
177
178struct char_sub CS_US[] = {
179 {{{0, }}, {{0, }}}
180};
181static struct char_sub CS_UK[] = {
182 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}},
183 {{{0, }}, {{0, }}}
184};
185static struct char_sub CS_SPECIAL[] = {
186 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond */
187 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell */
188 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT */
189 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF */
190 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR */
191 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF */
192 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree */
193 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus */
194 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL */
195 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT */
196 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB */
197 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT */
198 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT */
199 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_RB */
200 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS */
201 {{{'o', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
202 {{{'p', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
203 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
204 {{{'r', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
205 {{{'s', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
206 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR */
207 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL */
208 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU */
209 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD */
210 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V */
211 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE */
212 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE */
213 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI */
214 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ */
215 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND */
216 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT */
217 {{{0, }}, {{0, }}}
218};
219
220static void
221apply_char_set(character_set cs, union utf8_char *utf8)
222{
223 int i = 0;
224
225 while (cs[i].match.byte[0]) {
226 if ((*utf8).ch == cs[i].match.ch) {
227 *utf8 = cs[i].replace;
228 break;
229 }
230 i++;
231 }
232}
233
Callum Lowcay7e08e902011-01-07 19:47:02 +0000234struct key_map {
235 int sym;
236 int num;
237 char escape;
238 char code;
239};
240/* Set last key_sub sym to NULL */
241typedef struct key_map *keyboard_mode;
242
243static struct key_map KM_NORMAL[] = {
244 {XK_Left, 1, '[', 'D'},
245 {XK_Right, 1, '[', 'C'},
246 {XK_Up, 1, '[', 'A'},
247 {XK_Down, 1, '[', 'B'},
248 {XK_Home, 1, '[', 'H'},
249 {XK_End, 1, '[', 'F'},
250 {0, 0, 0, 0}
251};
252static struct key_map KM_APPLICATION[] = {
253 {XK_Left, 1, 'O', 'D'},
254 {XK_Right, 1, 'O', 'C'},
255 {XK_Up, 1, 'O', 'A'},
256 {XK_Down, 1, 'O', 'B'},
257 {XK_Home, 1, 'O', 'H'},
258 {XK_End, 1, 'O', 'F'},
259 {XK_KP_Enter, 1, 'O', 'M'},
260 {XK_KP_Multiply, 1, 'O', 'j'},
261 {XK_KP_Add, 1, 'O', 'k'},
262 {XK_KP_Separator, 1, 'O', 'l'},
263 {XK_KP_Subtract, 1, 'O', 'm'},
264 {XK_KP_Divide, 1, 'O', 'o'},
265 {0, 0, 0, 0}
266};
267
268static int
269function_key_response(char escape, int num, uint32_t modifiers,
270 char code, char *response)
271{
272 int mod_num = 0;
273 int len;
274
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -0500275 if (modifiers & XKB_COMMON_SHIFT_MASK) mod_num |= 1;
276 if (modifiers & XKB_COMMON_MOD1_MASK) mod_num |= 2;
277 if (modifiers & XKB_COMMON_CONTROL_MASK) mod_num |= 4;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000278
279 if (mod_num != 0)
280 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
281 num, mod_num + 1, code);
282 else if (code != '~')
283 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
284 escape, code);
285 else
286 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
287 escape, num, code);
288
289 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
290 else return len;
291}
292
293/* returns the number of bytes written into response,
294 * which must have room for MAX_RESPONSE bytes */
295static int
296apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
297{
298 struct key_map map;
299 int len = 0;
300 int i = 0;
301
302 while (mode[i].sym) {
303 map = mode[i++];
304 if (sym == map.sym) {
305 len = function_key_response(map.escape, map.num,
306 modifiers, map.code,
307 response);
308 break;
309 }
310 }
311
312 return len;
313}
314
Callum Lowcay30eeae52011-01-07 19:46:55 +0000315struct terminal_color { double r, g, b, a; };
316struct attr {
317 unsigned char fg, bg;
318 char a; /* attributes format:
319 * 76543210
Callum Lowcay81179db2011-01-10 12:14:01 +1300320 * cilub */
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500321 char s; /* in selection */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000322};
323struct color_scheme {
324 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500325 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000326 struct attr default_attr;
327};
328
329static void
330attr_init(struct attr *data_attr, struct attr attr, int n)
331{
332 int i;
333 for (i = 0; i < n; i++) {
334 data_attr[i] = attr;
335 }
336}
337
Callum Lowcay67a201d2011-01-12 19:23:41 +1300338enum escape_state {
339 escape_state_normal = 0,
340 escape_state_escape,
341 escape_state_dcs,
342 escape_state_csi,
343 escape_state_osc,
344 escape_state_inner_escape,
345 escape_state_ignore,
346 escape_state_special
347};
348
349#define ESC_FLAG_WHAT 0x01
350#define ESC_FLAG_GT 0x02
351#define ESC_FLAG_BANG 0x04
352#define ESC_FLAG_CASH 0x08
353#define ESC_FLAG_SQUOTE 0x10
354#define ESC_FLAG_DQUOTE 0x20
355#define ESC_FLAG_SPACE 0x40
356
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500357struct terminal {
358 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500359 struct widget *widget;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500360 struct display *display;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000361 union utf8_char *data;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400362 struct task io_task;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000363 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000364 struct attr *data_attr;
365 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000366 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000367 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000368 char saved_origin_mode;
369 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000370 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000371 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000372 character_set cs, g0, g1;
373 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000374 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000375 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500376 int width, height, start, row, column;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000377 int saved_row, saved_column;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500378 int fd, master;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500379 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300380 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500381 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300382 enum escape_state state;
383 enum escape_state outer_state;
384 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000385 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500386 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500387 int fullscreen;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500388 int focused;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400389 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000390 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400391 cairo_font_extents_t extents;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500392 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500393
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400394 struct wl_data_source *selection;
395 int32_t dragging;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500396 int selection_start_x, selection_start_y;
397 int selection_end_x, selection_end_y;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500398};
399
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000400/* Create default tab stops, every 8 characters */
401static void
402terminal_init_tabs(struct terminal *terminal)
403{
404 int i = 0;
405
406 while (i < terminal->width) {
407 if (i % 8 == 0)
408 terminal->tab_ruler[i] = 1;
409 else
410 terminal->tab_ruler[i] = 0;
411 i++;
412 }
413}
414
Callum Lowcay30eeae52011-01-07 19:46:55 +0000415static void
416terminal_init(struct terminal *terminal)
417{
418 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000419 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000420 terminal->mode = MODE_SHOW_CURSOR |
421 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000422 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000423 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000424
425 terminal->row = 0;
426 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000427
Callum Lowcay256e72f2011-01-07 19:47:01 +0000428 terminal->g0 = CS_US;
429 terminal->g1 = CS_US;
430 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000431 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000432
433 terminal->saved_g0 = terminal->g0;
434 terminal->saved_g1 = terminal->g1;
435 terminal->saved_cs = terminal->cs;
436
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000437 terminal->saved_attr = terminal->curr_attr;
438 terminal->saved_origin_mode = terminal->origin_mode;
439 terminal->saved_row = terminal->row;
440 terminal->saved_column = terminal->column;
441
442 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000443}
444
445static void
446init_color_table(struct terminal *terminal)
447{
448 int c, r;
449 struct terminal_color *color_table = terminal->color_table;
450
451 for (c = 0; c < 256; c ++) {
452 if (c < 16) {
453 color_table[c] = terminal->color_scheme->palette[c];
454 } else if (c < 232) {
455 r = c - 16;
456 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
457 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
458 color_table[c].r = ((double)(r % 6) / 6.0);
459 color_table[c].a = 1.0;
460 } else {
461 r = (c - 232) * 10 + 8;
462 color_table[c].r = ((double) r) / 256.0;
463 color_table[c].g = color_table[c].r;
464 color_table[c].b = color_table[c].r;
465 color_table[c].a = 1.0;
466 }
467 }
468}
469
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000470static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500471terminal_get_row(struct terminal *terminal, int row)
472{
473 int index;
474
475 index = (row + terminal->start) % terminal->height;
476
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000477 return &terminal->data[index * terminal->width];
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500478}
479
Callum Lowcay30eeae52011-01-07 19:46:55 +0000480static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500481terminal_get_attr_row(struct terminal *terminal, int row)
482{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000483 int index;
484
485 index = (row + terminal->start) % terminal->height;
486
487 return &terminal->data_attr[index * terminal->width];
488}
489
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500490union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300491 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500492 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500493};
494
Kristian Høgsberg59826582011-01-20 11:56:57 -0500495static int
496terminal_compare_position(struct terminal *terminal,
497 int x, int y, int32_t ref_row, int32_t ref_col)
498{
499 struct rectangle allocation;
500 int top_margin, side_margin, col, row, ref_x;
501
502 window_get_child_allocation(terminal->window, &allocation);
503 side_margin = allocation.x + (allocation.width - terminal->width * terminal->extents.max_x_advance) / 2;
504 top_margin = allocation.y + (allocation.height - terminal->height * terminal->extents.height) / 2;
505
506 col = (x - side_margin) / terminal->extents.max_x_advance;
507 row = (y - top_margin) / terminal->extents.height;
508
509 ref_x = side_margin + ref_col * terminal->extents.max_x_advance +
510 terminal->extents.max_x_advance / 2;
511
512 if (row < ref_row)
513 return -1;
514 if (row == ref_row) {
515 if (col < ref_col)
516 return -1;
517 if (col == ref_col && x < ref_x)
518 return -1;
519 }
520
521 return 1;
522}
523
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500524static void
525terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500526 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500527{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500528 struct attr attr;
529 int foreground, background, tmp;
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500530 int start_cmp, end_cmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500531
532 start_cmp =
533 terminal_compare_position(terminal,
534 terminal->selection_start_x,
535 terminal->selection_start_y,
536 row, col);
537 end_cmp =
538 terminal_compare_position(terminal,
539 terminal->selection_end_x,
540 terminal->selection_end_y,
541 row, col);
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500542 decoded->attr.s = 0;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500543 if (start_cmp < 0 && end_cmp > 0)
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500544 decoded->attr.s = 1;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500545 else if (end_cmp < 0 && start_cmp > 0)
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500546 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500547
548 /* get the attributes for this character cell */
549 attr = terminal_get_attr_row(terminal, row)[col];
550 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500551 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500552 ((terminal->mode & MODE_SHOW_CURSOR) &&
553 terminal->focused && terminal->row == row &&
554 terminal->column == col)) {
555 foreground = attr.bg;
556 background = attr.fg;
557 if (attr.a & ATTRMASK_BOLD) {
558 if (foreground <= 16) foreground |= 0x08;
559 if (background <= 16) background &= 0x07;
560 }
561 } else {
562 foreground = attr.fg;
563 background = attr.bg;
564 }
565
566 if (terminal->mode & MODE_INVERSE) {
567 tmp = foreground;
568 foreground = background;
569 background = tmp;
570 if (attr.a & ATTRMASK_BOLD) {
571 if (foreground <= 16) foreground |= 0x08;
572 if (background <= 16) background &= 0x07;
573 }
574 }
575
Callum Lowcay9d708b02011-01-12 20:06:17 +1300576 decoded->attr.fg = foreground;
577 decoded->attr.bg = background;
578 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000579}
580
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500581
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500582static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000583terminal_scroll_buffer(struct terminal *terminal, int d)
584{
585 int i;
586
587 d = d % (terminal->height + 1);
588 terminal->start = (terminal->start + d) % terminal->height;
589 if (terminal->start < 0) terminal->start = terminal->height + terminal->start;
590 if(d < 0) {
591 d = 0 - d;
592 for(i = 0; i < d; i++) {
593 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
594 attr_init(terminal_get_attr_row(terminal, i),
595 terminal->curr_attr, terminal->width);
596 }
597 } else {
598 for(i = terminal->height - d; i < terminal->height; i++) {
599 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
600 attr_init(terminal_get_attr_row(terminal, i),
601 terminal->curr_attr, terminal->width);
602 }
603 }
604}
605
606static void
607terminal_scroll_window(struct terminal *terminal, int d)
608{
609 int i;
610 int window_height;
611 int from_row, to_row;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000612
613 // scrolling range is inclusive
614 window_height = terminal->margin_bottom - terminal->margin_top + 1;
615 d = d % (window_height + 1);
616 if(d < 0) {
617 d = 0 - d;
618 to_row = terminal->margin_bottom;
619 from_row = terminal->margin_bottom - d;
620
621 for (i = 0; i < (window_height - d); i++) {
622 memcpy(terminal_get_row(terminal, to_row - i),
623 terminal_get_row(terminal, from_row - i),
624 terminal->data_pitch);
625 memcpy(terminal_get_attr_row(terminal, to_row - i),
626 terminal_get_attr_row(terminal, from_row - i),
627 terminal->attr_pitch);
628 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000629 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
630 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000631 attr_init(terminal_get_attr_row(terminal, i),
632 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000633 }
634 } else {
635 to_row = terminal->margin_top;
636 from_row = terminal->margin_top + d;
637
638 for (i = 0; i < (window_height - d); i++) {
639 memcpy(terminal_get_row(terminal, to_row + i),
640 terminal_get_row(terminal, from_row + i),
641 terminal->data_pitch);
642 memcpy(terminal_get_attr_row(terminal, to_row + i),
643 terminal_get_attr_row(terminal, from_row + i),
644 terminal->attr_pitch);
645 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000646 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
647 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000648 attr_init(terminal_get_attr_row(terminal, i),
649 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000650 }
651 }
652}
653
654static void
655terminal_scroll(struct terminal *terminal, int d)
656{
657 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
658 terminal_scroll_buffer(terminal, d);
659 else
660 terminal_scroll_window(terminal, d);
661}
662
663static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000664terminal_shift_line(struct terminal *terminal, int d)
665{
666 union utf8_char *row;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500667 struct attr *attr_row;
Callum Lowcay69e96582011-01-07 19:47:00 +0000668
669 row = terminal_get_row(terminal, terminal->row);
670 attr_row = terminal_get_attr_row(terminal, terminal->row);
671
672 if ((terminal->width + d) <= terminal->column)
673 d = terminal->column + 1 - terminal->width;
674 if ((terminal->column + d) >= terminal->width)
675 d = terminal->width - terminal->column - 1;
676
677 if (d < 0) {
678 d = 0 - d;
679 memmove(&row[terminal->column],
680 &row[terminal->column + d],
681 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
Callum Lowcay69e96582011-01-07 19:47:00 +0000682 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
683 (terminal->width - terminal->column - d) * sizeof(struct attr));
684 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
685 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
686 } else {
687 memmove(&row[terminal->column + d], &row[terminal->column],
688 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
689 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
690 (terminal->width - terminal->column - d) * sizeof(struct attr));
691 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
692 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
693 }
694}
695
696static void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500697terminal_resize(struct terminal *terminal, int width, int height)
698{
699 size_t size;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000700 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000701 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000702 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000703 int data_pitch, attr_pitch;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500704 int i, l, total_rows;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500705 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000706 struct winsize ws;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500707 int32_t pixel_width, pixel_height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500708
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500709 if (width < 1)
710 width = 1;
711 if (height < 1)
712 height = 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500713 if (terminal->width == width && terminal->height == height)
714 return;
715
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500716 if (!terminal->fullscreen) {
717 pixel_width = width *
718 terminal->extents.max_x_advance + 2 * terminal->margin;
719 pixel_height = height *
720 terminal->extents.height + 2 * terminal->margin;
721 window_set_child_size(terminal->window,
722 pixel_width, pixel_height);
723 }
724
725 window_schedule_redraw (terminal->window);
726
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000727 data_pitch = width * sizeof(union utf8_char);
728 size = data_pitch * height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500729 data = malloc(size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000730 attr_pitch = width * sizeof(struct attr);
731 data_attr = malloc(attr_pitch * height);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000732 tab_ruler = malloc(width);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500733 memset(data, 0, size);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000734 memset(tab_ruler, 0, width);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000735 attr_init(data_attr, terminal->curr_attr, width * height);
736 if (terminal->data && terminal->data_attr) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500737 if (width > terminal->width)
738 l = terminal->width;
739 else
740 l = width;
741
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500742 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500743 total_rows = height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500744 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500745 total_rows = terminal->height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500746 }
747
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000748 for (i = 0; i < total_rows; i++) {
749 memcpy(&data[width * i],
750 terminal_get_row(terminal, i),
751 l * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000752 memcpy(&data_attr[width * i],
753 terminal_get_attr_row(terminal, i),
754 l * sizeof(struct attr));
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000755 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500756
757 free(terminal->data);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000758 free(terminal->data_attr);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000759 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500760 }
761
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000762 terminal->data_pitch = data_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000763 terminal->attr_pitch = attr_pitch;
Callum Lowcay86653ed2011-01-07 19:47:03 +0000764 terminal->margin_bottom =
765 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500766 terminal->width = width;
767 terminal->height = height;
768 terminal->data = data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000769 terminal->data_attr = data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000770 terminal->tab_ruler = tab_ruler;
771 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500772
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000773 /* Update the window size */
774 ws.ws_row = terminal->height;
775 ws.ws_col = terminal->width;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500776 window_get_child_allocation(terminal->window, &allocation);
777 ws.ws_xpixel = allocation.width;
778 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000779 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500780}
781
Callum Lowcay30eeae52011-01-07 19:46:55 +0000782struct color_scheme DEFAULT_COLORS = {
783 {
784 {0, 0, 0, 1}, /* black */
785 {0.66, 0, 0, 1}, /* red */
786 {0 , 0.66, 0, 1}, /* green */
787 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
788 {0 , 0 , 0.66, 1}, /* blue */
789 {0.66, 0 , 0.66, 1}, /* magenta */
790 {0, 0.66, 0.66, 1}, /* cyan */
791 {0.66, 0.66, 0.66, 1}, /* light grey */
792 {0.22, 0.33, 0.33, 1}, /* dark grey */
793 {1, 0.33, 0.33, 1}, /* high red */
794 {0.33, 1, 0.33, 1}, /* high green */
795 {1, 1, 0.33, 1}, /* high yellow */
796 {0.33, 0.33, 1, 1}, /* high blue */
797 {1, 0.33, 1, 1}, /* high magenta */
798 {0.33, 1, 1, 1}, /* high cyan */
799 {1, 1, 1, 1} /* white */
800 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500801 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000802 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
803};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400804
Kristian Høgsberg22106762008-12-08 13:50:07 -0500805static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500806terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
807{
808 cairo_set_source_rgba(cr,
809 terminal->color_table[index].r,
810 terminal->color_table[index].g,
811 terminal->color_table[index].b,
812 terminal->color_table[index].a);
813}
814
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500815static void
816terminal_send_selection(struct terminal *terminal, int fd)
817{
818 int row, col;
819 union utf8_char *p_row;
820 union decoded_attr attr;
821 FILE *fp;
822 int len;
823
824 fp = fdopen(fd, "w");
825 for (row = 0; row < terminal->height; row++) {
826 p_row = terminal_get_row(terminal, row);
827 for (col = 0; col < terminal->width; col++) {
828 /* get the attributes for this character cell */
829 terminal_decode_attr(terminal, row, col, &attr);
830 if (!attr.attr.s)
831 continue;
832 len = strnlen((char *) p_row[col].byte, 4);
833 fwrite(p_row[col].byte, 1, len, fp);
834 }
835 }
836 fclose(fp);
837}
838
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500839struct glyph_run {
840 struct terminal *terminal;
841 cairo_t *cr;
842 int count;
843 union decoded_attr attr;
844 cairo_glyph_t glyphs[256], *g;
845};
846
847static void
848glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
849{
850 run->terminal = terminal;
851 run->cr = cr;
852 run->g = run->glyphs;
853 run->count = 0;
854 run->attr.key = 0;
855}
856
857static void
858glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
859{
860 cairo_scaled_font_t *font;
861
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500862 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
863 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300864 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500865 font = run->terminal->font_bold;
866 else
867 font = run->terminal->font_normal;
868 cairo_set_scaled_font(run->cr, font);
869 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300870 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500871
Callum Lowcay9d708b02011-01-12 20:06:17 +1300872 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
873 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500874 run->g = run->glyphs;
875 run->count = 0;
876 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300877 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500878}
879
880static void
881glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
882{
883 int num_glyphs;
884 cairo_scaled_font_t *font;
885
886 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
887
Callum Lowcay9d708b02011-01-12 20:06:17 +1300888 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500889 font = run->terminal->font_bold;
890 else
891 font = run->terminal->font_normal;
892
893 cairo_move_to(run->cr, x, y);
894 cairo_scaled_font_text_to_glyphs (font, x, y,
895 (char *) c->byte, 4,
896 &run->g, &num_glyphs,
897 NULL, NULL, NULL);
898 run->g += num_glyphs;
899 run->count += num_glyphs;
900}
901
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500902static void
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500903terminal_draw_contents(struct terminal *terminal)
904{
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500905 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500906 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000907 int top_margin, side_margin;
908 int row, col;
909 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500910 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000911 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500912 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500913 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500914 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500915 cairo_font_extents_t extents;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500916
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500917 surface = window_get_surface(terminal->window);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500918 window_get_child_allocation(terminal->window, &allocation);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500919 cr = cairo_create(surface);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500920 cairo_rectangle(cr, allocation.x, allocation.y,
921 allocation.width, allocation.height);
922 cairo_clip(cr);
923 cairo_push_group(cr);
924
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500925 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500926 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500927 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500928
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500929 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500930
Kristian Høgsberg59826582011-01-20 11:56:57 -0500931 extents = terminal->extents;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500932 side_margin = (allocation.width - terminal->width * extents.max_x_advance) / 2;
933 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500934
Callum Lowcay30eeae52011-01-07 19:46:55 +0000935 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500936 cairo_translate(cr, allocation.x + side_margin,
937 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500938 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000939 for (row = 0; row < terminal->height; row++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000940 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000941 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500942 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000943
Callum Lowcay9d708b02011-01-12 20:06:17 +1300944 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -0500945 continue;
946
Callum Lowcay9d708b02011-01-12 20:06:17 +1300947 terminal_set_color(terminal, cr, attr.attr.bg);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500948 cairo_move_to(cr, col * extents.max_x_advance,
949 row * extents.height);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000950 cairo_rel_line_to(cr, extents.max_x_advance, 0);
951 cairo_rel_line_to(cr, 0, extents.height);
952 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
953 cairo_close_path(cr);
954 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500955 }
956 }
Callum Lowcay30eeae52011-01-07 19:46:55 +0000957
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500958 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
959
960 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500961 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500962 for (row = 0; row < terminal->height; row++) {
963 p_row = terminal_get_row(terminal, row);
964 for (col = 0; col < terminal->width; col++) {
965 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500966 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500967
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500968 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000969
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500970 text_x = col * extents.max_x_advance;
971 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +1300972 if (attr.attr.a & ATTRMASK_UNDERLINE) {
973 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000974 cairo_move_to(cr, text_x, (double)text_y + 1.5);
975 cairo_line_to(cr, text_x + extents.max_x_advance, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000976 cairo_stroke(cr);
977 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -0500978
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500979 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000980 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500981 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500982
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500983 attr.key = ~0;
984 glyph_run_flush(&run, attr);
985
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000986 if ((terminal->mode & MODE_SHOW_CURSOR) && !terminal->focused) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000987 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500988
Callum Lowcay30eeae52011-01-07 19:46:55 +0000989 cairo_set_line_width(cr, 1);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500990 cairo_move_to(cr, terminal->column * extents.max_x_advance + d,
991 terminal->row * extents.height + d);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000992 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
993 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
994 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
995 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500996
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500997 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000998 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500999
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001000 cairo_pop_group_to_source(cr);
1001 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001002 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001003 cairo_surface_destroy(surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001004}
1005
1006static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001007terminal_write(struct terminal *terminal, const char *data, size_t length)
1008{
1009 if (write(terminal->master, data, length) < 0)
1010 abort();
1011}
1012
1013static void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001014resize_handler(struct window *window,
1015 int32_t pixel_width, int32_t pixel_height, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001016{
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001017 struct terminal *terminal = data;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001018 int32_t width, height;
Kristian Høgsberg22106762008-12-08 13:50:07 -05001019
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001020 width = (pixel_width - 2 * terminal->margin) /
Kristian Høgsberg09531622010-06-14 23:22:15 -04001021 (int32_t) terminal->extents.max_x_advance;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001022 height = (pixel_height - 2 * terminal->margin) /
Kristian Høgsberg09531622010-06-14 23:22:15 -04001023 (int32_t) terminal->extents.height;
Tiago Vignatti5fd89d22011-01-10 19:30:04 +02001024
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001025 if (terminal->fullscreen)
1026 window_set_child_size(terminal->window,
1027 pixel_width, pixel_height);
1028
Kristian Høgsberg22106762008-12-08 13:50:07 -05001029 terminal_resize(terminal, width, height);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001030}
Kristian Høgsberg22106762008-12-08 13:50:07 -05001031
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001032static void
1033terminal_draw(struct terminal *terminal)
1034{
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001035 window_draw(terminal->window);
1036 terminal_draw_contents(terminal);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001037 window_flush(terminal->window);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001038}
1039
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001040static void
1041redraw_handler(struct window *window, void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001042{
1043 struct terminal *terminal = data;
1044
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001045 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001046}
1047
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001048static void
1049terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001050
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001051static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001052handle_char(struct terminal *terminal, union utf8_char utf8);
1053
1054static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001055handle_sgr(struct terminal *terminal, int code);
1056
1057static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001058handle_term_parameter(struct terminal *terminal, int code, int sr)
1059{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001060 int i;
1061
Callum Lowcay67a201d2011-01-12 19:23:41 +13001062 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001063 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001064 case 1: /* DECCKM */
1065 if (sr) terminal->key_mode = KM_APPLICATION;
1066 else terminal->key_mode = KM_NORMAL;
1067 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001068 case 2: /* DECANM */
1069 /* No VT52 support yet */
1070 terminal->g0 = CS_US;
1071 terminal->g1 = CS_US;
1072 terminal->cs = terminal->g0;
1073 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001074 case 3: /* DECCOLM */
1075 if (sr)
1076 terminal_resize(terminal, 132, 24);
1077 else
1078 terminal_resize(terminal, 80, 24);
1079
1080 /* set columns, but also home cursor and clear screen */
1081 terminal->row = 0; terminal->column = 0;
1082 for (i = 0; i < terminal->height; i++) {
1083 memset(terminal_get_row(terminal, i),
1084 0, terminal->data_pitch);
1085 attr_init(terminal_get_attr_row(terminal, i),
1086 terminal->curr_attr, terminal->width);
1087 }
1088 break;
1089 case 5: /* DECSCNM */
1090 if (sr) terminal->mode |= MODE_INVERSE;
1091 else terminal->mode &= ~MODE_INVERSE;
1092 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001093 case 6: /* DECOM */
1094 terminal->origin_mode = sr;
1095 if (terminal->origin_mode)
1096 terminal->row = terminal->margin_top;
1097 else
1098 terminal->row = 0;
1099 terminal->column = 0;
1100 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001101 case 7: /* DECAWM */
1102 if (sr) terminal->mode |= MODE_AUTOWRAP;
1103 else terminal->mode &= ~MODE_AUTOWRAP;
1104 break;
1105 case 8: /* DECARM */
1106 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1107 else terminal->mode &= ~MODE_AUTOREPEAT;
1108 break;
1109 case 25:
1110 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1111 else terminal->mode &= ~MODE_SHOW_CURSOR;
1112 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001113 case 1037: /* deleteSendsDel */
1114 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1115 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1116 break;
1117 case 1039: /* altSendsEscape */
1118 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1119 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1120 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001121 default:
1122 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1123 break;
1124 }
1125 } else {
1126 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001127 case 4: /* IRM */
1128 if (sr) terminal->mode |= MODE_IRM;
1129 else terminal->mode &= ~MODE_IRM;
1130 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001131 case 20: /* LNM */
1132 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1133 else terminal->mode &= ~MODE_LF_NEWLINE;
1134 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001135 default:
1136 fprintf(stderr, "Unknown parameter: %d\n", code);
1137 break;
1138 }
1139 }
1140}
1141
1142static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001143handle_dcs(struct terminal *terminal)
1144{
1145}
1146
1147static void
1148handle_osc(struct terminal *terminal)
1149{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001150 char *p;
1151 int code;
1152
1153 terminal->escape[terminal->escape_length++] = '\0';
1154 p = &terminal->escape[2];
1155 code = strtol(p, &p, 10);
1156 if (*p == ';') p++;
1157
1158 switch (code) {
1159 case 0: /* Icon name and window title */
1160 case 1: /* Icon label */
1161 case 2: /* Window title*/
1162 window_set_title(terminal->window, p);
1163 break;
1164 default:
1165 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1166 break;
1167 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001168}
1169
1170static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001171handle_escape(struct terminal *terminal)
1172{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001173 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001174 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001175 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001176 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001177 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001178 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001179 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001180
1181 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001182 i = 0;
1183 p = &terminal->escape[2];
1184 while ((isdigit(*p) || *p == ';') && i < 10) {
1185 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001186 if (!set[i]) {
1187 args[i] = 0;
1188 set[i] = 1;
1189 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001190 p++;
1191 i++;
1192 } else {
1193 args[i] = strtol(p, &p, 10);
1194 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001195 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001196 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001197
1198 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001199 case '@': /* ICH */
1200 count = set[0] ? args[0] : 1;
1201 if (count == 0) count = 1;
1202 terminal_shift_line(terminal, count);
1203 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001204 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001205 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001206 if (count == 0) count = 1;
1207 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001208 terminal->row -= count;
1209 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001210 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001211 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001212 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001213 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001214 if (count == 0) count = 1;
1215 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001216 terminal->row += count;
1217 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001218 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001219 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001220 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001221 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001222 if (count == 0) count = 1;
1223 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001224 terminal->column += count;
1225 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001226 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001227 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001228 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001229 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001230 if (count == 0) count = 1;
1231 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001232 terminal->column -= count;
1233 else
1234 terminal->column = 0;
1235 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001236 case 'E': /* CNL */
1237 count = set[0] ? args[0] : 1;
1238 if (terminal->row + count <= terminal->margin_bottom)
1239 terminal->row += count;
1240 else
1241 terminal->row = terminal->margin_bottom;
1242 terminal->column = 0;
1243 break;
1244 case 'F': /* CPL */
1245 count = set[0] ? args[0] : 1;
1246 if (terminal->row - count >= terminal->margin_top)
1247 terminal->row -= count;
1248 else
1249 terminal->row = terminal->margin_top;
1250 terminal->column = 0;
1251 break;
1252 case 'G': /* CHA */
1253 y = set[0] ? args[0] : 1;
1254 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1255
1256 terminal->column = y - 1;
1257 break;
1258 case 'f': /* HVP */
1259 case 'H': /* CUP */
1260 x = (set[1] ? args[1] : 1) - 1;
1261 x = x < 0 ? 0 :
1262 (x >= terminal->width ? terminal->width - 1 : x);
1263
1264 y = (set[0] ? args[0] : 1) - 1;
1265 if (terminal->origin_mode) {
1266 y += terminal->margin_top;
1267 y = y < terminal->margin_top ? terminal->margin_top :
1268 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1269 } else {
1270 y = y < 0 ? 0 :
1271 (y >= terminal->height ? terminal->height - 1 : y);
1272 }
1273
1274 terminal->row = y;
1275 terminal->column = x;
1276 break;
1277 case 'I': /* CHT */
1278 count = set[0] ? args[0] : 1;
1279 if (count == 0) count = 1;
1280 while (count > 0 && terminal->column < terminal->width) {
1281 if (terminal->tab_ruler[terminal->column]) count--;
1282 terminal->column++;
1283 }
1284 terminal->column--;
1285 break;
1286 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001287 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001288 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001289 if (!set[0] || args[0] == 0 || args[0] > 2) {
1290 memset(&row[terminal->column],
1291 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1292 attr_init(&attr_row[terminal->column],
1293 terminal->curr_attr, terminal->width - terminal->column);
1294 for (i = terminal->row + 1; i < terminal->height; i++) {
1295 memset(terminal_get_row(terminal, i),
1296 0, terminal->data_pitch);
1297 attr_init(terminal_get_attr_row(terminal, i),
1298 terminal->curr_attr, terminal->width);
1299 }
1300 } else if (args[0] == 1) {
1301 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1302 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1303 for (i = 0; i < terminal->row; i++) {
1304 memset(terminal_get_row(terminal, i),
1305 0, terminal->data_pitch);
1306 attr_init(terminal_get_attr_row(terminal, i),
1307 terminal->curr_attr, terminal->width);
1308 }
1309 } else if (args[0] == 2) {
1310 for (i = 0; i < terminal->height; i++) {
1311 memset(terminal_get_row(terminal, i),
1312 0, terminal->data_pitch);
1313 attr_init(terminal_get_attr_row(terminal, i),
1314 terminal->curr_attr, terminal->width);
1315 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001316 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001317 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001318 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001319 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001320 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001321 if (!set[0] || args[0] == 0 || args[0] > 2) {
1322 memset(&row[terminal->column], 0,
1323 (terminal->width - terminal->column) * sizeof(union utf8_char));
1324 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1325 terminal->width - terminal->column);
1326 } else if (args[0] == 1) {
1327 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1328 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1329 } else if (args[0] == 2) {
1330 memset(row, 0, terminal->data_pitch);
1331 attr_init(attr_row, terminal->curr_attr, terminal->width);
1332 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001333 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001334 case 'L': /* IL */
1335 count = set[0] ? args[0] : 1;
1336 if (count == 0) count = 1;
1337 if (terminal->row >= terminal->margin_top &&
1338 terminal->row < terminal->margin_bottom)
1339 {
1340 top = terminal->margin_top;
1341 terminal->margin_top = terminal->row;
1342 terminal_scroll(terminal, 0 - count);
1343 terminal->margin_top = top;
1344 } else if (terminal->row == terminal->margin_bottom) {
1345 memset(terminal_get_row(terminal, terminal->row),
1346 0, terminal->data_pitch);
1347 attr_init(terminal_get_attr_row(terminal, terminal->row),
1348 terminal->curr_attr, terminal->width);
1349 }
1350 break;
1351 case 'M': /* DL */
1352 count = set[0] ? args[0] : 1;
1353 if (count == 0) count = 1;
1354 if (terminal->row >= terminal->margin_top &&
1355 terminal->row < terminal->margin_bottom)
1356 {
1357 top = terminal->margin_top;
1358 terminal->margin_top = terminal->row;
1359 terminal_scroll(terminal, count);
1360 terminal->margin_top = top;
1361 } else if (terminal->row == terminal->margin_bottom) {
1362 memset(terminal_get_row(terminal, terminal->row),
1363 0, terminal->data_pitch);
1364 }
1365 break;
1366 case 'P': /* DCH */
1367 count = set[0] ? args[0] : 1;
1368 if (count == 0) count = 1;
1369 terminal_shift_line(terminal, 0 - count);
1370 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001371 case 'S': /* SU */
1372 terminal_scroll(terminal, set[0] ? args[0] : 1);
1373 break;
1374 case 'T': /* SD */
1375 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1376 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001377 case 'X': /* ECH */
1378 count = set[0] ? args[0] : 1;
1379 if (count == 0) count = 1;
1380 if ((terminal->column + count) > terminal->width)
1381 count = terminal->width - terminal->column;
1382 row = terminal_get_row(terminal, terminal->row);
1383 attr_row = terminal_get_attr_row(terminal, terminal->row);
1384 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1385 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1386 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001387 case 'Z': /* CBT */
1388 count = set[0] ? args[0] : 1;
1389 if (count == 0) count = 1;
1390 while (count > 0 && terminal->column >= 0) {
1391 if (terminal->tab_ruler[terminal->column]) count--;
1392 terminal->column--;
1393 }
1394 terminal->column++;
1395 break;
1396 case '`': /* HPA */
1397 y = set[0] ? args[0] : 1;
1398 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1399
1400 terminal->column = y - 1;
1401 break;
1402 case 'b': /* REP */
1403 count = set[0] ? args[0] : 1;
1404 if (count == 0) count = 1;
1405 if (terminal->last_char.byte[0])
1406 for (i = 0; i < count; i++)
1407 handle_char(terminal, terminal->last_char);
1408 terminal->last_char.byte[0] = 0;
1409 break;
1410 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001411 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001412 break;
1413 case 'd': /* VPA */
1414 x = set[0] ? args[0] : 1;
1415 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1416
1417 terminal->row = x - 1;
1418 break;
1419 case 'g': /* TBC */
1420 if (!set[0] || args[0] == 0) {
1421 terminal->tab_ruler[terminal->column] = 0;
1422 } else if (args[0] == 3) {
1423 memset(terminal->tab_ruler, 0, terminal->width);
1424 }
1425 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001426 case 'h': /* SM */
1427 for(i = 0; i < 10 && set[i]; i++) {
1428 handle_term_parameter(terminal, args[i], 1);
1429 }
1430 break;
1431 case 'l': /* RM */
1432 for(i = 0; i < 10 && set[i]; i++) {
1433 handle_term_parameter(terminal, args[i], 0);
1434 }
1435 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001436 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001437 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001438 if (i <= 7 && set[i] && set[i + 1] &&
1439 set[i + 2] && args[i + 1] == 5)
1440 {
1441 if (args[i] == 38) {
1442 handle_sgr(terminal, args[i + 2] + 256);
1443 break;
1444 } else if (args[i] == 48) {
1445 handle_sgr(terminal, args[i + 2] + 512);
1446 break;
1447 }
1448 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001449 if(set[i]) {
1450 handle_sgr(terminal, args[i]);
1451 } else if(i == 0) {
1452 handle_sgr(terminal, 0);
1453 break;
1454 } else {
1455 break;
1456 }
1457 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001458 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001459 case 'n': /* DSR */
1460 i = set[0] ? args[0] : 0;
1461 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001462 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001463 } else if (i == 6) {
1464 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1465 terminal->origin_mode ?
1466 terminal->row+terminal->margin_top : terminal->row+1,
1467 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001468 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001469 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001470 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001471 case 'r':
1472 if(!set[0]) {
1473 terminal->margin_top = 0;
1474 terminal->margin_bottom = terminal->height-1;
1475 terminal->row = 0;
1476 terminal->column = 0;
1477 } else {
1478 top = (set[0] ? args[0] : 1) - 1;
1479 top = top < 0 ? 0 :
1480 (top >= terminal->height ? terminal->height - 1 : top);
1481 bottom = (set[1] ? args[1] : 1) - 1;
1482 bottom = bottom < 0 ? 0 :
1483 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1484 if(bottom > top) {
1485 terminal->margin_top = top;
1486 terminal->margin_bottom = bottom;
1487 } else {
1488 terminal->margin_top = 0;
1489 terminal->margin_bottom = terminal->height-1;
1490 }
1491 if(terminal->origin_mode)
1492 terminal->row = terminal->margin_top;
1493 else
1494 terminal->row = 0;
1495 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001496 }
1497 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001498 case 's':
1499 terminal->saved_row = terminal->row;
1500 terminal->saved_column = terminal->column;
1501 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001502 case 't': /* windowOps */
1503 if (!set[0]) break;
1504 switch (args[0]) {
1505 case 4: /* resize px */
1506 if (set[1] && set[2]) {
1507 window_set_child_size(terminal->window,
1508 args[2], args[1]);
1509 resize_handler(terminal->window,
1510 args[2], args[1], terminal);
1511 }
1512 break;
1513 case 8: /* resize ch */
1514 if (set[1] && set[2]) {
1515 terminal_resize(terminal, args[2], args[1]);
1516 }
1517 break;
1518 case 13: /* report position */
1519 window_get_child_allocation(terminal->window, &allocation);
1520 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1521 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001522 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001523 break;
1524 case 14: /* report px */
1525 window_get_child_allocation(terminal->window, &allocation);
1526 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1527 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001528 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001529 break;
1530 case 18: /* report ch */
1531 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1532 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001533 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001534 break;
1535 case 21: /* report title */
1536 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1537 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001538 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001539 break;
1540 default:
1541 if (args[0] >= 24)
1542 terminal_resize(terminal, terminal->width, args[0]);
1543 else
1544 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1545 break;
1546 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001547 case 'u':
1548 terminal->row = terminal->saved_row;
1549 terminal->column = terminal->saved_column;
1550 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001551 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001552 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001553 break;
1554 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001555}
1556
1557static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001558handle_non_csi_escape(struct terminal *terminal, char code)
1559{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001560 switch(code) {
1561 case 'M': /* RI */
1562 terminal->row -= 1;
1563 if(terminal->row < terminal->margin_top) {
1564 terminal->row = terminal->margin_top;
1565 terminal_scroll(terminal, -1);
1566 }
1567 break;
1568 case 'E': /* NEL */
1569 terminal->column = 0;
1570 // fallthrough
1571 case 'D': /* IND */
1572 terminal->row += 1;
1573 if(terminal->row > terminal->margin_bottom) {
1574 terminal->row = terminal->margin_bottom;
1575 terminal_scroll(terminal, +1);
1576 }
1577 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001578 case 'c': /* RIS */
1579 terminal_init(terminal);
1580 break;
1581 case 'H': /* HTS */
1582 terminal->tab_ruler[terminal->column] = 1;
1583 break;
1584 case '7': /* DECSC */
1585 terminal->saved_row = terminal->row;
1586 terminal->saved_column = terminal->column;
1587 terminal->saved_attr = terminal->curr_attr;
1588 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001589 terminal->saved_cs = terminal->cs;
1590 terminal->saved_g0 = terminal->g0;
1591 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001592 break;
1593 case '8': /* DECRC */
1594 terminal->row = terminal->saved_row;
1595 terminal->column = terminal->saved_column;
1596 terminal->curr_attr = terminal->saved_attr;
1597 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001598 terminal->cs = terminal->saved_cs;
1599 terminal->g0 = terminal->saved_g0;
1600 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001601 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001602 case '=': /* DECPAM */
1603 terminal->key_mode = KM_APPLICATION;
1604 break;
1605 case '>': /* DECPNM */
1606 terminal->key_mode = KM_NORMAL;
1607 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001608 default:
1609 fprintf(stderr, "Unknown escape code: %c\n", code);
1610 break;
1611 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001612}
1613
1614static void
1615handle_special_escape(struct terminal *terminal, char special, char code)
1616{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001617 int i, numChars;
1618
1619 if (special == '#') {
1620 switch(code) {
1621 case '8':
1622 /* fill with 'E', no cheap way to do this */
1623 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1624 numChars = terminal->width * terminal->height;
1625 for(i = 0; i < numChars; i++) {
1626 terminal->data[i].byte[0] = 'E';
1627 }
1628 break;
1629 default:
1630 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1631 break;
1632 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001633 } else if (special == '(' || special == ')') {
1634 switch(code) {
1635 case '0':
1636 if (special == '(')
1637 terminal->g0 = CS_SPECIAL;
1638 else
1639 terminal->g1 = CS_SPECIAL;
1640 break;
1641 case 'A':
1642 if (special == '(')
1643 terminal->g0 = CS_UK;
1644 else
1645 terminal->g1 = CS_UK;
1646 break;
1647 case 'B':
1648 if (special == '(')
1649 terminal->g0 = CS_US;
1650 else
1651 terminal->g1 = CS_US;
1652 break;
1653 default:
1654 fprintf(stderr, "Unknown character set %c\n", code);
1655 break;
1656 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001657 } else {
1658 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1659 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001660}
1661
1662static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001663handle_sgr(struct terminal *terminal, int code)
1664{
1665 switch(code) {
1666 case 0:
1667 terminal->curr_attr = terminal->color_scheme->default_attr;
1668 break;
1669 case 1:
1670 terminal->curr_attr.a |= ATTRMASK_BOLD;
1671 if (terminal->curr_attr.fg < 8)
1672 terminal->curr_attr.fg += 8;
1673 break;
1674 case 4:
1675 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1676 break;
1677 case 5:
1678 terminal->curr_attr.a |= ATTRMASK_BLINK;
1679 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001680 case 8:
1681 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1682 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001683 case 2:
1684 case 21:
1685 case 22:
1686 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1687 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1688 terminal->curr_attr.fg -= 8;
1689 break;
1690 case 24:
1691 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1692 break;
1693 case 25:
1694 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1695 break;
1696 case 7:
1697 case 26:
1698 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1699 break;
1700 case 27:
1701 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1702 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001703 case 28:
1704 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1705 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001706 case 39:
1707 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1708 break;
1709 case 49:
1710 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1711 break;
1712 default:
1713 if(code >= 30 && code <= 37) {
1714 terminal->curr_attr.fg = code - 30;
1715 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1716 terminal->curr_attr.fg += 8;
1717 } else if(code >= 40 && code <= 47) {
1718 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001719 } else if (code >= 90 && code <= 97) {
1720 terminal->curr_attr.fg = code - 90 + 8;
1721 } else if (code >= 100 && code <= 107) {
1722 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001723 } else if(code >= 256 && code < 512) {
1724 terminal->curr_attr.fg = code - 256;
1725 } else if(code >= 512 && code < 768) {
1726 terminal->curr_attr.bg = code - 512;
1727 } else {
1728 fprintf(stderr, "Unknown SGR code: %d\n", code);
1729 }
1730 break;
1731 }
1732}
1733
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001734/* Returns 1 if c was special, otherwise 0 */
1735static int
1736handle_special_char(struct terminal *terminal, char c)
1737{
1738 union utf8_char *row;
1739 struct attr *attr_row;
1740
1741 row = terminal_get_row(terminal, terminal->row);
1742 attr_row = terminal_get_attr_row(terminal, terminal->row);
1743
1744 switch(c) {
1745 case '\r':
1746 terminal->column = 0;
1747 break;
1748 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001749 if (terminal->mode & MODE_LF_NEWLINE) {
1750 terminal->column = 0;
1751 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001752 /* fallthrough */
1753 case '\v':
1754 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001755 terminal->row++;
1756 if(terminal->row > terminal->margin_bottom) {
1757 terminal->row = terminal->margin_bottom;
1758 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001759 }
1760
1761 break;
1762 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001763 while (terminal->column < terminal->width) {
1764 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001765 if (terminal->mode & MODE_IRM)
1766 terminal_shift_line(terminal, +1);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001767 row[terminal->column].byte[0] = ' ';
1768 row[terminal->column].byte[1] = '\0';
1769 attr_row[terminal->column] = terminal->curr_attr;
1770 terminal->column++;
1771 }
1772 if (terminal->column >= terminal->width) {
1773 terminal->column = terminal->width - 1;
1774 }
1775
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001776 break;
1777 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001778 if (terminal->column >= terminal->width) {
1779 terminal->column = terminal->width - 2;
1780 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001781 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001782 } else if (terminal->mode & MODE_AUTOWRAP) {
1783 terminal->column = terminal->width - 1;
1784 terminal->row -= 1;
1785 if (terminal->row < terminal->margin_top) {
1786 terminal->row = terminal->margin_top;
1787 terminal_scroll(terminal, -1);
1788 }
1789 }
1790
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001791 break;
1792 case '\a':
1793 /* Bell */
1794 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001795 case '\x0E': /* SO */
1796 terminal->cs = terminal->g1;
1797 break;
1798 case '\x0F': /* SI */
1799 terminal->cs = terminal->g0;
1800 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001801 default:
1802 return 0;
1803 }
1804
1805 return 1;
1806}
1807
1808static void
1809handle_char(struct terminal *terminal, union utf8_char utf8)
1810{
1811 union utf8_char *row;
1812 struct attr *attr_row;
1813
1814 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001815
1816 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001817
1818 /* There are a whole lot of non-characters, control codes,
1819 * and formatting codes that should probably be ignored,
1820 * for example: */
1821 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1822 /* BOM, ignore */
1823 return;
1824 }
1825
1826 /* Some of these non-characters should be translated, e.g.: */
1827 if (utf8.byte[0] < 32) {
1828 utf8.byte[0] = utf8.byte[0] + 64;
1829 }
1830
1831 /* handle right margin effects */
1832 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001833 if (terminal->mode & MODE_AUTOWRAP) {
1834 terminal->column = 0;
1835 terminal->row += 1;
1836 if (terminal->row > terminal->margin_bottom) {
1837 terminal->row = terminal->margin_bottom;
1838 terminal_scroll(terminal, +1);
1839 }
1840 } else {
1841 terminal->column--;
1842 }
1843 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001844
1845 row = terminal_get_row(terminal, terminal->row);
1846 attr_row = terminal_get_attr_row(terminal, terminal->row);
1847
Callum Lowcay69e96582011-01-07 19:47:00 +00001848 if (terminal->mode & MODE_IRM)
1849 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001850 row[terminal->column] = utf8;
1851 attr_row[terminal->column++] = terminal->curr_attr;
1852
1853 if (utf8.ch != terminal->last_char.ch)
1854 terminal->last_char = utf8;
1855}
1856
Callum Lowcay30eeae52011-01-07 19:46:55 +00001857static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001858escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1859{
1860 int len, i;
1861
1862 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1863 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1864 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1865 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1866 else len = 1; /* Invalid, cannot happen */
1867
1868 if (terminal->escape_length + len <= MAX_ESCAPE) {
1869 for (i = 0; i < len; i++)
1870 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1871 terminal->escape_length += len;
1872 } else if (terminal->escape_length < MAX_ESCAPE) {
1873 terminal->escape[terminal->escape_length++] = 0;
1874 }
1875}
1876
1877static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001878terminal_data(struct terminal *terminal, const char *data, size_t length)
1879{
1880 int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001881 union utf8_char utf8;
1882 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001883
1884 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001885 parser_state =
1886 utf8_next_char(&terminal->state_machine, data[i]);
1887 switch(parser_state) {
1888 case utf8state_accept:
1889 utf8.ch = terminal->state_machine.s.ch;
1890 break;
1891 case utf8state_reject:
1892 /* the unicode replacement character */
1893 utf8.byte[0] = 0xEF;
1894 utf8.byte[1] = 0xBF;
1895 utf8.byte[2] = 0xBD;
1896 utf8.byte[3] = 0x00;
1897 break;
1898 default:
1899 continue;
1900 }
1901
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001902 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001903 switch (terminal->state) {
1904 case escape_state_escape:
1905 escape_append_utf8(terminal, utf8);
1906 switch (utf8.byte[0]) {
1907 case 'P': /* DCS */
1908 terminal->state = escape_state_dcs;
1909 break;
1910 case '[': /* CSI */
1911 terminal->state = escape_state_csi;
1912 break;
1913 case ']': /* OSC */
1914 terminal->state = escape_state_osc;
1915 break;
1916 case '#':
1917 case '(':
1918 case ')': /* special */
1919 terminal->state = escape_state_special;
1920 break;
1921 case '^': /* PM (not implemented) */
1922 case '_': /* APC (not implemented) */
1923 terminal->state = escape_state_ignore;
1924 break;
1925 default:
1926 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001927 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13001928 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001929 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001930 continue;
1931 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001932 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1933 /* do nothing */
1934 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001935 terminal->escape_flags |= ESC_FLAG_WHAT;
1936 } else if (utf8.byte[0] == '>') {
1937 terminal->escape_flags |= ESC_FLAG_GT;
1938 } else if (utf8.byte[0] == '!') {
1939 terminal->escape_flags |= ESC_FLAG_BANG;
1940 } else if (utf8.byte[0] == '$') {
1941 terminal->escape_flags |= ESC_FLAG_CASH;
1942 } else if (utf8.byte[0] == '\'') {
1943 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1944 } else if (utf8.byte[0] == '"') {
1945 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1946 } else if (utf8.byte[0] == ' ') {
1947 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001948 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001949 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001950 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13001951 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001952 }
1953
1954 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1955 utf8.byte[0] == '`')
1956 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001957 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001958 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001959 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001960 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001961 continue;
1962 case escape_state_inner_escape:
1963 if (utf8.byte[0] == '\\') {
1964 terminal->state = escape_state_normal;
1965 if (terminal->outer_state == escape_state_dcs) {
1966 handle_dcs(terminal);
1967 } else if (terminal->outer_state == escape_state_osc) {
1968 handle_osc(terminal);
1969 }
1970 } else if (utf8.byte[0] == '\e') {
1971 terminal->state = terminal->outer_state;
1972 escape_append_utf8(terminal, utf8);
1973 if (terminal->escape_length >= MAX_ESCAPE)
1974 terminal->state = escape_state_normal;
1975 } else {
1976 terminal->state = terminal->outer_state;
1977 if (terminal->escape_length < MAX_ESCAPE)
1978 terminal->escape[terminal->escape_length++] = '\e';
1979 escape_append_utf8(terminal, utf8);
1980 if (terminal->escape_length >= MAX_ESCAPE)
1981 terminal->state = escape_state_normal;
1982 }
1983 continue;
1984 case escape_state_dcs:
1985 case escape_state_osc:
1986 case escape_state_ignore:
1987 if (utf8.byte[0] == '\e') {
1988 terminal->outer_state = terminal->state;
1989 terminal->state = escape_state_inner_escape;
1990 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
1991 terminal->state = escape_state_normal;
1992 handle_osc(terminal);
1993 } else {
1994 escape_append_utf8(terminal, utf8);
1995 if (terminal->escape_length >= MAX_ESCAPE)
1996 terminal->state = escape_state_normal;
1997 }
1998 continue;
1999 case escape_state_special:
2000 escape_append_utf8(terminal, utf8);
2001 terminal->state = escape_state_normal;
2002 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2003 handle_special_escape(terminal, terminal->escape[1],
2004 utf8.byte[0]);
2005 }
2006 continue;
2007 default:
2008 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002009 }
2010
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002011 /* this is valid, because ASCII characters are never used to
2012 * introduce a multibyte sequence in UTF-8 */
2013 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002014 terminal->state = escape_state_escape;
2015 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002016 terminal->escape[0] = '\e';
2017 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002018 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002019 } else {
2020 handle_char(terminal, utf8);
2021 } /* if */
2022 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002023
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002024 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002025}
2026
2027static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002028data_source_target(void *data,
2029 struct wl_data_source *source, const char *mime_type)
2030{
2031 fprintf(stderr, "data_source_target, %s\n", mime_type);
2032}
2033
2034static void
2035data_source_send(void *data,
2036 struct wl_data_source *source,
2037 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002038{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002039 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002040
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002041 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002042}
2043
2044static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002045data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002046{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002047 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002048}
2049
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002050static const struct wl_data_source_listener data_source_listener = {
2051 data_source_target,
2052 data_source_send,
2053 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002054};
2055
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002056static int
2057handle_bound_key(struct terminal *terminal,
2058 struct input *input, uint32_t sym, uint32_t time)
2059{
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002060 switch (sym) {
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002061 case XK_X:
2062 /* Cut selection; terminal doesn't do cut, fall
2063 * through to copy. */
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002064 case XK_C:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002065 terminal->selection =
2066 display_create_data_source(terminal->display);
2067 wl_data_source_offer(terminal->selection,
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002068 "text/plain;charset=utf-8");
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002069 wl_data_source_add_listener(terminal->selection,
2070 &data_source_listener, terminal);
2071 input_set_selection(input, terminal->selection, time);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002072 return 1;
2073 case XK_V:
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002074 input_receive_selection_data_to_fd(input,
2075 "text/plain;charset=utf-8",
2076 terminal->master);
2077
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002078 return 1;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002079 default:
2080 return 0;
2081 }
2082}
2083
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002084static void
2085key_handler(struct window *window, struct input *input, uint32_t time,
2086 uint32_t key, uint32_t sym, uint32_t state, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002087{
2088 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002089 char ch[MAX_RESPONSE];
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002090 uint32_t modifiers;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002091 int len = 0;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002092
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002093 modifiers = input_get_modifiers(input);
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002094 if ((modifiers & XKB_COMMON_CONTROL_MASK) &&
2095 (modifiers & XKB_COMMON_SHIFT_MASK) &&
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002096 state && handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002097 return;
2098
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002099 switch (sym) {
2100 case XK_F11:
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002101 if (!state)
2102 break;
2103 terminal->fullscreen ^= 1;
2104 window_set_fullscreen(window, terminal->fullscreen);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002105 window_schedule_redraw(terminal->window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002106 break;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002107
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002108 case XK_BackSpace:
2109 case XK_Tab:
2110 case XK_Linefeed:
2111 case XK_Clear:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002112 case XK_Pause:
2113 case XK_Scroll_Lock:
2114 case XK_Sys_Req:
2115 case XK_Escape:
2116 ch[len++] = sym & 0x7f;
2117 break;
2118
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002119 case XK_Return:
2120 if (terminal->mode & MODE_LF_NEWLINE) {
2121 ch[len++] = 0x0D;
2122 ch[len++] = 0x0A;
2123 } else {
2124 ch[len++] = 0x0D;
2125 }
2126 break;
2127
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002128 case XK_Shift_L:
2129 case XK_Shift_R:
2130 case XK_Control_L:
2131 case XK_Control_R:
2132 case XK_Alt_L:
2133 case XK_Alt_R:
2134 break;
2135
Callum Lowcay7e08e902011-01-07 19:47:02 +00002136 case XK_Insert:
2137 len = function_key_response('[', 2, modifiers, '~', ch);
2138 break;
2139 case XK_Delete:
2140 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2141 ch[len++] = '\x04';
2142 } else {
2143 len = function_key_response('[', 3, modifiers, '~', ch);
2144 }
2145 break;
2146 case XK_Page_Up:
2147 len = function_key_response('[', 5, modifiers, '~', ch);
2148 break;
2149 case XK_Page_Down:
2150 len = function_key_response('[', 6, modifiers, '~', ch);
2151 break;
2152 case XK_F1:
2153 len = function_key_response('O', 1, modifiers, 'P', ch);
2154 break;
2155 case XK_F2:
2156 len = function_key_response('O', 1, modifiers, 'Q', ch);
2157 break;
2158 case XK_F3:
2159 len = function_key_response('O', 1, modifiers, 'R', ch);
2160 break;
2161 case XK_F4:
2162 len = function_key_response('O', 1, modifiers, 'S', ch);
2163 break;
2164 case XK_F5:
2165 len = function_key_response('[', 15, modifiers, '~', ch);
2166 break;
2167 case XK_F6:
2168 len = function_key_response('[', 17, modifiers, '~', ch);
2169 break;
2170 case XK_F7:
2171 len = function_key_response('[', 18, modifiers, '~', ch);
2172 break;
2173 case XK_F8:
2174 len = function_key_response('[', 19, modifiers, '~', ch);
2175 break;
2176 case XK_F9:
2177 len = function_key_response('[', 20, modifiers, '~', ch);
2178 break;
2179 case XK_F10:
2180 len = function_key_response('[', 21, modifiers, '~', ch);
2181 break;
2182 case XK_F12:
2183 len = function_key_response('[', 24, modifiers, '~', ch);
2184 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002185 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002186 /* Handle special keys with alternate mappings */
2187 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2188 if (len != 0) break;
2189
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002190 if (modifiers & XKB_COMMON_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002191 if (sym >= '3' && sym <= '7')
2192 sym = (sym & 0x1f) + 8;
2193
2194 if (!((sym >= '!' && sym <= '/') ||
2195 (sym >= '8' && sym <= '?') ||
2196 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2197 else if (sym == '2') sym = 0x00;
2198 else if (sym == '/') sym = 0x1F;
2199 else if (sym == '8' || sym == '?') sym = 0x7F;
2200 } else if ((terminal->mode & MODE_ALT_SENDS_ESC) &&
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002201 (modifiers & XKB_COMMON_MOD1_MASK))
Callum Lowcay7e08e902011-01-07 19:47:02 +00002202 {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002203 ch[len++] = 0x1b;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002204 } else if (modifiers & XKB_COMMON_MOD1_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002205 sym = sym | 0x80;
2206 }
2207
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002208 if (sym < 256)
2209 ch[len++] = sym;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002210 break;
2211 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002212
2213 if (state && len > 0)
Kristian Høgsberg26130862011-08-24 11:30:21 -04002214 terminal_write(terminal, ch, len);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002215}
2216
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002217static void
2218keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002219 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002220{
2221 struct terminal *terminal = data;
2222
2223 terminal->focused = (device != NULL);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002224 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002225}
2226
Kristian Høgsberg59826582011-01-20 11:56:57 -05002227static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002228button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002229 struct input *input, uint32_t time,
2230 int button, int state, void *data)
2231{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002232 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002233
2234 switch (button) {
2235 case 272:
2236 if (state) {
2237 terminal->dragging = 1;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002238 input_get_position(input,
2239 &terminal->selection_start_x,
2240 &terminal->selection_start_y);
2241 terminal->selection_end_x = terminal->selection_start_x;
2242 terminal->selection_end_y = terminal->selection_start_y;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002243 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002244 } else {
2245 terminal->dragging = 0;
2246 }
2247 break;
2248 }
2249}
2250
2251static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002252motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002253 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002254 int32_t x, int32_t y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002255{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002256 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002257
2258 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002259 input_get_position(input,
2260 &terminal->selection_end_x,
2261 &terminal->selection_end_y);
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002262 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002263 }
2264
2265 return POINTER_IBEAM;
2266}
2267
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002268static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002269terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002270{
2271 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002272 cairo_surface_t *surface;
2273 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002274
2275 terminal = malloc(sizeof *terminal);
2276 if (terminal == NULL)
2277 return terminal;
2278
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002279 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002280 terminal->fullscreen = fullscreen;
Callum Lowcay30eeae52011-01-07 19:46:55 +00002281 terminal->color_scheme = &DEFAULT_COLORS;
2282 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002283 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002284 terminal->margin_bottom = -1;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002285 terminal->window = window_create(display, 500, 400);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002286 terminal->widget = window_add_widget(terminal->window, terminal);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002287 window_set_title(terminal->window, "Wayland Terminal");
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002288
2289 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002290 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002291
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002292 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002293 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002294
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002295 window_set_user_data(terminal->window, terminal);
2296 window_set_redraw_handler(terminal->window, redraw_handler);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05002297 window_set_resize_handler(terminal->window, resize_handler);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002298
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002299 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002300 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002301 keyboard_focus_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002302 widget_set_button_handler(terminal->widget, button_handler);
2303 widget_set_motion_handler(terminal->widget, motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002304
Kristian Høgsberg09531622010-06-14 23:22:15 -04002305 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2306 cr = cairo_create(surface);
Kristian Høgsberg09531622010-06-14 23:22:15 -04002307 cairo_set_font_size(cr, 14);
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002308 cairo_select_font_face (cr, "mono",
2309 CAIRO_FONT_SLANT_NORMAL,
2310 CAIRO_FONT_WEIGHT_BOLD);
2311 terminal->font_bold = cairo_get_scaled_font (cr);
2312 cairo_scaled_font_reference(terminal->font_bold);
2313
2314 cairo_select_font_face (cr, "mono",
2315 CAIRO_FONT_SLANT_NORMAL,
2316 CAIRO_FONT_WEIGHT_NORMAL);
2317 terminal->font_normal = cairo_get_scaled_font (cr);
2318 cairo_scaled_font_reference(terminal->font_normal);
2319
Kristian Høgsberg09531622010-06-14 23:22:15 -04002320 cairo_font_extents(cr, &terminal->extents);
2321 cairo_destroy(cr);
2322 cairo_surface_destroy(surface);
2323
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002324 return terminal;
2325}
2326
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002327static void
2328io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002329{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002330 struct terminal *terminal =
2331 container_of(task, struct terminal, io_task);
2332 char buffer[256];
2333 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002334
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002335 if (events & EPOLLHUP)
2336 exit(0);
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002337
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002338 len = read(terminal->master, buffer, sizeof buffer);
2339 if (len < 0)
2340 exit(0);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002341
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002342 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002343}
2344
2345static int
2346terminal_run(struct terminal *terminal, const char *path)
2347{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002348 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002349 pid_t pid;
2350
2351 pid = forkpty(&master, NULL, NULL, NULL);
2352 if (pid == 0) {
Callum Lowcay86653ed2011-01-07 19:47:03 +00002353 setenv("TERM", "xterm-256color", 1);
2354 setenv("COLORTERM", "xterm-256color", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002355 if (execl(path, path, NULL)) {
2356 printf("exec failed: %m\n");
2357 exit(EXIT_FAILURE);
2358 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002359 } else if (pid < 0) {
2360 fprintf(stderr, "failed to fork and create pty (%m).\n");
2361 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002362 }
2363
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002364 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002365 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002366 terminal->io_task.run = io_handler;
2367 display_watch_fd(terminal->display, terminal->master,
2368 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002369
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002370 window_set_fullscreen(terminal->window, terminal->fullscreen);
2371 if (!terminal->fullscreen)
2372 terminal_resize(terminal, 80, 24);
2373
Kristian Høgsberg54b86832011-06-21 16:31:11 -04002374 terminal_draw(terminal);
2375
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002376 return 0;
2377}
2378
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002379static const GOptionEntry option_entries[] = {
2380 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
2381 &option_fullscreen, "Run in fullscreen mode" },
2382 { NULL }
2383};
2384
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002385int main(int argc, char *argv[])
2386{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002387 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002388 struct terminal *terminal;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002389
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002390 d = display_create(&argc, &argv, option_entries);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002391 if (d == NULL) {
2392 fprintf(stderr, "failed to create display: %m\n");
2393 return -1;
2394 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002395
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002396 terminal = terminal_create(d, option_fullscreen);
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002397 if (terminal_run(terminal, "/bin/bash"))
2398 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002399
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002400 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002401
2402 return 0;
2403}