blob: be620fcfcea101ac22777ef354bf7d5efb2ff4c8 [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
99 if (terminal->buffer != NULL)
100 buffer_destroy(terminal->buffer, terminal->fd);
101
102 terminal->buffer = buffer_create_from_cairo_surface(terminal->fd, surface);
103 cairo_surface_destroy(surface);
104
105 window_copy(terminal->window,
106 &rectangle,
107 terminal->buffer->name, terminal->buffer->stride);
108}
109
110static void
111terminal_draw(struct terminal *terminal)
112{
113 window_draw(terminal->window);
114 terminal_draw_contents(terminal);
115 wl_display_commit(terminal->display, 0);
116}
117
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500118static gboolean
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500119idle_redraw(void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500120{
121 struct terminal *terminal = data;
122
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500123 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500124
125 return FALSE;
126}
127
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500128#define STATE_NORMAL 0
129#define STATE_SKIP_TO_ALPHA 1
130
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500131static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500132terminal_data(struct terminal *terminal, const char *data, size_t length)
133{
134 int i;
135 char *row;
136
137 for (i = 0; i < length; i++) {
138 row = &terminal->data[terminal->row * (terminal->width + 1)];
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500139
140 if (terminal->state == STATE_SKIP_TO_ALPHA) {
141 if (isalpha(data[i]))
142 terminal->state = STATE_NORMAL;
143 continue;
144 }
145
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500146 switch (data[i]) {
147 case '\r':
148 terminal->column = 0;
149 break;
150 case '\n':
151 terminal->row++;
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500152 terminal->total_rows++;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500153 terminal->column = 0;
154 if (terminal->row == terminal->height)
155 terminal->row = 0;
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500156 if (terminal->row == terminal->tail && terminal->total_rows > 0) {
157 memset(&terminal->data[terminal->row * (terminal->width + 1)],
158 0, terminal->width);
159 terminal->tail++;
160 }
161 if (terminal->tail == terminal->height)
162 terminal->tail = 0;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500163 break;
164 case '\t':
165 memset(&row[terminal->column], ' ', -terminal->column & 7);
166 terminal->column = (terminal->column + 7) & ~7;
167 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500168 case '\e':
169 terminal->state = STATE_SKIP_TO_ALPHA;
170 break;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500171 default:
172 if (terminal->column < terminal->width)
173 row[terminal->column++] = data[i];
174 break;
175 }
176 }
177}
178
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500179static void
180resize_handler(struct window *window, int32_t width, int32_t height, void *data)
181{
182 struct terminal *terminal = data;
183
184 if (!terminal->resize_scheduled) {
185 g_idle_add(idle_redraw, terminal);
186 terminal->resize_scheduled = 1;
187 }
188}
189
190static void
191acknowledge_handler(struct window *window, uint32_t key, void *data)
192{
193 struct terminal *terminal = data;
194
195 terminal->resize_scheduled = 0;
196}
197
198struct key {
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500199 int code[4];
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500200} evdev_keymap[] = {
201 { { 0, 0 } }, /* 0 */
202 { { 0x1b, 0x1b } },
203 { { '1', '!' } },
204 { { '2', '@' } },
205 { { '3', '#' } },
206 { { '4', '$' } },
207 { { '5', '%' } },
208 { { '6', '^' } },
209 { { '7', '&' } },
210 { { '8', '*' } },
211 { { '9', '(' } },
212 { { '0', ')' } },
213 { { '-', '_' } },
214 { { '=', '+' } },
215 { { '\b', '\b' } },
216 { { '\t', '\t' } },
217
218 { { 'q', 'Q' } }, /* 16 */
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500219 { { 'w', 'W', 0x17 } },
220 { { 'e', 'E', 0x05 } },
221 { { 'r', 'R', 0x12 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500222 { { 't', 'T' } },
223 { { 'y', 'Y' } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500224 { { 'u', 'U', 0x15 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500225 { { 'i', 'I' } },
226 { { 'o', 'O' } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500227 { { 'p', 'P', 0x10 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500228 { { '[', '{' } },
229 { { ']', '}' } },
230 { { '\n', '\n' } },
231 { { 0, 0 } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500232 { { 'a', 'A', 0x01} },
233 { { 's', 'S', 0x13 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500234
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500235 { { 'd', 'D', 0x04 } }, /* 32 */
236 { { 'f', 'F', 0x06 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500237 { { 'g', 'G' } },
238 { { 'h', 'H' } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500239 { { 'j', 'J', 0x0a } },
240 { { 'k', 'K', 0x0b } },
241 { { 'l', 'L', 0x0c } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500242 { { ';', ':' } },
243 { { '\'', '"' } },
244 { { '`', '~' } },
245 { { 0, 0 } },
246 { { '\\', '|' } },
247 { { 'z', 'Z' } },
248 { { 'x', 'X' } },
249 { { 'c', 'C' } },
250 { { 'v', 'V' } },
251
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500252 { { 'b', 'B', 0x02 } }, /* 48 */
253 { { 'n', 'N', 0x0e } },
254 { { 'm', 'M', 0x0d } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500255 { { ',', '<' } },
256 { { '.', '>' } },
257 { { '/', '?' } },
258 { { 0, 0 } },
259 { { '*', '*' } },
260 { { 0, 0 } },
261 { { ' ', ' ' } },
262 { { 0, 0 } }
263
264 /* 59 */
265};
266
267#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
268
269static void
270key_handler(struct window *window, uint32_t key, uint32_t state, void *data)
271{
272 struct terminal *terminal = data;
273 uint32_t mod = 0;
274 char c;
275
276 switch (key) {
277 case KEY_LEFTSHIFT:
278 case KEY_RIGHTSHIFT:
279 mod = MOD_SHIFT;
280 break;
281 case KEY_LEFTCTRL:
282 case KEY_RIGHTCTRL:
283 mod = MOD_CTRL;
284 break;
285 case KEY_LEFTALT:
286 case KEY_RIGHTALT:
287 mod = MOD_ALT;
288 break;
289 default:
290 if (key < ARRAY_LENGTH(evdev_keymap)) {
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500291 if (terminal->modifiers & MOD_CTRL)
292 c = evdev_keymap[key].code[2];
293 else if (terminal->modifiers & MOD_SHIFT)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500294 c = evdev_keymap[key].code[1];
295 else
296 c = evdev_keymap[key].code[0];
297 if (state && c)
298 write(terminal->master, &c, 1);
299 }
300 break;
301 }
302
303 if (state)
304 terminal->modifiers |= mod;
305 else
306 terminal->modifiers &= ~mod;
307}
308
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500309static struct terminal *
310terminal_create(struct wl_display *display, int fd)
311{
312 struct terminal *terminal;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500313 int size;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500314
315 terminal = malloc(sizeof *terminal);
316 if (terminal == NULL)
317 return terminal;
318
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500319 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500320 terminal->fd = fd;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500321 terminal->window = window_create(display, fd, "Wayland Terminal",
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500322 500, 100, 500, 400);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500323 terminal->display = display;
324 terminal->resize_scheduled = 1;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500325 terminal->width = 80;
326 terminal->height = 25;
327 size = (terminal->width + 1) * terminal->height;
328 terminal->data = malloc(size);
329 memset(terminal->data, 0, size);
330
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500331 window_set_resize_handler(terminal->window, resize_handler, terminal);
332 window_set_acknowledge_handler(terminal->window, acknowledge_handler, terminal);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500333 window_set_key_handler(terminal->window, key_handler, terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500334
335 return terminal;
336}
337
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500338static gboolean
339io_handler(GIOChannel *source,
340 GIOCondition condition,
341 gpointer data)
342{
343 struct terminal *terminal = data;
344 gchar buffer[256];
345 gsize bytes_read;
346 GError *error = NULL;
347
348 g_io_channel_read_chars(source, buffer, sizeof buffer,
349 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500350
351 terminal_data(terminal, buffer, bytes_read);
352
353 if (!terminal->resize_scheduled) {
354 g_idle_add(idle_redraw, terminal);
355 terminal->resize_scheduled = 1;
356 }
357
358 return TRUE;
359}
360
361static int
362terminal_run(struct terminal *terminal, const char *path)
363{
364 int master, slave;
365 pid_t pid;
366
367 pid = forkpty(&master, NULL, NULL, NULL);
368 if (pid == 0) {
369 close(master);
370 if (execl(path, path, NULL)) {
371 printf("exec failed: %m\n");
372 exit(EXIT_FAILURE);
373 }
374 }
375
376 close(slave);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500377 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500378 terminal->channel = g_io_channel_unix_new(master);
379 fcntl(master, F_SETFL, O_NONBLOCK);
380 g_io_add_watch(terminal->channel, G_IO_IN,
381 io_handler, terminal);
382
383 return 0;
384}
385
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500386int main(int argc, char *argv[])
387{
388 struct wl_display *display;
389 int fd;
390 GMainLoop *loop;
391 GSource *source;
392 struct terminal *terminal;
393
394 fd = open(gem_device, O_RDWR);
395 if (fd < 0) {
396 fprintf(stderr, "drm open failed: %m\n");
397 return -1;
398 }
399
400 display = wl_display_create(socket_name, sizeof socket_name);
401 if (display == NULL) {
402 fprintf(stderr, "failed to create display: %m\n");
403 return -1;
404 }
405
406 loop = g_main_loop_new(NULL, FALSE);
407 source = wl_glib_source_new(display);
408 g_source_attach(source, NULL);
409
410 terminal = terminal_create(display, fd);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500411 terminal_run(terminal, "/bin/bash");
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500412 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500413
414 g_main_loop_run(loop);
415
416 return 0;
417}