blob: f0cf76d9c84d5fc730eaa8ef75e345fcd1ac9444 [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øgsberg0c4457f2008-12-07 19:59:11 -050032#include <cairo.h>
33#include <glib.h>
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050034#include <linux/input.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050035
36#include <GL/gl.h>
37#include <eagle.h>
38
39#include "wayland-client.h"
40#include "wayland-glib.h"
41
42#include "cairo-util.h"
43#include "window.h"
44
45static const char gem_device[] = "/dev/dri/card0";
46static const char socket_name[] = "\0wayland";
47
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050048#define MOD_SHIFT 0x01
49#define MOD_ALT 0x02
50#define MOD_CTRL 0x04
51
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050052struct terminal {
53 struct window *window;
54 struct wl_display *display;
55 int resize_scheduled;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050056 char *data;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050057 int width, height, tail, row, column;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050058 int fd, master;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050059 struct buffer *buffer;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050060 GIOChannel *channel;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050061 uint32_t modifiers;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050062};
63
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050064static void
65terminal_draw_contents(struct terminal *terminal)
66{
67 struct rectangle rectangle;
68 cairo_surface_t *surface;
69 cairo_t *cr;
70 cairo_font_extents_t extents;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050071 int i, line;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050072
73 window_get_child_rectangle(terminal->window, &rectangle);
74
75 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
76 rectangle.width, rectangle.height);
77 cr = cairo_create(surface);
78 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
79 cairo_set_source_rgba(cr, 0, 0, 0, 0.9);
80 cairo_paint(cr);
81 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
82 cairo_set_source_rgba(cr, 0, 0.5, 0, 1);
83
84 cairo_select_font_face (cr, "mono",
85 CAIRO_FONT_SLANT_NORMAL,
86 CAIRO_FONT_WEIGHT_NORMAL);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050087 cairo_set_font_size(cr, 14);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050088
89 cairo_font_extents(cr, &extents);
90 for (i = 0; i < terminal->height; i++) {
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050091 line = (terminal->tail + i) % terminal->height;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050092 cairo_move_to(cr, 0, extents.ascent + extents.height * i);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050093 cairo_show_text(cr, &terminal->data[line * (terminal->width + 1)]);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050094 }
95 cairo_destroy(cr);
96
97 if (terminal->buffer != NULL)
98 buffer_destroy(terminal->buffer, terminal->fd);
99
100 terminal->buffer = buffer_create_from_cairo_surface(terminal->fd, surface);
101 cairo_surface_destroy(surface);
102
103 window_copy(terminal->window,
104 &rectangle,
105 terminal->buffer->name, terminal->buffer->stride);
106}
107
108static void
109terminal_draw(struct terminal *terminal)
110{
111 window_draw(terminal->window);
112 terminal_draw_contents(terminal);
113 wl_display_commit(terminal->display, 0);
114}
115
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500116static gboolean
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500117idle_redraw(void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500118{
119 struct terminal *terminal = data;
120
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500121 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500122
123 return FALSE;
124}
125
126static void
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500127terminal_data(struct terminal *terminal, const char *data, size_t length)
128{
129 int i;
130 char *row;
131
132 for (i = 0; i < length; i++) {
133 row = &terminal->data[terminal->row * (terminal->width + 1)];
134 switch (data[i]) {
135 case '\r':
136 terminal->column = 0;
137 break;
138 case '\n':
139 terminal->row++;
140 terminal->column = 0;
141 if (terminal->row == terminal->height)
142 terminal->row = 0;
143 break;
144 case '\t':
145 memset(&row[terminal->column], ' ', -terminal->column & 7);
146 terminal->column = (terminal->column + 7) & ~7;
147 break;
148 default:
149 if (terminal->column < terminal->width)
150 row[terminal->column++] = data[i];
151 break;
152 }
153 }
154}
155
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500156static void
157resize_handler(struct window *window, int32_t width, int32_t height, void *data)
158{
159 struct terminal *terminal = data;
160
161 if (!terminal->resize_scheduled) {
162 g_idle_add(idle_redraw, terminal);
163 terminal->resize_scheduled = 1;
164 }
165}
166
167static void
168acknowledge_handler(struct window *window, uint32_t key, void *data)
169{
170 struct terminal *terminal = data;
171
172 terminal->resize_scheduled = 0;
173}
174
175struct key {
176 int code[2];
177} evdev_keymap[] = {
178 { { 0, 0 } }, /* 0 */
179 { { 0x1b, 0x1b } },
180 { { '1', '!' } },
181 { { '2', '@' } },
182 { { '3', '#' } },
183 { { '4', '$' } },
184 { { '5', '%' } },
185 { { '6', '^' } },
186 { { '7', '&' } },
187 { { '8', '*' } },
188 { { '9', '(' } },
189 { { '0', ')' } },
190 { { '-', '_' } },
191 { { '=', '+' } },
192 { { '\b', '\b' } },
193 { { '\t', '\t' } },
194
195 { { 'q', 'Q' } }, /* 16 */
196 { { 'w', 'W' } },
197 { { 'e', 'E' } },
198 { { 'r', 'R' } },
199 { { 't', 'T' } },
200 { { 'y', 'Y' } },
201 { { 'u', 'U' } },
202 { { 'i', 'I' } },
203 { { 'o', 'O' } },
204 { { 'p', 'P' } },
205 { { '[', '{' } },
206 { { ']', '}' } },
207 { { '\n', '\n' } },
208 { { 0, 0 } },
209 { { 'a', 'A' } },
210 { { 's', 'S' } },
211
212 { { 'd', 'D' } }, /* 32 */
213 { { 'f', 'F' } },
214 { { 'g', 'G' } },
215 { { 'h', 'H' } },
216 { { 'j', 'J' } },
217 { { 'k', 'K' } },
218 { { 'l', 'L' } },
219 { { ';', ':' } },
220 { { '\'', '"' } },
221 { { '`', '~' } },
222 { { 0, 0 } },
223 { { '\\', '|' } },
224 { { 'z', 'Z' } },
225 { { 'x', 'X' } },
226 { { 'c', 'C' } },
227 { { 'v', 'V' } },
228
229 { { 'b', 'B' } }, /* 48 */
230 { { 'n', 'N' } },
231 { { 'm', 'M' } },
232 { { ',', '<' } },
233 { { '.', '>' } },
234 { { '/', '?' } },
235 { { 0, 0 } },
236 { { '*', '*' } },
237 { { 0, 0 } },
238 { { ' ', ' ' } },
239 { { 0, 0 } }
240
241 /* 59 */
242};
243
244#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
245
246static void
247key_handler(struct window *window, uint32_t key, uint32_t state, void *data)
248{
249 struct terminal *terminal = data;
250 uint32_t mod = 0;
251 char c;
252
253 switch (key) {
254 case KEY_LEFTSHIFT:
255 case KEY_RIGHTSHIFT:
256 mod = MOD_SHIFT;
257 break;
258 case KEY_LEFTCTRL:
259 case KEY_RIGHTCTRL:
260 mod = MOD_CTRL;
261 break;
262 case KEY_LEFTALT:
263 case KEY_RIGHTALT:
264 mod = MOD_ALT;
265 break;
266 default:
267 if (key < ARRAY_LENGTH(evdev_keymap)) {
268 if (terminal->modifiers & MOD_SHIFT)
269 c = evdev_keymap[key].code[1];
270 else
271 c = evdev_keymap[key].code[0];
272 if (state && c)
273 write(terminal->master, &c, 1);
274 }
275 break;
276 }
277
278 if (state)
279 terminal->modifiers |= mod;
280 else
281 terminal->modifiers &= ~mod;
282}
283
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500284static struct terminal *
285terminal_create(struct wl_display *display, int fd)
286{
287 struct terminal *terminal;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500288 int size;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500289
290 terminal = malloc(sizeof *terminal);
291 if (terminal == NULL)
292 return terminal;
293
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500294 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500295 terminal->fd = fd;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500296 terminal->window = window_create(display, fd, "Wayland Terminal",
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500297 500, 100, 500, 400);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500298 terminal->display = display;
299 terminal->resize_scheduled = 1;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500300 terminal->width = 80;
301 terminal->height = 25;
302 size = (terminal->width + 1) * terminal->height;
303 terminal->data = malloc(size);
304 memset(terminal->data, 0, size);
305
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500306 window_set_resize_handler(terminal->window, resize_handler, terminal);
307 window_set_acknowledge_handler(terminal->window, acknowledge_handler, terminal);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500308 window_set_key_handler(terminal->window, key_handler, terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500309
310 return terminal;
311}
312
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500313static gboolean
314io_handler(GIOChannel *source,
315 GIOCondition condition,
316 gpointer data)
317{
318 struct terminal *terminal = data;
319 gchar buffer[256];
320 gsize bytes_read;
321 GError *error = NULL;
322
323 g_io_channel_read_chars(source, buffer, sizeof buffer,
324 &bytes_read, &error);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500325
326 terminal_data(terminal, buffer, bytes_read);
327
328 if (!terminal->resize_scheduled) {
329 g_idle_add(idle_redraw, terminal);
330 terminal->resize_scheduled = 1;
331 }
332
333 return TRUE;
334}
335
336static int
337terminal_run(struct terminal *terminal, const char *path)
338{
339 int master, slave;
340 pid_t pid;
341
342 pid = forkpty(&master, NULL, NULL, NULL);
343 if (pid == 0) {
344 close(master);
345 if (execl(path, path, NULL)) {
346 printf("exec failed: %m\n");
347 exit(EXIT_FAILURE);
348 }
349 }
350
351 close(slave);
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500352 terminal->master = master;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500353 terminal->channel = g_io_channel_unix_new(master);
354 fcntl(master, F_SETFL, O_NONBLOCK);
355 g_io_add_watch(terminal->channel, G_IO_IN,
356 io_handler, terminal);
357
358 return 0;
359}
360
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500361int main(int argc, char *argv[])
362{
363 struct wl_display *display;
364 int fd;
365 GMainLoop *loop;
366 GSource *source;
367 struct terminal *terminal;
368
369 fd = open(gem_device, O_RDWR);
370 if (fd < 0) {
371 fprintf(stderr, "drm open failed: %m\n");
372 return -1;
373 }
374
375 display = wl_display_create(socket_name, sizeof socket_name);
376 if (display == NULL) {
377 fprintf(stderr, "failed to create display: %m\n");
378 return -1;
379 }
380
381 loop = g_main_loop_new(NULL, FALSE);
382 source = wl_glib_source_new(display);
383 g_source_attach(source, NULL);
384
385 terminal = terminal_create(display, fd);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500386 terminal_run(terminal, "/bin/bash");
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500387 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500388
389 g_main_loop_run(loop);
390
391 return 0;
392}