blob: d3d7a0bfad571bf9b1299c692b5ba51e86cca4ae [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
276 if (modifiers & WINDOW_MODIFIER_SHIFT) mod_num |= 1;
277 if (modifiers & WINDOW_MODIFIER_ALT) mod_num |= 2;
278 if (modifiers & WINDOW_MODIFIER_CONTROL) mod_num |= 4;
279
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øgsberg0c4457f2008-12-07 19:59:11 -0500393};
394
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000395/* Create default tab stops, every 8 characters */
396static void
397terminal_init_tabs(struct terminal *terminal)
398{
399 int i = 0;
400
401 while (i < terminal->width) {
402 if (i % 8 == 0)
403 terminal->tab_ruler[i] = 1;
404 else
405 terminal->tab_ruler[i] = 0;
406 i++;
407 }
408}
409
Callum Lowcay30eeae52011-01-07 19:46:55 +0000410static void
411terminal_init(struct terminal *terminal)
412{
413 terminal->curr_attr = terminal->color_scheme->default_attr;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000414 terminal->origin_mode = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000415 terminal->mode = MODE_SHOW_CURSOR |
416 MODE_AUTOREPEAT |
Callum Lowcay7e08e902011-01-07 19:47:02 +0000417 MODE_ALT_SENDS_ESC |
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000418 MODE_AUTOWRAP;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000419
420 terminal->row = 0;
421 terminal->column = 0;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000422
Callum Lowcay256e72f2011-01-07 19:47:01 +0000423 terminal->g0 = CS_US;
424 terminal->g1 = CS_US;
425 terminal->cs = terminal->g0;
Callum Lowcay7e08e902011-01-07 19:47:02 +0000426 terminal->key_mode = KM_NORMAL;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000427
428 terminal->saved_g0 = terminal->g0;
429 terminal->saved_g1 = terminal->g1;
430 terminal->saved_cs = terminal->cs;
431
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000432 terminal->saved_attr = terminal->curr_attr;
433 terminal->saved_origin_mode = terminal->origin_mode;
434 terminal->saved_row = terminal->row;
435 terminal->saved_column = terminal->column;
436
437 if (terminal->tab_ruler != NULL) terminal_init_tabs(terminal);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000438}
439
440static void
441init_color_table(struct terminal *terminal)
442{
443 int c, r;
444 struct terminal_color *color_table = terminal->color_table;
445
446 for (c = 0; c < 256; c ++) {
447 if (c < 16) {
448 color_table[c] = terminal->color_scheme->palette[c];
449 } else if (c < 232) {
450 r = c - 16;
451 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
452 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
453 color_table[c].r = ((double)(r % 6) / 6.0);
454 color_table[c].a = 1.0;
455 } else {
456 r = (c - 232) * 10 + 8;
457 color_table[c].r = ((double) r) / 256.0;
458 color_table[c].g = color_table[c].r;
459 color_table[c].b = color_table[c].r;
460 color_table[c].a = 1.0;
461 }
462 }
463}
464
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000465static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500466terminal_get_row(struct terminal *terminal, int row)
467{
468 int index;
469
470 index = (row + terminal->start) % terminal->height;
471
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000472 return &terminal->data[index * terminal->width];
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500473}
474
Callum Lowcay30eeae52011-01-07 19:46:55 +0000475static struct attr*
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500476terminal_get_attr_row(struct terminal *terminal, int row)
477{
Callum Lowcay30eeae52011-01-07 19:46:55 +0000478 int index;
479
480 index = (row + terminal->start) % terminal->height;
481
482 return &terminal->data_attr[index * terminal->width];
483}
484
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500485union decoded_attr {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300486 struct attr attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500487 uint32_t key;
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500488};
489
490static void
491terminal_decode_attr(struct terminal *terminal, int row, int col,
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500492 union decoded_attr *decoded)
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500493{
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500494 struct attr attr;
495 int foreground, background, tmp;
496
497 /* get the attributes for this character cell */
498 attr = terminal_get_attr_row(terminal, row)[col];
499 if ((attr.a & ATTRMASK_INVERSE) ||
500 ((terminal->mode & MODE_SHOW_CURSOR) &&
501 terminal->focused && terminal->row == row &&
502 terminal->column == col)) {
503 foreground = attr.bg;
504 background = attr.fg;
505 if (attr.a & ATTRMASK_BOLD) {
506 if (foreground <= 16) foreground |= 0x08;
507 if (background <= 16) background &= 0x07;
508 }
509 } else {
510 foreground = attr.fg;
511 background = attr.bg;
512 }
513
514 if (terminal->mode & MODE_INVERSE) {
515 tmp = foreground;
516 foreground = background;
517 background = tmp;
518 if (attr.a & ATTRMASK_BOLD) {
519 if (foreground <= 16) foreground |= 0x08;
520 if (background <= 16) background &= 0x07;
521 }
522 }
523
Callum Lowcay9d708b02011-01-12 20:06:17 +1300524 decoded->attr.fg = foreground;
525 decoded->attr.bg = background;
526 decoded->attr.a = attr.a;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000527}
528
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500529static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000530terminal_scroll_buffer(struct terminal *terminal, int d)
531{
532 int i;
533
534 d = d % (terminal->height + 1);
535 terminal->start = (terminal->start + d) % terminal->height;
536 if (terminal->start < 0) terminal->start = terminal->height + terminal->start;
537 if(d < 0) {
538 d = 0 - d;
539 for(i = 0; i < d; i++) {
540 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
541 attr_init(terminal_get_attr_row(terminal, i),
542 terminal->curr_attr, terminal->width);
543 }
544 } else {
545 for(i = terminal->height - d; i < terminal->height; i++) {
546 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
547 attr_init(terminal_get_attr_row(terminal, i),
548 terminal->curr_attr, terminal->width);
549 }
550 }
551}
552
553static void
554terminal_scroll_window(struct terminal *terminal, int d)
555{
556 int i;
557 int window_height;
558 int from_row, to_row;
Callum Lowcaybbeac602011-01-07 19:46:58 +0000559
560 // scrolling range is inclusive
561 window_height = terminal->margin_bottom - terminal->margin_top + 1;
562 d = d % (window_height + 1);
563 if(d < 0) {
564 d = 0 - d;
565 to_row = terminal->margin_bottom;
566 from_row = terminal->margin_bottom - d;
567
568 for (i = 0; i < (window_height - d); i++) {
569 memcpy(terminal_get_row(terminal, to_row - i),
570 terminal_get_row(terminal, from_row - i),
571 terminal->data_pitch);
572 memcpy(terminal_get_attr_row(terminal, to_row - i),
573 terminal_get_attr_row(terminal, from_row - i),
574 terminal->attr_pitch);
575 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000576 for (i = terminal->margin_top; i < (terminal->margin_top + d); i++) {
577 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000578 attr_init(terminal_get_attr_row(terminal, i),
579 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000580 }
581 } else {
582 to_row = terminal->margin_top;
583 from_row = terminal->margin_top + d;
584
585 for (i = 0; i < (window_height - d); i++) {
586 memcpy(terminal_get_row(terminal, to_row + i),
587 terminal_get_row(terminal, from_row + i),
588 terminal->data_pitch);
589 memcpy(terminal_get_attr_row(terminal, to_row + i),
590 terminal_get_attr_row(terminal, from_row + i),
591 terminal->attr_pitch);
592 }
Callum Lowcaybbeac602011-01-07 19:46:58 +0000593 for (i = terminal->margin_bottom - d + 1; i <= terminal->margin_bottom; i++) {
594 memset(terminal_get_row(terminal, i), 0, terminal->data_pitch);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000595 attr_init(terminal_get_attr_row(terminal, i),
596 terminal->curr_attr, terminal->width);
Callum Lowcaybbeac602011-01-07 19:46:58 +0000597 }
598 }
599}
600
601static void
602terminal_scroll(struct terminal *terminal, int d)
603{
604 if(terminal->margin_top == 0 && terminal->margin_bottom == terminal->height - 1)
605 terminal_scroll_buffer(terminal, d);
606 else
607 terminal_scroll_window(terminal, d);
608}
609
610static void
Callum Lowcay69e96582011-01-07 19:47:00 +0000611terminal_shift_line(struct terminal *terminal, int d)
612{
613 union utf8_char *row;
614 struct attr *attr_row, attr;
615
616 row = terminal_get_row(terminal, terminal->row);
617 attr_row = terminal_get_attr_row(terminal, terminal->row);
618
619 if ((terminal->width + d) <= terminal->column)
620 d = terminal->column + 1 - terminal->width;
621 if ((terminal->column + d) >= terminal->width)
622 d = terminal->width - terminal->column - 1;
623
624 if (d < 0) {
625 d = 0 - d;
626 memmove(&row[terminal->column],
627 &row[terminal->column + d],
628 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
629 attr = attr_row[terminal->width - 1];
630 memmove(&attr_row[terminal->column], &attr_row[terminal->column + d],
631 (terminal->width - terminal->column - d) * sizeof(struct attr));
632 memset(&row[terminal->width - d], 0, d * sizeof(union utf8_char));
633 attr_init(&attr_row[terminal->width - d], terminal->curr_attr, d);
634 } else {
635 memmove(&row[terminal->column + d], &row[terminal->column],
636 (terminal->width - terminal->column - d) * sizeof(union utf8_char));
637 memmove(&attr_row[terminal->column + d], &attr_row[terminal->column],
638 (terminal->width - terminal->column - d) * sizeof(struct attr));
639 memset(&row[terminal->column], 0, d * sizeof(union utf8_char));
640 attr_init(&attr_row[terminal->column], terminal->curr_attr, d);
641 }
642}
643
644static void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500645terminal_resize(struct terminal *terminal, int width, int height)
646{
647 size_t size;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000648 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000649 struct attr *data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000650 char *tab_ruler;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000651 int data_pitch, attr_pitch;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500652 int i, l, total_rows, start;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500653 struct rectangle allocation;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000654 struct winsize ws;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500655 int32_t pixel_width, pixel_height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500656
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500657 if (width < 1)
658 width = 1;
659 if (height < 1)
660 height = 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500661 if (terminal->width == width && terminal->height == height)
662 return;
663
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500664 if (!terminal->fullscreen) {
665 pixel_width = width *
666 terminal->extents.max_x_advance + 2 * terminal->margin;
667 pixel_height = height *
668 terminal->extents.height + 2 * terminal->margin;
669 window_set_child_size(terminal->window,
670 pixel_width, pixel_height);
671 }
672
673 window_schedule_redraw (terminal->window);
674
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000675 data_pitch = width * sizeof(union utf8_char);
676 size = data_pitch * height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500677 data = malloc(size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000678 attr_pitch = width * sizeof(struct attr);
679 data_attr = malloc(attr_pitch * height);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000680 tab_ruler = malloc(width);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500681 memset(data, 0, size);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000682 memset(tab_ruler, 0, width);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000683 attr_init(data_attr, terminal->curr_attr, width * height);
684 if (terminal->data && terminal->data_attr) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500685 if (width > terminal->width)
686 l = terminal->width;
687 else
688 l = width;
689
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500690 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500691 total_rows = height;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500692 start = terminal->height - height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500693 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500694 total_rows = terminal->height;
695 start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500696 }
697
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000698 for (i = 0; i < total_rows; i++) {
699 memcpy(&data[width * i],
700 terminal_get_row(terminal, i),
701 l * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000702 memcpy(&data_attr[width * i],
703 terminal_get_attr_row(terminal, i),
704 l * sizeof(struct attr));
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000705 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500706
707 free(terminal->data);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000708 free(terminal->data_attr);
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000709 free(terminal->tab_ruler);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500710 }
711
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000712 terminal->data_pitch = data_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000713 terminal->attr_pitch = attr_pitch;
Callum Lowcay86653ed2011-01-07 19:47:03 +0000714 terminal->margin_bottom =
715 height - (terminal->height - terminal->margin_bottom);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500716 terminal->width = width;
717 terminal->height = height;
718 terminal->data = data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000719 terminal->data_attr = data_attr;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000720 terminal->tab_ruler = tab_ruler;
721 terminal_init_tabs(terminal);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500722
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000723 /* Update the window size */
724 ws.ws_row = terminal->height;
725 ws.ws_col = terminal->width;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500726 window_get_child_allocation(terminal->window, &allocation);
727 ws.ws_xpixel = allocation.width;
728 ws.ws_ypixel = allocation.height;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000729 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500730}
731
Callum Lowcay30eeae52011-01-07 19:46:55 +0000732struct color_scheme DEFAULT_COLORS = {
733 {
734 {0, 0, 0, 1}, /* black */
735 {0.66, 0, 0, 1}, /* red */
736 {0 , 0.66, 0, 1}, /* green */
737 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
738 {0 , 0 , 0.66, 1}, /* blue */
739 {0.66, 0 , 0.66, 1}, /* magenta */
740 {0, 0.66, 0.66, 1}, /* cyan */
741 {0.66, 0.66, 0.66, 1}, /* light grey */
742 {0.22, 0.33, 0.33, 1}, /* dark grey */
743 {1, 0.33, 0.33, 1}, /* high red */
744 {0.33, 1, 0.33, 1}, /* high green */
745 {1, 1, 0.33, 1}, /* high yellow */
746 {0.33, 0.33, 1, 1}, /* high blue */
747 {1, 0.33, 1, 1}, /* high magenta */
748 {0.33, 1, 1, 1}, /* high cyan */
749 {1, 1, 1, 1} /* white */
750 },
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500751 0, /* black border */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000752 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
753};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400754
Kristian Høgsberg22106762008-12-08 13:50:07 -0500755static void
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500756terminal_set_color(struct terminal *terminal, cairo_t *cr, int index)
757{
758 cairo_set_source_rgba(cr,
759 terminal->color_table[index].r,
760 terminal->color_table[index].g,
761 terminal->color_table[index].b,
762 terminal->color_table[index].a);
763}
764
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500765struct glyph_run {
766 struct terminal *terminal;
767 cairo_t *cr;
768 int count;
769 union decoded_attr attr;
770 cairo_glyph_t glyphs[256], *g;
771};
772
773static void
774glyph_run_init(struct glyph_run *run, struct terminal *terminal, cairo_t *cr)
775{
776 run->terminal = terminal;
777 run->cr = cr;
778 run->g = run->glyphs;
779 run->count = 0;
780 run->attr.key = 0;
781}
782
783static void
784glyph_run_flush(struct glyph_run *run, union decoded_attr attr)
785{
786 cairo_scaled_font_t *font;
787
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500788 if (run->count > ARRAY_LENGTH(run->glyphs) - 10 ||
789 (attr.key != run->attr.key)) {
Callum Lowcay9d708b02011-01-12 20:06:17 +1300790 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500791 font = run->terminal->font_bold;
792 else
793 font = run->terminal->font_normal;
794 cairo_set_scaled_font(run->cr, font);
795 terminal_set_color(run->terminal, run->cr,
Callum Lowcay9d708b02011-01-12 20:06:17 +1300796 run->attr.attr.fg);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500797
Callum Lowcay9d708b02011-01-12 20:06:17 +1300798 if (!(run->attr.attr.a & ATTRMASK_CONCEALED))
799 cairo_show_glyphs (run->cr, run->glyphs, run->count);
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500800 run->g = run->glyphs;
801 run->count = 0;
802 }
Callum Lowcay9d708b02011-01-12 20:06:17 +1300803 run->attr = attr;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500804}
805
806static void
807glyph_run_add(struct glyph_run *run, int x, int y, union utf8_char *c)
808{
809 int num_glyphs;
810 cairo_scaled_font_t *font;
811
812 num_glyphs = ARRAY_LENGTH(run->glyphs) - run->count;
813
Callum Lowcay9d708b02011-01-12 20:06:17 +1300814 if (run->attr.attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK))
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500815 font = run->terminal->font_bold;
816 else
817 font = run->terminal->font_normal;
818
819 cairo_move_to(run->cr, x, y);
820 cairo_scaled_font_text_to_glyphs (font, x, y,
821 (char *) c->byte, 4,
822 &run->g, &num_glyphs,
823 NULL, NULL, NULL);
824 run->g += num_glyphs;
825 run->count += num_glyphs;
826}
827
Kristian Høgsbergf106fd52011-01-11 10:11:39 -0500828static void
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500829terminal_draw_contents(struct terminal *terminal)
830{
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500831 struct rectangle allocation;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500832 cairo_t *cr;
833 cairo_font_extents_t extents;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000834 int top_margin, side_margin;
835 int row, col;
836 union utf8_char *p_row;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500837 union decoded_attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000838 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500839 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500840 double d;
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500841 struct glyph_run run;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500842
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500843 window_get_child_allocation(terminal->window, &allocation);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000844
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500845 surface = display_create_surface(terminal->display, &allocation);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500846 cr = cairo_create(surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500847 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg71eca892011-01-11 10:13:00 -0500848 terminal_set_color(terminal, cr, terminal->color_scheme->border);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500849 cairo_paint(cr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500850
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -0500851 cairo_set_scaled_font(cr, terminal->font_normal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500852
853 cairo_font_extents(cr, &extents);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500854 side_margin = (allocation.width - terminal->width * extents.max_x_advance) / 2;
855 top_margin = (allocation.height - terminal->height * extents.height) / 2;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500856
Callum Lowcay30eeae52011-01-07 19:46:55 +0000857 cairo_set_line_width(cr, 1.0);
858
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500859 /* paint the background */
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000860 for (row = 0; row < terminal->height; row++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000861 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000862 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500863 terminal_decode_attr(terminal, row, col, &attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000864
Callum Lowcay9d708b02011-01-12 20:06:17 +1300865 if (attr.attr.bg == terminal->color_scheme->border)
Kristian Høgsbergfb266a32011-01-11 10:15:21 -0500866 continue;
867
Callum Lowcay9d708b02011-01-12 20:06:17 +1300868 terminal_set_color(terminal, cr, attr.attr.bg);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000869 cairo_move_to(cr, side_margin + (col * extents.max_x_advance),
870 top_margin + (row * extents.height));
871 cairo_rel_line_to(cr, extents.max_x_advance, 0);
872 cairo_rel_line_to(cr, 0, extents.height);
873 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
874 cairo_close_path(cr);
875 cairo_fill(cr);
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500876 }
877 }
Callum Lowcay30eeae52011-01-07 19:46:55 +0000878
Kristian Høgsberg8c254202010-12-25 08:58:46 -0500879 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
880
881 /* paint the foreground */
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500882 glyph_run_init(&run, terminal, cr);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500883 for (row = 0; row < terminal->height; row++) {
884 p_row = terminal_get_row(terminal, row);
885 for (col = 0; col < terminal->width; col++) {
886 /* get the attributes for this character cell */
Kristian Høgsberg01994a52011-01-11 10:26:04 -0500887 terminal_decode_attr(terminal, row, col, &attr);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500888
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500889 glyph_run_flush(&run, attr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000890
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000891 text_x = side_margin + col * extents.max_x_advance;
892 text_y = top_margin + extents.ascent + row * extents.height;
Callum Lowcay9d708b02011-01-12 20:06:17 +1300893 if (attr.attr.a & ATTRMASK_UNDERLINE) {
894 terminal_set_color(terminal, cr, attr.attr.fg);
Callum Lowcay86653ed2011-01-07 19:47:03 +0000895 cairo_move_to(cr, text_x, (double)text_y + 1.5);
896 cairo_line_to(cr, text_x + extents.max_x_advance, (double) text_y + 1.5);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000897 cairo_stroke(cr);
898 }
Kristian Høgsberg4f506702010-12-25 16:14:23 -0500899
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500900 glyph_run_add(&run, text_x, text_y, &p_row[col]);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000901 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500902 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500903
Kristian Høgsberg1d3e9392011-01-11 11:06:49 -0500904 attr.key = ~0;
905 glyph_run_flush(&run, attr);
906
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000907 if ((terminal->mode & MODE_SHOW_CURSOR) && !terminal->focused) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000908 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500909
Callum Lowcay30eeae52011-01-07 19:46:55 +0000910 cairo_set_line_width(cr, 1);
911 cairo_move_to(cr, side_margin + terminal->column * extents.max_x_advance + d,
912 top_margin + terminal->row * extents.height + d);
913 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
914 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
915 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
916 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500917
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500918 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000919 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500920
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500921 cairo_destroy(cr);
922
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500923 window_copy_surface(terminal->window, &allocation, surface);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500924
925 cairo_surface_destroy(surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500926}
927
928static void
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500929resize_handler(struct window *window,
930 int32_t pixel_width, int32_t pixel_height, void *data)
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500931{
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500932 struct terminal *terminal = data;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500933 int32_t width, height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500934
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500935 width = (pixel_width - 2 * terminal->margin) /
Kristian Høgsberg09531622010-06-14 23:22:15 -0400936 (int32_t) terminal->extents.max_x_advance;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500937 height = (pixel_height - 2 * terminal->margin) /
Kristian Høgsberg09531622010-06-14 23:22:15 -0400938 (int32_t) terminal->extents.height;
Tiago Vignatti5fd89d22011-01-10 19:30:04 +0200939
Kristian Høgsberg22106762008-12-08 13:50:07 -0500940 terminal_resize(terminal, width, height);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500941}
Kristian Høgsberg22106762008-12-08 13:50:07 -0500942
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500943static void
944terminal_draw(struct terminal *terminal)
945{
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500946 window_draw(terminal->window);
947 terminal_draw_contents(terminal);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400948 window_flush(terminal->window);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500949}
950
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400951static void
952redraw_handler(struct window *window, void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500953{
954 struct terminal *terminal = data;
955
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500956 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500957}
958
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500959static void
960terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500961
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500962static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +0000963handle_char(struct terminal *terminal, union utf8_char utf8);
964
965static void
Callum Lowcay30eeae52011-01-07 19:46:55 +0000966handle_sgr(struct terminal *terminal, int code);
967
968static void
Callum Lowcaybbeac602011-01-07 19:46:58 +0000969handle_term_parameter(struct terminal *terminal, int code, int sr)
970{
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000971 int i;
972
Callum Lowcay67a201d2011-01-12 19:23:41 +1300973 if (terminal->escape_flags & ESC_FLAG_WHAT) {
Callum Lowcaybbeac602011-01-07 19:46:58 +0000974 switch(code) {
Callum Lowcay7e08e902011-01-07 19:47:02 +0000975 case 1: /* DECCKM */
976 if (sr) terminal->key_mode = KM_APPLICATION;
977 else terminal->key_mode = KM_NORMAL;
978 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +0000979 case 2: /* DECANM */
980 /* No VT52 support yet */
981 terminal->g0 = CS_US;
982 terminal->g1 = CS_US;
983 terminal->cs = terminal->g0;
984 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +0000985 case 3: /* DECCOLM */
986 if (sr)
987 terminal_resize(terminal, 132, 24);
988 else
989 terminal_resize(terminal, 80, 24);
990
991 /* set columns, but also home cursor and clear screen */
992 terminal->row = 0; terminal->column = 0;
993 for (i = 0; i < terminal->height; i++) {
994 memset(terminal_get_row(terminal, i),
995 0, terminal->data_pitch);
996 attr_init(terminal_get_attr_row(terminal, i),
997 terminal->curr_attr, terminal->width);
998 }
999 break;
1000 case 5: /* DECSCNM */
1001 if (sr) terminal->mode |= MODE_INVERSE;
1002 else terminal->mode &= ~MODE_INVERSE;
1003 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001004 case 6: /* DECOM */
1005 terminal->origin_mode = sr;
1006 if (terminal->origin_mode)
1007 terminal->row = terminal->margin_top;
1008 else
1009 terminal->row = 0;
1010 terminal->column = 0;
1011 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001012 case 7: /* DECAWM */
1013 if (sr) terminal->mode |= MODE_AUTOWRAP;
1014 else terminal->mode &= ~MODE_AUTOWRAP;
1015 break;
1016 case 8: /* DECARM */
1017 if (sr) terminal->mode |= MODE_AUTOREPEAT;
1018 else terminal->mode &= ~MODE_AUTOREPEAT;
1019 break;
1020 case 25:
1021 if (sr) terminal->mode |= MODE_SHOW_CURSOR;
1022 else terminal->mode &= ~MODE_SHOW_CURSOR;
1023 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001024 case 1037: /* deleteSendsDel */
1025 if (sr) terminal->mode |= MODE_DELETE_SENDS_DEL;
1026 else terminal->mode &= ~MODE_DELETE_SENDS_DEL;
1027 break;
1028 case 1039: /* altSendsEscape */
1029 if (sr) terminal->mode |= MODE_ALT_SENDS_ESC;
1030 else terminal->mode &= ~MODE_ALT_SENDS_ESC;
1031 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001032 default:
1033 fprintf(stderr, "Unknown parameter: ?%d\n", code);
1034 break;
1035 }
1036 } else {
1037 switch(code) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001038 case 4: /* IRM */
1039 if (sr) terminal->mode |= MODE_IRM;
1040 else terminal->mode &= ~MODE_IRM;
1041 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001042 case 20: /* LNM */
1043 if (sr) terminal->mode |= MODE_LF_NEWLINE;
1044 else terminal->mode &= ~MODE_LF_NEWLINE;
1045 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001046 default:
1047 fprintf(stderr, "Unknown parameter: %d\n", code);
1048 break;
1049 }
1050 }
1051}
1052
1053static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001054handle_dcs(struct terminal *terminal)
1055{
1056}
1057
1058static void
1059handle_osc(struct terminal *terminal)
1060{
1061}
1062
1063static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001064handle_escape(struct terminal *terminal)
1065{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001066 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001067 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001068 char *p;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001069 int i, count, x, y, top, bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001070 int args[10], set[10] = { 0, };
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001071 char response[MAX_RESPONSE] = {0, };
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001072
1073 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001074 i = 0;
1075 p = &terminal->escape[2];
1076 while ((isdigit(*p) || *p == ';') && i < 10) {
1077 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +00001078 if (!set[i]) {
1079 args[i] = 0;
1080 set[i] = 1;
1081 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001082 p++;
1083 i++;
1084 } else {
1085 args[i] = strtol(p, &p, 10);
1086 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001087 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001088 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001089
1090 switch (*p) {
Callum Lowcay69e96582011-01-07 19:47:00 +00001091 case '@': /* ICH */
1092 count = set[0] ? args[0] : 1;
1093 if (count == 0) count = 1;
1094 terminal_shift_line(terminal, count);
1095 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001096 case 'A': /* CUU */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001097 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001098 if (count == 0) count = 1;
1099 if (terminal->row - count >= terminal->margin_top)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001100 terminal->row -= count;
1101 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001102 terminal->row = terminal->margin_top;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001103 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001104 case 'B': /* CUD */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001105 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001106 if (count == 0) count = 1;
1107 if (terminal->row + count <= terminal->margin_bottom)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001108 terminal->row += count;
1109 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001110 terminal->row = terminal->margin_bottom;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001111 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001112 case 'C': /* CUF */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001113 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001114 if (count == 0) count = 1;
1115 if ((terminal->column + count) < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001116 terminal->column += count;
1117 else
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001118 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001119 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001120 case 'D': /* CUB */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001121 count = set[0] ? args[0] : 1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001122 if (count == 0) count = 1;
1123 if ((terminal->column - count) >= 0)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001124 terminal->column -= count;
1125 else
1126 terminal->column = 0;
1127 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001128 case 'E': /* CNL */
1129 count = set[0] ? args[0] : 1;
1130 if (terminal->row + count <= terminal->margin_bottom)
1131 terminal->row += count;
1132 else
1133 terminal->row = terminal->margin_bottom;
1134 terminal->column = 0;
1135 break;
1136 case 'F': /* CPL */
1137 count = set[0] ? args[0] : 1;
1138 if (terminal->row - count >= terminal->margin_top)
1139 terminal->row -= count;
1140 else
1141 terminal->row = terminal->margin_top;
1142 terminal->column = 0;
1143 break;
1144 case 'G': /* CHA */
1145 y = set[0] ? args[0] : 1;
1146 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1147
1148 terminal->column = y - 1;
1149 break;
1150 case 'f': /* HVP */
1151 case 'H': /* CUP */
1152 x = (set[1] ? args[1] : 1) - 1;
1153 x = x < 0 ? 0 :
1154 (x >= terminal->width ? terminal->width - 1 : x);
1155
1156 y = (set[0] ? args[0] : 1) - 1;
1157 if (terminal->origin_mode) {
1158 y += terminal->margin_top;
1159 y = y < terminal->margin_top ? terminal->margin_top :
1160 (y > terminal->margin_bottom ? terminal->margin_bottom : y);
1161 } else {
1162 y = y < 0 ? 0 :
1163 (y >= terminal->height ? terminal->height - 1 : y);
1164 }
1165
1166 terminal->row = y;
1167 terminal->column = x;
1168 break;
1169 case 'I': /* CHT */
1170 count = set[0] ? args[0] : 1;
1171 if (count == 0) count = 1;
1172 while (count > 0 && terminal->column < terminal->width) {
1173 if (terminal->tab_ruler[terminal->column]) count--;
1174 terminal->column++;
1175 }
1176 terminal->column--;
1177 break;
1178 case 'J': /* ED */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001179 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001180 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001181 if (!set[0] || args[0] == 0 || args[0] > 2) {
1182 memset(&row[terminal->column],
1183 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
1184 attr_init(&attr_row[terminal->column],
1185 terminal->curr_attr, terminal->width - terminal->column);
1186 for (i = terminal->row + 1; i < terminal->height; i++) {
1187 memset(terminal_get_row(terminal, i),
1188 0, terminal->data_pitch);
1189 attr_init(terminal_get_attr_row(terminal, i),
1190 terminal->curr_attr, terminal->width);
1191 }
1192 } else if (args[0] == 1) {
1193 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1194 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1195 for (i = 0; i < terminal->row; i++) {
1196 memset(terminal_get_row(terminal, i),
1197 0, terminal->data_pitch);
1198 attr_init(terminal_get_attr_row(terminal, i),
1199 terminal->curr_attr, terminal->width);
1200 }
1201 } else if (args[0] == 2) {
1202 for (i = 0; i < terminal->height; i++) {
1203 memset(terminal_get_row(terminal, i),
1204 0, terminal->data_pitch);
1205 attr_init(terminal_get_attr_row(terminal, i),
1206 terminal->curr_attr, terminal->width);
1207 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001208 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001209 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001210 case 'K': /* EL */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001211 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +00001212 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001213 if (!set[0] || args[0] == 0 || args[0] > 2) {
1214 memset(&row[terminal->column], 0,
1215 (terminal->width - terminal->column) * sizeof(union utf8_char));
1216 attr_init(&attr_row[terminal->column], terminal->curr_attr,
1217 terminal->width - terminal->column);
1218 } else if (args[0] == 1) {
1219 memset(row, 0, (terminal->column+1) * sizeof(union utf8_char));
1220 attr_init(attr_row, terminal->curr_attr, terminal->column+1);
1221 } else if (args[0] == 2) {
1222 memset(row, 0, terminal->data_pitch);
1223 attr_init(attr_row, terminal->curr_attr, terminal->width);
1224 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001225 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001226 case 'L': /* IL */
1227 count = set[0] ? args[0] : 1;
1228 if (count == 0) count = 1;
1229 if (terminal->row >= terminal->margin_top &&
1230 terminal->row < terminal->margin_bottom)
1231 {
1232 top = terminal->margin_top;
1233 terminal->margin_top = terminal->row;
1234 terminal_scroll(terminal, 0 - count);
1235 terminal->margin_top = top;
1236 } else if (terminal->row == terminal->margin_bottom) {
1237 memset(terminal_get_row(terminal, terminal->row),
1238 0, terminal->data_pitch);
1239 attr_init(terminal_get_attr_row(terminal, terminal->row),
1240 terminal->curr_attr, terminal->width);
1241 }
1242 break;
1243 case 'M': /* DL */
1244 count = set[0] ? args[0] : 1;
1245 if (count == 0) count = 1;
1246 if (terminal->row >= terminal->margin_top &&
1247 terminal->row < terminal->margin_bottom)
1248 {
1249 top = terminal->margin_top;
1250 terminal->margin_top = terminal->row;
1251 terminal_scroll(terminal, count);
1252 terminal->margin_top = top;
1253 } else if (terminal->row == terminal->margin_bottom) {
1254 memset(terminal_get_row(terminal, terminal->row),
1255 0, terminal->data_pitch);
1256 }
1257 break;
1258 case 'P': /* DCH */
1259 count = set[0] ? args[0] : 1;
1260 if (count == 0) count = 1;
1261 terminal_shift_line(terminal, 0 - count);
1262 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001263 case 'S': /* SU */
1264 terminal_scroll(terminal, set[0] ? args[0] : 1);
1265 break;
1266 case 'T': /* SD */
1267 terminal_scroll(terminal, 0 - (set[0] ? args[0] : 1));
1268 break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001269 case 'X': /* ECH */
1270 count = set[0] ? args[0] : 1;
1271 if (count == 0) count = 1;
1272 if ((terminal->column + count) > terminal->width)
1273 count = terminal->width - terminal->column;
1274 row = terminal_get_row(terminal, terminal->row);
1275 attr_row = terminal_get_attr_row(terminal, terminal->row);
1276 memset(&row[terminal->column], 0, count * sizeof(union utf8_char));
1277 attr_init(&attr_row[terminal->column], terminal->curr_attr, count);
1278 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001279 case 'Z': /* CBT */
1280 count = set[0] ? args[0] : 1;
1281 if (count == 0) count = 1;
1282 while (count > 0 && terminal->column >= 0) {
1283 if (terminal->tab_ruler[terminal->column]) count--;
1284 terminal->column--;
1285 }
1286 terminal->column++;
1287 break;
1288 case '`': /* HPA */
1289 y = set[0] ? args[0] : 1;
1290 y = y <= 0 ? 1 : y > terminal->width ? terminal->width : y;
1291
1292 terminal->column = y - 1;
1293 break;
1294 case 'b': /* REP */
1295 count = set[0] ? args[0] : 1;
1296 if (count == 0) count = 1;
1297 if (terminal->last_char.byte[0])
1298 for (i = 0; i < count; i++)
1299 handle_char(terminal, terminal->last_char);
1300 terminal->last_char.byte[0] = 0;
1301 break;
1302 case 'c': /* Primary DA */
Callum Lowcay69e96582011-01-07 19:47:00 +00001303 write(terminal->master, "\e[?6c", 5);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001304 break;
1305 case 'd': /* VPA */
1306 x = set[0] ? args[0] : 1;
1307 x = x <= 0 ? 1 : x > terminal->height ? terminal->height : x;
1308
1309 terminal->row = x - 1;
1310 break;
1311 case 'g': /* TBC */
1312 if (!set[0] || args[0] == 0) {
1313 terminal->tab_ruler[terminal->column] = 0;
1314 } else if (args[0] == 3) {
1315 memset(terminal->tab_ruler, 0, terminal->width);
1316 }
1317 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001318 case 'h': /* SM */
1319 for(i = 0; i < 10 && set[i]; i++) {
1320 handle_term_parameter(terminal, args[i], 1);
1321 }
1322 break;
1323 case 'l': /* RM */
1324 for(i = 0; i < 10 && set[i]; i++) {
1325 handle_term_parameter(terminal, args[i], 0);
1326 }
1327 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001328 case 'm': /* SGR */
Callum Lowcay30eeae52011-01-07 19:46:55 +00001329 for(i = 0; i < 10; i++) {
Callum Lowcay81179db2011-01-10 12:14:01 +13001330 if (i <= 7 && set[i] && set[i + 1] &&
1331 set[i + 2] && args[i + 1] == 5)
1332 {
1333 if (args[i] == 38) {
1334 handle_sgr(terminal, args[i + 2] + 256);
1335 break;
1336 } else if (args[i] == 48) {
1337 handle_sgr(terminal, args[i + 2] + 512);
1338 break;
1339 }
1340 }
Callum Lowcay30eeae52011-01-07 19:46:55 +00001341 if(set[i]) {
1342 handle_sgr(terminal, args[i]);
1343 } else if(i == 0) {
1344 handle_sgr(terminal, 0);
1345 break;
1346 } else {
1347 break;
1348 }
1349 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001350 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001351 case 'n': /* DSR */
1352 i = set[0] ? args[0] : 0;
1353 if (i == 0 || i == 5) {
1354 write(terminal->master, "\e[0n", 4);
1355 } else if (i == 6) {
1356 snprintf(response, MAX_RESPONSE, "\e[%d;%dR",
1357 terminal->origin_mode ?
1358 terminal->row+terminal->margin_top : terminal->row+1,
1359 terminal->column+1);
1360 write(terminal->master, response, strlen(response));
1361 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001362 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001363 case 'r':
1364 if(!set[0]) {
1365 terminal->margin_top = 0;
1366 terminal->margin_bottom = terminal->height-1;
1367 terminal->row = 0;
1368 terminal->column = 0;
1369 } else {
1370 top = (set[0] ? args[0] : 1) - 1;
1371 top = top < 0 ? 0 :
1372 (top >= terminal->height ? terminal->height - 1 : top);
1373 bottom = (set[1] ? args[1] : 1) - 1;
1374 bottom = bottom < 0 ? 0 :
1375 (bottom >= terminal->height ? terminal->height - 1 : bottom);
1376 if(bottom > top) {
1377 terminal->margin_top = top;
1378 terminal->margin_bottom = bottom;
1379 } else {
1380 terminal->margin_top = 0;
1381 terminal->margin_bottom = terminal->height-1;
1382 }
1383 if(terminal->origin_mode)
1384 terminal->row = terminal->margin_top;
1385 else
1386 terminal->row = 0;
1387 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001388 }
1389 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001390 case 's':
1391 terminal->saved_row = terminal->row;
1392 terminal->saved_column = terminal->column;
1393 break;
1394 case 'u':
1395 terminal->row = terminal->saved_row;
1396 terminal->column = terminal->saved_column;
1397 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001398 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001399 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -05001400 break;
1401 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001402}
1403
1404static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001405handle_non_csi_escape(struct terminal *terminal, char code)
1406{
Callum Lowcaybbeac602011-01-07 19:46:58 +00001407 switch(code) {
1408 case 'M': /* RI */
1409 terminal->row -= 1;
1410 if(terminal->row < terminal->margin_top) {
1411 terminal->row = terminal->margin_top;
1412 terminal_scroll(terminal, -1);
1413 }
1414 break;
1415 case 'E': /* NEL */
1416 terminal->column = 0;
1417 // fallthrough
1418 case 'D': /* IND */
1419 terminal->row += 1;
1420 if(terminal->row > terminal->margin_bottom) {
1421 terminal->row = terminal->margin_bottom;
1422 terminal_scroll(terminal, +1);
1423 }
1424 break;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001425 case 'c': /* RIS */
1426 terminal_init(terminal);
1427 break;
1428 case 'H': /* HTS */
1429 terminal->tab_ruler[terminal->column] = 1;
1430 break;
1431 case '7': /* DECSC */
1432 terminal->saved_row = terminal->row;
1433 terminal->saved_column = terminal->column;
1434 terminal->saved_attr = terminal->curr_attr;
1435 terminal->saved_origin_mode = terminal->origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001436 terminal->saved_cs = terminal->cs;
1437 terminal->saved_g0 = terminal->g0;
1438 terminal->saved_g1 = terminal->g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001439 break;
1440 case '8': /* DECRC */
1441 terminal->row = terminal->saved_row;
1442 terminal->column = terminal->saved_column;
1443 terminal->curr_attr = terminal->saved_attr;
1444 terminal->origin_mode = terminal->saved_origin_mode;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001445 terminal->cs = terminal->saved_cs;
1446 terminal->g0 = terminal->saved_g0;
1447 terminal->g1 = terminal->saved_g1;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001448 break;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001449 case '=': /* DECPAM */
1450 terminal->key_mode = KM_APPLICATION;
1451 break;
1452 case '>': /* DECPNM */
1453 terminal->key_mode = KM_NORMAL;
1454 break;
Callum Lowcaybbeac602011-01-07 19:46:58 +00001455 default:
1456 fprintf(stderr, "Unknown escape code: %c\n", code);
1457 break;
1458 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001459}
1460
1461static void
1462handle_special_escape(struct terminal *terminal, char special, char code)
1463{
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001464 int i, numChars;
1465
1466 if (special == '#') {
1467 switch(code) {
1468 case '8':
1469 /* fill with 'E', no cheap way to do this */
1470 memset(terminal->data, 0, terminal->data_pitch * terminal->height);
1471 numChars = terminal->width * terminal->height;
1472 for(i = 0; i < numChars; i++) {
1473 terminal->data[i].byte[0] = 'E';
1474 }
1475 break;
1476 default:
1477 fprintf(stderr, "Unknown HASH escape #%c\n", code);
1478 break;
1479 }
Callum Lowcay256e72f2011-01-07 19:47:01 +00001480 } else if (special == '(' || special == ')') {
1481 switch(code) {
1482 case '0':
1483 if (special == '(')
1484 terminal->g0 = CS_SPECIAL;
1485 else
1486 terminal->g1 = CS_SPECIAL;
1487 break;
1488 case 'A':
1489 if (special == '(')
1490 terminal->g0 = CS_UK;
1491 else
1492 terminal->g1 = CS_UK;
1493 break;
1494 case 'B':
1495 if (special == '(')
1496 terminal->g0 = CS_US;
1497 else
1498 terminal->g1 = CS_US;
1499 break;
1500 default:
1501 fprintf(stderr, "Unknown character set %c\n", code);
1502 break;
1503 }
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001504 } else {
1505 fprintf(stderr, "Unknown special escape %c%c\n", special, code);
1506 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001507}
1508
1509static void
Callum Lowcay30eeae52011-01-07 19:46:55 +00001510handle_sgr(struct terminal *terminal, int code)
1511{
1512 switch(code) {
1513 case 0:
1514 terminal->curr_attr = terminal->color_scheme->default_attr;
1515 break;
1516 case 1:
1517 terminal->curr_attr.a |= ATTRMASK_BOLD;
1518 if (terminal->curr_attr.fg < 8)
1519 terminal->curr_attr.fg += 8;
1520 break;
1521 case 4:
1522 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
1523 break;
1524 case 5:
1525 terminal->curr_attr.a |= ATTRMASK_BLINK;
1526 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001527 case 8:
1528 terminal->curr_attr.a |= ATTRMASK_CONCEALED;
1529 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001530 case 2:
1531 case 21:
1532 case 22:
1533 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
1534 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
1535 terminal->curr_attr.fg -= 8;
1536 break;
1537 case 24:
1538 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
1539 break;
1540 case 25:
1541 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
1542 break;
1543 case 7:
1544 case 26:
1545 terminal->curr_attr.a |= ATTRMASK_INVERSE;
1546 break;
1547 case 27:
1548 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
1549 break;
Callum Lowcay81179db2011-01-10 12:14:01 +13001550 case 28:
1551 terminal->curr_attr.a &= ~ATTRMASK_CONCEALED;
1552 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001553 case 39:
1554 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
1555 break;
1556 case 49:
1557 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
1558 break;
1559 default:
1560 if(code >= 30 && code <= 37) {
1561 terminal->curr_attr.fg = code - 30;
1562 if (terminal->curr_attr.a & ATTRMASK_BOLD)
1563 terminal->curr_attr.fg += 8;
1564 } else if(code >= 40 && code <= 47) {
1565 terminal->curr_attr.bg = code - 40;
Callum Lowcay81179db2011-01-10 12:14:01 +13001566 } else if (code >= 90 && code <= 97) {
1567 terminal->curr_attr.fg = code - 90 + 8;
1568 } else if (code >= 100 && code <= 107) {
1569 terminal->curr_attr.bg = code - 100 + 8;
Callum Lowcay30eeae52011-01-07 19:46:55 +00001570 } else if(code >= 256 && code < 512) {
1571 terminal->curr_attr.fg = code - 256;
1572 } else if(code >= 512 && code < 768) {
1573 terminal->curr_attr.bg = code - 512;
1574 } else {
1575 fprintf(stderr, "Unknown SGR code: %d\n", code);
1576 }
1577 break;
1578 }
1579}
1580
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001581/* Returns 1 if c was special, otherwise 0 */
1582static int
1583handle_special_char(struct terminal *terminal, char c)
1584{
1585 union utf8_char *row;
1586 struct attr *attr_row;
1587
1588 row = terminal_get_row(terminal, terminal->row);
1589 attr_row = terminal_get_attr_row(terminal, terminal->row);
1590
1591 switch(c) {
1592 case '\r':
1593 terminal->column = 0;
1594 break;
1595 case '\n':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001596 if (terminal->mode & MODE_LF_NEWLINE) {
1597 terminal->column = 0;
1598 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001599 /* fallthrough */
1600 case '\v':
1601 case '\f':
Callum Lowcaybbeac602011-01-07 19:46:58 +00001602 terminal->row++;
1603 if(terminal->row > terminal->margin_bottom) {
1604 terminal->row = terminal->margin_bottom;
1605 terminal_scroll(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001606 }
1607
1608 break;
1609 case '\t':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001610 while (terminal->column < terminal->width) {
1611 if (terminal->tab_ruler[terminal->column]) break;
Callum Lowcay69e96582011-01-07 19:47:00 +00001612 if (terminal->mode & MODE_IRM)
1613 terminal_shift_line(terminal, +1);
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001614 row[terminal->column].byte[0] = ' ';
1615 row[terminal->column].byte[1] = '\0';
1616 attr_row[terminal->column] = terminal->curr_attr;
1617 terminal->column++;
1618 }
1619 if (terminal->column >= terminal->width) {
1620 terminal->column = terminal->width - 1;
1621 }
1622
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001623 break;
1624 case '\b':
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001625 if (terminal->column >= terminal->width) {
1626 terminal->column = terminal->width - 2;
1627 } else if (terminal->column > 0) {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001628 terminal->column--;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001629 } else if (terminal->mode & MODE_AUTOWRAP) {
1630 terminal->column = terminal->width - 1;
1631 terminal->row -= 1;
1632 if (terminal->row < terminal->margin_top) {
1633 terminal->row = terminal->margin_top;
1634 terminal_scroll(terminal, -1);
1635 }
1636 }
1637
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001638 break;
1639 case '\a':
1640 /* Bell */
1641 break;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001642 case '\x0E': /* SO */
1643 terminal->cs = terminal->g1;
1644 break;
1645 case '\x0F': /* SI */
1646 terminal->cs = terminal->g0;
1647 break;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001648 default:
1649 return 0;
1650 }
1651
1652 return 1;
1653}
1654
1655static void
1656handle_char(struct terminal *terminal, union utf8_char utf8)
1657{
1658 union utf8_char *row;
1659 struct attr *attr_row;
1660
1661 if (handle_special_char(terminal, utf8.byte[0])) return;
Callum Lowcay256e72f2011-01-07 19:47:01 +00001662
1663 apply_char_set(terminal->cs, &utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001664
1665 /* There are a whole lot of non-characters, control codes,
1666 * and formatting codes that should probably be ignored,
1667 * for example: */
1668 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
1669 /* BOM, ignore */
1670 return;
1671 }
1672
1673 /* Some of these non-characters should be translated, e.g.: */
1674 if (utf8.byte[0] < 32) {
1675 utf8.byte[0] = utf8.byte[0] + 64;
1676 }
1677
1678 /* handle right margin effects */
1679 if (terminal->column >= terminal->width) {
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001680 if (terminal->mode & MODE_AUTOWRAP) {
1681 terminal->column = 0;
1682 terminal->row += 1;
1683 if (terminal->row > terminal->margin_bottom) {
1684 terminal->row = terminal->margin_bottom;
1685 terminal_scroll(terminal, +1);
1686 }
1687 } else {
1688 terminal->column--;
1689 }
1690 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001691
1692 row = terminal_get_row(terminal, terminal->row);
1693 attr_row = terminal_get_attr_row(terminal, terminal->row);
1694
Callum Lowcay69e96582011-01-07 19:47:00 +00001695 if (terminal->mode & MODE_IRM)
1696 terminal_shift_line(terminal, +1);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001697 row[terminal->column] = utf8;
1698 attr_row[terminal->column++] = terminal->curr_attr;
1699
1700 if (utf8.ch != terminal->last_char.ch)
1701 terminal->last_char = utf8;
1702}
1703
Callum Lowcay30eeae52011-01-07 19:46:55 +00001704static void
Callum Lowcay67a201d2011-01-12 19:23:41 +13001705escape_append_utf8(struct terminal *terminal, union utf8_char utf8)
1706{
1707 int len, i;
1708
1709 if ((utf8.byte[0] & 0x80) == 0x00) len = 1;
1710 else if ((utf8.byte[0] & 0xE0) == 0xC0) len = 2;
1711 else if ((utf8.byte[0] & 0xF0) == 0xE0) len = 3;
1712 else if ((utf8.byte[0] & 0xF8) == 0xF0) len = 4;
1713 else len = 1; /* Invalid, cannot happen */
1714
1715 if (terminal->escape_length + len <= MAX_ESCAPE) {
1716 for (i = 0; i < len; i++)
1717 terminal->escape[terminal->escape_length + i] = utf8.byte[i];
1718 terminal->escape_length += len;
1719 } else if (terminal->escape_length < MAX_ESCAPE) {
1720 terminal->escape[terminal->escape_length++] = 0;
1721 }
1722}
1723
1724static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001725terminal_data(struct terminal *terminal, const char *data, size_t length)
1726{
1727 int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001728 union utf8_char utf8;
1729 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001730
1731 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00001732 parser_state =
1733 utf8_next_char(&terminal->state_machine, data[i]);
1734 switch(parser_state) {
1735 case utf8state_accept:
1736 utf8.ch = terminal->state_machine.s.ch;
1737 break;
1738 case utf8state_reject:
1739 /* the unicode replacement character */
1740 utf8.byte[0] = 0xEF;
1741 utf8.byte[1] = 0xBF;
1742 utf8.byte[2] = 0xBD;
1743 utf8.byte[3] = 0x00;
1744 break;
1745 default:
1746 continue;
1747 }
1748
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001749 /* assume escape codes never use non-ASCII characters */
Callum Lowcay67a201d2011-01-12 19:23:41 +13001750 switch (terminal->state) {
1751 case escape_state_escape:
1752 escape_append_utf8(terminal, utf8);
1753 switch (utf8.byte[0]) {
1754 case 'P': /* DCS */
1755 terminal->state = escape_state_dcs;
1756 break;
1757 case '[': /* CSI */
1758 terminal->state = escape_state_csi;
1759 break;
1760 case ']': /* OSC */
1761 terminal->state = escape_state_osc;
1762 break;
1763 case '#':
1764 case '(':
1765 case ')': /* special */
1766 terminal->state = escape_state_special;
1767 break;
1768 case '^': /* PM (not implemented) */
1769 case '_': /* APC (not implemented) */
1770 terminal->state = escape_state_ignore;
1771 break;
1772 default:
1773 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001774 handle_non_csi_escape(terminal, utf8.byte[0]);
Callum Lowcay67a201d2011-01-12 19:23:41 +13001775 break;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001776 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001777 continue;
1778 case escape_state_csi:
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001779 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
1780 /* do nothing */
1781 } else if (utf8.byte[0] == '?') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001782 terminal->escape_flags |= ESC_FLAG_WHAT;
1783 } else if (utf8.byte[0] == '>') {
1784 terminal->escape_flags |= ESC_FLAG_GT;
1785 } else if (utf8.byte[0] == '!') {
1786 terminal->escape_flags |= ESC_FLAG_BANG;
1787 } else if (utf8.byte[0] == '$') {
1788 terminal->escape_flags |= ESC_FLAG_CASH;
1789 } else if (utf8.byte[0] == '\'') {
1790 terminal->escape_flags |= ESC_FLAG_SQUOTE;
1791 } else if (utf8.byte[0] == '"') {
1792 terminal->escape_flags |= ESC_FLAG_DQUOTE;
1793 } else if (utf8.byte[0] == ' ') {
1794 terminal->escape_flags |= ESC_FLAG_SPACE;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001795 } else {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001796 escape_append_utf8(terminal, utf8);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001797 if (terminal->escape_length >= MAX_ESCAPE)
Callum Lowcay67a201d2011-01-12 19:23:41 +13001798 terminal->state = escape_state_normal;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001799 }
1800
1801 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
1802 utf8.byte[0] == '`')
1803 {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001804 terminal->state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001805 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001806 } else {
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001807 }
Callum Lowcay67a201d2011-01-12 19:23:41 +13001808 continue;
1809 case escape_state_inner_escape:
1810 if (utf8.byte[0] == '\\') {
1811 terminal->state = escape_state_normal;
1812 if (terminal->outer_state == escape_state_dcs) {
1813 handle_dcs(terminal);
1814 } else if (terminal->outer_state == escape_state_osc) {
1815 handle_osc(terminal);
1816 }
1817 } else if (utf8.byte[0] == '\e') {
1818 terminal->state = terminal->outer_state;
1819 escape_append_utf8(terminal, utf8);
1820 if (terminal->escape_length >= MAX_ESCAPE)
1821 terminal->state = escape_state_normal;
1822 } else {
1823 terminal->state = terminal->outer_state;
1824 if (terminal->escape_length < MAX_ESCAPE)
1825 terminal->escape[terminal->escape_length++] = '\e';
1826 escape_append_utf8(terminal, utf8);
1827 if (terminal->escape_length >= MAX_ESCAPE)
1828 terminal->state = escape_state_normal;
1829 }
1830 continue;
1831 case escape_state_dcs:
1832 case escape_state_osc:
1833 case escape_state_ignore:
1834 if (utf8.byte[0] == '\e') {
1835 terminal->outer_state = terminal->state;
1836 terminal->state = escape_state_inner_escape;
1837 } else if (utf8.byte[0] == '\a' && terminal->state == escape_state_osc) {
1838 terminal->state = escape_state_normal;
1839 handle_osc(terminal);
1840 } else {
1841 escape_append_utf8(terminal, utf8);
1842 if (terminal->escape_length >= MAX_ESCAPE)
1843 terminal->state = escape_state_normal;
1844 }
1845 continue;
1846 case escape_state_special:
1847 escape_append_utf8(terminal, utf8);
1848 terminal->state = escape_state_normal;
1849 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
1850 handle_special_escape(terminal, terminal->escape[1],
1851 utf8.byte[0]);
1852 }
1853 continue;
1854 default:
1855 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -05001856 }
1857
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001858 /* this is valid, because ASCII characters are never used to
1859 * introduce a multibyte sequence in UTF-8 */
1860 if (utf8.byte[0] == '\e') {
Callum Lowcay67a201d2011-01-12 19:23:41 +13001861 terminal->state = escape_state_escape;
1862 terminal->outer_state = escape_state_normal;
Kristian Høgsberg17809b12008-12-08 12:20:40 -05001863 terminal->escape[0] = '\e';
1864 terminal->escape_length = 1;
Callum Lowcay67a201d2011-01-12 19:23:41 +13001865 terminal->escape_flags = 0;
Callum Lowcayb8609ad2011-01-07 19:46:57 +00001866 } else {
1867 handle_char(terminal, utf8);
1868 } /* if */
1869 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -05001870
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001871 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001872}
1873
1874static void
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001875key_handler(struct window *window, uint32_t key, uint32_t sym,
Kristian Høgsberg55444912009-02-21 14:31:09 -05001876 uint32_t state, uint32_t modifiers, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001877{
1878 struct terminal *terminal = data;
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001879 char ch[MAX_RESPONSE];
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001880 int len = 0;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001881
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001882 switch (sym) {
1883 case XK_F11:
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001884 if (!state)
1885 break;
1886 terminal->fullscreen ^= 1;
1887 window_set_fullscreen(window, terminal->fullscreen);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04001888 window_schedule_redraw(terminal->window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001889 break;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001890
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001891 case XK_BackSpace:
1892 case XK_Tab:
1893 case XK_Linefeed:
1894 case XK_Clear:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001895 case XK_Pause:
1896 case XK_Scroll_Lock:
1897 case XK_Sys_Req:
1898 case XK_Escape:
1899 ch[len++] = sym & 0x7f;
1900 break;
1901
Callum Lowcay8e57dd52011-01-07 19:46:59 +00001902 case XK_Return:
1903 if (terminal->mode & MODE_LF_NEWLINE) {
1904 ch[len++] = 0x0D;
1905 ch[len++] = 0x0A;
1906 } else {
1907 ch[len++] = 0x0D;
1908 }
1909 break;
1910
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001911 case XK_Shift_L:
1912 case XK_Shift_R:
1913 case XK_Control_L:
1914 case XK_Control_R:
1915 case XK_Alt_L:
1916 case XK_Alt_R:
1917 break;
1918
Callum Lowcay7e08e902011-01-07 19:47:02 +00001919 case XK_Insert:
1920 len = function_key_response('[', 2, modifiers, '~', ch);
1921 break;
1922 case XK_Delete:
1923 if (terminal->mode & MODE_DELETE_SENDS_DEL) {
1924 ch[len++] = '\x04';
1925 } else {
1926 len = function_key_response('[', 3, modifiers, '~', ch);
1927 }
1928 break;
1929 case XK_Page_Up:
1930 len = function_key_response('[', 5, modifiers, '~', ch);
1931 break;
1932 case XK_Page_Down:
1933 len = function_key_response('[', 6, modifiers, '~', ch);
1934 break;
1935 case XK_F1:
1936 len = function_key_response('O', 1, modifiers, 'P', ch);
1937 break;
1938 case XK_F2:
1939 len = function_key_response('O', 1, modifiers, 'Q', ch);
1940 break;
1941 case XK_F3:
1942 len = function_key_response('O', 1, modifiers, 'R', ch);
1943 break;
1944 case XK_F4:
1945 len = function_key_response('O', 1, modifiers, 'S', ch);
1946 break;
1947 case XK_F5:
1948 len = function_key_response('[', 15, modifiers, '~', ch);
1949 break;
1950 case XK_F6:
1951 len = function_key_response('[', 17, modifiers, '~', ch);
1952 break;
1953 case XK_F7:
1954 len = function_key_response('[', 18, modifiers, '~', ch);
1955 break;
1956 case XK_F8:
1957 len = function_key_response('[', 19, modifiers, '~', ch);
1958 break;
1959 case XK_F9:
1960 len = function_key_response('[', 20, modifiers, '~', ch);
1961 break;
1962 case XK_F10:
1963 len = function_key_response('[', 21, modifiers, '~', ch);
1964 break;
1965 case XK_F12:
1966 len = function_key_response('[', 24, modifiers, '~', ch);
1967 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001968 default:
Callum Lowcay7e08e902011-01-07 19:47:02 +00001969 /* Handle special keys with alternate mappings */
1970 len = apply_key_map(terminal->key_mode, sym, modifiers, ch);
1971 if (len != 0) break;
1972
1973 if (modifiers & WINDOW_MODIFIER_CONTROL) {
1974 if (sym >= '3' && sym <= '7')
1975 sym = (sym & 0x1f) + 8;
1976
1977 if (!((sym >= '!' && sym <= '/') ||
1978 (sym >= '8' && sym <= '?') ||
1979 (sym >= '0' && sym <= '2'))) sym = sym & 0x1f;
1980 else if (sym == '2') sym = 0x00;
1981 else if (sym == '/') sym = 0x1F;
1982 else if (sym == '8' || sym == '?') sym = 0x7F;
1983 } else if ((terminal->mode & MODE_ALT_SENDS_ESC) &&
1984 (modifiers & WINDOW_MODIFIER_ALT))
1985 {
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001986 ch[len++] = 0x1b;
Callum Lowcay7e08e902011-01-07 19:47:02 +00001987 } else if (modifiers & WINDOW_MODIFIER_ALT) {
1988 sym = sym | 0x80;
1989 }
1990
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001991 if (sym < 256)
1992 ch[len++] = sym;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001993 break;
1994 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -04001995
1996 if (state && len > 0)
1997 write(terminal->master, ch, len);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001998}
1999
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002000static void
2001keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -04002002 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002003{
2004 struct terminal *terminal = data;
2005
2006 terminal->focused = (device != NULL);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -04002007 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002008}
2009
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002010static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002011terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002012{
2013 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -04002014 cairo_surface_t *surface;
2015 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002016
2017 terminal = malloc(sizeof *terminal);
2018 if (terminal == NULL)
2019 return terminal;
2020
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002021 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002022 terminal->fullscreen = fullscreen;
Callum Lowcay30eeae52011-01-07 19:46:55 +00002023 terminal->color_scheme = &DEFAULT_COLORS;
2024 terminal_init(terminal);
Callum Lowcaybbeac602011-01-07 19:46:58 +00002025 terminal->margin_top = 0;
Callum Lowcay86653ed2011-01-07 19:47:03 +00002026 terminal->margin_bottom = -1;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002027 terminal->window = window_create(display, "Wayland Terminal",
Kristian Høgsberg82da52b2010-12-17 09:53:12 -05002028 500, 400);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002029
2030 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +00002031 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +00002032
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002033 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -05002034 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -05002035
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002036 window_set_fullscreen(terminal->window, terminal->fullscreen);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002037 window_set_user_data(terminal->window, terminal);
2038 window_set_redraw_handler(terminal->window, redraw_handler);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -05002039 window_set_resize_handler(terminal->window, resize_handler);
Kristian Høgsberg94448c02008-12-30 11:03:33 -05002040
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002041 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -05002042 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -04002043 keyboard_focus_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002044
Kristian Høgsberg09531622010-06-14 23:22:15 -04002045 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
2046 cr = cairo_create(surface);
Kristian Høgsberg09531622010-06-14 23:22:15 -04002047 cairo_set_font_size(cr, 14);
Kristian Høgsberg7ae6b1a2010-12-25 16:58:31 -05002048 cairo_select_font_face (cr, "mono",
2049 CAIRO_FONT_SLANT_NORMAL,
2050 CAIRO_FONT_WEIGHT_BOLD);
2051 terminal->font_bold = cairo_get_scaled_font (cr);
2052 cairo_scaled_font_reference(terminal->font_bold);
2053
2054 cairo_select_font_face (cr, "mono",
2055 CAIRO_FONT_SLANT_NORMAL,
2056 CAIRO_FONT_WEIGHT_NORMAL);
2057 terminal->font_normal = cairo_get_scaled_font (cr);
2058 cairo_scaled_font_reference(terminal->font_normal);
2059
Kristian Høgsberg09531622010-06-14 23:22:15 -04002060 cairo_font_extents(cr, &terminal->extents);
2061 cairo_destroy(cr);
2062 cairo_surface_destroy(surface);
2063
Callum Lowcaya0ee21c2011-01-07 19:46:56 +00002064 terminal_resize(terminal, 80, 24);
Kristian Høgsberg22106762008-12-08 13:50:07 -05002065 terminal_draw(terminal);
2066
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002067 return terminal;
2068}
2069
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002070static gboolean
2071io_handler(GIOChannel *source,
2072 GIOCondition condition,
2073 gpointer data)
2074{
2075 struct terminal *terminal = data;
2076 gchar buffer[256];
2077 gsize bytes_read;
2078 GError *error = NULL;
2079
2080 g_io_channel_read_chars(source, buffer, sizeof buffer,
2081 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002082
2083 terminal_data(terminal, buffer, bytes_read);
2084
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002085 return TRUE;
2086}
2087
2088static int
2089terminal_run(struct terminal *terminal, const char *path)
2090{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002091 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002092 pid_t pid;
2093
2094 pid = forkpty(&master, NULL, NULL, NULL);
2095 if (pid == 0) {
Callum Lowcay86653ed2011-01-07 19:47:03 +00002096 setenv("TERM", "xterm-256color", 1);
2097 setenv("COLORTERM", "xterm-256color", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002098 if (execl(path, path, NULL)) {
2099 printf("exec failed: %m\n");
2100 exit(EXIT_FAILURE);
2101 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002102 } else if (pid < 0) {
2103 fprintf(stderr, "failed to fork and create pty (%m).\n");
2104 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002105 }
2106
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05002107 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05002108 terminal->channel = g_io_channel_unix_new(master);
2109 fcntl(master, F_SETFL, O_NONBLOCK);
2110 g_io_add_watch(terminal->channel, G_IO_IN,
2111 io_handler, terminal);
2112
2113 return 0;
2114}
2115
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002116static const GOptionEntry option_entries[] = {
2117 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
2118 &option_fullscreen, "Run in fullscreen mode" },
2119 { NULL }
2120};
2121
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002122int main(int argc, char *argv[])
2123{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002124 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002125 struct terminal *terminal;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05002126
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002127 d = display_create(&argc, &argv, option_entries);
Yuval Fledele9f5e362010-11-22 21:34:19 +02002128 if (d == NULL) {
2129 fprintf(stderr, "failed to create display: %m\n");
2130 return -1;
2131 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002132
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05002133 terminal = terminal_create(d, option_fullscreen);
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05002134 if (terminal_run(terminal, "/bin/bash"))
2135 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002136
Kristian Høgsberg7824d812010-06-08 14:59:44 -04002137 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002138
2139 return 0;
2140}