blob: 106e3d1dbf0580a52b0b50b651d7e4e35f759ac5 [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
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -040098utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000099{
100 switch(machine->state) {
101 case utf8state_start:
102 case utf8state_accept:
103 case utf8state_reject:
104 machine->s.ch = 0;
105 machine->len = 0;
106 if(c == 0xC0 || c == 0xC1) {
107 /* overlong encoding, reject */
108 machine->state = utf8state_reject;
109 } else if((c & 0x80) == 0) {
110 /* single byte, accept */
111 machine->s.byte[machine->len++] = c;
112 machine->state = utf8state_accept;
113 } else if((c & 0xC0) == 0x80) {
114 /* parser out of sync, ignore byte */
115 machine->state = utf8state_start;
116 } else if((c & 0xE0) == 0xC0) {
117 /* start of two byte sequence */
118 machine->s.byte[machine->len++] = c;
119 machine->state = utf8state_expect1;
120 } else if((c & 0xF0) == 0xE0) {
121 /* start of three byte sequence */
122 machine->s.byte[machine->len++] = c;
123 machine->state = utf8state_expect2;
124 } else if((c & 0xF8) == 0xF0) {
125 /* start of four byte sequence */
126 machine->s.byte[machine->len++] = c;
127 machine->state = utf8state_expect3;
128 } else {
129 /* overlong encoding, reject */
130 machine->state = utf8state_reject;
131 }
132 break;
133 case utf8state_expect3:
134 machine->s.byte[machine->len++] = c;
135 if((c & 0xC0) == 0x80) {
136 /* all good, continue */
137 machine->state = utf8state_expect2;
138 } else {
139 /* missing extra byte, reject */
140 machine->state = utf8state_reject;
141 }
142 break;
143 case utf8state_expect2:
144 machine->s.byte[machine->len++] = c;
145 if((c & 0xC0) == 0x80) {
146 /* all good, continue */
147 machine->state = utf8state_expect1;
148 } else {
149 /* missing extra byte, reject */
150 machine->state = utf8state_reject;
151 }
152 break;
153 case utf8state_expect1:
154 machine->s.byte[machine->len++] = c;
155 if((c & 0xC0) == 0x80) {
156 /* all good, accept */
157 machine->state = utf8state_accept;
158 } else {
159 /* missing extra byte, reject */
160 machine->state = utf8state_reject;
161 }
162 break;
163 default:
164 machine->state = utf8state_reject;
165 break;
166 }
167
168 return machine->state;
169}
170
Callum Lowcay256e72f2011-01-07 19:47:01 +0000171struct char_sub {
172 union utf8_char match;
173 union utf8_char replace;
174};
175/* Set last char_sub match to NULL char */
176typedef struct char_sub *character_set;
177
178struct char_sub CS_US[] = {
179 {{{0, }}, {{0, }}}
180};
181static struct char_sub CS_UK[] = {
182 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}},
183 {{{0, }}, {{0, }}}
184};
185static struct char_sub CS_SPECIAL[] = {
186 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond */
187 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell */
188 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT */
189 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF */
190 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR */
191 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF */
192 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree */
193 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus */
194 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL */
195 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT */
196 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB */
197 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT */
198 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT */
199 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_RB */
200 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS */
201 {{{'o', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
202 {{{'p', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
203 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
204 {{{'r', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
205 {{{'s', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
206 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR */
207 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL */
208 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU */
209 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD */
210 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V */
211 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE */
212 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE */
213 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI */
214 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ */
215 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND */
216 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT */
217 {{{0, }}, {{0, }}}
218};
219
220static void
221apply_char_set(character_set cs, union utf8_char *utf8)
222{
223 int i = 0;
224
225 while (cs[i].match.byte[0]) {
226 if ((*utf8).ch == cs[i].match.ch) {
227 *utf8 = cs[i].replace;
228 break;
229 }
230 i++;
231 }
232}
233
Callum Lowcay7e08e902011-01-07 19:47:02 +0000234struct key_map {
235 int sym;
236 int num;
237 char escape;
238 char code;
239};
240/* Set last key_sub sym to NULL */
241typedef struct key_map *keyboard_mode;
242
243static struct key_map KM_NORMAL[] = {
244 {XK_Left, 1, '[', 'D'},
245 {XK_Right, 1, '[', 'C'},
246 {XK_Up, 1, '[', 'A'},
247 {XK_Down, 1, '[', 'B'},
248 {XK_Home, 1, '[', 'H'},
249 {XK_End, 1, '[', 'F'},
250 {0, 0, 0, 0}
251};
252static struct key_map KM_APPLICATION[] = {
253 {XK_Left, 1, 'O', 'D'},
254 {XK_Right, 1, 'O', 'C'},
255 {XK_Up, 1, 'O', 'A'},
256 {XK_Down, 1, 'O', 'B'},
257 {XK_Home, 1, 'O', 'H'},
258 {XK_End, 1, 'O', 'F'},
259 {XK_KP_Enter, 1, 'O', 'M'},
260 {XK_KP_Multiply, 1, 'O', 'j'},
261 {XK_KP_Add, 1, 'O', 'k'},
262 {XK_KP_Separator, 1, 'O', 'l'},
263 {XK_KP_Subtract, 1, 'O', 'm'},
264 {XK_KP_Divide, 1, 'O', 'o'},
265 {0, 0, 0, 0}
266};
267
268static int
269function_key_response(char escape, int num, uint32_t modifiers,
270 char code, char *response)
271{
272 int mod_num = 0;
273 int len;
274
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -0500275 if (modifiers & XKB_COMMON_SHIFT_MASK) mod_num |= 1;
276 if (modifiers & XKB_COMMON_MOD1_MASK) mod_num |= 2;
277 if (modifiers & XKB_COMMON_CONTROL_MASK) mod_num |= 4;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000278
279 if (mod_num != 0)
280 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
281 num, mod_num + 1, code);
282 else if (code != '~')
283 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
284 escape, code);
285 else
286 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
287 escape, num, code);
288
289 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
290 else return len;
291}
292
293/* returns the number of bytes written into response,
294 * which must have room for MAX_RESPONSE bytes */
295static int
296apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
297{
298 struct key_map map;
299 int len = 0;
300 int i = 0;
301
302 while (mode[i].sym) {
303 map = mode[i++];
304 if (sym == map.sym) {
305 len = function_key_response(map.escape, map.num,
306 modifiers, map.code,
307 response);
308 break;
309 }
310 }
311
312 return len;
313}
314
Callum Lowcay30eeae52011-01-07 19:46:55 +0000315struct terminal_color { double r, g, b, a; };
316struct attr {
317 unsigned char fg, bg;
318 char a; /* attributes format:
319 * 76543210
Callum Lowcay81179db2011-01-10 12:14:01 +1300320 * cilub */
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500321 char s; /* in selection */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000322};
323struct color_scheme {
324 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500325 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000326 struct attr default_attr;
327};
328
329static void
330attr_init(struct attr *data_attr, struct attr attr, int n)
331{
332 int i;
333 for (i = 0; i < n; i++) {
334 data_attr[i] = attr;
335 }
336}
337
Callum Lowcay67a201d2011-01-12 19:23:41 +1300338enum escape_state {
339 escape_state_normal = 0,
340 escape_state_escape,
341 escape_state_dcs,
342 escape_state_csi,
343 escape_state_osc,
344 escape_state_inner_escape,
345 escape_state_ignore,
346 escape_state_special
347};
348
349#define ESC_FLAG_WHAT 0x01
350#define ESC_FLAG_GT 0x02
351#define ESC_FLAG_BANG 0x04
352#define ESC_FLAG_CASH 0x08
353#define ESC_FLAG_SQUOTE 0x10
354#define ESC_FLAG_DQUOTE 0x20
355#define ESC_FLAG_SPACE 0x40
356
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500357struct terminal {
358 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500359 struct widget *widget;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500360 struct display *display;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000361 union utf8_char *data;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400362 struct task io_task;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000363 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000364 struct attr *data_attr;
365 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000366 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000367 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000368 char saved_origin_mode;
369 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000370 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000371 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000372 character_set cs, g0, g1;
373 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000374 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000375 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500376 int width, height, start, row, column;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000377 int saved_row, saved_column;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500378 int fd, master;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500379 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300380 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500381 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300382 enum escape_state state;
383 enum escape_state outer_state;
384 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000385 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500386 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500387 int fullscreen;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500388 int focused;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400389 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000390 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400391 cairo_font_extents_t extents;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500392 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500393
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400394 struct wl_data_source *selection;
395 int32_t dragging;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500396 int selection_start_x, selection_start_y;
397 int selection_end_x, selection_end_y;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500398};
399
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000400/* Create default tab stops, every 8 characters */
401static void
402terminal_init_tabs(struct terminal *terminal)
403{
404 int i = 0;
405
406 while (i < terminal->width) {
407 if (i % 8 == 0)
408 terminal->tab_ruler[i] = 1;
409 else
410 terminal->tab_ruler[i] = 0;
411 i++;
412 }
413}
414
Callum Lowcay30eeae52011-01-07 19:46:55 +0000415static void
416terminal_init(struct terminal *terminal)
417{
418 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000419 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000420 terminal->mode = MODE_SHOW_CURSOR |
421 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000422 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000423 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000424
425 terminal->row = 0;
426 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000427
Callum Lowcay256e72f2011-01-07 19:47:01 +0000428 terminal->g0 = CS_US;
429 terminal->g1 = CS_US;
430 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000431 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000432
433 terminal->saved_g0 = terminal->g0;
434 terminal->saved_g1 = terminal->g1;
435 terminal->saved_cs = terminal->cs;
436
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000437 terminal->saved_attr = terminal->curr_attr;
438 terminal->saved_origin_mode = terminal->origin_mode;
439 terminal->saved_row = terminal->row;
440 terminal->saved_column = terminal->column;
441
442 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000443}
444
445static void
446init_color_table(struct terminal *terminal)
447{
448 int c, r;
449 struct terminal_color *color_table = terminal->color_table;
450
451 for (c = 0; c < 256; c ++) {
452 if (c < 16) {
453 color_table[c] = terminal->color_scheme->palette[c];
454 } else if (c < 232) {
455 r = c - 16;
456 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
457 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
458 color_table[c].r = ((double)(r % 6) / 6.0);
459 color_table[c].a = 1.0;
460 } else {
461 r = (c - 232) * 10 + 8;
462 color_table[c].r = ((double) r) / 256.0;
463 color_table[c].g = color_table[c].r;
464 color_table[c].b = color_table[c].r;
465 color_table[c].a = 1.0;
466 }
467 }
468}
469
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000470static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500471terminal_get_row(struct terminal *terminal, int row)
472{
473 int index;
474
475 index = (row + terminal->start) % terminal->height;
476
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000477 return &terminal->data[index * terminal->width];
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500478}
479
Callum Lowcay30eeae52011-01-07 19:46:55 +0000480static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500481terminal_get_attr_row(struct terminal *terminal, int row)
482{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000483 int index;
484
485 index = (row + terminal->start) % terminal->height;
486
487 return &terminal->data_attr[index * terminal->width];
488}
489
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500490union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300491 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500492 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500493};
494
Kristian Høgsberg59826582011-01-20 11:56:57 -0500495static int
496terminal_compare_position(struct terminal *terminal,
497 int x, int y, int32_t ref_row, int32_t ref_col)
498{
499 struct rectangle allocation;
500 int top_margin, side_margin, col, row, ref_x;
501
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500502 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsberg59826582011-01-20 11:56:57 -0500503 side_margin = allocation.x + (allocation.width - terminal->width * terminal->extents.max_x_advance) / 2;
504 top_margin = allocation.y + (allocation.height - terminal->height * terminal->extents.height) / 2;
505
506 col = (x - side_margin) / terminal->extents.max_x_advance;
507 row = (y - top_margin) / terminal->extents.height;
508
509 ref_x = side_margin + ref_col * terminal->extents.max_x_advance +
510 terminal->extents.max_x_advance / 2;
511
512 if (row < ref_row)
513 return -1;
514 if (row == ref_row) {
515 if (col < ref_col)
516 return -1;
517 if (col == ref_col && x < ref_x)
518 return -1;
519 }
520
521 return 1;
522}
523
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500524static void
525terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500526 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500527{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500528 struct attr attr;
529 int foreground, background, tmp;
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500530 int start_cmp, end_cmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500531
532 start_cmp =
533 terminal_compare_position(terminal,
534 terminal->selection_start_x,
535 terminal->selection_start_y,
536 row, col);
537 end_cmp =
538 terminal_compare_position(terminal,
539 terminal->selection_end_x,
540 terminal->selection_end_y,
541 row, col);
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500542 decoded->attr.s = 0;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500543 if (start_cmp < 0 && end_cmp > 0)
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500544 decoded->attr.s = 1;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500545 else if (end_cmp < 0 && start_cmp > 0)
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500546 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500547
548 /* get the attributes for this character cell */
549 attr = terminal_get_attr_row(terminal, row)[col];
550 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500551 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500552 ((terminal->mode & MODE_SHOW_CURSOR) &&
553 terminal->focused && terminal->row == row &&
554 terminal->column == col)) {
555 foreground = attr.bg;
556 background = attr.fg;
557 if (attr.a & ATTRMASK_BOLD) {
558 if (foreground <= 16) foreground |= 0x08;
559 if (background <= 16) background &= 0x07;
560 }
561 } else {
562 foreground = attr.fg;
563 background = attr.bg;
564 }
565
566 if (terminal->mode & MODE_INVERSE) {
567 tmp = foreground;
568 foreground = background;
569 background = tmp;
570 if (attr.a & ATTRMASK_BOLD) {
571 if (foreground <= 16) foreground |= 0x08;
572 if (background <= 16) background &= 0x07;
573 }
574 }
575
Callum Lowcay9d708b02011-01-12 20:06:17 +1300576 decoded->attr.fg = foreground;
577 decoded->attr.bg = background;
578 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000579}
580
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500581
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500582static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000583terminal_scroll_buffer(struct terminal *terminal, int d)
584{
585 int i;
586
587 d = d % (terminal->height + 1);
588 terminal->start = (terminal->start + d) % terminal->height;
589 if (terminal->start < 0) terminal->start = terminal->height + terminal->start;
590 if(d < 0) {
591 d = 0 - d;
592 for(i = 0; i < d; i++) {
593 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
594 attr_init(terminal_get_attr_row(terminal, i),
595 terminal->curr_attr, terminal->width);
596 }
597 } else {
598 for(i = terminal->height - d; i < terminal->height; i++) {
599 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
600 attr_init(terminal_get_attr_row(terminal, i),
601 terminal->curr_attr, terminal->width);
602 }
603 }
604}
605
606static void
607terminal_scroll_window(struct terminal *terminal, int d)
608{
609 int i;
610 int window_height;
611 int from_row, to_row;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000612
613 // scrolling range is inclusive
614 window_height = terminal->margin_bottom - terminal->margin_top + 1;
615 d = d % (window_height + 1);
616 if(d < 0) {
617 d = 0 - d;
618 to_row = terminal->margin_bottom;
619 from_row = terminal->margin_bottom - d;
620
621 for (i = 0; i < (window_height - d); i++) {
622 memcpy(terminal_get_row(terminal, to_row - i),
623 terminal_get_row(terminal, from_row - i),
624 terminal->data_pitch);
625 memcpy(terminal_get_attr_row(terminal, to_row - i),
626 terminal_get_attr_row(terminal, from_row - i),
627 terminal->attr_pitch);
628 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000629 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
630 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000631 attr_init(terminal_get_attr_row(terminal, i),
632 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000633 }
634 } else {
635 to_row = terminal->margin_top;
636 from_row = terminal->margin_top + d;
637
638 for (i = 0; i < (window_height - d); i++) {
639 memcpy(terminal_get_row(terminal, to_row + i),
640 terminal_get_row(terminal, from_row + i),
641 terminal->data_pitch);
642 memcpy(terminal_get_attr_row(terminal, to_row + i),
643 terminal_get_attr_row(terminal, from_row + i),
644 terminal->attr_pitch);
645 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000646 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
647 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000648 attr_init(terminal_get_attr_row(terminal, i),
649 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000650 }
651 }
652}
653
654static void
655terminal_scroll(struct terminal *terminal, int d)
656{
657 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
658 terminal_scroll_buffer(terminal, d);
659 else
660 terminal_scroll_window(terminal, d);
661}
662
663static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000664terminal_shift_line(struct terminal *terminal, int d)
665{
666 union utf8_char *row;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500667 struct attr *attr_row;
Callum Lowcay69e96582011-01-07 19:47:00 +0000668
669 row = terminal_get_row(terminal, terminal->row);
670 attr_row = terminal_get_attr_row(terminal, terminal->row);
671
672 if ((terminal->width + d) <= terminal->column)
673 d = terminal->column + 1 - terminal->width;
674 if ((terminal->column + d) >= terminal->width)
675 d = terminal->width - terminal->column - 1;
676
677 if (d < 0) {
678 d = 0 - d;
679 memmove(&row[terminal->column],
680 &row[terminal->column + d],
681 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
Callum Lowcay69e96582011-01-07 19:47:00 +0000682 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
683 (terminal->width - terminal->column - d) * sizeof(struct attr));
684 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
685 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
686 } else {
687 memmove(&row[terminal->column + d], &row[terminal->column],
688 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
689 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
690 (terminal->width - terminal->column - d) * sizeof(struct attr));
691 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
692 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
693 }
694}
695
696static void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500697terminal_resize_cells(struct terminal *terminal, int width, int height)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500698{
699 size_t size;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000700 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000701 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000702 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000703 int data_pitch, attr_pitch;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500704 int i, l, total_rows;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500705 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000706 struct winsize ws;
Kristian Hø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
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000715 data_pitch = width * sizeof(union utf8_char);
716 size = data_pitch * height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500717 data = malloc(size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000718 attr_pitch = width * sizeof(struct attr);
719 data_attr = malloc(attr_pitch * height);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000720 tab_ruler = malloc(width);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500721 memset(data, 0, size);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000722 memset(tab_ruler, 0, width);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000723 attr_init(data_attr, terminal->curr_attr, width * height);
724 if (terminal->data && terminal->data_attr) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500725 if (width > terminal->width)
726 l = terminal->width;
727 else
728 l = width;
729
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500730 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500731 total_rows = height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500732 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500733 total_rows = terminal->height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500734 }
735
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000736 for (i = 0; i < total_rows; i++) {
737 memcpy(&data[width * i],
738 terminal_get_row(terminal, i),
739 l * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000740 memcpy(&data_attr[width * i],
741 terminal_get_attr_row(terminal, i),
742 l * sizeof(struct attr));
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000743 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500744
745 free(terminal->data);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000746 free(terminal->data_attr);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000747 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500748 }
749
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000750 terminal->data_pitch = data_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000751 terminal->attr_pitch = attr_pitch;
Callum Lowcay86653ed2011-01-07 19:47:03 +0000752 terminal->margin_bottom =
753 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500754 terminal->width = width;
755 terminal->height = height;
756 terminal->data = data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000757 terminal->data_attr = data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000758 terminal->tab_ruler = tab_ruler;
759 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500760
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000761 /* Update the window size */
762 ws.ws_row = terminal->height;
763 ws.ws_col = terminal->width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500764 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500765 ws.ws_xpixel = allocation.width;
766 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000767 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500768}
769
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500770static void
771resize_handler(struct widget *widget,
772 int32_t width, int32_t height, void *data)
773{
774 struct terminal *terminal = data;
775 int32_t columns, rows, m;
776
777 m = 2 * terminal->margin;
778 columns = (width - m) / (int32_t) terminal->extents.max_x_advance;
779 rows = (height - m) / (int32_t) terminal->extents.height;
780
781 if (!terminal->fullscreen) {
782 width = columns * terminal->extents.max_x_advance + m;
783 height = rows * terminal->extents.height + m;
784 widget_set_size(terminal->widget, width, height);
785 }
786
787 terminal_resize_cells(terminal, columns, rows);
788}
789
790static void
791terminal_resize(struct terminal *terminal, int columns, int rows)
792{
793 int32_t width, height, m;
794
795 if (terminal->fullscreen)
796 return;
797
798 m = 2 * terminal->margin;
799 width = columns * terminal->extents.max_x_advance + m;
800 height = rows * terminal->extents.height + m;
801 widget_schedule_resize(terminal->widget, width, height);
802}
803
Callum Lowcay30eeae52011-01-07 19:46:55 +0000804struct color_scheme DEFAULT_COLORS = {
805 {
806 {0, 0, 0, 1}, /* black */
807 {0.66, 0, 0, 1}, /* red */
808 {0 , 0.66, 0, 1}, /* green */
809 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
810 {0 , 0 , 0.66, 1}, /* blue */
811 {0.66, 0 , 0.66, 1}, /* magenta */
812 {0, 0.66, 0.66, 1}, /* cyan */
813 {0.66, 0.66, 0.66, 1}, /* light grey */
814 {0.22, 0.33, 0.33, 1}, /* dark grey */
815 {1, 0.33, 0.33, 1}, /* high red */
816 {0.33, 1, 0.33, 1}, /* high green */
817 {1, 1, 0.33, 1}, /* high yellow */
818 {0.33, 0.33, 1, 1}, /* high blue */
819 {1, 0.33, 1, 1}, /* high magenta */
820 {0.33, 1, 1, 1}, /* high cyan */
821 {1, 1, 1, 1} /* white */
822 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500823 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000824 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
825};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400826
Kristian Høgsberg22106762008-12-08 13:50:07 -0500827static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500828terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
829{
830 cairo_set_source_rgba(cr,
831 terminal->color_table[index].r,
832 terminal->color_table[index].g,
833 terminal->color_table[index].b,
834 terminal->color_table[index].a);
835}
836
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500837static void
838terminal_send_selection(struct terminal *terminal, int fd)
839{
840 int row, col;
841 union utf8_char *p_row;
842 union decoded_attr attr;
843 FILE *fp;
844 int len;
845
846 fp = fdopen(fd, "w");
847 for (row = 0; row < terminal->height; row++) {
848 p_row = terminal_get_row(terminal, row);
849 for (col = 0; col < terminal->width; col++) {
850 /* get the attributes for this character cell */
851 terminal_decode_attr(terminal, row, col, &attr);
852 if (!attr.attr.s)
853 continue;
854 len = strnlen((char *) p_row[col].byte, 4);
855 fwrite(p_row[col].byte, 1, len, fp);
856 }
857 }
858 fclose(fp);
859}
860
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500861struct glyph_run {
862 struct terminal *terminal;
863 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400864 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500865 union decoded_attr attr;
866 cairo_glyph_t glyphs[256], *g;
867};
868
869static void
870glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
871{
872 run->terminal = terminal;
873 run->cr = cr;
874 run->g = run->glyphs;
875 run->count = 0;
876 run->attr.key = 0;
877}
878
879static void
880glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
881{
882 cairo_scaled_font_t *font;
883
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500884 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
885 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300886 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500887 font = run->terminal->font_bold;
888 else
889 font = run->terminal->font_normal;
890 cairo_set_scaled_font(run->cr, font);
891 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300892 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500893
Callum Lowcay9d708b02011-01-12 20:06:17 +1300894 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
895 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500896 run->g = run->glyphs;
897 run->count = 0;
898 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300899 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500900}
901
902static void
903glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
904{
905 int num_glyphs;
906 cairo_scaled_font_t *font;
907
908 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
909
Callum Lowcay9d708b02011-01-12 20:06:17 +1300910 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500911 font = run->terminal->font_bold;
912 else
913 font = run->terminal->font_normal;
914
915 cairo_move_to(run->cr, x, y);
916 cairo_scaled_font_text_to_glyphs (font, x, y,
917 (char *) c->byte, 4,
918 &run->g, &num_glyphs,
919 NULL, NULL, NULL);
920 run->g += num_glyphs;
921 run->count += num_glyphs;
922}
923
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500924
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500925static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500926redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500927{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500928 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500929 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500930 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000931 int top_margin, side_margin;
932 int row, col;
933 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500934 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000935 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500936 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500937 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500938 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500939 cairo_font_extents_t extents;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500940
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500941 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500942 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500943 cr = cairo_create(surface);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500944 cairo_rectangle(cr, allocation.x, allocation.y,
945 allocation.width, allocation.height);
946 cairo_clip(cr);
947 cairo_push_group(cr);
948
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500949 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500950 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500951 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500952
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500953 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500954
Kristian Høgsberg59826582011-01-20 11:56:57 -0500955 extents = terminal->extents;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500956 side_margin = (allocation.width - terminal->width * extents.max_x_advance) / 2;
957 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500958
Callum Lowcay30eeae52011-01-07 19:46:55 +0000959 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500960 cairo_translate(cr, allocation.x + side_margin,
961 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500962 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000963 for (row = 0; row < terminal->height; row++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000964 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000965 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500966 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000967
Callum Lowcay9d708b02011-01-12 20:06:17 +1300968 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -0500969 continue;
970
Callum Lowcay9d708b02011-01-12 20:06:17 +1300971 terminal_set_color(terminal, cr, attr.attr.bg);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500972 cairo_move_to(cr, col * extents.max_x_advance,
973 row * extents.height);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000974 cairo_rel_line_to(cr, extents.max_x_advance, 0);
975 cairo_rel_line_to(cr, 0, extents.height);
976 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
977 cairo_close_path(cr);
978 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500979 }
980 }
Callum Lowcay30eeae52011-01-07 19:46:55 +0000981
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500982 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
983
984 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500985 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500986 for (row = 0; row < terminal->height; row++) {
987 p_row = terminal_get_row(terminal, row);
988 for (col = 0; col < terminal->width; col++) {
989 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500990 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500991
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500992 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000993
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500994 text_x = col * extents.max_x_advance;
995 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +1300996 if (attr.attr.a & ATTRMASK_UNDERLINE) {
997 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000998 cairo_move_to(cr, text_x, (double)text_y + 1.5);
999 cairo_line_to(cr, text_x + extents.max_x_advance, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001000 cairo_stroke(cr);
1001 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -05001002
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001003 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001004 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001005 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001006
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001007 attr.key = ~0;
1008 glyph_run_flush(&run, attr);
1009
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001010 if ((terminal->mode & MODE_SHOW_CURSOR) && !terminal->focused) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001011 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001012
Callum Lowcay30eeae52011-01-07 19:46:55 +00001013 cairo_set_line_width(cr, 1);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001014 cairo_move_to(cr, terminal->column * extents.max_x_advance + d,
1015 terminal->row * extents.height + d);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001016 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
1017 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
1018 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
1019 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001020
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001021 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001022 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001023
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001024 cairo_pop_group_to_source(cr);
1025 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001026 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001027 cairo_surface_destroy(surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001028}
1029
1030static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001031terminal_write(struct terminal *terminal, const char *data, size_t length)
1032{
1033 if (write(terminal->master, data, length) < 0)
1034 abort();
1035}
1036
1037static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001038terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001039
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001040static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001041handle_char(struct terminal *terminal, union utf8_char utf8);
1042
1043static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001044handle_sgr(struct terminal *terminal, int code);
1045
1046static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001047handle_term_parameter(struct terminal *terminal, int code, int sr)
1048{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001049 int i;
1050
Callum Lowcay67a201d2011-01-12 19:23:41 +13001051 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001052 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001053 case 1: /* DECCKM */
1054 if (sr) terminal->key_mode = KM_APPLICATION;
1055 else terminal->key_mode = KM_NORMAL;
1056 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001057 case 2: /* DECANM */
1058 /* No VT52 support yet */
1059 terminal->g0 = CS_US;
1060 terminal->g1 = CS_US;
1061 terminal->cs = terminal->g0;
1062 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001063 case 3: /* DECCOLM */
1064 if (sr)
1065 terminal_resize(terminal, 132, 24);
1066 else
1067 terminal_resize(terminal, 80, 24);
1068
1069 /* set columns, but also home cursor and clear screen */
1070 terminal->row = 0; terminal->column = 0;
1071 for (i = 0; i < terminal->height; i++) {
1072 memset(terminal_get_row(terminal, i),
1073 0, terminal->data_pitch);
1074 attr_init(terminal_get_attr_row(terminal, i),
1075 terminal->curr_attr, terminal->width);
1076 }
1077 break;
1078 case 5: /* DECSCNM */
1079 if (sr) terminal->mode |= MODE_INVERSE;
1080 else terminal->mode &= ~MODE_INVERSE;
1081 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001082 case 6: /* DECOM */
1083 terminal->origin_mode = sr;
1084 if (terminal->origin_mode)
1085 terminal->row = terminal->margin_top;
1086 else
1087 terminal->row = 0;
1088 terminal->column = 0;
1089 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001090 case 7: /* DECAWM */
1091 if (sr) terminal->mode |= MODE_AUTOWRAP;
1092 else terminal->mode &= ~MODE_AUTOWRAP;
1093 break;
1094 case 8: /* DECARM */
1095 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1096 else terminal->mode &= ~MODE_AUTOREPEAT;
1097 break;
1098 case 25:
1099 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1100 else terminal->mode &= ~MODE_SHOW_CURSOR;
1101 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001102 case 1037: /* deleteSendsDel */
1103 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1104 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1105 break;
1106 case 1039: /* altSendsEscape */
1107 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1108 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1109 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001110 default:
1111 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1112 break;
1113 }
1114 } else {
1115 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001116 case 4: /* IRM */
1117 if (sr) terminal->mode |= MODE_IRM;
1118 else terminal->mode &= ~MODE_IRM;
1119 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001120 case 20: /* LNM */
1121 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1122 else terminal->mode &= ~MODE_LF_NEWLINE;
1123 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001124 default:
1125 fprintf(stderr, "Unknown parameter: %d\n", code);
1126 break;
1127 }
1128 }
1129}
1130
1131static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001132handle_dcs(struct terminal *terminal)
1133{
1134}
1135
1136static void
1137handle_osc(struct terminal *terminal)
1138{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001139 char *p;
1140 int code;
1141
1142 terminal->escape[terminal->escape_length++] = '\0';
1143 p = &terminal->escape[2];
1144 code = strtol(p, &p, 10);
1145 if (*p == ';') p++;
1146
1147 switch (code) {
1148 case 0: /* Icon name and window title */
1149 case 1: /* Icon label */
1150 case 2: /* Window title*/
1151 window_set_title(terminal->window, p);
1152 break;
1153 default:
1154 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1155 break;
1156 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001157}
1158
1159static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001160handle_escape(struct terminal *terminal)
1161{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001162 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001163 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001164 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001165 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001166 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001167 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001168 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001169
1170 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001171 i = 0;
1172 p = &terminal->escape[2];
1173 while ((isdigit(*p) || *p == ';') && i < 10) {
1174 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001175 if (!set[i]) {
1176 args[i] = 0;
1177 set[i] = 1;
1178 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001179 p++;
1180 i++;
1181 } else {
1182 args[i] = strtol(p, &p, 10);
1183 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001184 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001185 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001186
1187 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001188 case '@': /* ICH */
1189 count = set[0] ? args[0] : 1;
1190 if (count == 0) count = 1;
1191 terminal_shift_line(terminal, count);
1192 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001193 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001194 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001195 if (count == 0) count = 1;
1196 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001197 terminal->row -= count;
1198 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001199 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001200 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001201 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001202 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001203 if (count == 0) count = 1;
1204 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001205 terminal->row += count;
1206 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001207 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001208 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001209 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001210 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001211 if (count == 0) count = 1;
1212 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001213 terminal->column += count;
1214 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001215 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001216 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001217 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001218 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001219 if (count == 0) count = 1;
1220 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001221 terminal->column -= count;
1222 else
1223 terminal->column = 0;
1224 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001225 case 'E': /* CNL */
1226 count = set[0] ? args[0] : 1;
1227 if (terminal->row + count <= terminal->margin_bottom)
1228 terminal->row += count;
1229 else
1230 terminal->row = terminal->margin_bottom;
1231 terminal->column = 0;
1232 break;
1233 case 'F': /* CPL */
1234 count = set[0] ? args[0] : 1;
1235 if (terminal->row - count >= terminal->margin_top)
1236 terminal->row -= count;
1237 else
1238 terminal->row = terminal->margin_top;
1239 terminal->column = 0;
1240 break;
1241 case 'G': /* CHA */
1242 y = set[0] ? args[0] : 1;
1243 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1244
1245 terminal->column = y - 1;
1246 break;
1247 case 'f': /* HVP */
1248 case 'H': /* CUP */
1249 x = (set[1] ? args[1] : 1) - 1;
1250 x = x < 0 ? 0 :
1251 (x >= terminal->width ? terminal->width - 1 : x);
1252
1253 y = (set[0] ? args[0] : 1) - 1;
1254 if (terminal->origin_mode) {
1255 y += terminal->margin_top;
1256 y = y < terminal->margin_top ? terminal->margin_top :
1257 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1258 } else {
1259 y = y < 0 ? 0 :
1260 (y >= terminal->height ? terminal->height - 1 : y);
1261 }
1262
1263 terminal->row = y;
1264 terminal->column = x;
1265 break;
1266 case 'I': /* CHT */
1267 count = set[0] ? args[0] : 1;
1268 if (count == 0) count = 1;
1269 while (count > 0 && terminal->column < terminal->width) {
1270 if (terminal->tab_ruler[terminal->column]) count--;
1271 terminal->column++;
1272 }
1273 terminal->column--;
1274 break;
1275 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001276 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001277 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001278 if (!set[0] || args[0] == 0 || args[0] > 2) {
1279 memset(&row[terminal->column],
1280 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1281 attr_init(&attr_row[terminal->column],
1282 terminal->curr_attr, terminal->width - terminal->column);
1283 for (i = terminal->row + 1; i < terminal->height; i++) {
1284 memset(terminal_get_row(terminal, i),
1285 0, terminal->data_pitch);
1286 attr_init(terminal_get_attr_row(terminal, i),
1287 terminal->curr_attr, terminal->width);
1288 }
1289 } else if (args[0] == 1) {
1290 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1291 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1292 for (i = 0; i < terminal->row; i++) {
1293 memset(terminal_get_row(terminal, i),
1294 0, terminal->data_pitch);
1295 attr_init(terminal_get_attr_row(terminal, i),
1296 terminal->curr_attr, terminal->width);
1297 }
1298 } else if (args[0] == 2) {
1299 for (i = 0; i < terminal->height; i++) {
1300 memset(terminal_get_row(terminal, i),
1301 0, terminal->data_pitch);
1302 attr_init(terminal_get_attr_row(terminal, i),
1303 terminal->curr_attr, terminal->width);
1304 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001305 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001306 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001307 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001308 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001309 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001310 if (!set[0] || args[0] == 0 || args[0] > 2) {
1311 memset(&row[terminal->column], 0,
1312 (terminal->width - terminal->column) * sizeof(union utf8_char));
1313 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1314 terminal->width - terminal->column);
1315 } else if (args[0] == 1) {
1316 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1317 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1318 } else if (args[0] == 2) {
1319 memset(row, 0, terminal->data_pitch);
1320 attr_init(attr_row, terminal->curr_attr, terminal->width);
1321 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001322 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001323 case 'L': /* IL */
1324 count = set[0] ? args[0] : 1;
1325 if (count == 0) count = 1;
1326 if (terminal->row >= terminal->margin_top &&
1327 terminal->row < terminal->margin_bottom)
1328 {
1329 top = terminal->margin_top;
1330 terminal->margin_top = terminal->row;
1331 terminal_scroll(terminal, 0 - count);
1332 terminal->margin_top = top;
1333 } else if (terminal->row == terminal->margin_bottom) {
1334 memset(terminal_get_row(terminal, terminal->row),
1335 0, terminal->data_pitch);
1336 attr_init(terminal_get_attr_row(terminal, terminal->row),
1337 terminal->curr_attr, terminal->width);
1338 }
1339 break;
1340 case 'M': /* DL */
1341 count = set[0] ? args[0] : 1;
1342 if (count == 0) count = 1;
1343 if (terminal->row >= terminal->margin_top &&
1344 terminal->row < terminal->margin_bottom)
1345 {
1346 top = terminal->margin_top;
1347 terminal->margin_top = terminal->row;
1348 terminal_scroll(terminal, count);
1349 terminal->margin_top = top;
1350 } else if (terminal->row == terminal->margin_bottom) {
1351 memset(terminal_get_row(terminal, terminal->row),
1352 0, terminal->data_pitch);
1353 }
1354 break;
1355 case 'P': /* DCH */
1356 count = set[0] ? args[0] : 1;
1357 if (count == 0) count = 1;
1358 terminal_shift_line(terminal, 0 - count);
1359 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001360 case 'S': /* SU */
1361 terminal_scroll(terminal, set[0] ? args[0] : 1);
1362 break;
1363 case 'T': /* SD */
1364 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1365 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001366 case 'X': /* ECH */
1367 count = set[0] ? args[0] : 1;
1368 if (count == 0) count = 1;
1369 if ((terminal->column + count) > terminal->width)
1370 count = terminal->width - terminal->column;
1371 row = terminal_get_row(terminal, terminal->row);
1372 attr_row = terminal_get_attr_row(terminal, terminal->row);
1373 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1374 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1375 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001376 case 'Z': /* CBT */
1377 count = set[0] ? args[0] : 1;
1378 if (count == 0) count = 1;
1379 while (count > 0 && terminal->column >= 0) {
1380 if (terminal->tab_ruler[terminal->column]) count--;
1381 terminal->column--;
1382 }
1383 terminal->column++;
1384 break;
1385 case '`': /* HPA */
1386 y = set[0] ? args[0] : 1;
1387 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1388
1389 terminal->column = y - 1;
1390 break;
1391 case 'b': /* REP */
1392 count = set[0] ? args[0] : 1;
1393 if (count == 0) count = 1;
1394 if (terminal->last_char.byte[0])
1395 for (i = 0; i < count; i++)
1396 handle_char(terminal, terminal->last_char);
1397 terminal->last_char.byte[0] = 0;
1398 break;
1399 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001400 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001401 break;
1402 case 'd': /* VPA */
1403 x = set[0] ? args[0] : 1;
1404 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1405
1406 terminal->row = x - 1;
1407 break;
1408 case 'g': /* TBC */
1409 if (!set[0] || args[0] == 0) {
1410 terminal->tab_ruler[terminal->column] = 0;
1411 } else if (args[0] == 3) {
1412 memset(terminal->tab_ruler, 0, terminal->width);
1413 }
1414 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001415 case 'h': /* SM */
1416 for(i = 0; i < 10 && set[i]; i++) {
1417 handle_term_parameter(terminal, args[i], 1);
1418 }
1419 break;
1420 case 'l': /* RM */
1421 for(i = 0; i < 10 && set[i]; i++) {
1422 handle_term_parameter(terminal, args[i], 0);
1423 }
1424 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001425 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001426 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001427 if (i <= 7 && set[i] && set[i + 1] &&
1428 set[i + 2] && args[i + 1] == 5)
1429 {
1430 if (args[i] == 38) {
1431 handle_sgr(terminal, args[i + 2] + 256);
1432 break;
1433 } else if (args[i] == 48) {
1434 handle_sgr(terminal, args[i + 2] + 512);
1435 break;
1436 }
1437 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001438 if(set[i]) {
1439 handle_sgr(terminal, args[i]);
1440 } else if(i == 0) {
1441 handle_sgr(terminal, 0);
1442 break;
1443 } else {
1444 break;
1445 }
1446 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001447 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001448 case 'n': /* DSR */
1449 i = set[0] ? args[0] : 0;
1450 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001451 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001452 } else if (i == 6) {
1453 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1454 terminal->origin_mode ?
1455 terminal->row+terminal->margin_top : terminal->row+1,
1456 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001457 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001458 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001459 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001460 case 'r':
1461 if(!set[0]) {
1462 terminal->margin_top = 0;
1463 terminal->margin_bottom = terminal->height-1;
1464 terminal->row = 0;
1465 terminal->column = 0;
1466 } else {
1467 top = (set[0] ? args[0] : 1) - 1;
1468 top = top < 0 ? 0 :
1469 (top >= terminal->height ? terminal->height - 1 : top);
1470 bottom = (set[1] ? args[1] : 1) - 1;
1471 bottom = bottom < 0 ? 0 :
1472 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1473 if(bottom > top) {
1474 terminal->margin_top = top;
1475 terminal->margin_bottom = bottom;
1476 } else {
1477 terminal->margin_top = 0;
1478 terminal->margin_bottom = terminal->height-1;
1479 }
1480 if(terminal->origin_mode)
1481 terminal->row = terminal->margin_top;
1482 else
1483 terminal->row = 0;
1484 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001485 }
1486 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001487 case 's':
1488 terminal->saved_row = terminal->row;
1489 terminal->saved_column = terminal->column;
1490 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001491 case 't': /* windowOps */
1492 if (!set[0]) break;
1493 switch (args[0]) {
1494 case 4: /* resize px */
1495 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001496 widget_schedule_resize(terminal->widget,
1497 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001498 }
1499 break;
1500 case 8: /* resize ch */
1501 if (set[1] && set[2]) {
1502 terminal_resize(terminal, args[2], args[1]);
1503 }
1504 break;
1505 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001506 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001507 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1508 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001509 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001510 break;
1511 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001512 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001513 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1514 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001515 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001516 break;
1517 case 18: /* report ch */
1518 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1519 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001520 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001521 break;
1522 case 21: /* report title */
1523 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1524 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001525 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001526 break;
1527 default:
1528 if (args[0] >= 24)
1529 terminal_resize(terminal, terminal->width, args[0]);
1530 else
1531 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1532 break;
1533 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001534 case 'u':
1535 terminal->row = terminal->saved_row;
1536 terminal->column = terminal->saved_column;
1537 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001538 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001539 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001540 break;
1541 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001542}
1543
1544static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001545handle_non_csi_escape(struct terminal *terminal, char code)
1546{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001547 switch(code) {
1548 case 'M': /* RI */
1549 terminal->row -= 1;
1550 if(terminal->row < terminal->margin_top) {
1551 terminal->row = terminal->margin_top;
1552 terminal_scroll(terminal, -1);
1553 }
1554 break;
1555 case 'E': /* NEL */
1556 terminal->column = 0;
1557 // fallthrough
1558 case 'D': /* IND */
1559 terminal->row += 1;
1560 if(terminal->row > terminal->margin_bottom) {
1561 terminal->row = terminal->margin_bottom;
1562 terminal_scroll(terminal, +1);
1563 }
1564 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001565 case 'c': /* RIS */
1566 terminal_init(terminal);
1567 break;
1568 case 'H': /* HTS */
1569 terminal->tab_ruler[terminal->column] = 1;
1570 break;
1571 case '7': /* DECSC */
1572 terminal->saved_row = terminal->row;
1573 terminal->saved_column = terminal->column;
1574 terminal->saved_attr = terminal->curr_attr;
1575 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001576 terminal->saved_cs = terminal->cs;
1577 terminal->saved_g0 = terminal->g0;
1578 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001579 break;
1580 case '8': /* DECRC */
1581 terminal->row = terminal->saved_row;
1582 terminal->column = terminal->saved_column;
1583 terminal->curr_attr = terminal->saved_attr;
1584 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001585 terminal->cs = terminal->saved_cs;
1586 terminal->g0 = terminal->saved_g0;
1587 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001588 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001589 case '=': /* DECPAM */
1590 terminal->key_mode = KM_APPLICATION;
1591 break;
1592 case '>': /* DECPNM */
1593 terminal->key_mode = KM_NORMAL;
1594 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001595 default:
1596 fprintf(stderr, "Unknown escape code: %c\n", code);
1597 break;
1598 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001599}
1600
1601static void
1602handle_special_escape(struct terminal *terminal, char special, char code)
1603{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001604 int i, numChars;
1605
1606 if (special == '#') {
1607 switch(code) {
1608 case '8':
1609 /* fill with 'E', no cheap way to do this */
1610 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1611 numChars = terminal->width * terminal->height;
1612 for(i = 0; i < numChars; i++) {
1613 terminal->data[i].byte[0] = 'E';
1614 }
1615 break;
1616 default:
1617 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1618 break;
1619 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001620 } else if (special == '(' || special == ')') {
1621 switch(code) {
1622 case '0':
1623 if (special == '(')
1624 terminal->g0 = CS_SPECIAL;
1625 else
1626 terminal->g1 = CS_SPECIAL;
1627 break;
1628 case 'A':
1629 if (special == '(')
1630 terminal->g0 = CS_UK;
1631 else
1632 terminal->g1 = CS_UK;
1633 break;
1634 case 'B':
1635 if (special == '(')
1636 terminal->g0 = CS_US;
1637 else
1638 terminal->g1 = CS_US;
1639 break;
1640 default:
1641 fprintf(stderr, "Unknown character set %c\n", code);
1642 break;
1643 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001644 } else {
1645 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1646 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001647}
1648
1649static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001650handle_sgr(struct terminal *terminal, int code)
1651{
1652 switch(code) {
1653 case 0:
1654 terminal->curr_attr = terminal->color_scheme->default_attr;
1655 break;
1656 case 1:
1657 terminal->curr_attr.a |= ATTRMASK_BOLD;
1658 if (terminal->curr_attr.fg < 8)
1659 terminal->curr_attr.fg += 8;
1660 break;
1661 case 4:
1662 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1663 break;
1664 case 5:
1665 terminal->curr_attr.a |= ATTRMASK_BLINK;
1666 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001667 case 8:
1668 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1669 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001670 case 2:
1671 case 21:
1672 case 22:
1673 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1674 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1675 terminal->curr_attr.fg -= 8;
1676 break;
1677 case 24:
1678 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1679 break;
1680 case 25:
1681 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1682 break;
1683 case 7:
1684 case 26:
1685 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1686 break;
1687 case 27:
1688 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1689 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001690 case 28:
1691 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1692 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001693 case 39:
1694 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1695 break;
1696 case 49:
1697 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1698 break;
1699 default:
1700 if(code >= 30 && code <= 37) {
1701 terminal->curr_attr.fg = code - 30;
1702 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1703 terminal->curr_attr.fg += 8;
1704 } else if(code >= 40 && code <= 47) {
1705 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001706 } else if (code >= 90 && code <= 97) {
1707 terminal->curr_attr.fg = code - 90 + 8;
1708 } else if (code >= 100 && code <= 107) {
1709 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001710 } else if(code >= 256 && code < 512) {
1711 terminal->curr_attr.fg = code - 256;
1712 } else if(code >= 512 && code < 768) {
1713 terminal->curr_attr.bg = code - 512;
1714 } else {
1715 fprintf(stderr, "Unknown SGR code: %d\n", code);
1716 }
1717 break;
1718 }
1719}
1720
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001721/* Returns 1 if c was special, otherwise 0 */
1722static int
1723handle_special_char(struct terminal *terminal, char c)
1724{
1725 union utf8_char *row;
1726 struct attr *attr_row;
1727
1728 row = terminal_get_row(terminal, terminal->row);
1729 attr_row = terminal_get_attr_row(terminal, terminal->row);
1730
1731 switch(c) {
1732 case '\r':
1733 terminal->column = 0;
1734 break;
1735 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001736 if (terminal->mode & MODE_LF_NEWLINE) {
1737 terminal->column = 0;
1738 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001739 /* fallthrough */
1740 case '\v':
1741 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001742 terminal->row++;
1743 if(terminal->row > terminal->margin_bottom) {
1744 terminal->row = terminal->margin_bottom;
1745 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001746 }
1747
1748 break;
1749 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001750 while (terminal->column < terminal->width) {
1751 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001752 if (terminal->mode & MODE_IRM)
1753 terminal_shift_line(terminal, +1);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001754 row[terminal->column].byte[0] = ' ';
1755 row[terminal->column].byte[1] = '\0';
1756 attr_row[terminal->column] = terminal->curr_attr;
1757 terminal->column++;
1758 }
1759 if (terminal->column >= terminal->width) {
1760 terminal->column = terminal->width - 1;
1761 }
1762
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001763 break;
1764 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001765 if (terminal->column >= terminal->width) {
1766 terminal->column = terminal->width - 2;
1767 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001768 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001769 } else if (terminal->mode & MODE_AUTOWRAP) {
1770 terminal->column = terminal->width - 1;
1771 terminal->row -= 1;
1772 if (terminal->row < terminal->margin_top) {
1773 terminal->row = terminal->margin_top;
1774 terminal_scroll(terminal, -1);
1775 }
1776 }
1777
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001778 break;
1779 case '\a':
1780 /* Bell */
1781 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001782 case '\x0E': /* SO */
1783 terminal->cs = terminal->g1;
1784 break;
1785 case '\x0F': /* SI */
1786 terminal->cs = terminal->g0;
1787 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001788 default:
1789 return 0;
1790 }
1791
1792 return 1;
1793}
1794
1795static void
1796handle_char(struct terminal *terminal, union utf8_char utf8)
1797{
1798 union utf8_char *row;
1799 struct attr *attr_row;
1800
1801 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001802
1803 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001804
1805 /* There are a whole lot of non-characters, control codes,
1806 * and formatting codes that should probably be ignored,
1807 * for example: */
1808 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1809 /* BOM, ignore */
1810 return;
1811 }
1812
1813 /* Some of these non-characters should be translated, e.g.: */
1814 if (utf8.byte[0] < 32) {
1815 utf8.byte[0] = utf8.byte[0] + 64;
1816 }
1817
1818 /* handle right margin effects */
1819 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001820 if (terminal->mode & MODE_AUTOWRAP) {
1821 terminal->column = 0;
1822 terminal->row += 1;
1823 if (terminal->row > terminal->margin_bottom) {
1824 terminal->row = terminal->margin_bottom;
1825 terminal_scroll(terminal, +1);
1826 }
1827 } else {
1828 terminal->column--;
1829 }
1830 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001831
1832 row = terminal_get_row(terminal, terminal->row);
1833 attr_row = terminal_get_attr_row(terminal, terminal->row);
1834
Callum Lowcay69e96582011-01-07 19:47:00 +00001835 if (terminal->mode & MODE_IRM)
1836 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001837 row[terminal->column] = utf8;
1838 attr_row[terminal->column++] = terminal->curr_attr;
1839
1840 if (utf8.ch != terminal->last_char.ch)
1841 terminal->last_char = utf8;
1842}
1843
Callum Lowcay30eeae52011-01-07 19:46:55 +00001844static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001845escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1846{
1847 int len, i;
1848
1849 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1850 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1851 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1852 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1853 else len = 1; /* Invalid, cannot happen */
1854
1855 if (terminal->escape_length + len <= MAX_ESCAPE) {
1856 for (i = 0; i < len; i++)
1857 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1858 terminal->escape_length += len;
1859 } else if (terminal->escape_length < MAX_ESCAPE) {
1860 terminal->escape[terminal->escape_length++] = 0;
1861 }
1862}
1863
1864static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001865terminal_data(struct terminal *terminal, const char *data, size_t length)
1866{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001867 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001868 union utf8_char utf8;
1869 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001870
1871 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001872 parser_state =
1873 utf8_next_char(&terminal->state_machine, data[i]);
1874 switch(parser_state) {
1875 case utf8state_accept:
1876 utf8.ch = terminal->state_machine.s.ch;
1877 break;
1878 case utf8state_reject:
1879 /* the unicode replacement character */
1880 utf8.byte[0] = 0xEF;
1881 utf8.byte[1] = 0xBF;
1882 utf8.byte[2] = 0xBD;
1883 utf8.byte[3] = 0x00;
1884 break;
1885 default:
1886 continue;
1887 }
1888
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001889 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001890 switch (terminal->state) {
1891 case escape_state_escape:
1892 escape_append_utf8(terminal, utf8);
1893 switch (utf8.byte[0]) {
1894 case 'P': /* DCS */
1895 terminal->state = escape_state_dcs;
1896 break;
1897 case '[': /* CSI */
1898 terminal->state = escape_state_csi;
1899 break;
1900 case ']': /* OSC */
1901 terminal->state = escape_state_osc;
1902 break;
1903 case '#':
1904 case '(':
1905 case ')': /* special */
1906 terminal->state = escape_state_special;
1907 break;
1908 case '^': /* PM (not implemented) */
1909 case '_': /* APC (not implemented) */
1910 terminal->state = escape_state_ignore;
1911 break;
1912 default:
1913 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001914 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13001915 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001916 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001917 continue;
1918 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001919 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1920 /* do nothing */
1921 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001922 terminal->escape_flags |= ESC_FLAG_WHAT;
1923 } else if (utf8.byte[0] == '>') {
1924 terminal->escape_flags |= ESC_FLAG_GT;
1925 } else if (utf8.byte[0] == '!') {
1926 terminal->escape_flags |= ESC_FLAG_BANG;
1927 } else if (utf8.byte[0] == '$') {
1928 terminal->escape_flags |= ESC_FLAG_CASH;
1929 } else if (utf8.byte[0] == '\'') {
1930 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1931 } else if (utf8.byte[0] == '"') {
1932 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1933 } else if (utf8.byte[0] == ' ') {
1934 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001935 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001936 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001937 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13001938 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001939 }
1940
1941 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1942 utf8.byte[0] == '`')
1943 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001944 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001945 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001946 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001947 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001948 continue;
1949 case escape_state_inner_escape:
1950 if (utf8.byte[0] == '\\') {
1951 terminal->state = escape_state_normal;
1952 if (terminal->outer_state == escape_state_dcs) {
1953 handle_dcs(terminal);
1954 } else if (terminal->outer_state == escape_state_osc) {
1955 handle_osc(terminal);
1956 }
1957 } else if (utf8.byte[0] == '\e') {
1958 terminal->state = terminal->outer_state;
1959 escape_append_utf8(terminal, utf8);
1960 if (terminal->escape_length >= MAX_ESCAPE)
1961 terminal->state = escape_state_normal;
1962 } else {
1963 terminal->state = terminal->outer_state;
1964 if (terminal->escape_length < MAX_ESCAPE)
1965 terminal->escape[terminal->escape_length++] = '\e';
1966 escape_append_utf8(terminal, utf8);
1967 if (terminal->escape_length >= MAX_ESCAPE)
1968 terminal->state = escape_state_normal;
1969 }
1970 continue;
1971 case escape_state_dcs:
1972 case escape_state_osc:
1973 case escape_state_ignore:
1974 if (utf8.byte[0] == '\e') {
1975 terminal->outer_state = terminal->state;
1976 terminal->state = escape_state_inner_escape;
1977 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
1978 terminal->state = escape_state_normal;
1979 handle_osc(terminal);
1980 } else {
1981 escape_append_utf8(terminal, utf8);
1982 if (terminal->escape_length >= MAX_ESCAPE)
1983 terminal->state = escape_state_normal;
1984 }
1985 continue;
1986 case escape_state_special:
1987 escape_append_utf8(terminal, utf8);
1988 terminal->state = escape_state_normal;
1989 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
1990 handle_special_escape(terminal, terminal->escape[1],
1991 utf8.byte[0]);
1992 }
1993 continue;
1994 default:
1995 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001996 }
1997
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001998 /* this is valid, because ASCII characters are never used to
1999 * introduce a multibyte sequence in UTF-8 */
2000 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002001 terminal->state = escape_state_escape;
2002 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002003 terminal->escape[0] = '\e';
2004 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002005 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002006 } else {
2007 handle_char(terminal, utf8);
2008 } /* if */
2009 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002010
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002011 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002012}
2013
2014static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002015data_source_target(void *data,
2016 struct wl_data_source *source, const char *mime_type)
2017{
2018 fprintf(stderr, "data_source_target, %s\n", mime_type);
2019}
2020
2021static void
2022data_source_send(void *data,
2023 struct wl_data_source *source,
2024 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002025{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002026 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002027
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002028 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002029}
2030
2031static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002032data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002033{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002034 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002035}
2036
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002037static const struct wl_data_source_listener data_source_listener = {
2038 data_source_target,
2039 data_source_send,
2040 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002041};
2042
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002043static int
2044handle_bound_key(struct terminal *terminal,
2045 struct input *input, uint32_t sym, uint32_t time)
2046{
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002047 switch (sym) {
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002048 case XK_X:
2049 /* Cut selection; terminal doesn't do cut, fall
2050 * through to copy. */
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002051 case XK_C:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002052 terminal->selection =
2053 display_create_data_source(terminal->display);
2054 wl_data_source_offer(terminal->selection,
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002055 "text/plain;charset=utf-8");
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002056 wl_data_source_add_listener(terminal->selection,
2057 &data_source_listener, terminal);
2058 input_set_selection(input, terminal->selection, time);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002059 return 1;
2060 case XK_V:
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002061 input_receive_selection_data_to_fd(input,
2062 "text/plain;charset=utf-8",
2063 terminal->master);
2064
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002065 return 1;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002066 default:
2067 return 0;
2068 }
2069}
2070
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002071static void
2072key_handler(struct window *window, struct input *input, uint32_t time,
2073 uint32_t key, uint32_t sym, uint32_t state, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002074{
2075 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002076 char ch[MAX_RESPONSE];
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002077 uint32_t modifiers;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002078 int len = 0;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002079
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002080 modifiers = input_get_modifiers(input);
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002081 if ((modifiers & XKB_COMMON_CONTROL_MASK) &&
2082 (modifiers & XKB_COMMON_SHIFT_MASK) &&
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002083 state && handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002084 return;
2085
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002086 switch (sym) {
2087 case XK_F11:
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002088 if (!state)
2089 break;
2090 terminal->fullscreen ^= 1;
2091 window_set_fullscreen(window, terminal->fullscreen);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002092 break;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002093
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002094 case XK_BackSpace:
2095 case XK_Tab:
2096 case XK_Linefeed:
2097 case XK_Clear:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002098 case XK_Pause:
2099 case XK_Scroll_Lock:
2100 case XK_Sys_Req:
2101 case XK_Escape:
2102 ch[len++] = sym & 0x7f;
2103 break;
2104
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002105 case XK_Return:
2106 if (terminal->mode & MODE_LF_NEWLINE) {
2107 ch[len++] = 0x0D;
2108 ch[len++] = 0x0A;
2109 } else {
2110 ch[len++] = 0x0D;
2111 }
2112 break;
2113
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002114 case XK_Shift_L:
2115 case XK_Shift_R:
2116 case XK_Control_L:
2117 case XK_Control_R:
2118 case XK_Alt_L:
2119 case XK_Alt_R:
2120 break;
2121
Callum Lowcay7e08e902011-01-07 19:47:02 +00002122 case XK_Insert:
2123 len = function_key_response('[', 2, modifiers, '~', ch);
2124 break;
2125 case XK_Delete:
2126 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2127 ch[len++] = '\x04';
2128 } else {
2129 len = function_key_response('[', 3, modifiers, '~', ch);
2130 }
2131 break;
2132 case XK_Page_Up:
2133 len = function_key_response('[', 5, modifiers, '~', ch);
2134 break;
2135 case XK_Page_Down:
2136 len = function_key_response('[', 6, modifiers, '~', ch);
2137 break;
2138 case XK_F1:
2139 len = function_key_response('O', 1, modifiers, 'P', ch);
2140 break;
2141 case XK_F2:
2142 len = function_key_response('O', 1, modifiers, 'Q', ch);
2143 break;
2144 case XK_F3:
2145 len = function_key_response('O', 1, modifiers, 'R', ch);
2146 break;
2147 case XK_F4:
2148 len = function_key_response('O', 1, modifiers, 'S', ch);
2149 break;
2150 case XK_F5:
2151 len = function_key_response('[', 15, modifiers, '~', ch);
2152 break;
2153 case XK_F6:
2154 len = function_key_response('[', 17, modifiers, '~', ch);
2155 break;
2156 case XK_F7:
2157 len = function_key_response('[', 18, modifiers, '~', ch);
2158 break;
2159 case XK_F8:
2160 len = function_key_response('[', 19, modifiers, '~', ch);
2161 break;
2162 case XK_F9:
2163 len = function_key_response('[', 20, modifiers, '~', ch);
2164 break;
2165 case XK_F10:
2166 len = function_key_response('[', 21, modifiers, '~', ch);
2167 break;
2168 case XK_F12:
2169 len = function_key_response('[', 24, modifiers, '~', ch);
2170 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002171 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002172 /* Handle special keys with alternate mappings */
2173 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2174 if (len != 0) break;
2175
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002176 if (modifiers & XKB_COMMON_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002177 if (sym >= '3' && sym <= '7')
2178 sym = (sym & 0x1f) + 8;
2179
2180 if (!((sym >= '!' && sym <= '/') ||
2181 (sym >= '8' && sym <= '?') ||
2182 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2183 else if (sym == '2') sym = 0x00;
2184 else if (sym == '/') sym = 0x1F;
2185 else if (sym == '8' || sym == '?') sym = 0x7F;
2186 } else if ((terminal->mode & MODE_ALT_SENDS_ESC) &&
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002187 (modifiers & XKB_COMMON_MOD1_MASK))
Callum Lowcay7e08e902011-01-07 19:47:02 +00002188 {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002189 ch[len++] = 0x1b;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002190 } else if (modifiers & XKB_COMMON_MOD1_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002191 sym = sym | 0x80;
2192 }
2193
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002194 if (sym < 256)
2195 ch[len++] = sym;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002196 break;
2197 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002198
2199 if (state && len > 0)
Kristian Høgsberg26130862011-08-24 11:30:21 -04002200 terminal_write(terminal, ch, len);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002201}
2202
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002203static void
2204keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002205 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002206{
2207 struct terminal *terminal = data;
2208
2209 terminal->focused = (device != NULL);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002210 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002211}
2212
Kristian Høgsberg59826582011-01-20 11:56:57 -05002213static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002214button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002215 struct input *input, uint32_t time,
2216 int button, int state, void *data)
2217{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002218 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002219
2220 switch (button) {
2221 case 272:
2222 if (state) {
2223 terminal->dragging = 1;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002224 input_get_position(input,
2225 &terminal->selection_start_x,
2226 &terminal->selection_start_y);
2227 terminal->selection_end_x = terminal->selection_start_x;
2228 terminal->selection_end_y = terminal->selection_start_y;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002229 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002230 } else {
2231 terminal->dragging = 0;
2232 }
2233 break;
2234 }
2235}
2236
2237static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002238motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002239 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002240 int32_t x, int32_t y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002241{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002242 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002243
2244 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002245 input_get_position(input,
2246 &terminal->selection_end_x,
2247 &terminal->selection_end_y);
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002248 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002249 }
2250
2251 return POINTER_IBEAM;
2252}
2253
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002254static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002255terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002256{
2257 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002258 cairo_surface_t *surface;
2259 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002260
2261 terminal = malloc(sizeof *terminal);
2262 if (terminal == NULL)
2263 return terminal;
2264
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002265 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002266 terminal->fullscreen = fullscreen;
Callum Lowcay30eeae52011-01-07 19:46:55 +00002267 terminal->color_scheme = &DEFAULT_COLORS;
2268 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002269 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002270 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002271 terminal->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002272 terminal->widget = frame_create(terminal->window, terminal);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002273 window_set_title(terminal->window, "Wayland Terminal");
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002274 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002275
2276 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002277 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002278
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002279 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002280 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002281
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002282 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002283 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002284 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002285 keyboard_focus_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002286 widget_set_redraw_handler(terminal->widget, redraw_handler);
2287 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002288 widget_set_button_handler(terminal->widget, button_handler);
2289 widget_set_motion_handler(terminal->widget, motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002290
Kristian Høgsberg09531622010-06-14 23:22:15 -04002291 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2292 cr = cairo_create(surface);
Kristian Høgsberg09531622010-06-14 23:22:15 -04002293 cairo_set_font_size(cr, 14);
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002294 cairo_select_font_face (cr, "mono",
2295 CAIRO_FONT_SLANT_NORMAL,
2296 CAIRO_FONT_WEIGHT_BOLD);
2297 terminal->font_bold = cairo_get_scaled_font (cr);
2298 cairo_scaled_font_reference(terminal->font_bold);
2299
2300 cairo_select_font_face (cr, "mono",
2301 CAIRO_FONT_SLANT_NORMAL,
2302 CAIRO_FONT_WEIGHT_NORMAL);
2303 terminal->font_normal = cairo_get_scaled_font (cr);
2304 cairo_scaled_font_reference(terminal->font_normal);
2305
Kristian Høgsberg09531622010-06-14 23:22:15 -04002306 cairo_font_extents(cr, &terminal->extents);
2307 cairo_destroy(cr);
2308 cairo_surface_destroy(surface);
2309
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002310 window_schedule_resize(terminal->window, 500, 400);
2311
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002312 return terminal;
2313}
2314
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002315static void
2316io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002317{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002318 struct terminal *terminal =
2319 container_of(task, struct terminal, io_task);
2320 char buffer[256];
2321 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002322
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002323 if (events & EPOLLHUP)
2324 exit(0);
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002325
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002326 len = read(terminal->master, buffer, sizeof buffer);
2327 if (len < 0)
2328 exit(0);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002329
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002330 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002331}
2332
2333static int
2334terminal_run(struct terminal *terminal, const char *path)
2335{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002336 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002337 pid_t pid;
2338
2339 pid = forkpty(&master, NULL, NULL, NULL);
2340 if (pid == 0) {
Callum Lowcay86653ed2011-01-07 19:47:03 +00002341 setenv("TERM", "xterm-256color", 1);
2342 setenv("COLORTERM", "xterm-256color", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002343 if (execl(path, path, NULL)) {
2344 printf("exec failed: %m\n");
2345 exit(EXIT_FAILURE);
2346 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002347 } else if (pid < 0) {
2348 fprintf(stderr, "failed to fork and create pty (%m).\n");
2349 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002350 }
2351
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002352 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002353 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002354 terminal->io_task.run = io_handler;
2355 display_watch_fd(terminal->display, terminal->master,
2356 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002357
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002358 window_set_fullscreen(terminal->window, terminal->fullscreen);
2359 if (!terminal->fullscreen)
2360 terminal_resize(terminal, 80, 24);
2361
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002362 return 0;
2363}
2364
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002365static const struct weston_option terminal_options[] = {
2366 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002367};
2368
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002369int main(int argc, char *argv[])
2370{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002371 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002372 struct terminal *terminal;
Peter Hutterer035ac942012-02-03 20:58:19 +10002373 const char *shell;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002374
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002375 argc = parse_options(terminal_options,
2376 ARRAY_LENGTH(terminal_options), argc, argv);
2377
2378 d = display_create(argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002379 if (d == NULL) {
2380 fprintf(stderr, "failed to create display: %m\n");
2381 return -1;
2382 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002383
Peter Hutterer035ac942012-02-03 20:58:19 +10002384 shell = getenv("SHELL");
2385 if (!shell)
2386 shell = "/bin/bash";
2387
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002388 terminal = terminal_create(d, option_fullscreen);
Peter Hutterer035ac942012-02-03 20:58:19 +10002389 if (terminal_run(terminal, shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002390 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002391
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002392 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002393
2394 return 0;
2395}