blob: db9161ffc766680d2bff833b68031f41b08cdbca [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;
807 widget_schedule_resize(terminal->widget, width, height);
808}
809
Callum Lowcay30eeae52011-01-07 19:46:55 +0000810struct color_scheme DEFAULT_COLORS = {
811 {
812 {0, 0, 0, 1}, /* black */
813 {0.66, 0, 0, 1}, /* red */
814 {0 , 0.66, 0, 1}, /* green */
815 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
816 {0 , 0 , 0.66, 1}, /* blue */
817 {0.66, 0 , 0.66, 1}, /* magenta */
818 {0, 0.66, 0.66, 1}, /* cyan */
819 {0.66, 0.66, 0.66, 1}, /* light grey */
820 {0.22, 0.33, 0.33, 1}, /* dark grey */
821 {1, 0.33, 0.33, 1}, /* high red */
822 {0.33, 1, 0.33, 1}, /* high green */
823 {1, 1, 0.33, 1}, /* high yellow */
824 {0.33, 0.33, 1, 1}, /* high blue */
825 {1, 0.33, 1, 1}, /* high magenta */
826 {0.33, 1, 1, 1}, /* high cyan */
827 {1, 1, 1, 1} /* white */
828 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500829 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000830 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
831};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400832
Kristian Høgsberg22106762008-12-08 13:50:07 -0500833static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500834terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
835{
836 cairo_set_source_rgba(cr,
837 terminal->color_table[index].r,
838 terminal->color_table[index].g,
839 terminal->color_table[index].b,
840 terminal->color_table[index].a);
841}
842
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500843static void
844terminal_send_selection(struct terminal *terminal, int fd)
845{
846 int row, col;
847 union utf8_char *p_row;
848 union decoded_attr attr;
849 FILE *fp;
850 int len;
851
852 fp = fdopen(fd, "w");
853 for (row = 0; row < terminal->height; row++) {
854 p_row = terminal_get_row(terminal, row);
855 for (col = 0; col < terminal->width; col++) {
856 /* get the attributes for this character cell */
857 terminal_decode_attr(terminal, row, col, &attr);
858 if (!attr.attr.s)
859 continue;
860 len = strnlen((char *) p_row[col].byte, 4);
861 fwrite(p_row[col].byte, 1, len, fp);
862 }
863 }
864 fclose(fp);
865}
866
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500867struct glyph_run {
868 struct terminal *terminal;
869 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400870 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500871 union decoded_attr attr;
872 cairo_glyph_t glyphs[256], *g;
873};
874
875static void
876glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
877{
878 run->terminal = terminal;
879 run->cr = cr;
880 run->g = run->glyphs;
881 run->count = 0;
882 run->attr.key = 0;
883}
884
885static void
886glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
887{
888 cairo_scaled_font_t *font;
889
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500890 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
891 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300892 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500893 font = run->terminal->font_bold;
894 else
895 font = run->terminal->font_normal;
896 cairo_set_scaled_font(run->cr, font);
897 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300898 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500899
Callum Lowcay9d708b02011-01-12 20:06:17 +1300900 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
901 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500902 run->g = run->glyphs;
903 run->count = 0;
904 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300905 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500906}
907
908static void
909glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
910{
911 int num_glyphs;
912 cairo_scaled_font_t *font;
913
914 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
915
Callum Lowcay9d708b02011-01-12 20:06:17 +1300916 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500917 font = run->terminal->font_bold;
918 else
919 font = run->terminal->font_normal;
920
921 cairo_move_to(run->cr, x, y);
922 cairo_scaled_font_text_to_glyphs (font, x, y,
923 (char *) c->byte, 4,
924 &run->g, &num_glyphs,
925 NULL, NULL, NULL);
926 run->g += num_glyphs;
927 run->count += num_glyphs;
928}
929
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500930
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500931static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500932redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500933{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500934 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500935 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500936 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000937 int top_margin, side_margin;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600938 int row, col, cursor_x, cursor_y;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000939 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500940 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000941 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500942 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500943 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500944 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500945 cairo_font_extents_t extents;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500946
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500947 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500948 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500949 cr = cairo_create(surface);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500950 cairo_rectangle(cr, allocation.x, allocation.y,
951 allocation.width, allocation.height);
952 cairo_clip(cr);
953 cairo_push_group(cr);
954
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500955 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500956 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500957 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500958
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500959 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500960
Kristian Høgsberg59826582011-01-20 11:56:57 -0500961 extents = terminal->extents;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500962 side_margin = (allocation.width - terminal->width * extents.max_x_advance) / 2;
963 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500964
Callum Lowcay30eeae52011-01-07 19:46:55 +0000965 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500966 cairo_translate(cr, allocation.x + side_margin,
967 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500968 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000969 for (row = 0; row < terminal->height; row++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000970 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000971 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500972 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000973
Callum Lowcay9d708b02011-01-12 20:06:17 +1300974 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -0500975 continue;
976
Callum Lowcay9d708b02011-01-12 20:06:17 +1300977 terminal_set_color(terminal, cr, attr.attr.bg);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500978 cairo_move_to(cr, col * extents.max_x_advance,
979 row * extents.height);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000980 cairo_rel_line_to(cr, extents.max_x_advance, 0);
981 cairo_rel_line_to(cr, 0, extents.height);
982 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
983 cairo_close_path(cr);
984 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500985 }
986 }
Callum Lowcay30eeae52011-01-07 19:46:55 +0000987
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500988 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
989
990 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500991 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500992 for (row = 0; row < terminal->height; row++) {
993 p_row = terminal_get_row(terminal, row);
994 for (col = 0; col < terminal->width; col++) {
995 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500996 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500997
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500998 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000999
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001000 text_x = col * extents.max_x_advance;
1001 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +13001002 if (attr.attr.a & ATTRMASK_UNDERLINE) {
1003 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +00001004 cairo_move_to(cr, text_x, (double)text_y + 1.5);
1005 cairo_line_to(cr, text_x + extents.max_x_advance, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001006 cairo_stroke(cr);
1007 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -05001008
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001009 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001010 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001011 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001012
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001013 attr.key = ~0;
1014 glyph_run_flush(&run, attr);
1015
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001016 if ((terminal->mode & MODE_SHOW_CURSOR) && !terminal->focused) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001017 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001018
Callum Lowcay30eeae52011-01-07 19:46:55 +00001019 cairo_set_line_width(cr, 1);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001020 cairo_move_to(cr, terminal->column * extents.max_x_advance + d,
1021 terminal->row * extents.height + d);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001022 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
1023 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
1024 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
1025 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001026
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001027 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001028 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001029
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001030 cairo_pop_group_to_source(cr);
1031 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001032 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001033 cairo_surface_destroy(surface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001034
1035 if (terminal->send_cursor_position) {
1036 cursor_x = side_margin + allocation.x +
1037 terminal->column * extents.max_x_advance;
1038 cursor_y = top_margin + allocation.y +
1039 terminal->row * extents.height;
1040 window_set_text_cursor_position(terminal->window,
1041 cursor_x, cursor_y);
1042 terminal->send_cursor_position = 0;
1043 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001044}
1045
1046static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001047terminal_write(struct terminal *terminal, const char *data, size_t length)
1048{
1049 if (write(terminal->master, data, length) < 0)
1050 abort();
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001051 terminal->send_cursor_position = 1;
Kristian Høgsberg26130862011-08-24 11:30:21 -04001052}
1053
1054static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001055terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001056
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001057static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001058handle_char(struct terminal *terminal, union utf8_char utf8);
1059
1060static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001061handle_sgr(struct terminal *terminal, int code);
1062
1063static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001064handle_term_parameter(struct terminal *terminal, int code, int sr)
1065{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001066 int i;
1067
Callum Lowcay67a201d2011-01-12 19:23:41 +13001068 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001069 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001070 case 1: /* DECCKM */
1071 if (sr) terminal->key_mode = KM_APPLICATION;
1072 else terminal->key_mode = KM_NORMAL;
1073 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001074 case 2: /* DECANM */
1075 /* No VT52 support yet */
1076 terminal->g0 = CS_US;
1077 terminal->g1 = CS_US;
1078 terminal->cs = terminal->g0;
1079 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001080 case 3: /* DECCOLM */
1081 if (sr)
1082 terminal_resize(terminal, 132, 24);
1083 else
1084 terminal_resize(terminal, 80, 24);
1085
1086 /* set columns, but also home cursor and clear screen */
1087 terminal->row = 0; terminal->column = 0;
1088 for (i = 0; i < terminal->height; i++) {
1089 memset(terminal_get_row(terminal, i),
1090 0, terminal->data_pitch);
1091 attr_init(terminal_get_attr_row(terminal, i),
1092 terminal->curr_attr, terminal->width);
1093 }
1094 break;
1095 case 5: /* DECSCNM */
1096 if (sr) terminal->mode |= MODE_INVERSE;
1097 else terminal->mode &= ~MODE_INVERSE;
1098 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001099 case 6: /* DECOM */
1100 terminal->origin_mode = sr;
1101 if (terminal->origin_mode)
1102 terminal->row = terminal->margin_top;
1103 else
1104 terminal->row = 0;
1105 terminal->column = 0;
1106 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001107 case 7: /* DECAWM */
1108 if (sr) terminal->mode |= MODE_AUTOWRAP;
1109 else terminal->mode &= ~MODE_AUTOWRAP;
1110 break;
1111 case 8: /* DECARM */
1112 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1113 else terminal->mode &= ~MODE_AUTOREPEAT;
1114 break;
1115 case 25:
1116 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1117 else terminal->mode &= ~MODE_SHOW_CURSOR;
1118 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001119 case 1037: /* deleteSendsDel */
1120 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1121 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1122 break;
1123 case 1039: /* altSendsEscape */
1124 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1125 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1126 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001127 default:
1128 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1129 break;
1130 }
1131 } else {
1132 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001133 case 4: /* IRM */
1134 if (sr) terminal->mode |= MODE_IRM;
1135 else terminal->mode &= ~MODE_IRM;
1136 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001137 case 20: /* LNM */
1138 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1139 else terminal->mode &= ~MODE_LF_NEWLINE;
1140 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001141 default:
1142 fprintf(stderr, "Unknown parameter: %d\n", code);
1143 break;
1144 }
1145 }
1146}
1147
1148static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001149handle_dcs(struct terminal *terminal)
1150{
1151}
1152
1153static void
1154handle_osc(struct terminal *terminal)
1155{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001156 char *p;
1157 int code;
1158
1159 terminal->escape[terminal->escape_length++] = '\0';
1160 p = &terminal->escape[2];
1161 code = strtol(p, &p, 10);
1162 if (*p == ';') p++;
1163
1164 switch (code) {
1165 case 0: /* Icon name and window title */
1166 case 1: /* Icon label */
1167 case 2: /* Window title*/
1168 window_set_title(terminal->window, p);
1169 break;
1170 default:
1171 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1172 break;
1173 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001174}
1175
1176static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001177handle_escape(struct terminal *terminal)
1178{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001179 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001180 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001181 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001182 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001183 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001184 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001185 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001186
1187 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001188 i = 0;
1189 p = &terminal->escape[2];
1190 while ((isdigit(*p) || *p == ';') && i < 10) {
1191 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001192 if (!set[i]) {
1193 args[i] = 0;
1194 set[i] = 1;
1195 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001196 p++;
1197 i++;
1198 } else {
1199 args[i] = strtol(p, &p, 10);
1200 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001201 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001202 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001203
1204 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001205 case '@': /* ICH */
1206 count = set[0] ? args[0] : 1;
1207 if (count == 0) count = 1;
1208 terminal_shift_line(terminal, count);
1209 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001210 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001211 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001212 if (count == 0) count = 1;
1213 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001214 terminal->row -= count;
1215 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001216 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001217 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001218 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001219 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001220 if (count == 0) count = 1;
1221 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001222 terminal->row += count;
1223 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001224 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001225 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001226 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001227 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001228 if (count == 0) count = 1;
1229 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001230 terminal->column += count;
1231 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001232 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001233 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001234 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001235 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001236 if (count == 0) count = 1;
1237 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001238 terminal->column -= count;
1239 else
1240 terminal->column = 0;
1241 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001242 case 'E': /* CNL */
1243 count = set[0] ? args[0] : 1;
1244 if (terminal->row + count <= terminal->margin_bottom)
1245 terminal->row += count;
1246 else
1247 terminal->row = terminal->margin_bottom;
1248 terminal->column = 0;
1249 break;
1250 case 'F': /* CPL */
1251 count = set[0] ? args[0] : 1;
1252 if (terminal->row - count >= terminal->margin_top)
1253 terminal->row -= count;
1254 else
1255 terminal->row = terminal->margin_top;
1256 terminal->column = 0;
1257 break;
1258 case 'G': /* CHA */
1259 y = set[0] ? args[0] : 1;
1260 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1261
1262 terminal->column = y - 1;
1263 break;
1264 case 'f': /* HVP */
1265 case 'H': /* CUP */
1266 x = (set[1] ? args[1] : 1) - 1;
1267 x = x < 0 ? 0 :
1268 (x >= terminal->width ? terminal->width - 1 : x);
1269
1270 y = (set[0] ? args[0] : 1) - 1;
1271 if (terminal->origin_mode) {
1272 y += terminal->margin_top;
1273 y = y < terminal->margin_top ? terminal->margin_top :
1274 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1275 } else {
1276 y = y < 0 ? 0 :
1277 (y >= terminal->height ? terminal->height - 1 : y);
1278 }
1279
1280 terminal->row = y;
1281 terminal->column = x;
1282 break;
1283 case 'I': /* CHT */
1284 count = set[0] ? args[0] : 1;
1285 if (count == 0) count = 1;
1286 while (count > 0 && terminal->column < terminal->width) {
1287 if (terminal->tab_ruler[terminal->column]) count--;
1288 terminal->column++;
1289 }
1290 terminal->column--;
1291 break;
1292 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001293 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001294 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001295 if (!set[0] || args[0] == 0 || args[0] > 2) {
1296 memset(&row[terminal->column],
1297 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1298 attr_init(&attr_row[terminal->column],
1299 terminal->curr_attr, terminal->width - terminal->column);
1300 for (i = terminal->row + 1; i < terminal->height; i++) {
1301 memset(terminal_get_row(terminal, i),
1302 0, terminal->data_pitch);
1303 attr_init(terminal_get_attr_row(terminal, i),
1304 terminal->curr_attr, terminal->width);
1305 }
1306 } else if (args[0] == 1) {
1307 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1308 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1309 for (i = 0; i < terminal->row; i++) {
1310 memset(terminal_get_row(terminal, i),
1311 0, terminal->data_pitch);
1312 attr_init(terminal_get_attr_row(terminal, i),
1313 terminal->curr_attr, terminal->width);
1314 }
1315 } else if (args[0] == 2) {
1316 for (i = 0; i < terminal->height; i++) {
1317 memset(terminal_get_row(terminal, i),
1318 0, terminal->data_pitch);
1319 attr_init(terminal_get_attr_row(terminal, i),
1320 terminal->curr_attr, terminal->width);
1321 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001322 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001323 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001324 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001325 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001326 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001327 if (!set[0] || args[0] == 0 || args[0] > 2) {
1328 memset(&row[terminal->column], 0,
1329 (terminal->width - terminal->column) * sizeof(union utf8_char));
1330 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1331 terminal->width - terminal->column);
1332 } else if (args[0] == 1) {
1333 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1334 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1335 } else if (args[0] == 2) {
1336 memset(row, 0, terminal->data_pitch);
1337 attr_init(attr_row, terminal->curr_attr, terminal->width);
1338 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001339 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001340 case 'L': /* IL */
1341 count = set[0] ? args[0] : 1;
1342 if (count == 0) count = 1;
1343 if (terminal->row >= terminal->margin_top &&
1344 terminal->row < terminal->margin_bottom)
1345 {
1346 top = terminal->margin_top;
1347 terminal->margin_top = terminal->row;
1348 terminal_scroll(terminal, 0 - count);
1349 terminal->margin_top = top;
1350 } else if (terminal->row == terminal->margin_bottom) {
1351 memset(terminal_get_row(terminal, terminal->row),
1352 0, terminal->data_pitch);
1353 attr_init(terminal_get_attr_row(terminal, terminal->row),
1354 terminal->curr_attr, terminal->width);
1355 }
1356 break;
1357 case 'M': /* DL */
1358 count = set[0] ? args[0] : 1;
1359 if (count == 0) count = 1;
1360 if (terminal->row >= terminal->margin_top &&
1361 terminal->row < terminal->margin_bottom)
1362 {
1363 top = terminal->margin_top;
1364 terminal->margin_top = terminal->row;
1365 terminal_scroll(terminal, count);
1366 terminal->margin_top = top;
1367 } else if (terminal->row == terminal->margin_bottom) {
1368 memset(terminal_get_row(terminal, terminal->row),
1369 0, terminal->data_pitch);
1370 }
1371 break;
1372 case 'P': /* DCH */
1373 count = set[0] ? args[0] : 1;
1374 if (count == 0) count = 1;
1375 terminal_shift_line(terminal, 0 - count);
1376 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001377 case 'S': /* SU */
1378 terminal_scroll(terminal, set[0] ? args[0] : 1);
1379 break;
1380 case 'T': /* SD */
1381 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1382 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001383 case 'X': /* ECH */
1384 count = set[0] ? args[0] : 1;
1385 if (count == 0) count = 1;
1386 if ((terminal->column + count) > terminal->width)
1387 count = terminal->width - terminal->column;
1388 row = terminal_get_row(terminal, terminal->row);
1389 attr_row = terminal_get_attr_row(terminal, terminal->row);
1390 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1391 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1392 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001393 case 'Z': /* CBT */
1394 count = set[0] ? args[0] : 1;
1395 if (count == 0) count = 1;
1396 while (count > 0 && terminal->column >= 0) {
1397 if (terminal->tab_ruler[terminal->column]) count--;
1398 terminal->column--;
1399 }
1400 terminal->column++;
1401 break;
1402 case '`': /* HPA */
1403 y = set[0] ? args[0] : 1;
1404 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1405
1406 terminal->column = y - 1;
1407 break;
1408 case 'b': /* REP */
1409 count = set[0] ? args[0] : 1;
1410 if (count == 0) count = 1;
1411 if (terminal->last_char.byte[0])
1412 for (i = 0; i < count; i++)
1413 handle_char(terminal, terminal->last_char);
1414 terminal->last_char.byte[0] = 0;
1415 break;
1416 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001417 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001418 break;
1419 case 'd': /* VPA */
1420 x = set[0] ? args[0] : 1;
1421 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1422
1423 terminal->row = x - 1;
1424 break;
1425 case 'g': /* TBC */
1426 if (!set[0] || args[0] == 0) {
1427 terminal->tab_ruler[terminal->column] = 0;
1428 } else if (args[0] == 3) {
1429 memset(terminal->tab_ruler, 0, terminal->width);
1430 }
1431 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001432 case 'h': /* SM */
1433 for(i = 0; i < 10 && set[i]; i++) {
1434 handle_term_parameter(terminal, args[i], 1);
1435 }
1436 break;
1437 case 'l': /* RM */
1438 for(i = 0; i < 10 && set[i]; i++) {
1439 handle_term_parameter(terminal, args[i], 0);
1440 }
1441 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001442 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001443 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001444 if (i <= 7 && set[i] && set[i + 1] &&
1445 set[i + 2] && args[i + 1] == 5)
1446 {
1447 if (args[i] == 38) {
1448 handle_sgr(terminal, args[i + 2] + 256);
1449 break;
1450 } else if (args[i] == 48) {
1451 handle_sgr(terminal, args[i + 2] + 512);
1452 break;
1453 }
1454 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001455 if(set[i]) {
1456 handle_sgr(terminal, args[i]);
1457 } else if(i == 0) {
1458 handle_sgr(terminal, 0);
1459 break;
1460 } else {
1461 break;
1462 }
1463 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001464 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001465 case 'n': /* DSR */
1466 i = set[0] ? args[0] : 0;
1467 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001468 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001469 } else if (i == 6) {
1470 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1471 terminal->origin_mode ?
1472 terminal->row+terminal->margin_top : terminal->row+1,
1473 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001474 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001475 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001476 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001477 case 'r':
1478 if(!set[0]) {
1479 terminal->margin_top = 0;
1480 terminal->margin_bottom = terminal->height-1;
1481 terminal->row = 0;
1482 terminal->column = 0;
1483 } else {
1484 top = (set[0] ? args[0] : 1) - 1;
1485 top = top < 0 ? 0 :
1486 (top >= terminal->height ? terminal->height - 1 : top);
1487 bottom = (set[1] ? args[1] : 1) - 1;
1488 bottom = bottom < 0 ? 0 :
1489 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1490 if(bottom > top) {
1491 terminal->margin_top = top;
1492 terminal->margin_bottom = bottom;
1493 } else {
1494 terminal->margin_top = 0;
1495 terminal->margin_bottom = terminal->height-1;
1496 }
1497 if(terminal->origin_mode)
1498 terminal->row = terminal->margin_top;
1499 else
1500 terminal->row = 0;
1501 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001502 }
1503 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001504 case 's':
1505 terminal->saved_row = terminal->row;
1506 terminal->saved_column = terminal->column;
1507 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001508 case 't': /* windowOps */
1509 if (!set[0]) break;
1510 switch (args[0]) {
1511 case 4: /* resize px */
1512 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001513 widget_schedule_resize(terminal->widget,
1514 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001515 }
1516 break;
1517 case 8: /* resize ch */
1518 if (set[1] && set[2]) {
1519 terminal_resize(terminal, args[2], args[1]);
1520 }
1521 break;
1522 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001523 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001524 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1525 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001526 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001527 break;
1528 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001529 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001530 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1531 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001532 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001533 break;
1534 case 18: /* report ch */
1535 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1536 terminal->height, terminal->width);
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 21: /* report title */
1540 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1541 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001542 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001543 break;
1544 default:
1545 if (args[0] >= 24)
1546 terminal_resize(terminal, terminal->width, args[0]);
1547 else
1548 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1549 break;
1550 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001551 case 'u':
1552 terminal->row = terminal->saved_row;
1553 terminal->column = terminal->saved_column;
1554 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001555 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001556 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001557 break;
1558 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001559}
1560
1561static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001562handle_non_csi_escape(struct terminal *terminal, char code)
1563{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001564 switch(code) {
1565 case 'M': /* RI */
1566 terminal->row -= 1;
1567 if(terminal->row < terminal->margin_top) {
1568 terminal->row = terminal->margin_top;
1569 terminal_scroll(terminal, -1);
1570 }
1571 break;
1572 case 'E': /* NEL */
1573 terminal->column = 0;
1574 // fallthrough
1575 case 'D': /* IND */
1576 terminal->row += 1;
1577 if(terminal->row > terminal->margin_bottom) {
1578 terminal->row = terminal->margin_bottom;
1579 terminal_scroll(terminal, +1);
1580 }
1581 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001582 case 'c': /* RIS */
1583 terminal_init(terminal);
1584 break;
1585 case 'H': /* HTS */
1586 terminal->tab_ruler[terminal->column] = 1;
1587 break;
1588 case '7': /* DECSC */
1589 terminal->saved_row = terminal->row;
1590 terminal->saved_column = terminal->column;
1591 terminal->saved_attr = terminal->curr_attr;
1592 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001593 terminal->saved_cs = terminal->cs;
1594 terminal->saved_g0 = terminal->g0;
1595 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001596 break;
1597 case '8': /* DECRC */
1598 terminal->row = terminal->saved_row;
1599 terminal->column = terminal->saved_column;
1600 terminal->curr_attr = terminal->saved_attr;
1601 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001602 terminal->cs = terminal->saved_cs;
1603 terminal->g0 = terminal->saved_g0;
1604 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001605 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001606 case '=': /* DECPAM */
1607 terminal->key_mode = KM_APPLICATION;
1608 break;
1609 case '>': /* DECPNM */
1610 terminal->key_mode = KM_NORMAL;
1611 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001612 default:
1613 fprintf(stderr, "Unknown escape code: %c\n", code);
1614 break;
1615 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001616}
1617
1618static void
1619handle_special_escape(struct terminal *terminal, char special, char code)
1620{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001621 int i, numChars;
1622
1623 if (special == '#') {
1624 switch(code) {
1625 case '8':
1626 /* fill with 'E', no cheap way to do this */
1627 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1628 numChars = terminal->width * terminal->height;
1629 for(i = 0; i < numChars; i++) {
1630 terminal->data[i].byte[0] = 'E';
1631 }
1632 break;
1633 default:
1634 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1635 break;
1636 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001637 } else if (special == '(' || special == ')') {
1638 switch(code) {
1639 case '0':
1640 if (special == '(')
1641 terminal->g0 = CS_SPECIAL;
1642 else
1643 terminal->g1 = CS_SPECIAL;
1644 break;
1645 case 'A':
1646 if (special == '(')
1647 terminal->g0 = CS_UK;
1648 else
1649 terminal->g1 = CS_UK;
1650 break;
1651 case 'B':
1652 if (special == '(')
1653 terminal->g0 = CS_US;
1654 else
1655 terminal->g1 = CS_US;
1656 break;
1657 default:
1658 fprintf(stderr, "Unknown character set %c\n", code);
1659 break;
1660 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001661 } else {
1662 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1663 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001664}
1665
1666static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001667handle_sgr(struct terminal *terminal, int code)
1668{
1669 switch(code) {
1670 case 0:
1671 terminal->curr_attr = terminal->color_scheme->default_attr;
1672 break;
1673 case 1:
1674 terminal->curr_attr.a |= ATTRMASK_BOLD;
1675 if (terminal->curr_attr.fg < 8)
1676 terminal->curr_attr.fg += 8;
1677 break;
1678 case 4:
1679 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1680 break;
1681 case 5:
1682 terminal->curr_attr.a |= ATTRMASK_BLINK;
1683 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001684 case 8:
1685 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1686 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001687 case 2:
1688 case 21:
1689 case 22:
1690 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1691 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1692 terminal->curr_attr.fg -= 8;
1693 break;
1694 case 24:
1695 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1696 break;
1697 case 25:
1698 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1699 break;
1700 case 7:
1701 case 26:
1702 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1703 break;
1704 case 27:
1705 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1706 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001707 case 28:
1708 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1709 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001710 case 39:
1711 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1712 break;
1713 case 49:
1714 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1715 break;
1716 default:
1717 if(code >= 30 && code <= 37) {
1718 terminal->curr_attr.fg = code - 30;
1719 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1720 terminal->curr_attr.fg += 8;
1721 } else if(code >= 40 && code <= 47) {
1722 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001723 } else if (code >= 90 && code <= 97) {
1724 terminal->curr_attr.fg = code - 90 + 8;
1725 } else if (code >= 100 && code <= 107) {
1726 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001727 } else if(code >= 256 && code < 512) {
1728 terminal->curr_attr.fg = code - 256;
1729 } else if(code >= 512 && code < 768) {
1730 terminal->curr_attr.bg = code - 512;
1731 } else {
1732 fprintf(stderr, "Unknown SGR code: %d\n", code);
1733 }
1734 break;
1735 }
1736}
1737
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001738/* Returns 1 if c was special, otherwise 0 */
1739static int
1740handle_special_char(struct terminal *terminal, char c)
1741{
1742 union utf8_char *row;
1743 struct attr *attr_row;
1744
1745 row = terminal_get_row(terminal, terminal->row);
1746 attr_row = terminal_get_attr_row(terminal, terminal->row);
1747
1748 switch(c) {
1749 case '\r':
1750 terminal->column = 0;
1751 break;
1752 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001753 if (terminal->mode & MODE_LF_NEWLINE) {
1754 terminal->column = 0;
1755 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001756 /* fallthrough */
1757 case '\v':
1758 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001759 terminal->row++;
1760 if(terminal->row > terminal->margin_bottom) {
1761 terminal->row = terminal->margin_bottom;
1762 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001763 }
1764
1765 break;
1766 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001767 while (terminal->column < terminal->width) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001768 if (terminal->mode & MODE_IRM)
1769 terminal_shift_line(terminal, +1);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001770 row[terminal->column].byte[0] = ' ';
1771 row[terminal->column].byte[1] = '\0';
1772 attr_row[terminal->column] = terminal->curr_attr;
1773 terminal->column++;
Kristian Høgsbergcca3c2f2012-06-20 15:56:13 -04001774 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001775 }
1776 if (terminal->column >= terminal->width) {
1777 terminal->column = terminal->width - 1;
1778 }
1779
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001780 break;
1781 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001782 if (terminal->column >= terminal->width) {
1783 terminal->column = terminal->width - 2;
1784 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001785 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001786 } else if (terminal->mode & MODE_AUTOWRAP) {
1787 terminal->column = terminal->width - 1;
1788 terminal->row -= 1;
1789 if (terminal->row < terminal->margin_top) {
1790 terminal->row = terminal->margin_top;
1791 terminal_scroll(terminal, -1);
1792 }
1793 }
1794
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001795 break;
1796 case '\a':
1797 /* Bell */
1798 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001799 case '\x0E': /* SO */
1800 terminal->cs = terminal->g1;
1801 break;
1802 case '\x0F': /* SI */
1803 terminal->cs = terminal->g0;
1804 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001805 default:
1806 return 0;
1807 }
1808
1809 return 1;
1810}
1811
1812static void
1813handle_char(struct terminal *terminal, union utf8_char utf8)
1814{
1815 union utf8_char *row;
1816 struct attr *attr_row;
1817
1818 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001819
1820 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001821
1822 /* There are a whole lot of non-characters, control codes,
1823 * and formatting codes that should probably be ignored,
1824 * for example: */
1825 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1826 /* BOM, ignore */
1827 return;
1828 }
1829
1830 /* Some of these non-characters should be translated, e.g.: */
1831 if (utf8.byte[0] < 32) {
1832 utf8.byte[0] = utf8.byte[0] + 64;
1833 }
1834
1835 /* handle right margin effects */
1836 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001837 if (terminal->mode & MODE_AUTOWRAP) {
1838 terminal->column = 0;
1839 terminal->row += 1;
1840 if (terminal->row > terminal->margin_bottom) {
1841 terminal->row = terminal->margin_bottom;
1842 terminal_scroll(terminal, +1);
1843 }
1844 } else {
1845 terminal->column--;
1846 }
1847 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001848
1849 row = terminal_get_row(terminal, terminal->row);
1850 attr_row = terminal_get_attr_row(terminal, terminal->row);
1851
Callum Lowcay69e96582011-01-07 19:47:00 +00001852 if (terminal->mode & MODE_IRM)
1853 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001854 row[terminal->column] = utf8;
1855 attr_row[terminal->column++] = terminal->curr_attr;
1856
1857 if (utf8.ch != terminal->last_char.ch)
1858 terminal->last_char = utf8;
1859}
1860
Callum Lowcay30eeae52011-01-07 19:46:55 +00001861static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001862escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1863{
1864 int len, i;
1865
1866 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1867 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1868 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1869 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1870 else len = 1; /* Invalid, cannot happen */
1871
1872 if (terminal->escape_length + len <= MAX_ESCAPE) {
1873 for (i = 0; i < len; i++)
1874 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1875 terminal->escape_length += len;
1876 } else if (terminal->escape_length < MAX_ESCAPE) {
1877 terminal->escape[terminal->escape_length++] = 0;
1878 }
1879}
1880
1881static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001882terminal_data(struct terminal *terminal, const char *data, size_t length)
1883{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001884 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001885 union utf8_char utf8;
1886 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001887
1888 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001889 parser_state =
1890 utf8_next_char(&terminal->state_machine, data[i]);
1891 switch(parser_state) {
1892 case utf8state_accept:
1893 utf8.ch = terminal->state_machine.s.ch;
1894 break;
1895 case utf8state_reject:
1896 /* the unicode replacement character */
1897 utf8.byte[0] = 0xEF;
1898 utf8.byte[1] = 0xBF;
1899 utf8.byte[2] = 0xBD;
1900 utf8.byte[3] = 0x00;
1901 break;
1902 default:
1903 continue;
1904 }
1905
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001906 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001907 switch (terminal->state) {
1908 case escape_state_escape:
1909 escape_append_utf8(terminal, utf8);
1910 switch (utf8.byte[0]) {
1911 case 'P': /* DCS */
1912 terminal->state = escape_state_dcs;
1913 break;
1914 case '[': /* CSI */
1915 terminal->state = escape_state_csi;
1916 break;
1917 case ']': /* OSC */
1918 terminal->state = escape_state_osc;
1919 break;
1920 case '#':
1921 case '(':
1922 case ')': /* special */
1923 terminal->state = escape_state_special;
1924 break;
1925 case '^': /* PM (not implemented) */
1926 case '_': /* APC (not implemented) */
1927 terminal->state = escape_state_ignore;
1928 break;
1929 default:
1930 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001931 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13001932 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001933 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001934 continue;
1935 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001936 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1937 /* do nothing */
1938 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001939 terminal->escape_flags |= ESC_FLAG_WHAT;
1940 } else if (utf8.byte[0] == '>') {
1941 terminal->escape_flags |= ESC_FLAG_GT;
1942 } else if (utf8.byte[0] == '!') {
1943 terminal->escape_flags |= ESC_FLAG_BANG;
1944 } else if (utf8.byte[0] == '$') {
1945 terminal->escape_flags |= ESC_FLAG_CASH;
1946 } else if (utf8.byte[0] == '\'') {
1947 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1948 } else if (utf8.byte[0] == '"') {
1949 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1950 } else if (utf8.byte[0] == ' ') {
1951 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001952 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001953 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001954 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13001955 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001956 }
1957
1958 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1959 utf8.byte[0] == '`')
1960 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001961 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001962 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001963 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001964 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001965 continue;
1966 case escape_state_inner_escape:
1967 if (utf8.byte[0] == '\\') {
1968 terminal->state = escape_state_normal;
1969 if (terminal->outer_state == escape_state_dcs) {
1970 handle_dcs(terminal);
1971 } else if (terminal->outer_state == escape_state_osc) {
1972 handle_osc(terminal);
1973 }
1974 } else if (utf8.byte[0] == '\e') {
1975 terminal->state = terminal->outer_state;
1976 escape_append_utf8(terminal, utf8);
1977 if (terminal->escape_length >= MAX_ESCAPE)
1978 terminal->state = escape_state_normal;
1979 } else {
1980 terminal->state = terminal->outer_state;
1981 if (terminal->escape_length < MAX_ESCAPE)
1982 terminal->escape[terminal->escape_length++] = '\e';
1983 escape_append_utf8(terminal, utf8);
1984 if (terminal->escape_length >= MAX_ESCAPE)
1985 terminal->state = escape_state_normal;
1986 }
1987 continue;
1988 case escape_state_dcs:
1989 case escape_state_osc:
1990 case escape_state_ignore:
1991 if (utf8.byte[0] == '\e') {
1992 terminal->outer_state = terminal->state;
1993 terminal->state = escape_state_inner_escape;
1994 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
1995 terminal->state = escape_state_normal;
1996 handle_osc(terminal);
1997 } else {
1998 escape_append_utf8(terminal, utf8);
1999 if (terminal->escape_length >= MAX_ESCAPE)
2000 terminal->state = escape_state_normal;
2001 }
2002 continue;
2003 case escape_state_special:
2004 escape_append_utf8(terminal, utf8);
2005 terminal->state = escape_state_normal;
2006 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2007 handle_special_escape(terminal, terminal->escape[1],
2008 utf8.byte[0]);
2009 }
2010 continue;
2011 default:
2012 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002013 }
2014
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002015 /* this is valid, because ASCII characters are never used to
2016 * introduce a multibyte sequence in UTF-8 */
2017 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002018 terminal->state = escape_state_escape;
2019 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002020 terminal->escape[0] = '\e';
2021 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002022 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002023 } else {
2024 handle_char(terminal, utf8);
2025 } /* if */
2026 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002027
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002028 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002029}
2030
2031static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002032data_source_target(void *data,
2033 struct wl_data_source *source, const char *mime_type)
2034{
2035 fprintf(stderr, "data_source_target, %s\n", mime_type);
2036}
2037
2038static void
2039data_source_send(void *data,
2040 struct wl_data_source *source,
2041 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002042{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002043 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002044
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002045 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002046}
2047
2048static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002049data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002050{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002051 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002052}
2053
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002054static const struct wl_data_source_listener data_source_listener = {
2055 data_source_target,
2056 data_source_send,
2057 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002058};
2059
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002060static int
2061handle_bound_key(struct terminal *terminal,
2062 struct input *input, uint32_t sym, uint32_t time)
2063{
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002064 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002065 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002066 /* Cut selection; terminal doesn't do cut, fall
2067 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002068 case XKB_KEY_C:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002069 terminal->selection =
2070 display_create_data_source(terminal->display);
2071 wl_data_source_offer(terminal->selection,
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002072 "text/plain;charset=utf-8");
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002073 wl_data_source_add_listener(terminal->selection,
2074 &data_source_listener, terminal);
Kristian Høgsbergfee91be2012-06-02 21:21:41 -04002075 input_set_selection(input, terminal->selection,
2076 display_get_serial(terminal->display));
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002077 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002078 case XKB_KEY_V:
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002079 input_receive_selection_data_to_fd(input,
2080 "text/plain;charset=utf-8",
2081 terminal->master);
2082
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002083 return 1;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002084 default:
2085 return 0;
2086 }
2087}
2088
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002089static void
2090key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002091 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2092 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002093{
2094 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002095 char ch[MAX_RESPONSE];
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002096 uint32_t modifiers;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002097 int len = 0;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002098
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002099 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002100 if ((modifiers & MOD_CONTROL_MASK) &&
2101 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002102 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2103 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002104 return;
2105
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002106 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002107 case XKB_KEY_F11:
Daniel Stonec9785ea2012-05-30 16:31:52 +01002108 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002109 break;
2110 terminal->fullscreen ^= 1;
2111 window_set_fullscreen(window, terminal->fullscreen);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002112 break;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002113
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002114 case XKB_KEY_BackSpace:
2115 case XKB_KEY_Tab:
2116 case XKB_KEY_Linefeed:
2117 case XKB_KEY_Clear:
2118 case XKB_KEY_Pause:
2119 case XKB_KEY_Scroll_Lock:
2120 case XKB_KEY_Sys_Req:
2121 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002122 ch[len++] = sym & 0x7f;
2123 break;
2124
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002125 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002126 if (terminal->mode & MODE_LF_NEWLINE) {
2127 ch[len++] = 0x0D;
2128 ch[len++] = 0x0A;
2129 } else {
2130 ch[len++] = 0x0D;
2131 }
2132 break;
2133
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002134 case XKB_KEY_Shift_L:
2135 case XKB_KEY_Shift_R:
2136 case XKB_KEY_Control_L:
2137 case XKB_KEY_Control_R:
2138 case XKB_KEY_Alt_L:
2139 case XKB_KEY_Alt_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002140 break;
2141
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002142 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002143 len = function_key_response('[', 2, modifiers, '~', ch);
2144 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002145 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002146 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2147 ch[len++] = '\x04';
2148 } else {
2149 len = function_key_response('[', 3, modifiers, '~', ch);
2150 }
2151 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002152 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002153 len = function_key_response('[', 5, modifiers, '~', ch);
2154 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002155 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002156 len = function_key_response('[', 6, modifiers, '~', ch);
2157 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002158 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002159 len = function_key_response('O', 1, modifiers, 'P', ch);
2160 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002161 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002162 len = function_key_response('O', 1, modifiers, 'Q', ch);
2163 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002164 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002165 len = function_key_response('O', 1, modifiers, 'R', ch);
2166 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002167 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002168 len = function_key_response('O', 1, modifiers, 'S', ch);
2169 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002170 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002171 len = function_key_response('[', 15, modifiers, '~', ch);
2172 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002173 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002174 len = function_key_response('[', 17, modifiers, '~', ch);
2175 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002176 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002177 len = function_key_response('[', 18, modifiers, '~', ch);
2178 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002179 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002180 len = function_key_response('[', 19, modifiers, '~', ch);
2181 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002182 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002183 len = function_key_response('[', 20, modifiers, '~', ch);
2184 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002185 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002186 len = function_key_response('[', 21, modifiers, '~', ch);
2187 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002188 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002189 len = function_key_response('[', 24, modifiers, '~', ch);
2190 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002191 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002192 /* Handle special keys with alternate mappings */
2193 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2194 if (len != 0) break;
2195
Kristian Høgsberg70163132012-05-08 15:55:39 -04002196 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002197 if (sym >= '3' && sym <= '7')
2198 sym = (sym & 0x1f) + 8;
2199
2200 if (!((sym >= '!' && sym <= '/') ||
2201 (sym >= '8' && sym <= '?') ||
2202 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2203 else if (sym == '2') sym = 0x00;
2204 else if (sym == '/') sym = 0x1F;
2205 else if (sym == '8' || sym == '?') sym = 0x7F;
2206 } else if ((terminal->mode & MODE_ALT_SENDS_ESC) &&
Kristian Høgsberg70163132012-05-08 15:55:39 -04002207 (modifiers & MOD_ALT_MASK))
Callum Lowcay7e08e902011-01-07 19:47:02 +00002208 {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002209 ch[len++] = 0x1b;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002210 } else if (modifiers & MOD_ALT_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002211 sym = sym | 0x80;
2212 }
2213
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002214 if (sym < 256)
2215 ch[len++] = sym;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002216 break;
2217 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002218
Daniel Stonec9785ea2012-05-30 16:31:52 +01002219 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0)
Kristian Høgsberg26130862011-08-24 11:30:21 -04002220 terminal_write(terminal, ch, len);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002221}
2222
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002223static void
2224keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002225 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002226{
2227 struct terminal *terminal = data;
2228
2229 terminal->focused = (device != NULL);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002230 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002231}
2232
Kristian Høgsberg59826582011-01-20 11:56:57 -05002233static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002234button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002235 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002236 uint32_t button,
2237 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002238{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002239 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002240
2241 switch (button) {
2242 case 272:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002243 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002244 terminal->dragging = 1;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002245 input_get_position(input,
2246 &terminal->selection_start_x,
2247 &terminal->selection_start_y);
2248 terminal->selection_end_x = terminal->selection_start_x;
2249 terminal->selection_end_y = terminal->selection_start_y;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002250 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002251 } else {
2252 terminal->dragging = 0;
2253 }
2254 break;
2255 }
2256}
2257
2258static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002259motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002260 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002261 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002262{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002263 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002264
2265 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002266 input_get_position(input,
2267 &terminal->selection_end_x,
2268 &terminal->selection_end_y);
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002269 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002270 }
2271
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002272 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002273}
2274
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002275static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002276terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002277{
2278 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002279 cairo_surface_t *surface;
2280 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002281
2282 terminal = malloc(sizeof *terminal);
2283 if (terminal == NULL)
2284 return terminal;
2285
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002286 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002287 terminal->fullscreen = fullscreen;
Callum Lowcay30eeae52011-01-07 19:46:55 +00002288 terminal->color_scheme = &DEFAULT_COLORS;
2289 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002290 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002291 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002292 terminal->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002293 terminal->widget = frame_create(terminal->window, terminal);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002294 window_set_title(terminal->window, "Wayland Terminal");
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002295 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002296
2297 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002298 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002299
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002300 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002301 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002302
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002303 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002304 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002305 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002306 keyboard_focus_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002307 widget_set_redraw_handler(terminal->widget, redraw_handler);
2308 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002309 widget_set_button_handler(terminal->widget, button_handler);
2310 widget_set_motion_handler(terminal->widget, motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002311
Kristian Høgsberg09531622010-06-14 23:22:15 -04002312 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2313 cr = cairo_create(surface);
Kristian Høgsberg09531622010-06-14 23:22:15 -04002314 cairo_set_font_size(cr, 14);
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002315 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002316 CAIRO_FONT_SLANT_NORMAL,
2317 CAIRO_FONT_WEIGHT_BOLD);
2318 terminal->font_bold = cairo_get_scaled_font (cr);
2319 cairo_scaled_font_reference(terminal->font_bold);
2320
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002321 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002322 CAIRO_FONT_SLANT_NORMAL,
2323 CAIRO_FONT_WEIGHT_NORMAL);
2324 terminal->font_normal = cairo_get_scaled_font (cr);
2325 cairo_scaled_font_reference(terminal->font_normal);
2326
Kristian Høgsberg09531622010-06-14 23:22:15 -04002327 cairo_font_extents(cr, &terminal->extents);
2328 cairo_destroy(cr);
2329 cairo_surface_destroy(surface);
2330
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002331 window_schedule_resize(terminal->window, 500, 400);
2332
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002333 return terminal;
2334}
2335
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002336static void
2337io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002338{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002339 struct terminal *terminal =
2340 container_of(task, struct terminal, io_task);
2341 char buffer[256];
2342 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002343
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002344 if (events & EPOLLHUP)
2345 exit(0);
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002346
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002347 len = read(terminal->master, buffer, sizeof buffer);
2348 if (len < 0)
2349 exit(0);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002350
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002351 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002352}
2353
2354static int
2355terminal_run(struct terminal *terminal, const char *path)
2356{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002357 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002358 pid_t pid;
2359
2360 pid = forkpty(&master, NULL, NULL, NULL);
2361 if (pid == 0) {
Callum Lowcay86653ed2011-01-07 19:47:03 +00002362 setenv("TERM", "xterm-256color", 1);
2363 setenv("COLORTERM", "xterm-256color", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002364 if (execl(path, path, NULL)) {
2365 printf("exec failed: %m\n");
2366 exit(EXIT_FAILURE);
2367 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002368 } else if (pid < 0) {
2369 fprintf(stderr, "failed to fork and create pty (%m).\n");
2370 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002371 }
2372
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002373 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002374 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002375 terminal->io_task.run = io_handler;
2376 display_watch_fd(terminal->display, terminal->master,
2377 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002378
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002379 window_set_fullscreen(terminal->window, terminal->fullscreen);
2380 if (!terminal->fullscreen)
2381 terminal_resize(terminal, 80, 24);
2382
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002383 return 0;
2384}
2385
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002386static const struct config_key terminal_config_keys[] = {
2387 { "font", CONFIG_KEY_STRING, &option_font },
2388};
2389
2390static const struct config_section config_sections[] = {
2391 { "terminal",
2392 terminal_config_keys, ARRAY_LENGTH(terminal_config_keys) },
2393};
2394
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002395static const struct weston_option terminal_options[] = {
2396 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002397 { WESTON_OPTION_STRING, "font", 0, &option_font },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002398};
2399
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002400int main(int argc, char *argv[])
2401{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002402 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002403 struct terminal *terminal;
Peter Hutterer035ac942012-02-03 20:58:19 +10002404 const char *shell;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002405 char *config_file;
2406
2407 config_file = config_file_path("weston.ini");
2408 parse_config_file(config_file,
2409 config_sections, ARRAY_LENGTH(config_sections),
2410 NULL);
2411 free(config_file);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002412
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002413 argc = parse_options(terminal_options,
2414 ARRAY_LENGTH(terminal_options), argc, argv);
2415
2416 d = display_create(argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002417 if (d == NULL) {
2418 fprintf(stderr, "failed to create display: %m\n");
2419 return -1;
2420 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002421
Peter Hutterer035ac942012-02-03 20:58:19 +10002422 shell = getenv("SHELL");
2423 if (!shell)
2424 shell = "/bin/bash";
2425
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002426 terminal = terminal_create(d, option_fullscreen);
Peter Hutterer035ac942012-02-03 20:58:19 +10002427 if (terminal_run(terminal, shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002428 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002429
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002430 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002431
2432 return 0;
2433}