1 /*
2  * Copyright © 2008 Kristian Høgsberg
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include "config.h"
25 
26 #include <stdint.h>
27 #include <stdbool.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <math.h>
32 #include <time.h>
33 
34 #include <GL/gl.h>
35 #include <EGL/egl.h>
36 #include <EGL/eglext.h>
37 
38 #include <linux/input.h>
39 #include <wayland-client.h>
40 
41 #include "window.h"
42 
43 struct gears {
44 	struct window *window;
45 	struct widget *widget;
46 
47 	struct display *d;
48 
49 	EGLDisplay display;
50 	EGLDisplay config;
51 	EGLContext context;
52 	GLfloat angle;
53 
54 	struct {
55 		GLfloat rotx;
56 		GLfloat roty;
57 	} view;
58 
59 	int button_down;
60 	int last_x, last_y;
61 
62 	GLint gear_list[3];
63 	int fullscreen;
64 	int frames;
65 	uint32_t last_fps;
66 };
67 
68 struct gear_template {
69 	GLfloat material[4];
70 	GLfloat inner_radius;
71 	GLfloat outer_radius;
72 	GLfloat width;
73 	GLint teeth;
74 	GLfloat tooth_depth;
75 };
76 
77 static const struct gear_template gear_templates[] = {
78 	{ { 0.8, 0.1, 0.0, 1.0 }, 1.0, 4.0, 1.0, 20, 0.7 },
79 	{ { 0.0, 0.8, 0.2, 1.0 }, 0.5, 2.0, 2.0, 10, 0.7 },
80 	{ { 0.2, 0.2, 1.0, 1.0 }, 1.3, 2.0, 0.5, 10, 0.7 },
81 };
82 
83 static GLfloat light_pos[4] = {5.0, 5.0, 10.0, 0.0};
84 
die(const char * msg)85 static void die(const char *msg)
86 {
87 	fprintf(stderr, "%s", msg);
88 	exit(EXIT_FAILURE);
89 }
90 
91 static void
make_gear(const struct gear_template * t)92 make_gear(const struct gear_template *t)
93 {
94 	GLint i;
95 	GLfloat r0, r1, r2;
96 	GLfloat angle, da;
97 	GLfloat u, v, len;
98 
99 	glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, t->material);
100 
101 	r0 = t->inner_radius;
102 	r1 = t->outer_radius - t->tooth_depth / 2.0;
103 	r2 = t->outer_radius + t->tooth_depth / 2.0;
104 
105 	da = 2.0 * M_PI / t->teeth / 4.0;
106 
107 	glShadeModel(GL_FLAT);
108 
109 	glNormal3f(0.0, 0.0, 1.0);
110 
111 	/* draw front face */
112 	glBegin(GL_QUAD_STRIP);
113 	for (i = 0; i <= t->teeth; i++) {
114 		angle = i * 2.0 * M_PI / t->teeth;
115 		glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
116 		glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
117 		if (i < t->teeth) {
118 			glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
119 			glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
120 		}
121 	}
122 	glEnd();
123 
124 	/* draw front sides of teeth */
125 	glBegin(GL_QUADS);
126 	da = 2.0 * M_PI / t->teeth / 4.0;
127 	for (i = 0; i < t->teeth; i++) {
128 		angle = i * 2.0 * M_PI / t->teeth;
129 
130 		glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
131 		glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), t->width * 0.5);
132 		glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), t->width * 0.5);
133 		glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
134 	}
135 	glEnd();
136 
137 	glNormal3f(0.0, 0.0, -1.0);
138 
139 	/* draw back face */
140 	glBegin(GL_QUAD_STRIP);
141 	for (i = 0; i <= t->teeth; i++) {
142 		angle = i * 2.0 * M_PI / t->teeth;
143 		glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
144 		glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
145 		if (i < t->teeth) {
146 			glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
147 			glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
148 		}
149 	}
150 	glEnd();
151 
152 	/* draw back sides of teeth */
153 	glBegin(GL_QUADS);
154 	da = 2.0 * M_PI / t->teeth / 4.0;
155 	for (i = 0; i < t->teeth; i++) {
156 		angle = i * 2.0 * M_PI / t->teeth;
157 
158 		glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
159 		glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -t->width * 0.5);
160 		glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -t->width * 0.5);
161 		glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
162 	}
163 	glEnd();
164 
165 	/* draw outward faces of teeth */
166 	glBegin(GL_QUAD_STRIP);
167 	for (i = 0; i < t->teeth; i++) {
168 		angle = i * 2.0 * M_PI / t->teeth;
169 
170 		glVertex3f(r1 * cos(angle), r1 * sin(angle), t->width * 0.5);
171 		glVertex3f(r1 * cos(angle), r1 * sin(angle), -t->width * 0.5);
172 		u = r2 * cos(angle + da) - r1 * cos(angle);
173 		v = r2 * sin(angle + da) - r1 * sin(angle);
174 		len = sqrt(u * u + v * v);
175 		u /= len;
176 		v /= len;
177 		glNormal3f(v, -u, 0.0);
178 		glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), t->width * 0.5);
179 		glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -t->width * 0.5);
180 		glNormal3f(cos(angle), sin(angle), 0.0);
181 		glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), t->width * 0.5);
182 		glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -t->width * 0.5);
183 		u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
184 		v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
185 		glNormal3f(v, -u, 0.0);
186 		glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), t->width * 0.5);
187 		glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -t->width * 0.5);
188 		glNormal3f(cos(angle), sin(angle), 0.0);
189 	}
190 
191 	glVertex3f(r1 * cos(0), r1 * sin(0), t->width * 0.5);
192 	glVertex3f(r1 * cos(0), r1 * sin(0), -t->width * 0.5);
193 
194 	glEnd();
195 
196 	glShadeModel(GL_SMOOTH);
197 
198 	/* draw inside radius cylinder */
199 	glBegin(GL_QUAD_STRIP);
200 	for (i = 0; i <= t->teeth; i++) {
201 		angle = i * 2.0 * M_PI / t->teeth;
202 		glNormal3f(-cos(angle), -sin(angle), 0.0);
203 		glVertex3f(r0 * cos(angle), r0 * sin(angle), -t->width * 0.5);
204 		glVertex3f(r0 * cos(angle), r0 * sin(angle), t->width * 0.5);
205 	}
206 	glEnd();
207 }
208 
209 static void
update_fps(struct gears * gears,uint32_t time)210 update_fps(struct gears *gears, uint32_t time)
211 {
212 	long diff_ms;
213 	static bool first_call = true;
214 
215 	if (first_call) {
216 		gears->last_fps = time;
217 		first_call = false;
218 	} else
219 		gears->frames++;
220 
221 	diff_ms = time - gears->last_fps;
222 
223 	if (diff_ms > 5000) {
224 		float seconds = diff_ms / 1000.0;
225 		float fps = gears->frames / seconds;
226 
227 		printf("%d frames in %6.3f seconds = %6.3f FPS\n", gears->frames, seconds, fps);
228 		fflush(stdout);
229 
230 		gears->frames = 0;
231 		gears->last_fps = time;
232 	}
233 }
234 
235 static void
frame_callback(void * data,struct wl_callback * callback,uint32_t time)236 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
237 {
238 	struct gears *gears = data;
239 
240 	update_fps(gears, time);
241 
242 	gears->angle = (GLfloat) (time % 8192) * 360 / 8192.0;
243 
244 	window_schedule_redraw(gears->window);
245 
246 	if (callback)
247 		wl_callback_destroy(callback);
248 }
249 
250 static const struct wl_callback_listener listener = {
251 	frame_callback
252 };
253 
254 static int
motion_handler(struct widget * widget,struct input * input,uint32_t time,float x,float y,void * data)255 motion_handler(struct widget *widget, struct input *input,
256 		uint32_t time, float x, float y, void *data)
257 {
258 	struct gears *gears = data;
259 	int offset_x, offset_y;
260 	float step = 0.5;
261 
262 	if (gears->button_down) {
263 		offset_x = x - gears->last_x;
264 		offset_y = y - gears->last_y;
265 		gears->last_x = x;
266 		gears->last_y = y;
267 		gears->view.roty += offset_x * step;
268 		gears->view.rotx += offset_y * step;
269 		if (gears->view.roty >= 360)
270 			gears->view.roty = gears->view.roty - 360;
271 		if (gears->view.roty <= 0)
272 			gears->view.roty = gears->view.roty + 360;
273 		if (gears->view.rotx >= 360)
274 			gears->view.rotx = gears->view.rotx - 360;
275 		if (gears->view.rotx <= 0)
276 			gears->view.rotx = gears->view.rotx + 360;
277 	}
278 
279 	return CURSOR_LEFT_PTR;
280 }
281 
282 static void
button_handler(struct widget * widget,struct input * input,uint32_t time,uint32_t button,enum wl_pointer_button_state state,void * data)283 button_handler(struct widget *widget, struct input *input,
284 		uint32_t time, uint32_t button,
285 		enum wl_pointer_button_state state, void *data)
286 {
287 	struct gears *gears = data;
288 
289 	if (button == BTN_LEFT) {
290 		if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
291 			gears->button_down = 1;
292 			input_get_position(input,
293 					&gears->last_x, &gears->last_y);
294 		} else {
295 			gears->button_down = 0;
296 		}
297 	}
298 }
299 
300 static void
redraw_handler(struct widget * widget,void * data)301 redraw_handler(struct widget *widget, void *data)
302 {
303 	struct rectangle window_allocation;
304 	struct rectangle allocation;
305 	struct wl_callback *callback;
306 	struct gears *gears = data;
307 
308 	widget_get_allocation(gears->widget, &allocation);
309 	window_get_allocation(gears->window, &window_allocation);
310 
311 	if (display_acquire_window_surface(gears->d,
312 					    gears->window,
313 					    gears->context) < 0) {
314 		die("Unable to acquire window surface, "
315 		    "compiled without cairo-egl?\n");
316 	}
317 
318 	glViewport(allocation.x,
319 		   window_allocation.height - allocation.height - allocation.y,
320 		   allocation.width, allocation.height);
321 	glScissor(allocation.x,
322 		  window_allocation.height - allocation.height - allocation.y,
323 		  allocation.width, allocation.height);
324 
325 	glEnable(GL_SCISSOR_TEST);
326 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
327 
328 	glPushMatrix();
329 
330 	glTranslatef(0.0, 0.0, -50);
331 
332 	glRotatef(gears->view.rotx, 1.0, 0.0, 0.0);
333 	glRotatef(gears->view.roty, 0.0, 1.0, 0.0);
334 
335 	glPushMatrix();
336 	glTranslatef(-3.0, -2.0, 0.0);
337 	glRotatef(gears->angle, 0.0, 0.0, 1.0);
338 	glCallList(gears->gear_list[0]);
339 	glPopMatrix();
340 
341 	glPushMatrix();
342 	glTranslatef(3.1, -2.0, 0.0);
343 	glRotatef(-2.0 * gears->angle - 9.0, 0.0, 0.0, 1.0);
344 	glCallList(gears->gear_list[1]);
345 	glPopMatrix();
346 
347 	glPushMatrix();
348 	glTranslatef(-3.1, 4.2, 0.0);
349 	glRotatef(-2.0 * gears->angle - 25.0, 0.0, 0.0, 1.0);
350 	glCallList(gears->gear_list[2]);
351 	glPopMatrix();
352 
353 	glPopMatrix();
354 
355 	glFlush();
356 
357 	display_release_window_surface(gears->d, gears->window);
358 
359 	callback = wl_surface_frame(window_get_wl_surface(gears->window));
360 	wl_callback_add_listener(callback, &listener, gears);
361 }
362 
363 static void
resize_handler(struct widget * widget,int32_t width,int32_t height,void * data)364 resize_handler(struct widget *widget,
365 	       int32_t width, int32_t height, void *data)
366 {
367 	struct gears *gears = data;
368 	int32_t size, big, small;
369 
370 	/* Constrain child size to be square and at least 300x300 */
371 	if (width < height) {
372 		small = width;
373 		big = height;
374 	} else {
375 		small = height;
376 		big = width;
377 	}
378 
379 	if (gears->fullscreen)
380 		size = small;
381 	else
382 		size = big;
383 
384 	widget_set_size(gears->widget, size, size);
385 }
386 
387 static void
keyboard_focus_handler(struct window * window,struct input * device,void * data)388 keyboard_focus_handler(struct window *window,
389 		       struct input *device, void *data)
390 {
391 	window_schedule_redraw(window);
392 }
393 
394 static void
fullscreen_handler(struct window * window,void * data)395 fullscreen_handler(struct window *window, void *data)
396 {
397 	struct gears *gears = data;
398 
399 	gears->fullscreen ^= 1;
400 	window_set_fullscreen(window, gears->fullscreen);
401 }
402 
403 static struct gears *
gears_create(struct display * display)404 gears_create(struct display *display)
405 {
406 	const int width = 450, height = 500;
407 	struct gears *gears;
408 	int i;
409 
410 	gears = zalloc(sizeof *gears);
411 	gears->d = display;
412 	gears->window = window_create(display);
413 	gears->widget = window_frame_create(gears->window, gears);
414 	window_set_title(gears->window, "Wayland Gears");
415 
416 	gears->display = display_get_egl_display(gears->d);
417 	if (gears->display == NULL)
418 		die("failed to create egl display\n");
419 
420 	eglBindAPI(EGL_OPENGL_API);
421 
422 	gears->config = display_get_argb_egl_config(gears->d);
423 
424 	gears->context = eglCreateContext(gears->display, gears->config,
425 					  EGL_NO_CONTEXT, NULL);
426 	if (gears->context == NULL)
427 		die("failed to create context\n");
428 
429 	if (!eglMakeCurrent(gears->display, NULL, NULL, gears->context))
430 		die("failed to make context current\n");
431 
432 	for (i = 0; i < 3; i++) {
433 		gears->gear_list[i] = glGenLists(1);
434 		glNewList(gears->gear_list[i], GL_COMPILE);
435 		make_gear(&gear_templates[i]);
436 		glEndList();
437 	}
438 
439 	gears->button_down = 0;
440 	gears->last_x = 0;
441 	gears->last_y = 0;
442 
443 	gears->view.rotx = 20.0;
444 	gears->view.roty = 30.0;
445 
446 	printf("Warning: FPS count is limited by the wayland compositor or monitor refresh rate\n");
447 
448 	glEnable(GL_NORMALIZE);
449 
450 	glMatrixMode(GL_PROJECTION);
451 	glLoadIdentity();
452 	glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 200.0);
453 	glMatrixMode(GL_MODELVIEW);
454 
455 	glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
456 	glEnable(GL_CULL_FACE);
457 	glEnable(GL_LIGHTING);
458 	glEnable(GL_LIGHT0);
459 	glEnable(GL_DEPTH_TEST);
460 	glClearColor(0, 0, 0, 0.92);
461 
462 	window_set_user_data(gears->window, gears);
463 	widget_set_resize_handler(gears->widget, resize_handler);
464 	widget_set_redraw_handler(gears->widget, redraw_handler);
465 	widget_set_button_handler(gears->widget, button_handler);
466 	widget_set_motion_handler(gears->widget, motion_handler);
467 	window_set_keyboard_focus_handler(gears->window,
468 					  keyboard_focus_handler);
469 	window_set_fullscreen_handler(gears->window, fullscreen_handler);
470 
471 	window_schedule_resize(gears->window, width, height);
472 
473 	return gears;
474 }
475 
476 static void
gears_destroy(struct gears * gears)477 gears_destroy(struct gears *gears)
478 {
479 	widget_destroy(gears->widget);
480 	window_destroy(gears->window);
481 	free(gears);
482 }
483 
main(int argc,char * argv[])484 int main(int argc, char *argv[])
485 {
486 	struct display *d;
487 	struct gears *gears;
488 
489 	d = display_create(&argc, argv);
490 	if (d == NULL) {
491 		fprintf(stderr, "failed to create display: %m\n");
492 		return -1;
493 	}
494 	gears = gears_create(d);
495 	display_run(d);
496 
497 	gears_destroy(gears);
498 	display_destroy(d);
499 
500 	return 0;
501 }
502