blob: 49325324891f826381d9fd6b1c71135d6079c19a [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>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040034#include <sys/epoll.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050035
Pekka Paalanen50719bc2011-11-22 14:18:50 +020036#include <wayland-client.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030037#include <wayland-cursor.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050038
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050039#include "window.h"
40
Kristian Høgsberg0395f302008-12-22 12:14:50 -050041static int option_fullscreen;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050042
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050043#define MOD_SHIFT 0x01
44#define MOD_ALT 0x02
45#define MOD_CTRL 0x04
46
Callum Lowcay30eeae52011-01-07 19:46:55 +000047#define ATTRMASK_BOLD 0x01
48#define ATTRMASK_UNDERLINE 0x02
49#define ATTRMASK_BLINK 0x04
50#define ATTRMASK_INVERSE 0x08
Callum Lowcay81179db2011-01-10 12:14:01 +130051#define ATTRMASK_CONCEALED 0x10
Callum Lowcay30eeae52011-01-07 19:46:55 +000052
Callum Lowcayb8609ad2011-01-07 19:46:57 +000053/* Buffer sizes */
Callum Lowcayef57a9b2011-01-14 20:46:23 +130054#define MAX_RESPONSE 256
55#define MAX_ESCAPE 255
Callum Lowcayb8609ad2011-01-07 19:46:57 +000056
Callum Lowcay8e57dd52011-01-07 19:46:59 +000057/* Terminal modes */
58#define MODE_SHOW_CURSOR 0x00000001
59#define MODE_INVERSE 0x00000002
60#define MODE_AUTOWRAP 0x00000004
61#define MODE_AUTOREPEAT 0x00000008
62#define MODE_LF_NEWLINE 0x00000010
Callum Lowcay69e96582011-01-07 19:47:00 +000063#define MODE_IRM 0x00000020
Callum Lowcay7e08e902011-01-07 19:47:02 +000064#define MODE_DELETE_SENDS_DEL 0x00000040
65#define MODE_ALT_SENDS_ESC 0x00000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +000066
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000067union utf8_char {
68 unsigned char byte[4];
69 uint32_t ch;
70};
71
72enum utf8_state {
73 utf8state_start,
74 utf8state_accept,
75 utf8state_reject,
76 utf8state_expect3,
77 utf8state_expect2,
78 utf8state_expect1
79};
80
81struct utf8_state_machine {
82 enum utf8_state state;
83 int len;
84 union utf8_char s;
85};
86
87static void
88init_state_machine(struct utf8_state_machine *machine)
89{
90 machine->state = utf8state_start;
91 machine->len = 0;
92 machine->s.ch = 0;
93}
94
95static enum utf8_state
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -040096utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000097{
98 switch(machine->state) {
99 case utf8state_start:
100 case utf8state_accept:
101 case utf8state_reject:
102 machine->s.ch = 0;
103 machine->len = 0;
104 if(c == 0xC0 || c == 0xC1) {
105 /* overlong encoding, reject */
106 machine->state = utf8state_reject;
107 } else if((c & 0x80) == 0) {
108 /* single byte, accept */
109 machine->s.byte[machine->len++] = c;
110 machine->state = utf8state_accept;
111 } else if((c & 0xC0) == 0x80) {
112 /* parser out of sync, ignore byte */
113 machine->state = utf8state_start;
114 } else if((c & 0xE0) == 0xC0) {
115 /* start of two byte sequence */
116 machine->s.byte[machine->len++] = c;
117 machine->state = utf8state_expect1;
118 } else if((c & 0xF0) == 0xE0) {
119 /* start of three byte sequence */
120 machine->s.byte[machine->len++] = c;
121 machine->state = utf8state_expect2;
122 } else if((c & 0xF8) == 0xF0) {
123 /* start of four byte sequence */
124 machine->s.byte[machine->len++] = c;
125 machine->state = utf8state_expect3;
126 } else {
127 /* overlong encoding, reject */
128 machine->state = utf8state_reject;
129 }
130 break;
131 case utf8state_expect3:
132 machine->s.byte[machine->len++] = c;
133 if((c & 0xC0) == 0x80) {
134 /* all good, continue */
135 machine->state = utf8state_expect2;
136 } else {
137 /* missing extra byte, reject */
138 machine->state = utf8state_reject;
139 }
140 break;
141 case utf8state_expect2:
142 machine->s.byte[machine->len++] = c;
143 if((c & 0xC0) == 0x80) {
144 /* all good, continue */
145 machine->state = utf8state_expect1;
146 } else {
147 /* missing extra byte, reject */
148 machine->state = utf8state_reject;
149 }
150 break;
151 case utf8state_expect1:
152 machine->s.byte[machine->len++] = c;
153 if((c & 0xC0) == 0x80) {
154 /* all good, accept */
155 machine->state = utf8state_accept;
156 } else {
157 /* missing extra byte, reject */
158 machine->state = utf8state_reject;
159 }
160 break;
161 default:
162 machine->state = utf8state_reject;
163 break;
164 }
165
166 return machine->state;
167}
168
Callum Lowcay256e72f2011-01-07 19:47:01 +0000169struct char_sub {
170 union utf8_char match;
171 union utf8_char replace;
172};
173/* Set last char_sub match to NULL char */
174typedef struct char_sub *character_set;
175
176struct char_sub CS_US[] = {
177 {{{0, }}, {{0, }}}
178};
179static struct char_sub CS_UK[] = {
180 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}},
181 {{{0, }}, {{0, }}}
182};
183static struct char_sub CS_SPECIAL[] = {
184 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond */
185 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell */
186 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT */
187 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF */
188 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR */
189 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF */
190 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree */
191 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus */
192 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL */
193 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT */
194 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB */
195 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT */
196 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT */
197 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_RB */
198 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS */
199 {{{'o', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
200 {{{'p', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
201 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
202 {{{'r', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
203 {{{'s', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
204 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR */
205 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL */
206 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU */
207 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD */
208 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V */
209 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE */
210 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE */
211 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI */
212 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ */
213 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND */
214 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT */
215 {{{0, }}, {{0, }}}
216};
217
218static void
219apply_char_set(character_set cs, union utf8_char *utf8)
220{
221 int i = 0;
222
223 while (cs[i].match.byte[0]) {
224 if ((*utf8).ch == cs[i].match.ch) {
225 *utf8 = cs[i].replace;
226 break;
227 }
228 i++;
229 }
230}
231
Callum Lowcay7e08e902011-01-07 19:47:02 +0000232struct key_map {
233 int sym;
234 int num;
235 char escape;
236 char code;
237};
238/* Set last key_sub sym to NULL */
239typedef struct key_map *keyboard_mode;
240
241static struct key_map KM_NORMAL[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400242 { XKB_KEY_Left, 1, '[', 'D' },
243 { XKB_KEY_Right, 1, '[', 'C' },
244 { XKB_KEY_Up, 1, '[', 'A' },
245 { XKB_KEY_Down, 1, '[', 'B' },
246 { XKB_KEY_Home, 1, '[', 'H' },
247 { XKB_KEY_End, 1, '[', 'F' },
248 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000249};
250static struct key_map KM_APPLICATION[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400251 { XKB_KEY_Left, 1, 'O', 'D' },
252 { XKB_KEY_Right, 1, 'O', 'C' },
253 { XKB_KEY_Up, 1, 'O', 'A' },
254 { XKB_KEY_Down, 1, 'O', 'B' },
255 { XKB_KEY_Home, 1, 'O', 'H' },
256 { XKB_KEY_End, 1, 'O', 'F' },
257 { XKB_KEY_KP_Enter, 1, 'O', 'M' },
258 { XKB_KEY_KP_Multiply, 1, 'O', 'j' },
259 { XKB_KEY_KP_Add, 1, 'O', 'k' },
260 { XKB_KEY_KP_Separator, 1, 'O', 'l' },
261 { XKB_KEY_KP_Subtract, 1, 'O', 'm' },
262 { XKB_KEY_KP_Divide, 1, 'O', 'o' },
263 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000264};
265
266static int
267function_key_response(char escape, int num, uint32_t modifiers,
268 char code, char *response)
269{
270 int mod_num = 0;
271 int len;
272
Kristian Høgsberg70163132012-05-08 15:55:39 -0400273 if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
274 if (modifiers & MOD_ALT_MASK) mod_num |= 2;
275 if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000276
277 if (mod_num != 0)
278 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
279 num, mod_num + 1, code);
280 else if (code != '~')
281 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
282 escape, code);
283 else
284 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
285 escape, num, code);
286
287 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
288 else return len;
289}
290
291/* returns the number of bytes written into response,
292 * which must have room for MAX_RESPONSE bytes */
293static int
294apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
295{
296 struct key_map map;
297 int len = 0;
298 int i = 0;
299
300 while (mode[i].sym) {
301 map = mode[i++];
302 if (sym == map.sym) {
303 len = function_key_response(map.escape, map.num,
304 modifiers, map.code,
305 response);
306 break;
307 }
308 }
309
310 return len;
311}
312
Callum Lowcay30eeae52011-01-07 19:46:55 +0000313struct terminal_color { double r, g, b, a; };
314struct attr {
315 unsigned char fg, bg;
316 char a; /* attributes format:
317 * 76543210
Callum Lowcay81179db2011-01-10 12:14:01 +1300318 * cilub */
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500319 char s; /* in selection */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000320};
321struct color_scheme {
322 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500323 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000324 struct attr default_attr;
325};
326
327static void
328attr_init(struct attr *data_attr, struct attr attr, int n)
329{
330 int i;
331 for (i = 0; i < n; i++) {
332 data_attr[i] = attr;
333 }
334}
335
Callum Lowcay67a201d2011-01-12 19:23:41 +1300336enum escape_state {
337 escape_state_normal = 0,
338 escape_state_escape,
339 escape_state_dcs,
340 escape_state_csi,
341 escape_state_osc,
342 escape_state_inner_escape,
343 escape_state_ignore,
344 escape_state_special
345};
346
347#define ESC_FLAG_WHAT 0x01
348#define ESC_FLAG_GT 0x02
349#define ESC_FLAG_BANG 0x04
350#define ESC_FLAG_CASH 0x08
351#define ESC_FLAG_SQUOTE 0x10
352#define ESC_FLAG_DQUOTE 0x20
353#define ESC_FLAG_SPACE 0x40
354
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500355struct terminal {
356 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500357 struct widget *widget;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500358 struct display *display;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000359 union utf8_char *data;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400360 struct task io_task;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000361 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000362 struct attr *data_attr;
363 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000364 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000365 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000366 char saved_origin_mode;
367 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000368 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000369 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000370 character_set cs, g0, g1;
371 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000372 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000373 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500374 int width, height, start, row, column;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000375 int saved_row, saved_column;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500376 int fd, master;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500377 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300378 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500379 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300380 enum escape_state state;
381 enum escape_state outer_state;
382 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000383 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500384 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500385 int fullscreen;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500386 int focused;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400387 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000388 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400389 cairo_font_extents_t extents;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500390 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500391
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400392 struct wl_data_source *selection;
393 int32_t dragging;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500394 int selection_start_x, selection_start_y;
395 int selection_end_x, selection_end_y;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500396};
397
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000398/* Create default tab stops, every 8 characters */
399static void
400terminal_init_tabs(struct terminal *terminal)
401{
402 int i = 0;
403
404 while (i < terminal->width) {
405 if (i % 8 == 0)
406 terminal->tab_ruler[i] = 1;
407 else
408 terminal->tab_ruler[i] = 0;
409 i++;
410 }
411}
412
Callum Lowcay30eeae52011-01-07 19:46:55 +0000413static void
414terminal_init(struct terminal *terminal)
415{
416 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000417 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000418 terminal->mode = MODE_SHOW_CURSOR |
419 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000420 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000421 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000422
423 terminal->row = 0;
424 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000425
Callum Lowcay256e72f2011-01-07 19:47:01 +0000426 terminal->g0 = CS_US;
427 terminal->g1 = CS_US;
428 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000429 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000430
431 terminal->saved_g0 = terminal->g0;
432 terminal->saved_g1 = terminal->g1;
433 terminal->saved_cs = terminal->cs;
434
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000435 terminal->saved_attr = terminal->curr_attr;
436 terminal->saved_origin_mode = terminal->origin_mode;
437 terminal->saved_row = terminal->row;
438 terminal->saved_column = terminal->column;
439
440 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000441}
442
443static void
444init_color_table(struct terminal *terminal)
445{
446 int c, r;
447 struct terminal_color *color_table = terminal->color_table;
448
449 for (c = 0; c < 256; c ++) {
450 if (c < 16) {
451 color_table[c] = terminal->color_scheme->palette[c];
452 } else if (c < 232) {
453 r = c - 16;
454 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
455 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
456 color_table[c].r = ((double)(r % 6) / 6.0);
457 color_table[c].a = 1.0;
458 } else {
459 r = (c - 232) * 10 + 8;
460 color_table[c].r = ((double) r) / 256.0;
461 color_table[c].g = color_table[c].r;
462 color_table[c].b = color_table[c].r;
463 color_table[c].a = 1.0;
464 }
465 }
466}
467
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000468static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500469terminal_get_row(struct terminal *terminal, int row)
470{
471 int index;
472
473 index = (row + terminal->start) % terminal->height;
474
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000475 return &terminal->data[index * terminal->width];
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500476}
477
Callum Lowcay30eeae52011-01-07 19:46:55 +0000478static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500479terminal_get_attr_row(struct terminal *terminal, int row)
480{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000481 int index;
482
483 index = (row + terminal->start) % terminal->height;
484
485 return &terminal->data_attr[index * terminal->width];
486}
487
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500488union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300489 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500490 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500491};
492
Kristian Høgsberg59826582011-01-20 11:56:57 -0500493static int
494terminal_compare_position(struct terminal *terminal,
495 int x, int y, int32_t ref_row, int32_t ref_col)
496{
497 struct rectangle allocation;
498 int top_margin, side_margin, col, row, ref_x;
499
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500500 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsberg59826582011-01-20 11:56:57 -0500501 side_margin = allocation.x + (allocation.width - terminal->width * terminal->extents.max_x_advance) / 2;
502 top_margin = allocation.y + (allocation.height - terminal->height * terminal->extents.height) / 2;
503
504 col = (x - side_margin) / terminal->extents.max_x_advance;
505 row = (y - top_margin) / terminal->extents.height;
506
507 ref_x = side_margin + ref_col * terminal->extents.max_x_advance +
508 terminal->extents.max_x_advance / 2;
509
510 if (row < ref_row)
511 return -1;
512 if (row == ref_row) {
513 if (col < ref_col)
514 return -1;
515 if (col == ref_col && x < ref_x)
516 return -1;
517 }
518
519 return 1;
520}
521
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500522static void
523terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500524 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500525{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500526 struct attr attr;
527 int foreground, background, tmp;
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500528 int start_cmp, end_cmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500529
530 start_cmp =
531 terminal_compare_position(terminal,
532 terminal->selection_start_x,
533 terminal->selection_start_y,
534 row, col);
535 end_cmp =
536 terminal_compare_position(terminal,
537 terminal->selection_end_x,
538 terminal->selection_end_y,
539 row, col);
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500540 decoded->attr.s = 0;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500541 if (start_cmp < 0 && end_cmp > 0)
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500542 decoded->attr.s = 1;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500543 else if (end_cmp < 0 && start_cmp > 0)
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500544 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500545
546 /* get the attributes for this character cell */
547 attr = terminal_get_attr_row(terminal, row)[col];
548 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500549 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500550 ((terminal->mode & MODE_SHOW_CURSOR) &&
551 terminal->focused && terminal->row == row &&
552 terminal->column == col)) {
553 foreground = attr.bg;
554 background = attr.fg;
555 if (attr.a & ATTRMASK_BOLD) {
556 if (foreground <= 16) foreground |= 0x08;
557 if (background <= 16) background &= 0x07;
558 }
559 } else {
560 foreground = attr.fg;
561 background = attr.bg;
562 }
563
564 if (terminal->mode & MODE_INVERSE) {
565 tmp = foreground;
566 foreground = background;
567 background = tmp;
568 if (attr.a & ATTRMASK_BOLD) {
569 if (foreground <= 16) foreground |= 0x08;
570 if (background <= 16) background &= 0x07;
571 }
572 }
573
Callum Lowcay9d708b02011-01-12 20:06:17 +1300574 decoded->attr.fg = foreground;
575 decoded->attr.bg = background;
576 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000577}
578
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500579
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500580static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000581terminal_scroll_buffer(struct terminal *terminal, int d)
582{
583 int i;
584
585 d = d % (terminal->height + 1);
586 terminal->start = (terminal->start + d) % terminal->height;
587 if (terminal->start < 0) terminal->start = terminal->height + terminal->start;
588 if(d < 0) {
589 d = 0 - d;
590 for(i = 0; i < d; i++) {
591 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
592 attr_init(terminal_get_attr_row(terminal, i),
593 terminal->curr_attr, terminal->width);
594 }
595 } else {
596 for(i = terminal->height - d; i < terminal->height; i++) {
597 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
598 attr_init(terminal_get_attr_row(terminal, i),
599 terminal->curr_attr, terminal->width);
600 }
601 }
602}
603
604static void
605terminal_scroll_window(struct terminal *terminal, int d)
606{
607 int i;
608 int window_height;
609 int from_row, to_row;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000610
611 // scrolling range is inclusive
612 window_height = terminal->margin_bottom - terminal->margin_top + 1;
613 d = d % (window_height + 1);
614 if(d < 0) {
615 d = 0 - d;
616 to_row = terminal->margin_bottom;
617 from_row = terminal->margin_bottom - d;
618
619 for (i = 0; i < (window_height - d); i++) {
620 memcpy(terminal_get_row(terminal, to_row - i),
621 terminal_get_row(terminal, from_row - i),
622 terminal->data_pitch);
623 memcpy(terminal_get_attr_row(terminal, to_row - i),
624 terminal_get_attr_row(terminal, from_row - i),
625 terminal->attr_pitch);
626 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000627 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
628 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000629 attr_init(terminal_get_attr_row(terminal, i),
630 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000631 }
632 } else {
633 to_row = terminal->margin_top;
634 from_row = terminal->margin_top + d;
635
636 for (i = 0; i < (window_height - d); i++) {
637 memcpy(terminal_get_row(terminal, to_row + i),
638 terminal_get_row(terminal, from_row + i),
639 terminal->data_pitch);
640 memcpy(terminal_get_attr_row(terminal, to_row + i),
641 terminal_get_attr_row(terminal, from_row + i),
642 terminal->attr_pitch);
643 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000644 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
645 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000646 attr_init(terminal_get_attr_row(terminal, i),
647 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000648 }
649 }
650}
651
652static void
653terminal_scroll(struct terminal *terminal, int d)
654{
655 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
656 terminal_scroll_buffer(terminal, d);
657 else
658 terminal_scroll_window(terminal, d);
659}
660
661static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000662terminal_shift_line(struct terminal *terminal, int d)
663{
664 union utf8_char *row;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500665 struct attr *attr_row;
Callum Lowcay69e96582011-01-07 19:47:00 +0000666
667 row = terminal_get_row(terminal, terminal->row);
668 attr_row = terminal_get_attr_row(terminal, terminal->row);
669
670 if ((terminal->width + d) <= terminal->column)
671 d = terminal->column + 1 - terminal->width;
672 if ((terminal->column + d) >= terminal->width)
673 d = terminal->width - terminal->column - 1;
674
675 if (d < 0) {
676 d = 0 - d;
677 memmove(&row[terminal->column],
678 &row[terminal->column + d],
679 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
Callum Lowcay69e96582011-01-07 19:47:00 +0000680 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
681 (terminal->width - terminal->column - d) * sizeof(struct attr));
682 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
683 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
684 } else {
685 memmove(&row[terminal->column + d], &row[terminal->column],
686 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
687 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
688 (terminal->width - terminal->column - d) * sizeof(struct attr));
689 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
690 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
691 }
692}
693
694static void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500695terminal_resize_cells(struct terminal *terminal, int width, int height)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500696{
697 size_t size;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000698 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000699 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000700 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000701 int data_pitch, attr_pitch;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500702 int i, l, total_rows;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500703 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000704 struct winsize ws;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500705
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500706 if (width < 1)
707 width = 1;
708 if (height < 1)
709 height = 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500710 if (terminal->width == width && terminal->height == height)
711 return;
712
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000713 data_pitch = width * sizeof(union utf8_char);
714 size = data_pitch * height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500715 data = malloc(size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000716 attr_pitch = width * sizeof(struct attr);
717 data_attr = malloc(attr_pitch * height);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000718 tab_ruler = malloc(width);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500719 memset(data, 0, size);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000720 memset(tab_ruler, 0, width);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000721 attr_init(data_attr, terminal->curr_attr, width * height);
722 if (terminal->data && terminal->data_attr) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500723 if (width > terminal->width)
724 l = terminal->width;
725 else
726 l = width;
727
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500728 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500729 total_rows = height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500730 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500731 total_rows = terminal->height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500732 }
733
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000734 for (i = 0; i < total_rows; i++) {
735 memcpy(&data[width * i],
736 terminal_get_row(terminal, i),
737 l * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000738 memcpy(&data_attr[width * i],
739 terminal_get_attr_row(terminal, i),
740 l * sizeof(struct attr));
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000741 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500742
743 free(terminal->data);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000744 free(terminal->data_attr);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000745 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500746 }
747
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000748 terminal->data_pitch = data_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000749 terminal->attr_pitch = attr_pitch;
Callum Lowcay86653ed2011-01-07 19:47:03 +0000750 terminal->margin_bottom =
751 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500752 terminal->width = width;
753 terminal->height = height;
754 terminal->data = data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000755 terminal->data_attr = data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000756 terminal->tab_ruler = tab_ruler;
757 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500758
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000759 /* Update the window size */
760 ws.ws_row = terminal->height;
761 ws.ws_col = terminal->width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500762 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500763 ws.ws_xpixel = allocation.width;
764 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000765 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500766}
767
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500768static void
769resize_handler(struct widget *widget,
770 int32_t width, int32_t height, void *data)
771{
772 struct terminal *terminal = data;
773 int32_t columns, rows, m;
774
775 m = 2 * terminal->margin;
776 columns = (width - m) / (int32_t) terminal->extents.max_x_advance;
777 rows = (height - m) / (int32_t) terminal->extents.height;
778
779 if (!terminal->fullscreen) {
780 width = columns * terminal->extents.max_x_advance + m;
781 height = rows * terminal->extents.height + m;
782 widget_set_size(terminal->widget, width, height);
783 }
784
785 terminal_resize_cells(terminal, columns, rows);
786}
787
788static void
789terminal_resize(struct terminal *terminal, int columns, int rows)
790{
791 int32_t width, height, m;
792
793 if (terminal->fullscreen)
794 return;
795
796 m = 2 * terminal->margin;
797 width = columns * terminal->extents.max_x_advance + m;
798 height = rows * terminal->extents.height + m;
799 widget_schedule_resize(terminal->widget, width, height);
800}
801
Callum Lowcay30eeae52011-01-07 19:46:55 +0000802struct color_scheme DEFAULT_COLORS = {
803 {
804 {0, 0, 0, 1}, /* black */
805 {0.66, 0, 0, 1}, /* red */
806 {0 , 0.66, 0, 1}, /* green */
807 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
808 {0 , 0 , 0.66, 1}, /* blue */
809 {0.66, 0 , 0.66, 1}, /* magenta */
810 {0, 0.66, 0.66, 1}, /* cyan */
811 {0.66, 0.66, 0.66, 1}, /* light grey */
812 {0.22, 0.33, 0.33, 1}, /* dark grey */
813 {1, 0.33, 0.33, 1}, /* high red */
814 {0.33, 1, 0.33, 1}, /* high green */
815 {1, 1, 0.33, 1}, /* high yellow */
816 {0.33, 0.33, 1, 1}, /* high blue */
817 {1, 0.33, 1, 1}, /* high magenta */
818 {0.33, 1, 1, 1}, /* high cyan */
819 {1, 1, 1, 1} /* white */
820 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500821 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000822 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
823};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400824
Kristian Høgsberg22106762008-12-08 13:50:07 -0500825static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500826terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
827{
828 cairo_set_source_rgba(cr,
829 terminal->color_table[index].r,
830 terminal->color_table[index].g,
831 terminal->color_table[index].b,
832 terminal->color_table[index].a);
833}
834
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500835static void
836terminal_send_selection(struct terminal *terminal, int fd)
837{
838 int row, col;
839 union utf8_char *p_row;
840 union decoded_attr attr;
841 FILE *fp;
842 int len;
843
844 fp = fdopen(fd, "w");
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 */
849 terminal_decode_attr(terminal, row, col, &attr);
850 if (!attr.attr.s)
851 continue;
852 len = strnlen((char *) p_row[col].byte, 4);
853 fwrite(p_row[col].byte, 1, len, fp);
854 }
855 }
856 fclose(fp);
857}
858
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500859struct glyph_run {
860 struct terminal *terminal;
861 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400862 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500863 union decoded_attr attr;
864 cairo_glyph_t glyphs[256], *g;
865};
866
867static void
868glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
869{
870 run->terminal = terminal;
871 run->cr = cr;
872 run->g = run->glyphs;
873 run->count = 0;
874 run->attr.key = 0;
875}
876
877static void
878glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
879{
880 cairo_scaled_font_t *font;
881
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500882 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
883 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300884 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500885 font = run->terminal->font_bold;
886 else
887 font = run->terminal->font_normal;
888 cairo_set_scaled_font(run->cr, font);
889 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300890 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500891
Callum Lowcay9d708b02011-01-12 20:06:17 +1300892 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
893 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500894 run->g = run->glyphs;
895 run->count = 0;
896 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300897 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500898}
899
900static void
901glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
902{
903 int num_glyphs;
904 cairo_scaled_font_t *font;
905
906 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
907
Callum Lowcay9d708b02011-01-12 20:06:17 +1300908 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500909 font = run->terminal->font_bold;
910 else
911 font = run->terminal->font_normal;
912
913 cairo_move_to(run->cr, x, y);
914 cairo_scaled_font_text_to_glyphs (font, x, y,
915 (char *) c->byte, 4,
916 &run->g, &num_glyphs,
917 NULL, NULL, NULL);
918 run->g += num_glyphs;
919 run->count += num_glyphs;
920}
921
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500922
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500923static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500924redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500925{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500926 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500927 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500928 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000929 int top_margin, side_margin;
930 int row, col;
931 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500932 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000933 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500934 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500935 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500936 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500937 cairo_font_extents_t extents;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500938
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500939 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500940 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500941 cr = cairo_create(surface);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500942 cairo_rectangle(cr, allocation.x, allocation.y,
943 allocation.width, allocation.height);
944 cairo_clip(cr);
945 cairo_push_group(cr);
946
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500947 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500948 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500949 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500950
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500951 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500952
Kristian Høgsberg59826582011-01-20 11:56:57 -0500953 extents = terminal->extents;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500954 side_margin = (allocation.width - terminal->width * extents.max_x_advance) / 2;
955 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500956
Callum Lowcay30eeae52011-01-07 19:46:55 +0000957 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500958 cairo_translate(cr, allocation.x + side_margin,
959 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500960 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000961 for (row = 0; row < terminal->height; row++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000962 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000963 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500964 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000965
Callum Lowcay9d708b02011-01-12 20:06:17 +1300966 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -0500967 continue;
968
Callum Lowcay9d708b02011-01-12 20:06:17 +1300969 terminal_set_color(terminal, cr, attr.attr.bg);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500970 cairo_move_to(cr, col * extents.max_x_advance,
971 row * extents.height);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000972 cairo_rel_line_to(cr, extents.max_x_advance, 0);
973 cairo_rel_line_to(cr, 0, extents.height);
974 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
975 cairo_close_path(cr);
976 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500977 }
978 }
Callum Lowcay30eeae52011-01-07 19:46:55 +0000979
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500980 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
981
982 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500983 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500984 for (row = 0; row < terminal->height; row++) {
985 p_row = terminal_get_row(terminal, row);
986 for (col = 0; col < terminal->width; col++) {
987 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500988 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500989
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500990 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000991
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500992 text_x = col * extents.max_x_advance;
993 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +1300994 if (attr.attr.a & ATTRMASK_UNDERLINE) {
995 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000996 cairo_move_to(cr, text_x, (double)text_y + 1.5);
997 cairo_line_to(cr, text_x + extents.max_x_advance, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000998 cairo_stroke(cr);
999 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -05001000
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001001 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001002 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001003 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001004
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001005 attr.key = ~0;
1006 glyph_run_flush(&run, attr);
1007
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001008 if ((terminal->mode & MODE_SHOW_CURSOR) && !terminal->focused) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001009 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001010
Callum Lowcay30eeae52011-01-07 19:46:55 +00001011 cairo_set_line_width(cr, 1);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001012 cairo_move_to(cr, terminal->column * extents.max_x_advance + d,
1013 terminal->row * extents.height + d);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001014 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
1015 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
1016 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
1017 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001018
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001019 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001020 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001021
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001022 cairo_pop_group_to_source(cr);
1023 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001024 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001025 cairo_surface_destroy(surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001026}
1027
1028static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001029terminal_write(struct terminal *terminal, const char *data, size_t length)
1030{
1031 if (write(terminal->master, data, length) < 0)
1032 abort();
1033}
1034
1035static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001036terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001037
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001038static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001039handle_char(struct terminal *terminal, union utf8_char utf8);
1040
1041static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001042handle_sgr(struct terminal *terminal, int code);
1043
1044static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001045handle_term_parameter(struct terminal *terminal, int code, int sr)
1046{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001047 int i;
1048
Callum Lowcay67a201d2011-01-12 19:23:41 +13001049 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001050 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001051 case 1: /* DECCKM */
1052 if (sr) terminal->key_mode = KM_APPLICATION;
1053 else terminal->key_mode = KM_NORMAL;
1054 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001055 case 2: /* DECANM */
1056 /* No VT52 support yet */
1057 terminal->g0 = CS_US;
1058 terminal->g1 = CS_US;
1059 terminal->cs = terminal->g0;
1060 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001061 case 3: /* DECCOLM */
1062 if (sr)
1063 terminal_resize(terminal, 132, 24);
1064 else
1065 terminal_resize(terminal, 80, 24);
1066
1067 /* set columns, but also home cursor and clear screen */
1068 terminal->row = 0; terminal->column = 0;
1069 for (i = 0; i < terminal->height; i++) {
1070 memset(terminal_get_row(terminal, i),
1071 0, terminal->data_pitch);
1072 attr_init(terminal_get_attr_row(terminal, i),
1073 terminal->curr_attr, terminal->width);
1074 }
1075 break;
1076 case 5: /* DECSCNM */
1077 if (sr) terminal->mode |= MODE_INVERSE;
1078 else terminal->mode &= ~MODE_INVERSE;
1079 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001080 case 6: /* DECOM */
1081 terminal->origin_mode = sr;
1082 if (terminal->origin_mode)
1083 terminal->row = terminal->margin_top;
1084 else
1085 terminal->row = 0;
1086 terminal->column = 0;
1087 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001088 case 7: /* DECAWM */
1089 if (sr) terminal->mode |= MODE_AUTOWRAP;
1090 else terminal->mode &= ~MODE_AUTOWRAP;
1091 break;
1092 case 8: /* DECARM */
1093 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1094 else terminal->mode &= ~MODE_AUTOREPEAT;
1095 break;
1096 case 25:
1097 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1098 else terminal->mode &= ~MODE_SHOW_CURSOR;
1099 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001100 case 1037: /* deleteSendsDel */
1101 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1102 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1103 break;
1104 case 1039: /* altSendsEscape */
1105 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1106 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1107 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001108 default:
1109 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1110 break;
1111 }
1112 } else {
1113 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001114 case 4: /* IRM */
1115 if (sr) terminal->mode |= MODE_IRM;
1116 else terminal->mode &= ~MODE_IRM;
1117 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001118 case 20: /* LNM */
1119 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1120 else terminal->mode &= ~MODE_LF_NEWLINE;
1121 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001122 default:
1123 fprintf(stderr, "Unknown parameter: %d\n", code);
1124 break;
1125 }
1126 }
1127}
1128
1129static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001130handle_dcs(struct terminal *terminal)
1131{
1132}
1133
1134static void
1135handle_osc(struct terminal *terminal)
1136{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001137 char *p;
1138 int code;
1139
1140 terminal->escape[terminal->escape_length++] = '\0';
1141 p = &terminal->escape[2];
1142 code = strtol(p, &p, 10);
1143 if (*p == ';') p++;
1144
1145 switch (code) {
1146 case 0: /* Icon name and window title */
1147 case 1: /* Icon label */
1148 case 2: /* Window title*/
1149 window_set_title(terminal->window, p);
1150 break;
1151 default:
1152 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1153 break;
1154 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001155}
1156
1157static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001158handle_escape(struct terminal *terminal)
1159{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001160 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001161 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001162 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001163 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001164 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001165 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001166 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001167
1168 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001169 i = 0;
1170 p = &terminal->escape[2];
1171 while ((isdigit(*p) || *p == ';') && i < 10) {
1172 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001173 if (!set[i]) {
1174 args[i] = 0;
1175 set[i] = 1;
1176 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001177 p++;
1178 i++;
1179 } else {
1180 args[i] = strtol(p, &p, 10);
1181 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001182 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001183 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001184
1185 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001186 case '@': /* ICH */
1187 count = set[0] ? args[0] : 1;
1188 if (count == 0) count = 1;
1189 terminal_shift_line(terminal, count);
1190 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001191 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001192 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001193 if (count == 0) count = 1;
1194 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001195 terminal->row -= count;
1196 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001197 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001198 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001199 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001200 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001201 if (count == 0) count = 1;
1202 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001203 terminal->row += count;
1204 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001205 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001206 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001207 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001208 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001209 if (count == 0) count = 1;
1210 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001211 terminal->column += count;
1212 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001213 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001214 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001215 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001216 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001217 if (count == 0) count = 1;
1218 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001219 terminal->column -= count;
1220 else
1221 terminal->column = 0;
1222 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001223 case 'E': /* CNL */
1224 count = set[0] ? args[0] : 1;
1225 if (terminal->row + count <= terminal->margin_bottom)
1226 terminal->row += count;
1227 else
1228 terminal->row = terminal->margin_bottom;
1229 terminal->column = 0;
1230 break;
1231 case 'F': /* CPL */
1232 count = set[0] ? args[0] : 1;
1233 if (terminal->row - count >= terminal->margin_top)
1234 terminal->row -= count;
1235 else
1236 terminal->row = terminal->margin_top;
1237 terminal->column = 0;
1238 break;
1239 case 'G': /* CHA */
1240 y = set[0] ? args[0] : 1;
1241 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1242
1243 terminal->column = y - 1;
1244 break;
1245 case 'f': /* HVP */
1246 case 'H': /* CUP */
1247 x = (set[1] ? args[1] : 1) - 1;
1248 x = x < 0 ? 0 :
1249 (x >= terminal->width ? terminal->width - 1 : x);
1250
1251 y = (set[0] ? args[0] : 1) - 1;
1252 if (terminal->origin_mode) {
1253 y += terminal->margin_top;
1254 y = y < terminal->margin_top ? terminal->margin_top :
1255 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1256 } else {
1257 y = y < 0 ? 0 :
1258 (y >= terminal->height ? terminal->height - 1 : y);
1259 }
1260
1261 terminal->row = y;
1262 terminal->column = x;
1263 break;
1264 case 'I': /* CHT */
1265 count = set[0] ? args[0] : 1;
1266 if (count == 0) count = 1;
1267 while (count > 0 && terminal->column < terminal->width) {
1268 if (terminal->tab_ruler[terminal->column]) count--;
1269 terminal->column++;
1270 }
1271 terminal->column--;
1272 break;
1273 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001274 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001275 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001276 if (!set[0] || args[0] == 0 || args[0] > 2) {
1277 memset(&row[terminal->column],
1278 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1279 attr_init(&attr_row[terminal->column],
1280 terminal->curr_attr, terminal->width - terminal->column);
1281 for (i = terminal->row + 1; i < terminal->height; i++) {
1282 memset(terminal_get_row(terminal, i),
1283 0, terminal->data_pitch);
1284 attr_init(terminal_get_attr_row(terminal, i),
1285 terminal->curr_attr, terminal->width);
1286 }
1287 } else if (args[0] == 1) {
1288 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1289 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1290 for (i = 0; i < terminal->row; i++) {
1291 memset(terminal_get_row(terminal, i),
1292 0, terminal->data_pitch);
1293 attr_init(terminal_get_attr_row(terminal, i),
1294 terminal->curr_attr, terminal->width);
1295 }
1296 } else if (args[0] == 2) {
1297 for (i = 0; i < terminal->height; i++) {
1298 memset(terminal_get_row(terminal, i),
1299 0, terminal->data_pitch);
1300 attr_init(terminal_get_attr_row(terminal, i),
1301 terminal->curr_attr, terminal->width);
1302 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001303 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001304 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001305 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001306 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001307 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001308 if (!set[0] || args[0] == 0 || args[0] > 2) {
1309 memset(&row[terminal->column], 0,
1310 (terminal->width - terminal->column) * sizeof(union utf8_char));
1311 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1312 terminal->width - terminal->column);
1313 } else if (args[0] == 1) {
1314 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1315 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1316 } else if (args[0] == 2) {
1317 memset(row, 0, terminal->data_pitch);
1318 attr_init(attr_row, terminal->curr_attr, terminal->width);
1319 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001320 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001321 case 'L': /* IL */
1322 count = set[0] ? args[0] : 1;
1323 if (count == 0) count = 1;
1324 if (terminal->row >= terminal->margin_top &&
1325 terminal->row < terminal->margin_bottom)
1326 {
1327 top = terminal->margin_top;
1328 terminal->margin_top = terminal->row;
1329 terminal_scroll(terminal, 0 - count);
1330 terminal->margin_top = top;
1331 } else if (terminal->row == terminal->margin_bottom) {
1332 memset(terminal_get_row(terminal, terminal->row),
1333 0, terminal->data_pitch);
1334 attr_init(terminal_get_attr_row(terminal, terminal->row),
1335 terminal->curr_attr, terminal->width);
1336 }
1337 break;
1338 case 'M': /* DL */
1339 count = set[0] ? args[0] : 1;
1340 if (count == 0) count = 1;
1341 if (terminal->row >= terminal->margin_top &&
1342 terminal->row < terminal->margin_bottom)
1343 {
1344 top = terminal->margin_top;
1345 terminal->margin_top = terminal->row;
1346 terminal_scroll(terminal, count);
1347 terminal->margin_top = top;
1348 } else if (terminal->row == terminal->margin_bottom) {
1349 memset(terminal_get_row(terminal, terminal->row),
1350 0, terminal->data_pitch);
1351 }
1352 break;
1353 case 'P': /* DCH */
1354 count = set[0] ? args[0] : 1;
1355 if (count == 0) count = 1;
1356 terminal_shift_line(terminal, 0 - count);
1357 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001358 case 'S': /* SU */
1359 terminal_scroll(terminal, set[0] ? args[0] : 1);
1360 break;
1361 case 'T': /* SD */
1362 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1363 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001364 case 'X': /* ECH */
1365 count = set[0] ? args[0] : 1;
1366 if (count == 0) count = 1;
1367 if ((terminal->column + count) > terminal->width)
1368 count = terminal->width - terminal->column;
1369 row = terminal_get_row(terminal, terminal->row);
1370 attr_row = terminal_get_attr_row(terminal, terminal->row);
1371 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1372 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1373 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001374 case 'Z': /* CBT */
1375 count = set[0] ? args[0] : 1;
1376 if (count == 0) count = 1;
1377 while (count > 0 && terminal->column >= 0) {
1378 if (terminal->tab_ruler[terminal->column]) count--;
1379 terminal->column--;
1380 }
1381 terminal->column++;
1382 break;
1383 case '`': /* HPA */
1384 y = set[0] ? args[0] : 1;
1385 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1386
1387 terminal->column = y - 1;
1388 break;
1389 case 'b': /* REP */
1390 count = set[0] ? args[0] : 1;
1391 if (count == 0) count = 1;
1392 if (terminal->last_char.byte[0])
1393 for (i = 0; i < count; i++)
1394 handle_char(terminal, terminal->last_char);
1395 terminal->last_char.byte[0] = 0;
1396 break;
1397 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001398 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001399 break;
1400 case 'd': /* VPA */
1401 x = set[0] ? args[0] : 1;
1402 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1403
1404 terminal->row = x - 1;
1405 break;
1406 case 'g': /* TBC */
1407 if (!set[0] || args[0] == 0) {
1408 terminal->tab_ruler[terminal->column] = 0;
1409 } else if (args[0] == 3) {
1410 memset(terminal->tab_ruler, 0, terminal->width);
1411 }
1412 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001413 case 'h': /* SM */
1414 for(i = 0; i < 10 && set[i]; i++) {
1415 handle_term_parameter(terminal, args[i], 1);
1416 }
1417 break;
1418 case 'l': /* RM */
1419 for(i = 0; i < 10 && set[i]; i++) {
1420 handle_term_parameter(terminal, args[i], 0);
1421 }
1422 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001423 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001424 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001425 if (i <= 7 && set[i] && set[i + 1] &&
1426 set[i + 2] && args[i + 1] == 5)
1427 {
1428 if (args[i] == 38) {
1429 handle_sgr(terminal, args[i + 2] + 256);
1430 break;
1431 } else if (args[i] == 48) {
1432 handle_sgr(terminal, args[i + 2] + 512);
1433 break;
1434 }
1435 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001436 if(set[i]) {
1437 handle_sgr(terminal, args[i]);
1438 } else if(i == 0) {
1439 handle_sgr(terminal, 0);
1440 break;
1441 } else {
1442 break;
1443 }
1444 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001445 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001446 case 'n': /* DSR */
1447 i = set[0] ? args[0] : 0;
1448 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001449 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001450 } else if (i == 6) {
1451 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1452 terminal->origin_mode ?
1453 terminal->row+terminal->margin_top : terminal->row+1,
1454 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001455 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001456 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001457 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001458 case 'r':
1459 if(!set[0]) {
1460 terminal->margin_top = 0;
1461 terminal->margin_bottom = terminal->height-1;
1462 terminal->row = 0;
1463 terminal->column = 0;
1464 } else {
1465 top = (set[0] ? args[0] : 1) - 1;
1466 top = top < 0 ? 0 :
1467 (top >= terminal->height ? terminal->height - 1 : top);
1468 bottom = (set[1] ? args[1] : 1) - 1;
1469 bottom = bottom < 0 ? 0 :
1470 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1471 if(bottom > top) {
1472 terminal->margin_top = top;
1473 terminal->margin_bottom = bottom;
1474 } else {
1475 terminal->margin_top = 0;
1476 terminal->margin_bottom = terminal->height-1;
1477 }
1478 if(terminal->origin_mode)
1479 terminal->row = terminal->margin_top;
1480 else
1481 terminal->row = 0;
1482 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001483 }
1484 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001485 case 's':
1486 terminal->saved_row = terminal->row;
1487 terminal->saved_column = terminal->column;
1488 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001489 case 't': /* windowOps */
1490 if (!set[0]) break;
1491 switch (args[0]) {
1492 case 4: /* resize px */
1493 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001494 widget_schedule_resize(terminal->widget,
1495 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001496 }
1497 break;
1498 case 8: /* resize ch */
1499 if (set[1] && set[2]) {
1500 terminal_resize(terminal, args[2], args[1]);
1501 }
1502 break;
1503 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001504 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001505 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1506 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001507 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001508 break;
1509 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001510 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001511 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1512 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001513 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001514 break;
1515 case 18: /* report ch */
1516 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1517 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001518 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001519 break;
1520 case 21: /* report title */
1521 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1522 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001523 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001524 break;
1525 default:
1526 if (args[0] >= 24)
1527 terminal_resize(terminal, terminal->width, args[0]);
1528 else
1529 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1530 break;
1531 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001532 case 'u':
1533 terminal->row = terminal->saved_row;
1534 terminal->column = terminal->saved_column;
1535 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001536 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001537 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001538 break;
1539 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001540}
1541
1542static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001543handle_non_csi_escape(struct terminal *terminal, char code)
1544{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001545 switch(code) {
1546 case 'M': /* RI */
1547 terminal->row -= 1;
1548 if(terminal->row < terminal->margin_top) {
1549 terminal->row = terminal->margin_top;
1550 terminal_scroll(terminal, -1);
1551 }
1552 break;
1553 case 'E': /* NEL */
1554 terminal->column = 0;
1555 // fallthrough
1556 case 'D': /* IND */
1557 terminal->row += 1;
1558 if(terminal->row > terminal->margin_bottom) {
1559 terminal->row = terminal->margin_bottom;
1560 terminal_scroll(terminal, +1);
1561 }
1562 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001563 case 'c': /* RIS */
1564 terminal_init(terminal);
1565 break;
1566 case 'H': /* HTS */
1567 terminal->tab_ruler[terminal->column] = 1;
1568 break;
1569 case '7': /* DECSC */
1570 terminal->saved_row = terminal->row;
1571 terminal->saved_column = terminal->column;
1572 terminal->saved_attr = terminal->curr_attr;
1573 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001574 terminal->saved_cs = terminal->cs;
1575 terminal->saved_g0 = terminal->g0;
1576 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001577 break;
1578 case '8': /* DECRC */
1579 terminal->row = terminal->saved_row;
1580 terminal->column = terminal->saved_column;
1581 terminal->curr_attr = terminal->saved_attr;
1582 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001583 terminal->cs = terminal->saved_cs;
1584 terminal->g0 = terminal->saved_g0;
1585 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001586 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001587 case '=': /* DECPAM */
1588 terminal->key_mode = KM_APPLICATION;
1589 break;
1590 case '>': /* DECPNM */
1591 terminal->key_mode = KM_NORMAL;
1592 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001593 default:
1594 fprintf(stderr, "Unknown escape code: %c\n", code);
1595 break;
1596 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001597}
1598
1599static void
1600handle_special_escape(struct terminal *terminal, char special, char code)
1601{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001602 int i, numChars;
1603
1604 if (special == '#') {
1605 switch(code) {
1606 case '8':
1607 /* fill with 'E', no cheap way to do this */
1608 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1609 numChars = terminal->width * terminal->height;
1610 for(i = 0; i < numChars; i++) {
1611 terminal->data[i].byte[0] = 'E';
1612 }
1613 break;
1614 default:
1615 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1616 break;
1617 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001618 } else if (special == '(' || special == ')') {
1619 switch(code) {
1620 case '0':
1621 if (special == '(')
1622 terminal->g0 = CS_SPECIAL;
1623 else
1624 terminal->g1 = CS_SPECIAL;
1625 break;
1626 case 'A':
1627 if (special == '(')
1628 terminal->g0 = CS_UK;
1629 else
1630 terminal->g1 = CS_UK;
1631 break;
1632 case 'B':
1633 if (special == '(')
1634 terminal->g0 = CS_US;
1635 else
1636 terminal->g1 = CS_US;
1637 break;
1638 default:
1639 fprintf(stderr, "Unknown character set %c\n", code);
1640 break;
1641 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001642 } else {
1643 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1644 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001645}
1646
1647static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001648handle_sgr(struct terminal *terminal, int code)
1649{
1650 switch(code) {
1651 case 0:
1652 terminal->curr_attr = terminal->color_scheme->default_attr;
1653 break;
1654 case 1:
1655 terminal->curr_attr.a |= ATTRMASK_BOLD;
1656 if (terminal->curr_attr.fg < 8)
1657 terminal->curr_attr.fg += 8;
1658 break;
1659 case 4:
1660 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1661 break;
1662 case 5:
1663 terminal->curr_attr.a |= ATTRMASK_BLINK;
1664 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001665 case 8:
1666 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1667 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001668 case 2:
1669 case 21:
1670 case 22:
1671 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1672 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1673 terminal->curr_attr.fg -= 8;
1674 break;
1675 case 24:
1676 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1677 break;
1678 case 25:
1679 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1680 break;
1681 case 7:
1682 case 26:
1683 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1684 break;
1685 case 27:
1686 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1687 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001688 case 28:
1689 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1690 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001691 case 39:
1692 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1693 break;
1694 case 49:
1695 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1696 break;
1697 default:
1698 if(code >= 30 && code <= 37) {
1699 terminal->curr_attr.fg = code - 30;
1700 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1701 terminal->curr_attr.fg += 8;
1702 } else if(code >= 40 && code <= 47) {
1703 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001704 } else if (code >= 90 && code <= 97) {
1705 terminal->curr_attr.fg = code - 90 + 8;
1706 } else if (code >= 100 && code <= 107) {
1707 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001708 } else if(code >= 256 && code < 512) {
1709 terminal->curr_attr.fg = code - 256;
1710 } else if(code >= 512 && code < 768) {
1711 terminal->curr_attr.bg = code - 512;
1712 } else {
1713 fprintf(stderr, "Unknown SGR code: %d\n", code);
1714 }
1715 break;
1716 }
1717}
1718
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001719/* Returns 1 if c was special, otherwise 0 */
1720static int
1721handle_special_char(struct terminal *terminal, char c)
1722{
1723 union utf8_char *row;
1724 struct attr *attr_row;
1725
1726 row = terminal_get_row(terminal, terminal->row);
1727 attr_row = terminal_get_attr_row(terminal, terminal->row);
1728
1729 switch(c) {
1730 case '\r':
1731 terminal->column = 0;
1732 break;
1733 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001734 if (terminal->mode & MODE_LF_NEWLINE) {
1735 terminal->column = 0;
1736 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001737 /* fallthrough */
1738 case '\v':
1739 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001740 terminal->row++;
1741 if(terminal->row > terminal->margin_bottom) {
1742 terminal->row = terminal->margin_bottom;
1743 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001744 }
1745
1746 break;
1747 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001748 while (terminal->column < terminal->width) {
1749 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001750 if (terminal->mode & MODE_IRM)
1751 terminal_shift_line(terminal, +1);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001752 row[terminal->column].byte[0] = ' ';
1753 row[terminal->column].byte[1] = '\0';
1754 attr_row[terminal->column] = terminal->curr_attr;
1755 terminal->column++;
1756 }
1757 if (terminal->column >= terminal->width) {
1758 terminal->column = terminal->width - 1;
1759 }
1760
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001761 break;
1762 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001763 if (terminal->column >= terminal->width) {
1764 terminal->column = terminal->width - 2;
1765 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001766 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001767 } else if (terminal->mode & MODE_AUTOWRAP) {
1768 terminal->column = terminal->width - 1;
1769 terminal->row -= 1;
1770 if (terminal->row < terminal->margin_top) {
1771 terminal->row = terminal->margin_top;
1772 terminal_scroll(terminal, -1);
1773 }
1774 }
1775
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001776 break;
1777 case '\a':
1778 /* Bell */
1779 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001780 case '\x0E': /* SO */
1781 terminal->cs = terminal->g1;
1782 break;
1783 case '\x0F': /* SI */
1784 terminal->cs = terminal->g0;
1785 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001786 default:
1787 return 0;
1788 }
1789
1790 return 1;
1791}
1792
1793static void
1794handle_char(struct terminal *terminal, union utf8_char utf8)
1795{
1796 union utf8_char *row;
1797 struct attr *attr_row;
1798
1799 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001800
1801 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001802
1803 /* There are a whole lot of non-characters, control codes,
1804 * and formatting codes that should probably be ignored,
1805 * for example: */
1806 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1807 /* BOM, ignore */
1808 return;
1809 }
1810
1811 /* Some of these non-characters should be translated, e.g.: */
1812 if (utf8.byte[0] < 32) {
1813 utf8.byte[0] = utf8.byte[0] + 64;
1814 }
1815
1816 /* handle right margin effects */
1817 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001818 if (terminal->mode & MODE_AUTOWRAP) {
1819 terminal->column = 0;
1820 terminal->row += 1;
1821 if (terminal->row > terminal->margin_bottom) {
1822 terminal->row = terminal->margin_bottom;
1823 terminal_scroll(terminal, +1);
1824 }
1825 } else {
1826 terminal->column--;
1827 }
1828 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001829
1830 row = terminal_get_row(terminal, terminal->row);
1831 attr_row = terminal_get_attr_row(terminal, terminal->row);
1832
Callum Lowcay69e96582011-01-07 19:47:00 +00001833 if (terminal->mode & MODE_IRM)
1834 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001835 row[terminal->column] = utf8;
1836 attr_row[terminal->column++] = terminal->curr_attr;
1837
1838 if (utf8.ch != terminal->last_char.ch)
1839 terminal->last_char = utf8;
1840}
1841
Callum Lowcay30eeae52011-01-07 19:46:55 +00001842static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001843escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1844{
1845 int len, i;
1846
1847 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1848 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1849 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1850 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1851 else len = 1; /* Invalid, cannot happen */
1852
1853 if (terminal->escape_length + len <= MAX_ESCAPE) {
1854 for (i = 0; i < len; i++)
1855 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1856 terminal->escape_length += len;
1857 } else if (terminal->escape_length < MAX_ESCAPE) {
1858 terminal->escape[terminal->escape_length++] = 0;
1859 }
1860}
1861
1862static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001863terminal_data(struct terminal *terminal, const char *data, size_t length)
1864{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001865 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001866 union utf8_char utf8;
1867 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001868
1869 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001870 parser_state =
1871 utf8_next_char(&terminal->state_machine, data[i]);
1872 switch(parser_state) {
1873 case utf8state_accept:
1874 utf8.ch = terminal->state_machine.s.ch;
1875 break;
1876 case utf8state_reject:
1877 /* the unicode replacement character */
1878 utf8.byte[0] = 0xEF;
1879 utf8.byte[1] = 0xBF;
1880 utf8.byte[2] = 0xBD;
1881 utf8.byte[3] = 0x00;
1882 break;
1883 default:
1884 continue;
1885 }
1886
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001887 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001888 switch (terminal->state) {
1889 case escape_state_escape:
1890 escape_append_utf8(terminal, utf8);
1891 switch (utf8.byte[0]) {
1892 case 'P': /* DCS */
1893 terminal->state = escape_state_dcs;
1894 break;
1895 case '[': /* CSI */
1896 terminal->state = escape_state_csi;
1897 break;
1898 case ']': /* OSC */
1899 terminal->state = escape_state_osc;
1900 break;
1901 case '#':
1902 case '(':
1903 case ')': /* special */
1904 terminal->state = escape_state_special;
1905 break;
1906 case '^': /* PM (not implemented) */
1907 case '_': /* APC (not implemented) */
1908 terminal->state = escape_state_ignore;
1909 break;
1910 default:
1911 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001912 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13001913 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001914 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001915 continue;
1916 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001917 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1918 /* do nothing */
1919 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001920 terminal->escape_flags |= ESC_FLAG_WHAT;
1921 } else if (utf8.byte[0] == '>') {
1922 terminal->escape_flags |= ESC_FLAG_GT;
1923 } else if (utf8.byte[0] == '!') {
1924 terminal->escape_flags |= ESC_FLAG_BANG;
1925 } else if (utf8.byte[0] == '$') {
1926 terminal->escape_flags |= ESC_FLAG_CASH;
1927 } else if (utf8.byte[0] == '\'') {
1928 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1929 } else if (utf8.byte[0] == '"') {
1930 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1931 } else if (utf8.byte[0] == ' ') {
1932 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001933 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001934 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001935 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13001936 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001937 }
1938
1939 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1940 utf8.byte[0] == '`')
1941 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001942 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001943 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001944 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001945 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001946 continue;
1947 case escape_state_inner_escape:
1948 if (utf8.byte[0] == '\\') {
1949 terminal->state = escape_state_normal;
1950 if (terminal->outer_state == escape_state_dcs) {
1951 handle_dcs(terminal);
1952 } else if (terminal->outer_state == escape_state_osc) {
1953 handle_osc(terminal);
1954 }
1955 } else if (utf8.byte[0] == '\e') {
1956 terminal->state = terminal->outer_state;
1957 escape_append_utf8(terminal, utf8);
1958 if (terminal->escape_length >= MAX_ESCAPE)
1959 terminal->state = escape_state_normal;
1960 } else {
1961 terminal->state = terminal->outer_state;
1962 if (terminal->escape_length < MAX_ESCAPE)
1963 terminal->escape[terminal->escape_length++] = '\e';
1964 escape_append_utf8(terminal, utf8);
1965 if (terminal->escape_length >= MAX_ESCAPE)
1966 terminal->state = escape_state_normal;
1967 }
1968 continue;
1969 case escape_state_dcs:
1970 case escape_state_osc:
1971 case escape_state_ignore:
1972 if (utf8.byte[0] == '\e') {
1973 terminal->outer_state = terminal->state;
1974 terminal->state = escape_state_inner_escape;
1975 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
1976 terminal->state = escape_state_normal;
1977 handle_osc(terminal);
1978 } else {
1979 escape_append_utf8(terminal, utf8);
1980 if (terminal->escape_length >= MAX_ESCAPE)
1981 terminal->state = escape_state_normal;
1982 }
1983 continue;
1984 case escape_state_special:
1985 escape_append_utf8(terminal, utf8);
1986 terminal->state = escape_state_normal;
1987 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
1988 handle_special_escape(terminal, terminal->escape[1],
1989 utf8.byte[0]);
1990 }
1991 continue;
1992 default:
1993 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001994 }
1995
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001996 /* this is valid, because ASCII characters are never used to
1997 * introduce a multibyte sequence in UTF-8 */
1998 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001999 terminal->state = escape_state_escape;
2000 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002001 terminal->escape[0] = '\e';
2002 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002003 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002004 } else {
2005 handle_char(terminal, utf8);
2006 } /* if */
2007 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002008
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002009 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002010}
2011
2012static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002013data_source_target(void *data,
2014 struct wl_data_source *source, const char *mime_type)
2015{
2016 fprintf(stderr, "data_source_target, %s\n", mime_type);
2017}
2018
2019static void
2020data_source_send(void *data,
2021 struct wl_data_source *source,
2022 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002023{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002024 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002025
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002026 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002027}
2028
2029static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002030data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002031{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002032 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002033}
2034
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002035static const struct wl_data_source_listener data_source_listener = {
2036 data_source_target,
2037 data_source_send,
2038 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002039};
2040
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002041static int
2042handle_bound_key(struct terminal *terminal,
2043 struct input *input, uint32_t sym, uint32_t time)
2044{
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002045 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002046 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002047 /* Cut selection; terminal doesn't do cut, fall
2048 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002049 case XKB_KEY_C:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002050 terminal->selection =
2051 display_create_data_source(terminal->display);
2052 wl_data_source_offer(terminal->selection,
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002053 "text/plain;charset=utf-8");
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002054 wl_data_source_add_listener(terminal->selection,
2055 &data_source_listener, terminal);
2056 input_set_selection(input, terminal->selection, time);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002057 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002058 case XKB_KEY_V:
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002059 input_receive_selection_data_to_fd(input,
2060 "text/plain;charset=utf-8",
2061 terminal->master);
2062
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002063 return 1;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002064 default:
2065 return 0;
2066 }
2067}
2068
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002069static void
2070key_handler(struct window *window, struct input *input, uint32_t time,
2071 uint32_t key, uint32_t sym, uint32_t state, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002072{
2073 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002074 char ch[MAX_RESPONSE];
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002075 uint32_t modifiers;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002076 int len = 0;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002077
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002078 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002079 if ((modifiers & MOD_CONTROL_MASK) &&
2080 (modifiers & MOD_SHIFT_MASK) &&
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002081 state && handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002082 return;
2083
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002084 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002085 case XKB_KEY_F11:
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002086 if (!state)
2087 break;
2088 terminal->fullscreen ^= 1;
2089 window_set_fullscreen(window, terminal->fullscreen);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002090 break;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002091
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002092 case XKB_KEY_BackSpace:
2093 case XKB_KEY_Tab:
2094 case XKB_KEY_Linefeed:
2095 case XKB_KEY_Clear:
2096 case XKB_KEY_Pause:
2097 case XKB_KEY_Scroll_Lock:
2098 case XKB_KEY_Sys_Req:
2099 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002100 ch[len++] = sym & 0x7f;
2101 break;
2102
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002103 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002104 if (terminal->mode & MODE_LF_NEWLINE) {
2105 ch[len++] = 0x0D;
2106 ch[len++] = 0x0A;
2107 } else {
2108 ch[len++] = 0x0D;
2109 }
2110 break;
2111
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002112 case XKB_KEY_Shift_L:
2113 case XKB_KEY_Shift_R:
2114 case XKB_KEY_Control_L:
2115 case XKB_KEY_Control_R:
2116 case XKB_KEY_Alt_L:
2117 case XKB_KEY_Alt_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002118 break;
2119
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002120 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002121 len = function_key_response('[', 2, modifiers, '~', ch);
2122 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002123 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002124 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2125 ch[len++] = '\x04';
2126 } else {
2127 len = function_key_response('[', 3, modifiers, '~', ch);
2128 }
2129 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002130 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002131 len = function_key_response('[', 5, modifiers, '~', ch);
2132 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002133 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002134 len = function_key_response('[', 6, modifiers, '~', ch);
2135 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002136 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002137 len = function_key_response('O', 1, modifiers, 'P', ch);
2138 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002139 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002140 len = function_key_response('O', 1, modifiers, 'Q', ch);
2141 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002142 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002143 len = function_key_response('O', 1, modifiers, 'R', ch);
2144 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002145 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002146 len = function_key_response('O', 1, modifiers, 'S', ch);
2147 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002148 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002149 len = function_key_response('[', 15, modifiers, '~', ch);
2150 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002151 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002152 len = function_key_response('[', 17, modifiers, '~', ch);
2153 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002154 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002155 len = function_key_response('[', 18, modifiers, '~', ch);
2156 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002157 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002158 len = function_key_response('[', 19, modifiers, '~', ch);
2159 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002160 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002161 len = function_key_response('[', 20, modifiers, '~', ch);
2162 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002163 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002164 len = function_key_response('[', 21, modifiers, '~', ch);
2165 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002166 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002167 len = function_key_response('[', 24, modifiers, '~', ch);
2168 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002169 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002170 /* Handle special keys with alternate mappings */
2171 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2172 if (len != 0) break;
2173
Kristian Høgsberg70163132012-05-08 15:55:39 -04002174 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002175 if (sym >= '3' && sym <= '7')
2176 sym = (sym & 0x1f) + 8;
2177
2178 if (!((sym >= '!' && sym <= '/') ||
2179 (sym >= '8' && sym <= '?') ||
2180 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2181 else if (sym == '2') sym = 0x00;
2182 else if (sym == '/') sym = 0x1F;
2183 else if (sym == '8' || sym == '?') sym = 0x7F;
2184 } else if ((terminal->mode & MODE_ALT_SENDS_ESC) &&
Kristian Høgsberg70163132012-05-08 15:55:39 -04002185 (modifiers & MOD_ALT_MASK))
Callum Lowcay7e08e902011-01-07 19:47:02 +00002186 {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002187 ch[len++] = 0x1b;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002188 } else if (modifiers & MOD_ALT_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002189 sym = sym | 0x80;
2190 }
2191
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002192 if (sym < 256)
2193 ch[len++] = sym;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002194 break;
2195 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002196
2197 if (state && len > 0)
Kristian Høgsberg26130862011-08-24 11:30:21 -04002198 terminal_write(terminal, ch, len);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002199}
2200
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002201static void
2202keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002203 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002204{
2205 struct terminal *terminal = data;
2206
2207 terminal->focused = (device != NULL);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002208 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002209}
2210
Kristian Høgsberg59826582011-01-20 11:56:57 -05002211static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002212button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002213 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +01002214 uint32_t button, uint32_t state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002215{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002216 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002217
2218 switch (button) {
2219 case 272:
2220 if (state) {
2221 terminal->dragging = 1;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002222 input_get_position(input,
2223 &terminal->selection_start_x,
2224 &terminal->selection_start_y);
2225 terminal->selection_end_x = terminal->selection_start_x;
2226 terminal->selection_end_y = terminal->selection_start_y;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002227 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002228 } else {
2229 terminal->dragging = 0;
2230 }
2231 break;
2232 }
2233}
2234
2235static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002236motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002237 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002238 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002239{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002240 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002241
2242 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002243 input_get_position(input,
2244 &terminal->selection_end_x,
2245 &terminal->selection_end_y);
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002246 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002247 }
2248
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +03002249 return WL_CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002250}
2251
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002252static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002253terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002254{
2255 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002256 cairo_surface_t *surface;
2257 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002258
2259 terminal = malloc(sizeof *terminal);
2260 if (terminal == NULL)
2261 return terminal;
2262
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002263 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002264 terminal->fullscreen = fullscreen;
Callum Lowcay30eeae52011-01-07 19:46:55 +00002265 terminal->color_scheme = &DEFAULT_COLORS;
2266 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002267 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002268 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002269 terminal->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002270 terminal->widget = frame_create(terminal->window, terminal);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002271 window_set_title(terminal->window, "Wayland Terminal");
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002272 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002273
2274 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002275 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002276
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002277 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002278 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002279
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002280 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002281 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002282 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002283 keyboard_focus_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002284 widget_set_redraw_handler(terminal->widget, redraw_handler);
2285 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002286 widget_set_button_handler(terminal->widget, button_handler);
2287 widget_set_motion_handler(terminal->widget, motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002288
Kristian Høgsberg09531622010-06-14 23:22:15 -04002289 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2290 cr = cairo_create(surface);
Kristian Høgsberg09531622010-06-14 23:22:15 -04002291 cairo_set_font_size(cr, 14);
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002292 cairo_select_font_face (cr, "mono",
2293 CAIRO_FONT_SLANT_NORMAL,
2294 CAIRO_FONT_WEIGHT_BOLD);
2295 terminal->font_bold = cairo_get_scaled_font (cr);
2296 cairo_scaled_font_reference(terminal->font_bold);
2297
2298 cairo_select_font_face (cr, "mono",
2299 CAIRO_FONT_SLANT_NORMAL,
2300 CAIRO_FONT_WEIGHT_NORMAL);
2301 terminal->font_normal = cairo_get_scaled_font (cr);
2302 cairo_scaled_font_reference(terminal->font_normal);
2303
Kristian Høgsberg09531622010-06-14 23:22:15 -04002304 cairo_font_extents(cr, &terminal->extents);
2305 cairo_destroy(cr);
2306 cairo_surface_destroy(surface);
2307
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002308 window_schedule_resize(terminal->window, 500, 400);
2309
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002310 return terminal;
2311}
2312
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002313static void
2314io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002315{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002316 struct terminal *terminal =
2317 container_of(task, struct terminal, io_task);
2318 char buffer[256];
2319 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002320
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002321 if (events & EPOLLHUP)
2322 exit(0);
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002323
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002324 len = read(terminal->master, buffer, sizeof buffer);
2325 if (len < 0)
2326 exit(0);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002327
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002328 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002329}
2330
2331static int
2332terminal_run(struct terminal *terminal, const char *path)
2333{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002334 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002335 pid_t pid;
2336
2337 pid = forkpty(&master, NULL, NULL, NULL);
2338 if (pid == 0) {
Callum Lowcay86653ed2011-01-07 19:47:03 +00002339 setenv("TERM", "xterm-256color", 1);
2340 setenv("COLORTERM", "xterm-256color", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002341 if (execl(path, path, NULL)) {
2342 printf("exec failed: %m\n");
2343 exit(EXIT_FAILURE);
2344 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002345 } else if (pid < 0) {
2346 fprintf(stderr, "failed to fork and create pty (%m).\n");
2347 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002348 }
2349
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002350 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002351 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002352 terminal->io_task.run = io_handler;
2353 display_watch_fd(terminal->display, terminal->master,
2354 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002355
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002356 window_set_fullscreen(terminal->window, terminal->fullscreen);
2357 if (!terminal->fullscreen)
2358 terminal_resize(terminal, 80, 24);
2359
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002360 return 0;
2361}
2362
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002363static const struct weston_option terminal_options[] = {
2364 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002365};
2366
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002367int main(int argc, char *argv[])
2368{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002369 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002370 struct terminal *terminal;
Peter Hutterer035ac942012-02-03 20:58:19 +10002371 const char *shell;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002372
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002373 argc = parse_options(terminal_options,
2374 ARRAY_LENGTH(terminal_options), argc, argv);
2375
2376 d = display_create(argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002377 if (d == NULL) {
2378 fprintf(stderr, "failed to create display: %m\n");
2379 return -1;
2380 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002381
Peter Hutterer035ac942012-02-03 20:58:19 +10002382 shell = getenv("SHELL");
2383 if (!shell)
2384 shell = "/bin/bash";
2385
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002386 terminal = terminal_create(d, option_fullscreen);
Peter Hutterer035ac942012-02-03 20:58:19 +10002387 if (terminal_run(terminal, shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002388 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002389
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002390 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002391
2392 return 0;
2393}