blob: 82227e3d211e49fcbf5e74392362a3b827ffc3c5 [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
46static const char gem_device[] = "/dev/dri/card0";
47static const char socket_name[] = "\0wayland";
48
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050049#define MOD_SHIFT 0x01
50#define MOD_ALT 0x02
51#define MOD_CTRL 0x04
52
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050053struct terminal {
54 struct window *window;
55 struct wl_display *display;
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øgsbergb29415e2008-12-08 00:16:39 -050058 int width, height, tail, row, column, total_rows;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050059 int fd, master;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050060 struct buffer *buffer;
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øgsberg0c4457f2008-12-07 19:59:11 -050066};
67
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050068static void
69terminal_draw_contents(struct terminal *terminal)
70{
71 struct rectangle rectangle;
72 cairo_surface_t *surface;
73 cairo_t *cr;
74 cairo_font_extents_t extents;
Kristian Høgsberg17809b12008-12-08 12:20:40 -050075 int i, row;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050076
77 window_get_child_rectangle(terminal->window, &rectangle);
78
79 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
80 rectangle.width, rectangle.height);
81 cr = cairo_create(surface);
82 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
83 cairo_set_source_rgba(cr, 0, 0, 0, 0.9);
84 cairo_paint(cr);
85 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
86 cairo_set_source_rgba(cr, 0, 0.5, 0, 1);
87
88 cairo_select_font_face (cr, "mono",
89 CAIRO_FONT_SLANT_NORMAL,
90 CAIRO_FONT_WEIGHT_NORMAL);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050091 cairo_set_font_size(cr, 14);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050092
93 cairo_font_extents(cr, &extents);
Kristian Høgsberg17809b12008-12-08 12:20:40 -050094 for (i = 0; i < terminal->total_rows; i++) {
95 row = (terminal->tail + i) % terminal->height;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050096 cairo_move_to(cr, 0, extents.ascent + extents.height * i);
Kristian Høgsberg17809b12008-12-08 12:20:40 -050097 cairo_show_text(cr, &terminal->data[row * (terminal->width + 1)]);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050098 }
99 cairo_destroy(cr);
100
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500101 terminal->buffer = buffer_create_from_cairo_surface(terminal->fd, surface);
102 cairo_surface_destroy(surface);
103
104 window_copy(terminal->window,
105 &rectangle,
106 terminal->buffer->name, terminal->buffer->stride);
107}
108
109static void
110terminal_draw(struct terminal *terminal)
111{
112 window_draw(terminal->window);
113 terminal_draw_contents(terminal);
114 wl_display_commit(terminal->display, 0);
115}
116
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500117static gboolean
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500118idle_redraw(void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500119{
120 struct terminal *terminal = data;
121
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500122 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500123
124 return FALSE;
125}
126
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500127#define STATE_NORMAL 0
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500128#define STATE_ESCAPE 1
129
130static void
131terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500132
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500133static void
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500134terminal_schedule_redraw(struct terminal *terminal)
135{
136 if (!terminal->redraw_scheduled) {
137 g_idle_add(idle_redraw, terminal);
138 terminal->redraw_scheduled = 1;
139 } else {
140 terminal->redraw_pending = 1;
141 }
142}
143
144static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500145handle_escape(struct terminal *terminal)
146{
147 char *row;
148 int i, j;
149
150 terminal->escape[terminal->escape_length++] = '\0';
151 if (strcmp(terminal->escape, "\e[J") == 0) {
152 row = &terminal->data[terminal->row * (terminal->width + 1)];
153 memset(&row[terminal->column], 0, terminal->width - terminal->column);
154 for (i = terminal->total_rows; i < terminal->height; i++) {
155
156 j = terminal->row + i;
157 if (j >= terminal->height)
158 j -= terminal->height;
159
160 row = &terminal->data[j * (terminal->width + 1)];
161 memset(row, 0, terminal->width);
162 }
163 } else if (strcmp(terminal->escape, "\e[H") == 0) {
164 terminal->row = terminal->tail;
165 terminal->total_rows = 1;
166 terminal->column = 0;
167 }
168}
169
170static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500171terminal_data(struct terminal *terminal, const char *data, size_t length)
172{
173 int i;
174 char *row;
175
176 for (i = 0; i < length; i++) {
177 row = &terminal->data[terminal->row * (terminal->width + 1)];
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500178
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500179 if (terminal->state == STATE_ESCAPE) {
180 terminal->escape[terminal->escape_length++] = data[i];
181 if (terminal->escape_length == 2 && data[i] != '[') {
182 /* Bad escape sequence. */
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500183 terminal->state = STATE_NORMAL;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500184 goto cancel_escape;
185 }
186
187 if (isalpha(data[i])) {
188 terminal->state = STATE_NORMAL;
189 handle_escape(terminal);
190 }
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500191 continue;
192 }
193
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500194 cancel_escape:
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500195 switch (data[i]) {
196 case '\r':
197 terminal->column = 0;
198 break;
199 case '\n':
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500200 terminal->column = 0;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500201 terminal->row++;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500202 if (terminal->row == terminal->height)
203 terminal->row = 0;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500204 if (terminal->total_rows == terminal->height) {
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500205 memset(&terminal->data[terminal->row * (terminal->width + 1)],
206 0, terminal->width);
207 terminal->tail++;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500208 } else {
209 terminal->total_rows++;
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500210 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500211
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500212 if (terminal->tail == terminal->height)
213 terminal->tail = 0;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500214 break;
215 case '\t':
216 memset(&row[terminal->column], ' ', -terminal->column & 7);
217 terminal->column = (terminal->column + 7) & ~7;
218 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500219 case '\e':
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500220 terminal->state = STATE_ESCAPE;
221 terminal->escape[0] = '\e';
222 terminal->escape_length = 1;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500223 break;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500224 default:
225 if (terminal->column < terminal->width)
226 row[terminal->column++] = data[i];
227 break;
228 }
229 }
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500230
231 terminal_schedule_redraw(terminal);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500232}
233
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500234static void
235resize_handler(struct window *window, int32_t width, int32_t height, void *data)
236{
237 struct terminal *terminal = data;
238
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500239 terminal_schedule_redraw(terminal);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500240}
241
242static void
243acknowledge_handler(struct window *window, uint32_t key, void *data)
244{
245 struct terminal *terminal = data;
246
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500247 terminal->redraw_scheduled = 0;
Kristian Høgsbergc47303f2008-12-08 09:57:08 -0500248 buffer_destroy(terminal->buffer, terminal->fd);
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500249
250 if (terminal->redraw_pending) {
251 terminal->redraw_pending = 0;
252 terminal_schedule_redraw(terminal);
253 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500254}
255
256struct key {
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500257 int code[4];
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500258} evdev_keymap[] = {
259 { { 0, 0 } }, /* 0 */
260 { { 0x1b, 0x1b } },
261 { { '1', '!' } },
262 { { '2', '@' } },
263 { { '3', '#' } },
264 { { '4', '$' } },
265 { { '5', '%' } },
266 { { '6', '^' } },
267 { { '7', '&' } },
268 { { '8', '*' } },
269 { { '9', '(' } },
270 { { '0', ')' } },
271 { { '-', '_' } },
272 { { '=', '+' } },
273 { { '\b', '\b' } },
274 { { '\t', '\t' } },
275
276 { { 'q', 'Q' } }, /* 16 */
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500277 { { 'w', 'W', 0x17 } },
278 { { 'e', 'E', 0x05 } },
279 { { 'r', 'R', 0x12 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500280 { { 't', 'T' } },
281 { { 'y', 'Y' } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500282 { { 'u', 'U', 0x15 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500283 { { 'i', 'I' } },
284 { { 'o', 'O' } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500285 { { 'p', 'P', 0x10 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500286 { { '[', '{' } },
287 { { ']', '}' } },
288 { { '\n', '\n' } },
289 { { 0, 0 } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500290 { { 'a', 'A', 0x01} },
291 { { 's', 'S', 0x13 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500292
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500293 { { 'd', 'D', 0x04 } }, /* 32 */
294 { { 'f', 'F', 0x06 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500295 { { 'g', 'G' } },
296 { { 'h', 'H' } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500297 { { 'j', 'J', 0x0a } },
298 { { 'k', 'K', 0x0b } },
299 { { 'l', 'L', 0x0c } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500300 { { ';', ':' } },
301 { { '\'', '"' } },
302 { { '`', '~' } },
303 { { 0, 0 } },
304 { { '\\', '|' } },
305 { { 'z', 'Z' } },
306 { { 'x', 'X' } },
307 { { 'c', 'C' } },
308 { { 'v', 'V' } },
309
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500310 { { 'b', 'B', 0x02 } }, /* 48 */
311 { { 'n', 'N', 0x0e } },
312 { { 'm', 'M', 0x0d } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500313 { { ',', '<' } },
314 { { '.', '>' } },
315 { { '/', '?' } },
316 { { 0, 0 } },
317 { { '*', '*' } },
318 { { 0, 0 } },
319 { { ' ', ' ' } },
320 { { 0, 0 } }
321
322 /* 59 */
323};
324
325#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
326
327static void
328key_handler(struct window *window, uint32_t key, uint32_t state, void *data)
329{
330 struct terminal *terminal = data;
331 uint32_t mod = 0;
332 char c;
333
334 switch (key) {
335 case KEY_LEFTSHIFT:
336 case KEY_RIGHTSHIFT:
337 mod = MOD_SHIFT;
338 break;
339 case KEY_LEFTCTRL:
340 case KEY_RIGHTCTRL:
341 mod = MOD_CTRL;
342 break;
343 case KEY_LEFTALT:
344 case KEY_RIGHTALT:
345 mod = MOD_ALT;
346 break;
347 default:
348 if (key < ARRAY_LENGTH(evdev_keymap)) {
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500349 if (terminal->modifiers & MOD_CTRL)
350 c = evdev_keymap[key].code[2];
351 else if (terminal->modifiers & MOD_SHIFT)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500352 c = evdev_keymap[key].code[1];
353 else
354 c = evdev_keymap[key].code[0];
355 if (state && c)
356 write(terminal->master, &c, 1);
357 }
358 break;
359 }
360
361 if (state)
362 terminal->modifiers |= mod;
363 else
364 terminal->modifiers &= ~mod;
365}
366
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500367static struct terminal *
368terminal_create(struct wl_display *display, int fd)
369{
370 struct terminal *terminal;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500371 int size;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500372
373 terminal = malloc(sizeof *terminal);
374 if (terminal == NULL)
375 return terminal;
376
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500377 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500378 terminal->fd = fd;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500379 terminal->window = window_create(display, fd, "Wayland Terminal",
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500380 500, 100, 500, 400);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500381 terminal->display = display;
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500382 terminal->redraw_scheduled = 1;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500383 terminal->width = 80;
384 terminal->height = 25;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500385 terminal->total_rows = 1;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500386 size = (terminal->width + 1) * terminal->height;
387 terminal->data = malloc(size);
388 memset(terminal->data, 0, size);
389
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500390 window_set_resize_handler(terminal->window, resize_handler, terminal);
391 window_set_acknowledge_handler(terminal->window, acknowledge_handler, terminal);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500392 window_set_key_handler(terminal->window, key_handler, terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500393
394 return terminal;
395}
396
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500397static gboolean
398io_handler(GIOChannel *source,
399 GIOCondition condition,
400 gpointer data)
401{
402 struct terminal *terminal = data;
403 gchar buffer[256];
404 gsize bytes_read;
405 GError *error = NULL;
406
407 g_io_channel_read_chars(source, buffer, sizeof buffer,
408 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500409
410 terminal_data(terminal, buffer, bytes_read);
411
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500412 return TRUE;
413}
414
415static int
416terminal_run(struct terminal *terminal, const char *path)
417{
418 int master, slave;
419 pid_t pid;
420
421 pid = forkpty(&master, NULL, NULL, NULL);
422 if (pid == 0) {
423 close(master);
424 if (execl(path, path, NULL)) {
425 printf("exec failed: %m\n");
426 exit(EXIT_FAILURE);
427 }
428 }
429
430 close(slave);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500431 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500432 terminal->channel = g_io_channel_unix_new(master);
433 fcntl(master, F_SETFL, O_NONBLOCK);
434 g_io_add_watch(terminal->channel, G_IO_IN,
435 io_handler, terminal);
436
437 return 0;
438}
439
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500440int main(int argc, char *argv[])
441{
442 struct wl_display *display;
443 int fd;
444 GMainLoop *loop;
445 GSource *source;
446 struct terminal *terminal;
447
448 fd = open(gem_device, O_RDWR);
449 if (fd < 0) {
450 fprintf(stderr, "drm open failed: %m\n");
451 return -1;
452 }
453
454 display = wl_display_create(socket_name, sizeof socket_name);
455 if (display == NULL) {
456 fprintf(stderr, "failed to create display: %m\n");
457 return -1;
458 }
459
460 loop = g_main_loop_new(NULL, FALSE);
461 source = wl_glib_source_new(display);
462 g_source_attach(source, NULL);
463
464 terminal = terminal_create(display, fd);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500465 terminal_run(terminal, "/bin/bash");
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500466 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500467
468 g_main_loop_run(loop);
469
470 return 0;
471}