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> |
| 31 | #include <cairo.h> |
| 32 | #include <glib.h> |
| 33 | |
| 34 | #include <GL/gl.h> |
| 35 | #include <eagle.h> |
| 36 | |
| 37 | #include "wayland-client.h" |
| 38 | #include "wayland-glib.h" |
| 39 | |
| 40 | #include "cairo-util.h" |
| 41 | #include "window.h" |
| 42 | |
| 43 | static const char gem_device[] = "/dev/dri/card0"; |
| 44 | static const char socket_name[] = "\0wayland"; |
| 45 | |
| 46 | struct terminal { |
| 47 | struct window *window; |
| 48 | struct wl_display *display; |
| 49 | int resize_scheduled; |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame^] | 50 | char *data; |
| 51 | int width, height; |
| 52 | int fd; |
| 53 | struct buffer *buffer; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 54 | }; |
| 55 | |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame^] | 56 | static void |
| 57 | terminal_draw_contents(struct terminal *terminal) |
| 58 | { |
| 59 | struct rectangle rectangle; |
| 60 | cairo_surface_t *surface; |
| 61 | cairo_t *cr; |
| 62 | cairo_font_extents_t extents; |
| 63 | int i; |
| 64 | |
| 65 | window_get_child_rectangle(terminal->window, &rectangle); |
| 66 | |
| 67 | surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
| 68 | rectangle.width, rectangle.height); |
| 69 | cr = cairo_create(surface); |
| 70 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 71 | cairo_set_source_rgba(cr, 0, 0, 0, 0.9); |
| 72 | cairo_paint(cr); |
| 73 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 74 | cairo_set_source_rgba(cr, 0, 0.5, 0, 1); |
| 75 | |
| 76 | cairo_select_font_face (cr, "mono", |
| 77 | CAIRO_FONT_SLANT_NORMAL, |
| 78 | CAIRO_FONT_WEIGHT_NORMAL); |
| 79 | |
| 80 | cairo_font_extents(cr, &extents); |
| 81 | for (i = 0; i < terminal->height; i++) { |
| 82 | cairo_move_to(cr, 0, extents.ascent + extents.height * i); |
| 83 | cairo_show_text(cr, &terminal->data[i * (terminal->width + 1)]); |
| 84 | } |
| 85 | cairo_destroy(cr); |
| 86 | |
| 87 | if (terminal->buffer != NULL) |
| 88 | buffer_destroy(terminal->buffer, terminal->fd); |
| 89 | |
| 90 | terminal->buffer = buffer_create_from_cairo_surface(terminal->fd, surface); |
| 91 | cairo_surface_destroy(surface); |
| 92 | |
| 93 | window_copy(terminal->window, |
| 94 | &rectangle, |
| 95 | terminal->buffer->name, terminal->buffer->stride); |
| 96 | } |
| 97 | |
| 98 | static void |
| 99 | terminal_draw(struct terminal *terminal) |
| 100 | { |
| 101 | window_draw(terminal->window); |
| 102 | terminal_draw_contents(terminal); |
| 103 | wl_display_commit(terminal->display, 0); |
| 104 | } |
| 105 | |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 106 | static gboolean |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame^] | 107 | idle_redraw(void *data) |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 108 | { |
| 109 | struct terminal *terminal = data; |
| 110 | |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame^] | 111 | terminal_draw(terminal); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 112 | |
| 113 | return FALSE; |
| 114 | } |
| 115 | |
| 116 | static void |
| 117 | resize_handler(struct window *window, int32_t width, int32_t height, void *data) |
| 118 | { |
| 119 | struct terminal *terminal = data; |
| 120 | |
| 121 | if (!terminal->resize_scheduled) { |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame^] | 122 | g_idle_add(idle_redraw, terminal); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 123 | terminal->resize_scheduled = 1; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | static void |
| 128 | acknowledge_handler(struct window *window, uint32_t key, void *data) |
| 129 | { |
| 130 | struct terminal *terminal = data; |
| 131 | |
| 132 | terminal->resize_scheduled = 0; |
| 133 | } |
| 134 | |
| 135 | static struct terminal * |
| 136 | terminal_create(struct wl_display *display, int fd) |
| 137 | { |
| 138 | struct terminal *terminal; |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame^] | 139 | int size, i; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 140 | |
| 141 | terminal = malloc(sizeof *terminal); |
| 142 | if (terminal == NULL) |
| 143 | return terminal; |
| 144 | |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame^] | 145 | terminal->fd = fd; |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 146 | terminal->window = window_create(display, fd, "Wayland Terminal", |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame^] | 147 | 500, 100, 500, 400); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 148 | terminal->display = display; |
| 149 | terminal->resize_scheduled = 1; |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame^] | 150 | terminal->width = 80; |
| 151 | terminal->height = 25; |
| 152 | size = (terminal->width + 1) * terminal->height; |
| 153 | terminal->data = malloc(size); |
| 154 | memset(terminal->data, 0, size); |
| 155 | |
| 156 | for (i = 0; i < terminal->height; i++) { |
| 157 | snprintf(&terminal->data[i * (terminal->width + 1)], terminal->width, |
| 158 | "hello world, line %d", i); |
| 159 | } |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 160 | |
| 161 | window_set_resize_handler(terminal->window, resize_handler, terminal); |
| 162 | window_set_acknowledge_handler(terminal->window, acknowledge_handler, terminal); |
| 163 | |
| 164 | return terminal; |
| 165 | } |
| 166 | |
| 167 | int main(int argc, char *argv[]) |
| 168 | { |
| 169 | struct wl_display *display; |
| 170 | int fd; |
| 171 | GMainLoop *loop; |
| 172 | GSource *source; |
| 173 | struct terminal *terminal; |
| 174 | |
| 175 | fd = open(gem_device, O_RDWR); |
| 176 | if (fd < 0) { |
| 177 | fprintf(stderr, "drm open failed: %m\n"); |
| 178 | return -1; |
| 179 | } |
| 180 | |
| 181 | display = wl_display_create(socket_name, sizeof socket_name); |
| 182 | if (display == NULL) { |
| 183 | fprintf(stderr, "failed to create display: %m\n"); |
| 184 | return -1; |
| 185 | } |
| 186 | |
| 187 | loop = g_main_loop_new(NULL, FALSE); |
| 188 | source = wl_glib_source_new(display); |
| 189 | g_source_attach(source, NULL); |
| 190 | |
| 191 | terminal = terminal_create(display, fd); |
Kristian Høgsberg | 44e3c5e | 2008-12-07 21:51:58 -0500 | [diff] [blame^] | 192 | terminal_draw(terminal); |
Kristian Høgsberg | 0c4457f | 2008-12-07 19:59:11 -0500 | [diff] [blame] | 193 | |
| 194 | g_main_loop_run(loop); |
| 195 | |
| 196 | return 0; |
| 197 | } |