blob: 1b36ba9d8be7a17430d4fd1494594bd2b252370e [file] [log] [blame]
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <fcntl.h>
28#include <unistd.h>
29#include <math.h>
30#include <time.h>
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050031#include <pty.h>
Kristian Høgsbergf04e8382008-12-08 00:07:49 -050032#include <ctype.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050033#include <cairo.h>
34#include <glib.h>
35
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040036#include <X11/keysym.h>
37
Kristian Høgsberg12308a42009-09-28 13:08:50 -040038#include "wayland-util.h"
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050039#include "wayland-client.h"
40#include "wayland-glib.h"
41
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050042#include "window.h"
43
Kristian Høgsberg0395f302008-12-22 12:14:50 -050044static int option_fullscreen;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050045
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050046#define MOD_SHIFT 0x01
47#define MOD_ALT 0x02
48#define MOD_CTRL 0x04
49
Callum Lowcay30eeae52011-01-07 19:46:55 +000050#define ATTRMASK_BOLD 0x01
51#define ATTRMASK_UNDERLINE 0x02
52#define ATTRMASK_BLINK 0x04
53#define ATTRMASK_INVERSE 0x08
Callum Lowcay81179db2011-01-10 12:14:01 +130054#define ATTRMASK_CONCEALED 0x10
Callum Lowcay30eeae52011-01-07 19:46:55 +000055
Callum Lowcayb8609ad2011-01-07 19:46:57 +000056/* Buffer sizes */
57#define MAX_RESPONSE 11
58#define MAX_ESCAPE 64
59
Callum Lowcay8e57dd52011-01-07 19:46:59 +000060/* Terminal modes */
61#define MODE_SHOW_CURSOR 0x00000001
62#define MODE_INVERSE 0x00000002
63#define MODE_AUTOWRAP 0x00000004
64#define MODE_AUTOREPEAT 0x00000008
65#define MODE_LF_NEWLINE 0x00000010
Callum Lowcay69e96582011-01-07 19:47:00 +000066#define MODE_IRM 0x00000020
Callum Lowcay7e08e902011-01-07 19:47:02 +000067#define MODE_DELETE_SENDS_DEL 0x00000040
68#define MODE_ALT_SENDS_ESC 0x00000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +000069
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000070union utf8_char {
71 unsigned char byte[4];
72 uint32_t ch;
73};
74
75enum utf8_state {
76 utf8state_start,
77 utf8state_accept,
78 utf8state_reject,
79 utf8state_expect3,
80 utf8state_expect2,
81 utf8state_expect1
82};
83
84struct utf8_state_machine {
85 enum utf8_state state;
86 int len;
87 union utf8_char s;
88};
89
90static void
91init_state_machine(struct utf8_state_machine *machine)
92{
93 machine->state = utf8state_start;
94 machine->len = 0;
95 machine->s.ch = 0;
96}
97
98static enum utf8_state
99utf8_next_char(struct utf8_state_machine *machine, char c)
100{
101 switch(machine->state) {
102 case utf8state_start:
103 case utf8state_accept:
104 case utf8state_reject:
105 machine->s.ch = 0;
106 machine->len = 0;
107 if(c == 0xC0 || c == 0xC1) {
108 /* overlong encoding, reject */
109 machine->state = utf8state_reject;
110 } else if((c & 0x80) == 0) {
111 /* single byte, accept */
112 machine->s.byte[machine->len++] = c;
113 machine->state = utf8state_accept;
114 } else if((c & 0xC0) == 0x80) {
115 /* parser out of sync, ignore byte */
116 machine->state = utf8state_start;
117 } else if((c & 0xE0) == 0xC0) {
118 /* start of two byte sequence */
119 machine->s.byte[machine->len++] = c;
120 machine->state = utf8state_expect1;
121 } else if((c & 0xF0) == 0xE0) {
122 /* start of three byte sequence */
123 machine->s.byte[machine->len++] = c;
124 machine->state = utf8state_expect2;
125 } else if((c & 0xF8) == 0xF0) {
126 /* start of four byte sequence */
127 machine->s.byte[machine->len++] = c;
128 machine->state = utf8state_expect3;
129 } else {
130 /* overlong encoding, reject */
131 machine->state = utf8state_reject;
132 }
133 break;
134 case utf8state_expect3:
135 machine->s.byte[machine->len++] = c;
136 if((c & 0xC0) == 0x80) {
137 /* all good, continue */
138 machine->state = utf8state_expect2;
139 } else {
140 /* missing extra byte, reject */
141 machine->state = utf8state_reject;
142 }
143 break;
144 case utf8state_expect2:
145 machine->s.byte[machine->len++] = c;
146 if((c & 0xC0) == 0x80) {
147 /* all good, continue */
148 machine->state = utf8state_expect1;
149 } else {
150 /* missing extra byte, reject */
151 machine->state = utf8state_reject;
152 }
153 break;
154 case utf8state_expect1:
155 machine->s.byte[machine->len++] = c;
156 if((c & 0xC0) == 0x80) {
157 /* all good, accept */
158 machine->state = utf8state_accept;
159 } else {
160 /* missing extra byte, reject */
161 machine->state = utf8state_reject;
162 }
163 break;
164 default:
165 machine->state = utf8state_reject;
166 break;
167 }
168
169 return machine->state;
170}
171
Callum Lowcay256e72f2011-01-07 19:47:01 +0000172struct char_sub {
173 union utf8_char match;
174 union utf8_char replace;
175};
176/* Set last char_sub match to NULL char */
177typedef struct char_sub *character_set;
178
179struct char_sub CS_US[] = {
180 {{{0, }}, {{0, }}}
181};
182static struct char_sub CS_UK[] = {
183 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}},
184 {{{0, }}, {{0, }}}
185};
186static struct char_sub CS_SPECIAL[] = {
187 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond */
188 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell */
189 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT */
190 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF */
191 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR */
192 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF */
193 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree */
194 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus */
195 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL */
196 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT */
197 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB */
198 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT */
199 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT */
200 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_RB */
201 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS */
202 {{{'o', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
203 {{{'p', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
204 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
205 {{{'r', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
206 {{{'s', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
207 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR */
208 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL */
209 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU */
210 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD */
211 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V */
212 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE */
213 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE */
214 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI */
215 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ */
216 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND */
217 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT */
218 {{{0, }}, {{0, }}}
219};
220
221static void
222apply_char_set(character_set cs, union utf8_char *utf8)
223{
224 int i = 0;
225
226 while (cs[i].match.byte[0]) {
227 if ((*utf8).ch == cs[i].match.ch) {
228 *utf8 = cs[i].replace;
229 break;
230 }
231 i++;
232 }
233}
234
Callum Lowcay7e08e902011-01-07 19:47:02 +0000235struct key_map {
236 int sym;
237 int num;
238 char escape;
239 char code;
240};
241/* Set last key_sub sym to NULL */
242typedef struct key_map *keyboard_mode;
243
244static struct key_map KM_NORMAL[] = {
245 {XK_Left, 1, '[', 'D'},
246 {XK_Right, 1, '[', 'C'},
247 {XK_Up, 1, '[', 'A'},
248 {XK_Down, 1, '[', 'B'},
249 {XK_Home, 1, '[', 'H'},
250 {XK_End, 1, '[', 'F'},
251 {0, 0, 0, 0}
252};
253static struct key_map KM_APPLICATION[] = {
254 {XK_Left, 1, 'O', 'D'},
255 {XK_Right, 1, 'O', 'C'},
256 {XK_Up, 1, 'O', 'A'},
257 {XK_Down, 1, 'O', 'B'},
258 {XK_Home, 1, 'O', 'H'},
259 {XK_End, 1, 'O', 'F'},
260 {XK_KP_Enter, 1, 'O', 'M'},
261 {XK_KP_Multiply, 1, 'O', 'j'},
262 {XK_KP_Add, 1, 'O', 'k'},
263 {XK_KP_Separator, 1, 'O', 'l'},
264 {XK_KP_Subtract, 1, 'O', 'm'},
265 {XK_KP_Divide, 1, 'O', 'o'},
266 {0, 0, 0, 0}
267};
268
269static int
270function_key_response(char escape, int num, uint32_t modifiers,
271 char code, char *response)
272{
273 int mod_num = 0;
274 int len;
275
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -0500276 if (modifiers & XKB_COMMON_SHIFT_MASK) mod_num |= 1;
277 if (modifiers & XKB_COMMON_MOD1_MASK) mod_num |= 2;
278 if (modifiers & XKB_COMMON_CONTROL_MASK) mod_num |= 4;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000279
280 if (mod_num != 0)
281 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
282 num, mod_num + 1, code);
283 else if (code != '~')
284 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
285 escape, code);
286 else
287 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
288 escape, num, code);
289
290 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
291 else return len;
292}
293
294/* returns the number of bytes written into response,
295 * which must have room for MAX_RESPONSE bytes */
296static int
297apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
298{
299 struct key_map map;
300 int len = 0;
301 int i = 0;
302
303 while (mode[i].sym) {
304 map = mode[i++];
305 if (sym == map.sym) {
306 len = function_key_response(map.escape, map.num,
307 modifiers, map.code,
308 response);
309 break;
310 }
311 }
312
313 return len;
314}
315
Callum Lowcay30eeae52011-01-07 19:46:55 +0000316struct terminal_color { double r, g, b, a; };
317struct attr {
318 unsigned char fg, bg;
319 char a; /* attributes format:
320 * 76543210
Callum Lowcay81179db2011-01-10 12:14:01 +1300321 * cilub */
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500322 char s; /* in selection */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000323};
324struct color_scheme {
325 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500326 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000327 struct attr default_attr;
328};
329
330static void
331attr_init(struct attr *data_attr, struct attr attr, int n)
332{
333 int i;
334 for (i = 0; i < n; i++) {
335 data_attr[i] = attr;
336 }
337}
338
Callum Lowcay67a201d2011-01-12 19:23:41 +1300339enum escape_state {
340 escape_state_normal = 0,
341 escape_state_escape,
342 escape_state_dcs,
343 escape_state_csi,
344 escape_state_osc,
345 escape_state_inner_escape,
346 escape_state_ignore,
347 escape_state_special
348};
349
350#define ESC_FLAG_WHAT 0x01
351#define ESC_FLAG_GT 0x02
352#define ESC_FLAG_BANG 0x04
353#define ESC_FLAG_CASH 0x08
354#define ESC_FLAG_SQUOTE 0x10
355#define ESC_FLAG_DQUOTE 0x20
356#define ESC_FLAG_SPACE 0x40
357
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500358struct terminal {
359 struct window *window;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500360 struct display *display;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000361 union utf8_char *data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000362 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000363 struct attr *data_attr;
364 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000365 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000366 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000367 char saved_origin_mode;
368 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000369 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000370 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000371 character_set cs, g0, g1;
372 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000373 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000374 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500375 int width, height, start, row, column;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000376 int saved_row, saved_column;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500377 int fd, master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500378 GIOChannel *channel;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500379 uint32_t modifiers;
Callum Lowcayb8609ad2011-01-07 19:46:57 +0000380 char escape[MAX_ESCAPE];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500381 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300382 enum escape_state state;
383 enum escape_state outer_state;
384 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000385 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500386 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500387 int fullscreen;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500388 int focused;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400389 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000390 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400391 cairo_font_extents_t extents;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500392 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500393
394 uint32_t tag;
395 struct wl_selection *selection;
396 struct wl_selection_offer *selection_offer;
397 uint32_t selection_offer_has_text;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500398 int32_t dragging, selection_active;
399 int selection_start_x, selection_start_y;
400 int selection_end_x, selection_end_y;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500401};
402
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000403/* Create default tab stops, every 8 characters */
404static void
405terminal_init_tabs(struct terminal *terminal)
406{
407 int i = 0;
408
409 while (i < terminal->width) {
410 if (i % 8 == 0)
411 terminal->tab_ruler[i] = 1;
412 else
413 terminal->tab_ruler[i] = 0;
414 i++;
415 }
416}
417
Callum Lowcay30eeae52011-01-07 19:46:55 +0000418static void
419terminal_init(struct terminal *terminal)
420{
421 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000422 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000423 terminal->mode = MODE_SHOW_CURSOR |
424 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000425 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000426 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000427
428 terminal->row = 0;
429 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000430
Callum Lowcay256e72f2011-01-07 19:47:01 +0000431 terminal->g0 = CS_US;
432 terminal->g1 = CS_US;
433 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000434 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000435
436 terminal->saved_g0 = terminal->g0;
437 terminal->saved_g1 = terminal->g1;
438 terminal->saved_cs = terminal->cs;
439
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000440 terminal->saved_attr = terminal->curr_attr;
441 terminal->saved_origin_mode = terminal->origin_mode;
442 terminal->saved_row = terminal->row;
443 terminal->saved_column = terminal->column;
444
445 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000446}
447
448static void
449init_color_table(struct terminal *terminal)
450{
451 int c, r;
452 struct terminal_color *color_table = terminal->color_table;
453
454 for (c = 0; c < 256; c ++) {
455 if (c < 16) {
456 color_table[c] = terminal->color_scheme->palette[c];
457 } else if (c < 232) {
458 r = c - 16;
459 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
460 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
461 color_table[c].r = ((double)(r % 6) / 6.0);
462 color_table[c].a = 1.0;
463 } else {
464 r = (c - 232) * 10 + 8;
465 color_table[c].r = ((double) r) / 256.0;
466 color_table[c].g = color_table[c].r;
467 color_table[c].b = color_table[c].r;
468 color_table[c].a = 1.0;
469 }
470 }
471}
472
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000473static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500474terminal_get_row(struct terminal *terminal, int row)
475{
476 int index;
477
478 index = (row + terminal->start) % terminal->height;
479
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000480 return &terminal->data[index * terminal->width];
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500481}
482
Callum Lowcay30eeae52011-01-07 19:46:55 +0000483static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500484terminal_get_attr_row(struct terminal *terminal, int row)
485{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000486 int index;
487
488 index = (row + terminal->start) % terminal->height;
489
490 return &terminal->data_attr[index * terminal->width];
491}
492
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500493union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300494 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500495 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500496};
497
Kristian Høgsberg59826582011-01-20 11:56:57 -0500498static int
499terminal_compare_position(struct terminal *terminal,
500 int x, int y, int32_t ref_row, int32_t ref_col)
501{
502 struct rectangle allocation;
503 int top_margin, side_margin, col, row, ref_x;
504
505 window_get_child_allocation(terminal->window, &allocation);
506 side_margin = allocation.x + (allocation.width - terminal->width * terminal->extents.max_x_advance) / 2;
507 top_margin = allocation.y + (allocation.height - terminal->height * terminal->extents.height) / 2;
508
509 col = (x - side_margin) / terminal->extents.max_x_advance;
510 row = (y - top_margin) / terminal->extents.height;
511
512 ref_x = side_margin + ref_col * terminal->extents.max_x_advance +
513 terminal->extents.max_x_advance / 2;
514
515 if (row < ref_row)
516 return -1;
517 if (row == ref_row) {
518 if (col < ref_col)
519 return -1;
520 if (col == ref_col && x < ref_x)
521 return -1;
522 }
523
524 return 1;
525}
526
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500527static void
528terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500529 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500530{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500531 struct attr attr;
532 int foreground, background, tmp;
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500533 int start_cmp, end_cmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500534
535 start_cmp =
536 terminal_compare_position(terminal,
537 terminal->selection_start_x,
538 terminal->selection_start_y,
539 row, col);
540 end_cmp =
541 terminal_compare_position(terminal,
542 terminal->selection_end_x,
543 terminal->selection_end_y,
544 row, col);
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500545 decoded->attr.s = 0;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500546 if (start_cmp < 0 && end_cmp > 0)
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500547 decoded->attr.s = 1;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500548 else if (end_cmp < 0 && start_cmp > 0)
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500549 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500550
551 /* get the attributes for this character cell */
552 attr = terminal_get_attr_row(terminal, row)[col];
553 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500554 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500555 ((terminal->mode & MODE_SHOW_CURSOR) &&
556 terminal->focused && terminal->row == row &&
557 terminal->column == col)) {
558 foreground = attr.bg;
559 background = attr.fg;
560 if (attr.a & ATTRMASK_BOLD) {
561 if (foreground <= 16) foreground |= 0x08;
562 if (background <= 16) background &= 0x07;
563 }
564 } else {
565 foreground = attr.fg;
566 background = attr.bg;
567 }
568
569 if (terminal->mode & MODE_INVERSE) {
570 tmp = foreground;
571 foreground = background;
572 background = tmp;
573 if (attr.a & ATTRMASK_BOLD) {
574 if (foreground <= 16) foreground |= 0x08;
575 if (background <= 16) background &= 0x07;
576 }
577 }
578
Callum Lowcay9d708b02011-01-12 20:06:17 +1300579 decoded->attr.fg = foreground;
580 decoded->attr.bg = background;
581 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000582}
583
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500584
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500585static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000586terminal_scroll_buffer(struct terminal *terminal, int d)
587{
588 int i;
589
590 d = d % (terminal->height + 1);
591 terminal->start = (terminal->start + d) % terminal->height;
592 if (terminal->start < 0) terminal->start = terminal->height + terminal->start;
593 if(d < 0) {
594 d = 0 - d;
595 for(i = 0; i < d; i++) {
596 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
597 attr_init(terminal_get_attr_row(terminal, i),
598 terminal->curr_attr, terminal->width);
599 }
600 } else {
601 for(i = terminal->height - d; i < terminal->height; i++) {
602 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
603 attr_init(terminal_get_attr_row(terminal, i),
604 terminal->curr_attr, terminal->width);
605 }
606 }
607}
608
609static void
610terminal_scroll_window(struct terminal *terminal, int d)
611{
612 int i;
613 int window_height;
614 int from_row, to_row;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000615
616 // scrolling range is inclusive
617 window_height = terminal->margin_bottom - terminal->margin_top + 1;
618 d = d % (window_height + 1);
619 if(d < 0) {
620 d = 0 - d;
621 to_row = terminal->margin_bottom;
622 from_row = terminal->margin_bottom - d;
623
624 for (i = 0; i < (window_height - d); i++) {
625 memcpy(terminal_get_row(terminal, to_row - i),
626 terminal_get_row(terminal, from_row - i),
627 terminal->data_pitch);
628 memcpy(terminal_get_attr_row(terminal, to_row - i),
629 terminal_get_attr_row(terminal, from_row - i),
630 terminal->attr_pitch);
631 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000632 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
633 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000634 attr_init(terminal_get_attr_row(terminal, i),
635 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000636 }
637 } else {
638 to_row = terminal->margin_top;
639 from_row = terminal->margin_top + d;
640
641 for (i = 0; i < (window_height - d); i++) {
642 memcpy(terminal_get_row(terminal, to_row + i),
643 terminal_get_row(terminal, from_row + i),
644 terminal->data_pitch);
645 memcpy(terminal_get_attr_row(terminal, to_row + i),
646 terminal_get_attr_row(terminal, from_row + i),
647 terminal->attr_pitch);
648 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000649 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
650 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000651 attr_init(terminal_get_attr_row(terminal, i),
652 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000653 }
654 }
655}
656
657static void
658terminal_scroll(struct terminal *terminal, int d)
659{
660 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
661 terminal_scroll_buffer(terminal, d);
662 else
663 terminal_scroll_window(terminal, d);
664}
665
666static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000667terminal_shift_line(struct terminal *terminal, int d)
668{
669 union utf8_char *row;
670 struct attr *attr_row, attr;
671
672 row = terminal_get_row(terminal, terminal->row);
673 attr_row = terminal_get_attr_row(terminal, terminal->row);
674
675 if ((terminal->width + d) <= terminal->column)
676 d = terminal->column + 1 - terminal->width;
677 if ((terminal->column + d) >= terminal->width)
678 d = terminal->width - terminal->column - 1;
679
680 if (d < 0) {
681 d = 0 - d;
682 memmove(&row[terminal->column],
683 &row[terminal->column + d],
684 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
685 attr = attr_row[terminal->width - 1];
686 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
687 (terminal->width - terminal->column - d) * sizeof(struct attr));
688 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
689 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
690 } else {
691 memmove(&row[terminal->column + d], &row[terminal->column],
692 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
693 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
694 (terminal->width - terminal->column - d) * sizeof(struct attr));
695 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
696 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
697 }
698}
699
700static void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500701terminal_resize(struct terminal *terminal, int width, int height)
702{
703 size_t size;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000704 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000705 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000706 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000707 int data_pitch, attr_pitch;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500708 int i, l, total_rows, start;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500709 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000710 struct winsize ws;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500711 int32_t pixel_width, pixel_height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500712
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500713 if (width < 1)
714 width = 1;
715 if (height < 1)
716 height = 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500717 if (terminal->width == width && terminal->height == height)
718 return;
719
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500720 if (!terminal->fullscreen) {
721 pixel_width = width *
722 terminal->extents.max_x_advance + 2 * terminal->margin;
723 pixel_height = height *
724 terminal->extents.height + 2 * terminal->margin;
725 window_set_child_size(terminal->window,
726 pixel_width, pixel_height);
727 }
728
729 window_schedule_redraw (terminal->window);
730
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000731 data_pitch = width * sizeof(union utf8_char);
732 size = data_pitch * height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500733 data = malloc(size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000734 attr_pitch = width * sizeof(struct attr);
735 data_attr = malloc(attr_pitch * height);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000736 tab_ruler = malloc(width);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500737 memset(data, 0, size);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000738 memset(tab_ruler, 0, width);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000739 attr_init(data_attr, terminal->curr_attr, width * height);
740 if (terminal->data && terminal->data_attr) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500741 if (width > terminal->width)
742 l = terminal->width;
743 else
744 l = width;
745
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500746 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500747 total_rows = height;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500748 start = terminal->height - height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500749 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500750 total_rows = terminal->height;
751 start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500752 }
753
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000754 for (i = 0; i < total_rows; i++) {
755 memcpy(&data[width * i],
756 terminal_get_row(terminal, i),
757 l * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000758 memcpy(&data_attr[width * i],
759 terminal_get_attr_row(terminal, i),
760 l * sizeof(struct attr));
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000761 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500762
763 free(terminal->data);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000764 free(terminal->data_attr);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000765 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500766 }
767
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000768 terminal->data_pitch = data_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000769 terminal->attr_pitch = attr_pitch;
Callum Lowcay86653ed2011-01-07 19:47:03 +0000770 terminal->margin_bottom =
771 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500772 terminal->width = width;
773 terminal->height = height;
774 terminal->data = data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000775 terminal->data_attr = data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000776 terminal->tab_ruler = tab_ruler;
777 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500778
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000779 /* Update the window size */
780 ws.ws_row = terminal->height;
781 ws.ws_col = terminal->width;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500782 window_get_child_allocation(terminal->window, &allocation);
783 ws.ws_xpixel = allocation.width;
784 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000785 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500786}
787
Callum Lowcay30eeae52011-01-07 19:46:55 +0000788struct color_scheme DEFAULT_COLORS = {
789 {
790 {0, 0, 0, 1}, /* black */
791 {0.66, 0, 0, 1}, /* red */
792 {0 , 0.66, 0, 1}, /* green */
793 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
794 {0 , 0 , 0.66, 1}, /* blue */
795 {0.66, 0 , 0.66, 1}, /* magenta */
796 {0, 0.66, 0.66, 1}, /* cyan */
797 {0.66, 0.66, 0.66, 1}, /* light grey */
798 {0.22, 0.33, 0.33, 1}, /* dark grey */
799 {1, 0.33, 0.33, 1}, /* high red */
800 {0.33, 1, 0.33, 1}, /* high green */
801 {1, 1, 0.33, 1}, /* high yellow */
802 {0.33, 0.33, 1, 1}, /* high blue */
803 {1, 0.33, 1, 1}, /* high magenta */
804 {0.33, 1, 1, 1}, /* high cyan */
805 {1, 1, 1, 1} /* white */
806 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500807 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000808 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
809};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400810
Kristian Høgsberg22106762008-12-08 13:50:07 -0500811static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500812terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
813{
814 cairo_set_source_rgba(cr,
815 terminal->color_table[index].r,
816 terminal->color_table[index].g,
817 terminal->color_table[index].b,
818 terminal->color_table[index].a);
819}
820
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500821static void
822terminal_send_selection(struct terminal *terminal, int fd)
823{
824 int row, col;
825 union utf8_char *p_row;
826 union decoded_attr attr;
827 FILE *fp;
828 int len;
829
830 fp = fdopen(fd, "w");
831 for (row = 0; row < terminal->height; row++) {
832 p_row = terminal_get_row(terminal, row);
833 for (col = 0; col < terminal->width; col++) {
834 /* get the attributes for this character cell */
835 terminal_decode_attr(terminal, row, col, &attr);
836 if (!attr.attr.s)
837 continue;
838 len = strnlen((char *) p_row[col].byte, 4);
839 fwrite(p_row[col].byte, 1, len, fp);
840 }
841 }
842 fclose(fp);
843}
844
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500845struct glyph_run {
846 struct terminal *terminal;
847 cairo_t *cr;
848 int count;
849 union decoded_attr attr;
850 cairo_glyph_t glyphs[256], *g;
851};
852
853static void
854glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
855{
856 run->terminal = terminal;
857 run->cr = cr;
858 run->g = run->glyphs;
859 run->count = 0;
860 run->attr.key = 0;
861}
862
863static void
864glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
865{
866 cairo_scaled_font_t *font;
867
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500868 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
869 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300870 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500871 font = run->terminal->font_bold;
872 else
873 font = run->terminal->font_normal;
874 cairo_set_scaled_font(run->cr, font);
875 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300876 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500877
Callum Lowcay9d708b02011-01-12 20:06:17 +1300878 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
879 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500880 run->g = run->glyphs;
881 run->count = 0;
882 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300883 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500884}
885
886static void
887glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
888{
889 int num_glyphs;
890 cairo_scaled_font_t *font;
891
892 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
893
Callum Lowcay9d708b02011-01-12 20:06:17 +1300894 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500895 font = run->terminal->font_bold;
896 else
897 font = run->terminal->font_normal;
898
899 cairo_move_to(run->cr, x, y);
900 cairo_scaled_font_text_to_glyphs (font, x, y,
901 (char *) c->byte, 4,
902 &run->g, &num_glyphs,
903 NULL, NULL, NULL);
904 run->g += num_glyphs;
905 run->count += num_glyphs;
906}
907
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500908static void
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500909terminal_draw_contents(struct terminal *terminal)
910{
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500911 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500912 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000913 int top_margin, side_margin;
914 int row, col;
915 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500916 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000917 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500918 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500919 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500920 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500921 cairo_font_extents_t extents;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500922
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500923 surface = window_get_surface(terminal->window);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500924 window_get_child_allocation(terminal->window, &allocation);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500925 cr = cairo_create(surface);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500926 cairo_rectangle(cr, allocation.x, allocation.y,
927 allocation.width, allocation.height);
928 cairo_clip(cr);
929 cairo_push_group(cr);
930
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500931 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500932 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500933 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500934
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500935 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500936
Kristian Høgsberg59826582011-01-20 11:56:57 -0500937 extents = terminal->extents;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500938 side_margin = (allocation.width - terminal->width * extents.max_x_advance) / 2;
939 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500940
Callum Lowcay30eeae52011-01-07 19:46:55 +0000941 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500942 cairo_translate(cr, allocation.x + side_margin,
943 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500944 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000945 for (row = 0; row < terminal->height; row++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000946 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000947 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500948 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000949
Callum Lowcay9d708b02011-01-12 20:06:17 +1300950 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -0500951 continue;
952
Callum Lowcay9d708b02011-01-12 20:06:17 +1300953 terminal_set_color(terminal, cr, attr.attr.bg);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500954 cairo_move_to(cr, col * extents.max_x_advance,
955 row * extents.height);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000956 cairo_rel_line_to(cr, extents.max_x_advance, 0);
957 cairo_rel_line_to(cr, 0, extents.height);
958 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
959 cairo_close_path(cr);
960 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500961 }
962 }
Callum Lowcay30eeae52011-01-07 19:46:55 +0000963
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500964 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
965
966 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500967 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500968 for (row = 0; row < terminal->height; row++) {
969 p_row = terminal_get_row(terminal, row);
970 for (col = 0; col < terminal->width; col++) {
971 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500972 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500973
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500974 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000975
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500976 text_x = col * extents.max_x_advance;
977 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +1300978 if (attr.attr.a & ATTRMASK_UNDERLINE) {
979 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000980 cairo_move_to(cr, text_x, (double)text_y + 1.5);
981 cairo_line_to(cr, text_x + extents.max_x_advance, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000982 cairo_stroke(cr);
983 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -0500984
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500985 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000986 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500987 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500988
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500989 attr.key = ~0;
990 glyph_run_flush(&run, attr);
991
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000992 if ((terminal->mode & MODE_SHOW_CURSOR) && !terminal->focused) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000993 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500994
Callum Lowcay30eeae52011-01-07 19:46:55 +0000995 cairo_set_line_width(cr, 1);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500996 cairo_move_to(cr, terminal->column * extents.max_x_advance + d,
997 terminal->row * extents.height + d);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000998 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
999 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
1000 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
1001 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001002
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001003 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001004 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001005
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001006 cairo_pop_group_to_source(cr);
1007 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001008 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001009 cairo_surface_destroy(surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001010}
1011
1012static void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001013resize_handler(struct window *window,
1014 int32_t pixel_width, int32_t pixel_height, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001015{
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001016 struct terminal *terminal = data;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001017 int32_t width, height;
Kristian Høgsberg22106762008-12-08 13:50:07 -05001018
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001019 width = (pixel_width - 2 * terminal->margin) /
Kristian Høgsberg09531622010-06-14 23:22:15 -04001020 (int32_t) terminal->extents.max_x_advance;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001021 height = (pixel_height - 2 * terminal->margin) /
Kristian Høgsberg09531622010-06-14 23:22:15 -04001022 (int32_t) terminal->extents.height;
Tiago Vignatti5fd89d22011-01-10 19:30:04 +02001023
Kristian Høgsberg22106762008-12-08 13:50:07 -05001024 terminal_resize(terminal, width, height);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001025}
Kristian Høgsberg22106762008-12-08 13:50:07 -05001026
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001027static void
1028terminal_draw(struct terminal *terminal)
1029{
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001030 window_draw(terminal->window);
1031 terminal_draw_contents(terminal);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001032 window_flush(terminal->window);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001033}
1034
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001035static void
1036redraw_handler(struct window *window, void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001037{
1038 struct terminal *terminal = data;
1039
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001040 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001041}
1042
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001043static void
1044terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001045
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001046static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001047handle_char(struct terminal *terminal, union utf8_char utf8);
1048
1049static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001050handle_sgr(struct terminal *terminal, int code);
1051
1052static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001053handle_term_parameter(struct terminal *terminal, int code, int sr)
1054{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001055 int i;
1056
Callum Lowcay67a201d2011-01-12 19:23:41 +13001057 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001058 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001059 case 1: /* DECCKM */
1060 if (sr) terminal->key_mode = KM_APPLICATION;
1061 else terminal->key_mode = KM_NORMAL;
1062 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001063 case 2: /* DECANM */
1064 /* No VT52 support yet */
1065 terminal->g0 = CS_US;
1066 terminal->g1 = CS_US;
1067 terminal->cs = terminal->g0;
1068 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001069 case 3: /* DECCOLM */
1070 if (sr)
1071 terminal_resize(terminal, 132, 24);
1072 else
1073 terminal_resize(terminal, 80, 24);
1074
1075 /* set columns, but also home cursor and clear screen */
1076 terminal->row = 0; terminal->column = 0;
1077 for (i = 0; i < terminal->height; i++) {
1078 memset(terminal_get_row(terminal, i),
1079 0, terminal->data_pitch);
1080 attr_init(terminal_get_attr_row(terminal, i),
1081 terminal->curr_attr, terminal->width);
1082 }
1083 break;
1084 case 5: /* DECSCNM */
1085 if (sr) terminal->mode |= MODE_INVERSE;
1086 else terminal->mode &= ~MODE_INVERSE;
1087 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001088 case 6: /* DECOM */
1089 terminal->origin_mode = sr;
1090 if (terminal->origin_mode)
1091 terminal->row = terminal->margin_top;
1092 else
1093 terminal->row = 0;
1094 terminal->column = 0;
1095 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001096 case 7: /* DECAWM */
1097 if (sr) terminal->mode |= MODE_AUTOWRAP;
1098 else terminal->mode &= ~MODE_AUTOWRAP;
1099 break;
1100 case 8: /* DECARM */
1101 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1102 else terminal->mode &= ~MODE_AUTOREPEAT;
1103 break;
1104 case 25:
1105 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1106 else terminal->mode &= ~MODE_SHOW_CURSOR;
1107 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001108 case 1037: /* deleteSendsDel */
1109 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1110 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1111 break;
1112 case 1039: /* altSendsEscape */
1113 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1114 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1115 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001116 default:
1117 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1118 break;
1119 }
1120 } else {
1121 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001122 case 4: /* IRM */
1123 if (sr) terminal->mode |= MODE_IRM;
1124 else terminal->mode &= ~MODE_IRM;
1125 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001126 case 20: /* LNM */
1127 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1128 else terminal->mode &= ~MODE_LF_NEWLINE;
1129 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001130 default:
1131 fprintf(stderr, "Unknown parameter: %d\n", code);
1132 break;
1133 }
1134 }
1135}
1136
1137static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001138handle_dcs(struct terminal *terminal)
1139{
1140}
1141
1142static void
1143handle_osc(struct terminal *terminal)
1144{
1145}
1146
1147static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001148handle_escape(struct terminal *terminal)
1149{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001150 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001151 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001152 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001153 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001154 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001155 char response[MAX_RESPONSE] = {0, };
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001156
1157 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001158 i = 0;
1159 p = &terminal->escape[2];
1160 while ((isdigit(*p) || *p == ';') && i < 10) {
1161 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001162 if (!set[i]) {
1163 args[i] = 0;
1164 set[i] = 1;
1165 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001166 p++;
1167 i++;
1168 } else {
1169 args[i] = strtol(p, &p, 10);
1170 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001171 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001172 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001173
1174 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001175 case '@': /* ICH */
1176 count = set[0] ? args[0] : 1;
1177 if (count == 0) count = 1;
1178 terminal_shift_line(terminal, count);
1179 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001180 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001181 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001182 if (count == 0) count = 1;
1183 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001184 terminal->row -= count;
1185 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001186 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001187 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001188 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001189 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001190 if (count == 0) count = 1;
1191 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001192 terminal->row += count;
1193 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001194 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001195 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001196 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001197 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001198 if (count == 0) count = 1;
1199 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001200 terminal->column += count;
1201 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001202 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001203 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001204 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001205 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001206 if (count == 0) count = 1;
1207 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001208 terminal->column -= count;
1209 else
1210 terminal->column = 0;
1211 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001212 case 'E': /* CNL */
1213 count = set[0] ? args[0] : 1;
1214 if (terminal->row + count <= terminal->margin_bottom)
1215 terminal->row += count;
1216 else
1217 terminal->row = terminal->margin_bottom;
1218 terminal->column = 0;
1219 break;
1220 case 'F': /* CPL */
1221 count = set[0] ? args[0] : 1;
1222 if (terminal->row - count >= terminal->margin_top)
1223 terminal->row -= count;
1224 else
1225 terminal->row = terminal->margin_top;
1226 terminal->column = 0;
1227 break;
1228 case 'G': /* CHA */
1229 y = set[0] ? args[0] : 1;
1230 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1231
1232 terminal->column = y - 1;
1233 break;
1234 case 'f': /* HVP */
1235 case 'H': /* CUP */
1236 x = (set[1] ? args[1] : 1) - 1;
1237 x = x < 0 ? 0 :
1238 (x >= terminal->width ? terminal->width - 1 : x);
1239
1240 y = (set[0] ? args[0] : 1) - 1;
1241 if (terminal->origin_mode) {
1242 y += terminal->margin_top;
1243 y = y < terminal->margin_top ? terminal->margin_top :
1244 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1245 } else {
1246 y = y < 0 ? 0 :
1247 (y >= terminal->height ? terminal->height - 1 : y);
1248 }
1249
1250 terminal->row = y;
1251 terminal->column = x;
1252 break;
1253 case 'I': /* CHT */
1254 count = set[0] ? args[0] : 1;
1255 if (count == 0) count = 1;
1256 while (count > 0 && terminal->column < terminal->width) {
1257 if (terminal->tab_ruler[terminal->column]) count--;
1258 terminal->column++;
1259 }
1260 terminal->column--;
1261 break;
1262 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001263 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001264 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001265 if (!set[0] || args[0] == 0 || args[0] > 2) {
1266 memset(&row[terminal->column],
1267 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1268 attr_init(&attr_row[terminal->column],
1269 terminal->curr_attr, terminal->width - terminal->column);
1270 for (i = terminal->row + 1; i < terminal->height; i++) {
1271 memset(terminal_get_row(terminal, i),
1272 0, terminal->data_pitch);
1273 attr_init(terminal_get_attr_row(terminal, i),
1274 terminal->curr_attr, terminal->width);
1275 }
1276 } else if (args[0] == 1) {
1277 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1278 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1279 for (i = 0; i < terminal->row; i++) {
1280 memset(terminal_get_row(terminal, i),
1281 0, terminal->data_pitch);
1282 attr_init(terminal_get_attr_row(terminal, i),
1283 terminal->curr_attr, terminal->width);
1284 }
1285 } else if (args[0] == 2) {
1286 for (i = 0; i < terminal->height; i++) {
1287 memset(terminal_get_row(terminal, i),
1288 0, terminal->data_pitch);
1289 attr_init(terminal_get_attr_row(terminal, i),
1290 terminal->curr_attr, terminal->width);
1291 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001292 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001293 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001294 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001295 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001296 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001297 if (!set[0] || args[0] == 0 || args[0] > 2) {
1298 memset(&row[terminal->column], 0,
1299 (terminal->width - terminal->column) * sizeof(union utf8_char));
1300 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1301 terminal->width - terminal->column);
1302 } else if (args[0] == 1) {
1303 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1304 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1305 } else if (args[0] == 2) {
1306 memset(row, 0, terminal->data_pitch);
1307 attr_init(attr_row, terminal->curr_attr, terminal->width);
1308 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001309 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001310 case 'L': /* IL */
1311 count = set[0] ? args[0] : 1;
1312 if (count == 0) count = 1;
1313 if (terminal->row >= terminal->margin_top &&
1314 terminal->row < terminal->margin_bottom)
1315 {
1316 top = terminal->margin_top;
1317 terminal->margin_top = terminal->row;
1318 terminal_scroll(terminal, 0 - count);
1319 terminal->margin_top = top;
1320 } else if (terminal->row == terminal->margin_bottom) {
1321 memset(terminal_get_row(terminal, terminal->row),
1322 0, terminal->data_pitch);
1323 attr_init(terminal_get_attr_row(terminal, terminal->row),
1324 terminal->curr_attr, terminal->width);
1325 }
1326 break;
1327 case 'M': /* DL */
1328 count = set[0] ? args[0] : 1;
1329 if (count == 0) count = 1;
1330 if (terminal->row >= terminal->margin_top &&
1331 terminal->row < terminal->margin_bottom)
1332 {
1333 top = terminal->margin_top;
1334 terminal->margin_top = terminal->row;
1335 terminal_scroll(terminal, count);
1336 terminal->margin_top = top;
1337 } else if (terminal->row == terminal->margin_bottom) {
1338 memset(terminal_get_row(terminal, terminal->row),
1339 0, terminal->data_pitch);
1340 }
1341 break;
1342 case 'P': /* DCH */
1343 count = set[0] ? args[0] : 1;
1344 if (count == 0) count = 1;
1345 terminal_shift_line(terminal, 0 - count);
1346 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001347 case 'S': /* SU */
1348 terminal_scroll(terminal, set[0] ? args[0] : 1);
1349 break;
1350 case 'T': /* SD */
1351 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1352 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001353 case 'X': /* ECH */
1354 count = set[0] ? args[0] : 1;
1355 if (count == 0) count = 1;
1356 if ((terminal->column + count) > terminal->width)
1357 count = terminal->width - terminal->column;
1358 row = terminal_get_row(terminal, terminal->row);
1359 attr_row = terminal_get_attr_row(terminal, terminal->row);
1360 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1361 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1362 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001363 case 'Z': /* CBT */
1364 count = set[0] ? args[0] : 1;
1365 if (count == 0) count = 1;
1366 while (count > 0 && terminal->column >= 0) {
1367 if (terminal->tab_ruler[terminal->column]) count--;
1368 terminal->column--;
1369 }
1370 terminal->column++;
1371 break;
1372 case '`': /* HPA */
1373 y = set[0] ? args[0] : 1;
1374 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1375
1376 terminal->column = y - 1;
1377 break;
1378 case 'b': /* REP */
1379 count = set[0] ? args[0] : 1;
1380 if (count == 0) count = 1;
1381 if (terminal->last_char.byte[0])
1382 for (i = 0; i < count; i++)
1383 handle_char(terminal, terminal->last_char);
1384 terminal->last_char.byte[0] = 0;
1385 break;
1386 case 'c': /* Primary DA */
Callum Lowcay69e96582011-01-07 19:47:00 +00001387 write(terminal->master, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001388 break;
1389 case 'd': /* VPA */
1390 x = set[0] ? args[0] : 1;
1391 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1392
1393 terminal->row = x - 1;
1394 break;
1395 case 'g': /* TBC */
1396 if (!set[0] || args[0] == 0) {
1397 terminal->tab_ruler[terminal->column] = 0;
1398 } else if (args[0] == 3) {
1399 memset(terminal->tab_ruler, 0, terminal->width);
1400 }
1401 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001402 case 'h': /* SM */
1403 for(i = 0; i < 10 && set[i]; i++) {
1404 handle_term_parameter(terminal, args[i], 1);
1405 }
1406 break;
1407 case 'l': /* RM */
1408 for(i = 0; i < 10 && set[i]; i++) {
1409 handle_term_parameter(terminal, args[i], 0);
1410 }
1411 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001412 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001413 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001414 if (i <= 7 && set[i] && set[i + 1] &&
1415 set[i + 2] && args[i + 1] == 5)
1416 {
1417 if (args[i] == 38) {
1418 handle_sgr(terminal, args[i + 2] + 256);
1419 break;
1420 } else if (args[i] == 48) {
1421 handle_sgr(terminal, args[i + 2] + 512);
1422 break;
1423 }
1424 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001425 if(set[i]) {
1426 handle_sgr(terminal, args[i]);
1427 } else if(i == 0) {
1428 handle_sgr(terminal, 0);
1429 break;
1430 } else {
1431 break;
1432 }
1433 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001434 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001435 case 'n': /* DSR */
1436 i = set[0] ? args[0] : 0;
1437 if (i == 0 || i == 5) {
1438 write(terminal->master, "\e[0n", 4);
1439 } else if (i == 6) {
1440 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1441 terminal->origin_mode ?
1442 terminal->row+terminal->margin_top : terminal->row+1,
1443 terminal->column+1);
1444 write(terminal->master, response, strlen(response));
1445 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001446 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001447 case 'r':
1448 if(!set[0]) {
1449 terminal->margin_top = 0;
1450 terminal->margin_bottom = terminal->height-1;
1451 terminal->row = 0;
1452 terminal->column = 0;
1453 } else {
1454 top = (set[0] ? args[0] : 1) - 1;
1455 top = top < 0 ? 0 :
1456 (top >= terminal->height ? terminal->height - 1 : top);
1457 bottom = (set[1] ? args[1] : 1) - 1;
1458 bottom = bottom < 0 ? 0 :
1459 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1460 if(bottom > top) {
1461 terminal->margin_top = top;
1462 terminal->margin_bottom = bottom;
1463 } else {
1464 terminal->margin_top = 0;
1465 terminal->margin_bottom = terminal->height-1;
1466 }
1467 if(terminal->origin_mode)
1468 terminal->row = terminal->margin_top;
1469 else
1470 terminal->row = 0;
1471 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001472 }
1473 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001474 case 's':
1475 terminal->saved_row = terminal->row;
1476 terminal->saved_column = terminal->column;
1477 break;
1478 case 'u':
1479 terminal->row = terminal->saved_row;
1480 terminal->column = terminal->saved_column;
1481 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001482 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001483 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001484 break;
1485 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001486}
1487
1488static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001489handle_non_csi_escape(struct terminal *terminal, char code)
1490{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001491 switch(code) {
1492 case 'M': /* RI */
1493 terminal->row -= 1;
1494 if(terminal->row < terminal->margin_top) {
1495 terminal->row = terminal->margin_top;
1496 terminal_scroll(terminal, -1);
1497 }
1498 break;
1499 case 'E': /* NEL */
1500 terminal->column = 0;
1501 // fallthrough
1502 case 'D': /* IND */
1503 terminal->row += 1;
1504 if(terminal->row > terminal->margin_bottom) {
1505 terminal->row = terminal->margin_bottom;
1506 terminal_scroll(terminal, +1);
1507 }
1508 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001509 case 'c': /* RIS */
1510 terminal_init(terminal);
1511 break;
1512 case 'H': /* HTS */
1513 terminal->tab_ruler[terminal->column] = 1;
1514 break;
1515 case '7': /* DECSC */
1516 terminal->saved_row = terminal->row;
1517 terminal->saved_column = terminal->column;
1518 terminal->saved_attr = terminal->curr_attr;
1519 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001520 terminal->saved_cs = terminal->cs;
1521 terminal->saved_g0 = terminal->g0;
1522 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001523 break;
1524 case '8': /* DECRC */
1525 terminal->row = terminal->saved_row;
1526 terminal->column = terminal->saved_column;
1527 terminal->curr_attr = terminal->saved_attr;
1528 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001529 terminal->cs = terminal->saved_cs;
1530 terminal->g0 = terminal->saved_g0;
1531 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001532 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001533 case '=': /* DECPAM */
1534 terminal->key_mode = KM_APPLICATION;
1535 break;
1536 case '>': /* DECPNM */
1537 terminal->key_mode = KM_NORMAL;
1538 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001539 default:
1540 fprintf(stderr, "Unknown escape code: %c\n", code);
1541 break;
1542 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001543}
1544
1545static void
1546handle_special_escape(struct terminal *terminal, char special, char code)
1547{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001548 int i, numChars;
1549
1550 if (special == '#') {
1551 switch(code) {
1552 case '8':
1553 /* fill with 'E', no cheap way to do this */
1554 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1555 numChars = terminal->width * terminal->height;
1556 for(i = 0; i < numChars; i++) {
1557 terminal->data[i].byte[0] = 'E';
1558 }
1559 break;
1560 default:
1561 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1562 break;
1563 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001564 } else if (special == '(' || special == ')') {
1565 switch(code) {
1566 case '0':
1567 if (special == '(')
1568 terminal->g0 = CS_SPECIAL;
1569 else
1570 terminal->g1 = CS_SPECIAL;
1571 break;
1572 case 'A':
1573 if (special == '(')
1574 terminal->g0 = CS_UK;
1575 else
1576 terminal->g1 = CS_UK;
1577 break;
1578 case 'B':
1579 if (special == '(')
1580 terminal->g0 = CS_US;
1581 else
1582 terminal->g1 = CS_US;
1583 break;
1584 default:
1585 fprintf(stderr, "Unknown character set %c\n", code);
1586 break;
1587 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001588 } else {
1589 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1590 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001591}
1592
1593static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001594handle_sgr(struct terminal *terminal, int code)
1595{
1596 switch(code) {
1597 case 0:
1598 terminal->curr_attr = terminal->color_scheme->default_attr;
1599 break;
1600 case 1:
1601 terminal->curr_attr.a |= ATTRMASK_BOLD;
1602 if (terminal->curr_attr.fg < 8)
1603 terminal->curr_attr.fg += 8;
1604 break;
1605 case 4:
1606 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1607 break;
1608 case 5:
1609 terminal->curr_attr.a |= ATTRMASK_BLINK;
1610 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001611 case 8:
1612 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1613 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001614 case 2:
1615 case 21:
1616 case 22:
1617 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1618 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1619 terminal->curr_attr.fg -= 8;
1620 break;
1621 case 24:
1622 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1623 break;
1624 case 25:
1625 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1626 break;
1627 case 7:
1628 case 26:
1629 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1630 break;
1631 case 27:
1632 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1633 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001634 case 28:
1635 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1636 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001637 case 39:
1638 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1639 break;
1640 case 49:
1641 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1642 break;
1643 default:
1644 if(code >= 30 && code <= 37) {
1645 terminal->curr_attr.fg = code - 30;
1646 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1647 terminal->curr_attr.fg += 8;
1648 } else if(code >= 40 && code <= 47) {
1649 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001650 } else if (code >= 90 && code <= 97) {
1651 terminal->curr_attr.fg = code - 90 + 8;
1652 } else if (code >= 100 && code <= 107) {
1653 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001654 } else if(code >= 256 && code < 512) {
1655 terminal->curr_attr.fg = code - 256;
1656 } else if(code >= 512 && code < 768) {
1657 terminal->curr_attr.bg = code - 512;
1658 } else {
1659 fprintf(stderr, "Unknown SGR code: %d\n", code);
1660 }
1661 break;
1662 }
1663}
1664
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001665/* Returns 1 if c was special, otherwise 0 */
1666static int
1667handle_special_char(struct terminal *terminal, char c)
1668{
1669 union utf8_char *row;
1670 struct attr *attr_row;
1671
1672 row = terminal_get_row(terminal, terminal->row);
1673 attr_row = terminal_get_attr_row(terminal, terminal->row);
1674
1675 switch(c) {
1676 case '\r':
1677 terminal->column = 0;
1678 break;
1679 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001680 if (terminal->mode & MODE_LF_NEWLINE) {
1681 terminal->column = 0;
1682 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001683 /* fallthrough */
1684 case '\v':
1685 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001686 terminal->row++;
1687 if(terminal->row > terminal->margin_bottom) {
1688 terminal->row = terminal->margin_bottom;
1689 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001690 }
1691
1692 break;
1693 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001694 while (terminal->column < terminal->width) {
1695 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001696 if (terminal->mode & MODE_IRM)
1697 terminal_shift_line(terminal, +1);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001698 row[terminal->column].byte[0] = ' ';
1699 row[terminal->column].byte[1] = '\0';
1700 attr_row[terminal->column] = terminal->curr_attr;
1701 terminal->column++;
1702 }
1703 if (terminal->column >= terminal->width) {
1704 terminal->column = terminal->width - 1;
1705 }
1706
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001707 break;
1708 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001709 if (terminal->column >= terminal->width) {
1710 terminal->column = terminal->width - 2;
1711 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001712 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001713 } else if (terminal->mode & MODE_AUTOWRAP) {
1714 terminal->column = terminal->width - 1;
1715 terminal->row -= 1;
1716 if (terminal->row < terminal->margin_top) {
1717 terminal->row = terminal->margin_top;
1718 terminal_scroll(terminal, -1);
1719 }
1720 }
1721
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001722 break;
1723 case '\a':
1724 /* Bell */
1725 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001726 case '\x0E': /* SO */
1727 terminal->cs = terminal->g1;
1728 break;
1729 case '\x0F': /* SI */
1730 terminal->cs = terminal->g0;
1731 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001732 default:
1733 return 0;
1734 }
1735
1736 return 1;
1737}
1738
1739static void
1740handle_char(struct terminal *terminal, union utf8_char utf8)
1741{
1742 union utf8_char *row;
1743 struct attr *attr_row;
1744
1745 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001746
1747 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001748
1749 /* There are a whole lot of non-characters, control codes,
1750 * and formatting codes that should probably be ignored,
1751 * for example: */
1752 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1753 /* BOM, ignore */
1754 return;
1755 }
1756
1757 /* Some of these non-characters should be translated, e.g.: */
1758 if (utf8.byte[0] < 32) {
1759 utf8.byte[0] = utf8.byte[0] + 64;
1760 }
1761
1762 /* handle right margin effects */
1763 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001764 if (terminal->mode & MODE_AUTOWRAP) {
1765 terminal->column = 0;
1766 terminal->row += 1;
1767 if (terminal->row > terminal->margin_bottom) {
1768 terminal->row = terminal->margin_bottom;
1769 terminal_scroll(terminal, +1);
1770 }
1771 } else {
1772 terminal->column--;
1773 }
1774 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001775
1776 row = terminal_get_row(terminal, terminal->row);
1777 attr_row = terminal_get_attr_row(terminal, terminal->row);
1778
Callum Lowcay69e96582011-01-07 19:47:00 +00001779 if (terminal->mode & MODE_IRM)
1780 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001781 row[terminal->column] = utf8;
1782 attr_row[terminal->column++] = terminal->curr_attr;
1783
1784 if (utf8.ch != terminal->last_char.ch)
1785 terminal->last_char = utf8;
1786}
1787
Callum Lowcay30eeae52011-01-07 19:46:55 +00001788static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001789escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1790{
1791 int len, i;
1792
1793 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1794 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1795 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1796 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1797 else len = 1; /* Invalid, cannot happen */
1798
1799 if (terminal->escape_length + len <= MAX_ESCAPE) {
1800 for (i = 0; i < len; i++)
1801 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1802 terminal->escape_length += len;
1803 } else if (terminal->escape_length < MAX_ESCAPE) {
1804 terminal->escape[terminal->escape_length++] = 0;
1805 }
1806}
1807
1808static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001809terminal_data(struct terminal *terminal, const char *data, size_t length)
1810{
1811 int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001812 union utf8_char utf8;
1813 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001814
1815 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001816 parser_state =
1817 utf8_next_char(&terminal->state_machine, data[i]);
1818 switch(parser_state) {
1819 case utf8state_accept:
1820 utf8.ch = terminal->state_machine.s.ch;
1821 break;
1822 case utf8state_reject:
1823 /* the unicode replacement character */
1824 utf8.byte[0] = 0xEF;
1825 utf8.byte[1] = 0xBF;
1826 utf8.byte[2] = 0xBD;
1827 utf8.byte[3] = 0x00;
1828 break;
1829 default:
1830 continue;
1831 }
1832
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001833 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001834 switch (terminal->state) {
1835 case escape_state_escape:
1836 escape_append_utf8(terminal, utf8);
1837 switch (utf8.byte[0]) {
1838 case 'P': /* DCS */
1839 terminal->state = escape_state_dcs;
1840 break;
1841 case '[': /* CSI */
1842 terminal->state = escape_state_csi;
1843 break;
1844 case ']': /* OSC */
1845 terminal->state = escape_state_osc;
1846 break;
1847 case '#':
1848 case '(':
1849 case ')': /* special */
1850 terminal->state = escape_state_special;
1851 break;
1852 case '^': /* PM (not implemented) */
1853 case '_': /* APC (not implemented) */
1854 terminal->state = escape_state_ignore;
1855 break;
1856 default:
1857 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001858 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13001859 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001860 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001861 continue;
1862 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001863 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1864 /* do nothing */
1865 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001866 terminal->escape_flags |= ESC_FLAG_WHAT;
1867 } else if (utf8.byte[0] == '>') {
1868 terminal->escape_flags |= ESC_FLAG_GT;
1869 } else if (utf8.byte[0] == '!') {
1870 terminal->escape_flags |= ESC_FLAG_BANG;
1871 } else if (utf8.byte[0] == '$') {
1872 terminal->escape_flags |= ESC_FLAG_CASH;
1873 } else if (utf8.byte[0] == '\'') {
1874 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1875 } else if (utf8.byte[0] == '"') {
1876 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1877 } else if (utf8.byte[0] == ' ') {
1878 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001879 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001880 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001881 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13001882 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001883 }
1884
1885 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1886 utf8.byte[0] == '`')
1887 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001888 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001889 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001890 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001891 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001892 continue;
1893 case escape_state_inner_escape:
1894 if (utf8.byte[0] == '\\') {
1895 terminal->state = escape_state_normal;
1896 if (terminal->outer_state == escape_state_dcs) {
1897 handle_dcs(terminal);
1898 } else if (terminal->outer_state == escape_state_osc) {
1899 handle_osc(terminal);
1900 }
1901 } else if (utf8.byte[0] == '\e') {
1902 terminal->state = terminal->outer_state;
1903 escape_append_utf8(terminal, utf8);
1904 if (terminal->escape_length >= MAX_ESCAPE)
1905 terminal->state = escape_state_normal;
1906 } else {
1907 terminal->state = terminal->outer_state;
1908 if (terminal->escape_length < MAX_ESCAPE)
1909 terminal->escape[terminal->escape_length++] = '\e';
1910 escape_append_utf8(terminal, utf8);
1911 if (terminal->escape_length >= MAX_ESCAPE)
1912 terminal->state = escape_state_normal;
1913 }
1914 continue;
1915 case escape_state_dcs:
1916 case escape_state_osc:
1917 case escape_state_ignore:
1918 if (utf8.byte[0] == '\e') {
1919 terminal->outer_state = terminal->state;
1920 terminal->state = escape_state_inner_escape;
1921 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
1922 terminal->state = escape_state_normal;
1923 handle_osc(terminal);
1924 } else {
1925 escape_append_utf8(terminal, utf8);
1926 if (terminal->escape_length >= MAX_ESCAPE)
1927 terminal->state = escape_state_normal;
1928 }
1929 continue;
1930 case escape_state_special:
1931 escape_append_utf8(terminal, utf8);
1932 terminal->state = escape_state_normal;
1933 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
1934 handle_special_escape(terminal, terminal->escape[1],
1935 utf8.byte[0]);
1936 }
1937 continue;
1938 default:
1939 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001940 }
1941
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001942 /* this is valid, because ASCII characters are never used to
1943 * introduce a multibyte sequence in UTF-8 */
1944 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001945 terminal->state = escape_state_escape;
1946 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001947 terminal->escape[0] = '\e';
1948 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13001949 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001950 } else {
1951 handle_char(terminal, utf8);
1952 } /* if */
1953 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05001954
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001955 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001956}
1957
1958static void
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001959selection_listener_send(void *data, struct wl_selection *selection,
1960 const char *mime_type, int fd)
1961{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05001962 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001963
1964 fprintf(stderr, "selection send, fd is %d\n", fd);
Kristian Høgsberg31cce052011-01-21 15:18:55 -05001965 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001966 close(fd);
1967}
1968
1969static void
1970selection_listener_cancelled(void *data, struct wl_selection *selection)
1971{
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001972 fprintf(stderr, "selection cancelled\n");
1973 wl_selection_destroy(selection);
1974}
1975
1976static const struct wl_selection_listener selection_listener = {
1977 selection_listener_send,
1978 selection_listener_cancelled
1979};
1980
1981static gboolean
1982selection_io_func(GIOChannel *source, GIOCondition condition, gpointer data)
1983{
1984 struct terminal *terminal = data;
1985 char buffer[256];
1986 unsigned int len;
1987 int fd;
1988
1989 fd = g_io_channel_unix_get_fd(source);
1990 len = read(fd, buffer, sizeof buffer);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001991
Kristian Høgsberg59826582011-01-20 11:56:57 -05001992 write(terminal->master, buffer, len);
1993
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001994 close(fd);
1995 g_source_remove(terminal->tag);
1996
1997 g_io_channel_unref(source);
1998
1999 return TRUE;
2000}
2001
2002static int
2003handle_bound_key(struct terminal *terminal,
2004 struct input *input, uint32_t sym, uint32_t time)
2005{
2006 struct wl_shell *shell;
2007 GIOChannel *channel;
2008 int fd;
2009
2010 switch (sym) {
2011 case XK_C:
2012 shell = display_get_shell(terminal->display);
2013 terminal->selection = wl_shell_create_selection(shell);
2014 wl_selection_add_listener(terminal->selection,
2015 &selection_listener, terminal);
2016 wl_selection_offer(terminal->selection, "text/plain");
2017 wl_selection_activate(terminal->selection,
2018 input_get_input_device(input), time);
2019
2020 return 1;
2021 case XK_V:
2022 if (input_offers_mime_type(input, "text/plain")) {
2023 fd = input_receive_mime_type(input, "text/plain");
2024 channel = g_io_channel_unix_new(fd);
2025 terminal->tag = g_io_add_watch(channel, G_IO_IN,
2026 selection_io_func,
2027 terminal);
2028 }
2029
2030 return 1;
2031 case XK_X:
2032 /* cut selection; terminal doesn't do cut */
2033 return 0;
2034 default:
2035 return 0;
2036 }
2037}
2038
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002039static void
2040key_handler(struct window *window, struct input *input, uint32_t time,
2041 uint32_t key, uint32_t sym, uint32_t state, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002042{
2043 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002044 char ch[MAX_RESPONSE];
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002045 uint32_t modifiers;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002046 int len = 0;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002047
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002048 modifiers = input_get_modifiers(input);
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002049 if ((modifiers & XKB_COMMON_CONTROL_MASK) &&
2050 (modifiers & XKB_COMMON_SHIFT_MASK) &&
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002051 state && handle_bound_key(terminal, input, sym, 0))
2052 return;
2053
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002054 switch (sym) {
2055 case XK_F11:
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002056 if (!state)
2057 break;
2058 terminal->fullscreen ^= 1;
2059 window_set_fullscreen(window, terminal->fullscreen);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002060 window_schedule_redraw(terminal->window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002061 break;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002062
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002063 case XK_BackSpace:
2064 case XK_Tab:
2065 case XK_Linefeed:
2066 case XK_Clear:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002067 case XK_Pause:
2068 case XK_Scroll_Lock:
2069 case XK_Sys_Req:
2070 case XK_Escape:
2071 ch[len++] = sym & 0x7f;
2072 break;
2073
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002074 case XK_Return:
2075 if (terminal->mode & MODE_LF_NEWLINE) {
2076 ch[len++] = 0x0D;
2077 ch[len++] = 0x0A;
2078 } else {
2079 ch[len++] = 0x0D;
2080 }
2081 break;
2082
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002083 case XK_Shift_L:
2084 case XK_Shift_R:
2085 case XK_Control_L:
2086 case XK_Control_R:
2087 case XK_Alt_L:
2088 case XK_Alt_R:
2089 break;
2090
Callum Lowcay7e08e902011-01-07 19:47:02 +00002091 case XK_Insert:
2092 len = function_key_response('[', 2, modifiers, '~', ch);
2093 break;
2094 case XK_Delete:
2095 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2096 ch[len++] = '\x04';
2097 } else {
2098 len = function_key_response('[', 3, modifiers, '~', ch);
2099 }
2100 break;
2101 case XK_Page_Up:
2102 len = function_key_response('[', 5, modifiers, '~', ch);
2103 break;
2104 case XK_Page_Down:
2105 len = function_key_response('[', 6, modifiers, '~', ch);
2106 break;
2107 case XK_F1:
2108 len = function_key_response('O', 1, modifiers, 'P', ch);
2109 break;
2110 case XK_F2:
2111 len = function_key_response('O', 1, modifiers, 'Q', ch);
2112 break;
2113 case XK_F3:
2114 len = function_key_response('O', 1, modifiers, 'R', ch);
2115 break;
2116 case XK_F4:
2117 len = function_key_response('O', 1, modifiers, 'S', ch);
2118 break;
2119 case XK_F5:
2120 len = function_key_response('[', 15, modifiers, '~', ch);
2121 break;
2122 case XK_F6:
2123 len = function_key_response('[', 17, modifiers, '~', ch);
2124 break;
2125 case XK_F7:
2126 len = function_key_response('[', 18, modifiers, '~', ch);
2127 break;
2128 case XK_F8:
2129 len = function_key_response('[', 19, modifiers, '~', ch);
2130 break;
2131 case XK_F9:
2132 len = function_key_response('[', 20, modifiers, '~', ch);
2133 break;
2134 case XK_F10:
2135 len = function_key_response('[', 21, modifiers, '~', ch);
2136 break;
2137 case XK_F12:
2138 len = function_key_response('[', 24, modifiers, '~', ch);
2139 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002140 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002141 /* Handle special keys with alternate mappings */
2142 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2143 if (len != 0) break;
2144
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002145 if (modifiers & XKB_COMMON_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002146 if (sym >= '3' && sym <= '7')
2147 sym = (sym & 0x1f) + 8;
2148
2149 if (!((sym >= '!' && sym <= '/') ||
2150 (sym >= '8' && sym <= '?') ||
2151 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2152 else if (sym == '2') sym = 0x00;
2153 else if (sym == '/') sym = 0x1F;
2154 else if (sym == '8' || sym == '?') sym = 0x7F;
2155 } else if ((terminal->mode & MODE_ALT_SENDS_ESC) &&
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002156 (modifiers & XKB_COMMON_MOD1_MASK))
Callum Lowcay7e08e902011-01-07 19:47:02 +00002157 {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002158 ch[len++] = 0x1b;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002159 } else if (modifiers & XKB_COMMON_MOD1_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002160 sym = sym | 0x80;
2161 }
2162
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002163 if (sym < 256)
2164 ch[len++] = sym;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002165 break;
2166 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002167
2168 if (state && len > 0)
2169 write(terminal->master, ch, len);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002170}
2171
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002172static void
2173keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002174 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002175{
2176 struct terminal *terminal = data;
2177
2178 terminal->focused = (device != NULL);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002179 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002180}
2181
Kristian Høgsberg59826582011-01-20 11:56:57 -05002182static void
2183button_handler(struct window *window,
2184 struct input *input, uint32_t time,
2185 int button, int state, void *data)
2186{
2187 struct terminal *terminal = data;
2188
2189 switch (button) {
2190 case 272:
2191 if (state) {
2192 terminal->dragging = 1;
2193 terminal->selection_active = 0;
2194 input_get_position(input,
2195 &terminal->selection_start_x,
2196 &terminal->selection_start_y);
2197 terminal->selection_end_x = terminal->selection_start_x;
2198 terminal->selection_end_y = terminal->selection_start_y;
2199 window_schedule_redraw(window);
2200 } else {
2201 terminal->dragging = 0;
2202 }
2203 break;
2204 }
2205}
2206
2207static int
2208motion_handler(struct window *window,
2209 struct input *input, uint32_t time,
2210 int32_t x, int32_t y,
2211 int32_t sx, int32_t sy, void *data)
2212{
2213 struct terminal *terminal = data;
2214
2215 if (terminal->dragging) {
2216 terminal->selection_active = 1;
2217 input_get_position(input,
2218 &terminal->selection_end_x,
2219 &terminal->selection_end_y);
2220 window_schedule_redraw(window);
2221 }
2222
2223 return POINTER_IBEAM;
2224}
2225
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002226static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002227terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002228{
2229 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002230 cairo_surface_t *surface;
2231 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002232
2233 terminal = malloc(sizeof *terminal);
2234 if (terminal == NULL)
2235 return terminal;
2236
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002237 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002238 terminal->fullscreen = fullscreen;
Callum Lowcay30eeae52011-01-07 19:46:55 +00002239 terminal->color_scheme = &DEFAULT_COLORS;
2240 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002241 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002242 terminal->margin_bottom = -1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002243 terminal->window = window_create(display, "Wayland Terminal",
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002244 500, 400);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002245
2246 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002247 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002248
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002249 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002250 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002251
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002252 window_set_fullscreen(terminal->window, terminal->fullscreen);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002253 window_set_user_data(terminal->window, terminal);
2254 window_set_redraw_handler(terminal->window, redraw_handler);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05002255 window_set_resize_handler(terminal->window, resize_handler);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002256
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002257 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002258 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002259 keyboard_focus_handler);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002260 window_set_button_handler(terminal->window, button_handler);
2261 window_set_motion_handler(terminal->window, motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002262
Kristian Høgsberg09531622010-06-14 23:22:15 -04002263 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2264 cr = cairo_create(surface);
Kristian Høgsberg09531622010-06-14 23:22:15 -04002265 cairo_set_font_size(cr, 14);
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002266 cairo_select_font_face (cr, "mono",
2267 CAIRO_FONT_SLANT_NORMAL,
2268 CAIRO_FONT_WEIGHT_BOLD);
2269 terminal->font_bold = cairo_get_scaled_font (cr);
2270 cairo_scaled_font_reference(terminal->font_bold);
2271
2272 cairo_select_font_face (cr, "mono",
2273 CAIRO_FONT_SLANT_NORMAL,
2274 CAIRO_FONT_WEIGHT_NORMAL);
2275 terminal->font_normal = cairo_get_scaled_font (cr);
2276 cairo_scaled_font_reference(terminal->font_normal);
2277
Kristian Høgsberg09531622010-06-14 23:22:15 -04002278 cairo_font_extents(cr, &terminal->extents);
2279 cairo_destroy(cr);
2280 cairo_surface_destroy(surface);
2281
Callum Lowcaya0ee21c2011-01-07 19:46:56 +00002282 terminal_resize(terminal, 80, 24);
Kristian Høgsberg22106762008-12-08 13:50:07 -05002283 terminal_draw(terminal);
2284
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002285 return terminal;
2286}
2287
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002288static gboolean
2289io_handler(GIOChannel *source,
2290 GIOCondition condition,
2291 gpointer data)
2292{
2293 struct terminal *terminal = data;
2294 gchar buffer[256];
2295 gsize bytes_read;
2296 GError *error = NULL;
2297
2298 g_io_channel_read_chars(source, buffer, sizeof buffer,
2299 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002300
2301 terminal_data(terminal, buffer, bytes_read);
2302
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002303 return TRUE;
2304}
2305
2306static int
2307terminal_run(struct terminal *terminal, const char *path)
2308{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002309 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002310 pid_t pid;
2311
2312 pid = forkpty(&master, NULL, NULL, NULL);
2313 if (pid == 0) {
Callum Lowcay86653ed2011-01-07 19:47:03 +00002314 setenv("TERM", "xterm-256color", 1);
2315 setenv("COLORTERM", "xterm-256color", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002316 if (execl(path, path, NULL)) {
2317 printf("exec failed: %m\n");
2318 exit(EXIT_FAILURE);
2319 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002320 } else if (pid < 0) {
2321 fprintf(stderr, "failed to fork and create pty (%m).\n");
2322 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002323 }
2324
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002325 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002326 terminal->channel = g_io_channel_unix_new(master);
2327 fcntl(master, F_SETFL, O_NONBLOCK);
2328 g_io_add_watch(terminal->channel, G_IO_IN,
2329 io_handler, terminal);
2330
2331 return 0;
2332}
2333
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002334static const GOptionEntry option_entries[] = {
2335 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
2336 &option_fullscreen, "Run in fullscreen mode" },
2337 { NULL }
2338};
2339
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002340int main(int argc, char *argv[])
2341{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002342 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002343 struct terminal *terminal;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002344
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002345 d = display_create(&argc, &argv, option_entries);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002346 if (d == NULL) {
2347 fprintf(stderr, "failed to create display: %m\n");
2348 return -1;
2349 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002350
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002351 terminal = terminal_create(d, option_fullscreen);
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002352 if (terminal_run(terminal, "/bin/bash"))
2353 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002354
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002355 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002356
2357 return 0;
2358}