blob: 2868674adc7c5bf98895c5be3da41b54a3ab4b48 [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 Lowcay15bdc5d2011-01-07 19:46:54 +000050union utf8_char {
51 unsigned char byte[4];
52 uint32_t ch;
53};
54
55enum utf8_state {
56 utf8state_start,
57 utf8state_accept,
58 utf8state_reject,
59 utf8state_expect3,
60 utf8state_expect2,
61 utf8state_expect1
62};
63
64struct utf8_state_machine {
65 enum utf8_state state;
66 int len;
67 union utf8_char s;
68};
69
70static void
71init_state_machine(struct utf8_state_machine *machine)
72{
73 machine->state = utf8state_start;
74 machine->len = 0;
75 machine->s.ch = 0;
76}
77
78static enum utf8_state
79utf8_next_char(struct utf8_state_machine *machine, char c)
80{
81 switch(machine->state) {
82 case utf8state_start:
83 case utf8state_accept:
84 case utf8state_reject:
85 machine->s.ch = 0;
86 machine->len = 0;
87 if(c == 0xC0 || c == 0xC1) {
88 /* overlong encoding, reject */
89 machine->state = utf8state_reject;
90 } else if((c & 0x80) == 0) {
91 /* single byte, accept */
92 machine->s.byte[machine->len++] = c;
93 machine->state = utf8state_accept;
94 } else if((c & 0xC0) == 0x80) {
95 /* parser out of sync, ignore byte */
96 machine->state = utf8state_start;
97 } else if((c & 0xE0) == 0xC0) {
98 /* start of two byte sequence */
99 machine->s.byte[machine->len++] = c;
100 machine->state = utf8state_expect1;
101 } else if((c & 0xF0) == 0xE0) {
102 /* start of three byte sequence */
103 machine->s.byte[machine->len++] = c;
104 machine->state = utf8state_expect2;
105 } else if((c & 0xF8) == 0xF0) {
106 /* start of four byte sequence */
107 machine->s.byte[machine->len++] = c;
108 machine->state = utf8state_expect3;
109 } else {
110 /* overlong encoding, reject */
111 machine->state = utf8state_reject;
112 }
113 break;
114 case utf8state_expect3:
115 machine->s.byte[machine->len++] = c;
116 if((c & 0xC0) == 0x80) {
117 /* all good, continue */
118 machine->state = utf8state_expect2;
119 } else {
120 /* missing extra byte, reject */
121 machine->state = utf8state_reject;
122 }
123 break;
124 case utf8state_expect2:
125 machine->s.byte[machine->len++] = c;
126 if((c & 0xC0) == 0x80) {
127 /* all good, continue */
128 machine->state = utf8state_expect1;
129 } else {
130 /* missing extra byte, reject */
131 machine->state = utf8state_reject;
132 }
133 break;
134 case utf8state_expect1:
135 machine->s.byte[machine->len++] = c;
136 if((c & 0xC0) == 0x80) {
137 /* all good, accept */
138 machine->state = utf8state_accept;
139 } else {
140 /* missing extra byte, reject */
141 machine->state = utf8state_reject;
142 }
143 break;
144 default:
145 machine->state = utf8state_reject;
146 break;
147 }
148
149 return machine->state;
150}
151
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500152struct terminal {
153 struct window *window;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500154 struct display *display;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000155 union utf8_char *data;
156 union utf8_char last_char;
157 int data_pitch; /* The width in bytes of a line */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500158 int width, height, start, row, column;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500159 int fd, master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500160 GIOChannel *channel;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500161 uint32_t modifiers;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500162 char escape[64];
163 int escape_length;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500164 int state;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000165 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500166 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500167 int fullscreen;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500168 int focused;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400169 struct color_scheme *color_scheme;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400170 cairo_font_extents_t extents;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500171};
172
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000173static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500174terminal_get_row(struct terminal *terminal, int row)
175{
176 int index;
177
178 index = (row + terminal->start) % terminal->height;
179
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000180 return &terminal->data[index * terminal->width];
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500181}
182
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500183static void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500184terminal_resize(struct terminal *terminal, int width, int height)
185{
186 size_t size;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000187 union utf8_char *data;
188 int data_pitch;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500189 int i, l, total_rows, start;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500190
191 if (terminal->width == width && terminal->height == height)
192 return;
193
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000194 data_pitch = width * sizeof(union utf8_char);
195 size = data_pitch * height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500196 data = malloc(size);
197 memset(data, 0, size);
198 if (terminal->data) {
199 if (width > terminal->width)
200 l = terminal->width;
201 else
202 l = width;
203
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500204 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500205 total_rows = height;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500206 start = terminal->height - height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500207 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500208 total_rows = terminal->height;
209 start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500210 }
211
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000212 for (i = 0; i < total_rows; i++) {
213 memcpy(&data[width * i],
214 terminal_get_row(terminal, i),
215 l * sizeof(union utf8_char));
216 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500217
218 free(terminal->data);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500219 }
220
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000221 terminal->data_pitch = data_pitch;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500222 terminal->width = width;
223 terminal->height = height;
224 terminal->data = data;
225
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500226 if (terminal->row >= terminal->height)
227 terminal->row = terminal->height - 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500228 if (terminal->column >= terminal->width)
229 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500230 terminal->start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500231}
232
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400233struct color_scheme { struct { double r, g, b, a; } fg, bg; }
234 matrix_colors = { { 0, 0.7, 0, 1 }, { 0, 0, 0, 0.9 } },
235 jbarnes_colors = { { 1, 1, 1, 1 }, { 0, 0, 0, 1 } };
236
Kristian Høgsberg22106762008-12-08 13:50:07 -0500237static void
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500238terminal_draw_contents(struct terminal *terminal)
239{
240 struct rectangle rectangle;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500241 cairo_t *cr;
242 cairo_font_extents_t extents;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000243 int top_margin, side_margin;
244 int row, col;
245 union utf8_char *p_row;
246 struct utf8_chars {
247 union utf8_char c;
248 char null;
249 } toShow;
250 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500251 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500252 double d;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500253
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000254 toShow.null = 0;
255
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500256 window_get_child_rectangle(terminal->window, &rectangle);
257
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400258 surface = display_create_surface(terminal->display, &rectangle);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500259 cr = cairo_create(surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500260 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400261 cairo_set_source_rgba(cr,
262 terminal->color_scheme->bg.r,
263 terminal->color_scheme->bg.g,
264 terminal->color_scheme->bg.b,
265 terminal->color_scheme->bg.a);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500266 cairo_paint(cr);
267 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400268 cairo_set_source_rgba(cr,
269 terminal->color_scheme->fg.r,
270 terminal->color_scheme->fg.g,
271 terminal->color_scheme->fg.b,
272 terminal->color_scheme->fg.a);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500273
274 cairo_select_font_face (cr, "mono",
275 CAIRO_FONT_SLANT_NORMAL,
276 CAIRO_FONT_WEIGHT_NORMAL);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500277 cairo_set_font_size(cr, 14);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500278
279 cairo_font_extents(cr, &extents);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500280 side_margin = (rectangle.width - terminal->width * extents.max_x_advance) / 2;
281 top_margin = (rectangle.height - terminal->height * extents.height) / 2;
282
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000283 for (row = 0; row < terminal->height; row++) {
284 p_row = terminal_get_row(terminal, row);
285 for (col = 0; col < terminal->width; col++) {
286 /* paint the foreground */
287 text_x = side_margin + col * extents.max_x_advance;
288 text_y = top_margin + extents.ascent + row * extents.height;
289 cairo_move_to(cr, text_x, text_y);
290
291 toShow.c = p_row[col];
292 cairo_show_text(cr, (char *) toShow.c.byte);
293 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500294 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500295
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500296 d = terminal->focused ? 0 : 0.5;
297
298 cairo_set_line_width(cr, 1);
299 cairo_move_to(cr, side_margin + terminal->column * extents.max_x_advance + d,
300 top_margin + terminal->row * extents.height + d);
301 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
302 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
303 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500304 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500305
306 if (terminal->focused)
307 cairo_fill(cr);
308 else
309 cairo_stroke(cr);
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500310
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500311 cairo_destroy(cr);
312
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500313 window_copy_surface(terminal->window,
314 &rectangle,
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500315 surface);
316
317 cairo_surface_destroy(surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500318}
319
320static void
321terminal_draw(struct terminal *terminal)
322{
Kristian Høgsberg22106762008-12-08 13:50:07 -0500323 struct rectangle rectangle;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500324 int32_t width, height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500325
326 window_get_child_rectangle(terminal->window, &rectangle);
327
Kristian Høgsberg09531622010-06-14 23:22:15 -0400328 width = (rectangle.width - 2 * terminal->margin) /
329 (int32_t) terminal->extents.max_x_advance;
330 height = (rectangle.height - 2 * terminal->margin) /
331 (int32_t) terminal->extents.height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500332 terminal_resize(terminal, width, height);
333
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500334 if (!terminal->fullscreen) {
Kristian Høgsberg09531622010-06-14 23:22:15 -0400335 rectangle.width = terminal->width *
336 terminal->extents.max_x_advance + 2 * terminal->margin;
337 rectangle.height = terminal->height *
338 terminal->extents.height + 2 * terminal->margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500339 window_set_child_size(terminal->window, &rectangle);
340 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500341
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500342 window_draw(terminal->window);
343 terminal_draw_contents(terminal);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400344 window_flush(terminal->window);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500345}
346
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400347static void
348redraw_handler(struct window *window, void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500349{
350 struct terminal *terminal = data;
351
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500352 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500353}
354
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500355#define STATE_NORMAL 0
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500356#define STATE_ESCAPE 1
357
358static void
359terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500360
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500361static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500362handle_escape(struct terminal *terminal)
363{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000364 union utf8_char *row;
365 char *p;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500366 int i, count;
367 int args[10], set[10] = { 0, };
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500368
369 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500370 i = 0;
371 p = &terminal->escape[2];
372 while ((isdigit(*p) || *p == ';') && i < 10) {
373 if (*p == ';') {
374 p++;
375 i++;
376 } else {
377 args[i] = strtol(p, &p, 10);
378 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500379 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500380 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500381
382 switch (*p) {
383 case 'A':
384 count = set[0] ? args[0] : 1;
385 if (terminal->row - count >= 0)
386 terminal->row -= count;
387 else
388 terminal->row = 0;
389 break;
390 case 'B':
391 count = set[0] ? args[0] : 1;
392 if (terminal->row + count < terminal->height)
393 terminal->row += count;
394 else
395 terminal->row = terminal->height;
396 break;
397 case 'C':
398 count = set[0] ? args[0] : 1;
399 if (terminal->column + count < terminal->width)
400 terminal->column += count;
401 else
402 terminal->column = terminal->width;
403 break;
404 case 'D':
405 count = set[0] ? args[0] : 1;
406 if (terminal->column - count >= 0)
407 terminal->column -= count;
408 else
409 terminal->column = 0;
410 break;
411 case 'J':
412 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000413 memset(&row[terminal->column], 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500414 for (i = terminal->row + 1; i < terminal->height; i++)
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000415 memset(terminal_get_row(terminal, i), 0, terminal->width * sizeof(union utf8_char));
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500416 break;
417 case 'G':
418 if (set[0])
419 terminal->column = args[0] - 1;
420 break;
421 case 'H':
422 case 'f':
423 terminal->row = set[0] ? args[0] - 1 : 0;
424 terminal->column = set[1] ? args[1] - 1 : 0;
425 break;
426 case 'K':
427 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000428 memset(&row[terminal->column], 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500429 break;
430 case 'm':
431 /* color, blink, bold etc*/
432 break;
433 case '?':
434 if (strcmp(p, "?25l") == 0) {
435 /* hide cursor */
436 } else if (strcmp(p, "?25h") == 0) {
437 /* show cursor */
438 }
439 break;
440 default:
441 terminal_data(terminal,
442 terminal->escape + 1,
443 terminal->escape_length - 2);
444 break;
445 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500446}
447
448static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500449terminal_data(struct terminal *terminal, const char *data, size_t length)
450{
451 int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000452 union utf8_char utf8;
453 enum utf8_state parser_state;
454 union utf8_char *row;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500455
456 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000457 parser_state =
458 utf8_next_char(&terminal->state_machine, data[i]);
459 switch(parser_state) {
460 case utf8state_accept:
461 utf8.ch = terminal->state_machine.s.ch;
462 break;
463 case utf8state_reject:
464 /* the unicode replacement character */
465 utf8.byte[0] = 0xEF;
466 utf8.byte[1] = 0xBF;
467 utf8.byte[2] = 0xBD;
468 utf8.byte[3] = 0x00;
469 break;
470 default:
471 continue;
472 }
473
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500474 row = terminal_get_row(terminal, terminal->row);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500475
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500476 if (terminal->state == STATE_ESCAPE) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000477 terminal->escape[terminal->escape_length++] = utf8.byte[0];
478 if (terminal->escape_length == 2 && utf8.byte[0] != '[') {
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500479 /* Bad escape sequence. */
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500480 terminal->state = STATE_NORMAL;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500481 goto cancel_escape;
482 }
483
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000484 if (isalpha(utf8.byte[0])) {
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500485 terminal->state = STATE_NORMAL;
486 handle_escape(terminal);
487 }
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500488 continue;
489 }
490
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500491 cancel_escape:
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000492 switch (utf8.byte[0]) {
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500493 case '\r':
494 terminal->column = 0;
495 break;
496 case '\n':
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500497 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500498 if (terminal->row + 1 < terminal->height) {
499 terminal->row++;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500500 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500501 terminal->start++;
502 if (terminal->start == terminal->height)
503 terminal->start = 0;
504 memset(terminal_get_row(terminal, terminal->row),
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000505 0, terminal->width * sizeof(union utf8_char));
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500506 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500507
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500508 break;
509 case '\t':
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000510 memset(&row[terminal->column], ' ', (-terminal->column & 7) * sizeof(union utf8_char));
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500511 terminal->column = (terminal->column + 7) & ~7;
512 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500513 case '\e':
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500514 terminal->state = STATE_ESCAPE;
515 terminal->escape[0] = '\e';
516 terminal->escape_length = 1;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500517 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500518 case '\b':
519 if (terminal->column > 0)
520 terminal->column--;
521 break;
522 case '\a':
523 /* Bell */
524 break;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500525 default:
526 if (terminal->column < terminal->width)
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000527 if (utf8.byte[0] < 32) utf8.byte[0] += 64;
528 row[terminal->column++] = utf8;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500529 break;
530 }
531 }
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500532
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400533 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500534}
535
536static void
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400537key_handler(struct window *window, uint32_t key, uint32_t sym,
Kristian Høgsberg55444912009-02-21 14:31:09 -0500538 uint32_t state, uint32_t modifiers, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500539{
540 struct terminal *terminal = data;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400541 char ch[2];
542 int len = 0;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500543
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400544 switch (sym) {
545 case XK_F11:
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500546 if (!state)
547 break;
548 terminal->fullscreen ^= 1;
549 window_set_fullscreen(window, terminal->fullscreen);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400550 window_schedule_redraw(terminal->window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500551 break;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400552
553 case XK_Delete:
554 sym = 0x04;
555 case XK_BackSpace:
556 case XK_Tab:
557 case XK_Linefeed:
558 case XK_Clear:
559 case XK_Return:
560 case XK_Pause:
561 case XK_Scroll_Lock:
562 case XK_Sys_Req:
563 case XK_Escape:
564 ch[len++] = sym & 0x7f;
565 break;
566
567 case XK_Shift_L:
568 case XK_Shift_R:
569 case XK_Control_L:
570 case XK_Control_R:
571 case XK_Alt_L:
572 case XK_Alt_R:
573 break;
574
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500575 default:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400576 if (modifiers & WINDOW_MODIFIER_CONTROL)
577 sym = sym & 0x1f;
578 else if (modifiers & WINDOW_MODIFIER_ALT)
579 ch[len++] = 0x1b;
580 if (sym < 256)
581 ch[len++] = sym;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500582 break;
583 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400584
585 if (state && len > 0)
586 write(terminal->master, ch, len);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500587}
588
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500589static void
590keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -0400591 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500592{
593 struct terminal *terminal = data;
594
595 terminal->focused = (device != NULL);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400596 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500597}
598
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500599static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500600terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500601{
602 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400603 cairo_surface_t *surface;
604 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500605
606 terminal = malloc(sizeof *terminal);
607 if (terminal == NULL)
608 return terminal;
609
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500610 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500611 terminal->fullscreen = fullscreen;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400612 terminal->color_scheme = &jbarnes_colors;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500613 terminal->window = window_create(display, "Wayland Terminal",
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500614 500, 400);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000615
616 init_state_machine(&terminal->state_machine);
617
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500618 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500619 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500620
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500621 window_set_fullscreen(terminal->window, terminal->fullscreen);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400622 window_set_user_data(terminal->window, terminal);
623 window_set_redraw_handler(terminal->window, redraw_handler);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500624
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400625 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500626 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400627 keyboard_focus_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500628
Kristian Høgsberg09531622010-06-14 23:22:15 -0400629 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
630 cr = cairo_create(surface);
631 cairo_select_font_face (cr, "mono",
632 CAIRO_FONT_SLANT_NORMAL,
633 CAIRO_FONT_WEIGHT_NORMAL);
634 cairo_set_font_size(cr, 14);
635 cairo_font_extents(cr, &terminal->extents);
636 cairo_destroy(cr);
637 cairo_surface_destroy(surface);
638
Kristian Høgsberg22106762008-12-08 13:50:07 -0500639 terminal_draw(terminal);
640
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500641 return terminal;
642}
643
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500644static gboolean
645io_handler(GIOChannel *source,
646 GIOCondition condition,
647 gpointer data)
648{
649 struct terminal *terminal = data;
650 gchar buffer[256];
651 gsize bytes_read;
652 GError *error = NULL;
653
654 g_io_channel_read_chars(source, buffer, sizeof buffer,
655 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500656
657 terminal_data(terminal, buffer, bytes_read);
658
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500659 return TRUE;
660}
661
662static int
663terminal_run(struct terminal *terminal, const char *path)
664{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500665 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500666 pid_t pid;
667
668 pid = forkpty(&master, NULL, NULL, NULL);
669 if (pid == 0) {
Kristian Høgsbergc8c5d582008-12-18 14:50:08 -0500670 setenv("TERM", "vt100", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500671 if (execl(path, path, NULL)) {
672 printf("exec failed: %m\n");
673 exit(EXIT_FAILURE);
674 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500675 } else if (pid < 0) {
676 fprintf(stderr, "failed to fork and create pty (%m).\n");
677 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500678 }
679
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500680 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500681 terminal->channel = g_io_channel_unix_new(master);
682 fcntl(master, F_SETFL, O_NONBLOCK);
683 g_io_add_watch(terminal->channel, G_IO_IN,
684 io_handler, terminal);
685
686 return 0;
687}
688
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500689static const GOptionEntry option_entries[] = {
690 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
691 &option_fullscreen, "Run in fullscreen mode" },
692 { NULL }
693};
694
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500695int main(int argc, char *argv[])
696{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500697 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500698 struct terminal *terminal;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500699
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400700 d = display_create(&argc, &argv, option_entries);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200701 if (d == NULL) {
702 fprintf(stderr, "failed to create display: %m\n");
703 return -1;
704 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500705
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500706 terminal = terminal_create(d, option_fullscreen);
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500707 if (terminal_run(terminal, "/bin/bash"))
708 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500709
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400710 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500711
712 return 0;
713}