blob: e7e054b32d97710ae4abb9fe5119c0fd402ad81b [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øgsberg0ac16f02009-01-15 11:37:43 -050035#include <cairo-drm.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050036
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -040037#include <X11/keysym.h>
38
Kristian Høgsberg12308a42009-09-28 13:08:50 -040039#include "wayland-util.h"
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050040#include "wayland-client.h"
41#include "wayland-glib.h"
42
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050043#include "window.h"
44
Kristian Høgsberg0395f302008-12-22 12:14:50 -050045static int option_fullscreen;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050046
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050047#define MOD_SHIFT 0x01
48#define MOD_ALT 0x02
49#define MOD_CTRL 0x04
50
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050051struct terminal {
52 struct window *window;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050053 struct display *display;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050054 char *data;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050055 int width, height, start, row, column;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050056 int fd, master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050057 GIOChannel *channel;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050058 uint32_t modifiers;
Kristian Høgsberg17809b12008-12-08 12:20:40 -050059 char escape[64];
60 int escape_length;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -050061 int state;
Kristian Høgsberg1584c572008-12-08 12:59:37 -050062 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050063 int fullscreen;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050064 int focused;
Kristian Høgsberg12308a42009-09-28 13:08:50 -040065 struct color_scheme *color_scheme;
Kristian Høgsberg09531622010-06-14 23:22:15 -040066 cairo_font_extents_t extents;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050067};
68
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050069static char *
70terminal_get_row(struct terminal *terminal, int row)
71{
72 int index;
73
74 index = (row + terminal->start) % terminal->height;
75
76 return &terminal->data[index * (terminal->width + 1)];
77}
78
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050079static void
Kristian Høgsberg22106762008-12-08 13:50:07 -050080terminal_resize(struct terminal *terminal, int width, int height)
81{
82 size_t size;
83 char *data;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050084 int i, l, total_rows, start;
Kristian Høgsberg22106762008-12-08 13:50:07 -050085
86 if (terminal->width == width && terminal->height == height)
87 return;
88
89 size = (width + 1) * height;
90 data = malloc(size);
91 memset(data, 0, size);
92 if (terminal->data) {
93 if (width > terminal->width)
94 l = terminal->width;
95 else
96 l = width;
97
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050098 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -050099 total_rows = height;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500100 start = terminal->height - height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500101 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500102 total_rows = terminal->height;
103 start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500104 }
105
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500106 for (i = 0; i < total_rows; i++)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500107 memcpy(data + (width + 1) * i,
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500108 terminal_get_row(terminal, i), l);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500109
110 free(terminal->data);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500111 }
112
113 terminal->width = width;
114 terminal->height = height;
115 terminal->data = data;
116
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500117 if (terminal->row >= terminal->height)
118 terminal->row = terminal->height - 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500119 if (terminal->column >= terminal->width)
120 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500121 terminal->start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500122}
123
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400124struct color_scheme { struct { double r, g, b, a; } fg, bg; }
125 matrix_colors = { { 0, 0.7, 0, 1 }, { 0, 0, 0, 0.9 } },
126 jbarnes_colors = { { 1, 1, 1, 1 }, { 0, 0, 0, 1 } };
127
Kristian Høgsberg22106762008-12-08 13:50:07 -0500128static void
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500129terminal_draw_contents(struct terminal *terminal)
130{
131 struct rectangle rectangle;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500132 cairo_t *cr;
133 cairo_font_extents_t extents;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500134 int i, top_margin, side_margin;
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500135 cairo_surface_t *surface;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500136 double d;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500137
138 window_get_child_rectangle(terminal->window, &rectangle);
139
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400140 surface = display_create_surface(terminal->display, &rectangle);
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500141 cr = cairo_create(surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500142 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400143 cairo_set_source_rgba(cr,
144 terminal->color_scheme->bg.r,
145 terminal->color_scheme->bg.g,
146 terminal->color_scheme->bg.b,
147 terminal->color_scheme->bg.a);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500148 cairo_paint(cr);
149 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400150 cairo_set_source_rgba(cr,
151 terminal->color_scheme->fg.r,
152 terminal->color_scheme->fg.g,
153 terminal->color_scheme->fg.b,
154 terminal->color_scheme->fg.a);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500155
156 cairo_select_font_face (cr, "mono",
157 CAIRO_FONT_SLANT_NORMAL,
158 CAIRO_FONT_WEIGHT_NORMAL);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500159 cairo_set_font_size(cr, 14);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500160
161 cairo_font_extents(cr, &extents);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500162 side_margin = (rectangle.width - terminal->width * extents.max_x_advance) / 2;
163 top_margin = (rectangle.height - terminal->height * extents.height) / 2;
164
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500165 for (i = 0; i < terminal->height; i++) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500166 cairo_move_to(cr, side_margin,
167 top_margin + extents.ascent + extents.height * i);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500168 cairo_show_text(cr, terminal_get_row(terminal, i));
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500169 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500170
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500171 d = terminal->focused ? 0 : 0.5;
172
173 cairo_set_line_width(cr, 1);
174 cairo_move_to(cr, side_margin + terminal->column * extents.max_x_advance + d,
175 top_margin + terminal->row * extents.height + d);
176 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
177 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
178 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500179 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500180
181 if (terminal->focused)
182 cairo_fill(cr);
183 else
184 cairo_stroke(cr);
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500185
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500186 cairo_destroy(cr);
187
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500188 window_copy_surface(terminal->window,
189 &rectangle,
Kristian Høgsberg2aac3022009-12-21 10:04:53 -0500190 surface);
191
192 cairo_surface_destroy(surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500193}
194
195static void
196terminal_draw(struct terminal *terminal)
197{
Kristian Høgsberg22106762008-12-08 13:50:07 -0500198 struct rectangle rectangle;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500199 int32_t width, height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500200
201 window_get_child_rectangle(terminal->window, &rectangle);
202
Kristian Høgsberg09531622010-06-14 23:22:15 -0400203 width = (rectangle.width - 2 * terminal->margin) /
204 (int32_t) terminal->extents.max_x_advance;
205 height = (rectangle.height - 2 * terminal->margin) /
206 (int32_t) terminal->extents.height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500207 terminal_resize(terminal, width, height);
208
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500209 if (!terminal->fullscreen) {
Kristian Høgsberg09531622010-06-14 23:22:15 -0400210 rectangle.width = terminal->width *
211 terminal->extents.max_x_advance + 2 * terminal->margin;
212 rectangle.height = terminal->height *
213 terminal->extents.height + 2 * terminal->margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500214 window_set_child_size(terminal->window, &rectangle);
215 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500216
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500217 window_draw(terminal->window);
218 terminal_draw_contents(terminal);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500219 window_commit(terminal->window, 0);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500220}
221
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400222static void
223redraw_handler(struct window *window, void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500224{
225 struct terminal *terminal = data;
226
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500227 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500228}
229
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500230#define STATE_NORMAL 0
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500231#define STATE_ESCAPE 1
232
233static void
234terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500235
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500236static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500237handle_escape(struct terminal *terminal)
238{
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500239 char *row, *p;
240 int i, count;
241 int args[10], set[10] = { 0, };
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500242
243 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500244 i = 0;
245 p = &terminal->escape[2];
246 while ((isdigit(*p) || *p == ';') && i < 10) {
247 if (*p == ';') {
248 p++;
249 i++;
250 } else {
251 args[i] = strtol(p, &p, 10);
252 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500253 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500254 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500255
256 switch (*p) {
257 case 'A':
258 count = set[0] ? args[0] : 1;
259 if (terminal->row - count >= 0)
260 terminal->row -= count;
261 else
262 terminal->row = 0;
263 break;
264 case 'B':
265 count = set[0] ? args[0] : 1;
266 if (terminal->row + count < terminal->height)
267 terminal->row += count;
268 else
269 terminal->row = terminal->height;
270 break;
271 case 'C':
272 count = set[0] ? args[0] : 1;
273 if (terminal->column + count < terminal->width)
274 terminal->column += count;
275 else
276 terminal->column = terminal->width;
277 break;
278 case 'D':
279 count = set[0] ? args[0] : 1;
280 if (terminal->column - count >= 0)
281 terminal->column -= count;
282 else
283 terminal->column = 0;
284 break;
285 case 'J':
286 row = terminal_get_row(terminal, terminal->row);
287 memset(&row[terminal->column], 0, terminal->width - terminal->column);
288 for (i = terminal->row + 1; i < terminal->height; i++)
289 memset(terminal_get_row(terminal, i), 0, terminal->width);
290 break;
291 case 'G':
292 if (set[0])
293 terminal->column = args[0] - 1;
294 break;
295 case 'H':
296 case 'f':
297 terminal->row = set[0] ? args[0] - 1 : 0;
298 terminal->column = set[1] ? args[1] - 1 : 0;
299 break;
300 case 'K':
301 row = terminal_get_row(terminal, terminal->row);
302 memset(&row[terminal->column], 0, terminal->width - terminal->column);
303 break;
304 case 'm':
305 /* color, blink, bold etc*/
306 break;
307 case '?':
308 if (strcmp(p, "?25l") == 0) {
309 /* hide cursor */
310 } else if (strcmp(p, "?25h") == 0) {
311 /* show cursor */
312 }
313 break;
314 default:
315 terminal_data(terminal,
316 terminal->escape + 1,
317 terminal->escape_length - 2);
318 break;
319 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500320}
321
322static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500323terminal_data(struct terminal *terminal, const char *data, size_t length)
324{
325 int i;
326 char *row;
327
328 for (i = 0; i < length; i++) {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500329 row = terminal_get_row(terminal, terminal->row);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500330
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500331 if (terminal->state == STATE_ESCAPE) {
332 terminal->escape[terminal->escape_length++] = data[i];
333 if (terminal->escape_length == 2 && data[i] != '[') {
334 /* Bad escape sequence. */
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500335 terminal->state = STATE_NORMAL;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500336 goto cancel_escape;
337 }
338
339 if (isalpha(data[i])) {
340 terminal->state = STATE_NORMAL;
341 handle_escape(terminal);
342 }
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500343 continue;
344 }
345
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500346 cancel_escape:
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500347 switch (data[i]) {
348 case '\r':
349 terminal->column = 0;
350 break;
351 case '\n':
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500352 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500353 if (terminal->row + 1 < terminal->height) {
354 terminal->row++;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500355 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500356 terminal->start++;
357 if (terminal->start == terminal->height)
358 terminal->start = 0;
359 memset(terminal_get_row(terminal, terminal->row),
360 0, terminal->width);
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500361 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500362
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500363 break;
364 case '\t':
365 memset(&row[terminal->column], ' ', -terminal->column & 7);
366 terminal->column = (terminal->column + 7) & ~7;
367 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500368 case '\e':
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500369 terminal->state = STATE_ESCAPE;
370 terminal->escape[0] = '\e';
371 terminal->escape_length = 1;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500372 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500373 case '\b':
374 if (terminal->column > 0)
375 terminal->column--;
376 break;
377 case '\a':
378 /* Bell */
379 break;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500380 default:
381 if (terminal->column < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500382 row[terminal->column++] = data[i] < 32 ? data[i] + 64 : data[i];
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500383 break;
384 }
385 }
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500386
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400387 window_schedule_redraw(terminal->window);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500388}
389
390static void
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400391key_handler(struct window *window, uint32_t key, uint32_t sym,
Kristian Høgsberg55444912009-02-21 14:31:09 -0500392 uint32_t state, uint32_t modifiers, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500393{
394 struct terminal *terminal = data;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400395 char ch[2];
396 int len = 0;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500397
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400398 switch (sym) {
399 case XK_F11:
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500400 if (!state)
401 break;
402 terminal->fullscreen ^= 1;
403 window_set_fullscreen(window, terminal->fullscreen);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400404 window_schedule_redraw(terminal->window);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500405 break;
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400406
407 case XK_Delete:
408 sym = 0x04;
409 case XK_BackSpace:
410 case XK_Tab:
411 case XK_Linefeed:
412 case XK_Clear:
413 case XK_Return:
414 case XK_Pause:
415 case XK_Scroll_Lock:
416 case XK_Sys_Req:
417 case XK_Escape:
418 ch[len++] = sym & 0x7f;
419 break;
420
421 case XK_Shift_L:
422 case XK_Shift_R:
423 case XK_Control_L:
424 case XK_Control_R:
425 case XK_Alt_L:
426 case XK_Alt_R:
427 break;
428
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500429 default:
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400430 if (modifiers & WINDOW_MODIFIER_CONTROL)
431 sym = sym & 0x1f;
432 else if (modifiers & WINDOW_MODIFIER_ALT)
433 ch[len++] = 0x1b;
434 if (sym < 256)
435 ch[len++] = sym;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500436 break;
437 }
Kristian Høgsberg94adf6c2010-06-25 16:50:05 -0400438
439 if (state && len > 0)
440 write(terminal->master, ch, len);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500441}
442
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500443static void
444keyboard_focus_handler(struct window *window,
445 struct wl_input_device *device, void *data)
446{
447 struct terminal *terminal = data;
448
449 terminal->focused = (device != NULL);
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400450 window_schedule_redraw(terminal->window);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500451}
452
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500453static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500454terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500455{
456 struct terminal *terminal;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400457 cairo_surface_t *surface;
458 cairo_t *cr;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500459
460 terminal = malloc(sizeof *terminal);
461 if (terminal == NULL)
462 return terminal;
463
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500464 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500465 terminal->fullscreen = fullscreen;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400466 terminal->color_scheme = &jbarnes_colors;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500467 terminal->window = window_create(display, "Wayland Terminal",
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500468 500, 100, 500, 400);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500469 terminal->display = display;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500470 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500471
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500472 window_set_fullscreen(terminal->window, terminal->fullscreen);
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400473 window_set_user_data(terminal->window, terminal);
474 window_set_redraw_handler(terminal->window, redraw_handler);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500475
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400476 window_set_key_handler(terminal->window, key_handler);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500477 window_set_keyboard_focus_handler(terminal->window,
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400478 keyboard_focus_handler);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500479
Kristian Høgsberg09531622010-06-14 23:22:15 -0400480 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
481 cr = cairo_create(surface);
482 cairo_select_font_face (cr, "mono",
483 CAIRO_FONT_SLANT_NORMAL,
484 CAIRO_FONT_WEIGHT_NORMAL);
485 cairo_set_font_size(cr, 14);
486 cairo_font_extents(cr, &terminal->extents);
487 cairo_destroy(cr);
488 cairo_surface_destroy(surface);
489
Kristian Høgsberg22106762008-12-08 13:50:07 -0500490 terminal_draw(terminal);
491
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500492 return terminal;
493}
494
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500495static gboolean
496io_handler(GIOChannel *source,
497 GIOCondition condition,
498 gpointer data)
499{
500 struct terminal *terminal = data;
501 gchar buffer[256];
502 gsize bytes_read;
503 GError *error = NULL;
504
505 g_io_channel_read_chars(source, buffer, sizeof buffer,
506 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500507
508 terminal_data(terminal, buffer, bytes_read);
509
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500510 return TRUE;
511}
512
513static int
514terminal_run(struct terminal *terminal, const char *path)
515{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500516 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500517 pid_t pid;
518
519 pid = forkpty(&master, NULL, NULL, NULL);
520 if (pid == 0) {
Kristian Høgsbergc8c5d582008-12-18 14:50:08 -0500521 setenv("TERM", "vt100", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500522 if (execl(path, path, NULL)) {
523 printf("exec failed: %m\n");
524 exit(EXIT_FAILURE);
525 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500526 } else if (pid < 0) {
527 fprintf(stderr, "failed to fork and create pty (%m).\n");
528 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500529 }
530
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500531 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500532 terminal->channel = g_io_channel_unix_new(master);
533 fcntl(master, F_SETFL, O_NONBLOCK);
534 g_io_add_watch(terminal->channel, G_IO_IN,
535 io_handler, terminal);
536
537 return 0;
538}
539
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500540static const GOptionEntry option_entries[] = {
541 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
542 &option_fullscreen, "Run in fullscreen mode" },
543 { NULL }
544};
545
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500546int main(int argc, char *argv[])
547{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500548 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500549 struct terminal *terminal;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500550
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400551 d = display_create(&argc, &argv, option_entries);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500552
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500553 terminal = terminal_create(d, option_fullscreen);
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500554 if (terminal_run(terminal, "/bin/bash"))
555 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500556
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400557 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500558
559 return 0;
560}