blob: 09bc32153459c3a724b814e6f007d75d8c188a1e [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
Kristian Høgsberg0b36d972013-08-21 22:13:17 -070023#include <config.h>
Peng Wucfcc1112013-08-19 10:59:21 +080024
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +020025#include <stdbool.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050026#include <stdint.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <fcntl.h>
31#include <unistd.h>
32#include <math.h>
33#include <time.h>
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050034#include <pty.h>
Kristian Høgsbergf04e8382008-12-08 00:07:49 -050035#include <ctype.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050036#include <cairo.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040037#include <sys/epoll.h>
Peng Wucfcc1112013-08-19 10:59:21 +080038#include <wchar.h>
39#include <locale.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050040
Kristian Høgsberg67b82152013-10-23 16:52:05 -070041#include <linux/input.h>
42
Pekka Paalanen50719bc2011-11-22 14:18:50 +020043#include <wayland-client.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050044
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -040045#include "../shared/config-parser.h"
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050046#include "window.h"
47
Kristian Høgsberg0395f302008-12-22 12:14:50 -050048static int option_fullscreen;
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -070049static char *option_font;
50static int option_font_size;
51static char *option_term;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040052static char *option_shell;
53
54static struct wl_list terminal_list;
55
56static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -040057terminal_create(struct display *display);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -040058static void
59terminal_destroy(struct terminal *terminal);
60static int
61terminal_run(struct terminal *terminal, const char *path);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050062
Peng Wuf291f202013-06-06 15:32:41 +080063#define TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS \
64 " !\"#$%&'()*+,-./" \
65 "0123456789" \
66 ":;<=>?@" \
67 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
68 "[\\]^_`" \
69 "abcdefghijklmnopqrstuvwxyz" \
70 "{|}~" \
71 ""
72
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050073#define MOD_SHIFT 0x01
74#define MOD_ALT 0x02
75#define MOD_CTRL 0x04
76
Callum Lowcay30eeae52011-01-07 19:46:55 +000077#define ATTRMASK_BOLD 0x01
78#define ATTRMASK_UNDERLINE 0x02
79#define ATTRMASK_BLINK 0x04
80#define ATTRMASK_INVERSE 0x08
Callum Lowcay81179db2011-01-10 12:14:01 +130081#define ATTRMASK_CONCEALED 0x10
Callum Lowcay30eeae52011-01-07 19:46:55 +000082
Callum Lowcayb8609ad2011-01-07 19:46:57 +000083/* Buffer sizes */
Callum Lowcayef57a9b2011-01-14 20:46:23 +130084#define MAX_RESPONSE 256
85#define MAX_ESCAPE 255
Callum Lowcayb8609ad2011-01-07 19:46:57 +000086
Callum Lowcay8e57dd52011-01-07 19:46:59 +000087/* Terminal modes */
88#define MODE_SHOW_CURSOR 0x00000001
89#define MODE_INVERSE 0x00000002
90#define MODE_AUTOWRAP 0x00000004
91#define MODE_AUTOREPEAT 0x00000008
92#define MODE_LF_NEWLINE 0x00000010
Callum Lowcay69e96582011-01-07 19:47:00 +000093#define MODE_IRM 0x00000020
Callum Lowcay7e08e902011-01-07 19:47:02 +000094#define MODE_DELETE_SENDS_DEL 0x00000040
95#define MODE_ALT_SENDS_ESC 0x00000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +000096
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000097union utf8_char {
98 unsigned char byte[4];
99 uint32_t ch;
100};
101
102enum utf8_state {
103 utf8state_start,
104 utf8state_accept,
105 utf8state_reject,
106 utf8state_expect3,
107 utf8state_expect2,
108 utf8state_expect1
109};
110
111struct utf8_state_machine {
112 enum utf8_state state;
113 int len;
114 union utf8_char s;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700115 uint32_t unicode;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000116};
117
118static void
119init_state_machine(struct utf8_state_machine *machine)
120{
121 machine->state = utf8state_start;
122 machine->len = 0;
123 machine->s.ch = 0;
124}
125
126static enum utf8_state
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400127utf8_next_char(struct utf8_state_machine *machine, unsigned char c)
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000128{
129 switch(machine->state) {
130 case utf8state_start:
131 case utf8state_accept:
132 case utf8state_reject:
133 machine->s.ch = 0;
134 machine->len = 0;
135 if(c == 0xC0 || c == 0xC1) {
136 /* overlong encoding, reject */
137 machine->state = utf8state_reject;
138 } else if((c & 0x80) == 0) {
139 /* single byte, accept */
140 machine->s.byte[machine->len++] = c;
141 machine->state = utf8state_accept;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700142 machine->unicode = c;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000143 } else if((c & 0xC0) == 0x80) {
144 /* parser out of sync, ignore byte */
145 machine->state = utf8state_start;
146 } else if((c & 0xE0) == 0xC0) {
147 /* start of two byte sequence */
148 machine->s.byte[machine->len++] = c;
149 machine->state = utf8state_expect1;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700150 machine->unicode = c & 0x1f;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000151 } else if((c & 0xF0) == 0xE0) {
152 /* start of three byte sequence */
153 machine->s.byte[machine->len++] = c;
154 machine->state = utf8state_expect2;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700155 machine->unicode = c & 0x0f;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000156 } else if((c & 0xF8) == 0xF0) {
157 /* start of four byte sequence */
158 machine->s.byte[machine->len++] = c;
159 machine->state = utf8state_expect3;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700160 machine->unicode = c & 0x07;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000161 } else {
162 /* overlong encoding, reject */
163 machine->state = utf8state_reject;
164 }
165 break;
166 case utf8state_expect3:
167 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700168 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000169 if((c & 0xC0) == 0x80) {
170 /* all good, continue */
171 machine->state = utf8state_expect2;
172 } else {
173 /* missing extra byte, reject */
174 machine->state = utf8state_reject;
175 }
176 break;
177 case utf8state_expect2:
178 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700179 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000180 if((c & 0xC0) == 0x80) {
181 /* all good, continue */
182 machine->state = utf8state_expect1;
183 } else {
184 /* missing extra byte, reject */
185 machine->state = utf8state_reject;
186 }
187 break;
188 case utf8state_expect1:
189 machine->s.byte[machine->len++] = c;
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700190 machine->unicode = (machine->unicode << 6) | (c & 0x3f);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000191 if((c & 0xC0) == 0x80) {
192 /* all good, accept */
193 machine->state = utf8state_accept;
194 } else {
195 /* missing extra byte, reject */
196 machine->state = utf8state_reject;
197 }
198 break;
199 default:
200 machine->state = utf8state_reject;
201 break;
202 }
203
204 return machine->state;
205}
206
Kristian Høgsberg3e125832013-08-13 17:23:54 -0700207static uint32_t
208get_unicode(union utf8_char utf8)
209{
210 struct utf8_state_machine machine;
211 int i;
212
213 init_state_machine(&machine);
214 for (i = 0; i < 4; i++) {
215 utf8_next_char(&machine, utf8.byte[i]);
216 if (machine.state == utf8state_accept ||
217 machine.state == utf8state_reject)
218 break;
219 }
220
221 if (machine.state == utf8state_reject)
222 return 0xfffd;
223
224 return machine.unicode;
225}
226
Peng Wucfcc1112013-08-19 10:59:21 +0800227static bool
228is_wide(union utf8_char utf8)
229{
230 uint32_t unichar = get_unicode(utf8);
231 return wcwidth(unichar) > 1;
232}
233
Callum Lowcay256e72f2011-01-07 19:47:01 +0000234struct char_sub {
235 union utf8_char match;
236 union utf8_char replace;
237};
238/* Set last char_sub match to NULL char */
239typedef struct char_sub *character_set;
240
241struct char_sub CS_US[] = {
242 {{{0, }}, {{0, }}}
243};
244static struct char_sub CS_UK[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200245 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000246 {{{0, }}, {{0, }}}
247};
248static struct char_sub CS_SPECIAL[] = {
David Herrmanna6128d62012-05-29 09:37:02 +0200249 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond: ♦ */
250 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell: ▒ */
251 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT: ␉ */
252 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF: ␌ */
253 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR: ␍ */
254 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF: ␊ */
255 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree: ° */
256 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus: ± */
257 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL: ␤ */
258 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT: ␋ */
259 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB: ┘ */
260 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT: ┐ */
261 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT: ┌ */
262 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_LB: └ */
263 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS: ┼ */
264 {{{'o', 0, }}, {{0xE2, 0x8E, 0xBA, 0}}}, /* Horiz. Scan Line 1: ⎺ */
265 {{{'p', 0, }}, {{0xE2, 0x8E, 0xBB, 0}}}, /* Horiz. Scan Line 3: ⎻ */
266 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* Horiz. Scan Line 5: ─ */
267 {{{'r', 0, }}, {{0xE2, 0x8E, 0xBC, 0}}}, /* Horiz. Scan Line 7: ⎼ */
268 {{{'s', 0, }}, {{0xE2, 0x8E, 0xBD, 0}}}, /* Horiz. Scan Line 9: ⎽ */
269 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR: ├ */
270 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL: ┤ */
271 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU: ┴ */
272 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD: ┬ */
273 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V: │ */
274 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE: ≤ */
275 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE: ≥ */
276 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI: π */
277 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ: ≠ */
278 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND: £ */
279 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT: ⋅ */
Callum Lowcay256e72f2011-01-07 19:47:01 +0000280 {{{0, }}, {{0, }}}
281};
282
283static void
284apply_char_set(character_set cs, union utf8_char *utf8)
285{
286 int i = 0;
287
288 while (cs[i].match.byte[0]) {
289 if ((*utf8).ch == cs[i].match.ch) {
290 *utf8 = cs[i].replace;
291 break;
292 }
293 i++;
294 }
295}
296
Callum Lowcay7e08e902011-01-07 19:47:02 +0000297struct key_map {
298 int sym;
299 int num;
300 char escape;
301 char code;
302};
303/* Set last key_sub sym to NULL */
304typedef struct key_map *keyboard_mode;
305
306static struct key_map KM_NORMAL[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400307 { XKB_KEY_Left, 1, '[', 'D' },
308 { XKB_KEY_Right, 1, '[', 'C' },
309 { XKB_KEY_Up, 1, '[', 'A' },
310 { XKB_KEY_Down, 1, '[', 'B' },
311 { XKB_KEY_Home, 1, '[', 'H' },
312 { XKB_KEY_End, 1, '[', 'F' },
313 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000314};
315static struct key_map KM_APPLICATION[] = {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -0400316 { XKB_KEY_Left, 1, 'O', 'D' },
317 { XKB_KEY_Right, 1, 'O', 'C' },
318 { XKB_KEY_Up, 1, 'O', 'A' },
319 { XKB_KEY_Down, 1, 'O', 'B' },
320 { XKB_KEY_Home, 1, 'O', 'H' },
321 { XKB_KEY_End, 1, 'O', 'F' },
322 { XKB_KEY_KP_Enter, 1, 'O', 'M' },
323 { XKB_KEY_KP_Multiply, 1, 'O', 'j' },
324 { XKB_KEY_KP_Add, 1, 'O', 'k' },
325 { XKB_KEY_KP_Separator, 1, 'O', 'l' },
326 { XKB_KEY_KP_Subtract, 1, 'O', 'm' },
327 { XKB_KEY_KP_Divide, 1, 'O', 'o' },
328 { 0, 0, 0, 0 }
Callum Lowcay7e08e902011-01-07 19:47:02 +0000329};
330
331static int
332function_key_response(char escape, int num, uint32_t modifiers,
333 char code, char *response)
334{
335 int mod_num = 0;
336 int len;
337
Kristian Høgsberg70163132012-05-08 15:55:39 -0400338 if (modifiers & MOD_SHIFT_MASK) mod_num |= 1;
339 if (modifiers & MOD_ALT_MASK) mod_num |= 2;
340 if (modifiers & MOD_CONTROL_MASK) mod_num |= 4;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000341
342 if (mod_num != 0)
343 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
344 num, mod_num + 1, code);
345 else if (code != '~')
346 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
347 escape, code);
348 else
349 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
350 escape, num, code);
351
352 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
353 else return len;
354}
355
356/* returns the number of bytes written into response,
357 * which must have room for MAX_RESPONSE bytes */
358static int
359apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
360{
361 struct key_map map;
362 int len = 0;
363 int i = 0;
364
365 while (mode[i].sym) {
366 map = mode[i++];
367 if (sym == map.sym) {
368 len = function_key_response(map.escape, map.num,
369 modifiers, map.code,
370 response);
371 break;
372 }
373 }
374
375 return len;
376}
377
Callum Lowcay30eeae52011-01-07 19:46:55 +0000378struct terminal_color { double r, g, b, a; };
379struct attr {
380 unsigned char fg, bg;
381 char a; /* attributes format:
382 * 76543210
Callum Lowcay81179db2011-01-10 12:14:01 +1300383 * cilub */
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500384 char s; /* in selection */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000385};
386struct color_scheme {
387 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500388 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000389 struct attr default_attr;
390};
391
392static void
393attr_init(struct attr *data_attr, struct attr attr, int n)
394{
395 int i;
396 for (i = 0; i < n; i++) {
397 data_attr[i] = attr;
398 }
399}
400
Callum Lowcay67a201d2011-01-12 19:23:41 +1300401enum escape_state {
402 escape_state_normal = 0,
403 escape_state_escape,
404 escape_state_dcs,
405 escape_state_csi,
406 escape_state_osc,
407 escape_state_inner_escape,
408 escape_state_ignore,
409 escape_state_special
410};
411
412#define ESC_FLAG_WHAT 0x01
413#define ESC_FLAG_GT 0x02
414#define ESC_FLAG_BANG 0x04
415#define ESC_FLAG_CASH 0x08
416#define ESC_FLAG_SQUOTE 0x10
417#define ESC_FLAG_DQUOTE 0x20
418#define ESC_FLAG_SPACE 0x40
419
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400420enum {
421 SELECT_NONE,
422 SELECT_CHAR,
423 SELECT_WORD,
424 SELECT_LINE
425};
426
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500427struct terminal {
428 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500429 struct widget *widget;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500430 struct display *display;
Kristian Høgsberga83be202013-10-23 20:47:35 -0700431 char *title;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000432 union utf8_char *data;
Kristian Høgsberg3a696272011-09-14 17:33:48 -0400433 struct task io_task;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000434 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000435 struct attr *data_attr;
436 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000437 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000438 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000439 char saved_origin_mode;
440 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000441 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000442 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000443 character_set cs, g0, g1;
444 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000445 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000446 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700447 int width, height, row, column, max_width;
448 uint32_t buffer_height;
449 uint32_t start, end, saved_start, log_size;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000450 int saved_row, saved_column;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700451 int scrolling;
Scott Moreau7a1b32a2012-05-27 14:25:02 -0600452 int send_cursor_position;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500453 int fd, master;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500454 uint32_t modifiers;
Callum Lowcayef57a9b2011-01-14 20:46:23 +1300455 char escape[MAX_ESCAPE+1];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500456 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300457 enum escape_state state;
458 enum escape_state outer_state;
459 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000460 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500461 int margin;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400462 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000463 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400464 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +0800465 double average_width;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500466 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg88fd4082012-06-21 15:55:03 -0400467 uint32_t hide_cursor_serial;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500468
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400469 struct wl_data_source *selection;
Xiong Zhang49c6aee2013-11-25 18:42:52 +0800470 uint32_t click_time;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400471 int dragging, click_count;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500472 int selection_start_x, selection_start_y;
473 int selection_end_x, selection_end_y;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400474 int selection_start_row, selection_start_col;
475 int selection_end_row, selection_end_col;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -0400476 struct wl_list link;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500477};
478
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000479/* Create default tab stops, every 8 characters */
480static void
481terminal_init_tabs(struct terminal *terminal)
482{
483 int i = 0;
484
485 while (i < terminal->width) {
486 if (i % 8 == 0)
487 terminal->tab_ruler[i] = 1;
488 else
489 terminal->tab_ruler[i] = 0;
490 i++;
491 }
492}
493
Callum Lowcay30eeae52011-01-07 19:46:55 +0000494static void
495terminal_init(struct terminal *terminal)
496{
497 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000498 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000499 terminal->mode = MODE_SHOW_CURSOR |
500 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000501 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000502 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000503
504 terminal->row = 0;
505 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000506
Callum Lowcay256e72f2011-01-07 19:47:01 +0000507 terminal->g0 = CS_US;
508 terminal->g1 = CS_US;
509 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000510 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000511
512 terminal->saved_g0 = terminal->g0;
513 terminal->saved_g1 = terminal->g1;
514 terminal->saved_cs = terminal->cs;
515
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000516 terminal->saved_attr = terminal->curr_attr;
517 terminal->saved_origin_mode = terminal->origin_mode;
518 terminal->saved_row = terminal->row;
519 terminal->saved_column = terminal->column;
520
521 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000522}
523
524static void
525init_color_table(struct terminal *terminal)
526{
527 int c, r;
528 struct terminal_color *color_table = terminal->color_table;
529
530 for (c = 0; c < 256; c ++) {
531 if (c < 16) {
532 color_table[c] = terminal->color_scheme->palette[c];
533 } else if (c < 232) {
534 r = c - 16;
535 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
536 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
537 color_table[c].r = ((double)(r % 6) / 6.0);
538 color_table[c].a = 1.0;
539 } else {
540 r = (c - 232) * 10 + 8;
541 color_table[c].r = ((double) r) / 256.0;
542 color_table[c].g = color_table[c].r;
543 color_table[c].b = color_table[c].r;
544 color_table[c].a = 1.0;
545 }
546 }
547}
548
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000549static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500550terminal_get_row(struct terminal *terminal, int row)
551{
552 int index;
553
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700554 index = (row + terminal->start) & (terminal->buffer_height - 1);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500555
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700556 return (void *) terminal->data + index * terminal->data_pitch;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500557}
558
Callum Lowcay30eeae52011-01-07 19:46:55 +0000559static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500560terminal_get_attr_row(struct terminal *terminal, int row)
561{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000562 int index;
563
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700564 index = (row + terminal->start) & (terminal->buffer_height - 1);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000565
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700566 return (void *) terminal->data_attr + index * terminal->attr_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000567}
568
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500569union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300570 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500571 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500572};
573
574static void
575terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500576 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500577{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500578 struct attr attr;
579 int foreground, background, tmp;
Kristian Høgsberg59826582011-01-20 11:56:57 -0500580
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500581 decoded->attr.s = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -0400582 if (((row == terminal->selection_start_row &&
583 col >= terminal->selection_start_col) ||
584 row > terminal->selection_start_row) &&
585 ((row == terminal->selection_end_row &&
586 col < terminal->selection_end_col) ||
587 row < terminal->selection_end_row))
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500588 decoded->attr.s = 1;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500589
590 /* get the attributes for this character cell */
591 attr = terminal_get_attr_row(terminal, row)[col];
592 if ((attr.a & ATTRMASK_INVERSE) ||
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500593 decoded->attr.s ||
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500594 ((terminal->mode & MODE_SHOW_CURSOR) &&
Kristian Høgsberg86adef92012-08-13 22:25:53 -0400595 window_has_focus(terminal->window) && terminal->row == row &&
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500596 terminal->column == col)) {
597 foreground = attr.bg;
598 background = attr.fg;
599 if (attr.a & ATTRMASK_BOLD) {
600 if (foreground <= 16) foreground |= 0x08;
601 if (background <= 16) background &= 0x07;
602 }
603 } else {
604 foreground = attr.fg;
605 background = attr.bg;
606 }
607
608 if (terminal->mode & MODE_INVERSE) {
609 tmp = foreground;
610 foreground = background;
611 background = tmp;
612 if (attr.a & ATTRMASK_BOLD) {
613 if (foreground <= 16) foreground |= 0x08;
614 if (background <= 16) background &= 0x07;
615 }
616 }
617
Callum Lowcay9d708b02011-01-12 20:06:17 +1300618 decoded->attr.fg = foreground;
619 decoded->attr.bg = background;
620 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000621}
622
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500623
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500624static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000625terminal_scroll_buffer(struct terminal *terminal, int d)
626{
627 int i;
628
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700629 terminal->start += d;
630 if (d < 0) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000631 d = 0 - d;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700632 for (i = 0; i < d; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000633 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
634 attr_init(terminal_get_attr_row(terminal, i),
635 terminal->curr_attr, terminal->width);
636 }
637 } else {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700638 for (i = terminal->height - d; i < terminal->height; i++) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000639 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
640 attr_init(terminal_get_attr_row(terminal, i),
641 terminal->curr_attr, terminal->width);
642 }
643 }
Kristian Høgsberg18e928d2012-06-27 19:29:41 -0400644
645 terminal->selection_start_row -= d;
646 terminal->selection_end_row -= d;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000647}
648
649static void
650terminal_scroll_window(struct terminal *terminal, int d)
651{
652 int i;
653 int window_height;
654 int from_row, to_row;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000655
656 // scrolling range is inclusive
657 window_height = terminal->margin_bottom - terminal->margin_top + 1;
658 d = d % (window_height + 1);
659 if(d < 0) {
660 d = 0 - d;
661 to_row = terminal->margin_bottom;
662 from_row = terminal->margin_bottom - d;
663
664 for (i = 0; i < (window_height - d); i++) {
665 memcpy(terminal_get_row(terminal, to_row - i),
666 terminal_get_row(terminal, from_row - i),
667 terminal->data_pitch);
668 memcpy(terminal_get_attr_row(terminal, to_row - i),
669 terminal_get_attr_row(terminal, from_row - i),
670 terminal->attr_pitch);
671 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000672 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
673 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000674 attr_init(terminal_get_attr_row(terminal, i),
675 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000676 }
677 } else {
678 to_row = terminal->margin_top;
679 from_row = terminal->margin_top + d;
680
681 for (i = 0; i < (window_height - d); i++) {
682 memcpy(terminal_get_row(terminal, to_row + i),
683 terminal_get_row(terminal, from_row + i),
684 terminal->data_pitch);
685 memcpy(terminal_get_attr_row(terminal, to_row + i),
686 terminal_get_attr_row(terminal, from_row + i),
687 terminal->attr_pitch);
688 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000689 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
690 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000691 attr_init(terminal_get_attr_row(terminal, i),
692 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000693 }
694 }
695}
696
697static void
698terminal_scroll(struct terminal *terminal, int d)
699{
700 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
701 terminal_scroll_buffer(terminal, d);
702 else
703 terminal_scroll_window(terminal, d);
704}
705
706static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000707terminal_shift_line(struct terminal *terminal, int d)
708{
709 union utf8_char *row;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500710 struct attr *attr_row;
Callum Lowcay69e96582011-01-07 19:47:00 +0000711
712 row = terminal_get_row(terminal, terminal->row);
713 attr_row = terminal_get_attr_row(terminal, terminal->row);
714
715 if ((terminal->width + d) <= terminal->column)
716 d = terminal->column + 1 - terminal->width;
717 if ((terminal->column + d) >= terminal->width)
718 d = terminal->width - terminal->column - 1;
719
720 if (d < 0) {
721 d = 0 - d;
722 memmove(&row[terminal->column],
723 &row[terminal->column + d],
724 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
Callum Lowcay69e96582011-01-07 19:47:00 +0000725 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
726 (terminal->width - terminal->column - d) * sizeof(struct attr));
727 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
728 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
729 } else {
730 memmove(&row[terminal->column + d], &row[terminal->column],
731 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
732 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
733 (terminal->width - terminal->column - d) * sizeof(struct attr));
734 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
735 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
736 }
737}
738
739static void
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700740terminal_resize_cells(struct terminal *terminal,
741 int width, int height)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500742{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000743 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000744 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000745 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000746 int data_pitch, attr_pitch;
Kristian Høgsberg00439612011-01-25 15:16:01 -0500747 int i, l, total_rows;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700748 uint32_t d, uheight = height;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500749 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000750 struct winsize ws;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500751
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700752 if (uheight > terminal->buffer_height)
753 height = terminal->buffer_height;
754
Kristian Høgsberg22106762008-12-08 13:50:07 -0500755 if (terminal->width == width && terminal->height == height)
756 return;
757
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700758 if (terminal->data && width <= terminal->max_width) {
759 d = 0;
760 if (height < terminal->height && height <= terminal->row)
761 d = terminal->height - height;
762 else if (height > terminal->height &&
763 terminal->height - 1 == terminal->row) {
764 d = terminal->height - height;
765 if (terminal->log_size < uheight)
766 d = -terminal->start;
767 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500768
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700769 terminal->start += d;
770 terminal->row -= d;
771 } else {
772 terminal->max_width = width;
773 data_pitch = width * sizeof(union utf8_char);
774 data = zalloc(data_pitch * terminal->buffer_height);
775 attr_pitch = width * sizeof(struct attr);
776 data_attr = malloc(attr_pitch * terminal->buffer_height);
777 tab_ruler = zalloc(width);
778 attr_init(data_attr, terminal->curr_attr,
779 width * terminal->buffer_height);
780
781 if (terminal->data && terminal->data_attr) {
782 if (width > terminal->width)
783 l = terminal->width;
784 else
785 l = width;
786
787 if (terminal->height > height) {
788 total_rows = height;
789 i = 1 + terminal->row - height;
790 if (i > 0) {
791 terminal->start += i;
792 terminal->row = terminal->row - i;
793 }
794 } else {
795 total_rows = terminal->height;
José Bollo4a4704a2013-09-13 11:28:51 +0200796 }
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700797
798 for (i = 0; i < total_rows; i++) {
799 memcpy(&data[width * i],
800 terminal_get_row(terminal, i),
801 l * sizeof(union utf8_char));
802 memcpy(&data_attr[width * i],
803 terminal_get_attr_row(terminal, i),
804 l * sizeof(struct attr));
805 }
806
807 free(terminal->data);
808 free(terminal->data_attr);
809 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500810 }
811
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700812 terminal->data_pitch = data_pitch;
813 terminal->attr_pitch = attr_pitch;
814 terminal->data = data;
815 terminal->data_attr = data_attr;
816 terminal->tab_ruler = tab_ruler;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -0700817 terminal->start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500818 }
819
Callum Lowcay86653ed2011-01-07 19:47:03 +0000820 terminal->margin_bottom =
821 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500822 terminal->width = width;
823 terminal->height = height;
Kristian Høgsbergdcfff552013-11-22 22:43:20 -0800824 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500825
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000826 /* Update the window size */
827 ws.ws_row = terminal->height;
828 ws.ws_col = terminal->width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500829 widget_get_allocation(terminal->widget, &allocation);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500830 ws.ws_xpixel = allocation.width;
831 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000832 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500833}
834
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500835static void
836resize_handler(struct widget *widget,
837 int32_t width, int32_t height, void *data)
838{
839 struct terminal *terminal = data;
840 int32_t columns, rows, m;
Kristian Høgsberga83be202013-10-23 20:47:35 -0700841 char *p;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500842 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800843 columns = (width - m) / (int32_t) terminal->average_width;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500844 rows = (height - m) / (int32_t) terminal->extents.height;
845
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400846 if (!window_is_fullscreen(terminal->window) &&
847 !window_is_maximized(terminal->window)) {
Peng Wuf291f202013-06-06 15:32:41 +0800848 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500849 height = rows * terminal->extents.height + m;
850 widget_set_size(terminal->widget, width, height);
Bryce W. Harrington3abdafd2014-01-14 21:58:32 +0000851 if (asprintf(&p, "%s — [%dx%d]", terminal->title, columns, rows) > 0) {
852 window_set_title(terminal->window, p);
853 free(p);
854 }
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500855 }
856
857 terminal_resize_cells(terminal, columns, rows);
858}
859
860static void
861terminal_resize(struct terminal *terminal, int columns, int rows)
862{
863 int32_t width, height, m;
864
Kristian Høgsbergb36f7ef2012-10-10 11:41:21 -0400865 if (window_is_fullscreen(terminal->window) ||
866 window_is_maximized(terminal->window))
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500867 return;
868
869 m = 2 * terminal->margin;
Peng Wuf291f202013-06-06 15:32:41 +0800870 width = columns * terminal->average_width + m;
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500871 height = rows * terminal->extents.height + m;
Kristian Høgsberga1627922012-06-20 17:30:03 -0400872
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500873 window_frame_set_child_size(terminal->widget, width, height);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500874}
875
Callum Lowcay30eeae52011-01-07 19:46:55 +0000876struct color_scheme DEFAULT_COLORS = {
877 {
878 {0, 0, 0, 1}, /* black */
879 {0.66, 0, 0, 1}, /* red */
880 {0 , 0.66, 0, 1}, /* green */
881 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
882 {0 , 0 , 0.66, 1}, /* blue */
883 {0.66, 0 , 0.66, 1}, /* magenta */
884 {0, 0.66, 0.66, 1}, /* cyan */
885 {0.66, 0.66, 0.66, 1}, /* light grey */
886 {0.22, 0.33, 0.33, 1}, /* dark grey */
887 {1, 0.33, 0.33, 1}, /* high red */
888 {0.33, 1, 0.33, 1}, /* high green */
889 {1, 1, 0.33, 1}, /* high yellow */
890 {0.33, 0.33, 1, 1}, /* high blue */
891 {1, 0.33, 1, 1}, /* high magenta */
892 {0.33, 1, 1, 1}, /* high cyan */
893 {1, 1, 1, 1} /* white */
894 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500895 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000896 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
897};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400898
Kristian Høgsberg22106762008-12-08 13:50:07 -0500899static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500900terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
901{
902 cairo_set_source_rgba(cr,
903 terminal->color_table[index].r,
904 terminal->color_table[index].g,
905 terminal->color_table[index].b,
906 terminal->color_table[index].a);
907}
908
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500909static void
910terminal_send_selection(struct terminal *terminal, int fd)
911{
912 int row, col;
913 union utf8_char *p_row;
914 union decoded_attr attr;
915 FILE *fp;
916 int len;
917
918 fp = fdopen(fd, "w");
Brian Lovin1bf14812013-08-08 16:12:55 -0700919 if (fp == NULL){
920 close(fd);
921 return;
922 }
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500923 for (row = 0; row < terminal->height; row++) {
924 p_row = terminal_get_row(terminal, row);
925 for (col = 0; col < terminal->width; col++) {
Peng Wucfcc1112013-08-19 10:59:21 +0800926 if (p_row[col].ch == 0x200B) /* space glyph */
927 continue;
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500928 /* get the attributes for this character cell */
929 terminal_decode_attr(terminal, row, col, &attr);
930 if (!attr.attr.s)
931 continue;
932 len = strnlen((char *) p_row[col].byte, 4);
Kristian Høgsberg0dee6472012-07-01 21:25:41 -0400933 if (len > 0)
934 fwrite(p_row[col].byte, 1, len, fp);
935 if (len == 0 || col == terminal->width - 1) {
936 fwrite("\n", 1, 1, fp);
937 break;
938 }
Kristian Høgsberg31cce052011-01-21 15:18:55 -0500939 }
940 }
941 fclose(fp);
942}
943
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500944struct glyph_run {
945 struct terminal *terminal;
946 cairo_t *cr;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400947 unsigned int count;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500948 union decoded_attr attr;
949 cairo_glyph_t glyphs[256], *g;
950};
951
952static void
953glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
954{
955 run->terminal = terminal;
956 run->cr = cr;
957 run->g = run->glyphs;
958 run->count = 0;
959 run->attr.key = 0;
960}
961
962static void
963glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
964{
965 cairo_scaled_font_t *font;
966
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500967 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
968 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300969 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500970 font = run->terminal->font_bold;
971 else
972 font = run->terminal->font_normal;
973 cairo_set_scaled_font(run->cr, font);
974 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300975 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500976
Callum Lowcay9d708b02011-01-12 20:06:17 +1300977 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
978 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500979 run->g = run->glyphs;
980 run->count = 0;
981 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300982 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500983}
984
985static void
986glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
987{
988 int num_glyphs;
989 cairo_scaled_font_t *font;
990
991 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
992
Callum Lowcay9d708b02011-01-12 20:06:17 +1300993 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500994 font = run->terminal->font_bold;
995 else
996 font = run->terminal->font_normal;
997
998 cairo_move_to(run->cr, x, y);
999 cairo_scaled_font_text_to_glyphs (font, x, y,
1000 (char *) c->byte, 4,
1001 &run->g, &num_glyphs,
1002 NULL, NULL, NULL);
1003 run->g += num_glyphs;
1004 run->count += num_glyphs;
1005}
1006
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001007
Kristian Høgsbergf106fd52011-01-11 10:11:39 -05001008static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001009redraw_handler(struct widget *widget, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001010{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05001011 struct terminal *terminal = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001012 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001013 cairo_t *cr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001014 int top_margin, side_margin;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001015 int row, col, cursor_x, cursor_y;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001016 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001017 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001018 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001019 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001020 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001021 struct glyph_run run;
Kristian Høgsberg59826582011-01-20 11:56:57 -05001022 cairo_font_extents_t extents;
Peng Wuf291f202013-06-06 15:32:41 +08001023 double average_width;
Peng Wucfcc1112013-08-19 10:59:21 +08001024 double unichar_width;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001025
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001026 surface = window_get_surface(terminal->window);
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001027 widget_get_allocation(terminal->widget, &allocation);
Alexander Larssonde79dd02013-05-22 14:41:34 +02001028 cr = widget_cairo_create(terminal->widget);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001029 cairo_rectangle(cr, allocation.x, allocation.y,
1030 allocation.width, allocation.height);
1031 cairo_clip(cr);
1032 cairo_push_group(cr);
1033
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001034 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -05001035 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001036 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001037
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05001038 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001039
Kristian Høgsberg59826582011-01-20 11:56:57 -05001040 extents = terminal->extents;
Peng Wuf291f202013-06-06 15:32:41 +08001041 average_width = terminal->average_width;
1042 side_margin = (allocation.width - terminal->width * average_width) / 2;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05001043 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001044
Callum Lowcay30eeae52011-01-07 19:46:55 +00001045 cairo_set_line_width(cr, 1.0);
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001046 cairo_translate(cr, allocation.x + side_margin,
1047 allocation.y + top_margin);
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001048 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001049 for (row = 0; row < terminal->height; row++) {
Peng Wucfcc1112013-08-19 10:59:21 +08001050 p_row = terminal_get_row(terminal, row);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001051 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001052 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -05001053 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001054
Callum Lowcay9d708b02011-01-12 20:06:17 +13001055 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -05001056 continue;
1057
Peng Wucfcc1112013-08-19 10:59:21 +08001058 if (is_wide(p_row[col]))
1059 unichar_width = 2 * average_width;
1060 else
1061 unichar_width = average_width;
1062
Callum Lowcay9d708b02011-01-12 20:06:17 +13001063 terminal_set_color(terminal, cr, attr.attr.bg);
Peng Wuf291f202013-06-06 15:32:41 +08001064 cairo_move_to(cr, col * average_width,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001065 row * extents.height);
Peng Wucfcc1112013-08-19 10:59:21 +08001066 cairo_rel_line_to(cr, unichar_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001067 cairo_rel_line_to(cr, 0, extents.height);
Peng Wucfcc1112013-08-19 10:59:21 +08001068 cairo_rel_line_to(cr, -unichar_width, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001069 cairo_close_path(cr);
1070 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001071 }
1072 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001073
Kristian Høgsberg8c254202010-12-25 08:58:46 -05001074 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1075
1076 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001077 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001078 for (row = 0; row < terminal->height; row++) {
1079 p_row = terminal_get_row(terminal, row);
1080 for (col = 0; col < terminal->width; col++) {
1081 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -05001082 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001083
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001084 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001085
Peng Wuf291f202013-06-06 15:32:41 +08001086 text_x = col * average_width;
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001087 text_y = extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +13001088 if (attr.attr.a & ATTRMASK_UNDERLINE) {
1089 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +00001090 cairo_move_to(cr, text_x, (double)text_y + 1.5);
Peng Wuf291f202013-06-06 15:32:41 +08001091 cairo_line_to(cr, text_x + average_width, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001092 cairo_stroke(cr);
1093 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -05001094
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001095 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001096 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001097 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001098
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -05001099 attr.key = ~0;
1100 glyph_run_flush(&run, attr);
1101
Kristian Høgsberg86adef92012-08-13 22:25:53 -04001102 if ((terminal->mode & MODE_SHOW_CURSOR) &&
1103 !window_has_focus(terminal->window)) {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001104 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001105
Callum Lowcay30eeae52011-01-07 19:46:55 +00001106 cairo_set_line_width(cr, 1);
Peng Wuf291f202013-06-06 15:32:41 +08001107 cairo_move_to(cr, terminal->column * average_width + d,
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001108 terminal->row * extents.height + d);
Peng Wuf291f202013-06-06 15:32:41 +08001109 cairo_rel_line_to(cr, average_width - 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001110 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
Peng Wuf291f202013-06-06 15:32:41 +08001111 cairo_rel_line_to(cr, -average_width + 2 * d, 0);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001112 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001113
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05001114 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001115 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -05001116
Kristian Høgsbergf39a9cc2011-01-20 12:37:33 -05001117 cairo_pop_group_to_source(cr);
1118 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001119 cairo_destroy(cr);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -05001120 cairo_surface_destroy(surface);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001121
1122 if (terminal->send_cursor_position) {
1123 cursor_x = side_margin + allocation.x +
Peng Wuf291f202013-06-06 15:32:41 +08001124 terminal->column * average_width;
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001125 cursor_y = top_margin + allocation.y +
1126 terminal->row * extents.height;
1127 window_set_text_cursor_position(terminal->window,
1128 cursor_x, cursor_y);
1129 terminal->send_cursor_position = 0;
1130 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05001131}
1132
1133static void
Kristian Høgsberg26130862011-08-24 11:30:21 -04001134terminal_write(struct terminal *terminal, const char *data, size_t length)
1135{
1136 if (write(terminal->master, data, length) < 0)
1137 abort();
Scott Moreau7a1b32a2012-05-27 14:25:02 -06001138 terminal->send_cursor_position = 1;
Kristian Høgsberg26130862011-08-24 11:30:21 -04001139}
1140
1141static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001142terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001143
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001144static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001145handle_char(struct terminal *terminal, union utf8_char utf8);
1146
1147static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001148handle_sgr(struct terminal *terminal, int code);
1149
1150static void
Callum Lowcaybbeac602011-01-07 19:46:58 +00001151handle_term_parameter(struct terminal *terminal, int code, int sr)
1152{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001153 int i;
1154
Callum Lowcay67a201d2011-01-12 19:23:41 +13001155 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001156 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00001157 case 1: /* DECCKM */
1158 if (sr) terminal->key_mode = KM_APPLICATION;
1159 else terminal->key_mode = KM_NORMAL;
1160 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001161 case 2: /* DECANM */
1162 /* No VT52 support yet */
1163 terminal->g0 = CS_US;
1164 terminal->g1 = CS_US;
1165 terminal->cs = terminal->g0;
1166 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001167 case 3: /* DECCOLM */
1168 if (sr)
1169 terminal_resize(terminal, 132, 24);
1170 else
1171 terminal_resize(terminal, 80, 24);
1172
1173 /* set columns, but also home cursor and clear screen */
1174 terminal->row = 0; terminal->column = 0;
1175 for (i = 0; i < terminal->height; i++) {
1176 memset(terminal_get_row(terminal, i),
1177 0, terminal->data_pitch);
1178 attr_init(terminal_get_attr_row(terminal, i),
1179 terminal->curr_attr, terminal->width);
1180 }
1181 break;
1182 case 5: /* DECSCNM */
1183 if (sr) terminal->mode |= MODE_INVERSE;
1184 else terminal->mode &= ~MODE_INVERSE;
1185 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001186 case 6: /* DECOM */
1187 terminal->origin_mode = sr;
1188 if (terminal->origin_mode)
1189 terminal->row = terminal->margin_top;
1190 else
1191 terminal->row = 0;
1192 terminal->column = 0;
1193 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001194 case 7: /* DECAWM */
1195 if (sr) terminal->mode |= MODE_AUTOWRAP;
1196 else terminal->mode &= ~MODE_AUTOWRAP;
1197 break;
1198 case 8: /* DECARM */
1199 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1200 else terminal->mode &= ~MODE_AUTOREPEAT;
1201 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001202 case 12: /* Very visible cursor (CVVIS) */
1203 /* FIXME: What do we do here. */
1204 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001205 case 25:
1206 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1207 else terminal->mode &= ~MODE_SHOW_CURSOR;
1208 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001209 case 1034: /* smm/rmm, meta mode on/off */
1210 /* ignore */
1211 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001212 case 1037: /* deleteSendsDel */
1213 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1214 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1215 break;
1216 case 1039: /* altSendsEscape */
1217 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1218 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1219 break;
Kristian Høgsberge828e902012-06-20 16:59:17 -04001220 case 1049: /* rmcup/smcup, alternate screen */
1221 /* Ignore. Should be possible to implement,
1222 * but it's kind of annoying. */
1223 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001224 default:
1225 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1226 break;
1227 }
1228 } else {
1229 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001230 case 4: /* IRM */
1231 if (sr) terminal->mode |= MODE_IRM;
1232 else terminal->mode &= ~MODE_IRM;
1233 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001234 case 20: /* LNM */
1235 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1236 else terminal->mode &= ~MODE_LF_NEWLINE;
1237 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001238 default:
1239 fprintf(stderr, "Unknown parameter: %d\n", code);
1240 break;
1241 }
1242 }
1243}
1244
1245static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001246handle_dcs(struct terminal *terminal)
1247{
1248}
1249
1250static void
1251handle_osc(struct terminal *terminal)
1252{
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001253 char *p;
1254 int code;
1255
1256 terminal->escape[terminal->escape_length++] = '\0';
1257 p = &terminal->escape[2];
1258 code = strtol(p, &p, 10);
1259 if (*p == ';') p++;
1260
1261 switch (code) {
1262 case 0: /* Icon name and window title */
1263 case 1: /* Icon label */
1264 case 2: /* Window title*/
Kristian Høgsberga83be202013-10-23 20:47:35 -07001265 free(terminal->title);
1266 terminal->title = strdup(p);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001267 window_set_title(terminal->window, p);
1268 break;
Kristian Høgsberg8a205002013-09-11 11:52:56 -07001269 case 7: /* shell cwd as uri */
1270 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001271 default:
Kristian Høgsberg8a205002013-09-11 11:52:56 -07001272 fprintf(stderr, "Unknown OSC escape code %d, text %s\n",
1273 code, p);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001274 break;
1275 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001276}
1277
1278static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001279handle_escape(struct terminal *terminal)
1280{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001281 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001282 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001283 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001284 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001285 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001286 char response[MAX_RESPONSE] = {0, };
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001287 struct rectangle allocation;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001288
1289 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001290 i = 0;
1291 p = &terminal->escape[2];
1292 while ((isdigit(*p) || *p == ';') && i < 10) {
1293 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001294 if (!set[i]) {
1295 args[i] = 0;
1296 set[i] = 1;
1297 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001298 p++;
1299 i++;
1300 } else {
1301 args[i] = strtol(p, &p, 10);
1302 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001303 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001304 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001305
1306 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001307 case '@': /* ICH */
1308 count = set[0] ? args[0] : 1;
1309 if (count == 0) count = 1;
1310 terminal_shift_line(terminal, count);
1311 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001312 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001313 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001314 if (count == 0) count = 1;
1315 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001316 terminal->row -= count;
1317 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001318 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001319 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001320 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001321 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001322 if (count == 0) count = 1;
1323 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001324 terminal->row += count;
1325 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001326 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001327 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001328 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001329 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001330 if (count == 0) count = 1;
1331 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001332 terminal->column += count;
1333 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001334 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001335 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001336 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001337 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001338 if (count == 0) count = 1;
1339 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001340 terminal->column -= count;
1341 else
1342 terminal->column = 0;
1343 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001344 case 'E': /* CNL */
1345 count = set[0] ? args[0] : 1;
1346 if (terminal->row + count <= terminal->margin_bottom)
1347 terminal->row += count;
1348 else
1349 terminal->row = terminal->margin_bottom;
1350 terminal->column = 0;
1351 break;
1352 case 'F': /* CPL */
1353 count = set[0] ? args[0] : 1;
1354 if (terminal->row - count >= terminal->margin_top)
1355 terminal->row -= count;
1356 else
1357 terminal->row = terminal->margin_top;
1358 terminal->column = 0;
1359 break;
1360 case 'G': /* CHA */
1361 y = set[0] ? args[0] : 1;
1362 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1363
1364 terminal->column = y - 1;
1365 break;
1366 case 'f': /* HVP */
1367 case 'H': /* CUP */
1368 x = (set[1] ? args[1] : 1) - 1;
1369 x = x < 0 ? 0 :
1370 (x >= terminal->width ? terminal->width - 1 : x);
1371
1372 y = (set[0] ? args[0] : 1) - 1;
1373 if (terminal->origin_mode) {
1374 y += terminal->margin_top;
1375 y = y < terminal->margin_top ? terminal->margin_top :
1376 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1377 } else {
1378 y = y < 0 ? 0 :
1379 (y >= terminal->height ? terminal->height - 1 : y);
1380 }
1381
1382 terminal->row = y;
1383 terminal->column = x;
1384 break;
1385 case 'I': /* CHT */
1386 count = set[0] ? args[0] : 1;
1387 if (count == 0) count = 1;
1388 while (count > 0 && terminal->column < terminal->width) {
1389 if (terminal->tab_ruler[terminal->column]) count--;
1390 terminal->column++;
1391 }
1392 terminal->column--;
1393 break;
1394 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001395 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001396 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001397 if (!set[0] || args[0] == 0 || args[0] > 2) {
1398 memset(&row[terminal->column],
1399 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1400 attr_init(&attr_row[terminal->column],
1401 terminal->curr_attr, terminal->width - terminal->column);
1402 for (i = terminal->row + 1; i < terminal->height; i++) {
1403 memset(terminal_get_row(terminal, i),
1404 0, terminal->data_pitch);
1405 attr_init(terminal_get_attr_row(terminal, i),
1406 terminal->curr_attr, terminal->width);
1407 }
1408 } else if (args[0] == 1) {
1409 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1410 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1411 for (i = 0; i < terminal->row; i++) {
1412 memset(terminal_get_row(terminal, i),
1413 0, terminal->data_pitch);
1414 attr_init(terminal_get_attr_row(terminal, i),
1415 terminal->curr_attr, terminal->width);
1416 }
1417 } else if (args[0] == 2) {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07001418 /* Clear screen by scrolling contents out */
1419 terminal_scroll_buffer(terminal,
1420 terminal->end - terminal->start);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001421 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001422 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001423 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001424 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001425 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001426 if (!set[0] || args[0] == 0 || args[0] > 2) {
1427 memset(&row[terminal->column], 0,
1428 (terminal->width - terminal->column) * sizeof(union utf8_char));
1429 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1430 terminal->width - terminal->column);
1431 } else if (args[0] == 1) {
1432 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1433 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1434 } else if (args[0] == 2) {
1435 memset(row, 0, terminal->data_pitch);
1436 attr_init(attr_row, terminal->curr_attr, terminal->width);
1437 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001438 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001439 case 'L': /* IL */
1440 count = set[0] ? args[0] : 1;
1441 if (count == 0) count = 1;
1442 if (terminal->row >= terminal->margin_top &&
1443 terminal->row < terminal->margin_bottom)
1444 {
1445 top = terminal->margin_top;
1446 terminal->margin_top = terminal->row;
1447 terminal_scroll(terminal, 0 - count);
1448 terminal->margin_top = top;
1449 } else if (terminal->row == terminal->margin_bottom) {
1450 memset(terminal_get_row(terminal, terminal->row),
1451 0, terminal->data_pitch);
1452 attr_init(terminal_get_attr_row(terminal, terminal->row),
1453 terminal->curr_attr, terminal->width);
1454 }
1455 break;
1456 case 'M': /* DL */
1457 count = set[0] ? args[0] : 1;
1458 if (count == 0) count = 1;
1459 if (terminal->row >= terminal->margin_top &&
1460 terminal->row < terminal->margin_bottom)
1461 {
1462 top = terminal->margin_top;
1463 terminal->margin_top = terminal->row;
1464 terminal_scroll(terminal, count);
1465 terminal->margin_top = top;
1466 } else if (terminal->row == terminal->margin_bottom) {
1467 memset(terminal_get_row(terminal, terminal->row),
1468 0, terminal->data_pitch);
1469 }
1470 break;
1471 case 'P': /* DCH */
1472 count = set[0] ? args[0] : 1;
1473 if (count == 0) count = 1;
1474 terminal_shift_line(terminal, 0 - count);
1475 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001476 case 'S': /* SU */
1477 terminal_scroll(terminal, set[0] ? args[0] : 1);
1478 break;
1479 case 'T': /* SD */
1480 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1481 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001482 case 'X': /* ECH */
1483 count = set[0] ? args[0] : 1;
1484 if (count == 0) count = 1;
1485 if ((terminal->column + count) > terminal->width)
1486 count = terminal->width - terminal->column;
1487 row = terminal_get_row(terminal, terminal->row);
1488 attr_row = terminal_get_attr_row(terminal, terminal->row);
1489 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1490 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1491 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001492 case 'Z': /* CBT */
1493 count = set[0] ? args[0] : 1;
1494 if (count == 0) count = 1;
1495 while (count > 0 && terminal->column >= 0) {
1496 if (terminal->tab_ruler[terminal->column]) count--;
1497 terminal->column--;
1498 }
1499 terminal->column++;
1500 break;
1501 case '`': /* HPA */
1502 y = set[0] ? args[0] : 1;
1503 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1504
1505 terminal->column = y - 1;
1506 break;
1507 case 'b': /* REP */
1508 count = set[0] ? args[0] : 1;
1509 if (count == 0) count = 1;
1510 if (terminal->last_char.byte[0])
1511 for (i = 0; i < count; i++)
1512 handle_char(terminal, terminal->last_char);
1513 terminal->last_char.byte[0] = 0;
1514 break;
1515 case 'c': /* Primary DA */
Kristian Høgsberg26130862011-08-24 11:30:21 -04001516 terminal_write(terminal, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001517 break;
1518 case 'd': /* VPA */
1519 x = set[0] ? args[0] : 1;
1520 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1521
1522 terminal->row = x - 1;
1523 break;
1524 case 'g': /* TBC */
1525 if (!set[0] || args[0] == 0) {
1526 terminal->tab_ruler[terminal->column] = 0;
1527 } else if (args[0] == 3) {
1528 memset(terminal->tab_ruler, 0, terminal->width);
1529 }
1530 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001531 case 'h': /* SM */
1532 for(i = 0; i < 10 && set[i]; i++) {
1533 handle_term_parameter(terminal, args[i], 1);
1534 }
1535 break;
1536 case 'l': /* RM */
1537 for(i = 0; i < 10 && set[i]; i++) {
1538 handle_term_parameter(terminal, args[i], 0);
1539 }
1540 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001541 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001542 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001543 if (i <= 7 && set[i] && set[i + 1] &&
1544 set[i + 2] && args[i + 1] == 5)
1545 {
1546 if (args[i] == 38) {
1547 handle_sgr(terminal, args[i + 2] + 256);
1548 break;
1549 } else if (args[i] == 48) {
1550 handle_sgr(terminal, args[i + 2] + 512);
1551 break;
1552 }
1553 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001554 if(set[i]) {
1555 handle_sgr(terminal, args[i]);
1556 } else if(i == 0) {
1557 handle_sgr(terminal, 0);
1558 break;
1559 } else {
1560 break;
1561 }
1562 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001563 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001564 case 'n': /* DSR */
1565 i = set[0] ? args[0] : 0;
1566 if (i == 0 || i == 5) {
Kristian Høgsberg26130862011-08-24 11:30:21 -04001567 terminal_write(terminal, "\e[0n", 4);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001568 } else if (i == 6) {
1569 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1570 terminal->origin_mode ?
1571 terminal->row+terminal->margin_top : terminal->row+1,
1572 terminal->column+1);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001573 terminal_write(terminal, response, strlen(response));
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001574 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001575 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001576 case 'r':
1577 if(!set[0]) {
1578 terminal->margin_top = 0;
1579 terminal->margin_bottom = terminal->height-1;
1580 terminal->row = 0;
1581 terminal->column = 0;
1582 } else {
1583 top = (set[0] ? args[0] : 1) - 1;
1584 top = top < 0 ? 0 :
1585 (top >= terminal->height ? terminal->height - 1 : top);
1586 bottom = (set[1] ? args[1] : 1) - 1;
1587 bottom = bottom < 0 ? 0 :
1588 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1589 if(bottom > top) {
1590 terminal->margin_top = top;
1591 terminal->margin_bottom = bottom;
1592 } else {
1593 terminal->margin_top = 0;
1594 terminal->margin_bottom = terminal->height-1;
1595 }
1596 if(terminal->origin_mode)
1597 terminal->row = terminal->margin_top;
1598 else
1599 terminal->row = 0;
1600 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001601 }
1602 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001603 case 's':
1604 terminal->saved_row = terminal->row;
1605 terminal->saved_column = terminal->column;
1606 break;
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001607 case 't': /* windowOps */
1608 if (!set[0]) break;
1609 switch (args[0]) {
1610 case 4: /* resize px */
1611 if (set[1] && set[2]) {
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001612 widget_schedule_resize(terminal->widget,
1613 args[2], args[1]);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001614 }
1615 break;
1616 case 8: /* resize ch */
1617 if (set[1] && set[2]) {
1618 terminal_resize(terminal, args[2], args[1]);
1619 }
1620 break;
1621 case 13: /* report position */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001622 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001623 snprintf(response, MAX_RESPONSE, "\e[3;%d;%dt",
1624 allocation.x, allocation.y);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001625 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001626 break;
1627 case 14: /* report px */
Kristian Høgsbergbb977002012-01-10 19:11:42 -05001628 widget_get_allocation(terminal->widget, &allocation);
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001629 snprintf(response, MAX_RESPONSE, "\e[4;%d;%dt",
1630 allocation.height, allocation.width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001631 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001632 break;
1633 case 18: /* report ch */
1634 snprintf(response, MAX_RESPONSE, "\e[9;%d;%dt",
1635 terminal->height, terminal->width);
Kristian Høgsberg26130862011-08-24 11:30:21 -04001636 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001637 break;
1638 case 21: /* report title */
1639 snprintf(response, MAX_RESPONSE, "\e]l%s\e\\",
1640 window_get_title(terminal->window));
Kristian Høgsberg26130862011-08-24 11:30:21 -04001641 terminal_write(terminal, response, strlen(response));
Callum Lowcayef57a9b2011-01-14 20:46:23 +13001642 break;
1643 default:
1644 if (args[0] >= 24)
1645 terminal_resize(terminal, terminal->width, args[0]);
1646 else
1647 fprintf(stderr, "Unimplemented windowOp %d\n", args[0]);
1648 break;
1649 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001650 case 'u':
1651 terminal->row = terminal->saved_row;
1652 terminal->column = terminal->saved_column;
1653 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001654 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001655 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001656 break;
1657 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001658}
1659
1660static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001661handle_non_csi_escape(struct terminal *terminal, char code)
1662{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001663 switch(code) {
1664 case 'M': /* RI */
1665 terminal->row -= 1;
1666 if(terminal->row < terminal->margin_top) {
1667 terminal->row = terminal->margin_top;
1668 terminal_scroll(terminal, -1);
1669 }
1670 break;
1671 case 'E': /* NEL */
1672 terminal->column = 0;
1673 // fallthrough
1674 case 'D': /* IND */
1675 terminal->row += 1;
1676 if(terminal->row > terminal->margin_bottom) {
1677 terminal->row = terminal->margin_bottom;
1678 terminal_scroll(terminal, +1);
1679 }
1680 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001681 case 'c': /* RIS */
1682 terminal_init(terminal);
1683 break;
1684 case 'H': /* HTS */
1685 terminal->tab_ruler[terminal->column] = 1;
1686 break;
1687 case '7': /* DECSC */
1688 terminal->saved_row = terminal->row;
1689 terminal->saved_column = terminal->column;
1690 terminal->saved_attr = terminal->curr_attr;
1691 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001692 terminal->saved_cs = terminal->cs;
1693 terminal->saved_g0 = terminal->g0;
1694 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001695 break;
1696 case '8': /* DECRC */
1697 terminal->row = terminal->saved_row;
1698 terminal->column = terminal->saved_column;
1699 terminal->curr_attr = terminal->saved_attr;
1700 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001701 terminal->cs = terminal->saved_cs;
1702 terminal->g0 = terminal->saved_g0;
1703 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001704 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001705 case '=': /* DECPAM */
1706 terminal->key_mode = KM_APPLICATION;
1707 break;
1708 case '>': /* DECPNM */
1709 terminal->key_mode = KM_NORMAL;
1710 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001711 default:
1712 fprintf(stderr, "Unknown escape code: %c\n", code);
1713 break;
1714 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001715}
1716
1717static void
1718handle_special_escape(struct terminal *terminal, char special, char code)
1719{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001720 int i, numChars;
1721
1722 if (special == '#') {
1723 switch(code) {
1724 case '8':
1725 /* fill with 'E', no cheap way to do this */
1726 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1727 numChars = terminal->width * terminal->height;
1728 for(i = 0; i < numChars; i++) {
1729 terminal->data[i].byte[0] = 'E';
1730 }
1731 break;
1732 default:
1733 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1734 break;
1735 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001736 } else if (special == '(' || special == ')') {
1737 switch(code) {
1738 case '0':
1739 if (special == '(')
1740 terminal->g0 = CS_SPECIAL;
1741 else
1742 terminal->g1 = CS_SPECIAL;
1743 break;
1744 case 'A':
1745 if (special == '(')
1746 terminal->g0 = CS_UK;
1747 else
1748 terminal->g1 = CS_UK;
1749 break;
1750 case 'B':
1751 if (special == '(')
1752 terminal->g0 = CS_US;
1753 else
1754 terminal->g1 = CS_US;
1755 break;
1756 default:
1757 fprintf(stderr, "Unknown character set %c\n", code);
1758 break;
1759 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001760 } else {
1761 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1762 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001763}
1764
1765static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001766handle_sgr(struct terminal *terminal, int code)
1767{
1768 switch(code) {
1769 case 0:
1770 terminal->curr_attr = terminal->color_scheme->default_attr;
1771 break;
1772 case 1:
1773 terminal->curr_attr.a |= ATTRMASK_BOLD;
1774 if (terminal->curr_attr.fg < 8)
1775 terminal->curr_attr.fg += 8;
1776 break;
1777 case 4:
1778 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1779 break;
1780 case 5:
1781 terminal->curr_attr.a |= ATTRMASK_BLINK;
1782 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001783 case 8:
1784 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1785 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001786 case 2:
1787 case 21:
1788 case 22:
1789 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1790 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1791 terminal->curr_attr.fg -= 8;
1792 break;
1793 case 24:
1794 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1795 break;
1796 case 25:
1797 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1798 break;
1799 case 7:
1800 case 26:
1801 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1802 break;
1803 case 27:
1804 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1805 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001806 case 28:
1807 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1808 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001809 case 39:
1810 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1811 break;
1812 case 49:
1813 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1814 break;
1815 default:
1816 if(code >= 30 && code <= 37) {
1817 terminal->curr_attr.fg = code - 30;
1818 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1819 terminal->curr_attr.fg += 8;
1820 } else if(code >= 40 && code <= 47) {
1821 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001822 } else if (code >= 90 && code <= 97) {
1823 terminal->curr_attr.fg = code - 90 + 8;
1824 } else if (code >= 100 && code <= 107) {
1825 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001826 } else if(code >= 256 && code < 512) {
1827 terminal->curr_attr.fg = code - 256;
1828 } else if(code >= 512 && code < 768) {
1829 terminal->curr_attr.bg = code - 512;
1830 } else {
1831 fprintf(stderr, "Unknown SGR code: %d\n", code);
1832 }
1833 break;
1834 }
1835}
1836
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001837/* Returns 1 if c was special, otherwise 0 */
1838static int
1839handle_special_char(struct terminal *terminal, char c)
1840{
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001841 union utf8_char *row;
1842 struct attr *attr_row;
1843
1844 row = terminal_get_row(terminal, terminal->row);
1845 attr_row = terminal_get_attr_row(terminal, terminal->row);
1846
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001847 switch(c) {
1848 case '\r':
1849 terminal->column = 0;
1850 break;
1851 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001852 if (terminal->mode & MODE_LF_NEWLINE) {
1853 terminal->column = 0;
1854 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001855 /* fallthrough */
1856 case '\v':
1857 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001858 terminal->row++;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07001859 if (terminal->row > terminal->margin_bottom) {
Callum Lowcaybbeac602011-01-07 19:46:58 +00001860 terminal->row = terminal->margin_bottom;
1861 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001862 }
1863
1864 break;
1865 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001866 while (terminal->column < terminal->width) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001867 if (terminal->mode & MODE_IRM)
1868 terminal_shift_line(terminal, +1);
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04001869
1870 if (row[terminal->column].byte[0] == '\0') {
1871 row[terminal->column].byte[0] = ' ';
1872 row[terminal->column].byte[1] = '\0';
1873 attr_row[terminal->column] = terminal->curr_attr;
1874 }
1875
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001876 terminal->column++;
Kristian Høgsbergcca3c2f2012-06-20 15:56:13 -04001877 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001878 }
1879 if (terminal->column >= terminal->width) {
1880 terminal->column = terminal->width - 1;
1881 }
1882
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001883 break;
1884 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001885 if (terminal->column >= terminal->width) {
1886 terminal->column = terminal->width - 2;
1887 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001888 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001889 } else if (terminal->mode & MODE_AUTOWRAP) {
1890 terminal->column = terminal->width - 1;
1891 terminal->row -= 1;
1892 if (terminal->row < terminal->margin_top) {
1893 terminal->row = terminal->margin_top;
1894 terminal_scroll(terminal, -1);
1895 }
1896 }
1897
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001898 break;
1899 case '\a':
1900 /* Bell */
1901 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001902 case '\x0E': /* SO */
1903 terminal->cs = terminal->g1;
1904 break;
1905 case '\x0F': /* SI */
1906 terminal->cs = terminal->g0;
1907 break;
Kristian Høgsberg2a1aa4e2012-08-03 09:37:05 -04001908 case '\0':
1909 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001910 default:
1911 return 0;
1912 }
1913
1914 return 1;
1915}
1916
1917static void
1918handle_char(struct terminal *terminal, union utf8_char utf8)
1919{
1920 union utf8_char *row;
1921 struct attr *attr_row;
1922
1923 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001924
1925 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001926
1927 /* There are a whole lot of non-characters, control codes,
1928 * and formatting codes that should probably be ignored,
1929 * for example: */
1930 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1931 /* BOM, ignore */
1932 return;
1933 }
1934
1935 /* Some of these non-characters should be translated, e.g.: */
1936 if (utf8.byte[0] < 32) {
1937 utf8.byte[0] = utf8.byte[0] + 64;
1938 }
1939
1940 /* handle right margin effects */
1941 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001942 if (terminal->mode & MODE_AUTOWRAP) {
1943 terminal->column = 0;
1944 terminal->row += 1;
1945 if (terminal->row > terminal->margin_bottom) {
1946 terminal->row = terminal->margin_bottom;
1947 terminal_scroll(terminal, +1);
1948 }
1949 } else {
1950 terminal->column--;
1951 }
1952 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001953
1954 row = terminal_get_row(terminal, terminal->row);
1955 attr_row = terminal_get_attr_row(terminal, terminal->row);
1956
Callum Lowcay69e96582011-01-07 19:47:00 +00001957 if (terminal->mode & MODE_IRM)
1958 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001959 row[terminal->column] = utf8;
1960 attr_row[terminal->column++] = terminal->curr_attr;
1961
Kristian Høgsberg1d781ee2013-11-24 16:54:12 -08001962 if (terminal->row + terminal->start + 1 > terminal->end)
1963 terminal->end = terminal->row + terminal->start + 1;
1964 if (terminal->end == terminal->buffer_height)
1965 terminal->log_size = terminal->buffer_height;
1966 else if (terminal->log_size < terminal->buffer_height)
1967 terminal->log_size = terminal->end;
1968
Peng Wucfcc1112013-08-19 10:59:21 +08001969 /* cursor jump for wide character. */
1970 if (is_wide(utf8))
1971 row[terminal->column++].ch = 0x200B; /* space glyph */
1972
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001973 if (utf8.ch != terminal->last_char.ch)
1974 terminal->last_char = utf8;
1975}
1976
Callum Lowcay30eeae52011-01-07 19:46:55 +00001977static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001978escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1979{
1980 int len, i;
1981
1982 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1983 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1984 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1985 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1986 else len = 1; /* Invalid, cannot happen */
1987
1988 if (terminal->escape_length + len <= MAX_ESCAPE) {
1989 for (i = 0; i < len; i++)
1990 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1991 terminal->escape_length += len;
1992 } else if (terminal->escape_length < MAX_ESCAPE) {
1993 terminal->escape[terminal->escape_length++] = 0;
1994 }
1995}
1996
1997static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001998terminal_data(struct terminal *terminal, const char *data, size_t length)
1999{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002000 unsigned int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002001 union utf8_char utf8;
2002 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002003
2004 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002005 parser_state =
2006 utf8_next_char(&terminal->state_machine, data[i]);
2007 switch(parser_state) {
2008 case utf8state_accept:
2009 utf8.ch = terminal->state_machine.s.ch;
2010 break;
2011 case utf8state_reject:
2012 /* the unicode replacement character */
2013 utf8.byte[0] = 0xEF;
2014 utf8.byte[1] = 0xBF;
2015 utf8.byte[2] = 0xBD;
2016 utf8.byte[3] = 0x00;
2017 break;
2018 default:
2019 continue;
2020 }
2021
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002022 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13002023 switch (terminal->state) {
2024 case escape_state_escape:
2025 escape_append_utf8(terminal, utf8);
2026 switch (utf8.byte[0]) {
2027 case 'P': /* DCS */
2028 terminal->state = escape_state_dcs;
2029 break;
2030 case '[': /* CSI */
2031 terminal->state = escape_state_csi;
2032 break;
2033 case ']': /* OSC */
2034 terminal->state = escape_state_osc;
2035 break;
2036 case '#':
2037 case '(':
2038 case ')': /* special */
2039 terminal->state = escape_state_special;
2040 break;
2041 case '^': /* PM (not implemented) */
2042 case '_': /* APC (not implemented) */
2043 terminal->state = escape_state_ignore;
2044 break;
2045 default:
2046 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002047 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13002048 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002049 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002050 continue;
2051 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002052 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
2053 /* do nothing */
2054 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002055 terminal->escape_flags |= ESC_FLAG_WHAT;
2056 } else if (utf8.byte[0] == '>') {
2057 terminal->escape_flags |= ESC_FLAG_GT;
2058 } else if (utf8.byte[0] == '!') {
2059 terminal->escape_flags |= ESC_FLAG_BANG;
2060 } else if (utf8.byte[0] == '$') {
2061 terminal->escape_flags |= ESC_FLAG_CASH;
2062 } else if (utf8.byte[0] == '\'') {
2063 terminal->escape_flags |= ESC_FLAG_SQUOTE;
2064 } else if (utf8.byte[0] == '"') {
2065 terminal->escape_flags |= ESC_FLAG_DQUOTE;
2066 } else if (utf8.byte[0] == ' ') {
2067 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002068 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002069 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002070 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13002071 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002072 }
2073
2074 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
2075 utf8.byte[0] == '`')
2076 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002077 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002078 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002079 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002080 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13002081 continue;
2082 case escape_state_inner_escape:
2083 if (utf8.byte[0] == '\\') {
2084 terminal->state = escape_state_normal;
2085 if (terminal->outer_state == escape_state_dcs) {
2086 handle_dcs(terminal);
2087 } else if (terminal->outer_state == escape_state_osc) {
2088 handle_osc(terminal);
2089 }
2090 } else if (utf8.byte[0] == '\e') {
2091 terminal->state = terminal->outer_state;
2092 escape_append_utf8(terminal, utf8);
2093 if (terminal->escape_length >= MAX_ESCAPE)
2094 terminal->state = escape_state_normal;
2095 } else {
2096 terminal->state = terminal->outer_state;
2097 if (terminal->escape_length < MAX_ESCAPE)
2098 terminal->escape[terminal->escape_length++] = '\e';
2099 escape_append_utf8(terminal, utf8);
2100 if (terminal->escape_length >= MAX_ESCAPE)
2101 terminal->state = escape_state_normal;
2102 }
2103 continue;
2104 case escape_state_dcs:
2105 case escape_state_osc:
2106 case escape_state_ignore:
2107 if (utf8.byte[0] == '\e') {
2108 terminal->outer_state = terminal->state;
2109 terminal->state = escape_state_inner_escape;
2110 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
2111 terminal->state = escape_state_normal;
2112 handle_osc(terminal);
2113 } else {
2114 escape_append_utf8(terminal, utf8);
2115 if (terminal->escape_length >= MAX_ESCAPE)
2116 terminal->state = escape_state_normal;
2117 }
2118 continue;
2119 case escape_state_special:
2120 escape_append_utf8(terminal, utf8);
2121 terminal->state = escape_state_normal;
2122 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
2123 handle_special_escape(terminal, terminal->escape[1],
2124 utf8.byte[0]);
2125 }
2126 continue;
2127 default:
2128 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05002129 }
2130
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002131 /* this is valid, because ASCII characters are never used to
2132 * introduce a multibyte sequence in UTF-8 */
2133 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13002134 terminal->state = escape_state_escape;
2135 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05002136 terminal->escape[0] = '\e';
2137 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13002138 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00002139 } else {
2140 handle_char(terminal, utf8);
2141 } /* if */
2142 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05002143
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002144 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002145}
2146
2147static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002148data_source_target(void *data,
2149 struct wl_data_source *source, const char *mime_type)
2150{
2151 fprintf(stderr, "data_source_target, %s\n", mime_type);
2152}
2153
2154static void
2155data_source_send(void *data,
2156 struct wl_data_source *source,
2157 const char *mime_type, int32_t fd)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002158{
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002159 struct terminal *terminal = data;
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002160
Kristian Høgsberg31cce052011-01-21 15:18:55 -05002161 terminal_send_selection(terminal, fd);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002162}
2163
2164static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002165data_source_cancelled(void *data, struct wl_data_source *source)
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002166{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002167 wl_data_source_destroy(source);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002168}
2169
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002170static const struct wl_data_source_listener data_source_listener = {
2171 data_source_target,
2172 data_source_send,
2173 data_source_cancelled
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002174};
2175
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07002176static const char text_mime_type[] = "text/plain;charset=utf-8";
2177
2178static void
2179data_handler(struct window *window,
2180 struct input *input,
2181 float x, float y, const char **types, void *data)
2182{
2183 int i, has_text = 0;
2184
2185 if (!types)
2186 return;
2187 for (i = 0; types[i]; i++)
2188 if (strcmp(types[i], text_mime_type) == 0)
2189 has_text = 1;
2190
2191 if (!has_text) {
2192 input_accept(input, NULL);
2193 } else {
2194 input_accept(input, text_mime_type);
2195 }
2196}
2197
2198static void
2199drop_handler(struct window *window, struct input *input,
2200 int32_t x, int32_t y, void *data)
2201{
2202 struct terminal *terminal = data;
2203
2204 input_receive_drag_data_to_fd(input, text_mime_type, terminal->master);
2205}
2206
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002207static void
2208fullscreen_handler(struct window *window, void *data)
2209{
2210 struct terminal *terminal = data;
2211
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002212 window_set_fullscreen(window, !window_is_fullscreen(terminal->window));
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002213}
2214
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002215static void
Jasper St. Pierrebf175902013-11-12 20:19:58 -05002216close_handler(void *data)
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002217{
2218 struct terminal *terminal = data;
2219
2220 terminal_destroy(terminal);
2221}
2222
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002223static void
2224terminal_copy(struct terminal *terminal, struct input *input)
2225{
2226 terminal->selection =
2227 display_create_data_source(terminal->display);
2228 wl_data_source_offer(terminal->selection,
2229 "text/plain;charset=utf-8");
2230 wl_data_source_add_listener(terminal->selection,
2231 &data_source_listener, terminal);
2232 input_set_selection(input, terminal->selection,
2233 display_get_serial(terminal->display));
2234}
2235
2236static void
2237terminal_paste(struct terminal *terminal, struct input *input)
2238{
2239 input_receive_selection_data_to_fd(input,
2240 "text/plain;charset=utf-8",
2241 terminal->master);
2242
2243}
2244
2245static void
2246terminal_new_instance(struct terminal *terminal)
2247{
2248 struct terminal *new_terminal;
2249
2250 new_terminal = terminal_create(terminal->display);
2251 if (terminal_run(new_terminal, option_shell))
2252 terminal_destroy(new_terminal);
2253}
2254
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002255static int
2256handle_bound_key(struct terminal *terminal,
2257 struct input *input, uint32_t sym, uint32_t time)
2258{
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002259 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002260 case XKB_KEY_X:
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04002261 /* Cut selection; terminal doesn't do cut, fall
2262 * through to copy. */
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002263 case XKB_KEY_C:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002264 terminal_copy(terminal, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002265 return 1;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002266 case XKB_KEY_V:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002267 terminal_paste(terminal, input);
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002268 return 1;
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002269 case XKB_KEY_N:
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002270 terminal_new_instance(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002271 return 1;
2272
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002273 case XKB_KEY_Up:
2274 if (!terminal->scrolling)
2275 terminal->saved_start = terminal->start;
2276 if (terminal->start == terminal->end - terminal->log_size)
2277 return 1;
2278
2279 terminal->scrolling = 1;
2280 terminal->start--;
2281 terminal->row++;
2282 terminal->selection_start_row++;
2283 terminal->selection_end_row++;
2284 widget_schedule_redraw(terminal->widget);
2285 return 1;
2286
2287 case XKB_KEY_Down:
2288 if (!terminal->scrolling)
2289 terminal->saved_start = terminal->start;
2290
2291 if (terminal->start == terminal->saved_start)
2292 return 1;
2293
2294 terminal->scrolling = 1;
2295 terminal->start++;
2296 terminal->row--;
2297 terminal->selection_start_row--;
2298 terminal->selection_end_row--;
2299 widget_schedule_redraw(terminal->widget);
2300 return 1;
2301
Kristian Høgsberg58eec362011-01-19 14:27:42 -05002302 default:
2303 return 0;
2304 }
2305}
2306
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002307static void
2308key_handler(struct window *window, struct input *input, uint32_t time,
Daniel Stonec9785ea2012-05-30 16:31:52 +01002309 uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
2310 void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002311{
2312 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002313 char ch[MAX_RESPONSE];
Kristian Høgsberg88fd4082012-06-21 15:55:03 -04002314 uint32_t modifiers, serial;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002315 int ret, len = 0, d;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002316 bool convert_utf8 = true;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002317
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002318 modifiers = input_get_modifiers(input);
Kristian Høgsberg70163132012-05-08 15:55:39 -04002319 if ((modifiers & MOD_CONTROL_MASK) &&
2320 (modifiers & MOD_SHIFT_MASK) &&
Daniel Stonec9785ea2012-05-30 16:31:52 +01002321 state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2322 handle_bound_key(terminal, input, sym, time))
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05002323 return;
2324
Daniel Stonea8468712012-11-07 17:51:35 +11002325 /* Map keypad symbols to 'normal' equivalents before processing */
2326 switch (sym) {
2327 case XKB_KEY_KP_Space:
2328 sym = XKB_KEY_space;
2329 break;
2330 case XKB_KEY_KP_Tab:
2331 sym = XKB_KEY_Tab;
2332 break;
2333 case XKB_KEY_KP_Enter:
2334 sym = XKB_KEY_Return;
2335 break;
2336 case XKB_KEY_KP_Left:
2337 sym = XKB_KEY_Left;
2338 break;
2339 case XKB_KEY_KP_Up:
2340 sym = XKB_KEY_Up;
2341 break;
2342 case XKB_KEY_KP_Right:
2343 sym = XKB_KEY_Right;
2344 break;
2345 case XKB_KEY_KP_Down:
2346 sym = XKB_KEY_Down;
2347 break;
2348 case XKB_KEY_KP_Equal:
2349 sym = XKB_KEY_equal;
2350 break;
2351 case XKB_KEY_KP_Multiply:
2352 sym = XKB_KEY_asterisk;
2353 break;
2354 case XKB_KEY_KP_Add:
2355 sym = XKB_KEY_plus;
2356 break;
2357 case XKB_KEY_KP_Separator:
2358 /* Note this is actually locale-dependent and should mostly be
2359 * a comma. But leave it as period until we one day start
2360 * doing the right thing. */
2361 sym = XKB_KEY_period;
2362 break;
2363 case XKB_KEY_KP_Subtract:
2364 sym = XKB_KEY_minus;
2365 break;
2366 case XKB_KEY_KP_Decimal:
2367 sym = XKB_KEY_period;
2368 break;
2369 case XKB_KEY_KP_Divide:
2370 sym = XKB_KEY_slash;
2371 break;
2372 case XKB_KEY_KP_0:
2373 case XKB_KEY_KP_1:
2374 case XKB_KEY_KP_2:
2375 case XKB_KEY_KP_3:
2376 case XKB_KEY_KP_4:
2377 case XKB_KEY_KP_5:
2378 case XKB_KEY_KP_6:
2379 case XKB_KEY_KP_7:
2380 case XKB_KEY_KP_8:
2381 case XKB_KEY_KP_9:
2382 sym = (sym - XKB_KEY_KP_0) + XKB_KEY_0;
2383 break;
2384 default:
2385 break;
2386 }
2387
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002388 switch (sym) {
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002389 case XKB_KEY_BackSpace:
Kristian Høgsbergb7f94bf2012-06-21 12:29:36 -04002390 if (modifiers & MOD_ALT_MASK)
2391 ch[len++] = 0x1b;
Kristian Høgsberg71a4cf42012-06-20 17:57:56 -04002392 ch[len++] = 0x7f;
2393 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002394 case XKB_KEY_Tab:
2395 case XKB_KEY_Linefeed:
2396 case XKB_KEY_Clear:
2397 case XKB_KEY_Pause:
2398 case XKB_KEY_Scroll_Lock:
2399 case XKB_KEY_Sys_Req:
2400 case XKB_KEY_Escape:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002401 ch[len++] = sym & 0x7f;
2402 break;
2403
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002404 case XKB_KEY_Return:
Callum Lowcay8e57dd52011-01-07 19:46:59 +00002405 if (terminal->mode & MODE_LF_NEWLINE) {
2406 ch[len++] = 0x0D;
2407 ch[len++] = 0x0A;
2408 } else {
2409 ch[len++] = 0x0D;
2410 }
2411 break;
2412
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002413 case XKB_KEY_Shift_L:
2414 case XKB_KEY_Shift_R:
2415 case XKB_KEY_Control_L:
2416 case XKB_KEY_Control_R:
2417 case XKB_KEY_Alt_L:
2418 case XKB_KEY_Alt_R:
Kristian Høgsberg22fbcf72012-06-22 12:18:56 -04002419 case XKB_KEY_Meta_L:
2420 case XKB_KEY_Meta_R:
2421 case XKB_KEY_Super_L:
2422 case XKB_KEY_Super_R:
2423 case XKB_KEY_Hyper_L:
2424 case XKB_KEY_Hyper_R:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002425 break;
2426
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002427 case XKB_KEY_Insert:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002428 len = function_key_response('[', 2, modifiers, '~', ch);
2429 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002430 case XKB_KEY_Delete:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002431 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2432 ch[len++] = '\x04';
2433 } else {
2434 len = function_key_response('[', 3, modifiers, '~', ch);
2435 }
2436 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002437 case XKB_KEY_Page_Up:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002438 len = function_key_response('[', 5, modifiers, '~', ch);
2439 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002440 case XKB_KEY_Page_Down:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002441 len = function_key_response('[', 6, modifiers, '~', ch);
2442 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002443 case XKB_KEY_F1:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002444 len = function_key_response('O', 1, modifiers, 'P', ch);
2445 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002446 case XKB_KEY_F2:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002447 len = function_key_response('O', 1, modifiers, 'Q', ch);
2448 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002449 case XKB_KEY_F3:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002450 len = function_key_response('O', 1, modifiers, 'R', ch);
2451 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002452 case XKB_KEY_F4:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002453 len = function_key_response('O', 1, modifiers, 'S', ch);
2454 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002455 case XKB_KEY_F5:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002456 len = function_key_response('[', 15, modifiers, '~', ch);
2457 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002458 case XKB_KEY_F6:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002459 len = function_key_response('[', 17, modifiers, '~', ch);
2460 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002461 case XKB_KEY_F7:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002462 len = function_key_response('[', 18, modifiers, '~', ch);
2463 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002464 case XKB_KEY_F8:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002465 len = function_key_response('[', 19, modifiers, '~', ch);
2466 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002467 case XKB_KEY_F9:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002468 len = function_key_response('[', 20, modifiers, '~', ch);
2469 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002470 case XKB_KEY_F10:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002471 len = function_key_response('[', 21, modifiers, '~', ch);
2472 break;
Kristian Høgsbergbef52d12012-05-11 11:24:29 -04002473 case XKB_KEY_F12:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002474 len = function_key_response('[', 24, modifiers, '~', ch);
2475 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002476 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002477 /* Handle special keys with alternate mappings */
2478 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2479 if (len != 0) break;
2480
Kristian Høgsberg70163132012-05-08 15:55:39 -04002481 if (modifiers & MOD_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002482 if (sym >= '3' && sym <= '7')
2483 sym = (sym & 0x1f) + 8;
2484
2485 if (!((sym >= '!' && sym <= '/') ||
2486 (sym >= '8' && sym <= '?') ||
2487 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2488 else if (sym == '2') sym = 0x00;
2489 else if (sym == '/') sym = 0x1F;
2490 else if (sym == '8' || sym == '?') sym = 0x7F;
Kristian Høgsbergae9e0732012-06-21 12:30:15 -04002491 }
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002492 if (modifiers & MOD_ALT_MASK) {
2493 if (terminal->mode & MODE_ALT_SENDS_ESC) {
2494 ch[len++] = 0x1b;
2495 } else {
2496 sym = sym | 0x80;
2497 convert_utf8 = false;
2498 }
Callum Lowcay7e08e902011-01-07 19:47:02 +00002499 }
2500
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002501 if ((sym < 128) ||
2502 (!convert_utf8 && sym < 256)) {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002503 ch[len++] = sym;
Philipp Brüschweilerfdb4b022012-08-18 13:38:38 +02002504 } else {
2505 ret = xkb_keysym_to_utf8(sym, ch + len,
2506 MAX_RESPONSE - len);
2507 if (ret < 0)
2508 fprintf(stderr,
2509 "Warning: buffer too small to encode "
2510 "UTF8 character\n");
2511 else
2512 len += ret;
2513 }
2514
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002515 break;
2516 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002517
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002518 if (state == WL_KEYBOARD_KEY_STATE_PRESSED && len > 0) {
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002519 if (terminal->scrolling) {
2520 d = terminal->saved_start - terminal->start;
2521 terminal->row -= d;
2522 terminal->selection_start_row -= d;
2523 terminal->selection_end_row -= d;
2524 terminal->start = terminal->saved_start;
2525 terminal->scrolling = 0;
2526 widget_schedule_redraw(terminal->widget);
2527 }
2528
Kristian Høgsberg26130862011-08-24 11:30:21 -04002529 terminal_write(terminal, ch, len);
Kristian Høgsbergb24ab802012-06-22 12:17:17 -04002530
2531 /* Hide cursor, except if this was coming from a
2532 * repeating key press. */
2533 serial = display_get_serial(terminal->display);
2534 if (terminal->hide_cursor_serial != serial) {
2535 input_set_pointer_image(input, CURSOR_BLANK);
2536 terminal->hide_cursor_serial = serial;
2537 }
2538 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002539}
2540
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002541static void
2542keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002543 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002544{
2545 struct terminal *terminal = data;
2546
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002547 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002548}
2549
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002550static int wordsep(int ch)
2551{
2552 const char extra[] = "-,./?%&#:_=+@~";
2553
Andre Heider552d12b2012-08-02 20:59:43 +02002554 if (ch > 127)
2555 return 1;
2556
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002557 return ch == 0 || !(isalpha(ch) || isdigit(ch) || strchr(extra, ch));
2558}
2559
2560static int
2561recompute_selection(struct terminal *terminal)
2562{
2563 struct rectangle allocation;
2564 int col, x, width, height;
2565 int start_row, end_row;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002566 int word_start, eol;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002567 int side_margin, top_margin;
2568 int start_x, end_x;
2569 int cw, ch;
2570 union utf8_char *data;
2571
Peng Wuf291f202013-06-06 15:32:41 +08002572 cw = terminal->average_width;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002573 ch = terminal->extents.height;
2574 widget_get_allocation(terminal->widget, &allocation);
2575 width = terminal->width * cw;
2576 height = terminal->height * ch;
2577 side_margin = allocation.x + (allocation.width - width) / 2;
2578 top_margin = allocation.y + (allocation.height - height) / 2;
2579
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002580 start_row = (terminal->selection_start_y - top_margin + ch) / ch - 1;
2581 end_row = (terminal->selection_end_y - top_margin + ch) / ch - 1;
2582
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002583 if (start_row < end_row ||
2584 (start_row == end_row &&
2585 terminal->selection_start_x < terminal->selection_end_x)) {
2586 terminal->selection_start_row = start_row;
2587 terminal->selection_end_row = end_row;
2588 start_x = terminal->selection_start_x;
2589 end_x = terminal->selection_end_x;
2590 } else {
2591 terminal->selection_start_row = end_row;
2592 terminal->selection_end_row = start_row;
2593 start_x = terminal->selection_end_x;
2594 end_x = terminal->selection_start_x;
2595 }
2596
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002597 eol = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002598 if (terminal->selection_start_row < 0) {
2599 terminal->selection_start_row = 0;
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002600 terminal->selection_start_col = 0;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002601 } else {
2602 x = side_margin + cw / 2;
2603 data = terminal_get_row(terminal,
2604 terminal->selection_start_row);
2605 word_start = 0;
2606 for (col = 0; col < terminal->width; col++, x += cw) {
2607 if (col == 0 || wordsep(data[col - 1].ch))
2608 word_start = col;
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002609 if (data[col].ch != 0)
2610 eol = col + 1;
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002611 if (start_x < x)
2612 break;
2613 }
2614
2615 switch (terminal->dragging) {
2616 case SELECT_LINE:
2617 terminal->selection_start_col = 0;
2618 break;
2619 case SELECT_WORD:
2620 terminal->selection_start_col = word_start;
2621 break;
2622 case SELECT_CHAR:
2623 terminal->selection_start_col = col;
2624 break;
2625 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002626 }
2627
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002628 if (terminal->selection_end_row >= terminal->height) {
2629 terminal->selection_end_row = terminal->height;
2630 terminal->selection_end_col = 0;
2631 } else {
2632 x = side_margin + cw / 2;
2633 data = terminal_get_row(terminal, terminal->selection_end_row);
2634 for (col = 0; col < terminal->width; col++, x += cw) {
2635 if (terminal->dragging == SELECT_CHAR && end_x < x)
2636 break;
2637 if (terminal->dragging == SELECT_WORD &&
2638 end_x < x && wordsep(data[col].ch))
2639 break;
2640 }
Kristian Høgsberg8268d412012-06-29 11:35:24 -04002641 terminal->selection_end_col = col;
2642 }
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002643
Kristian Høgsberg0fe782b2012-07-01 21:31:46 -04002644 if (terminal->selection_end_col != terminal->selection_start_col ||
2645 terminal->selection_start_row != terminal->selection_end_row) {
2646 col = terminal->selection_end_col;
2647 if (col > 0 && data[col - 1].ch == 0)
2648 terminal->selection_end_col = terminal->width;
2649 data = terminal_get_row(terminal, terminal->selection_start_row);
2650 if (data[terminal->selection_start_col].ch == 0)
2651 terminal->selection_start_col = eol;
2652 }
2653
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002654 return 1;
2655}
2656
Kristian Høgsberg59826582011-01-20 11:56:57 -05002657static void
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002658menu_func(struct window *window, struct input *input, int index, void *data)
2659{
2660 struct terminal *terminal = data;
2661
2662 fprintf(stderr, "picked entry %d\n", index);
2663
2664 switch (index) {
2665 case 0:
2666 terminal_new_instance(terminal);
2667 break;
2668 case 1:
2669 terminal_copy(terminal, input);
2670 break;
2671 case 2:
2672 terminal_paste(terminal, input);
2673 break;
2674 }
2675}
2676
2677static void
2678show_menu(struct terminal *terminal, struct input *input, uint32_t time)
2679{
2680 int32_t x, y;
2681 static const char *entries[] = {
2682 "Open Terminal", "Copy", "Paste"
2683 };
2684
2685 input_get_position(input, &x, &y);
2686 window_show_menu(terminal->display, input, time, terminal->window,
2687 x - 10, y - 10, menu_func,
2688 entries, ARRAY_LENGTH(entries));
2689}
2690
2691static void
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002692click_handler(struct widget *widget, struct terminal *terminal,
2693 struct input *input, int32_t x, int32_t y,
2694 uint32_t time)
2695{
2696 if (time - terminal->click_time < 500)
2697 terminal->click_count++;
2698 else
2699 terminal->click_count = 1;
2700
2701 terminal->click_time = time;
2702 terminal->dragging = (terminal->click_count - 1) % 3 + SELECT_CHAR;
2703
2704 terminal->selection_end_x = terminal->selection_start_x = x;
2705 terminal->selection_end_y = terminal->selection_start_y = y;
2706 if (recompute_selection(terminal))
2707 widget_schedule_redraw(widget);
2708}
2709
2710static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -05002711button_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002712 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002713 uint32_t button,
2714 enum wl_pointer_button_state state, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002715{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002716 struct terminal *terminal = data;
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002717 int32_t x, y;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002718
2719 switch (button) {
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002720 case BTN_LEFT:
Daniel Stone4dbadb12012-05-30 16:31:51 +01002721 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002722 input_get_position(input, &x, &y);
2723 click_handler(widget, terminal, input, x, y, time);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002724 } else {
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002725 terminal->dragging = SELECT_NONE;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002726 }
2727 break;
Kristian Høgsberg67b82152013-10-23 16:52:05 -07002728
2729 case BTN_RIGHT:
2730 if (state == WL_POINTER_BUTTON_STATE_PRESSED)
2731 show_menu(terminal, input, time);
2732 break;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002733 }
2734}
2735
2736static int
Kristian Høgsberg29784402012-06-28 14:27:02 -04002737enter_handler(struct widget *widget,
2738 struct input *input, float x, float y, void *data)
2739{
Kristian Høgsberga83be202013-10-23 20:47:35 -07002740 struct terminal *terminal = data;
2741
2742 /* Reset title to get rid of resizing '[WxH]' in titlebar */
2743 window_set_title(terminal->window, terminal->title);
2744
Kristian Høgsberg29784402012-06-28 14:27:02 -04002745 return CURSOR_IBEAM;
2746}
2747
2748static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -05002749motion_handler(struct widget *widget,
Kristian Høgsberg59826582011-01-20 11:56:57 -05002750 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -04002751 float x, float y, void *data)
Kristian Høgsberg59826582011-01-20 11:56:57 -05002752{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002753 struct terminal *terminal = data;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002754
2755 if (terminal->dragging) {
Kristian Høgsberg59826582011-01-20 11:56:57 -05002756 input_get_position(input,
2757 &terminal->selection_end_x,
2758 &terminal->selection_end_y);
Kristian Høgsberg333db0a2012-06-27 17:43:10 -04002759
2760 if (recompute_selection(terminal))
2761 widget_schedule_redraw(widget);
Kristian Høgsberg59826582011-01-20 11:56:57 -05002762 }
2763
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +03002764 return CURSOR_IBEAM;
Kristian Høgsberg59826582011-01-20 11:56:57 -05002765}
2766
Alexander Larssonde79dd02013-05-22 14:41:34 +02002767static void
2768output_handler(struct window *window, struct output *output, int enter,
2769 void *data)
2770{
2771 if (enter)
2772 window_set_buffer_transform(window, output_get_transform(output));
2773 window_set_buffer_scale(window, window_get_output_scale(window));
2774 window_schedule_redraw(window);
2775}
2776
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002777static void
2778touch_down_handler(struct widget *widget, struct input *input,
2779 uint32_t serial, uint32_t time, int32_t id,
2780 float x, float y, void *data)
2781{
2782 struct terminal *terminal = data;
2783
2784 if (id == 0)
2785 click_handler(widget, terminal, input, x, y, time);
2786}
2787
2788static void
2789touch_up_handler(struct widget *widget, struct input *input,
2790 uint32_t serial, uint32_t time, int32_t id, void *data)
2791{
2792 struct terminal *terminal = data;
2793
2794 if (id == 0)
2795 terminal->dragging = SELECT_NONE;
2796}
2797
2798static void
2799touch_motion_handler(struct widget *widget, struct input *input,
2800 uint32_t time, int32_t id, float x, float y, void *data)
2801{
2802 struct terminal *terminal = data;
2803
2804 if (terminal->dragging &&
2805 id == 0) {
2806 terminal->selection_end_x = (int)x;
2807 terminal->selection_end_y = (int)y;
2808
2809 if (recompute_selection(terminal))
2810 widget_schedule_redraw(widget);
2811 }
2812}
2813
Peng Wuf291f202013-06-06 15:32:41 +08002814#ifndef howmany
2815#define howmany(x, y) (((x) + ((y) - 1)) / (y))
2816#endif
2817
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002818static struct terminal *
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002819terminal_create(struct display *display)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002820{
2821 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002822 cairo_surface_t *surface;
2823 cairo_t *cr;
Peng Wuf291f202013-06-06 15:32:41 +08002824 cairo_text_extents_t text_extents;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002825
Peter Huttererf3d62272013-08-08 11:57:05 +10002826 terminal = xzalloc(sizeof *terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002827 terminal->color_scheme = &DEFAULT_COLORS;
2828 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002829 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002830 terminal->margin_bottom = -1;
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002831 terminal->window = window_create(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -05002832 terminal->widget = window_frame_create(terminal->window, terminal);
Kristian Høgsberga83be202013-10-23 20:47:35 -07002833 terminal->title = strdup("Wayland Terminal");
2834 window_set_title(terminal->window, terminal->title);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002835 widget_set_transparent(terminal->widget, 0);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002836
2837 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002838 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002839
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002840 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002841 terminal->margin = 5;
Kristian Høgsberg14f39b22013-10-23 16:29:14 -07002842 terminal->buffer_height = 1024;
2843 terminal->end = 1;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002844
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002845 window_set_user_data(terminal->window, terminal);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002846 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002847 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002848 keyboard_focus_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002849 window_set_fullscreen_handler(terminal->window, fullscreen_handler);
Alexander Larssonde79dd02013-05-22 14:41:34 +02002850 window_set_output_handler(terminal->window, output_handler);
Dima Ryazanovd20fd4d2013-01-28 01:11:06 -08002851 window_set_close_handler(terminal->window, close_handler);
Kristian Høgsberg67ace202012-07-23 21:56:31 -04002852
Kristian Høgsberg0749e3f2013-09-04 20:41:06 -07002853 window_set_data_handler(terminal->window, data_handler);
2854 window_set_drop_handler(terminal->window, drop_handler);
2855
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -05002856 widget_set_redraw_handler(terminal->widget, redraw_handler);
2857 widget_set_resize_handler(terminal->widget, resize_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002858 widget_set_button_handler(terminal->widget, button_handler);
Kristian Høgsberg29784402012-06-28 14:27:02 -04002859 widget_set_enter_handler(terminal->widget, enter_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -05002860 widget_set_motion_handler(terminal->widget, motion_handler);
Xiong Zhang49c6aee2013-11-25 18:42:52 +08002861 widget_set_touch_up_handler(terminal->widget, touch_up_handler);
2862 widget_set_touch_down_handler(terminal->widget, touch_down_handler);
2863 widget_set_touch_motion_handler(terminal->widget, touch_motion_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002864
Kristian Høgsberg09531622010-06-14 23:22:15 -04002865 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2866 cr = cairo_create(surface);
Kristian Høgsberg38912df2012-06-27 17:52:23 -04002867 cairo_set_font_size(cr, option_font_size);
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002868 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002869 CAIRO_FONT_SLANT_NORMAL,
2870 CAIRO_FONT_WEIGHT_BOLD);
2871 terminal->font_bold = cairo_get_scaled_font (cr);
2872 cairo_scaled_font_reference(terminal->font_bold);
2873
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002874 cairo_select_font_face (cr, option_font,
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002875 CAIRO_FONT_SLANT_NORMAL,
2876 CAIRO_FONT_WEIGHT_NORMAL);
2877 terminal->font_normal = cairo_get_scaled_font (cr);
2878 cairo_scaled_font_reference(terminal->font_normal);
2879
Kristian Høgsberg09531622010-06-14 23:22:15 -04002880 cairo_font_extents(cr, &terminal->extents);
Peng Wuf291f202013-06-06 15:32:41 +08002881
2882 /* Compute the average ascii glyph width */
2883 cairo_text_extents(cr, TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS,
2884 &text_extents);
2885 terminal->average_width = howmany
2886 (text_extents.width,
2887 strlen(TERMINAL_DRAW_SINGLE_WIDE_CHARACTERS));
2888 terminal->average_width = ceil(terminal->average_width);
2889
Kristian Høgsberg09531622010-06-14 23:22:15 -04002890 cairo_destroy(cr);
2891 cairo_surface_destroy(surface);
2892
Kristian Høgsbergd3a19652012-07-20 11:32:51 -04002893 terminal_resize(terminal, 20, 5); /* Set minimum size first */
Kristian Høgsberga1627922012-06-20 17:30:03 -04002894 terminal_resize(terminal, 80, 25);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -05002895
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002896 wl_list_insert(terminal_list.prev, &terminal->link);
2897
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002898 return terminal;
2899}
2900
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002901static void
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002902terminal_destroy(struct terminal *terminal)
2903{
Dima Ryazanova85292e2012-11-29 00:27:09 -08002904 display_unwatch_fd(terminal->display, terminal->master);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002905 window_destroy(terminal->window);
2906 close(terminal->master);
2907 wl_list_remove(&terminal->link);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002908
2909 if (wl_list_empty(&terminal_list))
Dima Ryazanova85292e2012-11-29 00:27:09 -08002910 display_exit(terminal->display);
2911
Kristian Høgsberga83be202013-10-23 20:47:35 -07002912 free(terminal->title);
Dima Ryazanova85292e2012-11-29 00:27:09 -08002913 free(terminal);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002914}
2915
2916static void
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002917io_handler(struct task *task, uint32_t events)
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002918{
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002919 struct terminal *terminal =
2920 container_of(task, struct terminal, io_task);
2921 char buffer[256];
2922 int len;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002923
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002924 if (events & EPOLLHUP) {
2925 terminal_destroy(terminal);
2926 return;
2927 }
Tim Wiederhakef71accc2011-01-19 23:14:33 +01002928
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002929 len = read(terminal->master, buffer, sizeof buffer);
2930 if (len < 0)
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002931 terminal_destroy(terminal);
2932 else
2933 terminal_data(terminal, buffer, len);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002934}
2935
2936static int
2937terminal_run(struct terminal *terminal, const char *path)
2938{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002939 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002940 pid_t pid;
2941
2942 pid = forkpty(&master, NULL, NULL, NULL);
2943 if (pid == 0) {
Kristian Høgsbergde845cf2012-06-20 22:14:31 -04002944 setenv("TERM", option_term, 1);
2945 setenv("COLORTERM", option_term, 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002946 if (execl(path, path, NULL)) {
2947 printf("exec failed: %m\n");
2948 exit(EXIT_FAILURE);
2949 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002950 } else if (pid < 0) {
2951 fprintf(stderr, "failed to fork and create pty (%m).\n");
2952 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002953 }
2954
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002955 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002956 fcntl(master, F_SETFL, O_NONBLOCK);
Kristian Høgsberg3a696272011-09-14 17:33:48 -04002957 terminal->io_task.run = io_handler;
2958 display_watch_fd(terminal->display, terminal->master,
2959 EPOLLIN | EPOLLHUP, &terminal->io_task);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002960
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04002961 window_set_fullscreen(terminal->window, option_fullscreen);
2962 if (!window_is_fullscreen(terminal->window))
Kristian Høgsberg547da5a2011-09-13 20:58:00 -04002963 terminal_resize(terminal, 80, 24);
2964
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002965 return 0;
2966}
2967
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002968static const struct weston_option terminal_options[] = {
2969 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &option_fullscreen },
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002970 { WESTON_OPTION_STRING, "font", 0, &option_font },
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002971 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002972};
2973
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002974int main(int argc, char *argv[])
2975{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002976 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002977 struct terminal *terminal;
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -07002978 struct weston_config *config;
2979 struct weston_config_section *s;
Kristian Høgsberg82cd36b2012-06-20 15:29:07 -04002980
Peng Wucfcc1112013-08-19 10:59:21 +08002981 /* as wcwidth is locale-dependent,
2982 wcwidth needs setlocale call to function properly. */
2983 setlocale(LC_ALL, "");
2984
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04002985 option_shell = getenv("SHELL");
2986 if (!option_shell)
2987 option_shell = "/bin/bash";
2988
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07002989 config = weston_config_parse("weston.ini");
Kristian Høgsberg9c3dee12013-09-21 22:23:08 -07002990 s = weston_config_get_section(config, "terminal", NULL, NULL);
2991 weston_config_section_get_string(s, "font", &option_font, "mono");
2992 weston_config_section_get_int(s, "font-size", &option_font_size, 14);
2993 weston_config_section_get_string(s, "term", &option_term, "xterm");
2994 weston_config_destroy(config);
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04002995
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002996 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002997 if (d == NULL) {
2998 fprintf(stderr, "failed to create display: %m\n");
2999 return -1;
3000 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003001
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003002 wl_list_init(&terminal_list);
Kristian Høgsbergb7ed4cb2012-10-10 11:37:46 -04003003 terminal = terminal_create(d);
Kristian Høgsbergb21fb9f2012-06-20 22:44:03 -04003004 if (terminal_run(terminal, option_shell))
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05003005 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003006
Kristian Høgsberg7824d812010-06-08 14:59:44 -04003007 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05003008
3009 return 0;
3010}