summaryrefslogtreecommitdiff
path: root/fullscreen-shell/fullscreen-shell.c
blob: 1eea38af8db8c13d7100612bfbdf9ff00df83376 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
/*
 * Copyright © 2013 Jason Ekstrand
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "config.h"

#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>

#include "compositor.h"
#include "fullscreen-shell-unstable-v1-server-protocol.h"
#include "shared/helpers.h"

struct fullscreen_shell {
	struct wl_client *client;
	struct wl_listener client_destroyed;
	struct weston_compositor *compositor;

	struct weston_layer layer;
	struct wl_list output_list;
	struct wl_listener output_created_listener;

	struct wl_listener seat_created_listener;
};

struct fs_output {
	struct fullscreen_shell *shell;
	struct wl_list link;

	struct weston_output *output;
	struct wl_listener output_destroyed;

	struct {
		struct weston_surface *surface;
		struct wl_listener surface_destroyed;
		struct wl_resource *mode_feedback;

		int presented_for_mode;
		enum zwp_fullscreen_shell_v1_present_method method;
		int32_t framerate;
	} pending;

	struct weston_surface *surface;
	struct wl_listener surface_destroyed;
	struct weston_view *view;
	struct weston_view *black_view;
	struct weston_transform transform; /* matrix from x, y */

	int presented_for_mode;
	enum zwp_fullscreen_shell_v1_present_method method;
	uint32_t framerate;
};

struct pointer_focus_listener {
	struct fullscreen_shell *shell;
	struct wl_listener pointer_focus;
	struct wl_listener seat_caps;
	struct wl_listener seat_destroyed;
};

static void
pointer_focus_changed(struct wl_listener *listener, void *data)
{
	struct weston_pointer *pointer = data;

	if (pointer->focus && pointer->focus->surface->resource)
		weston_seat_set_keyboard_focus(pointer->seat, pointer->focus->surface);
}

static void
seat_caps_changed(struct wl_listener *l, void *data)
{
	struct weston_seat *seat = data;
	struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
	struct weston_pointer *pointer = weston_seat_get_pointer(seat);
	struct pointer_focus_listener *listener;
	struct fs_output *fsout;

	listener = container_of(l, struct pointer_focus_listener, seat_caps);

	/* no pointer */
	if (pointer) {
		if (!listener->pointer_focus.link.prev) {
			wl_signal_add(&pointer->focus_signal,
				      &listener->pointer_focus);
		}
	} else {
		if (listener->pointer_focus.link.prev) {
			wl_list_remove(&listener->pointer_focus.link);
		}
	}

	if (keyboard && keyboard->focus != NULL) {
		wl_list_for_each(fsout, &listener->shell->output_list, link) {
			if (fsout->surface) {
				weston_seat_set_keyboard_focus(seat, fsout->surface);
				return;
			}
		}
	}
}

static void
seat_destroyed(struct wl_listener *l, void *data)
{
	struct pointer_focus_listener *listener;

	listener = container_of(l, struct pointer_focus_listener,
				seat_destroyed);

	free(listener);
}

static void
seat_created(struct wl_listener *l, void *data)
{
	struct weston_seat *seat = data;
	struct pointer_focus_listener *listener;

	listener = zalloc(sizeof *listener);
	if (!listener)
		return;

	listener->shell = container_of(l, struct fullscreen_shell,
				       seat_created_listener);
	listener->pointer_focus.notify = pointer_focus_changed;
	listener->seat_caps.notify = seat_caps_changed;
	listener->seat_destroyed.notify = seat_destroyed;

	wl_signal_add(&seat->destroy_signal, &listener->seat_destroyed);
	wl_signal_add(&seat->updated_caps_signal, &listener->seat_caps);

	seat_caps_changed(&listener->seat_caps, seat);
}

static void
black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
{
}

