Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008 Kristian Høgsberg |
| 3 | * |
| 4 | * Permission to use, copy, modify, distribute, and sell this software and its |
| 5 | * documentation for any purpose is hereby granted without fee, provided that |
| 6 | * the above copyright notice appear in all copies and that both that copyright |
| 7 | * notice and this permission notice appear in supporting documentation, and |
| 8 | * that the name of the copyright holders not be used in advertising or |
| 9 | * publicity pertaining to distribution of the software without specific, |
| 10 | * written prior permission. The copyright holders make no representations |
| 11 | * about the suitability of this software for any purpose. It is provided "as |
| 12 | * is" without express or implied warranty. |
| 13 | * |
| 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 15 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 16 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 17 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 18 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 19 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 20 | * OF THIS SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #include <stdint.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <unistd.h> |
| 29 | #include <math.h> |
| 30 | #include <time.h> |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 31 | #include <pty.h> |
Kristian Høgsberg | f04e838 | 2008-12-08 00:07:49 -0500 | [diff] [blame] | 32 | #include <ctype.h> |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 33 | #include <cairo.h> |
| 34 | #include <glib.h> |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 35 | #include <linux/input.h> |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 36 | |
| 37 | #include <GL/gl.h> |
| 38 | #include <eagle.h> |
| 39 | |
| 40 | #include "wayland-client.h" |
| 41 | #include "wayland-glib.h" |
| 42 | |
| 43 | #include "cairo-util.h" |
| 44 | #include "window.h" |
| 45 | |
| 46 | static const char gem_device[] = "/dev/dri/card0"; |
| 47 | static const char socket_name[] = "\0wayland"; |
| 48 | |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 49 | #define MOD_SHIFT 0x01 |
| 50 | #define MOD_ALT 0x02 |
| 51 | #define MOD_CTRL 0x04 |
| 52 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 53 | struct terminal { |
| 54 | struct window *window; |
| 55 | struct wl_display *display; |
| 56 | int resize_scheduled; |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 57 | char *data; |
Kristian Høgsberg | b29415e | 2008-12-08 00:16:39 -0500 | [diff] [blame] | 58 | int width, height, tail, row, column, total_rows; |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 59 | int fd, master; |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 60 | struct buffer *buffer; |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 61 | GIOChannel *channel; |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 62 | uint32_t modifiers; |
Kristian Høgsberg | f04e838 | 2008-12-08 00:07:49 -0500 | [diff] [blame] | 63 | int state; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 64 | }; |
| 65 | |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 66 | static void |
| 67 | terminal_draw_contents(struct terminal *terminal) |
| 68 | { |
| 69 | struct rectangle rectangle; |
| 70 | cairo_surface_t *surface; |
| 71 | cairo_t *cr; |
| 72 | cairo_font_extents_t extents; |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 73 | int i, line; |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 74 | |
| 75 | window_get_child_rectangle(terminal->window, &rectangle); |
| 76 | |
| 77 | surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
| 78 | rectangle.width, rectangle.height); |
| 79 | cr = cairo_create(surface); |
| 80 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 81 | cairo_set_source_rgba(cr, 0, 0, 0, 0.9); |
| 82 | cairo_paint(cr); |
| 83 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 84 | cairo_set_source_rgba(cr, 0, 0.5, 0, 1); |
| 85 | |
| 86 | cairo_select_font_face (cr, "mono", |
| 87 | CAIRO_FONT_SLANT_NORMAL, |
| 88 | CAIRO_FONT_WEIGHT_NORMAL); |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 89 | cairo_set_font_size(cr, 14); |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 90 | |
| 91 | cairo_font_extents(cr, &extents); |
| 92 | for (i = 0; i < terminal->height; i++) { |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 93 | line = (terminal->tail + i) % terminal->height; |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 94 | cairo_move_to(cr, 0, extents.ascent + extents.height * i); |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 95 | cairo_show_text(cr, &terminal->data[line * (terminal->width + 1)]); |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 96 | } |
| 97 | cairo_destroy(cr); |
| 98 | |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 99 | terminal->buffer = buffer_create_from_cairo_surface(terminal->fd, surface); |
| 100 | cairo_surface_destroy(surface); |
| 101 | |
| 102 | window_copy(terminal->window, |
| 103 | &rectangle, |
| 104 | terminal->buffer->name, terminal->buffer->stride); |
| 105 | } |
| 106 | |
| 107 | static void |
| 108 | terminal_draw(struct terminal *terminal) |
| 109 | { |
| 110 | window_draw(terminal->window); |
| 111 | terminal_draw_contents(terminal); |
| 112 | wl_display_commit(terminal->display, 0); |
| 113 | } |
| 114 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 115 | static gboolean |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 116 | idle_redraw(void *data) |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 117 | { |
| 118 | struct terminal *terminal = data; |
| 119 | |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 120 | terminal_draw(terminal); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 121 | |
| 122 | return FALSE; |
| 123 | } |
| 124 | |
Kristian Høgsberg | f04e838 | 2008-12-08 00:07:49 -0500 | [diff] [blame] | 125 | #define STATE_NORMAL 0 |
| 126 | #define STATE_SKIP_TO_ALPHA 1 |
| 127 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 128 | static void |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 129 | terminal_data(struct terminal *terminal, const char *data, size_t length) |
| 130 | { |
| 131 | int i; |
| 132 | char *row; |
| 133 | |
| 134 | for (i = 0; i < length; i++) { |
| 135 | row = &terminal->data[terminal->row * (terminal->width + 1)]; |
Kristian Høgsberg | f04e838 | 2008-12-08 00:07:49 -0500 | [diff] [blame] | 136 | |
| 137 | if (terminal->state == STATE_SKIP_TO_ALPHA) { |
| 138 | if (isalpha(data[i])) |
| 139 | terminal->state = STATE_NORMAL; |
| 140 | continue; |
| 141 | } |
| 142 | |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 143 | switch (data[i]) { |
| 144 | case '\r': |
| 145 | terminal->column = 0; |
| 146 | break; |
| 147 | case '\n': |
| 148 | terminal->row++; |
Kristian Høgsberg | b29415e | 2008-12-08 00:16:39 -0500 | [diff] [blame] | 149 | terminal->total_rows++; |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 150 | terminal->column = 0; |
| 151 | if (terminal->row == terminal->height) |
| 152 | terminal->row = 0; |
Kristian Høgsberg | b29415e | 2008-12-08 00:16:39 -0500 | [diff] [blame] | 153 | if (terminal->row == terminal->tail && terminal->total_rows > 0) { |
| 154 | memset(&terminal->data[terminal->row * (terminal->width + 1)], |
| 155 | 0, terminal->width); |
| 156 | terminal->tail++; |
| 157 | } |
| 158 | if (terminal->tail == terminal->height) |
| 159 | terminal->tail = 0; |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 160 | break; |
| 161 | case '\t': |
| 162 | memset(&row[terminal->column], ' ', -terminal->column & 7); |
| 163 | terminal->column = (terminal->column + 7) & ~7; |
| 164 | break; |
Kristian Høgsberg | f04e838 | 2008-12-08 00:07:49 -0500 | [diff] [blame] | 165 | case '\e': |
| 166 | terminal->state = STATE_SKIP_TO_ALPHA; |
| 167 | break; |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 168 | default: |
| 169 | if (terminal->column < terminal->width) |
| 170 | row[terminal->column++] = data[i]; |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 176 | static void |
| 177 | resize_handler(struct window *window, int32_t width, int32_t height, void *data) |
| 178 | { |
| 179 | struct terminal *terminal = data; |
| 180 | |
| 181 | if (!terminal->resize_scheduled) { |
| 182 | g_idle_add(idle_redraw, terminal); |
| 183 | terminal->resize_scheduled = 1; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | static void |
| 188 | acknowledge_handler(struct window *window, uint32_t key, void *data) |
| 189 | { |
| 190 | struct terminal *terminal = data; |
| 191 | |
| 192 | terminal->resize_scheduled = 0; |
Kristian Høgsberg | c47303f | 2008-12-08 09:57:08 -0500 | [diff] [blame^] | 193 | buffer_destroy(terminal->buffer, terminal->fd); |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | struct key { |
Kristian Høgsberg | 0d77fd4 | 2008-12-08 00:23:55 -0500 | [diff] [blame] | 197 | int code[4]; |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 198 | } evdev_keymap[] = { |
| 199 | { { 0, 0 } }, /* 0 */ |
| 200 | { { 0x1b, 0x1b } }, |
| 201 | { { '1', '!' } }, |
| 202 | { { '2', '@' } }, |
| 203 | { { '3', '#' } }, |
| 204 | { { '4', '$' } }, |
| 205 | { { '5', '%' } }, |
| 206 | { { '6', '^' } }, |
| 207 | { { '7', '&' } }, |
| 208 | { { '8', '*' } }, |
| 209 | { { '9', '(' } }, |
| 210 | { { '0', ')' } }, |
| 211 | { { '-', '_' } }, |
| 212 | { { '=', '+' } }, |
| 213 | { { '\b', '\b' } }, |
| 214 | { { '\t', '\t' } }, |
| 215 | |
| 216 | { { 'q', 'Q' } }, /* 16 */ |
Kristian Høgsberg | 0d77fd4 | 2008-12-08 00:23:55 -0500 | [diff] [blame] | 217 | { { 'w', 'W', 0x17 } }, |
| 218 | { { 'e', 'E', 0x05 } }, |
| 219 | { { 'r', 'R', 0x12 } }, |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 220 | { { 't', 'T' } }, |
| 221 | { { 'y', 'Y' } }, |
Kristian Høgsberg | 0d77fd4 | 2008-12-08 00:23:55 -0500 | [diff] [blame] | 222 | { { 'u', 'U', 0x15 } }, |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 223 | { { 'i', 'I' } }, |
| 224 | { { 'o', 'O' } }, |
Kristian Høgsberg | 0d77fd4 | 2008-12-08 00:23:55 -0500 | [diff] [blame] | 225 | { { 'p', 'P', 0x10 } }, |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 226 | { { '[', '{' } }, |
| 227 | { { ']', '}' } }, |
| 228 | { { '\n', '\n' } }, |
| 229 | { { 0, 0 } }, |
Kristian Høgsberg | 0d77fd4 | 2008-12-08 00:23:55 -0500 | [diff] [blame] | 230 | { { 'a', 'A', 0x01} }, |
| 231 | { { 's', 'S', 0x13 } }, |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 232 | |
Kristian Høgsberg | 0d77fd4 | 2008-12-08 00:23:55 -0500 | [diff] [blame] | 233 | { { 'd', 'D', 0x04 } }, /* 32 */ |
| 234 | { { 'f', 'F', 0x06 } }, |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 235 | { { 'g', 'G' } }, |
| 236 | { { 'h', 'H' } }, |
Kristian Høgsberg | 0d77fd4 | 2008-12-08 00:23:55 -0500 | [diff] [blame] | 237 | { { 'j', 'J', 0x0a } }, |
| 238 | { { 'k', 'K', 0x0b } }, |
| 239 | { { 'l', 'L', 0x0c } }, |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 240 | { { ';', ':' } }, |
| 241 | { { '\'', '"' } }, |
| 242 | { { '`', '~' } }, |
| 243 | { { 0, 0 } }, |
| 244 | { { '\\', '|' } }, |
| 245 | { { 'z', 'Z' } }, |
| 246 | { { 'x', 'X' } }, |
| 247 | { { 'c', 'C' } }, |
| 248 | { { 'v', 'V' } }, |
| 249 | |
Kristian Høgsberg | 0d77fd4 | 2008-12-08 00:23:55 -0500 | [diff] [blame] | 250 | { { 'b', 'B', 0x02 } }, /* 48 */ |
| 251 | { { 'n', 'N', 0x0e } }, |
| 252 | { { 'm', 'M', 0x0d } }, |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 253 | { { ',', '<' } }, |
| 254 | { { '.', '>' } }, |
| 255 | { { '/', '?' } }, |
| 256 | { { 0, 0 } }, |
| 257 | { { '*', '*' } }, |
| 258 | { { 0, 0 } }, |
| 259 | { { ' ', ' ' } }, |
| 260 | { { 0, 0 } } |
| 261 | |
| 262 | /* 59 */ |
| 263 | }; |
| 264 | |
| 265 | #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0]) |
| 266 | |
| 267 | static void |
| 268 | key_handler(struct window *window, uint32_t key, uint32_t state, void *data) |
| 269 | { |
| 270 | struct terminal *terminal = data; |
| 271 | uint32_t mod = 0; |
| 272 | char c; |
| 273 | |
| 274 | switch (key) { |
| 275 | case KEY_LEFTSHIFT: |
| 276 | case KEY_RIGHTSHIFT: |
| 277 | mod = MOD_SHIFT; |
| 278 | break; |
| 279 | case KEY_LEFTCTRL: |
| 280 | case KEY_RIGHTCTRL: |
| 281 | mod = MOD_CTRL; |
| 282 | break; |
| 283 | case KEY_LEFTALT: |
| 284 | case KEY_RIGHTALT: |
| 285 | mod = MOD_ALT; |
| 286 | break; |
| 287 | default: |
| 288 | if (key < ARRAY_LENGTH(evdev_keymap)) { |
Kristian Høgsberg | 0d77fd4 | 2008-12-08 00:23:55 -0500 | [diff] [blame] | 289 | if (terminal->modifiers & MOD_CTRL) |
| 290 | c = evdev_keymap[key].code[2]; |
| 291 | else if (terminal->modifiers & MOD_SHIFT) |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 292 | c = evdev_keymap[key].code[1]; |
| 293 | else |
| 294 | c = evdev_keymap[key].code[0]; |
| 295 | if (state && c) |
| 296 | write(terminal->master, &c, 1); |
| 297 | } |
| 298 | break; |
| 299 | } |
| 300 | |
| 301 | if (state) |
| 302 | terminal->modifiers |= mod; |
| 303 | else |
| 304 | terminal->modifiers &= ~mod; |
| 305 | } |
| 306 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 307 | static struct terminal * |
| 308 | terminal_create(struct wl_display *display, int fd) |
| 309 | { |
| 310 | struct terminal *terminal; |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 311 | int size; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 312 | |
| 313 | terminal = malloc(sizeof *terminal); |
| 314 | if (terminal == NULL) |
| 315 | return terminal; |
| 316 | |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 317 | memset(terminal, 0, sizeof *terminal); |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 318 | terminal->fd = fd; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 319 | terminal->window = window_create(display, fd, "Wayland Terminal", |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 320 | 500, 100, 500, 400); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 321 | terminal->display = display; |
| 322 | terminal->resize_scheduled = 1; |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 323 | terminal->width = 80; |
| 324 | terminal->height = 25; |
| 325 | size = (terminal->width + 1) * terminal->height; |
| 326 | terminal->data = malloc(size); |
| 327 | memset(terminal->data, 0, size); |
| 328 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 329 | window_set_resize_handler(terminal->window, resize_handler, terminal); |
| 330 | window_set_acknowledge_handler(terminal->window, acknowledge_handler, terminal); |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 331 | window_set_key_handler(terminal->window, key_handler, terminal); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 332 | |
| 333 | return terminal; |
| 334 | } |
| 335 | |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 336 | static gboolean |
| 337 | io_handler(GIOChannel *source, |
| 338 | GIOCondition condition, |
| 339 | gpointer data) |
| 340 | { |
| 341 | struct terminal *terminal = data; |
| 342 | gchar buffer[256]; |
| 343 | gsize bytes_read; |
| 344 | GError *error = NULL; |
| 345 | |
| 346 | g_io_channel_read_chars(source, buffer, sizeof buffer, |
| 347 | &bytes_read, &error); |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 348 | |
| 349 | terminal_data(terminal, buffer, bytes_read); |
| 350 | |
| 351 | if (!terminal->resize_scheduled) { |
| 352 | g_idle_add(idle_redraw, terminal); |
| 353 | terminal->resize_scheduled = 1; |
| 354 | } |
| 355 | |
| 356 | return TRUE; |
| 357 | } |
| 358 | |
| 359 | static int |
| 360 | terminal_run(struct terminal *terminal, const char *path) |
| 361 | { |
| 362 | int master, slave; |
| 363 | pid_t pid; |
| 364 | |
| 365 | pid = forkpty(&master, NULL, NULL, NULL); |
| 366 | if (pid == 0) { |
| 367 | close(master); |
| 368 | if (execl(path, path, NULL)) { |
| 369 | printf("exec failed: %m\n"); |
| 370 | exit(EXIT_FAILURE); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | close(slave); |
Kristian Høgsberg | 6e83d58 | 2008-12-08 00:01:36 -0500 | [diff] [blame] | 375 | terminal->master = master; |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 376 | terminal->channel = g_io_channel_unix_new(master); |
| 377 | fcntl(master, F_SETFL, O_NONBLOCK); |
| 378 | g_io_add_watch(terminal->channel, G_IO_IN, |
| 379 | io_handler, terminal); |
| 380 | |
| 381 | return 0; |
| 382 | } |
| 383 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 384 | int main(int argc, char *argv[]) |
| 385 | { |
| 386 | struct wl_display *display; |
| 387 | int fd; |
| 388 | GMainLoop *loop; |
| 389 | GSource *source; |
| 390 | struct terminal *terminal; |
| 391 | |
| 392 | fd = open(gem_device, O_RDWR); |
| 393 | if (fd < 0) { |
| 394 | fprintf(stderr, "drm open failed: %m\n"); |
| 395 | return -1; |
| 396 | } |
| 397 | |
| 398 | display = wl_display_create(socket_name, sizeof socket_name); |
| 399 | if (display == NULL) { |
| 400 | fprintf(stderr, "failed to create display: %m\n"); |
| 401 | return -1; |
| 402 | } |
| 403 | |
| 404 | loop = g_main_loop_new(NULL, FALSE); |
| 405 | source = wl_glib_source_new(display); |
| 406 | g_source_attach(source, NULL); |
| 407 | |
| 408 | terminal = terminal_create(display, fd); |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame] | 409 | terminal_run(terminal, "/bin/bash"); |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 410 | terminal_draw(terminal); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 411 | |
| 412 | g_main_loop_run(loop); |
| 413 | |
| 414 | return 0; |
| 415 | } |