blob: 7db8c2c93593156861bea5d97832c93df9c5de22 [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);
157 if (surface == NULL)
158 die("failed to create surface\n");
159
160 if (!eglMakeCurrent(window->display,
161 window->egl_surface, window->egl_surface, window->context))
162 die("failed to make context current\n");
163
164 glViewport(border, window->height - h - margin - 300, 300, 300);
165
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500166 return surface;
167}
168
169static int
170connection_update(struct wl_connection *connection,
171 uint32_t mask, void *data)
172{
173 struct pollfd *p = data;
174
175 p->events = 0;
176 if (mask & WL_CONNECTION_READABLE)
177 p->events |= POLLIN;
178 if (mask & WL_CONNECTION_WRITABLE)
179 p->events |= POLLOUT;
180
181 return 0;
182}
183
184enum window_state {
185 WINDOW_STABLE,
186 WINDOW_MOVING,
187 WINDOW_RESIZING_UPPER_LEFT,
188 WINDOW_RESIZING_UPPER_RIGHT,
189 WINDOW_RESIZING_LOWER_LEFT,
190 WINDOW_RESIZING_LOWER_RIGHT
191};
192
193enum location {
194 LOCATION_INTERIOR,
195 LOCATION_UPPER_LEFT,
196 LOCATION_UPPER_RIGHT,
197 LOCATION_LOWER_LEFT,
198 LOCATION_LOWER_RIGHT,
199 LOCATION_OUTSIDE
200};
201
202void event_handler(struct wl_display *display,
203 uint32_t opcode,
204 uint32_t arg1, uint32_t arg2, void *data)
205{
206 struct window *window = data;
207 int location, border = 4;
208 int grip_size = 16;
209
210 if (opcode == 0) {
211 window->last_x = arg1;
212 window->last_y = arg2;
213 switch (window->state) {
214 case WINDOW_MOVING:
215 window->x = window->drag_x + arg1;
216 window->y = window->drag_y + arg2;
217 wl_surface_map(window->surface, window->x, window->y,
218 window->width, window->height);
219 break;
220 case WINDOW_RESIZING_LOWER_RIGHT:
221 window->width = window->drag_x + arg1;
222 window->height = window->drag_y + arg2;
Kristian Høgsberg35370f82008-11-03 11:42:01 -0500223 window->need_redraw = 1;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500224 break;
225 }
226 }
227
228 if (window->x + border <= window->last_x &&
229 window->last_x < window->x + window->width - border &&
230 window->y + border <= window->last_y &&
231 window->last_y < window->y + window->height - border) {
232 location = LOCATION_INTERIOR;
233 } else if (window->x + window->width - grip_size <= window->last_x &&
234 window->last_x < window->x + window->width &&
235 window->y + window->height - grip_size <= window->last_y &&
236 window->last_y < window->y + window->height) {
237 location = LOCATION_LOWER_RIGHT;
238 } else {
239 location = LOCATION_OUTSIDE;
240 }
241
242 if (opcode == 1 && arg1 == 0 && arg2 == 1) {
243 switch (location) {
244 case LOCATION_INTERIOR:
245 window->drag_x = window->x - window->last_x;
246 window->drag_y = window->y - window->last_y;
247 window->state = WINDOW_MOVING;
248 break;
249 case LOCATION_LOWER_RIGHT:
250 window->drag_x = window->width - window->last_x;
251 window->drag_y = window->height - window->last_y;
252 window->state = WINDOW_RESIZING_LOWER_RIGHT;
253 break;
254 default:
255 window->state = WINDOW_STABLE;
256 break;
257 }
258 } else if (opcode == 1 && arg1 == 0 && arg2 == 0) {
259 window->state = WINDOW_STABLE;
260 }
261}
262
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500263static void
264init_egl(struct window *window)
265{
266 EGLint major, minor, count;
267 EGLConfig configs[64];
268
269 window->display = eglCreateDisplayNative("/dev/dri/card0", "i965");
270 if (window->display == NULL)
271 die("failed to create display\n");
272
273 if (!eglInitialize(window->display, &major, &minor))
274 die("failed to initialize display\n");
275
276 if (!eglGetConfigs(window->display, configs, 64, &count))
277 die("failed to get configs\n");
278
279 window->config = configs[24];
280 window->context = eglCreateContext(window->display, window->config, NULL, NULL);
281 if (window->context == NULL)
282 die("failed to create context\n");
283
284 window->egl_surface = EGL_NO_SURFACE;
285}
286
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500287int main(int argc, char *argv[])
288{
289 struct wl_display *display;
290 int fd, ret;
291 uint32_t mask;
292 cairo_surface_t *s;
293 struct pollfd p[1];
294 struct window window;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500295 struct gears *gears;
296 GLfloat angle = 0.0;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500297
298 fd = open(gem_device, O_RDWR);
299 if (fd < 0) {
300 fprintf(stderr, "drm open failed: %m\n");
301 return -1;
302 }
303
304 display = wl_display_create(socket_name,
305 connection_update, &p[0]);
306 if (display == NULL) {
307 fprintf(stderr, "failed to create display: %m\n");
308 return -1;
309 }
310 p[0].fd = wl_display_get_fd(display);
311
312 window.surface = wl_display_create_surface(display);
313 window.x = 200;
314 window.y = 200;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500315 window.width = 450;
316 window.height = 500;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500317 window.state = WINDOW_STABLE;
318 window.fd = fd;
319
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500320 init_egl(&window);
321
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500322 s = draw_window(&window);
323
324 wl_display_set_event_handler(display, event_handler, &window);
325
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500326 gears = gears_create();
327
328 while (ret = poll(p, 1, 20), ret >= 0) {
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500329 mask = 0;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500330 gears_draw(gears, angle);
331 angle += 1;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500332 if (p[0].revents & POLLIN)
333 mask |= WL_CONNECTION_READABLE;
334 if (p[0].revents & POLLOUT)
335 mask |= WL_CONNECTION_WRITABLE;
336 if (mask)
337 wl_display_iterate(display, mask);
Kristian Høgsberg35370f82008-11-03 11:42:01 -0500338 if (window.need_redraw) {
339 draw_window(&window);
340 window.need_redraw = 0;
341 }
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500342 }
343
344 return 0;
345}