static struct weston_view *
create_black_surface(struct weston_compositor *ec, struct fs_output *fsout,
		     float x, float y, int w, int h)
{
	struct weston_surface *surface = NULL;
	struct weston_view *view;

	surface = weston_surface_create(ec);
	if (surface == NULL) {
		weston_log("no memory\n");
		return NULL;
	}
	view = weston_view_create(surface);
	if (!view) {
		weston_surface_destroy(surface);
		weston_log("no memory\n");
		return NULL;
	}

	surface->configure = black_surface_configure;
	surface->configure_private = fsout;
	weston_surface_set_color(surface, 0.0f, 0.0f, 0.0f, 1.0f);
	pixman_region32_fini(&surface->opaque);
	pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
	pixman_region32_fini(&surface->input);
	pixman_region32_init_rect(&surface->input, 0, 0, w, h);

	weston_surface_set_size(surface, w, h);
	weston_view_set_position(view, x, y);

	return view;
}

static void
fs_output_set_surface(struct fs_output *fsout, struct weston_surface *surface,
		      enum zwp_fullscreen_shell_v1_present_method method,
		      int32_t framerate, int presented_for_mode);
static void
fs_output_apply_pending(struct fs_output *fsout);
static void
fs_output_clear_pending(struct fs_output *fsout);

static void
fs_output_destroy(struct fs_output *fsout)
{
	fs_output_set_surface(fsout, NULL, 0, 0, 0);
	fs_output_clear_pending(fsout);

	wl_list_remove(&fsout->link);

	if (fsout->output)
		wl_list_remove(&fsout->output_destroyed.link);
}

static void
output_destroyed(struct wl_listener *listener, void *data)
{
	struct fs_output *output = container_of(listener,
						struct fs_output,
						output_destroyed);
	fs_output_destroy(output);
}

static void
surface_destroyed(struct wl_listener *listener, void *data)
{
	struct fs_output *fsout = container_of(listener,
					       struct fs_output,
					       surface_destroyed);
	fsout->surface = NULL;
	fsout->view = NULL;
}

static void
pending_surface_destroyed(struct wl_listener *listener, void *data)
{
	struct fs_output *fsout = container_of(listener,
					       struct fs_output,
					       pending.surface_destroyed);
	fsout->pending.surface = NULL;
}

static struct fs_output *
fs_output_create(struct fullscreen_shell *shell, struct weston_output *output)
{
	struct fs_output *fsout;

	fsout = zalloc(sizeof *fsout);
	if (!fsout)
		return NULL;

	fsout->shell = shell;
	wl_list_insert(&shell->output_list, &fsout->link);

	fsout->output = output;
	fsout->output_destroyed.notify = output_destroyed;
	wl_signal_add(&output->destroy_signal, &fsout->output_destroyed);

	fsout->surface_destroyed.notify = surface_destroyed;
	fsout->pending.surface_destroyed.notify = pending_surface_destroyed;
	fsout->black_view = create_black_surface(shell->compositor, fsout,
						 output->x, output->y,
						 output->width, output->height);
	fsout->black_view->surface->is_mapped = true;
	fsout->black_view->is_mapped = true;
	weston_layer_entry_insert(&shell->layer.view_list,
		       &fsout->black_view->layer_link);
	wl_list_init(&fsout->transform.link);
	return fsout;
}

static struct fs_output *
fs_output_for_output(struct weston_output *output)
{
	struct wl_listener *listener;

	if (!output)
		return NULL;

	listener = wl_signal_get(&output->destroy_signal, output_destroyed);

	return container_of(listener, struct fs_output, output_destroyed);
}

static void
restore_output_mode(struct weston_output *output)
{
	if (output && output->original_mode)
		weston_output_mode_switch_to_native(output);
}

/*
 * Returns the bounding box of a surface and all its sub-surfaces,
 * in surface-local coordinates. */
static void
surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
				int32_t *y, int32_t *w, int32_t *h) {
	pixman_region32_t region;
	pixman_box32_t *box;
	struct weston_subsurface *subsurface;

	pixman_region32_init_rect(&region, 0, 0,
	                          surface->width,
	                          surface->height);

	wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
		pixman_region32_union_rect(&region, &region,
		                           subsurface->position.x,
		                           subsurface->position.y,
		                           subsurface->surface->width,
		                           subsurface->surface->height);
	}

	box = pixman_region32_extents(&region);
	if (x)
		*x = box->x1;
	if (y)
		*y = box->y1;
	if (w)
		*w = box->x2 - box->x1;
	if (h)
		*h = box->y2 - box->y1;

	pixman_region32_fini(&region);
}

