2 * Copyright © 2008 Kristian Høgsberg
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.
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
35 #include <linux/input.h>
36 #include <cairo-drm.h>
41 #include "wayland-client.h"
42 #include "wayland-glib.h"
46 static int option_fullscreen;
47 static const char gem_device[] = "/dev/dri/card0";
48 static const char socket_name[] = "\0wayland";
50 #define MOD_SHIFT 0x01
55 struct window *window;
56 struct wl_display *display;
57 struct wl_compositor *compositor;
58 int redraw_scheduled, redraw_pending;
60 int width, height, start, row, column;
62 cairo_surface_t *surface;
73 terminal_get_row(struct terminal *terminal, int row)
77 index = (row + terminal->start) % terminal->height;
79 return &terminal->data[index * (terminal->width + 1)];
83 terminal_resize(struct terminal *terminal, int width, int height)
87 int i, l, total_rows, start;
89 if (terminal->width == width && terminal->height == height)
92 size = (width + 1) * height;
94 memset(data, 0, size);
96 if (width > terminal->width)
101 if (terminal->height > height) {
103 start = terminal->height - height;
105 total_rows = terminal->height;
109 for (i = 0; i < total_rows; i++)
110 memcpy(data + (width + 1) * i,
111 terminal_get_row(terminal, i), l);
113 free(terminal->data);
116 terminal->width = width;
117 terminal->height = height;
118 terminal->data = data;
120 if (terminal->row >= terminal->height)
121 terminal->row = terminal->height - 1;
122 if (terminal->column >= terminal->width)
123 terminal->column = terminal->width - 1;
128 terminal_draw_contents(struct terminal *terminal)
130 struct rectangle rectangle;
132 cairo_font_extents_t extents;
133 int i, top_margin, side_margin;
135 window_get_child_rectangle(terminal->window, &rectangle);
138 window_create_surface(terminal->window, &rectangle);
139 cr = cairo_create(terminal->surface);
140 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
141 cairo_set_source_rgba(cr, 0, 0, 0, 0.9);
143 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
144 cairo_set_source_rgba(cr, 0, 0.7, 0, 1);
146 cairo_select_font_face (cr, "mono",
147 CAIRO_FONT_SLANT_NORMAL,
148 CAIRO_FONT_WEIGHT_NORMAL);
149 cairo_set_font_size(cr, 14);
151 cairo_font_extents(cr, &extents);
152 side_margin = (rectangle.width - terminal->width * extents.max_x_advance) / 2;
153 top_margin = (rectangle.height - terminal->height * extents.height) / 2;
155 for (i = 0; i < terminal->height; i++) {
156 cairo_move_to(cr, side_margin,
157 top_margin + extents.ascent + extents.height * i);
158 cairo_show_text(cr, terminal_get_row(terminal, i));
162 window_copy_surface(terminal->window,
168 terminal_draw(struct terminal *terminal)
170 struct rectangle rectangle;
171 cairo_surface_t *surface;
172 cairo_font_extents_t extents;
174 int32_t width, height;
176 window_get_child_rectangle(terminal->window, &rectangle);
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);
186 cairo_surface_destroy(surface);
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);
192 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);
198 window_draw(terminal->window);
199 terminal_draw_contents(terminal);
200 wl_compositor_commit(terminal->compositor, 0);
204 idle_redraw(void *data)
206 struct terminal *terminal = data;
208 terminal_draw(terminal);
213 #define STATE_NORMAL 0
214 #define STATE_ESCAPE 1
217 terminal_data(struct terminal *terminal, const char *data, size_t length);
220 terminal_schedule_redraw(struct terminal *terminal)
222 if (!terminal->redraw_scheduled) {
223 g_idle_add(idle_redraw, terminal);
224 terminal->redraw_scheduled = 1;
226 terminal->redraw_pending = 1;
231 terminal_data(struct terminal *terminal, const char *data, size_t length);
234 handle_escape(struct terminal *terminal)
238 int args[10], set[10] = { 0, };
240 terminal->escape[terminal->escape_length++] = '\0';
242 p = &terminal->escape[2];
243 while ((isdigit(*p) || *p == ';') && i < 10) {
248 args[i] = strtol(p, &p, 10);
255 count = set[0] ? args[0] : 1;
256 if (terminal->row - count >= 0)
257 terminal->row -= count;
262 count = set[0] ? args[0] : 1;
263 if (terminal->row + count < terminal->height)
264 terminal->row += count;
266 terminal->row = terminal->height;
269 count = set[0] ? args[0] : 1;
270 if (terminal->column + count < terminal->width)
271 terminal->column += count;
273 terminal->column = terminal->width;
276 count = set[0] ? args[0] : 1;
277 if (terminal->column - count >= 0)
278 terminal->column -= count;
280 terminal->column = 0;
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);
290 terminal->column = args[0] - 1;
294 terminal->row = set[0] ? args[0] - 1 : 0;
295 terminal->column = set[1] ? args[1] - 1 : 0;
298 row = terminal_get_row(terminal, terminal->row);
299 memset(&row[terminal->column], 0, terminal->width - terminal->column);
302 /* color, blink, bold etc*/
305 if (strcmp(p, "?25l") == 0) {
307 } else if (strcmp(p, "?25h") == 0) {
312 terminal_data(terminal,
313 terminal->escape + 1,
314 terminal->escape_length - 2);
320 terminal_data(struct terminal *terminal, const char *data, size_t length)
325 for (i = 0; i < length; i++) {
326 row = terminal_get_row(terminal, terminal->row);
328 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. */
332 terminal->state = STATE_NORMAL;
336 if (isalpha(data[i])) {
337 terminal->state = STATE_NORMAL;
338 handle_escape(terminal);
346 terminal->column = 0;
349 terminal->column = 0;
350 if (terminal->row + 1 < terminal->height) {
354 if (terminal->start == terminal->height)
356 memset(terminal_get_row(terminal, terminal->row),
362 memset(&row[terminal->column], ' ', -terminal->column & 7);
363 terminal->column = (terminal->column + 7) & ~7;
366 terminal->state = STATE_ESCAPE;
367 terminal->escape[0] = '\e';
368 terminal->escape_length = 1;
371 if (terminal->column > 0)
378 if (terminal->column < terminal->width)
379 row[terminal->column++] = data[i] < 32 ? data[i] + 64 : data[i];
384 terminal_schedule_redraw(terminal);
388 resize_handler(struct window *window, void *data)
390 struct terminal *terminal = data;
392 terminal_schedule_redraw(terminal);
396 handle_acknowledge(void *data,
397 struct wl_compositor *compositor,
398 uint32_t key, uint32_t frame)
400 struct terminal *terminal = data;
402 terminal->redraw_scheduled = 0;
404 cairo_surface_destroy(terminal->surface);
406 if (terminal->redraw_pending) {
407 terminal->redraw_pending = 0;
408 terminal_schedule_redraw(terminal);
413 handle_frame(void *data,
414 struct wl_compositor *compositor,
415 uint32_t frame, uint32_t timestamp)
419 static const struct wl_compositor_listener compositor_listener = {
427 { { 0, 0 } }, /* 0 */
444 { { 'q', 'Q', 0x11 } }, /* 16 */
445 { { 'w', 'W', 0x17 } },
446 { { 'e', 'E', 0x05 } },
447 { { 'r', 'R', 0x12 } },
448 { { 't', 'T', 0x14 } },
449 { { 'y', 'Y', 0x19 } },
450 { { 'u', 'U', 0x15 } },
451 { { 'i', 'I', 0x09 } },
452 { { 'o', 'O', 0x0f } },
453 { { 'p', 'P', 0x10 } },
454 { { '[', '{', 0x1b } },
455 { { ']', '}', 0x1d } },
458 { { 'a', 'A', 0x01} },
459 { { 's', 'S', 0x13 } },
461 { { 'd', 'D', 0x04 } }, /* 32 */
462 { { 'f', 'F', 0x06 } },
463 { { 'g', 'G', 0x07 } },
464 { { 'h', 'H', 0x08 } },
465 { { 'j', 'J', 0x0a } },
466 { { 'k', 'K', 0x0b } },
467 { { 'l', 'L', 0x0c } },
472 { { '\\', '|', 0x1c } },
473 { { 'z', 'Z', 0x1a } },
474 { { 'x', 'X', 0x18 } },
475 { { 'c', 'C', 0x03 } },
476 { { 'v', 'V', 0x16 } },
478 { { 'b', 'B', 0x02 } }, /* 48 */
479 { { 'n', 'N', 0x0e } },
480 { { 'm', 'M', 0x0d } },
493 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
496 key_handler(struct window *window, uint32_t key, uint32_t state, void *data)
498 struct terminal *terminal = data;
518 terminal->fullscreen ^= 1;
519 window_set_fullscreen(window, terminal->fullscreen);
520 terminal_schedule_redraw(terminal);
523 if (key < ARRAY_LENGTH(evdev_keymap)) {
524 if (terminal->modifiers & MOD_CTRL)
525 c = evdev_keymap[key].code[2];
526 else if (terminal->modifiers & MOD_SHIFT)
527 c = evdev_keymap[key].code[1];
529 c = evdev_keymap[key].code[0];
531 write(terminal->master, &c, 1);
537 terminal->modifiers |= mod;
539 terminal->modifiers &= ~mod;
542 static struct terminal *
543 terminal_create(struct wl_display *display, int fd, int fullscreen)
545 struct terminal *terminal;
547 terminal = malloc(sizeof *terminal);
548 if (terminal == NULL)
551 memset(terminal, 0, sizeof *terminal);
552 terminal->fullscreen = fullscreen;
553 terminal->window = window_create(display, fd, "Wayland Terminal",
555 terminal->display = display;
556 terminal->redraw_scheduled = 1;
557 terminal->margin = 5;
559 terminal->compositor = wl_display_get_compositor(display);
560 window_set_fullscreen(terminal->window, terminal->fullscreen);
561 window_set_resize_handler(terminal->window, resize_handler, terminal);
563 wl_compositor_add_listener(terminal->compositor,
564 &compositor_listener, terminal);
566 window_set_key_handler(terminal->window, key_handler, terminal);
568 terminal_draw(terminal);
574 io_handler(GIOChannel *source,
575 GIOCondition condition,
578 struct terminal *terminal = data;
581 GError *error = NULL;
583 g_io_channel_read_chars(source, buffer, sizeof buffer,
584 &bytes_read, &error);
586 terminal_data(terminal, buffer, bytes_read);
592 terminal_run(struct terminal *terminal, const char *path)
597 pid = forkpty(&master, NULL, NULL, NULL);
600 setenv("TERM", "vt100", 1);
601 if (execl(path, path, NULL)) {
602 printf("exec failed: %m\n");
605 } else if (pid < 0) {
606 fprintf(stderr, "failed to fork and create pty (%m).\n");
610 terminal->master = master;
611 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);
619 static const GOptionEntry option_entries[] = {
620 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE,
621 &option_fullscreen, "Run in fullscreen mode" },
625 int main(int argc, char *argv[])
627 struct wl_display *display;
631 struct terminal *terminal;
632 GOptionContext *context;
635 context = g_option_context_new(NULL);
636 g_option_context_add_main_entries(context, option_entries, "Wayland Terminal");
637 if (!g_option_context_parse(context, &argc, &argv, &error)) {
638 fprintf(stderr, "option parsing failed: %s\n", error->message);
642 fd = open(gem_device, O_RDWR);
644 fprintf(stderr, "drm open failed: %m\n");
648 display = wl_display_create(socket_name, sizeof socket_name);
649 if (display == NULL) {
650 fprintf(stderr, "failed to create display: %m\n");
654 loop = g_main_loop_new(NULL, FALSE);
655 source = wl_glib_source_new(display);
656 g_source_attach(source, NULL);
658 terminal = terminal_create(display, fd, option_fullscreen);
659 if (terminal_run(terminal, "/bin/bash"))
662 g_main_loop_run(loop);