blob: 499e57075f34704d2a75c309426c5c10252d6f94 [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
Kristian Høgsberg22106762008-12-08 13:50:07 -050070terminal_resize(struct terminal *terminal, int width, int height)
71{
72 size_t size;
73 char *data;
74 int i, l, total_rows, row, tail;
75
76 if (terminal->width == width && terminal->height == height)
77 return;
78
79 size = (width + 1) * height;
80 data = malloc(size);
81 memset(data, 0, size);
82 if (terminal->data) {
83 if (width > terminal->width)
84 l = terminal->width;
85 else
86 l = width;
87
88 if (terminal->total_rows > height) {
89 total_rows = height;
90 tail = terminal->tail + terminal->total_rows - height;
91 } else {
92 total_rows = terminal->total_rows;
93 tail = terminal->tail;
94 }
95
96 for (i = 0; i < total_rows; i++) {
97 row = (tail + i) % terminal->height;
98 memcpy(data + (width + 1) * i,
99 &terminal->data[row * (terminal->width + 1)], l);
100 }
101
102 free(terminal->data);
103 } else {
104 total_rows = 1;
105 }
106
107 terminal->width = width;
108 terminal->height = height;
109 terminal->data = data;
110
111 terminal->total_rows = total_rows;
112 terminal->row = total_rows - 1;
113 if (terminal->column >= terminal->width)
114 terminal->column = terminal->width - 1;
115 terminal->tail = 0;
116}
117
118static void
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500119terminal_draw_contents(struct terminal *terminal)
120{
121 struct rectangle rectangle;
122 cairo_surface_t *surface;
123 cairo_t *cr;
124 cairo_font_extents_t extents;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500125 int i, row;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500126
127 window_get_child_rectangle(terminal->window, &rectangle);
128
129 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
130 rectangle.width, rectangle.height);
131 cr = cairo_create(surface);
132 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
133 cairo_set_source_rgba(cr, 0, 0, 0, 0.9);
134 cairo_paint(cr);
135 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
136 cairo_set_source_rgba(cr, 0, 0.5, 0, 1);
137
138 cairo_select_font_face (cr, "mono",
139 CAIRO_FONT_SLANT_NORMAL,
140 CAIRO_FONT_WEIGHT_NORMAL);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500141 cairo_set_font_size(cr, 14);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500142
143 cairo_font_extents(cr, &extents);
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500144 for (i = 0; i < terminal->total_rows; i++) {
145 row = (terminal->tail + i) % terminal->height;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500146 cairo_move_to(cr, terminal->margin,
147 terminal->margin + extents.ascent + extents.height * i);
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500148 cairo_show_text(cr, &terminal->data[row * (terminal->width + 1)]);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500149 }
150 cairo_destroy(cr);
151
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500152 terminal->buffer = buffer_create_from_cairo_surface(terminal->fd, surface);
153 cairo_surface_destroy(surface);
154
155 window_copy(terminal->window,
156 &rectangle,
157 terminal->buffer->name, terminal->buffer->stride);
158}
159
160static void
161terminal_draw(struct terminal *terminal)
162{
Kristian Høgsberg22106762008-12-08 13:50:07 -0500163 struct rectangle rectangle;
164 cairo_surface_t *surface;
165 cairo_font_extents_t extents;
166 cairo_t *cr;
167 int width, height;
168
169 window_get_child_rectangle(terminal->window, &rectangle);
170
171 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
172 cr = cairo_create(surface);
173 cairo_select_font_face (cr, "mono",
174 CAIRO_FONT_SLANT_NORMAL,
175 CAIRO_FONT_WEIGHT_NORMAL);
176 cairo_set_font_size(cr, 14);
177 cairo_font_extents(cr, &extents);
178 cairo_destroy(cr);
179 cairo_surface_destroy(surface);
180
181 width = (rectangle.width - 2 * terminal->margin) / (int32_t) extents.max_x_advance;
182 height = (rectangle.height - 2 * terminal->margin) / (int32_t) extents.height;
183 terminal_resize(terminal, width, height);
184
185 rectangle.width = terminal->width * extents.max_x_advance + 2 * terminal->margin;
186 rectangle.height = terminal->height * extents.height + 2 * terminal->margin;
187
188 window_set_child_size(terminal->window, &rectangle);
189
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500190 window_draw(terminal->window);
191 terminal_draw_contents(terminal);
192 wl_display_commit(terminal->display, 0);
193}
194
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500195static gboolean
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500196idle_redraw(void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500197{
198 struct terminal *terminal = data;
199
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500200 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500201
202 return FALSE;
203}
204
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500205#define STATE_NORMAL 0
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500206#define STATE_ESCAPE 1
207
208static void
209terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500210
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500211static void
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500212terminal_schedule_redraw(struct terminal *terminal)
213{
214 if (!terminal->redraw_scheduled) {
215 g_idle_add(idle_redraw, terminal);
216 terminal->redraw_scheduled = 1;
217 } else {
218 terminal->redraw_pending = 1;
219 }
220}
221
222static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500223handle_escape(struct terminal *terminal)
224{
225 char *row;
226 int i, j;
227
228 terminal->escape[terminal->escape_length++] = '\0';
229 if (strcmp(terminal->escape, "\e[J") == 0) {
230 row = &terminal->data[terminal->row * (terminal->width + 1)];
231 memset(&row[terminal->column], 0, terminal->width - terminal->column);
232 for (i = terminal->total_rows; i < terminal->height; i++) {
233
234 j = terminal->row + i;
235 if (j >= terminal->height)
236 j -= terminal->height;
237
238 row = &terminal->data[j * (terminal->width + 1)];
239 memset(row, 0, terminal->width);
240 }
241 } else if (strcmp(terminal->escape, "\e[H") == 0) {
242 terminal->row = terminal->tail;
243 terminal->total_rows = 1;
244 terminal->column = 0;
245 }
246}
247
248static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500249terminal_data(struct terminal *terminal, const char *data, size_t length)
250{
251 int i;
252 char *row;
253
254 for (i = 0; i < length; i++) {
255 row = &terminal->data[terminal->row * (terminal->width + 1)];
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500256
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500257 if (terminal->state == STATE_ESCAPE) {
258 terminal->escape[terminal->escape_length++] = data[i];
259 if (terminal->escape_length == 2 && data[i] != '[') {
260 /* Bad escape sequence. */
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500261 terminal->state = STATE_NORMAL;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500262 goto cancel_escape;
263 }
264
265 if (isalpha(data[i])) {
266 terminal->state = STATE_NORMAL;
267 handle_escape(terminal);
268 }
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500269 continue;
270 }
271
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500272 cancel_escape:
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500273 switch (data[i]) {
274 case '\r':
275 terminal->column = 0;
276 break;
277 case '\n':
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500278 terminal->column = 0;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500279 terminal->row++;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500280 if (terminal->row == terminal->height)
281 terminal->row = 0;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500282 if (terminal->total_rows == terminal->height) {
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500283 memset(&terminal->data[terminal->row * (terminal->width + 1)],
284 0, terminal->width);
285 terminal->tail++;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500286 } else {
287 terminal->total_rows++;
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500288 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500289
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500290 if (terminal->tail == terminal->height)
291 terminal->tail = 0;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500292 break;
293 case '\t':
294 memset(&row[terminal->column], ' ', -terminal->column & 7);
295 terminal->column = (terminal->column + 7) & ~7;
296 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500297 case '\e':
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500298 terminal->state = STATE_ESCAPE;
299 terminal->escape[0] = '\e';
300 terminal->escape_length = 1;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500301 break;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500302 default:
303 if (terminal->column < terminal->width)
304 row[terminal->column++] = data[i];
305 break;
306 }
307 }
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500308
309 terminal_schedule_redraw(terminal);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500310}
311
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500312static void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500313resize_handler(struct window *window, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500314{
315 struct terminal *terminal = data;
316
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500317 terminal_schedule_redraw(terminal);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500318}
319
320static void
321acknowledge_handler(struct window *window, uint32_t key, void *data)
322{
323 struct terminal *terminal = data;
324
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500325 terminal->redraw_scheduled = 0;
Kristian Høgsbergc47303f2008-12-08 09:57:08 -0500326 buffer_destroy(terminal->buffer, terminal->fd);
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500327
328 if (terminal->redraw_pending) {
329 terminal->redraw_pending = 0;
330 terminal_schedule_redraw(terminal);
331 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500332}
333
334struct key {
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500335 int code[4];
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500336} evdev_keymap[] = {
337 { { 0, 0 } }, /* 0 */
338 { { 0x1b, 0x1b } },
339 { { '1', '!' } },
340 { { '2', '@' } },
341 { { '3', '#' } },
342 { { '4', '$' } },
343 { { '5', '%' } },
344 { { '6', '^' } },
345 { { '7', '&' } },
346 { { '8', '*' } },
347 { { '9', '(' } },
348 { { '0', ')' } },
349 { { '-', '_' } },
350 { { '=', '+' } },
351 { { '\b', '\b' } },
352 { { '\t', '\t' } },
353
354 { { 'q', 'Q' } }, /* 16 */
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500355 { { 'w', 'W', 0x17 } },
356 { { 'e', 'E', 0x05 } },
357 { { 'r', 'R', 0x12 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500358 { { 't', 'T' } },
359 { { 'y', 'Y' } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500360 { { 'u', 'U', 0x15 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500361 { { 'i', 'I' } },
362 { { 'o', 'O' } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500363 { { 'p', 'P', 0x10 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500364 { { '[', '{' } },
365 { { ']', '}' } },
366 { { '\n', '\n' } },
367 { { 0, 0 } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500368 { { 'a', 'A', 0x01} },
369 { { 's', 'S', 0x13 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500370
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500371 { { 'd', 'D', 0x04 } }, /* 32 */
372 { { 'f', 'F', 0x06 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500373 { { 'g', 'G' } },
374 { { 'h', 'H' } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500375 { { 'j', 'J', 0x0a } },
376 { { 'k', 'K', 0x0b } },
377 { { 'l', 'L', 0x0c } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500378 { { ';', ':' } },
379 { { '\'', '"' } },
380 { { '`', '~' } },
381 { { 0, 0 } },
382 { { '\\', '|' } },
383 { { 'z', 'Z' } },
384 { { 'x', 'X' } },
385 { { 'c', 'C' } },
386 { { 'v', 'V' } },
387
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500388 { { 'b', 'B', 0x02 } }, /* 48 */
389 { { 'n', 'N', 0x0e } },
390 { { 'm', 'M', 0x0d } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500391 { { ',', '<' } },
392 { { '.', '>' } },
393 { { '/', '?' } },
394 { { 0, 0 } },
395 { { '*', '*' } },
396 { { 0, 0 } },
397 { { ' ', ' ' } },
398 { { 0, 0 } }
399
400 /* 59 */
401};
402
403#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
404
405static void
406key_handler(struct window *window, uint32_t key, uint32_t state, void *data)
407{
408 struct terminal *terminal = data;
409 uint32_t mod = 0;
410 char c;
411
412 switch (key) {
413 case KEY_LEFTSHIFT:
414 case KEY_RIGHTSHIFT:
415 mod = MOD_SHIFT;
416 break;
417 case KEY_LEFTCTRL:
418 case KEY_RIGHTCTRL:
419 mod = MOD_CTRL;
420 break;
421 case KEY_LEFTALT:
422 case KEY_RIGHTALT:
423 mod = MOD_ALT;
424 break;
425 default:
426 if (key < ARRAY_LENGTH(evdev_keymap)) {
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500427 if (terminal->modifiers & MOD_CTRL)
428 c = evdev_keymap[key].code[2];
429 else if (terminal->modifiers & MOD_SHIFT)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500430 c = evdev_keymap[key].code[1];
431 else
432 c = evdev_keymap[key].code[0];
433 if (state && c)
434 write(terminal->master, &c, 1);
435 }
436 break;
437 }
438
439 if (state)
440 terminal->modifiers |= mod;
441 else
442 terminal->modifiers &= ~mod;
443}
444
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500445static struct terminal *
446terminal_create(struct wl_display *display, int fd)
447{
448 struct terminal *terminal;
449
450 terminal = malloc(sizeof *terminal);
451 if (terminal == NULL)
452 return terminal;
453
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500454 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500455 terminal->fd = fd;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500456 terminal->window = window_create(display, fd, "Wayland Terminal",
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500457 500, 100, 500, 400);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500458 terminal->display = display;
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500459 terminal->redraw_scheduled = 1;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500460 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500461
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500462 window_set_resize_handler(terminal->window, resize_handler, terminal);
463 window_set_acknowledge_handler(terminal->window, acknowledge_handler, terminal);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500464 window_set_key_handler(terminal->window, key_handler, terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500465
Kristian Høgsberg22106762008-12-08 13:50:07 -0500466 terminal_draw(terminal);
467
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500468 return terminal;
469}
470
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500471static gboolean
472io_handler(GIOChannel *source,
473 GIOCondition condition,
474 gpointer data)
475{
476 struct terminal *terminal = data;
477 gchar buffer[256];
478 gsize bytes_read;
479 GError *error = NULL;
480
481 g_io_channel_read_chars(source, buffer, sizeof buffer,
482 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500483
484 terminal_data(terminal, buffer, bytes_read);
485
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500486 return TRUE;
487}
488
489static int
490terminal_run(struct terminal *terminal, const char *path)
491{
492 int master, slave;
493 pid_t pid;
494
495 pid = forkpty(&master, NULL, NULL, NULL);
496 if (pid == 0) {
497 close(master);
498 if (execl(path, path, NULL)) {
499 printf("exec failed: %m\n");
500 exit(EXIT_FAILURE);
501 }
502 }
503
504 close(slave);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500505 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500506 terminal->channel = g_io_channel_unix_new(master);
507 fcntl(master, F_SETFL, O_NONBLOCK);
508 g_io_add_watch(terminal->channel, G_IO_IN,
509 io_handler, terminal);
510
511 return 0;
512}
513
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500514int main(int argc, char *argv[])
515{
516 struct wl_display *display;
517 int fd;
518 GMainLoop *loop;
519 GSource *source;
520 struct terminal *terminal;
521
522 fd = open(gem_device, O_RDWR);
523 if (fd < 0) {
524 fprintf(stderr, "drm open failed: %m\n");
525 return -1;
526 }
527
528 display = wl_display_create(socket_name, sizeof socket_name);
529 if (display == NULL) {
530 fprintf(stderr, "failed to create display: %m\n");
531 return -1;
532 }
533
534 loop = g_main_loop_new(NULL, FALSE);
535 source = wl_glib_source_new(display);
536 g_source_attach(source, NULL);
537
538 terminal = terminal_create(display, fd);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500539 terminal_run(terminal, "/bin/bash");
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500540
541 g_main_loop_run(loop);
542
543 return 0;
544}