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