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>
35 #include <wayland-cursor.h>
37 #include <GLES2/gl2.h>
44 struct wl_display *display;
45 struct wl_registry *registry;
46 struct wl_compositor *compositor;
47 struct wl_shell *shell;
49 struct wl_pointer *pointer;
50 struct wl_keyboard *keyboard;
52 struct wl_cursor_theme *cursor_theme;
53 struct wl_cursor *default_cursor;
54 struct wl_surface *cursor_surface;
60 struct window *window;
68 struct display *display;
69 struct geometry geometry, window_size;
74 GLuint rotation_uniform;
80 struct wl_egl_window *native;
81 struct wl_surface *surface;
82 struct wl_shell_surface *shell_surface;
83 EGLSurface egl_surface;
84 struct wl_callback *callback;
85 int fullscreen, configured, opaque;
88 static const char *vert_shader_text =
89 "uniform mat4 rotation;\n"
90 "attribute vec4 pos;\n"
91 "attribute vec4 color;\n"
92 "varying vec4 v_color;\n"
94 " gl_Position = rotation * pos;\n"
98 static const char *frag_shader_text =
99 "precision mediump float;\n"
100 "varying vec4 v_color;\n"
102 " gl_FragColor = v_color;\n"
105 static int running = 1;
108 init_egl(struct display *display, int opaque)
110 static const EGLint context_attribs[] = {
111 EGL_CONTEXT_CLIENT_VERSION, 2,
115 EGLint config_attribs[] = {
116 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
121 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
125 EGLint major, minor, n;
129 config_attribs[9] = 0;
131 display->egl.dpy = eglGetDisplay(display->display);
132 assert(display->egl.dpy);
134 ret = eglInitialize(display->egl.dpy, &major, &minor);
135 assert(ret == EGL_TRUE);
136 ret = eglBindAPI(EGL_OPENGL_ES_API);
137 assert(ret == EGL_TRUE);
139 ret = eglChooseConfig(display->egl.dpy, config_attribs,
140 &display->egl.conf, 1, &n);
141 assert(ret && n == 1);
143 display->egl.ctx = eglCreateContext(display->egl.dpy,
145 EGL_NO_CONTEXT, context_attribs);
146 assert(display->egl.ctx);
151 fini_egl(struct display *display)
153 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
154 * on eglReleaseThread(). */
155 eglMakeCurrent(display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
158 eglTerminate(display->egl.dpy);
163 create_shader(struct window *window, const char *source, GLenum shader_type)
168 shader = glCreateShader(shader_type);
171 glShaderSource(shader, 1, (const char **) &source, NULL);
172 glCompileShader(shader);
174 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
178 glGetShaderInfoLog(shader, 1000, &len, log);
179 fprintf(stderr, "Error: compiling %s: %*s\n",
180 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
189 init_gl(struct window *window)
195 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
196 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
198 program = glCreateProgram();
199 glAttachShader(program, frag);
200 glAttachShader(program, vert);
201 glLinkProgram(program);
203 glGetProgramiv(program, GL_LINK_STATUS, &status);
207 glGetProgramInfoLog(program, 1000, &len, log);
208 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
212 glUseProgram(program);
217 glBindAttribLocation(program, window->gl.pos, "pos");
218 glBindAttribLocation(program, window->gl.col, "color");
219 glLinkProgram(program);
221 window->gl.rotation_uniform =
222 glGetUniformLocation(program, "rotation");
226 handle_ping(void *data, struct wl_shell_surface *shell_surface,
229 wl_shell_surface_pong(shell_surface, serial);
233 handle_configure(void *data, struct wl_shell_surface *shell_surface,
234 uint32_t edges, int32_t width, int32_t height)
236 struct window *window = data;
239 wl_egl_window_resize(window->native, width, height, 0, 0);
241 window->geometry.width = width;
242 window->geometry.height = height;
244 if (!window->fullscreen)
245 window->window_size = window->geometry;
249 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
253 static const struct wl_shell_surface_listener shell_surface_listener = {
260 redraw(void *data, struct wl_callback *callback, uint32_t time);
263 configure_callback(void *data, struct wl_callback *callback, uint32_t time)
265 struct window *window = data;
267 wl_callback_destroy(callback);
269 window->configured = 1;
271 if (window->callback == NULL)
272 redraw(data, NULL, time);
275 static struct wl_callback_listener configure_callback_listener = {
280 toggle_fullscreen(struct window *window, int fullscreen)
282 struct wl_callback *callback;
284 window->fullscreen = fullscreen;
285 window->configured = 0;
288 wl_shell_surface_set_fullscreen(window->shell_surface,
289 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
292 wl_shell_surface_set_toplevel(window->shell_surface);
293 handle_configure(window, window->shell_surface, 0,
294 window->window_size.width,
295 window->window_size.height);
298 callback = wl_display_sync(window->display->display);
299 wl_callback_add_listener(callback, &configure_callback_listener,
304 create_surface(struct window *window)
306 struct display *display = window->display;
309 window->surface = wl_compositor_create_surface(display->compositor);
310 window->shell_surface = wl_shell_get_shell_surface(display->shell,
313 wl_shell_surface_add_listener(window->shell_surface,
314 &shell_surface_listener, window);
317 wl_egl_window_create(window->surface,
318 window->window_size.width,
319 window->window_size.height);
320 window->egl_surface =
321 eglCreateWindowSurface(display->egl.dpy,
323 window->native, NULL);
325 wl_shell_surface_set_title(window->shell_surface, "simple-egl");
327 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
328 window->egl_surface, window->display->egl.ctx);
329 assert(ret == EGL_TRUE);
331 toggle_fullscreen(window, window->fullscreen);
335 destroy_surface(struct window *window)
337 wl_egl_window_destroy(window->native);
339 wl_shell_surface_destroy(window->shell_surface);
340 wl_surface_destroy(window->surface);
342 if (window->callback)
343 wl_callback_destroy(window->callback);
346 static const struct wl_callback_listener frame_listener;
349 redraw(void *data, struct wl_callback *callback, uint32_t time)
351 struct window *window = data;
352 static const GLfloat verts[3][2] = {
357 static const GLfloat colors[3][3] = {
363 GLfloat rotation[4][4] = {
369 static const int32_t speed_div = 5;
370 static uint32_t start_time = 0;
371 struct wl_region *region;
373 assert(window->callback == callback);
374 window->callback = NULL;
377 wl_callback_destroy(callback);
379 if (!window->configured)
385 angle = ((time-start_time) / speed_div) % 360 * M_PI / 180.0;
386 rotation[0][0] = cos(angle);
387 rotation[0][2] = sin(angle);
388 rotation[2][0] = -sin(angle);
389 rotation[2][2] = cos(angle);
391 glViewport(0, 0, window->geometry.width, window->geometry.height);
393 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
394 (GLfloat *) rotation);
396 glClearColor(0.0, 0.0, 0.0, 0.5);
397 glClear(GL_COLOR_BUFFER_BIT);
399 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
400 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
401 glEnableVertexAttribArray(window->gl.pos);
402 glEnableVertexAttribArray(window->gl.col);
404 glDrawArrays(GL_TRIANGLES, 0, 3);
406 glDisableVertexAttribArray(window->gl.pos);
407 glDisableVertexAttribArray(window->gl.col);
409 if (window->opaque || window->fullscreen) {
410 region = wl_compositor_create_region(window->display->compositor);
411 wl_region_add(region, 0, 0,
412 window->geometry.width,
413 window->geometry.height);
414 wl_surface_set_opaque_region(window->surface, region);
415 wl_region_destroy(region);
417 wl_surface_set_opaque_region(window->surface, NULL);
420 window->callback = wl_surface_frame(window->surface);
421 wl_callback_add_listener(window->callback, &frame_listener, window);
423 eglSwapBuffers(window->display->egl.dpy, window->egl_surface);
426 static const struct wl_callback_listener frame_listener = {
431 pointer_handle_enter(void *data, struct wl_pointer *pointer,
432 uint32_t serial, struct wl_surface *surface,
433 wl_fixed_t sx, wl_fixed_t sy)
435 struct display *display = data;
436 struct wl_buffer *buffer;
437 struct wl_cursor *cursor = display->default_cursor;
438 struct wl_cursor_image *image;
440 if (display->window->fullscreen)
441 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
443 image = display->default_cursor->images[0];
444 buffer = wl_cursor_image_get_buffer(image);
445 wl_pointer_set_cursor(pointer, serial,
446 display->cursor_surface,
449 wl_surface_attach(display->cursor_surface, buffer, 0, 0);
450 wl_surface_damage(display->cursor_surface, 0, 0,
451 image->width, image->height);
452 wl_surface_commit(display->cursor_surface);
457 pointer_handle_leave(void *data, struct wl_pointer *pointer,
458 uint32_t serial, struct wl_surface *surface)
463 pointer_handle_motion(void *data, struct wl_pointer *pointer,
464 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
469 pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
470 uint32_t serial, uint32_t time, uint32_t button,
473 struct display *display = data;
475 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
476 wl_shell_surface_move(display->window->shell_surface,
477 display->seat, serial);
481 pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
482 uint32_t time, uint32_t axis, wl_fixed_t value)
486 static const struct wl_pointer_listener pointer_listener = {
487 pointer_handle_enter,
488 pointer_handle_leave,
489 pointer_handle_motion,
490 pointer_handle_button,
495 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
496 uint32_t format, int fd, uint32_t size)
501 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
502 uint32_t serial, struct wl_surface *surface,
503 struct wl_array *keys)
508 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
509 uint32_t serial, struct wl_surface *surface)
514 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
515 uint32_t serial, uint32_t time, uint32_t key,
518 struct display *d = data;
520 if (key == KEY_F11 && state)
521 toggle_fullscreen(d->window, d->window->fullscreen ^ 1);
522 else if (key == KEY_ESC && state)
527 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
528 uint32_t serial, uint32_t mods_depressed,
529 uint32_t mods_latched, uint32_t mods_locked,
534 static const struct wl_keyboard_listener keyboard_listener = {
535 keyboard_handle_keymap,
536 keyboard_handle_enter,
537 keyboard_handle_leave,
539 keyboard_handle_modifiers,
543 seat_handle_capabilities(void *data, struct wl_seat *seat,
544 enum wl_seat_capability caps)
546 struct display *d = data;
548 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
549 d->pointer = wl_seat_get_pointer(seat);
550 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
551 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
552 wl_pointer_destroy(d->pointer);
556 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
557 d->keyboard = wl_seat_get_keyboard(seat);
558 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
559 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
560 wl_keyboard_destroy(d->keyboard);
565 static const struct wl_seat_listener seat_listener = {
566 seat_handle_capabilities,
570 registry_handle_global(void *data, struct wl_registry *registry,
571 uint32_t name, const char *interface, uint32_t version)
573 struct display *d = data;
575 if (strcmp(interface, "wl_compositor") == 0) {
577 wl_registry_bind(registry, name,
578 &wl_compositor_interface, 1);
579 } else if (strcmp(interface, "wl_shell") == 0) {
580 d->shell = wl_registry_bind(registry, name,
581 &wl_shell_interface, 1);
582 } else if (strcmp(interface, "wl_seat") == 0) {
583 d->seat = wl_registry_bind(registry, name,
584 &wl_seat_interface, 1);
585 wl_seat_add_listener(d->seat, &seat_listener, d);
586 } else if (strcmp(interface, "wl_shm") == 0) {
587 d->shm = wl_registry_bind(registry, name,
588 &wl_shm_interface, 1);
589 d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
591 wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
595 static const struct wl_registry_listener registry_listener = {
596 registry_handle_global
600 signal_int(int signum)
606 usage(int error_code)
608 fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
609 " -f\tRun in fullscreen mode\n"
610 " -o\tCreate an opaque surface\n"
611 " -h\tThis help text\n\n");
617 main(int argc, char **argv)
619 struct sigaction sigint;
620 struct display display = { 0 };
621 struct window window = { 0 };
624 window.display = &display;
625 display.window = &window;
626 window.window_size.width = 250;
627 window.window_size.height = 250;
629 for (i = 1; i < argc; i++) {
630 if (strcmp("-f", argv[i]) == 0)
631 window.fullscreen = 1;
632 else if (strcmp("-o", argv[i]) == 0)
634 else if (strcmp("-h", argv[i]) == 0)
640 display.display = wl_display_connect(NULL);
641 assert(display.display);
643 display.registry = wl_display_get_registry(display.display);
644 wl_registry_add_listener(display.registry,
645 ®istry_listener, &display);
647 wl_display_dispatch(display.display);
649 init_egl(&display, window.opaque);
650 create_surface(&window);
653 display.cursor_surface =
654 wl_compositor_create_surface(display.compositor);
656 sigint.sa_handler = signal_int;
657 sigemptyset(&sigint.sa_mask);
658 sigint.sa_flags = SA_RESETHAND;
659 sigaction(SIGINT, &sigint, NULL);
661 while (running && ret != -1)
662 ret = wl_display_dispatch(display.display);
664 fprintf(stderr, "simple-egl exiting\n");
666 destroy_surface(&window);
669 wl_surface_destroy(display.cursor_surface);
670 if (display.cursor_theme)
671 wl_cursor_theme_destroy(display.cursor_theme);
674 wl_shell_destroy(display.shell);
676 if (display.compositor)
677 wl_compositor_destroy(display.compositor);
679 wl_display_flush(display.display);
680 wl_display_disconnect(display.display);