static void
fs_output_center_view(struct fs_output *fsout)
{
	int32_t surf_x, surf_y, surf_width, surf_height;
	float x, y;
	struct weston_output *output = fsout->output;

	surface_subsurfaces_boundingbox(fsout->view->surface, &surf_x, &surf_y,
					&surf_width, &surf_height);

	x = output->x + (output->width - surf_width) / 2 - surf_x / 2;
	y = output->y + (output->height - surf_height) / 2 - surf_y / 2;

	weston_view_set_position(fsout->view, x, y);
}

static void
fs_output_scale_view(struct fs_output *fsout, float width, float height)
{
	float x, y;
	int32_t surf_x, surf_y, surf_width, surf_height;
	struct weston_matrix *matrix;
	struct weston_view *view = fsout->view;
	struct weston_output *output = fsout->output;

	surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y,
					&surf_width, &surf_height);

	if (output->width == surf_width && output->height == surf_height) {
		weston_view_set_position(view,
					 fsout->output->x - surf_x,
					 fsout->output->y - surf_y);
	} else {
		matrix = &fsout->transform.matrix;
		weston_matrix_init(matrix);

		weston_matrix_scale(matrix, width / surf_width,
				    height / surf_height, 1);
		wl_list_remove(&fsout->transform.link);
		wl_list_insert(&fsout->view->geometry.transformation_list,
			       &fsout->transform.link);

		x = output->x + (output->width - width) / 2 - surf_x;
		y = output->y + (output->height - height) / 2 - surf_y;

		weston_view_set_position(view, x, y);
	}
}

static void
fs_output_configure(struct fs_output *fsout, struct weston_surface *surface);

static void
fs_output_configure_simple(struct fs_output *fsout,
			   struct weston_surface *configured_surface)
{
	struct weston_output *output = fsout->output;
	float output_aspect, surface_aspect;
	int32_t surf_x, surf_y, surf_width, surf_height;

	if (fsout->pending.surface == configured_surface)
		fs_output_apply_pending(fsout);

	assert(fsout->view);

	restore_output_mode(fsout->output);

	wl_list_remove(&fsout->transform.link);
	wl_list_init(&fsout->transform.link);

	surface_subsurfaces_boundingbox(fsout->view->surface,
					&surf_x, &surf_y,
					&surf_width, &surf_height);

	output_aspect = (float) output->width / (float) output->height;
	surface_aspect = (float) surf_width / (float) surf_height;

	switch (fsout->method) {
	case ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_DEFAULT:
	case ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_CENTER:
		fs_output_center_view(fsout);
		break;

	case ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_ZOOM:
		if (output_aspect < surface_aspect)
			fs_output_scale_view(fsout,
					     output->width,
					     output->width / surface_aspect);
		else
			fs_output_scale_view(fsout,
					     output->height * surface_aspect,
					     output->height);
		break;

	case ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_ZOOM_CROP:
		if (output_aspect < surface_aspect)
			fs_output_scale_view(fsout,
					     output->height * surface_aspect,
					     output->height);
		else
			fs_output_scale_view(fsout,
					     output->width,
					     output->width / surface_aspect);
		break;

	case ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_STRETCH:
		fs_output_scale_view(fsout, output->width, output->height);
		break;
	default:
		break;
	}

	weston_view_set_position(fsout->black_view,
				 fsout->output->x - surf_x,
				 fsout->output->y - surf_y);
	weston_surface_set_size(fsout->black_view->surface,
				fsout->output->width,
				fsout->output->height);
}

static void
fs_output_configure_for_mode(struct fs_output *fsout,
			     struct weston_surface *configured_surface)
{
	int32_t surf_x, surf_y, surf_width, surf_height;
	struct weston_mode mode;
	int ret;

	if (fsout->pending.surface != configured_surface) {
		/* Nothing to really reconfigure.  We'll just recenter the
		 * view in case they played with subsurfaces */
		fs_output_center_view(fsout);
		return;
	}

