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