blob: 40aa2b733d286ed4afbee1bf3f71fbd16ff58059 [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øgsberg1584c572008-12-08 12:59:37 -050066 int margin;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050067};
68
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050069static void
70terminal_draw_contents(struct terminal *terminal)
71{
72 struct rectangle rectangle;
73 cairo_surface_t *surface;
74 cairo_t *cr;
75 cairo_font_extents_t extents;
Kristian Høgsberg17809b12008-12-08 12:20:40 -050076 int i, row;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050077
78 window_get_child_rectangle(terminal->window, &rectangle);
79
80 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
81 rectangle.width, rectangle.height);
82 cr = cairo_create(surface);
83 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
84 cairo_set_source_rgba(cr, 0, 0, 0, 0.9);
85 cairo_paint(cr);
86 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
87 cairo_set_source_rgba(cr, 0, 0.5, 0, 1);
88
89 cairo_select_font_face (cr, "mono",
90 CAIRO_FONT_SLANT_NORMAL,
91 CAIRO_FONT_WEIGHT_NORMAL);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050092 cairo_set_font_size(cr, 14);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050093
94 cairo_font_extents(cr, &extents);
Kristian Høgsberg17809b12008-12-08 12:20:40 -050095 for (i = 0; i < terminal->total_rows; i++) {
96 row = (terminal->tail + i) % terminal->height;
Kristian Høgsberg1584c572008-12-08 12:59:37 -050097 cairo_move_to(cr, terminal->margin,
98 terminal->margin + extents.ascent + extents.height * i);
Kristian Høgsberg17809b12008-12-08 12:20:40 -050099 cairo_show_text(cr, &terminal->data[row * (terminal->width + 1)]);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500100 }
101 cairo_destroy(cr);
102
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500103 terminal->buffer = buffer_create_from_cairo_surface(terminal->fd, surface);
104 cairo_surface_destroy(surface);
105
106 window_copy(terminal->window,
107 &rectangle,
108 terminal->buffer->name, terminal->buffer->stride);
109}
110
111static void
112terminal_draw(struct terminal *terminal)
113{
114 window_draw(terminal->window);
115 terminal_draw_contents(terminal);
116 wl_display_commit(terminal->display, 0);
117}
118
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500119static gboolean
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500120idle_redraw(void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500121{
122 struct terminal *terminal = data;
123
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500124 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500125
126 return FALSE;
127}
128
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500129#define STATE_NORMAL 0
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500130#define STATE_ESCAPE 1
131
132static void
133terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500134
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500135static void
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500136terminal_schedule_redraw(struct terminal *terminal)
137{
138 if (!terminal->redraw_scheduled) {
139 g_idle_add(idle_redraw, terminal);
140 terminal->redraw_scheduled = 1;
141 } else {
142 terminal->redraw_pending = 1;
143 }
144}
145
146static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500147handle_escape(struct terminal *terminal)
148{
149 char *row;
150 int i, j;
151
152 terminal->escape[terminal->escape_length++] = '\0';
153 if (strcmp(terminal->escape, "\e[J") == 0) {
154 row = &terminal->data[terminal->row * (terminal->width + 1)];
155 memset(&row[terminal->column], 0, terminal->width - terminal->column);
156 for (i = terminal->total_rows; i < terminal->height; i++) {
157
158 j = terminal->row + i;
159 if (j >= terminal->height)
160 j -= terminal->height;
161
162 row = &terminal->data[j * (terminal->width + 1)];
163 memset(row, 0, terminal->width);
164 }
165 } else if (strcmp(terminal->escape, "\e[H") == 0) {
166 terminal->row = terminal->tail;
167 terminal->total_rows = 1;
168 terminal->column = 0;
169 }
170}
171
172static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500173terminal_data(struct terminal *terminal, const char *data, size_t length)
174{
175 int i;
176 char *row;
177
178 for (i = 0; i < length; i++) {
179 row = &terminal->data[terminal->row * (terminal->width + 1)];
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500180
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500181 if (terminal->state == STATE_ESCAPE) {
182 terminal->escape[terminal->escape_length++] = data[i];
183 if (terminal->escape_length == 2 && data[i] != '[') {
184 /* Bad escape sequence. */
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500185 terminal->state = STATE_NORMAL;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500186 goto cancel_escape;
187 }
188
189 if (isalpha(data[i])) {
190 terminal->state = STATE_NORMAL;
191 handle_escape(terminal);
192 }
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500193 continue;
194 }
195
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500196 cancel_escape:
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500197 switch (data[i]) {
198 case '\r':
199 terminal->column = 0;
200 break;
201 case '\n':
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500202 terminal->column = 0;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500203 terminal->row++;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500204 if (terminal->row == terminal->height)
205 terminal->row = 0;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500206 if (terminal->total_rows == terminal->height) {
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500207 memset(&terminal->data[terminal->row * (terminal->width + 1)],
208 0, terminal->width);
209 terminal->tail++;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500210 } else {
211 terminal->total_rows++;
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500212 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500213
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500214 if (terminal->tail == terminal->height)
215 terminal->tail = 0;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500216 break;
217 case '\t':
218 memset(&row[terminal->column], ' ', -terminal->column & 7);
219 terminal->column = (terminal->column + 7) & ~7;
220 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500221 case '\e':
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500222 terminal->state = STATE_ESCAPE;
223 terminal->escape[0] = '\e';
224 terminal->escape_length = 1;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500225 break;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500226 default:
227 if (terminal->column < terminal->width)
228 row[terminal->column++] = data[i];
229 break;
230 }
231 }
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500232
233 terminal_schedule_redraw(terminal);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500234}
235
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500236static void
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500237resize_handler(struct window *window, struct rectangle *rectangle, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500238{
239 struct terminal *terminal = data;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500240 cairo_surface_t *surface;
241 cairo_font_extents_t extents;
242 cairo_t *cr;
243
244 /* Adjust the size to an integer number of character cells.
245 * Maybe this is better done in the redraw path, as we're
246 * creating the cr and setting the font there anyway. */
247
248 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
249 cr = cairo_create(surface);
250 cairo_select_font_face (cr, "mono",
251 CAIRO_FONT_SLANT_NORMAL,
252 CAIRO_FONT_WEIGHT_NORMAL);
253 cairo_set_font_size(cr, 14);
254 cairo_font_extents(cr, &extents);
255 cairo_destroy(cr);
256 cairo_surface_destroy(surface);
257
258 rectangle->width -= (rectangle->width - 2 * terminal->margin) % (int32_t) extents.max_x_advance;
259 rectangle->height -= (rectangle->height - 2 * terminal->margin) % (int32_t) extents.height;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500260
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500261 terminal_schedule_redraw(terminal);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500262}
263
264static void
265acknowledge_handler(struct window *window, uint32_t key, void *data)
266{
267 struct terminal *terminal = data;
268
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500269 terminal->redraw_scheduled = 0;
Kristian Høgsbergc47303f2008-12-08 09:57:08 -0500270 buffer_destroy(terminal->buffer, terminal->fd);
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500271
272 if (terminal->redraw_pending) {
273 terminal->redraw_pending = 0;
274 terminal_schedule_redraw(terminal);
275 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500276}
277
278struct key {
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500279 int code[4];
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500280} evdev_keymap[] = {
281 { { 0, 0 } }, /* 0 */
282 { { 0x1b, 0x1b } },
283 { { '1', '!' } },
284 { { '2', '@' } },
285 { { '3', '#' } },
286 { { '4', '$' } },
287 { { '5', '%' } },
288 { { '6', '^' } },
289 { { '7', '&' } },
290 { { '8', '*' } },
291 { { '9', '(' } },
292 { { '0', ')' } },
293 { { '-', '_' } },
294 { { '=', '+' } },
295 { { '\b', '\b' } },
296 { { '\t', '\t' } },
297
298 { { 'q', 'Q' } }, /* 16 */
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500299 { { 'w', 'W', 0x17 } },
300 { { 'e', 'E', 0x05 } },
301 { { 'r', 'R', 0x12 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500302 { { 't', 'T' } },
303 { { 'y', 'Y' } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500304 { { 'u', 'U', 0x15 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500305 { { 'i', 'I' } },
306 { { 'o', 'O' } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500307 { { 'p', 'P', 0x10 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500308 { { '[', '{' } },
309 { { ']', '}' } },
310 { { '\n', '\n' } },
311 { { 0, 0 } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500312 { { 'a', 'A', 0x01} },
313 { { 's', 'S', 0x13 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500314
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500315 { { 'd', 'D', 0x04 } }, /* 32 */
316 { { 'f', 'F', 0x06 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500317 { { 'g', 'G' } },
318 { { 'h', 'H' } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500319 { { 'j', 'J', 0x0a } },
320 { { 'k', 'K', 0x0b } },
321 { { 'l', 'L', 0x0c } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500322 { { ';', ':' } },
323 { { '\'', '"' } },
324 { { '`', '~' } },
325 { { 0, 0 } },
326 { { '\\', '|' } },
327 { { 'z', 'Z' } },
328 { { 'x', 'X' } },
329 { { 'c', 'C' } },
330 { { 'v', 'V' } },
331
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500332 { { 'b', 'B', 0x02 } }, /* 48 */
333 { { 'n', 'N', 0x0e } },
334 { { 'm', 'M', 0x0d } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500335 { { ',', '<' } },
336 { { '.', '>' } },
337 { { '/', '?' } },
338 { { 0, 0 } },
339 { { '*', '*' } },
340 { { 0, 0 } },
341 { { ' ', ' ' } },
342 { { 0, 0 } }
343
344 /* 59 */
345};
346
347#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
348
349static void
350key_handler(struct window *window, uint32_t key, uint32_t state, void *data)
351{
352 struct terminal *terminal = data;
353 uint32_t mod = 0;
354 char c;
355
356 switch (key) {
357 case KEY_LEFTSHIFT:
358 case KEY_RIGHTSHIFT:
359 mod = MOD_SHIFT;
360 break;
361 case KEY_LEFTCTRL:
362 case KEY_RIGHTCTRL:
363 mod = MOD_CTRL;
364 break;
365 case KEY_LEFTALT:
366 case KEY_RIGHTALT:
367 mod = MOD_ALT;
368 break;
369 default:
370 if (key < ARRAY_LENGTH(evdev_keymap)) {
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500371 if (terminal->modifiers & MOD_CTRL)
372 c = evdev_keymap[key].code[2];
373 else if (terminal->modifiers & MOD_SHIFT)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500374 c = evdev_keymap[key].code[1];
375 else
376 c = evdev_keymap[key].code[0];
377 if (state && c)
378 write(terminal->master, &c, 1);
379 }
380 break;
381 }
382
383 if (state)
384 terminal->modifiers |= mod;
385 else
386 terminal->modifiers &= ~mod;
387}
388
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500389static struct terminal *
390terminal_create(struct wl_display *display, int fd)
391{
392 struct terminal *terminal;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500393 int size;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500394
395 terminal = malloc(sizeof *terminal);
396 if (terminal == NULL)
397 return terminal;
398
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500399 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500400 terminal->fd = fd;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500401 terminal->window = window_create(display, fd, "Wayland Terminal",
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500402 500, 100, 500, 400);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500403 terminal->display = display;
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500404 terminal->redraw_scheduled = 1;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500405 terminal->width = 80;
406 terminal->height = 25;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500407 terminal->total_rows = 1;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500408 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500409 size = (terminal->width + 1) * terminal->height;
410 terminal->data = malloc(size);
411 memset(terminal->data, 0, size);
412
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500413 window_set_resize_handler(terminal->window, resize_handler, terminal);
414 window_set_acknowledge_handler(terminal->window, acknowledge_handler, terminal);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500415 window_set_key_handler(terminal->window, key_handler, terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500416
417 return terminal;
418}
419
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500420static gboolean
421io_handler(GIOChannel *source,
422 GIOCondition condition,
423 gpointer data)
424{
425 struct terminal *terminal = data;
426 gchar buffer[256];
427 gsize bytes_read;
428 GError *error = NULL;
429
430 g_io_channel_read_chars(source, buffer, sizeof buffer,
431 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500432
433 terminal_data(terminal, buffer, bytes_read);
434
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500435 return TRUE;
436}
437
438static int
439terminal_run(struct terminal *terminal, const char *path)
440{
441 int master, slave;
442 pid_t pid;
443
444 pid = forkpty(&master, NULL, NULL, NULL);
445 if (pid == 0) {
446 close(master);
447 if (execl(path, path, NULL)) {
448 printf("exec failed: %m\n");
449 exit(EXIT_FAILURE);
450 }
451 }
452
453 close(slave);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500454 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500455 terminal->channel = g_io_channel_unix_new(master);
456 fcntl(master, F_SETFL, O_NONBLOCK);
457 g_io_add_watch(terminal->channel, G_IO_IN,
458 io_handler, terminal);
459
460 return 0;
461}
462
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500463int main(int argc, char *argv[])
464{
465 struct wl_display *display;
466 int fd;
467 GMainLoop *loop;
468 GSource *source;
469 struct terminal *terminal;
470
471 fd = open(gem_device, O_RDWR);
472 if (fd < 0) {
473 fprintf(stderr, "drm open failed: %m\n");
474 return -1;
475 }
476
477 display = wl_display_create(socket_name, sizeof socket_name);
478 if (display == NULL) {
479 fprintf(stderr, "failed to create display: %m\n");
480 return -1;
481 }
482
483 loop = g_main_loop_new(NULL, FALSE);
484 source = wl_glib_source_new(display);
485 g_source_attach(source, NULL);
486
487 terminal = terminal_create(display, fd);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500488 terminal_run(terminal, "/bin/bash");
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500489 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500490
491 g_main_loop_run(loop);
492
493 return 0;
494}