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 | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 36 | #include <cairo-drm.h> |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 37 | |
| 38 | #include <GL/gl.h> |
| 39 | #include <eagle.h> |
| 40 | |
| 41 | #include "wayland-client.h" |
| 42 | #include "wayland-glib.h" |
| 43 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 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; |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 56 | struct 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 | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 62 | cairo_surface_t *surface; |
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; |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 131 | cairo_t *cr; |
| 132 | cairo_font_extents_t extents; |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 133 | int i, top_margin, side_margin; |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 134 | |
| 135 | window_get_child_rectangle(terminal->window, &rectangle); |
| 136 | |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 137 | terminal->surface = |
| 138 | window_create_surface(terminal->window, &rectangle); |
| 139 | cr = cairo_create(terminal->surface); |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 140 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 141 | cairo_set_source_rgba(cr, 0, 0, 0, 0.9); |
| 142 | cairo_paint(cr); |
| 143 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
Kristian Høgsberg | 6e0a2f8 | 2008-12-08 14:06:56 -0500 | [diff] [blame] | 144 | cairo_set_source_rgba(cr, 0, 0.7, 0, 1); |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 145 | |
| 146 | cairo_select_font_face (cr, "mono", |
| 147 | CAIRO_FONT_SLANT_NORMAL, |
| 148 | CAIRO_FONT_WEIGHT_NORMAL); |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 149 | cairo_set_font_size(cr, 14); |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 150 | |
| 151 | cairo_font_extents(cr, &extents); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 152 | side_margin = (rectangle.width - terminal->width * extents.max_x_advance) / 2; |
| 153 | top_margin = (rectangle.height - terminal->height * extents.height) / 2; |
| 154 | |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 155 | for (i = 0; i < terminal->height; i++) { |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 156 | cairo_move_to(cr, side_margin, |
| 157 | top_margin + extents.ascent + extents.height * i); |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 158 | cairo_show_text(cr, terminal_get_row(terminal, i)); |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 159 | } |
Kristian Høgsberg | b0b82e2 | 2009-02-21 15:42:25 -0500 | [diff] [blame] | 160 | |
| 161 | cairo_move_to(cr, side_margin + terminal->column * extents.max_x_advance, |
| 162 | top_margin + terminal->row * extents.height); |
| 163 | cairo_rel_line_to(cr, extents.max_x_advance, 0); |
| 164 | cairo_rel_line_to(cr, 0, extents.height); |
| 165 | cairo_rel_line_to(cr, -extents.max_x_advance, 0); |
| 166 | cairo_close_path(cr); |
| 167 | cairo_fill(cr); |
| 168 | |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 169 | cairo_destroy(cr); |
| 170 | |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 171 | window_copy_surface(terminal->window, |
| 172 | &rectangle, |
| 173 | terminal->surface); |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static void |
| 177 | terminal_draw(struct terminal *terminal) |
| 178 | { |
Kristian Høgsberg | 2210676 | 2008-12-08 13:50:07 -0500 | [diff] [blame] | 179 | struct rectangle rectangle; |
| 180 | cairo_surface_t *surface; |
| 181 | cairo_font_extents_t extents; |
| 182 | cairo_t *cr; |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 183 | int32_t width, height; |
Kristian Høgsberg | 2210676 | 2008-12-08 13:50:07 -0500 | [diff] [blame] | 184 | |
| 185 | window_get_child_rectangle(terminal->window, &rectangle); |
| 186 | |
| 187 | surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0); |
| 188 | cr = cairo_create(surface); |
| 189 | cairo_select_font_face (cr, "mono", |
| 190 | CAIRO_FONT_SLANT_NORMAL, |
| 191 | CAIRO_FONT_WEIGHT_NORMAL); |
| 192 | cairo_set_font_size(cr, 14); |
| 193 | cairo_font_extents(cr, &extents); |
| 194 | cairo_destroy(cr); |
| 195 | cairo_surface_destroy(surface); |
| 196 | |
| 197 | width = (rectangle.width - 2 * terminal->margin) / (int32_t) extents.max_x_advance; |
| 198 | height = (rectangle.height - 2 * terminal->margin) / (int32_t) extents.height; |
| 199 | terminal_resize(terminal, width, height); |
| 200 | |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 201 | if (!terminal->fullscreen) { |
| 202 | rectangle.width = terminal->width * extents.max_x_advance + 2 * terminal->margin; |
| 203 | rectangle.height = terminal->height * extents.height + 2 * terminal->margin; |
| 204 | window_set_child_size(terminal->window, &rectangle); |
| 205 | } |
Kristian Høgsberg | 2210676 | 2008-12-08 13:50:07 -0500 | [diff] [blame] | 206 | |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 207 | window_draw(terminal->window); |
| 208 | terminal_draw_contents(terminal); |
Kristian Høgsberg | d2412e2 | 2008-12-15 20:35:24 -0500 | [diff] [blame] | 209 | wl_compositor_commit(terminal->compositor, 0); |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 210 | } |
| 211 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 212 | static gboolean |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 213 | idle_redraw(void *data) |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 214 | { |
| 215 | struct terminal *terminal = data; |
| 216 | |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 217 | terminal_draw(terminal); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 218 | |
| 219 | return FALSE; |
| 220 | } |
| 221 | |
Kristian Høgsberg | f04e838 | 2008-12-08 00:07:49 -0500 | [diff] [blame] | 222 | #define STATE_NORMAL 0 |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame] | 223 | #define STATE_ESCAPE 1 |
| 224 | |
| 225 | static void |
| 226 | terminal_data(struct terminal *terminal, const char *data, size_t length); |
Kristian Høgsberg | f04e838 | 2008-12-08 00:07:49 -0500 | [diff] [blame] | 227 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 228 | static void |
Kristian Høgsberg | 721f09f | 2008-12-08 11:13:26 -0500 | [diff] [blame] | 229 | terminal_schedule_redraw(struct terminal *terminal) |
| 230 | { |
| 231 | if (!terminal->redraw_scheduled) { |
| 232 | g_idle_add(idle_redraw, terminal); |
| 233 | terminal->redraw_scheduled = 1; |
| 234 | } else { |
| 235 | terminal->redraw_pending = 1; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | static void |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame] | 240 | handle_escape(struct terminal *terminal) |
| 241 | { |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 242 | char *row, *p; |
| 243 | int i, count; |
| 244 | int args[10], set[10] = { 0, }; |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame] | 245 | |
| 246 | terminal->escape[terminal->escape_length++] = '\0'; |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 247 | i = 0; |
| 248 | p = &terminal->escape[2]; |
| 249 | while ((isdigit(*p) || *p == ';') && i < 10) { |
| 250 | if (*p == ';') { |
| 251 | p++; |
| 252 | i++; |
| 253 | } else { |
| 254 | args[i] = strtol(p, &p, 10); |
| 255 | set[i] = 1; |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame] | 256 | } |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame] | 257 | } |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 258 | |
| 259 | switch (*p) { |
| 260 | case 'A': |
| 261 | count = set[0] ? args[0] : 1; |
| 262 | if (terminal->row - count >= 0) |
| 263 | terminal->row -= count; |
| 264 | else |
| 265 | terminal->row = 0; |
| 266 | break; |
| 267 | case 'B': |
| 268 | count = set[0] ? args[0] : 1; |
| 269 | if (terminal->row + count < terminal->height) |
| 270 | terminal->row += count; |
| 271 | else |
| 272 | terminal->row = terminal->height; |
| 273 | break; |
| 274 | case 'C': |
| 275 | count = set[0] ? args[0] : 1; |
| 276 | if (terminal->column + count < terminal->width) |
| 277 | terminal->column += count; |
| 278 | else |
| 279 | terminal->column = terminal->width; |
| 280 | break; |
| 281 | case 'D': |
| 282 | count = set[0] ? args[0] : 1; |
| 283 | if (terminal->column - count >= 0) |
| 284 | terminal->column -= count; |
| 285 | else |
| 286 | terminal->column = 0; |
| 287 | break; |
| 288 | case 'J': |
| 289 | row = terminal_get_row(terminal, terminal->row); |
| 290 | memset(&row[terminal->column], 0, terminal->width - terminal->column); |
| 291 | for (i = terminal->row + 1; i < terminal->height; i++) |
| 292 | memset(terminal_get_row(terminal, i), 0, terminal->width); |
| 293 | break; |
| 294 | case 'G': |
| 295 | if (set[0]) |
| 296 | terminal->column = args[0] - 1; |
| 297 | break; |
| 298 | case 'H': |
| 299 | case 'f': |
| 300 | terminal->row = set[0] ? args[0] - 1 : 0; |
| 301 | terminal->column = set[1] ? args[1] - 1 : 0; |
| 302 | break; |
| 303 | case 'K': |
| 304 | row = terminal_get_row(terminal, terminal->row); |
| 305 | memset(&row[terminal->column], 0, terminal->width - terminal->column); |
| 306 | break; |
| 307 | case 'm': |
| 308 | /* color, blink, bold etc*/ |
| 309 | break; |
| 310 | case '?': |
| 311 | if (strcmp(p, "?25l") == 0) { |
| 312 | /* hide cursor */ |
| 313 | } else if (strcmp(p, "?25h") == 0) { |
| 314 | /* show cursor */ |
| 315 | } |
| 316 | break; |
| 317 | default: |
| 318 | terminal_data(terminal, |
| 319 | terminal->escape + 1, |
| 320 | terminal->escape_length - 2); |
| 321 | break; |
| 322 | } |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | static void |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 326 | terminal_data(struct terminal *terminal, const char *data, size_t length) |
| 327 | { |
| 328 | int i; |
| 329 | char *row; |
| 330 | |
| 331 | for (i = 0; i < length; i++) { |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 332 | row = terminal_get_row(terminal, terminal->row); |
Kristian Høgsberg | f04e838 | 2008-12-08 00:07:49 -0500 | [diff] [blame] | 333 | |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame] | 334 | if (terminal->state == STATE_ESCAPE) { |
| 335 | terminal->escape[terminal->escape_length++] = data[i]; |
| 336 | if (terminal->escape_length == 2 && data[i] != '[') { |
| 337 | /* Bad escape sequence. */ |
Kristian Høgsberg | f04e838 | 2008-12-08 00:07:49 -0500 | [diff] [blame] | 338 | terminal->state = STATE_NORMAL; |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame] | 339 | goto cancel_escape; |
| 340 | } |
| 341 | |
| 342 | if (isalpha(data[i])) { |
| 343 | terminal->state = STATE_NORMAL; |
| 344 | handle_escape(terminal); |
| 345 | } |
Kristian Høgsberg | f04e838 | 2008-12-08 00:07:49 -0500 | [diff] [blame] | 346 | continue; |
| 347 | } |
| 348 | |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame] | 349 | cancel_escape: |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 350 | switch (data[i]) { |
| 351 | case '\r': |
| 352 | terminal->column = 0; |
| 353 | break; |
| 354 | case '\n': |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 355 | terminal->column = 0; |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 356 | if (terminal->row + 1 < terminal->height) { |
| 357 | terminal->row++; |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame] | 358 | } else { |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 359 | terminal->start++; |
| 360 | if (terminal->start == terminal->height) |
| 361 | terminal->start = 0; |
| 362 | memset(terminal_get_row(terminal, terminal->row), |
| 363 | 0, terminal->width); |
Kristian Høgsberg | b29415e | 2008-12-08 00:16:39 -0500 | [diff] [blame] | 364 | } |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame] | 365 | |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 366 | break; |
| 367 | case '\t': |
| 368 | memset(&row[terminal->column], ' ', -terminal->column & 7); |
| 369 | terminal->column = (terminal->column + 7) & ~7; |
| 370 | break; |
Kristian Høgsberg | f04e838 | 2008-12-08 00:07:49 -0500 | [diff] [blame] | 371 | case '\e': |
Kristian Høgsberg | 17809b1 | 2008-12-08 12:20:40 -0500 | [diff] [blame] | 372 | terminal->state = STATE_ESCAPE; |
| 373 | terminal->escape[0] = '\e'; |
| 374 | terminal->escape_length = 1; |
Kristian Høgsberg | f04e838 | 2008-12-08 00:07:49 -0500 | [diff] [blame] | 375 | break; |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 376 | case '\b': |
| 377 | if (terminal->column > 0) |
| 378 | terminal->column--; |
| 379 | break; |
| 380 | case '\a': |
| 381 | /* Bell */ |
| 382 | break; |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 383 | default: |
| 384 | if (terminal->column < terminal->width) |
Kristian Høgsberg | dbd5464 | 2008-12-08 22:22:25 -0500 | [diff] [blame] | 385 | row[terminal->column++] = data[i] < 32 ? data[i] + 64 : data[i]; |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 386 | break; |
| 387 | } |
| 388 | } |
Kristian Høgsberg | 721f09f | 2008-12-08 11:13:26 -0500 | [diff] [blame] | 389 | |
| 390 | terminal_schedule_redraw(terminal); |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 391 | } |
| 392 | |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 393 | static void |
Kristian Høgsberg | 2210676 | 2008-12-08 13:50:07 -0500 | [diff] [blame] | 394 | resize_handler(struct window *window, void *data) |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 395 | { |
| 396 | struct terminal *terminal = data; |
| 397 | |
Kristian Høgsberg | 721f09f | 2008-12-08 11:13:26 -0500 | [diff] [blame] | 398 | terminal_schedule_redraw(terminal); |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | static void |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 402 | handle_acknowledge(void *data, |
| 403 | struct wl_compositor *compositor, |
| 404 | uint32_t key, uint32_t frame) |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 405 | { |
| 406 | struct terminal *terminal = data; |
| 407 | |
Kristian Høgsberg | 721f09f | 2008-12-08 11:13:26 -0500 | [diff] [blame] | 408 | terminal->redraw_scheduled = 0; |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 409 | if (key == 0) |
Kristian Høgsberg | 0ac16f0 | 2009-01-15 11:37:43 -0500 | [diff] [blame] | 410 | cairo_surface_destroy(terminal->surface); |
Kristian Høgsberg | 721f09f | 2008-12-08 11:13:26 -0500 | [diff] [blame] | 411 | |
| 412 | if (terminal->redraw_pending) { |
| 413 | terminal->redraw_pending = 0; |
| 414 | terminal_schedule_redraw(terminal); |
| 415 | } |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 416 | } |
| 417 | |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 418 | static void |
| 419 | handle_frame(void *data, |
| 420 | struct wl_compositor *compositor, |
| 421 | uint32_t frame, uint32_t timestamp) |
| 422 | { |
| 423 | } |
| 424 | |
| 425 | static const struct wl_compositor_listener compositor_listener = { |
| 426 | handle_acknowledge, |
| 427 | handle_frame, |
| 428 | }; |
| 429 | |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 430 | static void |
Kristian Høgsberg | 5544491 | 2009-02-21 14:31:09 -0500 | [diff] [blame] | 431 | key_handler(struct window *window, uint32_t key, uint32_t unicode, |
| 432 | uint32_t state, uint32_t modifiers, void *data) |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 433 | { |
| 434 | struct terminal *terminal = data; |
Kristian Høgsberg | 5544491 | 2009-02-21 14:31:09 -0500 | [diff] [blame] | 435 | char ch = unicode; |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 436 | |
| 437 | switch (key) { |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 438 | case KEY_F11: |
| 439 | if (!state) |
| 440 | break; |
| 441 | terminal->fullscreen ^= 1; |
| 442 | window_set_fullscreen(window, terminal->fullscreen); |
| 443 | terminal_schedule_redraw(terminal); |
| 444 | break; |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 445 | default: |
Kristian Høgsberg | 5544491 | 2009-02-21 14:31:09 -0500 | [diff] [blame] | 446 | if (state && unicode) |
| 447 | write(terminal->master, &ch, 1); |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 448 | break; |
| 449 | } |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 450 | } |
| 451 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 452 | static struct terminal * |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 453 | terminal_create(struct display *display, int fullscreen) |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 454 | { |
| 455 | struct terminal *terminal; |
| 456 | |
| 457 | terminal = malloc(sizeof *terminal); |
| 458 | if (terminal == NULL) |
| 459 | return terminal; |
| 460 | |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 461 | memset(terminal, 0, sizeof *terminal); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 462 | terminal->fullscreen = fullscreen; |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 463 | terminal->window = window_create(display, "Wayland Terminal", |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 464 | 500, 100, 500, 400); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 465 | terminal->display = display; |
Kristian Høgsberg | 721f09f | 2008-12-08 11:13:26 -0500 | [diff] [blame] | 466 | terminal->redraw_scheduled = 1; |
Kristian Høgsberg | 1584c57 | 2008-12-08 12:59:37 -0500 | [diff] [blame] | 467 | terminal->margin = 5; |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 468 | |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 469 | terminal->compositor = display_get_compositor(display); |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 470 | window_set_fullscreen(terminal->window, terminal->fullscreen); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 471 | window_set_resize_handler(terminal->window, resize_handler, terminal); |
Kristian Høgsberg | 94448c0 | 2008-12-30 11:03:33 -0500 | [diff] [blame] | 472 | |
| 473 | wl_compositor_add_listener(terminal->compositor, |
| 474 | &compositor_listener, terminal); |
| 475 | |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 476 | window_set_key_handler(terminal->window, key_handler, terminal); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 477 | |
Kristian Høgsberg | 2210676 | 2008-12-08 13:50:07 -0500 | [diff] [blame] | 478 | terminal_draw(terminal); |
| 479 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 480 | return terminal; |
| 481 | } |
| 482 | |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 483 | static gboolean |
| 484 | io_handler(GIOChannel *source, |
| 485 | GIOCondition condition, |
| 486 | gpointer data) |
| 487 | { |
| 488 | struct terminal *terminal = data; |
| 489 | gchar buffer[256]; |
| 490 | gsize bytes_read; |
| 491 | GError *error = NULL; |
| 492 | |
| 493 | g_io_channel_read_chars(source, buffer, sizeof buffer, |
| 494 | &bytes_read, &error); |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 495 | |
| 496 | terminal_data(terminal, buffer, bytes_read); |
| 497 | |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 498 | return TRUE; |
| 499 | } |
| 500 | |
| 501 | static int |
| 502 | terminal_run(struct terminal *terminal, const char *path) |
| 503 | { |
Kristian Høgsberg | f0c7b20 | 2008-12-12 13:39:03 -0500 | [diff] [blame] | 504 | int master; |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 505 | pid_t pid; |
| 506 | |
| 507 | pid = forkpty(&master, NULL, NULL, NULL); |
| 508 | if (pid == 0) { |
Kristian Høgsberg | c8c5d58 | 2008-12-18 14:50:08 -0500 | [diff] [blame] | 509 | setenv("TERM", "vt100", 1); |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 510 | if (execl(path, path, NULL)) { |
| 511 | printf("exec failed: %m\n"); |
| 512 | exit(EXIT_FAILURE); |
| 513 | } |
Kristian Høgsberg | f0c7b20 | 2008-12-12 13:39:03 -0500 | [diff] [blame] | 514 | } else if (pid < 0) { |
| 515 | fprintf(stderr, "failed to fork and create pty (%m).\n"); |
| 516 | return -1; |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 517 | } |
| 518 | |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 519 | terminal->master = master; |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 520 | terminal->channel = g_io_channel_unix_new(master); |
| 521 | fcntl(master, F_SETFL, O_NONBLOCK); |
| 522 | g_io_add_watch(terminal->channel, G_IO_IN, |
| 523 | io_handler, terminal); |
| 524 | |
| 525 | return 0; |
| 526 | } |
| 527 | |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 528 | static const GOptionEntry option_entries[] = { |
| 529 | { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, |
| 530 | &option_fullscreen, "Run in fullscreen mode" }, |
| 531 | { NULL } |
| 532 | }; |
| 533 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 534 | int main(int argc, char *argv[]) |
| 535 | { |
| 536 | struct wl_display *display; |
| 537 | int fd; |
| 538 | GMainLoop *loop; |
| 539 | GSource *source; |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 540 | struct display *d; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 541 | struct terminal *terminal; |
Kristian Høgsberg | 0395f30 | 2008-12-22 12:14:50 -0500 | [diff] [blame] | 542 | GOptionContext *context; |
| 543 | GError *error; |
| 544 | |
| 545 | context = g_option_context_new(NULL); |
| 546 | g_option_context_add_main_entries(context, option_entries, "Wayland Terminal"); |
| 547 | if (!g_option_context_parse(context, &argc, &argv, &error)) { |
| 548 | fprintf(stderr, "option parsing failed: %s\n", error->message); |
| 549 | exit(EXIT_FAILURE); |
| 550 | } |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 551 | |
| 552 | fd = open(gem_device, O_RDWR); |
| 553 | if (fd < 0) { |
| 554 | fprintf(stderr, "drm open failed: %m\n"); |
| 555 | return -1; |
| 556 | } |
| 557 | |
| 558 | display = wl_display_create(socket_name, sizeof socket_name); |
| 559 | if (display == NULL) { |
| 560 | fprintf(stderr, "failed to create display: %m\n"); |
| 561 | return -1; |
| 562 | } |
| 563 | |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 564 | d = display_create(display, fd); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 565 | loop = g_main_loop_new(NULL, FALSE); |
| 566 | source = wl_glib_source_new(display); |
| 567 | g_source_attach(source, NULL); |
| 568 | |
Kristian Høgsberg | 43c28ee | 2009-01-26 23:42:46 -0500 | [diff] [blame] | 569 | terminal = terminal_create(d, option_fullscreen); |
Kristian Høgsberg | f0c7b20 | 2008-12-12 13:39:03 -0500 | [diff] [blame] | 570 | if (terminal_run(terminal, "/bin/bash")) |
| 571 | exit(EXIT_FAILURE); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 572 | |
| 573 | g_main_loop_run(loop); |
| 574 | |
| 575 | return 0; |
| 576 | } |