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