blob: 8087938ff1ec6e189bead63bddd4ece105f94f7d [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øgsbergdbd54642008-12-08 22:22:25 -050058 int width, height, start, row, column;
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øgsbergdbd54642008-12-08 22:22:25 -050069static char *
70terminal_get_row(struct terminal *terminal, int row)
71{
72 int index;
73
74 index = (row + terminal->start) % terminal->height;
75
76 return &terminal->data[index * (terminal->width + 1)];
77}
78
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050079static void
Kristian Høgsberg22106762008-12-08 13:50:07 -050080terminal_resize(struct terminal *terminal, int width, int height)
81{
82 size_t size;
83 char *data;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050084 int i, l, total_rows, start;
Kristian Høgsberg22106762008-12-08 13:50:07 -050085
86 if (terminal->width == width && terminal->height == height)
87 return;
88
89 size = (width + 1) * height;
90 data = malloc(size);
91 memset(data, 0, size);
92 if (terminal->data) {
93 if (width > terminal->width)
94 l = terminal->width;
95 else
96 l = width;
97
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050098 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -050099 total_rows = height;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500100 start = terminal->height - height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500101 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500102 total_rows = terminal->height;
103 start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500104 }
105
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500106 for (i = 0; i < total_rows; i++)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500107 memcpy(data + (width + 1) * i,
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500108 terminal_get_row(terminal, i), l);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500109
110 free(terminal->data);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500111 }
112
113 terminal->width = width;
114 terminal->height = height;
115 terminal->data = data;
116
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500117 if (terminal->row >= terminal->height)
118 terminal->row = terminal->height - 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500119 if (terminal->column >= terminal->width)
120 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500121 terminal->start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500122}
123
124static void
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500125terminal_draw_contents(struct terminal *terminal)
126{
127 struct rectangle rectangle;
128 cairo_surface_t *surface;
129 cairo_t *cr;
130 cairo_font_extents_t extents;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500131 int i;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500132
133 window_get_child_rectangle(terminal->window, &rectangle);
134
135 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
136 rectangle.width, rectangle.height);
137 cr = cairo_create(surface);
138 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
139 cairo_set_source_rgba(cr, 0, 0, 0, 0.9);
140 cairo_paint(cr);
141 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsberg6e0a2f82008-12-08 14:06:56 -0500142 cairo_set_source_rgba(cr, 0, 0.7, 0, 1);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500143
144 cairo_select_font_face (cr, "mono",
145 CAIRO_FONT_SLANT_NORMAL,
146 CAIRO_FONT_WEIGHT_NORMAL);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500147 cairo_set_font_size(cr, 14);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500148
149 cairo_font_extents(cr, &extents);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500150 for (i = 0; i < terminal->height; i++) {
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500151 cairo_move_to(cr, terminal->margin,
152 terminal->margin + extents.ascent + extents.height * i);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500153 cairo_show_text(cr, terminal_get_row(terminal, i));
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500154 }
155 cairo_destroy(cr);
156
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500157 terminal->buffer = buffer_create_from_cairo_surface(terminal->fd, surface);
158 cairo_surface_destroy(surface);
159
160 window_copy(terminal->window,
161 &rectangle,
162 terminal->buffer->name, terminal->buffer->stride);
163}
164
165static void
166terminal_draw(struct terminal *terminal)
167{
Kristian Høgsberg22106762008-12-08 13:50:07 -0500168 struct rectangle rectangle;
169 cairo_surface_t *surface;
170 cairo_font_extents_t extents;
171 cairo_t *cr;
172 int width, height;
173
174 window_get_child_rectangle(terminal->window, &rectangle);
175
176 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
177 cr = cairo_create(surface);
178 cairo_select_font_face (cr, "mono",
179 CAIRO_FONT_SLANT_NORMAL,
180 CAIRO_FONT_WEIGHT_NORMAL);
181 cairo_set_font_size(cr, 14);
182 cairo_font_extents(cr, &extents);
183 cairo_destroy(cr);
184 cairo_surface_destroy(surface);
185
186 width = (rectangle.width - 2 * terminal->margin) / (int32_t) extents.max_x_advance;
187 height = (rectangle.height - 2 * terminal->margin) / (int32_t) extents.height;
188 terminal_resize(terminal, width, height);
189
190 rectangle.width = terminal->width * extents.max_x_advance + 2 * terminal->margin;
191 rectangle.height = terminal->height * extents.height + 2 * terminal->margin;
192
193 window_set_child_size(terminal->window, &rectangle);
194
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500195 window_draw(terminal->window);
196 terminal_draw_contents(terminal);
197 wl_display_commit(terminal->display, 0);
198}
199
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500200static gboolean
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500201idle_redraw(void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500202{
203 struct terminal *terminal = data;
204
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500205 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500206
207 return FALSE;
208}
209
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500210#define STATE_NORMAL 0
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500211#define STATE_ESCAPE 1
212
213static void
214terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500215
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500216static void
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500217terminal_schedule_redraw(struct terminal *terminal)
218{
219 if (!terminal->redraw_scheduled) {
220 g_idle_add(idle_redraw, terminal);
221 terminal->redraw_scheduled = 1;
222 } else {
223 terminal->redraw_pending = 1;
224 }
225}
226
227static void
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500228terminal_data(struct terminal *terminal, const char *data, size_t length);
229
230static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500231handle_escape(struct terminal *terminal)
232{
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500233 char *row, *p;
234 int i, count;
235 int args[10], set[10] = { 0, };
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500236
237 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500238 i = 0;
239 p = &terminal->escape[2];
240 while ((isdigit(*p) || *p == ';') && i < 10) {
241 if (*p == ';') {
242 p++;
243 i++;
244 } else {
245 args[i] = strtol(p, &p, 10);
246 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500247 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500248 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500249
250 switch (*p) {
251 case 'A':
252 count = set[0] ? args[0] : 1;
253 if (terminal->row - count >= 0)
254 terminal->row -= count;
255 else
256 terminal->row = 0;
257 break;
258 case 'B':
259 count = set[0] ? args[0] : 1;
260 if (terminal->row + count < terminal->height)
261 terminal->row += count;
262 else
263 terminal->row = terminal->height;
264 break;
265 case 'C':
266 count = set[0] ? args[0] : 1;
267 if (terminal->column + count < terminal->width)
268 terminal->column += count;
269 else
270 terminal->column = terminal->width;
271 break;
272 case 'D':
273 count = set[0] ? args[0] : 1;
274 if (terminal->column - count >= 0)
275 terminal->column -= count;
276 else
277 terminal->column = 0;
278 break;
279 case 'J':
280 row = terminal_get_row(terminal, terminal->row);
281 memset(&row[terminal->column], 0, terminal->width - terminal->column);
282 for (i = terminal->row + 1; i < terminal->height; i++)
283 memset(terminal_get_row(terminal, i), 0, terminal->width);
284 break;
285 case 'G':
286 if (set[0])
287 terminal->column = args[0] - 1;
288 break;
289 case 'H':
290 case 'f':
291 terminal->row = set[0] ? args[0] - 1 : 0;
292 terminal->column = set[1] ? args[1] - 1 : 0;
293 break;
294 case 'K':
295 row = terminal_get_row(terminal, terminal->row);
296 memset(&row[terminal->column], 0, terminal->width - terminal->column);
297 break;
298 case 'm':
299 /* color, blink, bold etc*/
300 break;
301 case '?':
302 if (strcmp(p, "?25l") == 0) {
303 /* hide cursor */
304 } else if (strcmp(p, "?25h") == 0) {
305 /* show cursor */
306 }
307 break;
308 default:
309 terminal_data(terminal,
310 terminal->escape + 1,
311 terminal->escape_length - 2);
312 break;
313 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500314}
315
316static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500317terminal_data(struct terminal *terminal, const char *data, size_t length)
318{
319 int i;
320 char *row;
321
322 for (i = 0; i < length; i++) {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500323 row = terminal_get_row(terminal, terminal->row);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500324
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500325 if (terminal->state == STATE_ESCAPE) {
326 terminal->escape[terminal->escape_length++] = data[i];
327 if (terminal->escape_length == 2 && data[i] != '[') {
328 /* Bad escape sequence. */
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500329 terminal->state = STATE_NORMAL;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500330 goto cancel_escape;
331 }
332
333 if (isalpha(data[i])) {
334 terminal->state = STATE_NORMAL;
335 handle_escape(terminal);
336 }
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500337 continue;
338 }
339
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500340 cancel_escape:
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500341 switch (data[i]) {
342 case '\r':
343 terminal->column = 0;
344 break;
345 case '\n':
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500346 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500347 if (terminal->row + 1 < terminal->height) {
348 terminal->row++;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500349 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500350 terminal->start++;
351 if (terminal->start == terminal->height)
352 terminal->start = 0;
353 memset(terminal_get_row(terminal, terminal->row),
354 0, terminal->width);
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500355 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500356
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500357 break;
358 case '\t':
359 memset(&row[terminal->column], ' ', -terminal->column & 7);
360 terminal->column = (terminal->column + 7) & ~7;
361 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500362 case '\e':
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500363 terminal->state = STATE_ESCAPE;
364 terminal->escape[0] = '\e';
365 terminal->escape_length = 1;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500366 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500367 case '\b':
368 if (terminal->column > 0)
369 terminal->column--;
370 break;
371 case '\a':
372 /* Bell */
373 break;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500374 default:
375 if (terminal->column < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500376 row[terminal->column++] = data[i] < 32 ? data[i] + 64 : data[i];
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500377 break;
378 }
379 }
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500380
381 terminal_schedule_redraw(terminal);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500382}
383
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500384static void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500385resize_handler(struct window *window, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500386{
387 struct terminal *terminal = data;
388
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500389 terminal_schedule_redraw(terminal);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500390}
391
392static void
393acknowledge_handler(struct window *window, uint32_t key, void *data)
394{
395 struct terminal *terminal = data;
396
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500397 terminal->redraw_scheduled = 0;
Kristian Høgsbergc47303f2008-12-08 09:57:08 -0500398 buffer_destroy(terminal->buffer, terminal->fd);
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500399
400 if (terminal->redraw_pending) {
401 terminal->redraw_pending = 0;
402 terminal_schedule_redraw(terminal);
403 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500404}
405
406struct key {
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500407 int code[4];
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500408} evdev_keymap[] = {
409 { { 0, 0 } }, /* 0 */
410 { { 0x1b, 0x1b } },
411 { { '1', '!' } },
412 { { '2', '@' } },
413 { { '3', '#' } },
414 { { '4', '$' } },
415 { { '5', '%' } },
416 { { '6', '^' } },
417 { { '7', '&' } },
418 { { '8', '*' } },
419 { { '9', '(' } },
420 { { '0', ')' } },
421 { { '-', '_' } },
422 { { '=', '+' } },
423 { { '\b', '\b' } },
424 { { '\t', '\t' } },
425
426 { { 'q', 'Q' } }, /* 16 */
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500427 { { 'w', 'W', 0x17 } },
428 { { 'e', 'E', 0x05 } },
429 { { 'r', 'R', 0x12 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500430 { { 't', 'T' } },
431 { { 'y', 'Y' } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500432 { { 'u', 'U', 0x15 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500433 { { 'i', 'I' } },
434 { { 'o', 'O' } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500435 { { 'p', 'P', 0x10 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500436 { { '[', '{' } },
437 { { ']', '}' } },
438 { { '\n', '\n' } },
439 { { 0, 0 } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500440 { { 'a', 'A', 0x01} },
441 { { 's', 'S', 0x13 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500442
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500443 { { 'd', 'D', 0x04 } }, /* 32 */
444 { { 'f', 'F', 0x06 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500445 { { 'g', 'G' } },
446 { { 'h', 'H' } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500447 { { 'j', 'J', 0x0a } },
448 { { 'k', 'K', 0x0b } },
449 { { 'l', 'L', 0x0c } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500450 { { ';', ':' } },
451 { { '\'', '"' } },
452 { { '`', '~' } },
453 { { 0, 0 } },
454 { { '\\', '|' } },
455 { { 'z', 'Z' } },
456 { { 'x', 'X' } },
457 { { 'c', 'C' } },
458 { { 'v', 'V' } },
459
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500460 { { 'b', 'B', 0x02 } }, /* 48 */
461 { { 'n', 'N', 0x0e } },
462 { { 'm', 'M', 0x0d } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500463 { { ',', '<' } },
464 { { '.', '>' } },
465 { { '/', '?' } },
466 { { 0, 0 } },
467 { { '*', '*' } },
468 { { 0, 0 } },
469 { { ' ', ' ' } },
470 { { 0, 0 } }
471
472 /* 59 */
473};
474
475#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
476
477static void
478key_handler(struct window *window, uint32_t key, uint32_t state, void *data)
479{
480 struct terminal *terminal = data;
481 uint32_t mod = 0;
482 char c;
483
484 switch (key) {
485 case KEY_LEFTSHIFT:
486 case KEY_RIGHTSHIFT:
487 mod = MOD_SHIFT;
488 break;
489 case KEY_LEFTCTRL:
490 case KEY_RIGHTCTRL:
491 mod = MOD_CTRL;
492 break;
493 case KEY_LEFTALT:
494 case KEY_RIGHTALT:
495 mod = MOD_ALT;
496 break;
497 default:
498 if (key < ARRAY_LENGTH(evdev_keymap)) {
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500499 if (terminal->modifiers & MOD_CTRL)
500 c = evdev_keymap[key].code[2];
501 else if (terminal->modifiers & MOD_SHIFT)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500502 c = evdev_keymap[key].code[1];
503 else
504 c = evdev_keymap[key].code[0];
505 if (state && c)
506 write(terminal->master, &c, 1);
507 }
508 break;
509 }
510
511 if (state)
512 terminal->modifiers |= mod;
513 else
514 terminal->modifiers &= ~mod;
515}
516
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500517static struct terminal *
518terminal_create(struct wl_display *display, int fd)
519{
520 struct terminal *terminal;
521
522 terminal = malloc(sizeof *terminal);
523 if (terminal == NULL)
524 return terminal;
525
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500526 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500527 terminal->fd = fd;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500528 terminal->window = window_create(display, fd, "Wayland Terminal",
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500529 500, 100, 500, 400);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500530 terminal->display = display;
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500531 terminal->redraw_scheduled = 1;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500532 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500533
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500534 window_set_resize_handler(terminal->window, resize_handler, terminal);
535 window_set_acknowledge_handler(terminal->window, acknowledge_handler, terminal);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500536 window_set_key_handler(terminal->window, key_handler, terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500537
Kristian Høgsberg22106762008-12-08 13:50:07 -0500538 terminal_draw(terminal);
539
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500540 return terminal;
541}
542
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500543static gboolean
544io_handler(GIOChannel *source,
545 GIOCondition condition,
546 gpointer data)
547{
548 struct terminal *terminal = data;
549 gchar buffer[256];
550 gsize bytes_read;
551 GError *error = NULL;
552
553 g_io_channel_read_chars(source, buffer, sizeof buffer,
554 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500555
556 terminal_data(terminal, buffer, bytes_read);
557
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500558 return TRUE;
559}
560
561static int
562terminal_run(struct terminal *terminal, const char *path)
563{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500564 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500565 pid_t pid;
566
567 pid = forkpty(&master, NULL, NULL, NULL);
568 if (pid == 0) {
569 close(master);
570 if (execl(path, path, NULL)) {
571 printf("exec failed: %m\n");
572 exit(EXIT_FAILURE);
573 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500574 } else if (pid < 0) {
575 fprintf(stderr, "failed to fork and create pty (%m).\n");
576 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500577 }
578
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500579 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500580 terminal->channel = g_io_channel_unix_new(master);
581 fcntl(master, F_SETFL, O_NONBLOCK);
582 g_io_add_watch(terminal->channel, G_IO_IN,
583 io_handler, terminal);
584
585 return 0;
586}
587
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500588int main(int argc, char *argv[])
589{
590 struct wl_display *display;
591 int fd;
592 GMainLoop *loop;
593 GSource *source;
594 struct terminal *terminal;
595
596 fd = open(gem_device, O_RDWR);
597 if (fd < 0) {
598 fprintf(stderr, "drm open failed: %m\n");
599 return -1;
600 }
601
602 display = wl_display_create(socket_name, sizeof socket_name);
603 if (display == NULL) {
604 fprintf(stderr, "failed to create display: %m\n");
605 return -1;
606 }
607
608 loop = g_main_loop_new(NULL, FALSE);
609 source = wl_glib_source_new(display);
610 g_source_attach(source, NULL);
611
612 terminal = terminal_create(display, fd);
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500613 if (terminal_run(terminal, "/bin/bash"))
614 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500615
616 g_main_loop_run(loop);
617
618 return 0;
619}