1 /*
2  * Copyright © 2011 Benjamin Franzke
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 <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdbool.h>
30 #include <math.h>
31 #include <assert.h>
32 #include <signal.h>
33 
34 #include <linux/input.h>
35 
36 #include <wayland-client.h>
37 #include <wayland-egl.h>
38 #include <wayland-cursor.h>
39 
40 #include <GLES2/gl2.h>
41 #include <EGL/egl.h>
42 #include <EGL/eglext.h>
43 
44 #include "xdg-shell-client-protocol.h"
45 #include <sys/types.h>
46 #include <unistd.h>
47 #include "protocol/ivi-application-client-protocol.h"
48 #define IVI_SURFACE_ID 9000
49 
50 #include "shared/platform.h"
51 
52 #ifndef EGL_EXT_swap_buffers_with_damage
53 #define EGL_EXT_swap_buffers_with_damage 1
54 typedef EGLBoolean (EGLAPIENTRYP PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)(EGLDisplay dpy, EGLSurface surface, EGLint *rects, EGLint n_rects);
55 #endif
56 
57 #ifndef EGL_EXT_buffer_age
58 #define EGL_EXT_buffer_age 1
59 #define EGL_BUFFER_AGE_EXT			0x313D
60 #endif
61 
62 struct window;
63 struct seat;
64 
65 struct display {
66 	struct wl_display *display;
67 	struct wl_registry *registry;
68 	struct wl_compositor *compositor;
69 	struct xdg_shell *shell;
70 	struct wl_seat *seat;
71 	struct wl_pointer *pointer;
72 	struct wl_touch *touch;
73 	struct wl_keyboard *keyboard;
74 	struct wl_shm *shm;
75 	struct wl_cursor_theme *cursor_theme;
76 	struct wl_cursor *default_cursor;
77 	struct wl_surface *cursor_surface;
78 	struct {
79 		EGLDisplay dpy;
80 		EGLContext ctx;
81 		EGLConfig conf;
82 	} egl;
83 	struct window *window;
84 	struct ivi_application *ivi_application;
85 
86 	PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
87 };
88 
89 struct geometry {
90 	int width, height;
91 };
92 
93 struct window {
94 	struct display *display;
95 	struct geometry geometry, window_size;
96 	struct {
97 		GLuint rotation_uniform;
98 		GLuint pos;
99 		GLuint col;
100 	} gl;
101 
102 	uint32_t benchmark_time, frames;
103 	struct wl_egl_window *native;
104 	struct wl_surface *surface;
105 	struct xdg_surface *xdg_surface;
106 	struct ivi_surface *ivi_surface;
107 	EGLSurface egl_surface;
108 	struct wl_callback *callback;
109 	int fullscreen, opaque, buffer_size, frame_sync;
110 };
111 
112 static const char *vert_shader_text =
113 	"uniform mat4 rotation;\n"
114 	"attribute vec4 pos;\n"
115 	"attribute vec4 color;\n"
116 	"varying vec4 v_color;\n"
117 	"void main() {\n"
118 	"  gl_Position = rotation * pos;\n"
119 	"  v_color = color;\n"
120 	"}\n";
121 
122 static const char *frag_shader_text =
123 	"precision mediump float;\n"
124 	"varying vec4 v_color;\n"
125 	"void main() {\n"
126 	"  gl_FragColor = v_color;\n"
127 	"}\n";
128 
129 static int running = 1;
130 
131 static void
init_egl(struct display * display,struct window * window)132 init_egl(struct display *display, struct window *window)
133 {
134 	static const EGLint context_attribs[] = {
135 		EGL_CONTEXT_CLIENT_VERSION, 2,
136 		EGL_NONE
137 	};
138 	const char *extensions;
139 
140 	EGLint config_attribs[] = {
141 		EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
142 		EGL_RED_SIZE, 1,
143 		EGL_GREEN_SIZE, 1,
144 		EGL_BLUE_SIZE, 1,
145 		EGL_ALPHA_SIZE, 1,
146 		EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
147 		EGL_NONE
148 	};
149 
150 	EGLint major, minor, n, count, i, size;
151 	EGLConfig *configs;
152 	EGLBoolean ret;
153 
154 	if (window->opaque || window->buffer_size == 16)
155 		config_attribs[9] = 0;
156 
157 	display->egl.dpy =
158 		weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
159 						display->display, NULL);
160 	assert(display->egl.dpy);
161 
162 	ret = eglInitialize(display->egl.dpy, &major, &minor);
163 	assert(ret == EGL_TRUE);
164 	ret = eglBindAPI(EGL_OPENGL_ES_API);
165 	assert(ret == EGL_TRUE);
166 
167 	if (!eglGetConfigs(display->egl.dpy, NULL, 0, &count) || count < 1)
168 		assert(0);
169 
170 	configs = calloc(count, sizeof *configs);
171 	assert(configs);
172 
173 	ret = eglChooseConfig(display->egl.dpy, config_attribs,
174 			      configs, count, &n);
175 	assert(ret && n >= 1);
176 
177 	for (i = 0; i < n; i++) {
178 		eglGetConfigAttrib(display->egl.dpy,
179 				   configs[i], EGL_BUFFER_SIZE, &size);
180 		if (window->buffer_size == size) {
181 			display->egl.conf = configs[i];
182 			break;
183 		}
184 	}
185 	free(configs);
186 	if (display->egl.conf == NULL) {
187 		fprintf(stderr, "did not find config with buffer size %d\n",
188 			window->buffer_size);
189 		exit(EXIT_FAILURE);
190 	}
191 
192 	display->egl.ctx = eglCreateContext(display->egl.dpy,
193 					    display->egl.conf,
194 					    EGL_NO_CONTEXT, context_attribs);
195 	assert(display->egl.ctx);
196 
197 	display->swap_buffers_with_damage = NULL;
198 	extensions = eglQueryString(display->egl.dpy, EGL_EXTENSIONS);
199 	if (extensions &&
200 	    strstr(extensions, "EGL_EXT_swap_buffers_with_damage") &&
201 	    strstr(extensions, "EGL_EXT_buffer_age"))
202 		display->swap_buffers_with_damage =
203 			(PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)
204 			eglGetProcAddress("eglSwapBuffersWithDamageEXT");
205 
206 	if (display->swap_buffers_with_damage)
207 		printf("has EGL_EXT_buffer_age and EGL_EXT_swap_buffers_with_damage\n");
208 
209 }
210 
211 static void
fini_egl(struct display * display)212 fini_egl(struct display *display)
213 {
214 	eglTerminate(display->egl.dpy);
215 	eglReleaseThread();
216 }
217 
218 static GLuint
create_shader(struct window * window,const char * source,GLenum shader_type)219 create_shader(struct window *window, const char *source, GLenum shader_type)
220 {
221 	GLuint shader;
222 	GLint status;
223 
224 	shader = glCreateShader(shader_type);
225 	assert(shader != 0);
226 
227 	glShaderSource(shader, 1, (const char **) &source, NULL);
228 	glCompileShader(shader);
229 
230 	glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
231 	if (!status) {
232 		char log[1000];
233 		GLsizei len;
234 		glGetShaderInfoLog(shader, 1000, &len, log);
235 		fprintf(stderr, "Error: compiling %s: %*s\n",
236 			shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
237 			len, log);
238 		exit(1);
239 	}
240 
241 	return shader;
242 }
243 
244 static void
init_gl(struct window * window)245 init_gl(struct window *window)
246 {
247 	GLuint frag, vert;
248 	GLuint program;
249 	GLint status;
250 
251 	frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
252 	vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
253 
254 	program = glCreateProgram();
255 	glAttachShader(program, frag);
256 	glAttachShader(program, vert);
257 	glLinkProgram(program);
258 
259 	glGetProgramiv(program, GL_LINK_STATUS, &status);
260 	if (!status) {
261 		char log[1000];
262 		GLsizei len;
263 		glGetProgramInfoLog(program, 1000, &len, log);
264 		fprintf(stderr, "Error: linking:\n%*s\n", len, log);
265 		exit(1);
266 	}
267 
268 	glUseProgram(program);
269 
270 	window->gl.pos = 0;
271 	window->gl.col = 1;
272 
273 	glBindAttribLocation(program, window->gl.pos, "pos");
274 	glBindAttribLocation(program, window->gl.col, "color");
275 	glLinkProgram(program);
276 
277 	window->gl.rotation_uniform =
278 		glGetUniformLocation(program, "rotation");
279 }
280 
281 static void
handle_surface_configure(void * data,struct xdg_surface * surface,int32_t width,int32_t height,struct wl_array * states,uint32_t serial)282 handle_surface_configure(void *data, struct xdg_surface *surface,
283 			 int32_t width, int32_t height,
284 			 struct wl_array *states, uint32_t serial)
285 {
286 	struct window *window = data;
287 	uint32_t *p;
288 
289 	window->fullscreen = 0;
290 	wl_array_for_each(p, states) {
291 		uint32_t state = *p;
292 		switch (state) {
293 		case XDG_SURFACE_STATE_FULLSCREEN:
294 			window->fullscreen = 1;
295 			break;
296 		}
297 	}
298 
299 	if (width > 0 && height > 0) {
300 		if (!window->fullscreen) {
301 			window->window_size.width = width;
302 			window->window_size.height = height;
303 		}
304 		window->geometry.width = width;
305 		window->geometry.height = height;
306 	} else if (!window->fullscreen) {
307 		window->geometry = window->window_size;
308 	}
309 
310 	if (window->native)
311 		wl_egl_window_resize(window->native,
312 				     window->geometry.width,
313 				     window->geometry.height, 0, 0);
314 
315 	xdg_surface_ack_configure(surface, serial);
316 }
317 
318 static void
handle_surface_delete(void * data,struct xdg_surface * xdg_surface)319 handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
320 {
321 	running = 0;
322 }
323 
324 static const struct xdg_surface_listener xdg_surface_listener = {
325 	handle_surface_configure,
326 	handle_surface_delete,
327 };
328 
329 static void
handle_ivi_surface_configure(void * data,struct ivi_surface * ivi_surface,int32_t width,int32_t height)330 handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
331                              int32_t width, int32_t height)
332 {
333 	struct window *window = data;
334 
335 	wl_egl_window_resize(window->native, width, height, 0, 0);
336 
337 	window->geometry.width = width;
338 	window->geometry.height = height;
339 
340 	if (!window->fullscreen)
341 		window->window_size = window->geometry;
342 }
343 
344 static const struct ivi_surface_listener ivi_surface_listener = {
345 	handle_ivi_surface_configure,
346 };
347 
348 static void
create_xdg_surface(struct window * window,struct display * display)349 create_xdg_surface(struct window *window, struct display *display)
350 {
351 	window->xdg_surface = xdg_shell_get_xdg_surface(display->shell,
352 							window->surface);
353 
354 	xdg_surface_add_listener(window->xdg_surface,
355 				 &xdg_surface_listener, window);
356 
357 	xdg_surface_set_title(window->xdg_surface, "simple-egl");
358 }
359 
360 static void
create_ivi_surface(struct window * window,struct display * display)361 create_ivi_surface(struct window *window, struct display *display)
362 {
363 	uint32_t id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
364 	window->ivi_surface =
365 		ivi_application_surface_create(display->ivi_application,
366 					       id_ivisurf, window->surface);
367 
368 	if (window->ivi_surface == NULL) {
369 		fprintf(stderr, "Failed to create ivi_client_surface\n");
370 		abort();
371 	}
372 
373 	ivi_surface_add_listener(window->ivi_surface,
374 				 &ivi_surface_listener, window);
375 }
376 
377 static void
create_surface(struct window * window)378 create_surface(struct window *window)
379 {
380 	struct display *display = window->display;
381 	EGLBoolean ret;
382 
383 	window->surface = wl_compositor_create_surface(display->compositor);
384 
385 	window->native =
386 		wl_egl_window_create(window->surface,
387 				     window->geometry.width,
388 				     window->geometry.height);
389 	window->egl_surface =
390 		weston_platform_create_egl_surface(display->egl.dpy,
391 						   display->egl.conf,
392 						   window->native, NULL);
393 
394 
395 	if (display->shell) {
396 		create_xdg_surface(window, display);
397 	} else if (display->ivi_application ) {
398 		create_ivi_surface(window, display);
399 	} else {
400 		assert(0);
401 	}
402 
403 	ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
404 			     window->egl_surface, window->display->egl.ctx);
405 	assert(ret == EGL_TRUE);
406 
407 	if (!window->frame_sync)
408 		eglSwapInterval(display->egl.dpy, 0);
409 
410 	if (!display->shell)
411 		return;
412 
413 	if (window->fullscreen)
414 		xdg_surface_set_fullscreen(window->xdg_surface, NULL);
415 }
416 
417 static void
destroy_surface(struct window * window)418 destroy_surface(struct window *window)
419 {
420 	/* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
421 	 * on eglReleaseThread(). */
422 	eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
423 		       EGL_NO_CONTEXT);
424 
425 	eglDestroySurface(window->display->egl.dpy, window->egl_surface);
426 	wl_egl_window_destroy(window->native);
427 
428 	if (window->xdg_surface)
429 		xdg_surface_destroy(window->xdg_surface);
430 	if (window->display->ivi_application)
431 		ivi_surface_destroy(window->ivi_surface);
432 	wl_surface_destroy(window->surface);
433 
434 	if (window->callback)
435 		wl_callback_destroy(window->callback);
436 }
437 
438 static void
redraw(void * data,struct wl_callback * callback,uint32_t time)439 redraw(void *data, struct wl_callback *callback, uint32_t time)
440 {
441 	struct window *window = data;
442 	struct display *display = window->display;
443 	static const GLfloat verts[3][2] = {
444 		{ -0.5, -0.5 },
445 		{  0.5, -0.5 },
446 		{  0,    0.5 }
447 	};
448 	static const GLfloat colors[3][3] = {
449 		{ 1, 0, 0 },
450 		{ 0, 1, 0 },
451 		{ 0, 0, 1 }
452 	};
453 	GLfloat angle;
454 	GLfloat rotation[4][4] = {
455 		{ 1, 0, 0, 0 },
456 		{ 0, 1, 0, 0 },
457 		{ 0, 0, 1, 0 },
458 		{ 0, 0, 0, 1 }
459 	};
460 	static const uint32_t speed_div = 5, benchmark_interval = 5;
461 	struct wl_region *region;
462 	EGLint rect[4];
463 	EGLint buffer_age = 0;
464 	struct timeval tv;
465 
466 	assert(window->callback == callback);
467 	window->callback = NULL;
468 
469 	if (callback)
470 		wl_callback_destroy(callback);
471 
472 	gettimeofday(&tv, NULL);
473 	time = tv.tv_sec * 1000 + tv.tv_usec / 1000;
474 	if (window->frames == 0)
475 		window->benchmark_time = time;
476 	if (time - window->benchmark_time > (benchmark_interval * 1000)) {
477 		printf("%d frames in %d seconds: %f fps\n",
478 		       window->frames,
479 		       benchmark_interval,
480 		       (float) window->frames / benchmark_interval);
481 		window->benchmark_time = time;
482 		window->frames = 0;
483 	}
484 
485 	angle = (time / speed_div) % 360 * M_PI / 180.0;
486 	rotation[0][0] =  cos(angle);
487 	rotation[0][2] =  sin(angle);
488 	rotation[2][0] = -sin(angle);
489 	rotation[2][2] =  cos(angle);
490 
491 	if (display->swap_buffers_with_damage)
492 		eglQuerySurface(display->egl.dpy, window->egl_surface,
493 				EGL_BUFFER_AGE_EXT, &buffer_age);
494 
495 	glViewport(0, 0, window->geometry.width, window->geometry.height);
496 
497 	glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
498 			   (GLfloat *) rotation);
499 
500 	glClearColor(0.0, 0.0, 0.0, 0.5);
501 	glClear(GL_COLOR_BUFFER_BIT);
502 
503 	glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
504 	glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
505 	glEnableVertexAttribArray(window->gl.pos);
506 	glEnableVertexAttribArray(window->gl.col);
507 
508 	glDrawArrays(GL_TRIANGLES, 0, 3);
509 
510 	glDisableVertexAttribArray(window->gl.pos);
511 	glDisableVertexAttribArray(window->gl.col);
512 
513 	if (window->opaque || window->fullscreen) {
514 		region = wl_compositor_create_region(window->display->compositor);
515 		wl_region_add(region, 0, 0,
516 			      window->geometry.width,
517 			      window->geometry.height);
518 		wl_surface_set_opaque_region(window->surface, region);
519 		wl_region_destroy(region);
520 	} else {
521 		wl_surface_set_opaque_region(window->surface, NULL);
522 	}
523 
524 	if (display->swap_buffers_with_damage && buffer_age > 0) {
525 		rect[0] = window->geometry.width / 4 - 1;
526 		rect[1] = window->geometry.height / 4 - 1;
527 		rect[2] = window->geometry.width / 2 + 2;
528 		rect[3] = window->geometry.height / 2 + 2;
529 		display->swap_buffers_with_damage(display->egl.dpy,
530 						  window->egl_surface,
531 						  rect, 1);
532 	} else {
533 		eglSwapBuffers(display->egl.dpy, window->egl_surface);
534 	}
535 	window->frames++;
536 }
537 
538 static void
pointer_handle_enter(void * data,struct wl_pointer * pointer,uint32_t serial,struct wl_surface * surface,wl_fixed_t sx,wl_fixed_t sy)539 pointer_handle_enter(void *data, struct wl_pointer *pointer,
540 		     uint32_t serial, struct wl_surface *surface,
541 		     wl_fixed_t sx, wl_fixed_t sy)
542 {
543 	struct display *display = data;
544 	struct wl_buffer *buffer;
545 	struct wl_cursor *cursor = display->default_cursor;
546 	struct wl_cursor_image *image;
547 
548 	if (display->window->fullscreen)
549 		wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
550 	else if (cursor) {
551 		image = display->default_cursor->images[0];
552 		buffer = wl_cursor_image_get_buffer(image);
553 		if (!buffer)
554 			return;
555 		wl_pointer_set_cursor(pointer, serial,
556 				      display->cursor_surface,
557 				      image->hotspot_x,
558 				      image->hotspot_y);
559 		wl_surface_attach(display->cursor_surface, buffer, 0, 0);
560 		wl_surface_damage(display->cursor_surface, 0, 0,
561 				  image->width, image->height);
562 		wl_surface_commit(display->cursor_surface);
563 	}
564 }
565 
566 static void
pointer_handle_leave(void * data,struct wl_pointer * pointer,uint32_t serial,struct wl_surface * surface)567 pointer_handle_leave(void *data, struct wl_pointer *pointer,
568 		     uint32_t serial, struct wl_surface *surface)
569 {
570 }
571 
572 static void
pointer_handle_motion(void * data,struct wl_pointer * pointer,uint32_t time,wl_fixed_t sx,wl_fixed_t sy)573 pointer_handle_motion(void *data, struct wl_pointer *pointer,
574 		      uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
575 {
576 }
577 
578 static void
pointer_handle_button(void * data,struct wl_pointer * wl_pointer,uint32_t serial,uint32_t time,uint32_t button,uint32_t state)579 pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
580 		      uint32_t serial, uint32_t time, uint32_t button,
581 		      uint32_t state)
582 {
583 	struct display *display = data;
584 
585 	if (!display->window->xdg_surface)
586 		return;
587 
588 	if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
589 		xdg_surface_move(display->window->xdg_surface,
590 						 display->seat, serial);
591 }
592 
593 static void
pointer_handle_axis(void * data,struct wl_pointer * wl_pointer,uint32_t time,uint32_t axis,wl_fixed_t value)594 pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
595 		    uint32_t time, uint32_t axis, wl_fixed_t value)
596 {
597 }
598 
599 static const struct wl_pointer_listener pointer_listener = {
600 	pointer_handle_enter,
601 	pointer_handle_leave,
602 	pointer_handle_motion,
603 	pointer_handle_button,
604 	pointer_handle_axis,
605 };
606 
607 static void
touch_handle_down(void * data,struct wl_touch * wl_touch,uint32_t serial,uint32_t time,struct wl_surface * surface,int32_t id,wl_fixed_t x_w,wl_fixed_t y_w)608 touch_handle_down(void *data, struct wl_touch *wl_touch,
609 		  uint32_t serial, uint32_t time, struct wl_surface *surface,
610 		  int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
611 {
612 	struct display *d = (struct display *)data;
613 
614 	if (!d->shell)
615 		return;
616 
617 	xdg_surface_move(d->window->xdg_surface, d->seat, serial);
618 }
619 
620 static void
touch_handle_up(void * data,struct wl_touch * wl_touch,uint32_t serial,uint32_t time,int32_t id)621 touch_handle_up(void *data, struct wl_touch *wl_touch,
622 		uint32_t serial, uint32_t time, int32_t id)
623 {
624 }
625 
626 static void
touch_handle_motion(void * data,struct wl_touch * wl_touch,uint32_t time,int32_t id,wl_fixed_t x_w,wl_fixed_t y_w)627 touch_handle_motion(void *data, struct wl_touch *wl_touch,
628 		    uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
629 {
630 }
631 
632 static void
touch_handle_frame(void * data,struct wl_touch * wl_touch)633 touch_handle_frame(void *data, struct wl_touch *wl_touch)
634 {
635 }
636 
637 static void
touch_handle_cancel(void * data,struct wl_touch * wl_touch)638 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
639 {
640 }
641 
642 static const struct wl_touch_listener touch_listener = {
643 	touch_handle_down,
644 	touch_handle_up,
645 	touch_handle_motion,
646 	touch_handle_frame,
647 	touch_handle_cancel,
648 };
649 
650 static void
keyboard_handle_keymap(void * data,struct wl_keyboard * keyboard,uint32_t format,int fd,uint32_t size)651 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
652 		       uint32_t format, int fd, uint32_t size)
653 {
654 }
655 
656 static void
keyboard_handle_enter(void * data,struct wl_keyboard * keyboard,uint32_t serial,struct wl_surface * surface,struct wl_array * keys)657 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
658 		      uint32_t serial, struct wl_surface *surface,
659 		      struct wl_array *keys)
660 {
661 }
662 
663 static void
keyboard_handle_leave(void * data,struct wl_keyboard * keyboard,uint32_t serial,struct wl_surface * surface)664 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
665 		      uint32_t serial, struct wl_surface *surface)
666 {
667 }
668 
669 static void
keyboard_handle_key(void * data,struct wl_keyboard * keyboard,uint32_t serial,uint32_t time,uint32_t key,uint32_t state)670 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
671 		    uint32_t serial, uint32_t time, uint32_t key,
672 		    uint32_t state)
673 {
674 	struct display *d = data;
675 
676 	if (!d->shell)
677 		return;
678 
679 	if (key == KEY_F11 && state) {
680 		if (d->window->fullscreen)
681 			xdg_surface_unset_fullscreen(d->window->xdg_surface);
682 		else
683 			xdg_surface_set_fullscreen(d->window->xdg_surface, NULL);
684 	} else if (key == KEY_ESC && state)
685 		running = 0;
686 }
687 
688 static void
keyboard_handle_modifiers(void * data,struct wl_keyboard * keyboard,uint32_t serial,uint32_t mods_depressed,uint32_t mods_latched,uint32_t mods_locked,uint32_t group)689 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
690 			  uint32_t serial, uint32_t mods_depressed,
691 			  uint32_t mods_latched, uint32_t mods_locked,
692 			  uint32_t group)
693 {
694 }
695 
696 static const struct wl_keyboard_listener keyboard_listener = {
697 	keyboard_handle_keymap,
698 	keyboard_handle_enter,
699 	keyboard_handle_leave,
700 	keyboard_handle_key,
701 	keyboard_handle_modifiers,
702 };
703 
704 static void
seat_handle_capabilities(void * data,struct wl_seat * seat,enum wl_seat_capability caps)705 seat_handle_capabilities(void *data, struct wl_seat *seat,
706 			 enum wl_seat_capability caps)
707 {
708 	struct display *d = data;
709 
710 	if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
711 		d->pointer = wl_seat_get_pointer(seat);
712 		wl_pointer_add_listener(d->pointer, &pointer_listener, d);
713 	} else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
714 		wl_pointer_destroy(d->pointer);
715 		d->pointer = NULL;
716 	}
717 
718 	if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
719 		d->keyboard = wl_seat_get_keyboard(seat);
720 		wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
721 	} else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
722 		wl_keyboard_destroy(d->keyboard);
723 		d->keyboard = NULL;
724 	}
725 
726 	if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !d->touch) {
727 		d->touch = wl_seat_get_touch(seat);
728 		wl_touch_set_user_data(d->touch, d);
729 		wl_touch_add_listener(d->touch, &touch_listener, d);
730 	} else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && d->touch) {
731 		wl_touch_destroy(d->touch);
732 		d->touch = NULL;
733 	}
734 }
735 
736 static const struct wl_seat_listener seat_listener = {
737 	seat_handle_capabilities,
738 };
739 
740 static void
xdg_shell_ping(void * data,struct xdg_shell * shell,uint32_t serial)741 xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
742 {
743 	xdg_shell_pong(shell, serial);
744 }
745 
746 static const struct xdg_shell_listener xdg_shell_listener = {
747 	xdg_shell_ping,
748 };
749 
750 #define XDG_VERSION 5 /* The version of xdg-shell that we implement */
751 #ifdef static_assert
752 static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
753 	      "Interface version doesn't match implementation version");
754 #endif
755 
756 static void
registry_handle_global(void * data,struct wl_registry * registry,uint32_t name,const char * interface,uint32_t version)757 registry_handle_global(void *data, struct wl_registry *registry,
758 		       uint32_t name, const char *interface, uint32_t version)
759 {
760 	struct display *d = data;
761 
762 	if (strcmp(interface, "wl_compositor") == 0) {
763 		d->compositor =
764 			wl_registry_bind(registry, name,
765 					 &wl_compositor_interface, 1);
766 	} else if (strcmp(interface, "xdg_shell") == 0) {
767 		d->shell = wl_registry_bind(registry, name,
768 					    &xdg_shell_interface, 1);
769 		xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);
770 		xdg_shell_use_unstable_version(d->shell, XDG_VERSION);
771 	} else if (strcmp(interface, "wl_seat") == 0) {
772 		d->seat = wl_registry_bind(registry, name,
773 					   &wl_seat_interface, 1);
774 		wl_seat_add_listener(d->seat, &seat_listener, d);
775 	} else if (strcmp(interface, "wl_shm") == 0) {
776 		d->shm = wl_registry_bind(registry, name,
777 					  &wl_shm_interface, 1);
778 		d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
779 		if (!d->cursor_theme) {
780 			fprintf(stderr, "unable to load default theme\n");
781 			return;
782 		}
783 		d->default_cursor =
784 			wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
785 		if (!d->default_cursor) {
786 			fprintf(stderr, "unable to load default left pointer\n");
787 			// TODO: abort ?
788 		}
789 	} else if (strcmp(interface, "ivi_application") == 0) {
790 		d->ivi_application =
791 			wl_registry_bind(registry, name,
792 					 &ivi_application_interface, 1);
793 	}
794 }
795 
796 static void
registry_handle_global_remove(void * data,struct wl_registry * registry,uint32_t name)797 registry_handle_global_remove(void *data, struct wl_registry *registry,
798 			      uint32_t name)
799 {
800 }
801 
802 static const struct wl_registry_listener registry_listener = {
803 	registry_handle_global,
804 	registry_handle_global_remove
805 };
806 
807 static void
signal_int(int signum)808 signal_int(int signum)
809 {
810 	running = 0;
811 }
812 
813 static void
usage(int error_code)814 usage(int error_code)
815 {
816 	fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
817 		"  -f\tRun in fullscreen mode\n"
818 		"  -o\tCreate an opaque surface\n"
819 		"  -s\tUse a 16 bpp EGL config\n"
820 		"  -b\tDon't sync to compositor redraw (eglSwapInterval 0)\n"
821 		"  -h\tThis help text\n\n");
822 
823 	exit(error_code);
824 }
825 
826 int
main(int argc,char ** argv)827 main(int argc, char **argv)
828 {
829 	struct sigaction sigint;
830 	struct display display = { 0 };
831 	struct window  window  = { 0 };
832 	int i, ret = 0;
833 
834 	window.display = &display;
835 	display.window = &window;
836 	window.geometry.width  = 250;
837 	window.geometry.height = 250;
838 	window.window_size = window.geometry;
839 	window.buffer_size = 32;
840 	window.frame_sync = 1;
841 
842 	for (i = 1; i < argc; i++) {
843 		if (strcmp("-f", argv[i]) == 0)
844 			window.fullscreen = 1;
845 		else if (strcmp("-o", argv[i]) == 0)
846 			window.opaque = 1;
847 		else if (strcmp("-s", argv[i]) == 0)
848 			window.buffer_size = 16;
849 		else if (strcmp("-b", argv[i]) == 0)
850 			window.frame_sync = 0;
851 		else if (strcmp("-h", argv[i]) == 0)
852 			usage(EXIT_SUCCESS);
853 		else
854 			usage(EXIT_FAILURE);
855 	}
856 
857 	display.display = wl_display_connect(NULL);
858 	assert(display.display);
859 
860 	display.registry = wl_display_get_registry(display.display);
861 	wl_registry_add_listener(display.registry,
862 				 &registry_listener, &display);
863 
864 	wl_display_dispatch(display.display);
865 
866 	init_egl(&display, &window);
867 	create_surface(&window);
868 	init_gl(&window);
869 
870 	display.cursor_surface =
871 		wl_compositor_create_surface(display.compositor);
872 
873 	sigint.sa_handler = signal_int;
874 	sigemptyset(&sigint.sa_mask);
875 	sigint.sa_flags = SA_RESETHAND;
876 	sigaction(SIGINT, &sigint, NULL);
877 
878 	/* The mainloop here is a little subtle.  Redrawing will cause
879 	 * EGL to read events so we can just call
880 	 * wl_display_dispatch_pending() to handle any events that got
881 	 * queued up as a side effect. */
882 	while (running && ret != -1) {
883 		wl_display_dispatch_pending(display.display);
884 		redraw(&window, NULL, 0);
885 	}
886 
887 	fprintf(stderr, "simple-egl exiting\n");
888 
889 	destroy_surface(&window);
890 	fini_egl(&display);
891 
892 	wl_surface_destroy(display.cursor_surface);
893 	if (display.cursor_theme)
894 		wl_cursor_theme_destroy(display.cursor_theme);
895 
896 	if (display.shell)
897 		xdg_shell_destroy(display.shell);
898 
899 	if (display.ivi_application)
900 		ivi_application_destroy(display.ivi_application);
901 
902 	if (display.compositor)
903 		wl_compositor_destroy(display.compositor);
904 
905 	wl_registry_destroy(display.registry);
906 	wl_display_flush(display.display);
907 	wl_display_disconnect(display.display);
908 
909 	return 0;
910 }
911