blob: 91fa59dc4d566f8ba2ff2b9203b7641d6745905f [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>
33
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050034#include <GL/gl.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050035#include <eagle.h>
36
37#include "wayland-client.h"
38#include "wayland-glib.h"
39
40#include "cairo-util.h"
41#include "window.h"
42
43static const char gem_device[] = "/dev/dri/card0";
44static const char socket_name[] = "\0wayland";
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050045
46struct gears {
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050047 struct window *window;
48
49 struct wl_display *wl_display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050050 struct wl_compositor *compositor;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050051 struct rectangle rectangle;
52
53 EGLDisplay display;
54 EGLConfig config;
55 EGLSurface surface;
56 EGLContext context;
57 int resized;
58 int fd;
59 GLfloat angle;
60 struct buffer *buffer;
61
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
261 if (gears->buffer != NULL)
262 buffer_destroy(gears->buffer, gears->fd);
263
264 gears->buffer = buffer_create(gears->fd,
265 gears->rectangle.width,
266 gears->rectangle.height,
267 (gears->rectangle.width * 4 + 15) & ~15);
268
269 gears->surface = eglCreateSurfaceForName(gears->display,
270 gears->config,
271 gears->buffer->name,
272 gears->buffer->width,
273 gears->buffer->height,
274 gears->buffer->stride, NULL);
275
276 eglMakeCurrent(gears->display,
277 gears->surface, gears->surface, gears->context);
278
279 glViewport(0, 0, gears->rectangle.width, gears->rectangle.height);
280 gears->resized = 0;
281}
282
283static void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500284resize_handler(struct window *window, void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500285{
286 struct gears *gears = data;
287
288 /* Right now, resizing the window from the per-frame callback
289 * is fine, since the window drawing code is so slow that we
290 * can't draw more than one window per frame anyway. However,
291 * once we implement faster resizing, this will show lag
292 * between pointer motion and window size even if resizing is
293 * fast. We need to keep processing motion events and posting
294 * new frames as fast as possible so when the server
295 * composites the next frame it will have the most recent size
296 * possible, like what we do for window moves. */
297
298 gears->resized = 1;
299}
300
301static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500302handle_acknowledge(void *data,
303 struct wl_compositor *compositor,
304 uint32_t key, uint32_t frame)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500305{
306 struct gears *gears = data;
307
308 if (key != 0)
309 return;
310
311 if (gears->resized)
312 resize_window(gears);
313
314 draw_gears(gears);
315}
316
317static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500318handle_frame(void *data,
319 struct wl_compositor *compositor,
320 uint32_t frame, uint32_t timestamp)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500321{
322 struct gears *gears = data;
323
324 window_copy(gears->window,
325 &gears->rectangle,
326 gears->buffer->name, gears->buffer->stride);
327
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500328 wl_compositor_commit(gears->compositor, 0);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500329
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500330 gears->angle = timestamp / 20.0;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500331}
332
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500333static const struct wl_compositor_listener compositor_listener = {
334 handle_acknowledge,
335 handle_frame,
336};
337
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500338static struct gears *
339gears_create(struct wl_display *display, int fd)
340{
341 const int x = 200, y = 200, width = 450, height = 500;
342 EGLint major, minor, count;
343 EGLConfig configs[64];
Kristian Høgsbergaa68fe32009-01-15 12:50:21 -0500344 struct udev *udev;
345 struct udev_device *device;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500346 struct gears *gears;
347 int i;
348
Kristian Høgsbergaa68fe32009-01-15 12:50:21 -0500349 udev = udev_new();
350 device = udev_device_new_from_syspath(udev, "/sys/class/drm/card0");
351
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500352 gears = malloc(sizeof *gears);
353 memset(gears, 0, sizeof *gears);
354 gears->wl_display = display;
355 gears->fd = fd;
356 gears->window = window_create(display, fd, "Wayland Gears",
357 x, y, width, height);
358
Kristian Høgsbergaa68fe32009-01-15 12:50:21 -0500359 gears->display = eglCreateDisplayNative(device);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500360 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
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500397 gears->compositor = wl_display_get_compositor(display);
398
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500399 draw_gears(gears);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500400
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500401 handle_frame(gears, gears->compositor, 0, 0);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500402
403 window_set_resize_handler(gears->window, resize_handler, gears);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500404
405 wl_compositor_add_listener(gears->compositor,
406 &compositor_listener, gears);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500407
408 return gears;
409}
410
411int 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}