toytoolkit: Don't draw shadows for maximized windows.
[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
36 #include <GLES2/gl2.h>
37 #include <EGL/egl.h>
38
39 struct window;
40 struct seat;
41
42 struct display {
43         struct wl_display *display;
44         struct wl_compositor *compositor;
45         struct wl_shell *shell;
46         struct wl_seat *seat;
47         struct wl_pointer *pointer;
48         struct wl_keyboard *keyboard;
49         struct {
50                 EGLDisplay dpy;
51                 EGLContext ctx;
52                 EGLConfig conf;
53         } egl;
54         uint32_t mask;
55         struct window *window;
56 };
57
58 struct geometry {
59         int width, height;
60 };
61
62 struct window {
63         struct display *display;
64         struct geometry geometry, window_size;
65         struct {
66                 GLuint fbo;
67                 GLuint color_rbo;
68
69                 GLuint rotation_uniform;
70
71                 GLuint pos;
72                 GLuint col;
73         } gl;
74
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;
81 };
82
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"
88         "void main() {\n"
89         "  gl_Position = rotation * pos;\n"
90         "  v_color = color;\n"
91         "}\n";
92
93 static const char *frag_shader_text =
94         "precision mediump float;\n"
95         "varying vec4 v_color;\n"
96         "void main() {\n"
97         "  gl_FragColor = v_color;\n"
98         "}\n";
99
100 static int running = 1;
101
102 static void
103 init_egl(struct display *display, int opaque)
104 {
105         static const EGLint context_attribs[] = {
106                 EGL_CONTEXT_CLIENT_VERSION, 2,
107                 EGL_NONE
108         };
109
110         EGLint config_attribs[] = {
111                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
112                 EGL_RED_SIZE, 1,
113                 EGL_GREEN_SIZE, 1,
114                 EGL_BLUE_SIZE, 1,
115                 EGL_ALPHA_SIZE, 1,
116                 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
117                 EGL_NONE
118         };
119
120         EGLint major, minor, n;
121         EGLBoolean ret;
122
123         if (opaque)
124                 config_attribs[9] = 0;
125
126         display->egl.dpy = eglGetDisplay(display->display);
127         assert(display->egl.dpy);
128
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);
133
134         ret = eglChooseConfig(display->egl.dpy, config_attribs,
135                               &display->egl.conf, 1, &n);
136         assert(ret && n == 1);
137
138         display->egl.ctx = eglCreateContext(display->egl.dpy,
139                                             display->egl.conf,
140                                             EGL_NO_CONTEXT, context_attribs);
141         assert(display->egl.ctx);
142
143 }
144
145 static void
146 fini_egl(struct display *display)
147 {
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,
151                        EGL_NO_CONTEXT);
152
153         eglTerminate(display->egl.dpy);
154         eglReleaseThread();
155 }
156
157 static GLuint
158 create_shader(struct window *window, const char *source, GLenum shader_type)
159 {
160         GLuint shader;
161         GLint status;
162
163         shader = glCreateShader(shader_type);
164         assert(shader != 0);
165
166         glShaderSource(shader, 1, (const char **) &source, NULL);
167         glCompileShader(shader);
168
169         glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
170         if (!status) {
171                 char log[1000];
172                 GLsizei len;
173                 glGetShaderInfoLog(shader, 1000, &len, log);
174                 fprintf(stderr, "Error: compiling %s: %*s\n",
175                         shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
176                         len, log);
177                 exit(1);
178         }
179
180         return shader;
181 }
182
183 static void
184 init_gl(struct window *window)
185 {
186         GLuint frag, vert;
187         GLuint program;
188         GLint status;
189
190         frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
191         vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
192
193         program = glCreateProgram();
194         glAttachShader(program, frag);
195         glAttachShader(program, vert);
196         glLinkProgram(program);
197
198         glGetProgramiv(program, GL_LINK_STATUS, &status);
199         if (!status) {
200                 char log[1000];
201                 GLsizei len;
202                 glGetProgramInfoLog(program, 1000, &len, log);
203                 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
204                 exit(1);
205         }
206
207         glUseProgram(program);
208         
209         window->gl.pos = 0;
210         window->gl.col = 1;
211
212         glBindAttribLocation(program, window->gl.pos, "pos");
213         glBindAttribLocation(program, window->gl.col, "color");
214         glLinkProgram(program);
215
216         window->gl.rotation_uniform =
217                 glGetUniformLocation(program, "rotation");
218 }
219
220 static void
221 handle_ping(void *data, struct wl_shell_surface *shell_surface,
222             uint32_t serial)
223 {
224         wl_shell_surface_pong(shell_surface, serial);
225 }
226
227 static void
228 handle_configure(void *data, struct wl_shell_surface *shell_surface,
229                  uint32_t edges, int32_t width, int32_t height)
230 {
231         struct window *window = data;
232
233         if (window->native)
234                 wl_egl_window_resize(window->native, width, height, 0, 0);
235
236         window->geometry.width = width;
237         window->geometry.height = height;
238
239         if (!window->fullscreen)
240                 window->window_size = window->geometry;
241 }
242
243 static void
244 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
245 {
246 }
247
248 static const struct wl_shell_surface_listener shell_surface_listener = {
249         handle_ping,
250         handle_configure,
251         handle_popup_done
252 };
253
254 static void
255 redraw(void *data, struct wl_callback *callback, uint32_t time);
256
257 static void
258 configure_callback(void *data, struct wl_callback *callback, uint32_t  time)
259 {
260         struct window *window = data;
261
262         wl_callback_destroy(callback);
263
264         window->configured = 1;
265
266         if (window->callback == NULL)
267                 redraw(data, NULL, time);
268 }
269
270 static struct wl_callback_listener configure_callback_listener = {
271         configure_callback,
272 };
273
274 static void
275 toggle_fullscreen(struct window *window, int fullscreen)
276 {
277         struct wl_callback *callback;
278
279         window->fullscreen = fullscreen;
280         window->configured = 0;
281
282         if (fullscreen) {
283                 wl_shell_surface_set_fullscreen(window->shell_surface,
284                                                 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
285                                                 0, NULL);
286         } else {
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);
291         }
292
293         callback = wl_display_sync(window->display->display);
294         wl_callback_add_listener(callback, &configure_callback_listener,
295                                  window);
296 }
297
298 static void
299 create_surface(struct window *window)
300 {
301         struct display *display = window->display;
302         EGLBoolean ret;
303         
304         window->surface = wl_compositor_create_surface(display->compositor);
305         window->shell_surface = wl_shell_get_shell_surface(display->shell,
306                                                            window->surface);
307
308         wl_shell_surface_add_listener(window->shell_surface,
309                                       &shell_surface_listener, window);
310
311         window->native =
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,
317                                        display->egl.conf,
318                                        window->native, NULL);
319
320         wl_shell_surface_set_title(window->shell_surface, "simple-egl");
321
322         ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
323                              window->egl_surface, window->display->egl.ctx);
324         assert(ret == EGL_TRUE);
325
326         toggle_fullscreen(window, window->fullscreen);
327 }
328
329 static void
330 destroy_surface(struct window *window)
331 {
332         wl_egl_window_destroy(window->native);
333
334         wl_shell_surface_destroy(window->shell_surface);
335         wl_surface_destroy(window->surface);
336
337         if (window->callback)
338                 wl_callback_destroy(window->callback);
339 }
340
341 static const struct wl_callback_listener frame_listener;
342
343 static void
344 redraw(void *data, struct wl_callback *callback, uint32_t time)
345 {
346         struct window *window = data;
347         static const GLfloat verts[3][2] = {
348                 { -0.5, -0.5 },
349                 {  0.5, -0.5 },
350                 {  0,    0.5 }
351         };
352         static const GLfloat colors[3][3] = {
353                 { 1, 0, 0 },
354                 { 0, 1, 0 },
355                 { 0, 0, 1 }
356         };
357         GLfloat angle;
358         GLfloat rotation[4][4] = {
359                 { 1, 0, 0, 0 },
360                 { 0, 1, 0, 0 },
361                 { 0, 0, 1, 0 },
362                 { 0, 0, 0, 1 }
363         };
364         static const int32_t speed_div = 5;
365         static uint32_t start_time = 0;
366         struct wl_region *region;
367
368         assert(window->callback == callback);
369         window->callback = NULL;
370
371         if (callback)
372                 wl_callback_destroy(callback);
373
374         if (!window->configured)
375                 return;
376
377         if (start_time == 0)
378                 start_time = time;
379
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);
385
386         glViewport(0, 0, window->geometry.width, window->geometry.height);
387
388         glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
389                            (GLfloat *) rotation);
390
391         glClearColor(0.0, 0.0, 0.0, 0.5);
392         glClear(GL_COLOR_BUFFER_BIT);
393
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);
398
399         glDrawArrays(GL_TRIANGLES, 0, 3);
400
401         glDisableVertexAttribArray(window->gl.pos);
402         glDisableVertexAttribArray(window->gl.col);
403
404         eglSwapBuffers(window->display->egl.dpy, window->egl_surface);
405
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);
413         }
414
415         window->callback = wl_surface_frame(window->surface);
416         wl_callback_add_listener(window->callback, &frame_listener, window);
417 }
418
419 static const struct wl_callback_listener frame_listener = {
420         redraw
421 };
422
423 static void
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)
427 {
428         struct display *display = data;
429
430         if (display->window->fullscreen)
431                 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
432 }
433
434 static void
435 pointer_handle_leave(void *data, struct wl_pointer *pointer,
436                      uint32_t serial, struct wl_surface *surface)
437 {
438 }
439
440 static void
441 pointer_handle_motion(void *data, struct wl_pointer *pointer,
442                       uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
443 {
444 }
445
446 static void
447 pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
448                       uint32_t serial, uint32_t time, uint32_t button,
449                       uint32_t state)
450 {
451         struct display *display = data;
452
453         if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
454                 wl_shell_surface_move(display->window->shell_surface,
455                                       display->seat, serial);
456 }
457
458 static void
459 pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
460                     uint32_t time, uint32_t axis, wl_fixed_t value)
461 {
462 }
463
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,
469         pointer_handle_axis,
470 };
471
472 static void
473 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
474                        uint32_t format, int fd, uint32_t size)
475 {
476 }
477
478 static void
479 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
480                       uint32_t serial, struct wl_surface *surface,
481                       struct wl_array *keys)
482 {
483 }
484
485 static void
486 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
487                       uint32_t serial, struct wl_surface *surface)
488 {
489 }
490
491 static void
492 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
493                     uint32_t serial, uint32_t time, uint32_t key,
494                     uint32_t state)
495 {
496         struct display *d = data;
497
498         if (key == KEY_F11 && state)
499                 toggle_fullscreen(d->window, d->window->fullscreen ^ 1);
500         else if (key == KEY_ESC && state)
501                 running = 0;
502 }
503
504 static void
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,
508                           uint32_t group)
509 {
510 }
511
512 static const struct wl_keyboard_listener keyboard_listener = {
513         keyboard_handle_keymap,
514         keyboard_handle_enter,
515         keyboard_handle_leave,
516         keyboard_handle_key,
517         keyboard_handle_modifiers,
518 };
519
520 static void
521 seat_handle_capabilities(void *data, struct wl_seat *seat,
522                          enum wl_seat_capability caps)
523 {
524         struct display *d = data;
525
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);
531                 d->pointer = NULL;
532         }
533
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);
539                 d->keyboard = NULL;
540         }
541 }
542
543 static const struct wl_seat_listener seat_listener = {
544         seat_handle_capabilities,
545 };
546
547 static void
548 display_handle_global(struct wl_display *display, uint32_t id,
549                       const char *interface, uint32_t version, void *data)
550 {
551         struct display *d = data;
552
553         if (strcmp(interface, "wl_compositor") == 0) {
554                 d->compositor =
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);
561         }
562 }
563
564 static int
565 event_mask_update(uint32_t mask, void *data)
566 {
567         struct display *d = data;
568
569         d->mask = mask;
570
571         return 0;
572 }
573
574 static void
575 signal_int(int signum)
576 {
577         running = 0;
578 }
579
580 static void
581 usage(int error_code)
582 {
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");
587
588         exit(error_code);
589 }
590
591 int
592 main(int argc, char **argv)
593 {
594         struct sigaction sigint;
595         struct display display = { 0 };
596         struct window  window  = { 0 };
597         int i;
598
599         window.display = &display;
600         display.window = &window;
601         window.window_size.width  = 250;
602         window.window_size.height = 250;
603
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)
608                         window.opaque = 1;
609                 else if (strcmp("-h", argv[i]) == 0)
610                         usage(EXIT_SUCCESS);
611                 else
612                         usage(EXIT_FAILURE);
613         }
614
615         display.display = wl_display_connect(NULL);
616         assert(display.display);
617
618         wl_display_add_global_listener(display.display,
619                                        display_handle_global, &display);
620
621         wl_display_get_fd(display.display, event_mask_update, &display);
622         wl_display_iterate(display.display, WL_DISPLAY_READABLE);
623
624         init_egl(&display, window.opaque);
625         create_surface(&window);
626         init_gl(&window);
627
628         sigint.sa_handler = signal_int;
629         sigemptyset(&sigint.sa_mask);
630         sigint.sa_flags = SA_RESETHAND;
631         sigaction(SIGINT, &sigint, NULL);
632
633         while (running)
634                 wl_display_iterate(display.display, display.mask);
635
636         fprintf(stderr, "simple-egl exiting\n");
637
638         destroy_surface(&window);
639         fini_egl(&display);
640
641         if (display.shell)
642                 wl_shell_destroy(display.shell);
643
644         if (display.compositor)
645                 wl_compositor_destroy(display.compositor);
646
647         wl_display_flush(display.display);
648         wl_display_disconnect(display.display);
649
650         return 0;
651 }