blob: d380bd89f23eaefacabdc78dadb01fd47c66ab65 [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øgsberg0ac16f02009-01-15 11:37:43 -050036#include <cairo-drm.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050037
38#include <GL/gl.h>
39#include <eagle.h>
40
41#include "wayland-client.h"
42#include "wayland-glib.h"
43
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050044#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;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050056 struct 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øgsberg0ac16f02009-01-15 11:37:43 -050062 cairo_surface_t *surface;
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;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500131 cairo_t *cr;
132 cairo_font_extents_t extents;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500133 int i, top_margin, side_margin;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500134
135 window_get_child_rectangle(terminal->window, &rectangle);
136
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500137 terminal->surface =
138 window_create_surface(terminal->window, &rectangle);
139 cr = cairo_create(terminal->surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500140 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
141 cairo_set_source_rgba(cr, 0, 0, 0, 0.9);
142 cairo_paint(cr);
143 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
Kristian Høgsberg6e0a2f82008-12-08 14:06:56 -0500144 cairo_set_source_rgba(cr, 0, 0.7, 0, 1);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500145
146 cairo_select_font_face (cr, "mono",
147 CAIRO_FONT_SLANT_NORMAL,
148 CAIRO_FONT_WEIGHT_NORMAL);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500149 cairo_set_font_size(cr, 14);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500150
151 cairo_font_extents(cr, &extents);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500152 side_margin = (rectangle.width - terminal->width * extents.max_x_advance) / 2;
153 top_margin = (rectangle.height - terminal->height * extents.height) / 2;
154
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500155 for (i = 0; i < terminal->height; i++) {
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500156 cairo_move_to(cr, side_margin,
157 top_margin + extents.ascent + extents.height * i);
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500158 cairo_show_text(cr, terminal_get_row(terminal, i));
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500159 }
160 cairo_destroy(cr);
161
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500162 window_copy_surface(terminal->window,
163 &rectangle,
164 terminal->surface);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500165}
166
167static void
168terminal_draw(struct terminal *terminal)
169{
Kristian Høgsberg22106762008-12-08 13:50:07 -0500170 struct rectangle rectangle;
171 cairo_surface_t *surface;
172 cairo_font_extents_t extents;
173 cairo_t *cr;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500174 int32_t width, height;
Kristian Høgsberg22106762008-12-08 13:50:07 -0500175
176 window_get_child_rectangle(terminal->window, &rectangle);
177
178 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
179 cr = cairo_create(surface);
180 cairo_select_font_face (cr, "mono",
181 CAIRO_FONT_SLANT_NORMAL,
182 CAIRO_FONT_WEIGHT_NORMAL);
183 cairo_set_font_size(cr, 14);
184 cairo_font_extents(cr, &extents);
185 cairo_destroy(cr);
186 cairo_surface_destroy(surface);
187
188 width = (rectangle.width - 2 * terminal->margin) / (int32_t) extents.max_x_advance;
189 height = (rectangle.height - 2 * terminal->margin) / (int32_t) extents.height;
190 terminal_resize(terminal, width, height);
191
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500192 if (!terminal->fullscreen) {
193 rectangle.width = terminal->width * extents.max_x_advance + 2 * terminal->margin;
194 rectangle.height = terminal->height * extents.height + 2 * terminal->margin;
195 window_set_child_size(terminal->window, &rectangle);
196 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500197
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500198 window_draw(terminal->window);
199 terminal_draw_contents(terminal);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500200 wl_compositor_commit(terminal->compositor, 0);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500201}
202
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500203static gboolean
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500204idle_redraw(void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500205{
206 struct terminal *terminal = data;
207
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500208 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500209
210 return FALSE;
211}
212
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500213#define STATE_NORMAL 0
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500214#define STATE_ESCAPE 1
215
216static void
217terminal_data(struct terminal *terminal, const char *data, size_t length);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500218
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500219static void
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500220terminal_schedule_redraw(struct terminal *terminal)
221{
222 if (!terminal->redraw_scheduled) {
223 g_idle_add(idle_redraw, terminal);
224 terminal->redraw_scheduled = 1;
225 } else {
226 terminal->redraw_pending = 1;
227 }
228}
229
230static void
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500231terminal_data(struct terminal *terminal, const char *data, size_t length);
232
233static void
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500234handle_escape(struct terminal *terminal)
235{
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500236 char *row, *p;
237 int i, count;
238 int args[10], set[10] = { 0, };
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500239
240 terminal->escape[terminal->escape_length++] = '\0';
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500241 i = 0;
242 p = &terminal->escape[2];
243 while ((isdigit(*p) || *p == ';') && i < 10) {
244 if (*p == ';') {
245 p++;
246 i++;
247 } else {
248 args[i] = strtol(p, &p, 10);
249 set[i] = 1;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500250 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500251 }
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500252
253 switch (*p) {
254 case 'A':
255 count = set[0] ? args[0] : 1;
256 if (terminal->row - count >= 0)
257 terminal->row -= count;
258 else
259 terminal->row = 0;
260 break;
261 case 'B':
262 count = set[0] ? args[0] : 1;
263 if (terminal->row + count < terminal->height)
264 terminal->row += count;
265 else
266 terminal->row = terminal->height;
267 break;
268 case 'C':
269 count = set[0] ? args[0] : 1;
270 if (terminal->column + count < terminal->width)
271 terminal->column += count;
272 else
273 terminal->column = terminal->width;
274 break;
275 case 'D':
276 count = set[0] ? args[0] : 1;
277 if (terminal->column - count >= 0)
278 terminal->column -= count;
279 else
280 terminal->column = 0;
281 break;
282 case 'J':
283 row = terminal_get_row(terminal, terminal->row);
284 memset(&row[terminal->column], 0, terminal->width - terminal->column);
285 for (i = terminal->row + 1; i < terminal->height; i++)
286 memset(terminal_get_row(terminal, i), 0, terminal->width);
287 break;
288 case 'G':
289 if (set[0])
290 terminal->column = args[0] - 1;
291 break;
292 case 'H':
293 case 'f':
294 terminal->row = set[0] ? args[0] - 1 : 0;
295 terminal->column = set[1] ? args[1] - 1 : 0;
296 break;
297 case 'K':
298 row = terminal_get_row(terminal, terminal->row);
299 memset(&row[terminal->column], 0, terminal->width - terminal->column);
300 break;
301 case 'm':
302 /* color, blink, bold etc*/
303 break;
304 case '?':
305 if (strcmp(p, "?25l") == 0) {
306 /* hide cursor */
307 } else if (strcmp(p, "?25h") == 0) {
308 /* show cursor */
309 }
310 break;
311 default:
312 terminal_data(terminal,
313 terminal->escape + 1,
314 terminal->escape_length - 2);
315 break;
316 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500317}
318
319static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500320terminal_data(struct terminal *terminal, const char *data, size_t length)
321{
322 int i;
323 char *row;
324
325 for (i = 0; i < length; i++) {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500326 row = terminal_get_row(terminal, terminal->row);
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500327
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500328 if (terminal->state == STATE_ESCAPE) {
329 terminal->escape[terminal->escape_length++] = data[i];
330 if (terminal->escape_length == 2 && data[i] != '[') {
331 /* Bad escape sequence. */
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500332 terminal->state = STATE_NORMAL;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500333 goto cancel_escape;
334 }
335
336 if (isalpha(data[i])) {
337 terminal->state = STATE_NORMAL;
338 handle_escape(terminal);
339 }
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500340 continue;
341 }
342
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500343 cancel_escape:
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500344 switch (data[i]) {
345 case '\r':
346 terminal->column = 0;
347 break;
348 case '\n':
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500349 terminal->column = 0;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500350 if (terminal->row + 1 < terminal->height) {
351 terminal->row++;
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500352 } else {
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500353 terminal->start++;
354 if (terminal->start == terminal->height)
355 terminal->start = 0;
356 memset(terminal_get_row(terminal, terminal->row),
357 0, terminal->width);
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500358 }
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500359
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500360 break;
361 case '\t':
362 memset(&row[terminal->column], ' ', -terminal->column & 7);
363 terminal->column = (terminal->column + 7) & ~7;
364 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500365 case '\e':
Kristian Høgsberg17809b12008-12-08 12:20:40 -0500366 terminal->state = STATE_ESCAPE;
367 terminal->escape[0] = '\e';
368 terminal->escape_length = 1;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500369 break;
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500370 case '\b':
371 if (terminal->column > 0)
372 terminal->column--;
373 break;
374 case '\a':
375 /* Bell */
376 break;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500377 default:
378 if (terminal->column < terminal->width)
Kristian Høgsbergdbd54642008-12-08 22:22:25 -0500379 row[terminal->column++] = data[i] < 32 ? data[i] + 64 : data[i];
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500380 break;
381 }
382 }
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500383
384 terminal_schedule_redraw(terminal);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500385}
386
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500387static void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500388resize_handler(struct window *window, void *data)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500389{
390 struct terminal *terminal = data;
391
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500392 terminal_schedule_redraw(terminal);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500393}
394
395static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500396handle_acknowledge(void *data,
397 struct wl_compositor *compositor,
398 uint32_t key, uint32_t frame)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500399{
400 struct terminal *terminal = data;
401
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500402 terminal->redraw_scheduled = 0;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500403 if (key == 0)
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -0500404 cairo_surface_destroy(terminal->surface);
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500405
406 if (terminal->redraw_pending) {
407 terminal->redraw_pending = 0;
408 terminal_schedule_redraw(terminal);
409 }
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500410}
411
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500412static void
413handle_frame(void *data,
414 struct wl_compositor *compositor,
415 uint32_t frame, uint32_t timestamp)
416{
417}
418
419static const struct wl_compositor_listener compositor_listener = {
420 handle_acknowledge,
421 handle_frame,
422};
423
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500424struct key {
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500425 int code[4];
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500426} evdev_keymap[] = {
427 { { 0, 0 } }, /* 0 */
428 { { 0x1b, 0x1b } },
429 { { '1', '!' } },
430 { { '2', '@' } },
431 { { '3', '#' } },
432 { { '4', '$' } },
433 { { '5', '%' } },
434 { { '6', '^' } },
435 { { '7', '&' } },
436 { { '8', '*' } },
437 { { '9', '(' } },
438 { { '0', ')' } },
439 { { '-', '_' } },
440 { { '=', '+' } },
441 { { '\b', '\b' } },
442 { { '\t', '\t' } },
443
Kristian Høgsberg33500892008-12-19 17:37:49 -0500444 { { 'q', 'Q', 0x11 } }, /* 16 */
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500445 { { 'w', 'W', 0x17 } },
446 { { 'e', 'E', 0x05 } },
447 { { 'r', 'R', 0x12 } },
Kristian Høgsberg33500892008-12-19 17:37:49 -0500448 { { 't', 'T', 0x14 } },
449 { { 'y', 'Y', 0x19 } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500450 { { 'u', 'U', 0x15 } },
Kristian Høgsberg33500892008-12-19 17:37:49 -0500451 { { 'i', 'I', 0x09 } },
452 { { 'o', 'O', 0x0f } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500453 { { 'p', 'P', 0x10 } },
Kristian Høgsberg33500892008-12-19 17:37:49 -0500454 { { '[', '{', 0x1b } },
455 { { ']', '}', 0x1d } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500456 { { '\n', '\n' } },
457 { { 0, 0 } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500458 { { 'a', 'A', 0x01} },
459 { { 's', 'S', 0x13 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500460
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500461 { { 'd', 'D', 0x04 } }, /* 32 */
462 { { 'f', 'F', 0x06 } },
Kristian Høgsberg33500892008-12-19 17:37:49 -0500463 { { 'g', 'G', 0x07 } },
464 { { 'h', 'H', 0x08 } },
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500465 { { 'j', 'J', 0x0a } },
466 { { 'k', 'K', 0x0b } },
467 { { 'l', 'L', 0x0c } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500468 { { ';', ':' } },
469 { { '\'', '"' } },
470 { { '`', '~' } },
471 { { 0, 0 } },
Kristian Høgsberg33500892008-12-19 17:37:49 -0500472 { { '\\', '|', 0x1c } },
473 { { 'z', 'Z', 0x1a } },
474 { { 'x', 'X', 0x18 } },
475 { { 'c', 'C', 0x03 } },
476 { { 'v', 'V', 0x16 } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500477
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500478 { { 'b', 'B', 0x02 } }, /* 48 */
479 { { 'n', 'N', 0x0e } },
480 { { 'm', 'M', 0x0d } },
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500481 { { ',', '<' } },
482 { { '.', '>' } },
483 { { '/', '?' } },
484 { { 0, 0 } },
485 { { '*', '*' } },
486 { { 0, 0 } },
487 { { ' ', ' ' } },
488 { { 0, 0 } }
489
490 /* 59 */
491};
492
493#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
494
495static void
496key_handler(struct window *window, uint32_t key, uint32_t state, void *data)
497{
498 struct terminal *terminal = data;
499 uint32_t mod = 0;
500 char c;
501
502 switch (key) {
503 case KEY_LEFTSHIFT:
504 case KEY_RIGHTSHIFT:
505 mod = MOD_SHIFT;
506 break;
507 case KEY_LEFTCTRL:
508 case KEY_RIGHTCTRL:
509 mod = MOD_CTRL;
510 break;
511 case KEY_LEFTALT:
512 case KEY_RIGHTALT:
513 mod = MOD_ALT;
514 break;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500515 case KEY_F11:
516 if (!state)
517 break;
518 terminal->fullscreen ^= 1;
519 window_set_fullscreen(window, terminal->fullscreen);
520 terminal_schedule_redraw(terminal);
521 break;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500522 default:
523 if (key < ARRAY_LENGTH(evdev_keymap)) {
Kristian Høgsberg0d77fd42008-12-08 00:23:55 -0500524 if (terminal->modifiers & MOD_CTRL)
525 c = evdev_keymap[key].code[2];
526 else if (terminal->modifiers & MOD_SHIFT)
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500527 c = evdev_keymap[key].code[1];
528 else
529 c = evdev_keymap[key].code[0];
530 if (state && c)
531 write(terminal->master, &c, 1);
532 }
533 break;
534 }
535
536 if (state)
537 terminal->modifiers |= mod;
538 else
539 terminal->modifiers &= ~mod;
540}
541
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500542static struct terminal *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500543terminal_create(struct display *display, int fullscreen)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500544{
545 struct terminal *terminal;
546
547 terminal = malloc(sizeof *terminal);
548 if (terminal == NULL)
549 return terminal;
550
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500551 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500552 terminal->fullscreen = fullscreen;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500553 terminal->window = window_create(display, "Wayland Terminal",
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500554 500, 100, 500, 400);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500555 terminal->display = display;
Kristian Høgsberg721f09f2008-12-08 11:13:26 -0500556 terminal->redraw_scheduled = 1;
Kristian Høgsberg1584c572008-12-08 12:59:37 -0500557 terminal->margin = 5;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500558
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500559 terminal->compositor = display_get_compositor(display);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500560 window_set_fullscreen(terminal->window, terminal->fullscreen);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500561 window_set_resize_handler(terminal->window, resize_handler, terminal);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500562
563 wl_compositor_add_listener(terminal->compositor,
564 &compositor_listener, terminal);
565
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500566 window_set_key_handler(terminal->window, key_handler, terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500567
Kristian Høgsberg22106762008-12-08 13:50:07 -0500568 terminal_draw(terminal);
569
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500570 return terminal;
571}
572
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500573static gboolean
574io_handler(GIOChannel *source,
575 GIOCondition condition,
576 gpointer data)
577{
578 struct terminal *terminal = data;
579 gchar buffer[256];
580 gsize bytes_read;
581 GError *error = NULL;
582
583 g_io_channel_read_chars(source, buffer, sizeof buffer,
584 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500585
586 terminal_data(terminal, buffer, bytes_read);
587
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500588 return TRUE;
589}
590
591static int
592terminal_run(struct terminal *terminal, const char *path)
593{
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500594 int master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500595 pid_t pid;
596
597 pid = forkpty(&master, NULL, NULL, NULL);
598 if (pid == 0) {
599 close(master);
Kristian Høgsbergc8c5d582008-12-18 14:50:08 -0500600 setenv("TERM", "vt100", 1);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500601 if (execl(path, path, NULL)) {
602 printf("exec failed: %m\n");
603 exit(EXIT_FAILURE);
604 }
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500605 } else if (pid < 0) {
606 fprintf(stderr, "failed to fork and create pty (%m).\n");
607 return -1;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500608 }
609
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500610 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500611 terminal->channel = g_io_channel_unix_new(master);
612 fcntl(master, F_SETFL, O_NONBLOCK);
613 g_io_add_watch(terminal->channel, G_IO_IN,
614 io_handler, terminal);
615
616 return 0;
617}
618
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500619static const GOptionEntry option_entries[] = {
620 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
621 &option_fullscreen, "Run in fullscreen mode" },
622 { NULL }
623};
624
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500625int main(int argc, char *argv[])
626{
627 struct wl_display *display;
628 int fd;
629 GMainLoop *loop;
630 GSource *source;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500631 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500632 struct terminal *terminal;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500633 GOptionContext *context;
634 GError *error;
635
636 context = g_option_context_new(NULL);
637 g_option_context_add_main_entries(context, option_entries, "Wayland Terminal");
638 if (!g_option_context_parse(context, &argc, &argv, &error)) {
639 fprintf(stderr, "option parsing failed: %s\n", error->message);
640 exit(EXIT_FAILURE);
641 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500642
643 fd = open(gem_device, O_RDWR);
644 if (fd < 0) {
645 fprintf(stderr, "drm open failed: %m\n");
646 return -1;
647 }
648
649 display = wl_display_create(socket_name, sizeof socket_name);
650 if (display == NULL) {
651 fprintf(stderr, "failed to create display: %m\n");
652 return -1;
653 }
654
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500655 d = display_create(display, fd);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500656 loop = g_main_loop_new(NULL, FALSE);
657 source = wl_glib_source_new(display);
658 g_source_attach(source, NULL);
659
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500660 terminal = terminal_create(d, option_fullscreen);
Kristian Høgsbergf0c7b202008-12-12 13:39:03 -0500661 if (terminal_run(terminal, "/bin/bash"))
662 exit(EXIT_FAILURE);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500663
664 g_main_loop_run(loop);
665
666 return 0;
667}