blob: 45d0306a65b01366dfffdbbc0e9a5f4f228c1f66 [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øgsberg61017b12008-11-02 18:51:48 -050030static uint32_t name_cairo_surface(int fd, cairo_surface_t *surface)
31{
32 struct drm_i915_gem_create create;
33 struct drm_gem_flink flink;
34 struct drm_i915_gem_pwrite pwrite;
35 int32_t width, height, stride;
36 void *data;
37
38 width = cairo_image_surface_get_width(surface);
39 height = cairo_image_surface_get_height(surface);
40 stride = cairo_image_surface_get_stride(surface);
41 data = cairo_image_surface_get_data(surface);
42
Kristian Høgsberg61017b12008-11-02 18:51:48 -050043 memset(&create, 0, sizeof(create));
44 create.size = height * stride;
45
46 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
47 fprintf(stderr, "gem create failed: %m\n");
48 return 0;
49 }
50
51 pwrite.handle = create.handle;
52 pwrite.offset = 0;
53 pwrite.size = height * stride;
54 pwrite.data_ptr = (uint64_t) (uintptr_t) data;
55 if (ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite) < 0) {
56 fprintf(stderr, "gem pwrite failed: %m\n");
57 return 0;
58 }
59
60 flink.handle = create.handle;
61 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
62 fprintf(stderr, "gem flink failed: %m\n");
63 return 0;
64 }
65
66#if 0
67 /* We need to hold on to the handle until the server has received
68 * the attach request... we probably need a confirmation event.
69 * I guess the breadcrumb idea will suffice. */
70 struct drm_gem_close close;
71 close.handle = create.handle;
72 if (ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close) < 0) {
73 fprintf(stderr, "gem close failed: %m\n");
74 return 0;
75 }
76#endif
77
78 return flink.name;
79}
80
81struct window {
82 struct wl_surface *surface;
83 int x, y, width, height, stride;
84 int drag_x, drag_y, last_x, last_y;
85 int state;
86 uint32_t name;
87 int fd;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050088 int redraw_scheduled;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050089
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050090 GLfloat gears_angle;
91 struct gears *gears;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050092 EGLDisplay display;
93 EGLContext context;
94 EGLConfig config;
95 EGLSurface egl_surface;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050096};
97
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050098static gboolean
99draw_window(void *data)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500100{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500101 struct window *window = data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500102 cairo_surface_t *surface;
103 cairo_t *cr;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500104 int border = 2, radius = 5, h;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500105 int margin = (border + 1) / 2;
106 cairo_text_extents_t extents;
107 const static char title[] = "Wayland First Post";
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500108
109 surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
110 window->width,
111 window->height);
112
113 cr = cairo_create(surface);
114 cairo_set_line_width (cr, border);
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500115 cairo_move_to(cr, margin, margin + radius);
116 cairo_arc(cr, margin + radius, margin + radius, radius,
117 M_PI, 3 * M_PI / 2);
118 cairo_line_to(cr, window->width - radius - margin, margin);
119 cairo_arc(cr, window->width - margin - radius, margin + radius, radius,
120 3 * M_PI / 2, 2 * M_PI);
121 cairo_line_to(cr, window->width - margin,
122 window->height - margin);
123 cairo_line_to(cr, margin, window->height - margin);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500124 cairo_close_path(cr);
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500125 cairo_set_source_rgba(cr, 0.2, 0.2, 0.2, 0.9);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500126 cairo_fill_preserve(cr);
127 cairo_set_source_rgba(cr, 0, 0, 0, 1);
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500128 cairo_set_font_size(cr, 14);
129 cairo_text_extents(cr, title, &extents);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500130 h = margin + radius + extents.height + 10;
131 cairo_move_to(cr, margin, h);
132 cairo_line_to(cr, margin + window->width, h);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500133 cairo_stroke(cr);
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500134
135 cairo_move_to(cr, (window->width - extents.width) / 2, 10 - extents.y_bearing);
136 cairo_show_text(cr, title);
137
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500138 cairo_destroy(cr);
139
140 window->stride = cairo_image_surface_get_stride(surface);
141
142 window->name = name_cairo_surface(window->fd, surface);
143 cairo_surface_destroy(surface);
144
145 wl_surface_attach(window->surface, window->name,
146 window->width, window->height, window->stride);
147
148 wl_surface_map(window->surface,
149 window->x, window->y,
150 window->width, window->height);
151
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500152 if (window->egl_surface != EGL_NO_SURFACE)
153 eglDestroySurface(window->display, window->egl_surface);
154
155 /* FIXME: We need to get the stride right here in a chipset
156 * independent way. Maybe do it in name_cairo_surface(). */
157 window->egl_surface = eglCreatePixmapForName(window->display,
158 window->config, window->name,
159 window->width, window->height,
160 window->stride, NULL);
Kristian Høgsbergb8bf19b2008-11-05 07:38:46 -0500161
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500162 if (surface == NULL)
163 die("failed to create surface\n");
164
165 if (!eglMakeCurrent(window->display,
166 window->egl_surface, window->egl_surface, window->context))
167 die("failed to make context current\n");
168
169 glViewport(border, window->height - h - margin - 300, 300, 300);
170
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500171 if (window->gears != NULL)
172 gears_draw(window->gears, window->gears_angle);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500173
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500174 window->redraw_scheduled = 0;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500175
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500176 return FALSE;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500177}
178
179enum window_state {
180 WINDOW_STABLE,
181 WINDOW_MOVING,
182 WINDOW_RESIZING_UPPER_LEFT,
183 WINDOW_RESIZING_UPPER_RIGHT,
184 WINDOW_RESIZING_LOWER_LEFT,
185 WINDOW_RESIZING_LOWER_RIGHT
186};
187
188enum location {
189 LOCATION_INTERIOR,
190 LOCATION_UPPER_LEFT,
191 LOCATION_UPPER_RIGHT,
192 LOCATION_LOWER_LEFT,
193 LOCATION_LOWER_RIGHT,
194 LOCATION_OUTSIDE
195};
196
197void event_handler(struct wl_display *display,
198 uint32_t opcode,
199 uint32_t arg1, uint32_t arg2, void *data)
200{
201 struct window *window = data;
202 int location, border = 4;
203 int grip_size = 16;
204
205 if (opcode == 0) {
206 window->last_x = arg1;
207 window->last_y = arg2;
208 switch (window->state) {
209 case WINDOW_MOVING:
210 window->x = window->drag_x + arg1;
211 window->y = window->drag_y + arg2;
212 wl_surface_map(window->surface, window->x, window->y,
213 window->width, window->height);
214 break;
215 case WINDOW_RESIZING_LOWER_RIGHT:
216 window->width = window->drag_x + arg1;
217 window->height = window->drag_y + arg2;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500218 if (!window->redraw_scheduled) {
219 window->redraw_scheduled = 1;
220 g_idle_add(draw_window, window);
221 }
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500222 break;
223 }
224 }
225
226 if (window->x + border <= window->last_x &&
227 window->last_x < window->x + window->width - border &&
228 window->y + border <= window->last_y &&
229 window->last_y < window->y + window->height - border) {
230 location = LOCATION_INTERIOR;
231 } else if (window->x + window->width - grip_size <= window->last_x &&
232 window->last_x < window->x + window->width &&
233 window->y + window->height - grip_size <= window->last_y &&
234 window->last_y < window->y + window->height) {
235 location = LOCATION_LOWER_RIGHT;
236 } else {
237 location = LOCATION_OUTSIDE;
238 }
239
240 if (opcode == 1 && arg1 == 0 && arg2 == 1) {
241 switch (location) {
242 case LOCATION_INTERIOR:
243 window->drag_x = window->x - window->last_x;
244 window->drag_y = window->y - window->last_y;
245 window->state = WINDOW_MOVING;
246 break;
247 case LOCATION_LOWER_RIGHT:
248 window->drag_x = window->width - window->last_x;
249 window->drag_y = window->height - window->last_y;
250 window->state = WINDOW_RESIZING_LOWER_RIGHT;
251 break;
252 default:
253 window->state = WINDOW_STABLE;
254 break;
255 }
256 } else if (opcode == 1 && arg1 == 0 && arg2 == 0) {
257 window->state = WINDOW_STABLE;
258 }
259}
260
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500261static struct window *
262window_create(struct wl_display *display, int fd)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500263{
264 EGLint major, minor, count;
265 EGLConfig configs[64];
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500266 struct window *window;
267
268 window = malloc(sizeof *window);
269 if (window == NULL)
270 return NULL;
271
272 window->surface = wl_display_create_surface(display);
273 window->x = 200;
274 window->y = 200;
275 window->width = 450;
276 window->height = 500;
277 window->state = WINDOW_STABLE;
278 window->fd = fd;
279 window->gears = NULL;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500280
281 window->display = eglCreateDisplayNative("/dev/dri/card0", "i965");
282 if (window->display == NULL)
283 die("failed to create display\n");
284
285 if (!eglInitialize(window->display, &major, &minor))
286 die("failed to initialize display\n");
287
288 if (!eglGetConfigs(window->display, configs, 64, &count))
289 die("failed to get configs\n");
290
291 window->config = configs[24];
292 window->context = eglCreateContext(window->display, window->config, NULL, NULL);
293 if (window->context == NULL)
294 die("failed to create context\n");
295
296 window->egl_surface = EGL_NO_SURFACE;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500297
298 draw_window(window);
299
300 window->gears = gears_create();
301 window->gears_angle = 0.0;
302
303 return window;
304}
305
306static gboolean
307draw(gpointer data)
308{
309 struct window *window = data;
310
311 gears_draw(window->gears, window->gears_angle);
312 wl_surface_damage(window->surface, 0, 0,
313 window->width, window->height);
314 window->gears_angle += 1;
315
316 return TRUE;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500317}
318
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500319int main(int argc, char *argv[])
320{
321 struct wl_display *display;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500322 int fd;
323 struct window *window;
324 GMainLoop *loop;
325 GSource *source;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500326
327 fd = open(gem_device, O_RDWR);
328 if (fd < 0) {
329 fprintf(stderr, "drm open failed: %m\n");
330 return -1;
331 }
332
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500333 display = wl_display_create(socket_name);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500334 if (display == NULL) {
335 fprintf(stderr, "failed to create display: %m\n");
336 return -1;
337 }
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500338
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500339 loop = g_main_loop_new(NULL, FALSE);
340 source = wayland_source_new(display);
341 g_source_attach(source, NULL);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500342
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500343 window = window_create(display, fd);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500344
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500345 draw_window(window);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500346
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500347 wl_display_set_event_handler(display, event_handler, window);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500348
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500349 g_timeout_add(20, draw, window);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500350
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500351 g_main_loop_run(loop);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500352
353 return 0;
354}