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 | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 32 | #include <cairo.h> |
| 33 | #include <glib.h> |
| 34 | |
| 35 | #include <GL/gl.h> |
| 36 | #include <eagle.h> |
| 37 | |
| 38 | #include "wayland-client.h" |
| 39 | #include "wayland-glib.h" |
| 40 | |
| 41 | #include "cairo-util.h" |
| 42 | #include "window.h" |
| 43 | |
| 44 | static const char gem_device[] = "/dev/dri/card0"; |
| 45 | static const char socket_name[] = "\0wayland"; |
| 46 | |
| 47 | struct terminal { |
| 48 | struct window *window; |
| 49 | struct wl_display *display; |
| 50 | int resize_scheduled; |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 51 | char *data; |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame^] | 52 | int width, height, tail, row, column; |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 53 | int fd; |
| 54 | struct buffer *buffer; |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame^] | 55 | GIOChannel *channel; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 56 | }; |
| 57 | |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 58 | static void |
| 59 | terminal_draw_contents(struct terminal *terminal) |
| 60 | { |
| 61 | struct rectangle rectangle; |
| 62 | cairo_surface_t *surface; |
| 63 | cairo_t *cr; |
| 64 | cairo_font_extents_t extents; |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame^] | 65 | int i, line; |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 66 | |
| 67 | window_get_child_rectangle(terminal->window, &rectangle); |
| 68 | |
| 69 | surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
| 70 | rectangle.width, rectangle.height); |
| 71 | cr = cairo_create(surface); |
| 72 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 73 | cairo_set_source_rgba(cr, 0, 0, 0, 0.9); |
| 74 | cairo_paint(cr); |
| 75 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 76 | cairo_set_source_rgba(cr, 0, 0.5, 0, 1); |
| 77 | |
| 78 | cairo_select_font_face (cr, "mono", |
| 79 | CAIRO_FONT_SLANT_NORMAL, |
| 80 | CAIRO_FONT_WEIGHT_NORMAL); |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame^] | 81 | cairo_set_font_size(cr, 14); |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 82 | |
| 83 | cairo_font_extents(cr, &extents); |
| 84 | for (i = 0; i < terminal->height; i++) { |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame^] | 85 | line = (terminal->tail + i) % terminal->height; |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 86 | cairo_move_to(cr, 0, extents.ascent + extents.height * i); |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame^] | 87 | cairo_show_text(cr, &terminal->data[line * (terminal->width + 1)]); |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 88 | } |
| 89 | cairo_destroy(cr); |
| 90 | |
| 91 | if (terminal->buffer != NULL) |
| 92 | buffer_destroy(terminal->buffer, terminal->fd); |
| 93 | |
| 94 | terminal->buffer = buffer_create_from_cairo_surface(terminal->fd, surface); |
| 95 | cairo_surface_destroy(surface); |
| 96 | |
| 97 | window_copy(terminal->window, |
| 98 | &rectangle, |
| 99 | terminal->buffer->name, terminal->buffer->stride); |
| 100 | } |
| 101 | |
| 102 | static void |
| 103 | terminal_draw(struct terminal *terminal) |
| 104 | { |
| 105 | window_draw(terminal->window); |
| 106 | terminal_draw_contents(terminal); |
| 107 | wl_display_commit(terminal->display, 0); |
| 108 | } |
| 109 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 110 | static gboolean |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 111 | idle_redraw(void *data) |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 112 | { |
| 113 | struct terminal *terminal = data; |
| 114 | |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 115 | terminal_draw(terminal); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 116 | |
| 117 | return FALSE; |
| 118 | } |
| 119 | |
| 120 | static void |
| 121 | resize_handler(struct window *window, int32_t width, int32_t height, void *data) |
| 122 | { |
| 123 | struct terminal *terminal = data; |
| 124 | |
| 125 | if (!terminal->resize_scheduled) { |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 126 | g_idle_add(idle_redraw, terminal); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 127 | terminal->resize_scheduled = 1; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | static void |
| 132 | acknowledge_handler(struct window *window, uint32_t key, void *data) |
| 133 | { |
| 134 | struct terminal *terminal = data; |
| 135 | |
| 136 | terminal->resize_scheduled = 0; |
| 137 | } |
| 138 | |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame^] | 139 | static void |
| 140 | terminal_data(struct terminal *terminal, const char *data, size_t length) |
| 141 | { |
| 142 | int i; |
| 143 | char *row; |
| 144 | |
| 145 | for (i = 0; i < length; i++) { |
| 146 | row = &terminal->data[terminal->row * (terminal->width + 1)]; |
| 147 | switch (data[i]) { |
| 148 | case '\r': |
| 149 | terminal->column = 0; |
| 150 | break; |
| 151 | case '\n': |
| 152 | terminal->row++; |
| 153 | terminal->column = 0; |
| 154 | if (terminal->row == terminal->height) |
| 155 | terminal->row = 0; |
| 156 | break; |
| 157 | case '\t': |
| 158 | memset(&row[terminal->column], ' ', -terminal->column & 7); |
| 159 | terminal->column = (terminal->column + 7) & ~7; |
| 160 | break; |
| 161 | default: |
| 162 | if (terminal->column < terminal->width) |
| 163 | row[terminal->column++] = data[i]; |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 169 | static struct terminal * |
| 170 | terminal_create(struct wl_display *display, int fd) |
| 171 | { |
| 172 | struct terminal *terminal; |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame^] | 173 | int size; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 174 | |
| 175 | terminal = malloc(sizeof *terminal); |
| 176 | if (terminal == NULL) |
| 177 | return terminal; |
| 178 | |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame^] | 179 | memset(terminal, 0, sizeof *terminal); |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 180 | terminal->fd = fd; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 181 | terminal->window = window_create(display, fd, "Wayland Terminal", |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 182 | 500, 100, 500, 400); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 183 | terminal->display = display; |
| 184 | terminal->resize_scheduled = 1; |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 185 | terminal->width = 80; |
| 186 | terminal->height = 25; |
| 187 | size = (terminal->width + 1) * terminal->height; |
| 188 | terminal->data = malloc(size); |
| 189 | memset(terminal->data, 0, size); |
| 190 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 191 | window_set_resize_handler(terminal->window, resize_handler, terminal); |
| 192 | window_set_acknowledge_handler(terminal->window, acknowledge_handler, terminal); |
| 193 | |
| 194 | return terminal; |
| 195 | } |
| 196 | |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame^] | 197 | static gboolean |
| 198 | io_handler(GIOChannel *source, |
| 199 | GIOCondition condition, |
| 200 | gpointer data) |
| 201 | { |
| 202 | struct terminal *terminal = data; |
| 203 | gchar buffer[256]; |
| 204 | gsize bytes_read; |
| 205 | GError *error = NULL; |
| 206 | |
| 207 | g_io_channel_read_chars(source, buffer, sizeof buffer, |
| 208 | &bytes_read, &error); |
| 209 | printf("got data: %.*s\n", bytes_read, buffer); |
| 210 | |
| 211 | terminal_data(terminal, buffer, bytes_read); |
| 212 | |
| 213 | if (!terminal->resize_scheduled) { |
| 214 | g_idle_add(idle_redraw, terminal); |
| 215 | terminal->resize_scheduled = 1; |
| 216 | } |
| 217 | |
| 218 | return TRUE; |
| 219 | } |
| 220 | |
| 221 | static int |
| 222 | terminal_run(struct terminal *terminal, const char *path) |
| 223 | { |
| 224 | int master, slave; |
| 225 | pid_t pid; |
| 226 | |
| 227 | pid = forkpty(&master, NULL, NULL, NULL); |
| 228 | if (pid == 0) { |
| 229 | close(master); |
| 230 | if (execl(path, path, NULL)) { |
| 231 | printf("exec failed: %m\n"); |
| 232 | exit(EXIT_FAILURE); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | close(slave); |
| 237 | terminal->channel = g_io_channel_unix_new(master); |
| 238 | fcntl(master, F_SETFL, O_NONBLOCK); |
| 239 | g_io_add_watch(terminal->channel, G_IO_IN, |
| 240 | io_handler, terminal); |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 245 | int main(int argc, char *argv[]) |
| 246 | { |
| 247 | struct wl_display *display; |
| 248 | int fd; |
| 249 | GMainLoop *loop; |
| 250 | GSource *source; |
| 251 | struct terminal *terminal; |
| 252 | |
| 253 | fd = open(gem_device, O_RDWR); |
| 254 | if (fd < 0) { |
| 255 | fprintf(stderr, "drm open failed: %m\n"); |
| 256 | return -1; |
| 257 | } |
| 258 | |
| 259 | display = wl_display_create(socket_name, sizeof socket_name); |
| 260 | if (display == NULL) { |
| 261 | fprintf(stderr, "failed to create display: %m\n"); |
| 262 | return -1; |
| 263 | } |
| 264 | |
| 265 | loop = g_main_loop_new(NULL, FALSE); |
| 266 | source = wl_glib_source_new(display); |
| 267 | g_source_attach(source, NULL); |
| 268 | |
| 269 | terminal = terminal_create(display, fd); |
Kristian Høgsberg | 269d6e3 | 2008-12-07 23:17:31 -0500 | [diff] [blame^] | 270 | terminal_run(terminal, "/bin/bash"); |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame] | 271 | terminal_draw(terminal); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 272 | |
| 273 | g_main_loop_run(loop); |
| 274 | |
| 275 | return 0; |
| 276 | } |