blob: ecbf0a4ff6840e6c4b954261144130b84aade3ce [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
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øgsbergdcb71b62010-06-15 17:16:35 -0400139 surface = display_create_surface(terminal->display, &rectangle);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500140 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øgsberg9d69f8e2010-09-03 14:46:38 -0400218 window_flush(terminal->window);
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øgsberg94adf6c2010-06-25 16:50:05 -0400390key_handler(struct window *window, uint32_t key, uint32_t sym,
Kristian Høgsberg55444912009-02-21 14:31:09 -0500391 uint32_t state, uint32_t modifiers, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500392{
393 struct terminal *terminal = data;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400394 char ch[2];
395 int len = 0;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500396
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400397 switch (sym) {
398 case XK_F11:
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500399 if (!state)
400 break;
401 terminal->fullscreen ^= 1;
402 window_set_fullscreen(window, terminal->fullscreen);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400403 window_schedule_redraw(terminal->window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500404 break;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400405
406 case XK_Delete:
407 sym = 0x04;
408 case XK_BackSpace:
409 case XK_Tab:
410 case XK_Linefeed:
411 case XK_Clear:
412 case XK_Return:
413 case XK_Pause:
414 case XK_Scroll_Lock:
415 case XK_Sys_Req:
416 case XK_Escape:
417 ch[len++] = sym & 0x7f;
418 break;
419
420 case XK_Shift_L:
421 case XK_Shift_R:
422 case XK_Control_L:
423 case XK_Control_R:
424 case XK_Alt_L:
425 case XK_Alt_R:
426 break;
427
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500428 default:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400429 if (modifiers & WINDOW_MODIFIER_CONTROL)
430 sym = sym & 0x1f;
431 else if (modifiers & WINDOW_MODIFIER_ALT)
432 ch[len++] = 0x1b;
433 if (sym < 256)
434 ch[len++] = sym;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500435 break;
436 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400437
438 if (state && len > 0)
439 write(terminal->master, ch, len);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500440}
441
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500442static void
443keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -0400444 struct input *device, void *data)
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500445{
446 struct terminal *terminal = data;
447
448 terminal->focused = (device != NULL);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400449 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500450}
451
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500452static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500453terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500454{
455 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400456 cairo_surface_t *surface;
457 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500458
459 terminal = malloc(sizeof *terminal);
460 if (terminal == NULL)
461 return terminal;
462
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500463 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500464 terminal->fullscreen = fullscreen;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400465 terminal->color_scheme = &jbarnes_colors;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500466 terminal->window = window_create(display, "Wayland Terminal",
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500467 500, 400);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500468 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500469 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500470
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500471 window_set_fullscreen(terminal->window, terminal->fullscreen);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400472 window_set_user_data(terminal->window, terminal);
473 window_set_redraw_handler(terminal->window, redraw_handler);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500474
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400475 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500476 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400477 keyboard_focus_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500478
Kristian Høgsberg09531622010-06-14 23:22:15 -0400479 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
480 cr = cairo_create(surface);
481 cairo_select_font_face (cr, "mono",
482 CAIRO_FONT_SLANT_NORMAL,
483 CAIRO_FONT_WEIGHT_NORMAL);
484 cairo_set_font_size(cr, 14);
485 cairo_font_extents(cr, &terminal->extents);
486 cairo_destroy(cr);
487 cairo_surface_destroy(surface);
488
Kristian Høgsberg22106762008-12-08 13:50:07 -0500489 terminal_draw(terminal);
490
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500491 return terminal;
492}
493
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500494static gboolean
495io_handler(GIOChannel *source,
496 GIOCondition condition,
497 gpointer data)
498{
499 struct terminal *terminal = data;
500 gchar buffer[256];
501 gsize bytes_read;
502 GError *error = NULL;
503
504 g_io_channel_read_chars(source, buffer, sizeof buffer,
505 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500506
507 terminal_data(terminal, buffer, bytes_read);
508
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500509 return TRUE;
510}
511
512static int
513terminal_run(struct terminal *terminal, const char *path)
514{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500515 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500516 pid_t pid;
517
518 pid = forkpty(&master, NULL, NULL, NULL);
519 if (pid == 0) {
Kristian Høgsbergc8c5d582008-12-18 14:50:08 -0500520 setenv("TERM", "vt100", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500521 if (execl(path, path, NULL)) {
522 printf("exec failed: %m\n");
523 exit(EXIT_FAILURE);
524 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500525 } else if (pid < 0) {
526 fprintf(stderr, "failed to fork and create pty (%m).\n");
527 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500528 }
529
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500530 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500531 terminal->channel = g_io_channel_unix_new(master);
532 fcntl(master, F_SETFL, O_NONBLOCK);
533 g_io_add_watch(terminal->channel, G_IO_IN,
534 io_handler, terminal);
535
536 return 0;
537}
538
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500539static const GOptionEntry option_entries[] = {
540 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
541 &option_fullscreen, "Run in fullscreen mode" },
542 { NULL }
543};
544
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500545int main(int argc, char *argv[])
546{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500547 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500548 struct terminal *terminal;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500549
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400550 d = display_create(&argc, &argv, option_entries);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200551 if (d == NULL) {
552 fprintf(stderr, "failed to create display: %m\n");
553 return -1;
554 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500555
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500556 terminal = terminal_create(d, option_fullscreen);
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500557 if (terminal_run(terminal, "/bin/bash"))
558 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500559
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400560 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500561
562 return 0;
563}