blob: 26f8c9daf625098e10c8e9223ea3875d5c7f880c [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 Lowcayb8609ad2011-01-07 19:46:57 +000055/* Buffer sizes */
56#define MAX_RESPONSE 11
57#define MAX_ESCAPE 64
58
Callum Lowcay15bdc5d2011-01-07 19:46:54 +000059union utf8_char {
60 unsigned char byte[4];
61 uint32_t ch;
62};
63
64enum utf8_state {
65 utf8state_start,
66 utf8state_accept,
67 utf8state_reject,
68 utf8state_expect3,
69 utf8state_expect2,
70 utf8state_expect1
71};
72
73struct utf8_state_machine {
74 enum utf8_state state;
75 int len;
76 union utf8_char s;
77};
78
79static void
80init_state_machine(struct utf8_state_machine *machine)
81{
82 machine->state = utf8state_start;
83 machine->len = 0;
84 machine->s.ch = 0;
85}
86
87static enum utf8_state
88utf8_next_char(struct utf8_state_machine *machine, char c)
89{
90 switch(machine->state) {
91 case utf8state_start:
92 case utf8state_accept:
93 case utf8state_reject:
94 machine->s.ch = 0;
95 machine->len = 0;
96 if(c == 0xC0 || c == 0xC1) {
97 /* overlong encoding, reject */
98 machine->state = utf8state_reject;
99 } else if((c & 0x80) == 0) {
100 /* single byte, accept */
101 machine->s.byte[machine->len++] = c;
102 machine->state = utf8state_accept;
103 } else if((c & 0xC0) == 0x80) {
104 /* parser out of sync, ignore byte */
105 machine->state = utf8state_start;
106 } else if((c & 0xE0) == 0xC0) {
107 /* start of two byte sequence */
108 machine->s.byte[machine->len++] = c;
109 machine->state = utf8state_expect1;
110 } else if((c & 0xF0) == 0xE0) {
111 /* start of three byte sequence */
112 machine->s.byte[machine->len++] = c;
113 machine->state = utf8state_expect2;
114 } else if((c & 0xF8) == 0xF0) {
115 /* start of four byte sequence */
116 machine->s.byte[machine->len++] = c;
117 machine->state = utf8state_expect3;
118 } else {
119 /* overlong encoding, reject */
120 machine->state = utf8state_reject;
121 }
122 break;
123 case utf8state_expect3:
124 machine->s.byte[machine->len++] = c;
125 if((c & 0xC0) == 0x80) {
126 /* all good, continue */
127 machine->state = utf8state_expect2;
128 } else {
129 /* missing extra byte, reject */
130 machine->state = utf8state_reject;
131 }
132 break;
133 case utf8state_expect2:
134 machine->s.byte[machine->len++] = c;
135 if((c & 0xC0) == 0x80) {
136 /* all good, continue */
137 machine->state = utf8state_expect1;
138 } else {
139 /* missing extra byte, reject */
140 machine->state = utf8state_reject;
141 }
142 break;
143 case utf8state_expect1:
144 machine->s.byte[machine->len++] = c;
145 if((c & 0xC0) == 0x80) {
146 /* all good, accept */
147 machine->state = utf8state_accept;
148 } else {
149 /* missing extra byte, reject */
150 machine->state = utf8state_reject;
151 }
152 break;
153 default:
154 machine->state = utf8state_reject;
155 break;
156 }
157
158 return machine->state;
159}
160
Callum Lowcay30eeae52011-01-07 19:46:55 +0000161struct terminal_color { double r, g, b, a; };
162struct attr {
163 unsigned char fg, bg;
164 char a; /* attributes format:
165 * 76543210
166 * ilub */
167 char r; /* reserved */
168};
169struct color_scheme {
170 struct terminal_color palette[16];
171 struct terminal_color border;
172 struct attr default_attr;
173};
174
175static void
176attr_init(struct attr *data_attr, struct attr attr, int n)
177{
178 int i;
179 for (i = 0; i < n; i++) {
180 data_attr[i] = attr;
181 }
182}
183
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500184struct terminal {
185 struct window *window;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500186 struct display *display;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000187 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000188 struct attr *data_attr;
189 struct attr curr_attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000190 union utf8_char last_char;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000191 int data_pitch, attr_pitch; /* The width in bytes of a line */
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500192 int width, height, start, row, column;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500193 int fd, master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500194 GIOChannel *channel;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500195 uint32_t modifiers;
Callum Lowcayb8609ad2011-01-07 19:46:57 +0000196 char escape[MAX_ESCAPE];
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500197 int escape_length;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500198 int state;
Callum Lowcayb8609ad2011-01-07 19:46:57 +0000199 int qmark_flag;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000200 struct utf8_state_machine state_machine;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500201 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500202 int fullscreen;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500203 int focused;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400204 struct color_scheme *color_scheme;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000205 struct terminal_color color_table[256];
Kristian Høgsberg09531622010-06-14 23:22:15 -0400206 cairo_font_extents_t extents;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000207 cairo_font_face_t *font_normal, *font_bold;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500208};
209
Callum Lowcay30eeae52011-01-07 19:46:55 +0000210static void
211terminal_init(struct terminal *terminal)
212{
213 terminal->curr_attr = terminal->color_scheme->default_attr;
214
215 terminal->row = 0;
216 terminal->column = 0;
217}
218
219static void
220init_color_table(struct terminal *terminal)
221{
222 int c, r;
223 struct terminal_color *color_table = terminal->color_table;
224
225 for (c = 0; c < 256; c ++) {
226 if (c < 16) {
227 color_table[c] = terminal->color_scheme->palette[c];
228 } else if (c < 232) {
229 r = c - 16;
230 color_table[c].b = ((double)(r % 6) / 6.0); r /= 6;
231 color_table[c].g = ((double)(r % 6) / 6.0); r /= 6;
232 color_table[c].r = ((double)(r % 6) / 6.0);
233 color_table[c].a = 1.0;
234 } else {
235 r = (c - 232) * 10 + 8;
236 color_table[c].r = ((double) r) / 256.0;
237 color_table[c].g = color_table[c].r;
238 color_table[c].b = color_table[c].r;
239 color_table[c].a = 1.0;
240 }
241 }
242}
243
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000244static union utf8_char *
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500245terminal_get_row(struct terminal *terminal, int row)
246{
247 int index;
248
249 index = (row + terminal->start) % terminal->height;
250
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000251 return &terminal->data[index * terminal->width];
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500252}
253
Callum Lowcay30eeae52011-01-07 19:46:55 +0000254static struct attr*
255terminal_get_attr_row(struct terminal *terminal, int row) {
256 int index;
257
258 index = (row + terminal->start) % terminal->height;
259
260 return &terminal->data_attr[index * terminal->width];
261}
262
263static struct attr
264terminal_get_attr(struct terminal *terminal, int row, int col) {
265 return terminal_get_attr_row(terminal, row)[col];
266}
267
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500268static void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500269terminal_resize(struct terminal *terminal, int width, int height)
270{
271 size_t size;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000272 union utf8_char *data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000273 struct attr *data_attr;
274 int data_pitch, attr_pitch;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500275 int i, l, total_rows, start;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000276 struct rectangle rectangle;
277 struct winsize ws;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500278
279 if (terminal->width == width && terminal->height == height)
280 return;
281
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000282 data_pitch = width * sizeof(union utf8_char);
283 size = data_pitch * height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500284 data = malloc(size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000285 attr_pitch = width * sizeof(struct attr);
286 data_attr = malloc(attr_pitch * height);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500287 memset(data, 0, size);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000288 attr_init(data_attr, terminal->curr_attr, width * height);
289 if (terminal->data && terminal->data_attr) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500290 if (width > terminal->width)
291 l = terminal->width;
292 else
293 l = width;
294
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500295 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500296 total_rows = height;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500297 start = terminal->height - height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500298 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500299 total_rows = terminal->height;
300 start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500301 }
302
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000303 for (i = 0; i < total_rows; i++) {
304 memcpy(&data[width * i],
305 terminal_get_row(terminal, i),
306 l * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000307 memcpy(&data_attr[width * i],
308 terminal_get_attr_row(terminal, i),
309 l * sizeof(struct attr));
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000310 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500311
312 free(terminal->data);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000313 free(terminal->data_attr);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500314 }
315
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000316 terminal->data_pitch = data_pitch;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000317 terminal->attr_pitch = attr_pitch;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500318 terminal->width = width;
319 terminal->height = height;
320 terminal->data = data;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000321 terminal->data_attr = data_attr;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500322
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500323 if (terminal->row >= terminal->height)
324 terminal->row = terminal->height - 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500325 if (terminal->column >= terminal->width)
326 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500327 terminal->start = 0;
Callum Lowcaya0ee21c2011-01-07 19:46:56 +0000328
329 if (!terminal->fullscreen) {
330 rectangle.width = terminal->width *
331 terminal->extents.max_x_advance + 2 * terminal->margin;
332 rectangle.height = terminal->height *
333 terminal->extents.height + 2 * terminal->margin;
334 window_set_child_size(terminal->window, &rectangle);
335 }
336
337 /* Update the window size */
338 ws.ws_row = terminal->height;
339 ws.ws_col = terminal->width;
340 window_get_child_rectangle(terminal->window, &rectangle);
341 ws.ws_xpixel = rectangle.width;
342 ws.ws_ypixel = rectangle.height;
343 ioctl(terminal->master, TIOCSWINSZ, &ws);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500344}
345
Callum Lowcay30eeae52011-01-07 19:46:55 +0000346struct color_scheme DEFAULT_COLORS = {
347 {
348 {0, 0, 0, 1}, /* black */
349 {0.66, 0, 0, 1}, /* red */
350 {0 , 0.66, 0, 1}, /* green */
351 {0.66, 0.33, 0, 1}, /* orange (nicer than muddy yellow) */
352 {0 , 0 , 0.66, 1}, /* blue */
353 {0.66, 0 , 0.66, 1}, /* magenta */
354 {0, 0.66, 0.66, 1}, /* cyan */
355 {0.66, 0.66, 0.66, 1}, /* light grey */
356 {0.22, 0.33, 0.33, 1}, /* dark grey */
357 {1, 0.33, 0.33, 1}, /* high red */
358 {0.33, 1, 0.33, 1}, /* high green */
359 {1, 1, 0.33, 1}, /* high yellow */
360 {0.33, 0.33, 1, 1}, /* high blue */
361 {1, 0.33, 1, 1}, /* high magenta */
362 {0.33, 1, 1, 1}, /* high cyan */
363 {1, 1, 1, 1} /* white */
364 },
365 {0, 0, 0, 1}, /* black border */
366 {7, 0, 0, } /* bg:black (0), fg:light gray (7) */
367};
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400368
Kristian Høgsberg22106762008-12-08 13:50:07 -0500369static void
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500370terminal_draw_contents(struct terminal *terminal)
371{
372 struct rectangle rectangle;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500373 cairo_t *cr;
374 cairo_font_extents_t extents;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000375 int top_margin, side_margin;
376 int row, col;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000377 struct attr attr;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000378 union utf8_char *p_row;
379 struct utf8_chars {
380 union utf8_char c;
381 char null;
382 } toShow;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000383 int foreground, background, bold, underline;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000384 int text_x, text_y;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500385 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500386 double d;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500387
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000388 toShow.null = 0;
389
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500390 window_get_child_rectangle(terminal->window, &rectangle);
391
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400392 surface = display_create_surface(terminal->display, &rectangle);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500393 cr = cairo_create(surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500394 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400395 cairo_set_source_rgba(cr,
Callum Lowcay30eeae52011-01-07 19:46:55 +0000396 terminal->color_scheme->border.r,
397 terminal->color_scheme->border.g,
398 terminal->color_scheme->border.b,
399 terminal->color_scheme->border.a);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500400 cairo_paint(cr);
401 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500402
Callum Lowcay30eeae52011-01-07 19:46:55 +0000403 cairo_set_font_face(cr, terminal->font_normal);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500404 cairo_set_font_size(cr, 14);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500405
406 cairo_font_extents(cr, &extents);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500407 side_margin = (rectangle.width - terminal->width * extents.max_x_advance) / 2;
408 top_margin = (rectangle.height - terminal->height * extents.height) / 2;
409
Callum Lowcay30eeae52011-01-07 19:46:55 +0000410 cairo_set_line_width(cr, 1.0);
411
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000412 for (row = 0; row < terminal->height; row++) {
413 p_row = terminal_get_row(terminal, row);
414 for (col = 0; col < terminal->width; col++) {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000415 /* get the attributes for this character cell */
416 attr = terminal_get_attr(terminal, row, col);
417 if ((attr.a & ATTRMASK_INVERSE) ||
418 (terminal->focused &&
419 terminal->row == row && terminal->column == col))
420 {
421 foreground = attr.bg;
422 background = attr.fg;
423 } else {
424 foreground = attr.fg;
425 background = attr.bg;
426 }
427 bold = attr.a & (ATTRMASK_BOLD | ATTRMASK_BLINK);
428 underline = attr.a & ATTRMASK_UNDERLINE;
429
430 /* paint the background */
431 cairo_set_source_rgba(cr,
432 terminal->color_table[background].r,
433 terminal->color_table[background].g,
434 terminal->color_table[background].b,
435 terminal->color_table[background].a);
436 cairo_move_to(cr, side_margin + (col * extents.max_x_advance),
437 top_margin + (row * extents.height));
438 cairo_rel_line_to(cr, extents.max_x_advance, 0);
439 cairo_rel_line_to(cr, 0, extents.height);
440 cairo_rel_line_to(cr, -extents.max_x_advance, 0);
441 cairo_close_path(cr);
442 cairo_fill(cr);
443
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000444 /* paint the foreground */
Callum Lowcay30eeae52011-01-07 19:46:55 +0000445 if (bold)
446 cairo_set_font_face(cr, terminal->font_bold);
447 else
448 cairo_set_font_face(cr, terminal->font_normal);
449 cairo_set_source_rgba(cr,
450 terminal->color_table[foreground].r,
451 terminal->color_table[foreground].g,
452 terminal->color_table[foreground].b,
453 terminal->color_table[foreground].a);
454
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000455 text_x = side_margin + col * extents.max_x_advance;
456 text_y = top_margin + extents.ascent + row * extents.height;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000457 if (underline) {
458 cairo_move_to(cr, text_x, text_y + 2);
459 cairo_line_to(cr, text_x + extents.max_x_advance, text_y + 2);
460 cairo_stroke(cr);
461 }
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000462 cairo_move_to(cr, text_x, text_y);
463
464 toShow.c = p_row[col];
465 cairo_show_text(cr, (char *) toShow.c.byte);
466 }
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500467 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500468
Callum Lowcay30eeae52011-01-07 19:46:55 +0000469 if (!terminal->focused) {
470 d = 0.5;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500471
Callum Lowcay30eeae52011-01-07 19:46:55 +0000472 cairo_set_line_width(cr, 1);
473 cairo_move_to(cr, side_margin + terminal->column * extents.max_x_advance + d,
474 top_margin + terminal->row * extents.height + d);
475 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
476 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
477 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
478 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500479
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500480 cairo_stroke(cr);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000481 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500482
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500483 cairo_destroy(cr);
484
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500485 window_copy_surface(terminal->window,
486 &rectangle,
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500487 surface);
488
489 cairo_surface_destroy(surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500490}
491
492static void
493terminal_draw(struct terminal *terminal)
494{
Kristian Høgsberg22106762008-12-08 13:50:07 -0500495 struct rectangle rectangle;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500496 int32_t width, height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500497
498 window_get_child_rectangle(terminal->window, &rectangle);
499
Kristian Høgsberg09531622010-06-14 23:22:15 -0400500 width = (rectangle.width - 2 * terminal->margin) /
501 (int32_t) terminal->extents.max_x_advance;
502 height = (rectangle.height - 2 * terminal->margin) /
503 (int32_t) terminal->extents.height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500504 terminal_resize(terminal, width, height);
505
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500506 window_draw(terminal->window);
507 terminal_draw_contents(terminal);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400508 window_flush(terminal->window);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500509}
510
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400511static void
512redraw_handler(struct window *window, void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500513{
514 struct terminal *terminal = data;
515
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500516 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500517}
518
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500519#define STATE_NORMAL 0
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500520#define STATE_ESCAPE 1
Callum Lowcayb8609ad2011-01-07 19:46:57 +0000521#define STATE_ESCAPE_SPECIAL 2
522#define STATE_ESCAPE_CSI 3
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500523
524static void
525terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500526
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500527static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +0000528handle_char(struct terminal *terminal, union utf8_char utf8);
529
530static void
Callum Lowcay30eeae52011-01-07 19:46:55 +0000531handle_sgr(struct terminal *terminal, int code);
532
533static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500534handle_escape(struct terminal *terminal)
535{
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000536 union utf8_char *row;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000537 struct attr *attr_row;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000538 char *p;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500539 int i, count;
540 int args[10], set[10] = { 0, };
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500541
542 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500543 i = 0;
544 p = &terminal->escape[2];
545 while ((isdigit(*p) || *p == ';') && i < 10) {
546 if (*p == ';') {
Callum Lowcay30eeae52011-01-07 19:46:55 +0000547 if (!set[i]) {
548 args[i] = 0;
549 set[i] = 1;
550 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500551 p++;
552 i++;
553 } else {
554 args[i] = strtol(p, &p, 10);
555 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500556 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500557 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500558
559 switch (*p) {
560 case 'A':
561 count = set[0] ? args[0] : 1;
562 if (terminal->row - count >= 0)
563 terminal->row -= count;
564 else
565 terminal->row = 0;
566 break;
567 case 'B':
568 count = set[0] ? args[0] : 1;
569 if (terminal->row + count < terminal->height)
570 terminal->row += count;
571 else
572 terminal->row = terminal->height;
573 break;
574 case 'C':
575 count = set[0] ? args[0] : 1;
576 if (terminal->column + count < terminal->width)
577 terminal->column += count;
578 else
579 terminal->column = terminal->width;
580 break;
581 case 'D':
582 count = set[0] ? args[0] : 1;
583 if (terminal->column - count >= 0)
584 terminal->column -= count;
585 else
586 terminal->column = 0;
587 break;
588 case 'J':
589 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000590 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000591 memset(&row[terminal->column], 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000592 attr_init(&attr_row[terminal->column], terminal->curr_attr, (terminal->width - terminal->column));
593 for (i = terminal->row + 1; i < terminal->height; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000594 memset(terminal_get_row(terminal, i), 0, terminal->width * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000595 attr_init(terminal_get_attr_row(terminal, i), terminal->curr_attr, terminal->width);
596 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500597 break;
598 case 'G':
599 if (set[0])
600 terminal->column = args[0] - 1;
601 break;
602 case 'H':
603 case 'f':
604 terminal->row = set[0] ? args[0] - 1 : 0;
605 terminal->column = set[1] ? args[1] - 1 : 0;
606 break;
607 case 'K':
608 row = terminal_get_row(terminal, terminal->row);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000609 attr_row = terminal_get_attr_row(terminal, terminal->row);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000610 memset(&row[terminal->column], 0, (terminal->width - terminal->column) * sizeof(union utf8_char));
Callum Lowcay30eeae52011-01-07 19:46:55 +0000611 attr_init(&attr_row[terminal->column], terminal->curr_attr, (terminal->width - terminal->column));
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500612 break;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000613 case 'm': /* SGR */
614 if (set[0] && set[1] && set[2] && args[1] == 5) {
615 if (args[0] == 38) {
616 handle_sgr(terminal, args[2] + 256);
617 break;
618 } else if (args[0] == 48) {
619 handle_sgr(terminal, args[2] + 512);
620 break;
621 }
622 }
623 for(i = 0; i < 10; i++) {
624 if(set[i]) {
625 handle_sgr(terminal, args[i]);
626 } else if(i == 0) {
627 handle_sgr(terminal, 0);
628 break;
629 } else {
630 break;
631 }
632 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500633 break;
634 case '?':
635 if (strcmp(p, "?25l") == 0) {
636 /* hide cursor */
637 } else if (strcmp(p, "?25h") == 0) {
638 /* show cursor */
639 }
640 break;
641 default:
Callum Lowcayb8609ad2011-01-07 19:46:57 +0000642 fprintf(stderr, "Unknown CSI escape: %c\n", *p);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500643 break;
644 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500645}
646
647static void
Callum Lowcayb8609ad2011-01-07 19:46:57 +0000648handle_non_csi_escape(struct terminal *terminal, char code)
649{
650}
651
652static void
653handle_special_escape(struct terminal *terminal, char special, char code)
654{
655}
656
657static void
Callum Lowcay30eeae52011-01-07 19:46:55 +0000658handle_sgr(struct terminal *terminal, int code)
659{
660 switch(code) {
661 case 0:
662 terminal->curr_attr = terminal->color_scheme->default_attr;
663 break;
664 case 1:
665 terminal->curr_attr.a |= ATTRMASK_BOLD;
666 if (terminal->curr_attr.fg < 8)
667 terminal->curr_attr.fg += 8;
668 break;
669 case 4:
670 terminal->curr_attr.a |= ATTRMASK_UNDERLINE;
671 break;
672 case 5:
673 terminal->curr_attr.a |= ATTRMASK_BLINK;
674 break;
675 case 2:
676 case 21:
677 case 22:
678 terminal->curr_attr.a &= ~ATTRMASK_BOLD;
679 if (terminal->curr_attr.fg < 16 && terminal->curr_attr.fg >= 8)
680 terminal->curr_attr.fg -= 8;
681 break;
682 case 24:
683 terminal->curr_attr.a &= ~ATTRMASK_UNDERLINE;
684 break;
685 case 25:
686 terminal->curr_attr.a &= ~ATTRMASK_BLINK;
687 break;
688 case 7:
689 case 26:
690 terminal->curr_attr.a |= ATTRMASK_INVERSE;
691 break;
692 case 27:
693 terminal->curr_attr.a &= ~ATTRMASK_INVERSE;
694 break;
695 case 39:
696 terminal->curr_attr.fg = terminal->color_scheme->default_attr.fg;
697 break;
698 case 49:
699 terminal->curr_attr.bg = terminal->color_scheme->default_attr.bg;
700 break;
701 default:
702 if(code >= 30 && code <= 37) {
703 terminal->curr_attr.fg = code - 30;
704 if (terminal->curr_attr.a & ATTRMASK_BOLD)
705 terminal->curr_attr.fg += 8;
706 } else if(code >= 40 && code <= 47) {
707 terminal->curr_attr.bg = code - 40;
708 } else if(code >= 256 && code < 512) {
709 terminal->curr_attr.fg = code - 256;
710 } else if(code >= 512 && code < 768) {
711 terminal->curr_attr.bg = code - 512;
712 } else {
713 fprintf(stderr, "Unknown SGR code: %d\n", code);
714 }
715 break;
716 }
717}
718
Callum Lowcayb8609ad2011-01-07 19:46:57 +0000719/* Returns 1 if c was special, otherwise 0 */
720static int
721handle_special_char(struct terminal *terminal, char c)
722{
723 union utf8_char *row;
724 struct attr *attr_row;
725
726 row = terminal_get_row(terminal, terminal->row);
727 attr_row = terminal_get_attr_row(terminal, terminal->row);
728
729 switch(c) {
730 case '\r':
731 terminal->column = 0;
732 break;
733 case '\n':
734 terminal->column = 0;
735 /* fallthrough */
736 case '\v':
737 case '\f':
738 if (terminal->row + 1 < terminal->height) {
739 terminal->row++;
740 } else {
741 terminal->start++;
742 if (terminal->start == terminal->height)
743 terminal->start = 0;
744 memset(terminal_get_row(terminal, terminal->row),
745 0, terminal->width * sizeof(union utf8_char));
746 attr_init(terminal_get_attr_row(terminal, terminal->row),
747 terminal->curr_attr, terminal->width);
748 }
749
750 break;
751 case '\t':
752 memset(&row[terminal->column], ' ', (-terminal->column & 7) * sizeof(union utf8_char));
753 attr_init(&attr_row[terminal->column], terminal->curr_attr, -terminal->column & 7);
754 terminal->column = (terminal->column + 7) & ~7;
755 break;
756 case '\b':
757 if (terminal->column > 0)
758 terminal->column--;
759 break;
760 case '\a':
761 /* Bell */
762 break;
763 default:
764 return 0;
765 }
766
767 return 1;
768}
769
770static void
771handle_char(struct terminal *terminal, union utf8_char utf8)
772{
773 union utf8_char *row;
774 struct attr *attr_row;
775
776 if (handle_special_char(terminal, utf8.byte[0])) return;
777
778 /* There are a whole lot of non-characters, control codes,
779 * and formatting codes that should probably be ignored,
780 * for example: */
781 if (strncmp((char*) utf8.byte, "\xEF\xBB\xBF", 3) == 0) {
782 /* BOM, ignore */
783 return;
784 }
785
786 /* Some of these non-characters should be translated, e.g.: */
787 if (utf8.byte[0] < 32) {
788 utf8.byte[0] = utf8.byte[0] + 64;
789 }
790
791 /* handle right margin effects */
792 if (terminal->column >= terminal->width) {
793 terminal->column--;
794 }
795
796 row = terminal_get_row(terminal, terminal->row);
797 attr_row = terminal_get_attr_row(terminal, terminal->row);
798
799 row[terminal->column] = utf8;
800 attr_row[terminal->column++] = terminal->curr_attr;
801
802 if (utf8.ch != terminal->last_char.ch)
803 terminal->last_char = utf8;
804}
805
Callum Lowcay30eeae52011-01-07 19:46:55 +0000806static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500807terminal_data(struct terminal *terminal, const char *data, size_t length)
808{
809 int i;
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000810 union utf8_char utf8;
811 enum utf8_state parser_state;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500812
813 for (i = 0; i < length; i++) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000814 parser_state =
815 utf8_next_char(&terminal->state_machine, data[i]);
816 switch(parser_state) {
817 case utf8state_accept:
818 utf8.ch = terminal->state_machine.s.ch;
819 break;
820 case utf8state_reject:
821 /* the unicode replacement character */
822 utf8.byte[0] = 0xEF;
823 utf8.byte[1] = 0xBF;
824 utf8.byte[2] = 0xBD;
825 utf8.byte[3] = 0x00;
826 break;
827 default:
828 continue;
829 }
830
Callum Lowcayb8609ad2011-01-07 19:46:57 +0000831 /* assume escape codes never use non-ASCII characters */
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500832 if (terminal->state == STATE_ESCAPE) {
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000833 terminal->escape[terminal->escape_length++] = utf8.byte[0];
Callum Lowcayb8609ad2011-01-07 19:46:57 +0000834 if (utf8.byte[0] == '[') {
835 terminal->state = STATE_ESCAPE_CSI;
836 continue;
837 } else if (utf8.byte[0] == '#' || utf8.byte[0] == '(' ||
838 utf8.byte[0] == ')')
839 {
840 terminal->state = STATE_ESCAPE_SPECIAL;
841 continue;
842 } else {
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500843 terminal->state = STATE_NORMAL;
Callum Lowcayb8609ad2011-01-07 19:46:57 +0000844 handle_non_csi_escape(terminal, utf8.byte[0]);
845 continue;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500846 }
Callum Lowcayb8609ad2011-01-07 19:46:57 +0000847 } else if (terminal->state == STATE_ESCAPE_SPECIAL) {
848 terminal->escape[terminal->escape_length++] = utf8.byte[0];
849 terminal->state = STATE_NORMAL;
850 if (isdigit(utf8.byte[0]) || isalpha(utf8.byte[0])) {
851 handle_special_escape(terminal, terminal->escape[1],
852 utf8.byte[0]);
853 continue;
854 }
855 } else if (terminal->state == STATE_ESCAPE_CSI) {
856 if (handle_special_char(terminal, utf8.byte[0]) != 0) {
857 /* do nothing */
858 } else if (utf8.byte[0] == '?') {
859 terminal->qmark_flag = 1;
860 } else {
861 /* Don't overflow the buffer */
862 if (terminal->escape_length < MAX_ESCAPE)
863 terminal->escape[terminal->escape_length++] = utf8.byte[0];
864 if (terminal->escape_length >= MAX_ESCAPE)
865 terminal->state = STATE_NORMAL;
866 }
867
868 if (isalpha(utf8.byte[0]) || utf8.byte[0] == '@' ||
869 utf8.byte[0] == '`')
870 {
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500871 terminal->state = STATE_NORMAL;
872 handle_escape(terminal);
Callum Lowcayb8609ad2011-01-07 19:46:57 +0000873 continue;
874 } else {
875 continue;
876 }
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500877 }
878
Callum Lowcayb8609ad2011-01-07 19:46:57 +0000879 /* this is valid, because ASCII characters are never used to
880 * introduce a multibyte sequence in UTF-8 */
881 if (utf8.byte[0] == '\e') {
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500882 terminal->state = STATE_ESCAPE;
883 terminal->escape[0] = '\e';
884 terminal->escape_length = 1;
Callum Lowcayb8609ad2011-01-07 19:46:57 +0000885 terminal->qmark_flag = 0;
886 } else {
887 handle_char(terminal, utf8);
888 } /* if */
889 } /* for */
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500890
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400891 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500892}
893
894static void
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400895key_handler(struct window *window, uint32_t key, uint32_t sym,
Kristian Høgsberg55444912009-02-21 14:31:09 -0500896 uint32_t state, uint32_t modifiers, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500897{
898 struct terminal *terminal = data;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400899 char ch[2];
900 int len = 0;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500901
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400902 switch (sym) {
903 case XK_F11:
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500904 if (!state)
905 break;
906 terminal->fullscreen ^= 1;
907 window_set_fullscreen(window, terminal->fullscreen);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400908 window_schedule_redraw(terminal->window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500909 break;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400910
911 case XK_Delete:
912 sym = 0x04;
913 case XK_BackSpace:
914 case XK_Tab:
915 case XK_Linefeed:
916 case XK_Clear:
917 case XK_Return:
918 case XK_Pause:
919 case XK_Scroll_Lock:
920 case XK_Sys_Req:
921 case XK_Escape:
922 ch[len++] = sym & 0x7f;
923 break;
924
925 case XK_Shift_L:
926 case XK_Shift_R:
927 case XK_Control_L:
928 case XK_Control_R:
929 case XK_Alt_L:
930 case XK_Alt_R:
931 break;
932
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500933 default:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400934 if (modifiers & WINDOW_MODIFIER_CONTROL)
935 sym = sym & 0x1f;
936 else if (modifiers & WINDOW_MODIFIER_ALT)
937 ch[len++] = 0x1b;
938 if (sym < 256)
939 ch[len++] = sym;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500940 break;
941 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400942
943 if (state && len > 0)
944 write(terminal->master, ch, len);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500945}
946
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500947static void
948keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -0400949 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500950{
951 struct terminal *terminal = data;
952
953 terminal->focused = (device != NULL);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400954 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500955}
956
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500957static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500958terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500959{
960 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400961 cairo_surface_t *surface;
962 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500963
964 terminal = malloc(sizeof *terminal);
965 if (terminal == NULL)
966 return terminal;
967
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500968 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500969 terminal->fullscreen = fullscreen;
Callum Lowcay30eeae52011-01-07 19:46:55 +0000970 terminal->color_scheme = &DEFAULT_COLORS;
971 terminal_init(terminal);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500972 terminal->window = window_create(display, "Wayland Terminal",
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500973 500, 400);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000974
975 init_state_machine(&terminal->state_machine);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000976 init_color_table(terminal);
Callum Lowcay15bdc5d2011-01-07 19:46:54 +0000977
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500978 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500979 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500980
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500981 window_set_fullscreen(terminal->window, terminal->fullscreen);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400982 window_set_user_data(terminal->window, terminal);
983 window_set_redraw_handler(terminal->window, redraw_handler);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500984
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400985 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500986 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400987 keyboard_focus_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500988
Kristian Høgsberg09531622010-06-14 23:22:15 -0400989 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
990 cr = cairo_create(surface);
Callum Lowcay30eeae52011-01-07 19:46:55 +0000991 terminal->font_bold = cairo_toy_font_face_create ("mono",
992 CAIRO_FONT_SLANT_NORMAL,
993 CAIRO_FONT_WEIGHT_BOLD);
994 cairo_font_face_reference(terminal->font_bold);
995 terminal->font_normal = cairo_toy_font_face_create ("mono",
996 CAIRO_FONT_SLANT_NORMAL,
997 CAIRO_FONT_WEIGHT_NORMAL);
998 cairo_font_face_reference(terminal->font_normal);
999 cairo_set_font_face(cr, terminal->font_normal);
Kristian Høgsberg09531622010-06-14 23:22:15 -04001000 cairo_set_font_size(cr, 14);
1001 cairo_font_extents(cr, &terminal->extents);
1002 cairo_destroy(cr);
1003 cairo_surface_destroy(surface);
1004
Callum Lowcaya0ee21c2011-01-07 19:46:56 +00001005 terminal_resize(terminal, 80, 24);
Kristian Høgsberg22106762008-12-08 13:50:07 -05001006 terminal_draw(terminal);
1007
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001008 return terminal;
1009}
1010
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001011static gboolean
1012io_handler(GIOChannel *source,
1013 GIOCondition condition,
1014 gpointer data)
1015{
1016 struct terminal *terminal = data;
1017 gchar buffer[256];
1018 gsize bytes_read;
1019 GError *error = NULL;
1020
1021 g_io_channel_read_chars(source, buffer, sizeof buffer,
1022 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001023
1024 terminal_data(terminal, buffer, bytes_read);
1025
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001026 return TRUE;
1027}
1028
1029static int
1030terminal_run(struct terminal *terminal, const char *path)
1031{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05001032 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001033 pid_t pid;
1034
1035 pid = forkpty(&master, NULL, NULL, NULL);
1036 if (pid == 0) {
Kristian Høgsbergc8c5d582008-12-18 14:50:08 -05001037 setenv("TERM", "vt100", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001038 if (execl(path, path, NULL)) {
1039 printf("exec failed: %m\n");
1040 exit(EXIT_FAILURE);
1041 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05001042 } else if (pid < 0) {
1043 fprintf(stderr, "failed to fork and create pty (%m).\n");
1044 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001045 }
1046
Kristian Høgsberg6e83d582008-12-08 00:01:36 -05001047 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -05001048 terminal->channel = g_io_channel_unix_new(master);
1049 fcntl(master, F_SETFL, O_NONBLOCK);
1050 g_io_add_watch(terminal->channel, G_IO_IN,
1051 io_handler, terminal);
1052
1053 return 0;
1054}
1055
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001056static const GOptionEntry option_entries[] = {
1057 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
1058 &option_fullscreen, "Run in fullscreen mode" },
1059 { NULL }
1060};
1061
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001062int main(int argc, char *argv[])
1063{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001064 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001065 struct terminal *terminal;
Kristian Høgsberg0395f302008-12-22 12:14:50 -05001066
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001067 d = display_create(&argc, &argv, option_entries);
Yuval Fledele9f5e362010-11-22 21:34:19 +02001068 if (d == NULL) {
1069 fprintf(stderr, "failed to create display: %m\n");
1070 return -1;
1071 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001072
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -05001073 terminal = terminal_create(d, option_fullscreen);
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -05001074 if (terminal_run(terminal, "/bin/bash"))
1075 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001076
Kristian Høgsberg7824d812010-06-08 14:59:44 -04001077 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001078
1079 return 0;
1080}