2 * Copyright © 2011 Benjamin Franzke
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.
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
31 #include <linux/input.h>
33 #include <wayland-client.h>
34 #include <wayland-egl.h>
36 #include <GLES2/gl2.h>
43 struct wl_display *display;
44 struct wl_compositor *compositor;
45 struct wl_shell *shell;
47 struct wl_pointer *pointer;
48 struct wl_keyboard *keyboard;
55 struct window *window;
63 struct display *display;
64 struct geometry geometry, window_size;
69 GLuint rotation_uniform;
75 struct wl_egl_window *native;
76 struct wl_surface *surface;
77 struct wl_shell_surface *shell_surface;
78 EGLSurface egl_surface;
79 struct wl_callback *callback;
80 int fullscreen, configured, opaque;
83 static const char *vert_shader_text =
84 "uniform mat4 rotation;\n"
85 "attribute vec4 pos;\n"
86 "attribute vec4 color;\n"
87 "varying vec4 v_color;\n"
89 " gl_Position = rotation * pos;\n"
93 static const char *frag_shader_text =
94 "precision mediump float;\n"
95 "varying vec4 v_color;\n"
97 " gl_FragColor = v_color;\n"
100 static int running = 1;
103 init_egl(struct display *display, int opaque)
105 static const EGLint context_attribs[] = {
106 EGL_CONTEXT_CLIENT_VERSION, 2,
110 EGLint config_attribs[] = {
111 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
116 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
120 EGLint major, minor, n;
124 config_attribs[9] = 0;
126 display->egl.dpy = eglGetDisplay(display->display);
127 assert(display->egl.dpy);
129 ret = eglInitialize(display->egl.dpy, &major, &minor);
130 assert(ret == EGL_TRUE);
131 ret = eglBindAPI(EGL_OPENGL_ES_API);
132 assert(ret == EGL_TRUE);
134 ret = eglChooseConfig(display->egl.dpy, config_attribs,
135 &display->egl.conf, 1, &n);
136 assert(ret && n == 1);
138 display->egl.ctx = eglCreateContext(display->egl.dpy,
140 EGL_NO_CONTEXT, context_attribs);
141 assert(display->egl.ctx);
146 fini_egl(struct display *display)
148 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
149 * on eglReleaseThread(). */
150 eglMakeCurrent(display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
153 eglTerminate(display->egl.dpy);
158 create_shader(struct window *window, const char *source, GLenum shader_type)
163 shader = glCreateShader(shader_type);
166 glShaderSource(shader, 1, (const char **) &source, NULL);
167 glCompileShader(shader);
169 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
173 glGetShaderInfoLog(shader, 1000, &len, log);
174 fprintf(stderr, "Error: compiling %s: %*s\n",
175 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
184 init_gl(struct window *window)
190 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
191 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
193 program = glCreateProgram();
194 glAttachShader(program, frag);
195 glAttachShader(program, vert);
196 glLinkProgram(program);
198 glGetProgramiv(program, GL_LINK_STATUS, &status);
202 glGetProgramInfoLog(program, 1000, &len, log);
203 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
207 glUseProgram(program);
212 glBindAttribLocation(program, window->gl.pos, "pos");
213 glBindAttribLocation(program, window->gl.col, "color");
214 glLinkProgram(program);
216 window->gl.rotation_uniform =
217 glGetUniformLocation(program, "rotation");
221 handle_ping(void *data, struct wl_shell_surface *shell_surface,
224 wl_shell_surface_pong(shell_surface, serial);
228 handle_configure(void *data, struct wl_shell_surface *shell_surface,
229 uint32_t edges, int32_t width, int32_t height)
231 struct window *window = data;
234 wl_egl_window_resize(window->native, width, height, 0, 0);
236 window->geometry.width = width;
237 window->geometry.height = height;
239 if (!window->fullscreen)
240 window->window_size = window->geometry;
244 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
248 static const struct wl_shell_surface_listener shell_surface_listener = {
255 redraw(void *data, struct wl_callback *callback, uint32_t time);
258 configure_callback(void *data, struct wl_callback *callback, uint32_t time)
260 struct window *window = data;
262 wl_callback_destroy(callback);
264 window->configured = 1;
266 if (window->callback == NULL)
267 redraw(data, NULL, time);
270 static struct wl_callback_listener configure_callback_listener = {
275 toggle_fullscreen(struct window *window, int fullscreen)
277 struct wl_callback *callback;
279 window->fullscreen = fullscreen;
280 window->configured = 0;
283 wl_shell_surface_set_fullscreen(window->shell_surface,
284 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
287 wl_shell_surface_set_toplevel(window->shell_surface);
288 handle_configure(window, window->shell_surface, 0,
289 window->window_size.width,
290 window->window_size.height);
293 callback = wl_display_sync(window->display->display);
294 wl_callback_add_listener(callback, &configure_callback_listener,
299 create_surface(struct window *window)
301 struct display *display = window->display;
304 window->surface = wl_compositor_create_surface(display->compositor);
305 window->shell_surface = wl_shell_get_shell_surface(display->shell,
308 wl_shell_surface_add_listener(window->shell_surface,
309 &shell_surface_listener, window);
312 wl_egl_window_create(window->surface,
313 window->window_size.width,
314 window->window_size.height);
315 window->egl_surface =
316 eglCreateWindowSurface(display->egl.dpy,
318 window->native, NULL);
320 wl_shell_surface_set_title(window->shell_surface, "simple-egl");
322 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
323 window->egl_surface, window->display->egl.ctx);
324 assert(ret == EGL_TRUE);
326 toggle_fullscreen(window, window->fullscreen);
330 destroy_surface(struct window *window)
332 wl_egl_window_destroy(window->native);
334 wl_shell_surface_destroy(window->shell_surface);
335 wl_surface_destroy(window->surface);
337 if (window->callback)
338 wl_callback_destroy(window->callback);
341 static const struct wl_callback_listener frame_listener;
344 redraw(void *data, struct wl_callback *callback, uint32_t time)
346 struct window *window = data;
347 static const GLfloat verts[3][2] = {
352 static const GLfloat colors[3][3] = {
358 GLfloat rotation[4][4] = {
364 static const int32_t speed_div = 5;
365 static uint32_t start_time = 0;
366 struct wl_region *region;
368 assert(window->callback == callback);
369 window->callback = NULL;
372 wl_callback_destroy(callback);
374 if (!window->configured)
380 angle = ((time-start_time) / speed_div) % 360 * M_PI / 180.0;
381 rotation[0][0] = cos(angle);
382 rotation[0][2] = sin(angle);
383 rotation[2][0] = -sin(angle);
384 rotation[2][2] = cos(angle);
386 glViewport(0, 0, window->geometry.width, window->geometry.height);
388 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
389 (GLfloat *) rotation);
391 glClearColor(0.0, 0.0, 0.0, 0.5);
392 glClear(GL_COLOR_BUFFER_BIT);
394 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
395 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
396 glEnableVertexAttribArray(window->gl.pos);
397 glEnableVertexAttribArray(window->gl.col);
399 glDrawArrays(GL_TRIANGLES, 0, 3);
401 glDisableVertexAttribArray(window->gl.pos);
402 glDisableVertexAttribArray(window->gl.col);
404 eglSwapBuffers(window->display->egl.dpy, window->egl_surface);
406 if (window->opaque || window->fullscreen) {
407 region = wl_compositor_create_region(window->display->compositor);
408 wl_region_add(region, 0, 0,
409 window->window_size.width,
410 window->window_size.height);
411 wl_surface_set_opaque_region(window->surface, region);
412 wl_region_destroy(region);
415 window->callback = wl_surface_frame(window->surface);
416 wl_callback_add_listener(window->callback, &frame_listener, window);
419 static const struct wl_callback_listener frame_listener = {
424 pointer_handle_enter(void *data, struct wl_pointer *pointer,
425 uint32_t serial, struct wl_surface *surface,
426 wl_fixed_t sx, wl_fixed_t sy)
428 struct display *display = data;
430 if (display->window->fullscreen)
431 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
435 pointer_handle_leave(void *data, struct wl_pointer *pointer,
436 uint32_t serial, struct wl_surface *surface)
441 pointer_handle_motion(void *data, struct wl_pointer *pointer,
442 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
447 pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
448 uint32_t serial, uint32_t time, uint32_t button,
451 struct display *display = data;
453 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
454 wl_shell_surface_move(display->window->shell_surface,
455 display->seat, serial);
459 pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
460 uint32_t time, uint32_t axis, wl_fixed_t value)
464 static const struct wl_pointer_listener pointer_listener = {
465 pointer_handle_enter,
466 pointer_handle_leave,
467 pointer_handle_motion,
468 pointer_handle_button,
473 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
474 uint32_t format, int fd, uint32_t size)
479 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
480 uint32_t serial, struct wl_surface *surface,
481 struct wl_array *keys)
486 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
487 uint32_t serial, struct wl_surface *surface)
492 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
493 uint32_t serial, uint32_t time, uint32_t key,
496 struct display *d = data;
498 if (key == KEY_F11 && state)
499 toggle_fullscreen(d->window, d->window->fullscreen ^ 1);
500 else if (key == KEY_ESC && state)
505 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
506 uint32_t serial, uint32_t mods_depressed,
507 uint32_t mods_latched, uint32_t mods_locked,
512 static const struct wl_keyboard_listener keyboard_listener = {
513 keyboard_handle_keymap,
514 keyboard_handle_enter,
515 keyboard_handle_leave,
517 keyboard_handle_modifiers,
521 seat_handle_capabilities(void *data, struct wl_seat *seat,
522 enum wl_seat_capability caps)
524 struct display *d = data;
526 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
527 d->pointer = wl_seat_get_pointer(seat);
528 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
529 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
530 wl_pointer_destroy(d->pointer);
534 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
535 d->keyboard = wl_seat_get_keyboard(seat);
536 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
537 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
538 wl_keyboard_destroy(d->keyboard);
543 static const struct wl_seat_listener seat_listener = {
544 seat_handle_capabilities,
548 display_handle_global(struct wl_display *display, uint32_t id,
549 const char *interface, uint32_t version, void *data)
551 struct display *d = data;
553 if (strcmp(interface, "wl_compositor") == 0) {
555 wl_display_bind(display, id, &wl_compositor_interface);
556 } else if (strcmp(interface, "wl_shell") == 0) {
557 d->shell = wl_display_bind(display, id, &wl_shell_interface);
558 } else if (strcmp(interface, "wl_seat") == 0) {
559 d->seat = wl_display_bind(d->display, id, &wl_seat_interface);
560 wl_seat_add_listener(d->seat, &seat_listener, d);
565 event_mask_update(uint32_t mask, void *data)
567 struct display *d = data;
575 signal_int(int signum)
581 usage(int error_code)
583 fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
584 " -f\tRun in fullscreen mode\n"
585 " -o\tCreate an opaque surface\n"
586 " -h\tThis help text\n\n");
592 main(int argc, char **argv)
594 struct sigaction sigint;
595 struct display display = { 0 };
596 struct window window = { 0 };
599 window.display = &display;
600 display.window = &window;
601 window.window_size.width = 250;
602 window.window_size.height = 250;
604 for (i = 1; i < argc; i++) {
605 if (strcmp("-f", argv[i]) == 0)
606 window.fullscreen = 1;
607 else if (strcmp("-o", argv[i]) == 0)
609 else if (strcmp("-h", argv[i]) == 0)
615 display.display = wl_display_connect(NULL);
616 assert(display.display);
618 wl_display_add_global_listener(display.display,
619 display_handle_global, &display);
621 wl_display_get_fd(display.display, event_mask_update, &display);
622 wl_display_iterate(display.display, WL_DISPLAY_READABLE);
624 init_egl(&display, window.opaque);
625 create_surface(&window);
628 sigint.sa_handler = signal_int;
629 sigemptyset(&sigint.sa_mask);
630 sigint.sa_flags = SA_RESETHAND;
631 sigaction(SIGINT, &sigint, NULL);
634 wl_display_iterate(display.display, display.mask);
636 fprintf(stderr, "simple-egl exiting\n");
638 destroy_surface(&window);
642 wl_shell_destroy(display.shell);
644 if (display.compositor)
645 wl_compositor_destroy(display.compositor);
647 wl_display_flush(display.display);
648 wl_display_disconnect(display.display);