clients: Remove superfluous #includes
[profile/ivi/weston.git] / clients / 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 "config.h"
24
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <math.h>
30 #include <time.h>
31
32 #include <GL/gl.h>
33 #include <EGL/egl.h>
34 #include <EGL/eglext.h>
35
36 #include <linux/input.h>
37 #include <wayland-client.h>
38
39 #include "window.h"
40
41 struct gears {
42         struct window *window;
43         struct widget *widget;
44
45         struct display *d;
46
47         EGLDisplay display;
48         EGLDisplay config;
49         EGLContext context;
50         GLfloat angle;
51
52         struct {
53                 GLfloat rotx;
54                 GLfloat roty;
55         } view;
56
57         int button_down;
58         int last_x, last_y;
59
60         GLint gear_list[3];
61 };
62
63 struct gear_template {
64         GLfloat material[4];
65         GLfloat inner_radius;
66         GLfloat outer_radius;
67         GLfloat width;
68         GLint teeth;
69         GLfloat tooth_depth;
70 };
71
72 static const struct gear_template gear_templates[] = {
73         { { 0.8, 0.1, 0.0, 1.0 }, 1.0, 4.0, 1.0, 20, 0.7 },
74         { { 0.0, 0.8, 0.2, 1.0 }, 0.5, 2.0, 2.0, 10, 0.7 },
75         { { 0.2, 0.2, 1.0, 1.0 }, 1.3, 2.0, 0.5, 10, 0.7 }, 
76 };
77
78 static GLfloat light_pos[4] = {5.0, 5.0, 10.0, 0.0};
79
80 static void die(const char *msg)
81 {
82         fprintf(stderr, "%s", msg);
83         exit(EXIT_FAILURE);
84 }
85
86 static void
87 make_gear(const struct gear_template *t)
88 {
89         GLint i;
90         GLfloat r0, r1, r2;
91         GLfloat angle, da;
92         GLfloat u, v, len;
93
94         glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, t->material);
95
96         r0 = t->inner_radius;
97         r1 = t->outer_radius - t->tooth_depth / 2.0;
98         r2 = t->outer_radius + t->tooth_depth / 2.0;
99
100         da = 2.0 * M_PI / t->teeth / 4.0;
101
102         glShadeModel(GL_FLAT);
103
104         glNormal3f(0.0, 0.0, 1.0);
105
106         /* draw front face */
107         glBegin(GL_QUAD_STRIP);
108         for (i = 0; i <= t->teeth; i++) {
109                 angle = i * 2.0 * M_PI / t->teeth;
110                 glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
111                 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
112                 if (i < t->teeth) {
113                         glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
114                         glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
115                 }
116         }
117         glEnd();
118
119         /* draw front sides of teeth */
120         glBegin(GL_QUADS);
121         da = 2.0 * M_PI / t->teeth / 4.0;
122         for (i = 0; i < t->teeth; i++) {
123                 angle = i * 2.0 * M_PI / t->teeth;
124
125                 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
126                 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), t->width * 0.5);
127                 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), t->width * 0.5);
128                 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
129         }
130         glEnd();
131
132         glNormal3f(0.0, 0.0, -1.0);
133
134         /* draw back face */
135         glBegin(GL_QUAD_STRIP);
136         for (i = 0; i <= t->teeth; i++) {
137                 angle = i * 2.0 * M_PI / t->teeth;
138                 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
139                 glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
140                 if (i < t->teeth) {
141                         glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
142                         glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
143                 }
144         }
145         glEnd();
146
147         /* draw back sides of teeth */
148         glBegin(GL_QUADS);
149         da = 2.0 * M_PI / t->teeth / 4.0;
150         for (i = 0; i < t->teeth; i++) {
151                 angle = i * 2.0 * M_PI / t->teeth;
152
153                 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
154                 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -t->width * 0.5);
155                 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -t->width * 0.5);
156                 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
157         }
158         glEnd();
159
160         /* draw outward faces of teeth */
161         glBegin(GL_QUAD_STRIP);
162         for (i = 0; i < t->teeth; i++) {
163                 angle = i * 2.0 * M_PI / t->teeth;
164
165                 glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
166                 glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
167                 u = r2 * cos(angle + da) - r1 * cos(angle);
168                 v = r2 * sin(angle + da) - r1 * sin(angle);
169                 len = sqrt(u * u + v * v);
170                 u /= len;
171                 v /= len;
172                 glNormal3f(v, -u, 0.0);
173                 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), t->width * 0.5);
174                 glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -t->width * 0.5);
175                 glNormal3f(cos(angle), sin(angle), 0.0);
176                 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), t->width * 0.5);
177                 glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -t->width * 0.5);
178                 u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
179                 v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
180                 glNormal3f(v, -u, 0.0);
181                 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
182                 glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
183                 glNormal3f(cos(angle), sin(angle), 0.0);
184         }
185
186         glVertex3f(r1 * cos(0), r1 * sin(0), t->width * 0.5);
187         glVertex3f(r1 * cos(0), r1 * sin(0), -t->width * 0.5);
188
189         glEnd();
190
191         glShadeModel(GL_SMOOTH);
192
193         /* draw inside radius cylinder */
194         glBegin(GL_QUAD_STRIP);
195         for (i = 0; i <= t->teeth; i++) {
196                 angle = i * 2.0 * M_PI / t->teeth;
197                 glNormal3f(-cos(angle), -sin(angle), 0.0);
198                 glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
199                 glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
200         }
201         glEnd();
202 }
203
204 static void
205 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
206 {
207         struct gears *gears = data;
208
209         gears->angle = (GLfloat) (time % 8192) * 360 / 8192.0;
210
211         window_schedule_redraw(gears->window);
212
213         if (callback)
214                 wl_callback_destroy(callback);
215 }
216
217 static const struct wl_callback_listener listener = {
218         frame_callback
219 };
220
221 static int
222 motion_handler(struct widget *widget, struct input *input,
223                 uint32_t time, float x, float y, void *data)
224 {
225         struct gears *gears = data;
226         int offset_x, offset_y;
227         float step = 0.5;
228
229         if (gears->button_down) {
230                 offset_x = x - gears->last_x;
231                 offset_y = y - gears->last_y;
232                 gears->last_x = x;
233                 gears->last_y = y;
234                 gears->view.roty += offset_x * step;
235                 gears->view.rotx += offset_y * step;
236                 if (gears->view.roty >= 360)
237                         gears->view.roty = gears->view.roty - 360;
238                 if (gears->view.roty <= 0)
239                         gears->view.roty = gears->view.roty + 360;
240                 if (gears->view.rotx >= 360)
241                         gears->view.rotx = gears->view.rotx - 360;
242                 if (gears->view.rotx <= 0)
243                         gears->view.rotx = gears->view.rotx + 360;
244         }
245
246         return POINTER_LEFT_PTR;
247 }
248
249 static void
250 button_handler(struct widget *widget, struct input *input,
251                 uint32_t time, uint32_t button, uint32_t state, void *data)
252 {
253         struct gears *gears = data;
254
255         if (button == BTN_LEFT) {
256                 if (state) {
257                         gears->button_down = 1;
258                         input_get_position(input,
259                                         &gears->last_x, &gears->last_y);
260                 } else {
261                         gears->button_down = 0;
262                 }
263         }
264 }
265
266 static void
267 redraw_handler(struct widget *widget, void *data)
268 {
269         struct rectangle window_allocation;
270         struct rectangle allocation;
271         struct wl_callback *callback;
272         struct gears *gears = data;
273
274         widget_get_allocation(gears->widget, &allocation);
275         window_get_allocation(gears->window, &window_allocation);
276
277         if (display_acquire_window_surface(gears->d,
278                                             gears->window,
279                                             gears->context) < 0) {
280                 die("Unable to acquire window surface, "
281                     "compiled without cairo-egl?\n");
282         }
283         
284         glViewport(allocation.x,
285                    window_allocation.height - allocation.height - allocation.y,
286                    allocation.width, allocation.height);
287         glScissor(allocation.x,
288                   window_allocation.height - allocation.height - allocation.y,
289                   allocation.width, allocation.height);
290
291         glEnable(GL_SCISSOR_TEST);
292         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
293
294         glPushMatrix();
295
296         glTranslatef(0.0, 0.0, -50);
297
298         glRotatef(gears->view.rotx, 1.0, 0.0, 0.0);
299         glRotatef(gears->view.roty, 0.0, 1.0, 0.0);
300
301         glPushMatrix();
302         glTranslatef(-3.0, -2.0, 0.0);
303         glRotatef(gears->angle, 0.0, 0.0, 1.0);
304         glCallList(gears->gear_list[0]);
305         glPopMatrix();
306
307         glPushMatrix();
308         glTranslatef(3.1, -2.0, 0.0);
309         glRotatef(-2.0 * gears->angle - 9.0, 0.0, 0.0, 1.0);
310         glCallList(gears->gear_list[1]);
311         glPopMatrix();
312
313         glPushMatrix();
314         glTranslatef(-3.1, 4.2, 0.0);
315         glRotatef(-2.0 * gears->angle - 25.0, 0.0, 0.0, 1.0);
316         glCallList(gears->gear_list[2]);
317         glPopMatrix();
318
319         glPopMatrix();
320
321         glFlush();
322
323         display_release_window_surface(gears->d, gears->window);
324
325         callback = wl_surface_frame(window_get_wl_surface(gears->window));
326         wl_callback_add_listener(callback, &listener, gears);
327 }
328
329 static void
330 resize_handler(struct widget *widget,
331                int32_t width, int32_t height, void *data)
332 {
333         struct gears *gears = data;
334
335         /* Constrain child size to be square and at least 300x300 */
336         if (width > height)
337                 height = width;
338         else
339                 width = height;
340         if (width < 300) {
341                 width = 300;
342                 height = 300;
343         }
344
345         widget_set_size(gears->widget, width, height);
346 }
347
348 static void
349 keyboard_focus_handler(struct window *window,
350                        struct input *device, void *data)
351 {
352         window_schedule_redraw(window);
353 }
354
355 static struct gears *
356 gears_create(struct display *display)
357 {
358         const int width = 450, height = 500;
359         struct gears *gears;
360         int i;
361
362         gears = malloc(sizeof *gears);
363         memset(gears, 0, sizeof *gears);
364         gears->d = display;
365         gears->window = window_create(display);
366         gears->widget = frame_create(gears->window, gears);
367         window_set_title(gears->window, "Wayland Gears");
368
369         gears->display = display_get_egl_display(gears->d);
370         if (gears->display == NULL)
371                 die("failed to create egl display\n");
372
373         eglBindAPI(EGL_OPENGL_API);
374
375         gears->config = display_get_argb_egl_config(gears->d);
376
377         gears->context = eglCreateContext(gears->display, gears->config,
378                                           EGL_NO_CONTEXT, NULL);
379         if (gears->context == NULL)
380                 die("failed to create context\n");
381
382         if (!eglMakeCurrent(gears->display, NULL, NULL, gears->context))
383                 die("failed to make context current\n");
384
385         for (i = 0; i < 3; i++) {
386                 gears->gear_list[i] = glGenLists(1);
387                 glNewList(gears->gear_list[i], GL_COMPILE);
388                 make_gear(&gear_templates[i]);
389                 glEndList();
390         }
391
392         gears->button_down = 0;
393         gears->last_x = 0;
394         gears->last_y = 0;
395
396         gears->view.rotx = 20.0;
397         gears->view.roty = 30.0;
398
399         glEnable(GL_NORMALIZE);
400
401         glMatrixMode(GL_PROJECTION);
402         glLoadIdentity();
403         glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 200.0);
404         glMatrixMode(GL_MODELVIEW);
405
406         glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
407         glEnable(GL_CULL_FACE);
408         glEnable(GL_LIGHTING);
409         glEnable(GL_LIGHT0);
410         glEnable(GL_DEPTH_TEST);
411         glClearColor(0, 0, 0, 0.92);
412
413         window_set_user_data(gears->window, gears);
414         widget_set_resize_handler(gears->widget, resize_handler);
415         widget_set_redraw_handler(gears->widget, redraw_handler);
416         widget_set_button_handler(gears->widget, button_handler);
417         widget_set_motion_handler(gears->widget, motion_handler);
418         window_set_keyboard_focus_handler(gears->window,
419                                           keyboard_focus_handler);
420
421         window_schedule_resize(gears->window, width, height);
422
423         return gears;
424 }
425
426 int main(int argc, char *argv[])
427 {
428         struct display *d;
429
430         d = display_create(argc, argv);
431         if (d == NULL) {
432                 fprintf(stderr, "failed to create display: %m\n");
433                 return -1;
434         }
435         gears_create(d);
436         display_run(d);
437
438         return 0;
439 }