	/* We have a pending surface */
	surface_subsurfaces_boundingbox(fsout->pending.surface,
					&surf_x, &surf_y,
					&surf_width, &surf_height);

	/* The actual output mode is in physical units.  We need to
	 * transform the surface size to physical unit size by flipping ans
	 * possibly scaling it.
	 */
	switch (fsout->output->transform) {
	case WL_OUTPUT_TRANSFORM_90:
	case WL_OUTPUT_TRANSFORM_FLIPPED_90:
	case WL_OUTPUT_TRANSFORM_270:
	case WL_OUTPUT_TRANSFORM_FLIPPED_270:
		mode.width = surf_height * fsout->output->native_scale;
		mode.height = surf_width * fsout->output->native_scale;
		break;

	case WL_OUTPUT_TRANSFORM_NORMAL:
	case WL_OUTPUT_TRANSFORM_FLIPPED:
	case WL_OUTPUT_TRANSFORM_180:
	case WL_OUTPUT_TRANSFORM_FLIPPED_180:
	default:
		mode.width = surf_width * fsout->output->native_scale;
		mode.height = surf_height * fsout->output->native_scale;
	}
	mode.flags = 0;
	mode.refresh = fsout->pending.framerate;

	ret = weston_output_mode_switch_to_temporary(fsout->output, &mode,
					fsout->output->native_scale);

	if (ret != 0) {
		/* The mode switch failed.  Clear the pending and
		 * reconfigure as per normal */
		if (fsout->pending.mode_feedback) {
			zwp_fullscreen_shell_mode_feedback_v1_send_mode_failed(
				fsout->pending.mode_feedback);
			wl_resource_destroy(fsout->pending.mode_feedback);
			fsout->pending.mode_feedback = NULL;
		}

		fs_output_clear_pending(fsout);
		return;
	}

	if (fsout->pending.mode_feedback) {
		zwp_fullscreen_shell_mode_feedback_v1_send_mode_successful(
			fsout->pending.mode_feedback);
		wl_resource_destroy(fsout->pending.mode_feedback);
		fsout->pending.mode_feedback = NULL;
	}

	fs_output_apply_pending(fsout);

	weston_view_set_position(fsout->view,
				 fsout->output->x - surf_x,
				 fsout->output->y - surf_y);
}

static void
fs_output_configure(struct fs_output *fsout,
		    struct weston_surface *surface)
{
	if (fsout->pending.surface == surface) {
		if (fsout->pending.presented_for_mode)
			fs_output_configure_for_mode(fsout, surface);
		else
			fs_output_configure_simple(fsout, surface);
	} else {
		if (fsout->presented_for_mode)
			fs_output_configure_for_mode(fsout, surface);
		else
			fs_output_configure_simple(fsout, surface);
	}

	weston_output_schedule_repaint(fsout->output);
}

static void
configure_presented_surface(struct weston_surface *surface, int32_t sx,
			    int32_t sy)
{
	struct fullscreen_shell *shell = surface->configure_private;
	struct fs_output *fsout;

	if (surface->configure != configure_presented_surface)
		return;

	wl_list_for_each(fsout, &shell->output_list, link)
		if (fsout->surface == surface ||
		    fsout->pending.surface == surface)
			fs_output_configure(fsout, surface);
}

static void
fs_output_apply_pending(struct fs_output *fsout)
{
	assert(fsout->pending.surface);

	if (fsout->surface && fsout->surface != fsout->pending.surface) {
		wl_list_remove(&fsout->surface_destroyed.link);

		weston_view_destroy(fsout->view);
		fsout->view = NULL;

		if (wl_list_empty(&fsout->surface->views)) {
			fsout->surface->configure = NULL;
			fsout->surface->configure_private = NULL;
		}

		fsout->surface = NULL;
	}

	fsout->method = fsout->pending.method;
	fsout->framerate = fsout->pending.framerate;
	fsout->presented_for_mode = fsout->pending.presented_for_mode;

	if (fsout->surface != fsout->pending.surface) {
		fsout->surface = fsout->pending.surface;

		fsout->view = weston_view_create(fsout->surface);
		if (!fsout->view) {
			weston_log("no memory\n");
			return;
		}
		fsout->view->is_mapped = true;

		wl_signal_add(&fsout->surface->destroy_signal,
			      &fsout->surface_destroyed);
		weston_layer_entry_insert(&fsout->shell->layer.view_list,
			       &fsout->view->layer_link);
	}

	fs_output_clear_pending(fsout);
}

