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