blob: f959acaa8fca65182179ab9ab53f55c0d0323fc5 [file] [log] [blame]
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <fcntl.h>
28#include <unistd.h>
29#include <math.h>
30#include <time.h>
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050031#include <pty.h>
Kristian Høgsbergf04e8382008-12-08 00:07:49 -050032#include <ctype.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050033#include <cairo.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040034#include <sys/epoll.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050035
Pekka Paalanen50719bc2011-11-22 14:18:50 +020036#include <wayland-client.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050037
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -040038#include "../shared/config-parser.h"
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050039#include "window.h"
40
Kristian Høgsberg0395f302008-12-22 12:14:50 -050041static int option_fullscreen;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -040042static char *option_font = "mono";
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050043
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050044#define MOD_SHIFT 0x01
45#define MOD_ALT 0x02
46#define MOD_CTRL 0x04
47
Callum Lowcay30eeae52011-01-07 19:46:55 +000048#define ATTRMASK_BOLD 0x01
49#define ATTRMASK_UNDERLINE 0x02
50#define ATTRMASK_BLINK 0x04
51#define ATTRMASK_INVERSE 0x08
Callum Lowcay81179db2011-01-10 12:14:01 +130052#define ATTRMASK_CONCEALED 0x10
Callum Lowcay30eeae52011-01-07 19:46:55 +000053
Callum Lowcayb8609ad2011-01-07 19:46:57 +000054/* Buffer sizes */
Callum Lowcayef57a9b2011-01-14 20:46:23 +130055#define MAX_RESPONSE 256
56#define MAX_ESCAPE 255
Callum Lowcayb8609ad2011-01-07 19:46:57 +000057
Callum Lowcay8e57dd52011-01-07 19:46:59 +000058/* Terminal modes */
59#define MODE_SHOW_CURSOR 0x00000001
60#define MODE_INVERSE 0x00000002
61#define MODE_AUTOWRAP 0x00000004
62#define MODE_AUTOREPEAT 0x00000008
63#define MODE_LF_NEWLINE 0x00000010
Callum Lowcay69e96582011-01-07 19:47:00 +000064#define MODE_IRM 0x00000020
Callum Lowcay7e08e902011-01-07 19:47:02 +000065#define MODE_DELETE_SENDS_DEL 0x00000040
66#define MODE_ALT_SENDS_ESC 0x00000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +000067
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000068union utf8_char {
69 unsigned char byte[4];
70 uint32_t ch;
71};
72
73enum utf8_state {
74 utf8state_start,
75 utf8state_accept,
76 utf8state_reject,
77 utf8state_expect3,
78 utf8state_expect2,
79 utf8state_expect1
80};
81
82struct utf8_state_machine {
83 enum utf8_state state;
84 int len;
85 union utf8_char s;
86};
87
88static void
89init_state_machine(struct utf8_state_machine *machine)
90{
91 machine->state = utf8state_start;
92 machine->len = 0;
93 machine->s.ch = 0;
94}
95
96static enum utf8_state
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -040097utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000098{
99 switch(machine->state) {
100 case utf8state_start:
101 case utf8state_accept:
102 case utf8state_reject:
103 machine->s.ch = 0;
104 machine->len = 0;
105 if(c == 0xC0 || c == 0xC1) {
106 /* overlong encoding, reject */
107 machine->state = utf8state_reject;
108 } else if((c & 0x80) == 0) {
109 /* single byte, accept */
110 machine->s.byte[machine->len++] = c;
111 machine->state = utf8state_accept;
112 } else if((c & 0xC0) == 0x80) {
113 /* parser out of sync, ignore byte */
114 machine->state = utf8state_start;
115 } else if((c & 0xE0) == 0xC0) {
116 /* start of two byte sequence */
117 machine->s.byte[machine->len++] = c;
118 machine->state = utf8state_expect1;
119 } else if((c & 0xF0) == 0xE0) {
120 /* start of three byte sequence */
121 machine->s.byte[machine->len++] = c;
122 machine->state = utf8state_expect2;
123 } else if((c & 0xF8) == 0xF0) {
124 /* start of four byte sequence */
125 machine->s.byte[machine->len++] = c;
126 machine->state = utf8state_expect3;
127 } else {
128 /* overlong encoding, reject */
129 machine->state = utf8state_reject;
130 }
131 break;
132 case utf8state_expect3:
133 machine->s.byte[machine->len++] = c;
134 if((c & 0xC0) == 0x80) {
135 /* all good, continue */
136 machine->state = utf8state_expect2;
137 } else {
138 /* missing extra byte, reject */
139 machine->state = utf8state_reject;
140 }
141 break;
142 case utf8state_expect2:
143 machine->s.byte[machine->len++] = c;
144 if((c & 0xC0) == 0x80) {
145 /* all good, continue */
146 machine->state = utf8state_expect1;
147 } else {
148 /* missing extra byte, reject */
149 machine->state = utf8state_reject;
150 }
151 break;
152 case utf8state_expect1:
153 machine->s.byte[machine->len++] = c;
154 if((c & 0xC0) == 0x80) {
155 /* all good, accept */
156 machine->state = utf8state_accept;
157 } else {
158 /* missing extra byte, reject */
159 machine->state = utf8state_reject;
160 }
161 break;
162 default:
163 machine->state = utf8state_reject;
164 break;
165 }
166
167 return machine->state;
168}
169
Callum Lowcay256e72f2011-01-07 19:47:01 +0000170struct char_sub {
171 union utf8_char match;
172 union utf8_char replace;
173};
174/* Set last char_sub match to NULL char */
175typedef struct char_sub *character_set;
176
177struct char_sub CS_US[] = {
178 {{{0, }}, {{0, }}}
179};
180static struct char_sub CS_UK[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200181 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000182 {{{0, }}, {{0, }}}
183};
184static struct char_sub CS_SPECIAL[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200185 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond: ♦ */
186 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell: ▒ */
187 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT: ␉ */
188 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF: ␌ */
189 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR: ␍ */
190 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF: ␊ */
191 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree: ° */
192 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus: ± */
193 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL: ␤ */
194 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT: ␋ */
195 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB: ┘ */
196 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT: ┐ */
197 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT: ┌ */
198 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_LB: └ */
199 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS: ┼ */
200 {{{'o', 0, }}, {{0xE2, 0x8E, 0xBA, 0}}}, /* Horiz. Scan Line 1: ⎺ */
201 {{{'p', 0, }}, {{0xE2, 0x8E, 0xBB, 0}}}, /* Horiz. Scan Line 3: ⎻ */
202 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* Horiz. Scan Line 5: ─ */
203 {{{'r', 0, }}, {{0xE2, 0x8E, 0xBC, 0}}}, /* Horiz. Scan Line 7: ⎼ */
204 {{{'s', 0, }}, {{0xE2, 0x8E, 0xBD, 0}}}, /* Horiz. Scan Line 9: ⎽ */
205 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR: ├ */
206 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL: ┤ */
207 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU: ┴ */
208 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD: ┬ */
209 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V: │ */
210 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE: ≤ */
211 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE: ≥ */
212 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI: π */
213 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ: ≠ */
214 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
215 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT: ⋅ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000216 {{{0, }}, {{0, }}}
217};
218
219static void
220apply_char_set(character_set cs, union utf8_char *utf8)
221{
222 int i = 0;
223
224 while (cs[i].match.byte[0]) {
225 if ((*utf8).ch == cs[i].match.ch) {
226 *utf8 = cs[i].replace;
227 break;
228 }
229 i++;
230 }
231}
232
Callum Lowcay7e08e902011-01-07 19:47:02 +0000233struct key_map {
234 int sym;
235 int num;
236 char escape;
237 char code;
238};
239/* Set last key_sub sym to NULL */
240typedef struct key_map *keyboard_mode;
241
242static struct key_map KM_NORMAL[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400243 { XKB_KEY_Left, 1, '[', 'D' },
244 { XKB_KEY_Right, 1, '[', 'C' },
245 { XKB_KEY_Up, 1, '[', 'A' },
246 { XKB_KEY_Down, 1, '[', 'B' },
247 { XKB_KEY_Home, 1, '[', 'H' },
248 { XKB_KEY_End, 1, '[', 'F' },
249 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000250};
251static struct key_map KM_APPLICATION[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400252 { XKB_KEY_Left, 1, 'O', 'D' },
253 { XKB_KEY_Right, 1, 'O', 'C' },
254 { XKB_KEY_Up, 1, 'O', 'A' },
255 { XKB_KEY_Down, 1, 'O', 'B' },
256 { XKB_KEY_Home, 1, 'O', 'H' },
257 { XKB_KEY_End, 1, 'O', 'F' },
258 { XKB_KEY_KP_Enter, 1, 'O', 'M' },
259 { XKB_KEY_KP_Multiply, 1, 'O', 'j' },
260 { XKB_KEY_KP_Add, 1, 'O', 'k' },
261 { XKB_KEY_KP_Separator, 1, 'O', 'l' },
262 { XKB_KEY_KP_Subtract, 1, 'O', 'm' },
263 { XKB_KEY_KP_Divide, 1, 'O', 'o' },
264 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000265};
266
267static int
268function_key_response(char escape, int num, uint32_t modifiers,
269 char code, char *response)
270{
271 int mod_num = 0;
272 int len;
273
Kristian Høgsberg70163132012-05-08 15:55:39 -0400274 if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
275 if (modifiers & MOD_ALT_MASK) mod_num |= 2;
276 if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000277
278 if (mod_num != 0)
279 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
280 num, mod_num + 1, code);
281 else if (code != '~')
282 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
283 escape, code);
284 else
285 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
286 escape, num, code);
287
288 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
289 else return len;
290}
291
292/* returns the number of bytes written into response,
293 * which must have room for MAX_RESPONSE bytes */
294static int
295apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
296{
297 struct key_map map;
298 int len = 0;
299 int i = 0;
300
301 while (mode[i].sym) {
302 map = mode[i++];
303 if (sym == map.sym) {
304 len = function_key_response(map.escape, map.num,
305 modifiers, map.code,
306 response);
307 break;
308 }
309 }
310
311 return len;
312}
313
Callum Lowcay30eeae52011-01-07 19:46:55 +0000314struct terminal_color { double r, g, b, a; };
315struct attr {
316 unsigned char fg, bg;
317 char a; /* attributes format:
318 * 76543210
Callum Lowcay81179db2011-01-10 12:14:01 +1300319 * cilub */
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500320 char s; /* in selection */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000321};
322struct color_scheme {
323 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500324 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000325 struct attr default_attr;
326};
327
328static void
329attr_init(struct attr *data_attr, struct attr attr, int n)
330{
331 int i;
332 for (i = 0; i < n; i++) {
333 data_attr[i] = attr;
334 }
335}
336
Callum Lowcay67a201d2011-01-12 19:23:41 +1300337enum escape_state {
338 escape_state_normal = 0,
339 escape_state_escape,
340 escape_state_dcs,
341 escape_state_csi,
342 escape_state_osc,
343 escape_state_inner_escape,
344 escape_state_ignore,
345 escape_state_special
346};
347
348#define ESC_FLAG_WHAT 0x01
349#define ESC_FLAG_GT 0x02
350#define ESC_FLAG_BANG 0x04
351#define ESC_FLAG_CASH 0x08
352#define ESC_FLAG_SQUOTE 0x10
353#define ESC_FLAG_DQUOTE 0x20
354#define ESC_FLAG_SPACE 0x40
355
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500356struct terminal {
357 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500358 struct widget *widget;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500359 struct display *display;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000360 union utf8_char *data;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400361 struct task io_task;
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;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600377 int send_cursor_position;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500378 int fd, master;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500379 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300380 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500381 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300382 enum escape_state state;
383 enum escape_state outer_state;
384 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000385 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500386 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500387 int fullscreen;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500388 int focused;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400389 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000390 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400391 cairo_font_extents_t extents;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500392 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500393
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400394 struct wl_data_source *selection;
395 int32_t dragging;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500396 int selection_start_x, selection_start_y;
397 int selection_end_x, selection_end_y;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500398};
399
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000400/* Create default tab stops, every 8 characters */
401static void
402terminal_init_tabs(struct terminal *terminal)
403{
404 int i = 0;
405
406 while (i < terminal->width) {
407 if (i % 8 == 0)
408 terminal->tab_ruler[i] = 1;
409 else
410 terminal->tab_ruler[i] = 0;
411 i++;
412 }
413}
414
Callum Lowcay30eeae52011-01-07 19:46:55 +0000415static void
416terminal_init(struct terminal *terminal)
417{
418 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000419 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000420 terminal->mode = MODE_SHOW_CURSOR |
421 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000422 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000423 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000424
425 terminal->row = 0;
426 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000427
Callum Lowcay256e72f2011-01-07 19:47:01 +0000428 terminal->g0 = CS_US;
429 terminal->g1 = CS_US;
430 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000431 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000432
433 terminal->saved_g0 = terminal->g0;
434 terminal->saved_g1 = terminal->g1;
435 terminal->saved_cs = terminal->cs;
436
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000437 terminal->saved_attr = terminal->curr_attr;
438 terminal->saved_origin_mode = terminal->origin_mode;
439 terminal->saved_row = terminal->row;
440 terminal->saved_column = terminal->column;
441
442 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000443}
444
445static void
446init_color_table(struct terminal *terminal)
447{
448 int c, r;
449 struct terminal_color *color_table = terminal->color_table;
450
451 for (c = 0; c < 256; c ++) {
452 if (c < 16) {
453 color_table[c] = terminal->color_scheme->palette[c];
454 } else if (c < 232) {
455 r = c - 16;
456 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
457 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
458 color_table[c].r = ((double)(r % 6) / 6.0);
459 color_table[c].a = 1.0;
460 } else {
461 r = (c - 232) * 10 + 8;
462 color_table[c].r = ((double) r) / 256.0;
463 color_table[c].g = color_table[c].r;
464 color_table[c].b = color_table[c].r;
465 color_table[c].a = 1.0;
466 }
467 }
468}
469
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000470static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500471terminal_get_row(struct terminal *terminal, int row)
472{
473 int index;
474
475 index = (row + terminal->start) % terminal->height;
476
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000477 return &terminal->data[index * terminal->width];
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500478}
479
Callum Lowcay30eeae52011-01-07 19:46:55 +0000480static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500481terminal_get_attr_row(struct terminal *terminal, int row)
482{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000483 int index;
484
485 index = (row + terminal->start) % terminal->height;
486
487 return &terminal->data_attr[index * terminal->width];
488}
489
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500490union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300491 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500492 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500493};
494
Kristian Høgsberg59826582011-01-20 11:56:57 -0500495static int
496terminal_compare_position(struct terminal *terminal,
497 int x, int y, int32_t ref_row, int32_t ref_col)
498{
499 struct rectangle allocation;
500 int top_margin, side_margin, col, row, ref_x;
501
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500502 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsberg59826582011-01-20 11:56:57 -0500503 side_margin = allocation.x + (allocation.width - terminal->width * terminal->extents.max_x_advance) / 2;
504 top_margin = allocation.y + (allocation.height - terminal->height * terminal->extents.height) / 2;
505
506 col = (x - side_margin) / terminal->extents.max_x_advance;
507 row = (y - top_margin) / terminal->extents.height;
508
509 ref_x = side_margin + ref_col * terminal->extents.max_x_advance +
510 terminal->extents.max_x_advance / 2;
511
512 if (row < ref_row)
513 return -1;
514 if (row == ref_row) {
515 if (col < ref_col)
516 return -1;
517 if (col == ref_col && x < ref_x)
518 return -1;
519 }
520
521 return 1;
522}
523
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500524static void
525terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500526 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500527{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500528 struct attr attr;
529 int foreground, background, tmp;
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500530 int start_cmp, end_cmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500531
532 start_cmp =
533 terminal_compare_position(terminal,
534 terminal->selection_start_x,
535 terminal->selection_start_y,
536 row, col);
537 end_cmp =
538 terminal_compare_position(terminal,
539 terminal->selection_end_x,
540 terminal->selection_end_y,
541 row, col);
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500542 decoded->attr.s = 0;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500543 if (start_cmp < 0 && end_cmp > 0)
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500544 decoded->attr.s = 1;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500545 else if (end_cmp < 0 && start_cmp > 0)
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500546 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500547
548 /* get the attributes for this character cell */
549 attr = terminal_get_attr_row(terminal, row)[col];
550 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500551 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500552 ((terminal->mode & MODE_SHOW_CURSOR) &&
553 terminal->focused && terminal->row == row &&
554 terminal->column == col)) {
555 foreground = attr.bg;
556 background = attr.fg;
557 if (attr.a & ATTRMASK_BOLD) {
558 if (foreground <= 16) foreground |= 0x08;
559 if (background <= 16) background &= 0x07;
560 }
561 } else {
562 foreground = attr.fg;
563 background = attr.bg;
564 }
565
566 if (terminal->mode & MODE_INVERSE) {
567 tmp = foreground;
568 foreground = background;
569 background = tmp;
570 if (attr.a & ATTRMASK_BOLD) {
571 if (foreground <= 16) foreground |= 0x08;
572 if (background <= 16) background &= 0x07;
573 }
574 }
575
Callum Lowcay9d708b02011-01-12 20:06:17 +1300576 decoded->attr.fg = foreground;
577 decoded->attr.bg = background;
578 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000579}
580
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500581
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500582static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000583terminal_scroll_buffer(struct terminal *terminal, int d)
584{
585 int i;
586
587 d = d % (terminal->height + 1);
588 terminal->start = (terminal->start + d) % terminal->height;
589 if (terminal->start < 0) terminal->start = terminal->height + terminal->start;
590 if(d < 0) {
591 d = 0 - d;
592 for(i = 0; i < d; i++) {
593 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
594 attr_init(terminal_get_attr_row(terminal, i),
595 terminal->curr_attr, terminal->width);
596 }
597 } else {
598 for(i = terminal->height - d; i < terminal->height; i++) {
599 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
600 attr_init(terminal_get_attr_row(terminal, i),
601 terminal->curr_attr, terminal->width);
602 }
603 }
604}
605
606static void
607terminal_scroll_window(struct terminal *terminal, int d)
608{
609 int i;
610 int window_height;
611 int from_row, to_row;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000612
613 // scrolling range is inclusive
614 window_height = terminal->margin_bottom - terminal->margin_top + 1;
615 d = d % (window_height + 1);
616 if(d < 0) {
617 d = 0 - d;
618 to_row = terminal->margin_bottom;
619 from_row = terminal->margin_bottom - d;
620
621 for (i = 0; i < (window_height - d); i++) {
622 memcpy(terminal_get_row(terminal, to_row - i),
623 terminal_get_row(terminal, from_row - i),
624 terminal->data_pitch);
625 memcpy(terminal_get_attr_row(terminal, to_row - i),
626 terminal_get_attr_row(terminal, from_row - i),
627 terminal->attr_pitch);
628 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000629 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
630 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000631 attr_init(terminal_get_attr_row(terminal, i),
632 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000633 }
634 } else {
635 to_row = terminal->margin_top;
636 from_row = terminal->margin_top + d;
637
638 for (i = 0; i < (window_height - d); i++) {
639 memcpy(terminal_get_row(terminal, to_row + i),
640 terminal_get_row(terminal, from_row + i),
641 terminal->data_pitch);
642 memcpy(terminal_get_attr_row(terminal, to_row + i),
643 terminal_get_attr_row(terminal, from_row + i),
644 terminal->attr_pitch);
645 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000646 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
647 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000648 attr_init(terminal_get_attr_row(terminal, i),
649 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000650 }
651 }
652}
653
654static void
655terminal_scroll(struct terminal *terminal, int d)
656{
657 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
658 terminal_scroll_buffer(terminal, d);
659 else
660 terminal_scroll_window(terminal, d);
661}
662
663static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000664terminal_shift_line(struct terminal *terminal, int d)
665{
666 union utf8_char *row;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500667 struct attr *attr_row;
Callum Lowcay69e96582011-01-07 19:47:00 +0000668
669 row = terminal_get_row(terminal, terminal->row);
670 attr_row = terminal_get_attr_row(terminal, terminal->row);
671
672 if ((terminal->width + d) <= terminal->column)
673 d = terminal->column + 1 - terminal->width;
674 if ((terminal->column + d) >= terminal->width)
675 d = terminal->width - terminal->column - 1;
676
677 if (d < 0) {
678 d = 0 - d;
679 memmove(&row[terminal->column],
680 &row[terminal->column + d],
681 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
Callum Lowcay69e96582011-01-07 19:47:00 +0000682 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
683 (terminal->width - terminal->column - d) * sizeof(struct attr));
684 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
685 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
686 } else {
687 memmove(&row[terminal->column + d], &row[terminal->column],
688 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
689 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
690 (terminal->width - terminal->column - d) * sizeof(struct attr));
691 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
692 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
693 }
694}
695
696static void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500697terminal_resize_cells(struct terminal *terminal, int width, int height)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500698{
699 size_t size;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000700 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000701 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000702 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000703 int data_pitch, attr_pitch;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500704 int i, l, total_rows;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500705 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000706 struct winsize ws;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500707
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500708 if (width < 1)
709 width = 1;
710 if (height < 1)
711 height = 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500712 if (terminal->width == width && terminal->height == height)
713 return;
714
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000715 data_pitch = width * sizeof(union utf8_char);
716 size = data_pitch * height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500717 data = malloc(size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000718 attr_pitch = width * sizeof(struct attr);
719 data_attr = malloc(attr_pitch * height);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000720 tab_ruler = malloc(width);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500721 memset(data, 0, size);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000722 memset(tab_ruler, 0, width);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000723 attr_init(data_attr, terminal->curr_attr, width * height);
724 if (terminal->data && terminal->data_attr) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500725 if (width > terminal->width)
726 l = terminal->width;
727 else
728 l = width;
729
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500730 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500731 total_rows = height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500732 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500733 total_rows = terminal->height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500734 }
735
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000736 for (i = 0; i < total_rows; i++) {
737 memcpy(&data[width * i],
738 terminal_get_row(terminal, i),
739 l * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000740 memcpy(&data_attr[width * i],
741 terminal_get_attr_row(terminal, i),
742 l * sizeof(struct attr));
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000743 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500744
745 free(terminal->data);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000746 free(terminal->data_attr);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000747 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500748 }
749
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000750 terminal->data_pitch = data_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000751 terminal->attr_pitch = attr_pitch;
Callum Lowcay86653ed2011-01-07 19:47:03 +0000752 terminal->margin_bottom =
753 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500754 terminal->width = width;
755 terminal->height = height;
756 terminal->data = data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000757 terminal->data_attr = data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000758 terminal->tab_ruler = tab_ruler;
759 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500760
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000761 /* Update the window size */
762 ws.ws_row = terminal->height;
763 ws.ws_col = terminal->width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500764 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500765 ws.ws_xpixel = allocation.width;
766 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000767 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500768}
769
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500770static void
771resize_handler(struct widget *widget,
772 int32_t width, int32_t height, void *data)
773{
774 struct terminal *terminal = data;
775 int32_t columns, rows, m;
776
Alexander Preisingere2b88c02012-06-18 20:59:26 +0200777 if (width < 200)
778 width = 200;
779
780 if (height < 50)
781 height = 50;
782
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500783 m = 2 * terminal->margin;
784 columns = (width - m) / (int32_t) terminal->extents.max_x_advance;
785 rows = (height - m) / (int32_t) terminal->extents.height;
786
787 if (!terminal->fullscreen) {
788 width = columns * terminal->extents.max_x_advance + m;
789 height = rows * terminal->extents.height + m;
790 widget_set_size(terminal->widget, width, height);
791 }
792
793 terminal_resize_cells(terminal, columns, rows);
794}
795
796static void
797terminal_resize(struct terminal *terminal, int columns, int rows)
798{
799 int32_t width, height, m;
800
801 if (terminal->fullscreen)
802 return;
803
804 m = 2 * terminal->margin;
805 width = columns * terminal->extents.max_x_advance + m;
806 height = rows * terminal->extents.height + m;
Kristian Høgsberga1627922012-06-20 17:30:03 -0400807
808 frame_set_child_size(terminal->widget, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500809}
810
Callum Lowcay30eeae52011-01-07 19:46:55 +0000811struct color_scheme DEFAULT_COLORS = {
812 {
813 {0, 0, 0, 1}, /* black */
814 {0.66, 0, 0, 1}, /* red */
815 {0 , 0.66, 0, 1}, /* green */
816 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
817 {0 , 0 , 0.66, 1}, /* blue */
818 {0.66, 0 , 0.66, 1}, /* magenta */
819 {0, 0.66, 0.66, 1}, /* cyan */
820 {0.66, 0.66, 0.66, 1}, /* light grey */
821 {0.22, 0.33, 0.33, 1}, /* dark grey */
822 {1, 0.33, 0.33, 1}, /* high red */
823 {0.33, 1, 0.33, 1}, /* high green */
824 {1, 1, 0.33, 1}, /* high yellow */
825 {0.33, 0.33, 1, 1}, /* high blue */
826 {1, 0.33, 1, 1}, /* high magenta */
827 {0.33, 1, 1, 1}, /* high cyan */
828 {1, 1, 1, 1} /* white */
829 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500830 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000831 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
832};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400833
Kristian Høgsberg22106762008-12-08 13:50:07 -0500834static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500835terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
836{
837 cairo_set_source_rgba(cr,
838 terminal->color_table[index].r,
839 terminal->color_table[index].g,
840 terminal->color_table[index].b,
841 terminal->color_table[index].a);
842}
843
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500844static void
845terminal_send_selection(struct terminal *terminal, int fd)
846{
847 int row, col;
848 union utf8_char *p_row;
849 union decoded_attr attr;
850 FILE *fp;
851 int len;
852
853 fp = fdopen(fd, "w");
854 for (row = 0; row < terminal->height; row++) {
855 p_row = terminal_get_row(terminal, row);
856 for (col = 0; col < terminal->width; col++) {
857 /* get the attributes for this character cell */
858 terminal_decode_attr(terminal, row, col, &attr);
859 if (!attr.attr.s)
860 continue;
861 len = strnlen((char *) p_row[col].byte, 4);
862 fwrite(p_row[col].byte, 1, len, fp);
863 }
864 }
865 fclose(fp);
866}
867
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500868struct glyph_run {
869 struct terminal *terminal;
870 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400871 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500872 union decoded_attr attr;
873 cairo_glyph_t glyphs[256], *g;
874};
875
876static void
877glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
878{
879 run->terminal = terminal;
880 run->cr = cr;
881 run->g = run->glyphs;
882 run->count = 0;
883 run->attr.key = 0;
884}
885
886static void
887glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
888{
889 cairo_scaled_font_t *font;
890
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500891 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
892 (attr.key != run->attr.key)) {
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 cairo_set_scaled_font(run->cr, font);
898 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300899 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500900
Callum Lowcay9d708b02011-01-12 20:06:17 +1300901 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
902 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500903 run->g = run->glyphs;
904 run->count = 0;
905 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300906 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500907}
908
909static void
910glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
911{
912 int num_glyphs;
913 cairo_scaled_font_t *font;
914
915 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
916
Callum Lowcay9d708b02011-01-12 20:06:17 +1300917 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500918 font = run->terminal->font_bold;
919 else
920 font = run->terminal->font_normal;
921
922 cairo_move_to(run->cr, x, y);
923 cairo_scaled_font_text_to_glyphs (font, x, y,
924 (char *) c->byte, 4,
925 &run->g, &num_glyphs,
926 NULL, NULL, NULL);
927 run->g += num_glyphs;
928 run->count += num_glyphs;
929}
930
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500931
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500932static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500933redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500934{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500935 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500936 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500937 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000938 int top_margin, side_margin;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600939 int row, col, cursor_x, cursor_y;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000940 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500941 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000942 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500943 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500944 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500945 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500946 cairo_font_extents_t extents;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500947
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500948 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500949 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500950 cr = cairo_create(surface);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500951 cairo_rectangle(cr, allocation.x, allocation.y,
952 allocation.width, allocation.height);
953 cairo_clip(cr);
954 cairo_push_group(cr);
955
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500956 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500957 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500958 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500959
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500960 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500961
Kristian Høgsberg59826582011-01-20 11:56:57 -0500962 extents = terminal->extents;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500963 side_margin = (allocation.width - terminal->width * extents.max_x_advance) / 2;
964 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500965
Callum Lowcay30eeae52011-01-07 19:46:55 +0000966 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500967 cairo_translate(cr, allocation.x + side_margin,
968 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500969 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000970 for (row = 0; row < terminal->height; row++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000971 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000972 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500973 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000974
Callum Lowcay9d708b02011-01-12 20:06:17 +1300975 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -0500976 continue;
977
Callum Lowcay9d708b02011-01-12 20:06:17 +1300978 terminal_set_color(terminal, cr, attr.attr.bg);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500979 cairo_move_to(cr, col * extents.max_x_advance,
980 row * extents.height);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000981 cairo_rel_line_to(cr, extents.max_x_advance, 0);
982 cairo_rel_line_to(cr, 0, extents.height);
983 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
984 cairo_close_path(cr);
985 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500986 }
987 }
Callum Lowcay30eeae52011-01-07 19:46:55 +0000988
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500989 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
990
991 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500992 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500993 for (row = 0; row < terminal->height; row++) {
994 p_row = terminal_get_row(terminal, row);
995 for (col = 0; col < terminal->width; col++) {
996 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500997 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500998
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500999 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001000
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001001 text_x = col * extents.max_x_advance;
1002 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +13001003 if (attr.attr.a & ATTRMASK_UNDERLINE) {
1004 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +00001005 cairo_move_to(cr, text_x, (double)text_y + 1.5);
1006 cairo_line_to(cr, text_x + extents.max_x_advance, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001007 cairo_stroke(cr);
1008 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -05001009
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001010 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001011 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001012 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001013
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001014 attr.key = ~0;
1015 glyph_run_flush(&run, attr);
1016
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001017 if ((terminal->mode & MODE_SHOW_CURSOR) && !terminal->focused) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001018 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001019
Callum Lowcay30eeae52011-01-07 19:46:55 +00001020 cairo_set_line_width(cr, 1);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001021 cairo_move_to(cr, terminal->column * extents.max_x_advance + d,
1022 terminal->row * extents.height + d);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001023 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
1024 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
1025 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
1026 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001027
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001028 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001029 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001030
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001031 cairo_pop_group_to_source(cr);
1032 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001033 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001034 cairo_surface_destroy(surface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001035
1036 if (terminal->send_cursor_position) {
1037 cursor_x = side_margin + allocation.x +
1038 terminal->column * extents.max_x_advance;
1039 cursor_y = top_margin + allocation.y +
1040 terminal->row * extents.height;
1041 window_set_text_cursor_position(terminal->window,
1042 cursor_x, cursor_y);
1043 terminal->send_cursor_position = 0;
1044 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001045}
1046
1047static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001048terminal_write(struct terminal *terminal, const char *data, size_t length)
1049{
1050 if (write(terminal->master, data, length) < 0)
1051 abort();
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001052 terminal->send_cursor_position = 1;
Kristian Høgsberg26130862011-08-24 11:30:21 -04001053}
1054
1055static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001056terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001057
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001058static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001059handle_char(struct terminal *terminal, union utf8_char utf8);
1060
1061static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001062handle_sgr(struct terminal *terminal, int code);
1063
1064static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001065handle_term_parameter(struct terminal *terminal, int code, int sr)
1066{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001067 int i;
1068
Callum Lowcay67a201d2011-01-12 19:23:41 +13001069 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001070 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001071 case 1: /* DECCKM */
1072 if (sr) terminal->key_mode = KM_APPLICATION;
1073 else terminal->key_mode = KM_NORMAL;
1074 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001075 case 2: /* DECANM */
1076 /* No VT52 support yet */
1077 terminal->g0 = CS_US;
1078 terminal->g1 = CS_US;
1079 terminal->cs = terminal->g0;
1080 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001081 case 3: /* DECCOLM */
1082 if (sr)
1083 terminal_resize(terminal, 132, 24);
1084 else
1085 terminal_resize(terminal, 80, 24);
1086
1087 /* set columns, but also home cursor and clear screen */
1088 terminal->row = 0; terminal->column = 0;
1089 for (i = 0; i < terminal->height; i++) {
1090 memset(terminal_get_row(terminal, i),
1091 0, terminal->data_pitch);
1092 attr_init(terminal_get_attr_row(terminal, i),
1093 terminal->curr_attr, terminal->width);
1094 }
1095 break;
1096 case 5: /* DECSCNM */
1097 if (sr) terminal->mode |= MODE_INVERSE;
1098 else terminal->mode &= ~MODE_INVERSE;
1099 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001100 case 6: /* DECOM */
1101 terminal->origin_mode = sr;
1102 if (terminal->origin_mode)
1103 terminal->row = terminal->margin_top;
1104 else
1105 terminal->row = 0;
1106 terminal->column = 0;
1107 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001108 case 7: /* DECAWM */
1109 if (sr) terminal->mode |= MODE_AUTOWRAP;
1110 else terminal->mode &= ~MODE_AUTOWRAP;
1111 break;
1112 case 8: /* DECARM */
1113 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1114 else terminal->mode &= ~MODE_AUTOREPEAT;
1115 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001116 case 12: /* Very visible cursor (CVVIS) */
1117 /* FIXME: What do we do here. */
1118 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001119 case 25:
1120 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1121 else terminal->mode &= ~MODE_SHOW_CURSOR;
1122 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001123 case 1034: /* smm/rmm, meta mode on/off */
1124 /* ignore */
1125 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001126 case 1037: /* deleteSendsDel */
1127 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1128 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1129 break;
1130 case 1039: /* altSendsEscape */
1131 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1132 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1133 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001134 case 1049: /* rmcup/smcup, alternate screen */
1135 /* Ignore. Should be possible to implement,
1136 * but it's kind of annoying. */
1137 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001138 default:
1139 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1140 break;
1141 }
1142 } else {
1143 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001144 case 4: /* IRM */
1145 if (sr) terminal->mode |= MODE_IRM;
1146 else terminal->mode &= ~MODE_IRM;
1147 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001148 case 20: /* LNM */
1149 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1150 else terminal->mode &= ~MODE_LF_NEWLINE;
1151 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001152 default:
1153 fprintf(stderr, "Unknown parameter: %d\n", code);
1154 break;
1155 }
1156 }
1157}
1158
1159static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001160handle_dcs(struct terminal *terminal)
1161{
1162}
1163
1164static void
1165handle_osc(struct terminal *terminal)
1166{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001167 char *p;
1168 int code;
1169
1170 terminal->escape[terminal->escape_length++] = '\0';
1171 p = &terminal->escape[2];
1172 code = strtol(p, &p, 10);
1173 if (*p == ';') p++;
1174
1175 switch (code) {
1176 case 0: /* Icon name and window title */
1177 case 1: /* Icon label */
1178 case 2: /* Window title*/
1179 window_set_title(terminal->window, p);
1180 break;
1181 default:
1182 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1183 break;
1184 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001185}
1186
1187static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001188handle_escape(struct terminal *terminal)
1189{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001190 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001191 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001192 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001193 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001194 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001195 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001196 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001197
1198 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001199 i = 0;
1200 p = &terminal->escape[2];
1201 while ((isdigit(*p) || *p == ';') && i < 10) {
1202 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001203 if (!set[i]) {
1204 args[i] = 0;
1205 set[i] = 1;
1206 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001207 p++;
1208 i++;
1209 } else {
1210 args[i] = strtol(p, &p, 10);
1211 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001212 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001213 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001214
1215 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001216 case '@': /* ICH */
1217 count = set[0] ? args[0] : 1;
1218 if (count == 0) count = 1;
1219 terminal_shift_line(terminal, count);
1220 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001221 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001222 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001223 if (count == 0) count = 1;
1224 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001225 terminal->row -= count;
1226 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001227 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001228 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001229 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001230 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001231 if (count == 0) count = 1;
1232 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001233 terminal->row += count;
1234 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001235 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001236 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001237 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001238 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001239 if (count == 0) count = 1;
1240 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001241 terminal->column += count;
1242 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001243 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001244 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001245 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001246 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001247 if (count == 0) count = 1;
1248 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001249 terminal->column -= count;
1250 else
1251 terminal->column = 0;
1252 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001253 case 'E': /* CNL */
1254 count = set[0] ? args[0] : 1;
1255 if (terminal->row + count <= terminal->margin_bottom)
1256 terminal->row += count;
1257 else
1258 terminal->row = terminal->margin_bottom;
1259 terminal->column = 0;
1260 break;
1261 case 'F': /* CPL */
1262 count = set[0] ? args[0] : 1;
1263 if (terminal->row - count >= terminal->margin_top)
1264 terminal->row -= count;
1265 else
1266 terminal->row = terminal->margin_top;
1267 terminal->column = 0;
1268 break;
1269 case 'G': /* CHA */
1270 y = set[0] ? args[0] : 1;
1271 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1272
1273 terminal->column = y - 1;
1274 break;
1275 case 'f': /* HVP */
1276 case 'H': /* CUP */
1277 x = (set[1] ? args[1] : 1) - 1;
1278 x = x < 0 ? 0 :
1279 (x >= terminal->width ? terminal->width - 1 : x);
1280
1281 y = (set[0] ? args[0] : 1) - 1;
1282 if (terminal->origin_mode) {
1283 y += terminal->margin_top;
1284 y = y < terminal->margin_top ? terminal->margin_top :
1285 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1286 } else {
1287 y = y < 0 ? 0 :
1288 (y >= terminal->height ? terminal->height - 1 : y);
1289 }
1290
1291 terminal->row = y;
1292 terminal->column = x;
1293 break;
1294 case 'I': /* CHT */
1295 count = set[0] ? args[0] : 1;
1296 if (count == 0) count = 1;
1297 while (count > 0 && terminal->column < terminal->width) {
1298 if (terminal->tab_ruler[terminal->column]) count--;
1299 terminal->column++;
1300 }
1301 terminal->column--;
1302 break;
1303 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001304 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001305 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001306 if (!set[0] || args[0] == 0 || args[0] > 2) {
1307 memset(&row[terminal->column],
1308 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1309 attr_init(&attr_row[terminal->column],
1310 terminal->curr_attr, terminal->width - terminal->column);
1311 for (i = terminal->row + 1; i < terminal->height; i++) {
1312 memset(terminal_get_row(terminal, i),
1313 0, terminal->data_pitch);
1314 attr_init(terminal_get_attr_row(terminal, i),
1315 terminal->curr_attr, terminal->width);
1316 }
1317 } else if (args[0] == 1) {
1318 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1319 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1320 for (i = 0; i < terminal->row; i++) {
1321 memset(terminal_get_row(terminal, i),
1322 0, terminal->data_pitch);
1323 attr_init(terminal_get_attr_row(terminal, i),
1324 terminal->curr_attr, terminal->width);
1325 }
1326 } else if (args[0] == 2) {
1327 for (i = 0; i < terminal->height; i++) {
1328 memset(terminal_get_row(terminal, i),
1329 0, terminal->data_pitch);
1330 attr_init(terminal_get_attr_row(terminal, i),
1331 terminal->curr_attr, terminal->width);
1332 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001333 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001334 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001335 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001336 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001337 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001338 if (!set[0] || args[0] == 0 || args[0] > 2) {
1339 memset(&row[terminal->column], 0,
1340 (terminal->width - terminal->column) * sizeof(union utf8_char));
1341 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1342 terminal->width - terminal->column);
1343 } else if (args[0] == 1) {
1344 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1345 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1346 } else if (args[0] == 2) {
1347 memset(row, 0, terminal->data_pitch);
1348 attr_init(attr_row, terminal->curr_attr, terminal->width);
1349 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001350 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001351 case 'L': /* IL */
1352 count = set[0] ? args[0] : 1;
1353 if (count == 0) count = 1;
1354 if (terminal->row >= terminal->margin_top &&
1355 terminal->row < terminal->margin_bottom)
1356 {
1357 top = terminal->margin_top;
1358 terminal->margin_top = terminal->row;
1359 terminal_scroll(terminal, 0 - count);
1360 terminal->margin_top = top;
1361 } else if (terminal->row == terminal->margin_bottom) {
1362 memset(terminal_get_row(terminal, terminal->row),
1363 0, terminal->data_pitch);
1364 attr_init(terminal_get_attr_row(terminal, terminal->row),
1365 terminal->curr_attr, terminal->width);
1366 }
1367 break;
1368 case 'M': /* DL */
1369 count = set[0] ? args[0] : 1;
1370 if (count == 0) count = 1;
1371 if (terminal->row >= terminal->margin_top &&
1372 terminal->row < terminal->margin_bottom)
1373 {
1374 top = terminal->margin_top;
1375 terminal->margin_top = terminal->row;
1376 terminal_scroll(terminal, count);
1377 terminal->margin_top = top;
1378 } else if (terminal->row == terminal->margin_bottom) {
1379 memset(terminal_get_row(terminal, terminal->row),
1380 0, terminal->data_pitch);
1381 }
1382 break;
1383 case 'P': /* DCH */
1384 count = set[0] ? args[0] : 1;
1385 if (count == 0) count = 1;
1386 terminal_shift_line(terminal, 0 - count);
1387 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001388 case 'S': /* SU */
1389 terminal_scroll(terminal, set[0] ? args[0] : 1);
1390 break;
1391 case 'T': /* SD */
1392 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1393 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001394 case 'X': /* ECH */
1395 count = set[0] ? args[0] : 1;
1396 if (count == 0) count = 1;
1397 if ((terminal->column + count) > terminal->width)
1398 count = terminal->width - terminal->column;
1399 row = terminal_get_row(terminal, terminal->row);
1400 attr_row = terminal_get_attr_row(terminal, terminal->row);
1401 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1402 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1403 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001404 case 'Z': /* CBT */
1405 count = set[0] ? args[0] : 1;
1406 if (count == 0) count = 1;
1407 while (count > 0 && terminal->column >= 0) {
1408 if (terminal->tab_ruler[terminal->column]) count--;
1409 terminal->column--;
1410 }
1411 terminal->column++;
1412 break;
1413 case '`': /* HPA */
1414 y = set[0] ? args[0] : 1;
1415 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1416
1417 terminal->column = y - 1;
1418 break;
1419 case 'b': /* REP */
1420 count = set[0] ? args[0] : 1;
1421 if (count == 0) count = 1;
1422 if (terminal->last_char.byte[0])
1423 for (i = 0; i < count; i++)
1424 handle_char(terminal, terminal->last_char);
1425 terminal->last_char.byte[0] = 0;
1426 break;
1427 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001428 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001429 break;
1430 case 'd': /* VPA */
1431 x = set[0] ? args[0] : 1;
1432 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1433
1434 terminal->row = x - 1;
1435 break;
1436 case 'g': /* TBC */
1437 if (!set[0] || args[0] == 0) {
1438 terminal->tab_ruler[terminal->column] = 0;
1439 } else if (args[0] == 3) {
1440 memset(terminal->tab_ruler, 0, terminal->width);
1441 }
1442 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001443 case 'h': /* SM */
1444 for(i = 0; i < 10 && set[i]; i++) {
1445 handle_term_parameter(terminal, args[i], 1);
1446 }
1447 break;
1448 case 'l': /* RM */
1449 for(i = 0; i < 10 && set[i]; i++) {
1450 handle_term_parameter(terminal, args[i], 0);
1451 }
1452 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001453 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001454 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001455 if (i <= 7 && set[i] && set[i + 1] &&
1456 set[i + 2] && args[i + 1] == 5)
1457 {
1458 if (args[i] == 38) {
1459 handle_sgr(terminal, args[i + 2] + 256);
1460 break;
1461 } else if (args[i] == 48) {
1462 handle_sgr(terminal, args[i + 2] + 512);
1463 break;
1464 }
1465 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001466 if(set[i]) {
1467 handle_sgr(terminal, args[i]);
1468 } else if(i == 0) {
1469 handle_sgr(terminal, 0);
1470 break;
1471 } else {
1472 break;
1473 }
1474 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001475 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001476 case 'n': /* DSR */
1477 i = set[0] ? args[0] : 0;
1478 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001479 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001480 } else if (i == 6) {
1481 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1482 terminal->origin_mode ?
1483 terminal->row+terminal->margin_top : terminal->row+1,
1484 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001485 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001486 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001487 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001488 case 'r':
1489 if(!set[0]) {
1490 terminal->margin_top = 0;
1491 terminal->margin_bottom = terminal->height-1;
1492 terminal->row = 0;
1493 terminal->column = 0;
1494 } else {
1495 top = (set[0] ? args[0] : 1) - 1;
1496 top = top < 0 ? 0 :
1497 (top >= terminal->height ? terminal->height - 1 : top);
1498 bottom = (set[1] ? args[1] : 1) - 1;
1499 bottom = bottom < 0 ? 0 :
1500 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1501 if(bottom > top) {
1502 terminal->margin_top = top;
1503 terminal->margin_bottom = bottom;
1504 } else {
1505 terminal->margin_top = 0;
1506 terminal->margin_bottom = terminal->height-1;
1507 }
1508 if(terminal->origin_mode)
1509 terminal->row = terminal->margin_top;
1510 else
1511 terminal->row = 0;
1512 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001513 }
1514 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001515 case 's':
1516 terminal->saved_row = terminal->row;
1517 terminal->saved_column = terminal->column;
1518 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001519 case 't': /* windowOps */
1520 if (!set[0]) break;
1521 switch (args[0]) {
1522 case 4: /* resize px */
1523 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001524 widget_schedule_resize(terminal->widget,
1525 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001526 }
1527 break;
1528 case 8: /* resize ch */
1529 if (set[1] && set[2]) {
1530 terminal_resize(terminal, args[2], args[1]);
1531 }
1532 break;
1533 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001534 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001535 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1536 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001537 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001538 break;
1539 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001540 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001541 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1542 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001543 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001544 break;
1545 case 18: /* report ch */
1546 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1547 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001548 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001549 break;
1550 case 21: /* report title */
1551 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1552 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001553 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001554 break;
1555 default:
1556 if (args[0] >= 24)
1557 terminal_resize(terminal, terminal->width, args[0]);
1558 else
1559 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1560 break;
1561 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001562 case 'u':
1563 terminal->row = terminal->saved_row;
1564 terminal->column = terminal->saved_column;
1565 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001566 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001567 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001568 break;
1569 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001570}
1571
1572static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001573handle_non_csi_escape(struct terminal *terminal, char code)
1574{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001575 switch(code) {
1576 case 'M': /* RI */
1577 terminal->row -= 1;
1578 if(terminal->row < terminal->margin_top) {
1579 terminal->row = terminal->margin_top;
1580 terminal_scroll(terminal, -1);
1581 }
1582 break;
1583 case 'E': /* NEL */
1584 terminal->column = 0;
1585 // fallthrough
1586 case 'D': /* IND */
1587 terminal->row += 1;
1588 if(terminal->row > terminal->margin_bottom) {
1589 terminal->row = terminal->margin_bottom;
1590 terminal_scroll(terminal, +1);
1591 }
1592 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001593 case 'c': /* RIS */
1594 terminal_init(terminal);
1595 break;
1596 case 'H': /* HTS */
1597 terminal->tab_ruler[terminal->column] = 1;
1598 break;
1599 case '7': /* DECSC */
1600 terminal->saved_row = terminal->row;
1601 terminal->saved_column = terminal->column;
1602 terminal->saved_attr = terminal->curr_attr;
1603 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001604 terminal->saved_cs = terminal->cs;
1605 terminal->saved_g0 = terminal->g0;
1606 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001607 break;
1608 case '8': /* DECRC */
1609 terminal->row = terminal->saved_row;
1610 terminal->column = terminal->saved_column;
1611 terminal->curr_attr = terminal->saved_attr;
1612 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001613 terminal->cs = terminal->saved_cs;
1614 terminal->g0 = terminal->saved_g0;
1615 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001616 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001617 case '=': /* DECPAM */
1618 terminal->key_mode = KM_APPLICATION;
1619 break;
1620 case '>': /* DECPNM */
1621 terminal->key_mode = KM_NORMAL;
1622 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001623 default:
1624 fprintf(stderr, "Unknown escape code: %c\n", code);
1625 break;
1626 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001627}
1628
1629static void
1630handle_special_escape(struct terminal *terminal, char special, char code)
1631{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001632 int i, numChars;
1633
1634 if (special == '#') {
1635 switch(code) {
1636 case '8':
1637 /* fill with 'E', no cheap way to do this */
1638 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1639 numChars = terminal->width * terminal->height;
1640 for(i = 0; i < numChars; i++) {
1641 terminal->data[i].byte[0] = 'E';
1642 }
1643 break;
1644 default:
1645 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1646 break;
1647 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001648 } else if (special == '(' || special == ')') {
1649 switch(code) {
1650 case '0':
1651 if (special == '(')
1652 terminal->g0 = CS_SPECIAL;
1653 else
1654 terminal->g1 = CS_SPECIAL;
1655 break;
1656 case 'A':
1657 if (special == '(')
1658 terminal->g0 = CS_UK;
1659 else
1660 terminal->g1 = CS_UK;
1661 break;
1662 case 'B':
1663 if (special == '(')
1664 terminal->g0 = CS_US;
1665 else
1666 terminal->g1 = CS_US;
1667 break;
1668 default:
1669 fprintf(stderr, "Unknown character set %c\n", code);
1670 break;
1671 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001672 } else {
1673 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1674 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001675}
1676
1677static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001678handle_sgr(struct terminal *terminal, int code)
1679{
1680 switch(code) {
1681 case 0:
1682 terminal->curr_attr = terminal->color_scheme->default_attr;
1683 break;
1684 case 1:
1685 terminal->curr_attr.a |= ATTRMASK_BOLD;
1686 if (terminal->curr_attr.fg < 8)
1687 terminal->curr_attr.fg += 8;
1688 break;
1689 case 4:
1690 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1691 break;
1692 case 5:
1693 terminal->curr_attr.a |= ATTRMASK_BLINK;
1694 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001695 case 8:
1696 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1697 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001698 case 2:
1699 case 21:
1700 case 22:
1701 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1702 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1703 terminal->curr_attr.fg -= 8;
1704 break;
1705 case 24:
1706 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1707 break;
1708 case 25:
1709 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1710 break;
1711 case 7:
1712 case 26:
1713 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1714 break;
1715 case 27:
1716 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1717 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001718 case 28:
1719 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1720 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001721 case 39:
1722 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1723 break;
1724 case 49:
1725 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1726 break;
1727 default:
1728 if(code >= 30 && code <= 37) {
1729 terminal->curr_attr.fg = code - 30;
1730 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1731 terminal->curr_attr.fg += 8;
1732 } else if(code >= 40 && code <= 47) {
1733 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001734 } else if (code >= 90 && code <= 97) {
1735 terminal->curr_attr.fg = code - 90 + 8;
1736 } else if (code >= 100 && code <= 107) {
1737 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001738 } else if(code >= 256 && code < 512) {
1739 terminal->curr_attr.fg = code - 256;
1740 } else if(code >= 512 && code < 768) {
1741 terminal->curr_attr.bg = code - 512;
1742 } else {
1743 fprintf(stderr, "Unknown SGR code: %d\n", code);
1744 }
1745 break;
1746 }
1747}
1748
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001749/* Returns 1 if c was special, otherwise 0 */
1750static int
1751handle_special_char(struct terminal *terminal, char c)
1752{
1753 union utf8_char *row;
1754 struct attr *attr_row;
1755
1756 row = terminal_get_row(terminal, terminal->row);
1757 attr_row = terminal_get_attr_row(terminal, terminal->row);
1758
1759 switch(c) {
1760 case '\r':
1761 terminal->column = 0;
1762 break;
1763 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001764 if (terminal->mode & MODE_LF_NEWLINE) {
1765 terminal->column = 0;
1766 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001767 /* fallthrough */
1768 case '\v':
1769 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001770 terminal->row++;
1771 if(terminal->row > terminal->margin_bottom) {
1772 terminal->row = terminal->margin_bottom;
1773 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001774 }
1775
1776 break;
1777 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001778 while (terminal->column < terminal->width) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001779 if (terminal->mode & MODE_IRM)
1780 terminal_shift_line(terminal, +1);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001781 row[terminal->column].byte[0] = ' ';
1782 row[terminal->column].byte[1] = '\0';
1783 attr_row[terminal->column] = terminal->curr_attr;
1784 terminal->column++;
Kristian Høgsbergcca3c2f2012-06-20 15:56:13 -04001785 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001786 }
1787 if (terminal->column >= terminal->width) {
1788 terminal->column = terminal->width - 1;
1789 }
1790
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001791 break;
1792 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001793 if (terminal->column >= terminal->width) {
1794 terminal->column = terminal->width - 2;
1795 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001796 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001797 } else if (terminal->mode & MODE_AUTOWRAP) {
1798 terminal->column = terminal->width - 1;
1799 terminal->row -= 1;
1800 if (terminal->row < terminal->margin_top) {
1801 terminal->row = terminal->margin_top;
1802 terminal_scroll(terminal, -1);
1803 }
1804 }
1805
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001806 break;
1807 case '\a':
1808 /* Bell */
1809 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001810 case '\x0E': /* SO */
1811 terminal->cs = terminal->g1;
1812 break;
1813 case '\x0F': /* SI */
1814 terminal->cs = terminal->g0;
1815 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001816 default:
1817 return 0;
1818 }
1819
1820 return 1;
1821}
1822
1823static void
1824handle_char(struct terminal *terminal, union utf8_char utf8)
1825{
1826 union utf8_char *row;
1827 struct attr *attr_row;
1828
1829 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001830
1831 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001832
1833 /* There are a whole lot of non-characters, control codes,
1834 * and formatting codes that should probably be ignored,
1835 * for example: */
1836 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1837 /* BOM, ignore */
1838 return;
1839 }
1840
1841 /* Some of these non-characters should be translated, e.g.: */
1842 if (utf8.byte[0] < 32) {
1843 utf8.byte[0] = utf8.byte[0] + 64;
1844 }
1845
1846 /* handle right margin effects */
1847 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001848 if (terminal->mode & MODE_AUTOWRAP) {
1849 terminal->column = 0;
1850 terminal->row += 1;
1851 if (terminal->row > terminal->margin_bottom) {
1852 terminal->row = terminal->margin_bottom;
1853 terminal_scroll(terminal, +1);
1854 }
1855 } else {
1856 terminal->column--;
1857 }
1858 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001859
1860 row = terminal_get_row(terminal, terminal->row);
1861 attr_row = terminal_get_attr_row(terminal, terminal->row);
1862
Callum Lowcay69e96582011-01-07 19:47:00 +00001863 if (terminal->mode & MODE_IRM)
1864 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001865 row[terminal->column] = utf8;
1866 attr_row[terminal->column++] = terminal->curr_attr;
1867
1868 if (utf8.ch != terminal->last_char.ch)
1869 terminal->last_char = utf8;
1870}
1871
Callum Lowcay30eeae52011-01-07 19:46:55 +00001872static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001873escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1874{
1875 int len, i;
1876
1877 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1878 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1879 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1880 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1881 else len = 1; /* Invalid, cannot happen */
1882
1883 if (terminal->escape_length + len <= MAX_ESCAPE) {
1884 for (i = 0; i < len; i++)
1885 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1886 terminal->escape_length += len;
1887 } else if (terminal->escape_length < MAX_ESCAPE) {
1888 terminal->escape[terminal->escape_length++] = 0;
1889 }
1890}
1891
1892static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001893terminal_data(struct terminal *terminal, const char *data, size_t length)
1894{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001895 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001896 union utf8_char utf8;
1897 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001898
1899 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001900 parser_state =
1901 utf8_next_char(&terminal->state_machine, data[i]);
1902 switch(parser_state) {
1903 case utf8state_accept:
1904 utf8.ch = terminal->state_machine.s.ch;
1905 break;
1906 case utf8state_reject:
1907 /* the unicode replacement character */
1908 utf8.byte[0] = 0xEF;
1909 utf8.byte[1] = 0xBF;
1910 utf8.byte[2] = 0xBD;
1911 utf8.byte[3] = 0x00;
1912 break;
1913 default:
1914 continue;
1915 }
1916
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001917 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001918 switch (terminal->state) {
1919 case escape_state_escape:
1920 escape_append_utf8(terminal, utf8);
1921 switch (utf8.byte[0]) {
1922 case 'P': /* DCS */
1923 terminal->state = escape_state_dcs;
1924 break;
1925 case '[': /* CSI */
1926 terminal->state = escape_state_csi;
1927 break;
1928 case ']': /* OSC */
1929 terminal->state = escape_state_osc;
1930 break;
1931 case '#':
1932 case '(':
1933 case ')': /* special */
1934 terminal->state = escape_state_special;
1935 break;
1936 case '^': /* PM (not implemented) */
1937 case '_': /* APC (not implemented) */
1938 terminal->state = escape_state_ignore;
1939 break;
1940 default:
1941 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001942 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13001943 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001944 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001945 continue;
1946 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001947 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1948 /* do nothing */
1949 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001950 terminal->escape_flags |= ESC_FLAG_WHAT;
1951 } else if (utf8.byte[0] == '>') {
1952 terminal->escape_flags |= ESC_FLAG_GT;
1953 } else if (utf8.byte[0] == '!') {
1954 terminal->escape_flags |= ESC_FLAG_BANG;
1955 } else if (utf8.byte[0] == '$') {
1956 terminal->escape_flags |= ESC_FLAG_CASH;
1957 } else if (utf8.byte[0] == '\'') {
1958 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1959 } else if (utf8.byte[0] == '"') {
1960 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1961 } else if (utf8.byte[0] == ' ') {
1962 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001963 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001964 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001965 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13001966 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001967 }
1968
1969 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1970 utf8.byte[0] == '`')
1971 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001972 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001973 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001974 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001975 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001976 continue;
1977 case escape_state_inner_escape:
1978 if (utf8.byte[0] == '\\') {
1979 terminal->state = escape_state_normal;
1980 if (terminal->outer_state == escape_state_dcs) {
1981 handle_dcs(terminal);
1982 } else if (terminal->outer_state == escape_state_osc) {
1983 handle_osc(terminal);
1984 }
1985 } else if (utf8.byte[0] == '\e') {
1986 terminal->state = terminal->outer_state;
1987 escape_append_utf8(terminal, utf8);
1988 if (terminal->escape_length >= MAX_ESCAPE)
1989 terminal->state = escape_state_normal;
1990 } else {
1991 terminal->state = terminal->outer_state;
1992 if (terminal->escape_length < MAX_ESCAPE)
1993 terminal->escape[terminal->escape_length++] = '\e';
1994 escape_append_utf8(terminal, utf8);
1995 if (terminal->escape_length >= MAX_ESCAPE)
1996 terminal->state = escape_state_normal;
1997 }
1998 continue;
1999 case escape_state_dcs:
2000 case escape_state_osc:
2001 case escape_state_ignore:
2002 if (utf8.byte[0] == '\e') {
2003 terminal->outer_state = terminal->state;
2004 terminal->state = escape_state_inner_escape;
2005 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
2006 terminal->state = escape_state_normal;
2007 handle_osc(terminal);
2008 } else {
2009 escape_append_utf8(terminal, utf8);
2010 if (terminal->escape_length >= MAX_ESCAPE)
2011 terminal->state = escape_state_normal;
2012 }
2013 continue;
2014 case escape_state_special:
2015 escape_append_utf8(terminal, utf8);
2016 terminal->state = escape_state_normal;
2017 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2018 handle_special_escape(terminal, terminal->escape[1],
2019 utf8.byte[0]);
2020 }
2021 continue;
2022 default:
2023 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002024 }
2025
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002026 /* this is valid, because ASCII characters are never used to
2027 * introduce a multibyte sequence in UTF-8 */
2028 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002029 terminal->state = escape_state_escape;
2030 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002031 terminal->escape[0] = '\e';
2032 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002033 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002034 } else {
2035 handle_char(terminal, utf8);
2036 } /* if */
2037 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002038
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002039 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002040}
2041
2042static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002043data_source_target(void *data,
2044 struct wl_data_source *source, const char *mime_type)
2045{
2046 fprintf(stderr, "data_source_target, %s\n", mime_type);
2047}
2048
2049static void
2050data_source_send(void *data,
2051 struct wl_data_source *source,
2052 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002053{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002054 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002055
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002056 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002057}
2058
2059static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002060data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002061{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002062 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002063}
2064
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002065static const struct wl_data_source_listener data_source_listener = {
2066 data_source_target,
2067 data_source_send,
2068 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002069};
2070
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002071static int
2072handle_bound_key(struct terminal *terminal,
2073 struct input *input, uint32_t sym, uint32_t time)
2074{
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002075 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002076 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002077 /* Cut selection; terminal doesn't do cut, fall
2078 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002079 case XKB_KEY_C:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002080 terminal->selection =
2081 display_create_data_source(terminal->display);
2082 wl_data_source_offer(terminal->selection,
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002083 "text/plain;charset=utf-8");
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002084 wl_data_source_add_listener(terminal->selection,
2085 &data_source_listener, terminal);
Kristian Høgsbergfee91be2012-06-02 21:21:41 -04002086 input_set_selection(input, terminal->selection,
2087 display_get_serial(terminal->display));
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002088 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002089 case XKB_KEY_V:
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002090 input_receive_selection_data_to_fd(input,
2091 "text/plain;charset=utf-8",
2092 terminal->master);
2093
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002094 return 1;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002095 default:
2096 return 0;
2097 }
2098}
2099
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002100static void
2101key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002102 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2103 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002104{
2105 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002106 char ch[MAX_RESPONSE];
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002107 uint32_t modifiers;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002108 int len = 0;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002109
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002110 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002111 if ((modifiers & MOD_CONTROL_MASK) &&
2112 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002113 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2114 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002115 return;
2116
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002117 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002118 case XKB_KEY_F11:
Daniel Stonec9785ea2012-05-30 16:31:52 +01002119 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002120 break;
2121 terminal->fullscreen ^= 1;
2122 window_set_fullscreen(window, terminal->fullscreen);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002123 break;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002124
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002125 case XKB_KEY_BackSpace:
Kristian Høgsberg71a4cf42012-06-20 17:57:56 -04002126 ch[len++] = 0x7f;
2127 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002128 case XKB_KEY_Tab:
2129 case XKB_KEY_Linefeed:
2130 case XKB_KEY_Clear:
2131 case XKB_KEY_Pause:
2132 case XKB_KEY_Scroll_Lock:
2133 case XKB_KEY_Sys_Req:
2134 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002135 ch[len++] = sym & 0x7f;
2136 break;
2137
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002138 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002139 if (terminal->mode & MODE_LF_NEWLINE) {
2140 ch[len++] = 0x0D;
2141 ch[len++] = 0x0A;
2142 } else {
2143 ch[len++] = 0x0D;
2144 }
2145 break;
2146
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002147 case XKB_KEY_Shift_L:
2148 case XKB_KEY_Shift_R:
2149 case XKB_KEY_Control_L:
2150 case XKB_KEY_Control_R:
2151 case XKB_KEY_Alt_L:
2152 case XKB_KEY_Alt_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002153 break;
2154
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002155 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002156 len = function_key_response('[', 2, modifiers, '~', ch);
2157 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002158 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002159 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2160 ch[len++] = '\x04';
2161 } else {
2162 len = function_key_response('[', 3, modifiers, '~', ch);
2163 }
2164 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002165 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002166 len = function_key_response('[', 5, modifiers, '~', ch);
2167 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002168 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002169 len = function_key_response('[', 6, modifiers, '~', ch);
2170 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002171 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002172 len = function_key_response('O', 1, modifiers, 'P', ch);
2173 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002174 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002175 len = function_key_response('O', 1, modifiers, 'Q', ch);
2176 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002177 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002178 len = function_key_response('O', 1, modifiers, 'R', ch);
2179 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002180 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002181 len = function_key_response('O', 1, modifiers, 'S', ch);
2182 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002183 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002184 len = function_key_response('[', 15, modifiers, '~', ch);
2185 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002186 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002187 len = function_key_response('[', 17, modifiers, '~', ch);
2188 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002189 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002190 len = function_key_response('[', 18, modifiers, '~', ch);
2191 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002192 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002193 len = function_key_response('[', 19, modifiers, '~', ch);
2194 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002195 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002196 len = function_key_response('[', 20, modifiers, '~', ch);
2197 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002198 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002199 len = function_key_response('[', 21, modifiers, '~', ch);
2200 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002201 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002202 len = function_key_response('[', 24, modifiers, '~', ch);
2203 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002204 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002205 /* Handle special keys with alternate mappings */
2206 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2207 if (len != 0) break;
2208
Kristian Høgsberg70163132012-05-08 15:55:39 -04002209 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002210 if (sym >= '3' && sym <= '7')
2211 sym = (sym & 0x1f) + 8;
2212
2213 if (!((sym >= '!' && sym <= '/') ||
2214 (sym >= '8' && sym <= '?') ||
2215 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2216 else if (sym == '2') sym = 0x00;
2217 else if (sym == '/') sym = 0x1F;
2218 else if (sym == '8' || sym == '?') sym = 0x7F;
2219 } else if ((terminal->mode & MODE_ALT_SENDS_ESC) &&
Kristian Høgsberg70163132012-05-08 15:55:39 -04002220 (modifiers & MOD_ALT_MASK))
Callum Lowcay7e08e902011-01-07 19:47:02 +00002221 {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002222 ch[len++] = 0x1b;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002223 } else if (modifiers & MOD_ALT_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002224 sym = sym | 0x80;
2225 }
2226
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002227 if (sym < 256)
2228 ch[len++] = sym;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002229 break;
2230 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002231
Daniel Stonec9785ea2012-05-30 16:31:52 +01002232 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0)
Kristian Høgsberg26130862011-08-24 11:30:21 -04002233 terminal_write(terminal, ch, len);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002234}
2235
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002236static void
2237keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002238 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002239{
2240 struct terminal *terminal = data;
2241
2242 terminal->focused = (device != NULL);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002243 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002244}
2245
Kristian Høgsberg59826582011-01-20 11:56:57 -05002246static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002247button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002248 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002249 uint32_t button,
2250 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002251{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002252 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002253
2254 switch (button) {
2255 case 272:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002256 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002257 terminal->dragging = 1;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002258 input_get_position(input,
2259 &terminal->selection_start_x,
2260 &terminal->selection_start_y);
2261 terminal->selection_end_x = terminal->selection_start_x;
2262 terminal->selection_end_y = terminal->selection_start_y;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002263 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002264 } else {
2265 terminal->dragging = 0;
2266 }
2267 break;
2268 }
2269}
2270
2271static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002272motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002273 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002274 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002275{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002276 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002277
2278 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002279 input_get_position(input,
2280 &terminal->selection_end_x,
2281 &terminal->selection_end_y);
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002282 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002283 }
2284
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002285 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002286}
2287
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002288static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002289terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002290{
2291 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002292 cairo_surface_t *surface;
2293 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002294
2295 terminal = malloc(sizeof *terminal);
2296 if (terminal == NULL)
2297 return terminal;
2298
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002299 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002300 terminal->fullscreen = fullscreen;
Callum Lowcay30eeae52011-01-07 19:46:55 +00002301 terminal->color_scheme = &DEFAULT_COLORS;
2302 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002303 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002304 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002305 terminal->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002306 terminal->widget = frame_create(terminal->window, terminal);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002307 window_set_title(terminal->window, "Wayland Terminal");
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002308 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002309
2310 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002311 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002312
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002313 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002314 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002315
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002316 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002317 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002318 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002319 keyboard_focus_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002320 widget_set_redraw_handler(terminal->widget, redraw_handler);
2321 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002322 widget_set_button_handler(terminal->widget, button_handler);
2323 widget_set_motion_handler(terminal->widget, motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002324
Kristian Høgsberg09531622010-06-14 23:22:15 -04002325 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2326 cr = cairo_create(surface);
Kristian Høgsberg09531622010-06-14 23:22:15 -04002327 cairo_set_font_size(cr, 14);
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002328 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002329 CAIRO_FONT_SLANT_NORMAL,
2330 CAIRO_FONT_WEIGHT_BOLD);
2331 terminal->font_bold = cairo_get_scaled_font (cr);
2332 cairo_scaled_font_reference(terminal->font_bold);
2333
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002334 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002335 CAIRO_FONT_SLANT_NORMAL,
2336 CAIRO_FONT_WEIGHT_NORMAL);
2337 terminal->font_normal = cairo_get_scaled_font (cr);
2338 cairo_scaled_font_reference(terminal->font_normal);
2339
Kristian Høgsberg09531622010-06-14 23:22:15 -04002340 cairo_font_extents(cr, &terminal->extents);
2341 cairo_destroy(cr);
2342 cairo_surface_destroy(surface);
2343
Kristian Høgsberga1627922012-06-20 17:30:03 -04002344 terminal_resize(terminal, 80, 25);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002345
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002346 return terminal;
2347}
2348
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002349static void
2350io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002351{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002352 struct terminal *terminal =
2353 container_of(task, struct terminal, io_task);
2354 char buffer[256];
2355 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002356
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002357 if (events & EPOLLHUP)
2358 exit(0);
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002359
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002360 len = read(terminal->master, buffer, sizeof buffer);
2361 if (len < 0)
2362 exit(0);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002363
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002364 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002365}
2366
2367static int
2368terminal_run(struct terminal *terminal, const char *path)
2369{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002370 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002371 pid_t pid;
2372
2373 pid = forkpty(&master, NULL, NULL, NULL);
2374 if (pid == 0) {
Callum Lowcay86653ed2011-01-07 19:47:03 +00002375 setenv("TERM", "xterm-256color", 1);
2376 setenv("COLORTERM", "xterm-256color", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002377 if (execl(path, path, NULL)) {
2378 printf("exec failed: %m\n");
2379 exit(EXIT_FAILURE);
2380 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002381 } else if (pid < 0) {
2382 fprintf(stderr, "failed to fork and create pty (%m).\n");
2383 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002384 }
2385
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002386 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002387 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002388 terminal->io_task.run = io_handler;
2389 display_watch_fd(terminal->display, terminal->master,
2390 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002391
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002392 window_set_fullscreen(terminal->window, terminal->fullscreen);
2393 if (!terminal->fullscreen)
2394 terminal_resize(terminal, 80, 24);
2395
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002396 return 0;
2397}
2398
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002399static const struct config_key terminal_config_keys[] = {
2400 { "font", CONFIG_KEY_STRING, &option_font },
2401};
2402
2403static const struct config_section config_sections[] = {
2404 { "terminal",
2405 terminal_config_keys, ARRAY_LENGTH(terminal_config_keys) },
2406};
2407
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002408static const struct weston_option terminal_options[] = {
2409 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002410 { WESTON_OPTION_STRING, "font", 0, &option_font },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002411};
2412
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002413int main(int argc, char *argv[])
2414{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002415 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002416 struct terminal *terminal;
Peter Hutterer035ac942012-02-03 20:58:19 +10002417 const char *shell;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002418 char *config_file;
2419
2420 config_file = config_file_path("weston.ini");
2421 parse_config_file(config_file,
2422 config_sections, ARRAY_LENGTH(config_sections),
2423 NULL);
2424 free(config_file);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002425
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002426 argc = parse_options(terminal_options,
2427 ARRAY_LENGTH(terminal_options), argc, argv);
2428
2429 d = display_create(argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002430 if (d == NULL) {
2431 fprintf(stderr, "failed to create display: %m\n");
2432 return -1;
2433 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002434
Peter Hutterer035ac942012-02-03 20:58:19 +10002435 shell = getenv("SHELL");
2436 if (!shell)
2437 shell = "/bin/bash";
2438
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002439 terminal = terminal_create(d, option_fullscreen);
Peter Hutterer035ac942012-02-03 20:58:19 +10002440 if (terminal_run(terminal, shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002441 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002442
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002443 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002444
2445 return 0;
2446}