blob: 3e173cd6030cb4bf92af1c003659ebebd841a7af [file] [log] [blame]
Kristian Høgsberg61017b12008-11-02 18:51:48 -05001#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øgsberg61017b12008-11-02 18:51:48 -05007#include <fcntl.h>
8#include <unistd.h>
9#include <math.h>
10#include <time.h>
11#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050012#include <glib.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050013
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050014#include <GL/gl.h>
15#include <eagle.h>
16
Kristian Høgsberg61017b12008-11-02 18:51:48 -050017#include "wayland-client.h"
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050018#include "wayland-glib.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050019#include "gears.h"
Kristian Høgsberg61017b12008-11-02 18:51:48 -050020
21static const char gem_device[] = "/dev/dri/card0";
22static const char socket_name[] = "\0wayland";
23
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050024static void die(const char *msg)
25{
26 fprintf(stderr, "%s", msg);
27 exit(EXIT_FAILURE);
28}
29
Kristian Høgsberg78231c82008-11-08 15:06:01 -050030struct buffer {
31 int width, height, stride;
32 uint32_t name, handle;
33};
34
35static struct buffer *
36buffer_create(int fd, int width, int height, int stride)
Kristian Høgsberg61017b12008-11-02 18:51:48 -050037{
Kristian Høgsberg78231c82008-11-08 15:06:01 -050038 struct buffer *buffer;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050039 struct drm_i915_gem_create create;
40 struct drm_gem_flink flink;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050041
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
69static int
70buffer_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
85static int
86buffer_data(struct buffer *buffer, int fd, void *data)
87{
Kristian Høgsberg61017b12008-11-02 18:51:48 -050088 struct drm_i915_gem_pwrite pwrite;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050089
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
103static struct buffer *
104buffer_create_from_cairo_surface(int fd, cairo_surface_t *surface)
105{
106 struct buffer *buffer;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500107 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øgsberg78231c82008-11-08 15:06:01 -0500115 buffer = buffer_create(fd, width, height, stride);
116 if (buffer == NULL)
117 return NULL;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500118
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500119 if (buffer_data(buffer, fd, data) < 0) {
120 buffer_destroy(buffer, fd);
121 return NULL;
122 }
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500123
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500124 return buffer;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500125}
126
127struct window {
128 struct wl_surface *surface;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500129 int x, y, width, height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500130 int drag_x, drag_y, last_x, last_y;
131 int state;
132 uint32_t name;
133 int fd;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500134 int redraw_scheduled;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500135 cairo_pattern_t *background;
136
137 struct buffer *buffer;
138 struct buffer *egl_buffer;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500139
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500140 GLfloat gears_angle;
141 struct gears *gears;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500142 EGLDisplay display;
143 EGLContext context;
144 EGLConfig config;
145 EGLSurface egl_surface;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500146};
147
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500148static void
149rounded_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øgsberg1cbaa6a2008-11-07 15:54:48 -0500162static gboolean
163draw_window(void *data)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500164{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500165 struct window *window = data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500166 cairo_surface_t *surface;
167 cairo_t *cr;
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500168 int border = 2, radius = 5;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500169 cairo_text_extents_t extents;
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500170 cairo_pattern_t *gradient, *outline, *bright, *dim;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500171 struct buffer *buffer;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500172 const static char title[] = "Wayland First Post";
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500173
174 surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500175 window->width, window->height);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500176
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500177 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øgsberg61017b12008-11-02 18:51:48 -0500181 cr = cairo_create(surface);
182 cairo_set_line_width (cr, border);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500183 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øgsberg61017b12008-11-02 18:51:48 -0500191 cairo_stroke(cr);
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500192
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500193 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øgsbergca1d1f62008-11-03 06:59:52 -0500229 cairo_move_to(cr, (window->width - extents.width) / 2, 10 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500230 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øgsbergca1d1f62008-11-03 06:59:52 -0500238
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500239 cairo_destroy(cr);
240
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500241 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øgsberg61017b12008-11-02 18:51:48 -0500245
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500246 cairo_surface_destroy(surface);
247
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500248 wl_surface_attach(window->surface, buffer->name,
249 buffer->width, buffer->height, buffer->stride);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500250
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500251 wl_surface_map(window->surface,
252 window->x, window->y,
253 buffer->width, buffer->height);
254
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500255 /* 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øgsberge4feb562008-11-08 18:53:37 -0500259 wl_surface_copy(window->surface,
260 (window->width - 300) / 2,
261 50 + (window->height - 50 - 300) / 2,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500262 buffer->name, buffer->stride,
263 0, 0, buffer->width, buffer->height);
264
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500265 window->redraw_scheduled = 0;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500266
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500267 return FALSE;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500268}
269
270enum 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
279enum 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øgsberge4feb562008-11-08 18:53:37 -0500288static void
289event_handler(struct wl_display *display,
290 uint32_t opcode, uint32_t arg1, uint32_t arg2, void *data)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500291{
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øgsberg78231c82008-11-08 15:06:01 -0500309 if (window->width < 400)
310 window->width = 400;
311 if (window->height < 400)
312 window->height = 400;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500313 if (!window->redraw_scheduled) {
314 window->redraw_scheduled = 1;
315 g_idle_add(draw_window, window);
316 }
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500317 break;
318 }
319 }
320
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500321 if (window->x + window->width - grip_size <= window->last_x &&
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500322 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øgsberge4feb562008-11-08 18:53:37 -0500326 } 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øgsberg61017b12008-11-02 18:51:48 -0500331 } 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øgsberg1cbaa6a2008-11-07 15:54:48 -0500356static struct window *
357window_create(struct wl_display *display, int fd)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500358{
359 EGLint major, minor, count;
360 EGLConfig configs[64];
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500361 struct window *window;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500362 struct buffer *buffer;
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500363 const GLfloat red = 0, green = 0, blue = 0, alpha = 0.9;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500364
365 window = malloc(sizeof *window);
366 if (window == NULL)
367 return NULL;
368
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500369 memset(window, 0, sizeof *window);
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500370 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øgsberg78231c82008-11-08 15:06:01 -0500377 window->background = cairo_pattern_create_rgba (red, green, blue, alpha);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500378
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øgsberg78231c82008-11-08 15:06:01 -0500394 /* 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øgsberg1cbaa6a2008-11-07 15:54:48 -0500414
415 draw_window(window);
416
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500417 return window;
418}
419
420static gboolean
421draw(gpointer data)
422{
423 struct window *window = data;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500424 struct buffer *buffer;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500425
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500426
427 if (!window->redraw_scheduled) {
428 gears_draw(window->gears, window->gears_angle);
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500429
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500430 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øgsberg78231c82008-11-08 15:06:01 -0500437
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500438 window->gears_angle += 1;
439
440 return TRUE;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500441}
442
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500443int main(int argc, char *argv[])
444{
445 struct wl_display *display;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500446 int fd;
447 struct window *window;
448 GMainLoop *loop;
449 GSource *source;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500450
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øgsbergfb590842008-11-07 14:27:23 -0500457 display = wl_display_create(socket_name);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500458 if (display == NULL) {
459 fprintf(stderr, "failed to create display: %m\n");
460 return -1;
461 }
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500462
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500463 loop = g_main_loop_new(NULL, FALSE);
464 source = wayland_source_new(display);
465 g_source_attach(source, NULL);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500466
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500467 window = window_create(display, fd);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500468
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500469 draw_window(window);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500470
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500471 wl_display_set_event_handler(display, event_handler, window);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500472
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500473 g_timeout_add(20, draw, window);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500474
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500475 g_main_loop_run(loop);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500476
477 return 0;
478}