blob: 48e137b4a16145f24b7cc2affd3553dc78e0e06e [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øgsbergd2412e22008-12-15 20:35:24 -050053 struct wl_compositor *compositor;
Kristian Høgsberg721f09f2008-12-08 11:13:26 -050054 int redraw_scheduled, redraw_pending;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050055 char *data;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050056 int width, height, start, row, column;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050057 int fd, master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050058 GIOChannel *channel;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050059 uint32_t modifiers;
Kristian Høgsberg17809b12008-12-08 12:20:40 -050060 char escape[64];
61 int escape_length;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -050062 int state;
Kristian Høgsberg1584c572008-12-08 12:59:37 -050063 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050064 int fullscreen;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050065 int focused;
Kristian Høgsberg12308a42009-09-28 13:08:50 -040066 struct color_scheme *color_scheme;
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øgsberg2aac3022009-12-21 10:04:53 -0500140 surface = window_create_surface(terminal->window, &rectangle);
141 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;
199 cairo_surface_t *surface;
200 cairo_font_extents_t extents;
201 cairo_t *cr;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500202 int32_t width, height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500203
204 window_get_child_rectangle(terminal->window, &rectangle);
205
206 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
207 cr = cairo_create(surface);
208 cairo_select_font_face (cr, "mono",
209 CAIRO_FONT_SLANT_NORMAL,
210 CAIRO_FONT_WEIGHT_NORMAL);
211 cairo_set_font_size(cr, 14);
212 cairo_font_extents(cr, &extents);
213 cairo_destroy(cr);
214 cairo_surface_destroy(surface);
215
216 width = (rectangle.width - 2 * terminal->margin) / (int32_t) extents.max_x_advance;
217 height = (rectangle.height - 2 * terminal->margin) / (int32_t) extents.height;
218 terminal_resize(terminal, width, height);
219
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500220 if (!terminal->fullscreen) {
221 rectangle.width = terminal->width * extents.max_x_advance + 2 * terminal->margin;
222 rectangle.height = terminal->height * extents.height + 2 * terminal->margin;
223 window_set_child_size(terminal->window, &rectangle);
224 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500225
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500226 window_draw(terminal->window);
227 terminal_draw_contents(terminal);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500228 window_commit(terminal->window, 0);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500229}
230
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500231static gboolean
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500232idle_redraw(void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500233{
234 struct terminal *terminal = data;
235
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500236 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500237
238 return FALSE;
239}
240
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500241#define STATE_NORMAL 0
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500242#define STATE_ESCAPE 1
243
244static void
245terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500246
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500247static void
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500248terminal_schedule_redraw(struct terminal *terminal)
249{
250 if (!terminal->redraw_scheduled) {
251 g_idle_add(idle_redraw, terminal);
252 terminal->redraw_scheduled = 1;
253 } else {
254 terminal->redraw_pending = 1;
255 }
256}
257
258static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500259handle_escape(struct terminal *terminal)
260{
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500261 char *row, *p;
262 int i, count;
263 int args[10], set[10] = { 0, };
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500264
265 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500266 i = 0;
267 p = &terminal->escape[2];
268 while ((isdigit(*p) || *p == ';') && i < 10) {
269 if (*p == ';') {
270 p++;
271 i++;
272 } else {
273 args[i] = strtol(p, &p, 10);
274 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500275 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500276 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500277
278 switch (*p) {
279 case 'A':
280 count = set[0] ? args[0] : 1;
281 if (terminal->row - count >= 0)
282 terminal->row -= count;
283 else
284 terminal->row = 0;
285 break;
286 case 'B':
287 count = set[0] ? args[0] : 1;
288 if (terminal->row + count < terminal->height)
289 terminal->row += count;
290 else
291 terminal->row = terminal->height;
292 break;
293 case 'C':
294 count = set[0] ? args[0] : 1;
295 if (terminal->column + count < terminal->width)
296 terminal->column += count;
297 else
298 terminal->column = terminal->width;
299 break;
300 case 'D':
301 count = set[0] ? args[0] : 1;
302 if (terminal->column - count >= 0)
303 terminal->column -= count;
304 else
305 terminal->column = 0;
306 break;
307 case 'J':
308 row = terminal_get_row(terminal, terminal->row);
309 memset(&row[terminal->column], 0, terminal->width - terminal->column);
310 for (i = terminal->row + 1; i < terminal->height; i++)
311 memset(terminal_get_row(terminal, i), 0, terminal->width);
312 break;
313 case 'G':
314 if (set[0])
315 terminal->column = args[0] - 1;
316 break;
317 case 'H':
318 case 'f':
319 terminal->row = set[0] ? args[0] - 1 : 0;
320 terminal->column = set[1] ? args[1] - 1 : 0;
321 break;
322 case 'K':
323 row = terminal_get_row(terminal, terminal->row);
324 memset(&row[terminal->column], 0, terminal->width - terminal->column);
325 break;
326 case 'm':
327 /* color, blink, bold etc*/
328 break;
329 case '?':
330 if (strcmp(p, "?25l") == 0) {
331 /* hide cursor */
332 } else if (strcmp(p, "?25h") == 0) {
333 /* show cursor */
334 }
335 break;
336 default:
337 terminal_data(terminal,
338 terminal->escape + 1,
339 terminal->escape_length - 2);
340 break;
341 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500342}
343
344static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500345terminal_data(struct terminal *terminal, const char *data, size_t length)
346{
347 int i;
348 char *row;
349
350 for (i = 0; i < length; i++) {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500351 row = terminal_get_row(terminal, terminal->row);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500352
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500353 if (terminal->state == STATE_ESCAPE) {
354 terminal->escape[terminal->escape_length++] = data[i];
355 if (terminal->escape_length == 2 && data[i] != '[') {
356 /* Bad escape sequence. */
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500357 terminal->state = STATE_NORMAL;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500358 goto cancel_escape;
359 }
360
361 if (isalpha(data[i])) {
362 terminal->state = STATE_NORMAL;
363 handle_escape(terminal);
364 }
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500365 continue;
366 }
367
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500368 cancel_escape:
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500369 switch (data[i]) {
370 case '\r':
371 terminal->column = 0;
372 break;
373 case '\n':
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500374 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500375 if (terminal->row + 1 < terminal->height) {
376 terminal->row++;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500377 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500378 terminal->start++;
379 if (terminal->start == terminal->height)
380 terminal->start = 0;
381 memset(terminal_get_row(terminal, terminal->row),
382 0, terminal->width);
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500383 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500384
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500385 break;
386 case '\t':
387 memset(&row[terminal->column], ' ', -terminal->column & 7);
388 terminal->column = (terminal->column + 7) & ~7;
389 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500390 case '\e':
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500391 terminal->state = STATE_ESCAPE;
392 terminal->escape[0] = '\e';
393 terminal->escape_length = 1;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500394 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500395 case '\b':
396 if (terminal->column > 0)
397 terminal->column--;
398 break;
399 case '\a':
400 /* Bell */
401 break;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500402 default:
403 if (terminal->column < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500404 row[terminal->column++] = data[i] < 32 ? data[i] + 64 : data[i];
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500405 break;
406 }
407 }
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500408
409 terminal_schedule_redraw(terminal);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500410}
411
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500412static void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500413resize_handler(struct window *window, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500414{
415 struct terminal *terminal = data;
416
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500417 terminal_schedule_redraw(terminal);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500418}
419
420static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500421handle_acknowledge(void *data,
422 struct wl_compositor *compositor,
423 uint32_t key, uint32_t frame)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500424{
425 struct terminal *terminal = data;
426
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500427 terminal->redraw_scheduled = 0;
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500428 if (terminal->redraw_pending) {
429 terminal->redraw_pending = 0;
430 terminal_schedule_redraw(terminal);
431 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500432}
433
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500434static void
435handle_frame(void *data,
436 struct wl_compositor *compositor,
437 uint32_t frame, uint32_t timestamp)
438{
439}
440
441static const struct wl_compositor_listener compositor_listener = {
442 handle_acknowledge,
443 handle_frame,
444};
445
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500446static void
Kristian Høgsberg55444912009-02-21 14:31:09 -0500447key_handler(struct window *window, uint32_t key, uint32_t unicode,
448 uint32_t state, uint32_t modifiers, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500449{
450 struct terminal *terminal = data;
Kristian Høgsberg55444912009-02-21 14:31:09 -0500451 char ch = unicode;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500452
453 switch (key) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500454 case KEY_F11:
455 if (!state)
456 break;
457 terminal->fullscreen ^= 1;
458 window_set_fullscreen(window, terminal->fullscreen);
459 terminal_schedule_redraw(terminal);
460 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500461 default:
Kristian Høgsberg55444912009-02-21 14:31:09 -0500462 if (state && unicode)
463 write(terminal->master, &ch, 1);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500464 break;
465 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500466}
467
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500468static void
469keyboard_focus_handler(struct window *window,
470 struct wl_input_device *device, void *data)
471{
472 struct terminal *terminal = data;
473
474 terminal->focused = (device != NULL);
475 terminal_schedule_redraw(terminal);
476}
477
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500478static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500479terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500480{
481 struct terminal *terminal;
482
483 terminal = malloc(sizeof *terminal);
484 if (terminal == NULL)
485 return terminal;
486
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500487 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500488 terminal->fullscreen = fullscreen;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400489 terminal->color_scheme = &jbarnes_colors;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500490 terminal->window = window_create(display, "Wayland Terminal",
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500491 500, 100, 500, 400);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500492 terminal->display = display;
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500493 terminal->redraw_scheduled = 1;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500494 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500495
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500496 terminal->compositor = display_get_compositor(display);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500497 window_set_fullscreen(terminal->window, terminal->fullscreen);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500498 window_set_resize_handler(terminal->window, resize_handler, terminal);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500499
500 wl_compositor_add_listener(terminal->compositor,
501 &compositor_listener, terminal);
502
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500503 window_set_key_handler(terminal->window, key_handler, terminal);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500504 window_set_keyboard_focus_handler(terminal->window,
505 keyboard_focus_handler, terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500506
Kristian Høgsberg22106762008-12-08 13:50:07 -0500507 terminal_draw(terminal);
508
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500509 return terminal;
510}
511
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500512static gboolean
513io_handler(GIOChannel *source,
514 GIOCondition condition,
515 gpointer data)
516{
517 struct terminal *terminal = data;
518 gchar buffer[256];
519 gsize bytes_read;
520 GError *error = NULL;
521
522 g_io_channel_read_chars(source, buffer, sizeof buffer,
523 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500524
525 terminal_data(terminal, buffer, bytes_read);
526
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500527 return TRUE;
528}
529
530static int
531terminal_run(struct terminal *terminal, const char *path)
532{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500533 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500534 pid_t pid;
535
536 pid = forkpty(&master, NULL, NULL, NULL);
537 if (pid == 0) {
Kristian Høgsbergc8c5d582008-12-18 14:50:08 -0500538 setenv("TERM", "vt100", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500539 if (execl(path, path, NULL)) {
540 printf("exec failed: %m\n");
541 exit(EXIT_FAILURE);
542 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500543 } else if (pid < 0) {
544 fprintf(stderr, "failed to fork and create pty (%m).\n");
545 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500546 }
547
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500548 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500549 terminal->channel = g_io_channel_unix_new(master);
550 fcntl(master, F_SETFL, O_NONBLOCK);
551 g_io_add_watch(terminal->channel, G_IO_IN,
552 io_handler, terminal);
553
554 return 0;
555}
556
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500557static const GOptionEntry option_entries[] = {
558 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
559 &option_fullscreen, "Run in fullscreen mode" },
560 { NULL }
561};
562
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500563int main(int argc, char *argv[])
564{
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500565 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500566 struct terminal *terminal;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500567
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400568 d = display_create(&argc, &argv, option_entries);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500569
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500570 terminal = terminal_create(d, option_fullscreen);
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500571 if (terminal_run(terminal, "/bin/bash"))
572 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500573
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400574 display_run(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500575
576 return 0;
577}