compositor-wayland: Hide cursor for compositor surfaces
[profile/ivi/weston.git] / src / compositor-wayland.c
1 /*
2  * Copyright © 2010-2011 Benjamin Franzke
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation for any purpose is hereby granted without fee, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of the copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  The copyright holders make
11  * no representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stddef.h>
28 #define _GNU_SOURCE
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34
35 #include <wayland-client.h>
36 #include <wayland-egl.h>
37
38 #include <GLES2/gl2.h>
39 #include <GLES2/gl2ext.h>
40 #include <EGL/egl.h>
41 #include <EGL/eglext.h>
42
43 #include "compositor.h"
44
45 struct wayland_compositor {
46         struct weston_compositor         base;
47
48         struct {
49                 struct wl_display *display;
50                 struct wl_compositor *compositor;
51                 struct wl_shell *shell;
52                 struct wl_output *output;
53
54                 struct {
55                         int32_t x, y, width, height;
56                 } screen_allocation;
57
58                 struct wl_event_source *wl_source;
59                 uint32_t event_mask;
60         } parent;
61
62         struct wl_list input_list;
63 };
64
65 struct wayland_output {
66         struct weston_output    base;
67
68         struct {
69                 struct wl_surface       *surface;
70                 struct wl_shell_surface *shell_surface;
71                 struct wl_egl_window    *egl_window;
72         } parent;
73         EGLSurface egl_surface;
74         struct weston_mode      mode;
75 };
76
77 struct wayland_input {
78         struct wayland_compositor *compositor;
79         struct wl_input_device *input_device;
80         struct wl_list link;
81 };
82
83 static int
84 wayland_input_create(struct wayland_compositor *c)
85 {
86         struct weston_input_device *input;
87
88         input = malloc(sizeof *input);
89         if (input == NULL)
90                 return -1;
91
92         memset(input, 0, sizeof *input);
93         weston_input_device_init(input, &c->base);
94
95         c->base.input_device = &input->input_device;
96
97         return 0;
98 }
99
100 static int
101 wayland_compositor_init_egl(struct wayland_compositor *c)
102 {
103         EGLint major, minor;
104         EGLint n;
105         const char *extensions;
106         EGLint config_attribs[] = {
107                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
108                 EGL_RED_SIZE, 1,
109                 EGL_GREEN_SIZE, 1,
110                 EGL_BLUE_SIZE, 1,
111                 EGL_ALPHA_SIZE, 0,
112                 EGL_DEPTH_SIZE, 1,
113                 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
114                 EGL_NONE
115         };
116         static const EGLint context_attribs[] = {
117                 EGL_CONTEXT_CLIENT_VERSION, 2,
118                 EGL_NONE
119         };
120
121         c->base.display = eglGetDisplay(c->parent.display);
122         if (c->base.display == NULL) {
123                 fprintf(stderr, "failed to create display\n");
124                 return -1;
125         }
126
127         if (!eglInitialize(c->base.display, &major, &minor)) {
128                 fprintf(stderr, "failed to initialize display\n");
129                 return -1;
130         }
131
132         extensions = eglQueryString(c->base.display, EGL_EXTENSIONS);
133         if (!strstr(extensions, "EGL_KHR_surfaceless_gles2")) {
134                 fprintf(stderr, "EGL_KHR_surfaceless_gles2 not available\n");
135                 return -1;
136         }
137
138         if (!eglBindAPI(EGL_OPENGL_ES_API)) {
139                 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
140                 return -1;
141         }
142         if (!eglChooseConfig(c->base.display, config_attribs,
143                              &c->base.config, 1, &n) || n == 0) {
144                 fprintf(stderr, "failed to choose config: %d\n", n);
145                 return -1;
146         }
147
148         c->base.context = eglCreateContext(c->base.display, c->base.config,
149                                            EGL_NO_CONTEXT, context_attribs);
150         if (c->base.context == NULL) {
151                 fprintf(stderr, "failed to create context\n");
152                 return -1;
153         }
154
155         if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
156                             EGL_NO_SURFACE, c->base.context)) {
157                 fprintf(stderr, "failed to make context current\n");
158                 return -1;
159         }
160
161         return 0;
162 }
163
164 static void
165 frame_done(void *data, struct wl_callback *callback, uint32_t time)
166 {
167         struct weston_output *output = data;
168
169         wl_callback_destroy(callback);
170         weston_output_finish_frame(output, time);
171 }
172
173 static const struct wl_callback_listener frame_listener = {
174         frame_done
175 };
176
177 static void
178 wayland_output_repaint(struct weston_output *output_base)
179 {
180         struct wayland_output *output = (struct wayland_output *) output_base;
181         struct wayland_compositor *compositor =
182                 (struct wayland_compositor *) output->base.compositor;
183         struct wl_callback *callback;
184         struct weston_surface *surface;
185
186         if (!eglMakeCurrent(compositor->base.display, output->egl_surface,
187                             output->egl_surface, compositor->base.context)) {
188                 fprintf(stderr, "failed to make current\n");
189                 return;
190         }
191
192         wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
193                 weston_surface_draw(surface, &output->base);
194
195         eglSwapBuffers(compositor->base.display, output->egl_surface);
196         callback = wl_surface_frame(output->parent.surface);
197         wl_callback_add_listener(callback, &frame_listener, output);
198
199         return;
200 }
201
202 static int
203 wayland_output_set_cursor(struct weston_output *output_base,
204                           struct weston_input_device *input)
205 {
206         return -1;
207 }
208
209 static void
210 wayland_output_destroy(struct weston_output *output_base)
211 {
212         struct wayland_output *output = (struct wayland_output *) output_base;
213         struct weston_compositor *ec = output->base.compositor;
214
215         eglDestroySurface(ec->display, output->egl_surface);
216         wl_egl_window_destroy(output->parent.egl_window);
217         free(output);
218
219         return;
220 }
221
222 static int
223 wayland_compositor_create_output(struct wayland_compositor *c,
224                                  int width, int height)
225 {
226         struct wayland_output *output;
227
228         output = malloc(sizeof *output);
229         if (output == NULL)
230                 return -1;
231         memset(output, 0, sizeof *output);
232
233         output->mode.flags =
234                 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
235         output->mode.width = width;
236         output->mode.height = height;
237         output->mode.refresh = 60;
238         wl_list_init(&output->base.mode_list);
239         wl_list_insert(&output->base.mode_list, &output->mode.link);
240
241         output->base.current = &output->mode;
242         weston_output_init(&output->base, &c->base, 0, 0, width, height,
243                          WL_OUTPUT_FLIPPED);
244
245         output->parent.surface =
246                 wl_compositor_create_surface(c->parent.compositor);
247         wl_surface_set_user_data(output->parent.surface, output);
248
249         output->parent.egl_window =
250                 wl_egl_window_create(output->parent.surface, width, height);
251         if (!output->parent.egl_window) {
252                 fprintf(stderr, "failure to create wl_egl_window\n");
253                 goto cleanup_output;
254         }
255
256         output->egl_surface =
257                 eglCreateWindowSurface(c->base.display, c->base.config,
258                                        output->parent.egl_window, NULL);
259         if (!output->egl_surface) {
260                 fprintf(stderr, "failed to create window surface\n");
261                 goto cleanup_window;
262         }
263
264         if (!eglMakeCurrent(c->base.display, output->egl_surface,
265                             output->egl_surface, c->base.context)) {
266                 fprintf(stderr, "failed to make surface current\n");
267                 goto cleanup_surface;
268                 return -1;
269         }
270
271         output->parent.shell_surface =
272                 wl_shell_get_shell_surface(c->parent.shell,
273                                            output->parent.surface);
274         /* FIXME: add shell_surface listener for resizing */
275         wl_shell_surface_set_toplevel(output->parent.shell_surface);
276
277         glClearColor(0, 0, 0, 0.5);
278
279         output->base.repaint = wayland_output_repaint;
280         output->base.set_hardware_cursor = wayland_output_set_cursor;
281         output->base.destroy = wayland_output_destroy;
282
283         wl_list_insert(c->base.output_list.prev, &output->base.link);
284
285         return 0;
286
287 cleanup_surface:
288         eglDestroySurface(c->base.display, output->egl_surface);
289 cleanup_window:
290         wl_egl_window_destroy(output->parent.egl_window);
291 cleanup_output:
292         /* FIXME: cleanup weston_output */
293         free(output);
294
295         return -1;
296 }
297
298 /* Events received from the wayland-server this compositor is client of: */
299
300 /* parent output interface */
301 static void
302 display_handle_geometry(void *data,
303                         struct wl_output *wl_output,
304                         int x,
305                         int y,
306                         int physical_width,
307                         int physical_height,
308                         int subpixel,
309                         const char *make,
310                         const char *model)
311 {
312         struct wayland_compositor *c = data;
313
314         c->parent.screen_allocation.x = x;
315         c->parent.screen_allocation.y = y;
316 }
317
318 static void
319 display_handle_mode(void *data,
320                     struct wl_output *wl_output,
321                     uint32_t flags,
322                     int width,
323                     int height,
324                     int refresh)
325 {
326         struct wayland_compositor *c = data;
327
328         c->parent.screen_allocation.width = width;
329         c->parent.screen_allocation.height = height;
330 }
331
332 static const struct wl_output_listener output_listener = {
333         display_handle_geometry,
334         display_handle_mode
335 };
336
337 /* parent input interface */
338 static void
339 input_handle_motion(void *data, struct wl_input_device *input_device,
340                      uint32_t time,
341                      int32_t x, int32_t y, int32_t sx, int32_t sy)
342 {
343         struct wayland_input *input = data;
344         struct wayland_compositor *c = input->compositor;
345
346         notify_motion(c->base.input_device, time, sx, sy);
347 }
348
349 static void
350 input_handle_button(void *data,
351                      struct wl_input_device *input_device,
352                      uint32_t time, uint32_t button, uint32_t state)
353 {
354         struct wayland_input *input = data;
355         struct wayland_compositor *c = input->compositor;
356
357         notify_button(c->base.input_device, time, button, state);
358 }
359
360 static void
361 input_handle_key(void *data, struct wl_input_device *input_device,
362                   uint32_t time, uint32_t key, uint32_t state)
363 {
364         struct wayland_input *input = data;
365         struct wayland_compositor *c = input->compositor;
366
367         notify_key(c->base.input_device, time, key, state);
368 }
369
370 static void
371 input_handle_pointer_focus(void *data,
372                             struct wl_input_device *input_device,
373                             uint32_t time, struct wl_surface *surface,
374                             int32_t x, int32_t y, int32_t sx, int32_t sy)
375 {
376         struct wayland_input *input = data;
377         struct wayland_output *output;
378         struct wayland_compositor *c = input->compositor;
379
380         if (surface) {
381                 output = wl_surface_get_user_data(surface);
382                 notify_pointer_focus(c->base.input_device,
383                                      time, &output->base, sx, sy);
384
385                 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
386         } else {
387                 notify_pointer_focus(c->base.input_device, time, NULL, 0, 0);
388         }
389 }
390
391 static void
392 input_handle_keyboard_focus(void *data,
393                              struct wl_input_device *input_device,
394                              uint32_t time,
395                              struct wl_surface *surface,
396                              struct wl_array *keys)
397 {
398         struct wayland_input *input = data;
399         struct wayland_compositor *c = input->compositor;
400         struct wayland_output *output;
401
402         if (surface) {
403                 output = wl_surface_get_user_data(surface);
404                 notify_keyboard_focus(c->base.input_device,
405                                       time, &output->base, keys);
406         } else {
407                 notify_keyboard_focus(c->base.input_device, time, NULL, NULL);
408         }
409 }
410
411 static const struct wl_input_device_listener input_device_listener = {
412         input_handle_motion,
413         input_handle_button,
414         input_handle_key,
415         input_handle_pointer_focus,
416         input_handle_keyboard_focus,
417 };
418
419 static void
420 display_add_input(struct wayland_compositor *c, uint32_t id)
421 {
422         struct wayland_input *input;
423
424         input = malloc(sizeof *input);
425         if (input == NULL)
426                 return;
427
428         memset(input, 0, sizeof *input);
429
430         input->compositor = c;
431         input->input_device = wl_display_bind(c->parent.display,
432                                               id, &wl_input_device_interface);
433         wl_list_insert(c->input_list.prev, &input->link);
434
435         wl_input_device_add_listener(input->input_device,
436                                      &input_device_listener, input);
437         wl_input_device_set_user_data(input->input_device, input);
438 }
439
440 static void
441 display_handle_global(struct wl_display *display, uint32_t id,
442                       const char *interface, uint32_t version, void *data)
443 {
444         struct wayland_compositor *c = data;
445
446         if (strcmp(interface, "wl_compositor") == 0) {
447                 c->parent.compositor =
448                         wl_display_bind(display, id, &wl_compositor_interface);
449         } else if (strcmp(interface, "wl_output") == 0) {
450                 c->parent.output =
451                         wl_display_bind(display, id, &wl_output_interface);
452                 wl_output_add_listener(c->parent.output, &output_listener, c);
453         } else if (strcmp(interface, "wl_input_device") == 0) {
454                 display_add_input(c, id);
455         } else if (strcmp(interface, "wl_shell") == 0) {
456                 c->parent.shell =
457                         wl_display_bind(display, id, &wl_shell_interface);
458         }
459 }
460
461 static int
462 update_event_mask(uint32_t mask, void *data)
463 {
464         struct wayland_compositor *c = data;
465
466         c->parent.event_mask = mask;
467         if (c->parent.wl_source)
468                 wl_event_source_fd_update(c->parent.wl_source, mask);
469
470         return 0;
471 }
472
473 static int
474 wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
475 {
476         struct wayland_compositor *c = data;
477
478         if (mask & WL_EVENT_READABLE)
479                 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
480         if (mask & WL_EVENT_WRITABLE)
481                 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
482
483         return 1;
484 }
485
486 static void
487 wayland_destroy(struct weston_compositor *ec)
488 {
489         weston_compositor_shutdown(ec);
490
491         free(ec);
492 }
493
494 static struct weston_compositor *
495 wayland_compositor_create(struct wl_display *display, int width, int height)
496 {
497         struct wayland_compositor *c;
498         struct wl_event_loop *loop;
499         int fd;
500
501         c = malloc(sizeof *c);
502         if (c == NULL)
503                 return NULL;
504
505         memset(c, 0, sizeof *c);
506
507         c->parent.display = wl_display_connect(NULL);
508
509         if (c->parent.display == NULL) {
510                 fprintf(stderr, "failed to create display: %m\n");
511                 return NULL;
512         }
513
514         wl_list_init(&c->input_list);
515         wl_display_add_global_listener(c->parent.display,
516                                 display_handle_global, c);
517
518         wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
519
520         c->base.wl_display = display;
521         if (wayland_compositor_init_egl(c) < 0)
522                 return NULL;
523
524         c->base.destroy = wayland_destroy;
525
526         /* Can't init base class until we have a current egl context */
527         if (weston_compositor_init(&c->base, display) < 0)
528                 return NULL;
529
530         if (wayland_compositor_create_output(c, width, height) < 0)
531                 return NULL;
532
533         if (wayland_input_create(c) < 0)
534                 return NULL;
535
536         loop = wl_display_get_event_loop(c->base.wl_display);
537
538         fd = wl_display_get_fd(c->parent.display, update_event_mask, c);
539         c->parent.wl_source =
540                 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
541                                      wayland_compositor_handle_event, c);
542         if (c->parent.wl_source == NULL)
543                 return NULL;
544
545         return &c->base;
546 }
547
548 struct weston_compositor *
549 backend_init(struct wl_display *display, char *options);
550
551 WL_EXPORT struct weston_compositor *
552 backend_init(struct wl_display *display, char *options)
553 {
554         int width = 1024, height = 640, i;
555         char *p, *value;
556
557         static char * const tokens[] = { "width", "height", NULL };
558         
559         p = options;
560         while (i = getsubopt(&p, tokens, &value), i != -1) {
561                 switch (i) {
562                 case 0:
563                         width = strtol(value, NULL, 0);
564                         break;
565                 case 1:
566                         height = strtol(value, NULL, 0);
567                         break;
568                 }
569         }
570
571         return wayland_compositor_create(display, width, height);
572 }