static void
fs_output_clear_pending(struct fs_output *fsout)
{
	if (!fsout->pending.surface)
		return;

	if (fsout->pending.mode_feedback) {
		zwp_fullscreen_shell_mode_feedback_v1_send_present_cancelled(
			fsout->pending.mode_feedback);
		wl_resource_destroy(fsout->pending.mode_feedback);
		fsout->pending.mode_feedback = NULL;
	}

	wl_list_remove(&fsout->pending.surface_destroyed.link);
	fsout->pending.surface = NULL;
}

static void
fs_output_set_surface(struct fs_output *fsout, struct weston_surface *surface,
		      enum zwp_fullscreen_shell_v1_present_method method,
		      int32_t framerate, int presented_for_mode)
{
	fs_output_clear_pending(fsout);

	if (surface) {
		if (!surface->configure) {
			surface->configure = configure_presented_surface;
			surface->configure_private = fsout->shell;
		}

		fsout->pending.surface = surface;
		wl_signal_add(&fsout->pending.surface->destroy_signal,
			      &fsout->pending.surface_destroyed);

		fsout->pending.method = method;
		fsout->pending.framerate = framerate;
		fsout->pending.presented_for_mode = presented_for_mode;
	} else if (fsout->surface) {
		/* we clear immediately */
		wl_list_remove(&fsout->surface_destroyed.link);

		weston_view_destroy(fsout->view);
		fsout->view = NULL;

		if (wl_list_empty(&fsout->surface->views)) {
			fsout->surface->configure = NULL;
			fsout->surface->configure_private = NULL;
		}

		fsout->surface = NULL;

		weston_output_schedule_repaint(fsout->output);
	}
}

static void
fullscreen_shell_release(struct wl_client *client,
			 struct wl_resource *resource)
{
	wl_resource_destroy(resource);
}

static void
fullscreen_shell_present_surface(struct wl_client *client,
				 struct wl_resource *resource,
				 struct wl_resource *surface_res,
				 uint32_t method,
				 struct wl_resource *output_res)
{
	struct fullscreen_shell *shell =
		wl_resource_get_user_data(resource);
	struct weston_output *output;
	struct weston_surface *surface;
	struct weston_seat *seat;
	struct fs_output *fsout;

	surface = surface_res ? wl_resource_get_user_data(surface_res) : NULL;

	switch(method) {
	case ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_DEFAULT:
	case ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_CENTER:
	case ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_ZOOM:
	case ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_ZOOM_CROP:
	case ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_STRETCH:
		break;
	default:
		wl_resource_post_error(resource,
				       ZWP_FULLSCREEN_SHELL_V1_ERROR_INVALID_METHOD,
				       "Invalid presentation method");
	}

	if (output_res) {
		output = wl_resource_get_user_data(output_res);
		fsout = fs_output_for_output(output);
		fs_output_set_surface(fsout, surface, method, 0, 0);
	} else {
		wl_list_for_each(fsout, &shell->output_list, link)
			fs_output_set_surface(fsout, surface, method, 0, 0);
	}

	if (surface) {
		wl_list_for_each(seat, &shell->compositor->seat_list, link) {
			struct weston_keyboard *keyboard =
				weston_seat_get_keyboard(seat);

			if (keyboard && !keyboard->focus)
				weston_seat_set_keyboard_focus(seat, surface);
		}
	}
}

static void
mode_feedback_destroyed(struct wl_resource *resource)
{
	struct fs_output *fsout = wl_resource_get_user_data(resource);

	fsout->pending.mode_feedback = NULL;
}

