Drop libdrm CFLAGS where no longer necessary.
[profile/ivi/wayland.git] / gears.c
1 /*
2  * 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.
21  */
22
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <math.h>
30 #include <time.h>
31 #include <cairo.h>
32 #include <glib.h>
33 #include <cairo-drm.h>
34
35 #include <GL/gl.h>
36 #include <eagle.h>
37
38 #include "wayland-client.h"
39 #include "wayland-glib.h"
40
41 #include "window.h"
42
43 static const char gem_device[] = "/dev/dri/card0";
44 static const char socket_name[] = "\0wayland";
45
46 struct gears {
47         struct window *window;
48
49         struct wl_display *wl_display;
50         struct wl_compositor *compositor;
51         struct rectangle rectangle;
52
53         EGLDisplay display;
54         EGLConfig config;
55         EGLSurface surface;
56         EGLContext context;
57         int resized;
58         GLfloat angle;
59         cairo_surface_t *cairo_surface;
60
61         GLint gear_list[3];
62 };
63
64 struct gear_template {
65         GLfloat material[4];
66         GLfloat inner_radius;
67         GLfloat outer_radius;
68         GLfloat width;
69         GLint teeth;
70         GLfloat tooth_depth;
71 };
72
73 const static struct gear_template gear_templates[] = {
74         { { 0.8, 0.1, 0.0, 1.0 }, 1.0, 4.0, 1.0, 20, 0.7 },
75         { { 0.0, 0.8, 0.2, 1.0 }, 0.5, 2.0, 2.0, 10, 0.7 },
76         { { 0.2, 0.2, 1.0, 1.0 }, 1.3, 2.0, 0.5, 10, 0.7 }, 
77 };
78
79 static GLfloat light_pos[4] = {5.0, 5.0, 10.0, 0.0};
80
81 static void die(const char *msg)
82 {
83         fprintf(stderr, "%s", msg);
84         exit(EXIT_FAILURE);
85 }
86
87 static void
88 make_gear(const struct gear_template *t)
89 {
90         GLint i;
91         GLfloat r0, r1, r2;
92         GLfloat angle, da;
93         GLfloat u, v, len;
94
95         glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, t->material);
96
97         r0 = t->inner_radius;
98         r1 = t->outer_radius - t->tooth_depth / 2.0;
99         r2 = t->outer_radius + t->tooth_depth / 2.0;
100
101         da = 2.0 * M_PI / t->teeth / 4.0;
102
103         glShadeModel(GL_FLAT);
104
105         glNormal3f(0.0, 0.0, 1.0);
106
107         /* draw front face */
108         glBegin(GL_QUAD_STRIP);
109         for (i = 0; i <= t->teeth; i++) {
110                 angle = i * 2.0 * M_PI / t->teeth;
111                 glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
112                 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
113                 if (i < t->teeth) {
114                         glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
115                         glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
116                 }
117         }
118         glEnd();
119
120         /* draw front sides of teeth */
121         glBegin(GL_QUADS);
122         da = 2.0 * M_PI / t->teeth / 4.0;
123         for (i = 0; i < t->teeth; i++) {
124                 angle = i * 2.0 * M_PI / t->teeth;
125
126                 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
127                 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), t->width * 0.5);
128                 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), t->width * 0.5);
129                 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
130         }
131         glEnd();
132
133         glNormal3f(0.0, 0.0, -1.0);
134
135         /* draw back face */
136         glBegin(GL_QUAD_STRIP);
137         for (i = 0; i <= t->teeth; i++) {
138                 angle = i * 2.0 * M_PI / t->teeth;
139                 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
140                 glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
141                 if (i < t->teeth) {
142                         glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
143                         glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
144                 }
145         }
146         glEnd();
147
148         /* draw back sides of teeth */
149         glBegin(GL_QUADS);
150         da = 2.0 * M_PI / t->teeth / 4.0;
151         for (i = 0; i < t->teeth; i++) {
152                 angle = i * 2.0 * M_PI / t->teeth;
153
154                 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
155                 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -t->width * 0.5);
156                 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -t->width * 0.5);
157                 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
158         }
159         glEnd();
160
161         /* draw outward faces of teeth */
162         glBegin(GL_QUAD_STRIP);
163         for (i = 0; i < t->teeth; i++) {
164                 angle = i * 2.0 * M_PI / t->teeth;
165
166                 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
167                 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
168                 u = r2 * cos(angle + da) - r1 * cos(angle);
169                 v = r2 * sin(angle + da) - r1 * sin(angle);
170                 len = sqrt(u * u + v * v);
171                 u /= len;
172                 v /= len;
173                 glNormal3f(v, -u, 0.0);
174                 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), t->width * 0.5);
175                 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -t->width * 0.5);
176                 glNormal3f(cos(angle), sin(angle), 0.0);
177                 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), t->width * 0.5);
178                 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -t->width * 0.5);
179                 u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
180                 v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
181                 glNormal3f(v, -u, 0.0);
182                 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
183                 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
184                 glNormal3f(cos(angle), sin(angle), 0.0);
185         }
186
187         glVertex3f(r1 * cos(0), r1 * sin(0), t->width * 0.5);
188         glVertex3f(r1 * cos(0), r1 * sin(0), -t->width * 0.5);
189
190         glEnd();
191
192         glShadeModel(GL_SMOOTH);
193
194         /* draw inside radius cylinder */
195         glBegin(GL_QUAD_STRIP);
196         for (i = 0; i <= t->teeth; i++) {
197                 angle = i * 2.0 * M_PI / t->teeth;
198                 glNormal3f(-cos(angle), -sin(angle), 0.0);
199                 glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
200                 glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
201         }
202         glEnd();
203 }
204
205 static void
206 draw_gears(struct gears *gears)
207 {
208         GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
209
210         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
211
212         glPushMatrix();
213
214         glTranslatef(0.0, 0.0, -50);
215
216         glRotatef(view_rotx, 1.0, 0.0, 0.0);
217         glRotatef(view_roty, 0.0, 1.0, 0.0);
218         glRotatef(view_rotz, 0.0, 0.0, 1.0);
219
220         glPushMatrix();
221         glTranslatef(-3.0, -2.0, 0.0);
222         glRotatef(gears->angle, 0.0, 0.0, 1.0);
223         glCallList(gears->gear_list[0]);
224         glPopMatrix();
225
226         glPushMatrix();
227         glTranslatef(3.1, -2.0, 0.0);
228         glRotatef(-2.0 * gears->angle - 9.0, 0.0, 0.0, 1.0);
229         glCallList(gears->gear_list[1]);
230         glPopMatrix();
231
232         glPushMatrix();
233         glTranslatef(-3.1, 4.2, 0.0);
234         glRotatef(-2.0 * gears->angle - 25.0, 0.0, 0.0, 1.0);
235         glCallList(gears->gear_list[2]);
236         glPopMatrix();
237
238         glPopMatrix();
239
240         glFlush();
241 }
242
243 static void
244 resize_window(struct gears *gears)
245 {
246         uint32_t name, stride;
247
248         /* Constrain child size to be square and at least 300x300 */
249         window_get_child_rectangle(gears->window, &gears->rectangle);
250         if (gears->rectangle.width > gears->rectangle.height)
251                 gears->rectangle.height = gears->rectangle.width;
252         else
253                 gears->rectangle.width = gears->rectangle.height;
254         if (gears->rectangle.width < 300) {
255                 gears->rectangle.width = 300;
256                 gears->rectangle.height = 300;
257         }
258         window_set_child_size(gears->window, &gears->rectangle);
259
260         window_draw(gears->window);
261
262         if (gears->cairo_surface != NULL)
263                 cairo_surface_destroy(gears->cairo_surface);
264
265         gears->cairo_surface = window_create_surface(gears->window,
266                                                      &gears->rectangle);
267
268         name = cairo_drm_surface_get_name(gears->cairo_surface);
269         stride = cairo_drm_surface_get_stride(gears->cairo_surface),
270         gears->surface = eglCreateSurfaceForName(gears->display,
271                                                  gears->config,
272                                                  name,
273                                                  gears->rectangle.width,
274                                                  gears->rectangle.height,
275                                                  stride, NULL);
276
277         eglMakeCurrent(gears->display,
278                        gears->surface, gears->surface, gears->context);
279
280         glViewport(0, 0, gears->rectangle.width, gears->rectangle.height);
281         gears->resized = 0;
282 }
283
284 static void
285 resize_handler(struct window *window, void *data)
286 {
287         struct gears *gears = data;
288
289         /* Right now, resizing the window from the per-frame callback
290          * is fine, since the window drawing code is so slow that we
291          * can't draw more than one window per frame anyway.  However,
292          * once we implement faster resizing, this will show lag
293          * between pointer motion and window size even if resizing is
294          * fast.  We need to keep processing motion events and posting
295          * new frames as fast as possible so when the server
296          * composites the next frame it will have the most recent size
297          * possible, like what we do for window moves. */
298
299         gears->resized = 1;
300 }
301
302 static void
303 handle_acknowledge(void *data,
304                    struct wl_compositor *compositor,
305                    uint32_t key, uint32_t frame)
306 {
307         struct gears *gears = data;
308
309         if (key != 0)
310                 return;
311
312         if (gears->resized)
313                 resize_window(gears);
314
315         draw_gears(gears);
316 }
317
318 static void
319 handle_frame(void *data,
320              struct wl_compositor *compositor,
321              uint32_t frame, uint32_t timestamp)
322 {
323         struct gears *gears = data;
324
325         window_copy_surface(gears->window,
326                             &gears->rectangle,
327                             gears->cairo_surface);
328
329         wl_compositor_commit(gears->compositor, 0);
330
331         gears->angle = timestamp / 20.0;
332 }
333
334 static const struct wl_compositor_listener compositor_listener = {
335         handle_acknowledge,
336         handle_frame,
337 };
338
339 static struct gears *
340 gears_create(struct wl_display *display, int fd)
341 {
342         const int x = 200, y = 200, width = 450, height = 500;
343         EGLint major, minor, count;
344         EGLConfig configs[64];
345         struct udev *udev;
346         struct udev_device *device;
347         struct gears *gears;
348         int i;
349
350         udev = udev_new();
351         device = udev_device_new_from_syspath(udev, "/sys/class/drm/card0");
352
353         gears = malloc(sizeof *gears);
354         memset(gears, 0, sizeof *gears);
355         gears->wl_display = display;
356         gears->window = window_create(display, fd, "Wayland Gears",
357                                       x, y, width, height);
358
359         gears->display = eglCreateDisplayNative(device);
360         if (gears->display == NULL)
361                 die("failed to create egl display\n");
362
363         if (!eglInitialize(gears->display, &major, &minor))
364                 die("failed to initialize display\n");
365
366         if (!eglGetConfigs(gears->display, configs, 64, &count))
367                 die("failed to get configs\n");
368
369         gears->config = configs[24];
370         gears->context = eglCreateContext(gears->display, gears->config, NULL, NULL);
371         if (gears->context == NULL)
372                 die("failed to create context\n");
373
374         resize_window(gears);
375
376         for (i = 0; i < 3; i++) {
377                 gears->gear_list[i] = glGenLists(1);
378                 glNewList(gears->gear_list[i], GL_COMPILE);
379                 make_gear(&gear_templates[i]);
380                 glEndList();
381         }
382
383         glEnable(GL_NORMALIZE);
384
385         glMatrixMode(GL_PROJECTION);
386         glLoadIdentity();
387         glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 200.0);
388         glMatrixMode(GL_MODELVIEW);
389
390         glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
391         glEnable(GL_CULL_FACE);
392         glEnable(GL_LIGHTING);
393         glEnable(GL_LIGHT0);
394         glEnable(GL_DEPTH_TEST);
395         glClearColor(0, 0, 0, 0.92);
396
397         gears->compositor = wl_display_get_compositor(display);
398
399         draw_gears(gears);
400
401         handle_frame(gears, gears->compositor, 0, 0);
402
403         window_set_resize_handler(gears->window, resize_handler, gears);
404
405         wl_compositor_add_listener(gears->compositor,
406                                    &compositor_listener, gears);
407
408         return gears;
409 }
410
411 int main(int argc, char *argv[])
412 {
413         struct wl_display *display;
414         int fd;
415         GMainLoop *loop;
416         GSource *source;
417         struct gears *gears;
418
419         fd = open(gem_device, O_RDWR);
420         if (fd < 0) {
421                 fprintf(stderr, "drm open failed: %m\n");
422                 return -1;
423         }
424
425         display = wl_display_create(socket_name, sizeof socket_name);
426         if (display == NULL) {
427                 fprintf(stderr, "failed to create display: %m\n");
428                 return -1;
429         }
430
431         loop = g_main_loop_new(NULL, FALSE);
432         source = wl_glib_source_new(display);
433         g_source_attach(source, NULL);
434
435         gears = gears_create(display, fd);
436
437         g_main_loop_run(loop);
438
439         return 0;
440 }