blob: e0702e5ee7d78a772cce07a55568824f24653038 [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>
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050035#include <linux/input.h>
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050036#include <cairo-drm.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050037
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
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050050struct terminal {
51 struct window *window;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050052 struct display *display;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050053 char *data;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050054 int width, height, start, row, column;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050055 int fd, master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050056 GIOChannel *channel;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050057 uint32_t modifiers;
Kristian Høgsberg17809b12008-12-08 12:20:40 -050058 char escape[64];
59 int escape_length;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -050060 int state;
Kristian Høgsberg1584c572008-12-08 12:59:37 -050061 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050062 int fullscreen;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050063 int focused;
Kristian Høgsberg12308a42009-09-28 13:08:50 -040064 struct color_scheme *color_scheme;
Kristian Høgsberg09531622010-06-14 23:22:15 -040065 cairo_font_extents_t extents;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050066};
67
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050068static char *
69terminal_get_row(struct terminal *terminal, int row)
70{
71 int index;
72
73 index = (row + terminal->start) % terminal->height;
74
75 return &terminal->data[index * (terminal->width + 1)];
76}
77
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050078static void
Kristian Høgsberg22106762008-12-08 13:50:07 -050079terminal_resize(struct terminal *terminal, int width, int height)
80{
81 size_t size;
82 char *data;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050083 int i, l, total_rows, start;
Kristian Høgsberg22106762008-12-08 13:50:07 -050084
85 if (terminal->width == width && terminal->height == height)
86 return;
87
88 size = (width + 1) * height;
89 data = malloc(size);
90 memset(data, 0, size);
91 if (terminal->data) {
92 if (width > terminal->width)
93 l = terminal->width;
94 else
95 l = width;
96
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050097 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -050098 total_rows = height;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050099 start = terminal->height - height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500100 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500101 total_rows = terminal->height;
102 start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500103 }
104
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500105 for (i = 0; i < total_rows; i++)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500106 memcpy(data + (width + 1) * i,
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500107 terminal_get_row(terminal, i), l);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500108
109 free(terminal->data);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500110 }
111
112 terminal->width = width;
113 terminal->height = height;
114 terminal->data = data;
115
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500116 if (terminal->row >= terminal->height)
117 terminal->row = terminal->height - 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500118 if (terminal->column >= terminal->width)
119 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500120 terminal->start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500121}
122
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400123struct color_scheme { struct { double r, g, b, a; } fg, bg; }
124 matrix_colors = { { 0, 0.7, 0, 1 }, { 0, 0, 0, 0.9 } },
125 jbarnes_colors = { { 1, 1, 1, 1 }, { 0, 0, 0, 1 } };
126
Kristian Høgsberg22106762008-12-08 13:50:07 -0500127static void
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500128terminal_draw_contents(struct terminal *terminal)
129{
130 struct rectangle rectangle;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500131 cairo_t *cr;
132 cairo_font_extents_t extents;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500133 int i, top_margin, side_margin;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500134 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500135 double d;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500136
137 window_get_child_rectangle(terminal->window, &rectangle);
138
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500139 surface = window_create_surface(terminal->window, &rectangle);
140 cr = cairo_create(surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500141 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400142 cairo_set_source_rgba(cr,
143 terminal->color_scheme->bg.r,
144 terminal->color_scheme->bg.g,
145 terminal->color_scheme->bg.b,
146 terminal->color_scheme->bg.a);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500147 cairo_paint(cr);
148 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400149 cairo_set_source_rgba(cr,
150 terminal->color_scheme->fg.r,
151 terminal->color_scheme->fg.g,
152 terminal->color_scheme->fg.b,
153 terminal->color_scheme->fg.a);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500154
155 cairo_select_font_face (cr, "mono",
156 CAIRO_FONT_SLANT_NORMAL,
157 CAIRO_FONT_WEIGHT_NORMAL);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500158 cairo_set_font_size(cr, 14);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500159
160 cairo_font_extents(cr, &extents);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500161 side_margin = (rectangle.width - terminal->width * extents.max_x_advance) / 2;
162 top_margin = (rectangle.height - terminal->height * extents.height) / 2;
163
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500164 for (i = 0; i < terminal->height; i++) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500165 cairo_move_to(cr, side_margin,
166 top_margin + extents.ascent + extents.height * i);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500167 cairo_show_text(cr, terminal_get_row(terminal, i));
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500168 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500169
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500170 d = terminal->focused ? 0 : 0.5;
171
172 cairo_set_line_width(cr, 1);
173 cairo_move_to(cr, side_margin + terminal->column * extents.max_x_advance + d,
174 top_margin + terminal->row * extents.height + d);
175 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
176 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
177 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500178 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500179
180 if (terminal->focused)
181 cairo_fill(cr);
182 else
183 cairo_stroke(cr);
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500184
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500185 cairo_destroy(cr);
186
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500187 window_copy_surface(terminal->window,
188 &rectangle,
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500189 surface);
190
191 cairo_surface_destroy(surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500192}
193
194static void
195terminal_draw(struct terminal *terminal)
196{
Kristian Høgsberg22106762008-12-08 13:50:07 -0500197 struct rectangle rectangle;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500198 int32_t width, height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500199
200 window_get_child_rectangle(terminal->window, &rectangle);
201
Kristian Høgsberg09531622010-06-14 23:22:15 -0400202 width = (rectangle.width - 2 * terminal->margin) /
203 (int32_t) terminal->extents.max_x_advance;
204 height = (rectangle.height - 2 * terminal->margin) /
205 (int32_t) terminal->extents.height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500206 terminal_resize(terminal, width, height);
207
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500208 if (!terminal->fullscreen) {
Kristian Høgsberg09531622010-06-14 23:22:15 -0400209 rectangle.width = terminal->width *
210 terminal->extents.max_x_advance + 2 * terminal->margin;
211 rectangle.height = terminal->height *
212 terminal->extents.height + 2 * terminal->margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500213 window_set_child_size(terminal->window, &rectangle);
214 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500215
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500216 window_draw(terminal->window);
217 terminal_draw_contents(terminal);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500218 window_commit(terminal->window, 0);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500219}
220
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400221static void
222redraw_handler(struct window *window, void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500223{
224 struct terminal *terminal = data;
225
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500226 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500227}
228
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500229#define STATE_NORMAL 0
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500230#define STATE_ESCAPE 1
231
232static void
233terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500234
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500235static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500236handle_escape(struct terminal *terminal)
237{
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500238 char *row, *p;
239 int i, count;
240 int args[10], set[10] = { 0, };
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500241
242 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500243 i = 0;
244 p = &terminal->escape[2];
245 while ((isdigit(*p) || *p == ';') && i < 10) {
246 if (*p == ';') {
247 p++;
248 i++;
249 } else {
250 args[i] = strtol(p, &p, 10);
251 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500252 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500253 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500254
255 switch (*p) {
256 case 'A':
257 count = set[0] ? args[0] : 1;
258 if (terminal->row - count >= 0)
259 terminal->row -= count;
260 else
261 terminal->row = 0;
262 break;
263 case 'B':
264 count = set[0] ? args[0] : 1;
265 if (terminal->row + count < terminal->height)
266 terminal->row += count;
267 else
268 terminal->row = terminal->height;
269 break;
270 case 'C':
271 count = set[0] ? args[0] : 1;
272 if (terminal->column + count < terminal->width)
273 terminal->column += count;
274 else
275 terminal->column = terminal->width;
276 break;
277 case 'D':
278 count = set[0] ? args[0] : 1;
279 if (terminal->column - count >= 0)
280 terminal->column -= count;
281 else
282 terminal->column = 0;
283 break;
284 case 'J':
285 row = terminal_get_row(terminal, terminal->row);
286 memset(&row[terminal->column], 0, terminal->width - terminal->column);
287 for (i = terminal->row + 1; i < terminal->height; i++)
288 memset(terminal_get_row(terminal, i), 0, terminal->width);
289 break;
290 case 'G':
291 if (set[0])
292 terminal->column = args[0] - 1;
293 break;
294 case 'H':
295 case 'f':
296 terminal->row = set[0] ? args[0] - 1 : 0;
297 terminal->column = set[1] ? args[1] - 1 : 0;
298 break;
299 case 'K':
300 row = terminal_get_row(terminal, terminal->row);
301 memset(&row[terminal->column], 0, terminal->width - terminal->column);
302 break;
303 case 'm':
304 /* color, blink, bold etc*/
305 break;
306 case '?':
307 if (strcmp(p, "?25l") == 0) {
308 /* hide cursor */
309 } else if (strcmp(p, "?25h") == 0) {
310 /* show cursor */
311 }
312 break;
313 default:
314 terminal_data(terminal,
315 terminal->escape + 1,
316 terminal->escape_length - 2);
317 break;
318 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500319}
320
321static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500322terminal_data(struct terminal *terminal, const char *data, size_t length)
323{
324 int i;
325 char *row;
326
327 for (i = 0; i < length; i++) {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500328 row = terminal_get_row(terminal, terminal->row);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500329
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500330 if (terminal->state == STATE_ESCAPE) {
331 terminal->escape[terminal->escape_length++] = data[i];
332 if (terminal->escape_length == 2 && data[i] != '[') {
333 /* Bad escape sequence. */
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500334 terminal->state = STATE_NORMAL;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500335 goto cancel_escape;
336 }
337
338 if (isalpha(data[i])) {
339 terminal->state = STATE_NORMAL;
340 handle_escape(terminal);
341 }
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500342 continue;
343 }
344
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500345 cancel_escape:
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500346 switch (data[i]) {
347 case '\r':
348 terminal->column = 0;
349 break;
350 case '\n':
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500351 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500352 if (terminal->row + 1 < terminal->height) {
353 terminal->row++;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500354 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500355 terminal->start++;
356 if (terminal->start == terminal->height)
357 terminal->start = 0;
358 memset(terminal_get_row(terminal, terminal->row),
359 0, terminal->width);
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500360 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500361
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500362 break;
363 case '\t':
364 memset(&row[terminal->column], ' ', -terminal->column & 7);
365 terminal->column = (terminal->column + 7) & ~7;
366 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500367 case '\e':
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500368 terminal->state = STATE_ESCAPE;
369 terminal->escape[0] = '\e';
370 terminal->escape_length = 1;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500371 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500372 case '\b':
373 if (terminal->column > 0)
374 terminal->column--;
375 break;
376 case '\a':
377 /* Bell */
378 break;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500379 default:
380 if (terminal->column < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500381 row[terminal->column++] = data[i] < 32 ? data[i] + 64 : data[i];
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500382 break;
383 }
384 }
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500385
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400386 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500387}
388
389static void
Kristian Høgsberg55444912009-02-21 14:31:09 -0500390key_handler(struct window *window, uint32_t key, uint32_t unicode,
391 uint32_t state, uint32_t modifiers, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500392{
393 struct terminal *terminal = data;
Kristian Høgsberg55444912009-02-21 14:31:09 -0500394 char ch = unicode;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500395
396 switch (key) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500397 case KEY_F11:
398 if (!state)
399 break;
400 terminal->fullscreen ^= 1;
401 window_set_fullscreen(window, terminal->fullscreen);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400402 window_schedule_redraw(terminal->window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500403 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500404 default:
Kristian Høgsberg55444912009-02-21 14:31:09 -0500405 if (state && unicode)
406 write(terminal->master, &ch, 1);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500407 break;
408 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500409}
410
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500411static void
412keyboard_focus_handler(struct window *window,
413 struct wl_input_device *device, void *data)
414{
415 struct terminal *terminal = data;
416
417 terminal->focused = (device != NULL);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400418 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500419}
420
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500421static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500422terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500423{
424 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400425 cairo_surface_t *surface;
426 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500427
428 terminal = malloc(sizeof *terminal);
429 if (terminal == NULL)
430 return terminal;
431
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500432 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500433 terminal->fullscreen = fullscreen;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400434 terminal->color_scheme = &jbarnes_colors;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500435 terminal->window = window_create(display, "Wayland Terminal",
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500436 500, 100, 500, 400);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500437 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500438 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500439
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500440 window_set_fullscreen(terminal->window, terminal->fullscreen);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400441 window_set_redraw_handler(terminal->window,
442 redraw_handler, terminal);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500443
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500444 window_set_key_handler(terminal->window, key_handler, terminal);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500445 window_set_keyboard_focus_handler(terminal->window,
446 keyboard_focus_handler, terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500447
Kristian Høgsberg09531622010-06-14 23:22:15 -0400448 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
449 cr = cairo_create(surface);
450 cairo_select_font_face (cr, "mono",
451 CAIRO_FONT_SLANT_NORMAL,
452 CAIRO_FONT_WEIGHT_NORMAL);
453 cairo_set_font_size(cr, 14);
454 cairo_font_extents(cr, &terminal->extents);
455 cairo_destroy(cr);
456 cairo_surface_destroy(surface);
457
Kristian Høgsberg22106762008-12-08 13:50:07 -0500458 terminal_draw(terminal);
459
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500460 return terminal;
461}
462
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500463static gboolean
464io_handler(GIOChannel *source,
465 GIOCondition condition,
466 gpointer data)
467{
468 struct terminal *terminal = data;
469 gchar buffer[256];
470 gsize bytes_read;
471 GError *error = NULL;
472
473 g_io_channel_read_chars(source, buffer, sizeof buffer,
474 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500475
476 terminal_data(terminal, buffer, bytes_read);
477
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500478 return TRUE;
479}
480
481static int
482terminal_run(struct terminal *terminal, const char *path)
483{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500484 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500485 pid_t pid;
486
487 pid = forkpty(&master, NULL, NULL, NULL);
488 if (pid == 0) {
Kristian Høgsbergc8c5d582008-12-18 14:50:08 -0500489 setenv("TERM", "vt100", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500490 if (execl(path, path, NULL)) {
491 printf("exec failed: %m\n");
492 exit(EXIT_FAILURE);
493 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500494 } else if (pid < 0) {
495 fprintf(stderr, "failed to fork and create pty (%m).\n");
496 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500497 }
498
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500499 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500500 terminal->channel = g_io_channel_unix_new(master);
501 fcntl(master, F_SETFL, O_NONBLOCK);
502 g_io_add_watch(terminal->channel, G_IO_IN,
503 io_handler, terminal);
504
505 return 0;
506}
507
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500508static const GOptionEntry option_entries[] = {
509 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
510 &option_fullscreen, "Run in fullscreen mode" },
511 { NULL }
512};
513
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500514int main(int argc, char *argv[])
515{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500516 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500517 struct terminal *terminal;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500518
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400519 d = display_create(&argc, &argv, option_entries);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500520
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500521 terminal = terminal_create(d, option_fullscreen);
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500522 if (terminal_run(terminal, "/bin/bash"))
523 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500524
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400525 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500526
527 return 0;
528}