blob: f210341eba090ed8a5210c0ba01182e864a21803 [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;
56 int resize_scheduled;
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øgsberg269d6e32008-12-07 23:17:31 -0500129terminal_data(struct terminal *terminal, const char *data, size_t length)
130{
131 int i;
132 char *row;
133
134 for (i = 0; i < length; i++) {
135 row = &terminal->data[terminal->row * (terminal->width + 1)];
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500136
137 if (terminal->state == STATE_SKIP_TO_ALPHA) {
138 if (isalpha(data[i]))
139 terminal->state = STATE_NORMAL;
140 continue;
141 }
142
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500143 switch (data[i]) {
144 case '\r':
145 terminal->column = 0;
146 break;
147 case '\n':
148 terminal->row++;
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500149 terminal->total_rows++;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500150 terminal->column = 0;
151 if (terminal->row == terminal->height)
152 terminal->row = 0;
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500153 if (terminal->row == terminal->tail && terminal->total_rows > 0) {
154 memset(&terminal->data[terminal->row * (terminal->width + 1)],
155 0, terminal->width);
156 terminal->tail++;
157 }
158 if (terminal->tail == terminal->height)
159 terminal->tail = 0;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500160 break;
161 case '\t':
162 memset(&row[terminal->column], ' ', -terminal->column & 7);
163 terminal->column = (terminal->column + 7) & ~7;
164 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500165 case '\e':
166 terminal->state = STATE_SKIP_TO_ALPHA;
167 break;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500168 default:
169 if (terminal->column < terminal->width)
170 row[terminal->column++] = data[i];
171 break;
172 }
173 }
174}
175
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500176static void
177resize_handler(struct window *window, int32_t width, int32_t height, void *data)
178{
179 struct terminal *terminal = data;
180
181 if (!terminal->resize_scheduled) {
182 g_idle_add(idle_redraw, terminal);
183 terminal->resize_scheduled = 1;
184 }
185}
186
187static void
188acknowledge_handler(struct window *window, uint32_t key, void *data)
189{
190 struct terminal *terminal = data;
191
192 terminal->resize_scheduled = 0;
Kristian Høgsbergc47303f2008-12-08 09:57:08 -0500193 buffer_destroy(terminal->buffer, terminal->fd);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500194}
195
196struct key {
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500197 int code[4];
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500198} evdev_keymap[] = {
199 { { 0, 0 } }, /* 0 */
200 { { 0x1b, 0x1b } },
201 { { '1', '!' } },
202 { { '2', '@' } },
203 { { '3', '#' } },
204 { { '4', '$' } },
205 { { '5', '%' } },
206 { { '6', '^' } },
207 { { '7', '&' } },
208 { { '8', '*' } },
209 { { '9', '(' } },
210 { { '0', ')' } },
211 { { '-', '_' } },
212 { { '=', '+' } },
213 { { '\b', '\b' } },
214 { { '\t', '\t' } },
215
216 { { 'q', 'Q' } }, /* 16 */
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500217 { { 'w', 'W', 0x17 } },
218 { { 'e', 'E', 0x05 } },
219 { { 'r', 'R', 0x12 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500220 { { 't', 'T' } },
221 { { 'y', 'Y' } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500222 { { 'u', 'U', 0x15 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500223 { { 'i', 'I' } },
224 { { 'o', 'O' } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500225 { { 'p', 'P', 0x10 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500226 { { '[', '{' } },
227 { { ']', '}' } },
228 { { '\n', '\n' } },
229 { { 0, 0 } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500230 { { 'a', 'A', 0x01} },
231 { { 's', 'S', 0x13 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500232
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500233 { { 'd', 'D', 0x04 } }, /* 32 */
234 { { 'f', 'F', 0x06 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500235 { { 'g', 'G' } },
236 { { 'h', 'H' } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500237 { { 'j', 'J', 0x0a } },
238 { { 'k', 'K', 0x0b } },
239 { { 'l', 'L', 0x0c } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500240 { { ';', ':' } },
241 { { '\'', '"' } },
242 { { '`', '~' } },
243 { { 0, 0 } },
244 { { '\\', '|' } },
245 { { 'z', 'Z' } },
246 { { 'x', 'X' } },
247 { { 'c', 'C' } },
248 { { 'v', 'V' } },
249
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500250 { { 'b', 'B', 0x02 } }, /* 48 */
251 { { 'n', 'N', 0x0e } },
252 { { 'm', 'M', 0x0d } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500253 { { ',', '<' } },
254 { { '.', '>' } },
255 { { '/', '?' } },
256 { { 0, 0 } },
257 { { '*', '*' } },
258 { { 0, 0 } },
259 { { ' ', ' ' } },
260 { { 0, 0 } }
261
262 /* 59 */
263};
264
265#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
266
267static void
268key_handler(struct window *window, uint32_t key, uint32_t state, void *data)
269{
270 struct terminal *terminal = data;
271 uint32_t mod = 0;
272 char c;
273
274 switch (key) {
275 case KEY_LEFTSHIFT:
276 case KEY_RIGHTSHIFT:
277 mod = MOD_SHIFT;
278 break;
279 case KEY_LEFTCTRL:
280 case KEY_RIGHTCTRL:
281 mod = MOD_CTRL;
282 break;
283 case KEY_LEFTALT:
284 case KEY_RIGHTALT:
285 mod = MOD_ALT;
286 break;
287 default:
288 if (key < ARRAY_LENGTH(evdev_keymap)) {
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500289 if (terminal->modifiers & MOD_CTRL)
290 c = evdev_keymap[key].code[2];
291 else if (terminal->modifiers & MOD_SHIFT)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500292 c = evdev_keymap[key].code[1];
293 else
294 c = evdev_keymap[key].code[0];
295 if (state && c)
296 write(terminal->master, &c, 1);
297 }
298 break;
299 }
300
301 if (state)
302 terminal->modifiers |= mod;
303 else
304 terminal->modifiers &= ~mod;
305}
306
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500307static struct terminal *
308terminal_create(struct wl_display *display, int fd)
309{
310 struct terminal *terminal;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500311 int size;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500312
313 terminal = malloc(sizeof *terminal);
314 if (terminal == NULL)
315 return terminal;
316
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500317 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500318 terminal->fd = fd;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500319 terminal->window = window_create(display, fd, "Wayland Terminal",
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500320 500, 100, 500, 400);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500321 terminal->display = display;
322 terminal->resize_scheduled = 1;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500323 terminal->width = 80;
324 terminal->height = 25;
325 size = (terminal->width + 1) * terminal->height;
326 terminal->data = malloc(size);
327 memset(terminal->data, 0, size);
328
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500329 window_set_resize_handler(terminal->window, resize_handler, terminal);
330 window_set_acknowledge_handler(terminal->window, acknowledge_handler, terminal);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500331 window_set_key_handler(terminal->window, key_handler, terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500332
333 return terminal;
334}
335
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500336static gboolean
337io_handler(GIOChannel *source,
338 GIOCondition condition,
339 gpointer data)
340{
341 struct terminal *terminal = data;
342 gchar buffer[256];
343 gsize bytes_read;
344 GError *error = NULL;
345
346 g_io_channel_read_chars(source, buffer, sizeof buffer,
347 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500348
349 terminal_data(terminal, buffer, bytes_read);
350
351 if (!terminal->resize_scheduled) {
352 g_idle_add(idle_redraw, terminal);
353 terminal->resize_scheduled = 1;
354 }
355
356 return TRUE;
357}
358
359static int
360terminal_run(struct terminal *terminal, const char *path)
361{
362 int master, slave;
363 pid_t pid;
364
365 pid = forkpty(&master, NULL, NULL, NULL);
366 if (pid == 0) {
367 close(master);
368 if (execl(path, path, NULL)) {
369 printf("exec failed: %m\n");
370 exit(EXIT_FAILURE);
371 }
372 }
373
374 close(slave);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500375 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500376 terminal->channel = g_io_channel_unix_new(master);
377 fcntl(master, F_SETFL, O_NONBLOCK);
378 g_io_add_watch(terminal->channel, G_IO_IN,
379 io_handler, terminal);
380
381 return 0;
382}
383
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500384int main(int argc, char *argv[])
385{
386 struct wl_display *display;
387 int fd;
388 GMainLoop *loop;
389 GSource *source;
390 struct terminal *terminal;
391
392 fd = open(gem_device, O_RDWR);
393 if (fd < 0) {
394 fprintf(stderr, "drm open failed: %m\n");
395 return -1;
396 }
397
398 display = wl_display_create(socket_name, sizeof socket_name);
399 if (display == NULL) {
400 fprintf(stderr, "failed to create display: %m\n");
401 return -1;
402 }
403
404 loop = g_main_loop_new(NULL, FALSE);
405 source = wl_glib_source_new(display);
406 g_source_attach(source, NULL);
407
408 terminal = terminal_create(display, fd);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500409 terminal_run(terminal, "/bin/bash");
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500410 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500411
412 g_main_loop_run(loop);
413
414 return 0;
415}