14 #include "wayland-client.h"
16 static const char gem_device[] = "/dev/dri/card0";
17 static const char socket_name[] = "\0wayland";
20 unpremultiply_data(uint8_t *data, int width, int height, int stride)
25 for (j = 0; j < height; j++) {
26 row = data + j * stride;
28 for (i = 0; i < width; i++) {
29 uint8_t *b = &row[i * 4];
33 memcpy (&pixel, b, sizeof (uint32_t));
34 alpha = (pixel & 0xff000000) >> 24;
36 b[0] = b[1] = b[2] = b[3] = 0;
38 b[0] = (((pixel & 0xff0000) >> 16) * 255 + alpha / 2) / alpha;
39 b[1] = (((pixel & 0x00ff00) >> 8) * 255 + alpha / 2) / alpha;
40 b[2] = (((pixel & 0x0000ff) >> 0) * 255 + alpha / 2) / alpha;
47 static uint32_t name_cairo_surface(int fd, cairo_surface_t *surface)
49 struct drm_i915_gem_create create;
50 struct drm_gem_flink flink;
51 struct drm_i915_gem_pwrite pwrite;
52 int32_t width, height, stride;
55 width = cairo_image_surface_get_width(surface);
56 height = cairo_image_surface_get_height(surface);
57 stride = cairo_image_surface_get_stride(surface);
58 data = cairo_image_surface_get_data(surface);
60 unpremultiply_data(data, width, height, stride);
62 memset(&create, 0, sizeof(create));
63 create.size = height * stride;
65 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
66 fprintf(stderr, "gem create failed: %m\n");
70 pwrite.handle = create.handle;
72 pwrite.size = height * stride;
73 pwrite.data_ptr = (uint64_t) (uintptr_t) data;
74 if (ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite) < 0) {
75 fprintf(stderr, "gem pwrite failed: %m\n");
79 flink.handle = create.handle;
80 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
81 fprintf(stderr, "gem flink failed: %m\n");
86 /* We need to hold on to the handle until the server has received
87 * the attach request... we probably need a confirmation event.
88 * I guess the breadcrumb idea will suffice. */
89 struct drm_gem_close close;
90 close.handle = create.handle;
91 if (ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close) < 0) {
92 fprintf(stderr, "gem close failed: %m\n");
101 struct wl_surface *surface;
102 int x, y, width, height, stride;
103 int drag_x, drag_y, last_x, last_y;
110 draw_window(struct window *window)
112 cairo_surface_t *surface;
115 int half = (border + 1) / 2;
117 surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
121 cr = cairo_create(surface);
122 cairo_set_line_width (cr, border);
123 cairo_move_to(cr, half, half);
124 cairo_line_to(cr, window->width - half, half);
125 cairo_line_to(cr, window->width - half,
126 window->height - half);
127 cairo_line_to(cr, half, window->height - half);
128 cairo_close_path(cr);
129 cairo_set_source_rgba(cr, 0, 0, 0, 0.85);
130 cairo_fill_preserve(cr);
131 cairo_set_source_rgba(cr, 0, 0, 0, 1);
135 window->stride = cairo_image_surface_get_stride(surface);
137 window->name = name_cairo_surface(window->fd, surface);
138 cairo_surface_destroy(surface);
140 wl_surface_attach(window->surface, window->name,
141 window->width, window->height, window->stride);
143 wl_surface_map(window->surface,
144 window->x, window->y,
145 window->width, window->height);
151 connection_update(struct wl_connection *connection,
152 uint32_t mask, void *data)
154 struct pollfd *p = data;
157 if (mask & WL_CONNECTION_READABLE)
159 if (mask & WL_CONNECTION_WRITABLE)
160 p->events |= POLLOUT;
168 WINDOW_RESIZING_UPPER_LEFT,
169 WINDOW_RESIZING_UPPER_RIGHT,
170 WINDOW_RESIZING_LOWER_LEFT,
171 WINDOW_RESIZING_LOWER_RIGHT
177 LOCATION_UPPER_RIGHT,
179 LOCATION_LOWER_RIGHT,
183 void event_handler(struct wl_display *display,
185 uint32_t arg1, uint32_t arg2, void *data)
187 struct window *window = data;
188 int location, border = 4;
192 window->last_x = arg1;
193 window->last_y = arg2;
194 switch (window->state) {
196 window->x = window->drag_x + arg1;
197 window->y = window->drag_y + arg2;
198 wl_surface_map(window->surface, window->x, window->y,
199 window->width, window->height);
201 case WINDOW_RESIZING_LOWER_RIGHT:
202 window->width = window->drag_x + arg1;
203 window->height = window->drag_y + arg2;
209 if (window->x + border <= window->last_x &&
210 window->last_x < window->x + window->width - border &&
211 window->y + border <= window->last_y &&
212 window->last_y < window->y + window->height - border) {
213 location = LOCATION_INTERIOR;
214 } else if (window->x + window->width - grip_size <= window->last_x &&
215 window->last_x < window->x + window->width &&
216 window->y + window->height - grip_size <= window->last_y &&
217 window->last_y < window->y + window->height) {
218 location = LOCATION_LOWER_RIGHT;
220 location = LOCATION_OUTSIDE;
223 if (opcode == 1 && arg1 == 0 && arg2 == 1) {
225 case LOCATION_INTERIOR:
226 window->drag_x = window->x - window->last_x;
227 window->drag_y = window->y - window->last_y;
228 window->state = WINDOW_MOVING;
230 case LOCATION_LOWER_RIGHT:
231 window->drag_x = window->width - window->last_x;
232 window->drag_y = window->height - window->last_y;
233 window->state = WINDOW_RESIZING_LOWER_RIGHT;
236 window->state = WINDOW_STABLE;
239 } else if (opcode == 1 && arg1 == 0 && arg2 == 0) {
240 window->state = WINDOW_STABLE;
244 int main(int argc, char *argv[])
246 struct wl_display *display;
251 struct window window;
253 fd = open(gem_device, O_RDWR);
255 fprintf(stderr, "drm open failed: %m\n");
259 display = wl_display_create(socket_name,
260 connection_update, &p[0]);
261 if (display == NULL) {
262 fprintf(stderr, "failed to create display: %m\n");
265 p[0].fd = wl_display_get_fd(display);
267 window.surface = wl_display_create_surface(display);
272 window.state = WINDOW_STABLE;
275 s = draw_window(&window);
277 wl_display_set_event_handler(display, event_handler, &window);
279 while (ret = poll(p, 1, -1), ret >= 0) {
281 if (p[0].revents & POLLIN)
282 mask |= WL_CONNECTION_READABLE;
283 if (p[0].revents & POLLOUT)
284 mask |= WL_CONNECTION_WRITABLE;
286 wl_display_iterate(display, mask);