blob: 9ec81909bec979cd2423af85cd9fec5975c5e4eb [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øgsberg43c28ee2009-01-26 23:42:46 -0500359 struct display *display;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000360 union utf8_char *data;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400361 struct task io_task;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000362 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000363 struct attr *data_attr;
364 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000365 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000366 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000367 char saved_origin_mode;
368 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000369 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000370 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000371 character_set cs, g0, g1;
372 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000373 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000374 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500375 int width, height, start, row, column;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000376 int saved_row, saved_column;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500377 int fd, master;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500378 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300379 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500380 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300381 enum escape_state state;
382 enum escape_state outer_state;
383 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000384 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500385 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500386 int fullscreen;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500387 int focused;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400388 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000389 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400390 cairo_font_extents_t extents;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500391 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500392
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400393 struct wl_data_source *selection;
394 int32_t dragging;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500395 int selection_start_x, selection_start_y;
396 int selection_end_x, selection_end_y;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500397};
398
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000399/* Create default tab stops, every 8 characters */
400static void
401terminal_init_tabs(struct terminal *terminal)
402{
403 int i = 0;
404
405 while (i < terminal->width) {
406 if (i % 8 == 0)
407 terminal->tab_ruler[i] = 1;
408 else
409 terminal->tab_ruler[i] = 0;
410 i++;
411 }
412}
413
Callum Lowcay30eeae52011-01-07 19:46:55 +0000414static void
415terminal_init(struct terminal *terminal)
416{
417 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000418 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000419 terminal->mode = MODE_SHOW_CURSOR |
420 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000421 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000422 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000423
424 terminal->row = 0;
425 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000426
Callum Lowcay256e72f2011-01-07 19:47:01 +0000427 terminal->g0 = CS_US;
428 terminal->g1 = CS_US;
429 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000430 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000431
432 terminal->saved_g0 = terminal->g0;
433 terminal->saved_g1 = terminal->g1;
434 terminal->saved_cs = terminal->cs;
435
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000436 terminal->saved_attr = terminal->curr_attr;
437 terminal->saved_origin_mode = terminal->origin_mode;
438 terminal->saved_row = terminal->row;
439 terminal->saved_column = terminal->column;
440
441 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000442}
443
444static void
445init_color_table(struct terminal *terminal)
446{
447 int c, r;
448 struct terminal_color *color_table = terminal->color_table;
449
450 for (c = 0; c < 256; c ++) {
451 if (c < 16) {
452 color_table[c] = terminal->color_scheme->palette[c];
453 } else if (c < 232) {
454 r = c - 16;
455 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
456 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
457 color_table[c].r = ((double)(r % 6) / 6.0);
458 color_table[c].a = 1.0;
459 } else {
460 r = (c - 232) * 10 + 8;
461 color_table[c].r = ((double) r) / 256.0;
462 color_table[c].g = color_table[c].r;
463 color_table[c].b = color_table[c].r;
464 color_table[c].a = 1.0;
465 }
466 }
467}
468
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000469static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500470terminal_get_row(struct terminal *terminal, int row)
471{
472 int index;
473
474 index = (row + terminal->start) % terminal->height;
475
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000476 return &terminal->data[index * terminal->width];
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500477}
478
Callum Lowcay30eeae52011-01-07 19:46:55 +0000479static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500480terminal_get_attr_row(struct terminal *terminal, int row)
481{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000482 int index;
483
484 index = (row + terminal->start) % terminal->height;
485
486 return &terminal->data_attr[index * terminal->width];
487}
488
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500489union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300490 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500491 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500492};
493
Kristian Høgsberg59826582011-01-20 11:56:57 -0500494static int
495terminal_compare_position(struct terminal *terminal,
496 int x, int y, int32_t ref_row, int32_t ref_col)
497{
498 struct rectangle allocation;
499 int top_margin, side_margin, col, row, ref_x;
500
501 window_get_child_allocation(terminal->window, &allocation);
502 side_margin = allocation.x + (allocation.width - terminal->width * terminal->extents.max_x_advance) / 2;
503 top_margin = allocation.y + (allocation.height - terminal->height * terminal->extents.height) / 2;
504
505 col = (x - side_margin) / terminal->extents.max_x_advance;
506 row = (y - top_margin) / terminal->extents.height;
507
508 ref_x = side_margin + ref_col * terminal->extents.max_x_advance +
509 terminal->extents.max_x_advance / 2;
510
511 if (row < ref_row)
512 return -1;
513 if (row == ref_row) {
514 if (col < ref_col)
515 return -1;
516 if (col == ref_col && x < ref_x)
517 return -1;
518 }
519
520 return 1;
521}
522
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500523static void
524terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500525 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500526{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500527 struct attr attr;
528 int foreground, background, tmp;
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500529 int start_cmp, end_cmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500530
531 start_cmp =
532 terminal_compare_position(terminal,
533 terminal->selection_start_x,
534 terminal->selection_start_y,
535 row, col);
536 end_cmp =
537 terminal_compare_position(terminal,
538 terminal->selection_end_x,
539 terminal->selection_end_y,
540 row, col);
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500541 decoded->attr.s = 0;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500542 if (start_cmp < 0 && end_cmp > 0)
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500543 decoded->attr.s = 1;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500544 else if (end_cmp < 0 && start_cmp > 0)
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500545 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500546
547 /* get the attributes for this character cell */
548 attr = terminal_get_attr_row(terminal, row)[col];
549 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500550 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500551 ((terminal->mode & MODE_SHOW_CURSOR) &&
552 terminal->focused && terminal->row == row &&
553 terminal->column == col)) {
554 foreground = attr.bg;
555 background = attr.fg;
556 if (attr.a & ATTRMASK_BOLD) {
557 if (foreground <= 16) foreground |= 0x08;
558 if (background <= 16) background &= 0x07;
559 }
560 } else {
561 foreground = attr.fg;
562 background = attr.bg;
563 }
564
565 if (terminal->mode & MODE_INVERSE) {
566 tmp = foreground;
567 foreground = background;
568 background = tmp;
569 if (attr.a & ATTRMASK_BOLD) {
570 if (foreground <= 16) foreground |= 0x08;
571 if (background <= 16) background &= 0x07;
572 }
573 }
574
Callum Lowcay9d708b02011-01-12 20:06:17 +1300575 decoded->attr.fg = foreground;
576 decoded->attr.bg = background;
577 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000578}
579
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500580
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500581static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000582terminal_scroll_buffer(struct terminal *terminal, int d)
583{
584 int i;
585
586 d = d % (terminal->height + 1);
587 terminal->start = (terminal->start + d) % terminal->height;
588 if (terminal->start < 0) terminal->start = terminal->height + terminal->start;
589 if(d < 0) {
590 d = 0 - d;
591 for(i = 0; i < d; i++) {
592 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
593 attr_init(terminal_get_attr_row(terminal, i),
594 terminal->curr_attr, terminal->width);
595 }
596 } else {
597 for(i = terminal->height - d; i < terminal->height; i++) {
598 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
599 attr_init(terminal_get_attr_row(terminal, i),
600 terminal->curr_attr, terminal->width);
601 }
602 }
603}
604
605static void
606terminal_scroll_window(struct terminal *terminal, int d)
607{
608 int i;
609 int window_height;
610 int from_row, to_row;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000611
612 // scrolling range is inclusive
613 window_height = terminal->margin_bottom - terminal->margin_top + 1;
614 d = d % (window_height + 1);
615 if(d < 0) {
616 d = 0 - d;
617 to_row = terminal->margin_bottom;
618 from_row = terminal->margin_bottom - d;
619
620 for (i = 0; i < (window_height - d); i++) {
621 memcpy(terminal_get_row(terminal, to_row - i),
622 terminal_get_row(terminal, from_row - i),
623 terminal->data_pitch);
624 memcpy(terminal_get_attr_row(terminal, to_row - i),
625 terminal_get_attr_row(terminal, from_row - i),
626 terminal->attr_pitch);
627 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000628 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
629 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000630 attr_init(terminal_get_attr_row(terminal, i),
631 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000632 }
633 } else {
634 to_row = terminal->margin_top;
635 from_row = terminal->margin_top + d;
636
637 for (i = 0; i < (window_height - d); i++) {
638 memcpy(terminal_get_row(terminal, to_row + i),
639 terminal_get_row(terminal, from_row + i),
640 terminal->data_pitch);
641 memcpy(terminal_get_attr_row(terminal, to_row + i),
642 terminal_get_attr_row(terminal, from_row + i),
643 terminal->attr_pitch);
644 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000645 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
646 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000647 attr_init(terminal_get_attr_row(terminal, i),
648 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000649 }
650 }
651}
652
653static void
654terminal_scroll(struct terminal *terminal, int d)
655{
656 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
657 terminal_scroll_buffer(terminal, d);
658 else
659 terminal_scroll_window(terminal, d);
660}
661
662static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000663terminal_shift_line(struct terminal *terminal, int d)
664{
665 union utf8_char *row;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500666 struct attr *attr_row;
Callum Lowcay69e96582011-01-07 19:47:00 +0000667
668 row = terminal_get_row(terminal, terminal->row);
669 attr_row = terminal_get_attr_row(terminal, terminal->row);
670
671 if ((terminal->width + d) <= terminal->column)
672 d = terminal->column + 1 - terminal->width;
673 if ((terminal->column + d) >= terminal->width)
674 d = terminal->width - terminal->column - 1;
675
676 if (d < 0) {
677 d = 0 - d;
678 memmove(&row[terminal->column],
679 &row[terminal->column + d],
680 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
Callum Lowcay69e96582011-01-07 19:47:00 +0000681 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
682 (terminal->width - terminal->column - d) * sizeof(struct attr));
683 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
684 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
685 } else {
686 memmove(&row[terminal->column + d], &row[terminal->column],
687 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
688 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
689 (terminal->width - terminal->column - d) * sizeof(struct attr));
690 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
691 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
692 }
693}
694
695static void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500696terminal_resize(struct terminal *terminal, int width, int height)
697{
698 size_t size;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000699 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000700 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000701 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000702 int data_pitch, attr_pitch;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500703 int i, l, total_rows;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500704 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000705 struct winsize ws;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500706 int32_t pixel_width, pixel_height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500707
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500708 if (width < 1)
709 width = 1;
710 if (height < 1)
711 height = 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500712 if (terminal->width == width && terminal->height == height)
713 return;
714
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500715 if (!terminal->fullscreen) {
716 pixel_width = width *
717 terminal->extents.max_x_advance + 2 * terminal->margin;
718 pixel_height = height *
719 terminal->extents.height + 2 * terminal->margin;
720 window_set_child_size(terminal->window,
721 pixel_width, pixel_height);
722 }
723
724 window_schedule_redraw (terminal->window);
725
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000726 data_pitch = width * sizeof(union utf8_char);
727 size = data_pitch * height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500728 data = malloc(size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000729 attr_pitch = width * sizeof(struct attr);
730 data_attr = malloc(attr_pitch * height);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000731 tab_ruler = malloc(width);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500732 memset(data, 0, size);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000733 memset(tab_ruler, 0, width);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000734 attr_init(data_attr, terminal->curr_attr, width * height);
735 if (terminal->data && terminal->data_attr) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500736 if (width > terminal->width)
737 l = terminal->width;
738 else
739 l = width;
740
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500741 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500742 total_rows = height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500743 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500744 total_rows = terminal->height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500745 }
746
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000747 for (i = 0; i < total_rows; i++) {
748 memcpy(&data[width * i],
749 terminal_get_row(terminal, i),
750 l * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000751 memcpy(&data_attr[width * i],
752 terminal_get_attr_row(terminal, i),
753 l * sizeof(struct attr));
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000754 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500755
756 free(terminal->data);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000757 free(terminal->data_attr);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000758 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500759 }
760
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000761 terminal->data_pitch = data_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000762 terminal->attr_pitch = attr_pitch;
Callum Lowcay86653ed2011-01-07 19:47:03 +0000763 terminal->margin_bottom =
764 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500765 terminal->width = width;
766 terminal->height = height;
767 terminal->data = data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000768 terminal->data_attr = data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000769 terminal->tab_ruler = tab_ruler;
770 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500771
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000772 /* Update the window size */
773 ws.ws_row = terminal->height;
774 ws.ws_col = terminal->width;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500775 window_get_child_allocation(terminal->window, &allocation);
776 ws.ws_xpixel = allocation.width;
777 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000778 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500779}
780
Callum Lowcay30eeae52011-01-07 19:46:55 +0000781struct color_scheme DEFAULT_COLORS = {
782 {
783 {0, 0, 0, 1}, /* black */
784 {0.66, 0, 0, 1}, /* red */
785 {0 , 0.66, 0, 1}, /* green */
786 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
787 {0 , 0 , 0.66, 1}, /* blue */
788 {0.66, 0 , 0.66, 1}, /* magenta */
789 {0, 0.66, 0.66, 1}, /* cyan */
790 {0.66, 0.66, 0.66, 1}, /* light grey */
791 {0.22, 0.33, 0.33, 1}, /* dark grey */
792 {1, 0.33, 0.33, 1}, /* high red */
793 {0.33, 1, 0.33, 1}, /* high green */
794 {1, 1, 0.33, 1}, /* high yellow */
795 {0.33, 0.33, 1, 1}, /* high blue */
796 {1, 0.33, 1, 1}, /* high magenta */
797 {0.33, 1, 1, 1}, /* high cyan */
798 {1, 1, 1, 1} /* white */
799 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500800 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000801 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
802};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400803
Kristian Høgsberg22106762008-12-08 13:50:07 -0500804static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500805terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
806{
807 cairo_set_source_rgba(cr,
808 terminal->color_table[index].r,
809 terminal->color_table[index].g,
810 terminal->color_table[index].b,
811 terminal->color_table[index].a);
812}
813
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500814static void
815terminal_send_selection(struct terminal *terminal, int fd)
816{
817 int row, col;
818 union utf8_char *p_row;
819 union decoded_attr attr;
820 FILE *fp;
821 int len;
822
823 fp = fdopen(fd, "w");
824 for (row = 0; row < terminal->height; row++) {
825 p_row = terminal_get_row(terminal, row);
826 for (col = 0; col < terminal->width; col++) {
827 /* get the attributes for this character cell */
828 terminal_decode_attr(terminal, row, col, &attr);
829 if (!attr.attr.s)
830 continue;
831 len = strnlen((char *) p_row[col].byte, 4);
832 fwrite(p_row[col].byte, 1, len, fp);
833 }
834 }
835 fclose(fp);
836}
837
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500838struct glyph_run {
839 struct terminal *terminal;
840 cairo_t *cr;
841 int count;
842 union decoded_attr attr;
843 cairo_glyph_t glyphs[256], *g;
844};
845
846static void
847glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
848{
849 run->terminal = terminal;
850 run->cr = cr;
851 run->g = run->glyphs;
852 run->count = 0;
853 run->attr.key = 0;
854}
855
856static void
857glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
858{
859 cairo_scaled_font_t *font;
860
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500861 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
862 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300863 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500864 font = run->terminal->font_bold;
865 else
866 font = run->terminal->font_normal;
867 cairo_set_scaled_font(run->cr, font);
868 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300869 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500870
Callum Lowcay9d708b02011-01-12 20:06:17 +1300871 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
872 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500873 run->g = run->glyphs;
874 run->count = 0;
875 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300876 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500877}
878
879static void
880glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
881{
882 int num_glyphs;
883 cairo_scaled_font_t *font;
884
885 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
886
Callum Lowcay9d708b02011-01-12 20:06:17 +1300887 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500888 font = run->terminal->font_bold;
889 else
890 font = run->terminal->font_normal;
891
892 cairo_move_to(run->cr, x, y);
893 cairo_scaled_font_text_to_glyphs (font, x, y,
894 (char *) c->byte, 4,
895 &run->g, &num_glyphs,
896 NULL, NULL, NULL);
897 run->g += num_glyphs;
898 run->count += num_glyphs;
899}
900
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500901static void
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500902terminal_draw_contents(struct terminal *terminal)
903{
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500904 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500905 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000906 int top_margin, side_margin;
907 int row, col;
908 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500909 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000910 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500911 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500912 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500913 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500914 cairo_font_extents_t extents;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500915
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500916 surface = window_get_surface(terminal->window);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500917 window_get_child_allocation(terminal->window, &allocation);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500918 cr = cairo_create(surface);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500919 cairo_rectangle(cr, allocation.x, allocation.y,
920 allocation.width, allocation.height);
921 cairo_clip(cr);
922 cairo_push_group(cr);
923
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500924 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500925 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500926 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500927
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500928 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500929
Kristian Høgsberg59826582011-01-20 11:56:57 -0500930 extents = terminal->extents;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500931 side_margin = (allocation.width - terminal->width * extents.max_x_advance) / 2;
932 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500933
Callum Lowcay30eeae52011-01-07 19:46:55 +0000934 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500935 cairo_translate(cr, allocation.x + side_margin,
936 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500937 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000938 for (row = 0; row < terminal->height; row++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000939 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000940 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500941 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000942
Callum Lowcay9d708b02011-01-12 20:06:17 +1300943 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -0500944 continue;
945
Callum Lowcay9d708b02011-01-12 20:06:17 +1300946 terminal_set_color(terminal, cr, attr.attr.bg);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500947 cairo_move_to(cr, col * extents.max_x_advance,
948 row * extents.height);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000949 cairo_rel_line_to(cr, extents.max_x_advance, 0);
950 cairo_rel_line_to(cr, 0, extents.height);
951 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
952 cairo_close_path(cr);
953 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500954 }
955 }
Callum Lowcay30eeae52011-01-07 19:46:55 +0000956
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500957 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
958
959 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500960 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500961 for (row = 0; row < terminal->height; row++) {
962 p_row = terminal_get_row(terminal, row);
963 for (col = 0; col < terminal->width; col++) {
964 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500965 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500966
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500967 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000968
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500969 text_x = col * extents.max_x_advance;
970 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +1300971 if (attr.attr.a & ATTRMASK_UNDERLINE) {
972 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000973 cairo_move_to(cr, text_x, (double)text_y + 1.5);
974 cairo_line_to(cr, text_x + extents.max_x_advance, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000975 cairo_stroke(cr);
976 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -0500977
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500978 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000979 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500980 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500981
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500982 attr.key = ~0;
983 glyph_run_flush(&run, attr);
984
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000985 if ((terminal->mode & MODE_SHOW_CURSOR) && !terminal->focused) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000986 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500987
Callum Lowcay30eeae52011-01-07 19:46:55 +0000988 cairo_set_line_width(cr, 1);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500989 cairo_move_to(cr, terminal->column * extents.max_x_advance + d,
990 terminal->row * extents.height + d);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000991 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
992 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
993 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
994 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500995
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500996 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000997 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500998
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500999 cairo_pop_group_to_source(cr);
1000 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001001 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001002 cairo_surface_destroy(surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001003}
1004
1005static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001006terminal_write(struct terminal *terminal, const char *data, size_t length)
1007{
1008 if (write(terminal->master, data, length) < 0)
1009 abort();
1010}
1011
1012static void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001013resize_handler(struct window *window,
1014 int32_t pixel_width, int32_t pixel_height, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001015{
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001016 struct terminal *terminal = data;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001017 int32_t width, height;
Kristian Høgsberg22106762008-12-08 13:50:07 -05001018
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001019 width = (pixel_width - 2 * terminal->margin) /
Kristian Høgsberg09531622010-06-14 23:22:15 -04001020 (int32_t) terminal->extents.max_x_advance;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001021 height = (pixel_height - 2 * terminal->margin) /
Kristian Høgsberg09531622010-06-14 23:22:15 -04001022 (int32_t) terminal->extents.height;
Tiago Vignatti5fd89d22011-01-10 19:30:04 +02001023
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001024 if (terminal->fullscreen)
1025 window_set_child_size(terminal->window,
1026 pixel_width, pixel_height);
1027
Kristian Høgsberg22106762008-12-08 13:50:07 -05001028 terminal_resize(terminal, width, height);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001029}
Kristian Høgsberg22106762008-12-08 13:50:07 -05001030
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001031static void
1032terminal_draw(struct terminal *terminal)
1033{
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001034 window_draw(terminal->window);
1035 terminal_draw_contents(terminal);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001036 window_flush(terminal->window);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001037}
1038
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001039static void
1040redraw_handler(struct window *window, void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001041{
1042 struct terminal *terminal = data;
1043
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001044 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001045}
1046
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001047static void
1048terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001049
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001050static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001051handle_char(struct terminal *terminal, union utf8_char utf8);
1052
1053static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001054handle_sgr(struct terminal *terminal, int code);
1055
1056static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001057handle_term_parameter(struct terminal *terminal, int code, int sr)
1058{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001059 int i;
1060
Callum Lowcay67a201d2011-01-12 19:23:41 +13001061 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001062 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001063 case 1: /* DECCKM */
1064 if (sr) terminal->key_mode = KM_APPLICATION;
1065 else terminal->key_mode = KM_NORMAL;
1066 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001067 case 2: /* DECANM */
1068 /* No VT52 support yet */
1069 terminal->g0 = CS_US;
1070 terminal->g1 = CS_US;
1071 terminal->cs = terminal->g0;
1072 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001073 case 3: /* DECCOLM */
1074 if (sr)
1075 terminal_resize(terminal, 132, 24);
1076 else
1077 terminal_resize(terminal, 80, 24);
1078
1079 /* set columns, but also home cursor and clear screen */
1080 terminal->row = 0; terminal->column = 0;
1081 for (i = 0; i < terminal->height; i++) {
1082 memset(terminal_get_row(terminal, i),
1083 0, terminal->data_pitch);
1084 attr_init(terminal_get_attr_row(terminal, i),
1085 terminal->curr_attr, terminal->width);
1086 }
1087 break;
1088 case 5: /* DECSCNM */
1089 if (sr) terminal->mode |= MODE_INVERSE;
1090 else terminal->mode &= ~MODE_INVERSE;
1091 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001092 case 6: /* DECOM */
1093 terminal->origin_mode = sr;
1094 if (terminal->origin_mode)
1095 terminal->row = terminal->margin_top;
1096 else
1097 terminal->row = 0;
1098 terminal->column = 0;
1099 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001100 case 7: /* DECAWM */
1101 if (sr) terminal->mode |= MODE_AUTOWRAP;
1102 else terminal->mode &= ~MODE_AUTOWRAP;
1103 break;
1104 case 8: /* DECARM */
1105 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1106 else terminal->mode &= ~MODE_AUTOREPEAT;
1107 break;
1108 case 25:
1109 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1110 else terminal->mode &= ~MODE_SHOW_CURSOR;
1111 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001112 case 1037: /* deleteSendsDel */
1113 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1114 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1115 break;
1116 case 1039: /* altSendsEscape */
1117 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1118 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1119 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001120 default:
1121 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1122 break;
1123 }
1124 } else {
1125 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001126 case 4: /* IRM */
1127 if (sr) terminal->mode |= MODE_IRM;
1128 else terminal->mode &= ~MODE_IRM;
1129 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001130 case 20: /* LNM */
1131 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1132 else terminal->mode &= ~MODE_LF_NEWLINE;
1133 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001134 default:
1135 fprintf(stderr, "Unknown parameter: %d\n", code);
1136 break;
1137 }
1138 }
1139}
1140
1141static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001142handle_dcs(struct terminal *terminal)
1143{
1144}
1145
1146static void
1147handle_osc(struct terminal *terminal)
1148{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001149 char *p;
1150 int code;
1151
1152 terminal->escape[terminal->escape_length++] = '\0';
1153 p = &terminal->escape[2];
1154 code = strtol(p, &p, 10);
1155 if (*p == ';') p++;
1156
1157 switch (code) {
1158 case 0: /* Icon name and window title */
1159 case 1: /* Icon label */
1160 case 2: /* Window title*/
1161 window_set_title(terminal->window, p);
1162 break;
1163 default:
1164 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1165 break;
1166 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001167}
1168
1169static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001170handle_escape(struct terminal *terminal)
1171{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001172 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001173 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001174 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001175 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001176 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001177 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001178 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001179
1180 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001181 i = 0;
1182 p = &terminal->escape[2];
1183 while ((isdigit(*p) || *p == ';') && i < 10) {
1184 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001185 if (!set[i]) {
1186 args[i] = 0;
1187 set[i] = 1;
1188 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001189 p++;
1190 i++;
1191 } else {
1192 args[i] = strtol(p, &p, 10);
1193 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001194 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001195 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001196
1197 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001198 case '@': /* ICH */
1199 count = set[0] ? args[0] : 1;
1200 if (count == 0) count = 1;
1201 terminal_shift_line(terminal, count);
1202 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001203 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001204 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001205 if (count == 0) count = 1;
1206 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001207 terminal->row -= count;
1208 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001209 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001210 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001211 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001212 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001213 if (count == 0) count = 1;
1214 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001215 terminal->row += count;
1216 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001217 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001218 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001219 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001220 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001221 if (count == 0) count = 1;
1222 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001223 terminal->column += count;
1224 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001225 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001226 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001227 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001228 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001229 if (count == 0) count = 1;
1230 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001231 terminal->column -= count;
1232 else
1233 terminal->column = 0;
1234 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001235 case 'E': /* CNL */
1236 count = set[0] ? args[0] : 1;
1237 if (terminal->row + count <= terminal->margin_bottom)
1238 terminal->row += count;
1239 else
1240 terminal->row = terminal->margin_bottom;
1241 terminal->column = 0;
1242 break;
1243 case 'F': /* CPL */
1244 count = set[0] ? args[0] : 1;
1245 if (terminal->row - count >= terminal->margin_top)
1246 terminal->row -= count;
1247 else
1248 terminal->row = terminal->margin_top;
1249 terminal->column = 0;
1250 break;
1251 case 'G': /* CHA */
1252 y = set[0] ? args[0] : 1;
1253 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1254
1255 terminal->column = y - 1;
1256 break;
1257 case 'f': /* HVP */
1258 case 'H': /* CUP */
1259 x = (set[1] ? args[1] : 1) - 1;
1260 x = x < 0 ? 0 :
1261 (x >= terminal->width ? terminal->width - 1 : x);
1262
1263 y = (set[0] ? args[0] : 1) - 1;
1264 if (terminal->origin_mode) {
1265 y += terminal->margin_top;
1266 y = y < terminal->margin_top ? terminal->margin_top :
1267 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1268 } else {
1269 y = y < 0 ? 0 :
1270 (y >= terminal->height ? terminal->height - 1 : y);
1271 }
1272
1273 terminal->row = y;
1274 terminal->column = x;
1275 break;
1276 case 'I': /* CHT */
1277 count = set[0] ? args[0] : 1;
1278 if (count == 0) count = 1;
1279 while (count > 0 && terminal->column < terminal->width) {
1280 if (terminal->tab_ruler[terminal->column]) count--;
1281 terminal->column++;
1282 }
1283 terminal->column--;
1284 break;
1285 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001286 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001287 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001288 if (!set[0] || args[0] == 0 || args[0] > 2) {
1289 memset(&row[terminal->column],
1290 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1291 attr_init(&attr_row[terminal->column],
1292 terminal->curr_attr, terminal->width - terminal->column);
1293 for (i = terminal->row + 1; i < terminal->height; i++) {
1294 memset(terminal_get_row(terminal, i),
1295 0, terminal->data_pitch);
1296 attr_init(terminal_get_attr_row(terminal, i),
1297 terminal->curr_attr, terminal->width);
1298 }
1299 } else if (args[0] == 1) {
1300 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1301 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1302 for (i = 0; i < terminal->row; i++) {
1303 memset(terminal_get_row(terminal, i),
1304 0, terminal->data_pitch);
1305 attr_init(terminal_get_attr_row(terminal, i),
1306 terminal->curr_attr, terminal->width);
1307 }
1308 } else if (args[0] == 2) {
1309 for (i = 0; i < terminal->height; i++) {
1310 memset(terminal_get_row(terminal, i),
1311 0, terminal->data_pitch);
1312 attr_init(terminal_get_attr_row(terminal, i),
1313 terminal->curr_attr, terminal->width);
1314 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001315 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001316 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001317 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001318 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001319 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001320 if (!set[0] || args[0] == 0 || args[0] > 2) {
1321 memset(&row[terminal->column], 0,
1322 (terminal->width - terminal->column) * sizeof(union utf8_char));
1323 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1324 terminal->width - terminal->column);
1325 } else if (args[0] == 1) {
1326 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1327 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1328 } else if (args[0] == 2) {
1329 memset(row, 0, terminal->data_pitch);
1330 attr_init(attr_row, terminal->curr_attr, terminal->width);
1331 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001332 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001333 case 'L': /* IL */
1334 count = set[0] ? args[0] : 1;
1335 if (count == 0) count = 1;
1336 if (terminal->row >= terminal->margin_top &&
1337 terminal->row < terminal->margin_bottom)
1338 {
1339 top = terminal->margin_top;
1340 terminal->margin_top = terminal->row;
1341 terminal_scroll(terminal, 0 - count);
1342 terminal->margin_top = top;
1343 } else if (terminal->row == terminal->margin_bottom) {
1344 memset(terminal_get_row(terminal, terminal->row),
1345 0, terminal->data_pitch);
1346 attr_init(terminal_get_attr_row(terminal, terminal->row),
1347 terminal->curr_attr, terminal->width);
1348 }
1349 break;
1350 case 'M': /* DL */
1351 count = set[0] ? args[0] : 1;
1352 if (count == 0) count = 1;
1353 if (terminal->row >= terminal->margin_top &&
1354 terminal->row < terminal->margin_bottom)
1355 {
1356 top = terminal->margin_top;
1357 terminal->margin_top = terminal->row;
1358 terminal_scroll(terminal, count);
1359 terminal->margin_top = top;
1360 } else if (terminal->row == terminal->margin_bottom) {
1361 memset(terminal_get_row(terminal, terminal->row),
1362 0, terminal->data_pitch);
1363 }
1364 break;
1365 case 'P': /* DCH */
1366 count = set[0] ? args[0] : 1;
1367 if (count == 0) count = 1;
1368 terminal_shift_line(terminal, 0 - count);
1369 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001370 case 'S': /* SU */
1371 terminal_scroll(terminal, set[0] ? args[0] : 1);
1372 break;
1373 case 'T': /* SD */
1374 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1375 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001376 case 'X': /* ECH */
1377 count = set[0] ? args[0] : 1;
1378 if (count == 0) count = 1;
1379 if ((terminal->column + count) > terminal->width)
1380 count = terminal->width - terminal->column;
1381 row = terminal_get_row(terminal, terminal->row);
1382 attr_row = terminal_get_attr_row(terminal, terminal->row);
1383 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1384 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1385 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001386 case 'Z': /* CBT */
1387 count = set[0] ? args[0] : 1;
1388 if (count == 0) count = 1;
1389 while (count > 0 && terminal->column >= 0) {
1390 if (terminal->tab_ruler[terminal->column]) count--;
1391 terminal->column--;
1392 }
1393 terminal->column++;
1394 break;
1395 case '`': /* HPA */
1396 y = set[0] ? args[0] : 1;
1397 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1398
1399 terminal->column = y - 1;
1400 break;
1401 case 'b': /* REP */
1402 count = set[0] ? args[0] : 1;
1403 if (count == 0) count = 1;
1404 if (terminal->last_char.byte[0])
1405 for (i = 0; i < count; i++)
1406 handle_char(terminal, terminal->last_char);
1407 terminal->last_char.byte[0] = 0;
1408 break;
1409 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001410 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001411 break;
1412 case 'd': /* VPA */
1413 x = set[0] ? args[0] : 1;
1414 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1415
1416 terminal->row = x - 1;
1417 break;
1418 case 'g': /* TBC */
1419 if (!set[0] || args[0] == 0) {
1420 terminal->tab_ruler[terminal->column] = 0;
1421 } else if (args[0] == 3) {
1422 memset(terminal->tab_ruler, 0, terminal->width);
1423 }
1424 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001425 case 'h': /* SM */
1426 for(i = 0; i < 10 && set[i]; i++) {
1427 handle_term_parameter(terminal, args[i], 1);
1428 }
1429 break;
1430 case 'l': /* RM */
1431 for(i = 0; i < 10 && set[i]; i++) {
1432 handle_term_parameter(terminal, args[i], 0);
1433 }
1434 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001435 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001436 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001437 if (i <= 7 && set[i] && set[i + 1] &&
1438 set[i + 2] && args[i + 1] == 5)
1439 {
1440 if (args[i] == 38) {
1441 handle_sgr(terminal, args[i + 2] + 256);
1442 break;
1443 } else if (args[i] == 48) {
1444 handle_sgr(terminal, args[i + 2] + 512);
1445 break;
1446 }
1447 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001448 if(set[i]) {
1449 handle_sgr(terminal, args[i]);
1450 } else if(i == 0) {
1451 handle_sgr(terminal, 0);
1452 break;
1453 } else {
1454 break;
1455 }
1456 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001457 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001458 case 'n': /* DSR */
1459 i = set[0] ? args[0] : 0;
1460 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001461 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001462 } else if (i == 6) {
1463 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1464 terminal->origin_mode ?
1465 terminal->row+terminal->margin_top : terminal->row+1,
1466 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001467 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001468 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001469 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001470 case 'r':
1471 if(!set[0]) {
1472 terminal->margin_top = 0;
1473 terminal->margin_bottom = terminal->height-1;
1474 terminal->row = 0;
1475 terminal->column = 0;
1476 } else {
1477 top = (set[0] ? args[0] : 1) - 1;
1478 top = top < 0 ? 0 :
1479 (top >= terminal->height ? terminal->height - 1 : top);
1480 bottom = (set[1] ? args[1] : 1) - 1;
1481 bottom = bottom < 0 ? 0 :
1482 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1483 if(bottom > top) {
1484 terminal->margin_top = top;
1485 terminal->margin_bottom = bottom;
1486 } else {
1487 terminal->margin_top = 0;
1488 terminal->margin_bottom = terminal->height-1;
1489 }
1490 if(terminal->origin_mode)
1491 terminal->row = terminal->margin_top;
1492 else
1493 terminal->row = 0;
1494 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001495 }
1496 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001497 case 's':
1498 terminal->saved_row = terminal->row;
1499 terminal->saved_column = terminal->column;
1500 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001501 case 't': /* windowOps */
1502 if (!set[0]) break;
1503 switch (args[0]) {
1504 case 4: /* resize px */
1505 if (set[1] && set[2]) {
1506 window_set_child_size(terminal->window,
1507 args[2], args[1]);
1508 resize_handler(terminal->window,
1509 args[2], args[1], terminal);
1510 }
1511 break;
1512 case 8: /* resize ch */
1513 if (set[1] && set[2]) {
1514 terminal_resize(terminal, args[2], args[1]);
1515 }
1516 break;
1517 case 13: /* report position */
1518 window_get_child_allocation(terminal->window, &allocation);
1519 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1520 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001521 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001522 break;
1523 case 14: /* report px */
1524 window_get_child_allocation(terminal->window, &allocation);
1525 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1526 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001527 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001528 break;
1529 case 18: /* report ch */
1530 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1531 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001532 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001533 break;
1534 case 21: /* report title */
1535 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1536 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001537 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001538 break;
1539 default:
1540 if (args[0] >= 24)
1541 terminal_resize(terminal, terminal->width, args[0]);
1542 else
1543 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1544 break;
1545 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001546 case 'u':
1547 terminal->row = terminal->saved_row;
1548 terminal->column = terminal->saved_column;
1549 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001550 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001551 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001552 break;
1553 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001554}
1555
1556static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001557handle_non_csi_escape(struct terminal *terminal, char code)
1558{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001559 switch(code) {
1560 case 'M': /* RI */
1561 terminal->row -= 1;
1562 if(terminal->row < terminal->margin_top) {
1563 terminal->row = terminal->margin_top;
1564 terminal_scroll(terminal, -1);
1565 }
1566 break;
1567 case 'E': /* NEL */
1568 terminal->column = 0;
1569 // fallthrough
1570 case 'D': /* IND */
1571 terminal->row += 1;
1572 if(terminal->row > terminal->margin_bottom) {
1573 terminal->row = terminal->margin_bottom;
1574 terminal_scroll(terminal, +1);
1575 }
1576 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001577 case 'c': /* RIS */
1578 terminal_init(terminal);
1579 break;
1580 case 'H': /* HTS */
1581 terminal->tab_ruler[terminal->column] = 1;
1582 break;
1583 case '7': /* DECSC */
1584 terminal->saved_row = terminal->row;
1585 terminal->saved_column = terminal->column;
1586 terminal->saved_attr = terminal->curr_attr;
1587 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001588 terminal->saved_cs = terminal->cs;
1589 terminal->saved_g0 = terminal->g0;
1590 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001591 break;
1592 case '8': /* DECRC */
1593 terminal->row = terminal->saved_row;
1594 terminal->column = terminal->saved_column;
1595 terminal->curr_attr = terminal->saved_attr;
1596 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001597 terminal->cs = terminal->saved_cs;
1598 terminal->g0 = terminal->saved_g0;
1599 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001600 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001601 case '=': /* DECPAM */
1602 terminal->key_mode = KM_APPLICATION;
1603 break;
1604 case '>': /* DECPNM */
1605 terminal->key_mode = KM_NORMAL;
1606 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001607 default:
1608 fprintf(stderr, "Unknown escape code: %c\n", code);
1609 break;
1610 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001611}
1612
1613static void
1614handle_special_escape(struct terminal *terminal, char special, char code)
1615{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001616 int i, numChars;
1617
1618 if (special == '#') {
1619 switch(code) {
1620 case '8':
1621 /* fill with 'E', no cheap way to do this */
1622 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1623 numChars = terminal->width * terminal->height;
1624 for(i = 0; i < numChars; i++) {
1625 terminal->data[i].byte[0] = 'E';
1626 }
1627 break;
1628 default:
1629 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1630 break;
1631 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001632 } else if (special == '(' || special == ')') {
1633 switch(code) {
1634 case '0':
1635 if (special == '(')
1636 terminal->g0 = CS_SPECIAL;
1637 else
1638 terminal->g1 = CS_SPECIAL;
1639 break;
1640 case 'A':
1641 if (special == '(')
1642 terminal->g0 = CS_UK;
1643 else
1644 terminal->g1 = CS_UK;
1645 break;
1646 case 'B':
1647 if (special == '(')
1648 terminal->g0 = CS_US;
1649 else
1650 terminal->g1 = CS_US;
1651 break;
1652 default:
1653 fprintf(stderr, "Unknown character set %c\n", code);
1654 break;
1655 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001656 } else {
1657 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1658 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001659}
1660
1661static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001662handle_sgr(struct terminal *terminal, int code)
1663{
1664 switch(code) {
1665 case 0:
1666 terminal->curr_attr = terminal->color_scheme->default_attr;
1667 break;
1668 case 1:
1669 terminal->curr_attr.a |= ATTRMASK_BOLD;
1670 if (terminal->curr_attr.fg < 8)
1671 terminal->curr_attr.fg += 8;
1672 break;
1673 case 4:
1674 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1675 break;
1676 case 5:
1677 terminal->curr_attr.a |= ATTRMASK_BLINK;
1678 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001679 case 8:
1680 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1681 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001682 case 2:
1683 case 21:
1684 case 22:
1685 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1686 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1687 terminal->curr_attr.fg -= 8;
1688 break;
1689 case 24:
1690 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1691 break;
1692 case 25:
1693 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1694 break;
1695 case 7:
1696 case 26:
1697 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1698 break;
1699 case 27:
1700 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1701 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001702 case 28:
1703 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1704 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001705 case 39:
1706 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1707 break;
1708 case 49:
1709 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1710 break;
1711 default:
1712 if(code >= 30 && code <= 37) {
1713 terminal->curr_attr.fg = code - 30;
1714 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1715 terminal->curr_attr.fg += 8;
1716 } else if(code >= 40 && code <= 47) {
1717 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001718 } else if (code >= 90 && code <= 97) {
1719 terminal->curr_attr.fg = code - 90 + 8;
1720 } else if (code >= 100 && code <= 107) {
1721 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001722 } else if(code >= 256 && code < 512) {
1723 terminal->curr_attr.fg = code - 256;
1724 } else if(code >= 512 && code < 768) {
1725 terminal->curr_attr.bg = code - 512;
1726 } else {
1727 fprintf(stderr, "Unknown SGR code: %d\n", code);
1728 }
1729 break;
1730 }
1731}
1732
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001733/* Returns 1 if c was special, otherwise 0 */
1734static int
1735handle_special_char(struct terminal *terminal, char c)
1736{
1737 union utf8_char *row;
1738 struct attr *attr_row;
1739
1740 row = terminal_get_row(terminal, terminal->row);
1741 attr_row = terminal_get_attr_row(terminal, terminal->row);
1742
1743 switch(c) {
1744 case '\r':
1745 terminal->column = 0;
1746 break;
1747 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001748 if (terminal->mode & MODE_LF_NEWLINE) {
1749 terminal->column = 0;
1750 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001751 /* fallthrough */
1752 case '\v':
1753 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001754 terminal->row++;
1755 if(terminal->row > terminal->margin_bottom) {
1756 terminal->row = terminal->margin_bottom;
1757 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001758 }
1759
1760 break;
1761 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001762 while (terminal->column < terminal->width) {
1763 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001764 if (terminal->mode & MODE_IRM)
1765 terminal_shift_line(terminal, +1);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001766 row[terminal->column].byte[0] = ' ';
1767 row[terminal->column].byte[1] = '\0';
1768 attr_row[terminal->column] = terminal->curr_attr;
1769 terminal->column++;
1770 }
1771 if (terminal->column >= terminal->width) {
1772 terminal->column = terminal->width - 1;
1773 }
1774
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001775 break;
1776 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001777 if (terminal->column >= terminal->width) {
1778 terminal->column = terminal->width - 2;
1779 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001780 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001781 } else if (terminal->mode & MODE_AUTOWRAP) {
1782 terminal->column = terminal->width - 1;
1783 terminal->row -= 1;
1784 if (terminal->row < terminal->margin_top) {
1785 terminal->row = terminal->margin_top;
1786 terminal_scroll(terminal, -1);
1787 }
1788 }
1789
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001790 break;
1791 case '\a':
1792 /* Bell */
1793 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001794 case '\x0E': /* SO */
1795 terminal->cs = terminal->g1;
1796 break;
1797 case '\x0F': /* SI */
1798 terminal->cs = terminal->g0;
1799 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001800 default:
1801 return 0;
1802 }
1803
1804 return 1;
1805}
1806
1807static void
1808handle_char(struct terminal *terminal, union utf8_char utf8)
1809{
1810 union utf8_char *row;
1811 struct attr *attr_row;
1812
1813 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001814
1815 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001816
1817 /* There are a whole lot of non-characters, control codes,
1818 * and formatting codes that should probably be ignored,
1819 * for example: */
1820 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1821 /* BOM, ignore */
1822 return;
1823 }
1824
1825 /* Some of these non-characters should be translated, e.g.: */
1826 if (utf8.byte[0] < 32) {
1827 utf8.byte[0] = utf8.byte[0] + 64;
1828 }
1829
1830 /* handle right margin effects */
1831 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001832 if (terminal->mode & MODE_AUTOWRAP) {
1833 terminal->column = 0;
1834 terminal->row += 1;
1835 if (terminal->row > terminal->margin_bottom) {
1836 terminal->row = terminal->margin_bottom;
1837 terminal_scroll(terminal, +1);
1838 }
1839 } else {
1840 terminal->column--;
1841 }
1842 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001843
1844 row = terminal_get_row(terminal, terminal->row);
1845 attr_row = terminal_get_attr_row(terminal, terminal->row);
1846
Callum Lowcay69e96582011-01-07 19:47:00 +00001847 if (terminal->mode & MODE_IRM)
1848 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001849 row[terminal->column] = utf8;
1850 attr_row[terminal->column++] = terminal->curr_attr;
1851
1852 if (utf8.ch != terminal->last_char.ch)
1853 terminal->last_char = utf8;
1854}
1855
Callum Lowcay30eeae52011-01-07 19:46:55 +00001856static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001857escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1858{
1859 int len, i;
1860
1861 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1862 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1863 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1864 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1865 else len = 1; /* Invalid, cannot happen */
1866
1867 if (terminal->escape_length + len <= MAX_ESCAPE) {
1868 for (i = 0; i < len; i++)
1869 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1870 terminal->escape_length += len;
1871 } else if (terminal->escape_length < MAX_ESCAPE) {
1872 terminal->escape[terminal->escape_length++] = 0;
1873 }
1874}
1875
1876static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001877terminal_data(struct terminal *terminal, const char *data, size_t length)
1878{
1879 int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001880 union utf8_char utf8;
1881 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001882
1883 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001884 parser_state =
1885 utf8_next_char(&terminal->state_machine, data[i]);
1886 switch(parser_state) {
1887 case utf8state_accept:
1888 utf8.ch = terminal->state_machine.s.ch;
1889 break;
1890 case utf8state_reject:
1891 /* the unicode replacement character */
1892 utf8.byte[0] = 0xEF;
1893 utf8.byte[1] = 0xBF;
1894 utf8.byte[2] = 0xBD;
1895 utf8.byte[3] = 0x00;
1896 break;
1897 default:
1898 continue;
1899 }
1900
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001901 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001902 switch (terminal->state) {
1903 case escape_state_escape:
1904 escape_append_utf8(terminal, utf8);
1905 switch (utf8.byte[0]) {
1906 case 'P': /* DCS */
1907 terminal->state = escape_state_dcs;
1908 break;
1909 case '[': /* CSI */
1910 terminal->state = escape_state_csi;
1911 break;
1912 case ']': /* OSC */
1913 terminal->state = escape_state_osc;
1914 break;
1915 case '#':
1916 case '(':
1917 case ')': /* special */
1918 terminal->state = escape_state_special;
1919 break;
1920 case '^': /* PM (not implemented) */
1921 case '_': /* APC (not implemented) */
1922 terminal->state = escape_state_ignore;
1923 break;
1924 default:
1925 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001926 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13001927 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001928 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001929 continue;
1930 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001931 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1932 /* do nothing */
1933 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001934 terminal->escape_flags |= ESC_FLAG_WHAT;
1935 } else if (utf8.byte[0] == '>') {
1936 terminal->escape_flags |= ESC_FLAG_GT;
1937 } else if (utf8.byte[0] == '!') {
1938 terminal->escape_flags |= ESC_FLAG_BANG;
1939 } else if (utf8.byte[0] == '$') {
1940 terminal->escape_flags |= ESC_FLAG_CASH;
1941 } else if (utf8.byte[0] == '\'') {
1942 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1943 } else if (utf8.byte[0] == '"') {
1944 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1945 } else if (utf8.byte[0] == ' ') {
1946 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001947 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001948 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001949 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13001950 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001951 }
1952
1953 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1954 utf8.byte[0] == '`')
1955 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001956 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001957 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001958 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001959 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001960 continue;
1961 case escape_state_inner_escape:
1962 if (utf8.byte[0] == '\\') {
1963 terminal->state = escape_state_normal;
1964 if (terminal->outer_state == escape_state_dcs) {
1965 handle_dcs(terminal);
1966 } else if (terminal->outer_state == escape_state_osc) {
1967 handle_osc(terminal);
1968 }
1969 } else if (utf8.byte[0] == '\e') {
1970 terminal->state = terminal->outer_state;
1971 escape_append_utf8(terminal, utf8);
1972 if (terminal->escape_length >= MAX_ESCAPE)
1973 terminal->state = escape_state_normal;
1974 } else {
1975 terminal->state = terminal->outer_state;
1976 if (terminal->escape_length < MAX_ESCAPE)
1977 terminal->escape[terminal->escape_length++] = '\e';
1978 escape_append_utf8(terminal, utf8);
1979 if (terminal->escape_length >= MAX_ESCAPE)
1980 terminal->state = escape_state_normal;
1981 }
1982 continue;
1983 case escape_state_dcs:
1984 case escape_state_osc:
1985 case escape_state_ignore:
1986 if (utf8.byte[0] == '\e') {
1987 terminal->outer_state = terminal->state;
1988 terminal->state = escape_state_inner_escape;
1989 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
1990 terminal->state = escape_state_normal;
1991 handle_osc(terminal);
1992 } else {
1993 escape_append_utf8(terminal, utf8);
1994 if (terminal->escape_length >= MAX_ESCAPE)
1995 terminal->state = escape_state_normal;
1996 }
1997 continue;
1998 case escape_state_special:
1999 escape_append_utf8(terminal, utf8);
2000 terminal->state = escape_state_normal;
2001 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2002 handle_special_escape(terminal, terminal->escape[1],
2003 utf8.byte[0]);
2004 }
2005 continue;
2006 default:
2007 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002008 }
2009
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002010 /* this is valid, because ASCII characters are never used to
2011 * introduce a multibyte sequence in UTF-8 */
2012 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002013 terminal->state = escape_state_escape;
2014 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002015 terminal->escape[0] = '\e';
2016 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002017 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002018 } else {
2019 handle_char(terminal, utf8);
2020 } /* if */
2021 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002022
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002023 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002024}
2025
2026static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002027data_source_target(void *data,
2028 struct wl_data_source *source, const char *mime_type)
2029{
2030 fprintf(stderr, "data_source_target, %s\n", mime_type);
2031}
2032
2033static void
2034data_source_send(void *data,
2035 struct wl_data_source *source,
2036 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002037{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002038 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002039
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002040 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002041}
2042
2043static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002044data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002045{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002046 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002047}
2048
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002049static const struct wl_data_source_listener data_source_listener = {
2050 data_source_target,
2051 data_source_send,
2052 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002053};
2054
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002055static int
2056handle_bound_key(struct terminal *terminal,
2057 struct input *input, uint32_t sym, uint32_t time)
2058{
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002059 switch (sym) {
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002060 case XK_X:
2061 /* Cut selection; terminal doesn't do cut, fall
2062 * through to copy. */
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002063 case XK_C:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002064 terminal->selection =
2065 display_create_data_source(terminal->display);
2066 wl_data_source_offer(terminal->selection,
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002067 "text/plain;charset=utf-8");
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002068 wl_data_source_add_listener(terminal->selection,
2069 &data_source_listener, terminal);
2070 input_set_selection(input, terminal->selection, time);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002071 return 1;
2072 case XK_V:
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002073 input_receive_selection_data_to_fd(input,
2074 "text/plain;charset=utf-8",
2075 terminal->master);
2076
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002077 return 1;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002078 default:
2079 return 0;
2080 }
2081}
2082
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002083static void
2084key_handler(struct window *window, struct input *input, uint32_t time,
2085 uint32_t key, uint32_t sym, uint32_t state, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002086{
2087 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002088 char ch[MAX_RESPONSE];
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002089 uint32_t modifiers;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002090 int len = 0;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002091
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002092 modifiers = input_get_modifiers(input);
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002093 if ((modifiers & XKB_COMMON_CONTROL_MASK) &&
2094 (modifiers & XKB_COMMON_SHIFT_MASK) &&
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002095 state && handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002096 return;
2097
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002098 switch (sym) {
2099 case XK_F11:
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002100 if (!state)
2101 break;
2102 terminal->fullscreen ^= 1;
2103 window_set_fullscreen(window, terminal->fullscreen);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002104 window_schedule_redraw(terminal->window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002105 break;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002106
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002107 case XK_BackSpace:
2108 case XK_Tab:
2109 case XK_Linefeed:
2110 case XK_Clear:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002111 case XK_Pause:
2112 case XK_Scroll_Lock:
2113 case XK_Sys_Req:
2114 case XK_Escape:
2115 ch[len++] = sym & 0x7f;
2116 break;
2117
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002118 case XK_Return:
2119 if (terminal->mode & MODE_LF_NEWLINE) {
2120 ch[len++] = 0x0D;
2121 ch[len++] = 0x0A;
2122 } else {
2123 ch[len++] = 0x0D;
2124 }
2125 break;
2126
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002127 case XK_Shift_L:
2128 case XK_Shift_R:
2129 case XK_Control_L:
2130 case XK_Control_R:
2131 case XK_Alt_L:
2132 case XK_Alt_R:
2133 break;
2134
Callum Lowcay7e08e902011-01-07 19:47:02 +00002135 case XK_Insert:
2136 len = function_key_response('[', 2, modifiers, '~', ch);
2137 break;
2138 case XK_Delete:
2139 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2140 ch[len++] = '\x04';
2141 } else {
2142 len = function_key_response('[', 3, modifiers, '~', ch);
2143 }
2144 break;
2145 case XK_Page_Up:
2146 len = function_key_response('[', 5, modifiers, '~', ch);
2147 break;
2148 case XK_Page_Down:
2149 len = function_key_response('[', 6, modifiers, '~', ch);
2150 break;
2151 case XK_F1:
2152 len = function_key_response('O', 1, modifiers, 'P', ch);
2153 break;
2154 case XK_F2:
2155 len = function_key_response('O', 1, modifiers, 'Q', ch);
2156 break;
2157 case XK_F3:
2158 len = function_key_response('O', 1, modifiers, 'R', ch);
2159 break;
2160 case XK_F4:
2161 len = function_key_response('O', 1, modifiers, 'S', ch);
2162 break;
2163 case XK_F5:
2164 len = function_key_response('[', 15, modifiers, '~', ch);
2165 break;
2166 case XK_F6:
2167 len = function_key_response('[', 17, modifiers, '~', ch);
2168 break;
2169 case XK_F7:
2170 len = function_key_response('[', 18, modifiers, '~', ch);
2171 break;
2172 case XK_F8:
2173 len = function_key_response('[', 19, modifiers, '~', ch);
2174 break;
2175 case XK_F9:
2176 len = function_key_response('[', 20, modifiers, '~', ch);
2177 break;
2178 case XK_F10:
2179 len = function_key_response('[', 21, modifiers, '~', ch);
2180 break;
2181 case XK_F12:
2182 len = function_key_response('[', 24, modifiers, '~', ch);
2183 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002184 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002185 /* Handle special keys with alternate mappings */
2186 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2187 if (len != 0) break;
2188
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002189 if (modifiers & XKB_COMMON_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002190 if (sym >= '3' && sym <= '7')
2191 sym = (sym & 0x1f) + 8;
2192
2193 if (!((sym >= '!' && sym <= '/') ||
2194 (sym >= '8' && sym <= '?') ||
2195 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2196 else if (sym == '2') sym = 0x00;
2197 else if (sym == '/') sym = 0x1F;
2198 else if (sym == '8' || sym == '?') sym = 0x7F;
2199 } else if ((terminal->mode & MODE_ALT_SENDS_ESC) &&
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002200 (modifiers & XKB_COMMON_MOD1_MASK))
Callum Lowcay7e08e902011-01-07 19:47:02 +00002201 {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002202 ch[len++] = 0x1b;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002203 } else if (modifiers & XKB_COMMON_MOD1_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002204 sym = sym | 0x80;
2205 }
2206
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002207 if (sym < 256)
2208 ch[len++] = sym;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002209 break;
2210 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002211
2212 if (state && len > 0)
Kristian Høgsberg26130862011-08-24 11:30:21 -04002213 terminal_write(terminal, ch, len);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002214}
2215
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002216static void
2217keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002218 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002219{
2220 struct terminal *terminal = data;
2221
2222 terminal->focused = (device != NULL);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002223 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002224}
2225
Kristian Høgsberg59826582011-01-20 11:56:57 -05002226static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002227button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002228 struct input *input, uint32_t time,
2229 int button, int state, void *data)
2230{
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002231 struct window *window = data;
2232 struct terminal *terminal = window_get_user_data(window);
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;
2243 window_schedule_redraw(window);
2244 } 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øgsberg87a57bb2012-01-09 10:34:35 -05002256 struct window *window = data;
2257 struct terminal *terminal = window_get_user_data(window);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002258
2259 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002260 input_get_position(input,
2261 &terminal->selection_end_x,
2262 &terminal->selection_end_y);
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002263 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002264 }
2265
2266 return POINTER_IBEAM;
2267}
2268
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002269static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002270terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002271{
2272 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002273 cairo_surface_t *surface;
2274 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002275
2276 terminal = malloc(sizeof *terminal);
2277 if (terminal == NULL)
2278 return terminal;
2279
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002280 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002281 terminal->fullscreen = fullscreen;
Callum Lowcay30eeae52011-01-07 19:46:55 +00002282 terminal->color_scheme = &DEFAULT_COLORS;
2283 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002284 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002285 terminal->margin_bottom = -1;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002286 terminal->window = window_create(display, 500, 400);
2287 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øgsberga8a0db32012-01-09 11:12:05 -05002302 widget_set_button_handler(window_get_widget(terminal->window),
2303 button_handler);
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002304
Kristian Høgsberg87a57bb2012-01-09 10:34:35 -05002305 widget_set_motion_handler(window_get_widget(terminal->window),
2306 motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002307
Kristian Høgsberg09531622010-06-14 23:22:15 -04002308 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2309 cr = cairo_create(surface);
Kristian Høgsberg09531622010-06-14 23:22:15 -04002310 cairo_set_font_size(cr, 14);
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002311 cairo_select_font_face (cr, "mono",
2312 CAIRO_FONT_SLANT_NORMAL,
2313 CAIRO_FONT_WEIGHT_BOLD);
2314 terminal->font_bold = cairo_get_scaled_font (cr);
2315 cairo_scaled_font_reference(terminal->font_bold);
2316
2317 cairo_select_font_face (cr, "mono",
2318 CAIRO_FONT_SLANT_NORMAL,
2319 CAIRO_FONT_WEIGHT_NORMAL);
2320 terminal->font_normal = cairo_get_scaled_font (cr);
2321 cairo_scaled_font_reference(terminal->font_normal);
2322
Kristian Høgsberg09531622010-06-14 23:22:15 -04002323 cairo_font_extents(cr, &terminal->extents);
2324 cairo_destroy(cr);
2325 cairo_surface_destroy(surface);
2326
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002327 return terminal;
2328}
2329
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002330static void
2331io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002332{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002333 struct terminal *terminal =
2334 container_of(task, struct terminal, io_task);
2335 char buffer[256];
2336 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002337
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002338 if (events & EPOLLHUP)
2339 exit(0);
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002340
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002341 len = read(terminal->master, buffer, sizeof buffer);
2342 if (len < 0)
2343 exit(0);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002344
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002345 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002346}
2347
2348static int
2349terminal_run(struct terminal *terminal, const char *path)
2350{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002351 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002352 pid_t pid;
2353
2354 pid = forkpty(&master, NULL, NULL, NULL);
2355 if (pid == 0) {
Callum Lowcay86653ed2011-01-07 19:47:03 +00002356 setenv("TERM", "xterm-256color", 1);
2357 setenv("COLORTERM", "xterm-256color", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002358 if (execl(path, path, NULL)) {
2359 printf("exec failed: %m\n");
2360 exit(EXIT_FAILURE);
2361 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002362 } else if (pid < 0) {
2363 fprintf(stderr, "failed to fork and create pty (%m).\n");
2364 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002365 }
2366
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002367 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002368 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002369 terminal->io_task.run = io_handler;
2370 display_watch_fd(terminal->display, terminal->master,
2371 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002372
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002373 window_set_fullscreen(terminal->window, terminal->fullscreen);
2374 if (!terminal->fullscreen)
2375 terminal_resize(terminal, 80, 24);
2376
Kristian Høgsberg54b86832011-06-21 16:31:11 -04002377 terminal_draw(terminal);
2378
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002379 return 0;
2380}
2381
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002382static const GOptionEntry option_entries[] = {
2383 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
2384 &option_fullscreen, "Run in fullscreen mode" },
2385 { NULL }
2386};
2387
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002388int main(int argc, char *argv[])
2389{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002390 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002391 struct terminal *terminal;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002392
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002393 d = display_create(&argc, &argv, option_entries);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002394 if (d == NULL) {
2395 fprintf(stderr, "failed to create display: %m\n");
2396 return -1;
2397 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002398
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002399 terminal = terminal_create(d, option_fullscreen);
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002400 if (terminal_run(terminal, "/bin/bash"))
2401 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002402
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002403 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002404
2405 return 0;
2406}