blob: 52b0c2428edd90416fc3ff63098f7b5770223a37 [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øgsbergde845cf2012-06-20 22:14:31 -040043static char *option_term = "xterm";
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050044
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050045#define MOD_SHIFT 0x01
46#define MOD_ALT 0x02
47#define MOD_CTRL 0x04
48
Callum Lowcay30eeae52011-01-07 19:46:55 +000049#define ATTRMASK_BOLD 0x01
50#define ATTRMASK_UNDERLINE 0x02
51#define ATTRMASK_BLINK 0x04
52#define ATTRMASK_INVERSE 0x08
Callum Lowcay81179db2011-01-10 12:14:01 +130053#define ATTRMASK_CONCEALED 0x10
Callum Lowcay30eeae52011-01-07 19:46:55 +000054
Callum Lowcayb8609ad2011-01-07 19:46:57 +000055/* Buffer sizes */
Callum Lowcayef57a9b2011-01-14 20:46:23 +130056#define MAX_RESPONSE 256
57#define MAX_ESCAPE 255
Callum Lowcayb8609ad2011-01-07 19:46:57 +000058
Callum Lowcay8e57dd52011-01-07 19:46:59 +000059/* Terminal modes */
60#define MODE_SHOW_CURSOR 0x00000001
61#define MODE_INVERSE 0x00000002
62#define MODE_AUTOWRAP 0x00000004
63#define MODE_AUTOREPEAT 0x00000008
64#define MODE_LF_NEWLINE 0x00000010
Callum Lowcay69e96582011-01-07 19:47:00 +000065#define MODE_IRM 0x00000020
Callum Lowcay7e08e902011-01-07 19:47:02 +000066#define MODE_DELETE_SENDS_DEL 0x00000040
67#define MODE_ALT_SENDS_ESC 0x00000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +000068
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000069union utf8_char {
70 unsigned char byte[4];
71 uint32_t ch;
72};
73
74enum utf8_state {
75 utf8state_start,
76 utf8state_accept,
77 utf8state_reject,
78 utf8state_expect3,
79 utf8state_expect2,
80 utf8state_expect1
81};
82
83struct utf8_state_machine {
84 enum utf8_state state;
85 int len;
86 union utf8_char s;
87};
88
89static void
90init_state_machine(struct utf8_state_machine *machine)
91{
92 machine->state = utf8state_start;
93 machine->len = 0;
94 machine->s.ch = 0;
95}
96
97static enum utf8_state
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -040098utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000099{
100 switch(machine->state) {
101 case utf8state_start:
102 case utf8state_accept:
103 case utf8state_reject:
104 machine->s.ch = 0;
105 machine->len = 0;
106 if(c == 0xC0 || c == 0xC1) {
107 /* overlong encoding, reject */
108 machine->state = utf8state_reject;
109 } else if((c & 0x80) == 0) {
110 /* single byte, accept */
111 machine->s.byte[machine->len++] = c;
112 machine->state = utf8state_accept;
113 } else if((c & 0xC0) == 0x80) {
114 /* parser out of sync, ignore byte */
115 machine->state = utf8state_start;
116 } else if((c & 0xE0) == 0xC0) {
117 /* start of two byte sequence */
118 machine->s.byte[machine->len++] = c;
119 machine->state = utf8state_expect1;
120 } else if((c & 0xF0) == 0xE0) {
121 /* start of three byte sequence */
122 machine->s.byte[machine->len++] = c;
123 machine->state = utf8state_expect2;
124 } else if((c & 0xF8) == 0xF0) {
125 /* start of four byte sequence */
126 machine->s.byte[machine->len++] = c;
127 machine->state = utf8state_expect3;
128 } else {
129 /* overlong encoding, reject */
130 machine->state = utf8state_reject;
131 }
132 break;
133 case utf8state_expect3:
134 machine->s.byte[machine->len++] = c;
135 if((c & 0xC0) == 0x80) {
136 /* all good, continue */
137 machine->state = utf8state_expect2;
138 } else {
139 /* missing extra byte, reject */
140 machine->state = utf8state_reject;
141 }
142 break;
143 case utf8state_expect2:
144 machine->s.byte[machine->len++] = c;
145 if((c & 0xC0) == 0x80) {
146 /* all good, continue */
147 machine->state = utf8state_expect1;
148 } else {
149 /* missing extra byte, reject */
150 machine->state = utf8state_reject;
151 }
152 break;
153 case utf8state_expect1:
154 machine->s.byte[machine->len++] = c;
155 if((c & 0xC0) == 0x80) {
156 /* all good, accept */
157 machine->state = utf8state_accept;
158 } else {
159 /* missing extra byte, reject */
160 machine->state = utf8state_reject;
161 }
162 break;
163 default:
164 machine->state = utf8state_reject;
165 break;
166 }
167
168 return machine->state;
169}
170
Callum Lowcay256e72f2011-01-07 19:47:01 +0000171struct char_sub {
172 union utf8_char match;
173 union utf8_char replace;
174};
175/* Set last char_sub match to NULL char */
176typedef struct char_sub *character_set;
177
178struct char_sub CS_US[] = {
179 {{{0, }}, {{0, }}}
180};
181static struct char_sub CS_UK[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200182 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000183 {{{0, }}, {{0, }}}
184};
185static struct char_sub CS_SPECIAL[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200186 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond: ♦ */
187 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell: ▒ */
188 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT: ␉ */
189 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF: ␌ */
190 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR: ␍ */
191 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF: ␊ */
192 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree: ° */
193 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus: ± */
194 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL: ␤ */
195 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT: ␋ */
196 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB: ┘ */
197 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT: ┐ */
198 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT: ┌ */
199 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_LB: └ */
200 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS: ┼ */
201 {{{'o', 0, }}, {{0xE2, 0x8E, 0xBA, 0}}}, /* Horiz. Scan Line 1: ⎺ */
202 {{{'p', 0, }}, {{0xE2, 0x8E, 0xBB, 0}}}, /* Horiz. Scan Line 3: ⎻ */
203 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* Horiz. Scan Line 5: ─ */
204 {{{'r', 0, }}, {{0xE2, 0x8E, 0xBC, 0}}}, /* Horiz. Scan Line 7: ⎼ */
205 {{{'s', 0, }}, {{0xE2, 0x8E, 0xBD, 0}}}, /* Horiz. Scan Line 9: ⎽ */
206 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR: ├ */
207 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL: ┤ */
208 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU: ┴ */
209 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD: ┬ */
210 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V: │ */
211 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE: ≤ */
212 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE: ≥ */
213 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI: π */
214 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ: ≠ */
215 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
216 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT: ⋅ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000217 {{{0, }}, {{0, }}}
218};
219
220static void
221apply_char_set(character_set cs, union utf8_char *utf8)
222{
223 int i = 0;
224
225 while (cs[i].match.byte[0]) {
226 if ((*utf8).ch == cs[i].match.ch) {
227 *utf8 = cs[i].replace;
228 break;
229 }
230 i++;
231 }
232}
233
Callum Lowcay7e08e902011-01-07 19:47:02 +0000234struct key_map {
235 int sym;
236 int num;
237 char escape;
238 char code;
239};
240/* Set last key_sub sym to NULL */
241typedef struct key_map *keyboard_mode;
242
243static struct key_map KM_NORMAL[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400244 { XKB_KEY_Left, 1, '[', 'D' },
245 { XKB_KEY_Right, 1, '[', 'C' },
246 { XKB_KEY_Up, 1, '[', 'A' },
247 { XKB_KEY_Down, 1, '[', 'B' },
248 { XKB_KEY_Home, 1, '[', 'H' },
249 { XKB_KEY_End, 1, '[', 'F' },
250 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000251};
252static struct key_map KM_APPLICATION[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400253 { XKB_KEY_Left, 1, 'O', 'D' },
254 { XKB_KEY_Right, 1, 'O', 'C' },
255 { XKB_KEY_Up, 1, 'O', 'A' },
256 { XKB_KEY_Down, 1, 'O', 'B' },
257 { XKB_KEY_Home, 1, 'O', 'H' },
258 { XKB_KEY_End, 1, 'O', 'F' },
259 { XKB_KEY_KP_Enter, 1, 'O', 'M' },
260 { XKB_KEY_KP_Multiply, 1, 'O', 'j' },
261 { XKB_KEY_KP_Add, 1, 'O', 'k' },
262 { XKB_KEY_KP_Separator, 1, 'O', 'l' },
263 { XKB_KEY_KP_Subtract, 1, 'O', 'm' },
264 { XKB_KEY_KP_Divide, 1, 'O', 'o' },
265 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000266};
267
268static int
269function_key_response(char escape, int num, uint32_t modifiers,
270 char code, char *response)
271{
272 int mod_num = 0;
273 int len;
274
Kristian Høgsberg70163132012-05-08 15:55:39 -0400275 if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
276 if (modifiers & MOD_ALT_MASK) mod_num |= 2;
277 if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000278
279 if (mod_num != 0)
280 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
281 num, mod_num + 1, code);
282 else if (code != '~')
283 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
284 escape, code);
285 else
286 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
287 escape, num, code);
288
289 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
290 else return len;
291}
292
293/* returns the number of bytes written into response,
294 * which must have room for MAX_RESPONSE bytes */
295static int
296apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
297{
298 struct key_map map;
299 int len = 0;
300 int i = 0;
301
302 while (mode[i].sym) {
303 map = mode[i++];
304 if (sym == map.sym) {
305 len = function_key_response(map.escape, map.num,
306 modifiers, map.code,
307 response);
308 break;
309 }
310 }
311
312 return len;
313}
314
Callum Lowcay30eeae52011-01-07 19:46:55 +0000315struct terminal_color { double r, g, b, a; };
316struct attr {
317 unsigned char fg, bg;
318 char a; /* attributes format:
319 * 76543210
Callum Lowcay81179db2011-01-10 12:14:01 +1300320 * cilub */
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500321 char s; /* in selection */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000322};
323struct color_scheme {
324 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500325 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000326 struct attr default_attr;
327};
328
329static void
330attr_init(struct attr *data_attr, struct attr attr, int n)
331{
332 int i;
333 for (i = 0; i < n; i++) {
334 data_attr[i] = attr;
335 }
336}
337
Callum Lowcay67a201d2011-01-12 19:23:41 +1300338enum escape_state {
339 escape_state_normal = 0,
340 escape_state_escape,
341 escape_state_dcs,
342 escape_state_csi,
343 escape_state_osc,
344 escape_state_inner_escape,
345 escape_state_ignore,
346 escape_state_special
347};
348
349#define ESC_FLAG_WHAT 0x01
350#define ESC_FLAG_GT 0x02
351#define ESC_FLAG_BANG 0x04
352#define ESC_FLAG_CASH 0x08
353#define ESC_FLAG_SQUOTE 0x10
354#define ESC_FLAG_DQUOTE 0x20
355#define ESC_FLAG_SPACE 0x40
356
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500357struct terminal {
358 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500359 struct widget *widget;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500360 struct display *display;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000361 union utf8_char *data;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400362 struct task io_task;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000363 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000364 struct attr *data_attr;
365 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000366 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000367 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000368 char saved_origin_mode;
369 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000370 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000371 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000372 character_set cs, g0, g1;
373 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000374 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000375 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500376 int width, height, start, row, column;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000377 int saved_row, saved_column;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600378 int send_cursor_position;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500379 int fd, master;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500380 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300381 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500382 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300383 enum escape_state state;
384 enum escape_state outer_state;
385 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000386 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500387 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500388 int fullscreen;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500389 int focused;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400390 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000391 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400392 cairo_font_extents_t extents;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500393 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500394
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400395 struct wl_data_source *selection;
396 int32_t dragging;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500397 int selection_start_x, selection_start_y;
398 int selection_end_x, selection_end_y;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500399};
400
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000401/* Create default tab stops, every 8 characters */
402static void
403terminal_init_tabs(struct terminal *terminal)
404{
405 int i = 0;
406
407 while (i < terminal->width) {
408 if (i % 8 == 0)
409 terminal->tab_ruler[i] = 1;
410 else
411 terminal->tab_ruler[i] = 0;
412 i++;
413 }
414}
415
Callum Lowcay30eeae52011-01-07 19:46:55 +0000416static void
417terminal_init(struct terminal *terminal)
418{
419 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000420 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000421 terminal->mode = MODE_SHOW_CURSOR |
422 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000423 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000424 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000425
426 terminal->row = 0;
427 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000428
Callum Lowcay256e72f2011-01-07 19:47:01 +0000429 terminal->g0 = CS_US;
430 terminal->g1 = CS_US;
431 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000432 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000433
434 terminal->saved_g0 = terminal->g0;
435 terminal->saved_g1 = terminal->g1;
436 terminal->saved_cs = terminal->cs;
437
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000438 terminal->saved_attr = terminal->curr_attr;
439 terminal->saved_origin_mode = terminal->origin_mode;
440 terminal->saved_row = terminal->row;
441 terminal->saved_column = terminal->column;
442
443 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000444}
445
446static void
447init_color_table(struct terminal *terminal)
448{
449 int c, r;
450 struct terminal_color *color_table = terminal->color_table;
451
452 for (c = 0; c < 256; c ++) {
453 if (c < 16) {
454 color_table[c] = terminal->color_scheme->palette[c];
455 } else if (c < 232) {
456 r = c - 16;
457 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
458 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
459 color_table[c].r = ((double)(r % 6) / 6.0);
460 color_table[c].a = 1.0;
461 } else {
462 r = (c - 232) * 10 + 8;
463 color_table[c].r = ((double) r) / 256.0;
464 color_table[c].g = color_table[c].r;
465 color_table[c].b = color_table[c].r;
466 color_table[c].a = 1.0;
467 }
468 }
469}
470
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000471static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500472terminal_get_row(struct terminal *terminal, int row)
473{
474 int index;
475
476 index = (row + terminal->start) % terminal->height;
477
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000478 return &terminal->data[index * terminal->width];
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500479}
480
Callum Lowcay30eeae52011-01-07 19:46:55 +0000481static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500482terminal_get_attr_row(struct terminal *terminal, int row)
483{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000484 int index;
485
486 index = (row + terminal->start) % terminal->height;
487
488 return &terminal->data_attr[index * terminal->width];
489}
490
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500491union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300492 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500493 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500494};
495
Kristian Høgsberg59826582011-01-20 11:56:57 -0500496static int
497terminal_compare_position(struct terminal *terminal,
498 int x, int y, int32_t ref_row, int32_t ref_col)
499{
500 struct rectangle allocation;
501 int top_margin, side_margin, col, row, ref_x;
502
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500503 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsberg59826582011-01-20 11:56:57 -0500504 side_margin = allocation.x + (allocation.width - terminal->width * terminal->extents.max_x_advance) / 2;
505 top_margin = allocation.y + (allocation.height - terminal->height * terminal->extents.height) / 2;
506
507 col = (x - side_margin) / terminal->extents.max_x_advance;
508 row = (y - top_margin) / terminal->extents.height;
509
510 ref_x = side_margin + ref_col * terminal->extents.max_x_advance +
511 terminal->extents.max_x_advance / 2;
512
513 if (row < ref_row)
514 return -1;
515 if (row == ref_row) {
516 if (col < ref_col)
517 return -1;
518 if (col == ref_col && x < ref_x)
519 return -1;
520 }
521
522 return 1;
523}
524
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500525static void
526terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500527 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500528{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500529 struct attr attr;
530 int foreground, background, tmp;
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500531 int start_cmp, end_cmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500532
533 start_cmp =
534 terminal_compare_position(terminal,
535 terminal->selection_start_x,
536 terminal->selection_start_y,
537 row, col);
538 end_cmp =
539 terminal_compare_position(terminal,
540 terminal->selection_end_x,
541 terminal->selection_end_y,
542 row, col);
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500543 decoded->attr.s = 0;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500544 if (start_cmp < 0 && end_cmp > 0)
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500545 decoded->attr.s = 1;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500546 else if (end_cmp < 0 && start_cmp > 0)
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500547 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500548
549 /* get the attributes for this character cell */
550 attr = terminal_get_attr_row(terminal, row)[col];
551 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500552 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500553 ((terminal->mode & MODE_SHOW_CURSOR) &&
554 terminal->focused && terminal->row == row &&
555 terminal->column == col)) {
556 foreground = attr.bg;
557 background = attr.fg;
558 if (attr.a & ATTRMASK_BOLD) {
559 if (foreground <= 16) foreground |= 0x08;
560 if (background <= 16) background &= 0x07;
561 }
562 } else {
563 foreground = attr.fg;
564 background = attr.bg;
565 }
566
567 if (terminal->mode & MODE_INVERSE) {
568 tmp = foreground;
569 foreground = background;
570 background = tmp;
571 if (attr.a & ATTRMASK_BOLD) {
572 if (foreground <= 16) foreground |= 0x08;
573 if (background <= 16) background &= 0x07;
574 }
575 }
576
Callum Lowcay9d708b02011-01-12 20:06:17 +1300577 decoded->attr.fg = foreground;
578 decoded->attr.bg = background;
579 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000580}
581
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500582
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500583static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000584terminal_scroll_buffer(struct terminal *terminal, int d)
585{
586 int i;
587
588 d = d % (terminal->height + 1);
589 terminal->start = (terminal->start + d) % terminal->height;
590 if (terminal->start < 0) terminal->start = terminal->height + terminal->start;
591 if(d < 0) {
592 d = 0 - d;
593 for(i = 0; i < d; i++) {
594 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
595 attr_init(terminal_get_attr_row(terminal, i),
596 terminal->curr_attr, terminal->width);
597 }
598 } else {
599 for(i = terminal->height - d; i < terminal->height; i++) {
600 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
601 attr_init(terminal_get_attr_row(terminal, i),
602 terminal->curr_attr, terminal->width);
603 }
604 }
605}
606
607static void
608terminal_scroll_window(struct terminal *terminal, int d)
609{
610 int i;
611 int window_height;
612 int from_row, to_row;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000613
614 // scrolling range is inclusive
615 window_height = terminal->margin_bottom - terminal->margin_top + 1;
616 d = d % (window_height + 1);
617 if(d < 0) {
618 d = 0 - d;
619 to_row = terminal->margin_bottom;
620 from_row = terminal->margin_bottom - d;
621
622 for (i = 0; i < (window_height - d); i++) {
623 memcpy(terminal_get_row(terminal, to_row - i),
624 terminal_get_row(terminal, from_row - i),
625 terminal->data_pitch);
626 memcpy(terminal_get_attr_row(terminal, to_row - i),
627 terminal_get_attr_row(terminal, from_row - i),
628 terminal->attr_pitch);
629 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000630 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
631 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000632 attr_init(terminal_get_attr_row(terminal, i),
633 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000634 }
635 } else {
636 to_row = terminal->margin_top;
637 from_row = terminal->margin_top + d;
638
639 for (i = 0; i < (window_height - d); i++) {
640 memcpy(terminal_get_row(terminal, to_row + i),
641 terminal_get_row(terminal, from_row + i),
642 terminal->data_pitch);
643 memcpy(terminal_get_attr_row(terminal, to_row + i),
644 terminal_get_attr_row(terminal, from_row + i),
645 terminal->attr_pitch);
646 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000647 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
648 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000649 attr_init(terminal_get_attr_row(terminal, i),
650 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000651 }
652 }
653}
654
655static void
656terminal_scroll(struct terminal *terminal, int d)
657{
658 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
659 terminal_scroll_buffer(terminal, d);
660 else
661 terminal_scroll_window(terminal, d);
662}
663
664static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000665terminal_shift_line(struct terminal *terminal, int d)
666{
667 union utf8_char *row;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500668 struct attr *attr_row;
Callum Lowcay69e96582011-01-07 19:47:00 +0000669
670 row = terminal_get_row(terminal, terminal->row);
671 attr_row = terminal_get_attr_row(terminal, terminal->row);
672
673 if ((terminal->width + d) <= terminal->column)
674 d = terminal->column + 1 - terminal->width;
675 if ((terminal->column + d) >= terminal->width)
676 d = terminal->width - terminal->column - 1;
677
678 if (d < 0) {
679 d = 0 - d;
680 memmove(&row[terminal->column],
681 &row[terminal->column + d],
682 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
Callum Lowcay69e96582011-01-07 19:47:00 +0000683 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
684 (terminal->width - terminal->column - d) * sizeof(struct attr));
685 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
686 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
687 } else {
688 memmove(&row[terminal->column + d], &row[terminal->column],
689 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
690 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
691 (terminal->width - terminal->column - d) * sizeof(struct attr));
692 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
693 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
694 }
695}
696
697static void
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500698terminal_resize_cells(struct terminal *terminal, int width, int height)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500699{
700 size_t size;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000701 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000702 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000703 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000704 int data_pitch, attr_pitch;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500705 int i, l, total_rows;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500706 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000707 struct winsize ws;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500708
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500709 if (width < 1)
710 width = 1;
711 if (height < 1)
712 height = 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500713 if (terminal->width == width && terminal->height == height)
714 return;
715
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000716 data_pitch = width * sizeof(union utf8_char);
717 size = data_pitch * height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500718 data = malloc(size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000719 attr_pitch = width * sizeof(struct attr);
720 data_attr = malloc(attr_pitch * height);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000721 tab_ruler = malloc(width);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500722 memset(data, 0, size);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000723 memset(tab_ruler, 0, width);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000724 attr_init(data_attr, terminal->curr_attr, width * height);
725 if (terminal->data && terminal->data_attr) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500726 if (width > terminal->width)
727 l = terminal->width;
728 else
729 l = width;
730
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500731 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500732 total_rows = height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500733 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500734 total_rows = terminal->height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500735 }
736
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000737 for (i = 0; i < total_rows; i++) {
738 memcpy(&data[width * i],
739 terminal_get_row(terminal, i),
740 l * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000741 memcpy(&data_attr[width * i],
742 terminal_get_attr_row(terminal, i),
743 l * sizeof(struct attr));
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000744 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500745
746 free(terminal->data);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000747 free(terminal->data_attr);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000748 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500749 }
750
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000751 terminal->data_pitch = data_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000752 terminal->attr_pitch = attr_pitch;
Callum Lowcay86653ed2011-01-07 19:47:03 +0000753 terminal->margin_bottom =
754 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500755 terminal->width = width;
756 terminal->height = height;
757 terminal->data = data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000758 terminal->data_attr = data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000759 terminal->tab_ruler = tab_ruler;
760 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500761
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000762 /* Update the window size */
763 ws.ws_row = terminal->height;
764 ws.ws_col = terminal->width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500765 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500766 ws.ws_xpixel = allocation.width;
767 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000768 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500769}
770
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500771static void
772resize_handler(struct widget *widget,
773 int32_t width, int32_t height, void *data)
774{
775 struct terminal *terminal = data;
776 int32_t columns, rows, m;
777
Alexander Preisingere2b88c02012-06-18 20:59:26 +0200778 if (width < 200)
779 width = 200;
780
781 if (height < 50)
782 height = 50;
783
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500784 m = 2 * terminal->margin;
785 columns = (width - m) / (int32_t) terminal->extents.max_x_advance;
786 rows = (height - m) / (int32_t) terminal->extents.height;
787
788 if (!terminal->fullscreen) {
789 width = columns * terminal->extents.max_x_advance + m;
790 height = rows * terminal->extents.height + m;
791 widget_set_size(terminal->widget, width, height);
792 }
793
794 terminal_resize_cells(terminal, columns, rows);
795}
796
797static void
798terminal_resize(struct terminal *terminal, int columns, int rows)
799{
800 int32_t width, height, m;
801
802 if (terminal->fullscreen)
803 return;
804
805 m = 2 * terminal->margin;
806 width = columns * terminal->extents.max_x_advance + m;
807 height = rows * terminal->extents.height + m;
Kristian Høgsberga1627922012-06-20 17:30:03 -0400808
809 frame_set_child_size(terminal->widget, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500810}
811
Callum Lowcay30eeae52011-01-07 19:46:55 +0000812struct color_scheme DEFAULT_COLORS = {
813 {
814 {0, 0, 0, 1}, /* black */
815 {0.66, 0, 0, 1}, /* red */
816 {0 , 0.66, 0, 1}, /* green */
817 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
818 {0 , 0 , 0.66, 1}, /* blue */
819 {0.66, 0 , 0.66, 1}, /* magenta */
820 {0, 0.66, 0.66, 1}, /* cyan */
821 {0.66, 0.66, 0.66, 1}, /* light grey */
822 {0.22, 0.33, 0.33, 1}, /* dark grey */
823 {1, 0.33, 0.33, 1}, /* high red */
824 {0.33, 1, 0.33, 1}, /* high green */
825 {1, 1, 0.33, 1}, /* high yellow */
826 {0.33, 0.33, 1, 1}, /* high blue */
827 {1, 0.33, 1, 1}, /* high magenta */
828 {0.33, 1, 1, 1}, /* high cyan */
829 {1, 1, 1, 1} /* white */
830 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500831 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000832 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
833};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400834
Kristian Høgsberg22106762008-12-08 13:50:07 -0500835static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500836terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
837{
838 cairo_set_source_rgba(cr,
839 terminal->color_table[index].r,
840 terminal->color_table[index].g,
841 terminal->color_table[index].b,
842 terminal->color_table[index].a);
843}
844
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500845static void
846terminal_send_selection(struct terminal *terminal, int fd)
847{
848 int row, col;
849 union utf8_char *p_row;
850 union decoded_attr attr;
851 FILE *fp;
852 int len;
853
854 fp = fdopen(fd, "w");
855 for (row = 0; row < terminal->height; row++) {
856 p_row = terminal_get_row(terminal, row);
857 for (col = 0; col < terminal->width; col++) {
858 /* get the attributes for this character cell */
859 terminal_decode_attr(terminal, row, col, &attr);
860 if (!attr.attr.s)
861 continue;
862 len = strnlen((char *) p_row[col].byte, 4);
863 fwrite(p_row[col].byte, 1, len, fp);
864 }
865 }
866 fclose(fp);
867}
868
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500869struct glyph_run {
870 struct terminal *terminal;
871 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400872 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500873 union decoded_attr attr;
874 cairo_glyph_t glyphs[256], *g;
875};
876
877static void
878glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
879{
880 run->terminal = terminal;
881 run->cr = cr;
882 run->g = run->glyphs;
883 run->count = 0;
884 run->attr.key = 0;
885}
886
887static void
888glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
889{
890 cairo_scaled_font_t *font;
891
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500892 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
893 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300894 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500895 font = run->terminal->font_bold;
896 else
897 font = run->terminal->font_normal;
898 cairo_set_scaled_font(run->cr, font);
899 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300900 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500901
Callum Lowcay9d708b02011-01-12 20:06:17 +1300902 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
903 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500904 run->g = run->glyphs;
905 run->count = 0;
906 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300907 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500908}
909
910static void
911glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
912{
913 int num_glyphs;
914 cairo_scaled_font_t *font;
915
916 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
917
Callum Lowcay9d708b02011-01-12 20:06:17 +1300918 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500919 font = run->terminal->font_bold;
920 else
921 font = run->terminal->font_normal;
922
923 cairo_move_to(run->cr, x, y);
924 cairo_scaled_font_text_to_glyphs (font, x, y,
925 (char *) c->byte, 4,
926 &run->g, &num_glyphs,
927 NULL, NULL, NULL);
928 run->g += num_glyphs;
929 run->count += num_glyphs;
930}
931
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500932
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500933static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500934redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500935{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500936 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500937 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500938 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000939 int top_margin, side_margin;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600940 int row, col, cursor_x, cursor_y;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000941 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500942 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000943 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500944 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500945 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500946 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500947 cairo_font_extents_t extents;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500948
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500949 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500950 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500951 cr = cairo_create(surface);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500952 cairo_rectangle(cr, allocation.x, allocation.y,
953 allocation.width, allocation.height);
954 cairo_clip(cr);
955 cairo_push_group(cr);
956
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500957 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500958 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500959 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500960
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500961 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500962
Kristian Høgsberg59826582011-01-20 11:56:57 -0500963 extents = terminal->extents;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500964 side_margin = (allocation.width - terminal->width * extents.max_x_advance) / 2;
965 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500966
Callum Lowcay30eeae52011-01-07 19:46:55 +0000967 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500968 cairo_translate(cr, allocation.x + side_margin,
969 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500970 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000971 for (row = 0; row < terminal->height; row++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000972 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000973 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500974 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000975
Callum Lowcay9d708b02011-01-12 20:06:17 +1300976 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -0500977 continue;
978
Callum Lowcay9d708b02011-01-12 20:06:17 +1300979 terminal_set_color(terminal, cr, attr.attr.bg);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -0500980 cairo_move_to(cr, col * extents.max_x_advance,
981 row * extents.height);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000982 cairo_rel_line_to(cr, extents.max_x_advance, 0);
983 cairo_rel_line_to(cr, 0, extents.height);
984 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
985 cairo_close_path(cr);
986 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500987 }
988 }
Callum Lowcay30eeae52011-01-07 19:46:55 +0000989
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500990 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
991
992 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500993 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500994 for (row = 0; row < terminal->height; row++) {
995 p_row = terminal_get_row(terminal, row);
996 for (col = 0; col < terminal->width; col++) {
997 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500998 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500999
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001000 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001001
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001002 text_x = col * extents.max_x_advance;
1003 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +13001004 if (attr.attr.a & ATTRMASK_UNDERLINE) {
1005 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +00001006 cairo_move_to(cr, text_x, (double)text_y + 1.5);
1007 cairo_line_to(cr, text_x + extents.max_x_advance, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001008 cairo_stroke(cr);
1009 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -05001010
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001011 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001012 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001013 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001014
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001015 attr.key = ~0;
1016 glyph_run_flush(&run, attr);
1017
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001018 if ((terminal->mode & MODE_SHOW_CURSOR) && !terminal->focused) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001019 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001020
Callum Lowcay30eeae52011-01-07 19:46:55 +00001021 cairo_set_line_width(cr, 1);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001022 cairo_move_to(cr, terminal->column * extents.max_x_advance + d,
1023 terminal->row * extents.height + d);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001024 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
1025 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
1026 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
1027 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001028
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001029 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001030 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001031
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001032 cairo_pop_group_to_source(cr);
1033 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001034 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001035 cairo_surface_destroy(surface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001036
1037 if (terminal->send_cursor_position) {
1038 cursor_x = side_margin + allocation.x +
1039 terminal->column * extents.max_x_advance;
1040 cursor_y = top_margin + allocation.y +
1041 terminal->row * extents.height;
1042 window_set_text_cursor_position(terminal->window,
1043 cursor_x, cursor_y);
1044 terminal->send_cursor_position = 0;
1045 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001046}
1047
1048static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001049terminal_write(struct terminal *terminal, const char *data, size_t length)
1050{
1051 if (write(terminal->master, data, length) < 0)
1052 abort();
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001053 terminal->send_cursor_position = 1;
Kristian Høgsberg26130862011-08-24 11:30:21 -04001054}
1055
1056static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001057terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001058
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001059static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001060handle_char(struct terminal *terminal, union utf8_char utf8);
1061
1062static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001063handle_sgr(struct terminal *terminal, int code);
1064
1065static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001066handle_term_parameter(struct terminal *terminal, int code, int sr)
1067{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001068 int i;
1069
Callum Lowcay67a201d2011-01-12 19:23:41 +13001070 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001071 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001072 case 1: /* DECCKM */
1073 if (sr) terminal->key_mode = KM_APPLICATION;
1074 else terminal->key_mode = KM_NORMAL;
1075 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001076 case 2: /* DECANM */
1077 /* No VT52 support yet */
1078 terminal->g0 = CS_US;
1079 terminal->g1 = CS_US;
1080 terminal->cs = terminal->g0;
1081 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001082 case 3: /* DECCOLM */
1083 if (sr)
1084 terminal_resize(terminal, 132, 24);
1085 else
1086 terminal_resize(terminal, 80, 24);
1087
1088 /* set columns, but also home cursor and clear screen */
1089 terminal->row = 0; terminal->column = 0;
1090 for (i = 0; i < terminal->height; i++) {
1091 memset(terminal_get_row(terminal, i),
1092 0, terminal->data_pitch);
1093 attr_init(terminal_get_attr_row(terminal, i),
1094 terminal->curr_attr, terminal->width);
1095 }
1096 break;
1097 case 5: /* DECSCNM */
1098 if (sr) terminal->mode |= MODE_INVERSE;
1099 else terminal->mode &= ~MODE_INVERSE;
1100 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001101 case 6: /* DECOM */
1102 terminal->origin_mode = sr;
1103 if (terminal->origin_mode)
1104 terminal->row = terminal->margin_top;
1105 else
1106 terminal->row = 0;
1107 terminal->column = 0;
1108 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001109 case 7: /* DECAWM */
1110 if (sr) terminal->mode |= MODE_AUTOWRAP;
1111 else terminal->mode &= ~MODE_AUTOWRAP;
1112 break;
1113 case 8: /* DECARM */
1114 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1115 else terminal->mode &= ~MODE_AUTOREPEAT;
1116 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001117 case 12: /* Very visible cursor (CVVIS) */
1118 /* FIXME: What do we do here. */
1119 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001120 case 25:
1121 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1122 else terminal->mode &= ~MODE_SHOW_CURSOR;
1123 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001124 case 1034: /* smm/rmm, meta mode on/off */
1125 /* ignore */
1126 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001127 case 1037: /* deleteSendsDel */
1128 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1129 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1130 break;
1131 case 1039: /* altSendsEscape */
1132 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1133 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1134 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001135 case 1049: /* rmcup/smcup, alternate screen */
1136 /* Ignore. Should be possible to implement,
1137 * but it's kind of annoying. */
1138 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001139 default:
1140 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1141 break;
1142 }
1143 } else {
1144 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001145 case 4: /* IRM */
1146 if (sr) terminal->mode |= MODE_IRM;
1147 else terminal->mode &= ~MODE_IRM;
1148 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001149 case 20: /* LNM */
1150 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1151 else terminal->mode &= ~MODE_LF_NEWLINE;
1152 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001153 default:
1154 fprintf(stderr, "Unknown parameter: %d\n", code);
1155 break;
1156 }
1157 }
1158}
1159
1160static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001161handle_dcs(struct terminal *terminal)
1162{
1163}
1164
1165static void
1166handle_osc(struct terminal *terminal)
1167{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001168 char *p;
1169 int code;
1170
1171 terminal->escape[terminal->escape_length++] = '\0';
1172 p = &terminal->escape[2];
1173 code = strtol(p, &p, 10);
1174 if (*p == ';') p++;
1175
1176 switch (code) {
1177 case 0: /* Icon name and window title */
1178 case 1: /* Icon label */
1179 case 2: /* Window title*/
1180 window_set_title(terminal->window, p);
1181 break;
1182 default:
1183 fprintf(stderr, "Unknown OSC escape code %d\n", code);
1184 break;
1185 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001186}
1187
1188static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001189handle_escape(struct terminal *terminal)
1190{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001191 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001192 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001193 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001194 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001195 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001196 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001197 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001198
1199 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001200 i = 0;
1201 p = &terminal->escape[2];
1202 while ((isdigit(*p) || *p == ';') && i < 10) {
1203 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001204 if (!set[i]) {
1205 args[i] = 0;
1206 set[i] = 1;
1207 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001208 p++;
1209 i++;
1210 } else {
1211 args[i] = strtol(p, &p, 10);
1212 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001213 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001214 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001215
1216 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001217 case '@': /* ICH */
1218 count = set[0] ? args[0] : 1;
1219 if (count == 0) count = 1;
1220 terminal_shift_line(terminal, count);
1221 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001222 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001223 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001224 if (count == 0) count = 1;
1225 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001226 terminal->row -= count;
1227 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001228 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001229 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001230 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001231 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001232 if (count == 0) count = 1;
1233 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001234 terminal->row += count;
1235 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001236 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001237 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001238 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001239 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001240 if (count == 0) count = 1;
1241 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001242 terminal->column += count;
1243 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001244 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001245 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001246 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001247 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001248 if (count == 0) count = 1;
1249 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001250 terminal->column -= count;
1251 else
1252 terminal->column = 0;
1253 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001254 case 'E': /* CNL */
1255 count = set[0] ? args[0] : 1;
1256 if (terminal->row + count <= terminal->margin_bottom)
1257 terminal->row += count;
1258 else
1259 terminal->row = terminal->margin_bottom;
1260 terminal->column = 0;
1261 break;
1262 case 'F': /* CPL */
1263 count = set[0] ? args[0] : 1;
1264 if (terminal->row - count >= terminal->margin_top)
1265 terminal->row -= count;
1266 else
1267 terminal->row = terminal->margin_top;
1268 terminal->column = 0;
1269 break;
1270 case 'G': /* CHA */
1271 y = set[0] ? args[0] : 1;
1272 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1273
1274 terminal->column = y - 1;
1275 break;
1276 case 'f': /* HVP */
1277 case 'H': /* CUP */
1278 x = (set[1] ? args[1] : 1) - 1;
1279 x = x < 0 ? 0 :
1280 (x >= terminal->width ? terminal->width - 1 : x);
1281
1282 y = (set[0] ? args[0] : 1) - 1;
1283 if (terminal->origin_mode) {
1284 y += terminal->margin_top;
1285 y = y < terminal->margin_top ? terminal->margin_top :
1286 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1287 } else {
1288 y = y < 0 ? 0 :
1289 (y >= terminal->height ? terminal->height - 1 : y);
1290 }
1291
1292 terminal->row = y;
1293 terminal->column = x;
1294 break;
1295 case 'I': /* CHT */
1296 count = set[0] ? args[0] : 1;
1297 if (count == 0) count = 1;
1298 while (count > 0 && terminal->column < terminal->width) {
1299 if (terminal->tab_ruler[terminal->column]) count--;
1300 terminal->column++;
1301 }
1302 terminal->column--;
1303 break;
1304 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001305 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001306 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001307 if (!set[0] || args[0] == 0 || args[0] > 2) {
1308 memset(&row[terminal->column],
1309 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1310 attr_init(&attr_row[terminal->column],
1311 terminal->curr_attr, terminal->width - terminal->column);
1312 for (i = terminal->row + 1; i < terminal->height; i++) {
1313 memset(terminal_get_row(terminal, i),
1314 0, terminal->data_pitch);
1315 attr_init(terminal_get_attr_row(terminal, i),
1316 terminal->curr_attr, terminal->width);
1317 }
1318 } else if (args[0] == 1) {
1319 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1320 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1321 for (i = 0; i < terminal->row; i++) {
1322 memset(terminal_get_row(terminal, i),
1323 0, terminal->data_pitch);
1324 attr_init(terminal_get_attr_row(terminal, i),
1325 terminal->curr_attr, terminal->width);
1326 }
1327 } else if (args[0] == 2) {
1328 for (i = 0; i < terminal->height; i++) {
1329 memset(terminal_get_row(terminal, i),
1330 0, terminal->data_pitch);
1331 attr_init(terminal_get_attr_row(terminal, i),
1332 terminal->curr_attr, terminal->width);
1333 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001334 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001335 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001336 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001337 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001338 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001339 if (!set[0] || args[0] == 0 || args[0] > 2) {
1340 memset(&row[terminal->column], 0,
1341 (terminal->width - terminal->column) * sizeof(union utf8_char));
1342 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1343 terminal->width - terminal->column);
1344 } else if (args[0] == 1) {
1345 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1346 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1347 } else if (args[0] == 2) {
1348 memset(row, 0, terminal->data_pitch);
1349 attr_init(attr_row, terminal->curr_attr, terminal->width);
1350 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001351 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001352 case 'L': /* IL */
1353 count = set[0] ? args[0] : 1;
1354 if (count == 0) count = 1;
1355 if (terminal->row >= terminal->margin_top &&
1356 terminal->row < terminal->margin_bottom)
1357 {
1358 top = terminal->margin_top;
1359 terminal->margin_top = terminal->row;
1360 terminal_scroll(terminal, 0 - count);
1361 terminal->margin_top = top;
1362 } else if (terminal->row == terminal->margin_bottom) {
1363 memset(terminal_get_row(terminal, terminal->row),
1364 0, terminal->data_pitch);
1365 attr_init(terminal_get_attr_row(terminal, terminal->row),
1366 terminal->curr_attr, terminal->width);
1367 }
1368 break;
1369 case 'M': /* DL */
1370 count = set[0] ? args[0] : 1;
1371 if (count == 0) count = 1;
1372 if (terminal->row >= terminal->margin_top &&
1373 terminal->row < terminal->margin_bottom)
1374 {
1375 top = terminal->margin_top;
1376 terminal->margin_top = terminal->row;
1377 terminal_scroll(terminal, count);
1378 terminal->margin_top = top;
1379 } else if (terminal->row == terminal->margin_bottom) {
1380 memset(terminal_get_row(terminal, terminal->row),
1381 0, terminal->data_pitch);
1382 }
1383 break;
1384 case 'P': /* DCH */
1385 count = set[0] ? args[0] : 1;
1386 if (count == 0) count = 1;
1387 terminal_shift_line(terminal, 0 - count);
1388 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001389 case 'S': /* SU */
1390 terminal_scroll(terminal, set[0] ? args[0] : 1);
1391 break;
1392 case 'T': /* SD */
1393 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1394 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001395 case 'X': /* ECH */
1396 count = set[0] ? args[0] : 1;
1397 if (count == 0) count = 1;
1398 if ((terminal->column + count) > terminal->width)
1399 count = terminal->width - terminal->column;
1400 row = terminal_get_row(terminal, terminal->row);
1401 attr_row = terminal_get_attr_row(terminal, terminal->row);
1402 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1403 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1404 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001405 case 'Z': /* CBT */
1406 count = set[0] ? args[0] : 1;
1407 if (count == 0) count = 1;
1408 while (count > 0 && terminal->column >= 0) {
1409 if (terminal->tab_ruler[terminal->column]) count--;
1410 terminal->column--;
1411 }
1412 terminal->column++;
1413 break;
1414 case '`': /* HPA */
1415 y = set[0] ? args[0] : 1;
1416 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1417
1418 terminal->column = y - 1;
1419 break;
1420 case 'b': /* REP */
1421 count = set[0] ? args[0] : 1;
1422 if (count == 0) count = 1;
1423 if (terminal->last_char.byte[0])
1424 for (i = 0; i < count; i++)
1425 handle_char(terminal, terminal->last_char);
1426 terminal->last_char.byte[0] = 0;
1427 break;
1428 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001429 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001430 break;
1431 case 'd': /* VPA */
1432 x = set[0] ? args[0] : 1;
1433 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1434
1435 terminal->row = x - 1;
1436 break;
1437 case 'g': /* TBC */
1438 if (!set[0] || args[0] == 0) {
1439 terminal->tab_ruler[terminal->column] = 0;
1440 } else if (args[0] == 3) {
1441 memset(terminal->tab_ruler, 0, terminal->width);
1442 }
1443 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001444 case 'h': /* SM */
1445 for(i = 0; i < 10 && set[i]; i++) {
1446 handle_term_parameter(terminal, args[i], 1);
1447 }
1448 break;
1449 case 'l': /* RM */
1450 for(i = 0; i < 10 && set[i]; i++) {
1451 handle_term_parameter(terminal, args[i], 0);
1452 }
1453 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001454 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001455 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001456 if (i <= 7 && set[i] && set[i + 1] &&
1457 set[i + 2] && args[i + 1] == 5)
1458 {
1459 if (args[i] == 38) {
1460 handle_sgr(terminal, args[i + 2] + 256);
1461 break;
1462 } else if (args[i] == 48) {
1463 handle_sgr(terminal, args[i + 2] + 512);
1464 break;
1465 }
1466 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001467 if(set[i]) {
1468 handle_sgr(terminal, args[i]);
1469 } else if(i == 0) {
1470 handle_sgr(terminal, 0);
1471 break;
1472 } else {
1473 break;
1474 }
1475 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001476 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001477 case 'n': /* DSR */
1478 i = set[0] ? args[0] : 0;
1479 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001480 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001481 } else if (i == 6) {
1482 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1483 terminal->origin_mode ?
1484 terminal->row+terminal->margin_top : terminal->row+1,
1485 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001486 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001487 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001488 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001489 case 'r':
1490 if(!set[0]) {
1491 terminal->margin_top = 0;
1492 terminal->margin_bottom = terminal->height-1;
1493 terminal->row = 0;
1494 terminal->column = 0;
1495 } else {
1496 top = (set[0] ? args[0] : 1) - 1;
1497 top = top < 0 ? 0 :
1498 (top >= terminal->height ? terminal->height - 1 : top);
1499 bottom = (set[1] ? args[1] : 1) - 1;
1500 bottom = bottom < 0 ? 0 :
1501 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1502 if(bottom > top) {
1503 terminal->margin_top = top;
1504 terminal->margin_bottom = bottom;
1505 } else {
1506 terminal->margin_top = 0;
1507 terminal->margin_bottom = terminal->height-1;
1508 }
1509 if(terminal->origin_mode)
1510 terminal->row = terminal->margin_top;
1511 else
1512 terminal->row = 0;
1513 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001514 }
1515 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001516 case 's':
1517 terminal->saved_row = terminal->row;
1518 terminal->saved_column = terminal->column;
1519 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001520 case 't': /* windowOps */
1521 if (!set[0]) break;
1522 switch (args[0]) {
1523 case 4: /* resize px */
1524 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001525 widget_schedule_resize(terminal->widget,
1526 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001527 }
1528 break;
1529 case 8: /* resize ch */
1530 if (set[1] && set[2]) {
1531 terminal_resize(terminal, args[2], args[1]);
1532 }
1533 break;
1534 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001535 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001536 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1537 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001538 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001539 break;
1540 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001541 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001542 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1543 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001544 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001545 break;
1546 case 18: /* report ch */
1547 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1548 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001549 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001550 break;
1551 case 21: /* report title */
1552 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1553 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001554 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001555 break;
1556 default:
1557 if (args[0] >= 24)
1558 terminal_resize(terminal, terminal->width, args[0]);
1559 else
1560 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1561 break;
1562 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001563 case 'u':
1564 terminal->row = terminal->saved_row;
1565 terminal->column = terminal->saved_column;
1566 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001567 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001568 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001569 break;
1570 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001571}
1572
1573static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001574handle_non_csi_escape(struct terminal *terminal, char code)
1575{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001576 switch(code) {
1577 case 'M': /* RI */
1578 terminal->row -= 1;
1579 if(terminal->row < terminal->margin_top) {
1580 terminal->row = terminal->margin_top;
1581 terminal_scroll(terminal, -1);
1582 }
1583 break;
1584 case 'E': /* NEL */
1585 terminal->column = 0;
1586 // fallthrough
1587 case 'D': /* IND */
1588 terminal->row += 1;
1589 if(terminal->row > terminal->margin_bottom) {
1590 terminal->row = terminal->margin_bottom;
1591 terminal_scroll(terminal, +1);
1592 }
1593 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001594 case 'c': /* RIS */
1595 terminal_init(terminal);
1596 break;
1597 case 'H': /* HTS */
1598 terminal->tab_ruler[terminal->column] = 1;
1599 break;
1600 case '7': /* DECSC */
1601 terminal->saved_row = terminal->row;
1602 terminal->saved_column = terminal->column;
1603 terminal->saved_attr = terminal->curr_attr;
1604 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001605 terminal->saved_cs = terminal->cs;
1606 terminal->saved_g0 = terminal->g0;
1607 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001608 break;
1609 case '8': /* DECRC */
1610 terminal->row = terminal->saved_row;
1611 terminal->column = terminal->saved_column;
1612 terminal->curr_attr = terminal->saved_attr;
1613 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001614 terminal->cs = terminal->saved_cs;
1615 terminal->g0 = terminal->saved_g0;
1616 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001617 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001618 case '=': /* DECPAM */
1619 terminal->key_mode = KM_APPLICATION;
1620 break;
1621 case '>': /* DECPNM */
1622 terminal->key_mode = KM_NORMAL;
1623 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001624 default:
1625 fprintf(stderr, "Unknown escape code: %c\n", code);
1626 break;
1627 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001628}
1629
1630static void
1631handle_special_escape(struct terminal *terminal, char special, char code)
1632{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001633 int i, numChars;
1634
1635 if (special == '#') {
1636 switch(code) {
1637 case '8':
1638 /* fill with 'E', no cheap way to do this */
1639 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1640 numChars = terminal->width * terminal->height;
1641 for(i = 0; i < numChars; i++) {
1642 terminal->data[i].byte[0] = 'E';
1643 }
1644 break;
1645 default:
1646 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1647 break;
1648 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001649 } else if (special == '(' || special == ')') {
1650 switch(code) {
1651 case '0':
1652 if (special == '(')
1653 terminal->g0 = CS_SPECIAL;
1654 else
1655 terminal->g1 = CS_SPECIAL;
1656 break;
1657 case 'A':
1658 if (special == '(')
1659 terminal->g0 = CS_UK;
1660 else
1661 terminal->g1 = CS_UK;
1662 break;
1663 case 'B':
1664 if (special == '(')
1665 terminal->g0 = CS_US;
1666 else
1667 terminal->g1 = CS_US;
1668 break;
1669 default:
1670 fprintf(stderr, "Unknown character set %c\n", code);
1671 break;
1672 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001673 } else {
1674 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1675 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001676}
1677
1678static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001679handle_sgr(struct terminal *terminal, int code)
1680{
1681 switch(code) {
1682 case 0:
1683 terminal->curr_attr = terminal->color_scheme->default_attr;
1684 break;
1685 case 1:
1686 terminal->curr_attr.a |= ATTRMASK_BOLD;
1687 if (terminal->curr_attr.fg < 8)
1688 terminal->curr_attr.fg += 8;
1689 break;
1690 case 4:
1691 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1692 break;
1693 case 5:
1694 terminal->curr_attr.a |= ATTRMASK_BLINK;
1695 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001696 case 8:
1697 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1698 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001699 case 2:
1700 case 21:
1701 case 22:
1702 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1703 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1704 terminal->curr_attr.fg -= 8;
1705 break;
1706 case 24:
1707 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1708 break;
1709 case 25:
1710 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1711 break;
1712 case 7:
1713 case 26:
1714 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1715 break;
1716 case 27:
1717 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1718 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001719 case 28:
1720 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1721 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001722 case 39:
1723 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1724 break;
1725 case 49:
1726 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1727 break;
1728 default:
1729 if(code >= 30 && code <= 37) {
1730 terminal->curr_attr.fg = code - 30;
1731 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1732 terminal->curr_attr.fg += 8;
1733 } else if(code >= 40 && code <= 47) {
1734 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001735 } else if (code >= 90 && code <= 97) {
1736 terminal->curr_attr.fg = code - 90 + 8;
1737 } else if (code >= 100 && code <= 107) {
1738 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001739 } else if(code >= 256 && code < 512) {
1740 terminal->curr_attr.fg = code - 256;
1741 } else if(code >= 512 && code < 768) {
1742 terminal->curr_attr.bg = code - 512;
1743 } else {
1744 fprintf(stderr, "Unknown SGR code: %d\n", code);
1745 }
1746 break;
1747 }
1748}
1749
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001750/* Returns 1 if c was special, otherwise 0 */
1751static int
1752handle_special_char(struct terminal *terminal, char c)
1753{
1754 union utf8_char *row;
1755 struct attr *attr_row;
1756
1757 row = terminal_get_row(terminal, terminal->row);
1758 attr_row = terminal_get_attr_row(terminal, terminal->row);
1759
1760 switch(c) {
1761 case '\r':
1762 terminal->column = 0;
1763 break;
1764 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001765 if (terminal->mode & MODE_LF_NEWLINE) {
1766 terminal->column = 0;
1767 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001768 /* fallthrough */
1769 case '\v':
1770 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001771 terminal->row++;
1772 if(terminal->row > terminal->margin_bottom) {
1773 terminal->row = terminal->margin_bottom;
1774 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001775 }
1776
1777 break;
1778 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001779 while (terminal->column < terminal->width) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001780 if (terminal->mode & MODE_IRM)
1781 terminal_shift_line(terminal, +1);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001782 row[terminal->column].byte[0] = ' ';
1783 row[terminal->column].byte[1] = '\0';
1784 attr_row[terminal->column] = terminal->curr_attr;
1785 terminal->column++;
Kristian Høgsbergcca3c2f2012-06-20 15:56:13 -04001786 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001787 }
1788 if (terminal->column >= terminal->width) {
1789 terminal->column = terminal->width - 1;
1790 }
1791
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001792 break;
1793 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001794 if (terminal->column >= terminal->width) {
1795 terminal->column = terminal->width - 2;
1796 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001797 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001798 } else if (terminal->mode & MODE_AUTOWRAP) {
1799 terminal->column = terminal->width - 1;
1800 terminal->row -= 1;
1801 if (terminal->row < terminal->margin_top) {
1802 terminal->row = terminal->margin_top;
1803 terminal_scroll(terminal, -1);
1804 }
1805 }
1806
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001807 break;
1808 case '\a':
1809 /* Bell */
1810 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001811 case '\x0E': /* SO */
1812 terminal->cs = terminal->g1;
1813 break;
1814 case '\x0F': /* SI */
1815 terminal->cs = terminal->g0;
1816 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001817 default:
1818 return 0;
1819 }
1820
1821 return 1;
1822}
1823
1824static void
1825handle_char(struct terminal *terminal, union utf8_char utf8)
1826{
1827 union utf8_char *row;
1828 struct attr *attr_row;
1829
1830 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001831
1832 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001833
1834 /* There are a whole lot of non-characters, control codes,
1835 * and formatting codes that should probably be ignored,
1836 * for example: */
1837 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1838 /* BOM, ignore */
1839 return;
1840 }
1841
1842 /* Some of these non-characters should be translated, e.g.: */
1843 if (utf8.byte[0] < 32) {
1844 utf8.byte[0] = utf8.byte[0] + 64;
1845 }
1846
1847 /* handle right margin effects */
1848 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001849 if (terminal->mode & MODE_AUTOWRAP) {
1850 terminal->column = 0;
1851 terminal->row += 1;
1852 if (terminal->row > terminal->margin_bottom) {
1853 terminal->row = terminal->margin_bottom;
1854 terminal_scroll(terminal, +1);
1855 }
1856 } else {
1857 terminal->column--;
1858 }
1859 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001860
1861 row = terminal_get_row(terminal, terminal->row);
1862 attr_row = terminal_get_attr_row(terminal, terminal->row);
1863
Callum Lowcay69e96582011-01-07 19:47:00 +00001864 if (terminal->mode & MODE_IRM)
1865 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001866 row[terminal->column] = utf8;
1867 attr_row[terminal->column++] = terminal->curr_attr;
1868
1869 if (utf8.ch != terminal->last_char.ch)
1870 terminal->last_char = utf8;
1871}
1872
Callum Lowcay30eeae52011-01-07 19:46:55 +00001873static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001874escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1875{
1876 int len, i;
1877
1878 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1879 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1880 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1881 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1882 else len = 1; /* Invalid, cannot happen */
1883
1884 if (terminal->escape_length + len <= MAX_ESCAPE) {
1885 for (i = 0; i < len; i++)
1886 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1887 terminal->escape_length += len;
1888 } else if (terminal->escape_length < MAX_ESCAPE) {
1889 terminal->escape[terminal->escape_length++] = 0;
1890 }
1891}
1892
1893static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001894terminal_data(struct terminal *terminal, const char *data, size_t length)
1895{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001896 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001897 union utf8_char utf8;
1898 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001899
1900 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001901 parser_state =
1902 utf8_next_char(&terminal->state_machine, data[i]);
1903 switch(parser_state) {
1904 case utf8state_accept:
1905 utf8.ch = terminal->state_machine.s.ch;
1906 break;
1907 case utf8state_reject:
1908 /* the unicode replacement character */
1909 utf8.byte[0] = 0xEF;
1910 utf8.byte[1] = 0xBF;
1911 utf8.byte[2] = 0xBD;
1912 utf8.byte[3] = 0x00;
1913 break;
1914 default:
1915 continue;
1916 }
1917
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001918 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001919 switch (terminal->state) {
1920 case escape_state_escape:
1921 escape_append_utf8(terminal, utf8);
1922 switch (utf8.byte[0]) {
1923 case 'P': /* DCS */
1924 terminal->state = escape_state_dcs;
1925 break;
1926 case '[': /* CSI */
1927 terminal->state = escape_state_csi;
1928 break;
1929 case ']': /* OSC */
1930 terminal->state = escape_state_osc;
1931 break;
1932 case '#':
1933 case '(':
1934 case ')': /* special */
1935 terminal->state = escape_state_special;
1936 break;
1937 case '^': /* PM (not implemented) */
1938 case '_': /* APC (not implemented) */
1939 terminal->state = escape_state_ignore;
1940 break;
1941 default:
1942 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001943 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13001944 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001945 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001946 continue;
1947 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001948 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1949 /* do nothing */
1950 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001951 terminal->escape_flags |= ESC_FLAG_WHAT;
1952 } else if (utf8.byte[0] == '>') {
1953 terminal->escape_flags |= ESC_FLAG_GT;
1954 } else if (utf8.byte[0] == '!') {
1955 terminal->escape_flags |= ESC_FLAG_BANG;
1956 } else if (utf8.byte[0] == '$') {
1957 terminal->escape_flags |= ESC_FLAG_CASH;
1958 } else if (utf8.byte[0] == '\'') {
1959 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1960 } else if (utf8.byte[0] == '"') {
1961 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1962 } else if (utf8.byte[0] == ' ') {
1963 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001964 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001965 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001966 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13001967 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001968 }
1969
1970 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1971 utf8.byte[0] == '`')
1972 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001973 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001974 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001975 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001976 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001977 continue;
1978 case escape_state_inner_escape:
1979 if (utf8.byte[0] == '\\') {
1980 terminal->state = escape_state_normal;
1981 if (terminal->outer_state == escape_state_dcs) {
1982 handle_dcs(terminal);
1983 } else if (terminal->outer_state == escape_state_osc) {
1984 handle_osc(terminal);
1985 }
1986 } else if (utf8.byte[0] == '\e') {
1987 terminal->state = terminal->outer_state;
1988 escape_append_utf8(terminal, utf8);
1989 if (terminal->escape_length >= MAX_ESCAPE)
1990 terminal->state = escape_state_normal;
1991 } else {
1992 terminal->state = terminal->outer_state;
1993 if (terminal->escape_length < MAX_ESCAPE)
1994 terminal->escape[terminal->escape_length++] = '\e';
1995 escape_append_utf8(terminal, utf8);
1996 if (terminal->escape_length >= MAX_ESCAPE)
1997 terminal->state = escape_state_normal;
1998 }
1999 continue;
2000 case escape_state_dcs:
2001 case escape_state_osc:
2002 case escape_state_ignore:
2003 if (utf8.byte[0] == '\e') {
2004 terminal->outer_state = terminal->state;
2005 terminal->state = escape_state_inner_escape;
2006 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
2007 terminal->state = escape_state_normal;
2008 handle_osc(terminal);
2009 } else {
2010 escape_append_utf8(terminal, utf8);
2011 if (terminal->escape_length >= MAX_ESCAPE)
2012 terminal->state = escape_state_normal;
2013 }
2014 continue;
2015 case escape_state_special:
2016 escape_append_utf8(terminal, utf8);
2017 terminal->state = escape_state_normal;
2018 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2019 handle_special_escape(terminal, terminal->escape[1],
2020 utf8.byte[0]);
2021 }
2022 continue;
2023 default:
2024 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002025 }
2026
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002027 /* this is valid, because ASCII characters are never used to
2028 * introduce a multibyte sequence in UTF-8 */
2029 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002030 terminal->state = escape_state_escape;
2031 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002032 terminal->escape[0] = '\e';
2033 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002034 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002035 } else {
2036 handle_char(terminal, utf8);
2037 } /* if */
2038 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002039
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002040 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002041}
2042
2043static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002044data_source_target(void *data,
2045 struct wl_data_source *source, const char *mime_type)
2046{
2047 fprintf(stderr, "data_source_target, %s\n", mime_type);
2048}
2049
2050static void
2051data_source_send(void *data,
2052 struct wl_data_source *source,
2053 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002054{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002055 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002056
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002057 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002058}
2059
2060static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002061data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002062{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002063 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002064}
2065
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002066static const struct wl_data_source_listener data_source_listener = {
2067 data_source_target,
2068 data_source_send,
2069 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002070};
2071
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002072static int
2073handle_bound_key(struct terminal *terminal,
2074 struct input *input, uint32_t sym, uint32_t time)
2075{
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002076 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002077 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002078 /* Cut selection; terminal doesn't do cut, fall
2079 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002080 case XKB_KEY_C:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002081 terminal->selection =
2082 display_create_data_source(terminal->display);
2083 wl_data_source_offer(terminal->selection,
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002084 "text/plain;charset=utf-8");
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002085 wl_data_source_add_listener(terminal->selection,
2086 &data_source_listener, terminal);
Kristian Høgsbergfee91be2012-06-02 21:21:41 -04002087 input_set_selection(input, terminal->selection,
2088 display_get_serial(terminal->display));
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002089 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002090 case XKB_KEY_V:
Kristian Høgsberge7aaec32011-12-27 13:50:04 -05002091 input_receive_selection_data_to_fd(input,
2092 "text/plain;charset=utf-8",
2093 terminal->master);
2094
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002095 return 1;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002096 default:
2097 return 0;
2098 }
2099}
2100
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002101static void
2102key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002103 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2104 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002105{
2106 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002107 char ch[MAX_RESPONSE];
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002108 uint32_t modifiers;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002109 int len = 0;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002110
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002111 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002112 if ((modifiers & MOD_CONTROL_MASK) &&
2113 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002114 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2115 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002116 return;
2117
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002118 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002119 case XKB_KEY_F11:
Daniel Stonec9785ea2012-05-30 16:31:52 +01002120 if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002121 break;
2122 terminal->fullscreen ^= 1;
2123 window_set_fullscreen(window, terminal->fullscreen);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002124 break;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002125
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002126 case XKB_KEY_BackSpace:
Kristian Høgsberg71a4cf42012-06-20 17:57:56 -04002127 ch[len++] = 0x7f;
2128 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002129 case XKB_KEY_Tab:
2130 case XKB_KEY_Linefeed:
2131 case XKB_KEY_Clear:
2132 case XKB_KEY_Pause:
2133 case XKB_KEY_Scroll_Lock:
2134 case XKB_KEY_Sys_Req:
2135 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002136 ch[len++] = sym & 0x7f;
2137 break;
2138
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002139 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002140 if (terminal->mode & MODE_LF_NEWLINE) {
2141 ch[len++] = 0x0D;
2142 ch[len++] = 0x0A;
2143 } else {
2144 ch[len++] = 0x0D;
2145 }
2146 break;
2147
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002148 case XKB_KEY_Shift_L:
2149 case XKB_KEY_Shift_R:
2150 case XKB_KEY_Control_L:
2151 case XKB_KEY_Control_R:
2152 case XKB_KEY_Alt_L:
2153 case XKB_KEY_Alt_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002154 break;
2155
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002156 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002157 len = function_key_response('[', 2, modifiers, '~', ch);
2158 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002159 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002160 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2161 ch[len++] = '\x04';
2162 } else {
2163 len = function_key_response('[', 3, modifiers, '~', ch);
2164 }
2165 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002166 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002167 len = function_key_response('[', 5, modifiers, '~', ch);
2168 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002169 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002170 len = function_key_response('[', 6, modifiers, '~', ch);
2171 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002172 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002173 len = function_key_response('O', 1, modifiers, 'P', ch);
2174 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002175 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002176 len = function_key_response('O', 1, modifiers, 'Q', ch);
2177 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002178 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002179 len = function_key_response('O', 1, modifiers, 'R', ch);
2180 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002181 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002182 len = function_key_response('O', 1, modifiers, 'S', ch);
2183 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002184 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002185 len = function_key_response('[', 15, modifiers, '~', ch);
2186 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002187 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002188 len = function_key_response('[', 17, modifiers, '~', ch);
2189 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002190 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002191 len = function_key_response('[', 18, modifiers, '~', ch);
2192 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002193 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002194 len = function_key_response('[', 19, modifiers, '~', ch);
2195 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002196 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002197 len = function_key_response('[', 20, modifiers, '~', ch);
2198 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002199 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002200 len = function_key_response('[', 21, modifiers, '~', ch);
2201 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002202 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002203 len = function_key_response('[', 24, modifiers, '~', ch);
2204 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002205 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002206 /* Handle special keys with alternate mappings */
2207 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2208 if (len != 0) break;
2209
Kristian Høgsberg70163132012-05-08 15:55:39 -04002210 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002211 if (sym >= '3' && sym <= '7')
2212 sym = (sym & 0x1f) + 8;
2213
2214 if (!((sym >= '!' && sym <= '/') ||
2215 (sym >= '8' && sym <= '?') ||
2216 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2217 else if (sym == '2') sym = 0x00;
2218 else if (sym == '/') sym = 0x1F;
2219 else if (sym == '8' || sym == '?') sym = 0x7F;
2220 } else if ((terminal->mode & MODE_ALT_SENDS_ESC) &&
Kristian Høgsberg70163132012-05-08 15:55:39 -04002221 (modifiers & MOD_ALT_MASK))
Callum Lowcay7e08e902011-01-07 19:47:02 +00002222 {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002223 ch[len++] = 0x1b;
Kristian Høgsberg70163132012-05-08 15:55:39 -04002224 } else if (modifiers & MOD_ALT_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002225 sym = sym | 0x80;
2226 }
2227
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002228 if (sym < 256)
2229 ch[len++] = sym;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002230 break;
2231 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002232
Daniel Stonec9785ea2012-05-30 16:31:52 +01002233 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0)
Kristian Høgsberg26130862011-08-24 11:30:21 -04002234 terminal_write(terminal, ch, len);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002235}
2236
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002237static void
2238keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002239 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002240{
2241 struct terminal *terminal = data;
2242
2243 terminal->focused = (device != NULL);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002244 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002245}
2246
Kristian Høgsberg59826582011-01-20 11:56:57 -05002247static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002248button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002249 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002250 uint32_t button,
2251 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002252{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002253 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002254
2255 switch (button) {
2256 case 272:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002257 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002258 terminal->dragging = 1;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002259 input_get_position(input,
2260 &terminal->selection_start_x,
2261 &terminal->selection_start_y);
2262 terminal->selection_end_x = terminal->selection_start_x;
2263 terminal->selection_end_y = terminal->selection_start_y;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002264 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002265 } else {
2266 terminal->dragging = 0;
2267 }
2268 break;
2269 }
2270}
2271
2272static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002273motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002274 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002275 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002276{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002277 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002278
2279 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002280 input_get_position(input,
2281 &terminal->selection_end_x,
2282 &terminal->selection_end_y);
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002283 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002284 }
2285
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002286 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002287}
2288
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002289static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002290terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002291{
2292 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002293 cairo_surface_t *surface;
2294 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002295
2296 terminal = malloc(sizeof *terminal);
2297 if (terminal == NULL)
2298 return terminal;
2299
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002300 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002301 terminal->fullscreen = fullscreen;
Callum Lowcay30eeae52011-01-07 19:46:55 +00002302 terminal->color_scheme = &DEFAULT_COLORS;
2303 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002304 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002305 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002306 terminal->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -05002307 terminal->widget = frame_create(terminal->window, terminal);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -05002308 window_set_title(terminal->window, "Wayland Terminal");
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002309 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002310
2311 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002312 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002313
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002314 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002315 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002316
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002317 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002318 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002319 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002320 keyboard_focus_handler);
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002321 widget_set_redraw_handler(terminal->widget, redraw_handler);
2322 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002323 widget_set_button_handler(terminal->widget, button_handler);
2324 widget_set_motion_handler(terminal->widget, motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002325
Kristian Høgsberg09531622010-06-14 23:22:15 -04002326 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2327 cr = cairo_create(surface);
Kristian Høgsberg09531622010-06-14 23:22:15 -04002328 cairo_set_font_size(cr, 14);
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002329 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002330 CAIRO_FONT_SLANT_NORMAL,
2331 CAIRO_FONT_WEIGHT_BOLD);
2332 terminal->font_bold = cairo_get_scaled_font (cr);
2333 cairo_scaled_font_reference(terminal->font_bold);
2334
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002335 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002336 CAIRO_FONT_SLANT_NORMAL,
2337 CAIRO_FONT_WEIGHT_NORMAL);
2338 terminal->font_normal = cairo_get_scaled_font (cr);
2339 cairo_scaled_font_reference(terminal->font_normal);
2340
Kristian Høgsberg09531622010-06-14 23:22:15 -04002341 cairo_font_extents(cr, &terminal->extents);
2342 cairo_destroy(cr);
2343 cairo_surface_destroy(surface);
2344
Kristian Høgsberga1627922012-06-20 17:30:03 -04002345 terminal_resize(terminal, 80, 25);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002346
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002347 return terminal;
2348}
2349
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002350static void
2351io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002352{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002353 struct terminal *terminal =
2354 container_of(task, struct terminal, io_task);
2355 char buffer[256];
2356 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002357
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002358 if (events & EPOLLHUP)
2359 exit(0);
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002360
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002361 len = read(terminal->master, buffer, sizeof buffer);
2362 if (len < 0)
2363 exit(0);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002364
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002365 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002366}
2367
2368static int
2369terminal_run(struct terminal *terminal, const char *path)
2370{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002371 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002372 pid_t pid;
2373
2374 pid = forkpty(&master, NULL, NULL, NULL);
2375 if (pid == 0) {
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04002376 setenv("TERM", option_term, 1);
2377 setenv("COLORTERM", option_term, 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002378 if (execl(path, path, NULL)) {
2379 printf("exec failed: %m\n");
2380 exit(EXIT_FAILURE);
2381 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002382 } else if (pid < 0) {
2383 fprintf(stderr, "failed to fork and create pty (%m).\n");
2384 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002385 }
2386
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002387 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002388 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002389 terminal->io_task.run = io_handler;
2390 display_watch_fd(terminal->display, terminal->master,
2391 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002392
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002393 window_set_fullscreen(terminal->window, terminal->fullscreen);
2394 if (!terminal->fullscreen)
2395 terminal_resize(terminal, 80, 24);
2396
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002397 return 0;
2398}
2399
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002400static const struct config_key terminal_config_keys[] = {
2401 { "font", CONFIG_KEY_STRING, &option_font },
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04002402 { "term", CONFIG_KEY_STRING, &option_term },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002403};
2404
2405static const struct config_section config_sections[] = {
2406 { "terminal",
2407 terminal_config_keys, ARRAY_LENGTH(terminal_config_keys) },
2408};
2409
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002410static const struct weston_option terminal_options[] = {
2411 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002412 { WESTON_OPTION_STRING, "font", 0, &option_font },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002413};
2414
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002415int main(int argc, char *argv[])
2416{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002417 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002418 struct terminal *terminal;
Peter Hutterer035ac942012-02-03 20:58:19 +10002419 const char *shell;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002420 char *config_file;
2421
2422 config_file = config_file_path("weston.ini");
2423 parse_config_file(config_file,
2424 config_sections, ARRAY_LENGTH(config_sections),
2425 NULL);
2426 free(config_file);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002427
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002428 argc = parse_options(terminal_options,
2429 ARRAY_LENGTH(terminal_options), argc, argv);
2430
2431 d = display_create(argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002432 if (d == NULL) {
2433 fprintf(stderr, "failed to create display: %m\n");
2434 return -1;
2435 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002436
Peter Hutterer035ac942012-02-03 20:58:19 +10002437 shell = getenv("SHELL");
2438 if (!shell)
2439 shell = "/bin/bash";
2440
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002441 terminal = terminal_create(d, option_fullscreen);
Peter Hutterer035ac942012-02-03 20:58:19 +10002442 if (terminal_run(terminal, shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002443 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002444
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002445 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002446
2447 return 0;
2448}