blob: 78f575f5c006072863be45b0602c2c2d97a316b6 [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
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500394 struct wl_selection *selection;
395 struct wl_selection_offer *selection_offer;
396 uint32_t selection_offer_has_text;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500397 int32_t dragging, selection_active;
398 int selection_start_x, selection_start_y;
399 int selection_end_x, selection_end_y;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500400};
401
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000402/* Create default tab stops, every 8 characters */
403static void
404terminal_init_tabs(struct terminal *terminal)
405{
406 int i = 0;
407
408 while (i < terminal->width) {
409 if (i % 8 == 0)
410 terminal->tab_ruler[i] = 1;
411 else
412 terminal->tab_ruler[i] = 0;
413 i++;
414 }
415}
416
Callum Lowcay30eeae52011-01-07 19:46:55 +0000417static void
418terminal_init(struct terminal *terminal)
419{
420 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000421 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000422 terminal->mode = MODE_SHOW_CURSOR |
423 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000424 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000425 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000426
427 terminal->row = 0;
428 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000429
Callum Lowcay256e72f2011-01-07 19:47:01 +0000430 terminal->g0 = CS_US;
431 terminal->g1 = CS_US;
432 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000433 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000434
435 terminal->saved_g0 = terminal->g0;
436 terminal->saved_g1 = terminal->g1;
437 terminal->saved_cs = terminal->cs;
438
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000439 terminal->saved_attr = terminal->curr_attr;
440 terminal->saved_origin_mode = terminal->origin_mode;
441 terminal->saved_row = terminal->row;
442 terminal->saved_column = terminal->column;
443
444 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000445}
446
447static void
448init_color_table(struct terminal *terminal)
449{
450 int c, r;
451 struct terminal_color *color_table = terminal->color_table;
452
453 for (c = 0; c < 256; c ++) {
454 if (c < 16) {
455 color_table[c] = terminal->color_scheme->palette[c];
456 } else if (c < 232) {
457 r = c - 16;
458 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
459 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
460 color_table[c].r = ((double)(r % 6) / 6.0);
461 color_table[c].a = 1.0;
462 } else {
463 r = (c - 232) * 10 + 8;
464 color_table[c].r = ((double) r) / 256.0;
465 color_table[c].g = color_table[c].r;
466 color_table[c].b = color_table[c].r;
467 color_table[c].a = 1.0;
468 }
469 }
470}
471
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000472static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500473terminal_get_row(struct terminal *terminal, int row)
474{
475 int index;
476
477 index = (row + terminal->start) % terminal->height;
478
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000479 return &terminal->data[index * terminal->width];
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500480}
481
Callum Lowcay30eeae52011-01-07 19:46:55 +0000482static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500483terminal_get_attr_row(struct terminal *terminal, int row)
484{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000485 int index;
486
487 index = (row + terminal->start) % terminal->height;
488
489 return &terminal->data_attr[index * terminal->width];
490}
491
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500492union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300493 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500494 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500495};
496
Kristian Høgsberg59826582011-01-20 11:56:57 -0500497static int
498terminal_compare_position(struct terminal *terminal,
499 int x, int y, int32_t ref_row, int32_t ref_col)
500{
501 struct rectangle allocation;
502 int top_margin, side_margin, col, row, ref_x;
503
504 window_get_child_allocation(terminal->window, &allocation);
505 side_margin = allocation.x + (allocation.width - terminal->width * terminal->extents.max_x_advance) / 2;
506 top_margin = allocation.y + (allocation.height - terminal->height * terminal->extents.height) / 2;
507
508 col = (x - side_margin) / terminal->extents.max_x_advance;
509 row = (y - top_margin) / terminal->extents.height;
510
511 ref_x = side_margin + ref_col * terminal->extents.max_x_advance +
512 terminal->extents.max_x_advance / 2;
513
514 if (row < ref_row)
515 return -1;
516 if (row == ref_row) {
517 if (col < ref_col)
518 return -1;
519 if (col == ref_col && x < ref_x)
520 return -1;
521 }
522
523 return 1;
524}
525
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500526static void
527terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500528 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500529{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500530 struct attr attr;
531 int foreground, background, tmp;
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500532 int start_cmp, end_cmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500533
534 start_cmp =
535 terminal_compare_position(terminal,
536 terminal->selection_start_x,
537 terminal->selection_start_y,
538 row, col);
539 end_cmp =
540 terminal_compare_position(terminal,
541 terminal->selection_end_x,
542 terminal->selection_end_y,
543 row, col);
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500544 decoded->attr.s = 0;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500545 if (start_cmp < 0 && end_cmp > 0)
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500546 decoded->attr.s = 1;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500547 else if (end_cmp < 0 && start_cmp > 0)
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500548 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500549
550 /* get the attributes for this character cell */
551 attr = terminal_get_attr_row(terminal, row)[col];
552 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500553 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500554 ((terminal->mode & MODE_SHOW_CURSOR) &&
555 terminal->focused && terminal->row == row &&
556 terminal->column == col)) {
557 foreground = attr.bg;
558 background = attr.fg;
559 if (attr.a & ATTRMASK_BOLD) {
560 if (foreground <= 16) foreground |= 0x08;
561 if (background <= 16) background &= 0x07;
562 }
563 } else {
564 foreground = attr.fg;
565 background = attr.bg;
566 }
567
568 if (terminal->mode & MODE_INVERSE) {
569 tmp = foreground;
570 foreground = background;
571 background = tmp;
572 if (attr.a & ATTRMASK_BOLD) {
573 if (foreground <= 16) foreground |= 0x08;
574 if (background <= 16) background &= 0x07;
575 }
576 }
577
Callum Lowcay9d708b02011-01-12 20:06:17 +1300578 decoded->attr.fg = foreground;
579 decoded->attr.bg = background;
580 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000581}
582
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500583
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500584static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000585terminal_scroll_buffer(struct terminal *terminal, int d)
586{
587 int i;
588
589 d = d % (terminal->height + 1);
590 terminal->start = (terminal->start + d) % terminal->height;
591 if (terminal->start < 0) terminal->start = terminal->height + terminal->start;
592 if(d < 0) {
593 d = 0 - d;
594 for(i = 0; i < d; i++) {
595 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
596 attr_init(terminal_get_attr_row(terminal, i),
597 terminal->curr_attr, terminal->width);
598 }
599 } else {
600 for(i = terminal->height - d; i < terminal->height; i++) {
601 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
602 attr_init(terminal_get_attr_row(terminal, i),
603 terminal->curr_attr, terminal->width);
604 }
605 }
606}
607
608static void
609terminal_scroll_window(struct terminal *terminal, int d)
610{
611 int i;
612 int window_height;
613 int from_row, to_row;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000614
615 // scrolling range is inclusive
616 window_height = terminal->margin_bottom - terminal->margin_top + 1;
617 d = d % (window_height + 1);
618 if(d < 0) {
619 d = 0 - d;
620 to_row = terminal->margin_bottom;
621 from_row = terminal->margin_bottom - d;
622
623 for (i = 0; i < (window_height - d); i++) {
624 memcpy(terminal_get_row(terminal, to_row - i),
625 terminal_get_row(terminal, from_row - i),
626 terminal->data_pitch);
627 memcpy(terminal_get_attr_row(terminal, to_row - i),
628 terminal_get_attr_row(terminal, from_row - i),
629 terminal->attr_pitch);
630 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000631 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
632 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000633 attr_init(terminal_get_attr_row(terminal, i),
634 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000635 }
636 } else {
637 to_row = terminal->margin_top;
638 from_row = terminal->margin_top + d;
639
640 for (i = 0; i < (window_height - d); i++) {
641 memcpy(terminal_get_row(terminal, to_row + i),
642 terminal_get_row(terminal, from_row + i),
643 terminal->data_pitch);
644 memcpy(terminal_get_attr_row(terminal, to_row + i),
645 terminal_get_attr_row(terminal, from_row + i),
646 terminal->attr_pitch);
647 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000648 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
649 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000650 attr_init(terminal_get_attr_row(terminal, i),
651 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000652 }
653 }
654}
655
656static void
657terminal_scroll(struct terminal *terminal, int d)
658{
659 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
660 terminal_scroll_buffer(terminal, d);
661 else
662 terminal_scroll_window(terminal, d);
663}
664
665static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000666terminal_shift_line(struct terminal *terminal, int d)
667{
668 union utf8_char *row;
669 struct attr *attr_row, attr;
670
671 row = terminal_get_row(terminal, terminal->row);
672 attr_row = terminal_get_attr_row(terminal, terminal->row);
673
674 if ((terminal->width + d) <= terminal->column)
675 d = terminal->column + 1 - terminal->width;
676 if ((terminal->column + d) >= terminal->width)
677 d = terminal->width - terminal->column - 1;
678
679 if (d < 0) {
680 d = 0 - d;
681 memmove(&row[terminal->column],
682 &row[terminal->column + d],
683 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
684 attr = attr_row[terminal->width - 1];
685 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
686 (terminal->width - terminal->column - d) * sizeof(struct attr));
687 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
688 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
689 } else {
690 memmove(&row[terminal->column + d], &row[terminal->column],
691 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
692 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
693 (terminal->width - terminal->column - d) * sizeof(struct attr));
694 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
695 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
696 }
697}
698
699static void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500700terminal_resize(struct terminal *terminal, int width, int height)
701{
702 size_t size;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000703 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000704 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000705 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000706 int data_pitch, attr_pitch;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500707 int i, l, total_rows, start;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500708 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000709 struct winsize ws;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500710 int32_t pixel_width, pixel_height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500711
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500712 if (width < 1)
713 width = 1;
714 if (height < 1)
715 height = 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500716 if (terminal->width == width && terminal->height == height)
717 return;
718
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500719 if (!terminal->fullscreen) {
720 pixel_width = width *
721 terminal->extents.max_x_advance + 2 * terminal->margin;
722 pixel_height = height *
723 terminal->extents.height + 2 * terminal->margin;
724 window_set_child_size(terminal->window,
725 pixel_width, pixel_height);
726 }
727
728 window_schedule_redraw (terminal->window);
729
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000730 data_pitch = width * sizeof(union utf8_char);
731 size = data_pitch * height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500732 data = malloc(size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000733 attr_pitch = width * sizeof(struct attr);
734 data_attr = malloc(attr_pitch * height);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000735 tab_ruler = malloc(width);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500736 memset(data, 0, size);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000737 memset(tab_ruler, 0, width);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000738 attr_init(data_attr, terminal->curr_attr, width * height);
739 if (terminal->data && terminal->data_attr) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500740 if (width > terminal->width)
741 l = terminal->width;
742 else
743 l = width;
744
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500745 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500746 total_rows = height;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500747 start = terminal->height - height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500748 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500749 total_rows = terminal->height;
750 start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500751 }
752
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000753 for (i = 0; i < total_rows; i++) {
754 memcpy(&data[width * i],
755 terminal_get_row(terminal, i),
756 l * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000757 memcpy(&data_attr[width * i],
758 terminal_get_attr_row(terminal, i),
759 l * sizeof(struct attr));
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000760 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500761
762 free(terminal->data);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000763 free(terminal->data_attr);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000764 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500765 }
766
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000767 terminal->data_pitch = data_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000768 terminal->attr_pitch = attr_pitch;
Callum Lowcay86653ed2011-01-07 19:47:03 +0000769 terminal->margin_bottom =
770 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500771 terminal->width = width;
772 terminal->height = height;
773 terminal->data = data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000774 terminal->data_attr = data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000775 terminal->tab_ruler = tab_ruler;
776 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500777
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000778 /* Update the window size */
779 ws.ws_row = terminal->height;
780 ws.ws_col = terminal->width;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500781 window_get_child_allocation(terminal->window, &allocation);
782 ws.ws_xpixel = allocation.width;
783 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000784 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500785}
786
Callum Lowcay30eeae52011-01-07 19:46:55 +0000787struct color_scheme DEFAULT_COLORS = {
788 {
789 {0, 0, 0, 1}, /* black */
790 {0.66, 0, 0, 1}, /* red */
791 {0 , 0.66, 0, 1}, /* green */
792 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
793 {0 , 0 , 0.66, 1}, /* blue */
794 {0.66, 0 , 0.66, 1}, /* magenta */
795 {0, 0.66, 0.66, 1}, /* cyan */
796 {0.66, 0.66, 0.66, 1}, /* light grey */
797 {0.22, 0.33, 0.33, 1}, /* dark grey */
798 {1, 0.33, 0.33, 1}, /* high red */
799 {0.33, 1, 0.33, 1}, /* high green */
800 {1, 1, 0.33, 1}, /* high yellow */
801 {0.33, 0.33, 1, 1}, /* high blue */
802 {1, 0.33, 1, 1}, /* high magenta */
803 {0.33, 1, 1, 1}, /* high cyan */
804 {1, 1, 1, 1} /* white */
805 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500806 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000807 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
808};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400809
Kristian Høgsberg22106762008-12-08 13:50:07 -0500810static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500811terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
812{
813 cairo_set_source_rgba(cr,
814 terminal->color_table[index].r,
815 terminal->color_table[index].g,
816 terminal->color_table[index].b,
817 terminal->color_table[index].a);
818}
819
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500820static void
821terminal_send_selection(struct terminal *terminal, int fd)
822{
823 int row, col;
824 union utf8_char *p_row;
825 union decoded_attr attr;
826 FILE *fp;
827 int len;
828
829 fp = fdopen(fd, "w");
830 for (row = 0; row < terminal->height; row++) {
831 p_row = terminal_get_row(terminal, row);
832 for (col = 0; col < terminal->width; col++) {
833 /* get the attributes for this character cell */
834 terminal_decode_attr(terminal, row, col, &attr);
835 if (!attr.attr.s)
836 continue;
837 len = strnlen((char *) p_row[col].byte, 4);
838 fwrite(p_row[col].byte, 1, len, fp);
839 }
840 }
841 fclose(fp);
842}
843
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500844struct glyph_run {
845 struct terminal *terminal;
846 cairo_t *cr;
847 int count;
848 union decoded_attr attr;
849 cairo_glyph_t glyphs[256], *g;
850};
851
852static void
853glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
854{
855 run->terminal = terminal;
856 run->cr = cr;
857 run->g = run->glyphs;
858 run->count = 0;
859 run->attr.key = 0;
860}
861
862static void
863glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
864{
865 cairo_scaled_font_t *font;
866
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500867 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
868 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300869 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500870 font = run->terminal->font_bold;
871 else
872 font = run->terminal->font_normal;
873 cairo_set_scaled_font(run->cr, font);
874 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300875 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500876
Callum Lowcay9d708b02011-01-12 20:06:17 +1300877 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
878 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500879 run->g = run->glyphs;
880 run->count = 0;
881 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300882 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500883}
884
885static void
886glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
887{
888 int num_glyphs;
889 cairo_scaled_font_t *font;
890
891 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
892
Callum Lowcay9d708b02011-01-12 20:06:17 +1300893 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500894 font = run->terminal->font_bold;
895 else
896 font = run->terminal->font_normal;
897
898 cairo_move_to(run->cr, x, y);
899 cairo_scaled_font_text_to_glyphs (font, x, y,
900 (char *) c->byte, 4,
901 &run->g, &num_glyphs,
902 NULL, NULL, NULL);
903 run->g += num_glyphs;
904 run->count += num_glyphs;
905}
906
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500907static void
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500908terminal_draw_contents(struct terminal *terminal)
909{
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500910 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500911 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000912 int top_margin, side_margin;
913 int row, col;
914 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500915 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000916 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500917 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500918 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500919 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500920 cairo_font_extents_t extents;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500921
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500922 surface = window_get_surface(terminal->window);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500923 window_get_child_allocation(terminal->window, &allocation);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500924 cr = cairo_create(surface);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500925 cairo_rectangle(cr, allocation.x, allocation.y,
926 allocation.width, allocation.height);
927 cairo_clip(cr);
928 cairo_push_group(cr);
929
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500930 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500931 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500932 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500933
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500934 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500935
Kristian Høgsberg59826582011-01-20 11:56:57 -0500936 extents = terminal->extents;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500937 side_margin = (allocation.width - terminal->width * extents.max_x_advance) / 2;
938 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500939
Callum Lowcay30eeae52011-01-07 19:46:55 +0000940 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500941 cairo_translate(cr, allocation.x + side_margin,
942 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500943 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000944 for (row = 0; row < terminal->height; row++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000945 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000946 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500947 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000948
Callum Lowcay9d708b02011-01-12 20:06:17 +1300949 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -0500950 continue;
951
Callum Lowcay9d708b02011-01-12 20:06:17 +1300952 terminal_set_color(terminal, cr, attr.attr.bg);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500953 cairo_move_to(cr, col * extents.max_x_advance,
954 row * extents.height);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000955 cairo_rel_line_to(cr, extents.max_x_advance, 0);
956 cairo_rel_line_to(cr, 0, extents.height);
957 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
958 cairo_close_path(cr);
959 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500960 }
961 }
Callum Lowcay30eeae52011-01-07 19:46:55 +0000962
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500963 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
964
965 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500966 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500967 for (row = 0; row < terminal->height; row++) {
968 p_row = terminal_get_row(terminal, row);
969 for (col = 0; col < terminal->width; col++) {
970 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500971 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500972
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500973 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000974
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500975 text_x = col * extents.max_x_advance;
976 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +1300977 if (attr.attr.a & ATTRMASK_UNDERLINE) {
978 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000979 cairo_move_to(cr, text_x, (double)text_y + 1.5);
980 cairo_line_to(cr, text_x + extents.max_x_advance, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000981 cairo_stroke(cr);
982 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -0500983
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500984 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000985 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500986 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500987
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500988 attr.key = ~0;
989 glyph_run_flush(&run, attr);
990
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000991 if ((terminal->mode & MODE_SHOW_CURSOR) && !terminal->focused) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000992 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500993
Callum Lowcay30eeae52011-01-07 19:46:55 +0000994 cairo_set_line_width(cr, 1);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500995 cairo_move_to(cr, terminal->column * extents.max_x_advance + d,
996 terminal->row * extents.height + d);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000997 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
998 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
999 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
1000 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001001
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001002 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001003 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001004
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001005 cairo_pop_group_to_source(cr);
1006 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001007 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001008 cairo_surface_destroy(surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001009}
1010
1011static void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001012resize_handler(struct window *window,
1013 int32_t pixel_width, int32_t pixel_height, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001014{
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001015 struct terminal *terminal = data;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001016 int32_t width, height;
Kristian Høgsberg22106762008-12-08 13:50:07 -05001017
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001018 width = (pixel_width - 2 * terminal->margin) /
Kristian Høgsberg09531622010-06-14 23:22:15 -04001019 (int32_t) terminal->extents.max_x_advance;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001020 height = (pixel_height - 2 * terminal->margin) /
Kristian Høgsberg09531622010-06-14 23:22:15 -04001021 (int32_t) terminal->extents.height;
Tiago Vignatti5fd89d22011-01-10 19:30:04 +02001022
Kristian Høgsberg22106762008-12-08 13:50:07 -05001023 terminal_resize(terminal, width, height);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001024}
Kristian Høgsberg22106762008-12-08 13:50:07 -05001025
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001026static void
1027terminal_draw(struct terminal *terminal)
1028{
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001029 window_draw(terminal->window);
1030 terminal_draw_contents(terminal);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001031 window_flush(terminal->window);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001032}
1033
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001034static void
1035redraw_handler(struct window *window, void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001036{
1037 struct terminal *terminal = data;
1038
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001039 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001040}
1041
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001042static void
1043terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001044
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001045static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001046handle_char(struct terminal *terminal, union utf8_char utf8);
1047
1048static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001049handle_sgr(struct terminal *terminal, int code);
1050
1051static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001052handle_term_parameter(struct terminal *terminal, int code, int sr)
1053{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001054 int i;
1055
Callum Lowcay67a201d2011-01-12 19:23:41 +13001056 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001057 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001058 case 1: /* DECCKM */
1059 if (sr) terminal->key_mode = KM_APPLICATION;
1060 else terminal->key_mode = KM_NORMAL;
1061 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001062 case 2: /* DECANM */
1063 /* No VT52 support yet */
1064 terminal->g0 = CS_US;
1065 terminal->g1 = CS_US;
1066 terminal->cs = terminal->g0;
1067 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001068 case 3: /* DECCOLM */
1069 if (sr)
1070 terminal_resize(terminal, 132, 24);
1071 else
1072 terminal_resize(terminal, 80, 24);
1073
1074 /* set columns, but also home cursor and clear screen */
1075 terminal->row = 0; terminal->column = 0;
1076 for (i = 0; i < terminal->height; i++) {
1077 memset(terminal_get_row(terminal, i),
1078 0, terminal->data_pitch);
1079 attr_init(terminal_get_attr_row(terminal, i),
1080 terminal->curr_attr, terminal->width);
1081 }
1082 break;
1083 case 5: /* DECSCNM */
1084 if (sr) terminal->mode |= MODE_INVERSE;
1085 else terminal->mode &= ~MODE_INVERSE;
1086 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001087 case 6: /* DECOM */
1088 terminal->origin_mode = sr;
1089 if (terminal->origin_mode)
1090 terminal->row = terminal->margin_top;
1091 else
1092 terminal->row = 0;
1093 terminal->column = 0;
1094 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001095 case 7: /* DECAWM */
1096 if (sr) terminal->mode |= MODE_AUTOWRAP;
1097 else terminal->mode &= ~MODE_AUTOWRAP;
1098 break;
1099 case 8: /* DECARM */
1100 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1101 else terminal->mode &= ~MODE_AUTOREPEAT;
1102 break;
1103 case 25:
1104 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1105 else terminal->mode &= ~MODE_SHOW_CURSOR;
1106 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001107 case 1037: /* deleteSendsDel */
1108 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1109 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1110 break;
1111 case 1039: /* altSendsEscape */
1112 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1113 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1114 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001115 default:
1116 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1117 break;
1118 }
1119 } else {
1120 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001121 case 4: /* IRM */
1122 if (sr) terminal->mode |= MODE_IRM;
1123 else terminal->mode &= ~MODE_IRM;
1124 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001125 case 20: /* LNM */
1126 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1127 else terminal->mode &= ~MODE_LF_NEWLINE;
1128 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001129 default:
1130 fprintf(stderr, "Unknown parameter: %d\n", code);
1131 break;
1132 }
1133 }
1134}
1135
1136static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001137handle_dcs(struct terminal *terminal)
1138{
1139}
1140
1141static void
1142handle_osc(struct terminal *terminal)
1143{
1144}
1145
1146static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001147handle_escape(struct terminal *terminal)
1148{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001149 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001150 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001151 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001152 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001153 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001154 char response[MAX_RESPONSE] = {0, };
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001155
1156 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001157 i = 0;
1158 p = &terminal->escape[2];
1159 while ((isdigit(*p) || *p == ';') && i < 10) {
1160 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001161 if (!set[i]) {
1162 args[i] = 0;
1163 set[i] = 1;
1164 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001165 p++;
1166 i++;
1167 } else {
1168 args[i] = strtol(p, &p, 10);
1169 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001170 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001171 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001172
1173 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001174 case '@': /* ICH */
1175 count = set[0] ? args[0] : 1;
1176 if (count == 0) count = 1;
1177 terminal_shift_line(terminal, count);
1178 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001179 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001180 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001181 if (count == 0) count = 1;
1182 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001183 terminal->row -= count;
1184 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001185 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001186 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001187 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001188 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001189 if (count == 0) count = 1;
1190 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001191 terminal->row += count;
1192 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001193 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001194 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001195 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001196 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001197 if (count == 0) count = 1;
1198 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001199 terminal->column += count;
1200 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001201 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001202 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001203 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001204 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001205 if (count == 0) count = 1;
1206 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001207 terminal->column -= count;
1208 else
1209 terminal->column = 0;
1210 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001211 case 'E': /* CNL */
1212 count = set[0] ? args[0] : 1;
1213 if (terminal->row + count <= terminal->margin_bottom)
1214 terminal->row += count;
1215 else
1216 terminal->row = terminal->margin_bottom;
1217 terminal->column = 0;
1218 break;
1219 case 'F': /* CPL */
1220 count = set[0] ? args[0] : 1;
1221 if (terminal->row - count >= terminal->margin_top)
1222 terminal->row -= count;
1223 else
1224 terminal->row = terminal->margin_top;
1225 terminal->column = 0;
1226 break;
1227 case 'G': /* CHA */
1228 y = set[0] ? args[0] : 1;
1229 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1230
1231 terminal->column = y - 1;
1232 break;
1233 case 'f': /* HVP */
1234 case 'H': /* CUP */
1235 x = (set[1] ? args[1] : 1) - 1;
1236 x = x < 0 ? 0 :
1237 (x >= terminal->width ? terminal->width - 1 : x);
1238
1239 y = (set[0] ? args[0] : 1) - 1;
1240 if (terminal->origin_mode) {
1241 y += terminal->margin_top;
1242 y = y < terminal->margin_top ? terminal->margin_top :
1243 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1244 } else {
1245 y = y < 0 ? 0 :
1246 (y >= terminal->height ? terminal->height - 1 : y);
1247 }
1248
1249 terminal->row = y;
1250 terminal->column = x;
1251 break;
1252 case 'I': /* CHT */
1253 count = set[0] ? args[0] : 1;
1254 if (count == 0) count = 1;
1255 while (count > 0 && terminal->column < terminal->width) {
1256 if (terminal->tab_ruler[terminal->column]) count--;
1257 terminal->column++;
1258 }
1259 terminal->column--;
1260 break;
1261 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001262 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001263 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001264 if (!set[0] || args[0] == 0 || args[0] > 2) {
1265 memset(&row[terminal->column],
1266 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1267 attr_init(&attr_row[terminal->column],
1268 terminal->curr_attr, terminal->width - terminal->column);
1269 for (i = terminal->row + 1; i < terminal->height; i++) {
1270 memset(terminal_get_row(terminal, i),
1271 0, terminal->data_pitch);
1272 attr_init(terminal_get_attr_row(terminal, i),
1273 terminal->curr_attr, terminal->width);
1274 }
1275 } else if (args[0] == 1) {
1276 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1277 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1278 for (i = 0; i < terminal->row; i++) {
1279 memset(terminal_get_row(terminal, i),
1280 0, terminal->data_pitch);
1281 attr_init(terminal_get_attr_row(terminal, i),
1282 terminal->curr_attr, terminal->width);
1283 }
1284 } else if (args[0] == 2) {
1285 for (i = 0; i < terminal->height; i++) {
1286 memset(terminal_get_row(terminal, i),
1287 0, terminal->data_pitch);
1288 attr_init(terminal_get_attr_row(terminal, i),
1289 terminal->curr_attr, terminal->width);
1290 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001291 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001292 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001293 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001294 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001295 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001296 if (!set[0] || args[0] == 0 || args[0] > 2) {
1297 memset(&row[terminal->column], 0,
1298 (terminal->width - terminal->column) * sizeof(union utf8_char));
1299 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1300 terminal->width - terminal->column);
1301 } else if (args[0] == 1) {
1302 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1303 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1304 } else if (args[0] == 2) {
1305 memset(row, 0, terminal->data_pitch);
1306 attr_init(attr_row, terminal->curr_attr, terminal->width);
1307 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001308 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001309 case 'L': /* IL */
1310 count = set[0] ? args[0] : 1;
1311 if (count == 0) count = 1;
1312 if (terminal->row >= terminal->margin_top &&
1313 terminal->row < terminal->margin_bottom)
1314 {
1315 top = terminal->margin_top;
1316 terminal->margin_top = terminal->row;
1317 terminal_scroll(terminal, 0 - count);
1318 terminal->margin_top = top;
1319 } else if (terminal->row == terminal->margin_bottom) {
1320 memset(terminal_get_row(terminal, terminal->row),
1321 0, terminal->data_pitch);
1322 attr_init(terminal_get_attr_row(terminal, terminal->row),
1323 terminal->curr_attr, terminal->width);
1324 }
1325 break;
1326 case 'M': /* DL */
1327 count = set[0] ? args[0] : 1;
1328 if (count == 0) count = 1;
1329 if (terminal->row >= terminal->margin_top &&
1330 terminal->row < terminal->margin_bottom)
1331 {
1332 top = terminal->margin_top;
1333 terminal->margin_top = terminal->row;
1334 terminal_scroll(terminal, count);
1335 terminal->margin_top = top;
1336 } else if (terminal->row == terminal->margin_bottom) {
1337 memset(terminal_get_row(terminal, terminal->row),
1338 0, terminal->data_pitch);
1339 }
1340 break;
1341 case 'P': /* DCH */
1342 count = set[0] ? args[0] : 1;
1343 if (count == 0) count = 1;
1344 terminal_shift_line(terminal, 0 - count);
1345 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001346 case 'S': /* SU */
1347 terminal_scroll(terminal, set[0] ? args[0] : 1);
1348 break;
1349 case 'T': /* SD */
1350 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1351 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001352 case 'X': /* ECH */
1353 count = set[0] ? args[0] : 1;
1354 if (count == 0) count = 1;
1355 if ((terminal->column + count) > terminal->width)
1356 count = terminal->width - terminal->column;
1357 row = terminal_get_row(terminal, terminal->row);
1358 attr_row = terminal_get_attr_row(terminal, terminal->row);
1359 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1360 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1361 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001362 case 'Z': /* CBT */
1363 count = set[0] ? args[0] : 1;
1364 if (count == 0) count = 1;
1365 while (count > 0 && terminal->column >= 0) {
1366 if (terminal->tab_ruler[terminal->column]) count--;
1367 terminal->column--;
1368 }
1369 terminal->column++;
1370 break;
1371 case '`': /* HPA */
1372 y = set[0] ? args[0] : 1;
1373 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1374
1375 terminal->column = y - 1;
1376 break;
1377 case 'b': /* REP */
1378 count = set[0] ? args[0] : 1;
1379 if (count == 0) count = 1;
1380 if (terminal->last_char.byte[0])
1381 for (i = 0; i < count; i++)
1382 handle_char(terminal, terminal->last_char);
1383 terminal->last_char.byte[0] = 0;
1384 break;
1385 case 'c': /* Primary DA */
Callum Lowcay69e96582011-01-07 19:47:00 +00001386 write(terminal->master, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001387 break;
1388 case 'd': /* VPA */
1389 x = set[0] ? args[0] : 1;
1390 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1391
1392 terminal->row = x - 1;
1393 break;
1394 case 'g': /* TBC */
1395 if (!set[0] || args[0] == 0) {
1396 terminal->tab_ruler[terminal->column] = 0;
1397 } else if (args[0] == 3) {
1398 memset(terminal->tab_ruler, 0, terminal->width);
1399 }
1400 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001401 case 'h': /* SM */
1402 for(i = 0; i < 10 && set[i]; i++) {
1403 handle_term_parameter(terminal, args[i], 1);
1404 }
1405 break;
1406 case 'l': /* RM */
1407 for(i = 0; i < 10 && set[i]; i++) {
1408 handle_term_parameter(terminal, args[i], 0);
1409 }
1410 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001411 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001412 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001413 if (i <= 7 && set[i] && set[i + 1] &&
1414 set[i + 2] && args[i + 1] == 5)
1415 {
1416 if (args[i] == 38) {
1417 handle_sgr(terminal, args[i + 2] + 256);
1418 break;
1419 } else if (args[i] == 48) {
1420 handle_sgr(terminal, args[i + 2] + 512);
1421 break;
1422 }
1423 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001424 if(set[i]) {
1425 handle_sgr(terminal, args[i]);
1426 } else if(i == 0) {
1427 handle_sgr(terminal, 0);
1428 break;
1429 } else {
1430 break;
1431 }
1432 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001433 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001434 case 'n': /* DSR */
1435 i = set[0] ? args[0] : 0;
1436 if (i == 0 || i == 5) {
1437 write(terminal->master, "\e[0n", 4);
1438 } else if (i == 6) {
1439 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1440 terminal->origin_mode ?
1441 terminal->row+terminal->margin_top : terminal->row+1,
1442 terminal->column+1);
1443 write(terminal->master, response, strlen(response));
1444 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001445 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001446 case 'r':
1447 if(!set[0]) {
1448 terminal->margin_top = 0;
1449 terminal->margin_bottom = terminal->height-1;
1450 terminal->row = 0;
1451 terminal->column = 0;
1452 } else {
1453 top = (set[0] ? args[0] : 1) - 1;
1454 top = top < 0 ? 0 :
1455 (top >= terminal->height ? terminal->height - 1 : top);
1456 bottom = (set[1] ? args[1] : 1) - 1;
1457 bottom = bottom < 0 ? 0 :
1458 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1459 if(bottom > top) {
1460 terminal->margin_top = top;
1461 terminal->margin_bottom = bottom;
1462 } else {
1463 terminal->margin_top = 0;
1464 terminal->margin_bottom = terminal->height-1;
1465 }
1466 if(terminal->origin_mode)
1467 terminal->row = terminal->margin_top;
1468 else
1469 terminal->row = 0;
1470 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001471 }
1472 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001473 case 's':
1474 terminal->saved_row = terminal->row;
1475 terminal->saved_column = terminal->column;
1476 break;
1477 case 'u':
1478 terminal->row = terminal->saved_row;
1479 terminal->column = terminal->saved_column;
1480 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001481 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001482 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001483 break;
1484 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001485}
1486
1487static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001488handle_non_csi_escape(struct terminal *terminal, char code)
1489{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001490 switch(code) {
1491 case 'M': /* RI */
1492 terminal->row -= 1;
1493 if(terminal->row < terminal->margin_top) {
1494 terminal->row = terminal->margin_top;
1495 terminal_scroll(terminal, -1);
1496 }
1497 break;
1498 case 'E': /* NEL */
1499 terminal->column = 0;
1500 // fallthrough
1501 case 'D': /* IND */
1502 terminal->row += 1;
1503 if(terminal->row > terminal->margin_bottom) {
1504 terminal->row = terminal->margin_bottom;
1505 terminal_scroll(terminal, +1);
1506 }
1507 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001508 case 'c': /* RIS */
1509 terminal_init(terminal);
1510 break;
1511 case 'H': /* HTS */
1512 terminal->tab_ruler[terminal->column] = 1;
1513 break;
1514 case '7': /* DECSC */
1515 terminal->saved_row = terminal->row;
1516 terminal->saved_column = terminal->column;
1517 terminal->saved_attr = terminal->curr_attr;
1518 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001519 terminal->saved_cs = terminal->cs;
1520 terminal->saved_g0 = terminal->g0;
1521 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001522 break;
1523 case '8': /* DECRC */
1524 terminal->row = terminal->saved_row;
1525 terminal->column = terminal->saved_column;
1526 terminal->curr_attr = terminal->saved_attr;
1527 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001528 terminal->cs = terminal->saved_cs;
1529 terminal->g0 = terminal->saved_g0;
1530 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001531 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001532 case '=': /* DECPAM */
1533 terminal->key_mode = KM_APPLICATION;
1534 break;
1535 case '>': /* DECPNM */
1536 terminal->key_mode = KM_NORMAL;
1537 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001538 default:
1539 fprintf(stderr, "Unknown escape code: %c\n", code);
1540 break;
1541 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001542}
1543
1544static void
1545handle_special_escape(struct terminal *terminal, char special, char code)
1546{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001547 int i, numChars;
1548
1549 if (special == '#') {
1550 switch(code) {
1551 case '8':
1552 /* fill with 'E', no cheap way to do this */
1553 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1554 numChars = terminal->width * terminal->height;
1555 for(i = 0; i < numChars; i++) {
1556 terminal->data[i].byte[0] = 'E';
1557 }
1558 break;
1559 default:
1560 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1561 break;
1562 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001563 } else if (special == '(' || special == ')') {
1564 switch(code) {
1565 case '0':
1566 if (special == '(')
1567 terminal->g0 = CS_SPECIAL;
1568 else
1569 terminal->g1 = CS_SPECIAL;
1570 break;
1571 case 'A':
1572 if (special == '(')
1573 terminal->g0 = CS_UK;
1574 else
1575 terminal->g1 = CS_UK;
1576 break;
1577 case 'B':
1578 if (special == '(')
1579 terminal->g0 = CS_US;
1580 else
1581 terminal->g1 = CS_US;
1582 break;
1583 default:
1584 fprintf(stderr, "Unknown character set %c\n", code);
1585 break;
1586 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001587 } else {
1588 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1589 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001590}
1591
1592static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001593handle_sgr(struct terminal *terminal, int code)
1594{
1595 switch(code) {
1596 case 0:
1597 terminal->curr_attr = terminal->color_scheme->default_attr;
1598 break;
1599 case 1:
1600 terminal->curr_attr.a |= ATTRMASK_BOLD;
1601 if (terminal->curr_attr.fg < 8)
1602 terminal->curr_attr.fg += 8;
1603 break;
1604 case 4:
1605 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1606 break;
1607 case 5:
1608 terminal->curr_attr.a |= ATTRMASK_BLINK;
1609 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001610 case 8:
1611 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1612 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001613 case 2:
1614 case 21:
1615 case 22:
1616 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1617 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1618 terminal->curr_attr.fg -= 8;
1619 break;
1620 case 24:
1621 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1622 break;
1623 case 25:
1624 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1625 break;
1626 case 7:
1627 case 26:
1628 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1629 break;
1630 case 27:
1631 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1632 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001633 case 28:
1634 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1635 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001636 case 39:
1637 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1638 break;
1639 case 49:
1640 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1641 break;
1642 default:
1643 if(code >= 30 && code <= 37) {
1644 terminal->curr_attr.fg = code - 30;
1645 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1646 terminal->curr_attr.fg += 8;
1647 } else if(code >= 40 && code <= 47) {
1648 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001649 } else if (code >= 90 && code <= 97) {
1650 terminal->curr_attr.fg = code - 90 + 8;
1651 } else if (code >= 100 && code <= 107) {
1652 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001653 } else if(code >= 256 && code < 512) {
1654 terminal->curr_attr.fg = code - 256;
1655 } else if(code >= 512 && code < 768) {
1656 terminal->curr_attr.bg = code - 512;
1657 } else {
1658 fprintf(stderr, "Unknown SGR code: %d\n", code);
1659 }
1660 break;
1661 }
1662}
1663
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001664/* Returns 1 if c was special, otherwise 0 */
1665static int
1666handle_special_char(struct terminal *terminal, char c)
1667{
1668 union utf8_char *row;
1669 struct attr *attr_row;
1670
1671 row = terminal_get_row(terminal, terminal->row);
1672 attr_row = terminal_get_attr_row(terminal, terminal->row);
1673
1674 switch(c) {
1675 case '\r':
1676 terminal->column = 0;
1677 break;
1678 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001679 if (terminal->mode & MODE_LF_NEWLINE) {
1680 terminal->column = 0;
1681 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001682 /* fallthrough */
1683 case '\v':
1684 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001685 terminal->row++;
1686 if(terminal->row > terminal->margin_bottom) {
1687 terminal->row = terminal->margin_bottom;
1688 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001689 }
1690
1691 break;
1692 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001693 while (terminal->column < terminal->width) {
1694 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001695 if (terminal->mode & MODE_IRM)
1696 terminal_shift_line(terminal, +1);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001697 row[terminal->column].byte[0] = ' ';
1698 row[terminal->column].byte[1] = '\0';
1699 attr_row[terminal->column] = terminal->curr_attr;
1700 terminal->column++;
1701 }
1702 if (terminal->column >= terminal->width) {
1703 terminal->column = terminal->width - 1;
1704 }
1705
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001706 break;
1707 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001708 if (terminal->column >= terminal->width) {
1709 terminal->column = terminal->width - 2;
1710 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001711 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001712 } else if (terminal->mode & MODE_AUTOWRAP) {
1713 terminal->column = terminal->width - 1;
1714 terminal->row -= 1;
1715 if (terminal->row < terminal->margin_top) {
1716 terminal->row = terminal->margin_top;
1717 terminal_scroll(terminal, -1);
1718 }
1719 }
1720
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001721 break;
1722 case '\a':
1723 /* Bell */
1724 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001725 case '\x0E': /* SO */
1726 terminal->cs = terminal->g1;
1727 break;
1728 case '\x0F': /* SI */
1729 terminal->cs = terminal->g0;
1730 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001731 default:
1732 return 0;
1733 }
1734
1735 return 1;
1736}
1737
1738static void
1739handle_char(struct terminal *terminal, union utf8_char utf8)
1740{
1741 union utf8_char *row;
1742 struct attr *attr_row;
1743
1744 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001745
1746 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001747
1748 /* There are a whole lot of non-characters, control codes,
1749 * and formatting codes that should probably be ignored,
1750 * for example: */
1751 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1752 /* BOM, ignore */
1753 return;
1754 }
1755
1756 /* Some of these non-characters should be translated, e.g.: */
1757 if (utf8.byte[0] < 32) {
1758 utf8.byte[0] = utf8.byte[0] + 64;
1759 }
1760
1761 /* handle right margin effects */
1762 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001763 if (terminal->mode & MODE_AUTOWRAP) {
1764 terminal->column = 0;
1765 terminal->row += 1;
1766 if (terminal->row > terminal->margin_bottom) {
1767 terminal->row = terminal->margin_bottom;
1768 terminal_scroll(terminal, +1);
1769 }
1770 } else {
1771 terminal->column--;
1772 }
1773 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001774
1775 row = terminal_get_row(terminal, terminal->row);
1776 attr_row = terminal_get_attr_row(terminal, terminal->row);
1777
Callum Lowcay69e96582011-01-07 19:47:00 +00001778 if (terminal->mode & MODE_IRM)
1779 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001780 row[terminal->column] = utf8;
1781 attr_row[terminal->column++] = terminal->curr_attr;
1782
1783 if (utf8.ch != terminal->last_char.ch)
1784 terminal->last_char = utf8;
1785}
1786
Callum Lowcay30eeae52011-01-07 19:46:55 +00001787static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001788escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1789{
1790 int len, i;
1791
1792 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1793 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1794 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1795 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1796 else len = 1; /* Invalid, cannot happen */
1797
1798 if (terminal->escape_length + len <= MAX_ESCAPE) {
1799 for (i = 0; i < len; i++)
1800 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1801 terminal->escape_length += len;
1802 } else if (terminal->escape_length < MAX_ESCAPE) {
1803 terminal->escape[terminal->escape_length++] = 0;
1804 }
1805}
1806
1807static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001808terminal_data(struct terminal *terminal, const char *data, size_t length)
1809{
1810 int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001811 union utf8_char utf8;
1812 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001813
1814 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001815 parser_state =
1816 utf8_next_char(&terminal->state_machine, data[i]);
1817 switch(parser_state) {
1818 case utf8state_accept:
1819 utf8.ch = terminal->state_machine.s.ch;
1820 break;
1821 case utf8state_reject:
1822 /* the unicode replacement character */
1823 utf8.byte[0] = 0xEF;
1824 utf8.byte[1] = 0xBF;
1825 utf8.byte[2] = 0xBD;
1826 utf8.byte[3] = 0x00;
1827 break;
1828 default:
1829 continue;
1830 }
1831
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001832 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001833 switch (terminal->state) {
1834 case escape_state_escape:
1835 escape_append_utf8(terminal, utf8);
1836 switch (utf8.byte[0]) {
1837 case 'P': /* DCS */
1838 terminal->state = escape_state_dcs;
1839 break;
1840 case '[': /* CSI */
1841 terminal->state = escape_state_csi;
1842 break;
1843 case ']': /* OSC */
1844 terminal->state = escape_state_osc;
1845 break;
1846 case '#':
1847 case '(':
1848 case ')': /* special */
1849 terminal->state = escape_state_special;
1850 break;
1851 case '^': /* PM (not implemented) */
1852 case '_': /* APC (not implemented) */
1853 terminal->state = escape_state_ignore;
1854 break;
1855 default:
1856 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001857 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13001858 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001859 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001860 continue;
1861 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001862 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1863 /* do nothing */
1864 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001865 terminal->escape_flags |= ESC_FLAG_WHAT;
1866 } else if (utf8.byte[0] == '>') {
1867 terminal->escape_flags |= ESC_FLAG_GT;
1868 } else if (utf8.byte[0] == '!') {
1869 terminal->escape_flags |= ESC_FLAG_BANG;
1870 } else if (utf8.byte[0] == '$') {
1871 terminal->escape_flags |= ESC_FLAG_CASH;
1872 } else if (utf8.byte[0] == '\'') {
1873 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1874 } else if (utf8.byte[0] == '"') {
1875 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1876 } else if (utf8.byte[0] == ' ') {
1877 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001878 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001879 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001880 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13001881 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001882 }
1883
1884 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1885 utf8.byte[0] == '`')
1886 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001887 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001888 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001889 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001890 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001891 continue;
1892 case escape_state_inner_escape:
1893 if (utf8.byte[0] == '\\') {
1894 terminal->state = escape_state_normal;
1895 if (terminal->outer_state == escape_state_dcs) {
1896 handle_dcs(terminal);
1897 } else if (terminal->outer_state == escape_state_osc) {
1898 handle_osc(terminal);
1899 }
1900 } else if (utf8.byte[0] == '\e') {
1901 terminal->state = terminal->outer_state;
1902 escape_append_utf8(terminal, utf8);
1903 if (terminal->escape_length >= MAX_ESCAPE)
1904 terminal->state = escape_state_normal;
1905 } else {
1906 terminal->state = terminal->outer_state;
1907 if (terminal->escape_length < MAX_ESCAPE)
1908 terminal->escape[terminal->escape_length++] = '\e';
1909 escape_append_utf8(terminal, utf8);
1910 if (terminal->escape_length >= MAX_ESCAPE)
1911 terminal->state = escape_state_normal;
1912 }
1913 continue;
1914 case escape_state_dcs:
1915 case escape_state_osc:
1916 case escape_state_ignore:
1917 if (utf8.byte[0] == '\e') {
1918 terminal->outer_state = terminal->state;
1919 terminal->state = escape_state_inner_escape;
1920 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
1921 terminal->state = escape_state_normal;
1922 handle_osc(terminal);
1923 } else {
1924 escape_append_utf8(terminal, utf8);
1925 if (terminal->escape_length >= MAX_ESCAPE)
1926 terminal->state = escape_state_normal;
1927 }
1928 continue;
1929 case escape_state_special:
1930 escape_append_utf8(terminal, utf8);
1931 terminal->state = escape_state_normal;
1932 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
1933 handle_special_escape(terminal, terminal->escape[1],
1934 utf8.byte[0]);
1935 }
1936 continue;
1937 default:
1938 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001939 }
1940
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001941 /* this is valid, because ASCII characters are never used to
1942 * introduce a multibyte sequence in UTF-8 */
1943 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001944 terminal->state = escape_state_escape;
1945 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001946 terminal->escape[0] = '\e';
1947 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13001948 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001949 } else {
1950 handle_char(terminal, utf8);
1951 } /* if */
1952 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05001953
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001954 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001955}
1956
1957static void
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001958selection_listener_send(void *data, struct wl_selection *selection,
1959 const char *mime_type, int fd)
1960{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05001961 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001962
1963 fprintf(stderr, "selection send, fd is %d\n", fd);
Kristian Høgsberg31cce052011-01-21 15:18:55 -05001964 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001965 close(fd);
1966}
1967
1968static void
1969selection_listener_cancelled(void *data, struct wl_selection *selection)
1970{
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001971 fprintf(stderr, "selection cancelled\n");
1972 wl_selection_destroy(selection);
1973}
1974
1975static const struct wl_selection_listener selection_listener = {
1976 selection_listener_send,
1977 selection_listener_cancelled
1978};
1979
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001980static int
1981handle_bound_key(struct terminal *terminal,
1982 struct input *input, uint32_t sym, uint32_t time)
1983{
1984 struct wl_shell *shell;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001985
1986 switch (sym) {
1987 case XK_C:
1988 shell = display_get_shell(terminal->display);
1989 terminal->selection = wl_shell_create_selection(shell);
1990 wl_selection_add_listener(terminal->selection,
1991 &selection_listener, terminal);
1992 wl_selection_offer(terminal->selection, "text/plain");
1993 wl_selection_activate(terminal->selection,
1994 input_get_input_device(input), time);
1995
1996 return 1;
1997 case XK_V:
Kristian Høgsberg6bccebe2011-01-21 16:23:09 -05001998 /* Just pass the master fd of the pty to receive the
1999 * selection. */
2000 if (input_offers_mime_type(input, "text/plain"))
2001 input_receive_mime_type(input, "text/plain",
2002 terminal->master);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002003 return 1;
2004 case XK_X:
2005 /* cut selection; terminal doesn't do cut */
2006 return 0;
2007 default:
2008 return 0;
2009 }
2010}
2011
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002012static void
2013key_handler(struct window *window, struct input *input, uint32_t time,
2014 uint32_t key, uint32_t sym, uint32_t state, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002015{
2016 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002017 char ch[MAX_RESPONSE];
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002018 uint32_t modifiers;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002019 int len = 0;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002020
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002021 modifiers = input_get_modifiers(input);
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002022 if ((modifiers & XKB_COMMON_CONTROL_MASK) &&
2023 (modifiers & XKB_COMMON_SHIFT_MASK) &&
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002024 state && handle_bound_key(terminal, input, sym, 0))
2025 return;
2026
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002027 switch (sym) {
2028 case XK_F11:
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002029 if (!state)
2030 break;
2031 terminal->fullscreen ^= 1;
2032 window_set_fullscreen(window, terminal->fullscreen);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002033 window_schedule_redraw(terminal->window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002034 break;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002035
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002036 case XK_BackSpace:
2037 case XK_Tab:
2038 case XK_Linefeed:
2039 case XK_Clear:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002040 case XK_Pause:
2041 case XK_Scroll_Lock:
2042 case XK_Sys_Req:
2043 case XK_Escape:
2044 ch[len++] = sym & 0x7f;
2045 break;
2046
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002047 case XK_Return:
2048 if (terminal->mode & MODE_LF_NEWLINE) {
2049 ch[len++] = 0x0D;
2050 ch[len++] = 0x0A;
2051 } else {
2052 ch[len++] = 0x0D;
2053 }
2054 break;
2055
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002056 case XK_Shift_L:
2057 case XK_Shift_R:
2058 case XK_Control_L:
2059 case XK_Control_R:
2060 case XK_Alt_L:
2061 case XK_Alt_R:
2062 break;
2063
Callum Lowcay7e08e902011-01-07 19:47:02 +00002064 case XK_Insert:
2065 len = function_key_response('[', 2, modifiers, '~', ch);
2066 break;
2067 case XK_Delete:
2068 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2069 ch[len++] = '\x04';
2070 } else {
2071 len = function_key_response('[', 3, modifiers, '~', ch);
2072 }
2073 break;
2074 case XK_Page_Up:
2075 len = function_key_response('[', 5, modifiers, '~', ch);
2076 break;
2077 case XK_Page_Down:
2078 len = function_key_response('[', 6, modifiers, '~', ch);
2079 break;
2080 case XK_F1:
2081 len = function_key_response('O', 1, modifiers, 'P', ch);
2082 break;
2083 case XK_F2:
2084 len = function_key_response('O', 1, modifiers, 'Q', ch);
2085 break;
2086 case XK_F3:
2087 len = function_key_response('O', 1, modifiers, 'R', ch);
2088 break;
2089 case XK_F4:
2090 len = function_key_response('O', 1, modifiers, 'S', ch);
2091 break;
2092 case XK_F5:
2093 len = function_key_response('[', 15, modifiers, '~', ch);
2094 break;
2095 case XK_F6:
2096 len = function_key_response('[', 17, modifiers, '~', ch);
2097 break;
2098 case XK_F7:
2099 len = function_key_response('[', 18, modifiers, '~', ch);
2100 break;
2101 case XK_F8:
2102 len = function_key_response('[', 19, modifiers, '~', ch);
2103 break;
2104 case XK_F9:
2105 len = function_key_response('[', 20, modifiers, '~', ch);
2106 break;
2107 case XK_F10:
2108 len = function_key_response('[', 21, modifiers, '~', ch);
2109 break;
2110 case XK_F12:
2111 len = function_key_response('[', 24, modifiers, '~', ch);
2112 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002113 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002114 /* Handle special keys with alternate mappings */
2115 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2116 if (len != 0) break;
2117
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002118 if (modifiers & XKB_COMMON_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002119 if (sym >= '3' && sym <= '7')
2120 sym = (sym & 0x1f) + 8;
2121
2122 if (!((sym >= '!' && sym <= '/') ||
2123 (sym >= '8' && sym <= '?') ||
2124 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2125 else if (sym == '2') sym = 0x00;
2126 else if (sym == '/') sym = 0x1F;
2127 else if (sym == '8' || sym == '?') sym = 0x7F;
2128 } else if ((terminal->mode & MODE_ALT_SENDS_ESC) &&
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002129 (modifiers & XKB_COMMON_MOD1_MASK))
Callum Lowcay7e08e902011-01-07 19:47:02 +00002130 {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002131 ch[len++] = 0x1b;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002132 } else if (modifiers & XKB_COMMON_MOD1_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002133 sym = sym | 0x80;
2134 }
2135
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002136 if (sym < 256)
2137 ch[len++] = sym;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002138 break;
2139 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002140
2141 if (state && len > 0)
2142 write(terminal->master, ch, len);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002143}
2144
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002145static void
2146keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002147 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002148{
2149 struct terminal *terminal = data;
2150
2151 terminal->focused = (device != NULL);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002152 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002153}
2154
Kristian Høgsberg59826582011-01-20 11:56:57 -05002155static void
2156button_handler(struct window *window,
2157 struct input *input, uint32_t time,
2158 int button, int state, void *data)
2159{
2160 struct terminal *terminal = data;
2161
2162 switch (button) {
2163 case 272:
2164 if (state) {
2165 terminal->dragging = 1;
2166 terminal->selection_active = 0;
2167 input_get_position(input,
2168 &terminal->selection_start_x,
2169 &terminal->selection_start_y);
2170 terminal->selection_end_x = terminal->selection_start_x;
2171 terminal->selection_end_y = terminal->selection_start_y;
2172 window_schedule_redraw(window);
2173 } else {
2174 terminal->dragging = 0;
2175 }
2176 break;
2177 }
2178}
2179
2180static int
2181motion_handler(struct window *window,
2182 struct input *input, uint32_t time,
2183 int32_t x, int32_t y,
2184 int32_t sx, int32_t sy, void *data)
2185{
2186 struct terminal *terminal = data;
2187
2188 if (terminal->dragging) {
2189 terminal->selection_active = 1;
2190 input_get_position(input,
2191 &terminal->selection_end_x,
2192 &terminal->selection_end_y);
2193 window_schedule_redraw(window);
2194 }
2195
2196 return POINTER_IBEAM;
2197}
2198
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002199static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002200terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002201{
2202 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002203 cairo_surface_t *surface;
2204 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002205
2206 terminal = malloc(sizeof *terminal);
2207 if (terminal == NULL)
2208 return terminal;
2209
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002210 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002211 terminal->fullscreen = fullscreen;
Callum Lowcay30eeae52011-01-07 19:46:55 +00002212 terminal->color_scheme = &DEFAULT_COLORS;
2213 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002214 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002215 terminal->margin_bottom = -1;
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002216 terminal->window = window_create(display, 500, 400);
2217 window_set_title(terminal->window, "Wayland Terminal");
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002218
2219 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002220 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002221
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002222 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002223 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002224
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002225 window_set_fullscreen(terminal->window, terminal->fullscreen);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002226 window_set_user_data(terminal->window, terminal);
2227 window_set_redraw_handler(terminal->window, redraw_handler);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05002228 window_set_resize_handler(terminal->window, resize_handler);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002229
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002230 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002231 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002232 keyboard_focus_handler);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002233 window_set_button_handler(terminal->window, button_handler);
2234 window_set_motion_handler(terminal->window, motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002235
Kristian Høgsberg09531622010-06-14 23:22:15 -04002236 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2237 cr = cairo_create(surface);
Kristian Høgsberg09531622010-06-14 23:22:15 -04002238 cairo_set_font_size(cr, 14);
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002239 cairo_select_font_face (cr, "mono",
2240 CAIRO_FONT_SLANT_NORMAL,
2241 CAIRO_FONT_WEIGHT_BOLD);
2242 terminal->font_bold = cairo_get_scaled_font (cr);
2243 cairo_scaled_font_reference(terminal->font_bold);
2244
2245 cairo_select_font_face (cr, "mono",
2246 CAIRO_FONT_SLANT_NORMAL,
2247 CAIRO_FONT_WEIGHT_NORMAL);
2248 terminal->font_normal = cairo_get_scaled_font (cr);
2249 cairo_scaled_font_reference(terminal->font_normal);
2250
Kristian Høgsberg09531622010-06-14 23:22:15 -04002251 cairo_font_extents(cr, &terminal->extents);
2252 cairo_destroy(cr);
2253 cairo_surface_destroy(surface);
2254
Callum Lowcaya0ee21c2011-01-07 19:46:56 +00002255 terminal_resize(terminal, 80, 24);
Kristian Høgsberg22106762008-12-08 13:50:07 -05002256 terminal_draw(terminal);
2257
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002258 return terminal;
2259}
2260
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002261static gboolean
2262io_handler(GIOChannel *source,
2263 GIOCondition condition,
2264 gpointer data)
2265{
2266 struct terminal *terminal = data;
2267 gchar buffer[256];
2268 gsize bytes_read;
2269 GError *error = NULL;
2270
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002271 if(condition == G_IO_HUP)
2272 exit(0);
2273
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002274 g_io_channel_read_chars(source, buffer, sizeof buffer,
2275 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002276
2277 terminal_data(terminal, buffer, bytes_read);
2278
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002279 return TRUE;
2280}
2281
2282static int
2283terminal_run(struct terminal *terminal, const char *path)
2284{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002285 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002286 pid_t pid;
2287
2288 pid = forkpty(&master, NULL, NULL, NULL);
2289 if (pid == 0) {
Callum Lowcay86653ed2011-01-07 19:47:03 +00002290 setenv("TERM", "xterm-256color", 1);
2291 setenv("COLORTERM", "xterm-256color", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002292 if (execl(path, path, NULL)) {
2293 printf("exec failed: %m\n");
2294 exit(EXIT_FAILURE);
2295 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002296 } else if (pid < 0) {
2297 fprintf(stderr, "failed to fork and create pty (%m).\n");
2298 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002299 }
2300
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002301 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002302 terminal->channel = g_io_channel_unix_new(master);
2303 fcntl(master, F_SETFL, O_NONBLOCK);
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002304 g_io_add_watch(terminal->channel, G_IO_IN, io_handler, terminal);
2305 g_io_add_watch(terminal->channel, G_IO_HUP, io_handler, terminal);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002306
2307 return 0;
2308}
2309
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002310static const GOptionEntry option_entries[] = {
2311 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
2312 &option_fullscreen, "Run in fullscreen mode" },
2313 { NULL }
2314};
2315
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002316int main(int argc, char *argv[])
2317{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002318 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002319 struct terminal *terminal;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002320
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002321 d = display_create(&argc, &argv, option_entries);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002322 if (d == NULL) {
2323 fprintf(stderr, "failed to create display: %m\n");
2324 return -1;
2325 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002326
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002327 terminal = terminal_create(d, option_fullscreen);
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002328 if (terminal_run(terminal, "/bin/bash"))
2329 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002330
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002331 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002332
2333 return 0;
2334}