button-test: Use wl_display_roundtrip instead of yield()
[profile/ivi/weston.git] / clients / simple-egl.c
1 /*
2  * Copyright © 2011 Benjamin Franzke
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 <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdbool.h>
27 #include <math.h>
28 #include <assert.h>
29 #include <signal.h>
30
31 #include <linux/input.h>
32
33 #include <wayland-client.h>
34 #include <wayland-egl.h>
35 #include <wayland-cursor.h>
36
37 #include <GLES2/gl2.h>
38 #include <EGL/egl.h>
39
40 struct window;
41 struct seat;
42
43 struct display {
44         struct wl_display *display;
45         struct wl_registry *registry;
46         struct wl_compositor *compositor;
47         struct wl_shell *shell;
48         struct wl_seat *seat;
49         struct wl_pointer *pointer;
50         struct wl_keyboard *keyboard;
51         struct wl_shm *shm;
52         struct wl_cursor_theme *cursor_theme;
53         struct wl_cursor *default_cursor;
54         struct wl_surface *cursor_surface;
55         struct {
56                 EGLDisplay dpy;
57                 EGLContext ctx;
58                 EGLConfig conf;
59         } egl;
60         struct window *window;
61 };
62
63 struct geometry {
64         int width, height;
65 };
66
67 struct window {
68         struct display *display;
69         struct geometry geometry, window_size;
70         struct {
71                 GLuint fbo;
72                 GLuint color_rbo;
73
74                 GLuint rotation_uniform;
75
76                 GLuint pos;
77                 GLuint col;
78         } gl;
79
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;
86 };
87
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"
93         "void main() {\n"
94         "  gl_Position = rotation * pos;\n"
95         "  v_color = color;\n"
96         "}\n";
97
98 static const char *frag_shader_text =
99         "precision mediump float;\n"
100         "varying vec4 v_color;\n"
101         "void main() {\n"
102         "  gl_FragColor = v_color;\n"
103         "}\n";
104
105 static int running = 1;
106
107 static void
108 init_egl(struct display *display, int opaque)
109 {
110         static const EGLint context_attribs[] = {
111                 EGL_CONTEXT_CLIENT_VERSION, 2,
112                 EGL_NONE
113         };
114
115         EGLint config_attribs[] = {
116                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
117                 EGL_RED_SIZE, 1,
118                 EGL_GREEN_SIZE, 1,
119                 EGL_BLUE_SIZE, 1,
120                 EGL_ALPHA_SIZE, 1,
121                 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
122                 EGL_NONE
123         };
124
125         EGLint major, minor, n;
126         EGLBoolean ret;
127
128         if (opaque)
129                 config_attribs[9] = 0;
130
131         display->egl.dpy = eglGetDisplay(display->display);
132         assert(display->egl.dpy);
133
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);
138
139         ret = eglChooseConfig(display->egl.dpy, config_attribs,
140                               &display->egl.conf, 1, &n);
141         assert(ret && n == 1);
142
143         display->egl.ctx = eglCreateContext(display->egl.dpy,
144                                             display->egl.conf,
145                                             EGL_NO_CONTEXT, context_attribs);
146         assert(display->egl.ctx);
147
148 }
149
150 static void
151 fini_egl(struct display *display)
152 {
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,
156                        EGL_NO_CONTEXT);
157
158         eglTerminate(display->egl.dpy);
159         eglReleaseThread();
160 }
161
162 static GLuint
163 create_shader(struct window *window, const char *source, GLenum shader_type)
164 {
165         GLuint shader;
166         GLint status;
167
168         shader = glCreateShader(shader_type);
169         assert(shader != 0);
170
171         glShaderSource(shader, 1, (const char **) &source, NULL);
172         glCompileShader(shader);
173
174         glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
175         if (!status) {
176                 char log[1000];
177                 GLsizei len;
178                 glGetShaderInfoLog(shader, 1000, &len, log);
179                 fprintf(stderr, "Error: compiling %s: %*s\n",
180                         shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
181                         len, log);
182                 exit(1);
183         }
184
185         return shader;
186 }
187
188 static void
189 init_gl(struct window *window)
190 {
191         GLuint frag, vert;
192         GLuint program;
193         GLint status;
194
195         frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
196         vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
197
198         program = glCreateProgram();
199         glAttachShader(program, frag);
200         glAttachShader(program, vert);
201         glLinkProgram(program);
202
203         glGetProgramiv(program, GL_LINK_STATUS, &status);
204         if (!status) {
205                 char log[1000];
206                 GLsizei len;
207                 glGetProgramInfoLog(program, 1000, &len, log);
208                 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
209                 exit(1);
210         }
211
212         glUseProgram(program);
213         
214         window->gl.pos = 0;
215         window->gl.col = 1;
216
217         glBindAttribLocation(program, window->gl.pos, "pos");
218         glBindAttribLocation(program, window->gl.col, "color");
219         glLinkProgram(program);
220
221         window->gl.rotation_uniform =
222                 glGetUniformLocation(program, "rotation");
223 }
224
225 static void
226 handle_ping(void *data, struct wl_shell_surface *shell_surface,
227             uint32_t serial)
228 {
229         wl_shell_surface_pong(shell_surface, serial);
230 }
231
232 static void
233 handle_configure(void *data, struct wl_shell_surface *shell_surface,
234                  uint32_t edges, int32_t width, int32_t height)
235 {
236         struct window *window = data;
237
238         if (window->native)
239                 wl_egl_window_resize(window->native, width, height, 0, 0);
240
241         window->geometry.width = width;
242         window->geometry.height = height;
243
244         if (!window->fullscreen)
245                 window->window_size = window->geometry;
246 }
247
248 static void
249 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
250 {
251 }
252
253 static const struct wl_shell_surface_listener shell_surface_listener = {
254         handle_ping,
255         handle_configure,
256         handle_popup_done
257 };
258
259 static void
260 redraw(void *data, struct wl_callback *callback, uint32_t time);
261
262 static void
263 configure_callback(void *data, struct wl_callback *callback, uint32_t  time)
264 {
265         struct window *window = data;
266
267         wl_callback_destroy(callback);
268
269         window->configured = 1;
270
271         if (window->callback == NULL)
272                 redraw(data, NULL, time);
273 }
274
275 static struct wl_callback_listener configure_callback_listener = {
276         configure_callback,
277 };
278
279 static void
280 toggle_fullscreen(struct window *window, int fullscreen)
281 {
282         struct wl_callback *callback;
283
284         window->fullscreen = fullscreen;
285         window->configured = 0;
286
287         if (fullscreen) {
288                 wl_shell_surface_set_fullscreen(window->shell_surface,
289                                                 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
290                                                 0, NULL);
291         } else {
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);
296         }
297
298         callback = wl_display_sync(window->display->display);
299         wl_callback_add_listener(callback, &configure_callback_listener,
300                                  window);
301 }
302
303 static void
304 create_surface(struct window *window)
305 {
306         struct display *display = window->display;
307         EGLBoolean ret;
308         
309         window->surface = wl_compositor_create_surface(display->compositor);
310         window->shell_surface = wl_shell_get_shell_surface(display->shell,
311                                                            window->surface);
312
313         wl_shell_surface_add_listener(window->shell_surface,
314                                       &shell_surface_listener, window);
315
316         window->native =
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,
322                                        display->egl.conf,
323                                        window->native, NULL);
324
325         wl_shell_surface_set_title(window->shell_surface, "simple-egl");
326
327         ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
328                              window->egl_surface, window->display->egl.ctx);
329         assert(ret == EGL_TRUE);
330
331         toggle_fullscreen(window, window->fullscreen);
332 }
333
334 static void
335 destroy_surface(struct window *window)
336 {
337         wl_egl_window_destroy(window->native);
338
339         wl_shell_surface_destroy(window->shell_surface);
340         wl_surface_destroy(window->surface);
341
342         if (window->callback)
343                 wl_callback_destroy(window->callback);
344 }
345
346 static const struct wl_callback_listener frame_listener;
347
348 static void
349 redraw(void *data, struct wl_callback *callback, uint32_t time)
350 {
351         struct window *window = data;
352         static const GLfloat verts[3][2] = {
353                 { -0.5, -0.5 },
354                 {  0.5, -0.5 },
355                 {  0,    0.5 }
356         };
357         static const GLfloat colors[3][3] = {
358                 { 1, 0, 0 },
359                 { 0, 1, 0 },
360                 { 0, 0, 1 }
361         };
362         GLfloat angle;
363         GLfloat rotation[4][4] = {
364                 { 1, 0, 0, 0 },
365                 { 0, 1, 0, 0 },
366                 { 0, 0, 1, 0 },
367                 { 0, 0, 0, 1 }
368         };
369         static const int32_t speed_div = 5;
370         static uint32_t start_time = 0;
371         struct wl_region *region;
372
373         assert(window->callback == callback);
374         window->callback = NULL;
375
376         if (callback)
377                 wl_callback_destroy(callback);
378
379         if (!window->configured)
380                 return;
381
382         if (start_time == 0)
383                 start_time = time;
384
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);
390
391         glViewport(0, 0, window->geometry.width, window->geometry.height);
392
393         glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
394                            (GLfloat *) rotation);
395
396         glClearColor(0.0, 0.0, 0.0, 0.5);
397         glClear(GL_COLOR_BUFFER_BIT);
398
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);
403
404         glDrawArrays(GL_TRIANGLES, 0, 3);
405
406         glDisableVertexAttribArray(window->gl.pos);
407         glDisableVertexAttribArray(window->gl.col);
408
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);
416         } else {
417                 wl_surface_set_opaque_region(window->surface, NULL);
418         }
419
420         window->callback = wl_surface_frame(window->surface);
421         wl_callback_add_listener(window->callback, &frame_listener, window);
422
423         eglSwapBuffers(window->display->egl.dpy, window->egl_surface);
424 }
425
426 static const struct wl_callback_listener frame_listener = {
427         redraw
428 };
429
430 static void
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)
434 {
435         struct display *display = data;
436         struct wl_buffer *buffer;
437         struct wl_cursor *cursor = display->default_cursor;
438         struct wl_cursor_image *image;
439
440         if (display->window->fullscreen)
441                 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
442         else if (cursor) {
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,
447                                       image->hotspot_x,
448                                       image->hotspot_y);
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);
453         }
454 }
455
456 static void
457 pointer_handle_leave(void *data, struct wl_pointer *pointer,
458                      uint32_t serial, struct wl_surface *surface)
459 {
460 }
461
462 static void
463 pointer_handle_motion(void *data, struct wl_pointer *pointer,
464                       uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
465 {
466 }
467
468 static void
469 pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
470                       uint32_t serial, uint32_t time, uint32_t button,
471                       uint32_t state)
472 {
473         struct display *display = data;
474
475         if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
476                 wl_shell_surface_move(display->window->shell_surface,
477                                       display->seat, serial);
478 }
479
480 static void
481 pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
482                     uint32_t time, uint32_t axis, wl_fixed_t value)
483 {
484 }
485
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,
491         pointer_handle_axis,
492 };
493
494 static void
495 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
496                        uint32_t format, int fd, uint32_t size)
497 {
498 }
499
500 static void
501 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
502                       uint32_t serial, struct wl_surface *surface,
503                       struct wl_array *keys)
504 {
505 }
506
507 static void
508 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
509                       uint32_t serial, struct wl_surface *surface)
510 {
511 }
512
513 static void
514 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
515                     uint32_t serial, uint32_t time, uint32_t key,
516                     uint32_t state)
517 {
518         struct display *d = data;
519
520         if (key == KEY_F11 && state)
521                 toggle_fullscreen(d->window, d->window->fullscreen ^ 1);
522         else if (key == KEY_ESC && state)
523                 running = 0;
524 }
525
526 static void
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,
530                           uint32_t group)
531 {
532 }
533
534 static const struct wl_keyboard_listener keyboard_listener = {
535         keyboard_handle_keymap,
536         keyboard_handle_enter,
537         keyboard_handle_leave,
538         keyboard_handle_key,
539         keyboard_handle_modifiers,
540 };
541
542 static void
543 seat_handle_capabilities(void *data, struct wl_seat *seat,
544                          enum wl_seat_capability caps)
545 {
546         struct display *d = data;
547
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);
553                 d->pointer = NULL;
554         }
555
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);
561                 d->keyboard = NULL;
562         }
563 }
564
565 static const struct wl_seat_listener seat_listener = {
566         seat_handle_capabilities,
567 };
568
569 static void
570 registry_handle_global(void *data, struct wl_registry *registry,
571                        uint32_t name, const char *interface, uint32_t version)
572 {
573         struct display *d = data;
574
575         if (strcmp(interface, "wl_compositor") == 0) {
576                 d->compositor =
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);
590                 d->default_cursor =
591                         wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
592         }
593 }
594
595 static const struct wl_registry_listener registry_listener = {
596         registry_handle_global
597 };
598
599 static void
600 signal_int(int signum)
601 {
602         running = 0;
603 }
604
605 static void
606 usage(int error_code)
607 {
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");
612
613         exit(error_code);
614 }
615
616 int
617 main(int argc, char **argv)
618 {
619         struct sigaction sigint;
620         struct display display = { 0 };
621         struct window  window  = { 0 };
622         int i, ret = 0;
623
624         window.display = &display;
625         display.window = &window;
626         window.window_size.width  = 250;
627         window.window_size.height = 250;
628
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)
633                         window.opaque = 1;
634                 else if (strcmp("-h", argv[i]) == 0)
635                         usage(EXIT_SUCCESS);
636                 else
637                         usage(EXIT_FAILURE);
638         }
639
640         display.display = wl_display_connect(NULL);
641         assert(display.display);
642
643         display.registry = wl_display_get_registry(display.display);
644         wl_registry_add_listener(display.registry,
645                                  &registry_listener, &display);
646
647         wl_display_dispatch(display.display);
648
649         init_egl(&display, window.opaque);
650         create_surface(&window);
651         init_gl(&window);
652
653         display.cursor_surface =
654                 wl_compositor_create_surface(display.compositor);
655
656         sigint.sa_handler = signal_int;
657         sigemptyset(&sigint.sa_mask);
658         sigint.sa_flags = SA_RESETHAND;
659         sigaction(SIGINT, &sigint, NULL);
660
661         while (running && ret != -1)
662                 ret = wl_display_dispatch(display.display);
663
664         fprintf(stderr, "simple-egl exiting\n");
665
666         destroy_surface(&window);
667         fini_egl(&display);
668
669         wl_surface_destroy(display.cursor_surface);
670         if (display.cursor_theme)
671                 wl_cursor_theme_destroy(display.cursor_theme);
672
673         if (display.shell)
674                 wl_shell_destroy(display.shell);
675
676         if (display.compositor)
677                 wl_compositor_destroy(display.compositor);
678
679         wl_display_flush(display.display);
680         wl_display_disconnect(display.display);
681
682         return 0;
683 }