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