blob: 865107369b9436829e50a7063cd76a6440fd0646 [file] [log] [blame]
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <fcntl.h>
28#include <unistd.h>
29#include <math.h>
30#include <time.h>
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050031#include <pty.h>
Kristian Høgsbergf04e8382008-12-08 00:07:49 -050032#include <ctype.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050033#include <cairo.h>
34#include <glib.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040035#include <sys/epoll.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050036
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040037#include <X11/keysym.h>
38
Pekka Paalanen50719bc2011-11-22 14:18:50 +020039#include <wayland-client.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050040
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050041#include "window.h"
42
Kristian Høgsberg0395f302008-12-22 12:14:50 -050043static int option_fullscreen;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050044
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050045#define MOD_SHIFT 0x01
46#define MOD_ALT 0x02
47#define MOD_CTRL 0x04
48
Callum Lowcay30eeae52011-01-07 19:46:55 +000049#define ATTRMASK_BOLD 0x01
50#define ATTRMASK_UNDERLINE 0x02
51#define ATTRMASK_BLINK 0x04
52#define ATTRMASK_INVERSE 0x08
Callum Lowcay81179db2011-01-10 12:14:01 +130053#define ATTRMASK_CONCEALED 0x10
Callum Lowcay30eeae52011-01-07 19:46:55 +000054
Callum Lowcayb8609ad2011-01-07 19:46:57 +000055/* Buffer sizes */
Callum Lowcayef57a9b2011-01-14 20:46:23 +130056#define MAX_RESPONSE 256
57#define MAX_ESCAPE 255
Callum Lowcayb8609ad2011-01-07 19:46:57 +000058
Callum Lowcay8e57dd52011-01-07 19:46:59 +000059/* Terminal modes */
60#define MODE_SHOW_CURSOR 0x00000001
61#define MODE_INVERSE 0x00000002
62#define MODE_AUTOWRAP 0x00000004
63#define MODE_AUTOREPEAT 0x00000008
64#define MODE_LF_NEWLINE 0x00000010
Callum Lowcay69e96582011-01-07 19:47:00 +000065#define MODE_IRM 0x00000020
Callum Lowcay7e08e902011-01-07 19:47:02 +000066#define MODE_DELETE_SENDS_DEL 0x00000040
67#define MODE_ALT_SENDS_ESC 0x00000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +000068
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000069union utf8_char {
70 unsigned char byte[4];
71 uint32_t ch;
72};
73
74enum utf8_state {
75 utf8state_start,
76 utf8state_accept,
77 utf8state_reject,
78 utf8state_expect3,
79 utf8state_expect2,
80 utf8state_expect1
81};
82
83struct utf8_state_machine {
84 enum utf8_state state;
85 int len;
86 union utf8_char s;
87};
88
89static void
90init_state_machine(struct utf8_state_machine *machine)
91{
92 machine->state = utf8state_start;
93 machine->len = 0;
94 machine->s.ch = 0;
95}
96
97static enum utf8_state
98utf8_next_char(struct utf8_state_machine *machine, char c)
99{
100 switch(machine->state) {
101 case utf8state_start:
102 case utf8state_accept:
103 case utf8state_reject:
104 machine->s.ch = 0;
105 machine->len = 0;
106 if(c == 0xC0 || c == 0xC1) {
107 /* overlong encoding, reject */
108 machine->state = utf8state_reject;
109 } else if((c & 0x80) == 0) {
110 /* single byte, accept */
111 machine->s.byte[machine->len++] = c;
112 machine->state = utf8state_accept;
113 } else if((c & 0xC0) == 0x80) {
114 /* parser out of sync, ignore byte */
115 machine->state = utf8state_start;
116 } else if((c & 0xE0) == 0xC0) {
117 /* start of two byte sequence */
118 machine->s.byte[machine->len++] = c;
119 machine->state = utf8state_expect1;
120 } else if((c & 0xF0) == 0xE0) {
121 /* start of three byte sequence */
122 machine->s.byte[machine->len++] = c;
123 machine->state = utf8state_expect2;
124 } else if((c & 0xF8) == 0xF0) {
125 /* start of four byte sequence */
126 machine->s.byte[machine->len++] = c;
127 machine->state = utf8state_expect3;
128 } else {
129 /* overlong encoding, reject */
130 machine->state = utf8state_reject;
131 }
132 break;
133 case utf8state_expect3:
134 machine->s.byte[machine->len++] = c;
135 if((c & 0xC0) == 0x80) {
136 /* all good, continue */
137 machine->state = utf8state_expect2;
138 } else {
139 /* missing extra byte, reject */
140 machine->state = utf8state_reject;
141 }
142 break;
143 case utf8state_expect2:
144 machine->s.byte[machine->len++] = c;
145 if((c & 0xC0) == 0x80) {
146 /* all good, continue */
147 machine->state = utf8state_expect1;
148 } else {
149 /* missing extra byte, reject */
150 machine->state = utf8state_reject;
151 }
152 break;
153 case utf8state_expect1:
154 machine->s.byte[machine->len++] = c;
155 if((c & 0xC0) == 0x80) {
156 /* all good, accept */
157 machine->state = utf8state_accept;
158 } else {
159 /* missing extra byte, reject */
160 machine->state = utf8state_reject;
161 }
162 break;
163 default:
164 machine->state = utf8state_reject;
165 break;
166 }
167
168 return machine->state;
169}
170
Callum Lowcay256e72f2011-01-07 19:47:01 +0000171struct char_sub {
172 union utf8_char match;
173 union utf8_char replace;
174};
175/* Set last char_sub match to NULL char */
176typedef struct char_sub *character_set;
177
178struct char_sub CS_US[] = {
179 {{{0, }}, {{0, }}}
180};
181static struct char_sub CS_UK[] = {
182 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}},
183 {{{0, }}, {{0, }}}
184};
185static struct char_sub CS_SPECIAL[] = {
186 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond */
187 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell */
188 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT */
189 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF */
190 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR */
191 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF */
192 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree */
193 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus */
194 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL */
195 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT */
196 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB */
197 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT */
198 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT */
199 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_RB */
200 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS */
201 {{{'o', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
202 {{{'p', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
203 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
204 {{{'r', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
205 {{{'s', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
206 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR */
207 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL */
208 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU */
209 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD */
210 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V */
211 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE */
212 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE */
213 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI */
214 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ */
215 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND */
216 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT */
217 {{{0, }}, {{0, }}}
218};
219
220static void
221apply_char_set(character_set cs, union utf8_char *utf8)
222{
223 int i = 0;
224
225 while (cs[i].match.byte[0]) {
226 if ((*utf8).ch == cs[i].match.ch) {
227 *utf8 = cs[i].replace;
228 break;
229 }
230 i++;
231 }
232}
233
Callum Lowcay7e08e902011-01-07 19:47:02 +0000234struct key_map {
235 int sym;
236 int num;
237 char escape;
238 char code;
239};
240/* Set last key_sub sym to NULL */
241typedef struct key_map *keyboard_mode;
242
243static struct key_map KM_NORMAL[] = {
244 {XK_Left, 1, '[', 'D'},
245 {XK_Right, 1, '[', 'C'},
246 {XK_Up, 1, '[', 'A'},
247 {XK_Down, 1, '[', 'B'},
248 {XK_Home, 1, '[', 'H'},
249 {XK_End, 1, '[', 'F'},
250 {0, 0, 0, 0}
251};
252static struct key_map KM_APPLICATION[] = {
253 {XK_Left, 1, 'O', 'D'},
254 {XK_Right, 1, 'O', 'C'},
255 {XK_Up, 1, 'O', 'A'},
256 {XK_Down, 1, 'O', 'B'},
257 {XK_Home, 1, 'O', 'H'},
258 {XK_End, 1, 'O', 'F'},
259 {XK_KP_Enter, 1, 'O', 'M'},
260 {XK_KP_Multiply, 1, 'O', 'j'},
261 {XK_KP_Add, 1, 'O', 'k'},
262 {XK_KP_Separator, 1, 'O', 'l'},
263 {XK_KP_Subtract, 1, 'O', 'm'},
264 {XK_KP_Divide, 1, 'O', 'o'},
265 {0, 0, 0, 0}
266};
267
268static int
269function_key_response(char escape, int num, uint32_t modifiers,
270 char code, char *response)
271{
272 int mod_num = 0;
273 int len;
274
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -0500275 if (modifiers & XKB_COMMON_SHIFT_MASK) mod_num |= 1;
276 if (modifiers & XKB_COMMON_MOD1_MASK) mod_num |= 2;
277 if (modifiers & XKB_COMMON_CONTROL_MASK) mod_num |= 4;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000278
279 if (mod_num != 0)
280 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
281 num, mod_num + 1, code);
282 else if (code != '~')
283 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
284 escape, code);
285 else
286 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
287 escape, num, code);
288
289 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
290 else return len;
291}
292
293/* returns the number of bytes written into response,
294 * which must have room for MAX_RESPONSE bytes */
295static int
296apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
297{
298 struct key_map map;
299 int len = 0;
300 int i = 0;
301
302 while (mode[i].sym) {
303 map = mode[i++];
304 if (sym == map.sym) {
305 len = function_key_response(map.escape, map.num,
306 modifiers, map.code,
307 response);
308 break;
309 }
310 }
311
312 return len;
313}
314
Callum Lowcay30eeae52011-01-07 19:46:55 +0000315struct terminal_color { double r, g, b, a; };
316struct attr {
317 unsigned char fg, bg;
318 char a; /* attributes format:
319 * 76543210
Callum Lowcay81179db2011-01-10 12:14:01 +1300320 * cilub */
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500321 char s; /* in selection */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000322};
323struct color_scheme {
324 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500325 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000326 struct attr default_attr;
327};
328
329static void
330attr_init(struct attr *data_attr, struct attr attr, int n)
331{
332 int i;
333 for (i = 0; i < n; i++) {
334 data_attr[i] = attr;
335 }
336}
337
Callum Lowcay67a201d2011-01-12 19:23:41 +1300338enum escape_state {
339 escape_state_normal = 0,
340 escape_state_escape,
341 escape_state_dcs,
342 escape_state_csi,
343 escape_state_osc,
344 escape_state_inner_escape,
345 escape_state_ignore,
346 escape_state_special
347};
348
349#define ESC_FLAG_WHAT 0x01
350#define ESC_FLAG_GT 0x02
351#define ESC_FLAG_BANG 0x04
352#define ESC_FLAG_CASH 0x08
353#define ESC_FLAG_SQUOTE 0x10
354#define ESC_FLAG_DQUOTE 0x20
355#define ESC_FLAG_SPACE 0x40
356
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500357struct terminal {
358 struct window *window;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500359 struct display *display;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000360 union utf8_char *data;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400361 struct task io_task;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000362 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000363 struct attr *data_attr;
364 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000365 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000366 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000367 char saved_origin_mode;
368 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000369 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000370 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000371 character_set cs, g0, g1;
372 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000373 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000374 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500375 int width, height, start, row, column;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000376 int saved_row, saved_column;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500377 int fd, master;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500378 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300379 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500380 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300381 enum escape_state state;
382 enum escape_state outer_state;
383 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000384 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500385 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500386 int fullscreen;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500387 int focused;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400388 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000389 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400390 cairo_font_extents_t extents;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500391 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500392
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500393 struct wl_selection *selection;
394 struct wl_selection_offer *selection_offer;
395 uint32_t selection_offer_has_text;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500396 int32_t dragging, selection_active;
397 int selection_start_x, selection_start_y;
398 int selection_end_x, selection_end_y;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500399};
400
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000401/* Create default tab stops, every 8 characters */
402static void
403terminal_init_tabs(struct terminal *terminal)
404{
405 int i = 0;
406
407 while (i < terminal->width) {
408 if (i % 8 == 0)
409 terminal->tab_ruler[i] = 1;
410 else
411 terminal->tab_ruler[i] = 0;
412 i++;
413 }
414}
415
Callum Lowcay30eeae52011-01-07 19:46:55 +0000416static void
417terminal_init(struct terminal *terminal)
418{
419 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000420 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000421 terminal->mode = MODE_SHOW_CURSOR |
422 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000423 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000424 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000425
426 terminal->row = 0;
427 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000428
Callum Lowcay256e72f2011-01-07 19:47:01 +0000429 terminal->g0 = CS_US;
430 terminal->g1 = CS_US;
431 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000432 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000433
434 terminal->saved_g0 = terminal->g0;
435 terminal->saved_g1 = terminal->g1;
436 terminal->saved_cs = terminal->cs;
437
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000438 terminal->saved_attr = terminal->curr_attr;
439 terminal->saved_origin_mode = terminal->origin_mode;
440 terminal->saved_row = terminal->row;
441 terminal->saved_column = terminal->column;
442
443 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000444}
445
446static void
447init_color_table(struct terminal *terminal)
448{
449 int c, r;
450 struct terminal_color *color_table = terminal->color_table;
451
452 for (c = 0; c < 256; c ++) {
453 if (c < 16) {
454 color_table[c] = terminal->color_scheme->palette[c];
455 } else if (c < 232) {
456 r = c - 16;
457 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
458 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
459 color_table[c].r = ((double)(r % 6) / 6.0);
460 color_table[c].a = 1.0;
461 } else {
462 r = (c - 232) * 10 + 8;
463 color_table[c].r = ((double) r) / 256.0;
464 color_table[c].g = color_table[c].r;
465 color_table[c].b = color_table[c].r;
466 color_table[c].a = 1.0;
467 }
468 }
469}
470
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000471static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500472terminal_get_row(struct terminal *terminal, int row)
473{
474 int index;
475
476 index = (row + terminal->start) % terminal->height;
477
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000478 return &terminal->data[index * terminal->width];
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500479}
480
Callum Lowcay30eeae52011-01-07 19:46:55 +0000481static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500482terminal_get_attr_row(struct terminal *terminal, int row)
483{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000484 int index;
485
486 index = (row + terminal->start) % terminal->height;
487
488 return &terminal->data_attr[index * terminal->width];
489}
490
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500491union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300492 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500493 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500494};
495
Kristian Høgsberg59826582011-01-20 11:56:57 -0500496static int
497terminal_compare_position(struct terminal *terminal,
498 int x, int y, int32_t ref_row, int32_t ref_col)
499{
500 struct rectangle allocation;
501 int top_margin, side_margin, col, row, ref_x;
502
503 window_get_child_allocation(terminal->window, &allocation);
504 side_margin = allocation.x + (allocation.width - terminal->width * terminal->extents.max_x_advance) / 2;
505 top_margin = allocation.y + (allocation.height - terminal->height * terminal->extents.height) / 2;
506
507 col = (x - side_margin) / terminal->extents.max_x_advance;
508 row = (y - top_margin) / terminal->extents.height;
509
510 ref_x = side_margin + ref_col * terminal->extents.max_x_advance +
511 terminal->extents.max_x_advance / 2;
512
513 if (row < ref_row)
514 return -1;
515 if (row == ref_row) {
516 if (col < ref_col)
517 return -1;
518 if (col == ref_col && x < ref_x)
519 return -1;
520 }
521
522 return 1;
523}
524
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500525static void
526terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500527 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500528{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500529 struct attr attr;
530 int foreground, background, tmp;
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500531 int start_cmp, end_cmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500532
533 start_cmp =
534 terminal_compare_position(terminal,
535 terminal->selection_start_x,
536 terminal->selection_start_y,
537 row, col);
538 end_cmp =
539 terminal_compare_position(terminal,
540 terminal->selection_end_x,
541 terminal->selection_end_y,
542 row, col);
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500543 decoded->attr.s = 0;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500544 if (start_cmp < 0 && end_cmp > 0)
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500545 decoded->attr.s = 1;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500546 else if (end_cmp < 0 && start_cmp > 0)
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500547 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500548
549 /* get the attributes for this character cell */
550 attr = terminal_get_attr_row(terminal, row)[col];
551 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500552 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500553 ((terminal->mode & MODE_SHOW_CURSOR) &&
554 terminal->focused && terminal->row == row &&
555 terminal->column == col)) {
556 foreground = attr.bg;
557 background = attr.fg;
558 if (attr.a & ATTRMASK_BOLD) {
559 if (foreground <= 16) foreground |= 0x08;
560 if (background <= 16) background &= 0x07;
561 }
562 } else {
563 foreground = attr.fg;
564 background = attr.bg;
565 }
566
567 if (terminal->mode & MODE_INVERSE) {
568 tmp = foreground;
569 foreground = background;
570 background = tmp;
571 if (attr.a & ATTRMASK_BOLD) {
572 if (foreground <= 16) foreground |= 0x08;
573 if (background <= 16) background &= 0x07;
574 }
575 }
576
Callum Lowcay9d708b02011-01-12 20:06:17 +1300577 decoded->attr.fg = foreground;
578 decoded->attr.bg = background;
579 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000580}
581
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500582
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500583static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000584terminal_scroll_buffer(struct terminal *terminal, int d)
585{
586 int i;
587
588 d = d % (terminal->height + 1);
589 terminal->start = (terminal->start + d) % terminal->height;
590 if (terminal->start < 0) terminal->start = terminal->height + terminal->start;
591 if(d < 0) {
592 d = 0 - d;
593 for(i = 0; i < d; i++) {
594 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
595 attr_init(terminal_get_attr_row(terminal, i),
596 terminal->curr_attr, terminal->width);
597 }
598 } else {
599 for(i = terminal->height - d; i < terminal->height; i++) {
600 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
601 attr_init(terminal_get_attr_row(terminal, i),
602 terminal->curr_attr, terminal->width);
603 }
604 }
605}
606
607static void
608terminal_scroll_window(struct terminal *terminal, int d)
609{
610 int i;
611 int window_height;
612 int from_row, to_row;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000613
614 // scrolling range is inclusive
615 window_height = terminal->margin_bottom - terminal->margin_top + 1;
616 d = d % (window_height + 1);
617 if(d < 0) {
618 d = 0 - d;
619 to_row = terminal->margin_bottom;
620 from_row = terminal->margin_bottom - d;
621
622 for (i = 0; i < (window_height - d); i++) {
623 memcpy(terminal_get_row(terminal, to_row - i),
624 terminal_get_row(terminal, from_row - i),
625 terminal->data_pitch);
626 memcpy(terminal_get_attr_row(terminal, to_row - i),
627 terminal_get_attr_row(terminal, from_row - i),
628 terminal->attr_pitch);
629 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000630 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
631 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000632 attr_init(terminal_get_attr_row(terminal, i),
633 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000634 }
635 } else {
636 to_row = terminal->margin_top;
637 from_row = terminal->margin_top + d;
638
639 for (i = 0; i < (window_height - d); i++) {
640 memcpy(terminal_get_row(terminal, to_row + i),
641 terminal_get_row(terminal, from_row + i),
642 terminal->data_pitch);
643 memcpy(terminal_get_attr_row(terminal, to_row + i),
644 terminal_get_attr_row(terminal, from_row + i),
645 terminal->attr_pitch);
646 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000647 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
648 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000649 attr_init(terminal_get_attr_row(terminal, i),
650 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000651 }
652 }
653}
654
655static void
656terminal_scroll(struct terminal *terminal, int d)
657{
658 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
659 terminal_scroll_buffer(terminal, d);
660 else
661 terminal_scroll_window(terminal, d);
662}
663
664static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000665terminal_shift_line(struct terminal *terminal, int d)
666{
667 union utf8_char *row;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500668 struct attr *attr_row;
Callum Lowcay69e96582011-01-07 19:47:00 +0000669
670 row = terminal_get_row(terminal, terminal->row);
671 attr_row = terminal_get_attr_row(terminal, terminal->row);
672
673 if ((terminal->width + d) <= terminal->column)
674 d = terminal->column + 1 - terminal->width;
675 if ((terminal->column + d) >= terminal->width)
676 d = terminal->width - terminal->column - 1;
677
678 if (d < 0) {
679 d = 0 - d;
680 memmove(&row[terminal->column],
681 &row[terminal->column + d],
682 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
Callum Lowcay69e96582011-01-07 19:47:00 +0000683 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
684 (terminal->width - terminal->column - d) * sizeof(struct attr));
685 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
686 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
687 } else {
688 memmove(&row[terminal->column + d], &row[terminal->column],
689 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
690 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
691 (terminal->width - terminal->column - d) * sizeof(struct attr));
692 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
693 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
694 }
695}
696
697static void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500698terminal_resize(struct terminal *terminal, int width, int height)
699{
700 size_t size;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000701 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000702 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000703 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000704 int data_pitch, attr_pitch;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500705 int i, l, total_rows;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500706 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000707 struct winsize ws;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500708 int32_t pixel_width, pixel_height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500709
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500710 if (width < 1)
711 width = 1;
712 if (height < 1)
713 height = 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500714 if (terminal->width == width && terminal->height == height)
715 return;
716
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500717 if (!terminal->fullscreen) {
718 pixel_width = width *
719 terminal->extents.max_x_advance + 2 * terminal->margin;
720 pixel_height = height *
721 terminal->extents.height + 2 * terminal->margin;
722 window_set_child_size(terminal->window,
723 pixel_width, pixel_height);
724 }
725
726 window_schedule_redraw (terminal->window);
727
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000728 data_pitch = width * sizeof(union utf8_char);
729 size = data_pitch * height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500730 data = malloc(size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000731 attr_pitch = width * sizeof(struct attr);
732 data_attr = malloc(attr_pitch * height);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000733 tab_ruler = malloc(width);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500734 memset(data, 0, size);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000735 memset(tab_ruler, 0, width);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000736 attr_init(data_attr, terminal->curr_attr, width * height);
737 if (terminal->data && terminal->data_attr) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500738 if (width > terminal->width)
739 l = terminal->width;
740 else
741 l = width;
742
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500743 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500744 total_rows = height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500745 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500746 total_rows = terminal->height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500747 }
748
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000749 for (i = 0; i < total_rows; i++) {
750 memcpy(&data[width * i],
751 terminal_get_row(terminal, i),
752 l * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000753 memcpy(&data_attr[width * i],
754 terminal_get_attr_row(terminal, i),
755 l * sizeof(struct attr));
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000756 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500757
758 free(terminal->data);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000759 free(terminal->data_attr);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000760 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500761 }
762
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000763 terminal->data_pitch = data_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000764 terminal->attr_pitch = attr_pitch;
Callum Lowcay86653ed2011-01-07 19:47:03 +0000765 terminal->margin_bottom =
766 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500767 terminal->width = width;
768 terminal->height = height;
769 terminal->data = data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000770 terminal->data_attr = data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000771 terminal->tab_ruler = tab_ruler;
772 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500773
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000774 /* Update the window size */
775 ws.ws_row = terminal->height;
776 ws.ws_col = terminal->width;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500777 window_get_child_allocation(terminal->window, &allocation);
778 ws.ws_xpixel = allocation.width;
779 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000780 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500781}
782
Callum Lowcay30eeae52011-01-07 19:46:55 +0000783struct color_scheme DEFAULT_COLORS = {
784 {
785 {0, 0, 0, 1}, /* black */
786 {0.66, 0, 0, 1}, /* red */
787 {0 , 0.66, 0, 1}, /* green */
788 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
789 {0 , 0 , 0.66, 1}, /* blue */
790 {0.66, 0 , 0.66, 1}, /* magenta */
791 {0, 0.66, 0.66, 1}, /* cyan */
792 {0.66, 0.66, 0.66, 1}, /* light grey */
793 {0.22, 0.33, 0.33, 1}, /* dark grey */
794 {1, 0.33, 0.33, 1}, /* high red */
795 {0.33, 1, 0.33, 1}, /* high green */
796 {1, 1, 0.33, 1}, /* high yellow */
797 {0.33, 0.33, 1, 1}, /* high blue */
798 {1, 0.33, 1, 1}, /* high magenta */
799 {0.33, 1, 1, 1}, /* high cyan */
800 {1, 1, 1, 1} /* white */
801 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500802 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000803 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
804};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400805
Kristian Høgsberg22106762008-12-08 13:50:07 -0500806static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500807terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
808{
809 cairo_set_source_rgba(cr,
810 terminal->color_table[index].r,
811 terminal->color_table[index].g,
812 terminal->color_table[index].b,
813 terminal->color_table[index].a);
814}
815
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500816static void
817terminal_send_selection(struct terminal *terminal, int fd)
818{
819 int row, col;
820 union utf8_char *p_row;
821 union decoded_attr attr;
822 FILE *fp;
823 int len;
824
825 fp = fdopen(fd, "w");
826 for (row = 0; row < terminal->height; row++) {
827 p_row = terminal_get_row(terminal, row);
828 for (col = 0; col < terminal->width; col++) {
829 /* get the attributes for this character cell */
830 terminal_decode_attr(terminal, row, col, &attr);
831 if (!attr.attr.s)
832 continue;
833 len = strnlen((char *) p_row[col].byte, 4);
834 fwrite(p_row[col].byte, 1, len, fp);
835 }
836 }
837 fclose(fp);
838}
839
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500840struct glyph_run {
841 struct terminal *terminal;
842 cairo_t *cr;
843 int count;
844 union decoded_attr attr;
845 cairo_glyph_t glyphs[256], *g;
846};
847
848static void
849glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
850{
851 run->terminal = terminal;
852 run->cr = cr;
853 run->g = run->glyphs;
854 run->count = 0;
855 run->attr.key = 0;
856}
857
858static void
859glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
860{
861 cairo_scaled_font_t *font;
862
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500863 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
864 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300865 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500866 font = run->terminal->font_bold;
867 else
868 font = run->terminal->font_normal;
869 cairo_set_scaled_font(run->cr, font);
870 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300871 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500872
Callum Lowcay9d708b02011-01-12 20:06:17 +1300873 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
874 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500875 run->g = run->glyphs;
876 run->count = 0;
877 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300878 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500879}
880
881static void
882glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
883{
884 int num_glyphs;
885 cairo_scaled_font_t *font;
886
887 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
888
Callum Lowcay9d708b02011-01-12 20:06:17 +1300889 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500890 font = run->terminal->font_bold;
891 else
892 font = run->terminal->font_normal;
893
894 cairo_move_to(run->cr, x, y);
895 cairo_scaled_font_text_to_glyphs (font, x, y,
896 (char *) c->byte, 4,
897 &run->g, &num_glyphs,
898 NULL, NULL, NULL);
899 run->g += num_glyphs;
900 run->count += num_glyphs;
901}
902
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500903static void
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500904terminal_draw_contents(struct terminal *terminal)
905{
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500906 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500907 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000908 int top_margin, side_margin;
909 int row, col;
910 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500911 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000912 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500913 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500914 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500915 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500916 cairo_font_extents_t extents;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500917
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500918 surface = window_get_surface(terminal->window);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500919 window_get_child_allocation(terminal->window, &allocation);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500920 cr = cairo_create(surface);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500921 cairo_rectangle(cr, allocation.x, allocation.y,
922 allocation.width, allocation.height);
923 cairo_clip(cr);
924 cairo_push_group(cr);
925
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500926 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500927 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500928 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500929
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500930 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500931
Kristian Høgsberg59826582011-01-20 11:56:57 -0500932 extents = terminal->extents;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500933 side_margin = (allocation.width - terminal->width * extents.max_x_advance) / 2;
934 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500935
Callum Lowcay30eeae52011-01-07 19:46:55 +0000936 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500937 cairo_translate(cr, allocation.x + side_margin,
938 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500939 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000940 for (row = 0; row < terminal->height; row++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000941 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000942 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500943 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000944
Callum Lowcay9d708b02011-01-12 20:06:17 +1300945 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -0500946 continue;
947
Callum Lowcay9d708b02011-01-12 20:06:17 +1300948 terminal_set_color(terminal, cr, attr.attr.bg);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500949 cairo_move_to(cr, col * extents.max_x_advance,
950 row * extents.height);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000951 cairo_rel_line_to(cr, extents.max_x_advance, 0);
952 cairo_rel_line_to(cr, 0, extents.height);
953 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
954 cairo_close_path(cr);
955 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500956 }
957 }
Callum Lowcay30eeae52011-01-07 19:46:55 +0000958
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500959 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
960
961 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500962 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500963 for (row = 0; row < terminal->height; row++) {
964 p_row = terminal_get_row(terminal, row);
965 for (col = 0; col < terminal->width; col++) {
966 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500967 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500968
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500969 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000970
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500971 text_x = col * extents.max_x_advance;
972 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +1300973 if (attr.attr.a & ATTRMASK_UNDERLINE) {
974 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000975 cairo_move_to(cr, text_x, (double)text_y + 1.5);
976 cairo_line_to(cr, text_x + extents.max_x_advance, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000977 cairo_stroke(cr);
978 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -0500979
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500980 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000981 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500982 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500983
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500984 attr.key = ~0;
985 glyph_run_flush(&run, attr);
986
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000987 if ((terminal->mode & MODE_SHOW_CURSOR) && !terminal->focused) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000988 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500989
Callum Lowcay30eeae52011-01-07 19:46:55 +0000990 cairo_set_line_width(cr, 1);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500991 cairo_move_to(cr, terminal->column * extents.max_x_advance + d,
992 terminal->row * extents.height + d);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000993 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
994 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
995 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
996 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500997
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500998 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000999 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001000
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001001 cairo_pop_group_to_source(cr);
1002 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001003 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001004 cairo_surface_destroy(surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001005}
1006
1007static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001008terminal_write(struct terminal *terminal, const char *data, size_t length)
1009{
1010 if (write(terminal->master, data, length) < 0)
1011 abort();
1012}
1013
1014static void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001015resize_handler(struct window *window,
1016 int32_t pixel_width, int32_t pixel_height, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001017{
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001018 struct terminal *terminal = data;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001019 int32_t width, height;
Kristian Høgsberg22106762008-12-08 13:50:07 -05001020
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001021 width = (pixel_width - 2 * terminal->margin) /
Kristian Høgsberg09531622010-06-14 23:22:15 -04001022 (int32_t) terminal->extents.max_x_advance;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001023 height = (pixel_height - 2 * terminal->margin) /
Kristian Høgsberg09531622010-06-14 23:22:15 -04001024 (int32_t) terminal->extents.height;
Tiago Vignatti5fd89d22011-01-10 19:30:04 +02001025
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001026 if (terminal->fullscreen)
1027 window_set_child_size(terminal->window,
1028 pixel_width, pixel_height);
1029
Kristian Høgsberg22106762008-12-08 13:50:07 -05001030 terminal_resize(terminal, width, height);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001031}
Kristian Høgsberg22106762008-12-08 13:50:07 -05001032
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001033static void
1034terminal_draw(struct terminal *terminal)
1035{
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001036 window_draw(terminal->window);
1037 terminal_draw_contents(terminal);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001038 window_flush(terminal->window);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001039}
1040
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001041static void
1042redraw_handler(struct window *window, void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001043{
1044 struct terminal *terminal = data;
1045
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001046 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001047}
1048
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001049static void
1050terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001051
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001052static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001053handle_char(struct terminal *terminal, union utf8_char utf8);
1054
1055static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001056handle_sgr(struct terminal *terminal, int code);
1057
1058static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001059handle_term_parameter(struct terminal *terminal, int code, int sr)
1060{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001061 int i;
1062
Callum Lowcay67a201d2011-01-12 19:23:41 +13001063 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001064 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001065 case 1: /* DECCKM */
1066 if (sr) terminal->key_mode = KM_APPLICATION;
1067 else terminal->key_mode = KM_NORMAL;
1068 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001069 case 2: /* DECANM */
1070 /* No VT52 support yet */
1071 terminal->g0 = CS_US;
1072 terminal->g1 = CS_US;
1073 terminal->cs = terminal->g0;
1074 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001075 case 3: /* DECCOLM */
1076 if (sr)
1077 terminal_resize(terminal, 132, 24);
1078 else
1079 terminal_resize(terminal, 80, 24);
1080
1081 /* set columns, but also home cursor and clear screen */
1082 terminal->row = 0; terminal->column = 0;
1083 for (i = 0; i < terminal->height; i++) {
1084 memset(terminal_get_row(terminal, i),
1085 0, terminal->data_pitch);
1086 attr_init(terminal_get_attr_row(terminal, i),
1087 terminal->curr_attr, terminal->width);
1088 }
1089 break;
1090 case 5: /* DECSCNM */
1091 if (sr) terminal->mode |= MODE_INVERSE;
1092 else terminal->mode &= ~MODE_INVERSE;
1093 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001094 case 6: /* DECOM */
1095 terminal->origin_mode = sr;
1096 if (terminal->origin_mode)
1097 terminal->row = terminal->margin_top;
1098 else
1099 terminal->row = 0;
1100 terminal->column = 0;
1101 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001102 case 7: /* DECAWM */
1103 if (sr) terminal->mode |= MODE_AUTOWRAP;
1104 else terminal->mode &= ~MODE_AUTOWRAP;
1105 break;
1106 case 8: /* DECARM */
1107 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1108 else terminal->mode &= ~MODE_AUTOREPEAT;
1109 break;
1110 case 25:
1111 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1112 else terminal->mode &= ~MODE_SHOW_CURSOR;
1113 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001114 case 1037: /* deleteSendsDel */
1115 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1116 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1117 break;
1118 case 1039: /* altSendsEscape */
1119 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1120 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1121 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001122 default:
1123 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1124 break;
1125 }
1126 } else {
1127 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001128 case 4: /* IRM */
1129 if (sr) terminal->mode |= MODE_IRM;
1130 else terminal->mode &= ~MODE_IRM;
1131 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001132 case 20: /* LNM */
1133 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1134 else terminal->mode &= ~MODE_LF_NEWLINE;
1135 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001136 default:
1137 fprintf(stderr, "Unknown parameter: %d\n", code);
1138 break;
1139 }
1140 }
1141}
1142
1143static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001144handle_dcs(struct terminal *terminal)
1145{
1146}
1147
1148static void
1149handle_osc(struct terminal *terminal)
1150{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001151 char *p;
1152 int code;
1153
1154 terminal->escape[terminal->escape_length++] = '\0';
1155 p = &terminal->escape[2];
1156 code = strtol(p, &p, 10);
1157 if (*p == ';') p++;
1158
1159 switch (code) {
1160 case 0: /* Icon name and window title */
1161 case 1: /* Icon label */
1162 case 2: /* Window title*/
1163 window_set_title(terminal->window, p);
1164 break;
1165 default:
1166 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1167 break;
1168 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001169}
1170
1171static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001172handle_escape(struct terminal *terminal)
1173{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001174 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001175 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001176 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001177 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001178 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001179 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001180 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001181
1182 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001183 i = 0;
1184 p = &terminal->escape[2];
1185 while ((isdigit(*p) || *p == ';') && i < 10) {
1186 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001187 if (!set[i]) {
1188 args[i] = 0;
1189 set[i] = 1;
1190 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001191 p++;
1192 i++;
1193 } else {
1194 args[i] = strtol(p, &p, 10);
1195 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001196 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001197 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001198
1199 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001200 case '@': /* ICH */
1201 count = set[0] ? args[0] : 1;
1202 if (count == 0) count = 1;
1203 terminal_shift_line(terminal, count);
1204 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001205 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001206 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001207 if (count == 0) count = 1;
1208 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001209 terminal->row -= count;
1210 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001211 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001212 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001213 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001214 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001215 if (count == 0) count = 1;
1216 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001217 terminal->row += count;
1218 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001219 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001220 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001221 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001222 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001223 if (count == 0) count = 1;
1224 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001225 terminal->column += count;
1226 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001227 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001228 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001229 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001230 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001231 if (count == 0) count = 1;
1232 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001233 terminal->column -= count;
1234 else
1235 terminal->column = 0;
1236 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001237 case 'E': /* CNL */
1238 count = set[0] ? args[0] : 1;
1239 if (terminal->row + count <= terminal->margin_bottom)
1240 terminal->row += count;
1241 else
1242 terminal->row = terminal->margin_bottom;
1243 terminal->column = 0;
1244 break;
1245 case 'F': /* CPL */
1246 count = set[0] ? args[0] : 1;
1247 if (terminal->row - count >= terminal->margin_top)
1248 terminal->row -= count;
1249 else
1250 terminal->row = terminal->margin_top;
1251 terminal->column = 0;
1252 break;
1253 case 'G': /* CHA */
1254 y = set[0] ? args[0] : 1;
1255 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1256
1257 terminal->column = y - 1;
1258 break;
1259 case 'f': /* HVP */
1260 case 'H': /* CUP */
1261 x = (set[1] ? args[1] : 1) - 1;
1262 x = x < 0 ? 0 :
1263 (x >= terminal->width ? terminal->width - 1 : x);
1264
1265 y = (set[0] ? args[0] : 1) - 1;
1266 if (terminal->origin_mode) {
1267 y += terminal->margin_top;
1268 y = y < terminal->margin_top ? terminal->margin_top :
1269 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1270 } else {
1271 y = y < 0 ? 0 :
1272 (y >= terminal->height ? terminal->height - 1 : y);
1273 }
1274
1275 terminal->row = y;
1276 terminal->column = x;
1277 break;
1278 case 'I': /* CHT */
1279 count = set[0] ? args[0] : 1;
1280 if (count == 0) count = 1;
1281 while (count > 0 && terminal->column < terminal->width) {
1282 if (terminal->tab_ruler[terminal->column]) count--;
1283 terminal->column++;
1284 }
1285 terminal->column--;
1286 break;
1287 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001288 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001289 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001290 if (!set[0] || args[0] == 0 || args[0] > 2) {
1291 memset(&row[terminal->column],
1292 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1293 attr_init(&attr_row[terminal->column],
1294 terminal->curr_attr, terminal->width - terminal->column);
1295 for (i = terminal->row + 1; i < terminal->height; i++) {
1296 memset(terminal_get_row(terminal, i),
1297 0, terminal->data_pitch);
1298 attr_init(terminal_get_attr_row(terminal, i),
1299 terminal->curr_attr, terminal->width);
1300 }
1301 } else if (args[0] == 1) {
1302 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1303 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1304 for (i = 0; i < terminal->row; i++) {
1305 memset(terminal_get_row(terminal, i),
1306 0, terminal->data_pitch);
1307 attr_init(terminal_get_attr_row(terminal, i),
1308 terminal->curr_attr, terminal->width);
1309 }
1310 } else if (args[0] == 2) {
1311 for (i = 0; i < terminal->height; i++) {
1312 memset(terminal_get_row(terminal, i),
1313 0, terminal->data_pitch);
1314 attr_init(terminal_get_attr_row(terminal, i),
1315 terminal->curr_attr, terminal->width);
1316 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001317 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001318 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001319 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001320 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001321 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001322 if (!set[0] || args[0] == 0 || args[0] > 2) {
1323 memset(&row[terminal->column], 0,
1324 (terminal->width - terminal->column) * sizeof(union utf8_char));
1325 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1326 terminal->width - terminal->column);
1327 } else if (args[0] == 1) {
1328 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1329 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1330 } else if (args[0] == 2) {
1331 memset(row, 0, terminal->data_pitch);
1332 attr_init(attr_row, terminal->curr_attr, terminal->width);
1333 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001334 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001335 case 'L': /* IL */
1336 count = set[0] ? args[0] : 1;
1337 if (count == 0) count = 1;
1338 if (terminal->row >= terminal->margin_top &&
1339 terminal->row < terminal->margin_bottom)
1340 {
1341 top = terminal->margin_top;
1342 terminal->margin_top = terminal->row;
1343 terminal_scroll(terminal, 0 - count);
1344 terminal->margin_top = top;
1345 } else if (terminal->row == terminal->margin_bottom) {
1346 memset(terminal_get_row(terminal, terminal->row),
1347 0, terminal->data_pitch);
1348 attr_init(terminal_get_attr_row(terminal, terminal->row),
1349 terminal->curr_attr, terminal->width);
1350 }
1351 break;
1352 case 'M': /* DL */
1353 count = set[0] ? args[0] : 1;
1354 if (count == 0) count = 1;
1355 if (terminal->row >= terminal->margin_top &&
1356 terminal->row < terminal->margin_bottom)
1357 {
1358 top = terminal->margin_top;
1359 terminal->margin_top = terminal->row;
1360 terminal_scroll(terminal, count);
1361 terminal->margin_top = top;
1362 } else if (terminal->row == terminal->margin_bottom) {
1363 memset(terminal_get_row(terminal, terminal->row),
1364 0, terminal->data_pitch);
1365 }
1366 break;
1367 case 'P': /* DCH */
1368 count = set[0] ? args[0] : 1;
1369 if (count == 0) count = 1;
1370 terminal_shift_line(terminal, 0 - count);
1371 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001372 case 'S': /* SU */
1373 terminal_scroll(terminal, set[0] ? args[0] : 1);
1374 break;
1375 case 'T': /* SD */
1376 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1377 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001378 case 'X': /* ECH */
1379 count = set[0] ? args[0] : 1;
1380 if (count == 0) count = 1;
1381 if ((terminal->column + count) > terminal->width)
1382 count = terminal->width - terminal->column;
1383 row = terminal_get_row(terminal, terminal->row);
1384 attr_row = terminal_get_attr_row(terminal, terminal->row);
1385 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1386 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1387 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001388 case 'Z': /* CBT */
1389 count = set[0] ? args[0] : 1;
1390 if (count == 0) count = 1;
1391 while (count > 0 && terminal->column >= 0) {
1392 if (terminal->tab_ruler[terminal->column]) count--;
1393 terminal->column--;
1394 }
1395 terminal->column++;
1396 break;
1397 case '`': /* HPA */
1398 y = set[0] ? args[0] : 1;
1399 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1400
1401 terminal->column = y - 1;
1402 break;
1403 case 'b': /* REP */
1404 count = set[0] ? args[0] : 1;
1405 if (count == 0) count = 1;
1406 if (terminal->last_char.byte[0])
1407 for (i = 0; i < count; i++)
1408 handle_char(terminal, terminal->last_char);
1409 terminal->last_char.byte[0] = 0;
1410 break;
1411 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001412 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001413 break;
1414 case 'd': /* VPA */
1415 x = set[0] ? args[0] : 1;
1416 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1417
1418 terminal->row = x - 1;
1419 break;
1420 case 'g': /* TBC */
1421 if (!set[0] || args[0] == 0) {
1422 terminal->tab_ruler[terminal->column] = 0;
1423 } else if (args[0] == 3) {
1424 memset(terminal->tab_ruler, 0, terminal->width);
1425 }
1426 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001427 case 'h': /* SM */
1428 for(i = 0; i < 10 && set[i]; i++) {
1429 handle_term_parameter(terminal, args[i], 1);
1430 }
1431 break;
1432 case 'l': /* RM */
1433 for(i = 0; i < 10 && set[i]; i++) {
1434 handle_term_parameter(terminal, args[i], 0);
1435 }
1436 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001437 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001438 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001439 if (i <= 7 && set[i] && set[i + 1] &&
1440 set[i + 2] && args[i + 1] == 5)
1441 {
1442 if (args[i] == 38) {
1443 handle_sgr(terminal, args[i + 2] + 256);
1444 break;
1445 } else if (args[i] == 48) {
1446 handle_sgr(terminal, args[i + 2] + 512);
1447 break;
1448 }
1449 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001450 if(set[i]) {
1451 handle_sgr(terminal, args[i]);
1452 } else if(i == 0) {
1453 handle_sgr(terminal, 0);
1454 break;
1455 } else {
1456 break;
1457 }
1458 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001459 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001460 case 'n': /* DSR */
1461 i = set[0] ? args[0] : 0;
1462 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001463 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001464 } else if (i == 6) {
1465 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1466 terminal->origin_mode ?
1467 terminal->row+terminal->margin_top : terminal->row+1,
1468 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001469 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001470 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001471 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001472 case 'r':
1473 if(!set[0]) {
1474 terminal->margin_top = 0;
1475 terminal->margin_bottom = terminal->height-1;
1476 terminal->row = 0;
1477 terminal->column = 0;
1478 } else {
1479 top = (set[0] ? args[0] : 1) - 1;
1480 top = top < 0 ? 0 :
1481 (top >= terminal->height ? terminal->height - 1 : top);
1482 bottom = (set[1] ? args[1] : 1) - 1;
1483 bottom = bottom < 0 ? 0 :
1484 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1485 if(bottom > top) {
1486 terminal->margin_top = top;
1487 terminal->margin_bottom = bottom;
1488 } else {
1489 terminal->margin_top = 0;
1490 terminal->margin_bottom = terminal->height-1;
1491 }
1492 if(terminal->origin_mode)
1493 terminal->row = terminal->margin_top;
1494 else
1495 terminal->row = 0;
1496 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001497 }
1498 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001499 case 's':
1500 terminal->saved_row = terminal->row;
1501 terminal->saved_column = terminal->column;
1502 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001503 case 't': /* windowOps */
1504 if (!set[0]) break;
1505 switch (args[0]) {
1506 case 4: /* resize px */
1507 if (set[1] && set[2]) {
1508 window_set_child_size(terminal->window,
1509 args[2], args[1]);
1510 resize_handler(terminal->window,
1511 args[2], args[1], terminal);
1512 }
1513 break;
1514 case 8: /* resize ch */
1515 if (set[1] && set[2]) {
1516 terminal_resize(terminal, args[2], args[1]);
1517 }
1518 break;
1519 case 13: /* report position */
1520 window_get_child_allocation(terminal->window, &allocation);
1521 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1522 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001523 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001524 break;
1525 case 14: /* report px */
1526 window_get_child_allocation(terminal->window, &allocation);
1527 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1528 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001529 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001530 break;
1531 case 18: /* report ch */
1532 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1533 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001534 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001535 break;
1536 case 21: /* report title */
1537 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1538 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001539 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001540 break;
1541 default:
1542 if (args[0] >= 24)
1543 terminal_resize(terminal, terminal->width, args[0]);
1544 else
1545 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1546 break;
1547 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001548 case 'u':
1549 terminal->row = terminal->saved_row;
1550 terminal->column = terminal->saved_column;
1551 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001552 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001553 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001554 break;
1555 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001556}
1557
1558static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001559handle_non_csi_escape(struct terminal *terminal, char code)
1560{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001561 switch(code) {
1562 case 'M': /* RI */
1563 terminal->row -= 1;
1564 if(terminal->row < terminal->margin_top) {
1565 terminal->row = terminal->margin_top;
1566 terminal_scroll(terminal, -1);
1567 }
1568 break;
1569 case 'E': /* NEL */
1570 terminal->column = 0;
1571 // fallthrough
1572 case 'D': /* IND */
1573 terminal->row += 1;
1574 if(terminal->row > terminal->margin_bottom) {
1575 terminal->row = terminal->margin_bottom;
1576 terminal_scroll(terminal, +1);
1577 }
1578 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001579 case 'c': /* RIS */
1580 terminal_init(terminal);
1581 break;
1582 case 'H': /* HTS */
1583 terminal->tab_ruler[terminal->column] = 1;
1584 break;
1585 case '7': /* DECSC */
1586 terminal->saved_row = terminal->row;
1587 terminal->saved_column = terminal->column;
1588 terminal->saved_attr = terminal->curr_attr;
1589 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001590 terminal->saved_cs = terminal->cs;
1591 terminal->saved_g0 = terminal->g0;
1592 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001593 break;
1594 case '8': /* DECRC */
1595 terminal->row = terminal->saved_row;
1596 terminal->column = terminal->saved_column;
1597 terminal->curr_attr = terminal->saved_attr;
1598 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001599 terminal->cs = terminal->saved_cs;
1600 terminal->g0 = terminal->saved_g0;
1601 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001602 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001603 case '=': /* DECPAM */
1604 terminal->key_mode = KM_APPLICATION;
1605 break;
1606 case '>': /* DECPNM */
1607 terminal->key_mode = KM_NORMAL;
1608 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001609 default:
1610 fprintf(stderr, "Unknown escape code: %c\n", code);
1611 break;
1612 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001613}
1614
1615static void
1616handle_special_escape(struct terminal *terminal, char special, char code)
1617{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001618 int i, numChars;
1619
1620 if (special == '#') {
1621 switch(code) {
1622 case '8':
1623 /* fill with 'E', no cheap way to do this */
1624 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1625 numChars = terminal->width * terminal->height;
1626 for(i = 0; i < numChars; i++) {
1627 terminal->data[i].byte[0] = 'E';
1628 }
1629 break;
1630 default:
1631 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1632 break;
1633 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001634 } else if (special == '(' || special == ')') {
1635 switch(code) {
1636 case '0':
1637 if (special == '(')
1638 terminal->g0 = CS_SPECIAL;
1639 else
1640 terminal->g1 = CS_SPECIAL;
1641 break;
1642 case 'A':
1643 if (special == '(')
1644 terminal->g0 = CS_UK;
1645 else
1646 terminal->g1 = CS_UK;
1647 break;
1648 case 'B':
1649 if (special == '(')
1650 terminal->g0 = CS_US;
1651 else
1652 terminal->g1 = CS_US;
1653 break;
1654 default:
1655 fprintf(stderr, "Unknown character set %c\n", code);
1656 break;
1657 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001658 } else {
1659 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1660 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001661}
1662
1663static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001664handle_sgr(struct terminal *terminal, int code)
1665{
1666 switch(code) {
1667 case 0:
1668 terminal->curr_attr = terminal->color_scheme->default_attr;
1669 break;
1670 case 1:
1671 terminal->curr_attr.a |= ATTRMASK_BOLD;
1672 if (terminal->curr_attr.fg < 8)
1673 terminal->curr_attr.fg += 8;
1674 break;
1675 case 4:
1676 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1677 break;
1678 case 5:
1679 terminal->curr_attr.a |= ATTRMASK_BLINK;
1680 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001681 case 8:
1682 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1683 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001684 case 2:
1685 case 21:
1686 case 22:
1687 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1688 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1689 terminal->curr_attr.fg -= 8;
1690 break;
1691 case 24:
1692 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1693 break;
1694 case 25:
1695 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1696 break;
1697 case 7:
1698 case 26:
1699 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1700 break;
1701 case 27:
1702 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1703 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001704 case 28:
1705 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1706 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001707 case 39:
1708 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1709 break;
1710 case 49:
1711 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1712 break;
1713 default:
1714 if(code >= 30 && code <= 37) {
1715 terminal->curr_attr.fg = code - 30;
1716 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1717 terminal->curr_attr.fg += 8;
1718 } else if(code >= 40 && code <= 47) {
1719 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001720 } else if (code >= 90 && code <= 97) {
1721 terminal->curr_attr.fg = code - 90 + 8;
1722 } else if (code >= 100 && code <= 107) {
1723 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001724 } else if(code >= 256 && code < 512) {
1725 terminal->curr_attr.fg = code - 256;
1726 } else if(code >= 512 && code < 768) {
1727 terminal->curr_attr.bg = code - 512;
1728 } else {
1729 fprintf(stderr, "Unknown SGR code: %d\n", code);
1730 }
1731 break;
1732 }
1733}
1734
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001735/* Returns 1 if c was special, otherwise 0 */
1736static int
1737handle_special_char(struct terminal *terminal, char c)
1738{
1739 union utf8_char *row;
1740 struct attr *attr_row;
1741
1742 row = terminal_get_row(terminal, terminal->row);
1743 attr_row = terminal_get_attr_row(terminal, terminal->row);
1744
1745 switch(c) {
1746 case '\r':
1747 terminal->column = 0;
1748 break;
1749 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001750 if (terminal->mode & MODE_LF_NEWLINE) {
1751 terminal->column = 0;
1752 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001753 /* fallthrough */
1754 case '\v':
1755 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001756 terminal->row++;
1757 if(terminal->row > terminal->margin_bottom) {
1758 terminal->row = terminal->margin_bottom;
1759 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001760 }
1761
1762 break;
1763 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001764 while (terminal->column < terminal->width) {
1765 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001766 if (terminal->mode & MODE_IRM)
1767 terminal_shift_line(terminal, +1);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001768 row[terminal->column].byte[0] = ' ';
1769 row[terminal->column].byte[1] = '\0';
1770 attr_row[terminal->column] = terminal->curr_attr;
1771 terminal->column++;
1772 }
1773 if (terminal->column >= terminal->width) {
1774 terminal->column = terminal->width - 1;
1775 }
1776
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001777 break;
1778 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001779 if (terminal->column >= terminal->width) {
1780 terminal->column = terminal->width - 2;
1781 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001782 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001783 } else if (terminal->mode & MODE_AUTOWRAP) {
1784 terminal->column = terminal->width - 1;
1785 terminal->row -= 1;
1786 if (terminal->row < terminal->margin_top) {
1787 terminal->row = terminal->margin_top;
1788 terminal_scroll(terminal, -1);
1789 }
1790 }
1791
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001792 break;
1793 case '\a':
1794 /* Bell */
1795 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001796 case '\x0E': /* SO */
1797 terminal->cs = terminal->g1;
1798 break;
1799 case '\x0F': /* SI */
1800 terminal->cs = terminal->g0;
1801 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001802 default:
1803 return 0;
1804 }
1805
1806 return 1;
1807}
1808
1809static void
1810handle_char(struct terminal *terminal, union utf8_char utf8)
1811{
1812 union utf8_char *row;
1813 struct attr *attr_row;
1814
1815 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001816
1817 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001818
1819 /* There are a whole lot of non-characters, control codes,
1820 * and formatting codes that should probably be ignored,
1821 * for example: */
1822 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1823 /* BOM, ignore */
1824 return;
1825 }
1826
1827 /* Some of these non-characters should be translated, e.g.: */
1828 if (utf8.byte[0] < 32) {
1829 utf8.byte[0] = utf8.byte[0] + 64;
1830 }
1831
1832 /* handle right margin effects */
1833 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001834 if (terminal->mode & MODE_AUTOWRAP) {
1835 terminal->column = 0;
1836 terminal->row += 1;
1837 if (terminal->row > terminal->margin_bottom) {
1838 terminal->row = terminal->margin_bottom;
1839 terminal_scroll(terminal, +1);
1840 }
1841 } else {
1842 terminal->column--;
1843 }
1844 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001845
1846 row = terminal_get_row(terminal, terminal->row);
1847 attr_row = terminal_get_attr_row(terminal, terminal->row);
1848
Callum Lowcay69e96582011-01-07 19:47:00 +00001849 if (terminal->mode & MODE_IRM)
1850 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001851 row[terminal->column] = utf8;
1852 attr_row[terminal->column++] = terminal->curr_attr;
1853
1854 if (utf8.ch != terminal->last_char.ch)
1855 terminal->last_char = utf8;
1856}
1857
Callum Lowcay30eeae52011-01-07 19:46:55 +00001858static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001859escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1860{
1861 int len, i;
1862
1863 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1864 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1865 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1866 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1867 else len = 1; /* Invalid, cannot happen */
1868
1869 if (terminal->escape_length + len <= MAX_ESCAPE) {
1870 for (i = 0; i < len; i++)
1871 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1872 terminal->escape_length += len;
1873 } else if (terminal->escape_length < MAX_ESCAPE) {
1874 terminal->escape[terminal->escape_length++] = 0;
1875 }
1876}
1877
1878static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001879terminal_data(struct terminal *terminal, const char *data, size_t length)
1880{
1881 int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001882 union utf8_char utf8;
1883 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001884
1885 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001886 parser_state =
1887 utf8_next_char(&terminal->state_machine, data[i]);
1888 switch(parser_state) {
1889 case utf8state_accept:
1890 utf8.ch = terminal->state_machine.s.ch;
1891 break;
1892 case utf8state_reject:
1893 /* the unicode replacement character */
1894 utf8.byte[0] = 0xEF;
1895 utf8.byte[1] = 0xBF;
1896 utf8.byte[2] = 0xBD;
1897 utf8.byte[3] = 0x00;
1898 break;
1899 default:
1900 continue;
1901 }
1902
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001903 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001904 switch (terminal->state) {
1905 case escape_state_escape:
1906 escape_append_utf8(terminal, utf8);
1907 switch (utf8.byte[0]) {
1908 case 'P': /* DCS */
1909 terminal->state = escape_state_dcs;
1910 break;
1911 case '[': /* CSI */
1912 terminal->state = escape_state_csi;
1913 break;
1914 case ']': /* OSC */
1915 terminal->state = escape_state_osc;
1916 break;
1917 case '#':
1918 case '(':
1919 case ')': /* special */
1920 terminal->state = escape_state_special;
1921 break;
1922 case '^': /* PM (not implemented) */
1923 case '_': /* APC (not implemented) */
1924 terminal->state = escape_state_ignore;
1925 break;
1926 default:
1927 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001928 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13001929 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001930 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001931 continue;
1932 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001933 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1934 /* do nothing */
1935 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001936 terminal->escape_flags |= ESC_FLAG_WHAT;
1937 } else if (utf8.byte[0] == '>') {
1938 terminal->escape_flags |= ESC_FLAG_GT;
1939 } else if (utf8.byte[0] == '!') {
1940 terminal->escape_flags |= ESC_FLAG_BANG;
1941 } else if (utf8.byte[0] == '$') {
1942 terminal->escape_flags |= ESC_FLAG_CASH;
1943 } else if (utf8.byte[0] == '\'') {
1944 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1945 } else if (utf8.byte[0] == '"') {
1946 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1947 } else if (utf8.byte[0] == ' ') {
1948 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001949 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001950 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001951 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13001952 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001953 }
1954
1955 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1956 utf8.byte[0] == '`')
1957 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001958 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001959 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001960 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001961 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001962 continue;
1963 case escape_state_inner_escape:
1964 if (utf8.byte[0] == '\\') {
1965 terminal->state = escape_state_normal;
1966 if (terminal->outer_state == escape_state_dcs) {
1967 handle_dcs(terminal);
1968 } else if (terminal->outer_state == escape_state_osc) {
1969 handle_osc(terminal);
1970 }
1971 } else if (utf8.byte[0] == '\e') {
1972 terminal->state = terminal->outer_state;
1973 escape_append_utf8(terminal, utf8);
1974 if (terminal->escape_length >= MAX_ESCAPE)
1975 terminal->state = escape_state_normal;
1976 } else {
1977 terminal->state = terminal->outer_state;
1978 if (terminal->escape_length < MAX_ESCAPE)
1979 terminal->escape[terminal->escape_length++] = '\e';
1980 escape_append_utf8(terminal, utf8);
1981 if (terminal->escape_length >= MAX_ESCAPE)
1982 terminal->state = escape_state_normal;
1983 }
1984 continue;
1985 case escape_state_dcs:
1986 case escape_state_osc:
1987 case escape_state_ignore:
1988 if (utf8.byte[0] == '\e') {
1989 terminal->outer_state = terminal->state;
1990 terminal->state = escape_state_inner_escape;
1991 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
1992 terminal->state = escape_state_normal;
1993 handle_osc(terminal);
1994 } else {
1995 escape_append_utf8(terminal, utf8);
1996 if (terminal->escape_length >= MAX_ESCAPE)
1997 terminal->state = escape_state_normal;
1998 }
1999 continue;
2000 case escape_state_special:
2001 escape_append_utf8(terminal, utf8);
2002 terminal->state = escape_state_normal;
2003 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2004 handle_special_escape(terminal, terminal->escape[1],
2005 utf8.byte[0]);
2006 }
2007 continue;
2008 default:
2009 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002010 }
2011
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002012 /* this is valid, because ASCII characters are never used to
2013 * introduce a multibyte sequence in UTF-8 */
2014 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002015 terminal->state = escape_state_escape;
2016 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002017 terminal->escape[0] = '\e';
2018 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002019 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002020 } else {
2021 handle_char(terminal, utf8);
2022 } /* if */
2023 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002024
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002025 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002026}
2027
2028static void
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002029selection_listener_send(void *data, struct wl_selection *selection,
2030 const char *mime_type, int fd)
2031{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002032 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002033
2034 fprintf(stderr, "selection send, fd is %d\n", fd);
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002035 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002036 close(fd);
2037}
2038
2039static void
2040selection_listener_cancelled(void *data, struct wl_selection *selection)
2041{
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002042 fprintf(stderr, "selection cancelled\n");
2043 wl_selection_destroy(selection);
2044}
2045
2046static const struct wl_selection_listener selection_listener = {
2047 selection_listener_send,
2048 selection_listener_cancelled
2049};
2050
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002051static int
2052handle_bound_key(struct terminal *terminal,
2053 struct input *input, uint32_t sym, uint32_t time)
2054{
2055 struct wl_shell *shell;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002056
2057 switch (sym) {
2058 case XK_C:
2059 shell = display_get_shell(terminal->display);
2060 terminal->selection = wl_shell_create_selection(shell);
2061 wl_selection_add_listener(terminal->selection,
2062 &selection_listener, terminal);
2063 wl_selection_offer(terminal->selection, "text/plain");
2064 wl_selection_activate(terminal->selection,
2065 input_get_input_device(input), time);
2066
2067 return 1;
2068 case XK_V:
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05002069 /* Just pass the master fd of the pty to receive the
2070 * selection. */
2071 if (input_offers_mime_type(input, "text/plain"))
2072 input_receive_mime_type(input, "text/plain",
2073 terminal->master);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002074 return 1;
2075 case XK_X:
2076 /* cut selection; terminal doesn't do cut */
2077 return 0;
2078 default:
2079 return 0;
2080 }
2081}
2082
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002083static void
2084key_handler(struct window *window, struct input *input, uint32_t time,
2085 uint32_t key, uint32_t sym, uint32_t state, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002086{
2087 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002088 char ch[MAX_RESPONSE];
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002089 uint32_t modifiers;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002090 int len = 0;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002091
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002092 modifiers = input_get_modifiers(input);
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002093 if ((modifiers & XKB_COMMON_CONTROL_MASK) &&
2094 (modifiers & XKB_COMMON_SHIFT_MASK) &&
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002095 state && handle_bound_key(terminal, input, sym, 0))
2096 return;
2097
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002098 switch (sym) {
2099 case XK_F11:
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002100 if (!state)
2101 break;
2102 terminal->fullscreen ^= 1;
2103 window_set_fullscreen(window, terminal->fullscreen);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002104 window_schedule_redraw(terminal->window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002105 break;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002106
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002107 case XK_BackSpace:
2108 case XK_Tab:
2109 case XK_Linefeed:
2110 case XK_Clear:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002111 case XK_Pause:
2112 case XK_Scroll_Lock:
2113 case XK_Sys_Req:
2114 case XK_Escape:
2115 ch[len++] = sym & 0x7f;
2116 break;
2117
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002118 case XK_Return:
2119 if (terminal->mode & MODE_LF_NEWLINE) {
2120 ch[len++] = 0x0D;
2121 ch[len++] = 0x0A;
2122 } else {
2123 ch[len++] = 0x0D;
2124 }
2125 break;
2126
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002127 case XK_Shift_L:
2128 case XK_Shift_R:
2129 case XK_Control_L:
2130 case XK_Control_R:
2131 case XK_Alt_L:
2132 case XK_Alt_R:
2133 break;
2134
Callum Lowcay7e08e902011-01-07 19:47:02 +00002135 case XK_Insert:
2136 len = function_key_response('[', 2, modifiers, '~', ch);
2137 break;
2138 case XK_Delete:
2139 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2140 ch[len++] = '\x04';
2141 } else {
2142 len = function_key_response('[', 3, modifiers, '~', ch);
2143 }
2144 break;
2145 case XK_Page_Up:
2146 len = function_key_response('[', 5, modifiers, '~', ch);
2147 break;
2148 case XK_Page_Down:
2149 len = function_key_response('[', 6, modifiers, '~', ch);
2150 break;
2151 case XK_F1:
2152 len = function_key_response('O', 1, modifiers, 'P', ch);
2153 break;
2154 case XK_F2:
2155 len = function_key_response('O', 1, modifiers, 'Q', ch);
2156 break;
2157 case XK_F3:
2158 len = function_key_response('O', 1, modifiers, 'R', ch);
2159 break;
2160 case XK_F4:
2161 len = function_key_response('O', 1, modifiers, 'S', ch);
2162 break;
2163 case XK_F5:
2164 len = function_key_response('[', 15, modifiers, '~', ch);
2165 break;
2166 case XK_F6:
2167 len = function_key_response('[', 17, modifiers, '~', ch);
2168 break;
2169 case XK_F7:
2170 len = function_key_response('[', 18, modifiers, '~', ch);
2171 break;
2172 case XK_F8:
2173 len = function_key_response('[', 19, modifiers, '~', ch);
2174 break;
2175 case XK_F9:
2176 len = function_key_response('[', 20, modifiers, '~', ch);
2177 break;
2178 case XK_F10:
2179 len = function_key_response('[', 21, modifiers, '~', ch);
2180 break;
2181 case XK_F12:
2182 len = function_key_response('[', 24, modifiers, '~', ch);
2183 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002184 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002185 /* Handle special keys with alternate mappings */
2186 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2187 if (len != 0) break;
2188
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002189 if (modifiers & XKB_COMMON_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002190 if (sym >= '3' && sym <= '7')
2191 sym = (sym & 0x1f) + 8;
2192
2193 if (!((sym >= '!' && sym <= '/') ||
2194 (sym >= '8' && sym <= '?') ||
2195 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2196 else if (sym == '2') sym = 0x00;
2197 else if (sym == '/') sym = 0x1F;
2198 else if (sym == '8' || sym == '?') sym = 0x7F;
2199 } else if ((terminal->mode & MODE_ALT_SENDS_ESC) &&
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002200 (modifiers & XKB_COMMON_MOD1_MASK))
Callum Lowcay7e08e902011-01-07 19:47:02 +00002201 {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002202 ch[len++] = 0x1b;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002203 } else if (modifiers & XKB_COMMON_MOD1_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002204 sym = sym | 0x80;
2205 }
2206
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002207 if (sym < 256)
2208 ch[len++] = sym;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002209 break;
2210 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002211
2212 if (state && len > 0)
Kristian Høgsberg26130862011-08-24 11:30:21 -04002213 terminal_write(terminal, ch, len);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002214}
2215
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002216static void
2217keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002218 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002219{
2220 struct terminal *terminal = data;
2221
2222 terminal->focused = (device != NULL);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002223 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002224}
2225
Kristian Høgsberg59826582011-01-20 11:56:57 -05002226static void
2227button_handler(struct window *window,
2228 struct input *input, uint32_t time,
2229 int button, int state, void *data)
2230{
2231 struct terminal *terminal = data;
2232
2233 switch (button) {
2234 case 272:
2235 if (state) {
2236 terminal->dragging = 1;
2237 terminal->selection_active = 0;
2238 input_get_position(input,
2239 &terminal->selection_start_x,
2240 &terminal->selection_start_y);
2241 terminal->selection_end_x = terminal->selection_start_x;
2242 terminal->selection_end_y = terminal->selection_start_y;
2243 window_schedule_redraw(window);
2244 } else {
2245 terminal->dragging = 0;
2246 }
2247 break;
2248 }
2249}
2250
2251static int
2252motion_handler(struct window *window,
2253 struct input *input, uint32_t time,
2254 int32_t x, int32_t y,
2255 int32_t sx, int32_t sy, void *data)
2256{
2257 struct terminal *terminal = data;
2258
2259 if (terminal->dragging) {
2260 terminal->selection_active = 1;
2261 input_get_position(input,
2262 &terminal->selection_end_x,
2263 &terminal->selection_end_y);
2264 window_schedule_redraw(window);
2265 }
2266
2267 return POINTER_IBEAM;
2268}
2269
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002270static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002271terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002272{
2273 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002274 cairo_surface_t *surface;
2275 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002276
2277 terminal = malloc(sizeof *terminal);
2278 if (terminal == NULL)
2279 return terminal;
2280
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002281 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002282 terminal->fullscreen = fullscreen;
Callum Lowcay30eeae52011-01-07 19:46:55 +00002283 terminal->color_scheme = &DEFAULT_COLORS;
2284 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002285 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002286 terminal->margin_bottom = -1;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002287 terminal->window = window_create(display, 500, 400);
2288 window_set_title(terminal->window, "Wayland Terminal");
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002289
2290 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002291 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002292
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002293 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002294 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002295
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002296 window_set_user_data(terminal->window, terminal);
2297 window_set_redraw_handler(terminal->window, redraw_handler);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05002298 window_set_resize_handler(terminal->window, resize_handler);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002299
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002300 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002301 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002302 keyboard_focus_handler);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002303 window_set_button_handler(terminal->window, button_handler);
2304 window_set_motion_handler(terminal->window, motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002305
Kristian Høgsberg09531622010-06-14 23:22:15 -04002306 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2307 cr = cairo_create(surface);
Kristian Høgsberg09531622010-06-14 23:22:15 -04002308 cairo_set_font_size(cr, 14);
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002309 cairo_select_font_face (cr, "mono",
2310 CAIRO_FONT_SLANT_NORMAL,
2311 CAIRO_FONT_WEIGHT_BOLD);
2312 terminal->font_bold = cairo_get_scaled_font (cr);
2313 cairo_scaled_font_reference(terminal->font_bold);
2314
2315 cairo_select_font_face (cr, "mono",
2316 CAIRO_FONT_SLANT_NORMAL,
2317 CAIRO_FONT_WEIGHT_NORMAL);
2318 terminal->font_normal = cairo_get_scaled_font (cr);
2319 cairo_scaled_font_reference(terminal->font_normal);
2320
Kristian Høgsberg09531622010-06-14 23:22:15 -04002321 cairo_font_extents(cr, &terminal->extents);
2322 cairo_destroy(cr);
2323 cairo_surface_destroy(surface);
2324
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002325 return terminal;
2326}
2327
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002328static void
2329io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002330{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002331 struct terminal *terminal =
2332 container_of(task, struct terminal, io_task);
2333 char buffer[256];
2334 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002335
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002336 if (events & EPOLLHUP)
2337 exit(0);
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002338
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002339 len = read(terminal->master, buffer, sizeof buffer);
2340 if (len < 0)
2341 exit(0);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002342
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002343 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002344}
2345
2346static int
2347terminal_run(struct terminal *terminal, const char *path)
2348{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002349 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002350 pid_t pid;
2351
2352 pid = forkpty(&master, NULL, NULL, NULL);
2353 if (pid == 0) {
Callum Lowcay86653ed2011-01-07 19:47:03 +00002354 setenv("TERM", "xterm-256color", 1);
2355 setenv("COLORTERM", "xterm-256color", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002356 if (execl(path, path, NULL)) {
2357 printf("exec failed: %m\n");
2358 exit(EXIT_FAILURE);
2359 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002360 } else if (pid < 0) {
2361 fprintf(stderr, "failed to fork and create pty (%m).\n");
2362 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002363 }
2364
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002365 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002366 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002367 terminal->io_task.run = io_handler;
2368 display_watch_fd(terminal->display, terminal->master,
2369 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002370
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002371 window_set_fullscreen(terminal->window, terminal->fullscreen);
2372 if (!terminal->fullscreen)
2373 terminal_resize(terminal, 80, 24);
2374
Kristian Høgsberg54b86832011-06-21 16:31:11 -04002375 terminal_draw(terminal);
2376
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002377 return 0;
2378}
2379
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002380static const GOptionEntry option_entries[] = {
2381 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
2382 &option_fullscreen, "Run in fullscreen mode" },
2383 { NULL }
2384};
2385
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002386int main(int argc, char *argv[])
2387{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002388 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002389 struct terminal *terminal;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002390
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002391 d = display_create(&argc, &argv, option_entries);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002392 if (d == NULL) {
2393 fprintf(stderr, "failed to create display: %m\n");
2394 return -1;
2395 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002396
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002397 terminal = terminal_create(d, option_fullscreen);
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002398 if (terminal_run(terminal, "/bin/bash"))
2399 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002400
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002401 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002402
2403 return 0;
2404}