blob: def1f99ccb0bce69989a21a1e3a250e582072d14 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -05002 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050021 */
22
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050023#include <stdint.h>
24#include <stdio.h>
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050025#include <stdlib.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050026#include <string.h>
27#include <fcntl.h>
28#include <unistd.h>
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050029#include <math.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050030#include <time.h>
31#include <cairo.h>
32#include <glib.h>
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050033#include <cairo-drm.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050034
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050035#include <GL/gl.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050036#include <eagle.h>
37
Kristian Høgsbergda1f30a2009-03-06 21:24:01 -050038#include "wayland-util.h"
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050039#include "wayland-client.h"
40#include "wayland-glib.h"
41
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050042#include "window.h"
43
44static const char gem_device[] = "/dev/dri/card0";
45static const char socket_name[] = "\0wayland";
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050046
47struct gears {
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050048 struct window *window;
49
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -050050 struct display *d;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050051 struct wl_compositor *compositor;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050052 struct rectangle rectangle;
53
54 EGLDisplay display;
55 EGLConfig config;
56 EGLSurface surface;
57 EGLContext context;
58 int resized;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050059 GLfloat angle;
Kristian Høgsberg0ac16f02009-01-15 11:37:43 -050060 cairo_surface_t *cairo_surface;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050061
62 GLint gear_list[3];
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050063};
64
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050065struct gear_template {
66 GLfloat material[4];
67 GLfloat inner_radius;
68 GLfloat outer_radius;
69 GLfloat width;
70 GLint teeth;
71 GLfloat tooth_depth;
72};
73
74const static struct gear_template gear_templates[] = {
75 { { 0.8, 0.1, 0.0, 1.0 }, 1.0, 4.0, 1.0, 20, 0.7 },
76 { { 0.0, 0.8, 0.2, 1.0 }, 0.5, 2.0, 2.0, 10, 0.7 },
77 { { 0.2, 0.2, 1.0, 1.0 }, 1.3, 2.0, 0.5, 10, 0.7 },
78};
79
80static GLfloat light_pos[4] = {5.0, 5.0, 10.0, 0.0};
81
82static void die(const char *msg)
83{
84 fprintf(stderr, "%s", msg);
85 exit(EXIT_FAILURE);
86}
87
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050088static void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050089make_gear(const struct gear_template *t)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050090{
91 GLint i;
92 GLfloat r0, r1, r2;
93 GLfloat angle, da;
94 GLfloat u, v, len;
95
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050096 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, t->material);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050097
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050098 r0 = t->inner_radius;
99 r1 = t->outer_radius - t->tooth_depth / 2.0;
100 r2 = t->outer_radius + t->tooth_depth / 2.0;
101
102 da = 2.0 * M_PI / t->teeth / 4.0;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500103
104 glShadeModel(GL_FLAT);
105
106 glNormal3f(0.0, 0.0, 1.0);
107
108 /* draw front face */
109 glBegin(GL_QUAD_STRIP);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500110 for (i = 0; i <= t->teeth; i++) {
111 angle = i * 2.0 * M_PI / t->teeth;
112 glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
113 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
114 if (i < t->teeth) {
115 glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
116 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500117 }
118 }
119 glEnd();
120
121 /* draw front sides of teeth */
122 glBegin(GL_QUADS);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500123 da = 2.0 * M_PI / t->teeth / 4.0;
124 for (i = 0; i < t->teeth; i++) {
125 angle = i * 2.0 * M_PI / t->teeth;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500126
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500127 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
128 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), t->width * 0.5);
129 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), t->width * 0.5);
130 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500131 }
132 glEnd();
133
134 glNormal3f(0.0, 0.0, -1.0);
135
136 /* draw back face */
137 glBegin(GL_QUAD_STRIP);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500138 for (i = 0; i <= t->teeth; i++) {
139 angle = i * 2.0 * M_PI / t->teeth;
140 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
141 glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
142 if (i < t->teeth) {
143 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
144 glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500145 }
146 }
147 glEnd();
148
149 /* draw back sides of teeth */
150 glBegin(GL_QUADS);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500151 da = 2.0 * M_PI / t->teeth / 4.0;
152 for (i = 0; i < t->teeth; i++) {
153 angle = i * 2.0 * M_PI / t->teeth;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500154
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500155 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
156 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -t->width * 0.5);
157 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -t->width * 0.5);
158 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500159 }
160 glEnd();
161
162 /* draw outward faces of teeth */
163 glBegin(GL_QUAD_STRIP);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500164 for (i = 0; i < t->teeth; i++) {
165 angle = i * 2.0 * M_PI / t->teeth;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500166
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500167 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
168 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500169 u = r2 * cos(angle + da) - r1 * cos(angle);
170 v = r2 * sin(angle + da) - r1 * sin(angle);
171 len = sqrt(u * u + v * v);
172 u /= len;
173 v /= len;
174 glNormal3f(v, -u, 0.0);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500175 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), t->width * 0.5);
176 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -t->width * 0.5);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500177 glNormal3f(cos(angle), sin(angle), 0.0);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500178 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), t->width * 0.5);
179 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -t->width * 0.5);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500180 u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
181 v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
182 glNormal3f(v, -u, 0.0);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500183 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
184 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500185 glNormal3f(cos(angle), sin(angle), 0.0);
186 }
187
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500188 glVertex3f(r1 * cos(0), r1 * sin(0), t->width * 0.5);
189 glVertex3f(r1 * cos(0), r1 * sin(0), -t->width * 0.5);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500190
191 glEnd();
192
193 glShadeModel(GL_SMOOTH);
194
195 /* draw inside radius cylinder */
196 glBegin(GL_QUAD_STRIP);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500197 for (i = 0; i <= t->teeth; i++) {
198 angle = i * 2.0 * M_PI / t->teeth;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500199 glNormal3f(-cos(angle), -sin(angle), 0.0);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500200 glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
201 glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500202 }
203 glEnd();
204}
205
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500206static void
207draw_gears(struct gears *gears)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500208{
209 GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
210
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500211 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Kristian Høgsbergb8bf19b2008-11-05 07:38:46 -0500212
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500213 glPushMatrix();
214
215 glTranslatef(0.0, 0.0, -50);
216
217 glRotatef(view_rotx, 1.0, 0.0, 0.0);
218 glRotatef(view_roty, 0.0, 1.0, 0.0);
219 glRotatef(view_rotz, 0.0, 0.0, 1.0);
220
221 glPushMatrix();
222 glTranslatef(-3.0, -2.0, 0.0);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500223 glRotatef(gears->angle, 0.0, 0.0, 1.0);
224 glCallList(gears->gear_list[0]);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500225 glPopMatrix();
226
227 glPushMatrix();
228 glTranslatef(3.1, -2.0, 0.0);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500229 glRotatef(-2.0 * gears->angle - 9.0, 0.0, 0.0, 1.0);
230 glCallList(gears->gear_list[1]);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500231 glPopMatrix();
232
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500233 glPushMatrix();
234 glTranslatef(-3.1, 4.2, 0.0);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500235 glRotatef(-2.0 * gears->angle - 25.0, 0.0, 0.0, 1.0);
236 glCallList(gears->gear_list[2]);
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500237 glPopMatrix();
238
239 glPopMatrix();
240
241 glFlush();
242}
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500243
244static void
245resize_window(struct gears *gears)
246{
Kristian Høgsberg22106762008-12-08 13:50:07 -0500247 /* Constrain child size to be square and at least 300x300 */
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500248 window_get_child_rectangle(gears->window, &gears->rectangle);
Kristian Høgsberg22106762008-12-08 13:50:07 -0500249 if (gears->rectangle.width > gears->rectangle.height)
250 gears->rectangle.height = gears->rectangle.width;
251 else
252 gears->rectangle.width = gears->rectangle.height;
253 if (gears->rectangle.width < 300) {
254 gears->rectangle.width = 300;
255 gears->rectangle.height = 300;
256 }
257 window_set_child_size(gears->window, &gears->rectangle);
258
259 window_draw(gears->window);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500260
Kristian Høgsbergda1f30a2009-03-06 21:24:01 -0500261 if (gears->surface != NULL)
262 eglDestroySurface(gears->display, gears->surface);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500263
Kristian Høgsbergcda0e0d2009-05-27 20:07:38 -0400264 gears->surface = eglCreateSurface(gears->display,
265 gears->config,
266 gears->rectangle.width,
267 gears->rectangle.height,
268 1, NULL);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500269
270 eglMakeCurrent(gears->display,
271 gears->surface, gears->surface, gears->context);
272
273 glViewport(0, 0, gears->rectangle.width, gears->rectangle.height);
274 gears->resized = 0;
275}
276
277static void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500278resize_handler(struct window *window, void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500279{
280 struct gears *gears = data;
281
282 /* Right now, resizing the window from the per-frame callback
283 * is fine, since the window drawing code is so slow that we
284 * can't draw more than one window per frame anyway. However,
285 * once we implement faster resizing, this will show lag
286 * between pointer motion and window size even if resizing is
287 * fast. We need to keep processing motion events and posting
288 * new frames as fast as possible so when the server
289 * composites the next frame it will have the most recent size
290 * possible, like what we do for window moves. */
291
292 gears->resized = 1;
293}
294
295static void
Kristian Høgsberg9ae561d2009-09-21 13:47:53 -0400296keyboard_focus_handler(struct window *window,
297 struct wl_input_device *device, void *data)
298{
299 struct gears *gears = data;
300
301 gears->resized = 1;
302}
303
304static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500305handle_acknowledge(void *data,
306 struct wl_compositor *compositor,
307 uint32_t key, uint32_t frame)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500308{
309 struct gears *gears = data;
310
311 if (key != 0)
312 return;
313
314 if (gears->resized)
315 resize_window(gears);
316
317 draw_gears(gears);
318}
319
320static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500321handle_frame(void *data,
322 struct wl_compositor *compositor,
323 uint32_t frame, uint32_t timestamp)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500324{
325 struct gears *gears = data;
Kristian Høgsbergb22382b2009-03-10 23:40:35 -0400326 uint32_t name, handle, stride;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500327
Kristian Høgsbergcda0e0d2009-05-27 20:07:38 -0400328 eglGetColorBuffer(gears->surface, 0, &name, &handle, &stride);
Kristian Høgsbergda1f30a2009-03-06 21:24:01 -0500329
330 window_copy(gears->window, &gears->rectangle, name, stride);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500331
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500332 wl_compositor_commit(gears->compositor, 0);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500333
Kristian Høgsberg75b70ce2009-09-12 21:08:48 -0400334 gears->angle = (GLfloat) (timestamp % 8192) * 360 / 8192.0;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500335}
336
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500337static const struct wl_compositor_listener compositor_listener = {
338 handle_acknowledge,
339 handle_frame,
340};
341
Kristian Høgsberg1eff73c2009-02-16 00:26:22 -0500342static const EGLint config_attribs[] = {
343 EGL_DEPTH_SIZE, 24,
344 EGL_CONFIG_CAVEAT, EGL_NONE,
345 EGL_RED_SIZE, 8,
346 EGL_NONE
347};
348
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500349static struct gears *
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500350gears_create(struct display *display)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500351{
352 const int x = 200, y = 200, width = 450, height = 500;
353 EGLint major, minor, count;
354 EGLConfig configs[64];
Kristian Høgsbergaa68fe32009-01-15 12:50:21 -0500355 struct udev *udev;
356 struct udev_device *device;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500357 struct gears *gears;
358 int i;
359
Kristian Høgsbergaa68fe32009-01-15 12:50:21 -0500360 udev = udev_new();
361 device = udev_device_new_from_syspath(udev, "/sys/class/drm/card0");
362
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500363 gears = malloc(sizeof *gears);
364 memset(gears, 0, sizeof *gears);
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500365 gears->d = display;
366 gears->window = window_create(display, "Wayland Gears",
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500367 x, y, width, height);
368
Kristian Høgsbergaa68fe32009-01-15 12:50:21 -0500369 gears->display = eglCreateDisplayNative(device);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500370 if (gears->display == NULL)
371 die("failed to create egl display\n");
372
373 if (!eglInitialize(gears->display, &major, &minor))
374 die("failed to initialize display\n");
375
376 if (!eglGetConfigs(gears->display, configs, 64, &count))
377 die("failed to get configs\n");
378
Kristian Høgsberg1eff73c2009-02-16 00:26:22 -0500379 if (!eglChooseConfig(gears->display, config_attribs, &gears->config, 1, NULL))
380 die("failed to pick a config\n");
381
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500382 gears->context = eglCreateContext(gears->display, gears->config, NULL, NULL);
383 if (gears->context == NULL)
384 die("failed to create context\n");
385
386 resize_window(gears);
387
388 for (i = 0; i < 3; i++) {
389 gears->gear_list[i] = glGenLists(1);
390 glNewList(gears->gear_list[i], GL_COMPILE);
391 make_gear(&gear_templates[i]);
392 glEndList();
393 }
394
395 glEnable(GL_NORMALIZE);
396
397 glMatrixMode(GL_PROJECTION);
398 glLoadIdentity();
399 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 200.0);
400 glMatrixMode(GL_MODELVIEW);
401
402 glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
403 glEnable(GL_CULL_FACE);
404 glEnable(GL_LIGHTING);
405 glEnable(GL_LIGHT0);
406 glEnable(GL_DEPTH_TEST);
407 glClearColor(0, 0, 0, 0.92);
408
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500409 gears->compositor = display_get_compositor(display);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500410
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500411 draw_gears(gears);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500412
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500413 handle_frame(gears, gears->compositor, 0, 0);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500414
415 window_set_resize_handler(gears->window, resize_handler, gears);
Kristian Høgsberg9ae561d2009-09-21 13:47:53 -0400416 window_set_keyboard_focus_handler(gears->window, keyboard_focus_handler, gears);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500417
418 wl_compositor_add_listener(gears->compositor,
419 &compositor_listener, gears);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500420
421 return gears;
422}
423
424int main(int argc, char *argv[])
425{
426 struct wl_display *display;
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500427 struct display *d;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500428 int fd;
429 GMainLoop *loop;
430 GSource *source;
431 struct gears *gears;
432
433 fd = open(gem_device, O_RDWR);
434 if (fd < 0) {
435 fprintf(stderr, "drm open failed: %m\n");
436 return -1;
437 }
438
439 display = wl_display_create(socket_name, sizeof socket_name);
440 if (display == NULL) {
441 fprintf(stderr, "failed to create display: %m\n");
442 return -1;
443 }
444
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500445 d = display_create(display, fd);
446
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500447 loop = g_main_loop_new(NULL, FALSE);
448 source = wl_glib_source_new(display);
449 g_source_attach(source, NULL);
450
Kristian Høgsberg43c28ee2009-01-26 23:42:46 -0500451 gears = gears_create(d);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500452
453 g_main_loop_run(loop);
454
455 return 0;
456}