libweston: Rename weston_surface::configure to ::committed
[platform/upstream/weston.git] / fullscreen-shell / fullscreen-shell.c
1 /*
2  * Copyright © 2013 Jason Ekstrand
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #include "config.h"
27
28 #include <sys/wait.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <assert.h>
35
36 #include "compositor.h"
37 #include "fullscreen-shell-unstable-v1-server-protocol.h"
38 #include "shared/helpers.h"
39
40 struct fullscreen_shell {
41         struct wl_client *client;
42         struct wl_listener client_destroyed;
43         struct weston_compositor *compositor;
44
45         struct weston_layer layer;
46         struct wl_list output_list;
47         struct wl_listener output_created_listener;
48
49         struct wl_listener seat_created_listener;
50
51         /* List of one surface per client, presented for the NULL output
52          *
53          * This is implemented as a list in case someone fixes the shell
54          * implementation to support more than one client.
55          */
56         struct wl_list default_surface_list; /* struct fs_client_surface::link */
57 };
58
59 struct fs_output {
60         struct fullscreen_shell *shell;
61         struct wl_list link;
62
63         struct weston_output *output;
64         struct wl_listener output_destroyed;
65
66         struct {
67                 struct weston_surface *surface;
68                 struct wl_listener surface_destroyed;
69                 struct wl_resource *mode_feedback;
70
71                 int presented_for_mode;
72                 enum zwp_fullscreen_shell_v1_present_method method;
73                 int32_t framerate;
74         } pending;
75
76         struct weston_surface *surface;
77         struct wl_listener surface_destroyed;
78         struct weston_view *view;
79         struct weston_view *black_view;
80         struct weston_transform transform; /* matrix from x, y */
81
82         int presented_for_mode;
83         enum zwp_fullscreen_shell_v1_present_method method;
84         uint32_t framerate;
85 };
86
87 struct pointer_focus_listener {
88         struct fullscreen_shell *shell;
89         struct wl_listener pointer_focus;
90         struct wl_listener seat_caps;
91         struct wl_listener seat_destroyed;
92 };
93
94 struct fs_client_surface {
95         struct weston_surface *surface;
96         enum zwp_fullscreen_shell_v1_present_method method;
97         struct wl_list link; /* struct fullscreen_shell::default_surface_list */
98         struct wl_listener surface_destroyed;
99 };
100
101 static void
102 remove_default_surface(struct fs_client_surface *surf)
103 {
104         wl_list_remove(&surf->surface_destroyed.link);
105         wl_list_remove(&surf->link);
106         free(surf);
107 }
108
109 static void
110 default_surface_destroy_listener(struct wl_listener *listener, void *data)
111 {
112         struct fs_client_surface *surf;
113
114         surf = container_of(listener, struct fs_client_surface, surface_destroyed);
115
116         remove_default_surface(surf);
117 }
118
119 static void
120 replace_default_surface(struct fullscreen_shell *shell, struct weston_surface *surface,
121                         enum zwp_fullscreen_shell_v1_present_method method)
122 {
123         struct fs_client_surface *surf, *prev = NULL;
124
125         if (!wl_list_empty(&shell->default_surface_list))
126                 prev = container_of(shell->default_surface_list.prev,
127                                     struct fs_client_surface, link);
128
129         surf = zalloc(sizeof *surf);
130         if (!surf)
131                 return;
132
133         surf->surface = surface;
134         surf->method = method;
135
136         if (prev)
137                 remove_default_surface(prev);
138
139         wl_list_insert(shell->default_surface_list.prev, &surf->link);
140
141         surf->surface_destroyed.notify = default_surface_destroy_listener;
142         wl_signal_add(&surface->destroy_signal, &surf->surface_destroyed);
143 }
144
145 static void
146 pointer_focus_changed(struct wl_listener *listener, void *data)
147 {
148         struct weston_pointer *pointer = data;
149
150         if (pointer->focus && pointer->focus->surface->resource)
151                 weston_seat_set_keyboard_focus(pointer->seat, pointer->focus->surface);
152 }
153
154 static void
155 seat_caps_changed(struct wl_listener *l, void *data)
156 {
157         struct weston_seat *seat = data;
158         struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
159         struct weston_pointer *pointer = weston_seat_get_pointer(seat);
160         struct pointer_focus_listener *listener;
161         struct fs_output *fsout;
162
163         listener = container_of(l, struct pointer_focus_listener, seat_caps);
164
165         /* no pointer */
166         if (pointer) {
167                 if (!listener->pointer_focus.link.prev) {
168                         wl_signal_add(&pointer->focus_signal,
169                                       &listener->pointer_focus);
170                 }
171         } else {
172                 if (listener->pointer_focus.link.prev) {
173                         wl_list_remove(&listener->pointer_focus.link);
174                 }
175         }
176
177         if (keyboard && keyboard->focus != NULL) {
178                 wl_list_for_each(fsout, &listener->shell->output_list, link) {
179                         if (fsout->surface) {
180                                 weston_seat_set_keyboard_focus(seat, fsout->surface);
181                                 return;
182                         }
183                 }
184         }
185 }
186
187 static void
188 seat_destroyed(struct wl_listener *l, void *data)
189 {
190         struct pointer_focus_listener *listener;
191
192         listener = container_of(l, struct pointer_focus_listener,
193                                 seat_destroyed);
194
195         free(listener);
196 }
197
198 static void
199 seat_created(struct wl_listener *l, void *data)
200 {
201         struct weston_seat *seat = data;
202         struct pointer_focus_listener *listener;
203
204         listener = zalloc(sizeof *listener);
205         if (!listener)
206                 return;
207
208         listener->shell = container_of(l, struct fullscreen_shell,
209                                        seat_created_listener);
210         listener->pointer_focus.notify = pointer_focus_changed;
211         listener->seat_caps.notify = seat_caps_changed;
212         listener->seat_destroyed.notify = seat_destroyed;
213
214         wl_signal_add(&seat->destroy_signal, &listener->seat_destroyed);
215         wl_signal_add(&seat->updated_caps_signal, &listener->seat_caps);
216
217         seat_caps_changed(&listener->seat_caps, seat);
218 }
219
220 static void
221 black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
222 {
223 }
224
225 static struct weston_view *
226 create_black_surface(struct weston_compositor *ec, struct fs_output *fsout,
227                      float x, float y, int w, int h)
228 {
229         struct weston_surface *surface = NULL;
230         struct weston_view *view;
231
232         surface = weston_surface_create(ec);
233         if (surface == NULL) {
234                 weston_log("no memory\n");
235                 return NULL;
236         }
237         view = weston_view_create(surface);
238         if (!view) {
239                 weston_surface_destroy(surface);
240                 weston_log("no memory\n");
241                 return NULL;
242         }
243
244         surface->committed = black_surface_committed;
245         surface->committed_private = fsout;
246         weston_surface_set_color(surface, 0.0f, 0.0f, 0.0f, 1.0f);
247         pixman_region32_fini(&surface->opaque);
248         pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
249         pixman_region32_fini(&surface->input);
250         pixman_region32_init_rect(&surface->input, 0, 0, w, h);
251
252         weston_surface_set_size(surface, w, h);
253         weston_view_set_position(view, x, y);
254
255         return view;
256 }
257
258 static void
259 fs_output_set_surface(struct fs_output *fsout, struct weston_surface *surface,
260                       enum zwp_fullscreen_shell_v1_present_method method,
261                       int32_t framerate, int presented_for_mode);
262 static void
263 fs_output_apply_pending(struct fs_output *fsout);
264 static void
265 fs_output_clear_pending(struct fs_output *fsout);
266
267 static void
268 fs_output_destroy(struct fs_output *fsout)
269 {
270         fs_output_set_surface(fsout, NULL, 0, 0, 0);
271         fs_output_clear_pending(fsout);
272
273         wl_list_remove(&fsout->link);
274
275         if (fsout->output)
276                 wl_list_remove(&fsout->output_destroyed.link);
277 }
278
279 static void
280 output_destroyed(struct wl_listener *listener, void *data)
281 {
282         struct fs_output *output = container_of(listener,
283                                                 struct fs_output,
284                                                 output_destroyed);
285         fs_output_destroy(output);
286 }
287
288 static void
289 surface_destroyed(struct wl_listener *listener, void *data)
290 {
291         struct fs_output *fsout = container_of(listener,
292                                                struct fs_output,
293                                                surface_destroyed);
294         fsout->surface = NULL;
295         fsout->view = NULL;
296 }
297
298 static void
299 pending_surface_destroyed(struct wl_listener *listener, void *data)
300 {
301         struct fs_output *fsout = container_of(listener,
302                                                struct fs_output,
303                                                pending.surface_destroyed);
304         fsout->pending.surface = NULL;
305 }
306
307 static void
308 configure_presented_surface(struct weston_surface *surface, int32_t sx,
309                             int32_t sy);
310
311 static struct fs_output *
312 fs_output_create(struct fullscreen_shell *shell, struct weston_output *output)
313 {
314         struct fs_output *fsout;
315         struct fs_client_surface *surf;
316
317         fsout = zalloc(sizeof *fsout);
318         if (!fsout)
319                 return NULL;
320
321         fsout->shell = shell;
322         wl_list_insert(&shell->output_list, &fsout->link);
323
324         fsout->output = output;
325         fsout->output_destroyed.notify = output_destroyed;
326         wl_signal_add(&output->destroy_signal, &fsout->output_destroyed);
327
328         fsout->surface_destroyed.notify = surface_destroyed;
329         fsout->pending.surface_destroyed.notify = pending_surface_destroyed;
330         fsout->black_view = create_black_surface(shell->compositor, fsout,
331                                                  output->x, output->y,
332                                                  output->width, output->height);
333         fsout->black_view->surface->is_mapped = true;
334         fsout->black_view->is_mapped = true;
335         weston_layer_entry_insert(&shell->layer.view_list,
336                        &fsout->black_view->layer_link);
337         wl_list_init(&fsout->transform.link);
338
339         if (!wl_list_empty(&shell->default_surface_list)) {
340                 surf = container_of(shell->default_surface_list.prev,
341                                     struct fs_client_surface, link);
342
343                 fs_output_set_surface(fsout, surf->surface, surf->method, 0, 0);
344                 configure_presented_surface(surf->surface, 0, 0);
345         }
346
347         return fsout;
348 }
349
350 static struct fs_output *
351 fs_output_for_output(struct weston_output *output)
352 {
353         struct wl_listener *listener;
354
355         if (!output)
356                 return NULL;
357
358         listener = wl_signal_get(&output->destroy_signal, output_destroyed);
359
360         return container_of(listener, struct fs_output, output_destroyed);
361 }
362
363 static void
364 restore_output_mode(struct weston_output *output)
365 {
366         if (output && output->original_mode)
367                 weston_output_mode_switch_to_native(output);
368 }
369
370 /*
371  * Returns the bounding box of a surface and all its sub-surfaces,
372  * in surface-local coordinates. */
373 static void
374 surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
375                                 int32_t *y, int32_t *w, int32_t *h) {
376         pixman_region32_t region;
377         pixman_box32_t *box;
378         struct weston_subsurface *subsurface;
379
380         pixman_region32_init_rect(&region, 0, 0,
381                                   surface->width,
382                                   surface->height);
383
384         wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
385                 pixman_region32_union_rect(&region, &region,
386                                            subsurface->position.x,
387                                            subsurface->position.y,
388                                            subsurface->surface->width,
389                                            subsurface->surface->height);
390         }
391
392         box = pixman_region32_extents(&region);
393         if (x)
394                 *x = box->x1;
395         if (y)
396                 *y = box->y1;
397         if (w)
398                 *w = box->x2 - box->x1;
399         if (h)
400                 *h = box->y2 - box->y1;
401
402         pixman_region32_fini(&region);
403 }
404
405 static void
406 fs_output_center_view(struct fs_output *fsout)
407 {
408         int32_t surf_x, surf_y, surf_width, surf_height;
409         float x, y;
410         struct weston_output *output = fsout->output;
411
412         surface_subsurfaces_boundingbox(fsout->view->surface, &surf_x, &surf_y,
413                                         &surf_width, &surf_height);
414
415         x = output->x + (output->width - surf_width) / 2 - surf_x / 2;
416         y = output->y + (output->height - surf_height) / 2 - surf_y / 2;
417
418         weston_view_set_position(fsout->view, x, y);
419 }
420
421 static void
422 fs_output_scale_view(struct fs_output *fsout, float width, float height)
423 {
424         float x, y;
425         int32_t surf_x, surf_y, surf_width, surf_height;
426         struct weston_matrix *matrix;
427         struct weston_view *view = fsout->view;
428         struct weston_output *output = fsout->output;
429
430         surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y,
431                                         &surf_width, &surf_height);
432
433         if (output->width == surf_width && output->height == surf_height) {
434                 weston_view_set_position(view,
435                                          fsout->output->x - surf_x,
436                                          fsout->output->y - surf_y);
437         } else {
438                 matrix = &fsout->transform.matrix;
439                 weston_matrix_init(matrix);
440
441                 weston_matrix_scale(matrix, width / surf_width,
442                                     height / surf_height, 1);
443                 wl_list_remove(&fsout->transform.link);
444                 wl_list_insert(&fsout->view->geometry.transformation_list,
445                                &fsout->transform.link);
446
447                 x = output->x + (output->width - width) / 2 - surf_x;
448                 y = output->y + (output->height - height) / 2 - surf_y;
449
450                 weston_view_set_position(view, x, y);
451         }
452 }
453
454 static void
455 fs_output_configure(struct fs_output *fsout, struct weston_surface *surface);
456
457 static void
458 fs_output_configure_simple(struct fs_output *fsout,
459                            struct weston_surface *configured_surface)
460 {
461         struct weston_output *output = fsout->output;
462         float output_aspect, surface_aspect;
463         int32_t surf_x, surf_y, surf_width, surf_height;
464
465         if (fsout->pending.surface == configured_surface)
466                 fs_output_apply_pending(fsout);
467
468         assert(fsout->view);
469
470         restore_output_mode(fsout->output);
471
472         wl_list_remove(&fsout->transform.link);
473         wl_list_init(&fsout->transform.link);
474
475         surface_subsurfaces_boundingbox(fsout->view->surface,
476                                         &surf_x, &surf_y,
477                                         &surf_width, &surf_height);
478
479         output_aspect = (float) output->width / (float) output->height;
480         surface_aspect = (float) surf_width / (float) surf_height;
481
482         switch (fsout->method) {
483         case ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_DEFAULT:
484         case ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_CENTER:
485                 fs_output_center_view(fsout);
486                 break;
487
488         case ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_ZOOM:
489                 if (output_aspect < surface_aspect)
490                         fs_output_scale_view(fsout,
491                                              output->width,
492                                              output->width / surface_aspect);
493                 else
494                         fs_output_scale_view(fsout,
495                                              output->height * surface_aspect,
496                                              output->height);
497                 break;
498
499         case ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_ZOOM_CROP:
500                 if (output_aspect < surface_aspect)
501                         fs_output_scale_view(fsout,
502                                              output->height * surface_aspect,
503                                              output->height);
504                 else
505                         fs_output_scale_view(fsout,
506                                              output->width,
507                                              output->width / surface_aspect);
508                 break;
509
510         case ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_STRETCH:
511                 fs_output_scale_view(fsout, output->width, output->height);
512                 break;
513         default:
514                 break;
515         }
516
517         weston_view_set_position(fsout->black_view,
518                                  fsout->output->x - surf_x,
519                                  fsout->output->y - surf_y);
520         weston_surface_set_size(fsout->black_view->surface,
521                                 fsout->output->width,
522                                 fsout->output->height);
523 }
524
525 static void
526 fs_output_configure_for_mode(struct fs_output *fsout,
527                              struct weston_surface *configured_surface)
528 {
529         int32_t surf_x, surf_y, surf_width, surf_height;
530         struct weston_mode mode;
531         int ret;
532
533         if (fsout->pending.surface != configured_surface) {
534                 /* Nothing to really reconfigure.  We'll just recenter the
535                  * view in case they played with subsurfaces */
536                 fs_output_center_view(fsout);
537                 return;
538         }
539
540         /* We have a pending surface */
541         surface_subsurfaces_boundingbox(fsout->pending.surface,
542                                         &surf_x, &surf_y,
543                                         &surf_width, &surf_height);
544
545         /* The actual output mode is in physical units.  We need to
546          * transform the surface size to physical unit size by flipping ans
547          * possibly scaling it.
548          */
549         switch (fsout->output->transform) {
550         case WL_OUTPUT_TRANSFORM_90:
551         case WL_OUTPUT_TRANSFORM_FLIPPED_90:
552         case WL_OUTPUT_TRANSFORM_270:
553         case WL_OUTPUT_TRANSFORM_FLIPPED_270:
554                 mode.width = surf_height * fsout->output->native_scale;
555                 mode.height = surf_width * fsout->output->native_scale;
556                 break;
557
558         case WL_OUTPUT_TRANSFORM_NORMAL:
559         case WL_OUTPUT_TRANSFORM_FLIPPED:
560         case WL_OUTPUT_TRANSFORM_180:
561         case WL_OUTPUT_TRANSFORM_FLIPPED_180:
562         default:
563                 mode.width = surf_width * fsout->output->native_scale;
564                 mode.height = surf_height * fsout->output->native_scale;
565         }
566         mode.flags = 0;
567         mode.refresh = fsout->pending.framerate;
568
569         ret = weston_output_mode_switch_to_temporary(fsout->output, &mode,
570                                         fsout->output->native_scale);
571
572         if (ret != 0) {
573                 /* The mode switch failed.  Clear the pending and
574                  * reconfigure as per normal */
575                 if (fsout->pending.mode_feedback) {
576                         zwp_fullscreen_shell_mode_feedback_v1_send_mode_failed(
577                                 fsout->pending.mode_feedback);
578                         wl_resource_destroy(fsout->pending.mode_feedback);
579                         fsout->pending.mode_feedback = NULL;
580                 }
581
582                 fs_output_clear_pending(fsout);
583                 return;
584         }
585
586         if (fsout->pending.mode_feedback) {
587                 zwp_fullscreen_shell_mode_feedback_v1_send_mode_successful(
588                         fsout->pending.mode_feedback);
589                 wl_resource_destroy(fsout->pending.mode_feedback);
590                 fsout->pending.mode_feedback = NULL;
591         }
592
593         fs_output_apply_pending(fsout);
594
595         weston_view_set_position(fsout->view,
596                                  fsout->output->x - surf_x,
597                                  fsout->output->y - surf_y);
598 }
599
600 static void
601 fs_output_configure(struct fs_output *fsout,
602                     struct weston_surface *surface)
603 {
604         if (fsout->pending.surface == surface) {
605                 if (fsout->pending.presented_for_mode)
606                         fs_output_configure_for_mode(fsout, surface);
607                 else
608                         fs_output_configure_simple(fsout, surface);
609         } else {
610                 if (fsout->presented_for_mode)
611                         fs_output_configure_for_mode(fsout, surface);
612                 else
613                         fs_output_configure_simple(fsout, surface);
614         }
615
616         weston_output_schedule_repaint(fsout->output);
617 }
618
619 static void
620 configure_presented_surface(struct weston_surface *surface, int32_t sx,
621                             int32_t sy)
622 {
623         struct fullscreen_shell *shell = surface->committed_private;
624         struct fs_output *fsout;
625
626         if (surface->committed != configure_presented_surface)
627                 return;
628
629         wl_list_for_each(fsout, &shell->output_list, link)
630                 if (fsout->surface == surface ||
631                     fsout->pending.surface == surface)
632                         fs_output_configure(fsout, surface);
633 }
634
635 static void
636 fs_output_apply_pending(struct fs_output *fsout)
637 {
638         assert(fsout->pending.surface);
639
640         if (fsout->surface && fsout->surface != fsout->pending.surface) {
641                 wl_list_remove(&fsout->surface_destroyed.link);
642
643                 weston_view_destroy(fsout->view);
644                 fsout->view = NULL;
645
646                 if (wl_list_empty(&fsout->surface->views)) {
647                         fsout->surface->committed = NULL;
648                         fsout->surface->committed_private = NULL;
649                 }
650
651                 fsout->surface = NULL;
652         }
653
654         fsout->method = fsout->pending.method;
655         fsout->framerate = fsout->pending.framerate;
656         fsout->presented_for_mode = fsout->pending.presented_for_mode;
657
658         if (fsout->surface != fsout->pending.surface) {
659                 fsout->surface = fsout->pending.surface;
660
661                 fsout->view = weston_view_create(fsout->surface);
662                 if (!fsout->view) {
663                         weston_log("no memory\n");
664                         return;
665                 }
666                 fsout->view->is_mapped = true;
667
668                 wl_signal_add(&fsout->surface->destroy_signal,
669                               &fsout->surface_destroyed);
670                 weston_layer_entry_insert(&fsout->shell->layer.view_list,
671                                &fsout->view->layer_link);
672         }
673
674         fs_output_clear_pending(fsout);
675 }
676
677 static void
678 fs_output_clear_pending(struct fs_output *fsout)
679 {
680         if (!fsout->pending.surface)
681                 return;
682
683         if (fsout->pending.mode_feedback) {
684                 zwp_fullscreen_shell_mode_feedback_v1_send_present_cancelled(
685                         fsout->pending.mode_feedback);
686                 wl_resource_destroy(fsout->pending.mode_feedback);
687                 fsout->pending.mode_feedback = NULL;
688         }
689
690         wl_list_remove(&fsout->pending.surface_destroyed.link);
691         fsout->pending.surface = NULL;
692 }
693
694 static void
695 fs_output_set_surface(struct fs_output *fsout, struct weston_surface *surface,
696                       enum zwp_fullscreen_shell_v1_present_method method,
697                       int32_t framerate, int presented_for_mode)
698 {
699         fs_output_clear_pending(fsout);
700
701         if (surface) {
702                 if (!surface->committed) {
703                         surface->committed = configure_presented_surface;
704                         surface->committed_private = fsout->shell;
705                 }
706
707                 fsout->pending.surface = surface;
708                 wl_signal_add(&fsout->pending.surface->destroy_signal,
709                               &fsout->pending.surface_destroyed);
710
711                 fsout->pending.method = method;
712                 fsout->pending.framerate = framerate;
713                 fsout->pending.presented_for_mode = presented_for_mode;
714         } else if (fsout->surface) {
715                 /* we clear immediately */
716                 wl_list_remove(&fsout->surface_destroyed.link);
717
718                 weston_view_destroy(fsout->view);
719                 fsout->view = NULL;
720
721                 if (wl_list_empty(&fsout->surface->views)) {
722                         fsout->surface->committed = NULL;
723                         fsout->surface->committed_private = NULL;
724                 }
725
726                 fsout->surface = NULL;
727
728                 weston_output_schedule_repaint(fsout->output);
729         }
730 }
731
732 static void
733 fullscreen_shell_release(struct wl_client *client,
734                          struct wl_resource *resource)
735 {
736         wl_resource_destroy(resource);
737 }
738
739 static void
740 fullscreen_shell_present_surface(struct wl_client *client,
741                                  struct wl_resource *resource,
742                                  struct wl_resource *surface_res,
743                                  uint32_t method,
744                                  struct wl_resource *output_res)
745 {
746         struct fullscreen_shell *shell =
747                 wl_resource_get_user_data(resource);
748         struct weston_output *output;
749         struct weston_surface *surface;
750         struct weston_seat *seat;
751         struct fs_output *fsout;
752
753         surface = surface_res ? wl_resource_get_user_data(surface_res) : NULL;
754
755         switch(method) {
756         case ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_DEFAULT:
757         case ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_CENTER:
758         case ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_ZOOM:
759         case ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_ZOOM_CROP:
760         case ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_STRETCH:
761                 break;
762         default:
763                 wl_resource_post_error(resource,
764                                        ZWP_FULLSCREEN_SHELL_V1_ERROR_INVALID_METHOD,
765                                        "Invalid presentation method");
766         }
767
768         if (output_res) {
769                 output = wl_resource_get_user_data(output_res);
770                 fsout = fs_output_for_output(output);
771                 fs_output_set_surface(fsout, surface, method, 0, 0);
772         } else {
773                 replace_default_surface(shell, surface, method);
774
775                 wl_list_for_each(fsout, &shell->output_list, link)
776                         fs_output_set_surface(fsout, surface, method, 0, 0);
777         }
778
779         if (surface) {
780                 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
781                         struct weston_keyboard *keyboard =
782                                 weston_seat_get_keyboard(seat);
783
784                         if (keyboard && !keyboard->focus)
785                                 weston_seat_set_keyboard_focus(seat, surface);
786                 }
787         }
788 }
789
790 static void
791 mode_feedback_destroyed(struct wl_resource *resource)
792 {
793         struct fs_output *fsout = wl_resource_get_user_data(resource);
794
795         fsout->pending.mode_feedback = NULL;
796 }
797
798 static void
799 fullscreen_shell_present_surface_for_mode(struct wl_client *client,
800                                           struct wl_resource *resource,
801                                           struct wl_resource *surface_res,
802                                           struct wl_resource *output_res,
803                                           int32_t framerate,
804                                           uint32_t feedback_id)
805 {
806         struct fullscreen_shell *shell =
807                 wl_resource_get_user_data(resource);
808         struct weston_output *output;
809         struct weston_surface *surface;
810         struct weston_seat *seat;
811         struct fs_output *fsout;
812
813         output = wl_resource_get_user_data(output_res);
814         fsout = fs_output_for_output(output);
815
816         if (surface_res == NULL) {
817                 fs_output_set_surface(fsout, NULL, 0, 0, 0);
818                 return;
819         }
820
821         surface = wl_resource_get_user_data(surface_res);
822         fs_output_set_surface(fsout, surface, 0, framerate, 1);
823
824         fsout->pending.mode_feedback =
825                 wl_resource_create(client,
826                                    &zwp_fullscreen_shell_mode_feedback_v1_interface,
827                                    1, feedback_id);
828         wl_resource_set_implementation(fsout->pending.mode_feedback, NULL,
829                                        fsout, mode_feedback_destroyed);
830
831         wl_list_for_each(seat, &shell->compositor->seat_list, link) {
832                 struct weston_keyboard *keyboard =
833                         weston_seat_get_keyboard(seat);
834
835                 if (keyboard && !keyboard->focus)
836                         weston_seat_set_keyboard_focus(seat, surface);
837         }
838 }
839
840 struct zwp_fullscreen_shell_v1_interface fullscreen_shell_implementation = {
841         fullscreen_shell_release,
842         fullscreen_shell_present_surface,
843         fullscreen_shell_present_surface_for_mode,
844 };
845
846 static void
847 output_created(struct wl_listener *listener, void *data)
848 {
849         struct fullscreen_shell *shell;
850
851         shell = container_of(listener, struct fullscreen_shell,
852                              output_created_listener);
853
854         fs_output_create(shell, data);
855 }
856
857 static void
858 client_destroyed(struct wl_listener *listener, void *data)
859 {
860         struct fullscreen_shell *shell = container_of(listener,
861                                                      struct fullscreen_shell,
862                                                      client_destroyed);
863         shell->client = NULL;
864 }
865
866 static void
867 bind_fullscreen_shell(struct wl_client *client, void *data, uint32_t version,
868                        uint32_t id)
869 {
870         struct fullscreen_shell *shell = data;
871         struct wl_resource *resource;
872
873         if (shell->client != NULL && shell->client != client)
874                 return;
875         else if (shell->client == NULL) {
876                 shell->client = client;
877                 wl_client_add_destroy_listener(client, &shell->client_destroyed);
878         }
879
880         resource = wl_resource_create(client,
881                                       &zwp_fullscreen_shell_v1_interface,
882                                       1, id);
883         wl_resource_set_implementation(resource,
884                                        &fullscreen_shell_implementation,
885                                        shell, NULL);
886
887         if (shell->compositor->capabilities & WESTON_CAP_CURSOR_PLANE)
888                 zwp_fullscreen_shell_v1_send_capability(resource,
889                         ZWP_FULLSCREEN_SHELL_V1_CAPABILITY_CURSOR_PLANE);
890
891         if (shell->compositor->capabilities & WESTON_CAP_ARBITRARY_MODES)
892                 zwp_fullscreen_shell_v1_send_capability(resource,
893                         ZWP_FULLSCREEN_SHELL_V1_CAPABILITY_ARBITRARY_MODES);
894 }
895
896 WL_EXPORT int
897 module_init(struct weston_compositor *compositor,
898             int *argc, char *argv[])
899 {
900         struct fullscreen_shell *shell;
901         struct weston_seat *seat;
902         struct weston_output *output;
903
904         shell = zalloc(sizeof *shell);
905         if (shell == NULL)
906                 return -1;
907
908         shell->compositor = compositor;
909         wl_list_init(&shell->default_surface_list);
910
911         shell->client_destroyed.notify = client_destroyed;
912
913         weston_layer_init(&shell->layer, &compositor->cursor_layer.link);
914
915         wl_list_init(&shell->output_list);
916         shell->output_created_listener.notify = output_created;
917         wl_signal_add(&compositor->output_created_signal,
918                       &shell->output_created_listener);
919         wl_list_for_each(output, &compositor->output_list, link)
920                 fs_output_create(shell, output);
921
922         shell->seat_created_listener.notify = seat_created;
923         wl_signal_add(&compositor->seat_created_signal,
924                       &shell->seat_created_listener);
925         wl_list_for_each(seat, &compositor->seat_list, link)
926                 seat_created(NULL, seat);
927
928         wl_global_create(compositor->wl_display,
929                          &zwp_fullscreen_shell_v1_interface, 1, shell,
930                          bind_fullscreen_shell);
931
932         return 0;
933 }