blob: d1e57fc6c0734c1a8fe161fb40266f634eea765f [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 -050045static const char gem_device[] = "/dev/dri/card0";
46static const char socket_name[] = "\0wayland";
47
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050048#define MOD_SHIFT 0x01
49#define MOD_ALT 0x02
50#define MOD_CTRL 0x04
51
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050052struct terminal {
53 struct window *window;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050054 struct display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050055 struct wl_compositor *compositor;
Kristian Høgsberg721f09f2008-12-08 11:13:26 -050056 int redraw_scheduled, redraw_pending;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050057 char *data;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050058 int width, height, start, row, column;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050059 int fd, master;
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050060 cairo_surface_t *surface;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050061 GIOChannel *channel;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050062 uint32_t modifiers;
Kristian Høgsberg17809b12008-12-08 12:20:40 -050063 char escape[64];
64 int escape_length;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -050065 int state;
Kristian Høgsberg1584c572008-12-08 12:59:37 -050066 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050067 int fullscreen;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -050068 int focused;
Kristian Høgsberg12308a42009-09-28 13:08:50 -040069 struct color_scheme *color_scheme;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050070};
71
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050072static char *
73terminal_get_row(struct terminal *terminal, int row)
74{
75 int index;
76
77 index = (row + terminal->start) % terminal->height;
78
79 return &terminal->data[index * (terminal->width + 1)];
80}
81
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050082static void
Kristian Høgsberg22106762008-12-08 13:50:07 -050083terminal_resize(struct terminal *terminal, int width, int height)
84{
85 size_t size;
86 char *data;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050087 int i, l, total_rows, start;
Kristian Høgsberg22106762008-12-08 13:50:07 -050088
89 if (terminal->width == width && terminal->height == height)
90 return;
91
92 size = (width + 1) * height;
93 data = malloc(size);
94 memset(data, 0, size);
95 if (terminal->data) {
96 if (width > terminal->width)
97 l = terminal->width;
98 else
99 l = width;
100
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500101 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500102 total_rows = height;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500103 start = terminal->height - height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500104 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500105 total_rows = terminal->height;
106 start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500107 }
108
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500109 for (i = 0; i < total_rows; i++)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500110 memcpy(data + (width + 1) * i,
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500111 terminal_get_row(terminal, i), l);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500112
113 free(terminal->data);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500114 }
115
116 terminal->width = width;
117 terminal->height = height;
118 terminal->data = data;
119
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500120 if (terminal->row >= terminal->height)
121 terminal->row = terminal->height - 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500122 if (terminal->column >= terminal->width)
123 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500124 terminal->start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500125}
126
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400127struct color_scheme { struct { double r, g, b, a; } fg, bg; }
128 matrix_colors = { { 0, 0.7, 0, 1 }, { 0, 0, 0, 0.9 } },
129 jbarnes_colors = { { 1, 1, 1, 1 }, { 0, 0, 0, 1 } };
130
Kristian Høgsberg22106762008-12-08 13:50:07 -0500131static void
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500132terminal_draw_contents(struct terminal *terminal)
133{
134 struct rectangle rectangle;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500135 cairo_t *cr;
136 cairo_font_extents_t extents;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500137 int i, top_margin, side_margin;
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500138 double d;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500139
140 window_get_child_rectangle(terminal->window, &rectangle);
141
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500142 terminal->surface =
143 window_create_surface(terminal->window, &rectangle);
144 cr = cairo_create(terminal->surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500145 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400146 cairo_set_source_rgba(cr,
147 terminal->color_scheme->bg.r,
148 terminal->color_scheme->bg.g,
149 terminal->color_scheme->bg.b,
150 terminal->color_scheme->bg.a);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500151 cairo_paint(cr);
152 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400153 cairo_set_source_rgba(cr,
154 terminal->color_scheme->fg.r,
155 terminal->color_scheme->fg.g,
156 terminal->color_scheme->fg.b,
157 terminal->color_scheme->fg.a);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500158
159 cairo_select_font_face (cr, "mono",
160 CAIRO_FONT_SLANT_NORMAL,
161 CAIRO_FONT_WEIGHT_NORMAL);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500162 cairo_set_font_size(cr, 14);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500163
164 cairo_font_extents(cr, &extents);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500165 side_margin = (rectangle.width - terminal->width * extents.max_x_advance) / 2;
166 top_margin = (rectangle.height - terminal->height * extents.height) / 2;
167
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500168 for (i = 0; i < terminal->height; i++) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500169 cairo_move_to(cr, side_margin,
170 top_margin + extents.ascent + extents.height * i);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500171 cairo_show_text(cr, terminal_get_row(terminal, i));
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500172 }
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500173
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500174 d = terminal->focused ? 0 : 0.5;
175
176 cairo_set_line_width(cr, 1);
177 cairo_move_to(cr, side_margin + terminal->column * extents.max_x_advance + d,
178 top_margin + terminal->row * extents.height + d);
179 cairo_rel_line_to(cr, extents.max_x_advance - 2 * d, 0);
180 cairo_rel_line_to(cr, 0, extents.height - 2 * d);
181 cairo_rel_line_to(cr, -extents.max_x_advance + 2 * d, 0);
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500182 cairo_close_path(cr);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500183
184 if (terminal->focused)
185 cairo_fill(cr);
186 else
187 cairo_stroke(cr);
Kristian Høgsbergb0b82e22009-02-21 15:42:25 -0500188
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500189 cairo_destroy(cr);
190
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500191 window_copy_surface(terminal->window,
192 &rectangle,
193 terminal->surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500194}
195
196static void
197terminal_draw(struct terminal *terminal)
198{
Kristian Høgsberg22106762008-12-08 13:50:07 -0500199 struct rectangle rectangle;
200 cairo_surface_t *surface;
201 cairo_font_extents_t extents;
202 cairo_t *cr;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500203 int32_t width, height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500204
205 window_get_child_rectangle(terminal->window, &rectangle);
206
207 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
208 cr = cairo_create(surface);
209 cairo_select_font_face (cr, "mono",
210 CAIRO_FONT_SLANT_NORMAL,
211 CAIRO_FONT_WEIGHT_NORMAL);
212 cairo_set_font_size(cr, 14);
213 cairo_font_extents(cr, &extents);
214 cairo_destroy(cr);
215 cairo_surface_destroy(surface);
216
217 width = (rectangle.width - 2 * terminal->margin) / (int32_t) extents.max_x_advance;
218 height = (rectangle.height - 2 * terminal->margin) / (int32_t) extents.height;
219 terminal_resize(terminal, width, height);
220
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500221 if (!terminal->fullscreen) {
222 rectangle.width = terminal->width * extents.max_x_advance + 2 * terminal->margin;
223 rectangle.height = terminal->height * extents.height + 2 * terminal->margin;
224 window_set_child_size(terminal->window, &rectangle);
225 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500226
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500227 window_draw(terminal->window);
228 terminal_draw_contents(terminal);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500229 wl_compositor_commit(terminal->compositor, 0);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500230}
231
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500232static gboolean
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500233idle_redraw(void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500234{
235 struct terminal *terminal = data;
236
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500237 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500238
239 return FALSE;
240}
241
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500242#define STATE_NORMAL 0
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500243#define STATE_ESCAPE 1
244
245static void
246terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500247
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500248static void
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500249terminal_schedule_redraw(struct terminal *terminal)
250{
251 if (!terminal->redraw_scheduled) {
252 g_idle_add(idle_redraw, terminal);
253 terminal->redraw_scheduled = 1;
254 } else {
255 terminal->redraw_pending = 1;
256 }
257}
258
259static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500260handle_escape(struct terminal *terminal)
261{
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500262 char *row, *p;
263 int i, count;
264 int args[10], set[10] = { 0, };
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500265
266 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500267 i = 0;
268 p = &terminal->escape[2];
269 while ((isdigit(*p) || *p == ';') && i < 10) {
270 if (*p == ';') {
271 p++;
272 i++;
273 } else {
274 args[i] = strtol(p, &p, 10);
275 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500276 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500277 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500278
279 switch (*p) {
280 case 'A':
281 count = set[0] ? args[0] : 1;
282 if (terminal->row - count >= 0)
283 terminal->row -= count;
284 else
285 terminal->row = 0;
286 break;
287 case 'B':
288 count = set[0] ? args[0] : 1;
289 if (terminal->row + count < terminal->height)
290 terminal->row += count;
291 else
292 terminal->row = terminal->height;
293 break;
294 case 'C':
295 count = set[0] ? args[0] : 1;
296 if (terminal->column + count < terminal->width)
297 terminal->column += count;
298 else
299 terminal->column = terminal->width;
300 break;
301 case 'D':
302 count = set[0] ? args[0] : 1;
303 if (terminal->column - count >= 0)
304 terminal->column -= count;
305 else
306 terminal->column = 0;
307 break;
308 case 'J':
309 row = terminal_get_row(terminal, terminal->row);
310 memset(&row[terminal->column], 0, terminal->width - terminal->column);
311 for (i = terminal->row + 1; i < terminal->height; i++)
312 memset(terminal_get_row(terminal, i), 0, terminal->width);
313 break;
314 case 'G':
315 if (set[0])
316 terminal->column = args[0] - 1;
317 break;
318 case 'H':
319 case 'f':
320 terminal->row = set[0] ? args[0] - 1 : 0;
321 terminal->column = set[1] ? args[1] - 1 : 0;
322 break;
323 case 'K':
324 row = terminal_get_row(terminal, terminal->row);
325 memset(&row[terminal->column], 0, terminal->width - terminal->column);
326 break;
327 case 'm':
328 /* color, blink, bold etc*/
329 break;
330 case '?':
331 if (strcmp(p, "?25l") == 0) {
332 /* hide cursor */
333 } else if (strcmp(p, "?25h") == 0) {
334 /* show cursor */
335 }
336 break;
337 default:
338 terminal_data(terminal,
339 terminal->escape + 1,
340 terminal->escape_length - 2);
341 break;
342 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500343}
344
345static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500346terminal_data(struct terminal *terminal, const char *data, size_t length)
347{
348 int i;
349 char *row;
350
351 for (i = 0; i < length; i++) {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500352 row = terminal_get_row(terminal, terminal->row);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500353
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500354 if (terminal->state == STATE_ESCAPE) {
355 terminal->escape[terminal->escape_length++] = data[i];
356 if (terminal->escape_length == 2 && data[i] != '[') {
357 /* Bad escape sequence. */
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500358 terminal->state = STATE_NORMAL;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500359 goto cancel_escape;
360 }
361
362 if (isalpha(data[i])) {
363 terminal->state = STATE_NORMAL;
364 handle_escape(terminal);
365 }
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500366 continue;
367 }
368
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500369 cancel_escape:
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500370 switch (data[i]) {
371 case '\r':
372 terminal->column = 0;
373 break;
374 case '\n':
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500375 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500376 if (terminal->row + 1 < terminal->height) {
377 terminal->row++;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500378 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500379 terminal->start++;
380 if (terminal->start == terminal->height)
381 terminal->start = 0;
382 memset(terminal_get_row(terminal, terminal->row),
383 0, terminal->width);
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500384 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500385
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500386 break;
387 case '\t':
388 memset(&row[terminal->column], ' ', -terminal->column & 7);
389 terminal->column = (terminal->column + 7) & ~7;
390 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500391 case '\e':
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500392 terminal->state = STATE_ESCAPE;
393 terminal->escape[0] = '\e';
394 terminal->escape_length = 1;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500395 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500396 case '\b':
397 if (terminal->column > 0)
398 terminal->column--;
399 break;
400 case '\a':
401 /* Bell */
402 break;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500403 default:
404 if (terminal->column < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500405 row[terminal->column++] = data[i] < 32 ? data[i] + 64 : data[i];
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500406 break;
407 }
408 }
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500409
410 terminal_schedule_redraw(terminal);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500411}
412
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500413static void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500414resize_handler(struct window *window, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500415{
416 struct terminal *terminal = data;
417
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500418 terminal_schedule_redraw(terminal);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500419}
420
421static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500422handle_acknowledge(void *data,
423 struct wl_compositor *compositor,
424 uint32_t key, uint32_t frame)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500425{
426 struct terminal *terminal = data;
427
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500428 terminal->redraw_scheduled = 0;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500429 if (key == 0)
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500430 cairo_surface_destroy(terminal->surface);
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500431
432 if (terminal->redraw_pending) {
433 terminal->redraw_pending = 0;
434 terminal_schedule_redraw(terminal);
435 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500436}
437
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500438static void
439handle_frame(void *data,
440 struct wl_compositor *compositor,
441 uint32_t frame, uint32_t timestamp)
442{
443}
444
445static const struct wl_compositor_listener compositor_listener = {
446 handle_acknowledge,
447 handle_frame,
448};
449
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500450static void
Kristian Høgsberg55444912009-02-21 14:31:09 -0500451key_handler(struct window *window, uint32_t key, uint32_t unicode,
452 uint32_t state, uint32_t modifiers, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500453{
454 struct terminal *terminal = data;
Kristian Høgsberg55444912009-02-21 14:31:09 -0500455 char ch = unicode;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500456
457 switch (key) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500458 case KEY_F11:
459 if (!state)
460 break;
461 terminal->fullscreen ^= 1;
462 window_set_fullscreen(window, terminal->fullscreen);
463 terminal_schedule_redraw(terminal);
464 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500465 default:
Kristian Høgsberg55444912009-02-21 14:31:09 -0500466 if (state && unicode)
467 write(terminal->master, &ch, 1);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500468 break;
469 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500470}
471
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500472static void
473keyboard_focus_handler(struct window *window,
474 struct wl_input_device *device, void *data)
475{
476 struct terminal *terminal = data;
477
478 terminal->focused = (device != NULL);
479 terminal_schedule_redraw(terminal);
480}
481
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500482static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500483terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500484{
485 struct terminal *terminal;
486
487 terminal = malloc(sizeof *terminal);
488 if (terminal == NULL)
489 return terminal;
490
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500491 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500492 terminal->fullscreen = fullscreen;
Kristian Høgsberg12308a42009-09-28 13:08:50 -0400493 terminal->color_scheme = &jbarnes_colors;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500494 terminal->window = window_create(display, "Wayland Terminal",
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500495 500, 100, 500, 400);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500496 terminal->display = display;
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500497 terminal->redraw_scheduled = 1;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500498 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500499
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500500 terminal->compositor = display_get_compositor(display);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500501 window_set_fullscreen(terminal->window, terminal->fullscreen);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500502 window_set_resize_handler(terminal->window, resize_handler, terminal);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500503
504 wl_compositor_add_listener(terminal->compositor,
505 &compositor_listener, terminal);
506
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500507 window_set_key_handler(terminal->window, key_handler, terminal);
Kristian Høgsberg3c248cc2009-02-22 23:01:35 -0500508 window_set_keyboard_focus_handler(terminal->window,
509 keyboard_focus_handler, terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500510
Kristian Høgsberg22106762008-12-08 13:50:07 -0500511 terminal_draw(terminal);
512
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500513 return terminal;
514}
515
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500516static gboolean
517io_handler(GIOChannel *source,
518 GIOCondition condition,
519 gpointer data)
520{
521 struct terminal *terminal = data;
522 gchar buffer[256];
523 gsize bytes_read;
524 GError *error = NULL;
525
526 g_io_channel_read_chars(source, buffer, sizeof buffer,
527 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500528
529 terminal_data(terminal, buffer, bytes_read);
530
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500531 return TRUE;
532}
533
534static int
535terminal_run(struct terminal *terminal, const char *path)
536{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500537 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500538 pid_t pid;
539
540 pid = forkpty(&master, NULL, NULL, NULL);
541 if (pid == 0) {
Kristian Høgsbergc8c5d582008-12-18 14:50:08 -0500542 setenv("TERM", "vt100", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500543 if (execl(path, path, NULL)) {
544 printf("exec failed: %m\n");
545 exit(EXIT_FAILURE);
546 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500547 } else if (pid < 0) {
548 fprintf(stderr, "failed to fork and create pty (%m).\n");
549 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500550 }
551
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500552 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500553 terminal->channel = g_io_channel_unix_new(master);
554 fcntl(master, F_SETFL, O_NONBLOCK);
555 g_io_add_watch(terminal->channel, G_IO_IN,
556 io_handler, terminal);
557
558 return 0;
559}
560
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500561static const GOptionEntry option_entries[] = {
562 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
563 &option_fullscreen, "Run in fullscreen mode" },
564 { NULL }
565};
566
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500567int main(int argc, char *argv[])
568{
569 struct wl_display *display;
570 int fd;
571 GMainLoop *loop;
572 GSource *source;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500573 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500574 struct terminal *terminal;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500575 GOptionContext *context;
576 GError *error;
577
578 context = g_option_context_new(NULL);
579 g_option_context_add_main_entries(context, option_entries, "Wayland Terminal");
580 if (!g_option_context_parse(context, &argc, &argv, &error)) {
581 fprintf(stderr, "option parsing failed: %s\n", error->message);
582 exit(EXIT_FAILURE);
583 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500584
585 fd = open(gem_device, O_RDWR);
586 if (fd < 0) {
587 fprintf(stderr, "drm open failed: %m\n");
588 return -1;
589 }
590
591 display = wl_display_create(socket_name, sizeof socket_name);
592 if (display == NULL) {
593 fprintf(stderr, "failed to create display: %m\n");
594 return -1;
595 }
596
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500597 d = display_create(display, fd);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500598 loop = g_main_loop_new(NULL, FALSE);
599 source = wl_glib_source_new(display);
600 g_source_attach(source, NULL);
601
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500602 terminal = terminal_create(d, option_fullscreen);
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500603 if (terminal_run(terminal, "/bin/bash"))
604 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500605
606 g_main_loop_run(loop);
607
608 return 0;
609}