blob: 409a148e36f82747277314b05b92f4eb135a2c5c [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;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001115 case 12: /* Very visible cursor (CVVIS) */
1116 /* FIXME: What do we do here. */
1117 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001118 case 25:
1119 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1120 else terminal->mode &= ~MODE_SHOW_CURSOR;
1121 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001122 case 1034: /* smm/rmm, meta mode on/off */
1123 /* ignore */
1124 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001125 case 1037: /* deleteSendsDel */
1126 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1127 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1128 break;
1129 case 1039: /* altSendsEscape */
1130 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1131 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1132 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001133 case 1049: /* rmcup/smcup, alternate screen */
1134 /* Ignore. Should be possible to implement,
1135 * but it's kind of annoying. */
1136 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001137 default:
1138 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1139 break;
1140 }
1141 } else {
1142 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001143 case 4: /* IRM */
1144 if (sr) terminal->mode |= MODE_IRM;
1145 else terminal->mode &= ~MODE_IRM;
1146 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001147 case 20: /* LNM */
1148 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1149 else terminal->mode &= ~MODE_LF_NEWLINE;
1150 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001151 default:
1152 fprintf(stderr, "Unknown parameter: %d\n", code);
1153 break;
1154 }
1155 }
1156}
1157
1158static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001159handle_dcs(struct terminal *terminal)
1160{
1161}
1162
1163static void
1164handle_osc(struct terminal *terminal)
1165{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001166 char *p;
1167 int code;
1168
1169 terminal->escape[terminal->escape_length++] = '\0';
1170 p = &terminal->escape[2];
1171 code = strtol(p, &p, 10);
1172 if (*p == ';') p++;
1173
1174 switch (code) {
1175 case 0: /* Icon name and window title */
1176 case 1: /* Icon label */
1177 case 2: /* Window title*/
1178 window_set_title(terminal->window, p);
1179 break;
1180 default:
1181 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1182 break;
1183 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001184}
1185
1186static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001187handle_escape(struct terminal *terminal)
1188{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001189 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001190 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001191 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001192 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001193 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001194 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001195 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001196
1197 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001198 i = 0;
1199 p = &terminal->escape[2];
1200 while ((isdigit(*p) || *p == ';') && i < 10) {
1201 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001202 if (!set[i]) {
1203 args[i] = 0;
1204 set[i] = 1;
1205 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001206 p++;
1207 i++;
1208 } else {
1209 args[i] = strtol(p, &p, 10);
1210 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001211 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001212 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001213
1214 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001215 case '@': /* ICH */
1216 count = set[0] ? args[0] : 1;
1217 if (count == 0) count = 1;
1218 terminal_shift_line(terminal, count);
1219 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001220 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001221 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001222 if (count == 0) count = 1;
1223 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001224 terminal->row -= count;
1225 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001226 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001227 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001228 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001229 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001230 if (count == 0) count = 1;
1231 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001232 terminal->row += count;
1233 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001234 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001235 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001236 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001237 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001238 if (count == 0) count = 1;
1239 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001240 terminal->column += count;
1241 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001242 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001243 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001244 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001245 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001246 if (count == 0) count = 1;
1247 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001248 terminal->column -= count;
1249 else
1250 terminal->column = 0;
1251 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001252 case 'E': /* CNL */
1253 count = set[0] ? args[0] : 1;
1254 if (terminal->row + count <= terminal->margin_bottom)
1255 terminal->row += count;
1256 else
1257 terminal->row = terminal->margin_bottom;
1258 terminal->column = 0;
1259 break;
1260 case 'F': /* CPL */
1261 count = set[0] ? args[0] : 1;
1262 if (terminal->row - count >= terminal->margin_top)
1263 terminal->row -= count;
1264 else
1265 terminal->row = terminal->margin_top;
1266 terminal->column = 0;
1267 break;
1268 case 'G': /* CHA */
1269 y = set[0] ? args[0] : 1;
1270 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1271
1272 terminal->column = y - 1;
1273 break;
1274 case 'f': /* HVP */
1275 case 'H': /* CUP */
1276 x = (set[1] ? args[1] : 1) - 1;
1277 x = x < 0 ? 0 :
1278 (x >= terminal->width ? terminal->width - 1 : x);
1279
1280 y = (set[0] ? args[0] : 1) - 1;
1281 if (terminal->origin_mode) {
1282 y += terminal->margin_top;
1283 y = y < terminal->margin_top ? terminal->margin_top :
1284 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1285 } else {
1286 y = y < 0 ? 0 :
1287 (y >= terminal->height ? terminal->height - 1 : y);
1288 }
1289
1290 terminal->row = y;
1291 terminal->column = x;
1292 break;
1293 case 'I': /* CHT */
1294 count = set[0] ? args[0] : 1;
1295 if (count == 0) count = 1;
1296 while (count > 0 && terminal->column < terminal->width) {
1297 if (terminal->tab_ruler[terminal->column]) count--;
1298 terminal->column++;
1299 }
1300 terminal->column--;
1301 break;
1302 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001303 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001304 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001305 if (!set[0] || args[0] == 0 || args[0] > 2) {
1306 memset(&row[terminal->column],
1307 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1308 attr_init(&attr_row[terminal->column],
1309 terminal->curr_attr, terminal->width - terminal->column);
1310 for (i = terminal->row + 1; i < terminal->height; i++) {
1311 memset(terminal_get_row(terminal, i),
1312 0, terminal->data_pitch);
1313 attr_init(terminal_get_attr_row(terminal, i),
1314 terminal->curr_attr, terminal->width);
1315 }
1316 } else if (args[0] == 1) {
1317 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1318 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1319 for (i = 0; i < terminal->row; i++) {
1320 memset(terminal_get_row(terminal, i),
1321 0, terminal->data_pitch);
1322 attr_init(terminal_get_attr_row(terminal, i),
1323 terminal->curr_attr, terminal->width);
1324 }
1325 } else if (args[0] == 2) {
1326 for (i = 0; i < terminal->height; i++) {
1327 memset(terminal_get_row(terminal, i),
1328 0, terminal->data_pitch);
1329 attr_init(terminal_get_attr_row(terminal, i),
1330 terminal->curr_attr, terminal->width);
1331 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001332 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001333 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001334 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001335 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001336 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001337 if (!set[0] || args[0] == 0 || args[0] > 2) {
1338 memset(&row[terminal->column], 0,
1339 (terminal->width - terminal->column) * sizeof(union utf8_char));
1340 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1341 terminal->width - terminal->column);
1342 } else if (args[0] == 1) {
1343 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1344 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1345 } else if (args[0] == 2) {
1346 memset(row, 0, terminal->data_pitch);
1347 attr_init(attr_row, terminal->curr_attr, terminal->width);
1348 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001349 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001350 case 'L': /* IL */
1351 count = set[0] ? args[0] : 1;
1352 if (count == 0) count = 1;
1353 if (terminal->row >= terminal->margin_top &&
1354 terminal->row < terminal->margin_bottom)
1355 {
1356 top = terminal->margin_top;
1357 terminal->margin_top = terminal->row;
1358 terminal_scroll(terminal, 0 - count);
1359 terminal->margin_top = top;
1360 } else if (terminal->row == terminal->margin_bottom) {
1361 memset(terminal_get_row(terminal, terminal->row),
1362 0, terminal->data_pitch);
1363 attr_init(terminal_get_attr_row(terminal, terminal->row),
1364 terminal->curr_attr, terminal->width);
1365 }
1366 break;
1367 case 'M': /* DL */
1368 count = set[0] ? args[0] : 1;
1369 if (count == 0) count = 1;
1370 if (terminal->row >= terminal->margin_top &&
1371 terminal->row < terminal->margin_bottom)
1372 {
1373 top = terminal->margin_top;
1374 terminal->margin_top = terminal->row;
1375 terminal_scroll(terminal, count);
1376 terminal->margin_top = top;
1377 } else if (terminal->row == terminal->margin_bottom) {
1378 memset(terminal_get_row(terminal, terminal->row),
1379 0, terminal->data_pitch);
1380 }
1381 break;
1382 case 'P': /* DCH */
1383 count = set[0] ? args[0] : 1;
1384 if (count == 0) count = 1;
1385 terminal_shift_line(terminal, 0 - count);
1386 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001387 case 'S': /* SU */
1388 terminal_scroll(terminal, set[0] ? args[0] : 1);
1389 break;
1390 case 'T': /* SD */
1391 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1392 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001393 case 'X': /* ECH */
1394 count = set[0] ? args[0] : 1;
1395 if (count == 0) count = 1;
1396 if ((terminal->column + count) > terminal->width)
1397 count = terminal->width - terminal->column;
1398 row = terminal_get_row(terminal, terminal->row);
1399 attr_row = terminal_get_attr_row(terminal, terminal->row);
1400 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1401 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1402 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001403 case 'Z': /* CBT */
1404 count = set[0] ? args[0] : 1;
1405 if (count == 0) count = 1;
1406 while (count > 0 && terminal->column >= 0) {
1407 if (terminal->tab_ruler[terminal->column]) count--;
1408 terminal->column--;
1409 }
1410 terminal->column++;
1411 break;
1412 case '`': /* HPA */
1413 y = set[0] ? args[0] : 1;
1414 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1415
1416 terminal->column = y - 1;
1417 break;
1418 case 'b': /* REP */
1419 count = set[0] ? args[0] : 1;
1420 if (count == 0) count = 1;
1421 if (terminal->last_char.byte[0])
1422 for (i = 0; i < count; i++)
1423 handle_char(terminal, terminal->last_char);
1424 terminal->last_char.byte[0] = 0;
1425 break;
1426 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001427 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001428 break;
1429 case 'd': /* VPA */
1430 x = set[0] ? args[0] : 1;
1431 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1432
1433 terminal->row = x - 1;
1434 break;
1435 case 'g': /* TBC */
1436 if (!set[0] || args[0] == 0) {
1437 terminal->tab_ruler[terminal->column] = 0;
1438 } else if (args[0] == 3) {
1439 memset(terminal->tab_ruler, 0, terminal->width);
1440 }
1441 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001442 case 'h': /* SM */
1443 for(i = 0; i < 10 && set[i]; i++) {
1444 handle_term_parameter(terminal, args[i], 1);
1445 }
1446 break;
1447 case 'l': /* RM */
1448 for(i = 0; i < 10 && set[i]; i++) {
1449 handle_term_parameter(terminal, args[i], 0);
1450 }
1451 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001452 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001453 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001454 if (i <= 7 && set[i] && set[i + 1] &&
1455 set[i + 2] && args[i + 1] == 5)
1456 {
1457 if (args[i] == 38) {
1458 handle_sgr(terminal, args[i + 2] + 256);
1459 break;
1460 } else if (args[i] == 48) {
1461 handle_sgr(terminal, args[i + 2] + 512);
1462 break;
1463 }
1464 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001465 if(set[i]) {
1466 handle_sgr(terminal, args[i]);
1467 } else if(i == 0) {
1468 handle_sgr(terminal, 0);
1469 break;
1470 } else {
1471 break;
1472 }
1473 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001474 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001475 case 'n': /* DSR */
1476 i = set[0] ? args[0] : 0;
1477 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001478 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001479 } else if (i == 6) {
1480 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1481 terminal->origin_mode ?
1482 terminal->row+terminal->margin_top : terminal->row+1,
1483 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001484 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001485 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001486 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001487 case 'r':
1488 if(!set[0]) {
1489 terminal->margin_top = 0;
1490 terminal->margin_bottom = terminal->height-1;
1491 terminal->row = 0;
1492 terminal->column = 0;
1493 } else {
1494 top = (set[0] ? args[0] : 1) - 1;
1495 top = top < 0 ? 0 :
1496 (top >= terminal->height ? terminal->height - 1 : top);
1497 bottom = (set[1] ? args[1] : 1) - 1;
1498 bottom = bottom < 0 ? 0 :
1499 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1500 if(bottom > top) {
1501 terminal->margin_top = top;
1502 terminal->margin_bottom = bottom;
1503 } else {
1504 terminal->margin_top = 0;
1505 terminal->margin_bottom = terminal->height-1;
1506 }
1507 if(terminal->origin_mode)
1508 terminal->row = terminal->margin_top;
1509 else
1510 terminal->row = 0;
1511 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001512 }
1513 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001514 case 's':
1515 terminal->saved_row = terminal->row;
1516 terminal->saved_column = terminal->column;
1517 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001518 case 't': /* windowOps */
1519 if (!set[0]) break;
1520 switch (args[0]) {
1521 case 4: /* resize px */
1522 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001523 widget_schedule_resize(terminal->widget,
1524 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001525 }
1526 break;
1527 case 8: /* resize ch */
1528 if (set[1] && set[2]) {
1529 terminal_resize(terminal, args[2], args[1]);
1530 }
1531 break;
1532 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001533 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001534 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1535 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001536 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001537 break;
1538 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001539 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001540 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1541 allocation.height, allocation.width);
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 case 18: /* report ch */
1545 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1546 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001547 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001548 break;
1549 case 21: /* report title */
1550 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1551 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001552 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001553 break;
1554 default:
1555 if (args[0] >= 24)
1556 terminal_resize(terminal, terminal->width, args[0]);
1557 else
1558 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1559 break;
1560 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001561 case 'u':
1562 terminal->row = terminal->saved_row;
1563 terminal->column = terminal->saved_column;
1564 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001565 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001566 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001567 break;
1568 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001569}
1570
1571static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001572handle_non_csi_escape(struct terminal *terminal, char code)
1573{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001574 switch(code) {
1575 case 'M': /* RI */
1576 terminal->row -= 1;
1577 if(terminal->row < terminal->margin_top) {
1578 terminal->row = terminal->margin_top;
1579 terminal_scroll(terminal, -1);
1580 }
1581 break;
1582 case 'E': /* NEL */
1583 terminal->column = 0;
1584 // fallthrough
1585 case 'D': /* IND */
1586 terminal->row += 1;
1587 if(terminal->row > terminal->margin_bottom) {
1588 terminal->row = terminal->margin_bottom;
1589 terminal_scroll(terminal, +1);
1590 }
1591 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001592 case 'c': /* RIS */
1593 terminal_init(terminal);
1594 break;
1595 case 'H': /* HTS */
1596 terminal->tab_ruler[terminal->column] = 1;
1597 break;
1598 case '7': /* DECSC */
1599 terminal->saved_row = terminal->row;
1600 terminal->saved_column = terminal->column;
1601 terminal->saved_attr = terminal->curr_attr;
1602 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001603 terminal->saved_cs = terminal->cs;
1604 terminal->saved_g0 = terminal->g0;
1605 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001606 break;
1607 case '8': /* DECRC */
1608 terminal->row = terminal->saved_row;
1609 terminal->column = terminal->saved_column;
1610 terminal->curr_attr = terminal->saved_attr;
1611 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001612 terminal->cs = terminal->saved_cs;
1613 terminal->g0 = terminal->saved_g0;
1614 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001615 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001616 case '=': /* DECPAM */
1617 terminal->key_mode = KM_APPLICATION;
1618 break;
1619 case '>': /* DECPNM */
1620 terminal->key_mode = KM_NORMAL;
1621 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001622 default:
1623 fprintf(stderr, "Unknown escape code: %c\n", code);
1624 break;
1625 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001626}
1627
1628static void
1629handle_special_escape(struct terminal *terminal, char special, char code)
1630{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001631 int i, numChars;
1632
1633 if (special == '#') {
1634 switch(code) {
1635 case '8':
1636 /* fill with 'E', no cheap way to do this */
1637 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1638 numChars = terminal->width * terminal->height;
1639 for(i = 0; i < numChars; i++) {
1640 terminal->data[i].byte[0] = 'E';
1641 }
1642 break;
1643 default:
1644 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1645 break;
1646 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001647 } else if (special == '(' || special == ')') {
1648 switch(code) {
1649 case '0':
1650 if (special == '(')
1651 terminal->g0 = CS_SPECIAL;
1652 else
1653 terminal->g1 = CS_SPECIAL;
1654 break;
1655 case 'A':
1656 if (special == '(')
1657 terminal->g0 = CS_UK;
1658 else
1659 terminal->g1 = CS_UK;
1660 break;
1661 case 'B':
1662 if (special == '(')
1663 terminal->g0 = CS_US;
1664 else
1665 terminal->g1 = CS_US;
1666 break;
1667 default:
1668 fprintf(stderr, "Unknown character set %c\n", code);
1669 break;
1670 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001671 } else {
1672 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1673 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001674}
1675
1676static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001677handle_sgr(struct terminal *terminal, int code)
1678{
1679 switch(code) {
1680 case 0:
1681 terminal->curr_attr = terminal->color_scheme->default_attr;
1682 break;
1683 case 1:
1684 terminal->curr_attr.a |= ATTRMASK_BOLD;
1685 if (terminal->curr_attr.fg < 8)
1686 terminal->curr_attr.fg += 8;
1687 break;
1688 case 4:
1689 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1690 break;
1691 case 5:
1692 terminal->curr_attr.a |= ATTRMASK_BLINK;
1693 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001694 case 8:
1695 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1696 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001697 case 2:
1698 case 21:
1699 case 22:
1700 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1701 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1702 terminal->curr_attr.fg -= 8;
1703 break;
1704 case 24:
1705 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1706 break;
1707 case 25:
1708 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1709 break;
1710 case 7:
1711 case 26:
1712 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1713 break;
1714 case 27:
1715 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1716 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001717 case 28:
1718 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1719 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001720 case 39:
1721 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1722 break;
1723 case 49:
1724 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1725 break;
1726 default:
1727 if(code >= 30 && code <= 37) {
1728 terminal->curr_attr.fg = code - 30;
1729 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1730 terminal->curr_attr.fg += 8;
1731 } else if(code >= 40 && code <= 47) {
1732 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001733 } else if (code >= 90 && code <= 97) {
1734 terminal->curr_attr.fg = code - 90 + 8;
1735 } else if (code >= 100 && code <= 107) {
1736 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001737 } else if(code >= 256 && code < 512) {
1738 terminal->curr_attr.fg = code - 256;
1739 } else if(code >= 512 && code < 768) {
1740 terminal->curr_attr.bg = code - 512;
1741 } else {
1742 fprintf(stderr, "Unknown SGR code: %d\n", code);
1743 }
1744 break;
1745 }
1746}
1747
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001748/* Returns 1 if c was special, otherwise 0 */
1749static int
1750handle_special_char(struct terminal *terminal, char c)
1751{
1752 union utf8_char *row;
1753 struct attr *attr_row;
1754
1755 row = terminal_get_row(terminal, terminal->row);
1756 attr_row = terminal_get_attr_row(terminal, terminal->row);
1757
1758 switch(c) {
1759 case '\r':
1760 terminal->column = 0;
1761 break;
1762 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001763 if (terminal->mode & MODE_LF_NEWLINE) {
1764 terminal->column = 0;
1765 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001766 /* fallthrough */
1767 case '\v':
1768 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001769 terminal->row++;
1770 if(terminal->row > terminal->margin_bottom) {
1771 terminal->row = terminal->margin_bottom;
1772 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001773 }
1774
1775 break;
1776 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001777 while (terminal->column < terminal->width) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001778 if (terminal->mode & MODE_IRM)
1779 terminal_shift_line(terminal, +1);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001780 row[terminal->column].byte[0] = ' ';
1781 row[terminal->column].byte[1] = '\0';
1782 attr_row[terminal->column] = terminal->curr_attr;
1783 terminal->column++;
Kristian Høgsbergcca3c2f2012-06-20 15:56:13 -04001784 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001785 }
1786 if (terminal->column >= terminal->width) {
1787 terminal->column = terminal->width - 1;
1788 }
1789
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001790 break;
1791 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001792 if (terminal->column >= terminal->width) {
1793 terminal->column = terminal->width - 2;
1794 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001795 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001796 } else if (terminal->mode & MODE_AUTOWRAP) {
1797 terminal->column = terminal->width - 1;
1798 terminal->row -= 1;
1799 if (terminal->row < terminal->margin_top) {
1800 terminal->row = terminal->margin_top;
1801 terminal_scroll(terminal, -1);
1802 }
1803 }
1804
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001805 break;
1806 case '\a':
1807 /* Bell */
1808 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001809 case '\x0E': /* SO */
1810 terminal->cs = terminal->g1;
1811 break;
1812 case '\x0F': /* SI */
1813 terminal->cs = terminal->g0;
1814 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001815 default:
1816 return 0;
1817 }
1818
1819 return 1;
1820}
1821
1822static void
1823handle_char(struct terminal *terminal, union utf8_char utf8)
1824{
1825 union utf8_char *row;
1826 struct attr *attr_row;
1827
1828 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001829
1830 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001831
1832 /* There are a whole lot of non-characters, control codes,
1833 * and formatting codes that should probably be ignored,
1834 * for example: */
1835 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1836 /* BOM, ignore */
1837 return;
1838 }
1839
1840 /* Some of these non-characters should be translated, e.g.: */
1841 if (utf8.byte[0] < 32) {
1842 utf8.byte[0] = utf8.byte[0] + 64;
1843 }
1844
1845 /* handle right margin effects */
1846 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001847 if (terminal->mode & MODE_AUTOWRAP) {
1848 terminal->column = 0;
1849 terminal->row += 1;
1850 if (terminal->row > terminal->margin_bottom) {
1851 terminal->row = terminal->margin_bottom;
1852 terminal_scroll(terminal, +1);
1853 }
1854 } else {
1855 terminal->column--;
1856 }
1857 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001858
1859 row = terminal_get_row(terminal, terminal->row);
1860 attr_row = terminal_get_attr_row(terminal, terminal->row);
1861
Callum Lowcay69e96582011-01-07 19:47:00 +00001862 if (terminal->mode & MODE_IRM)
1863 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001864 row[terminal->column] = utf8;
1865 attr_row[terminal->column++] = terminal->curr_attr;
1866
1867 if (utf8.ch != terminal->last_char.ch)
1868 terminal->last_char = utf8;
1869}
1870
Callum Lowcay30eeae52011-01-07 19:46:55 +00001871static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001872escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1873{
1874 int len, i;
1875
1876 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1877 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1878 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1879 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1880 else len = 1; /* Invalid, cannot happen */
1881
1882 if (terminal->escape_length + len <= MAX_ESCAPE) {
1883 for (i = 0; i < len; i++)
1884 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1885 terminal->escape_length += len;
1886 } else if (terminal->escape_length < MAX_ESCAPE) {
1887 terminal->escape[terminal->escape_length++] = 0;
1888 }
1889}
1890
1891static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001892terminal_data(struct terminal *terminal, const char *data, size_t length)
1893{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001894 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001895 union utf8_char utf8;
1896 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001897
1898 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001899 parser_state =
1900 utf8_next_char(&terminal->state_machine, data[i]);
1901 switch(parser_state) {
1902 case utf8state_accept:
1903 utf8.ch = terminal->state_machine.s.ch;
1904 break;
1905 case utf8state_reject:
1906 /* the unicode replacement character */
1907 utf8.byte[0] = 0xEF;
1908 utf8.byte[1] = 0xBF;
1909 utf8.byte[2] = 0xBD;
1910 utf8.byte[3] = 0x00;
1911 break;
1912 default:
1913 continue;
1914 }
1915
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001916 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001917 switch (terminal->state) {
1918 case escape_state_escape:
1919 escape_append_utf8(terminal, utf8);
1920 switch (utf8.byte[0]) {
1921 case 'P': /* DCS */
1922 terminal->state = escape_state_dcs;
1923 break;
1924 case '[': /* CSI */
1925 terminal->state = escape_state_csi;
1926 break;
1927 case ']': /* OSC */
1928 terminal->state = escape_state_osc;
1929 break;
1930 case '#':
1931 case '(':
1932 case ')': /* special */
1933 terminal->state = escape_state_special;
1934 break;
1935 case '^': /* PM (not implemented) */
1936 case '_': /* APC (not implemented) */
1937 terminal->state = escape_state_ignore;
1938 break;
1939 default:
1940 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001941 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13001942 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001943 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001944 continue;
1945 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001946 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1947 /* do nothing */
1948 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001949 terminal->escape_flags |= ESC_FLAG_WHAT;
1950 } else if (utf8.byte[0] == '>') {
1951 terminal->escape_flags |= ESC_FLAG_GT;
1952 } else if (utf8.byte[0] == '!') {
1953 terminal->escape_flags |= ESC_FLAG_BANG;
1954 } else if (utf8.byte[0] == '$') {
1955 terminal->escape_flags |= ESC_FLAG_CASH;
1956 } else if (utf8.byte[0] == '\'') {
1957 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1958 } else if (utf8.byte[0] == '"') {
1959 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1960 } else if (utf8.byte[0] == ' ') {
1961 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001962 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001963 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001964 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13001965 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001966 }
1967
1968 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1969 utf8.byte[0] == '`')
1970 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001971 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001972 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001973 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001974 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001975 continue;
1976 case escape_state_inner_escape:
1977 if (utf8.byte[0] == '\\') {
1978 terminal->state = escape_state_normal;
1979 if (terminal->outer_state == escape_state_dcs) {
1980 handle_dcs(terminal);
1981 } else if (terminal->outer_state == escape_state_osc) {
1982 handle_osc(terminal);
1983 }
1984 } else if (utf8.byte[0] == '\e') {
1985 terminal->state = terminal->outer_state;
1986 escape_append_utf8(terminal, utf8);
1987 if (terminal->escape_length >= MAX_ESCAPE)
1988 terminal->state = escape_state_normal;
1989 } else {
1990 terminal->state = terminal->outer_state;
1991 if (terminal->escape_length < MAX_ESCAPE)
1992 terminal->escape[terminal->escape_length++] = '\e';
1993 escape_append_utf8(terminal, utf8);
1994 if (terminal->escape_length >= MAX_ESCAPE)
1995 terminal->state = escape_state_normal;
1996 }
1997 continue;
1998 case escape_state_dcs:
1999 case escape_state_osc:
2000 case escape_state_ignore:
2001 if (utf8.byte[0] == '\e') {
2002 terminal->outer_state = terminal->state;
2003 terminal->state = escape_state_inner_escape;
2004 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
2005 terminal->state = escape_state_normal;
2006 handle_osc(terminal);
2007 } else {
2008 escape_append_utf8(terminal, utf8);
2009 if (terminal->escape_length >= MAX_ESCAPE)
2010 terminal->state = escape_state_normal;
2011 }
2012 continue;
2013 case escape_state_special:
2014 escape_append_utf8(terminal, utf8);
2015 terminal->state = escape_state_normal;
2016 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2017 handle_special_escape(terminal, terminal->escape[1],
2018 utf8.byte[0]);
2019 }
2020 continue;
2021 default:
2022 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002023 }
2024
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002025 /* this is valid, because ASCII characters are never used to
2026 * introduce a multibyte sequence in UTF-8 */
2027 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002028 terminal->state = escape_state_escape;
2029 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002030 terminal->escape[0] = '\e';
2031 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002032 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002033 } else {
2034 handle_char(terminal, utf8);
2035 } /* if */
2036 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002037
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002038 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002039}
2040
2041static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002042data_source_target(void *data,
2043 struct wl_data_source *source, const char *mime_type)
2044{
2045 fprintf(stderr, "data_source_target, %s\n", mime_type);
2046}
2047
2048static void
2049data_source_send(void *data,
2050 struct wl_data_source *source,
2051 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002052{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002053 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002054
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002055 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002056}
2057
2058static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002059data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002060{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002061 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002062}
2063
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002064static const struct wl_data_source_listener data_source_listener = {
2065 data_source_target,
2066 data_source_send,
2067 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002068};
2069
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002070static int
2071handle_bound_key(struct terminal *terminal,
2072 struct input *input, uint32_t sym, uint32_t time)
2073{
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002074 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002075 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002076 /* Cut selection; terminal doesn't do cut, fall
2077 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002078 case XKB_KEY_C:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002079 terminal->selection =
2080 display_create_data_source(terminal->display);
2081 wl_data_source_offer(terminal->selection,
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002082 "text/plain;charset=utf-8");
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002083 wl_data_source_add_listener(terminal->selection,
2084 &data_source_listener, terminal);
Kristian Høgsbergfee91be2012-06-02 21:21:41 -04002085 input_set_selection(input, terminal->selection,
2086 display_get_serial(terminal->display));
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002087 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002088 case XKB_KEY_V:
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002089 input_receive_selection_data_to_fd(input,
2090 "text/plain;charset=utf-8",
2091 terminal->master);
2092
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002093 return 1;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002094 default:
2095 return 0;
2096 }
2097}
2098
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002099static void
2100key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002101 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2102 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002103{
2104 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002105 char ch[MAX_RESPONSE];
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002106 uint32_t modifiers;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002107 int len = 0;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002108
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002109 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002110 if ((modifiers & MOD_CONTROL_MASK) &&
2111 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002112 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2113 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002114 return;
2115
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002116 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002117 case XKB_KEY_F11:
Daniel Stonec9785ea2012-05-30 16:31:52 +01002118 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002119 break;
2120 terminal->fullscreen ^= 1;
2121 window_set_fullscreen(window, terminal->fullscreen);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002122 break;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002123
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002124 case XKB_KEY_BackSpace:
2125 case XKB_KEY_Tab:
2126 case XKB_KEY_Linefeed:
2127 case XKB_KEY_Clear:
2128 case XKB_KEY_Pause:
2129 case XKB_KEY_Scroll_Lock:
2130 case XKB_KEY_Sys_Req:
2131 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002132 ch[len++] = sym & 0x7f;
2133 break;
2134
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002135 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002136 if (terminal->mode & MODE_LF_NEWLINE) {
2137 ch[len++] = 0x0D;
2138 ch[len++] = 0x0A;
2139 } else {
2140 ch[len++] = 0x0D;
2141 }
2142 break;
2143
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002144 case XKB_KEY_Shift_L:
2145 case XKB_KEY_Shift_R:
2146 case XKB_KEY_Control_L:
2147 case XKB_KEY_Control_R:
2148 case XKB_KEY_Alt_L:
2149 case XKB_KEY_Alt_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002150 break;
2151
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002152 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002153 len = function_key_response('[', 2, modifiers, '~', ch);
2154 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002155 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002156 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2157 ch[len++] = '\x04';
2158 } else {
2159 len = function_key_response('[', 3, modifiers, '~', ch);
2160 }
2161 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002162 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002163 len = function_key_response('[', 5, modifiers, '~', ch);
2164 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002165 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002166 len = function_key_response('[', 6, modifiers, '~', ch);
2167 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002168 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002169 len = function_key_response('O', 1, modifiers, 'P', ch);
2170 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002171 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002172 len = function_key_response('O', 1, modifiers, 'Q', ch);
2173 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002174 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002175 len = function_key_response('O', 1, modifiers, 'R', ch);
2176 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002177 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002178 len = function_key_response('O', 1, modifiers, 'S', ch);
2179 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002180 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002181 len = function_key_response('[', 15, modifiers, '~', ch);
2182 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002183 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002184 len = function_key_response('[', 17, modifiers, '~', ch);
2185 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002186 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002187 len = function_key_response('[', 18, modifiers, '~', ch);
2188 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002189 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002190 len = function_key_response('[', 19, modifiers, '~', ch);
2191 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002192 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002193 len = function_key_response('[', 20, modifiers, '~', ch);
2194 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002195 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002196 len = function_key_response('[', 21, modifiers, '~', ch);
2197 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002198 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002199 len = function_key_response('[', 24, modifiers, '~', ch);
2200 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002201 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002202 /* Handle special keys with alternate mappings */
2203 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2204 if (len != 0) break;
2205
Kristian Høgsberg70163132012-05-08 15:55:39 -04002206 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002207 if (sym >= '3' && sym <= '7')
2208 sym = (sym & 0x1f) + 8;
2209
2210 if (!((sym >= '!' && sym <= '/') ||
2211 (sym >= '8' && sym <= '?') ||
2212 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2213 else if (sym == '2') sym = 0x00;
2214 else if (sym == '/') sym = 0x1F;
2215 else if (sym == '8' || sym == '?') sym = 0x7F;
2216 } else if ((terminal->mode & MODE_ALT_SENDS_ESC) &&
Kristian Høgsberg70163132012-05-08 15:55:39 -04002217 (modifiers & MOD_ALT_MASK))
Callum Lowcay7e08e902011-01-07 19:47:02 +00002218 {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002219 ch[len++] = 0x1b;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002220 } else if (modifiers & MOD_ALT_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002221 sym = sym | 0x80;
2222 }
2223
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002224 if (sym < 256)
2225 ch[len++] = sym;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002226 break;
2227 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002228
Daniel Stonec9785ea2012-05-30 16:31:52 +01002229 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0)
Kristian Høgsberg26130862011-08-24 11:30:21 -04002230 terminal_write(terminal, ch, len);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002231}
2232
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002233static void
2234keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002235 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002236{
2237 struct terminal *terminal = data;
2238
2239 terminal->focused = (device != NULL);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002240 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002241}
2242
Kristian Høgsberg59826582011-01-20 11:56:57 -05002243static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002244button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002245 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002246 uint32_t button,
2247 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002248{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002249 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002250
2251 switch (button) {
2252 case 272:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002253 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002254 terminal->dragging = 1;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002255 input_get_position(input,
2256 &terminal->selection_start_x,
2257 &terminal->selection_start_y);
2258 terminal->selection_end_x = terminal->selection_start_x;
2259 terminal->selection_end_y = terminal->selection_start_y;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002260 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002261 } else {
2262 terminal->dragging = 0;
2263 }
2264 break;
2265 }
2266}
2267
2268static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002269motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002270 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002271 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002272{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002273 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002274
2275 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002276 input_get_position(input,
2277 &terminal->selection_end_x,
2278 &terminal->selection_end_y);
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002279 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002280 }
2281
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002282 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002283}
2284
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002285static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002286terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002287{
2288 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002289 cairo_surface_t *surface;
2290 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002291
2292 terminal = malloc(sizeof *terminal);
2293 if (terminal == NULL)
2294 return terminal;
2295
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002296 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002297 terminal->fullscreen = fullscreen;
Callum Lowcay30eeae52011-01-07 19:46:55 +00002298 terminal->color_scheme = &DEFAULT_COLORS;
2299 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002300 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002301 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002302 terminal->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002303 terminal->widget = frame_create(terminal->window, terminal);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002304 window_set_title(terminal->window, "Wayland Terminal");
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002305 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002306
2307 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002308 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002309
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002310 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002311 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002312
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002313 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002314 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002315 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002316 keyboard_focus_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002317 widget_set_redraw_handler(terminal->widget, redraw_handler);
2318 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002319 widget_set_button_handler(terminal->widget, button_handler);
2320 widget_set_motion_handler(terminal->widget, motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002321
Kristian Høgsberg09531622010-06-14 23:22:15 -04002322 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2323 cr = cairo_create(surface);
Kristian Høgsberg09531622010-06-14 23:22:15 -04002324 cairo_set_font_size(cr, 14);
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002325 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002326 CAIRO_FONT_SLANT_NORMAL,
2327 CAIRO_FONT_WEIGHT_BOLD);
2328 terminal->font_bold = cairo_get_scaled_font (cr);
2329 cairo_scaled_font_reference(terminal->font_bold);
2330
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002331 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002332 CAIRO_FONT_SLANT_NORMAL,
2333 CAIRO_FONT_WEIGHT_NORMAL);
2334 terminal->font_normal = cairo_get_scaled_font (cr);
2335 cairo_scaled_font_reference(terminal->font_normal);
2336
Kristian Høgsberg09531622010-06-14 23:22:15 -04002337 cairo_font_extents(cr, &terminal->extents);
2338 cairo_destroy(cr);
2339 cairo_surface_destroy(surface);
2340
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002341 window_schedule_resize(terminal->window, 500, 400);
2342
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002343 return terminal;
2344}
2345
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002346static void
2347io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002348{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002349 struct terminal *terminal =
2350 container_of(task, struct terminal, io_task);
2351 char buffer[256];
2352 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002353
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002354 if (events & EPOLLHUP)
2355 exit(0);
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002356
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002357 len = read(terminal->master, buffer, sizeof buffer);
2358 if (len < 0)
2359 exit(0);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002360
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002361 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002362}
2363
2364static int
2365terminal_run(struct terminal *terminal, const char *path)
2366{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002367 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002368 pid_t pid;
2369
2370 pid = forkpty(&master, NULL, NULL, NULL);
2371 if (pid == 0) {
Callum Lowcay86653ed2011-01-07 19:47:03 +00002372 setenv("TERM", "xterm-256color", 1);
2373 setenv("COLORTERM", "xterm-256color", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002374 if (execl(path, path, NULL)) {
2375 printf("exec failed: %m\n");
2376 exit(EXIT_FAILURE);
2377 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002378 } else if (pid < 0) {
2379 fprintf(stderr, "failed to fork and create pty (%m).\n");
2380 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002381 }
2382
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002383 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002384 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002385 terminal->io_task.run = io_handler;
2386 display_watch_fd(terminal->display, terminal->master,
2387 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002388
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002389 window_set_fullscreen(terminal->window, terminal->fullscreen);
2390 if (!terminal->fullscreen)
2391 terminal_resize(terminal, 80, 24);
2392
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002393 return 0;
2394}
2395
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002396static const struct config_key terminal_config_keys[] = {
2397 { "font", CONFIG_KEY_STRING, &option_font },
2398};
2399
2400static const struct config_section config_sections[] = {
2401 { "terminal",
2402 terminal_config_keys, ARRAY_LENGTH(terminal_config_keys) },
2403};
2404
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002405static const struct weston_option terminal_options[] = {
2406 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002407 { WESTON_OPTION_STRING, "font", 0, &option_font },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002408};
2409
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002410int main(int argc, char *argv[])
2411{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002412 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002413 struct terminal *terminal;
Peter Hutterer035ac942012-02-03 20:58:19 +10002414 const char *shell;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002415 char *config_file;
2416
2417 config_file = config_file_path("weston.ini");
2418 parse_config_file(config_file,
2419 config_sections, ARRAY_LENGTH(config_sections),
2420 NULL);
2421 free(config_file);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002422
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002423 argc = parse_options(terminal_options,
2424 ARRAY_LENGTH(terminal_options), argc, argv);
2425
2426 d = display_create(argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002427 if (d == NULL) {
2428 fprintf(stderr, "failed to create display: %m\n");
2429 return -1;
2430 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002431
Peter Hutterer035ac942012-02-03 20:58:19 +10002432 shell = getenv("SHELL");
2433 if (!shell)
2434 shell = "/bin/bash";
2435
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002436 terminal = terminal_create(d, option_fullscreen);
Peter Hutterer035ac942012-02-03 20:58:19 +10002437 if (terminal_run(terminal, shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002438 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002439
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002440 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002441
2442 return 0;
2443}