static void
fullscreen_shell_present_surface_for_mode(struct wl_client *client,
					  struct wl_resource *resource,
					  struct wl_resource *surface_res,
					  struct wl_resource *output_res,
					  int32_t framerate,
					  uint32_t feedback_id)
{
	struct fullscreen_shell *shell =
		wl_resource_get_user_data(resource);
	struct weston_output *output;
	struct weston_surface *surface;
	struct weston_seat *seat;
	struct fs_output *fsout;

	output = wl_resource_get_user_data(output_res);
	fsout = fs_output_for_output(output);

	if (surface_res == NULL) {
		fs_output_set_surface(fsout, NULL, 0, 0, 0);
		return;
	}

	surface = wl_resource_get_user_data(surface_res);
	fs_output_set_surface(fsout, surface, 0, framerate, 1);

	fsout->pending.mode_feedback =
		wl_resource_create(client,
				   &zwp_fullscreen_shell_mode_feedback_v1_interface,
				   1, feedback_id);
	wl_resource_set_implementation(fsout->pending.mode_feedback, NULL,
				       fsout, mode_feedback_destroyed);

	wl_list_for_each(seat, &shell->compositor->seat_list, link) {
		struct weston_keyboard *keyboard =
			weston_seat_get_keyboard(seat);

		if (keyboard && !keyboard->focus)
			weston_seat_set_keyboard_focus(seat, surface);
	}
}

struct zwp_fullscreen_shell_v1_interface fullscreen_shell_implementation = {
	fullscreen_shell_release,
	fullscreen_shell_present_surface,
	fullscreen_shell_present_surface_for_mode,
};

static void
output_created(struct wl_listener *listener, void *data)
{
	struct fullscreen_shell *shell;

	shell = container_of(listener, struct fullscreen_shell,
			     output_created_listener);

	fs_output_create(shell, data);
}

static void
client_destroyed(struct wl_listener *listener, void *data)
{
	struct fullscreen_shell *shell = container_of(listener,
						     struct fullscreen_shell,
						     client_destroyed);
	shell->client = NULL;
}

static void
bind_fullscreen_shell(struct wl_client *client, void *data, uint32_t version,
		       uint32_t id)
{
	struct fullscreen_shell *shell = data;
	struct wl_resource *resource;

	if (shell->client != NULL && shell->client != client)
		return;
	else if (shell->client == NULL) {
		shell->client = client;
		wl_client_add_destroy_listener(client, &shell->client_destroyed);
	}

	resource = wl_resource_create(client,
				      &zwp_fullscreen_shell_v1_interface,
				      1, id);
	wl_resource_set_implementation(resource,
				       &fullscreen_shell_implementation,
				       shell, NULL);

	if (shell->compositor->capabilities & WESTON_CAP_CURSOR_PLANE)
		zwp_fullscreen_shell_v1_send_capability(resource,
			ZWP_FULLSCREEN_SHELL_V1_CAPABILITY_CURSOR_PLANE);

	if (shell->compositor->capabilities & WESTON_CAP_ARBITRARY_MODES)
		zwp_fullscreen_shell_v1_send_capability(resource,
			ZWP_FULLSCREEN_SHELL_V1_CAPABILITY_ARBITRARY_MODES);
}

WL_EXPORT int
module_init(struct weston_compositor *compositor,
	    int *argc, char *argv[])
{
	struct fullscreen_shell *shell;
	struct weston_seat *seat;
	struct weston_output *output;

	shell = zalloc(sizeof *shell);
	if (shell == NULL)
		return -1;

	shell->compositor = compositor;

	shell->client_destroyed.notify = client_destroyed;

	weston_layer_init(&shell->layer, &compositor->cursor_layer.link);

	wl_list_init(&shell->output_list);
	shell->output_created_listener.notify = output_created;
	wl_signal_add(&compositor->output_created_signal,
		      &shell->output_created_listener);
	wl_list_for_each(output, &compositor->output_list, link)
		fs_output_create(shell, output);

	shell->seat_created_listener.notify = seat_created;
	wl_signal_add(&compositor->seat_created_signal,
		      &shell->seat_created_listener);
	wl_list_for_each(seat, &compositor->seat_list, link)
		seat_created(NULL, seat);

	wl_global_create(compositor->wl_display,
			 &zwp_fullscreen_shell_v1_interface, 1, shell,
			 bind_fullscreen_shell);

	return 0;
}