blob: 523358a68cb6bdb039dffc0736257cf1e07f38b5 [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>
35
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040036#include <X11/keysym.h>
37
Kristian Høgsberg12308a42009-09-28 13:08:50 -040038#include "wayland-util.h"
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050039#include "wayland-client.h"
40#include "wayland-glib.h"
41
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050042#include "window.h"
43
Kristian Høgsberg0395f302008-12-22 12:14:50 -050044static int option_fullscreen;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050045
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050046#define MOD_SHIFT 0x01
47#define MOD_ALT 0x02
48#define MOD_CTRL 0x04
49
Callum Lowcay30eeae52011-01-07 19:46:55 +000050#define ATTRMASK_BOLD 0x01
51#define ATTRMASK_UNDERLINE 0x02
52#define ATTRMASK_BLINK 0x04
53#define ATTRMASK_INVERSE 0x08
Callum Lowcay81179db2011-01-10 12:14:01 +130054#define ATTRMASK_CONCEALED 0x10
Callum Lowcay30eeae52011-01-07 19:46:55 +000055
Callum Lowcayb8609ad2011-01-07 19:46:57 +000056/* Buffer sizes */
Callum Lowcayef57a9b2011-01-14 20:46:23 +130057#define MAX_RESPONSE 256
58#define MAX_ESCAPE 255
Callum Lowcayb8609ad2011-01-07 19:46:57 +000059
Callum Lowcay8e57dd52011-01-07 19:46:59 +000060/* Terminal modes */
61#define MODE_SHOW_CURSOR 0x00000001
62#define MODE_INVERSE 0x00000002
63#define MODE_AUTOWRAP 0x00000004
64#define MODE_AUTOREPEAT 0x00000008
65#define MODE_LF_NEWLINE 0x00000010
Callum Lowcay69e96582011-01-07 19:47:00 +000066#define MODE_IRM 0x00000020
Callum Lowcay7e08e902011-01-07 19:47:02 +000067#define MODE_DELETE_SENDS_DEL 0x00000040
68#define MODE_ALT_SENDS_ESC 0x00000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +000069
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000070union utf8_char {
71 unsigned char byte[4];
72 uint32_t ch;
73};
74
75enum utf8_state {
76 utf8state_start,
77 utf8state_accept,
78 utf8state_reject,
79 utf8state_expect3,
80 utf8state_expect2,
81 utf8state_expect1
82};
83
84struct utf8_state_machine {
85 enum utf8_state state;
86 int len;
87 union utf8_char s;
88};
89
90static void
91init_state_machine(struct utf8_state_machine *machine)
92{
93 machine->state = utf8state_start;
94 machine->len = 0;
95 machine->s.ch = 0;
96}
97
98static enum utf8_state
99utf8_next_char(struct utf8_state_machine *machine, char c)
100{
101 switch(machine->state) {
102 case utf8state_start:
103 case utf8state_accept:
104 case utf8state_reject:
105 machine->s.ch = 0;
106 machine->len = 0;
107 if(c == 0xC0 || c == 0xC1) {
108 /* overlong encoding, reject */
109 machine->state = utf8state_reject;
110 } else if((c & 0x80) == 0) {
111 /* single byte, accept */
112 machine->s.byte[machine->len++] = c;
113 machine->state = utf8state_accept;
114 } else if((c & 0xC0) == 0x80) {
115 /* parser out of sync, ignore byte */
116 machine->state = utf8state_start;
117 } else if((c & 0xE0) == 0xC0) {
118 /* start of two byte sequence */
119 machine->s.byte[machine->len++] = c;
120 machine->state = utf8state_expect1;
121 } else if((c & 0xF0) == 0xE0) {
122 /* start of three byte sequence */
123 machine->s.byte[machine->len++] = c;
124 machine->state = utf8state_expect2;
125 } else if((c & 0xF8) == 0xF0) {
126 /* start of four byte sequence */
127 machine->s.byte[machine->len++] = c;
128 machine->state = utf8state_expect3;
129 } else {
130 /* overlong encoding, reject */
131 machine->state = utf8state_reject;
132 }
133 break;
134 case utf8state_expect3:
135 machine->s.byte[machine->len++] = c;
136 if((c & 0xC0) == 0x80) {
137 /* all good, continue */
138 machine->state = utf8state_expect2;
139 } else {
140 /* missing extra byte, reject */
141 machine->state = utf8state_reject;
142 }
143 break;
144 case utf8state_expect2:
145 machine->s.byte[machine->len++] = c;
146 if((c & 0xC0) == 0x80) {
147 /* all good, continue */
148 machine->state = utf8state_expect1;
149 } else {
150 /* missing extra byte, reject */
151 machine->state = utf8state_reject;
152 }
153 break;
154 case utf8state_expect1:
155 machine->s.byte[machine->len++] = c;
156 if((c & 0xC0) == 0x80) {
157 /* all good, accept */
158 machine->state = utf8state_accept;
159 } else {
160 /* missing extra byte, reject */
161 machine->state = utf8state_reject;
162 }
163 break;
164 default:
165 machine->state = utf8state_reject;
166 break;
167 }
168
169 return machine->state;
170}
171
Callum Lowcay256e72f2011-01-07 19:47:01 +0000172struct char_sub {
173 union utf8_char match;
174 union utf8_char replace;
175};
176/* Set last char_sub match to NULL char */
177typedef struct char_sub *character_set;
178
179struct char_sub CS_US[] = {
180 {{{0, }}, {{0, }}}
181};
182static struct char_sub CS_UK[] = {
183 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}},
184 {{{0, }}, {{0, }}}
185};
186static struct char_sub CS_SPECIAL[] = {
187 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond */
188 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell */
189 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT */
190 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF */
191 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR */
192 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF */
193 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree */
194 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus */
195 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL */
196 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT */
197 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB */
198 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT */
199 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT */
200 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_RB */
201 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS */
202 {{{'o', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
203 {{{'p', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
204 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
205 {{{'r', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
206 {{{'s', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
207 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR */
208 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL */
209 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU */
210 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD */
211 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V */
212 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE */
213 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE */
214 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI */
215 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ */
216 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND */
217 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT */
218 {{{0, }}, {{0, }}}
219};
220
221static void
222apply_char_set(character_set cs, union utf8_char *utf8)
223{
224 int i = 0;
225
226 while (cs[i].match.byte[0]) {
227 if ((*utf8).ch == cs[i].match.ch) {
228 *utf8 = cs[i].replace;
229 break;
230 }
231 i++;
232 }
233}
234
Callum Lowcay7e08e902011-01-07 19:47:02 +0000235struct key_map {
236 int sym;
237 int num;
238 char escape;
239 char code;
240};
241/* Set last key_sub sym to NULL */
242typedef struct key_map *keyboard_mode;
243
244static struct key_map KM_NORMAL[] = {
245 {XK_Left, 1, '[', 'D'},
246 {XK_Right, 1, '[', 'C'},
247 {XK_Up, 1, '[', 'A'},
248 {XK_Down, 1, '[', 'B'},
249 {XK_Home, 1, '[', 'H'},
250 {XK_End, 1, '[', 'F'},
251 {0, 0, 0, 0}
252};
253static struct key_map KM_APPLICATION[] = {
254 {XK_Left, 1, 'O', 'D'},
255 {XK_Right, 1, 'O', 'C'},
256 {XK_Up, 1, 'O', 'A'},
257 {XK_Down, 1, 'O', 'B'},
258 {XK_Home, 1, 'O', 'H'},
259 {XK_End, 1, 'O', 'F'},
260 {XK_KP_Enter, 1, 'O', 'M'},
261 {XK_KP_Multiply, 1, 'O', 'j'},
262 {XK_KP_Add, 1, 'O', 'k'},
263 {XK_KP_Separator, 1, 'O', 'l'},
264 {XK_KP_Subtract, 1, 'O', 'm'},
265 {XK_KP_Divide, 1, 'O', 'o'},
266 {0, 0, 0, 0}
267};
268
269static int
270function_key_response(char escape, int num, uint32_t modifiers,
271 char code, char *response)
272{
273 int mod_num = 0;
274 int len;
275
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -0500276 if (modifiers & XKB_COMMON_SHIFT_MASK) mod_num |= 1;
277 if (modifiers & XKB_COMMON_MOD1_MASK) mod_num |= 2;
278 if (modifiers & XKB_COMMON_CONTROL_MASK) mod_num |= 4;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000279
280 if (mod_num != 0)
281 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
282 num, mod_num + 1, code);
283 else if (code != '~')
284 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
285 escape, code);
286 else
287 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
288 escape, num, code);
289
290 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
291 else return len;
292}
293
294/* returns the number of bytes written into response,
295 * which must have room for MAX_RESPONSE bytes */
296static int
297apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
298{
299 struct key_map map;
300 int len = 0;
301 int i = 0;
302
303 while (mode[i].sym) {
304 map = mode[i++];
305 if (sym == map.sym) {
306 len = function_key_response(map.escape, map.num,
307 modifiers, map.code,
308 response);
309 break;
310 }
311 }
312
313 return len;
314}
315
Callum Lowcay30eeae52011-01-07 19:46:55 +0000316struct terminal_color { double r, g, b, a; };
317struct attr {
318 unsigned char fg, bg;
319 char a; /* attributes format:
320 * 76543210
Callum Lowcay81179db2011-01-10 12:14:01 +1300321 * cilub */
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500322 char s; /* in selection */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000323};
324struct color_scheme {
325 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500326 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000327 struct attr default_attr;
328};
329
330static void
331attr_init(struct attr *data_attr, struct attr attr, int n)
332{
333 int i;
334 for (i = 0; i < n; i++) {
335 data_attr[i] = attr;
336 }
337}
338
Callum Lowcay67a201d2011-01-12 19:23:41 +1300339enum escape_state {
340 escape_state_normal = 0,
341 escape_state_escape,
342 escape_state_dcs,
343 escape_state_csi,
344 escape_state_osc,
345 escape_state_inner_escape,
346 escape_state_ignore,
347 escape_state_special
348};
349
350#define ESC_FLAG_WHAT 0x01
351#define ESC_FLAG_GT 0x02
352#define ESC_FLAG_BANG 0x04
353#define ESC_FLAG_CASH 0x08
354#define ESC_FLAG_SQUOTE 0x10
355#define ESC_FLAG_DQUOTE 0x20
356#define ESC_FLAG_SPACE 0x40
357
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500358struct terminal {
359 struct window *window;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500360 struct display *display;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000361 union utf8_char *data;
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øgsberg269d6e32008-12-07 23:17:31 -0500378 GIOChannel *channel;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500379 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300380 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500381 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300382 enum escape_state state;
383 enum escape_state outer_state;
384 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000385 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500386 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500387 int fullscreen;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500388 int focused;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400389 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000390 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400391 cairo_font_extents_t extents;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500392 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500393
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500394 struct wl_selection *selection;
395 struct wl_selection_offer *selection_offer;
396 uint32_t selection_offer_has_text;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500397 int32_t dragging, selection_active;
398 int selection_start_x, selection_start_y;
399 int selection_end_x, selection_end_y;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500400};
401
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000402/* Create default tab stops, every 8 characters */
403static void
404terminal_init_tabs(struct terminal *terminal)
405{
406 int i = 0;
407
408 while (i < terminal->width) {
409 if (i % 8 == 0)
410 terminal->tab_ruler[i] = 1;
411 else
412 terminal->tab_ruler[i] = 0;
413 i++;
414 }
415}
416
Callum Lowcay30eeae52011-01-07 19:46:55 +0000417static void
418terminal_init(struct terminal *terminal)
419{
420 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000421 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000422 terminal->mode = MODE_SHOW_CURSOR |
423 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000424 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000425 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000426
427 terminal->row = 0;
428 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000429
Callum Lowcay256e72f2011-01-07 19:47:01 +0000430 terminal->g0 = CS_US;
431 terminal->g1 = CS_US;
432 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000433 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000434
435 terminal->saved_g0 = terminal->g0;
436 terminal->saved_g1 = terminal->g1;
437 terminal->saved_cs = terminal->cs;
438
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000439 terminal->saved_attr = terminal->curr_attr;
440 terminal->saved_origin_mode = terminal->origin_mode;
441 terminal->saved_row = terminal->row;
442 terminal->saved_column = terminal->column;
443
444 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000445}
446
447static void
448init_color_table(struct terminal *terminal)
449{
450 int c, r;
451 struct terminal_color *color_table = terminal->color_table;
452
453 for (c = 0; c < 256; c ++) {
454 if (c < 16) {
455 color_table[c] = terminal->color_scheme->palette[c];
456 } else if (c < 232) {
457 r = c - 16;
458 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
459 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
460 color_table[c].r = ((double)(r % 6) / 6.0);
461 color_table[c].a = 1.0;
462 } else {
463 r = (c - 232) * 10 + 8;
464 color_table[c].r = ((double) r) / 256.0;
465 color_table[c].g = color_table[c].r;
466 color_table[c].b = color_table[c].r;
467 color_table[c].a = 1.0;
468 }
469 }
470}
471
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000472static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500473terminal_get_row(struct terminal *terminal, int row)
474{
475 int index;
476
477 index = (row + terminal->start) % terminal->height;
478
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000479 return &terminal->data[index * terminal->width];
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500480}
481
Callum Lowcay30eeae52011-01-07 19:46:55 +0000482static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500483terminal_get_attr_row(struct terminal *terminal, int row)
484{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000485 int index;
486
487 index = (row + terminal->start) % terminal->height;
488
489 return &terminal->data_attr[index * terminal->width];
490}
491
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500492union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300493 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500494 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500495};
496
Kristian Høgsberg59826582011-01-20 11:56:57 -0500497static int
498terminal_compare_position(struct terminal *terminal,
499 int x, int y, int32_t ref_row, int32_t ref_col)
500{
501 struct rectangle allocation;
502 int top_margin, side_margin, col, row, ref_x;
503
504 window_get_child_allocation(terminal->window, &allocation);
505 side_margin = allocation.x + (allocation.width - terminal->width * terminal->extents.max_x_advance) / 2;
506 top_margin = allocation.y + (allocation.height - terminal->height * terminal->extents.height) / 2;
507
508 col = (x - side_margin) / terminal->extents.max_x_advance;
509 row = (y - top_margin) / terminal->extents.height;
510
511 ref_x = side_margin + ref_col * terminal->extents.max_x_advance +
512 terminal->extents.max_x_advance / 2;
513
514 if (row < ref_row)
515 return -1;
516 if (row == ref_row) {
517 if (col < ref_col)
518 return -1;
519 if (col == ref_col && x < ref_x)
520 return -1;
521 }
522
523 return 1;
524}
525
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500526static void
527terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500528 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500529{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500530 struct attr attr;
531 int foreground, background, tmp;
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500532 int start_cmp, end_cmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500533
534 start_cmp =
535 terminal_compare_position(terminal,
536 terminal->selection_start_x,
537 terminal->selection_start_y,
538 row, col);
539 end_cmp =
540 terminal_compare_position(terminal,
541 terminal->selection_end_x,
542 terminal->selection_end_y,
543 row, col);
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500544 decoded->attr.s = 0;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500545 if (start_cmp < 0 && end_cmp > 0)
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500546 decoded->attr.s = 1;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500547 else if (end_cmp < 0 && start_cmp > 0)
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500548 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500549
550 /* get the attributes for this character cell */
551 attr = terminal_get_attr_row(terminal, row)[col];
552 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500553 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500554 ((terminal->mode & MODE_SHOW_CURSOR) &&
555 terminal->focused && terminal->row == row &&
556 terminal->column == col)) {
557 foreground = attr.bg;
558 background = attr.fg;
559 if (attr.a & ATTRMASK_BOLD) {
560 if (foreground <= 16) foreground |= 0x08;
561 if (background <= 16) background &= 0x07;
562 }
563 } else {
564 foreground = attr.fg;
565 background = attr.bg;
566 }
567
568 if (terminal->mode & MODE_INVERSE) {
569 tmp = foreground;
570 foreground = background;
571 background = tmp;
572 if (attr.a & ATTRMASK_BOLD) {
573 if (foreground <= 16) foreground |= 0x08;
574 if (background <= 16) background &= 0x07;
575 }
576 }
577
Callum Lowcay9d708b02011-01-12 20:06:17 +1300578 decoded->attr.fg = foreground;
579 decoded->attr.bg = background;
580 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000581}
582
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500583
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500584static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000585terminal_scroll_buffer(struct terminal *terminal, int d)
586{
587 int i;
588
589 d = d % (terminal->height + 1);
590 terminal->start = (terminal->start + d) % terminal->height;
591 if (terminal->start < 0) terminal->start = terminal->height + terminal->start;
592 if(d < 0) {
593 d = 0 - d;
594 for(i = 0; i < d; i++) {
595 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
596 attr_init(terminal_get_attr_row(terminal, i),
597 terminal->curr_attr, terminal->width);
598 }
599 } else {
600 for(i = terminal->height - d; i < terminal->height; i++) {
601 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
602 attr_init(terminal_get_attr_row(terminal, i),
603 terminal->curr_attr, terminal->width);
604 }
605 }
606}
607
608static void
609terminal_scroll_window(struct terminal *terminal, int d)
610{
611 int i;
612 int window_height;
613 int from_row, to_row;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000614
615 // scrolling range is inclusive
616 window_height = terminal->margin_bottom - terminal->margin_top + 1;
617 d = d % (window_height + 1);
618 if(d < 0) {
619 d = 0 - d;
620 to_row = terminal->margin_bottom;
621 from_row = terminal->margin_bottom - d;
622
623 for (i = 0; i < (window_height - d); i++) {
624 memcpy(terminal_get_row(terminal, to_row - i),
625 terminal_get_row(terminal, from_row - i),
626 terminal->data_pitch);
627 memcpy(terminal_get_attr_row(terminal, to_row - i),
628 terminal_get_attr_row(terminal, from_row - i),
629 terminal->attr_pitch);
630 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000631 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
632 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000633 attr_init(terminal_get_attr_row(terminal, i),
634 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000635 }
636 } else {
637 to_row = terminal->margin_top;
638 from_row = terminal->margin_top + d;
639
640 for (i = 0; i < (window_height - d); i++) {
641 memcpy(terminal_get_row(terminal, to_row + i),
642 terminal_get_row(terminal, from_row + i),
643 terminal->data_pitch);
644 memcpy(terminal_get_attr_row(terminal, to_row + i),
645 terminal_get_attr_row(terminal, from_row + i),
646 terminal->attr_pitch);
647 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000648 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
649 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000650 attr_init(terminal_get_attr_row(terminal, i),
651 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000652 }
653 }
654}
655
656static void
657terminal_scroll(struct terminal *terminal, int d)
658{
659 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
660 terminal_scroll_buffer(terminal, d);
661 else
662 terminal_scroll_window(terminal, d);
663}
664
665static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000666terminal_shift_line(struct terminal *terminal, int d)
667{
668 union utf8_char *row;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500669 struct attr *attr_row;
Callum Lowcay69e96582011-01-07 19:47:00 +0000670
671 row = terminal_get_row(terminal, terminal->row);
672 attr_row = terminal_get_attr_row(terminal, terminal->row);
673
674 if ((terminal->width + d) <= terminal->column)
675 d = terminal->column + 1 - terminal->width;
676 if ((terminal->column + d) >= terminal->width)
677 d = terminal->width - terminal->column - 1;
678
679 if (d < 0) {
680 d = 0 - d;
681 memmove(&row[terminal->column],
682 &row[terminal->column + d],
683 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
Callum Lowcay69e96582011-01-07 19:47:00 +0000684 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
685 (terminal->width - terminal->column - d) * sizeof(struct attr));
686 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
687 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
688 } else {
689 memmove(&row[terminal->column + d], &row[terminal->column],
690 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
691 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
692 (terminal->width - terminal->column - d) * sizeof(struct attr));
693 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
694 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
695 }
696}
697
698static void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500699terminal_resize(struct terminal *terminal, int width, int height)
700{
701 size_t size;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000702 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000703 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000704 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000705 int data_pitch, attr_pitch;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500706 int i, l, total_rows;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500707 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000708 struct winsize ws;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500709 int32_t pixel_width, pixel_height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500710
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500711 if (width < 1)
712 width = 1;
713 if (height < 1)
714 height = 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500715 if (terminal->width == width && terminal->height == height)
716 return;
717
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500718 if (!terminal->fullscreen) {
719 pixel_width = width *
720 terminal->extents.max_x_advance + 2 * terminal->margin;
721 pixel_height = height *
722 terminal->extents.height + 2 * terminal->margin;
723 window_set_child_size(terminal->window,
724 pixel_width, pixel_height);
725 }
726
727 window_schedule_redraw (terminal->window);
728
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000729 data_pitch = width * sizeof(union utf8_char);
730 size = data_pitch * height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500731 data = malloc(size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000732 attr_pitch = width * sizeof(struct attr);
733 data_attr = malloc(attr_pitch * height);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000734 tab_ruler = malloc(width);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500735 memset(data, 0, size);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000736 memset(tab_ruler, 0, width);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000737 attr_init(data_attr, terminal->curr_attr, width * height);
738 if (terminal->data && terminal->data_attr) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500739 if (width > terminal->width)
740 l = terminal->width;
741 else
742 l = width;
743
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500744 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500745 total_rows = height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500746 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500747 total_rows = terminal->height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500748 }
749
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000750 for (i = 0; i < total_rows; i++) {
751 memcpy(&data[width * i],
752 terminal_get_row(terminal, i),
753 l * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000754 memcpy(&data_attr[width * i],
755 terminal_get_attr_row(terminal, i),
756 l * sizeof(struct attr));
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000757 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500758
759 free(terminal->data);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000760 free(terminal->data_attr);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000761 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500762 }
763
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000764 terminal->data_pitch = data_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000765 terminal->attr_pitch = attr_pitch;
Callum Lowcay86653ed2011-01-07 19:47:03 +0000766 terminal->margin_bottom =
767 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500768 terminal->width = width;
769 terminal->height = height;
770 terminal->data = data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000771 terminal->data_attr = data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000772 terminal->tab_ruler = tab_ruler;
773 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500774
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000775 /* Update the window size */
776 ws.ws_row = terminal->height;
777 ws.ws_col = terminal->width;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500778 window_get_child_allocation(terminal->window, &allocation);
779 ws.ws_xpixel = allocation.width;
780 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000781 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500782}
783
Callum Lowcay30eeae52011-01-07 19:46:55 +0000784struct color_scheme DEFAULT_COLORS = {
785 {
786 {0, 0, 0, 1}, /* black */
787 {0.66, 0, 0, 1}, /* red */
788 {0 , 0.66, 0, 1}, /* green */
789 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
790 {0 , 0 , 0.66, 1}, /* blue */
791 {0.66, 0 , 0.66, 1}, /* magenta */
792 {0, 0.66, 0.66, 1}, /* cyan */
793 {0.66, 0.66, 0.66, 1}, /* light grey */
794 {0.22, 0.33, 0.33, 1}, /* dark grey */
795 {1, 0.33, 0.33, 1}, /* high red */
796 {0.33, 1, 0.33, 1}, /* high green */
797 {1, 1, 0.33, 1}, /* high yellow */
798 {0.33, 0.33, 1, 1}, /* high blue */
799 {1, 0.33, 1, 1}, /* high magenta */
800 {0.33, 1, 1, 1}, /* high cyan */
801 {1, 1, 1, 1} /* white */
802 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500803 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000804 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
805};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400806
Kristian Høgsberg22106762008-12-08 13:50:07 -0500807static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500808terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
809{
810 cairo_set_source_rgba(cr,
811 terminal->color_table[index].r,
812 terminal->color_table[index].g,
813 terminal->color_table[index].b,
814 terminal->color_table[index].a);
815}
816
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500817static void
818terminal_send_selection(struct terminal *terminal, int fd)
819{
820 int row, col;
821 union utf8_char *p_row;
822 union decoded_attr attr;
823 FILE *fp;
824 int len;
825
826 fp = fdopen(fd, "w");
827 for (row = 0; row < terminal->height; row++) {
828 p_row = terminal_get_row(terminal, row);
829 for (col = 0; col < terminal->width; col++) {
830 /* get the attributes for this character cell */
831 terminal_decode_attr(terminal, row, col, &attr);
832 if (!attr.attr.s)
833 continue;
834 len = strnlen((char *) p_row[col].byte, 4);
835 fwrite(p_row[col].byte, 1, len, fp);
836 }
837 }
838 fclose(fp);
839}
840
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500841struct glyph_run {
842 struct terminal *terminal;
843 cairo_t *cr;
844 int count;
845 union decoded_attr attr;
846 cairo_glyph_t glyphs[256], *g;
847};
848
849static void
850glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
851{
852 run->terminal = terminal;
853 run->cr = cr;
854 run->g = run->glyphs;
855 run->count = 0;
856 run->attr.key = 0;
857}
858
859static void
860glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
861{
862 cairo_scaled_font_t *font;
863
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500864 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
865 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300866 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500867 font = run->terminal->font_bold;
868 else
869 font = run->terminal->font_normal;
870 cairo_set_scaled_font(run->cr, font);
871 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300872 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500873
Callum Lowcay9d708b02011-01-12 20:06:17 +1300874 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
875 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500876 run->g = run->glyphs;
877 run->count = 0;
878 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300879 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500880}
881
882static void
883glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
884{
885 int num_glyphs;
886 cairo_scaled_font_t *font;
887
888 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
889
Callum Lowcay9d708b02011-01-12 20:06:17 +1300890 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500891 font = run->terminal->font_bold;
892 else
893 font = run->terminal->font_normal;
894
895 cairo_move_to(run->cr, x, y);
896 cairo_scaled_font_text_to_glyphs (font, x, y,
897 (char *) c->byte, 4,
898 &run->g, &num_glyphs,
899 NULL, NULL, NULL);
900 run->g += num_glyphs;
901 run->count += num_glyphs;
902}
903
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500904static void
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500905terminal_draw_contents(struct terminal *terminal)
906{
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500907 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500908 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000909 int top_margin, side_margin;
910 int row, col;
911 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500912 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000913 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500914 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500915 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500916 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500917 cairo_font_extents_t extents;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500918
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500919 surface = window_get_surface(terminal->window);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500920 window_get_child_allocation(terminal->window, &allocation);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500921 cr = cairo_create(surface);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500922 cairo_rectangle(cr, allocation.x, allocation.y,
923 allocation.width, allocation.height);
924 cairo_clip(cr);
925 cairo_push_group(cr);
926
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500927 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500928 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500929 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500930
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500931 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500932
Kristian Høgsberg59826582011-01-20 11:56:57 -0500933 extents = terminal->extents;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500934 side_margin = (allocation.width - terminal->width * extents.max_x_advance) / 2;
935 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500936
Callum Lowcay30eeae52011-01-07 19:46:55 +0000937 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500938 cairo_translate(cr, allocation.x + side_margin,
939 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500940 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000941 for (row = 0; row < terminal->height; row++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000942 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000943 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500944 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000945
Callum Lowcay9d708b02011-01-12 20:06:17 +1300946 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -0500947 continue;
948
Callum Lowcay9d708b02011-01-12 20:06:17 +1300949 terminal_set_color(terminal, cr, attr.attr.bg);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500950 cairo_move_to(cr, col * extents.max_x_advance,
951 row * extents.height);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000952 cairo_rel_line_to(cr, extents.max_x_advance, 0);
953 cairo_rel_line_to(cr, 0, extents.height);
954 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
955 cairo_close_path(cr);
956 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500957 }
958 }
Callum Lowcay30eeae52011-01-07 19:46:55 +0000959
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500960 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
961
962 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500963 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500964 for (row = 0; row < terminal->height; row++) {
965 p_row = terminal_get_row(terminal, row);
966 for (col = 0; col < terminal->width; col++) {
967 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500968 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500969
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500970 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000971
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500972 text_x = col * extents.max_x_advance;
973 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +1300974 if (attr.attr.a & ATTRMASK_UNDERLINE) {
975 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000976 cairo_move_to(cr, text_x, (double)text_y + 1.5);
977 cairo_line_to(cr, text_x + extents.max_x_advance, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000978 cairo_stroke(cr);
979 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -0500980
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500981 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000982 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500983 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500984
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500985 attr.key = ~0;
986 glyph_run_flush(&run, attr);
987
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000988 if ((terminal->mode & MODE_SHOW_CURSOR) && !terminal->focused) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000989 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500990
Callum Lowcay30eeae52011-01-07 19:46:55 +0000991 cairo_set_line_width(cr, 1);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500992 cairo_move_to(cr, terminal->column * extents.max_x_advance + d,
993 terminal->row * extents.height + d);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000994 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
995 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
996 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
997 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500998
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500999 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001000 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001001
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001002 cairo_pop_group_to_source(cr);
1003 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001004 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001005 cairo_surface_destroy(surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001006}
1007
1008static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001009terminal_write(struct terminal *terminal, const char *data, size_t length)
1010{
1011 if (write(terminal->master, data, length) < 0)
1012 abort();
1013}
1014
1015static void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001016resize_handler(struct window *window,
1017 int32_t pixel_width, int32_t pixel_height, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001018{
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001019 struct terminal *terminal = data;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001020 int32_t width, height;
Kristian Høgsberg22106762008-12-08 13:50:07 -05001021
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001022 width = (pixel_width - 2 * terminal->margin) /
Kristian Høgsberg09531622010-06-14 23:22:15 -04001023 (int32_t) terminal->extents.max_x_advance;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001024 height = (pixel_height - 2 * terminal->margin) /
Kristian Høgsberg09531622010-06-14 23:22:15 -04001025 (int32_t) terminal->extents.height;
Tiago Vignatti5fd89d22011-01-10 19:30:04 +02001026
Kristian Høgsberg0ce24572011-01-28 15:18:33 -05001027 if (terminal->fullscreen)
1028 window_set_child_size(terminal->window,
1029 pixel_width, pixel_height);
1030
Kristian Høgsberg22106762008-12-08 13:50:07 -05001031 terminal_resize(terminal, width, height);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001032}
Kristian Høgsberg22106762008-12-08 13:50:07 -05001033
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001034static void
1035terminal_draw(struct terminal *terminal)
1036{
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001037 window_draw(terminal->window);
1038 terminal_draw_contents(terminal);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001039 window_flush(terminal->window);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001040}
1041
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001042static void
1043redraw_handler(struct window *window, void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001044{
1045 struct terminal *terminal = data;
1046
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001047 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001048}
1049
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001050static void
1051terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001052
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001053static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001054handle_char(struct terminal *terminal, union utf8_char utf8);
1055
1056static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001057handle_sgr(struct terminal *terminal, int code);
1058
1059static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001060handle_term_parameter(struct terminal *terminal, int code, int sr)
1061{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001062 int i;
1063
Callum Lowcay67a201d2011-01-12 19:23:41 +13001064 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001065 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001066 case 1: /* DECCKM */
1067 if (sr) terminal->key_mode = KM_APPLICATION;
1068 else terminal->key_mode = KM_NORMAL;
1069 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001070 case 2: /* DECANM */
1071 /* No VT52 support yet */
1072 terminal->g0 = CS_US;
1073 terminal->g1 = CS_US;
1074 terminal->cs = terminal->g0;
1075 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001076 case 3: /* DECCOLM */
1077 if (sr)
1078 terminal_resize(terminal, 132, 24);
1079 else
1080 terminal_resize(terminal, 80, 24);
1081
1082 /* set columns, but also home cursor and clear screen */
1083 terminal->row = 0; terminal->column = 0;
1084 for (i = 0; i < terminal->height; i++) {
1085 memset(terminal_get_row(terminal, i),
1086 0, terminal->data_pitch);
1087 attr_init(terminal_get_attr_row(terminal, i),
1088 terminal->curr_attr, terminal->width);
1089 }
1090 break;
1091 case 5: /* DECSCNM */
1092 if (sr) terminal->mode |= MODE_INVERSE;
1093 else terminal->mode &= ~MODE_INVERSE;
1094 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001095 case 6: /* DECOM */
1096 terminal->origin_mode = sr;
1097 if (terminal->origin_mode)
1098 terminal->row = terminal->margin_top;
1099 else
1100 terminal->row = 0;
1101 terminal->column = 0;
1102 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001103 case 7: /* DECAWM */
1104 if (sr) terminal->mode |= MODE_AUTOWRAP;
1105 else terminal->mode &= ~MODE_AUTOWRAP;
1106 break;
1107 case 8: /* DECARM */
1108 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1109 else terminal->mode &= ~MODE_AUTOREPEAT;
1110 break;
1111 case 25:
1112 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1113 else terminal->mode &= ~MODE_SHOW_CURSOR;
1114 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001115 case 1037: /* deleteSendsDel */
1116 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1117 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1118 break;
1119 case 1039: /* altSendsEscape */
1120 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1121 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1122 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001123 default:
1124 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1125 break;
1126 }
1127 } else {
1128 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001129 case 4: /* IRM */
1130 if (sr) terminal->mode |= MODE_IRM;
1131 else terminal->mode &= ~MODE_IRM;
1132 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001133 case 20: /* LNM */
1134 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1135 else terminal->mode &= ~MODE_LF_NEWLINE;
1136 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001137 default:
1138 fprintf(stderr, "Unknown parameter: %d\n", code);
1139 break;
1140 }
1141 }
1142}
1143
1144static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001145handle_dcs(struct terminal *terminal)
1146{
1147}
1148
1149static void
1150handle_osc(struct terminal *terminal)
1151{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001152 char *p;
1153 int code;
1154
1155 terminal->escape[terminal->escape_length++] = '\0';
1156 p = &terminal->escape[2];
1157 code = strtol(p, &p, 10);
1158 if (*p == ';') p++;
1159
1160 switch (code) {
1161 case 0: /* Icon name and window title */
1162 case 1: /* Icon label */
1163 case 2: /* Window title*/
1164 window_set_title(terminal->window, p);
1165 break;
1166 default:
1167 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1168 break;
1169 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001170}
1171
1172static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001173handle_escape(struct terminal *terminal)
1174{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001175 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001176 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001177 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001178 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001179 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001180 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001181 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001182
1183 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001184 i = 0;
1185 p = &terminal->escape[2];
1186 while ((isdigit(*p) || *p == ';') && i < 10) {
1187 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001188 if (!set[i]) {
1189 args[i] = 0;
1190 set[i] = 1;
1191 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001192 p++;
1193 i++;
1194 } else {
1195 args[i] = strtol(p, &p, 10);
1196 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001197 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001198 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001199
1200 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001201 case '@': /* ICH */
1202 count = set[0] ? args[0] : 1;
1203 if (count == 0) count = 1;
1204 terminal_shift_line(terminal, count);
1205 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001206 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001207 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001208 if (count == 0) count = 1;
1209 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001210 terminal->row -= count;
1211 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001212 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001213 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001214 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001215 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001216 if (count == 0) count = 1;
1217 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001218 terminal->row += count;
1219 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001220 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001221 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001222 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001223 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001224 if (count == 0) count = 1;
1225 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001226 terminal->column += count;
1227 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001228 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001229 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001230 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001231 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001232 if (count == 0) count = 1;
1233 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001234 terminal->column -= count;
1235 else
1236 terminal->column = 0;
1237 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001238 case 'E': /* CNL */
1239 count = set[0] ? args[0] : 1;
1240 if (terminal->row + count <= terminal->margin_bottom)
1241 terminal->row += count;
1242 else
1243 terminal->row = terminal->margin_bottom;
1244 terminal->column = 0;
1245 break;
1246 case 'F': /* CPL */
1247 count = set[0] ? args[0] : 1;
1248 if (terminal->row - count >= terminal->margin_top)
1249 terminal->row -= count;
1250 else
1251 terminal->row = terminal->margin_top;
1252 terminal->column = 0;
1253 break;
1254 case 'G': /* CHA */
1255 y = set[0] ? args[0] : 1;
1256 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1257
1258 terminal->column = y - 1;
1259 break;
1260 case 'f': /* HVP */
1261 case 'H': /* CUP */
1262 x = (set[1] ? args[1] : 1) - 1;
1263 x = x < 0 ? 0 :
1264 (x >= terminal->width ? terminal->width - 1 : x);
1265
1266 y = (set[0] ? args[0] : 1) - 1;
1267 if (terminal->origin_mode) {
1268 y += terminal->margin_top;
1269 y = y < terminal->margin_top ? terminal->margin_top :
1270 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1271 } else {
1272 y = y < 0 ? 0 :
1273 (y >= terminal->height ? terminal->height - 1 : y);
1274 }
1275
1276 terminal->row = y;
1277 terminal->column = x;
1278 break;
1279 case 'I': /* CHT */
1280 count = set[0] ? args[0] : 1;
1281 if (count == 0) count = 1;
1282 while (count > 0 && terminal->column < terminal->width) {
1283 if (terminal->tab_ruler[terminal->column]) count--;
1284 terminal->column++;
1285 }
1286 terminal->column--;
1287 break;
1288 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001289 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001290 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001291 if (!set[0] || args[0] == 0 || args[0] > 2) {
1292 memset(&row[terminal->column],
1293 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1294 attr_init(&attr_row[terminal->column],
1295 terminal->curr_attr, terminal->width - terminal->column);
1296 for (i = terminal->row + 1; i < terminal->height; i++) {
1297 memset(terminal_get_row(terminal, i),
1298 0, terminal->data_pitch);
1299 attr_init(terminal_get_attr_row(terminal, i),
1300 terminal->curr_attr, terminal->width);
1301 }
1302 } else if (args[0] == 1) {
1303 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1304 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1305 for (i = 0; i < terminal->row; i++) {
1306 memset(terminal_get_row(terminal, i),
1307 0, terminal->data_pitch);
1308 attr_init(terminal_get_attr_row(terminal, i),
1309 terminal->curr_attr, terminal->width);
1310 }
1311 } else if (args[0] == 2) {
1312 for (i = 0; i < terminal->height; i++) {
1313 memset(terminal_get_row(terminal, i),
1314 0, terminal->data_pitch);
1315 attr_init(terminal_get_attr_row(terminal, i),
1316 terminal->curr_attr, terminal->width);
1317 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001318 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001319 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001320 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001321 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001322 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001323 if (!set[0] || args[0] == 0 || args[0] > 2) {
1324 memset(&row[terminal->column], 0,
1325 (terminal->width - terminal->column) * sizeof(union utf8_char));
1326 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1327 terminal->width - terminal->column);
1328 } else if (args[0] == 1) {
1329 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1330 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1331 } else if (args[0] == 2) {
1332 memset(row, 0, terminal->data_pitch);
1333 attr_init(attr_row, terminal->curr_attr, terminal->width);
1334 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001335 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001336 case 'L': /* IL */
1337 count = set[0] ? args[0] : 1;
1338 if (count == 0) count = 1;
1339 if (terminal->row >= terminal->margin_top &&
1340 terminal->row < terminal->margin_bottom)
1341 {
1342 top = terminal->margin_top;
1343 terminal->margin_top = terminal->row;
1344 terminal_scroll(terminal, 0 - count);
1345 terminal->margin_top = top;
1346 } else if (terminal->row == terminal->margin_bottom) {
1347 memset(terminal_get_row(terminal, terminal->row),
1348 0, terminal->data_pitch);
1349 attr_init(terminal_get_attr_row(terminal, terminal->row),
1350 terminal->curr_attr, terminal->width);
1351 }
1352 break;
1353 case 'M': /* DL */
1354 count = set[0] ? args[0] : 1;
1355 if (count == 0) count = 1;
1356 if (terminal->row >= terminal->margin_top &&
1357 terminal->row < terminal->margin_bottom)
1358 {
1359 top = terminal->margin_top;
1360 terminal->margin_top = terminal->row;
1361 terminal_scroll(terminal, count);
1362 terminal->margin_top = top;
1363 } else if (terminal->row == terminal->margin_bottom) {
1364 memset(terminal_get_row(terminal, terminal->row),
1365 0, terminal->data_pitch);
1366 }
1367 break;
1368 case 'P': /* DCH */
1369 count = set[0] ? args[0] : 1;
1370 if (count == 0) count = 1;
1371 terminal_shift_line(terminal, 0 - count);
1372 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001373 case 'S': /* SU */
1374 terminal_scroll(terminal, set[0] ? args[0] : 1);
1375 break;
1376 case 'T': /* SD */
1377 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1378 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001379 case 'X': /* ECH */
1380 count = set[0] ? args[0] : 1;
1381 if (count == 0) count = 1;
1382 if ((terminal->column + count) > terminal->width)
1383 count = terminal->width - terminal->column;
1384 row = terminal_get_row(terminal, terminal->row);
1385 attr_row = terminal_get_attr_row(terminal, terminal->row);
1386 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1387 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1388 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001389 case 'Z': /* CBT */
1390 count = set[0] ? args[0] : 1;
1391 if (count == 0) count = 1;
1392 while (count > 0 && terminal->column >= 0) {
1393 if (terminal->tab_ruler[terminal->column]) count--;
1394 terminal->column--;
1395 }
1396 terminal->column++;
1397 break;
1398 case '`': /* HPA */
1399 y = set[0] ? args[0] : 1;
1400 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1401
1402 terminal->column = y - 1;
1403 break;
1404 case 'b': /* REP */
1405 count = set[0] ? args[0] : 1;
1406 if (count == 0) count = 1;
1407 if (terminal->last_char.byte[0])
1408 for (i = 0; i < count; i++)
1409 handle_char(terminal, terminal->last_char);
1410 terminal->last_char.byte[0] = 0;
1411 break;
1412 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001413 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001414 break;
1415 case 'd': /* VPA */
1416 x = set[0] ? args[0] : 1;
1417 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1418
1419 terminal->row = x - 1;
1420 break;
1421 case 'g': /* TBC */
1422 if (!set[0] || args[0] == 0) {
1423 terminal->tab_ruler[terminal->column] = 0;
1424 } else if (args[0] == 3) {
1425 memset(terminal->tab_ruler, 0, terminal->width);
1426 }
1427 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001428 case 'h': /* SM */
1429 for(i = 0; i < 10 && set[i]; i++) {
1430 handle_term_parameter(terminal, args[i], 1);
1431 }
1432 break;
1433 case 'l': /* RM */
1434 for(i = 0; i < 10 && set[i]; i++) {
1435 handle_term_parameter(terminal, args[i], 0);
1436 }
1437 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001438 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001439 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001440 if (i <= 7 && set[i] && set[i + 1] &&
1441 set[i + 2] && args[i + 1] == 5)
1442 {
1443 if (args[i] == 38) {
1444 handle_sgr(terminal, args[i + 2] + 256);
1445 break;
1446 } else if (args[i] == 48) {
1447 handle_sgr(terminal, args[i + 2] + 512);
1448 break;
1449 }
1450 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001451 if(set[i]) {
1452 handle_sgr(terminal, args[i]);
1453 } else if(i == 0) {
1454 handle_sgr(terminal, 0);
1455 break;
1456 } else {
1457 break;
1458 }
1459 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001460 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001461 case 'n': /* DSR */
1462 i = set[0] ? args[0] : 0;
1463 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001464 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001465 } else if (i == 6) {
1466 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1467 terminal->origin_mode ?
1468 terminal->row+terminal->margin_top : terminal->row+1,
1469 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001470 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001471 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001472 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001473 case 'r':
1474 if(!set[0]) {
1475 terminal->margin_top = 0;
1476 terminal->margin_bottom = terminal->height-1;
1477 terminal->row = 0;
1478 terminal->column = 0;
1479 } else {
1480 top = (set[0] ? args[0] : 1) - 1;
1481 top = top < 0 ? 0 :
1482 (top >= terminal->height ? terminal->height - 1 : top);
1483 bottom = (set[1] ? args[1] : 1) - 1;
1484 bottom = bottom < 0 ? 0 :
1485 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1486 if(bottom > top) {
1487 terminal->margin_top = top;
1488 terminal->margin_bottom = bottom;
1489 } else {
1490 terminal->margin_top = 0;
1491 terminal->margin_bottom = terminal->height-1;
1492 }
1493 if(terminal->origin_mode)
1494 terminal->row = terminal->margin_top;
1495 else
1496 terminal->row = 0;
1497 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001498 }
1499 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001500 case 's':
1501 terminal->saved_row = terminal->row;
1502 terminal->saved_column = terminal->column;
1503 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001504 case 't': /* windowOps */
1505 if (!set[0]) break;
1506 switch (args[0]) {
1507 case 4: /* resize px */
1508 if (set[1] && set[2]) {
1509 window_set_child_size(terminal->window,
1510 args[2], args[1]);
1511 resize_handler(terminal->window,
1512 args[2], args[1], terminal);
1513 }
1514 break;
1515 case 8: /* resize ch */
1516 if (set[1] && set[2]) {
1517 terminal_resize(terminal, args[2], args[1]);
1518 }
1519 break;
1520 case 13: /* report position */
1521 window_get_child_allocation(terminal->window, &allocation);
1522 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1523 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001524 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001525 break;
1526 case 14: /* report px */
1527 window_get_child_allocation(terminal->window, &allocation);
1528 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1529 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001530 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001531 break;
1532 case 18: /* report ch */
1533 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1534 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001535 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001536 break;
1537 case 21: /* report title */
1538 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1539 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001540 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001541 break;
1542 default:
1543 if (args[0] >= 24)
1544 terminal_resize(terminal, terminal->width, args[0]);
1545 else
1546 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1547 break;
1548 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001549 case 'u':
1550 terminal->row = terminal->saved_row;
1551 terminal->column = terminal->saved_column;
1552 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001553 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001554 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001555 break;
1556 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001557}
1558
1559static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001560handle_non_csi_escape(struct terminal *terminal, char code)
1561{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001562 switch(code) {
1563 case 'M': /* RI */
1564 terminal->row -= 1;
1565 if(terminal->row < terminal->margin_top) {
1566 terminal->row = terminal->margin_top;
1567 terminal_scroll(terminal, -1);
1568 }
1569 break;
1570 case 'E': /* NEL */
1571 terminal->column = 0;
1572 // fallthrough
1573 case 'D': /* IND */
1574 terminal->row += 1;
1575 if(terminal->row > terminal->margin_bottom) {
1576 terminal->row = terminal->margin_bottom;
1577 terminal_scroll(terminal, +1);
1578 }
1579 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001580 case 'c': /* RIS */
1581 terminal_init(terminal);
1582 break;
1583 case 'H': /* HTS */
1584 terminal->tab_ruler[terminal->column] = 1;
1585 break;
1586 case '7': /* DECSC */
1587 terminal->saved_row = terminal->row;
1588 terminal->saved_column = terminal->column;
1589 terminal->saved_attr = terminal->curr_attr;
1590 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001591 terminal->saved_cs = terminal->cs;
1592 terminal->saved_g0 = terminal->g0;
1593 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001594 break;
1595 case '8': /* DECRC */
1596 terminal->row = terminal->saved_row;
1597 terminal->column = terminal->saved_column;
1598 terminal->curr_attr = terminal->saved_attr;
1599 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001600 terminal->cs = terminal->saved_cs;
1601 terminal->g0 = terminal->saved_g0;
1602 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001603 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001604 case '=': /* DECPAM */
1605 terminal->key_mode = KM_APPLICATION;
1606 break;
1607 case '>': /* DECPNM */
1608 terminal->key_mode = KM_NORMAL;
1609 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001610 default:
1611 fprintf(stderr, "Unknown escape code: %c\n", code);
1612 break;
1613 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001614}
1615
1616static void
1617handle_special_escape(struct terminal *terminal, char special, char code)
1618{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001619 int i, numChars;
1620
1621 if (special == '#') {
1622 switch(code) {
1623 case '8':
1624 /* fill with 'E', no cheap way to do this */
1625 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1626 numChars = terminal->width * terminal->height;
1627 for(i = 0; i < numChars; i++) {
1628 terminal->data[i].byte[0] = 'E';
1629 }
1630 break;
1631 default:
1632 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1633 break;
1634 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001635 } else if (special == '(' || special == ')') {
1636 switch(code) {
1637 case '0':
1638 if (special == '(')
1639 terminal->g0 = CS_SPECIAL;
1640 else
1641 terminal->g1 = CS_SPECIAL;
1642 break;
1643 case 'A':
1644 if (special == '(')
1645 terminal->g0 = CS_UK;
1646 else
1647 terminal->g1 = CS_UK;
1648 break;
1649 case 'B':
1650 if (special == '(')
1651 terminal->g0 = CS_US;
1652 else
1653 terminal->g1 = CS_US;
1654 break;
1655 default:
1656 fprintf(stderr, "Unknown character set %c\n", code);
1657 break;
1658 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001659 } else {
1660 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1661 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001662}
1663
1664static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001665handle_sgr(struct terminal *terminal, int code)
1666{
1667 switch(code) {
1668 case 0:
1669 terminal->curr_attr = terminal->color_scheme->default_attr;
1670 break;
1671 case 1:
1672 terminal->curr_attr.a |= ATTRMASK_BOLD;
1673 if (terminal->curr_attr.fg < 8)
1674 terminal->curr_attr.fg += 8;
1675 break;
1676 case 4:
1677 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1678 break;
1679 case 5:
1680 terminal->curr_attr.a |= ATTRMASK_BLINK;
1681 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001682 case 8:
1683 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1684 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001685 case 2:
1686 case 21:
1687 case 22:
1688 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1689 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1690 terminal->curr_attr.fg -= 8;
1691 break;
1692 case 24:
1693 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1694 break;
1695 case 25:
1696 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1697 break;
1698 case 7:
1699 case 26:
1700 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1701 break;
1702 case 27:
1703 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1704 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001705 case 28:
1706 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1707 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001708 case 39:
1709 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1710 break;
1711 case 49:
1712 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1713 break;
1714 default:
1715 if(code >= 30 && code <= 37) {
1716 terminal->curr_attr.fg = code - 30;
1717 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1718 terminal->curr_attr.fg += 8;
1719 } else if(code >= 40 && code <= 47) {
1720 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001721 } else if (code >= 90 && code <= 97) {
1722 terminal->curr_attr.fg = code - 90 + 8;
1723 } else if (code >= 100 && code <= 107) {
1724 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001725 } else if(code >= 256 && code < 512) {
1726 terminal->curr_attr.fg = code - 256;
1727 } else if(code >= 512 && code < 768) {
1728 terminal->curr_attr.bg = code - 512;
1729 } else {
1730 fprintf(stderr, "Unknown SGR code: %d\n", code);
1731 }
1732 break;
1733 }
1734}
1735
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001736/* Returns 1 if c was special, otherwise 0 */
1737static int
1738handle_special_char(struct terminal *terminal, char c)
1739{
1740 union utf8_char *row;
1741 struct attr *attr_row;
1742
1743 row = terminal_get_row(terminal, terminal->row);
1744 attr_row = terminal_get_attr_row(terminal, terminal->row);
1745
1746 switch(c) {
1747 case '\r':
1748 terminal->column = 0;
1749 break;
1750 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001751 if (terminal->mode & MODE_LF_NEWLINE) {
1752 terminal->column = 0;
1753 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001754 /* fallthrough */
1755 case '\v':
1756 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001757 terminal->row++;
1758 if(terminal->row > terminal->margin_bottom) {
1759 terminal->row = terminal->margin_bottom;
1760 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001761 }
1762
1763 break;
1764 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001765 while (terminal->column < terminal->width) {
1766 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001767 if (terminal->mode & MODE_IRM)
1768 terminal_shift_line(terminal, +1);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001769 row[terminal->column].byte[0] = ' ';
1770 row[terminal->column].byte[1] = '\0';
1771 attr_row[terminal->column] = terminal->curr_attr;
1772 terminal->column++;
1773 }
1774 if (terminal->column >= terminal->width) {
1775 terminal->column = terminal->width - 1;
1776 }
1777
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001778 break;
1779 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001780 if (terminal->column >= terminal->width) {
1781 terminal->column = terminal->width - 2;
1782 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001783 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001784 } else if (terminal->mode & MODE_AUTOWRAP) {
1785 terminal->column = terminal->width - 1;
1786 terminal->row -= 1;
1787 if (terminal->row < terminal->margin_top) {
1788 terminal->row = terminal->margin_top;
1789 terminal_scroll(terminal, -1);
1790 }
1791 }
1792
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001793 break;
1794 case '\a':
1795 /* Bell */
1796 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001797 case '\x0E': /* SO */
1798 terminal->cs = terminal->g1;
1799 break;
1800 case '\x0F': /* SI */
1801 terminal->cs = terminal->g0;
1802 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001803 default:
1804 return 0;
1805 }
1806
1807 return 1;
1808}
1809
1810static void
1811handle_char(struct terminal *terminal, union utf8_char utf8)
1812{
1813 union utf8_char *row;
1814 struct attr *attr_row;
1815
1816 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001817
1818 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001819
1820 /* There are a whole lot of non-characters, control codes,
1821 * and formatting codes that should probably be ignored,
1822 * for example: */
1823 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1824 /* BOM, ignore */
1825 return;
1826 }
1827
1828 /* Some of these non-characters should be translated, e.g.: */
1829 if (utf8.byte[0] < 32) {
1830 utf8.byte[0] = utf8.byte[0] + 64;
1831 }
1832
1833 /* handle right margin effects */
1834 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001835 if (terminal->mode & MODE_AUTOWRAP) {
1836 terminal->column = 0;
1837 terminal->row += 1;
1838 if (terminal->row > terminal->margin_bottom) {
1839 terminal->row = terminal->margin_bottom;
1840 terminal_scroll(terminal, +1);
1841 }
1842 } else {
1843 terminal->column--;
1844 }
1845 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001846
1847 row = terminal_get_row(terminal, terminal->row);
1848 attr_row = terminal_get_attr_row(terminal, terminal->row);
1849
Callum Lowcay69e96582011-01-07 19:47:00 +00001850 if (terminal->mode & MODE_IRM)
1851 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001852 row[terminal->column] = utf8;
1853 attr_row[terminal->column++] = terminal->curr_attr;
1854
1855 if (utf8.ch != terminal->last_char.ch)
1856 terminal->last_char = utf8;
1857}
1858
Callum Lowcay30eeae52011-01-07 19:46:55 +00001859static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001860escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1861{
1862 int len, i;
1863
1864 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1865 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1866 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1867 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1868 else len = 1; /* Invalid, cannot happen */
1869
1870 if (terminal->escape_length + len <= MAX_ESCAPE) {
1871 for (i = 0; i < len; i++)
1872 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1873 terminal->escape_length += len;
1874 } else if (terminal->escape_length < MAX_ESCAPE) {
1875 terminal->escape[terminal->escape_length++] = 0;
1876 }
1877}
1878
1879static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001880terminal_data(struct terminal *terminal, const char *data, size_t length)
1881{
1882 int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001883 union utf8_char utf8;
1884 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001885
1886 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001887 parser_state =
1888 utf8_next_char(&terminal->state_machine, data[i]);
1889 switch(parser_state) {
1890 case utf8state_accept:
1891 utf8.ch = terminal->state_machine.s.ch;
1892 break;
1893 case utf8state_reject:
1894 /* the unicode replacement character */
1895 utf8.byte[0] = 0xEF;
1896 utf8.byte[1] = 0xBF;
1897 utf8.byte[2] = 0xBD;
1898 utf8.byte[3] = 0x00;
1899 break;
1900 default:
1901 continue;
1902 }
1903
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001904 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001905 switch (terminal->state) {
1906 case escape_state_escape:
1907 escape_append_utf8(terminal, utf8);
1908 switch (utf8.byte[0]) {
1909 case 'P': /* DCS */
1910 terminal->state = escape_state_dcs;
1911 break;
1912 case '[': /* CSI */
1913 terminal->state = escape_state_csi;
1914 break;
1915 case ']': /* OSC */
1916 terminal->state = escape_state_osc;
1917 break;
1918 case '#':
1919 case '(':
1920 case ')': /* special */
1921 terminal->state = escape_state_special;
1922 break;
1923 case '^': /* PM (not implemented) */
1924 case '_': /* APC (not implemented) */
1925 terminal->state = escape_state_ignore;
1926 break;
1927 default:
1928 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001929 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13001930 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001931 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001932 continue;
1933 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001934 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1935 /* do nothing */
1936 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001937 terminal->escape_flags |= ESC_FLAG_WHAT;
1938 } else if (utf8.byte[0] == '>') {
1939 terminal->escape_flags |= ESC_FLAG_GT;
1940 } else if (utf8.byte[0] == '!') {
1941 terminal->escape_flags |= ESC_FLAG_BANG;
1942 } else if (utf8.byte[0] == '$') {
1943 terminal->escape_flags |= ESC_FLAG_CASH;
1944 } else if (utf8.byte[0] == '\'') {
1945 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1946 } else if (utf8.byte[0] == '"') {
1947 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1948 } else if (utf8.byte[0] == ' ') {
1949 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001950 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001951 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001952 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13001953 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001954 }
1955
1956 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1957 utf8.byte[0] == '`')
1958 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001959 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001960 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001961 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001962 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001963 continue;
1964 case escape_state_inner_escape:
1965 if (utf8.byte[0] == '\\') {
1966 terminal->state = escape_state_normal;
1967 if (terminal->outer_state == escape_state_dcs) {
1968 handle_dcs(terminal);
1969 } else if (terminal->outer_state == escape_state_osc) {
1970 handle_osc(terminal);
1971 }
1972 } else if (utf8.byte[0] == '\e') {
1973 terminal->state = terminal->outer_state;
1974 escape_append_utf8(terminal, utf8);
1975 if (terminal->escape_length >= MAX_ESCAPE)
1976 terminal->state = escape_state_normal;
1977 } else {
1978 terminal->state = terminal->outer_state;
1979 if (terminal->escape_length < MAX_ESCAPE)
1980 terminal->escape[terminal->escape_length++] = '\e';
1981 escape_append_utf8(terminal, utf8);
1982 if (terminal->escape_length >= MAX_ESCAPE)
1983 terminal->state = escape_state_normal;
1984 }
1985 continue;
1986 case escape_state_dcs:
1987 case escape_state_osc:
1988 case escape_state_ignore:
1989 if (utf8.byte[0] == '\e') {
1990 terminal->outer_state = terminal->state;
1991 terminal->state = escape_state_inner_escape;
1992 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
1993 terminal->state = escape_state_normal;
1994 handle_osc(terminal);
1995 } else {
1996 escape_append_utf8(terminal, utf8);
1997 if (terminal->escape_length >= MAX_ESCAPE)
1998 terminal->state = escape_state_normal;
1999 }
2000 continue;
2001 case escape_state_special:
2002 escape_append_utf8(terminal, utf8);
2003 terminal->state = escape_state_normal;
2004 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2005 handle_special_escape(terminal, terminal->escape[1],
2006 utf8.byte[0]);
2007 }
2008 continue;
2009 default:
2010 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002011 }
2012
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002013 /* this is valid, because ASCII characters are never used to
2014 * introduce a multibyte sequence in UTF-8 */
2015 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002016 terminal->state = escape_state_escape;
2017 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002018 terminal->escape[0] = '\e';
2019 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002020 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002021 } else {
2022 handle_char(terminal, utf8);
2023 } /* if */
2024 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002025
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002026 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002027}
2028
2029static void
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002030selection_listener_send(void *data, struct wl_selection *selection,
2031 const char *mime_type, int fd)
2032{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002033 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002034
2035 fprintf(stderr, "selection send, fd is %d\n", fd);
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002036 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002037 close(fd);
2038}
2039
2040static void
2041selection_listener_cancelled(void *data, struct wl_selection *selection)
2042{
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002043 fprintf(stderr, "selection cancelled\n");
2044 wl_selection_destroy(selection);
2045}
2046
2047static const struct wl_selection_listener selection_listener = {
2048 selection_listener_send,
2049 selection_listener_cancelled
2050};
2051
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002052static int
2053handle_bound_key(struct terminal *terminal,
2054 struct input *input, uint32_t sym, uint32_t time)
2055{
2056 struct wl_shell *shell;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002057
2058 switch (sym) {
2059 case XK_C:
2060 shell = display_get_shell(terminal->display);
2061 terminal->selection = wl_shell_create_selection(shell);
2062 wl_selection_add_listener(terminal->selection,
2063 &selection_listener, terminal);
2064 wl_selection_offer(terminal->selection, "text/plain");
2065 wl_selection_activate(terminal->selection,
2066 input_get_input_device(input), time);
2067
2068 return 1;
2069 case XK_V:
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05002070 /* Just pass the master fd of the pty to receive the
2071 * selection. */
2072 if (input_offers_mime_type(input, "text/plain"))
2073 input_receive_mime_type(input, "text/plain",
2074 terminal->master);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002075 return 1;
2076 case XK_X:
2077 /* cut selection; terminal doesn't do cut */
2078 return 0;
2079 default:
2080 return 0;
2081 }
2082}
2083
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002084static void
2085key_handler(struct window *window, struct input *input, uint32_t time,
2086 uint32_t key, uint32_t sym, uint32_t state, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002087{
2088 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002089 char ch[MAX_RESPONSE];
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002090 uint32_t modifiers;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002091 int len = 0;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002092
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002093 modifiers = input_get_modifiers(input);
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002094 if ((modifiers & XKB_COMMON_CONTROL_MASK) &&
2095 (modifiers & XKB_COMMON_SHIFT_MASK) &&
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002096 state && handle_bound_key(terminal, input, sym, 0))
2097 return;
2098
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002099 switch (sym) {
2100 case XK_F11:
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002101 if (!state)
2102 break;
2103 terminal->fullscreen ^= 1;
2104 window_set_fullscreen(window, terminal->fullscreen);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002105 window_schedule_redraw(terminal->window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002106 break;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002107
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002108 case XK_BackSpace:
2109 case XK_Tab:
2110 case XK_Linefeed:
2111 case XK_Clear:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002112 case XK_Pause:
2113 case XK_Scroll_Lock:
2114 case XK_Sys_Req:
2115 case XK_Escape:
2116 ch[len++] = sym & 0x7f;
2117 break;
2118
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002119 case XK_Return:
2120 if (terminal->mode & MODE_LF_NEWLINE) {
2121 ch[len++] = 0x0D;
2122 ch[len++] = 0x0A;
2123 } else {
2124 ch[len++] = 0x0D;
2125 }
2126 break;
2127
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002128 case XK_Shift_L:
2129 case XK_Shift_R:
2130 case XK_Control_L:
2131 case XK_Control_R:
2132 case XK_Alt_L:
2133 case XK_Alt_R:
2134 break;
2135
Callum Lowcay7e08e902011-01-07 19:47:02 +00002136 case XK_Insert:
2137 len = function_key_response('[', 2, modifiers, '~', ch);
2138 break;
2139 case XK_Delete:
2140 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2141 ch[len++] = '\x04';
2142 } else {
2143 len = function_key_response('[', 3, modifiers, '~', ch);
2144 }
2145 break;
2146 case XK_Page_Up:
2147 len = function_key_response('[', 5, modifiers, '~', ch);
2148 break;
2149 case XK_Page_Down:
2150 len = function_key_response('[', 6, modifiers, '~', ch);
2151 break;
2152 case XK_F1:
2153 len = function_key_response('O', 1, modifiers, 'P', ch);
2154 break;
2155 case XK_F2:
2156 len = function_key_response('O', 1, modifiers, 'Q', ch);
2157 break;
2158 case XK_F3:
2159 len = function_key_response('O', 1, modifiers, 'R', ch);
2160 break;
2161 case XK_F4:
2162 len = function_key_response('O', 1, modifiers, 'S', ch);
2163 break;
2164 case XK_F5:
2165 len = function_key_response('[', 15, modifiers, '~', ch);
2166 break;
2167 case XK_F6:
2168 len = function_key_response('[', 17, modifiers, '~', ch);
2169 break;
2170 case XK_F7:
2171 len = function_key_response('[', 18, modifiers, '~', ch);
2172 break;
2173 case XK_F8:
2174 len = function_key_response('[', 19, modifiers, '~', ch);
2175 break;
2176 case XK_F9:
2177 len = function_key_response('[', 20, modifiers, '~', ch);
2178 break;
2179 case XK_F10:
2180 len = function_key_response('[', 21, modifiers, '~', ch);
2181 break;
2182 case XK_F12:
2183 len = function_key_response('[', 24, modifiers, '~', ch);
2184 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002185 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002186 /* Handle special keys with alternate mappings */
2187 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2188 if (len != 0) break;
2189
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002190 if (modifiers & XKB_COMMON_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002191 if (sym >= '3' && sym <= '7')
2192 sym = (sym & 0x1f) + 8;
2193
2194 if (!((sym >= '!' && sym <= '/') ||
2195 (sym >= '8' && sym <= '?') ||
2196 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2197 else if (sym == '2') sym = 0x00;
2198 else if (sym == '/') sym = 0x1F;
2199 else if (sym == '8' || sym == '?') sym = 0x7F;
2200 } else if ((terminal->mode & MODE_ALT_SENDS_ESC) &&
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002201 (modifiers & XKB_COMMON_MOD1_MASK))
Callum Lowcay7e08e902011-01-07 19:47:02 +00002202 {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002203 ch[len++] = 0x1b;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002204 } else if (modifiers & XKB_COMMON_MOD1_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002205 sym = sym | 0x80;
2206 }
2207
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002208 if (sym < 256)
2209 ch[len++] = sym;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002210 break;
2211 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002212
2213 if (state && len > 0)
Kristian Høgsberg26130862011-08-24 11:30:21 -04002214 terminal_write(terminal, ch, len);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002215}
2216
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002217static void
2218keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002219 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002220{
2221 struct terminal *terminal = data;
2222
2223 terminal->focused = (device != NULL);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002224 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002225}
2226
Kristian Høgsberg59826582011-01-20 11:56:57 -05002227static void
2228button_handler(struct window *window,
2229 struct input *input, uint32_t time,
2230 int button, int state, void *data)
2231{
2232 struct terminal *terminal = data;
2233
2234 switch (button) {
2235 case 272:
2236 if (state) {
2237 terminal->dragging = 1;
2238 terminal->selection_active = 0;
2239 input_get_position(input,
2240 &terminal->selection_start_x,
2241 &terminal->selection_start_y);
2242 terminal->selection_end_x = terminal->selection_start_x;
2243 terminal->selection_end_y = terminal->selection_start_y;
2244 window_schedule_redraw(window);
2245 } else {
2246 terminal->dragging = 0;
2247 }
2248 break;
2249 }
2250}
2251
2252static int
2253motion_handler(struct window *window,
2254 struct input *input, uint32_t time,
2255 int32_t x, int32_t y,
2256 int32_t sx, int32_t sy, void *data)
2257{
2258 struct terminal *terminal = data;
2259
2260 if (terminal->dragging) {
2261 terminal->selection_active = 1;
2262 input_get_position(input,
2263 &terminal->selection_end_x,
2264 &terminal->selection_end_y);
2265 window_schedule_redraw(window);
2266 }
2267
2268 return POINTER_IBEAM;
2269}
2270
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002271static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002272terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002273{
2274 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002275 cairo_surface_t *surface;
2276 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002277
2278 terminal = malloc(sizeof *terminal);
2279 if (terminal == NULL)
2280 return terminal;
2281
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002282 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002283 terminal->fullscreen = fullscreen;
Callum Lowcay30eeae52011-01-07 19:46:55 +00002284 terminal->color_scheme = &DEFAULT_COLORS;
2285 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002286 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002287 terminal->margin_bottom = -1;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002288 terminal->window = window_create(display, 500, 400);
2289 window_set_title(terminal->window, "Wayland Terminal");
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002290
2291 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002292 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002293
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002294 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002295 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002296
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002297 window_set_user_data(terminal->window, terminal);
2298 window_set_redraw_handler(terminal->window, redraw_handler);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05002299 window_set_resize_handler(terminal->window, resize_handler);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002300
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002301 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002302 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002303 keyboard_focus_handler);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002304 window_set_button_handler(terminal->window, button_handler);
2305 window_set_motion_handler(terminal->window, motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002306
Kristian Høgsberg09531622010-06-14 23:22:15 -04002307 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2308 cr = cairo_create(surface);
Kristian Høgsberg09531622010-06-14 23:22:15 -04002309 cairo_set_font_size(cr, 14);
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002310 cairo_select_font_face (cr, "mono",
2311 CAIRO_FONT_SLANT_NORMAL,
2312 CAIRO_FONT_WEIGHT_BOLD);
2313 terminal->font_bold = cairo_get_scaled_font (cr);
2314 cairo_scaled_font_reference(terminal->font_bold);
2315
2316 cairo_select_font_face (cr, "mono",
2317 CAIRO_FONT_SLANT_NORMAL,
2318 CAIRO_FONT_WEIGHT_NORMAL);
2319 terminal->font_normal = cairo_get_scaled_font (cr);
2320 cairo_scaled_font_reference(terminal->font_normal);
2321
Kristian Høgsberg09531622010-06-14 23:22:15 -04002322 cairo_font_extents(cr, &terminal->extents);
2323 cairo_destroy(cr);
2324 cairo_surface_destroy(surface);
2325
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002326 return terminal;
2327}
2328
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002329static gboolean
2330io_handler(GIOChannel *source,
2331 GIOCondition condition,
2332 gpointer data)
2333{
2334 struct terminal *terminal = data;
2335 gchar buffer[256];
2336 gsize bytes_read;
2337 GError *error = NULL;
2338
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002339 if(condition == G_IO_HUP)
2340 exit(0);
2341
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002342 g_io_channel_read_chars(source, buffer, sizeof buffer,
2343 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002344
2345 terminal_data(terminal, buffer, bytes_read);
2346
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002347 return TRUE;
2348}
2349
2350static int
2351terminal_run(struct terminal *terminal, const char *path)
2352{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002353 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002354 pid_t pid;
2355
2356 pid = forkpty(&master, NULL, NULL, NULL);
2357 if (pid == 0) {
Callum Lowcay86653ed2011-01-07 19:47:03 +00002358 setenv("TERM", "xterm-256color", 1);
2359 setenv("COLORTERM", "xterm-256color", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002360 if (execl(path, path, NULL)) {
2361 printf("exec failed: %m\n");
2362 exit(EXIT_FAILURE);
2363 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002364 } else if (pid < 0) {
2365 fprintf(stderr, "failed to fork and create pty (%m).\n");
2366 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002367 }
2368
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002369 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002370 terminal->channel = g_io_channel_unix_new(master);
2371 fcntl(master, F_SETFL, O_NONBLOCK);
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002372 g_io_add_watch(terminal->channel, G_IO_IN, io_handler, terminal);
2373 g_io_add_watch(terminal->channel, G_IO_HUP, io_handler, terminal);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002374
Kristian Høgsberg54b86832011-06-21 16:31:11 -04002375 terminal_resize(terminal, 80, 24);
2376 terminal_draw(terminal);
2377
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002378 return 0;
2379}
2380
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002381static const GOptionEntry option_entries[] = {
2382 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
2383 &option_fullscreen, "Run in fullscreen mode" },
2384 { NULL }
2385};
2386
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002387int main(int argc, char *argv[])
2388{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002389 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002390 struct terminal *terminal;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002391
Kristian Høgsberg9de79a92011-08-24 11:09:53 -04002392 d = display_create(&argc, &argv, option_entries);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002393 if (d == NULL) {
2394 fprintf(stderr, "failed to create display: %m\n");
2395 return -1;
2396 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002397
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002398 terminal = terminal_create(d, option_fullscreen);
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002399 if (terminal_run(terminal, "/bin/bash"))
2400 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002401
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002402 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002403
2404 return 0;
2405}