Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 1 | /* |
| 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øgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 31 | #include <pty.h> |
Kristian Høgsberg | f04e838 | 2008-12-08 00:07:49 -0500 | [diff] [blame] | 32 | #include <ctype.h> |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 33 | #include <cairo.h> |
| 34 | #include <glib.h> |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 35 | #include <linux/input.h> |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 36 | |
| 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 | |
| 46 | static const char gem_device[] = "/dev/dri/card0"; |
| 47 | static const char socket_name[] = "\0wayland"; |
| 48 | |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 49 | #define MOD_SHIFT 0x01 |
| 50 | #define MOD_ALT 0x02 |
| 51 | #define MOD_CTRL 0x04 |
| 52 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 53 | struct terminal { |
| 54 | struct window *window; |
| 55 | struct wl_display *display; |
Kristian Høgsberg | 721f09f | 2008-12-08 11:13:26 -0500 | [diff] [blame] | 56 | int redraw_scheduled, redraw_pending; |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 57 | char *data; |
Kristian Høgsberg | b29415e | 2008-12-08 00:16:39 -0500 | [diff] [blame] | 58 | int width, height, tail, row, column, total_rows; |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 59 | int fd, master; |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 60 | struct buffer *buffer; |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 61 | GIOChannel *channel; |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 62 | uint32_t modifiers; |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame^] | 63 | char escape[64]; |
| 64 | int escape_length; |
Kristian Høgsberg | f04e838 | 2008-12-08 00:07:49 -0500 | [diff] [blame] | 65 | int state; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 66 | }; |
| 67 | |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 68 | static void |
| 69 | terminal_draw_contents(struct terminal *terminal) |
| 70 | { |
| 71 | struct rectangle rectangle; |
| 72 | cairo_surface_t *surface; |
| 73 | cairo_t *cr; |
| 74 | cairo_font_extents_t extents; |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame^] | 75 | int i, row; |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 76 | |
| 77 | window_get_child_rectangle(terminal->window, &rectangle); |
| 78 | |
| 79 | surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
| 80 | rectangle.width, rectangle.height); |
| 81 | cr = cairo_create(surface); |
| 82 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 83 | cairo_set_source_rgba(cr, 0, 0, 0, 0.9); |
| 84 | cairo_paint(cr); |
| 85 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 86 | cairo_set_source_rgba(cr, 0, 0.5, 0, 1); |
| 87 | |
| 88 | cairo_select_font_face (cr, "mono", |
| 89 | CAIRO_FONT_SLANT_NORMAL, |
| 90 | CAIRO_FONT_WEIGHT_NORMAL); |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 91 | cairo_set_font_size(cr, 14); |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 92 | |
| 93 | cairo_font_extents(cr, &extents); |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame^] | 94 | for (i = 0; i < terminal->total_rows; i++) { |
| 95 | row = (terminal->tail + i) % terminal->height; |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 96 | cairo_move_to(cr, 0, extents.ascent + extents.height * i); |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame^] | 97 | cairo_show_text(cr, &terminal->data[row * (terminal->width + 1)]); |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 98 | } |
| 99 | cairo_destroy(cr); |
| 100 | |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 101 | terminal->buffer = buffer_create_from_cairo_surface(terminal->fd, surface); |
| 102 | cairo_surface_destroy(surface); |
| 103 | |
| 104 | window_copy(terminal->window, |
| 105 | &rectangle, |
| 106 | terminal->buffer->name, terminal->buffer->stride); |
| 107 | } |
| 108 | |
| 109 | static void |
| 110 | terminal_draw(struct terminal *terminal) |
| 111 | { |
| 112 | window_draw(terminal->window); |
| 113 | terminal_draw_contents(terminal); |
| 114 | wl_display_commit(terminal->display, 0); |
| 115 | } |
| 116 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 117 | static gboolean |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 118 | idle_redraw(void *data) |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 119 | { |
| 120 | struct terminal *terminal = data; |
| 121 | |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 122 | terminal_draw(terminal); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 123 | |
| 124 | return FALSE; |
| 125 | } |
| 126 | |
Kristian Høgsberg | f04e838 | 2008-12-08 00:07:49 -0500 | [diff] [blame] | 127 | #define STATE_NORMAL 0 |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame^] | 128 | #define STATE_ESCAPE 1 |
| 129 | |
| 130 | static void |
| 131 | terminal_data(struct terminal *terminal, const char *data, size_t length); |
Kristian Høgsberg | f04e838 | 2008-12-08 00:07:49 -0500 | [diff] [blame] | 132 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 133 | static void |
Kristian Høgsberg | 721f09f | 2008-12-08 11:13:26 -0500 | [diff] [blame] | 134 | terminal_schedule_redraw(struct terminal *terminal) |
| 135 | { |
| 136 | if (!terminal->redraw_scheduled) { |
| 137 | g_idle_add(idle_redraw, terminal); |
| 138 | terminal->redraw_scheduled = 1; |
| 139 | } else { |
| 140 | terminal->redraw_pending = 1; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | static void |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame^] | 145 | handle_escape(struct terminal *terminal) |
| 146 | { |
| 147 | char *row; |
| 148 | int i, j; |
| 149 | |
| 150 | terminal->escape[terminal->escape_length++] = '\0'; |
| 151 | if (strcmp(terminal->escape, "\e[J") == 0) { |
| 152 | row = &terminal->data[terminal->row * (terminal->width + 1)]; |
| 153 | memset(&row[terminal->column], 0, terminal->width - terminal->column); |
| 154 | for (i = terminal->total_rows; i < terminal->height; i++) { |
| 155 | |
| 156 | j = terminal->row + i; |
| 157 | if (j >= terminal->height) |
| 158 | j -= terminal->height; |
| 159 | |
| 160 | row = &terminal->data[j * (terminal->width + 1)]; |
| 161 | memset(row, 0, terminal->width); |
| 162 | } |
| 163 | } else if (strcmp(terminal->escape, "\e[H") == 0) { |
| 164 | terminal->row = terminal->tail; |
| 165 | terminal->total_rows = 1; |
| 166 | terminal->column = 0; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | static void |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 171 | terminal_data(struct terminal *terminal, const char *data, size_t length) |
| 172 | { |
| 173 | int i; |
| 174 | char *row; |
| 175 | |
| 176 | for (i = 0; i < length; i++) { |
| 177 | row = &terminal->data[terminal->row * (terminal->width + 1)]; |
Kristian Høgsberg | f04e838 | 2008-12-08 00:07:49 -0500 | [diff] [blame] | 178 | |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame^] | 179 | if (terminal->state == STATE_ESCAPE) { |
| 180 | terminal->escape[terminal->escape_length++] = data[i]; |
| 181 | if (terminal->escape_length == 2 && data[i] != '[') { |
| 182 | /* Bad escape sequence. */ |
Kristian Høgsberg | f04e838 | 2008-12-08 00:07:49 -0500 | [diff] [blame] | 183 | terminal->state = STATE_NORMAL; |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame^] | 184 | goto cancel_escape; |
| 185 | } |
| 186 | |
| 187 | if (isalpha(data[i])) { |
| 188 | terminal->state = STATE_NORMAL; |
| 189 | handle_escape(terminal); |
| 190 | } |
Kristian Høgsberg | f04e838 | 2008-12-08 00:07:49 -0500 | [diff] [blame] | 191 | continue; |
| 192 | } |
| 193 | |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame^] | 194 | cancel_escape: |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 195 | switch (data[i]) { |
| 196 | case '\r': |
| 197 | terminal->column = 0; |
| 198 | break; |
| 199 | case '\n': |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 200 | terminal->column = 0; |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame^] | 201 | terminal->row++; |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 202 | if (terminal->row == terminal->height) |
| 203 | terminal->row = 0; |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame^] | 204 | if (terminal->total_rows == terminal->height) { |
Kristian Høgsberg | b29415e | 2008-12-08 00:16:39 -0500 | [diff] [blame] | 205 | memset(&terminal->data[terminal->row * (terminal->width + 1)], |
| 206 | 0, terminal->width); |
| 207 | terminal->tail++; |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame^] | 208 | } else { |
| 209 | terminal->total_rows++; |
Kristian Høgsberg | b29415e | 2008-12-08 00:16:39 -0500 | [diff] [blame] | 210 | } |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame^] | 211 | |
Kristian Høgsberg | b29415e | 2008-12-08 00:16:39 -0500 | [diff] [blame] | 212 | if (terminal->tail == terminal->height) |
| 213 | terminal->tail = 0; |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 214 | break; |
| 215 | case '\t': |
| 216 | memset(&row[terminal->column], ' ', -terminal->column & 7); |
| 217 | terminal->column = (terminal->column + 7) & ~7; |
| 218 | break; |
Kristian Høgsberg | f04e838 | 2008-12-08 00:07:49 -0500 | [diff] [blame] | 219 | case '\e': |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame^] | 220 | terminal->state = STATE_ESCAPE; |
| 221 | terminal->escape[0] = '\e'; |
| 222 | terminal->escape_length = 1; |
Kristian Høgsberg | f04e838 | 2008-12-08 00:07:49 -0500 | [diff] [blame] | 223 | break; |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 224 | default: |
| 225 | if (terminal->column < terminal->width) |
| 226 | row[terminal->column++] = data[i]; |
| 227 | break; |
| 228 | } |
| 229 | } |
Kristian Høgsberg | 721f09f | 2008-12-08 11:13:26 -0500 | [diff] [blame] | 230 | |
| 231 | terminal_schedule_redraw(terminal); |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 232 | } |
| 233 | |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 234 | static void |
| 235 | resize_handler(struct window *window, int32_t width, int32_t height, void *data) |
| 236 | { |
| 237 | struct terminal *terminal = data; |
| 238 | |
Kristian Høgsberg | 721f09f | 2008-12-08 11:13:26 -0500 | [diff] [blame] | 239 | terminal_schedule_redraw(terminal); |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | static void |
| 243 | acknowledge_handler(struct window *window, uint32_t key, void *data) |
| 244 | { |
| 245 | struct terminal *terminal = data; |
| 246 | |
Kristian Høgsberg | 721f09f | 2008-12-08 11:13:26 -0500 | [diff] [blame] | 247 | terminal->redraw_scheduled = 0; |
Kristian Høgsberg | c47303f | 2008-12-08 09:57:08 -0500 | [diff] [blame] | 248 | buffer_destroy(terminal->buffer, terminal->fd); |
Kristian Høgsberg | 721f09f | 2008-12-08 11:13:26 -0500 | [diff] [blame] | 249 | |
| 250 | if (terminal->redraw_pending) { |
| 251 | terminal->redraw_pending = 0; |
| 252 | terminal_schedule_redraw(terminal); |
| 253 | } |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | struct key { |
Kristian Høgsberg | 0d77fd4 | 2008-12-08 00:23:55 -0500 | [diff] [blame] | 257 | int code[4]; |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 258 | } evdev_keymap[] = { |
| 259 | { { 0, 0 } }, /* 0 */ |
| 260 | { { 0x1b, 0x1b } }, |
| 261 | { { '1', '!' } }, |
| 262 | { { '2', '@' } }, |
| 263 | { { '3', '#' } }, |
| 264 | { { '4', '$' } }, |
| 265 | { { '5', '%' } }, |
| 266 | { { '6', '^' } }, |
| 267 | { { '7', '&' } }, |
| 268 | { { '8', '*' } }, |
| 269 | { { '9', '(' } }, |
| 270 | { { '0', ')' } }, |
| 271 | { { '-', '_' } }, |
| 272 | { { '=', '+' } }, |
| 273 | { { '\b', '\b' } }, |
| 274 | { { '\t', '\t' } }, |
| 275 | |
| 276 | { { 'q', 'Q' } }, /* 16 */ |
Kristian Høgsberg | 0d77fd4 | 2008-12-08 00:23:55 -0500 | [diff] [blame] | 277 | { { 'w', 'W', 0x17 } }, |
| 278 | { { 'e', 'E', 0x05 } }, |
| 279 | { { 'r', 'R', 0x12 } }, |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 280 | { { 't', 'T' } }, |
| 281 | { { 'y', 'Y' } }, |
Kristian Høgsberg | 0d77fd4 | 2008-12-08 00:23:55 -0500 | [diff] [blame] | 282 | { { 'u', 'U', 0x15 } }, |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 283 | { { 'i', 'I' } }, |
| 284 | { { 'o', 'O' } }, |
Kristian Høgsberg | 0d77fd4 | 2008-12-08 00:23:55 -0500 | [diff] [blame] | 285 | { { 'p', 'P', 0x10 } }, |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 286 | { { '[', '{' } }, |
| 287 | { { ']', '}' } }, |
| 288 | { { '\n', '\n' } }, |
| 289 | { { 0, 0 } }, |
Kristian Høgsberg | 0d77fd4 | 2008-12-08 00:23:55 -0500 | [diff] [blame] | 290 | { { 'a', 'A', 0x01} }, |
| 291 | { { 's', 'S', 0x13 } }, |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 292 | |
Kristian Høgsberg | 0d77fd4 | 2008-12-08 00:23:55 -0500 | [diff] [blame] | 293 | { { 'd', 'D', 0x04 } }, /* 32 */ |
| 294 | { { 'f', 'F', 0x06 } }, |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 295 | { { 'g', 'G' } }, |
| 296 | { { 'h', 'H' } }, |
Kristian Høgsberg | 0d77fd4 | 2008-12-08 00:23:55 -0500 | [diff] [blame] | 297 | { { 'j', 'J', 0x0a } }, |
| 298 | { { 'k', 'K', 0x0b } }, |
| 299 | { { 'l', 'L', 0x0c } }, |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 300 | { { ';', ':' } }, |
| 301 | { { '\'', '"' } }, |
| 302 | { { '`', '~' } }, |
| 303 | { { 0, 0 } }, |
| 304 | { { '\\', '|' } }, |
| 305 | { { 'z', 'Z' } }, |
| 306 | { { 'x', 'X' } }, |
| 307 | { { 'c', 'C' } }, |
| 308 | { { 'v', 'V' } }, |
| 309 | |
Kristian Høgsberg | 0d77fd4 | 2008-12-08 00:23:55 -0500 | [diff] [blame] | 310 | { { 'b', 'B', 0x02 } }, /* 48 */ |
| 311 | { { 'n', 'N', 0x0e } }, |
| 312 | { { 'm', 'M', 0x0d } }, |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 313 | { { ',', '<' } }, |
| 314 | { { '.', '>' } }, |
| 315 | { { '/', '?' } }, |
| 316 | { { 0, 0 } }, |
| 317 | { { '*', '*' } }, |
| 318 | { { 0, 0 } }, |
| 319 | { { ' ', ' ' } }, |
| 320 | { { 0, 0 } } |
| 321 | |
| 322 | /* 59 */ |
| 323 | }; |
| 324 | |
| 325 | #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0]) |
| 326 | |
| 327 | static void |
| 328 | key_handler(struct window *window, uint32_t key, uint32_t state, void *data) |
| 329 | { |
| 330 | struct terminal *terminal = data; |
| 331 | uint32_t mod = 0; |
| 332 | char c; |
| 333 | |
| 334 | switch (key) { |
| 335 | case KEY_LEFTSHIFT: |
| 336 | case KEY_RIGHTSHIFT: |
| 337 | mod = MOD_SHIFT; |
| 338 | break; |
| 339 | case KEY_LEFTCTRL: |
| 340 | case KEY_RIGHTCTRL: |
| 341 | mod = MOD_CTRL; |
| 342 | break; |
| 343 | case KEY_LEFTALT: |
| 344 | case KEY_RIGHTALT: |
| 345 | mod = MOD_ALT; |
| 346 | break; |
| 347 | default: |
| 348 | if (key < ARRAY_LENGTH(evdev_keymap)) { |
Kristian Høgsberg | 0d77fd4 | 2008-12-08 00:23:55 -0500 | [diff] [blame] | 349 | if (terminal->modifiers & MOD_CTRL) |
| 350 | c = evdev_keymap[key].code[2]; |
| 351 | else if (terminal->modifiers & MOD_SHIFT) |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 352 | c = evdev_keymap[key].code[1]; |
| 353 | else |
| 354 | c = evdev_keymap[key].code[0]; |
| 355 | if (state && c) |
| 356 | write(terminal->master, &c, 1); |
| 357 | } |
| 358 | break; |
| 359 | } |
| 360 | |
| 361 | if (state) |
| 362 | terminal->modifiers |= mod; |
| 363 | else |
| 364 | terminal->modifiers &= ~mod; |
| 365 | } |
| 366 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 367 | static struct terminal * |
| 368 | terminal_create(struct wl_display *display, int fd) |
| 369 | { |
| 370 | struct terminal *terminal; |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 371 | int size; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 372 | |
| 373 | terminal = malloc(sizeof *terminal); |
| 374 | if (terminal == NULL) |
| 375 | return terminal; |
| 376 | |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 377 | memset(terminal, 0, sizeof *terminal); |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 378 | terminal->fd = fd; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 379 | terminal->window = window_create(display, fd, "Wayland Terminal", |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 380 | 500, 100, 500, 400); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 381 | terminal->display = display; |
Kristian Høgsberg | 721f09f | 2008-12-08 11:13:26 -0500 | [diff] [blame] | 382 | terminal->redraw_scheduled = 1; |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 383 | terminal->width = 80; |
| 384 | terminal->height = 25; |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame^] | 385 | terminal->total_rows = 1; |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 386 | size = (terminal->width + 1) * terminal->height; |
| 387 | terminal->data = malloc(size); |
| 388 | memset(terminal->data, 0, size); |
| 389 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 390 | window_set_resize_handler(terminal->window, resize_handler, terminal); |
| 391 | window_set_acknowledge_handler(terminal->window, acknowledge_handler, terminal); |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 392 | window_set_key_handler(terminal->window, key_handler, terminal); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 393 | |
| 394 | return terminal; |
| 395 | } |
| 396 | |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 397 | static gboolean |
| 398 | io_handler(GIOChannel *source, |
| 399 | GIOCondition condition, |
| 400 | gpointer data) |
| 401 | { |
| 402 | struct terminal *terminal = data; |
| 403 | gchar buffer[256]; |
| 404 | gsize bytes_read; |
| 405 | GError *error = NULL; |
| 406 | |
| 407 | g_io_channel_read_chars(source, buffer, sizeof buffer, |
| 408 | &bytes_read, &error); |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 409 | |
| 410 | terminal_data(terminal, buffer, bytes_read); |
| 411 | |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 412 | return TRUE; |
| 413 | } |
| 414 | |
| 415 | static int |
| 416 | terminal_run(struct terminal *terminal, const char *path) |
| 417 | { |
| 418 | int master, slave; |
| 419 | pid_t pid; |
| 420 | |
| 421 | pid = forkpty(&master, NULL, NULL, NULL); |
| 422 | if (pid == 0) { |
| 423 | close(master); |
| 424 | if (execl(path, path, NULL)) { |
| 425 | printf("exec failed: %m\n"); |
| 426 | exit(EXIT_FAILURE); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | close(slave); |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 431 | terminal->master = master; |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 432 | terminal->channel = g_io_channel_unix_new(master); |
| 433 | fcntl(master, F_SETFL, O_NONBLOCK); |
| 434 | g_io_add_watch(terminal->channel, G_IO_IN, |
| 435 | io_handler, terminal); |
| 436 | |
| 437 | return 0; |
| 438 | } |
| 439 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 440 | int main(int argc, char *argv[]) |
| 441 | { |
| 442 | struct wl_display *display; |
| 443 | int fd; |
| 444 | GMainLoop *loop; |
| 445 | GSource *source; |
| 446 | struct terminal *terminal; |
| 447 | |
| 448 | fd = open(gem_device, O_RDWR); |
| 449 | if (fd < 0) { |
| 450 | fprintf(stderr, "drm open failed: %m\n"); |
| 451 | return -1; |
| 452 | } |
| 453 | |
| 454 | display = wl_display_create(socket_name, sizeof socket_name); |
| 455 | if (display == NULL) { |
| 456 | fprintf(stderr, "failed to create display: %m\n"); |
| 457 | return -1; |
| 458 | } |
| 459 | |
| 460 | loop = g_main_loop_new(NULL, FALSE); |
| 461 | source = wl_glib_source_new(display); |
| 462 | g_source_attach(source, NULL); |
| 463 | |
| 464 | terminal = terminal_create(display, fd); |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 465 | terminal_run(terminal, "/bin/bash"); |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 466 | terminal_draw(terminal); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 467 | |
| 468 | g_main_loop_run(loop); |
| 469 | |
| 470 | return 0; |
| 471 | } |