blob: dda5f9fc24b2a68167abd35da66da7bbb2f8bdb9 [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
54
Callum Lowcayb8609ad2011-01-07 19:46:57 +000055/* Buffer sizes */
56#define MAX_RESPONSE 11
57#define MAX_ESCAPE 64
58
Callum Lowcay8e57dd52011-01-07 19:46:59 +000059/* Terminal modes */
60#define MODE_SHOW_CURSOR 0x00000001
61#define MODE_INVERSE 0x00000002
62#define MODE_AUTOWRAP 0x00000004
63#define MODE_AUTOREPEAT 0x00000008
64#define MODE_LF_NEWLINE 0x00000010
Callum Lowcay69e96582011-01-07 19:47:00 +000065#define MODE_IRM 0x00000020
Callum Lowcay7e08e902011-01-07 19:47:02 +000066#define MODE_DELETE_SENDS_DEL 0x00000040
67#define MODE_ALT_SENDS_ESC 0x00000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +000068
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000069union utf8_char {
70 unsigned char byte[4];
71 uint32_t ch;
72};
73
74enum utf8_state {
75 utf8state_start,
76 utf8state_accept,
77 utf8state_reject,
78 utf8state_expect3,
79 utf8state_expect2,
80 utf8state_expect1
81};
82
83struct utf8_state_machine {
84 enum utf8_state state;
85 int len;
86 union utf8_char s;
87};
88
89static void
90init_state_machine(struct utf8_state_machine *machine)
91{
92 machine->state = utf8state_start;
93 machine->len = 0;
94 machine->s.ch = 0;
95}
96
97static enum utf8_state
98utf8_next_char(struct utf8_state_machine *machine, char c)
99{
100 switch(machine->state) {
101 case utf8state_start:
102 case utf8state_accept:
103 case utf8state_reject:
104 machine->s.ch = 0;
105 machine->len = 0;
106 if(c == 0xC0 || c == 0xC1) {
107 /* overlong encoding, reject */
108 machine->state = utf8state_reject;
109 } else if((c & 0x80) == 0) {
110 /* single byte, accept */
111 machine->s.byte[machine->len++] = c;
112 machine->state = utf8state_accept;
113 } else if((c & 0xC0) == 0x80) {
114 /* parser out of sync, ignore byte */
115 machine->state = utf8state_start;
116 } else if((c & 0xE0) == 0xC0) {
117 /* start of two byte sequence */
118 machine->s.byte[machine->len++] = c;
119 machine->state = utf8state_expect1;
120 } else if((c & 0xF0) == 0xE0) {
121 /* start of three byte sequence */
122 machine->s.byte[machine->len++] = c;
123 machine->state = utf8state_expect2;
124 } else if((c & 0xF8) == 0xF0) {
125 /* start of four byte sequence */
126 machine->s.byte[machine->len++] = c;
127 machine->state = utf8state_expect3;
128 } else {
129 /* overlong encoding, reject */
130 machine->state = utf8state_reject;
131 }
132 break;
133 case utf8state_expect3:
134 machine->s.byte[machine->len++] = c;
135 if((c & 0xC0) == 0x80) {
136 /* all good, continue */
137 machine->state = utf8state_expect2;
138 } else {
139 /* missing extra byte, reject */
140 machine->state = utf8state_reject;
141 }
142 break;
143 case utf8state_expect2:
144 machine->s.byte[machine->len++] = c;
145 if((c & 0xC0) == 0x80) {
146 /* all good, continue */
147 machine->state = utf8state_expect1;
148 } else {
149 /* missing extra byte, reject */
150 machine->state = utf8state_reject;
151 }
152 break;
153 case utf8state_expect1:
154 machine->s.byte[machine->len++] = c;
155 if((c & 0xC0) == 0x80) {
156 /* all good, accept */
157 machine->state = utf8state_accept;
158 } else {
159 /* missing extra byte, reject */
160 machine->state = utf8state_reject;
161 }
162 break;
163 default:
164 machine->state = utf8state_reject;
165 break;
166 }
167
168 return machine->state;
169}
170
Callum Lowcay256e72f2011-01-07 19:47:01 +0000171struct char_sub {
172 union utf8_char match;
173 union utf8_char replace;
174};
175/* Set last char_sub match to NULL char */
176typedef struct char_sub *character_set;
177
178struct char_sub CS_US[] = {
179 {{{0, }}, {{0, }}}
180};
181static struct char_sub CS_UK[] = {
182 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}},
183 {{{0, }}, {{0, }}}
184};
185static struct char_sub CS_SPECIAL[] = {
186 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond */
187 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell */
188 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT */
189 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF */
190 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR */
191 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF */
192 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree */
193 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus */
194 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL */
195 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT */
196 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB */
197 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT */
198 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT */
199 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_RB */
200 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS */
201 {{{'o', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
202 {{{'p', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
203 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
204 {{{'r', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
205 {{{'s', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
206 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR */
207 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL */
208 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU */
209 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD */
210 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V */
211 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE */
212 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE */
213 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI */
214 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ */
215 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND */
216 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT */
217 {{{0, }}, {{0, }}}
218};
219
220static void
221apply_char_set(character_set cs, union utf8_char *utf8)
222{
223 int i = 0;
224
225 while (cs[i].match.byte[0]) {
226 if ((*utf8).ch == cs[i].match.ch) {
227 *utf8 = cs[i].replace;
228 break;
229 }
230 i++;
231 }
232}
233
Callum Lowcay7e08e902011-01-07 19:47:02 +0000234struct key_map {
235 int sym;
236 int num;
237 char escape;
238 char code;
239};
240/* Set last key_sub sym to NULL */
241typedef struct key_map *keyboard_mode;
242
243static struct key_map KM_NORMAL[] = {
244 {XK_Left, 1, '[', 'D'},
245 {XK_Right, 1, '[', 'C'},
246 {XK_Up, 1, '[', 'A'},
247 {XK_Down, 1, '[', 'B'},
248 {XK_Home, 1, '[', 'H'},
249 {XK_End, 1, '[', 'F'},
250 {0, 0, 0, 0}
251};
252static struct key_map KM_APPLICATION[] = {
253 {XK_Left, 1, 'O', 'D'},
254 {XK_Right, 1, 'O', 'C'},
255 {XK_Up, 1, 'O', 'A'},
256 {XK_Down, 1, 'O', 'B'},
257 {XK_Home, 1, 'O', 'H'},
258 {XK_End, 1, 'O', 'F'},
259 {XK_KP_Enter, 1, 'O', 'M'},
260 {XK_KP_Multiply, 1, 'O', 'j'},
261 {XK_KP_Add, 1, 'O', 'k'},
262 {XK_KP_Separator, 1, 'O', 'l'},
263 {XK_KP_Subtract, 1, 'O', 'm'},
264 {XK_KP_Divide, 1, 'O', 'o'},
265 {0, 0, 0, 0}
266};
267
268static int
269function_key_response(char escape, int num, uint32_t modifiers,
270 char code, char *response)
271{
272 int mod_num = 0;
273 int len;
274
275 if (modifiers & WINDOW_MODIFIER_SHIFT) mod_num |= 1;
276 if (modifiers & WINDOW_MODIFIER_ALT) mod_num |= 2;
277 if (modifiers & WINDOW_MODIFIER_CONTROL) mod_num |= 4;
278
279 if (mod_num != 0)
280 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
281 num, mod_num + 1, code);
282 else if (code != '~')
283 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
284 escape, code);
285 else
286 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
287 escape, num, code);
288
289 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
290 else return len;
291}
292
293/* returns the number of bytes written into response,
294 * which must have room for MAX_RESPONSE bytes */
295static int
296apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
297{
298 struct key_map map;
299 int len = 0;
300 int i = 0;
301
302 while (mode[i].sym) {
303 map = mode[i++];
304 if (sym == map.sym) {
305 len = function_key_response(map.escape, map.num,
306 modifiers, map.code,
307 response);
308 break;
309 }
310 }
311
312 return len;
313}
314
Callum Lowcay30eeae52011-01-07 19:46:55 +0000315struct terminal_color { double r, g, b, a; };
316struct attr {
317 unsigned char fg, bg;
318 char a; /* attributes format:
319 * 76543210
320 * ilub */
321 char r; /* reserved */
322};
323struct color_scheme {
324 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500325 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000326 struct attr default_attr;
327};
328
329static void
330attr_init(struct attr *data_attr, struct attr attr, int n)
331{
332 int i;
333 for (i = 0; i < n; i++) {
334 data_attr[i] = attr;
335 }
336}
337
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500338struct terminal {
339 struct window *window;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500340 struct display *display;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000341 union utf8_char *data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000342 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000343 struct attr *data_attr;
344 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000345 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000346 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000347 char saved_origin_mode;
348 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000349 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000350 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000351 character_set cs, g0, g1;
352 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000353 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000354 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500355 int width, height, start, row, column;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000356 int saved_row, saved_column;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500357 int fd, master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500358 GIOChannel *channel;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500359 uint32_t modifiers;
Callum Lowcayb8609ad2011-01-07 19:46:57 +0000360 char escape[MAX_ESCAPE];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500361 int escape_length;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500362 int state;
Callum Lowcayb8609ad2011-01-07 19:46:57 +0000363 int qmark_flag;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000364 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500365 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500366 int fullscreen;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500367 int focused;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400368 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000369 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400370 cairo_font_extents_t extents;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500371 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500372};
373
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000374/* Create default tab stops, every 8 characters */
375static void
376terminal_init_tabs(struct terminal *terminal)
377{
378 int i = 0;
379
380 while (i < terminal->width) {
381 if (i % 8 == 0)
382 terminal->tab_ruler[i] = 1;
383 else
384 terminal->tab_ruler[i] = 0;
385 i++;
386 }
387}
388
Callum Lowcay30eeae52011-01-07 19:46:55 +0000389static void
390terminal_init(struct terminal *terminal)
391{
392 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000393 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000394 terminal->mode = MODE_SHOW_CURSOR |
395 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000396 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000397 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000398
399 terminal->row = 0;
400 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000401
Callum Lowcay256e72f2011-01-07 19:47:01 +0000402 terminal->g0 = CS_US;
403 terminal->g1 = CS_US;
404 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000405 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000406
407 terminal->saved_g0 = terminal->g0;
408 terminal->saved_g1 = terminal->g1;
409 terminal->saved_cs = terminal->cs;
410
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000411 terminal->saved_attr = terminal->curr_attr;
412 terminal->saved_origin_mode = terminal->origin_mode;
413 terminal->saved_row = terminal->row;
414 terminal->saved_column = terminal->column;
415
416 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000417}
418
419static void
420init_color_table(struct terminal *terminal)
421{
422 int c, r;
423 struct terminal_color *color_table = terminal->color_table;
424
425 for (c = 0; c < 256; c ++) {
426 if (c < 16) {
427 color_table[c] = terminal->color_scheme->palette[c];
428 } else if (c < 232) {
429 r = c - 16;
430 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
431 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
432 color_table[c].r = ((double)(r % 6) / 6.0);
433 color_table[c].a = 1.0;
434 } else {
435 r = (c - 232) * 10 + 8;
436 color_table[c].r = ((double) r) / 256.0;
437 color_table[c].g = color_table[c].r;
438 color_table[c].b = color_table[c].r;
439 color_table[c].a = 1.0;
440 }
441 }
442}
443
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000444static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500445terminal_get_row(struct terminal *terminal, int row)
446{
447 int index;
448
449 index = (row + terminal->start) % terminal->height;
450
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000451 return &terminal->data[index * terminal->width];
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500452}
453
Callum Lowcay30eeae52011-01-07 19:46:55 +0000454static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500455terminal_get_attr_row(struct terminal *terminal, int row)
456{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000457 int index;
458
459 index = (row + terminal->start) % terminal->height;
460
461 return &terminal->data_attr[index * terminal->width];
462}
463
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500464union decoded_attr {
465 struct {
466 unsigned char foreground;
467 unsigned char background;
468 unsigned char bold;
469 unsigned char underline;
470 };
471 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500472};
473
474static void
475terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500476 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500477{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500478 struct attr attr;
479 int foreground, background, tmp;
480
481 /* get the attributes for this character cell */
482 attr = terminal_get_attr_row(terminal, row)[col];
483 if ((attr.a & ATTRMASK_INVERSE) ||
484 ((terminal->mode & MODE_SHOW_CURSOR) &&
485 terminal->focused && terminal->row == row &&
486 terminal->column == col)) {
487 foreground = attr.bg;
488 background = attr.fg;
489 if (attr.a & ATTRMASK_BOLD) {
490 if (foreground <= 16) foreground |= 0x08;
491 if (background <= 16) background &= 0x07;
492 }
493 } else {
494 foreground = attr.fg;
495 background = attr.bg;
496 }
497
498 if (terminal->mode & MODE_INVERSE) {
499 tmp = foreground;
500 foreground = background;
501 background = tmp;
502 if (attr.a & ATTRMASK_BOLD) {
503 if (foreground <= 16) foreground |= 0x08;
504 if (background <= 16) background &= 0x07;
505 }
506 }
507
508 decoded->foreground = foreground;
509 decoded->background = background;
510 decoded->bold = attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK);
511 decoded->underline = attr.a & ATTRMASK_UNDERLINE;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000512}
513
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500514static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000515terminal_scroll_buffer(struct terminal *terminal, int d)
516{
517 int i;
518
519 d = d % (terminal->height + 1);
520 terminal->start = (terminal->start + d) % terminal->height;
521 if (terminal->start < 0) terminal->start = terminal->height + terminal->start;
522 if(d < 0) {
523 d = 0 - d;
524 for(i = 0; i < d; i++) {
525 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
526 attr_init(terminal_get_attr_row(terminal, i),
527 terminal->curr_attr, terminal->width);
528 }
529 } else {
530 for(i = terminal->height - d; i < terminal->height; i++) {
531 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
532 attr_init(terminal_get_attr_row(terminal, i),
533 terminal->curr_attr, terminal->width);
534 }
535 }
536}
537
538static void
539terminal_scroll_window(struct terminal *terminal, int d)
540{
541 int i;
542 int window_height;
543 int from_row, to_row;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000544
545 // scrolling range is inclusive
546 window_height = terminal->margin_bottom - terminal->margin_top + 1;
547 d = d % (window_height + 1);
548 if(d < 0) {
549 d = 0 - d;
550 to_row = terminal->margin_bottom;
551 from_row = terminal->margin_bottom - d;
552
553 for (i = 0; i < (window_height - d); i++) {
554 memcpy(terminal_get_row(terminal, to_row - i),
555 terminal_get_row(terminal, from_row - i),
556 terminal->data_pitch);
557 memcpy(terminal_get_attr_row(terminal, to_row - i),
558 terminal_get_attr_row(terminal, from_row - i),
559 terminal->attr_pitch);
560 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000561 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
562 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000563 attr_init(terminal_get_attr_row(terminal, i),
564 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000565 }
566 } else {
567 to_row = terminal->margin_top;
568 from_row = terminal->margin_top + d;
569
570 for (i = 0; i < (window_height - d); i++) {
571 memcpy(terminal_get_row(terminal, to_row + i),
572 terminal_get_row(terminal, from_row + i),
573 terminal->data_pitch);
574 memcpy(terminal_get_attr_row(terminal, to_row + i),
575 terminal_get_attr_row(terminal, from_row + i),
576 terminal->attr_pitch);
577 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000578 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
579 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000580 attr_init(terminal_get_attr_row(terminal, i),
581 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000582 }
583 }
584}
585
586static void
587terminal_scroll(struct terminal *terminal, int d)
588{
589 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
590 terminal_scroll_buffer(terminal, d);
591 else
592 terminal_scroll_window(terminal, d);
593}
594
595static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000596terminal_shift_line(struct terminal *terminal, int d)
597{
598 union utf8_char *row;
599 struct attr *attr_row, attr;
600
601 row = terminal_get_row(terminal, terminal->row);
602 attr_row = terminal_get_attr_row(terminal, terminal->row);
603
604 if ((terminal->width + d) <= terminal->column)
605 d = terminal->column + 1 - terminal->width;
606 if ((terminal->column + d) >= terminal->width)
607 d = terminal->width - terminal->column - 1;
608
609 if (d < 0) {
610 d = 0 - d;
611 memmove(&row[terminal->column],
612 &row[terminal->column + d],
613 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
614 attr = attr_row[terminal->width - 1];
615 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
616 (terminal->width - terminal->column - d) * sizeof(struct attr));
617 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
618 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
619 } else {
620 memmove(&row[terminal->column + d], &row[terminal->column],
621 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
622 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
623 (terminal->width - terminal->column - d) * sizeof(struct attr));
624 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
625 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
626 }
627}
628
629static void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500630terminal_resize(struct terminal *terminal, int width, int height)
631{
632 size_t size;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000633 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000634 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000635 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000636 int data_pitch, attr_pitch;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500637 int i, l, total_rows, start;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500638 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000639 struct winsize ws;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500640 int32_t pixel_width, pixel_height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500641
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500642 if (width < 1)
643 width = 1;
644 if (height < 1)
645 height = 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500646 if (terminal->width == width && terminal->height == height)
647 return;
648
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500649 if (!terminal->fullscreen) {
650 pixel_width = width *
651 terminal->extents.max_x_advance + 2 * terminal->margin;
652 pixel_height = height *
653 terminal->extents.height + 2 * terminal->margin;
654 window_set_child_size(terminal->window,
655 pixel_width, pixel_height);
656 }
657
658 window_schedule_redraw (terminal->window);
659
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000660 data_pitch = width * sizeof(union utf8_char);
661 size = data_pitch * height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500662 data = malloc(size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000663 attr_pitch = width * sizeof(struct attr);
664 data_attr = malloc(attr_pitch * height);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000665 tab_ruler = malloc(width);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500666 memset(data, 0, size);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000667 memset(tab_ruler, 0, width);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000668 attr_init(data_attr, terminal->curr_attr, width * height);
669 if (terminal->data && terminal->data_attr) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500670 if (width > terminal->width)
671 l = terminal->width;
672 else
673 l = width;
674
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500675 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500676 total_rows = height;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500677 start = terminal->height - height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500678 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500679 total_rows = terminal->height;
680 start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500681 }
682
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000683 for (i = 0; i < total_rows; i++) {
684 memcpy(&data[width * i],
685 terminal_get_row(terminal, i),
686 l * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000687 memcpy(&data_attr[width * i],
688 terminal_get_attr_row(terminal, i),
689 l * sizeof(struct attr));
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000690 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500691
692 free(terminal->data);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000693 free(terminal->data_attr);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000694 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500695 }
696
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000697 terminal->data_pitch = data_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000698 terminal->attr_pitch = attr_pitch;
Callum Lowcay86653ed2011-01-07 19:47:03 +0000699 terminal->margin_bottom =
700 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500701 terminal->width = width;
702 terminal->height = height;
703 terminal->data = data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000704 terminal->data_attr = data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000705 terminal->tab_ruler = tab_ruler;
706 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500707
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000708 /* Update the window size */
709 ws.ws_row = terminal->height;
710 ws.ws_col = terminal->width;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500711 window_get_child_allocation(terminal->window, &allocation);
712 ws.ws_xpixel = allocation.width;
713 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000714 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500715}
716
Callum Lowcay30eeae52011-01-07 19:46:55 +0000717struct color_scheme DEFAULT_COLORS = {
718 {
719 {0, 0, 0, 1}, /* black */
720 {0.66, 0, 0, 1}, /* red */
721 {0 , 0.66, 0, 1}, /* green */
722 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
723 {0 , 0 , 0.66, 1}, /* blue */
724 {0.66, 0 , 0.66, 1}, /* magenta */
725 {0, 0.66, 0.66, 1}, /* cyan */
726 {0.66, 0.66, 0.66, 1}, /* light grey */
727 {0.22, 0.33, 0.33, 1}, /* dark grey */
728 {1, 0.33, 0.33, 1}, /* high red */
729 {0.33, 1, 0.33, 1}, /* high green */
730 {1, 1, 0.33, 1}, /* high yellow */
731 {0.33, 0.33, 1, 1}, /* high blue */
732 {1, 0.33, 1, 1}, /* high magenta */
733 {0.33, 1, 1, 1}, /* high cyan */
734 {1, 1, 1, 1} /* white */
735 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500736 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000737 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
738};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400739
Kristian Høgsberg22106762008-12-08 13:50:07 -0500740static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500741terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
742{
743 cairo_set_source_rgba(cr,
744 terminal->color_table[index].r,
745 terminal->color_table[index].g,
746 terminal->color_table[index].b,
747 terminal->color_table[index].a);
748}
749
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500750struct glyph_run {
751 struct terminal *terminal;
752 cairo_t *cr;
753 int count;
754 union decoded_attr attr;
755 cairo_glyph_t glyphs[256], *g;
756};
757
758static void
759glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
760{
761 run->terminal = terminal;
762 run->cr = cr;
763 run->g = run->glyphs;
764 run->count = 0;
765 run->attr.key = 0;
766}
767
768static void
769glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
770{
771 cairo_scaled_font_t *font;
772
773 if (run->count == 0)
774 run->attr = attr;
775 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
776 (attr.key != run->attr.key)) {
777 if (run->attr.bold)
778 font = run->terminal->font_bold;
779 else
780 font = run->terminal->font_normal;
781 cairo_set_scaled_font(run->cr, font);
782 terminal_set_color(run->terminal, run->cr,
783 run->attr.foreground);
784
785 cairo_show_glyphs (run->cr, run->glyphs, run->count);
786 run->g = run->glyphs;
787 run->count = 0;
788 }
789}
790
791static void
792glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
793{
794 int num_glyphs;
795 cairo_scaled_font_t *font;
796
797 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
798
799 if (run->attr.bold)
800 font = run->terminal->font_bold;
801 else
802 font = run->terminal->font_normal;
803
804 cairo_move_to(run->cr, x, y);
805 cairo_scaled_font_text_to_glyphs (font, x, y,
806 (char *) c->byte, 4,
807 &run->g, &num_glyphs,
808 NULL, NULL, NULL);
809 run->g += num_glyphs;
810 run->count += num_glyphs;
811}
812
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500813static void
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500814terminal_draw_contents(struct terminal *terminal)
815{
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500816 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500817 cairo_t *cr;
818 cairo_font_extents_t extents;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000819 int top_margin, side_margin;
820 int row, col;
821 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500822 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000823 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500824 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500825 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500826 struct glyph_run run;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000827
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500828 window_get_child_allocation(terminal->window, &allocation);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500829
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500830 surface = display_create_surface(terminal->display, &allocation);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500831 cr = cairo_create(surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500832 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500833 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500834 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500835
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500836 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500837
838 cairo_font_extents(cr, &extents);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500839 side_margin = (allocation.width - terminal->width * extents.max_x_advance) / 2;
840 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500841
Callum Lowcay30eeae52011-01-07 19:46:55 +0000842 cairo_set_line_width(cr, 1.0);
843
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500844 /* paint the background */
845 for (row = 0; row < terminal->height; row++) {
846 p_row = terminal_get_row(terminal, row);
847 for (col = 0; col < terminal->width; col++) {
848 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500849 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500850
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500851 if (attr.background == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -0500852 continue;
853
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500854 terminal_set_color(terminal, cr, attr.background);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500855 cairo_move_to(cr, side_margin + (col * extents.max_x_advance),
856 top_margin + (row * extents.height));
857 cairo_rel_line_to(cr, extents.max_x_advance, 0);
858 cairo_rel_line_to(cr, 0, extents.height);
859 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
860 cairo_close_path(cr);
861 cairo_fill(cr);
862 }
863 }
864
865 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
866
867 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500868 glyph_run_init(&run, terminal, cr);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000869 for (row = 0; row < terminal->height; row++) {
870 p_row = terminal_get_row(terminal, row);
871 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000872 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500873 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000874
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500875 glyph_run_flush(&run, attr);
876
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000877 text_x = side_margin + col * extents.max_x_advance;
878 text_y = top_margin + extents.ascent + row * extents.height;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500879 if (attr.underline) {
Callum Lowcay86653ed2011-01-07 19:47:03 +0000880 cairo_move_to(cr, text_x, (double)text_y + 1.5);
881 cairo_line_to(cr, text_x + extents.max_x_advance, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000882 cairo_stroke(cr);
883 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -0500884
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500885 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000886 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500887 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500888
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500889 attr.key = ~0;
890 glyph_run_flush(&run, attr);
891
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000892 if ((terminal->mode & MODE_SHOW_CURSOR) && !terminal->focused) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000893 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500894
Callum Lowcay30eeae52011-01-07 19:46:55 +0000895 cairo_set_line_width(cr, 1);
896 cairo_move_to(cr, side_margin + terminal->column * extents.max_x_advance + d,
897 top_margin + terminal->row * extents.height + d);
898 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
899 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
900 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
901 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500902
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500903 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000904 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500905
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500906 cairo_destroy(cr);
907
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500908 window_copy_surface(terminal->window, &allocation, surface);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500909
910 cairo_surface_destroy(surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500911}
912
913static void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500914resize_handler(struct window *window,
915 int32_t pixel_width, int32_t pixel_height, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500916{
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500917 struct terminal *terminal = data;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500918 int32_t width, height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500919
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500920 width = (pixel_width - 2 * terminal->margin) /
Kristian Høgsberg09531622010-06-14 23:22:15 -0400921 (int32_t) terminal->extents.max_x_advance;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500922 height = (pixel_height - 2 * terminal->margin) /
Kristian Høgsberg09531622010-06-14 23:22:15 -0400923 (int32_t) terminal->extents.height;
Tiago Vignatti5fd89d22011-01-10 19:30:04 +0200924
Kristian Høgsberg22106762008-12-08 13:50:07 -0500925 terminal_resize(terminal, width, height);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500926}
Kristian Høgsberg22106762008-12-08 13:50:07 -0500927
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500928static void
929terminal_draw(struct terminal *terminal)
930{
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500931 window_draw(terminal->window);
932 terminal_draw_contents(terminal);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400933 window_flush(terminal->window);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500934}
935
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400936static void
937redraw_handler(struct window *window, void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500938{
939 struct terminal *terminal = data;
940
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500941 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500942}
943
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500944#define STATE_NORMAL 0
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500945#define STATE_ESCAPE 1
Callum Lowcayb8609ad2011-01-07 19:46:57 +0000946#define STATE_ESCAPE_SPECIAL 2
947#define STATE_ESCAPE_CSI 3
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500948
949static void
950terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500951
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500952static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +0000953handle_char(struct terminal *terminal, union utf8_char utf8);
954
955static void
Callum Lowcay30eeae52011-01-07 19:46:55 +0000956handle_sgr(struct terminal *terminal, int code);
957
958static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000959handle_term_parameter(struct terminal *terminal, int code, int sr)
960{
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000961 int i;
962
Callum Lowcaybbeac602011-01-07 19:46:58 +0000963 if (terminal->qmark_flag) {
964 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +0000965 case 1: /* DECCKM */
966 if (sr) terminal->key_mode = KM_APPLICATION;
967 else terminal->key_mode = KM_NORMAL;
968 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000969 case 2: /* DECANM */
970 /* No VT52 support yet */
971 terminal->g0 = CS_US;
972 terminal->g1 = CS_US;
973 terminal->cs = terminal->g0;
974 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000975 case 3: /* DECCOLM */
976 if (sr)
977 terminal_resize(terminal, 132, 24);
978 else
979 terminal_resize(terminal, 80, 24);
980
981 /* set columns, but also home cursor and clear screen */
982 terminal->row = 0; terminal->column = 0;
983 for (i = 0; i < terminal->height; i++) {
984 memset(terminal_get_row(terminal, i),
985 0, terminal->data_pitch);
986 attr_init(terminal_get_attr_row(terminal, i),
987 terminal->curr_attr, terminal->width);
988 }
989 break;
990 case 5: /* DECSCNM */
991 if (sr) terminal->mode |= MODE_INVERSE;
992 else terminal->mode &= ~MODE_INVERSE;
993 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000994 case 6: /* DECOM */
995 terminal->origin_mode = sr;
996 if (terminal->origin_mode)
997 terminal->row = terminal->margin_top;
998 else
999 terminal->row = 0;
1000 terminal->column = 0;
1001 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001002 case 7: /* DECAWM */
1003 if (sr) terminal->mode |= MODE_AUTOWRAP;
1004 else terminal->mode &= ~MODE_AUTOWRAP;
1005 break;
1006 case 8: /* DECARM */
1007 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1008 else terminal->mode &= ~MODE_AUTOREPEAT;
1009 break;
1010 case 25:
1011 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1012 else terminal->mode &= ~MODE_SHOW_CURSOR;
1013 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001014 case 1037: /* deleteSendsDel */
1015 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1016 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1017 break;
1018 case 1039: /* altSendsEscape */
1019 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1020 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1021 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001022 default:
1023 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1024 break;
1025 }
1026 } else {
1027 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001028 case 4: /* IRM */
1029 if (sr) terminal->mode |= MODE_IRM;
1030 else terminal->mode &= ~MODE_IRM;
1031 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001032 case 20: /* LNM */
1033 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1034 else terminal->mode &= ~MODE_LF_NEWLINE;
1035 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001036 default:
1037 fprintf(stderr, "Unknown parameter: %d\n", code);
1038 break;
1039 }
1040 }
1041}
1042
1043static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001044handle_escape(struct terminal *terminal)
1045{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001046 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001047 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001048 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001049 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001050 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001051 char response[MAX_RESPONSE] = {0, };
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001052
1053 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001054 i = 0;
1055 p = &terminal->escape[2];
1056 while ((isdigit(*p) || *p == ';') && i < 10) {
1057 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001058 if (!set[i]) {
1059 args[i] = 0;
1060 set[i] = 1;
1061 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001062 p++;
1063 i++;
1064 } else {
1065 args[i] = strtol(p, &p, 10);
1066 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001067 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001068 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001069
1070 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001071 case '@': /* ICH */
1072 count = set[0] ? args[0] : 1;
1073 if (count == 0) count = 1;
1074 terminal_shift_line(terminal, count);
1075 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001076 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001077 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001078 if (count == 0) count = 1;
1079 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001080 terminal->row -= count;
1081 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001082 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001083 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001084 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001085 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001086 if (count == 0) count = 1;
1087 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001088 terminal->row += count;
1089 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001090 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001091 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001092 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001093 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001094 if (count == 0) count = 1;
1095 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001096 terminal->column += count;
1097 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001098 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001099 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001100 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001101 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001102 if (count == 0) count = 1;
1103 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001104 terminal->column -= count;
1105 else
1106 terminal->column = 0;
1107 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001108 case 'E': /* CNL */
1109 count = set[0] ? args[0] : 1;
1110 if (terminal->row + count <= terminal->margin_bottom)
1111 terminal->row += count;
1112 else
1113 terminal->row = terminal->margin_bottom;
1114 terminal->column = 0;
1115 break;
1116 case 'F': /* CPL */
1117 count = set[0] ? args[0] : 1;
1118 if (terminal->row - count >= terminal->margin_top)
1119 terminal->row -= count;
1120 else
1121 terminal->row = terminal->margin_top;
1122 terminal->column = 0;
1123 break;
1124 case 'G': /* CHA */
1125 y = set[0] ? args[0] : 1;
1126 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1127
1128 terminal->column = y - 1;
1129 break;
1130 case 'f': /* HVP */
1131 case 'H': /* CUP */
1132 x = (set[1] ? args[1] : 1) - 1;
1133 x = x < 0 ? 0 :
1134 (x >= terminal->width ? terminal->width - 1 : x);
1135
1136 y = (set[0] ? args[0] : 1) - 1;
1137 if (terminal->origin_mode) {
1138 y += terminal->margin_top;
1139 y = y < terminal->margin_top ? terminal->margin_top :
1140 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1141 } else {
1142 y = y < 0 ? 0 :
1143 (y >= terminal->height ? terminal->height - 1 : y);
1144 }
1145
1146 terminal->row = y;
1147 terminal->column = x;
1148 break;
1149 case 'I': /* CHT */
1150 count = set[0] ? args[0] : 1;
1151 if (count == 0) count = 1;
1152 while (count > 0 && terminal->column < terminal->width) {
1153 if (terminal->tab_ruler[terminal->column]) count--;
1154 terminal->column++;
1155 }
1156 terminal->column--;
1157 break;
1158 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001159 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001160 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001161 if (!set[0] || args[0] == 0 || args[0] > 2) {
1162 memset(&row[terminal->column],
1163 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1164 attr_init(&attr_row[terminal->column],
1165 terminal->curr_attr, terminal->width - terminal->column);
1166 for (i = terminal->row + 1; i < terminal->height; i++) {
1167 memset(terminal_get_row(terminal, i),
1168 0, terminal->data_pitch);
1169 attr_init(terminal_get_attr_row(terminal, i),
1170 terminal->curr_attr, terminal->width);
1171 }
1172 } else if (args[0] == 1) {
1173 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1174 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1175 for (i = 0; i < terminal->row; i++) {
1176 memset(terminal_get_row(terminal, i),
1177 0, terminal->data_pitch);
1178 attr_init(terminal_get_attr_row(terminal, i),
1179 terminal->curr_attr, terminal->width);
1180 }
1181 } else if (args[0] == 2) {
1182 for (i = 0; i < terminal->height; i++) {
1183 memset(terminal_get_row(terminal, i),
1184 0, terminal->data_pitch);
1185 attr_init(terminal_get_attr_row(terminal, i),
1186 terminal->curr_attr, terminal->width);
1187 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001188 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001189 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001190 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001191 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001192 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001193 if (!set[0] || args[0] == 0 || args[0] > 2) {
1194 memset(&row[terminal->column], 0,
1195 (terminal->width - terminal->column) * sizeof(union utf8_char));
1196 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1197 terminal->width - terminal->column);
1198 } else if (args[0] == 1) {
1199 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1200 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1201 } else if (args[0] == 2) {
1202 memset(row, 0, terminal->data_pitch);
1203 attr_init(attr_row, terminal->curr_attr, terminal->width);
1204 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001205 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001206 case 'L': /* IL */
1207 count = set[0] ? args[0] : 1;
1208 if (count == 0) count = 1;
1209 if (terminal->row >= terminal->margin_top &&
1210 terminal->row < terminal->margin_bottom)
1211 {
1212 top = terminal->margin_top;
1213 terminal->margin_top = terminal->row;
1214 terminal_scroll(terminal, 0 - count);
1215 terminal->margin_top = top;
1216 } else if (terminal->row == terminal->margin_bottom) {
1217 memset(terminal_get_row(terminal, terminal->row),
1218 0, terminal->data_pitch);
1219 attr_init(terminal_get_attr_row(terminal, terminal->row),
1220 terminal->curr_attr, terminal->width);
1221 }
1222 break;
1223 case 'M': /* DL */
1224 count = set[0] ? args[0] : 1;
1225 if (count == 0) count = 1;
1226 if (terminal->row >= terminal->margin_top &&
1227 terminal->row < terminal->margin_bottom)
1228 {
1229 top = terminal->margin_top;
1230 terminal->margin_top = terminal->row;
1231 terminal_scroll(terminal, count);
1232 terminal->margin_top = top;
1233 } else if (terminal->row == terminal->margin_bottom) {
1234 memset(terminal_get_row(terminal, terminal->row),
1235 0, terminal->data_pitch);
1236 }
1237 break;
1238 case 'P': /* DCH */
1239 count = set[0] ? args[0] : 1;
1240 if (count == 0) count = 1;
1241 terminal_shift_line(terminal, 0 - count);
1242 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001243 case 'S': /* SU */
1244 terminal_scroll(terminal, set[0] ? args[0] : 1);
1245 break;
1246 case 'T': /* SD */
1247 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1248 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001249 case 'X': /* ECH */
1250 count = set[0] ? args[0] : 1;
1251 if (count == 0) count = 1;
1252 if ((terminal->column + count) > terminal->width)
1253 count = terminal->width - terminal->column;
1254 row = terminal_get_row(terminal, terminal->row);
1255 attr_row = terminal_get_attr_row(terminal, terminal->row);
1256 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1257 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1258 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001259 case 'Z': /* CBT */
1260 count = set[0] ? args[0] : 1;
1261 if (count == 0) count = 1;
1262 while (count > 0 && terminal->column >= 0) {
1263 if (terminal->tab_ruler[terminal->column]) count--;
1264 terminal->column--;
1265 }
1266 terminal->column++;
1267 break;
1268 case '`': /* HPA */
1269 y = set[0] ? args[0] : 1;
1270 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1271
1272 terminal->column = y - 1;
1273 break;
1274 case 'b': /* REP */
1275 count = set[0] ? args[0] : 1;
1276 if (count == 0) count = 1;
1277 if (terminal->last_char.byte[0])
1278 for (i = 0; i < count; i++)
1279 handle_char(terminal, terminal->last_char);
1280 terminal->last_char.byte[0] = 0;
1281 break;
1282 case 'c': /* Primary DA */
Callum Lowcay69e96582011-01-07 19:47:00 +00001283 write(terminal->master, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001284 break;
1285 case 'd': /* VPA */
1286 x = set[0] ? args[0] : 1;
1287 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1288
1289 terminal->row = x - 1;
1290 break;
1291 case 'g': /* TBC */
1292 if (!set[0] || args[0] == 0) {
1293 terminal->tab_ruler[terminal->column] = 0;
1294 } else if (args[0] == 3) {
1295 memset(terminal->tab_ruler, 0, terminal->width);
1296 }
1297 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001298 case 'h': /* SM */
1299 for(i = 0; i < 10 && set[i]; i++) {
1300 handle_term_parameter(terminal, args[i], 1);
1301 }
1302 break;
1303 case 'l': /* RM */
1304 for(i = 0; i < 10 && set[i]; i++) {
1305 handle_term_parameter(terminal, args[i], 0);
1306 }
1307 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001308 case 'm': /* SGR */
1309 if (set[0] && set[1] && set[2] && args[1] == 5) {
1310 if (args[0] == 38) {
1311 handle_sgr(terminal, args[2] + 256);
1312 break;
1313 } else if (args[0] == 48) {
1314 handle_sgr(terminal, args[2] + 512);
1315 break;
1316 }
1317 }
1318 for(i = 0; i < 10; i++) {
1319 if(set[i]) {
1320 handle_sgr(terminal, args[i]);
1321 } else if(i == 0) {
1322 handle_sgr(terminal, 0);
1323 break;
1324 } else {
1325 break;
1326 }
1327 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001328 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001329 case 'n': /* DSR */
1330 i = set[0] ? args[0] : 0;
1331 if (i == 0 || i == 5) {
1332 write(terminal->master, "\e[0n", 4);
1333 } else if (i == 6) {
1334 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1335 terminal->origin_mode ?
1336 terminal->row+terminal->margin_top : terminal->row+1,
1337 terminal->column+1);
1338 write(terminal->master, response, strlen(response));
1339 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001340 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001341 case 'r':
1342 if(!set[0]) {
1343 terminal->margin_top = 0;
1344 terminal->margin_bottom = terminal->height-1;
1345 terminal->row = 0;
1346 terminal->column = 0;
1347 } else {
1348 top = (set[0] ? args[0] : 1) - 1;
1349 top = top < 0 ? 0 :
1350 (top >= terminal->height ? terminal->height - 1 : top);
1351 bottom = (set[1] ? args[1] : 1) - 1;
1352 bottom = bottom < 0 ? 0 :
1353 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1354 if(bottom > top) {
1355 terminal->margin_top = top;
1356 terminal->margin_bottom = bottom;
1357 } else {
1358 terminal->margin_top = 0;
1359 terminal->margin_bottom = terminal->height-1;
1360 }
1361 if(terminal->origin_mode)
1362 terminal->row = terminal->margin_top;
1363 else
1364 terminal->row = 0;
1365 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001366 }
1367 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001368 case 's':
1369 terminal->saved_row = terminal->row;
1370 terminal->saved_column = terminal->column;
1371 break;
1372 case 'u':
1373 terminal->row = terminal->saved_row;
1374 terminal->column = terminal->saved_column;
1375 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001376 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001377 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001378 break;
1379 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001380}
1381
1382static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001383handle_non_csi_escape(struct terminal *terminal, char code)
1384{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001385 switch(code) {
1386 case 'M': /* RI */
1387 terminal->row -= 1;
1388 if(terminal->row < terminal->margin_top) {
1389 terminal->row = terminal->margin_top;
1390 terminal_scroll(terminal, -1);
1391 }
1392 break;
1393 case 'E': /* NEL */
1394 terminal->column = 0;
1395 // fallthrough
1396 case 'D': /* IND */
1397 terminal->row += 1;
1398 if(terminal->row > terminal->margin_bottom) {
1399 terminal->row = terminal->margin_bottom;
1400 terminal_scroll(terminal, +1);
1401 }
1402 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001403 case 'c': /* RIS */
1404 terminal_init(terminal);
1405 break;
1406 case 'H': /* HTS */
1407 terminal->tab_ruler[terminal->column] = 1;
1408 break;
1409 case '7': /* DECSC */
1410 terminal->saved_row = terminal->row;
1411 terminal->saved_column = terminal->column;
1412 terminal->saved_attr = terminal->curr_attr;
1413 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001414 terminal->saved_cs = terminal->cs;
1415 terminal->saved_g0 = terminal->g0;
1416 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001417 break;
1418 case '8': /* DECRC */
1419 terminal->row = terminal->saved_row;
1420 terminal->column = terminal->saved_column;
1421 terminal->curr_attr = terminal->saved_attr;
1422 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001423 terminal->cs = terminal->saved_cs;
1424 terminal->g0 = terminal->saved_g0;
1425 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001426 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001427 case '=': /* DECPAM */
1428 terminal->key_mode = KM_APPLICATION;
1429 break;
1430 case '>': /* DECPNM */
1431 terminal->key_mode = KM_NORMAL;
1432 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001433 default:
1434 fprintf(stderr, "Unknown escape code: %c\n", code);
1435 break;
1436 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001437}
1438
1439static void
1440handle_special_escape(struct terminal *terminal, char special, char code)
1441{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001442 int i, numChars;
1443
1444 if (special == '#') {
1445 switch(code) {
1446 case '8':
1447 /* fill with 'E', no cheap way to do this */
1448 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1449 numChars = terminal->width * terminal->height;
1450 for(i = 0; i < numChars; i++) {
1451 terminal->data[i].byte[0] = 'E';
1452 }
1453 break;
1454 default:
1455 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1456 break;
1457 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001458 } else if (special == '(' || special == ')') {
1459 switch(code) {
1460 case '0':
1461 if (special == '(')
1462 terminal->g0 = CS_SPECIAL;
1463 else
1464 terminal->g1 = CS_SPECIAL;
1465 break;
1466 case 'A':
1467 if (special == '(')
1468 terminal->g0 = CS_UK;
1469 else
1470 terminal->g1 = CS_UK;
1471 break;
1472 case 'B':
1473 if (special == '(')
1474 terminal->g0 = CS_US;
1475 else
1476 terminal->g1 = CS_US;
1477 break;
1478 default:
1479 fprintf(stderr, "Unknown character set %c\n", code);
1480 break;
1481 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001482 } else {
1483 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1484 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001485}
1486
1487static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001488handle_sgr(struct terminal *terminal, int code)
1489{
1490 switch(code) {
1491 case 0:
1492 terminal->curr_attr = terminal->color_scheme->default_attr;
1493 break;
1494 case 1:
1495 terminal->curr_attr.a |= ATTRMASK_BOLD;
1496 if (terminal->curr_attr.fg < 8)
1497 terminal->curr_attr.fg += 8;
1498 break;
1499 case 4:
1500 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1501 break;
1502 case 5:
1503 terminal->curr_attr.a |= ATTRMASK_BLINK;
1504 break;
1505 case 2:
1506 case 21:
1507 case 22:
1508 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1509 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1510 terminal->curr_attr.fg -= 8;
1511 break;
1512 case 24:
1513 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1514 break;
1515 case 25:
1516 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1517 break;
1518 case 7:
1519 case 26:
1520 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1521 break;
1522 case 27:
1523 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1524 break;
1525 case 39:
1526 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1527 break;
1528 case 49:
1529 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1530 break;
1531 default:
1532 if(code >= 30 && code <= 37) {
1533 terminal->curr_attr.fg = code - 30;
1534 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1535 terminal->curr_attr.fg += 8;
1536 } else if(code >= 40 && code <= 47) {
1537 terminal->curr_attr.bg = code - 40;
1538 } else if(code >= 256 && code < 512) {
1539 terminal->curr_attr.fg = code - 256;
1540 } else if(code >= 512 && code < 768) {
1541 terminal->curr_attr.bg = code - 512;
1542 } else {
1543 fprintf(stderr, "Unknown SGR code: %d\n", code);
1544 }
1545 break;
1546 }
1547}
1548
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001549/* Returns 1 if c was special, otherwise 0 */
1550static int
1551handle_special_char(struct terminal *terminal, char c)
1552{
1553 union utf8_char *row;
1554 struct attr *attr_row;
1555
1556 row = terminal_get_row(terminal, terminal->row);
1557 attr_row = terminal_get_attr_row(terminal, terminal->row);
1558
1559 switch(c) {
1560 case '\r':
1561 terminal->column = 0;
1562 break;
1563 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001564 if (terminal->mode & MODE_LF_NEWLINE) {
1565 terminal->column = 0;
1566 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001567 /* fallthrough */
1568 case '\v':
1569 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001570 terminal->row++;
1571 if(terminal->row > terminal->margin_bottom) {
1572 terminal->row = terminal->margin_bottom;
1573 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001574 }
1575
1576 break;
1577 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001578 while (terminal->column < terminal->width) {
1579 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001580 if (terminal->mode & MODE_IRM)
1581 terminal_shift_line(terminal, +1);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001582 row[terminal->column].byte[0] = ' ';
1583 row[terminal->column].byte[1] = '\0';
1584 attr_row[terminal->column] = terminal->curr_attr;
1585 terminal->column++;
1586 }
1587 if (terminal->column >= terminal->width) {
1588 terminal->column = terminal->width - 1;
1589 }
1590
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001591 break;
1592 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001593 if (terminal->column >= terminal->width) {
1594 terminal->column = terminal->width - 2;
1595 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001596 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001597 } else if (terminal->mode & MODE_AUTOWRAP) {
1598 terminal->column = terminal->width - 1;
1599 terminal->row -= 1;
1600 if (terminal->row < terminal->margin_top) {
1601 terminal->row = terminal->margin_top;
1602 terminal_scroll(terminal, -1);
1603 }
1604 }
1605
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001606 break;
1607 case '\a':
1608 /* Bell */
1609 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001610 case '\x0E': /* SO */
1611 terminal->cs = terminal->g1;
1612 break;
1613 case '\x0F': /* SI */
1614 terminal->cs = terminal->g0;
1615 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001616 default:
1617 return 0;
1618 }
1619
1620 return 1;
1621}
1622
1623static void
1624handle_char(struct terminal *terminal, union utf8_char utf8)
1625{
1626 union utf8_char *row;
1627 struct attr *attr_row;
1628
1629 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001630
1631 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001632
1633 /* There are a whole lot of non-characters, control codes,
1634 * and formatting codes that should probably be ignored,
1635 * for example: */
1636 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1637 /* BOM, ignore */
1638 return;
1639 }
1640
1641 /* Some of these non-characters should be translated, e.g.: */
1642 if (utf8.byte[0] < 32) {
1643 utf8.byte[0] = utf8.byte[0] + 64;
1644 }
1645
1646 /* handle right margin effects */
1647 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001648 if (terminal->mode & MODE_AUTOWRAP) {
1649 terminal->column = 0;
1650 terminal->row += 1;
1651 if (terminal->row > terminal->margin_bottom) {
1652 terminal->row = terminal->margin_bottom;
1653 terminal_scroll(terminal, +1);
1654 }
1655 } else {
1656 terminal->column--;
1657 }
1658 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001659
1660 row = terminal_get_row(terminal, terminal->row);
1661 attr_row = terminal_get_attr_row(terminal, terminal->row);
1662
Callum Lowcay69e96582011-01-07 19:47:00 +00001663 if (terminal->mode & MODE_IRM)
1664 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001665 row[terminal->column] = utf8;
1666 attr_row[terminal->column++] = terminal->curr_attr;
1667
1668 if (utf8.ch != terminal->last_char.ch)
1669 terminal->last_char = utf8;
1670}
1671
Callum Lowcay30eeae52011-01-07 19:46:55 +00001672static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001673terminal_data(struct terminal *terminal, const char *data, size_t length)
1674{
1675 int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001676 union utf8_char utf8;
1677 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001678
1679 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001680 parser_state =
1681 utf8_next_char(&terminal->state_machine, data[i]);
1682 switch(parser_state) {
1683 case utf8state_accept:
1684 utf8.ch = terminal->state_machine.s.ch;
1685 break;
1686 case utf8state_reject:
1687 /* the unicode replacement character */
1688 utf8.byte[0] = 0xEF;
1689 utf8.byte[1] = 0xBF;
1690 utf8.byte[2] = 0xBD;
1691 utf8.byte[3] = 0x00;
1692 break;
1693 default:
1694 continue;
1695 }
1696
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001697 /* assume escape codes never use non-ASCII characters */
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001698 if (terminal->state == STATE_ESCAPE) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001699 terminal->escape[terminal->escape_length++] = utf8.byte[0];
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001700 if (utf8.byte[0] == '[') {
1701 terminal->state = STATE_ESCAPE_CSI;
1702 continue;
1703 } else if (utf8.byte[0] == '#' || utf8.byte[0] == '(' ||
1704 utf8.byte[0] == ')')
1705 {
1706 terminal->state = STATE_ESCAPE_SPECIAL;
1707 continue;
1708 } else {
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001709 terminal->state = STATE_NORMAL;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001710 handle_non_csi_escape(terminal, utf8.byte[0]);
1711 continue;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001712 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001713 } else if (terminal->state == STATE_ESCAPE_SPECIAL) {
1714 terminal->escape[terminal->escape_length++] = utf8.byte[0];
1715 terminal->state = STATE_NORMAL;
1716 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
1717 handle_special_escape(terminal, terminal->escape[1],
1718 utf8.byte[0]);
1719 continue;
1720 }
1721 } else if (terminal->state == STATE_ESCAPE_CSI) {
1722 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1723 /* do nothing */
1724 } else if (utf8.byte[0] == '?') {
1725 terminal->qmark_flag = 1;
1726 } else {
1727 /* Don't overflow the buffer */
1728 if (terminal->escape_length < MAX_ESCAPE)
1729 terminal->escape[terminal->escape_length++] = utf8.byte[0];
1730 if (terminal->escape_length >= MAX_ESCAPE)
1731 terminal->state = STATE_NORMAL;
1732 }
1733
1734 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1735 utf8.byte[0] == '`')
1736 {
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001737 terminal->state = STATE_NORMAL;
1738 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001739 continue;
1740 } else {
1741 continue;
1742 }
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001743 }
1744
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001745 /* this is valid, because ASCII characters are never used to
1746 * introduce a multibyte sequence in UTF-8 */
1747 if (utf8.byte[0] == '\e') {
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001748 terminal->state = STATE_ESCAPE;
1749 terminal->escape[0] = '\e';
1750 terminal->escape_length = 1;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001751 terminal->qmark_flag = 0;
1752 } else {
1753 handle_char(terminal, utf8);
1754 } /* if */
1755 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05001756
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001757 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001758}
1759
1760static void
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001761key_handler(struct window *window, uint32_t key, uint32_t sym,
Kristian Høgsberg55444912009-02-21 14:31:09 -05001762 uint32_t state, uint32_t modifiers, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001763{
1764 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001765 char ch[MAX_RESPONSE];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001766 int len = 0;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001767
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001768 switch (sym) {
1769 case XK_F11:
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001770 if (!state)
1771 break;
1772 terminal->fullscreen ^= 1;
1773 window_set_fullscreen(window, terminal->fullscreen);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001774 window_schedule_redraw(terminal->window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001775 break;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001776
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001777 case XK_BackSpace:
1778 case XK_Tab:
1779 case XK_Linefeed:
1780 case XK_Clear:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001781 case XK_Pause:
1782 case XK_Scroll_Lock:
1783 case XK_Sys_Req:
1784 case XK_Escape:
1785 ch[len++] = sym & 0x7f;
1786 break;
1787
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001788 case XK_Return:
1789 if (terminal->mode & MODE_LF_NEWLINE) {
1790 ch[len++] = 0x0D;
1791 ch[len++] = 0x0A;
1792 } else {
1793 ch[len++] = 0x0D;
1794 }
1795 break;
1796
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001797 case XK_Shift_L:
1798 case XK_Shift_R:
1799 case XK_Control_L:
1800 case XK_Control_R:
1801 case XK_Alt_L:
1802 case XK_Alt_R:
1803 break;
1804
Callum Lowcay7e08e902011-01-07 19:47:02 +00001805 case XK_Insert:
1806 len = function_key_response('[', 2, modifiers, '~', ch);
1807 break;
1808 case XK_Delete:
1809 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
1810 ch[len++] = '\x04';
1811 } else {
1812 len = function_key_response('[', 3, modifiers, '~', ch);
1813 }
1814 break;
1815 case XK_Page_Up:
1816 len = function_key_response('[', 5, modifiers, '~', ch);
1817 break;
1818 case XK_Page_Down:
1819 len = function_key_response('[', 6, modifiers, '~', ch);
1820 break;
1821 case XK_F1:
1822 len = function_key_response('O', 1, modifiers, 'P', ch);
1823 break;
1824 case XK_F2:
1825 len = function_key_response('O', 1, modifiers, 'Q', ch);
1826 break;
1827 case XK_F3:
1828 len = function_key_response('O', 1, modifiers, 'R', ch);
1829 break;
1830 case XK_F4:
1831 len = function_key_response('O', 1, modifiers, 'S', ch);
1832 break;
1833 case XK_F5:
1834 len = function_key_response('[', 15, modifiers, '~', ch);
1835 break;
1836 case XK_F6:
1837 len = function_key_response('[', 17, modifiers, '~', ch);
1838 break;
1839 case XK_F7:
1840 len = function_key_response('[', 18, modifiers, '~', ch);
1841 break;
1842 case XK_F8:
1843 len = function_key_response('[', 19, modifiers, '~', ch);
1844 break;
1845 case XK_F9:
1846 len = function_key_response('[', 20, modifiers, '~', ch);
1847 break;
1848 case XK_F10:
1849 len = function_key_response('[', 21, modifiers, '~', ch);
1850 break;
1851 case XK_F12:
1852 len = function_key_response('[', 24, modifiers, '~', ch);
1853 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001854 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00001855 /* Handle special keys with alternate mappings */
1856 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
1857 if (len != 0) break;
1858
1859 if (modifiers & WINDOW_MODIFIER_CONTROL) {
1860 if (sym >= '3' && sym <= '7')
1861 sym = (sym & 0x1f) + 8;
1862
1863 if (!((sym >= '!' && sym <= '/') ||
1864 (sym >= '8' && sym <= '?') ||
1865 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
1866 else if (sym == '2') sym = 0x00;
1867 else if (sym == '/') sym = 0x1F;
1868 else if (sym == '8' || sym == '?') sym = 0x7F;
1869 } else if ((terminal->mode & MODE_ALT_SENDS_ESC) &&
1870 (modifiers & WINDOW_MODIFIER_ALT))
1871 {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001872 ch[len++] = 0x1b;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001873 } else if (modifiers & WINDOW_MODIFIER_ALT) {
1874 sym = sym | 0x80;
1875 }
1876
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001877 if (sym < 256)
1878 ch[len++] = sym;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001879 break;
1880 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001881
1882 if (state && len > 0)
1883 write(terminal->master, ch, len);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001884}
1885
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001886static void
1887keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04001888 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001889{
1890 struct terminal *terminal = data;
1891
1892 terminal->focused = (device != NULL);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001893 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001894}
1895
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001896static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001897terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001898{
1899 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04001900 cairo_surface_t *surface;
1901 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001902
1903 terminal = malloc(sizeof *terminal);
1904 if (terminal == NULL)
1905 return terminal;
1906
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001907 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001908 terminal->fullscreen = fullscreen;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001909 terminal->color_scheme = &DEFAULT_COLORS;
1910 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00001911 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00001912 terminal->margin_bottom = -1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001913 terminal->window = window_create(display, "Wayland Terminal",
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05001914 500, 400);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001915
1916 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001917 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001918
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001919 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05001920 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001921
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001922 window_set_fullscreen(terminal->window, terminal->fullscreen);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001923 window_set_user_data(terminal->window, terminal);
1924 window_set_redraw_handler(terminal->window, redraw_handler);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001925 window_set_resize_handler(terminal->window, resize_handler);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05001926
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001927 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001928 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04001929 keyboard_focus_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001930
Kristian Høgsberg09531622010-06-14 23:22:15 -04001931 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
1932 cr = cairo_create(surface);
Kristian Høgsberg09531622010-06-14 23:22:15 -04001933 cairo_set_font_size(cr, 14);
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05001934 cairo_select_font_face (cr, "mono",
1935 CAIRO_FONT_SLANT_NORMAL,
1936 CAIRO_FONT_WEIGHT_BOLD);
1937 terminal->font_bold = cairo_get_scaled_font (cr);
1938 cairo_scaled_font_reference(terminal->font_bold);
1939
1940 cairo_select_font_face (cr, "mono",
1941 CAIRO_FONT_SLANT_NORMAL,
1942 CAIRO_FONT_WEIGHT_NORMAL);
1943 terminal->font_normal = cairo_get_scaled_font (cr);
1944 cairo_scaled_font_reference(terminal->font_normal);
1945
Kristian Høgsberg09531622010-06-14 23:22:15 -04001946 cairo_font_extents(cr, &terminal->extents);
1947 cairo_destroy(cr);
1948 cairo_surface_destroy(surface);
1949
Callum Lowcaya0ee21c2011-01-07 19:46:56 +00001950 terminal_resize(terminal, 80, 24);
Kristian Høgsberg22106762008-12-08 13:50:07 -05001951 terminal_draw(terminal);
1952
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001953 return terminal;
1954}
1955
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001956static gboolean
1957io_handler(GIOChannel *source,
1958 GIOCondition condition,
1959 gpointer data)
1960{
1961 struct terminal *terminal = data;
1962 gchar buffer[256];
1963 gsize bytes_read;
1964 GError *error = NULL;
1965
1966 g_io_channel_read_chars(source, buffer, sizeof buffer,
1967 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001968
1969 terminal_data(terminal, buffer, bytes_read);
1970
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001971 return TRUE;
1972}
1973
1974static int
1975terminal_run(struct terminal *terminal, const char *path)
1976{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05001977 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001978 pid_t pid;
1979
1980 pid = forkpty(&master, NULL, NULL, NULL);
1981 if (pid == 0) {
Callum Lowcay86653ed2011-01-07 19:47:03 +00001982 setenv("TERM", "xterm-256color", 1);
1983 setenv("COLORTERM", "xterm-256color", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001984 if (execl(path, path, NULL)) {
1985 printf("exec failed: %m\n");
1986 exit(EXIT_FAILURE);
1987 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05001988 } else if (pid < 0) {
1989 fprintf(stderr, "failed to fork and create pty (%m).\n");
1990 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001991 }
1992
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001993 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001994 terminal->channel = g_io_channel_unix_new(master);
1995 fcntl(master, F_SETFL, O_NONBLOCK);
1996 g_io_add_watch(terminal->channel, G_IO_IN,
1997 io_handler, terminal);
1998
1999 return 0;
2000}
2001
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002002static const GOptionEntry option_entries[] = {
2003 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
2004 &option_fullscreen, "Run in fullscreen mode" },
2005 { NULL }
2006};
2007
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002008int main(int argc, char *argv[])
2009{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002010 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002011 struct terminal *terminal;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002012
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002013 d = display_create(&argc, &argv, option_entries);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002014 if (d == NULL) {
2015 fprintf(stderr, "failed to create display: %m\n");
2016 return -1;
2017 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002018
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002019 terminal = terminal_create(d, option_fullscreen);
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002020 if (terminal_run(terminal, "/bin/bash"))
2021 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002022
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002023 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002024
2025 return 0;
2026}