window: use libwayland-cursor instead of libXcursor
[platform/upstream/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 #include <wayland-cursor.h>
39
40 #include "window.h"
41
42 struct gears {
43         struct window *window;
44         struct widget *widget;
45
46         struct display *d;
47
48         EGLDisplay display;
49         EGLDisplay config;
50         EGLContext context;
51         GLfloat angle;
52
53         struct {
54                 GLfloat rotx;
55                 GLfloat roty;
56         } view;
57
58         int button_down;
59         int last_x, last_y;
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 static const 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 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
207 {
208         struct gears *gears = data;
209
210         gears->angle = (GLfloat) (time % 8192) * 360 / 8192.0;
211
212         window_schedule_redraw(gears->window);
213
214         if (callback)
215                 wl_callback_destroy(callback);
216 }
217
218 static const struct wl_callback_listener listener = {
219         frame_callback
220 };
221
222 static int
223 motion_handler(struct widget *widget, struct input *input,
224                 uint32_t time, float x, float y, void *data)
225 {
226         struct gears *gears = data;
227         int offset_x, offset_y;
228         float step = 0.5;
229
230         if (gears->button_down) {
231                 offset_x = x - gears->last_x;
232                 offset_y = y - gears->last_y;
233                 gears->last_x = x;
234                 gears->last_y = y;
235                 gears->view.roty += offset_x * step;
236                 gears->view.rotx += offset_y * step;
237                 if (gears->view.roty >= 360)
238                         gears->view.roty = gears->view.roty - 360;
239                 if (gears->view.roty <= 0)
240                         gears->view.roty = gears->view.roty + 360;
241                 if (gears->view.rotx >= 360)
242                         gears->view.rotx = gears->view.rotx - 360;
243                 if (gears->view.rotx <= 0)
244                         gears->view.rotx = gears->view.rotx + 360;
245         }
246
247         return WL_CURSOR_LEFT_PTR;
248 }
249
250 static void
251 button_handler(struct widget *widget, struct input *input,
252                 uint32_t time, uint32_t button, uint32_t state, void *data)
253 {
254         struct gears *gears = data;
255
256         if (button == BTN_LEFT) {
257                 if (state) {
258                         gears->button_down = 1;
259                         input_get_position(input,
260                                         &gears->last_x, &gears->last_y);
261                 } else {
262                         gears->button_down = 0;
263                 }
264         }
265 }
266
267 static void
268 redraw_handler(struct widget *widget, void *data)
269 {
270         struct rectangle window_allocation;
271         struct rectangle allocation;
272         struct wl_callback *callback;
273         struct gears *gears = data;
274
275         widget_get_allocation(gears->widget, &allocation);
276         window_get_allocation(gears->window, &window_allocation);
277
278         if (display_acquire_window_surface(gears->d,
279                                             gears->window,
280                                             gears->context) < 0) {
281                 die("Unable to acquire window surface, "
282                     "compiled without cairo-egl?\n");
283         }
284         
285         glViewport(allocation.x,
286                    window_allocation.height - allocation.height - allocation.y,
287                    allocation.width, allocation.height);
288         glScissor(allocation.x,
289                   window_allocation.height - allocation.height - allocation.y,
290                   allocation.width, allocation.height);
291
292         glEnable(GL_SCISSOR_TEST);
293         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
294
295         glPushMatrix();
296
297         glTranslatef(0.0, 0.0, -50);
298
299         glRotatef(gears->view.rotx, 1.0, 0.0, 0.0);
300         glRotatef(gears->view.roty, 0.0, 1.0, 0.0);
301
302         glPushMatrix();
303         glTranslatef(-3.0, -2.0, 0.0);
304         glRotatef(gears->angle, 0.0, 0.0, 1.0);
305         glCallList(gears->gear_list[0]);
306         glPopMatrix();
307
308         glPushMatrix();
309         glTranslatef(3.1, -2.0, 0.0);
310         glRotatef(-2.0 * gears->angle - 9.0, 0.0, 0.0, 1.0);
311         glCallList(gears->gear_list[1]);
312         glPopMatrix();
313
314         glPushMatrix();
315         glTranslatef(-3.1, 4.2, 0.0);
316         glRotatef(-2.0 * gears->angle - 25.0, 0.0, 0.0, 1.0);
317         glCallList(gears->gear_list[2]);
318         glPopMatrix();
319
320         glPopMatrix();
321
322         glFlush();
323
324         display_release_window_surface(gears->d, gears->window);
325
326         callback = wl_surface_frame(window_get_wl_surface(gears->window));
327         wl_callback_add_listener(callback, &listener, gears);
328 }
329
330 static void
331 resize_handler(struct widget *widget,
332                int32_t width, int32_t height, void *data)
333 {
334         struct gears *gears = data;
335
336         /* Constrain child size to be square and at least 300x300 */
337         if (width > height)
338                 height = width;
339         else
340                 width = height;
341         if (width < 300) {
342                 width = 300;
343                 height = 300;
344         }
345
346         widget_set_size(gears->widget, width, height);
347 }
348
349 static void
350 keyboard_focus_handler(struct window *window,
351                        struct input *device, void *data)
352 {
353         window_schedule_redraw(window);
354 }
355
356 static struct gears *
357 gears_create(struct display *display)
358 {
359         const int width = 450, height = 500;
360         struct gears *gears;
361         int i;
362
363         gears = malloc(sizeof *gears);
364         memset(gears, 0, sizeof *gears);
365         gears->d = display;
366         gears->window = window_create(display);
367         gears->widget = frame_create(gears->window, gears);
368         window_set_title(gears->window, "Wayland Gears");
369
370         gears->display = display_get_egl_display(gears->d);
371         if (gears->display == NULL)
372                 die("failed to create egl display\n");
373
374         eglBindAPI(EGL_OPENGL_API);
375
376         gears->config = display_get_argb_egl_config(gears->d);
377
378         gears->context = eglCreateContext(gears->display, gears->config,
379                                           EGL_NO_CONTEXT, NULL);
380         if (gears->context == NULL)
381                 die("failed to create context\n");
382
383         if (!eglMakeCurrent(gears->display, NULL, NULL, gears->context))
384                 die("failed to make context current\n");
385
386         for (i = 0; i < 3; i++) {
387                 gears->gear_list[i] = glGenLists(1);
388                 glNewList(gears->gear_list[i], GL_COMPILE);
389                 make_gear(&gear_templates[i]);
390                 glEndList();
391         }
392
393         gears->button_down = 0;
394         gears->last_x = 0;
395         gears->last_y = 0;
396
397         gears->view.rotx = 20.0;
398         gears->view.roty = 30.0;
399
400         glEnable(GL_NORMALIZE);
401
402         glMatrixMode(GL_PROJECTION);
403         glLoadIdentity();
404         glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 200.0);
405         glMatrixMode(GL_MODELVIEW);
406
407         glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
408         glEnable(GL_CULL_FACE);
409         glEnable(GL_LIGHTING);
410         glEnable(GL_LIGHT0);
411         glEnable(GL_DEPTH_TEST);
412         glClearColor(0, 0, 0, 0.92);
413
414         window_set_user_data(gears->window, gears);
415         widget_set_resize_handler(gears->widget, resize_handler);
416         widget_set_redraw_handler(gears->widget, redraw_handler);
417         widget_set_button_handler(gears->widget, button_handler);
418         widget_set_motion_handler(gears->widget, motion_handler);
419         window_set_keyboard_focus_handler(gears->window,
420                                           keyboard_focus_handler);
421
422         window_schedule_resize(gears->window, width, height);
423
424         return gears;
425 }
426
427 int main(int argc, char *argv[])
428 {
429         struct display *d;
430
431         d = display_create(argc, argv);
432         if (d == NULL) {
433                 fprintf(stderr, "failed to create display: %m\n");
434                 return -1;
435         }
436         gears_create(d);
437         display_run(d);
438
439         return 0;
440 }