blob: 3cc894f89272f3e2eef94ba52561fd9d89e96b8b [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>
7#include <sys/poll.h>
8#include <fcntl.h>
9#include <unistd.h>
10#include <math.h>
11#include <time.h>
12#include <cairo.h>
13
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øgsberg8a9cda82008-11-03 15:31:30 -050018#include "gears.h"
Kristian Høgsberg61017b12008-11-02 18:51:48 -050019
20static const char gem_device[] = "/dev/dri/card0";
21static const char socket_name[] = "\0wayland";
22
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050023static void die(const char *msg)
24{
25 fprintf(stderr, "%s", msg);
26 exit(EXIT_FAILURE);
27}
28
Kristian Høgsberg61017b12008-11-02 18:51:48 -050029static uint32_t name_cairo_surface(int fd, cairo_surface_t *surface)
30{
31 struct drm_i915_gem_create create;
32 struct drm_gem_flink flink;
33 struct drm_i915_gem_pwrite pwrite;
34 int32_t width, height, stride;
35 void *data;
36
37 width = cairo_image_surface_get_width(surface);
38 height = cairo_image_surface_get_height(surface);
39 stride = cairo_image_surface_get_stride(surface);
40 data = cairo_image_surface_get_data(surface);
41
Kristian Høgsberg61017b12008-11-02 18:51:48 -050042 memset(&create, 0, sizeof(create));
43 create.size = height * stride;
44
45 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
46 fprintf(stderr, "gem create failed: %m\n");
47 return 0;
48 }
49
50 pwrite.handle = create.handle;
51 pwrite.offset = 0;
52 pwrite.size = height * stride;
53 pwrite.data_ptr = (uint64_t) (uintptr_t) data;
54 if (ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite) < 0) {
55 fprintf(stderr, "gem pwrite failed: %m\n");
56 return 0;
57 }
58
59 flink.handle = create.handle;
60 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
61 fprintf(stderr, "gem flink failed: %m\n");
62 return 0;
63 }
64
65#if 0
66 /* We need to hold on to the handle until the server has received
67 * the attach request... we probably need a confirmation event.
68 * I guess the breadcrumb idea will suffice. */
69 struct drm_gem_close close;
70 close.handle = create.handle;
71 if (ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close) < 0) {
72 fprintf(stderr, "gem close failed: %m\n");
73 return 0;
74 }
75#endif
76
77 return flink.name;
78}
79
80struct window {
81 struct wl_surface *surface;
82 int x, y, width, height, stride;
83 int drag_x, drag_y, last_x, last_y;
84 int state;
85 uint32_t name;
86 int fd;
Kristian Høgsberg35370f82008-11-03 11:42:01 -050087 int need_redraw;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050088
89 EGLDisplay display;
90 EGLContext context;
91 EGLConfig config;
92 EGLSurface egl_surface;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050093};
94
95static void *
96draw_window(struct window *window)
97{
98 cairo_surface_t *surface;
99 cairo_t *cr;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500100 int border = 2, radius = 5, h;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500101 int margin = (border + 1) / 2;
102 cairo_text_extents_t extents;
103 const static char title[] = "Wayland First Post";
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500104
105 surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
106 window->width,
107 window->height);
108
109 cr = cairo_create(surface);
110 cairo_set_line_width (cr, border);
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500111 cairo_move_to(cr, margin, margin + radius);
112 cairo_arc(cr, margin + radius, margin + radius, radius,
113 M_PI, 3 * M_PI / 2);
114 cairo_line_to(cr, window->width - radius - margin, margin);
115 cairo_arc(cr, window->width - margin - radius, margin + radius, radius,
116 3 * M_PI / 2, 2 * M_PI);
117 cairo_line_to(cr, window->width - margin,
118 window->height - margin);
119 cairo_line_to(cr, margin, window->height - margin);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500120 cairo_close_path(cr);
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500121 cairo_set_source_rgba(cr, 0.2, 0.2, 0.2, 0.9);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500122 cairo_fill_preserve(cr);
123 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500124 cairo_set_font_size(cr, 14);
125 cairo_text_extents(cr, title, &extents);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500126 h = margin + radius + extents.height + 10;
127 cairo_move_to(cr, margin, h);
128 cairo_line_to(cr, margin + window->width, h);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500129 cairo_stroke(cr);
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500130
131 cairo_move_to(cr, (window->width - extents.width) / 2, 10 - extents.y_bearing);
132 cairo_show_text(cr, title);
133
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500134 cairo_destroy(cr);
135
136 window->stride = cairo_image_surface_get_stride(surface);
137
138 window->name = name_cairo_surface(window->fd, surface);
139 cairo_surface_destroy(surface);
140
141 wl_surface_attach(window->surface, window->name,
142 window->width, window->height, window->stride);
143
144 wl_surface_map(window->surface,
145 window->x, window->y,
146 window->width, window->height);
147
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500148 if (window->egl_surface != EGL_NO_SURFACE)
149 eglDestroySurface(window->display, window->egl_surface);
150
151 /* FIXME: We need to get the stride right here in a chipset
152 * independent way. Maybe do it in name_cairo_surface(). */
153 window->egl_surface = eglCreatePixmapForName(window->display,
154 window->config, window->name,
155 window->width, window->height,
156 window->stride, NULL);
Kristian Høgsbergb8bf19b2008-11-05 07:38:46 -0500157
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500158 if (surface == NULL)
159 die("failed to create surface\n");
160
161 if (!eglMakeCurrent(window->display,
162 window->egl_surface, window->egl_surface, window->context))
163 die("failed to make context current\n");
164
165 glViewport(border, window->height - h - margin - 300, 300, 300);
166
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500167 return surface;
168}
169
170static int
171connection_update(struct wl_connection *connection,
172 uint32_t mask, void *data)
173{
174 struct pollfd *p = data;
175
176 p->events = 0;
177 if (mask & WL_CONNECTION_READABLE)
178 p->events |= POLLIN;
179 if (mask & WL_CONNECTION_WRITABLE)
180 p->events |= POLLOUT;
181
182 return 0;
183}
184
185enum window_state {
186 WINDOW_STABLE,
187 WINDOW_MOVING,
188 WINDOW_RESIZING_UPPER_LEFT,
189 WINDOW_RESIZING_UPPER_RIGHT,
190 WINDOW_RESIZING_LOWER_LEFT,
191 WINDOW_RESIZING_LOWER_RIGHT
192};
193
194enum location {
195 LOCATION_INTERIOR,
196 LOCATION_UPPER_LEFT,
197 LOCATION_UPPER_RIGHT,
198 LOCATION_LOWER_LEFT,
199 LOCATION_LOWER_RIGHT,
200 LOCATION_OUTSIDE
201};
202
203void event_handler(struct wl_display *display,
204 uint32_t opcode,
205 uint32_t arg1, uint32_t arg2, void *data)
206{
207 struct window *window = data;
208 int location, border = 4;
209 int grip_size = 16;
210
211 if (opcode == 0) {
212 window->last_x = arg1;
213 window->last_y = arg2;
214 switch (window->state) {
215 case WINDOW_MOVING:
216 window->x = window->drag_x + arg1;
217 window->y = window->drag_y + arg2;
218 wl_surface_map(window->surface, window->x, window->y,
219 window->width, window->height);
220 break;
221 case WINDOW_RESIZING_LOWER_RIGHT:
222 window->width = window->drag_x + arg1;
223 window->height = window->drag_y + arg2;
Kristian Høgsberg35370f82008-11-03 11:42:01 -0500224 window->need_redraw = 1;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500225 break;
226 }
227 }
228
229 if (window->x + border <= window->last_x &&
230 window->last_x < window->x + window->width - border &&
231 window->y + border <= window->last_y &&
232 window->last_y < window->y + window->height - border) {
233 location = LOCATION_INTERIOR;
234 } else if (window->x + window->width - grip_size <= window->last_x &&
235 window->last_x < window->x + window->width &&
236 window->y + window->height - grip_size <= window->last_y &&
237 window->last_y < window->y + window->height) {
238 location = LOCATION_LOWER_RIGHT;
239 } else {
240 location = LOCATION_OUTSIDE;
241 }
242
243 if (opcode == 1 && arg1 == 0 && arg2 == 1) {
244 switch (location) {
245 case LOCATION_INTERIOR:
246 window->drag_x = window->x - window->last_x;
247 window->drag_y = window->y - window->last_y;
248 window->state = WINDOW_MOVING;
249 break;
250 case LOCATION_LOWER_RIGHT:
251 window->drag_x = window->width - window->last_x;
252 window->drag_y = window->height - window->last_y;
253 window->state = WINDOW_RESIZING_LOWER_RIGHT;
254 break;
255 default:
256 window->state = WINDOW_STABLE;
257 break;
258 }
259 } else if (opcode == 1 && arg1 == 0 && arg2 == 0) {
260 window->state = WINDOW_STABLE;
261 }
262}
263
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500264static void
265init_egl(struct window *window)
266{
267 EGLint major, minor, count;
268 EGLConfig configs[64];
269
270 window->display = eglCreateDisplayNative("/dev/dri/card0", "i965");
271 if (window->display == NULL)
272 die("failed to create display\n");
273
274 if (!eglInitialize(window->display, &major, &minor))
275 die("failed to initialize display\n");
276
277 if (!eglGetConfigs(window->display, configs, 64, &count))
278 die("failed to get configs\n");
279
280 window->config = configs[24];
281 window->context = eglCreateContext(window->display, window->config, NULL, NULL);
282 if (window->context == NULL)
283 die("failed to create context\n");
284
285 window->egl_surface = EGL_NO_SURFACE;
286}
287
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500288int main(int argc, char *argv[])
289{
290 struct wl_display *display;
291 int fd, ret;
292 uint32_t mask;
293 cairo_surface_t *s;
294 struct pollfd p[1];
295 struct window window;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500296 struct gears *gears;
297 GLfloat angle = 0.0;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500298
299 fd = open(gem_device, O_RDWR);
300 if (fd < 0) {
301 fprintf(stderr, "drm open failed: %m\n");
302 return -1;
303 }
304
305 display = wl_display_create(socket_name,
306 connection_update, &p[0]);
307 if (display == NULL) {
308 fprintf(stderr, "failed to create display: %m\n");
309 return -1;
310 }
311 p[0].fd = wl_display_get_fd(display);
312
313 window.surface = wl_display_create_surface(display);
314 window.x = 200;
315 window.y = 200;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500316 window.width = 450;
317 window.height = 500;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500318 window.state = WINDOW_STABLE;
319 window.fd = fd;
320
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500321 init_egl(&window);
322
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500323 s = draw_window(&window);
324
325 wl_display_set_event_handler(display, event_handler, &window);
326
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500327 gears = gears_create();
328
329 while (ret = poll(p, 1, 20), ret >= 0) {
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500330 mask = 0;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500331 gears_draw(gears, angle);
332 angle += 1;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500333 if (p[0].revents & POLLIN)
334 mask |= WL_CONNECTION_READABLE;
335 if (p[0].revents & POLLOUT)
336 mask |= WL_CONNECTION_WRITABLE;
337 if (mask)
338 wl_display_iterate(display, mask);
Kristian Høgsberg35370f82008-11-03 11:42:01 -0500339 if (window.need_redraw) {
340 draw_window(&window);
341 window.need_redraw = 0;
342 }
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500343 }
344
345 return 0;
346}