Kristian Høgsberg | 5a27f3e | 2008-11-02 10:55:25 -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 | 2a20d83 | 2008-11-02 17:22:39 -0500 | [diff] [blame] | 7 | #include <sys/poll.h> |
Kristian Høgsberg | 5a27f3e | 2008-11-02 10:55:25 -0500 | [diff] [blame] | 8 | #include <fcntl.h> |
| 9 | #include <unistd.h> |
| 10 | #include <math.h> |
| 11 | #include <time.h> |
| 12 | #include <cairo.h> |
| 13 | |
| 14 | #include "wayland-client.h" |
| 15 | |
| 16 | static const char gem_device[] = "/dev/dri/card0"; |
| 17 | static const char socket_name[] = "\0wayland"; |
| 18 | |
| 19 | static uint32_t name_cairo_surface(int fd, cairo_surface_t *surface) |
| 20 | { |
| 21 | struct drm_i915_gem_create create; |
| 22 | struct drm_gem_flink flink; |
| 23 | struct drm_i915_gem_pwrite pwrite; |
| 24 | int32_t width, height, stride; |
Kristian Høgsberg | 1b2f439 | 2008-11-02 15:50:55 -0500 | [diff] [blame] | 25 | void *data; |
Kristian Høgsberg | 5a27f3e | 2008-11-02 10:55:25 -0500 | [diff] [blame] | 26 | |
| 27 | width = cairo_image_surface_get_width(surface); |
| 28 | height = cairo_image_surface_get_height(surface); |
| 29 | stride = cairo_image_surface_get_stride(surface); |
Kristian Høgsberg | 1b2f439 | 2008-11-02 15:50:55 -0500 | [diff] [blame] | 30 | data = cairo_image_surface_get_data(surface); |
| 31 | |
Kristian Høgsberg | 5a27f3e | 2008-11-02 10:55:25 -0500 | [diff] [blame] | 32 | memset(&create, 0, sizeof(create)); |
| 33 | create.size = height * stride; |
| 34 | |
| 35 | if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) { |
| 36 | fprintf(stderr, "gem create failed: %m\n"); |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | pwrite.handle = create.handle; |
| 41 | pwrite.offset = 0; |
| 42 | pwrite.size = height * stride; |
Kristian Høgsberg | 1b2f439 | 2008-11-02 15:50:55 -0500 | [diff] [blame] | 43 | pwrite.data_ptr = (uint64_t) (uintptr_t) data; |
Kristian Høgsberg | 5a27f3e | 2008-11-02 10:55:25 -0500 | [diff] [blame] | 44 | if (ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite) < 0) { |
| 45 | fprintf(stderr, "gem pwrite failed: %m\n"); |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | flink.handle = create.handle; |
| 50 | if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) { |
| 51 | fprintf(stderr, "gem flink failed: %m\n"); |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | #if 0 |
| 56 | /* We need to hold on to the handle until the server has received |
| 57 | * the attach request... we probably need a confirmation event. |
| 58 | * I guess the breadcrumb idea will suffice. */ |
| 59 | struct drm_gem_close close; |
| 60 | close.handle = create.handle; |
| 61 | if (ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close) < 0) { |
| 62 | fprintf(stderr, "gem close failed: %m\n"); |
| 63 | return 0; |
| 64 | } |
| 65 | #endif |
| 66 | |
| 67 | return flink.name; |
| 68 | } |
| 69 | |
| 70 | static void * |
| 71 | draw_pointer(int width, int height) |
| 72 | { |
Kristian Høgsberg | 1b2f439 | 2008-11-02 15:50:55 -0500 | [diff] [blame] | 73 | const int d = 2, end = 4, tx = 2, ty = 6, dx = 4, dy = 4; |
Kristian Høgsberg | 5a27f3e | 2008-11-02 10:55:25 -0500 | [diff] [blame] | 74 | cairo_surface_t *surface; |
| 75 | cairo_t *cr; |
| 76 | |
| 77 | surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, |
| 78 | width, height); |
| 79 | |
| 80 | cr = cairo_create(surface); |
| 81 | cairo_set_line_width (cr, d); |
| 82 | cairo_move_to(cr, d, d); |
| 83 | cairo_line_to(cr, d + tx, d + ty); |
| 84 | cairo_line_to(cr, d + dx, d + dy); |
| 85 | cairo_line_to(cr, width - end, height - d); |
| 86 | cairo_line_to(cr, width - d, height - end); |
| 87 | cairo_line_to(cr, d + dy, d + dx); |
| 88 | cairo_line_to(cr, d + ty, d + tx); |
| 89 | cairo_close_path(cr); |
| 90 | cairo_set_source_rgb(cr, 1, 1, 1); |
| 91 | cairo_stroke_preserve(cr); |
| 92 | cairo_set_source_rgb(cr, 1, 0, 0); |
| 93 | cairo_fill(cr); |
| 94 | |
| 95 | cairo_destroy(cr); |
| 96 | |
| 97 | return surface; |
| 98 | } |
| 99 | |
| 100 | static int |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame^] | 101 | connection_update(uint32_t mask, void *data) |
Kristian Høgsberg | 5a27f3e | 2008-11-02 10:55:25 -0500 | [diff] [blame] | 102 | { |
Kristian Høgsberg | 2a20d83 | 2008-11-02 17:22:39 -0500 | [diff] [blame] | 103 | struct pollfd *p = data; |
| 104 | |
| 105 | p->events = 0; |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame^] | 106 | if (mask & WL_DISPLAY_READABLE) |
Kristian Høgsberg | 2a20d83 | 2008-11-02 17:22:39 -0500 | [diff] [blame] | 107 | p->events |= POLLIN; |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame^] | 108 | if (mask & WL_DISPLAY_WRITABLE) |
Kristian Høgsberg | 2a20d83 | 2008-11-02 17:22:39 -0500 | [diff] [blame] | 109 | p->events |= POLLOUT; |
| 110 | |
Kristian Høgsberg | 5a27f3e | 2008-11-02 10:55:25 -0500 | [diff] [blame] | 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | struct pointer { |
| 115 | int width, height; |
| 116 | struct wl_surface *surface; |
| 117 | }; |
| 118 | |
| 119 | void event_handler(struct wl_display *display, |
| 120 | uint32_t opcode, |
| 121 | uint32_t arg1, uint32_t arg2, void *data) |
| 122 | { |
| 123 | struct pointer *pointer = data; |
| 124 | |
Kristian Høgsberg | 2a20d83 | 2008-11-02 17:22:39 -0500 | [diff] [blame] | 125 | if (opcode == 0) |
| 126 | wl_surface_map(pointer->surface, arg1, arg2, pointer->width, pointer->height); |
Kristian Høgsberg | 5a27f3e | 2008-11-02 10:55:25 -0500 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | int main(int argc, char *argv[]) |
| 130 | { |
| 131 | struct wl_display *display; |
| 132 | struct pointer pointer; |
| 133 | int fd; |
Kristian Høgsberg | 2a20d83 | 2008-11-02 17:22:39 -0500 | [diff] [blame] | 134 | uint32_t name, mask; |
Kristian Høgsberg | 5a27f3e | 2008-11-02 10:55:25 -0500 | [diff] [blame] | 135 | cairo_surface_t *s; |
Kristian Høgsberg | 2a20d83 | 2008-11-02 17:22:39 -0500 | [diff] [blame] | 136 | struct pollfd p[1]; |
Kristian Høgsberg | 5a27f3e | 2008-11-02 10:55:25 -0500 | [diff] [blame] | 137 | |
Kristian Høgsberg | 5a27f3e | 2008-11-02 10:55:25 -0500 | [diff] [blame] | 138 | fd = open(gem_device, O_RDWR); |
| 139 | if (fd < 0) { |
| 140 | fprintf(stderr, "drm open failed: %m\n"); |
| 141 | return -1; |
| 142 | } |
| 143 | |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame^] | 144 | display = wl_display_create(socket_name); |
Kristian Høgsberg | 5a27f3e | 2008-11-02 10:55:25 -0500 | [diff] [blame] | 145 | if (display == NULL) { |
| 146 | fprintf(stderr, "failed to create display: %m\n"); |
| 147 | return -1; |
| 148 | } |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame^] | 149 | p[0].fd = wl_display_get_fd(display, connection_update, &p[0]); |
Kristian Høgsberg | 5a27f3e | 2008-11-02 10:55:25 -0500 | [diff] [blame] | 150 | |
| 151 | pointer.width = 16; |
| 152 | pointer.height = 16; |
| 153 | pointer.surface = wl_display_create_surface(display); |
| 154 | |
| 155 | s = draw_pointer(pointer.width, pointer.height); |
| 156 | name = name_cairo_surface(fd, s); |
| 157 | |
| 158 | wl_surface_attach(pointer.surface, name, |
| 159 | pointer.width, pointer.height, |
| 160 | cairo_image_surface_get_stride(s)); |
Kristian Høgsberg | 2a20d83 | 2008-11-02 17:22:39 -0500 | [diff] [blame] | 161 | wl_surface_map(pointer.surface, 512, 384, pointer.width, pointer.height); |
Kristian Høgsberg | 5a27f3e | 2008-11-02 10:55:25 -0500 | [diff] [blame] | 162 | |
| 163 | wl_display_set_event_handler(display, event_handler, &pointer); |
| 164 | |
Kristian Høgsberg | 2a20d83 | 2008-11-02 17:22:39 -0500 | [diff] [blame] | 165 | while (1) { |
| 166 | poll(p, 1, -1); |
| 167 | mask = 0; |
| 168 | if (p[0].revents & POLLIN) |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame^] | 169 | mask |= WL_DISPLAY_READABLE; |
Kristian Høgsberg | 2a20d83 | 2008-11-02 17:22:39 -0500 | [diff] [blame] | 170 | if (p[0].revents & POLLOUT) |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame^] | 171 | mask |= WL_DISPLAY_WRITABLE; |
Kristian Høgsberg | 2a20d83 | 2008-11-02 17:22:39 -0500 | [diff] [blame] | 172 | wl_display_iterate(display, mask); |
| 173 | } |
Kristian Høgsberg | 5a27f3e | 2008-11-02 10:55:25 -0500 | [diff] [blame] | 174 | |
| 175 | return 0; |
| 176 | } |