blob: 6bc6a51ae5397087a8177f45621ca9e046e2ea79 [file] [log] [blame]
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <fcntl.h>
28#include <unistd.h>
29#include <math.h>
30#include <time.h>
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050031#include <pty.h>
Kristian Høgsbergf04e8382008-12-08 00:07:49 -050032#include <ctype.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050033#include <cairo.h>
34#include <glib.h>
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050035#include <linux/input.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050036
37#include <GL/gl.h>
38#include <eagle.h>
39
40#include "wayland-client.h"
41#include "wayland-glib.h"
42
43#include "cairo-util.h"
44#include "window.h"
45
46static const char gem_device[] = "/dev/dri/card0";
47static const char socket_name[] = "\0wayland";
48
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050049#define MOD_SHIFT 0x01
50#define MOD_ALT 0x02
51#define MOD_CTRL 0x04
52
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050053struct terminal {
54 struct window *window;
55 struct wl_display *display;
56 int resize_scheduled;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050057 char *data;
Kristian Høgsbergb29415e2008-12-08 00:16:39 -050058 int width, height, tail, row, column, total_rows;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050059 int fd, master;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050060 struct buffer *buffer;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050061 GIOChannel *channel;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050062 uint32_t modifiers;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -050063 int state;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050064};
65
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050066static void
67terminal_draw_contents(struct terminal *terminal)
68{
69 struct rectangle rectangle;
70 cairo_surface_t *surface;
71 cairo_t *cr;
72 cairo_font_extents_t extents;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050073 int i, line;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050074
75 window_get_child_rectangle(terminal->window, &rectangle);
76
77 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
78 rectangle.width, rectangle.height);
79 cr = cairo_create(surface);
80 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
81 cairo_set_source_rgba(cr, 0, 0, 0, 0.9);
82 cairo_paint(cr);
83 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
84 cairo_set_source_rgba(cr, 0, 0.5, 0, 1);
85
86 cairo_select_font_face (cr, "mono",
87 CAIRO_FONT_SLANT_NORMAL,
88 CAIRO_FONT_WEIGHT_NORMAL);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050089 cairo_set_font_size(cr, 14);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050090
91 cairo_font_extents(cr, &extents);
92 for (i = 0; i < terminal->height; i++) {
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050093 line = (terminal->tail + i) % terminal->height;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050094 cairo_move_to(cr, 0, extents.ascent + extents.height * i);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050095 cairo_show_text(cr, &terminal->data[line * (terminal->width + 1)]);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050096 }
97 cairo_destroy(cr);
98
99 if (terminal->buffer != NULL)
100 buffer_destroy(terminal->buffer, terminal->fd);
101
102 terminal->buffer = buffer_create_from_cairo_surface(terminal->fd, surface);
103 cairo_surface_destroy(surface);
104
105 window_copy(terminal->window,
106 &rectangle,
107 terminal->buffer->name, terminal->buffer->stride);
108}
109
110static void
111terminal_draw(struct terminal *terminal)
112{
113 window_draw(terminal->window);
114 terminal_draw_contents(terminal);
115 wl_display_commit(terminal->display, 0);
116}
117
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500118static gboolean
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500119idle_redraw(void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500120{
121 struct terminal *terminal = data;
122
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500123 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500124
125 return FALSE;
126}
127
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500128#define STATE_NORMAL 0
129#define STATE_SKIP_TO_ALPHA 1
130
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500131static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500132terminal_data(struct terminal *terminal, const char *data, size_t length)
133{
134 int i;
135 char *row;
136
137 for (i = 0; i < length; i++) {
138 row = &terminal->data[terminal->row * (terminal->width + 1)];
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500139
140 if (terminal->state == STATE_SKIP_TO_ALPHA) {
141 if (isalpha(data[i]))
142 terminal->state = STATE_NORMAL;
143 continue;
144 }
145
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500146 switch (data[i]) {
147 case '\r':
148 terminal->column = 0;
149 break;
150 case '\n':
151 terminal->row++;
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500152 terminal->total_rows++;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500153 terminal->column = 0;
154 if (terminal->row == terminal->height)
155 terminal->row = 0;
Kristian Høgsbergb29415e2008-12-08 00:16:39 -0500156 if (terminal->row == terminal->tail && terminal->total_rows > 0) {
157 memset(&terminal->data[terminal->row * (terminal->width + 1)],
158 0, terminal->width);
159 terminal->tail++;
160 }
161 if (terminal->tail == terminal->height)
162 terminal->tail = 0;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500163 break;
164 case '\t':
165 memset(&row[terminal->column], ' ', -terminal->column & 7);
166 terminal->column = (terminal->column + 7) & ~7;
167 break;
Kristian Høgsbergf04e8382008-12-08 00:07:49 -0500168 case '\e':
169 terminal->state = STATE_SKIP_TO_ALPHA;
170 break;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500171 default:
172 if (terminal->column < terminal->width)
173 row[terminal->column++] = data[i];
174 break;
175 }
176 }
177}
178
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500179static void
180resize_handler(struct window *window, int32_t width, int32_t height, void *data)
181{
182 struct terminal *terminal = data;
183
184 if (!terminal->resize_scheduled) {
185 g_idle_add(idle_redraw, terminal);
186 terminal->resize_scheduled = 1;
187 }
188}
189
190static void
191acknowledge_handler(struct window *window, uint32_t key, void *data)
192{
193 struct terminal *terminal = data;
194
195 terminal->resize_scheduled = 0;
196}
197
198struct key {
199 int code[2];
200} evdev_keymap[] = {
201 { { 0, 0 } }, /* 0 */
202 { { 0x1b, 0x1b } },
203 { { '1', '!' } },
204 { { '2', '@' } },
205 { { '3', '#' } },
206 { { '4', '$' } },
207 { { '5', '%' } },
208 { { '6', '^' } },
209 { { '7', '&' } },
210 { { '8', '*' } },
211 { { '9', '(' } },
212 { { '0', ')' } },
213 { { '-', '_' } },
214 { { '=', '+' } },
215 { { '\b', '\b' } },
216 { { '\t', '\t' } },
217
218 { { 'q', 'Q' } }, /* 16 */
219 { { 'w', 'W' } },
220 { { 'e', 'E' } },
221 { { 'r', 'R' } },
222 { { 't', 'T' } },
223 { { 'y', 'Y' } },
224 { { 'u', 'U' } },
225 { { 'i', 'I' } },
226 { { 'o', 'O' } },
227 { { 'p', 'P' } },
228 { { '[', '{' } },
229 { { ']', '}' } },
230 { { '\n', '\n' } },
231 { { 0, 0 } },
232 { { 'a', 'A' } },
233 { { 's', 'S' } },
234
235 { { 'd', 'D' } }, /* 32 */
236 { { 'f', 'F' } },
237 { { 'g', 'G' } },
238 { { 'h', 'H' } },
239 { { 'j', 'J' } },
240 { { 'k', 'K' } },
241 { { 'l', 'L' } },
242 { { ';', ':' } },
243 { { '\'', '"' } },
244 { { '`', '~' } },
245 { { 0, 0 } },
246 { { '\\', '|' } },
247 { { 'z', 'Z' } },
248 { { 'x', 'X' } },
249 { { 'c', 'C' } },
250 { { 'v', 'V' } },
251
252 { { 'b', 'B' } }, /* 48 */
253 { { 'n', 'N' } },
254 { { 'm', 'M' } },
255 { { ',', '<' } },
256 { { '.', '>' } },
257 { { '/', '?' } },
258 { { 0, 0 } },
259 { { '*', '*' } },
260 { { 0, 0 } },
261 { { ' ', ' ' } },
262 { { 0, 0 } }
263
264 /* 59 */
265};
266
267#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
268
269static void
270key_handler(struct window *window, uint32_t key, uint32_t state, void *data)
271{
272 struct terminal *terminal = data;
273 uint32_t mod = 0;
274 char c;
275
276 switch (key) {
277 case KEY_LEFTSHIFT:
278 case KEY_RIGHTSHIFT:
279 mod = MOD_SHIFT;
280 break;
281 case KEY_LEFTCTRL:
282 case KEY_RIGHTCTRL:
283 mod = MOD_CTRL;
284 break;
285 case KEY_LEFTALT:
286 case KEY_RIGHTALT:
287 mod = MOD_ALT;
288 break;
289 default:
290 if (key < ARRAY_LENGTH(evdev_keymap)) {
291 if (terminal->modifiers & MOD_SHIFT)
292 c = evdev_keymap[key].code[1];
293 else
294 c = evdev_keymap[key].code[0];
295 if (state && c)
296 write(terminal->master, &c, 1);
297 }
298 break;
299 }
300
301 if (state)
302 terminal->modifiers |= mod;
303 else
304 terminal->modifiers &= ~mod;
305}
306
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500307static struct terminal *
308terminal_create(struct wl_display *display, int fd)
309{
310 struct terminal *terminal;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500311 int size;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500312
313 terminal = malloc(sizeof *terminal);
314 if (terminal == NULL)
315 return terminal;
316
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500317 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500318 terminal->fd = fd;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500319 terminal->window = window_create(display, fd, "Wayland Terminal",
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500320 500, 100, 500, 400);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500321 terminal->display = display;
322 terminal->resize_scheduled = 1;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500323 terminal->width = 80;
324 terminal->height = 25;
325 size = (terminal->width + 1) * terminal->height;
326 terminal->data = malloc(size);
327 memset(terminal->data, 0, size);
328
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500329 window_set_resize_handler(terminal->window, resize_handler, terminal);
330 window_set_acknowledge_handler(terminal->window, acknowledge_handler, terminal);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500331 window_set_key_handler(terminal->window, key_handler, terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500332
333 return terminal;
334}
335
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500336static gboolean
337io_handler(GIOChannel *source,
338 GIOCondition condition,
339 gpointer data)
340{
341 struct terminal *terminal = data;
342 gchar buffer[256];
343 gsize bytes_read;
344 GError *error = NULL;
345
346 g_io_channel_read_chars(source, buffer, sizeof buffer,
347 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500348
349 terminal_data(terminal, buffer, bytes_read);
350
351 if (!terminal->resize_scheduled) {
352 g_idle_add(idle_redraw, terminal);
353 terminal->resize_scheduled = 1;
354 }
355
356 return TRUE;
357}
358
359static int
360terminal_run(struct terminal *terminal, const char *path)
361{
362 int master, slave;
363 pid_t pid;
364
365 pid = forkpty(&master, NULL, NULL, NULL);
366 if (pid == 0) {
367 close(master);
368 if (execl(path, path, NULL)) {
369 printf("exec failed: %m\n");
370 exit(EXIT_FAILURE);
371 }
372 }
373
374 close(slave);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500375 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500376 terminal->channel = g_io_channel_unix_new(master);
377 fcntl(master, F_SETFL, O_NONBLOCK);
378 g_io_add_watch(terminal->channel, G_IO_IN,
379 io_handler, terminal);
380
381 return 0;
382}
383
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500384int main(int argc, char *argv[])
385{
386 struct wl_display *display;
387 int fd;
388 GMainLoop *loop;
389 GSource *source;
390 struct terminal *terminal;
391
392 fd = open(gem_device, O_RDWR);
393 if (fd < 0) {
394 fprintf(stderr, "drm open failed: %m\n");
395 return -1;
396 }
397
398 display = wl_display_create(socket_name, sizeof socket_name);
399 if (display == NULL) {
400 fprintf(stderr, "failed to create display: %m\n");
401 return -1;
402 }
403
404 loop = g_main_loop_new(NULL, FALSE);
405 source = wl_glib_source_new(display);
406 g_source_attach(source, NULL);
407
408 terminal = terminal_create(display, fd);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500409 terminal_run(terminal, "/bin/bash");
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500410 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500411
412 g_main_loop_run(loop);
413
414 return 0;
415}