blob: 923539ef924ff7b5656db0e98acbf82169815d5c [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øgsbergf04e8382008-12-08 00:07:49 -050063 int state;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050064};
65
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050066static void
67terminal_draw_contents(struct terminal *terminal)
68{
69 struct rectangle rectangle;
70 cairo_surface_t *surface;
71 cairo_t *cr;
72 cairo_font_extents_t extents;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050073 int i, line;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050074
75 window_get_child_rectangle(terminal->window, &rectangle);
76
77 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
78 rectangle.width, rectangle.height);
79 cr = cairo_create(surface);
80 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
81 cairo_set_source_rgba(cr, 0, 0, 0, 0.9);
82 cairo_paint(cr);
83 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
84 cairo_set_source_rgba(cr, 0, 0.5, 0, 1);
85
86 cairo_select_font_face (cr, "mono",
87 CAIRO_FONT_SLANT_NORMAL,
88 CAIRO_FONT_WEIGHT_NORMAL);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050089 cairo_set_font_size(cr, 14);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050090
91 cairo_font_extents(cr, &extents);
92 for (i = 0; i < terminal->height; i++) {
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050093 line = (terminal->tail + i) % terminal->height;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050094 cairo_move_to(cr, 0, extents.ascent + extents.height * i);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050095 cairo_show_text(cr, &terminal->data[line * (terminal->width + 1)]);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050096 }
97 cairo_destroy(cr);
98
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050099 terminal->buffer = buffer_create_from_cairo_surface(terminal->fd, surface);
100 cairo_surface_destroy(surface);
101
102 window_copy(terminal->window,
103 &rectangle,
104 terminal->buffer->name, terminal->buffer->stride);
105}
106
107static void
108terminal_draw(struct terminal *terminal)
109{
110 window_draw(terminal->window);
111 terminal_draw_contents(terminal);
112 wl_display_commit(terminal->display, 0);
113}
114
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500115static gboolean
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500116idle_redraw(void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500117{
118 struct terminal *terminal = data;
119
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500120 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500121
122 return FALSE;
123}
124
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500125#define STATE_NORMAL 0
126#define STATE_SKIP_TO_ALPHA 1
127
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500128static void
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500129terminal_schedule_redraw(struct terminal *terminal)
130{
131 if (!terminal->redraw_scheduled) {
132 g_idle_add(idle_redraw, terminal);
133 terminal->redraw_scheduled = 1;
134 } else {
135 terminal->redraw_pending = 1;
136 }
137}
138
139static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500140terminal_data(struct terminal *terminal, const char *data, size_t length)
141{
142 int i;
143 char *row;
144
145 for (i = 0; i < length; i++) {
146 row = &terminal->data[terminal->row * (terminal->width + 1)];
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500147
148 if (terminal->state == STATE_SKIP_TO_ALPHA) {
149 if (isalpha(data[i]))
150 terminal->state = STATE_NORMAL;
151 continue;
152 }
153
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500154 switch (data[i]) {
155 case '\r':
156 terminal->column = 0;
157 break;
158 case '\n':
159 terminal->row++;
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500160 terminal->total_rows++;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500161 terminal->column = 0;
162 if (terminal->row == terminal->height)
163 terminal->row = 0;
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500164 if (terminal->row == terminal->tail && terminal->total_rows > 0) {
165 memset(&terminal->data[terminal->row * (terminal->width + 1)],
166 0, terminal->width);
167 terminal->tail++;
168 }
169 if (terminal->tail == terminal->height)
170 terminal->tail = 0;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500171 break;
172 case '\t':
173 memset(&row[terminal->column], ' ', -terminal->column & 7);
174 terminal->column = (terminal->column + 7) & ~7;
175 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500176 case '\e':
177 terminal->state = STATE_SKIP_TO_ALPHA;
178 break;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500179 default:
180 if (terminal->column < terminal->width)
181 row[terminal->column++] = data[i];
182 break;
183 }
184 }
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500185
186 terminal_schedule_redraw(terminal);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500187}
188
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500189static void
190resize_handler(struct window *window, int32_t width, int32_t height, void *data)
191{
192 struct terminal *terminal = data;
193
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500194 terminal_schedule_redraw(terminal);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500195}
196
197static void
198acknowledge_handler(struct window *window, uint32_t key, void *data)
199{
200 struct terminal *terminal = data;
201
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500202 terminal->redraw_scheduled = 0;
Kristian Høgsbergc47303f2008-12-08 09:57:08 -0500203 buffer_destroy(terminal->buffer, terminal->fd);
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500204
205 if (terminal->redraw_pending) {
206 terminal->redraw_pending = 0;
207 terminal_schedule_redraw(terminal);
208 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500209}
210
211struct key {
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500212 int code[4];
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500213} evdev_keymap[] = {
214 { { 0, 0 } }, /* 0 */
215 { { 0x1b, 0x1b } },
216 { { '1', '!' } },
217 { { '2', '@' } },
218 { { '3', '#' } },
219 { { '4', '$' } },
220 { { '5', '%' } },
221 { { '6', '^' } },
222 { { '7', '&' } },
223 { { '8', '*' } },
224 { { '9', '(' } },
225 { { '0', ')' } },
226 { { '-', '_' } },
227 { { '=', '+' } },
228 { { '\b', '\b' } },
229 { { '\t', '\t' } },
230
231 { { 'q', 'Q' } }, /* 16 */
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500232 { { 'w', 'W', 0x17 } },
233 { { 'e', 'E', 0x05 } },
234 { { 'r', 'R', 0x12 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500235 { { 't', 'T' } },
236 { { 'y', 'Y' } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500237 { { 'u', 'U', 0x15 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500238 { { 'i', 'I' } },
239 { { 'o', 'O' } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500240 { { 'p', 'P', 0x10 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500241 { { '[', '{' } },
242 { { ']', '}' } },
243 { { '\n', '\n' } },
244 { { 0, 0 } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500245 { { 'a', 'A', 0x01} },
246 { { 's', 'S', 0x13 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500247
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500248 { { 'd', 'D', 0x04 } }, /* 32 */
249 { { 'f', 'F', 0x06 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500250 { { 'g', 'G' } },
251 { { 'h', 'H' } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500252 { { 'j', 'J', 0x0a } },
253 { { 'k', 'K', 0x0b } },
254 { { 'l', 'L', 0x0c } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500255 { { ';', ':' } },
256 { { '\'', '"' } },
257 { { '`', '~' } },
258 { { 0, 0 } },
259 { { '\\', '|' } },
260 { { 'z', 'Z' } },
261 { { 'x', 'X' } },
262 { { 'c', 'C' } },
263 { { 'v', 'V' } },
264
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500265 { { 'b', 'B', 0x02 } }, /* 48 */
266 { { 'n', 'N', 0x0e } },
267 { { 'm', 'M', 0x0d } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500268 { { ',', '<' } },
269 { { '.', '>' } },
270 { { '/', '?' } },
271 { { 0, 0 } },
272 { { '*', '*' } },
273 { { 0, 0 } },
274 { { ' ', ' ' } },
275 { { 0, 0 } }
276
277 /* 59 */
278};
279
280#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
281
282static void
283key_handler(struct window *window, uint32_t key, uint32_t state, void *data)
284{
285 struct terminal *terminal = data;
286 uint32_t mod = 0;
287 char c;
288
289 switch (key) {
290 case KEY_LEFTSHIFT:
291 case KEY_RIGHTSHIFT:
292 mod = MOD_SHIFT;
293 break;
294 case KEY_LEFTCTRL:
295 case KEY_RIGHTCTRL:
296 mod = MOD_CTRL;
297 break;
298 case KEY_LEFTALT:
299 case KEY_RIGHTALT:
300 mod = MOD_ALT;
301 break;
302 default:
303 if (key < ARRAY_LENGTH(evdev_keymap)) {
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500304 if (terminal->modifiers & MOD_CTRL)
305 c = evdev_keymap[key].code[2];
306 else if (terminal->modifiers & MOD_SHIFT)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500307 c = evdev_keymap[key].code[1];
308 else
309 c = evdev_keymap[key].code[0];
310 if (state && c)
311 write(terminal->master, &c, 1);
312 }
313 break;
314 }
315
316 if (state)
317 terminal->modifiers |= mod;
318 else
319 terminal->modifiers &= ~mod;
320}
321
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500322static struct terminal *
323terminal_create(struct wl_display *display, int fd)
324{
325 struct terminal *terminal;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500326 int size;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500327
328 terminal = malloc(sizeof *terminal);
329 if (terminal == NULL)
330 return terminal;
331
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500332 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500333 terminal->fd = fd;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500334 terminal->window = window_create(display, fd, "Wayland Terminal",
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500335 500, 100, 500, 400);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500336 terminal->display = display;
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500337 terminal->redraw_scheduled = 1;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500338 terminal->width = 80;
339 terminal->height = 25;
340 size = (terminal->width + 1) * terminal->height;
341 terminal->data = malloc(size);
342 memset(terminal->data, 0, size);
343
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500344 window_set_resize_handler(terminal->window, resize_handler, terminal);
345 window_set_acknowledge_handler(terminal->window, acknowledge_handler, terminal);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500346 window_set_key_handler(terminal->window, key_handler, terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500347
348 return terminal;
349}
350
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500351static gboolean
352io_handler(GIOChannel *source,
353 GIOCondition condition,
354 gpointer data)
355{
356 struct terminal *terminal = data;
357 gchar buffer[256];
358 gsize bytes_read;
359 GError *error = NULL;
360
361 g_io_channel_read_chars(source, buffer, sizeof buffer,
362 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500363
364 terminal_data(terminal, buffer, bytes_read);
365
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500366 return TRUE;
367}
368
369static int
370terminal_run(struct terminal *terminal, const char *path)
371{
372 int master, slave;
373 pid_t pid;
374
375 pid = forkpty(&master, NULL, NULL, NULL);
376 if (pid == 0) {
377 close(master);
378 if (execl(path, path, NULL)) {
379 printf("exec failed: %m\n");
380 exit(EXIT_FAILURE);
381 }
382 }
383
384 close(slave);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500385 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500386 terminal->channel = g_io_channel_unix_new(master);
387 fcntl(master, F_SETFL, O_NONBLOCK);
388 g_io_add_watch(terminal->channel, G_IO_IN,
389 io_handler, terminal);
390
391 return 0;
392}
393
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500394int main(int argc, char *argv[])
395{
396 struct wl_display *display;
397 int fd;
398 GMainLoop *loop;
399 GSource *source;
400 struct terminal *terminal;
401
402 fd = open(gem_device, O_RDWR);
403 if (fd < 0) {
404 fprintf(stderr, "drm open failed: %m\n");
405 return -1;
406 }
407
408 display = wl_display_create(socket_name, sizeof socket_name);
409 if (display == NULL) {
410 fprintf(stderr, "failed to create display: %m\n");
411 return -1;
412 }
413
414 loop = g_main_loop_new(NULL, FALSE);
415 source = wl_glib_source_new(display);
416 g_source_attach(source, NULL);
417
418 terminal = terminal_create(display, fd);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500419 terminal_run(terminal, "/bin/bash");
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500420 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500421
422 g_main_loop_run(loop);
423
424 return 0;
425}