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 | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 30 | static uint32_t name_cairo_surface(int fd, cairo_surface_t *surface) |
| 31 | { |
| 32 | struct drm_i915_gem_create create; |
| 33 | struct drm_gem_flink flink; |
| 34 | struct drm_i915_gem_pwrite pwrite; |
| 35 | int32_t width, height, stride; |
| 36 | void *data; |
| 37 | |
| 38 | width = cairo_image_surface_get_width(surface); |
| 39 | height = cairo_image_surface_get_height(surface); |
| 40 | stride = cairo_image_surface_get_stride(surface); |
| 41 | data = cairo_image_surface_get_data(surface); |
| 42 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 43 | memset(&create, 0, sizeof(create)); |
| 44 | create.size = height * stride; |
| 45 | |
| 46 | if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) { |
| 47 | fprintf(stderr, "gem create failed: %m\n"); |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | pwrite.handle = create.handle; |
| 52 | pwrite.offset = 0; |
| 53 | pwrite.size = height * stride; |
| 54 | pwrite.data_ptr = (uint64_t) (uintptr_t) data; |
| 55 | if (ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite) < 0) { |
| 56 | fprintf(stderr, "gem pwrite failed: %m\n"); |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | flink.handle = create.handle; |
| 61 | if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) { |
| 62 | fprintf(stderr, "gem flink failed: %m\n"); |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | #if 0 |
| 67 | /* We need to hold on to the handle until the server has received |
| 68 | * the attach request... we probably need a confirmation event. |
| 69 | * I guess the breadcrumb idea will suffice. */ |
| 70 | struct drm_gem_close close; |
| 71 | close.handle = create.handle; |
| 72 | if (ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close) < 0) { |
| 73 | fprintf(stderr, "gem close failed: %m\n"); |
| 74 | return 0; |
| 75 | } |
| 76 | #endif |
| 77 | |
| 78 | return flink.name; |
| 79 | } |
| 80 | |
| 81 | struct window { |
| 82 | struct wl_surface *surface; |
| 83 | int x, y, width, height, stride; |
| 84 | int drag_x, drag_y, last_x, last_y; |
| 85 | int state; |
| 86 | uint32_t name; |
| 87 | int fd; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame^] | 88 | int redraw_scheduled; |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 89 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame^] | 90 | GLfloat gears_angle; |
| 91 | struct gears *gears; |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 92 | EGLDisplay display; |
| 93 | EGLContext context; |
| 94 | EGLConfig config; |
| 95 | EGLSurface egl_surface; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 96 | }; |
| 97 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame^] | 98 | static gboolean |
| 99 | draw_window(void *data) |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 100 | { |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame^] | 101 | struct window *window = data; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 102 | cairo_surface_t *surface; |
| 103 | cairo_t *cr; |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 104 | int border = 2, radius = 5, h; |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 105 | int margin = (border + 1) / 2; |
| 106 | cairo_text_extents_t extents; |
| 107 | const static char title[] = "Wayland First Post"; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 108 | |
| 109 | surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, |
| 110 | window->width, |
| 111 | window->height); |
| 112 | |
| 113 | cr = cairo_create(surface); |
| 114 | cairo_set_line_width (cr, border); |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 115 | cairo_move_to(cr, margin, margin + radius); |
| 116 | cairo_arc(cr, margin + radius, margin + radius, radius, |
| 117 | M_PI, 3 * M_PI / 2); |
| 118 | cairo_line_to(cr, window->width - radius - margin, margin); |
| 119 | cairo_arc(cr, window->width - margin - radius, margin + radius, radius, |
| 120 | 3 * M_PI / 2, 2 * M_PI); |
| 121 | cairo_line_to(cr, window->width - margin, |
| 122 | window->height - margin); |
| 123 | cairo_line_to(cr, margin, window->height - margin); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 124 | cairo_close_path(cr); |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 125 | cairo_set_source_rgba(cr, 0.2, 0.2, 0.2, 0.9); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 126 | cairo_fill_preserve(cr); |
| 127 | cairo_set_source_rgba(cr, 0, 0, 0, 1); |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 128 | cairo_set_font_size(cr, 14); |
| 129 | cairo_text_extents(cr, title, &extents); |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 130 | h = margin + radius + extents.height + 10; |
| 131 | cairo_move_to(cr, margin, h); |
| 132 | cairo_line_to(cr, margin + window->width, h); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 133 | cairo_stroke(cr); |
Kristian Høgsberg | ca1d1f6 | 2008-11-03 06:59:52 -0500 | [diff] [blame] | 134 | |
| 135 | cairo_move_to(cr, (window->width - extents.width) / 2, 10 - extents.y_bearing); |
| 136 | cairo_show_text(cr, title); |
| 137 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 138 | cairo_destroy(cr); |
| 139 | |
| 140 | window->stride = cairo_image_surface_get_stride(surface); |
| 141 | |
| 142 | window->name = name_cairo_surface(window->fd, surface); |
| 143 | cairo_surface_destroy(surface); |
| 144 | |
| 145 | wl_surface_attach(window->surface, window->name, |
| 146 | window->width, window->height, window->stride); |
| 147 | |
| 148 | wl_surface_map(window->surface, |
| 149 | window->x, window->y, |
| 150 | window->width, window->height); |
| 151 | |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 152 | if (window->egl_surface != EGL_NO_SURFACE) |
| 153 | eglDestroySurface(window->display, window->egl_surface); |
| 154 | |
| 155 | /* FIXME: We need to get the stride right here in a chipset |
| 156 | * independent way. Maybe do it in name_cairo_surface(). */ |
| 157 | window->egl_surface = eglCreatePixmapForName(window->display, |
| 158 | window->config, window->name, |
| 159 | window->width, window->height, |
| 160 | window->stride, NULL); |
Kristian Høgsberg | b8bf19b | 2008-11-05 07:38:46 -0500 | [diff] [blame] | 161 | |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 162 | if (surface == NULL) |
| 163 | die("failed to create surface\n"); |
| 164 | |
| 165 | if (!eglMakeCurrent(window->display, |
| 166 | window->egl_surface, window->egl_surface, window->context)) |
| 167 | die("failed to make context current\n"); |
| 168 | |
| 169 | glViewport(border, window->height - h - margin - 300, 300, 300); |
| 170 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame^] | 171 | if (window->gears != NULL) |
| 172 | gears_draw(window->gears, window->gears_angle); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 173 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame^] | 174 | window->redraw_scheduled = 0; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 175 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame^] | 176 | return FALSE; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | enum window_state { |
| 180 | WINDOW_STABLE, |
| 181 | WINDOW_MOVING, |
| 182 | WINDOW_RESIZING_UPPER_LEFT, |
| 183 | WINDOW_RESIZING_UPPER_RIGHT, |
| 184 | WINDOW_RESIZING_LOWER_LEFT, |
| 185 | WINDOW_RESIZING_LOWER_RIGHT |
| 186 | }; |
| 187 | |
| 188 | enum location { |
| 189 | LOCATION_INTERIOR, |
| 190 | LOCATION_UPPER_LEFT, |
| 191 | LOCATION_UPPER_RIGHT, |
| 192 | LOCATION_LOWER_LEFT, |
| 193 | LOCATION_LOWER_RIGHT, |
| 194 | LOCATION_OUTSIDE |
| 195 | }; |
| 196 | |
| 197 | void event_handler(struct wl_display *display, |
| 198 | uint32_t opcode, |
| 199 | uint32_t arg1, uint32_t arg2, void *data) |
| 200 | { |
| 201 | struct window *window = data; |
| 202 | int location, border = 4; |
| 203 | int grip_size = 16; |
| 204 | |
| 205 | if (opcode == 0) { |
| 206 | window->last_x = arg1; |
| 207 | window->last_y = arg2; |
| 208 | switch (window->state) { |
| 209 | case WINDOW_MOVING: |
| 210 | window->x = window->drag_x + arg1; |
| 211 | window->y = window->drag_y + arg2; |
| 212 | wl_surface_map(window->surface, window->x, window->y, |
| 213 | window->width, window->height); |
| 214 | break; |
| 215 | case WINDOW_RESIZING_LOWER_RIGHT: |
| 216 | window->width = window->drag_x + arg1; |
| 217 | window->height = window->drag_y + arg2; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame^] | 218 | if (!window->redraw_scheduled) { |
| 219 | window->redraw_scheduled = 1; |
| 220 | g_idle_add(draw_window, window); |
| 221 | } |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 222 | break; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | if (window->x + border <= window->last_x && |
| 227 | window->last_x < window->x + window->width - border && |
| 228 | window->y + border <= window->last_y && |
| 229 | window->last_y < window->y + window->height - border) { |
| 230 | location = LOCATION_INTERIOR; |
| 231 | } else if (window->x + window->width - grip_size <= window->last_x && |
| 232 | window->last_x < window->x + window->width && |
| 233 | window->y + window->height - grip_size <= window->last_y && |
| 234 | window->last_y < window->y + window->height) { |
| 235 | location = LOCATION_LOWER_RIGHT; |
| 236 | } else { |
| 237 | location = LOCATION_OUTSIDE; |
| 238 | } |
| 239 | |
| 240 | if (opcode == 1 && arg1 == 0 && arg2 == 1) { |
| 241 | switch (location) { |
| 242 | case LOCATION_INTERIOR: |
| 243 | window->drag_x = window->x - window->last_x; |
| 244 | window->drag_y = window->y - window->last_y; |
| 245 | window->state = WINDOW_MOVING; |
| 246 | break; |
| 247 | case LOCATION_LOWER_RIGHT: |
| 248 | window->drag_x = window->width - window->last_x; |
| 249 | window->drag_y = window->height - window->last_y; |
| 250 | window->state = WINDOW_RESIZING_LOWER_RIGHT; |
| 251 | break; |
| 252 | default: |
| 253 | window->state = WINDOW_STABLE; |
| 254 | break; |
| 255 | } |
| 256 | } else if (opcode == 1 && arg1 == 0 && arg2 == 0) { |
| 257 | window->state = WINDOW_STABLE; |
| 258 | } |
| 259 | } |
| 260 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame^] | 261 | static struct window * |
| 262 | window_create(struct wl_display *display, int fd) |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 263 | { |
| 264 | EGLint major, minor, count; |
| 265 | EGLConfig configs[64]; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame^] | 266 | struct window *window; |
| 267 | |
| 268 | window = malloc(sizeof *window); |
| 269 | if (window == NULL) |
| 270 | return NULL; |
| 271 | |
| 272 | window->surface = wl_display_create_surface(display); |
| 273 | window->x = 200; |
| 274 | window->y = 200; |
| 275 | window->width = 450; |
| 276 | window->height = 500; |
| 277 | window->state = WINDOW_STABLE; |
| 278 | window->fd = fd; |
| 279 | window->gears = NULL; |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 280 | |
| 281 | window->display = eglCreateDisplayNative("/dev/dri/card0", "i965"); |
| 282 | if (window->display == NULL) |
| 283 | die("failed to create display\n"); |
| 284 | |
| 285 | if (!eglInitialize(window->display, &major, &minor)) |
| 286 | die("failed to initialize display\n"); |
| 287 | |
| 288 | if (!eglGetConfigs(window->display, configs, 64, &count)) |
| 289 | die("failed to get configs\n"); |
| 290 | |
| 291 | window->config = configs[24]; |
| 292 | window->context = eglCreateContext(window->display, window->config, NULL, NULL); |
| 293 | if (window->context == NULL) |
| 294 | die("failed to create context\n"); |
| 295 | |
| 296 | window->egl_surface = EGL_NO_SURFACE; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame^] | 297 | |
| 298 | draw_window(window); |
| 299 | |
| 300 | window->gears = gears_create(); |
| 301 | window->gears_angle = 0.0; |
| 302 | |
| 303 | return window; |
| 304 | } |
| 305 | |
| 306 | static gboolean |
| 307 | draw(gpointer data) |
| 308 | { |
| 309 | struct window *window = data; |
| 310 | |
| 311 | gears_draw(window->gears, window->gears_angle); |
| 312 | wl_surface_damage(window->surface, 0, 0, |
| 313 | window->width, window->height); |
| 314 | window->gears_angle += 1; |
| 315 | |
| 316 | return TRUE; |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 317 | } |
| 318 | |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 319 | int main(int argc, char *argv[]) |
| 320 | { |
| 321 | struct wl_display *display; |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame^] | 322 | int fd; |
| 323 | struct window *window; |
| 324 | GMainLoop *loop; |
| 325 | GSource *source; |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 326 | |
| 327 | fd = open(gem_device, O_RDWR); |
| 328 | if (fd < 0) { |
| 329 | fprintf(stderr, "drm open failed: %m\n"); |
| 330 | return -1; |
| 331 | } |
| 332 | |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame] | 333 | display = wl_display_create(socket_name); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 334 | if (display == NULL) { |
| 335 | fprintf(stderr, "failed to create display: %m\n"); |
| 336 | return -1; |
| 337 | } |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 338 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame^] | 339 | loop = g_main_loop_new(NULL, FALSE); |
| 340 | source = wayland_source_new(display); |
| 341 | g_source_attach(source, NULL); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 342 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame^] | 343 | window = window_create(display, fd); |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 344 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame^] | 345 | draw_window(window); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 346 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame^] | 347 | wl_display_set_event_handler(display, event_handler, window); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 348 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame^] | 349 | g_timeout_add(20, draw, window); |
Kristian Høgsberg | 8a9cda8 | 2008-11-03 15:31:30 -0500 | [diff] [blame] | 350 | |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame^] | 351 | g_main_loop_run(loop); |
Kristian Høgsberg | 61017b1 | 2008-11-02 18:51:48 -0500 | [diff] [blame] | 352 | |
| 353 | return 0; |
| 354 | } |