blob: 61d569798cdf9c5e6ce8fe4c84e45791a67347f3 [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
Kristian Høgsberg0395f302008-12-22 12:14:50 -050046static int option_fullscreen;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050047static const char gem_device[] = "/dev/dri/card0";
48static const char socket_name[] = "\0wayland";
49
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050050#define MOD_SHIFT 0x01
51#define MOD_ALT 0x02
52#define MOD_CTRL 0x04
53
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050054struct terminal {
55 struct window *window;
56 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050057 struct wl_compositor *compositor;
Kristian Høgsberg721f09f2008-12-08 11:13:26 -050058 int redraw_scheduled, redraw_pending;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050059 char *data;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050060 int width, height, start, row, column;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050061 int fd, master;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050062 struct buffer *buffer;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050063 GIOChannel *channel;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050064 uint32_t modifiers;
Kristian Høgsberg17809b12008-12-08 12:20:40 -050065 char escape[64];
66 int escape_length;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -050067 int state;
Kristian Høgsberg1584c572008-12-08 12:59:37 -050068 int margin;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050069 int fullscreen;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050070};
71
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050072static char *
73terminal_get_row(struct terminal *terminal, int row)
74{
75 int index;
76
77 index = (row + terminal->start) % terminal->height;
78
79 return &terminal->data[index * (terminal->width + 1)];
80}
81
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050082static void
Kristian Høgsberg22106762008-12-08 13:50:07 -050083terminal_resize(struct terminal *terminal, int width, int height)
84{
85 size_t size;
86 char *data;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -050087 int i, l, total_rows, start;
Kristian Høgsberg22106762008-12-08 13:50:07 -050088
89 if (terminal->width == width && terminal->height == height)
90 return;
91
92 size = (width + 1) * height;
93 data = malloc(size);
94 memset(data, 0, size);
95 if (terminal->data) {
96 if (width > terminal->width)
97 l = terminal->width;
98 else
99 l = width;
100
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500101 if (terminal->height > height) {
Kristian Høgsberg22106762008-12-08 13:50:07 -0500102 total_rows = height;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500103 start = terminal->height - height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500104 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500105 total_rows = terminal->height;
106 start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500107 }
108
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500109 for (i = 0; i < total_rows; i++)
Kristian Høgsberg22106762008-12-08 13:50:07 -0500110 memcpy(data + (width + 1) * i,
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500111 terminal_get_row(terminal, i), l);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500112
113 free(terminal->data);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500114 }
115
116 terminal->width = width;
117 terminal->height = height;
118 terminal->data = data;
119
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500120 if (terminal->row >= terminal->height)
121 terminal->row = terminal->height - 1;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500122 if (terminal->column >= terminal->width)
123 terminal->column = terminal->width - 1;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500124 terminal->start = 0;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500125}
126
127static void
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500128terminal_draw_contents(struct terminal *terminal)
129{
130 struct rectangle rectangle;
131 cairo_surface_t *surface;
132 cairo_t *cr;
133 cairo_font_extents_t extents;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500134 int i, top_margin, side_margin;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500135
136 window_get_child_rectangle(terminal->window, &rectangle);
137
138 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
139 rectangle.width, rectangle.height);
140 cr = cairo_create(surface);
141 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
142 cairo_set_source_rgba(cr, 0, 0, 0, 0.9);
143 cairo_paint(cr);
144 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsberg6e0a2f82008-12-08 14:06:56 -0500145 cairo_set_source_rgba(cr, 0, 0.7, 0, 1);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500146
147 cairo_select_font_face (cr, "mono",
148 CAIRO_FONT_SLANT_NORMAL,
149 CAIRO_FONT_WEIGHT_NORMAL);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500150 cairo_set_font_size(cr, 14);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500151
152 cairo_font_extents(cr, &extents);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500153 side_margin = (rectangle.width - terminal->width * extents.max_x_advance) / 2;
154 top_margin = (rectangle.height - terminal->height * extents.height) / 2;
155
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500156 for (i = 0; i < terminal->height; i++) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500157 cairo_move_to(cr, side_margin,
158 top_margin + extents.ascent + extents.height * i);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500159 cairo_show_text(cr, terminal_get_row(terminal, i));
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500160 }
161 cairo_destroy(cr);
162
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500163 terminal->buffer = buffer_create_from_cairo_surface(terminal->fd, surface);
164 cairo_surface_destroy(surface);
165
166 window_copy(terminal->window,
167 &rectangle,
168 terminal->buffer->name, terminal->buffer->stride);
169}
170
171static void
172terminal_draw(struct terminal *terminal)
173{
Kristian Høgsberg22106762008-12-08 13:50:07 -0500174 struct rectangle rectangle;
175 cairo_surface_t *surface;
176 cairo_font_extents_t extents;
177 cairo_t *cr;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500178 int32_t width, height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500179
180 window_get_child_rectangle(terminal->window, &rectangle);
181
182 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
183 cr = cairo_create(surface);
184 cairo_select_font_face (cr, "mono",
185 CAIRO_FONT_SLANT_NORMAL,
186 CAIRO_FONT_WEIGHT_NORMAL);
187 cairo_set_font_size(cr, 14);
188 cairo_font_extents(cr, &extents);
189 cairo_destroy(cr);
190 cairo_surface_destroy(surface);
191
192 width = (rectangle.width - 2 * terminal->margin) / (int32_t) extents.max_x_advance;
193 height = (rectangle.height - 2 * terminal->margin) / (int32_t) extents.height;
194 terminal_resize(terminal, width, height);
195
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500196 if (!terminal->fullscreen) {
197 rectangle.width = terminal->width * extents.max_x_advance + 2 * terminal->margin;
198 rectangle.height = terminal->height * extents.height + 2 * terminal->margin;
199 window_set_child_size(terminal->window, &rectangle);
200 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500201
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500202 window_draw(terminal->window);
203 terminal_draw_contents(terminal);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500204 wl_compositor_commit(terminal->compositor, 0);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500205}
206
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500207static gboolean
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500208idle_redraw(void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500209{
210 struct terminal *terminal = data;
211
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500212 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500213
214 return FALSE;
215}
216
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500217#define STATE_NORMAL 0
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500218#define STATE_ESCAPE 1
219
220static void
221terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500222
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500223static void
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500224terminal_schedule_redraw(struct terminal *terminal)
225{
226 if (!terminal->redraw_scheduled) {
227 g_idle_add(idle_redraw, terminal);
228 terminal->redraw_scheduled = 1;
229 } else {
230 terminal->redraw_pending = 1;
231 }
232}
233
234static void
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500235terminal_data(struct terminal *terminal, const char *data, size_t length);
236
237static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500238handle_escape(struct terminal *terminal)
239{
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500240 char *row, *p;
241 int i, count;
242 int args[10], set[10] = { 0, };
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500243
244 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500245 i = 0;
246 p = &terminal->escape[2];
247 while ((isdigit(*p) || *p == ';') && i < 10) {
248 if (*p == ';') {
249 p++;
250 i++;
251 } else {
252 args[i] = strtol(p, &p, 10);
253 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500254 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500255 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500256
257 switch (*p) {
258 case 'A':
259 count = set[0] ? args[0] : 1;
260 if (terminal->row - count >= 0)
261 terminal->row -= count;
262 else
263 terminal->row = 0;
264 break;
265 case 'B':
266 count = set[0] ? args[0] : 1;
267 if (terminal->row + count < terminal->height)
268 terminal->row += count;
269 else
270 terminal->row = terminal->height;
271 break;
272 case 'C':
273 count = set[0] ? args[0] : 1;
274 if (terminal->column + count < terminal->width)
275 terminal->column += count;
276 else
277 terminal->column = terminal->width;
278 break;
279 case 'D':
280 count = set[0] ? args[0] : 1;
281 if (terminal->column - count >= 0)
282 terminal->column -= count;
283 else
284 terminal->column = 0;
285 break;
286 case 'J':
287 row = terminal_get_row(terminal, terminal->row);
288 memset(&row[terminal->column], 0, terminal->width - terminal->column);
289 for (i = terminal->row + 1; i < terminal->height; i++)
290 memset(terminal_get_row(terminal, i), 0, terminal->width);
291 break;
292 case 'G':
293 if (set[0])
294 terminal->column = args[0] - 1;
295 break;
296 case 'H':
297 case 'f':
298 terminal->row = set[0] ? args[0] - 1 : 0;
299 terminal->column = set[1] ? args[1] - 1 : 0;
300 break;
301 case 'K':
302 row = terminal_get_row(terminal, terminal->row);
303 memset(&row[terminal->column], 0, terminal->width - terminal->column);
304 break;
305 case 'm':
306 /* color, blink, bold etc*/
307 break;
308 case '?':
309 if (strcmp(p, "?25l") == 0) {
310 /* hide cursor */
311 } else if (strcmp(p, "?25h") == 0) {
312 /* show cursor */
313 }
314 break;
315 default:
316 terminal_data(terminal,
317 terminal->escape + 1,
318 terminal->escape_length - 2);
319 break;
320 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500321}
322
323static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500324terminal_data(struct terminal *terminal, const char *data, size_t length)
325{
326 int i;
327 char *row;
328
329 for (i = 0; i < length; i++) {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500330 row = terminal_get_row(terminal, terminal->row);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500331
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500332 if (terminal->state == STATE_ESCAPE) {
333 terminal->escape[terminal->escape_length++] = data[i];
334 if (terminal->escape_length == 2 && data[i] != '[') {
335 /* Bad escape sequence. */
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500336 terminal->state = STATE_NORMAL;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500337 goto cancel_escape;
338 }
339
340 if (isalpha(data[i])) {
341 terminal->state = STATE_NORMAL;
342 handle_escape(terminal);
343 }
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500344 continue;
345 }
346
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500347 cancel_escape:
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500348 switch (data[i]) {
349 case '\r':
350 terminal->column = 0;
351 break;
352 case '\n':
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500353 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500354 if (terminal->row + 1 < terminal->height) {
355 terminal->row++;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500356 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500357 terminal->start++;
358 if (terminal->start == terminal->height)
359 terminal->start = 0;
360 memset(terminal_get_row(terminal, terminal->row),
361 0, terminal->width);
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500362 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500363
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500364 break;
365 case '\t':
366 memset(&row[terminal->column], ' ', -terminal->column & 7);
367 terminal->column = (terminal->column + 7) & ~7;
368 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500369 case '\e':
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500370 terminal->state = STATE_ESCAPE;
371 terminal->escape[0] = '\e';
372 terminal->escape_length = 1;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500373 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500374 case '\b':
375 if (terminal->column > 0)
376 terminal->column--;
377 break;
378 case '\a':
379 /* Bell */
380 break;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500381 default:
382 if (terminal->column < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500383 row[terminal->column++] = data[i] < 32 ? data[i] + 64 : data[i];
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500384 break;
385 }
386 }
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500387
388 terminal_schedule_redraw(terminal);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500389}
390
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500391static void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500392resize_handler(struct window *window, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500393{
394 struct terminal *terminal = data;
395
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500396 terminal_schedule_redraw(terminal);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500397}
398
399static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500400handle_acknowledge(void *data,
401 struct wl_compositor *compositor,
402 uint32_t key, uint32_t frame)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500403{
404 struct terminal *terminal = data;
405
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500406 terminal->redraw_scheduled = 0;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500407 if (key == 0)
408 buffer_destroy(terminal->buffer, terminal->fd);
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500409
410 if (terminal->redraw_pending) {
411 terminal->redraw_pending = 0;
412 terminal_schedule_redraw(terminal);
413 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500414}
415
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500416static void
417handle_frame(void *data,
418 struct wl_compositor *compositor,
419 uint32_t frame, uint32_t timestamp)
420{
421}
422
423static const struct wl_compositor_listener compositor_listener = {
424 handle_acknowledge,
425 handle_frame,
426};
427
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500428struct key {
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500429 int code[4];
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500430} evdev_keymap[] = {
431 { { 0, 0 } }, /* 0 */
432 { { 0x1b, 0x1b } },
433 { { '1', '!' } },
434 { { '2', '@' } },
435 { { '3', '#' } },
436 { { '4', '$' } },
437 { { '5', '%' } },
438 { { '6', '^' } },
439 { { '7', '&' } },
440 { { '8', '*' } },
441 { { '9', '(' } },
442 { { '0', ')' } },
443 { { '-', '_' } },
444 { { '=', '+' } },
445 { { '\b', '\b' } },
446 { { '\t', '\t' } },
447
Kristian Høgsberg33500892008-12-19 17:37:49 -0500448 { { 'q', 'Q', 0x11 } }, /* 16 */
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500449 { { 'w', 'W', 0x17 } },
450 { { 'e', 'E', 0x05 } },
451 { { 'r', 'R', 0x12 } },
Kristian Høgsberg33500892008-12-19 17:37:49 -0500452 { { 't', 'T', 0x14 } },
453 { { 'y', 'Y', 0x19 } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500454 { { 'u', 'U', 0x15 } },
Kristian Høgsberg33500892008-12-19 17:37:49 -0500455 { { 'i', 'I', 0x09 } },
456 { { 'o', 'O', 0x0f } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500457 { { 'p', 'P', 0x10 } },
Kristian Høgsberg33500892008-12-19 17:37:49 -0500458 { { '[', '{', 0x1b } },
459 { { ']', '}', 0x1d } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500460 { { '\n', '\n' } },
461 { { 0, 0 } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500462 { { 'a', 'A', 0x01} },
463 { { 's', 'S', 0x13 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500464
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500465 { { 'd', 'D', 0x04 } }, /* 32 */
466 { { 'f', 'F', 0x06 } },
Kristian Høgsberg33500892008-12-19 17:37:49 -0500467 { { 'g', 'G', 0x07 } },
468 { { 'h', 'H', 0x08 } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500469 { { 'j', 'J', 0x0a } },
470 { { 'k', 'K', 0x0b } },
471 { { 'l', 'L', 0x0c } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500472 { { ';', ':' } },
473 { { '\'', '"' } },
474 { { '`', '~' } },
475 { { 0, 0 } },
Kristian Høgsberg33500892008-12-19 17:37:49 -0500476 { { '\\', '|', 0x1c } },
477 { { 'z', 'Z', 0x1a } },
478 { { 'x', 'X', 0x18 } },
479 { { 'c', 'C', 0x03 } },
480 { { 'v', 'V', 0x16 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500481
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500482 { { 'b', 'B', 0x02 } }, /* 48 */
483 { { 'n', 'N', 0x0e } },
484 { { 'm', 'M', 0x0d } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500485 { { ',', '<' } },
486 { { '.', '>' } },
487 { { '/', '?' } },
488 { { 0, 0 } },
489 { { '*', '*' } },
490 { { 0, 0 } },
491 { { ' ', ' ' } },
492 { { 0, 0 } }
493
494 /* 59 */
495};
496
497#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
498
499static void
500key_handler(struct window *window, uint32_t key, uint32_t state, void *data)
501{
502 struct terminal *terminal = data;
503 uint32_t mod = 0;
504 char c;
505
506 switch (key) {
507 case KEY_LEFTSHIFT:
508 case KEY_RIGHTSHIFT:
509 mod = MOD_SHIFT;
510 break;
511 case KEY_LEFTCTRL:
512 case KEY_RIGHTCTRL:
513 mod = MOD_CTRL;
514 break;
515 case KEY_LEFTALT:
516 case KEY_RIGHTALT:
517 mod = MOD_ALT;
518 break;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500519 case KEY_F11:
520 if (!state)
521 break;
522 terminal->fullscreen ^= 1;
523 window_set_fullscreen(window, terminal->fullscreen);
524 terminal_schedule_redraw(terminal);
525 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500526 default:
527 if (key < ARRAY_LENGTH(evdev_keymap)) {
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500528 if (terminal->modifiers & MOD_CTRL)
529 c = evdev_keymap[key].code[2];
530 else if (terminal->modifiers & MOD_SHIFT)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500531 c = evdev_keymap[key].code[1];
532 else
533 c = evdev_keymap[key].code[0];
534 if (state && c)
535 write(terminal->master, &c, 1);
536 }
537 break;
538 }
539
540 if (state)
541 terminal->modifiers |= mod;
542 else
543 terminal->modifiers &= ~mod;
544}
545
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500546static struct terminal *
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500547terminal_create(struct wl_display *display, int fd, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500548{
549 struct terminal *terminal;
550
551 terminal = malloc(sizeof *terminal);
552 if (terminal == NULL)
553 return terminal;
554
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500555 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500556 terminal->fd = fd;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500557 terminal->fullscreen = fullscreen;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500558 terminal->window = window_create(display, fd, "Wayland Terminal",
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500559 500, 100, 500, 400);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500560 terminal->display = display;
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500561 terminal->redraw_scheduled = 1;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500562 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500563
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500564 terminal->compositor = wl_display_get_compositor(display);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500565 window_set_fullscreen(terminal->window, terminal->fullscreen);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500566 window_set_resize_handler(terminal->window, resize_handler, terminal);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500567
568 wl_compositor_add_listener(terminal->compositor,
569 &compositor_listener, terminal);
570
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500571 window_set_key_handler(terminal->window, key_handler, terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500572
Kristian Høgsberg22106762008-12-08 13:50:07 -0500573 terminal_draw(terminal);
574
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500575 return terminal;
576}
577
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500578static gboolean
579io_handler(GIOChannel *source,
580 GIOCondition condition,
581 gpointer data)
582{
583 struct terminal *terminal = data;
584 gchar buffer[256];
585 gsize bytes_read;
586 GError *error = NULL;
587
588 g_io_channel_read_chars(source, buffer, sizeof buffer,
589 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500590
591 terminal_data(terminal, buffer, bytes_read);
592
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500593 return TRUE;
594}
595
596static int
597terminal_run(struct terminal *terminal, const char *path)
598{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500599 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500600 pid_t pid;
601
602 pid = forkpty(&master, NULL, NULL, NULL);
603 if (pid == 0) {
604 close(master);
Kristian Høgsbergc8c5d582008-12-18 14:50:08 -0500605 setenv("TERM", "vt100", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500606 if (execl(path, path, NULL)) {
607 printf("exec failed: %m\n");
608 exit(EXIT_FAILURE);
609 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500610 } else if (pid < 0) {
611 fprintf(stderr, "failed to fork and create pty (%m).\n");
612 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500613 }
614
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500615 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500616 terminal->channel = g_io_channel_unix_new(master);
617 fcntl(master, F_SETFL, O_NONBLOCK);
618 g_io_add_watch(terminal->channel, G_IO_IN,
619 io_handler, terminal);
620
621 return 0;
622}
623
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500624static const GOptionEntry option_entries[] = {
625 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
626 &option_fullscreen, "Run in fullscreen mode" },
627 { NULL }
628};
629
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500630int main(int argc, char *argv[])
631{
632 struct wl_display *display;
633 int fd;
634 GMainLoop *loop;
635 GSource *source;
636 struct terminal *terminal;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500637 GOptionContext *context;
638 GError *error;
639
640 context = g_option_context_new(NULL);
641 g_option_context_add_main_entries(context, option_entries, "Wayland Terminal");
642 if (!g_option_context_parse(context, &argc, &argv, &error)) {
643 fprintf(stderr, "option parsing failed: %s\n", error->message);
644 exit(EXIT_FAILURE);
645 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500646
647 fd = open(gem_device, O_RDWR);
648 if (fd < 0) {
649 fprintf(stderr, "drm open failed: %m\n");
650 return -1;
651 }
652
653 display = wl_display_create(socket_name, sizeof socket_name);
654 if (display == NULL) {
655 fprintf(stderr, "failed to create display: %m\n");
656 return -1;
657 }
658
659 loop = g_main_loop_new(NULL, FALSE);
660 source = wl_glib_source_new(display);
661 g_source_attach(source, NULL);
662
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500663 terminal = terminal_create(display, fd, option_fullscreen);
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500664 if (terminal_run(terminal, "/bin/bash"))
665 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500666
667 g_main_loop_run(loop);
668
669 return 0;
670}