blob: 686496735917c05090b62fcbcfc51a4824137a27 [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
54
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000055union utf8_char {
56 unsigned char byte[4];
57 uint32_t ch;
58};
59
60enum utf8_state {
61 utf8state_start,
62 utf8state_accept,
63 utf8state_reject,
64 utf8state_expect3,
65 utf8state_expect2,
66 utf8state_expect1
67};
68
69struct utf8_state_machine {
70 enum utf8_state state;
71 int len;
72 union utf8_char s;
73};
74
75static void
76init_state_machine(struct utf8_state_machine *machine)
77{
78 machine->state = utf8state_start;
79 machine->len = 0;
80 machine->s.ch = 0;
81}
82
83static enum utf8_state
84utf8_next_char(struct utf8_state_machine *machine, char c)
85{
86 switch(machine->state) {
87 case utf8state_start:
88 case utf8state_accept:
89 case utf8state_reject:
90 machine->s.ch = 0;
91 machine->len = 0;
92 if(c == 0xC0 || c == 0xC1) {
93 /* overlong encoding, reject */
94 machine->state = utf8state_reject;
95 } else if((c & 0x80) == 0) {
96 /* single byte, accept */
97 machine->s.byte[machine->len++] = c;
98 machine->state = utf8state_accept;
99 } else if((c & 0xC0) == 0x80) {
100 /* parser out of sync, ignore byte */
101 machine->state = utf8state_start;
102 } else if((c & 0xE0) == 0xC0) {
103 /* start of two byte sequence */
104 machine->s.byte[machine->len++] = c;
105 machine->state = utf8state_expect1;
106 } else if((c & 0xF0) == 0xE0) {
107 /* start of three byte sequence */
108 machine->s.byte[machine->len++] = c;
109 machine->state = utf8state_expect2;
110 } else if((c & 0xF8) == 0xF0) {
111 /* start of four byte sequence */
112 machine->s.byte[machine->len++] = c;
113 machine->state = utf8state_expect3;
114 } else {
115 /* overlong encoding, reject */
116 machine->state = utf8state_reject;
117 }
118 break;
119 case utf8state_expect3:
120 machine->s.byte[machine->len++] = c;
121 if((c & 0xC0) == 0x80) {
122 /* all good, continue */
123 machine->state = utf8state_expect2;
124 } else {
125 /* missing extra byte, reject */
126 machine->state = utf8state_reject;
127 }
128 break;
129 case utf8state_expect2:
130 machine->s.byte[machine->len++] = c;
131 if((c & 0xC0) == 0x80) {
132 /* all good, continue */
133 machine->state = utf8state_expect1;
134 } else {
135 /* missing extra byte, reject */
136 machine->state = utf8state_reject;
137 }
138 break;
139 case utf8state_expect1:
140 machine->s.byte[machine->len++] = c;
141 if((c & 0xC0) == 0x80) {
142 /* all good, accept */
143 machine->state = utf8state_accept;
144 } else {
145 /* missing extra byte, reject */
146 machine->state = utf8state_reject;
147 }
148 break;
149 default:
150 machine->state = utf8state_reject;
151 break;
152 }
153
154 return machine->state;
155}
156
Callum Lowcay30eeae52011-01-07 19:46:55 +0000157struct terminal_color { double r, g, b, a; };
158struct attr {
159 unsigned char fg, bg;
160 char a; /* attributes format:
161 * 76543210
162 * ilub */
163 char r; /* reserved */
164};
165struct color_scheme {
166 struct terminal_color palette[16];
167 struct terminal_color border;
168 struct attr default_attr;
169};
170
171static void
172attr_init(struct attr *data_attr, struct attr attr, int n)
173{
174 int i;
175 for (i = 0; i < n; i++) {
176 data_attr[i] = attr;
177 }
178}
179
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500180struct terminal {
181 struct window *window;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500182 struct display *display;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000183 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000184 struct attr *data_attr;
185 struct attr curr_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000186 union utf8_char last_char;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000187 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500188 int width, height, start, row, column;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500189 int fd, master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500190 GIOChannel *channel;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500191 uint32_t modifiers;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500192 char escape[64];
193 int escape_length;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500194 int state;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000195 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500196 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500197 int fullscreen;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500198 int focused;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400199 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000200 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400201 cairo_font_extents_t extents;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000202 cairo_font_face_t *font_normal, *font_bold;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500203};
204
Callum Lowcay30eeae52011-01-07 19:46:55 +0000205static void
206terminal_init(struct terminal *terminal)
207{
208 terminal->curr_attr = terminal->color_scheme->default_attr;
209
210 terminal->row = 0;
211 terminal->column = 0;
212}
213
214static void
215init_color_table(struct terminal *terminal)
216{
217 int c, r;
218 struct terminal_color *color_table = terminal->color_table;
219
220 for (c = 0; c < 256; c ++) {
221 if (c < 16) {
222 color_table[c] = terminal->color_scheme->palette[c];
223 } else if (c < 232) {
224 r = c - 16;
225 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
226 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
227 color_table[c].r = ((double)(r % 6) / 6.0);
228 color_table[c].a = 1.0;
229 } else {
230 r = (c - 232) * 10 + 8;
231 color_table[c].r = ((double) r) / 256.0;
232 color_table[c].g = color_table[c].r;
233 color_table[c].b = color_table[c].r;
234 color_table[c].a = 1.0;
235 }
236 }
237}
238
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000239static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500240terminal_get_row(struct terminal *terminal, int row)
241{
242 int index;
243
244 index = (row + terminal->start) % terminal->height;
245
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000246 return &terminal->data[index * terminal->width];
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500247}
248
Callum Lowcay30eeae52011-01-07 19:46:55 +0000249static struct attr*
250terminal_get_attr_row(struct terminal *terminal, int row) {
251 int index;
252
253 index = (row + terminal->start) % terminal->height;
254
255 return &terminal->data_attr[index * terminal->width];
256}
257
258static struct attr
259terminal_get_attr(struct terminal *terminal, int row, int col) {
260 return terminal_get_attr_row(terminal, row)[col];
261}
262
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500263static void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500264terminal_resize(struct terminal *terminal, int width, int height)
265{
266 size_t size;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000267 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000268 struct attr *data_attr;
269 int data_pitch, attr_pitch;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500270 int i, l, total_rows, start;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500271
272 if (terminal->width == width && terminal->height == height)
273 return;
274
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000275 data_pitch = width * sizeof(union utf8_char);
276 size = data_pitch * height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500277 data = malloc(size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000278 attr_pitch = width * sizeof(struct attr);
279 data_attr = malloc(attr_pitch * height);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500280 memset(data, 0, size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000281 attr_init(data_attr, terminal->curr_attr, width * height);
282 if (terminal->data && terminal->data_attr) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500283 if (width > terminal->width)
284 l = terminal->width;
285 else
286 l = width;
287
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500288 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500289 total_rows = height;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500290 start = terminal->height - height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500291 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500292 total_rows = terminal->height;
293 start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500294 }
295
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000296 for (i = 0; i < total_rows; i++) {
297 memcpy(&data[width * i],
298 terminal_get_row(terminal, i),
299 l * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000300 memcpy(&data_attr[width * i],
301 terminal_get_attr_row(terminal, i),
302 l * sizeof(struct attr));
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000303 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500304
305 free(terminal->data);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000306 free(terminal->data_attr);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500307 }
308
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000309 terminal->data_pitch = data_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000310 terminal->attr_pitch = attr_pitch;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500311 terminal->width = width;
312 terminal->height = height;
313 terminal->data = data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000314 terminal->data_attr = data_attr;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500315
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500316 if (terminal->row >= terminal->height)
317 terminal->row = terminal->height - 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500318 if (terminal->column >= terminal->width)
319 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500320 terminal->start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500321}
322
Callum Lowcay30eeae52011-01-07 19:46:55 +0000323struct color_scheme DEFAULT_COLORS = {
324 {
325 {0, 0, 0, 1}, /* black */
326 {0.66, 0, 0, 1}, /* red */
327 {0 , 0.66, 0, 1}, /* green */
328 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
329 {0 , 0 , 0.66, 1}, /* blue */
330 {0.66, 0 , 0.66, 1}, /* magenta */
331 {0, 0.66, 0.66, 1}, /* cyan */
332 {0.66, 0.66, 0.66, 1}, /* light grey */
333 {0.22, 0.33, 0.33, 1}, /* dark grey */
334 {1, 0.33, 0.33, 1}, /* high red */
335 {0.33, 1, 0.33, 1}, /* high green */
336 {1, 1, 0.33, 1}, /* high yellow */
337 {0.33, 0.33, 1, 1}, /* high blue */
338 {1, 0.33, 1, 1}, /* high magenta */
339 {0.33, 1, 1, 1}, /* high cyan */
340 {1, 1, 1, 1} /* white */
341 },
342 {0, 0, 0, 1}, /* black border */
343 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
344};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400345
Kristian Høgsberg22106762008-12-08 13:50:07 -0500346static void
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500347terminal_draw_contents(struct terminal *terminal)
348{
349 struct rectangle rectangle;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500350 cairo_t *cr;
351 cairo_font_extents_t extents;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000352 int top_margin, side_margin;
353 int row, col;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000354 struct attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000355 union utf8_char *p_row;
356 struct utf8_chars {
357 union utf8_char c;
358 char null;
359 } toShow;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000360 int foreground, background, bold, underline;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000361 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500362 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500363 double d;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500364
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000365 toShow.null = 0;
366
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500367 window_get_child_rectangle(terminal->window, &rectangle);
368
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400369 surface = display_create_surface(terminal->display, &rectangle);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500370 cr = cairo_create(surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500371 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400372 cairo_set_source_rgba(cr,
Callum Lowcay30eeae52011-01-07 19:46:55 +0000373 terminal->color_scheme->border.r,
374 terminal->color_scheme->border.g,
375 terminal->color_scheme->border.b,
376 terminal->color_scheme->border.a);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500377 cairo_paint(cr);
378 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500379
Callum Lowcay30eeae52011-01-07 19:46:55 +0000380 cairo_set_font_face(cr, terminal->font_normal);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500381 cairo_set_font_size(cr, 14);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500382
383 cairo_font_extents(cr, &extents);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500384 side_margin = (rectangle.width - terminal->width * extents.max_x_advance) / 2;
385 top_margin = (rectangle.height - terminal->height * extents.height) / 2;
386
Callum Lowcay30eeae52011-01-07 19:46:55 +0000387 cairo_set_line_width(cr, 1.0);
388
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000389 for (row = 0; row < terminal->height; row++) {
390 p_row = terminal_get_row(terminal, row);
391 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000392 /* get the attributes for this character cell */
393 attr = terminal_get_attr(terminal, row, col);
394 if ((attr.a & ATTRMASK_INVERSE) ||
395 (terminal->focused &&
396 terminal->row == row && terminal->column == col))
397 {
398 foreground = attr.bg;
399 background = attr.fg;
400 } else {
401 foreground = attr.fg;
402 background = attr.bg;
403 }
404 bold = attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK);
405 underline = attr.a & ATTRMASK_UNDERLINE;
406
407 /* paint the background */
408 cairo_set_source_rgba(cr,
409 terminal->color_table[background].r,
410 terminal->color_table[background].g,
411 terminal->color_table[background].b,
412 terminal->color_table[background].a);
413 cairo_move_to(cr, side_margin + (col * extents.max_x_advance),
414 top_margin + (row * extents.height));
415 cairo_rel_line_to(cr, extents.max_x_advance, 0);
416 cairo_rel_line_to(cr, 0, extents.height);
417 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
418 cairo_close_path(cr);
419 cairo_fill(cr);
420
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000421 /* paint the foreground */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000422 if (bold)
423 cairo_set_font_face(cr, terminal->font_bold);
424 else
425 cairo_set_font_face(cr, terminal->font_normal);
426 cairo_set_source_rgba(cr,
427 terminal->color_table[foreground].r,
428 terminal->color_table[foreground].g,
429 terminal->color_table[foreground].b,
430 terminal->color_table[foreground].a);
431
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000432 text_x = side_margin + col * extents.max_x_advance;
433 text_y = top_margin + extents.ascent + row * extents.height;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000434 if (underline) {
435 cairo_move_to(cr, text_x, text_y + 2);
436 cairo_line_to(cr, text_x + extents.max_x_advance, text_y + 2);
437 cairo_stroke(cr);
438 }
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000439 cairo_move_to(cr, text_x, text_y);
440
441 toShow.c = p_row[col];
442 cairo_show_text(cr, (char *) toShow.c.byte);
443 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500444 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500445
Callum Lowcay30eeae52011-01-07 19:46:55 +0000446 if (!terminal->focused) {
447 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500448
Callum Lowcay30eeae52011-01-07 19:46:55 +0000449 cairo_set_line_width(cr, 1);
450 cairo_move_to(cr, side_margin + terminal->column * extents.max_x_advance + d,
451 top_margin + terminal->row * extents.height + d);
452 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
453 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
454 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
455 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500456
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500457 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000458 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500459
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500460 cairo_destroy(cr);
461
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500462 window_copy_surface(terminal->window,
463 &rectangle,
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500464 surface);
465
466 cairo_surface_destroy(surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500467}
468
469static void
470terminal_draw(struct terminal *terminal)
471{
Kristian Høgsberg22106762008-12-08 13:50:07 -0500472 struct rectangle rectangle;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500473 int32_t width, height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500474
475 window_get_child_rectangle(terminal->window, &rectangle);
476
Kristian Høgsberg09531622010-06-14 23:22:15 -0400477 width = (rectangle.width - 2 * terminal->margin) /
478 (int32_t) terminal->extents.max_x_advance;
479 height = (rectangle.height - 2 * terminal->margin) /
480 (int32_t) terminal->extents.height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500481 terminal_resize(terminal, width, height);
482
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500483 if (!terminal->fullscreen) {
Kristian Høgsberg09531622010-06-14 23:22:15 -0400484 rectangle.width = terminal->width *
485 terminal->extents.max_x_advance + 2 * terminal->margin;
486 rectangle.height = terminal->height *
487 terminal->extents.height + 2 * terminal->margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500488 window_set_child_size(terminal->window, &rectangle);
489 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500490
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500491 window_draw(terminal->window);
492 terminal_draw_contents(terminal);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400493 window_flush(terminal->window);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500494}
495
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400496static void
497redraw_handler(struct window *window, void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500498{
499 struct terminal *terminal = data;
500
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500501 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500502}
503
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500504#define STATE_NORMAL 0
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500505#define STATE_ESCAPE 1
506
507static void
508terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500509
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500510static void
Callum Lowcay30eeae52011-01-07 19:46:55 +0000511handle_sgr(struct terminal *terminal, int code);
512
513static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500514handle_escape(struct terminal *terminal)
515{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000516 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000517 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000518 char *p;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500519 int i, count;
520 int args[10], set[10] = { 0, };
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500521
522 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500523 i = 0;
524 p = &terminal->escape[2];
525 while ((isdigit(*p) || *p == ';') && i < 10) {
526 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000527 if (!set[i]) {
528 args[i] = 0;
529 set[i] = 1;
530 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500531 p++;
532 i++;
533 } else {
534 args[i] = strtol(p, &p, 10);
535 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500536 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500537 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500538
539 switch (*p) {
540 case 'A':
541 count = set[0] ? args[0] : 1;
542 if (terminal->row - count >= 0)
543 terminal->row -= count;
544 else
545 terminal->row = 0;
546 break;
547 case 'B':
548 count = set[0] ? args[0] : 1;
549 if (terminal->row + count < terminal->height)
550 terminal->row += count;
551 else
552 terminal->row = terminal->height;
553 break;
554 case 'C':
555 count = set[0] ? args[0] : 1;
556 if (terminal->column + count < terminal->width)
557 terminal->column += count;
558 else
559 terminal->column = terminal->width;
560 break;
561 case 'D':
562 count = set[0] ? args[0] : 1;
563 if (terminal->column - count >= 0)
564 terminal->column -= count;
565 else
566 terminal->column = 0;
567 break;
568 case 'J':
569 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000570 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000571 memset(&row[terminal->column], 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000572 attr_init(&attr_row[terminal->column], terminal->curr_attr, (terminal->width - terminal->column));
573 for (i = terminal->row + 1; i < terminal->height; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000574 memset(terminal_get_row(terminal, i), 0, terminal->width * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000575 attr_init(terminal_get_attr_row(terminal, i), terminal->curr_attr, terminal->width);
576 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500577 break;
578 case 'G':
579 if (set[0])
580 terminal->column = args[0] - 1;
581 break;
582 case 'H':
583 case 'f':
584 terminal->row = set[0] ? args[0] - 1 : 0;
585 terminal->column = set[1] ? args[1] - 1 : 0;
586 break;
587 case 'K':
588 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000589 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000590 memset(&row[terminal->column], 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000591 attr_init(&attr_row[terminal->column], terminal->curr_attr, (terminal->width - terminal->column));
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500592 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000593 case 'm': /* SGR */
594 if (set[0] && set[1] && set[2] && args[1] == 5) {
595 if (args[0] == 38) {
596 handle_sgr(terminal, args[2] + 256);
597 break;
598 } else if (args[0] == 48) {
599 handle_sgr(terminal, args[2] + 512);
600 break;
601 }
602 }
603 for(i = 0; i < 10; i++) {
604 if(set[i]) {
605 handle_sgr(terminal, args[i]);
606 } else if(i == 0) {
607 handle_sgr(terminal, 0);
608 break;
609 } else {
610 break;
611 }
612 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500613 break;
614 case '?':
615 if (strcmp(p, "?25l") == 0) {
616 /* hide cursor */
617 } else if (strcmp(p, "?25h") == 0) {
618 /* show cursor */
619 }
620 break;
621 default:
622 terminal_data(terminal,
623 terminal->escape + 1,
624 terminal->escape_length - 2);
625 break;
626 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500627}
628
629static void
Callum Lowcay30eeae52011-01-07 19:46:55 +0000630handle_sgr(struct terminal *terminal, int code)
631{
632 switch(code) {
633 case 0:
634 terminal->curr_attr = terminal->color_scheme->default_attr;
635 break;
636 case 1:
637 terminal->curr_attr.a |= ATTRMASK_BOLD;
638 if (terminal->curr_attr.fg < 8)
639 terminal->curr_attr.fg += 8;
640 break;
641 case 4:
642 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
643 break;
644 case 5:
645 terminal->curr_attr.a |= ATTRMASK_BLINK;
646 break;
647 case 2:
648 case 21:
649 case 22:
650 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
651 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
652 terminal->curr_attr.fg -= 8;
653 break;
654 case 24:
655 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
656 break;
657 case 25:
658 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
659 break;
660 case 7:
661 case 26:
662 terminal->curr_attr.a |= ATTRMASK_INVERSE;
663 break;
664 case 27:
665 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
666 break;
667 case 39:
668 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
669 break;
670 case 49:
671 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
672 break;
673 default:
674 if(code >= 30 && code <= 37) {
675 terminal->curr_attr.fg = code - 30;
676 if (terminal->curr_attr.a & ATTRMASK_BOLD)
677 terminal->curr_attr.fg += 8;
678 } else if(code >= 40 && code <= 47) {
679 terminal->curr_attr.bg = code - 40;
680 } else if(code >= 256 && code < 512) {
681 terminal->curr_attr.fg = code - 256;
682 } else if(code >= 512 && code < 768) {
683 terminal->curr_attr.bg = code - 512;
684 } else {
685 fprintf(stderr, "Unknown SGR code: %d\n", code);
686 }
687 break;
688 }
689}
690
691static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500692terminal_data(struct terminal *terminal, const char *data, size_t length)
693{
694 int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000695 union utf8_char utf8;
696 enum utf8_state parser_state;
697 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000698 struct attr *attr_row;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500699
700 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000701 parser_state =
702 utf8_next_char(&terminal->state_machine, data[i]);
703 switch(parser_state) {
704 case utf8state_accept:
705 utf8.ch = terminal->state_machine.s.ch;
706 break;
707 case utf8state_reject:
708 /* the unicode replacement character */
709 utf8.byte[0] = 0xEF;
710 utf8.byte[1] = 0xBF;
711 utf8.byte[2] = 0xBD;
712 utf8.byte[3] = 0x00;
713 break;
714 default:
715 continue;
716 }
717
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500718 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000719 attr_row = terminal_get_attr_row(terminal, terminal->row);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500720
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500721 if (terminal->state == STATE_ESCAPE) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000722 terminal->escape[terminal->escape_length++] = utf8.byte[0];
723 if (terminal->escape_length == 2 && utf8.byte[0] != '[') {
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500724 /* Bad escape sequence. */
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500725 terminal->state = STATE_NORMAL;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500726 goto cancel_escape;
727 }
728
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000729 if (isalpha(utf8.byte[0])) {
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500730 terminal->state = STATE_NORMAL;
731 handle_escape(terminal);
732 }
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500733 continue;
734 }
735
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500736 cancel_escape:
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000737 switch (utf8.byte[0]) {
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500738 case '\r':
739 terminal->column = 0;
740 break;
741 case '\n':
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500742 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500743 if (terminal->row + 1 < terminal->height) {
744 terminal->row++;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500745 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500746 terminal->start++;
747 if (terminal->start == terminal->height)
748 terminal->start = 0;
749 memset(terminal_get_row(terminal, terminal->row),
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000750 0, terminal->width * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000751 attr_init(terminal_get_attr_row(terminal, terminal->row),
752 terminal->curr_attr, terminal->width);
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500753 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500754
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500755 break;
756 case '\t':
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000757 memset(&row[terminal->column], ' ', (-terminal->column & 7) * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000758 attr_init(&attr_row[terminal->column], terminal->curr_attr, -terminal->column & 7);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500759 terminal->column = (terminal->column + 7) & ~7;
760 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500761 case '\e':
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500762 terminal->state = STATE_ESCAPE;
763 terminal->escape[0] = '\e';
764 terminal->escape_length = 1;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500765 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500766 case '\b':
767 if (terminal->column > 0)
768 terminal->column--;
769 break;
770 case '\a':
771 /* Bell */
772 break;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500773 default:
774 if (terminal->column < terminal->width)
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000775 if (utf8.byte[0] < 32) utf8.byte[0] += 64;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000776 row[terminal->column] = utf8;
777 attr_row[terminal->column++] = terminal->curr_attr;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500778 break;
779 }
780 }
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500781
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400782 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500783}
784
785static void
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400786key_handler(struct window *window, uint32_t key, uint32_t sym,
Kristian Høgsberg55444912009-02-21 14:31:09 -0500787 uint32_t state, uint32_t modifiers, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500788{
789 struct terminal *terminal = data;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400790 char ch[2];
791 int len = 0;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500792
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400793 switch (sym) {
794 case XK_F11:
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500795 if (!state)
796 break;
797 terminal->fullscreen ^= 1;
798 window_set_fullscreen(window, terminal->fullscreen);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400799 window_schedule_redraw(terminal->window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500800 break;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400801
802 case XK_Delete:
803 sym = 0x04;
804 case XK_BackSpace:
805 case XK_Tab:
806 case XK_Linefeed:
807 case XK_Clear:
808 case XK_Return:
809 case XK_Pause:
810 case XK_Scroll_Lock:
811 case XK_Sys_Req:
812 case XK_Escape:
813 ch[len++] = sym & 0x7f;
814 break;
815
816 case XK_Shift_L:
817 case XK_Shift_R:
818 case XK_Control_L:
819 case XK_Control_R:
820 case XK_Alt_L:
821 case XK_Alt_R:
822 break;
823
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500824 default:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400825 if (modifiers & WINDOW_MODIFIER_CONTROL)
826 sym = sym & 0x1f;
827 else if (modifiers & WINDOW_MODIFIER_ALT)
828 ch[len++] = 0x1b;
829 if (sym < 256)
830 ch[len++] = sym;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500831 break;
832 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400833
834 if (state && len > 0)
835 write(terminal->master, ch, len);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500836}
837
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500838static void
839keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -0400840 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500841{
842 struct terminal *terminal = data;
843
844 terminal->focused = (device != NULL);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400845 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500846}
847
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500848static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500849terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500850{
851 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400852 cairo_surface_t *surface;
853 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500854
855 terminal = malloc(sizeof *terminal);
856 if (terminal == NULL)
857 return terminal;
858
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500859 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500860 terminal->fullscreen = fullscreen;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000861 terminal->color_scheme = &DEFAULT_COLORS;
862 terminal_init(terminal);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500863 terminal->window = window_create(display, "Wayland Terminal",
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500864 500, 400);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000865
866 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000867 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000868
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500869 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500870 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500871
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500872 window_set_fullscreen(terminal->window, terminal->fullscreen);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400873 window_set_user_data(terminal->window, terminal);
874 window_set_redraw_handler(terminal->window, redraw_handler);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500875
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400876 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500877 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400878 keyboard_focus_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500879
Kristian Høgsberg09531622010-06-14 23:22:15 -0400880 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
881 cr = cairo_create(surface);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000882 terminal->font_bold = cairo_toy_font_face_create ("mono",
883 CAIRO_FONT_SLANT_NORMAL,
884 CAIRO_FONT_WEIGHT_BOLD);
885 cairo_font_face_reference(terminal->font_bold);
886 terminal->font_normal = cairo_toy_font_face_create ("mono",
887 CAIRO_FONT_SLANT_NORMAL,
888 CAIRO_FONT_WEIGHT_NORMAL);
889 cairo_font_face_reference(terminal->font_normal);
890 cairo_set_font_face(cr, terminal->font_normal);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400891 cairo_set_font_size(cr, 14);
892 cairo_font_extents(cr, &terminal->extents);
893 cairo_destroy(cr);
894 cairo_surface_destroy(surface);
895
Kristian Høgsberg22106762008-12-08 13:50:07 -0500896 terminal_draw(terminal);
897
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500898 return terminal;
899}
900
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500901static gboolean
902io_handler(GIOChannel *source,
903 GIOCondition condition,
904 gpointer data)
905{
906 struct terminal *terminal = data;
907 gchar buffer[256];
908 gsize bytes_read;
909 GError *error = NULL;
910
911 g_io_channel_read_chars(source, buffer, sizeof buffer,
912 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500913
914 terminal_data(terminal, buffer, bytes_read);
915
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500916 return TRUE;
917}
918
919static int
920terminal_run(struct terminal *terminal, const char *path)
921{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500922 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500923 pid_t pid;
924
925 pid = forkpty(&master, NULL, NULL, NULL);
926 if (pid == 0) {
Kristian Høgsbergc8c5d582008-12-18 14:50:08 -0500927 setenv("TERM", "vt100", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500928 if (execl(path, path, NULL)) {
929 printf("exec failed: %m\n");
930 exit(EXIT_FAILURE);
931 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500932 } else if (pid < 0) {
933 fprintf(stderr, "failed to fork and create pty (%m).\n");
934 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500935 }
936
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500937 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500938 terminal->channel = g_io_channel_unix_new(master);
939 fcntl(master, F_SETFL, O_NONBLOCK);
940 g_io_add_watch(terminal->channel, G_IO_IN,
941 io_handler, terminal);
942
943 return 0;
944}
945
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500946static const GOptionEntry option_entries[] = {
947 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
948 &option_fullscreen, "Run in fullscreen mode" },
949 { NULL }
950};
951
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500952int main(int argc, char *argv[])
953{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500954 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500955 struct terminal *terminal;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500956
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400957 d = display_create(&argc, &argv, option_entries);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200958 if (d == NULL) {
959 fprintf(stderr, "failed to create display: %m\n");
960 return -1;
961 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500962
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500963 terminal = terminal_create(d, option_fullscreen);
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500964 if (terminal_run(terminal, "/bin/bash"))
965 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500966
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400967 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500968
969 return 0;
970}