blob: 10108366f8908cf42d3d6e157b8eceb4b9a2d96c [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øgsberg0c4457f2008-12-07 19:59:11 -050036
37#include <GL/gl.h>
38#include <eagle.h>
39
40#include "wayland-client.h"
41#include "wayland-glib.h"
42
43#include "cairo-util.h"
44#include "window.h"
45
Kristian Høgsberg0395f302008-12-22 12:14:50 -050046static int option_fullscreen;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050047static const char gem_device[] = "/dev/dri/card0";
48static const char socket_name[] = "\0wayland";
49
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050050#define MOD_SHIFT 0x01
51#define MOD_ALT 0x02
52#define MOD_CTRL 0x04
53
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050054struct terminal {
55 struct window *window;
56 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050057 struct wl_compositor *compositor;
Kristian Høgsberg721f09f2008-12-08 11:13:26 -050058 int redraw_scheduled, redraw_pending;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050059 char *data;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050060 int width, height, start, row, column;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050061 int fd, master;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050062 struct buffer *buffer;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050063 GIOChannel *channel;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050064 uint32_t modifiers;
Kristian Høgsberg17809b12008-12-08 12:20:40 -050065 char escape[64];
66 int escape_length;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -050067 int state;
Kristian Høgsberg1584c572008-12-08 12:59:37 -050068 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050069 int fullscreen;
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
127static void
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500128terminal_draw_contents(struct terminal *terminal)
129{
130 struct rectangle rectangle;
131 cairo_surface_t *surface;
132 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øgsberg44e3c5e2008-12-07 21:51:58 -0500135
136 window_get_child_rectangle(terminal->window, &rectangle);
137
138 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
139 rectangle.width, rectangle.height);
140 cr = cairo_create(surface);
141 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
142 cairo_set_source_rgba(cr, 0, 0, 0, 0.9);
143 cairo_paint(cr);
144 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsberg6e0a2f82008-12-08 14:06:56 -0500145 cairo_set_source_rgba(cr, 0, 0.7, 0, 1);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500146
147 cairo_select_font_face (cr, "mono",
148 CAIRO_FONT_SLANT_NORMAL,
149 CAIRO_FONT_WEIGHT_NORMAL);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500150 cairo_set_font_size(cr, 14);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500151
152 cairo_font_extents(cr, &extents);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500153 side_margin = (rectangle.width - terminal->width * extents.max_x_advance) / 2;
154 top_margin = (rectangle.height - terminal->height * extents.height) / 2;
155
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500156 for (i = 0; i < terminal->height; i++) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500157 cairo_move_to(cr, side_margin,
158 top_margin + extents.ascent + extents.height * i);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500159 cairo_show_text(cr, terminal_get_row(terminal, i));
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500160 }
161 cairo_destroy(cr);
162
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500163 terminal->buffer = buffer_create_from_cairo_surface(terminal->fd, surface);
164 cairo_surface_destroy(surface);
165
166 window_copy(terminal->window,
167 &rectangle,
168 terminal->buffer->name, terminal->buffer->stride);
169}
170
171static void
172terminal_draw(struct terminal *terminal)
173{
Kristian Høgsberg22106762008-12-08 13:50:07 -0500174 struct rectangle rectangle;
175 cairo_surface_t *surface;
176 cairo_font_extents_t extents;
177 cairo_t *cr;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500178 int32_t width, height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500179
180 window_get_child_rectangle(terminal->window, &rectangle);
181
182 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
183 cr = cairo_create(surface);
184 cairo_select_font_face (cr, "mono",
185 CAIRO_FONT_SLANT_NORMAL,
186 CAIRO_FONT_WEIGHT_NORMAL);
187 cairo_set_font_size(cr, 14);
188 cairo_font_extents(cr, &extents);
189 cairo_destroy(cr);
190 cairo_surface_destroy(surface);
191
192 width = (rectangle.width - 2 * terminal->margin) / (int32_t) extents.max_x_advance;
193 height = (rectangle.height - 2 * terminal->margin) / (int32_t) extents.height;
194 terminal_resize(terminal, width, height);
195
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500196 if (!terminal->fullscreen) {
197 rectangle.width = terminal->width * extents.max_x_advance + 2 * terminal->margin;
198 rectangle.height = terminal->height * extents.height + 2 * terminal->margin;
199 window_set_child_size(terminal->window, &rectangle);
200 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500201
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500202 window_draw(terminal->window);
203 terminal_draw_contents(terminal);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500204 wl_compositor_commit(terminal->compositor, 0);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500205}
206
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500207static gboolean
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500208idle_redraw(void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500209{
210 struct terminal *terminal = data;
211
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500212 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500213
214 return FALSE;
215}
216
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500217#define STATE_NORMAL 0
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500218#define STATE_ESCAPE 1
219
220static void
221terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500222
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500223static void
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500224terminal_schedule_redraw(struct terminal *terminal)
225{
226 if (!terminal->redraw_scheduled) {
227 g_idle_add(idle_redraw, terminal);
228 terminal->redraw_scheduled = 1;
229 } else {
230 terminal->redraw_pending = 1;
231 }
232}
233
234static void
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500235terminal_data(struct terminal *terminal, const char *data, size_t length);
236
237static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500238handle_escape(struct terminal *terminal)
239{
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500240 char *row, *p;
241 int i, count;
242 int args[10], set[10] = { 0, };
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500243
244 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500245 i = 0;
246 p = &terminal->escape[2];
247 while ((isdigit(*p) || *p == ';') && i < 10) {
248 if (*p == ';') {
249 p++;
250 i++;
251 } else {
252 args[i] = strtol(p, &p, 10);
253 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500254 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500255 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500256
257 switch (*p) {
258 case 'A':
259 count = set[0] ? args[0] : 1;
260 if (terminal->row - count >= 0)
261 terminal->row -= count;
262 else
263 terminal->row = 0;
264 break;
265 case 'B':
266 count = set[0] ? args[0] : 1;
267 if (terminal->row + count < terminal->height)
268 terminal->row += count;
269 else
270 terminal->row = terminal->height;
271 break;
272 case 'C':
273 count = set[0] ? args[0] : 1;
274 if (terminal->column + count < terminal->width)
275 terminal->column += count;
276 else
277 terminal->column = terminal->width;
278 break;
279 case 'D':
280 count = set[0] ? args[0] : 1;
281 if (terminal->column - count >= 0)
282 terminal->column -= count;
283 else
284 terminal->column = 0;
285 break;
286 case 'J':
287 row = terminal_get_row(terminal, terminal->row);
288 memset(&row[terminal->column], 0, terminal->width - terminal->column);
289 for (i = terminal->row + 1; i < terminal->height; i++)
290 memset(terminal_get_row(terminal, i), 0, terminal->width);
291 break;
292 case 'G':
293 if (set[0])
294 terminal->column = args[0] - 1;
295 break;
296 case 'H':
297 case 'f':
298 terminal->row = set[0] ? args[0] - 1 : 0;
299 terminal->column = set[1] ? args[1] - 1 : 0;
300 break;
301 case 'K':
302 row = terminal_get_row(terminal, terminal->row);
303 memset(&row[terminal->column], 0, terminal->width - terminal->column);
304 break;
305 case 'm':
306 /* color, blink, bold etc*/
307 break;
308 case '?':
309 if (strcmp(p, "?25l") == 0) {
310 /* hide cursor */
311 } else if (strcmp(p, "?25h") == 0) {
312 /* show cursor */
313 }
314 break;
315 default:
316 terminal_data(terminal,
317 terminal->escape + 1,
318 terminal->escape_length - 2);
319 break;
320 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500321}
322
323static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500324terminal_data(struct terminal *terminal, const char *data, size_t length)
325{
326 int i;
327 char *row;
328
329 for (i = 0; i < length; i++) {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500330 row = terminal_get_row(terminal, terminal->row);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500331
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500332 if (terminal->state == STATE_ESCAPE) {
333 terminal->escape[terminal->escape_length++] = data[i];
334 if (terminal->escape_length == 2 && data[i] != '[') {
335 /* Bad escape sequence. */
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500336 terminal->state = STATE_NORMAL;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500337 goto cancel_escape;
338 }
339
340 if (isalpha(data[i])) {
341 terminal->state = STATE_NORMAL;
342 handle_escape(terminal);
343 }
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500344 continue;
345 }
346
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500347 cancel_escape:
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500348 switch (data[i]) {
349 case '\r':
350 terminal->column = 0;
351 break;
352 case '\n':
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500353 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500354 if (terminal->row + 1 < terminal->height) {
355 terminal->row++;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500356 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500357 terminal->start++;
358 if (terminal->start == terminal->height)
359 terminal->start = 0;
360 memset(terminal_get_row(terminal, terminal->row),
361 0, terminal->width);
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500362 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500363
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500364 break;
365 case '\t':
366 memset(&row[terminal->column], ' ', -terminal->column & 7);
367 terminal->column = (terminal->column + 7) & ~7;
368 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500369 case '\e':
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500370 terminal->state = STATE_ESCAPE;
371 terminal->escape[0] = '\e';
372 terminal->escape_length = 1;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500373 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500374 case '\b':
375 if (terminal->column > 0)
376 terminal->column--;
377 break;
378 case '\a':
379 /* Bell */
380 break;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500381 default:
382 if (terminal->column < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500383 row[terminal->column++] = data[i] < 32 ? data[i] + 64 : data[i];
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500384 break;
385 }
386 }
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500387
388 terminal_schedule_redraw(terminal);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500389}
390
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500391static void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500392resize_handler(struct window *window, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500393{
394 struct terminal *terminal = data;
395
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500396 terminal_schedule_redraw(terminal);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500397}
398
399static void
400acknowledge_handler(struct window *window, uint32_t key, void *data)
401{
402 struct terminal *terminal = data;
403
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500404 terminal->redraw_scheduled = 0;
Kristian Høgsbergc47303f2008-12-08 09:57:08 -0500405 buffer_destroy(terminal->buffer, terminal->fd);
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500406
407 if (terminal->redraw_pending) {
408 terminal->redraw_pending = 0;
409 terminal_schedule_redraw(terminal);
410 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500411}
412
413struct key {
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500414 int code[4];
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500415} evdev_keymap[] = {
416 { { 0, 0 } }, /* 0 */
417 { { 0x1b, 0x1b } },
418 { { '1', '!' } },
419 { { '2', '@' } },
420 { { '3', '#' } },
421 { { '4', '$' } },
422 { { '5', '%' } },
423 { { '6', '^' } },
424 { { '7', '&' } },
425 { { '8', '*' } },
426 { { '9', '(' } },
427 { { '0', ')' } },
428 { { '-', '_' } },
429 { { '=', '+' } },
430 { { '\b', '\b' } },
431 { { '\t', '\t' } },
432
Kristian Høgsberg33500892008-12-19 17:37:49 -0500433 { { 'q', 'Q', 0x11 } }, /* 16 */
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500434 { { 'w', 'W', 0x17 } },
435 { { 'e', 'E', 0x05 } },
436 { { 'r', 'R', 0x12 } },
Kristian Høgsberg33500892008-12-19 17:37:49 -0500437 { { 't', 'T', 0x14 } },
438 { { 'y', 'Y', 0x19 } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500439 { { 'u', 'U', 0x15 } },
Kristian Høgsberg33500892008-12-19 17:37:49 -0500440 { { 'i', 'I', 0x09 } },
441 { { 'o', 'O', 0x0f } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500442 { { 'p', 'P', 0x10 } },
Kristian Høgsberg33500892008-12-19 17:37:49 -0500443 { { '[', '{', 0x1b } },
444 { { ']', '}', 0x1d } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500445 { { '\n', '\n' } },
446 { { 0, 0 } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500447 { { 'a', 'A', 0x01} },
448 { { 's', 'S', 0x13 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500449
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500450 { { 'd', 'D', 0x04 } }, /* 32 */
451 { { 'f', 'F', 0x06 } },
Kristian Høgsberg33500892008-12-19 17:37:49 -0500452 { { 'g', 'G', 0x07 } },
453 { { 'h', 'H', 0x08 } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500454 { { 'j', 'J', 0x0a } },
455 { { 'k', 'K', 0x0b } },
456 { { 'l', 'L', 0x0c } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500457 { { ';', ':' } },
458 { { '\'', '"' } },
459 { { '`', '~' } },
460 { { 0, 0 } },
Kristian Høgsberg33500892008-12-19 17:37:49 -0500461 { { '\\', '|', 0x1c } },
462 { { 'z', 'Z', 0x1a } },
463 { { 'x', 'X', 0x18 } },
464 { { 'c', 'C', 0x03 } },
465 { { 'v', 'V', 0x16 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500466
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500467 { { 'b', 'B', 0x02 } }, /* 48 */
468 { { 'n', 'N', 0x0e } },
469 { { 'm', 'M', 0x0d } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500470 { { ',', '<' } },
471 { { '.', '>' } },
472 { { '/', '?' } },
473 { { 0, 0 } },
474 { { '*', '*' } },
475 { { 0, 0 } },
476 { { ' ', ' ' } },
477 { { 0, 0 } }
478
479 /* 59 */
480};
481
482#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
483
484static void
485key_handler(struct window *window, uint32_t key, uint32_t state, void *data)
486{
487 struct terminal *terminal = data;
488 uint32_t mod = 0;
489 char c;
490
491 switch (key) {
492 case KEY_LEFTSHIFT:
493 case KEY_RIGHTSHIFT:
494 mod = MOD_SHIFT;
495 break;
496 case KEY_LEFTCTRL:
497 case KEY_RIGHTCTRL:
498 mod = MOD_CTRL;
499 break;
500 case KEY_LEFTALT:
501 case KEY_RIGHTALT:
502 mod = MOD_ALT;
503 break;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500504 case KEY_F11:
505 if (!state)
506 break;
507 terminal->fullscreen ^= 1;
508 window_set_fullscreen(window, terminal->fullscreen);
509 terminal_schedule_redraw(terminal);
510 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500511 default:
512 if (key < ARRAY_LENGTH(evdev_keymap)) {
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500513 if (terminal->modifiers & MOD_CTRL)
514 c = evdev_keymap[key].code[2];
515 else if (terminal->modifiers & MOD_SHIFT)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500516 c = evdev_keymap[key].code[1];
517 else
518 c = evdev_keymap[key].code[0];
519 if (state && c)
520 write(terminal->master, &c, 1);
521 }
522 break;
523 }
524
525 if (state)
526 terminal->modifiers |= mod;
527 else
528 terminal->modifiers &= ~mod;
529}
530
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500531static struct terminal *
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500532terminal_create(struct wl_display *display, int fd, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500533{
534 struct terminal *terminal;
535
536 terminal = malloc(sizeof *terminal);
537 if (terminal == NULL)
538 return terminal;
539
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500540 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500541 terminal->fd = fd;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500542 terminal->fullscreen = fullscreen;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500543 terminal->window = window_create(display, fd, "Wayland Terminal",
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500544 500, 100, 500, 400);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500545 terminal->display = display;
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500546 terminal->redraw_scheduled = 1;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500547 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500548
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500549 terminal->compositor = wl_display_get_compositor(display);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500550 window_set_fullscreen(terminal->window, terminal->fullscreen);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500551 window_set_resize_handler(terminal->window, resize_handler, terminal);
552 window_set_acknowledge_handler(terminal->window, acknowledge_handler, terminal);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500553 window_set_key_handler(terminal->window, key_handler, terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500554
Kristian Høgsberg22106762008-12-08 13:50:07 -0500555 terminal_draw(terminal);
556
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500557 return terminal;
558}
559
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500560static gboolean
561io_handler(GIOChannel *source,
562 GIOCondition condition,
563 gpointer data)
564{
565 struct terminal *terminal = data;
566 gchar buffer[256];
567 gsize bytes_read;
568 GError *error = NULL;
569
570 g_io_channel_read_chars(source, buffer, sizeof buffer,
571 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500572
573 terminal_data(terminal, buffer, bytes_read);
574
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500575 return TRUE;
576}
577
578static int
579terminal_run(struct terminal *terminal, const char *path)
580{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500581 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500582 pid_t pid;
583
584 pid = forkpty(&master, NULL, NULL, NULL);
585 if (pid == 0) {
586 close(master);
Kristian Høgsbergc8c5d582008-12-18 14:50:08 -0500587 setenv("TERM", "vt100", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500588 if (execl(path, path, NULL)) {
589 printf("exec failed: %m\n");
590 exit(EXIT_FAILURE);
591 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500592 } else if (pid < 0) {
593 fprintf(stderr, "failed to fork and create pty (%m).\n");
594 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500595 }
596
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500597 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500598 terminal->channel = g_io_channel_unix_new(master);
599 fcntl(master, F_SETFL, O_NONBLOCK);
600 g_io_add_watch(terminal->channel, G_IO_IN,
601 io_handler, terminal);
602
603 return 0;
604}
605
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500606static const GOptionEntry option_entries[] = {
607 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
608 &option_fullscreen, "Run in fullscreen mode" },
609 { NULL }
610};
611
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500612int main(int argc, char *argv[])
613{
614 struct wl_display *display;
615 int fd;
616 GMainLoop *loop;
617 GSource *source;
618 struct terminal *terminal;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500619 GOptionContext *context;
620 GError *error;
621
622 context = g_option_context_new(NULL);
623 g_option_context_add_main_entries(context, option_entries, "Wayland Terminal");
624 if (!g_option_context_parse(context, &argc, &argv, &error)) {
625 fprintf(stderr, "option parsing failed: %s\n", error->message);
626 exit(EXIT_FAILURE);
627 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500628
629 fd = open(gem_device, O_RDWR);
630 if (fd < 0) {
631 fprintf(stderr, "drm open failed: %m\n");
632 return -1;
633 }
634
635 display = wl_display_create(socket_name, sizeof socket_name);
636 if (display == NULL) {
637 fprintf(stderr, "failed to create display: %m\n");
638 return -1;
639 }
640
641 loop = g_main_loop_new(NULL, FALSE);
642 source = wl_glib_source_new(display);
643 g_source_attach(source, NULL);
644
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500645 terminal = terminal_create(display, fd, option_fullscreen);
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500646 if (terminal_run(terminal, "/bin/bash"))
647 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500648
649 g_main_loop_run(loop);
650
651 return 0;
652}