8 #include <sys/timerfd.h>
16 #include "wayland-client.h"
18 static const char gem_device[] = "/dev/dri/card0";
19 static const char socket_name[] = "\0wayland";
21 static uint32_t name_cairo_surface(int fd, cairo_surface_t *surface)
23 struct drm_i915_gem_create create;
24 struct drm_gem_flink flink;
25 struct drm_i915_gem_pwrite pwrite;
26 int32_t width, height, stride;
29 width = cairo_image_surface_get_width(surface);
30 height = cairo_image_surface_get_height(surface);
31 stride = cairo_image_surface_get_stride(surface);
32 data = cairo_image_surface_get_data(surface);
34 memset(&create, 0, sizeof(create));
35 create.size = height * stride;
37 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
38 fprintf(stderr, "gem create failed: %m\n");
42 pwrite.handle = create.handle;
44 pwrite.size = height * stride;
45 pwrite.data_ptr = (uint64_t) (uintptr_t) data;
46 if (ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite) < 0) {
47 fprintf(stderr, "gem pwrite failed: %m\n");
51 flink.handle = create.handle;
52 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
53 fprintf(stderr, "gem flink failed: %m\n");
58 /* We need to hold on to the handle until the server has received
59 * the attach request... we probably need a confirmation event.
60 * I guess the breadcrumb idea will suffice. */
61 struct drm_gem_close close;
62 close.handle = create.handle;
63 if (ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close) < 0) {
64 fprintf(stderr, "gem close failed: %m\n");
73 set_random_color(cairo_t *cr)
75 cairo_set_source_rgba(cr,
76 0.5 + (random() % 50) / 49.0,
77 0.5 + (random() % 50) / 49.0,
78 0.5 + (random() % 50) / 49.0,
79 0.5 + (random() % 100) / 99.0);
84 draw_stuff(int width, int height)
86 const int petal_count = 3 + random() % 5;
87 const double r1 = 60 + random() % 35;
88 const double r2 = 20 + random() % 40;
89 const double u = (10 + random() % 90) / 100.0;
90 const double v = (random() % 90) / 100.0;
92 cairo_surface_t *surface;
95 double t, dt = 2 * M_PI / (petal_count * 2);
96 double x1, y1, x2, y2, x3, y3;
98 surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
101 cr = cairo_create(surface);
102 cairo_translate(cr, width / 2, height / 2);
103 cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
104 for (t = 0, i = 0; i < petal_count; i++, t += dt * 2) {
107 x2 = cos(t + dt) * r2;
108 y2 = sin(t + dt) * r2;
109 x3 = cos(t + 2 * dt) * r1;
110 y3 = sin(t + 2 * dt) * r1;
113 x1 - y1 * u, y1 + x1 * u,
114 x2 + y2 * v, y2 - x2 * v,
118 x2 - y2 * v, y2 + x2 * v,
119 x3 + y3 * u, y3 - x3 * u,
123 cairo_close_path(cr);
124 set_random_color(cr);
125 cairo_fill_preserve(cr);
126 set_random_color(cr);
135 connection_update(struct wl_connection *connection,
136 uint32_t mask, void *data)
138 struct pollfd *p = data;
141 if (mask & WL_CONNECTION_READABLE)
143 if (mask & WL_CONNECTION_WRITABLE)
144 p->events |= POLLOUT;
149 int main(int argc, char *argv[])
151 struct wl_display *display;
152 struct wl_surface *surface;
153 const int x = 512, y = 384, width = 200, height = 200;
159 struct itimerspec its;
162 fd = open(gem_device, O_RDWR);
164 fprintf(stderr, "drm open failed: %m\n");
168 display = wl_display_create(socket_name,
169 connection_update, &p[0]);
170 if (display == NULL) {
171 fprintf(stderr, "failed to create display: %m\n");
174 p[0].fd = wl_display_get_fd(display);
176 surface = wl_display_create_surface(display);
178 p[1].fd = timerfd_create(CLOCK_MONOTONIC, 0);
180 fprintf(stderr, "could not create timerfd\n: %m");
184 p[1].events = POLLIN;
185 clock_gettime(CLOCK_MONOTONIC, &ts);
187 its.it_interval.tv_sec = 0;
188 its.it_interval.tv_nsec = 20 * 1000000;
189 if (timerfd_settime(p[1].fd, TFD_TIMER_ABSTIME, &its, NULL) < 0) {
190 fprintf(stderr, "could not set timerfd\n: %m");
196 s = draw_stuff(width, height);
197 name = name_cairo_surface(fd, s);
199 wl_surface_attach(surface, name, width, height,
200 cairo_image_surface_get_stride(s));
203 while (ret = poll(p, 2, -1), ret >= 0) {
204 if (p[1].revents & POLLIN) {
205 read(p[1].fd, &expires, sizeof expires);
206 wl_surface_map(surface,
207 x + cos(i / 31.0) * 400 - width / 2,
208 y + sin(i / 27.0) * 300 - height / 2,
215 if (p[0].revents & POLLIN)
216 mask |= WL_CONNECTION_READABLE;
217 if (p[0].revents & POLLOUT)
218 mask |= WL_CONNECTION_WRITABLE;
220 wl_display_iterate(display, mask);