blob: 20974008891ae79fb1adde8cacc926076bfae2ce [file] [log] [blame]
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <fcntl.h>
28#include <unistd.h>
29#include <math.h>
30#include <time.h>
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050031#include <pty.h>
Kristian Høgsbergf04e8382008-12-08 00:07:49 -050032#include <ctype.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050033#include <cairo.h>
34#include <glib.h>
35
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040036#include <X11/keysym.h>
37
Kristian Høgsberg12308a42009-09-28 13:08:50 -040038#include "wayland-util.h"
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050039#include "wayland-client.h"
40#include "wayland-glib.h"
41
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050042#include "window.h"
43
Kristian Høgsberg0395f302008-12-22 12:14:50 -050044static int option_fullscreen;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050045
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050046#define MOD_SHIFT 0x01
47#define MOD_ALT 0x02
48#define MOD_CTRL 0x04
49
Callum Lowcay30eeae52011-01-07 19:46:55 +000050#define ATTRMASK_BOLD 0x01
51#define ATTRMASK_UNDERLINE 0x02
52#define ATTRMASK_BLINK 0x04
53#define ATTRMASK_INVERSE 0x08
Callum Lowcay81179db2011-01-10 12:14:01 +130054#define ATTRMASK_CONCEALED 0x10
Callum Lowcay30eeae52011-01-07 19:46:55 +000055
Callum Lowcayb8609ad2011-01-07 19:46:57 +000056/* Buffer sizes */
57#define MAX_RESPONSE 11
58#define MAX_ESCAPE 64
59
Callum Lowcay8e57dd52011-01-07 19:46:59 +000060/* Terminal modes */
61#define MODE_SHOW_CURSOR 0x00000001
62#define MODE_INVERSE 0x00000002
63#define MODE_AUTOWRAP 0x00000004
64#define MODE_AUTOREPEAT 0x00000008
65#define MODE_LF_NEWLINE 0x00000010
Callum Lowcay69e96582011-01-07 19:47:00 +000066#define MODE_IRM 0x00000020
Callum Lowcay7e08e902011-01-07 19:47:02 +000067#define MODE_DELETE_SENDS_DEL 0x00000040
68#define MODE_ALT_SENDS_ESC 0x00000080
Callum Lowcay8e57dd52011-01-07 19:46:59 +000069
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000070union utf8_char {
71 unsigned char byte[4];
72 uint32_t ch;
73};
74
75enum utf8_state {
76 utf8state_start,
77 utf8state_accept,
78 utf8state_reject,
79 utf8state_expect3,
80 utf8state_expect2,
81 utf8state_expect1
82};
83
84struct utf8_state_machine {
85 enum utf8_state state;
86 int len;
87 union utf8_char s;
88};
89
90static void
91init_state_machine(struct utf8_state_machine *machine)
92{
93 machine->state = utf8state_start;
94 machine->len = 0;
95 machine->s.ch = 0;
96}
97
98static enum utf8_state
99utf8_next_char(struct utf8_state_machine *machine, char c)
100{
101 switch(machine->state) {
102 case utf8state_start:
103 case utf8state_accept:
104 case utf8state_reject:
105 machine->s.ch = 0;
106 machine->len = 0;
107 if(c == 0xC0 || c == 0xC1) {
108 /* overlong encoding, reject */
109 machine->state = utf8state_reject;
110 } else if((c & 0x80) == 0) {
111 /* single byte, accept */
112 machine->s.byte[machine->len++] = c;
113 machine->state = utf8state_accept;
114 } else if((c & 0xC0) == 0x80) {
115 /* parser out of sync, ignore byte */
116 machine->state = utf8state_start;
117 } else if((c & 0xE0) == 0xC0) {
118 /* start of two byte sequence */
119 machine->s.byte[machine->len++] = c;
120 machine->state = utf8state_expect1;
121 } else if((c & 0xF0) == 0xE0) {
122 /* start of three byte sequence */
123 machine->s.byte[machine->len++] = c;
124 machine->state = utf8state_expect2;
125 } else if((c & 0xF8) == 0xF0) {
126 /* start of four byte sequence */
127 machine->s.byte[machine->len++] = c;
128 machine->state = utf8state_expect3;
129 } else {
130 /* overlong encoding, reject */
131 machine->state = utf8state_reject;
132 }
133 break;
134 case utf8state_expect3:
135 machine->s.byte[machine->len++] = c;
136 if((c & 0xC0) == 0x80) {
137 /* all good, continue */
138 machine->state = utf8state_expect2;
139 } else {
140 /* missing extra byte, reject */
141 machine->state = utf8state_reject;
142 }
143 break;
144 case utf8state_expect2:
145 machine->s.byte[machine->len++] = c;
146 if((c & 0xC0) == 0x80) {
147 /* all good, continue */
148 machine->state = utf8state_expect1;
149 } else {
150 /* missing extra byte, reject */
151 machine->state = utf8state_reject;
152 }
153 break;
154 case utf8state_expect1:
155 machine->s.byte[machine->len++] = c;
156 if((c & 0xC0) == 0x80) {
157 /* all good, accept */
158 machine->state = utf8state_accept;
159 } else {
160 /* missing extra byte, reject */
161 machine->state = utf8state_reject;
162 }
163 break;
164 default:
165 machine->state = utf8state_reject;
166 break;
167 }
168
169 return machine->state;
170}
171
Callum Lowcay256e72f2011-01-07 19:47:01 +0000172struct char_sub {
173 union utf8_char match;
174 union utf8_char replace;
175};
176/* Set last char_sub match to NULL char */
177typedef struct char_sub *character_set;
178
179struct char_sub CS_US[] = {
180 {{{0, }}, {{0, }}}
181};
182static struct char_sub CS_UK[] = {
183 {{{'#', 0, }}, {{0xC2, 0xA3, 0, }}},
184 {{{0, }}, {{0, }}}
185};
186static struct char_sub CS_SPECIAL[] = {
187 {{{'`', 0, }}, {{0xE2, 0x99, 0xA6, 0}}}, /* diamond */
188 {{{'a', 0, }}, {{0xE2, 0x96, 0x92, 0}}}, /* 50% cell */
189 {{{'b', 0, }}, {{0xE2, 0x90, 0x89, 0}}}, /* HT */
190 {{{'c', 0, }}, {{0xE2, 0x90, 0x8C, 0}}}, /* FF */
191 {{{'d', 0, }}, {{0xE2, 0x90, 0x8D, 0}}}, /* CR */
192 {{{'e', 0, }}, {{0xE2, 0x90, 0x8A, 0}}}, /* LF */
193 {{{'f', 0, }}, {{0xC2, 0xB0, 0, }}}, /* Degree */
194 {{{'g', 0, }}, {{0xC2, 0xB1, 0, }}}, /* Plus/Minus */
195 {{{'h', 0, }}, {{0xE2, 0x90, 0xA4, 0}}}, /* NL */
196 {{{'i', 0, }}, {{0xE2, 0x90, 0x8B, 0}}}, /* VT */
197 {{{'j', 0, }}, {{0xE2, 0x94, 0x98, 0}}}, /* CN_RB */
198 {{{'k', 0, }}, {{0xE2, 0x94, 0x90, 0}}}, /* CN_RT */
199 {{{'l', 0, }}, {{0xE2, 0x94, 0x8C, 0}}}, /* CN_LT */
200 {{{'m', 0, }}, {{0xE2, 0x94, 0x94, 0}}}, /* CN_RB */
201 {{{'n', 0, }}, {{0xE2, 0x94, 0xBC, 0}}}, /* CROSS */
202 {{{'o', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
203 {{{'p', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
204 {{{'q', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
205 {{{'r', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
206 {{{'s', 0, }}, {{0xE2, 0x94, 0x80, 0}}}, /* H */
207 {{{'t', 0, }}, {{0xE2, 0x94, 0x9C, 0}}}, /* TR */
208 {{{'u', 0, }}, {{0xE2, 0x94, 0xA4, 0}}}, /* TL */
209 {{{'v', 0, }}, {{0xE2, 0x94, 0xB4, 0}}}, /* TU */
210 {{{'w', 0, }}, {{0xE2, 0x94, 0xAC, 0}}}, /* TD */
211 {{{'x', 0, }}, {{0xE2, 0x94, 0x82, 0}}}, /* V */
212 {{{'y', 0, }}, {{0xE2, 0x89, 0xA4, 0}}}, /* LE */
213 {{{'z', 0, }}, {{0xE2, 0x89, 0xA5, 0}}}, /* GE */
214 {{{'{', 0, }}, {{0xCF, 0x80, 0, }}}, /* PI */
215 {{{'|', 0, }}, {{0xE2, 0x89, 0xA0, 0}}}, /* NEQ */
216 {{{'}', 0, }}, {{0xC2, 0xA3, 0, }}}, /* POUND */
217 {{{'~', 0, }}, {{0xE2, 0x8B, 0x85, 0}}}, /* DOT */
218 {{{0, }}, {{0, }}}
219};
220
221static void
222apply_char_set(character_set cs, union utf8_char *utf8)
223{
224 int i = 0;
225
226 while (cs[i].match.byte[0]) {
227 if ((*utf8).ch == cs[i].match.ch) {
228 *utf8 = cs[i].replace;
229 break;
230 }
231 i++;
232 }
233}
234
Callum Lowcay7e08e902011-01-07 19:47:02 +0000235struct key_map {
236 int sym;
237 int num;
238 char escape;
239 char code;
240};
241/* Set last key_sub sym to NULL */
242typedef struct key_map *keyboard_mode;
243
244static struct key_map KM_NORMAL[] = {
245 {XK_Left, 1, '[', 'D'},
246 {XK_Right, 1, '[', 'C'},
247 {XK_Up, 1, '[', 'A'},
248 {XK_Down, 1, '[', 'B'},
249 {XK_Home, 1, '[', 'H'},
250 {XK_End, 1, '[', 'F'},
251 {0, 0, 0, 0}
252};
253static struct key_map KM_APPLICATION[] = {
254 {XK_Left, 1, 'O', 'D'},
255 {XK_Right, 1, 'O', 'C'},
256 {XK_Up, 1, 'O', 'A'},
257 {XK_Down, 1, 'O', 'B'},
258 {XK_Home, 1, 'O', 'H'},
259 {XK_End, 1, 'O', 'F'},
260 {XK_KP_Enter, 1, 'O', 'M'},
261 {XK_KP_Multiply, 1, 'O', 'j'},
262 {XK_KP_Add, 1, 'O', 'k'},
263 {XK_KP_Separator, 1, 'O', 'l'},
264 {XK_KP_Subtract, 1, 'O', 'm'},
265 {XK_KP_Divide, 1, 'O', 'o'},
266 {0, 0, 0, 0}
267};
268
269static int
270function_key_response(char escape, int num, uint32_t modifiers,
271 char code, char *response)
272{
273 int mod_num = 0;
274 int len;
275
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -0500276 if (modifiers & XKB_COMMON_SHIFT_MASK) mod_num |= 1;
277 if (modifiers & XKB_COMMON_MOD1_MASK) mod_num |= 2;
278 if (modifiers & XKB_COMMON_CONTROL_MASK) mod_num |= 4;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000279
280 if (mod_num != 0)
281 len = snprintf(response, MAX_RESPONSE, "\e[%d;%d%c",
282 num, mod_num + 1, code);
283 else if (code != '~')
284 len = snprintf(response, MAX_RESPONSE, "\e%c%c",
285 escape, code);
286 else
287 len = snprintf(response, MAX_RESPONSE, "\e%c%d%c",
288 escape, num, code);
289
290 if (len >= MAX_RESPONSE) return MAX_RESPONSE - 1;
291 else return len;
292}
293
294/* returns the number of bytes written into response,
295 * which must have room for MAX_RESPONSE bytes */
296static int
297apply_key_map(keyboard_mode mode, int sym, uint32_t modifiers, char *response)
298{
299 struct key_map map;
300 int len = 0;
301 int i = 0;
302
303 while (mode[i].sym) {
304 map = mode[i++];
305 if (sym == map.sym) {
306 len = function_key_response(map.escape, map.num,
307 modifiers, map.code,
308 response);
309 break;
310 }
311 }
312
313 return len;
314}
315
Callum Lowcay30eeae52011-01-07 19:46:55 +0000316struct terminal_color { double r, g, b, a; };
317struct attr {
318 unsigned char fg, bg;
319 char a; /* attributes format:
320 * 76543210
Callum Lowcay81179db2011-01-10 12:14:01 +1300321 * cilub */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000322 char r; /* reserved */
323};
324struct color_scheme {
325 struct terminal_color palette[16];
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500326 char border;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000327 struct attr default_attr;
328};
329
330static void
331attr_init(struct attr *data_attr, struct attr attr, int n)
332{
333 int i;
334 for (i = 0; i < n; i++) {
335 data_attr[i] = attr;
336 }
337}
338
Callum Lowcay67a201d2011-01-12 19:23:41 +1300339enum escape_state {
340 escape_state_normal = 0,
341 escape_state_escape,
342 escape_state_dcs,
343 escape_state_csi,
344 escape_state_osc,
345 escape_state_inner_escape,
346 escape_state_ignore,
347 escape_state_special
348};
349
350#define ESC_FLAG_WHAT 0x01
351#define ESC_FLAG_GT 0x02
352#define ESC_FLAG_BANG 0x04
353#define ESC_FLAG_CASH 0x08
354#define ESC_FLAG_SQUOTE 0x10
355#define ESC_FLAG_DQUOTE 0x20
356#define ESC_FLAG_SPACE 0x40
357
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500358struct terminal {
359 struct window *window;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500360 struct display *display;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000361 union utf8_char *data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000362 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000363 struct attr *data_attr;
364 struct attr curr_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000365 uint32_t mode;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000366 char origin_mode;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000367 char saved_origin_mode;
368 struct attr saved_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000369 union utf8_char last_char;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000370 int margin_top, margin_bottom;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000371 character_set cs, g0, g1;
372 character_set saved_cs, saved_g0, saved_g1;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000373 keyboard_mode key_mode;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000374 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500375 int width, height, start, row, column;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000376 int saved_row, saved_column;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500377 int fd, master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500378 GIOChannel *channel;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500379 uint32_t modifiers;
Callum Lowcayb8609ad2011-01-07 19:46:57 +0000380 char escape[MAX_ESCAPE];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500381 int escape_length;
Callum Lowcay67a201d2011-01-12 19:23:41 +1300382 enum escape_state state;
383 enum escape_state outer_state;
384 int escape_flags;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000385 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500386 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500387 int fullscreen;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500388 int focused;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400389 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000390 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400391 cairo_font_extents_t extents;
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500392 cairo_scaled_font_t *font_normal, *font_bold;
Kristian Høgsberg58eec362011-01-19 14:27:42 -0500393
394 uint32_t tag;
395 struct wl_selection *selection;
396 struct wl_selection_offer *selection_offer;
397 uint32_t selection_offer_has_text;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500398};
399
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000400/* Create default tab stops, every 8 characters */
401static void
402terminal_init_tabs(struct terminal *terminal)
403{
404 int i = 0;
405
406 while (i < terminal->width) {
407 if (i % 8 == 0)
408 terminal->tab_ruler[i] = 1;
409 else
410 terminal->tab_ruler[i] = 0;
411 i++;
412 }
413}
414
Callum Lowcay30eeae52011-01-07 19:46:55 +0000415static void
416terminal_init(struct terminal *terminal)
417{
418 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000419 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000420 terminal->mode = MODE_SHOW_CURSOR |
421 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000422 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000423 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000424
425 terminal->row = 0;
426 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000427
Callum Lowcay256e72f2011-01-07 19:47:01 +0000428 terminal->g0 = CS_US;
429 terminal->g1 = CS_US;
430 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000431 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000432
433 terminal->saved_g0 = terminal->g0;
434 terminal->saved_g1 = terminal->g1;
435 terminal->saved_cs = terminal->cs;
436
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000437 terminal->saved_attr = terminal->curr_attr;
438 terminal->saved_origin_mode = terminal->origin_mode;
439 terminal->saved_row = terminal->row;
440 terminal->saved_column = terminal->column;
441
442 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000443}
444
445static void
446init_color_table(struct terminal *terminal)
447{
448 int c, r;
449 struct terminal_color *color_table = terminal->color_table;
450
451 for (c = 0; c < 256; c ++) {
452 if (c < 16) {
453 color_table[c] = terminal->color_scheme->palette[c];
454 } else if (c < 232) {
455 r = c - 16;
456 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
457 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
458 color_table[c].r = ((double)(r % 6) / 6.0);
459 color_table[c].a = 1.0;
460 } else {
461 r = (c - 232) * 10 + 8;
462 color_table[c].r = ((double) r) / 256.0;
463 color_table[c].g = color_table[c].r;
464 color_table[c].b = color_table[c].r;
465 color_table[c].a = 1.0;
466 }
467 }
468}
469
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000470static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500471terminal_get_row(struct terminal *terminal, int row)
472{
473 int index;
474
475 index = (row + terminal->start) % terminal->height;
476
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000477 return &terminal->data[index * terminal->width];
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500478}
479
Callum Lowcay30eeae52011-01-07 19:46:55 +0000480static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500481terminal_get_attr_row(struct terminal *terminal, int row)
482{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000483 int index;
484
485 index = (row + terminal->start) % terminal->height;
486
487 return &terminal->data_attr[index * terminal->width];
488}
489
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500490union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300491 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500492 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500493};
494
495static void
496terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500497 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500498{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500499 struct attr attr;
500 int foreground, background, tmp;
501
502 /* get the attributes for this character cell */
503 attr = terminal_get_attr_row(terminal, row)[col];
504 if ((attr.a & ATTRMASK_INVERSE) ||
505 ((terminal->mode & MODE_SHOW_CURSOR) &&
506 terminal->focused && terminal->row == row &&
507 terminal->column == col)) {
508 foreground = attr.bg;
509 background = attr.fg;
510 if (attr.a & ATTRMASK_BOLD) {
511 if (foreground <= 16) foreground |= 0x08;
512 if (background <= 16) background &= 0x07;
513 }
514 } else {
515 foreground = attr.fg;
516 background = attr.bg;
517 }
518
519 if (terminal->mode & MODE_INVERSE) {
520 tmp = foreground;
521 foreground = background;
522 background = tmp;
523 if (attr.a & ATTRMASK_BOLD) {
524 if (foreground <= 16) foreground |= 0x08;
525 if (background <= 16) background &= 0x07;
526 }
527 }
528
Callum Lowcay9d708b02011-01-12 20:06:17 +1300529 decoded->attr.fg = foreground;
530 decoded->attr.bg = background;
531 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000532}
533
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500534static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000535terminal_scroll_buffer(struct terminal *terminal, int d)
536{
537 int i;
538
539 d = d % (terminal->height + 1);
540 terminal->start = (terminal->start + d) % terminal->height;
541 if (terminal->start < 0) terminal->start = terminal->height + terminal->start;
542 if(d < 0) {
543 d = 0 - d;
544 for(i = 0; i < d; i++) {
545 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
546 attr_init(terminal_get_attr_row(terminal, i),
547 terminal->curr_attr, terminal->width);
548 }
549 } else {
550 for(i = terminal->height - d; i < terminal->height; i++) {
551 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
552 attr_init(terminal_get_attr_row(terminal, i),
553 terminal->curr_attr, terminal->width);
554 }
555 }
556}
557
558static void
559terminal_scroll_window(struct terminal *terminal, int d)
560{
561 int i;
562 int window_height;
563 int from_row, to_row;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000564
565 // scrolling range is inclusive
566 window_height = terminal->margin_bottom - terminal->margin_top + 1;
567 d = d % (window_height + 1);
568 if(d < 0) {
569 d = 0 - d;
570 to_row = terminal->margin_bottom;
571 from_row = terminal->margin_bottom - d;
572
573 for (i = 0; i < (window_height - d); i++) {
574 memcpy(terminal_get_row(terminal, to_row - i),
575 terminal_get_row(terminal, from_row - i),
576 terminal->data_pitch);
577 memcpy(terminal_get_attr_row(terminal, to_row - i),
578 terminal_get_attr_row(terminal, from_row - i),
579 terminal->attr_pitch);
580 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000581 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
582 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000583 attr_init(terminal_get_attr_row(terminal, i),
584 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000585 }
586 } else {
587 to_row = terminal->margin_top;
588 from_row = terminal->margin_top + d;
589
590 for (i = 0; i < (window_height - d); i++) {
591 memcpy(terminal_get_row(terminal, to_row + i),
592 terminal_get_row(terminal, from_row + i),
593 terminal->data_pitch);
594 memcpy(terminal_get_attr_row(terminal, to_row + i),
595 terminal_get_attr_row(terminal, from_row + i),
596 terminal->attr_pitch);
597 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000598 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
599 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000600 attr_init(terminal_get_attr_row(terminal, i),
601 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000602 }
603 }
604}
605
606static void
607terminal_scroll(struct terminal *terminal, int d)
608{
609 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
610 terminal_scroll_buffer(terminal, d);
611 else
612 terminal_scroll_window(terminal, d);
613}
614
615static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000616terminal_shift_line(struct terminal *terminal, int d)
617{
618 union utf8_char *row;
619 struct attr *attr_row, attr;
620
621 row = terminal_get_row(terminal, terminal->row);
622 attr_row = terminal_get_attr_row(terminal, terminal->row);
623
624 if ((terminal->width + d) <= terminal->column)
625 d = terminal->column + 1 - terminal->width;
626 if ((terminal->column + d) >= terminal->width)
627 d = terminal->width - terminal->column - 1;
628
629 if (d < 0) {
630 d = 0 - d;
631 memmove(&row[terminal->column],
632 &row[terminal->column + d],
633 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
634 attr = attr_row[terminal->width - 1];
635 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
636 (terminal->width - terminal->column - d) * sizeof(struct attr));
637 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
638 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
639 } else {
640 memmove(&row[terminal->column + d], &row[terminal->column],
641 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
642 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
643 (terminal->width - terminal->column - d) * sizeof(struct attr));
644 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
645 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
646 }
647}
648
649static void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500650terminal_resize(struct terminal *terminal, int width, int height)
651{
652 size_t size;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000653 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000654 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000655 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000656 int data_pitch, attr_pitch;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500657 int i, l, total_rows, start;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500658 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000659 struct winsize ws;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500660 int32_t pixel_width, pixel_height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500661
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500662 if (width < 1)
663 width = 1;
664 if (height < 1)
665 height = 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500666 if (terminal->width == width && terminal->height == height)
667 return;
668
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500669 if (!terminal->fullscreen) {
670 pixel_width = width *
671 terminal->extents.max_x_advance + 2 * terminal->margin;
672 pixel_height = height *
673 terminal->extents.height + 2 * terminal->margin;
674 window_set_child_size(terminal->window,
675 pixel_width, pixel_height);
676 }
677
678 window_schedule_redraw (terminal->window);
679
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000680 data_pitch = width * sizeof(union utf8_char);
681 size = data_pitch * height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500682 data = malloc(size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000683 attr_pitch = width * sizeof(struct attr);
684 data_attr = malloc(attr_pitch * height);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000685 tab_ruler = malloc(width);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500686 memset(data, 0, size);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000687 memset(tab_ruler, 0, width);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000688 attr_init(data_attr, terminal->curr_attr, width * height);
689 if (terminal->data && terminal->data_attr) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500690 if (width > terminal->width)
691 l = terminal->width;
692 else
693 l = width;
694
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500695 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500696 total_rows = height;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500697 start = terminal->height - height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500698 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500699 total_rows = terminal->height;
700 start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500701 }
702
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000703 for (i = 0; i < total_rows; i++) {
704 memcpy(&data[width * i],
705 terminal_get_row(terminal, i),
706 l * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000707 memcpy(&data_attr[width * i],
708 terminal_get_attr_row(terminal, i),
709 l * sizeof(struct attr));
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000710 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500711
712 free(terminal->data);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000713 free(terminal->data_attr);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000714 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500715 }
716
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000717 terminal->data_pitch = data_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000718 terminal->attr_pitch = attr_pitch;
Callum Lowcay86653ed2011-01-07 19:47:03 +0000719 terminal->margin_bottom =
720 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500721 terminal->width = width;
722 terminal->height = height;
723 terminal->data = data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000724 terminal->data_attr = data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000725 terminal->tab_ruler = tab_ruler;
726 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500727
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000728 /* Update the window size */
729 ws.ws_row = terminal->height;
730 ws.ws_col = terminal->width;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500731 window_get_child_allocation(terminal->window, &allocation);
732 ws.ws_xpixel = allocation.width;
733 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000734 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500735}
736
Callum Lowcay30eeae52011-01-07 19:46:55 +0000737struct color_scheme DEFAULT_COLORS = {
738 {
739 {0, 0, 0, 1}, /* black */
740 {0.66, 0, 0, 1}, /* red */
741 {0 , 0.66, 0, 1}, /* green */
742 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
743 {0 , 0 , 0.66, 1}, /* blue */
744 {0.66, 0 , 0.66, 1}, /* magenta */
745 {0, 0.66, 0.66, 1}, /* cyan */
746 {0.66, 0.66, 0.66, 1}, /* light grey */
747 {0.22, 0.33, 0.33, 1}, /* dark grey */
748 {1, 0.33, 0.33, 1}, /* high red */
749 {0.33, 1, 0.33, 1}, /* high green */
750 {1, 1, 0.33, 1}, /* high yellow */
751 {0.33, 0.33, 1, 1}, /* high blue */
752 {1, 0.33, 1, 1}, /* high magenta */
753 {0.33, 1, 1, 1}, /* high cyan */
754 {1, 1, 1, 1} /* white */
755 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500756 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000757 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
758};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400759
Kristian Høgsberg22106762008-12-08 13:50:07 -0500760static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500761terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
762{
763 cairo_set_source_rgba(cr,
764 terminal->color_table[index].r,
765 terminal->color_table[index].g,
766 terminal->color_table[index].b,
767 terminal->color_table[index].a);
768}
769
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500770struct glyph_run {
771 struct terminal *terminal;
772 cairo_t *cr;
773 int count;
774 union decoded_attr attr;
775 cairo_glyph_t glyphs[256], *g;
776};
777
778static void
779glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
780{
781 run->terminal = terminal;
782 run->cr = cr;
783 run->g = run->glyphs;
784 run->count = 0;
785 run->attr.key = 0;
786}
787
788static void
789glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
790{
791 cairo_scaled_font_t *font;
792
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500793 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
794 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300795 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500796 font = run->terminal->font_bold;
797 else
798 font = run->terminal->font_normal;
799 cairo_set_scaled_font(run->cr, font);
800 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300801 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500802
Callum Lowcay9d708b02011-01-12 20:06:17 +1300803 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
804 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500805 run->g = run->glyphs;
806 run->count = 0;
807 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300808 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500809}
810
811static void
812glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
813{
814 int num_glyphs;
815 cairo_scaled_font_t *font;
816
817 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
818
Callum Lowcay9d708b02011-01-12 20:06:17 +1300819 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500820 font = run->terminal->font_bold;
821 else
822 font = run->terminal->font_normal;
823
824 cairo_move_to(run->cr, x, y);
825 cairo_scaled_font_text_to_glyphs (font, x, y,
826 (char *) c->byte, 4,
827 &run->g, &num_glyphs,
828 NULL, NULL, NULL);
829 run->g += num_glyphs;
830 run->count += num_glyphs;
831}
832
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500833static void
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500834terminal_draw_contents(struct terminal *terminal)
835{
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500836 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500837 cairo_t *cr;
838 cairo_font_extents_t extents;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000839 int top_margin, side_margin;
840 int row, col;
841 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500842 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000843 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500844 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500845 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500846 struct glyph_run run;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500847
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500848 window_get_child_allocation(terminal->window, &allocation);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000849
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500850 surface = display_create_surface(terminal->display, &allocation);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500851 cr = cairo_create(surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500852 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500853 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500854 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500855
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500856 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500857
858 cairo_font_extents(cr, &extents);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500859 side_margin = (allocation.width - terminal->width * extents.max_x_advance) / 2;
860 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500861
Callum Lowcay30eeae52011-01-07 19:46:55 +0000862 cairo_set_line_width(cr, 1.0);
863
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500864 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000865 for (row = 0; row < terminal->height; row++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000866 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000867 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500868 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000869
Callum Lowcay9d708b02011-01-12 20:06:17 +1300870 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -0500871 continue;
872
Callum Lowcay9d708b02011-01-12 20:06:17 +1300873 terminal_set_color(terminal, cr, attr.attr.bg);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000874 cairo_move_to(cr, side_margin + (col * extents.max_x_advance),
875 top_margin + (row * extents.height));
876 cairo_rel_line_to(cr, extents.max_x_advance, 0);
877 cairo_rel_line_to(cr, 0, extents.height);
878 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
879 cairo_close_path(cr);
880 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500881 }
882 }
Callum Lowcay30eeae52011-01-07 19:46:55 +0000883
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500884 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
885
886 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500887 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500888 for (row = 0; row < terminal->height; row++) {
889 p_row = terminal_get_row(terminal, row);
890 for (col = 0; col < terminal->width; col++) {
891 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500892 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500893
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500894 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000895
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000896 text_x = side_margin + col * extents.max_x_advance;
897 text_y = top_margin + extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +1300898 if (attr.attr.a & ATTRMASK_UNDERLINE) {
899 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000900 cairo_move_to(cr, text_x, (double)text_y + 1.5);
901 cairo_line_to(cr, text_x + extents.max_x_advance, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000902 cairo_stroke(cr);
903 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -0500904
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500905 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000906 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500907 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500908
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500909 attr.key = ~0;
910 glyph_run_flush(&run, attr);
911
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000912 if ((terminal->mode & MODE_SHOW_CURSOR) && !terminal->focused) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000913 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500914
Callum Lowcay30eeae52011-01-07 19:46:55 +0000915 cairo_set_line_width(cr, 1);
916 cairo_move_to(cr, side_margin + terminal->column * extents.max_x_advance + d,
917 top_margin + terminal->row * extents.height + d);
918 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
919 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
920 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
921 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500922
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500923 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000924 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500925
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500926 cairo_destroy(cr);
927
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500928 window_copy_surface(terminal->window, &allocation, surface);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500929
930 cairo_surface_destroy(surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500931}
932
933static void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500934resize_handler(struct window *window,
935 int32_t pixel_width, int32_t pixel_height, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500936{
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500937 struct terminal *terminal = data;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500938 int32_t width, height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500939
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500940 width = (pixel_width - 2 * terminal->margin) /
Kristian Høgsberg09531622010-06-14 23:22:15 -0400941 (int32_t) terminal->extents.max_x_advance;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500942 height = (pixel_height - 2 * terminal->margin) /
Kristian Høgsberg09531622010-06-14 23:22:15 -0400943 (int32_t) terminal->extents.height;
Tiago Vignatti5fd89d22011-01-10 19:30:04 +0200944
Kristian Høgsberg22106762008-12-08 13:50:07 -0500945 terminal_resize(terminal, width, height);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500946}
Kristian Høgsberg22106762008-12-08 13:50:07 -0500947
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500948static void
949terminal_draw(struct terminal *terminal)
950{
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500951 window_draw(terminal->window);
952 terminal_draw_contents(terminal);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400953 window_flush(terminal->window);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500954}
955
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400956static void
957redraw_handler(struct window *window, void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500958{
959 struct terminal *terminal = data;
960
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500961 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500962}
963
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500964static void
965terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500966
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500967static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +0000968handle_char(struct terminal *terminal, union utf8_char utf8);
969
970static void
Callum Lowcay30eeae52011-01-07 19:46:55 +0000971handle_sgr(struct terminal *terminal, int code);
972
973static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000974handle_term_parameter(struct terminal *terminal, int code, int sr)
975{
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000976 int i;
977
Callum Lowcay67a201d2011-01-12 19:23:41 +1300978 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000979 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +0000980 case 1: /* DECCKM */
981 if (sr) terminal->key_mode = KM_APPLICATION;
982 else terminal->key_mode = KM_NORMAL;
983 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000984 case 2: /* DECANM */
985 /* No VT52 support yet */
986 terminal->g0 = CS_US;
987 terminal->g1 = CS_US;
988 terminal->cs = terminal->g0;
989 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000990 case 3: /* DECCOLM */
991 if (sr)
992 terminal_resize(terminal, 132, 24);
993 else
994 terminal_resize(terminal, 80, 24);
995
996 /* set columns, but also home cursor and clear screen */
997 terminal->row = 0; terminal->column = 0;
998 for (i = 0; i < terminal->height; i++) {
999 memset(terminal_get_row(terminal, i),
1000 0, terminal->data_pitch);
1001 attr_init(terminal_get_attr_row(terminal, i),
1002 terminal->curr_attr, terminal->width);
1003 }
1004 break;
1005 case 5: /* DECSCNM */
1006 if (sr) terminal->mode |= MODE_INVERSE;
1007 else terminal->mode &= ~MODE_INVERSE;
1008 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001009 case 6: /* DECOM */
1010 terminal->origin_mode = sr;
1011 if (terminal->origin_mode)
1012 terminal->row = terminal->margin_top;
1013 else
1014 terminal->row = 0;
1015 terminal->column = 0;
1016 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001017 case 7: /* DECAWM */
1018 if (sr) terminal->mode |= MODE_AUTOWRAP;
1019 else terminal->mode &= ~MODE_AUTOWRAP;
1020 break;
1021 case 8: /* DECARM */
1022 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1023 else terminal->mode &= ~MODE_AUTOREPEAT;
1024 break;
1025 case 25:
1026 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1027 else terminal->mode &= ~MODE_SHOW_CURSOR;
1028 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001029 case 1037: /* deleteSendsDel */
1030 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1031 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1032 break;
1033 case 1039: /* altSendsEscape */
1034 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1035 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1036 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001037 default:
1038 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1039 break;
1040 }
1041 } else {
1042 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001043 case 4: /* IRM */
1044 if (sr) terminal->mode |= MODE_IRM;
1045 else terminal->mode &= ~MODE_IRM;
1046 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001047 case 20: /* LNM */
1048 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1049 else terminal->mode &= ~MODE_LF_NEWLINE;
1050 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001051 default:
1052 fprintf(stderr, "Unknown parameter: %d\n", code);
1053 break;
1054 }
1055 }
1056}
1057
1058static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001059handle_dcs(struct terminal *terminal)
1060{
1061}
1062
1063static void
1064handle_osc(struct terminal *terminal)
1065{
1066}
1067
1068static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001069handle_escape(struct terminal *terminal)
1070{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001071 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001072 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001073 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001074 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001075 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001076 char response[MAX_RESPONSE] = {0, };
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001077
1078 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001079 i = 0;
1080 p = &terminal->escape[2];
1081 while ((isdigit(*p) || *p == ';') && i < 10) {
1082 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001083 if (!set[i]) {
1084 args[i] = 0;
1085 set[i] = 1;
1086 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001087 p++;
1088 i++;
1089 } else {
1090 args[i] = strtol(p, &p, 10);
1091 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001092 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001093 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001094
1095 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001096 case '@': /* ICH */
1097 count = set[0] ? args[0] : 1;
1098 if (count == 0) count = 1;
1099 terminal_shift_line(terminal, count);
1100 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001101 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001102 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001103 if (count == 0) count = 1;
1104 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001105 terminal->row -= count;
1106 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001107 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001108 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001109 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001110 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001111 if (count == 0) count = 1;
1112 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001113 terminal->row += count;
1114 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001115 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001116 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001117 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001118 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001119 if (count == 0) count = 1;
1120 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001121 terminal->column += count;
1122 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001123 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001124 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001125 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001126 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001127 if (count == 0) count = 1;
1128 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001129 terminal->column -= count;
1130 else
1131 terminal->column = 0;
1132 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001133 case 'E': /* CNL */
1134 count = set[0] ? args[0] : 1;
1135 if (terminal->row + count <= terminal->margin_bottom)
1136 terminal->row += count;
1137 else
1138 terminal->row = terminal->margin_bottom;
1139 terminal->column = 0;
1140 break;
1141 case 'F': /* CPL */
1142 count = set[0] ? args[0] : 1;
1143 if (terminal->row - count >= terminal->margin_top)
1144 terminal->row -= count;
1145 else
1146 terminal->row = terminal->margin_top;
1147 terminal->column = 0;
1148 break;
1149 case 'G': /* CHA */
1150 y = set[0] ? args[0] : 1;
1151 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1152
1153 terminal->column = y - 1;
1154 break;
1155 case 'f': /* HVP */
1156 case 'H': /* CUP */
1157 x = (set[1] ? args[1] : 1) - 1;
1158 x = x < 0 ? 0 :
1159 (x >= terminal->width ? terminal->width - 1 : x);
1160
1161 y = (set[0] ? args[0] : 1) - 1;
1162 if (terminal->origin_mode) {
1163 y += terminal->margin_top;
1164 y = y < terminal->margin_top ? terminal->margin_top :
1165 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1166 } else {
1167 y = y < 0 ? 0 :
1168 (y >= terminal->height ? terminal->height - 1 : y);
1169 }
1170
1171 terminal->row = y;
1172 terminal->column = x;
1173 break;
1174 case 'I': /* CHT */
1175 count = set[0] ? args[0] : 1;
1176 if (count == 0) count = 1;
1177 while (count > 0 && terminal->column < terminal->width) {
1178 if (terminal->tab_ruler[terminal->column]) count--;
1179 terminal->column++;
1180 }
1181 terminal->column--;
1182 break;
1183 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001184 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001185 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001186 if (!set[0] || args[0] == 0 || args[0] > 2) {
1187 memset(&row[terminal->column],
1188 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1189 attr_init(&attr_row[terminal->column],
1190 terminal->curr_attr, terminal->width - terminal->column);
1191 for (i = terminal->row + 1; i < terminal->height; i++) {
1192 memset(terminal_get_row(terminal, i),
1193 0, terminal->data_pitch);
1194 attr_init(terminal_get_attr_row(terminal, i),
1195 terminal->curr_attr, terminal->width);
1196 }
1197 } else if (args[0] == 1) {
1198 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1199 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1200 for (i = 0; i < terminal->row; i++) {
1201 memset(terminal_get_row(terminal, i),
1202 0, terminal->data_pitch);
1203 attr_init(terminal_get_attr_row(terminal, i),
1204 terminal->curr_attr, terminal->width);
1205 }
1206 } else if (args[0] == 2) {
1207 for (i = 0; i < terminal->height; i++) {
1208 memset(terminal_get_row(terminal, i),
1209 0, terminal->data_pitch);
1210 attr_init(terminal_get_attr_row(terminal, i),
1211 terminal->curr_attr, terminal->width);
1212 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001213 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001214 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001215 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001216 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001217 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001218 if (!set[0] || args[0] == 0 || args[0] > 2) {
1219 memset(&row[terminal->column], 0,
1220 (terminal->width - terminal->column) * sizeof(union utf8_char));
1221 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1222 terminal->width - terminal->column);
1223 } else if (args[0] == 1) {
1224 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1225 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1226 } else if (args[0] == 2) {
1227 memset(row, 0, terminal->data_pitch);
1228 attr_init(attr_row, terminal->curr_attr, terminal->width);
1229 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001230 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001231 case 'L': /* IL */
1232 count = set[0] ? args[0] : 1;
1233 if (count == 0) count = 1;
1234 if (terminal->row >= terminal->margin_top &&
1235 terminal->row < terminal->margin_bottom)
1236 {
1237 top = terminal->margin_top;
1238 terminal->margin_top = terminal->row;
1239 terminal_scroll(terminal, 0 - count);
1240 terminal->margin_top = top;
1241 } else if (terminal->row == terminal->margin_bottom) {
1242 memset(terminal_get_row(terminal, terminal->row),
1243 0, terminal->data_pitch);
1244 attr_init(terminal_get_attr_row(terminal, terminal->row),
1245 terminal->curr_attr, terminal->width);
1246 }
1247 break;
1248 case 'M': /* DL */
1249 count = set[0] ? args[0] : 1;
1250 if (count == 0) count = 1;
1251 if (terminal->row >= terminal->margin_top &&
1252 terminal->row < terminal->margin_bottom)
1253 {
1254 top = terminal->margin_top;
1255 terminal->margin_top = terminal->row;
1256 terminal_scroll(terminal, count);
1257 terminal->margin_top = top;
1258 } else if (terminal->row == terminal->margin_bottom) {
1259 memset(terminal_get_row(terminal, terminal->row),
1260 0, terminal->data_pitch);
1261 }
1262 break;
1263 case 'P': /* DCH */
1264 count = set[0] ? args[0] : 1;
1265 if (count == 0) count = 1;
1266 terminal_shift_line(terminal, 0 - count);
1267 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001268 case 'S': /* SU */
1269 terminal_scroll(terminal, set[0] ? args[0] : 1);
1270 break;
1271 case 'T': /* SD */
1272 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1273 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001274 case 'X': /* ECH */
1275 count = set[0] ? args[0] : 1;
1276 if (count == 0) count = 1;
1277 if ((terminal->column + count) > terminal->width)
1278 count = terminal->width - terminal->column;
1279 row = terminal_get_row(terminal, terminal->row);
1280 attr_row = terminal_get_attr_row(terminal, terminal->row);
1281 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1282 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1283 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001284 case 'Z': /* CBT */
1285 count = set[0] ? args[0] : 1;
1286 if (count == 0) count = 1;
1287 while (count > 0 && terminal->column >= 0) {
1288 if (terminal->tab_ruler[terminal->column]) count--;
1289 terminal->column--;
1290 }
1291 terminal->column++;
1292 break;
1293 case '`': /* HPA */
1294 y = set[0] ? args[0] : 1;
1295 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1296
1297 terminal->column = y - 1;
1298 break;
1299 case 'b': /* REP */
1300 count = set[0] ? args[0] : 1;
1301 if (count == 0) count = 1;
1302 if (terminal->last_char.byte[0])
1303 for (i = 0; i < count; i++)
1304 handle_char(terminal, terminal->last_char);
1305 terminal->last_char.byte[0] = 0;
1306 break;
1307 case 'c': /* Primary DA */
Callum Lowcay69e96582011-01-07 19:47:00 +00001308 write(terminal->master, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001309 break;
1310 case 'd': /* VPA */
1311 x = set[0] ? args[0] : 1;
1312 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1313
1314 terminal->row = x - 1;
1315 break;
1316 case 'g': /* TBC */
1317 if (!set[0] || args[0] == 0) {
1318 terminal->tab_ruler[terminal->column] = 0;
1319 } else if (args[0] == 3) {
1320 memset(terminal->tab_ruler, 0, terminal->width);
1321 }
1322 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001323 case 'h': /* SM */
1324 for(i = 0; i < 10 && set[i]; i++) {
1325 handle_term_parameter(terminal, args[i], 1);
1326 }
1327 break;
1328 case 'l': /* RM */
1329 for(i = 0; i < 10 && set[i]; i++) {
1330 handle_term_parameter(terminal, args[i], 0);
1331 }
1332 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001333 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001334 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001335 if (i <= 7 && set[i] && set[i + 1] &&
1336 set[i + 2] && args[i + 1] == 5)
1337 {
1338 if (args[i] == 38) {
1339 handle_sgr(terminal, args[i + 2] + 256);
1340 break;
1341 } else if (args[i] == 48) {
1342 handle_sgr(terminal, args[i + 2] + 512);
1343 break;
1344 }
1345 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001346 if(set[i]) {
1347 handle_sgr(terminal, args[i]);
1348 } else if(i == 0) {
1349 handle_sgr(terminal, 0);
1350 break;
1351 } else {
1352 break;
1353 }
1354 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001355 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001356 case 'n': /* DSR */
1357 i = set[0] ? args[0] : 0;
1358 if (i == 0 || i == 5) {
1359 write(terminal->master, "\e[0n", 4);
1360 } else if (i == 6) {
1361 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1362 terminal->origin_mode ?
1363 terminal->row+terminal->margin_top : terminal->row+1,
1364 terminal->column+1);
1365 write(terminal->master, response, strlen(response));
1366 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001367 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001368 case 'r':
1369 if(!set[0]) {
1370 terminal->margin_top = 0;
1371 terminal->margin_bottom = terminal->height-1;
1372 terminal->row = 0;
1373 terminal->column = 0;
1374 } else {
1375 top = (set[0] ? args[0] : 1) - 1;
1376 top = top < 0 ? 0 :
1377 (top >= terminal->height ? terminal->height - 1 : top);
1378 bottom = (set[1] ? args[1] : 1) - 1;
1379 bottom = bottom < 0 ? 0 :
1380 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1381 if(bottom > top) {
1382 terminal->margin_top = top;
1383 terminal->margin_bottom = bottom;
1384 } else {
1385 terminal->margin_top = 0;
1386 terminal->margin_bottom = terminal->height-1;
1387 }
1388 if(terminal->origin_mode)
1389 terminal->row = terminal->margin_top;
1390 else
1391 terminal->row = 0;
1392 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001393 }
1394 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001395 case 's':
1396 terminal->saved_row = terminal->row;
1397 terminal->saved_column = terminal->column;
1398 break;
1399 case 'u':
1400 terminal->row = terminal->saved_row;
1401 terminal->column = terminal->saved_column;
1402 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001403 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001404 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001405 break;
1406 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001407}
1408
1409static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001410handle_non_csi_escape(struct terminal *terminal, char code)
1411{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001412 switch(code) {
1413 case 'M': /* RI */
1414 terminal->row -= 1;
1415 if(terminal->row < terminal->margin_top) {
1416 terminal->row = terminal->margin_top;
1417 terminal_scroll(terminal, -1);
1418 }
1419 break;
1420 case 'E': /* NEL */
1421 terminal->column = 0;
1422 // fallthrough
1423 case 'D': /* IND */
1424 terminal->row += 1;
1425 if(terminal->row > terminal->margin_bottom) {
1426 terminal->row = terminal->margin_bottom;
1427 terminal_scroll(terminal, +1);
1428 }
1429 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001430 case 'c': /* RIS */
1431 terminal_init(terminal);
1432 break;
1433 case 'H': /* HTS */
1434 terminal->tab_ruler[terminal->column] = 1;
1435 break;
1436 case '7': /* DECSC */
1437 terminal->saved_row = terminal->row;
1438 terminal->saved_column = terminal->column;
1439 terminal->saved_attr = terminal->curr_attr;
1440 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001441 terminal->saved_cs = terminal->cs;
1442 terminal->saved_g0 = terminal->g0;
1443 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001444 break;
1445 case '8': /* DECRC */
1446 terminal->row = terminal->saved_row;
1447 terminal->column = terminal->saved_column;
1448 terminal->curr_attr = terminal->saved_attr;
1449 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001450 terminal->cs = terminal->saved_cs;
1451 terminal->g0 = terminal->saved_g0;
1452 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001453 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001454 case '=': /* DECPAM */
1455 terminal->key_mode = KM_APPLICATION;
1456 break;
1457 case '>': /* DECPNM */
1458 terminal->key_mode = KM_NORMAL;
1459 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001460 default:
1461 fprintf(stderr, "Unknown escape code: %c\n", code);
1462 break;
1463 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001464}
1465
1466static void
1467handle_special_escape(struct terminal *terminal, char special, char code)
1468{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001469 int i, numChars;
1470
1471 if (special == '#') {
1472 switch(code) {
1473 case '8':
1474 /* fill with 'E', no cheap way to do this */
1475 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1476 numChars = terminal->width * terminal->height;
1477 for(i = 0; i < numChars; i++) {
1478 terminal->data[i].byte[0] = 'E';
1479 }
1480 break;
1481 default:
1482 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1483 break;
1484 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001485 } else if (special == '(' || special == ')') {
1486 switch(code) {
1487 case '0':
1488 if (special == '(')
1489 terminal->g0 = CS_SPECIAL;
1490 else
1491 terminal->g1 = CS_SPECIAL;
1492 break;
1493 case 'A':
1494 if (special == '(')
1495 terminal->g0 = CS_UK;
1496 else
1497 terminal->g1 = CS_UK;
1498 break;
1499 case 'B':
1500 if (special == '(')
1501 terminal->g0 = CS_US;
1502 else
1503 terminal->g1 = CS_US;
1504 break;
1505 default:
1506 fprintf(stderr, "Unknown character set %c\n", code);
1507 break;
1508 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001509 } else {
1510 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1511 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001512}
1513
1514static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001515handle_sgr(struct terminal *terminal, int code)
1516{
1517 switch(code) {
1518 case 0:
1519 terminal->curr_attr = terminal->color_scheme->default_attr;
1520 break;
1521 case 1:
1522 terminal->curr_attr.a |= ATTRMASK_BOLD;
1523 if (terminal->curr_attr.fg < 8)
1524 terminal->curr_attr.fg += 8;
1525 break;
1526 case 4:
1527 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1528 break;
1529 case 5:
1530 terminal->curr_attr.a |= ATTRMASK_BLINK;
1531 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001532 case 8:
1533 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1534 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001535 case 2:
1536 case 21:
1537 case 22:
1538 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1539 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1540 terminal->curr_attr.fg -= 8;
1541 break;
1542 case 24:
1543 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1544 break;
1545 case 25:
1546 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1547 break;
1548 case 7:
1549 case 26:
1550 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1551 break;
1552 case 27:
1553 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1554 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001555 case 28:
1556 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1557 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001558 case 39:
1559 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1560 break;
1561 case 49:
1562 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1563 break;
1564 default:
1565 if(code >= 30 && code <= 37) {
1566 terminal->curr_attr.fg = code - 30;
1567 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1568 terminal->curr_attr.fg += 8;
1569 } else if(code >= 40 && code <= 47) {
1570 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001571 } else if (code >= 90 && code <= 97) {
1572 terminal->curr_attr.fg = code - 90 + 8;
1573 } else if (code >= 100 && code <= 107) {
1574 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001575 } else if(code >= 256 && code < 512) {
1576 terminal->curr_attr.fg = code - 256;
1577 } else if(code >= 512 && code < 768) {
1578 terminal->curr_attr.bg = code - 512;
1579 } else {
1580 fprintf(stderr, "Unknown SGR code: %d\n", code);
1581 }
1582 break;
1583 }
1584}
1585
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001586/* Returns 1 if c was special, otherwise 0 */
1587static int
1588handle_special_char(struct terminal *terminal, char c)
1589{
1590 union utf8_char *row;
1591 struct attr *attr_row;
1592
1593 row = terminal_get_row(terminal, terminal->row);
1594 attr_row = terminal_get_attr_row(terminal, terminal->row);
1595
1596 switch(c) {
1597 case '\r':
1598 terminal->column = 0;
1599 break;
1600 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001601 if (terminal->mode & MODE_LF_NEWLINE) {
1602 terminal->column = 0;
1603 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001604 /* fallthrough */
1605 case '\v':
1606 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001607 terminal->row++;
1608 if(terminal->row > terminal->margin_bottom) {
1609 terminal->row = terminal->margin_bottom;
1610 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001611 }
1612
1613 break;
1614 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001615 while (terminal->column < terminal->width) {
1616 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001617 if (terminal->mode & MODE_IRM)
1618 terminal_shift_line(terminal, +1);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001619 row[terminal->column].byte[0] = ' ';
1620 row[terminal->column].byte[1] = '\0';
1621 attr_row[terminal->column] = terminal->curr_attr;
1622 terminal->column++;
1623 }
1624 if (terminal->column >= terminal->width) {
1625 terminal->column = terminal->width - 1;
1626 }
1627
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001628 break;
1629 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001630 if (terminal->column >= terminal->width) {
1631 terminal->column = terminal->width - 2;
1632 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001633 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001634 } else if (terminal->mode & MODE_AUTOWRAP) {
1635 terminal->column = terminal->width - 1;
1636 terminal->row -= 1;
1637 if (terminal->row < terminal->margin_top) {
1638 terminal->row = terminal->margin_top;
1639 terminal_scroll(terminal, -1);
1640 }
1641 }
1642
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001643 break;
1644 case '\a':
1645 /* Bell */
1646 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001647 case '\x0E': /* SO */
1648 terminal->cs = terminal->g1;
1649 break;
1650 case '\x0F': /* SI */
1651 terminal->cs = terminal->g0;
1652 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001653 default:
1654 return 0;
1655 }
1656
1657 return 1;
1658}
1659
1660static void
1661handle_char(struct terminal *terminal, union utf8_char utf8)
1662{
1663 union utf8_char *row;
1664 struct attr *attr_row;
1665
1666 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001667
1668 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001669
1670 /* There are a whole lot of non-characters, control codes,
1671 * and formatting codes that should probably be ignored,
1672 * for example: */
1673 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1674 /* BOM, ignore */
1675 return;
1676 }
1677
1678 /* Some of these non-characters should be translated, e.g.: */
1679 if (utf8.byte[0] < 32) {
1680 utf8.byte[0] = utf8.byte[0] + 64;
1681 }
1682
1683 /* handle right margin effects */
1684 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001685 if (terminal->mode & MODE_AUTOWRAP) {
1686 terminal->column = 0;
1687 terminal->row += 1;
1688 if (terminal->row > terminal->margin_bottom) {
1689 terminal->row = terminal->margin_bottom;
1690 terminal_scroll(terminal, +1);
1691 }
1692 } else {
1693 terminal->column--;
1694 }
1695 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001696
1697 row = terminal_get_row(terminal, terminal->row);
1698 attr_row = terminal_get_attr_row(terminal, terminal->row);
1699
Callum Lowcay69e96582011-01-07 19:47:00 +00001700 if (terminal->mode & MODE_IRM)
1701 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001702 row[terminal->column] = utf8;
1703 attr_row[terminal->column++] = terminal->curr_attr;
1704
1705 if (utf8.ch != terminal->last_char.ch)
1706 terminal->last_char = utf8;
1707}
1708
Callum Lowcay30eeae52011-01-07 19:46:55 +00001709static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001710escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1711{
1712 int len, i;
1713
1714 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1715 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1716 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1717 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1718 else len = 1; /* Invalid, cannot happen */
1719
1720 if (terminal->escape_length + len <= MAX_ESCAPE) {
1721 for (i = 0; i < len; i++)
1722 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1723 terminal->escape_length += len;
1724 } else if (terminal->escape_length < MAX_ESCAPE) {
1725 terminal->escape[terminal->escape_length++] = 0;
1726 }
1727}
1728
1729static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001730terminal_data(struct terminal *terminal, const char *data, size_t length)
1731{
1732 int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001733 union utf8_char utf8;
1734 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001735
1736 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001737 parser_state =
1738 utf8_next_char(&terminal->state_machine, data[i]);
1739 switch(parser_state) {
1740 case utf8state_accept:
1741 utf8.ch = terminal->state_machine.s.ch;
1742 break;
1743 case utf8state_reject:
1744 /* the unicode replacement character */
1745 utf8.byte[0] = 0xEF;
1746 utf8.byte[1] = 0xBF;
1747 utf8.byte[2] = 0xBD;
1748 utf8.byte[3] = 0x00;
1749 break;
1750 default:
1751 continue;
1752 }
1753
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001754 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001755 switch (terminal->state) {
1756 case escape_state_escape:
1757 escape_append_utf8(terminal, utf8);
1758 switch (utf8.byte[0]) {
1759 case 'P': /* DCS */
1760 terminal->state = escape_state_dcs;
1761 break;
1762 case '[': /* CSI */
1763 terminal->state = escape_state_csi;
1764 break;
1765 case ']': /* OSC */
1766 terminal->state = escape_state_osc;
1767 break;
1768 case '#':
1769 case '(':
1770 case ')': /* special */
1771 terminal->state = escape_state_special;
1772 break;
1773 case '^': /* PM (not implemented) */
1774 case '_': /* APC (not implemented) */
1775 terminal->state = escape_state_ignore;
1776 break;
1777 default:
1778 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001779 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13001780 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001781 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001782 continue;
1783 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001784 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1785 /* do nothing */
1786 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001787 terminal->escape_flags |= ESC_FLAG_WHAT;
1788 } else if (utf8.byte[0] == '>') {
1789 terminal->escape_flags |= ESC_FLAG_GT;
1790 } else if (utf8.byte[0] == '!') {
1791 terminal->escape_flags |= ESC_FLAG_BANG;
1792 } else if (utf8.byte[0] == '$') {
1793 terminal->escape_flags |= ESC_FLAG_CASH;
1794 } else if (utf8.byte[0] == '\'') {
1795 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1796 } else if (utf8.byte[0] == '"') {
1797 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1798 } else if (utf8.byte[0] == ' ') {
1799 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001800 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001801 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001802 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13001803 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001804 }
1805
1806 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1807 utf8.byte[0] == '`')
1808 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001809 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001810 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001811 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001812 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001813 continue;
1814 case escape_state_inner_escape:
1815 if (utf8.byte[0] == '\\') {
1816 terminal->state = escape_state_normal;
1817 if (terminal->outer_state == escape_state_dcs) {
1818 handle_dcs(terminal);
1819 } else if (terminal->outer_state == escape_state_osc) {
1820 handle_osc(terminal);
1821 }
1822 } else if (utf8.byte[0] == '\e') {
1823 terminal->state = terminal->outer_state;
1824 escape_append_utf8(terminal, utf8);
1825 if (terminal->escape_length >= MAX_ESCAPE)
1826 terminal->state = escape_state_normal;
1827 } else {
1828 terminal->state = terminal->outer_state;
1829 if (terminal->escape_length < MAX_ESCAPE)
1830 terminal->escape[terminal->escape_length++] = '\e';
1831 escape_append_utf8(terminal, utf8);
1832 if (terminal->escape_length >= MAX_ESCAPE)
1833 terminal->state = escape_state_normal;
1834 }
1835 continue;
1836 case escape_state_dcs:
1837 case escape_state_osc:
1838 case escape_state_ignore:
1839 if (utf8.byte[0] == '\e') {
1840 terminal->outer_state = terminal->state;
1841 terminal->state = escape_state_inner_escape;
1842 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
1843 terminal->state = escape_state_normal;
1844 handle_osc(terminal);
1845 } else {
1846 escape_append_utf8(terminal, utf8);
1847 if (terminal->escape_length >= MAX_ESCAPE)
1848 terminal->state = escape_state_normal;
1849 }
1850 continue;
1851 case escape_state_special:
1852 escape_append_utf8(terminal, utf8);
1853 terminal->state = escape_state_normal;
1854 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
1855 handle_special_escape(terminal, terminal->escape[1],
1856 utf8.byte[0]);
1857 }
1858 continue;
1859 default:
1860 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001861 }
1862
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001863 /* this is valid, because ASCII characters are never used to
1864 * introduce a multibyte sequence in UTF-8 */
1865 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001866 terminal->state = escape_state_escape;
1867 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001868 terminal->escape[0] = '\e';
1869 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13001870 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001871 } else {
1872 handle_char(terminal, utf8);
1873 } /* if */
1874 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05001875
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001876 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001877}
1878
1879static void
Kristian Høgsberg58eec362011-01-19 14:27:42 -05001880selection_listener_send(void *data, struct wl_selection *selection,
1881 const char *mime_type, int fd)
1882{
1883 struct terminal *terminal = data;
1884 static const char msg[] = "selection data";
1885
1886 fprintf(stderr, "selection send, fd is %d\n", fd);
1887 write(fd, msg, sizeof msg);
1888 close(fd);
1889}
1890
1891static void
1892selection_listener_cancelled(void *data, struct wl_selection *selection)
1893{
1894 struct terminal *terminal = data;
1895
1896 fprintf(stderr, "selection cancelled\n");
1897 wl_selection_destroy(selection);
1898}
1899
1900static const struct wl_selection_listener selection_listener = {
1901 selection_listener_send,
1902 selection_listener_cancelled
1903};
1904
1905static gboolean
1906selection_io_func(GIOChannel *source, GIOCondition condition, gpointer data)
1907{
1908 struct terminal *terminal = data;
1909 char buffer[256];
1910 unsigned int len;
1911 int fd;
1912
1913 fd = g_io_channel_unix_get_fd(source);
1914 len = read(fd, buffer, sizeof buffer);
1915 fprintf(stderr, "read %d bytes: %.*s\n", len, len, buffer);
1916
1917 close(fd);
1918 g_source_remove(terminal->tag);
1919
1920 g_io_channel_unref(source);
1921
1922 return TRUE;
1923}
1924
1925static int
1926handle_bound_key(struct terminal *terminal,
1927 struct input *input, uint32_t sym, uint32_t time)
1928{
1929 struct wl_shell *shell;
1930 GIOChannel *channel;
1931 int fd;
1932
1933 switch (sym) {
1934 case XK_C:
1935 shell = display_get_shell(terminal->display);
1936 terminal->selection = wl_shell_create_selection(shell);
1937 wl_selection_add_listener(terminal->selection,
1938 &selection_listener, terminal);
1939 wl_selection_offer(terminal->selection, "text/plain");
1940 wl_selection_activate(terminal->selection,
1941 input_get_input_device(input), time);
1942
1943 return 1;
1944 case XK_V:
1945 if (input_offers_mime_type(input, "text/plain")) {
1946 fd = input_receive_mime_type(input, "text/plain");
1947 channel = g_io_channel_unix_new(fd);
1948 terminal->tag = g_io_add_watch(channel, G_IO_IN,
1949 selection_io_func,
1950 terminal);
1951 }
1952
1953 return 1;
1954 case XK_X:
1955 /* cut selection; terminal doesn't do cut */
1956 return 0;
1957 default:
1958 return 0;
1959 }
1960}
1961
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001962static void
1963key_handler(struct window *window, struct input *input, uint32_t time,
1964 uint32_t key, uint32_t sym, uint32_t state, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001965{
1966 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001967 char ch[MAX_RESPONSE];
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001968 uint32_t modifiers;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001969 int len = 0;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001970
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001971 modifiers = input_get_modifiers(input);
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05001972 if ((modifiers & XKB_COMMON_CONTROL_MASK) &&
1973 (modifiers & XKB_COMMON_SHIFT_MASK) &&
Kristian Høgsberg67cac8a2011-01-19 14:20:33 -05001974 state && handle_bound_key(terminal, input, sym, 0))
1975 return;
1976
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001977 switch (sym) {
1978 case XK_F11:
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001979 if (!state)
1980 break;
1981 terminal->fullscreen ^= 1;
1982 window_set_fullscreen(window, terminal->fullscreen);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001983 window_schedule_redraw(terminal->window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001984 break;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001985
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001986 case XK_BackSpace:
1987 case XK_Tab:
1988 case XK_Linefeed:
1989 case XK_Clear:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001990 case XK_Pause:
1991 case XK_Scroll_Lock:
1992 case XK_Sys_Req:
1993 case XK_Escape:
1994 ch[len++] = sym & 0x7f;
1995 break;
1996
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001997 case XK_Return:
1998 if (terminal->mode & MODE_LF_NEWLINE) {
1999 ch[len++] = 0x0D;
2000 ch[len++] = 0x0A;
2001 } else {
2002 ch[len++] = 0x0D;
2003 }
2004 break;
2005
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002006 case XK_Shift_L:
2007 case XK_Shift_R:
2008 case XK_Control_L:
2009 case XK_Control_R:
2010 case XK_Alt_L:
2011 case XK_Alt_R:
2012 break;
2013
Callum Lowcay7e08e902011-01-07 19:47:02 +00002014 case XK_Insert:
2015 len = function_key_response('[', 2, modifiers, '~', ch);
2016 break;
2017 case XK_Delete:
2018 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
2019 ch[len++] = '\x04';
2020 } else {
2021 len = function_key_response('[', 3, modifiers, '~', ch);
2022 }
2023 break;
2024 case XK_Page_Up:
2025 len = function_key_response('[', 5, modifiers, '~', ch);
2026 break;
2027 case XK_Page_Down:
2028 len = function_key_response('[', 6, modifiers, '~', ch);
2029 break;
2030 case XK_F1:
2031 len = function_key_response('O', 1, modifiers, 'P', ch);
2032 break;
2033 case XK_F2:
2034 len = function_key_response('O', 1, modifiers, 'Q', ch);
2035 break;
2036 case XK_F3:
2037 len = function_key_response('O', 1, modifiers, 'R', ch);
2038 break;
2039 case XK_F4:
2040 len = function_key_response('O', 1, modifiers, 'S', ch);
2041 break;
2042 case XK_F5:
2043 len = function_key_response('[', 15, modifiers, '~', ch);
2044 break;
2045 case XK_F6:
2046 len = function_key_response('[', 17, modifiers, '~', ch);
2047 break;
2048 case XK_F7:
2049 len = function_key_response('[', 18, modifiers, '~', ch);
2050 break;
2051 case XK_F8:
2052 len = function_key_response('[', 19, modifiers, '~', ch);
2053 break;
2054 case XK_F9:
2055 len = function_key_response('[', 20, modifiers, '~', ch);
2056 break;
2057 case XK_F10:
2058 len = function_key_response('[', 21, modifiers, '~', ch);
2059 break;
2060 case XK_F12:
2061 len = function_key_response('[', 24, modifiers, '~', ch);
2062 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002063 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00002064 /* Handle special keys with alternate mappings */
2065 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
2066 if (len != 0) break;
2067
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002068 if (modifiers & XKB_COMMON_CONTROL_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002069 if (sym >= '3' && sym <= '7')
2070 sym = (sym & 0x1f) + 8;
2071
2072 if (!((sym >= '!' && sym <= '/') ||
2073 (sym >= '8' && sym <= '?') ||
2074 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
2075 else if (sym == '2') sym = 0x00;
2076 else if (sym == '/') sym = 0x1F;
2077 else if (sym == '8' || sym == '?') sym = 0x7F;
2078 } else if ((terminal->mode & MODE_ALT_SENDS_ESC) &&
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002079 (modifiers & XKB_COMMON_MOD1_MASK))
Callum Lowcay7e08e902011-01-07 19:47:02 +00002080 {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002081 ch[len++] = 0x1b;
Kristian Høgsberg23c03ad2011-01-19 14:41:20 -05002082 } else if (modifiers & XKB_COMMON_MOD1_MASK) {
Callum Lowcay7e08e902011-01-07 19:47:02 +00002083 sym = sym | 0x80;
2084 }
2085
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002086 if (sym < 256)
2087 ch[len++] = sym;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002088 break;
2089 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04002090
2091 if (state && len > 0)
2092 write(terminal->master, ch, len);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002093}
2094
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002095static void
2096keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002097 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002098{
2099 struct terminal *terminal = data;
2100
2101 terminal->focused = (device != NULL);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002102 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002103}
2104
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002105static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002106terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002107{
2108 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002109 cairo_surface_t *surface;
2110 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002111
2112 terminal = malloc(sizeof *terminal);
2113 if (terminal == NULL)
2114 return terminal;
2115
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002116 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002117 terminal->fullscreen = fullscreen;
Callum Lowcay30eeae52011-01-07 19:46:55 +00002118 terminal->color_scheme = &DEFAULT_COLORS;
2119 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002120 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002121 terminal->margin_bottom = -1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002122 terminal->window = window_create(display, "Wayland Terminal",
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002123 500, 400);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002124
2125 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002126 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002127
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002128 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002129 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002130
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002131 window_set_fullscreen(terminal->window, terminal->fullscreen);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002132 window_set_user_data(terminal->window, terminal);
2133 window_set_redraw_handler(terminal->window, redraw_handler);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05002134 window_set_resize_handler(terminal->window, resize_handler);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002135
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002136 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002137 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002138 keyboard_focus_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002139
Kristian Høgsberg09531622010-06-14 23:22:15 -04002140 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2141 cr = cairo_create(surface);
Kristian Høgsberg09531622010-06-14 23:22:15 -04002142 cairo_set_font_size(cr, 14);
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002143 cairo_select_font_face (cr, "mono",
2144 CAIRO_FONT_SLANT_NORMAL,
2145 CAIRO_FONT_WEIGHT_BOLD);
2146 terminal->font_bold = cairo_get_scaled_font (cr);
2147 cairo_scaled_font_reference(terminal->font_bold);
2148
2149 cairo_select_font_face (cr, "mono",
2150 CAIRO_FONT_SLANT_NORMAL,
2151 CAIRO_FONT_WEIGHT_NORMAL);
2152 terminal->font_normal = cairo_get_scaled_font (cr);
2153 cairo_scaled_font_reference(terminal->font_normal);
2154
Kristian Høgsberg09531622010-06-14 23:22:15 -04002155 cairo_font_extents(cr, &terminal->extents);
2156 cairo_destroy(cr);
2157 cairo_surface_destroy(surface);
2158
Callum Lowcaya0ee21c2011-01-07 19:46:56 +00002159 terminal_resize(terminal, 80, 24);
Kristian Høgsberg22106762008-12-08 13:50:07 -05002160 terminal_draw(terminal);
2161
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002162 return terminal;
2163}
2164
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002165static gboolean
2166io_handler(GIOChannel *source,
2167 GIOCondition condition,
2168 gpointer data)
2169{
2170 struct terminal *terminal = data;
2171 gchar buffer[256];
2172 gsize bytes_read;
2173 GError *error = NULL;
2174
2175 g_io_channel_read_chars(source, buffer, sizeof buffer,
2176 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002177
2178 terminal_data(terminal, buffer, bytes_read);
2179
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002180 return TRUE;
2181}
2182
2183static int
2184terminal_run(struct terminal *terminal, const char *path)
2185{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002186 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002187 pid_t pid;
2188
2189 pid = forkpty(&master, NULL, NULL, NULL);
2190 if (pid == 0) {
Callum Lowcay86653ed2011-01-07 19:47:03 +00002191 setenv("TERM", "xterm-256color", 1);
2192 setenv("COLORTERM", "xterm-256color", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002193 if (execl(path, path, NULL)) {
2194 printf("exec failed: %m\n");
2195 exit(EXIT_FAILURE);
2196 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002197 } else if (pid < 0) {
2198 fprintf(stderr, "failed to fork and create pty (%m).\n");
2199 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002200 }
2201
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002202 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002203 terminal->channel = g_io_channel_unix_new(master);
2204 fcntl(master, F_SETFL, O_NONBLOCK);
2205 g_io_add_watch(terminal->channel, G_IO_IN,
2206 io_handler, terminal);
2207
2208 return 0;
2209}
2210
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002211static const GOptionEntry option_entries[] = {
2212 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
2213 &option_fullscreen, "Run in fullscreen mode" },
2214 { NULL }
2215};
2216
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002217int main(int argc, char *argv[])
2218{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002219 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002220 struct terminal *terminal;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002221
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002222 d = display_create(&argc, &argv, option_entries);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002223 if (d == NULL) {
2224 fprintf(stderr, "failed to create display: %m\n");
2225 return -1;
2226 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002227
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002228 terminal = terminal_create(d, option_fullscreen);
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002229 if (terminal_run(terminal, "/bin/bash"))
2230 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002231
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002232 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002233
2234 return 0;
2235}