Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 1 | #include <stdint.h> |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include <i915_drm.h> |
| 6 | #include <sys/ioctl.h> |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 7 | #include <fcntl.h> |
| 8 | #include <unistd.h> |
| 9 | #include <math.h> |
| 10 | #include <time.h> |
| 11 | #include <cairo.h> |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 12 | #include <glib.h> |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 13 | |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 14 | #include <GL/gl.h> |
| 15 | #include <eagle.h> |
| 16 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 17 | #include "wayland-client.h" |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 18 | #include "wayland-glib.h" |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 19 | #include "gears.h" |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 20 | |
| 21 | static const char gem_device[] = "/dev/dri/card0"; |
| 22 | static const char socket_name[] = "\0wayland"; |
| 23 | |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 24 | static void die(const char *msg) |
| 25 | { |
| 26 | fprintf(stderr, "%s", msg); |
| 27 | exit(EXIT_FAILURE); |
| 28 | } |
| 29 | |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 30 | struct buffer { |
| 31 | int width, height, stride; |
| 32 | uint32_t name, handle; |
| 33 | }; |
| 34 | |
| 35 | static struct buffer * |
| 36 | buffer_create(int fd, int width, int height, int stride) |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 37 | { |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 38 | struct buffer *buffer; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 39 | struct drm_i915_gem_create create; |
| 40 | struct drm_gem_flink flink; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 41 | |
| 42 | buffer = malloc(sizeof *buffer); |
| 43 | buffer->width = width; |
| 44 | buffer->height = height; |
| 45 | buffer->stride = stride; |
| 46 | |
| 47 | memset(&create, 0, sizeof(create)); |
| 48 | create.size = height * stride; |
| 49 | |
| 50 | if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) { |
| 51 | fprintf(stderr, "gem create failed: %m\n"); |
| 52 | free(buffer); |
| 53 | return NULL; |
| 54 | } |
| 55 | |
| 56 | flink.handle = create.handle; |
| 57 | if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) { |
| 58 | fprintf(stderr, "gem flink failed: %m\n"); |
| 59 | free(buffer); |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | buffer->handle = flink.handle; |
| 64 | buffer->name = flink.name; |
| 65 | |
| 66 | return buffer; |
| 67 | } |
| 68 | |
| 69 | static int |
| 70 | buffer_destroy(struct buffer *buffer, int fd) |
| 71 | { |
| 72 | struct drm_gem_close close; |
| 73 | |
| 74 | close.handle = buffer->handle; |
| 75 | if (ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close) < 0) { |
| 76 | fprintf(stderr, "gem close failed: %m\n"); |
| 77 | return -1; |
| 78 | } |
| 79 | |
| 80 | free(buffer); |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static int |
| 86 | buffer_data(struct buffer *buffer, int fd, void *data) |
| 87 | { |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 88 | struct drm_i915_gem_pwrite pwrite; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 89 | |
| 90 | pwrite.handle = buffer->handle; |
| 91 | pwrite.offset = 0; |
| 92 | pwrite.size = buffer->height * buffer->stride; |
| 93 | pwrite.data_ptr = (uint64_t) (uintptr_t) data; |
| 94 | |
| 95 | if (ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite) < 0) { |
| 96 | fprintf(stderr, "gem pwrite failed: %m\n"); |
| 97 | return -1; |
| 98 | } |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static struct buffer * |
| 104 | buffer_create_from_cairo_surface(int fd, cairo_surface_t *surface) |
| 105 | { |
| 106 | struct buffer *buffer; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 107 | int32_t width, height, stride; |
| 108 | void *data; |
| 109 | |
| 110 | width = cairo_image_surface_get_width(surface); |
| 111 | height = cairo_image_surface_get_height(surface); |
| 112 | stride = cairo_image_surface_get_stride(surface); |
| 113 | data = cairo_image_surface_get_data(surface); |
| 114 | |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 115 | buffer = buffer_create(fd, width, height, stride); |
| 116 | if (buffer == NULL) |
| 117 | return NULL; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 118 | |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 119 | if (buffer_data(buffer, fd, data) < 0) { |
| 120 | buffer_destroy(buffer, fd); |
| 121 | return NULL; |
| 122 | } |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 123 | |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 124 | return buffer; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | struct window { |
| 128 | struct wl_surface *surface; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 129 | int x, y, width, height; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 130 | int drag_x, drag_y, last_x, last_y; |
| 131 | int state; |
| 132 | uint32_t name; |
| 133 | int fd; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 134 | int redraw_scheduled; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 135 | cairo_pattern_t *background; |
| 136 | |
| 137 | struct buffer *buffer; |
| 138 | struct buffer *egl_buffer; |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 139 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 140 | GLfloat gears_angle; |
| 141 | struct gears *gears; |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 142 | EGLDisplay display; |
| 143 | EGLContext context; |
| 144 | EGLConfig config; |
| 145 | EGLSurface egl_surface; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 146 | }; |
| 147 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame^] | 148 | static void |
| 149 | rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius) |
| 150 | { |
| 151 | cairo_move_to(cr, x0, y0 + radius); |
| 152 | cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2); |
| 153 | cairo_line_to(cr, x1 - radius, y0); |
| 154 | cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI); |
| 155 | cairo_line_to(cr, x1, y1 - radius); |
| 156 | cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2); |
| 157 | cairo_line_to(cr, x0 + radius, y1); |
| 158 | cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI); |
| 159 | cairo_close_path(cr); |
| 160 | } |
| 161 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 162 | static gboolean |
| 163 | draw_window(void *data) |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 164 | { |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 165 | struct window *window = data; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 166 | cairo_surface_t *surface; |
| 167 | cairo_t *cr; |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame^] | 168 | int border = 2, radius = 5; |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 169 | cairo_text_extents_t extents; |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame^] | 170 | cairo_pattern_t *gradient, *outline, *bright, *dim; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 171 | struct buffer *buffer; |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 172 | const static char title[] = "Wayland First Post"; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 173 | |
| 174 | surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 175 | window->width, window->height); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 176 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame^] | 177 | outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1); |
| 178 | bright = cairo_pattern_create_rgb(0.6, 0.6, 0.6); |
| 179 | dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4); |
| 180 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 181 | cr = cairo_create(surface); |
| 182 | cairo_set_line_width (cr, border); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame^] | 183 | rounded_rect(cr, 1, 1, window->width - 1, window->height - 1, radius); |
| 184 | cairo_set_source(cr, outline); |
| 185 | cairo_stroke(cr); |
| 186 | rounded_rect(cr, 2, 2, window->width - 2, window->height - 2, radius); |
| 187 | cairo_set_source(cr, bright); |
| 188 | cairo_stroke(cr); |
| 189 | rounded_rect(cr, 3, 3, window->width - 2, window->height - 2, radius); |
| 190 | cairo_set_source(cr, dim); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 191 | cairo_stroke(cr); |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 192 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame^] | 193 | rounded_rect(cr, 2, 2, window->width - 2, window->height - 2, radius); |
| 194 | gradient = cairo_pattern_create_linear (0, 0, 0, window->height); |
| 195 | cairo_pattern_add_color_stop_rgb(gradient, 0, 0.4, 0.4, 0.4); |
| 196 | cairo_pattern_add_color_stop_rgb(gradient, 0.2, 0.7, 0.7, 0.7); |
| 197 | cairo_set_source(cr, gradient); |
| 198 | cairo_fill(cr); |
| 199 | cairo_pattern_destroy(gradient); |
| 200 | |
| 201 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 202 | cairo_move_to(cr, 10, 50); |
| 203 | cairo_line_to(cr, window->width - 10, 50); |
| 204 | cairo_line_to(cr, window->width - 10, window->height - 10); |
| 205 | cairo_line_to(cr, 10, window->height - 10); |
| 206 | cairo_close_path(cr); |
| 207 | cairo_set_source(cr, dim); |
| 208 | cairo_stroke(cr); |
| 209 | |
| 210 | cairo_move_to(cr, 11, 51); |
| 211 | cairo_line_to(cr, window->width - 10, 51); |
| 212 | cairo_line_to(cr, window->width - 10, window->height - 10); |
| 213 | cairo_line_to(cr, 11, window->height - 10); |
| 214 | cairo_close_path(cr); |
| 215 | cairo_set_source(cr, bright); |
| 216 | cairo_stroke(cr); |
| 217 | |
| 218 | cairo_move_to(cr, 10, 50); |
| 219 | cairo_line_to(cr, window->width - 10, 50); |
| 220 | cairo_line_to(cr, window->width - 10, window->height - 10); |
| 221 | cairo_line_to(cr, 10, window->height - 10); |
| 222 | cairo_close_path(cr); |
| 223 | cairo_set_source_rgba(cr, 0, 0, 0, 0.9); |
| 224 | cairo_fill(cr); |
| 225 | |
| 226 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 227 | cairo_set_font_size(cr, 14); |
| 228 | cairo_text_extents(cr, title, &extents); |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 229 | cairo_move_to(cr, (window->width - extents.width) / 2, 10 - extents.y_bearing); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame^] | 230 | cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); |
| 231 | cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND); |
| 232 | cairo_set_line_width (cr, 4); |
| 233 | cairo_text_path(cr, title); |
| 234 | cairo_set_source_rgb(cr, 0.1, 0.1, 0.1); |
| 235 | cairo_stroke_preserve(cr); |
| 236 | cairo_set_source_rgb(cr, 1, 1, 1); |
| 237 | cairo_fill(cr); |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 238 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 239 | cairo_destroy(cr); |
| 240 | |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 241 | if (window->buffer != NULL) |
| 242 | buffer_destroy(window->buffer, window->fd); |
| 243 | buffer = buffer_create_from_cairo_surface(window->fd, surface); |
| 244 | window->buffer = buffer; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 245 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 246 | cairo_surface_destroy(surface); |
| 247 | |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 248 | wl_surface_attach(window->surface, buffer->name, |
| 249 | buffer->width, buffer->height, buffer->stride); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 250 | |
Kristian Høgsberg | b7a0192 | 2008-11-08 15:39:41 -0500 | [diff] [blame] | 251 | wl_surface_map(window->surface, |
| 252 | window->x, window->y, |
| 253 | buffer->width, buffer->height); |
| 254 | |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 255 | /* FIXME: Free window->buffer when we receive the ack event. */ |
| 256 | |
| 257 | buffer = window->egl_buffer; |
| 258 | gears_draw(window->gears, window->gears_angle); |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame^] | 259 | wl_surface_copy(window->surface, |
| 260 | (window->width - 300) / 2, |
| 261 | 50 + (window->height - 50 - 300) / 2, |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 262 | buffer->name, buffer->stride, |
| 263 | 0, 0, buffer->width, buffer->height); |
| 264 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 265 | window->redraw_scheduled = 0; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 266 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 267 | return FALSE; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | enum window_state { |
| 271 | WINDOW_STABLE, |
| 272 | WINDOW_MOVING, |
| 273 | WINDOW_RESIZING_UPPER_LEFT, |
| 274 | WINDOW_RESIZING_UPPER_RIGHT, |
| 275 | WINDOW_RESIZING_LOWER_LEFT, |
| 276 | WINDOW_RESIZING_LOWER_RIGHT |
| 277 | }; |
| 278 | |
| 279 | enum location { |
| 280 | LOCATION_INTERIOR, |
| 281 | LOCATION_UPPER_LEFT, |
| 282 | LOCATION_UPPER_RIGHT, |
| 283 | LOCATION_LOWER_LEFT, |
| 284 | LOCATION_LOWER_RIGHT, |
| 285 | LOCATION_OUTSIDE |
| 286 | }; |
| 287 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame^] | 288 | static void |
| 289 | event_handler(struct wl_display *display, |
| 290 | uint32_t opcode, uint32_t arg1, uint32_t arg2, void *data) |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 291 | { |
| 292 | struct window *window = data; |
| 293 | int location, border = 4; |
| 294 | int grip_size = 16; |
| 295 | |
| 296 | if (opcode == 0) { |
| 297 | window->last_x = arg1; |
| 298 | window->last_y = arg2; |
| 299 | switch (window->state) { |
| 300 | case WINDOW_MOVING: |
| 301 | window->x = window->drag_x + arg1; |
| 302 | window->y = window->drag_y + arg2; |
| 303 | wl_surface_map(window->surface, window->x, window->y, |
| 304 | window->width, window->height); |
| 305 | break; |
| 306 | case WINDOW_RESIZING_LOWER_RIGHT: |
| 307 | window->width = window->drag_x + arg1; |
| 308 | window->height = window->drag_y + arg2; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 309 | if (window->width < 400) |
| 310 | window->width = 400; |
| 311 | if (window->height < 400) |
| 312 | window->height = 400; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 313 | if (!window->redraw_scheduled) { |
| 314 | window->redraw_scheduled = 1; |
| 315 | g_idle_add(draw_window, window); |
| 316 | } |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 317 | break; |
| 318 | } |
| 319 | } |
| 320 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame^] | 321 | if (window->x + window->width - grip_size <= window->last_x && |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 322 | window->last_x < window->x + window->width && |
| 323 | window->y + window->height - grip_size <= window->last_y && |
| 324 | window->last_y < window->y + window->height) { |
| 325 | location = LOCATION_LOWER_RIGHT; |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame^] | 326 | } else if (window->x + border <= window->last_x && |
| 327 | window->last_x < window->x + window->width - border && |
| 328 | window->y + border <= window->last_y && |
| 329 | window->last_y < window->y + window->height - border) { |
| 330 | location = LOCATION_INTERIOR; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 331 | } else { |
| 332 | location = LOCATION_OUTSIDE; |
| 333 | } |
| 334 | |
| 335 | if (opcode == 1 && arg1 == 0 && arg2 == 1) { |
| 336 | switch (location) { |
| 337 | case LOCATION_INTERIOR: |
| 338 | window->drag_x = window->x - window->last_x; |
| 339 | window->drag_y = window->y - window->last_y; |
| 340 | window->state = WINDOW_MOVING; |
| 341 | break; |
| 342 | case LOCATION_LOWER_RIGHT: |
| 343 | window->drag_x = window->width - window->last_x; |
| 344 | window->drag_y = window->height - window->last_y; |
| 345 | window->state = WINDOW_RESIZING_LOWER_RIGHT; |
| 346 | break; |
| 347 | default: |
| 348 | window->state = WINDOW_STABLE; |
| 349 | break; |
| 350 | } |
| 351 | } else if (opcode == 1 && arg1 == 0 && arg2 == 0) { |
| 352 | window->state = WINDOW_STABLE; |
| 353 | } |
| 354 | } |
| 355 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 356 | static struct window * |
| 357 | window_create(struct wl_display *display, int fd) |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 358 | { |
| 359 | EGLint major, minor, count; |
| 360 | EGLConfig configs[64]; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 361 | struct window *window; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 362 | struct buffer *buffer; |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame^] | 363 | const GLfloat red = 0, green = 0, blue = 0, alpha = 0.9; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 364 | |
| 365 | window = malloc(sizeof *window); |
| 366 | if (window == NULL) |
| 367 | return NULL; |
| 368 | |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 369 | memset(window, 0, sizeof *window); |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 370 | window->surface = wl_display_create_surface(display); |
| 371 | window->x = 200; |
| 372 | window->y = 200; |
| 373 | window->width = 450; |
| 374 | window->height = 500; |
| 375 | window->state = WINDOW_STABLE; |
| 376 | window->fd = fd; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 377 | window->background = cairo_pattern_create_rgba (red, green, blue, alpha); |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 378 | |
| 379 | window->display = eglCreateDisplayNative("/dev/dri/card0", "i965"); |
| 380 | if (window->display == NULL) |
| 381 | die("failed to create display\n"); |
| 382 | |
| 383 | if (!eglInitialize(window->display, &major, &minor)) |
| 384 | die("failed to initialize display\n"); |
| 385 | |
| 386 | if (!eglGetConfigs(window->display, configs, 64, &count)) |
| 387 | die("failed to get configs\n"); |
| 388 | |
| 389 | window->config = configs[24]; |
| 390 | window->context = eglCreateContext(window->display, window->config, NULL, NULL); |
| 391 | if (window->context == NULL) |
| 392 | die("failed to create context\n"); |
| 393 | |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 394 | /* FIXME: We need to get the stride right here in a chipset |
| 395 | * independent way. Maybe do it in name_cairo_surface(). */ |
| 396 | buffer = buffer_create(window->fd, 300, 300, (300 * 4 + 15) & ~15); |
| 397 | window->egl_buffer = buffer; |
| 398 | window->egl_surface = eglCreateSurfaceForName(window->display, |
| 399 | window->config, buffer->name, |
| 400 | buffer->width, buffer->height, |
| 401 | buffer->stride, NULL); |
| 402 | |
| 403 | if (window->egl_surface == NULL) |
| 404 | die("failed to create egl surface\n"); |
| 405 | |
| 406 | if (!eglMakeCurrent(window->display, |
| 407 | window->egl_surface, window->egl_surface, window->context)) |
| 408 | die("failed to make context current\n"); |
| 409 | |
| 410 | glViewport(0, 0, 300, 300); |
| 411 | |
| 412 | window->gears = gears_create(red, green, blue, alpha); |
| 413 | window->gears_angle = 0.0; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 414 | |
| 415 | draw_window(window); |
| 416 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 417 | return window; |
| 418 | } |
| 419 | |
| 420 | static gboolean |
| 421 | draw(gpointer data) |
| 422 | { |
| 423 | struct window *window = data; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 424 | struct buffer *buffer; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 425 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame^] | 426 | |
| 427 | if (!window->redraw_scheduled) { |
| 428 | gears_draw(window->gears, window->gears_angle); |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 429 | |
Kristian Høgsberg | e4feb56 | 2008-11-08 18:53:37 -0500 | [diff] [blame^] | 430 | buffer = window->egl_buffer; |
| 431 | wl_surface_copy(window->surface, |
| 432 | (window->width - 300) / 2, |
| 433 | 50 + (window->height - 50 - 300) / 2, |
| 434 | buffer->name, buffer->stride, |
| 435 | 0, 0, buffer->width, buffer->height); |
| 436 | } |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 437 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 438 | window->gears_angle += 1; |
| 439 | |
| 440 | return TRUE; |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 441 | } |
| 442 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 443 | int main(int argc, char *argv[]) |
| 444 | { |
| 445 | struct wl_display *display; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 446 | int fd; |
| 447 | struct window *window; |
| 448 | GMainLoop *loop; |
| 449 | GSource *source; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 450 | |
| 451 | fd = open(gem_device, O_RDWR); |
| 452 | if (fd < 0) { |
| 453 | fprintf(stderr, "drm open failed: %m\n"); |
| 454 | return -1; |
| 455 | } |
| 456 | |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame] | 457 | display = wl_display_create(socket_name); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 458 | if (display == NULL) { |
| 459 | fprintf(stderr, "failed to create display: %m\n"); |
| 460 | return -1; |
| 461 | } |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 462 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 463 | loop = g_main_loop_new(NULL, FALSE); |
| 464 | source = wayland_source_new(display); |
| 465 | g_source_attach(source, NULL); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 466 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 467 | window = window_create(display, fd); |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 468 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 469 | draw_window(window); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 470 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 471 | wl_display_set_event_handler(display, event_handler, window); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 472 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 473 | g_timeout_add(20, draw, window); |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 474 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 475 | g_main_loop_run(loop); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 476 | |
| 477 | return 0; |
| 478 | } |