• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2010-2012 Intel Corporation
3  * Copyright © 2011-2012 Collabora, Ltd.
4  * Copyright © 2013 Raspberry Pi Foundation
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25 
26 #include "config.h"
27 
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <linux/input.h>
34 #include <assert.h>
35 #include <signal.h>
36 #include <math.h>
37 #include <sys/types.h>
38 
39 #include "shell.h"
40 #include "compositor/weston.h"
41 #include "weston-desktop-shell-server-protocol.h"
42 #include <libweston/config-parser.h>
43 #include "shared/helpers.h"
44 #include "shared/timespec-util.h"
45 #include <libweston-desktop/libweston-desktop.h>
46 
47 #define DEFAULT_NUM_WORKSPACES 1
48 #define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
49 
50 struct focus_state {
51 	struct desktop_shell *shell;
52 	struct weston_seat *seat;
53 	struct workspace *ws;
54 	struct weston_surface *keyboard_focus;
55 	struct wl_list link;
56 	struct wl_listener seat_destroy_listener;
57 	struct wl_listener surface_destroy_listener;
58 };
59 
60 /*
61  * Surface stacking and ordering.
62  *
63  * This is handled using several linked lists of surfaces, organised into
64  * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
65  * above all of the surfaces in the layer below. The set of layers is static and
66  * in the following order (top-most first):
67  *  • Lock layer (only ever displayed on its own)
68  *  • Cursor layer
69  *  • Input panel layer
70  *  • Fullscreen layer
71  *  • Panel layer
72  *  • Workspace layers
73  *  • Background layer
74  *
75  * The list of layers may be manipulated to remove whole layers of surfaces from
76  * display. For example, when locking the screen, all layers except the lock
77  * layer are removed.
78  *
79  * A surface’s layer is modified on configuring the surface, in
80  * set_surface_type() (which is only called when the surface’s type change is
81  * _committed_). If a surface’s type changes (e.g. when making a window
82  * fullscreen) its layer changes too.
83  *
84  * In order to allow popup and transient surfaces to be correctly stacked above
85  * their parent surfaces, each surface tracks both its parent surface, and a
86  * linked list of its children. When a surface’s layer is updated, so are the
87  * layers of its children. Note that child surfaces are *not* the same as
88  * subsurfaces — child/parent surfaces are purely for maintaining stacking
89  * order.
90  *
91  * The children_link list of siblings of a surface (i.e. those surfaces which
92  * have the same parent) only contains weston_surfaces which have a
93  * shell_surface. Stacking is not implemented for non-shell_surface
94  * weston_surfaces. This means that the following implication does *not* hold:
95  *     (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
96  */
97 
98 struct shell_surface {
99 	struct wl_signal destroy_signal;
100 
101 	struct weston_desktop_surface *desktop_surface;
102 	struct weston_view *view;
103 	int32_t last_width, last_height;
104 
105 	struct desktop_shell *shell;
106 
107 	struct wl_list children_list;
108 	struct wl_list children_link;
109 
110 	int32_t saved_x, saved_y;
111 	bool saved_position_valid;
112 	bool saved_rotation_valid;
113 	int unresponsive, grabbed;
114 	uint32_t resize_edges;
115 
116 	struct {
117 		struct weston_transform transform;
118 		struct weston_matrix rotation;
119 	} rotation;
120 
121 	struct {
122 		struct weston_transform transform; /* matrix from x, y */
123 		struct weston_view *black_view;
124 	} fullscreen;
125 
126 	struct weston_transform workspace_transform;
127 
128 	struct weston_output *fullscreen_output;
129 	struct weston_output *output;
130 	struct wl_listener output_destroy_listener;
131 
132 	struct surface_state {
133 		bool fullscreen;
134 		bool maximized;
135 		bool lowered;
136 	} state;
137 
138 	struct {
139 		bool is_set;
140 		int32_t x;
141 		int32_t y;
142 	} xwayland;
143 
144 	int focus_count;
145 
146 	bool destroying;
147 };
148 
149 struct shell_grab {
150 	struct weston_pointer_grab grab;
151 	struct shell_surface *shsurf;
152 	struct wl_listener shsurf_destroy_listener;
153 };
154 
155 struct shell_touch_grab {
156 	struct weston_touch_grab grab;
157 	struct shell_surface *shsurf;
158 	struct wl_listener shsurf_destroy_listener;
159 	struct weston_touch *touch;
160 };
161 
162 struct weston_move_grab {
163 	struct shell_grab base;
164 	wl_fixed_t dx, dy;
165 	bool client_initiated;
166 };
167 
168 struct weston_touch_move_grab {
169 	struct shell_touch_grab base;
170 	int active;
171 	wl_fixed_t dx, dy;
172 };
173 
174 struct rotate_grab {
175 	struct shell_grab base;
176 	struct weston_matrix rotation;
177 	struct {
178 		float x;
179 		float y;
180 	} center;
181 };
182 
183 struct shell_seat {
184 	struct weston_seat *seat;
185 	struct wl_listener seat_destroy_listener;
186 	struct weston_surface *focused_surface;
187 
188 	struct wl_listener caps_changed_listener;
189 	struct wl_listener pointer_focus_listener;
190 	struct wl_listener keyboard_focus_listener;
191 };
192 
193 
194 static struct desktop_shell *
195 shell_surface_get_shell(struct shell_surface *shsurf);
196 
197 static void
198 set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
199 
200 static void
201 surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
202 
203 static void
204 shell_fade_startup(struct desktop_shell *shell);
205 
206 static void
207 shell_fade(struct desktop_shell *shell, enum fade_type type);
208 
209 static struct shell_seat *
210 get_shell_seat(struct weston_seat *seat);
211 
212 static void
213 get_output_panel_size(struct desktop_shell *shell,
214 		      struct weston_output *output,
215 		      int *width, int *height);
216 
217 static void
218 shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
219 
220 static int
shell_surface_get_label(struct weston_surface * surface,char * buf,size_t len)221 shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
222 {
223 	const char *t, *c;
224 	struct weston_desktop_surface *desktop_surface =
225 		weston_surface_get_desktop_surface(surface);
226 
227 	t = weston_desktop_surface_get_title(desktop_surface);
228 	c = weston_desktop_surface_get_app_id(desktop_surface);
229 
230 	return snprintf(buf, len, "%s window%s%s%s%s%s",
231 		"top-level",
232 		t ? " '" : "", t ?: "", t ? "'" : "",
233 		c ? " of " : "", c ?: "");
234 }
235 
236 static void
destroy_shell_grab_shsurf(struct wl_listener * listener,void * data)237 destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
238 {
239 	struct shell_grab *grab;
240 
241 	grab = container_of(listener, struct shell_grab,
242 			    shsurf_destroy_listener);
243 
244 	grab->shsurf = NULL;
245 }
246 
247 struct weston_view *
get_default_view(struct weston_surface * surface)248 get_default_view(struct weston_surface *surface)
249 {
250 	struct shell_surface *shsurf;
251 	struct weston_view *view;
252 
253 	if (!surface || wl_list_empty(&surface->views))
254 		return NULL;
255 
256 	shsurf = get_shell_surface(surface);
257 	if (shsurf)
258 		return shsurf->view;
259 
260 	wl_list_for_each(view, &surface->views, surface_link)
261 		if (weston_view_is_mapped(view))
262 			return view;
263 
264 	return container_of(surface->views.next, struct weston_view, surface_link);
265 }
266 
267 static void
shell_grab_start(struct shell_grab * grab,const struct weston_pointer_grab_interface * interface,struct shell_surface * shsurf,struct weston_pointer * pointer,enum weston_desktop_shell_cursor cursor)268 shell_grab_start(struct shell_grab *grab,
269 		 const struct weston_pointer_grab_interface *interface,
270 		 struct shell_surface *shsurf,
271 		 struct weston_pointer *pointer,
272 		 enum weston_desktop_shell_cursor cursor)
273 {
274 	struct desktop_shell *shell = shsurf->shell;
275 
276 	weston_seat_break_desktop_grabs(pointer->seat);
277 
278 	grab->grab.interface = interface;
279 	grab->shsurf = shsurf;
280 	grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
281 	wl_signal_add(&shsurf->destroy_signal,
282 		      &grab->shsurf_destroy_listener);
283 
284 	shsurf->grabbed = 1;
285 	weston_pointer_start_grab(pointer, &grab->grab);
286 	if (shell->child.desktop_shell) {
287 		weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
288 					       cursor);
289 		weston_pointer_set_focus(pointer,
290 					 get_default_view(shell->grab_surface),
291 					 wl_fixed_from_int(0),
292 					 wl_fixed_from_int(0));
293 	}
294 }
295 
296 static void
get_panel_size(struct desktop_shell * shell,struct weston_view * view,int * width,int * height)297 get_panel_size(struct desktop_shell *shell,
298 	       struct weston_view *view,
299 	       int *width,
300 	       int *height)
301 {
302 	float x1, y1;
303 	float x2, y2;
304 	weston_view_to_global_float(view, 0, 0, &x1, &y1);
305 	weston_view_to_global_float(view,
306 				    view->surface->width,
307 				    view->surface->height,
308 				    &x2, &y2);
309 	*width = (int)(x2 - x1);
310 	*height = (int)(y2 - y1);
311 }
312 
313 static void
get_output_panel_size(struct desktop_shell * shell,struct weston_output * output,int * width,int * height)314 get_output_panel_size(struct desktop_shell *shell,
315 		      struct weston_output *output,
316 		      int *width,
317 		      int *height)
318 {
319 	struct weston_view *view;
320 
321 	*width = 0;
322 	*height = 0;
323 
324 	if (!output)
325 		return;
326 
327 	wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
328 		if (view->surface->output == output) {
329 			get_panel_size(shell, view, width, height);
330 			return;
331 		}
332 	}
333 
334 	/* the correct view wasn't found */
335 }
336 
337 void
get_output_work_area(struct desktop_shell * shell,struct weston_output * output,pixman_rectangle32_t * area)338 get_output_work_area(struct desktop_shell *shell,
339 		     struct weston_output *output,
340 		     pixman_rectangle32_t *area)
341 {
342 	int32_t panel_width = 0, panel_height = 0;
343 
344 	if (!output) {
345 		area->x = 0;
346 		area->y = 0;
347 		area->width = 0;
348 		area->height = 0;
349 
350 		return;
351 	}
352 
353 	area->x = output->x;
354 	area->y = output->y;
355 
356 	get_output_panel_size(shell, output, &panel_width, &panel_height);
357 	switch (shell->panel_position) {
358 	case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
359 	default:
360 		area->y += panel_height;
361 		/* fallthrough */
362 	case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
363 		area->width = output->width;
364 		area->height = output->height - panel_height;
365 		break;
366 	case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
367 		area->x += panel_width;
368 		/* fallthrough */
369 	case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
370 		area->width = output->width - panel_width;
371 		area->height = output->height;
372 		break;
373 	}
374 }
375 
376 static void
shell_grab_end(struct shell_grab * grab)377 shell_grab_end(struct shell_grab *grab)
378 {
379 	if (grab->shsurf) {
380 		wl_list_remove(&grab->shsurf_destroy_listener.link);
381 		grab->shsurf->grabbed = 0;
382 
383 		if (grab->shsurf->resize_edges) {
384 			grab->shsurf->resize_edges = 0;
385 		}
386 	}
387 
388 	weston_pointer_end_grab(grab->grab.pointer);
389 }
390 
391 static void
shell_touch_grab_start(struct shell_touch_grab * grab,const struct weston_touch_grab_interface * interface,struct shell_surface * shsurf,struct weston_touch * touch)392 shell_touch_grab_start(struct shell_touch_grab *grab,
393 		       const struct weston_touch_grab_interface *interface,
394 		       struct shell_surface *shsurf,
395 		       struct weston_touch *touch)
396 {
397 	struct desktop_shell *shell = shsurf->shell;
398 
399 	weston_seat_break_desktop_grabs(touch->seat);
400 
401 	grab->grab.interface = interface;
402 	grab->shsurf = shsurf;
403 	grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
404 	wl_signal_add(&shsurf->destroy_signal,
405 		      &grab->shsurf_destroy_listener);
406 
407 	grab->touch = touch;
408 	shsurf->grabbed = 1;
409 
410 	weston_touch_start_grab(touch, &grab->grab);
411 	if (shell->child.desktop_shell)
412 		weston_touch_set_focus(touch,
413 				       get_default_view(shell->grab_surface));
414 }
415 
416 static void
shell_touch_grab_end(struct shell_touch_grab * grab)417 shell_touch_grab_end(struct shell_touch_grab *grab)
418 {
419 	if (grab->shsurf) {
420 		wl_list_remove(&grab->shsurf_destroy_listener.link);
421 		grab->shsurf->grabbed = 0;
422 	}
423 
424 	weston_touch_end_grab(grab->touch);
425 }
426 
427 static void
428 center_on_output(struct weston_view *view,
429 		 struct weston_output *output);
430 
431 static enum weston_keyboard_modifier
get_modifier(char * modifier)432 get_modifier(char *modifier)
433 {
434 	if (!modifier)
435 		return MODIFIER_SUPER;
436 
437 	if (!strcmp("ctrl", modifier))
438 		return MODIFIER_CTRL;
439 	else if (!strcmp("alt", modifier))
440 		return MODIFIER_ALT;
441 	else if (!strcmp("super", modifier))
442 		return MODIFIER_SUPER;
443 	else if (!strcmp("none", modifier))
444 		return 0;
445 	else
446 		return MODIFIER_SUPER;
447 }
448 
449 static enum animation_type
get_animation_type(char * animation)450 get_animation_type(char *animation)
451 {
452 	if (!animation)
453 		return ANIMATION_NONE;
454 
455 	if (!strcmp("zoom", animation))
456 		return ANIMATION_ZOOM;
457 	else if (!strcmp("fade", animation))
458 		return ANIMATION_FADE;
459 	else if (!strcmp("dim-layer", animation))
460 		return ANIMATION_DIM_LAYER;
461 	else
462 		return ANIMATION_NONE;
463 }
464 
465 static void
shell_configuration(struct desktop_shell * shell)466 shell_configuration(struct desktop_shell *shell)
467 {
468 	struct weston_config_section *section;
469 	char *s, *client;
470 	bool allow_zap;
471 
472 	section = weston_config_get_section(wet_get_config(shell->compositor),
473 					    "shell", NULL, NULL);
474 	client = wet_get_libexec_path(WESTON_SHELL_CLIENT);
475 	weston_config_section_get_string(section, "client", &s, client);
476 	free(client);
477 	shell->client = s;
478 
479 	weston_config_section_get_bool(section,
480 				       "allow-zap", &allow_zap, true);
481 	shell->allow_zap = allow_zap;
482 
483 	weston_config_section_get_string(section,
484 					 "binding-modifier", &s, "super");
485 	shell->binding_modifier = get_modifier(s);
486 	free(s);
487 
488 	weston_config_section_get_string(section,
489 					 "exposay-modifier", &s, "none");
490 	shell->exposay_modifier = get_modifier(s);
491 	free(s);
492 
493 	weston_config_section_get_string(section, "animation", &s, "none");
494 	shell->win_animation_type = get_animation_type(s);
495 	free(s);
496 	weston_config_section_get_string(section, "close-animation", &s, "fade");
497 	shell->win_close_animation_type = get_animation_type(s);
498 	free(s);
499 	weston_config_section_get_string(section,
500 					 "startup-animation", &s, "fade");
501 	shell->startup_animation_type = get_animation_type(s);
502 	free(s);
503 	if (shell->startup_animation_type == ANIMATION_ZOOM)
504 		shell->startup_animation_type = ANIMATION_NONE;
505 	weston_config_section_get_string(section, "focus-animation", &s, "none");
506 	shell->focus_animation_type = get_animation_type(s);
507 	free(s);
508 	weston_config_section_get_uint(section, "num-workspaces",
509 				       &shell->workspaces.num,
510 				       DEFAULT_NUM_WORKSPACES);
511 }
512 
513 struct weston_output *
get_default_output(struct weston_compositor * compositor)514 get_default_output(struct weston_compositor *compositor)
515 {
516 	if (wl_list_empty(&compositor->output_list))
517 		return NULL;
518 
519 	return container_of(compositor->output_list.next,
520 			    struct weston_output, link);
521 }
522 
523 static int
focus_surface_get_label(struct weston_surface * surface,char * buf,size_t len)524 focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
525 {
526 	return snprintf(buf, len, "focus highlight effect for output %s",
527 			(surface->output ? surface->output->name : "NULL"));
528 }
529 
530 /* no-op func for checking focus surface */
531 static void
focus_surface_committed(struct weston_surface * es,int32_t sx,int32_t sy)532 focus_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
533 {
534 }
535 
536 static struct focus_surface *
get_focus_surface(struct weston_surface * surface)537 get_focus_surface(struct weston_surface *surface)
538 {
539 	if (surface->committed == focus_surface_committed)
540 		return surface->committed_private;
541 	else
542 		return NULL;
543 }
544 
545 static bool
is_focus_surface(struct weston_surface * es)546 is_focus_surface (struct weston_surface *es)
547 {
548 	return (es->committed == focus_surface_committed);
549 }
550 
551 static bool
is_focus_view(struct weston_view * view)552 is_focus_view (struct weston_view *view)
553 {
554 	return is_focus_surface (view->surface);
555 }
556 
557 static struct focus_surface *
create_focus_surface(struct weston_compositor * ec,struct weston_output * output)558 create_focus_surface(struct weston_compositor *ec,
559 		     struct weston_output *output)
560 {
561 	struct focus_surface *fsurf = NULL;
562 	struct weston_surface *surface = NULL;
563 
564 	fsurf = malloc(sizeof *fsurf);
565 	if (!fsurf)
566 		return NULL;
567 
568 	fsurf->surface = weston_surface_create(ec);
569 	surface = fsurf->surface;
570 	if (surface == NULL) {
571 		free(fsurf);
572 		return NULL;
573 	}
574 
575 	surface->committed = focus_surface_committed;
576 	surface->output = output;
577 	surface->is_mapped = true;
578 	surface->committed_private = fsurf;
579 	weston_surface_set_label_func(surface, focus_surface_get_label);
580 
581 	fsurf->view = weston_view_create(surface);
582 	if (fsurf->view == NULL) {
583 		weston_surface_destroy(surface);
584 		free(fsurf);
585 		return NULL;
586 	}
587 	weston_view_set_output(fsurf->view, output);
588 	fsurf->view->is_mapped = true;
589 
590 	weston_surface_set_size(surface, output->width, output->height);
591 	weston_view_set_position(fsurf->view, output->x, output->y);
592 	weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
593 	pixman_region32_fini(&surface->opaque);
594 	pixman_region32_init_rect(&surface->opaque, output->x, output->y,
595 				  output->width, output->height);
596 	pixman_region32_fini(&surface->input);
597 	pixman_region32_init(&surface->input);
598 
599 	wl_list_init(&fsurf->workspace_transform.link);
600 
601 	return fsurf;
602 }
603 
604 static void
focus_surface_destroy(struct focus_surface * fsurf)605 focus_surface_destroy(struct focus_surface *fsurf)
606 {
607 	weston_surface_destroy(fsurf->surface);
608 	free(fsurf);
609 }
610 
611 static void
focus_animation_done(struct weston_view_animation * animation,void * data)612 focus_animation_done(struct weston_view_animation *animation, void *data)
613 {
614 	struct workspace *ws = data;
615 
616 	ws->focus_animation = NULL;
617 }
618 
619 static void
focus_state_destroy(struct focus_state * state)620 focus_state_destroy(struct focus_state *state)
621 {
622 	wl_list_remove(&state->seat_destroy_listener.link);
623 	wl_list_remove(&state->surface_destroy_listener.link);
624 	free(state);
625 }
626 
627 static void
focus_state_seat_destroy(struct wl_listener * listener,void * data)628 focus_state_seat_destroy(struct wl_listener *listener, void *data)
629 {
630 	struct focus_state *state = container_of(listener,
631 						 struct focus_state,
632 						 seat_destroy_listener);
633 
634 	wl_list_remove(&state->link);
635 	focus_state_destroy(state);
636 }
637 
638 static void
focus_state_surface_destroy(struct wl_listener * listener,void * data)639 focus_state_surface_destroy(struct wl_listener *listener, void *data)
640 {
641 	struct focus_state *state = container_of(listener,
642 						 struct focus_state,
643 						 surface_destroy_listener);
644 	struct weston_surface *main_surface;
645 	struct weston_view *next;
646 	struct weston_view *view;
647 
648 	main_surface = weston_surface_get_main_surface(state->keyboard_focus);
649 
650 	next = NULL;
651 	wl_list_for_each(view,
652 			 &state->ws->layer.view_list.link, layer_link.link) {
653 		if (view->surface == main_surface)
654 			continue;
655 		if (is_focus_view(view))
656 			continue;
657 		if (!get_shell_surface(view->surface))
658 			continue;
659 
660 		next = view;
661 		break;
662 	}
663 
664 	/* if the focus was a sub-surface, activate its main surface */
665 	if (main_surface != state->keyboard_focus)
666 		next = get_default_view(main_surface);
667 
668 	if (next) {
669 		if (state->keyboard_focus) {
670 			wl_list_remove(&state->surface_destroy_listener.link);
671 			wl_list_init(&state->surface_destroy_listener.link);
672 		}
673 		state->keyboard_focus = NULL;
674 		activate(state->shell, next, state->seat,
675 			 WESTON_ACTIVATE_FLAG_CONFIGURE);
676 	} else {
677 		if (state->shell->focus_animation_type == ANIMATION_DIM_LAYER) {
678 			if (state->ws->focus_animation)
679 				weston_view_animation_destroy(state->ws->focus_animation);
680 
681 			state->ws->focus_animation = weston_fade_run(
682 				state->ws->fsurf_front->view,
683 				state->ws->fsurf_front->view->alpha, 0.0, 300,
684 				focus_animation_done, state->ws);
685 		}
686 
687 		wl_list_remove(&state->link);
688 		focus_state_destroy(state);
689 	}
690 }
691 
692 static struct focus_state *
focus_state_create(struct desktop_shell * shell,struct weston_seat * seat,struct workspace * ws)693 focus_state_create(struct desktop_shell *shell, struct weston_seat *seat,
694 		   struct workspace *ws)
695 {
696 	struct focus_state *state;
697 
698 	state = malloc(sizeof *state);
699 	if (state == NULL)
700 		return NULL;
701 
702 	state->shell = shell;
703 	state->keyboard_focus = NULL;
704 	state->ws = ws;
705 	state->seat = seat;
706 	wl_list_insert(&ws->focus_list, &state->link);
707 
708 	state->seat_destroy_listener.notify = focus_state_seat_destroy;
709 	state->surface_destroy_listener.notify = focus_state_surface_destroy;
710 	wl_signal_add(&seat->destroy_signal,
711 		      &state->seat_destroy_listener);
712 	wl_list_init(&state->surface_destroy_listener.link);
713 
714 	return state;
715 }
716 
717 static struct focus_state *
ensure_focus_state(struct desktop_shell * shell,struct weston_seat * seat)718 ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
719 {
720 	struct workspace *ws = get_current_workspace(shell);
721 	struct focus_state *state;
722 
723 	wl_list_for_each(state, &ws->focus_list, link)
724 		if (state->seat == seat)
725 			break;
726 
727 	if (&state->link == &ws->focus_list)
728 		state = focus_state_create(shell, seat, ws);
729 
730 	return state;
731 }
732 
733 static void
focus_state_set_focus(struct focus_state * state,struct weston_surface * surface)734 focus_state_set_focus(struct focus_state *state,
735 		      struct weston_surface *surface)
736 {
737 	if (state->keyboard_focus) {
738 		wl_list_remove(&state->surface_destroy_listener.link);
739 		wl_list_init(&state->surface_destroy_listener.link);
740 	}
741 
742 	state->keyboard_focus = surface;
743 	if (surface)
744 		wl_signal_add(&surface->destroy_signal,
745 			      &state->surface_destroy_listener);
746 }
747 
748 static void
restore_focus_state(struct desktop_shell * shell,struct workspace * ws)749 restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
750 {
751 	struct focus_state *state, *next;
752 	struct weston_surface *surface;
753 	struct wl_list pending_seat_list;
754 	struct weston_seat *seat, *next_seat;
755 
756 	/* Temporarily steal the list of seats so that we can keep
757 	 * track of the seats we've already processed */
758 	wl_list_init(&pending_seat_list);
759 	wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
760 	wl_list_init(&shell->compositor->seat_list);
761 
762 	wl_list_for_each_safe(state, next, &ws->focus_list, link) {
763 		struct weston_keyboard *keyboard =
764 			weston_seat_get_keyboard(state->seat);
765 
766 		wl_list_remove(&state->seat->link);
767 		wl_list_insert(&shell->compositor->seat_list,
768 			       &state->seat->link);
769 
770 		if (!keyboard)
771 			continue;
772 
773 		surface = state->keyboard_focus;
774 
775 		weston_keyboard_set_focus(keyboard, surface);
776 	}
777 
778 	/* For any remaining seats that we don't have a focus state
779 	 * for we'll reset the keyboard focus to NULL */
780 	wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
781 		struct weston_keyboard *keyboard =
782 			weston_seat_get_keyboard(seat);
783 
784 		wl_list_insert(&shell->compositor->seat_list, &seat->link);
785 
786 		if (!keyboard)
787 			continue;
788 
789 		weston_keyboard_set_focus(keyboard, NULL);
790 	}
791 }
792 
793 static void
replace_focus_state(struct desktop_shell * shell,struct workspace * ws,struct weston_seat * seat)794 replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
795 		    struct weston_seat *seat)
796 {
797 	struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
798 	struct focus_state *state;
799 
800 	wl_list_for_each(state, &ws->focus_list, link) {
801 		if (state->seat == seat) {
802 			focus_state_set_focus(state, keyboard->focus);
803 			return;
804 		}
805 	}
806 }
807 
808 static void
drop_focus_state(struct desktop_shell * shell,struct workspace * ws,struct weston_surface * surface)809 drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
810 		 struct weston_surface *surface)
811 {
812 	struct focus_state *state;
813 
814 	wl_list_for_each(state, &ws->focus_list, link)
815 		if (state->keyboard_focus == surface)
816 			focus_state_set_focus(state, NULL);
817 }
818 
819 static void
animate_focus_change(struct desktop_shell * shell,struct workspace * ws,struct weston_view * from,struct weston_view * to)820 animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
821 		     struct weston_view *from, struct weston_view *to)
822 {
823 	struct weston_output *output;
824 	bool focus_surface_created = false;
825 
826 	/* FIXME: Only support dim animation using two layers */
827 	if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
828 		return;
829 
830 	output = get_default_output(shell->compositor);
831 	if (ws->fsurf_front == NULL && (from || to)) {
832 		ws->fsurf_front = create_focus_surface(shell->compositor, output);
833 		if (ws->fsurf_front == NULL)
834 			return;
835 		ws->fsurf_front->view->alpha = 0.0;
836 
837 		ws->fsurf_back = create_focus_surface(shell->compositor, output);
838 		if (ws->fsurf_back == NULL) {
839 			focus_surface_destroy(ws->fsurf_front);
840 			return;
841 		}
842 		ws->fsurf_back->view->alpha = 0.0;
843 
844 		focus_surface_created = true;
845 	} else {
846 		weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
847 		weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
848 	}
849 
850 	if (ws->focus_animation) {
851 		weston_view_animation_destroy(ws->focus_animation);
852 		ws->focus_animation = NULL;
853 	}
854 
855 	if (to)
856 		weston_layer_entry_insert(&to->layer_link,
857 					  &ws->fsurf_front->view->layer_link);
858 	else if (from)
859 		weston_layer_entry_insert(&ws->layer.view_list,
860 					  &ws->fsurf_front->view->layer_link);
861 
862 	if (focus_surface_created) {
863 		ws->focus_animation = weston_fade_run(
864 			ws->fsurf_front->view,
865 			ws->fsurf_front->view->alpha, 0.4, 300,
866 			focus_animation_done, ws);
867 	} else if (from) {
868 		weston_layer_entry_insert(&from->layer_link,
869 					  &ws->fsurf_back->view->layer_link);
870 		ws->focus_animation = weston_stable_fade_run(
871 			ws->fsurf_front->view, 0.0,
872 			ws->fsurf_back->view, 0.4,
873 			focus_animation_done, ws);
874 	} else if (to) {
875 		weston_layer_entry_insert(&ws->layer.view_list,
876 					  &ws->fsurf_back->view->layer_link);
877 		ws->focus_animation = weston_stable_fade_run(
878 			ws->fsurf_front->view, 0.0,
879 			ws->fsurf_back->view, 0.4,
880 			focus_animation_done, ws);
881 	}
882 }
883 
884 static void
workspace_destroy(struct workspace * ws)885 workspace_destroy(struct workspace *ws)
886 {
887 	struct focus_state *state, *next;
888 
889 	wl_list_for_each_safe(state, next, &ws->focus_list, link)
890 		focus_state_destroy(state);
891 
892 	if (ws->fsurf_front)
893 		focus_surface_destroy(ws->fsurf_front);
894 	if (ws->fsurf_back)
895 		focus_surface_destroy(ws->fsurf_back);
896 
897 	free(ws);
898 }
899 
900 static void
seat_destroyed(struct wl_listener * listener,void * data)901 seat_destroyed(struct wl_listener *listener, void *data)
902 {
903 	struct weston_seat *seat = data;
904 	struct focus_state *state, *next;
905 	struct workspace *ws = container_of(listener,
906 					    struct workspace,
907 					    seat_destroyed_listener);
908 
909 	wl_list_for_each_safe(state, next, &ws->focus_list, link)
910 		if (state->seat == seat)
911 			wl_list_remove(&state->link);
912 }
913 
914 static struct workspace *
workspace_create(struct desktop_shell * shell)915 workspace_create(struct desktop_shell *shell)
916 {
917 	struct workspace *ws = malloc(sizeof *ws);
918 	if (ws == NULL)
919 		return NULL;
920 
921 	weston_layer_init(&ws->layer, shell->compositor);
922 
923 	wl_list_init(&ws->focus_list);
924 	wl_list_init(&ws->seat_destroyed_listener.link);
925 	ws->seat_destroyed_listener.notify = seat_destroyed;
926 	ws->fsurf_front = NULL;
927 	ws->fsurf_back = NULL;
928 	ws->focus_animation = NULL;
929 
930 	return ws;
931 }
932 
933 static int
workspace_is_empty(struct workspace * ws)934 workspace_is_empty(struct workspace *ws)
935 {
936 	return wl_list_empty(&ws->layer.view_list.link);
937 }
938 
939 static struct workspace *
get_workspace(struct desktop_shell * shell,unsigned int index)940 get_workspace(struct desktop_shell *shell, unsigned int index)
941 {
942 	struct workspace **pws = shell->workspaces.array.data;
943 	assert(index < shell->workspaces.num);
944 	pws += index;
945 	return *pws;
946 }
947 
948 struct workspace *
get_current_workspace(struct desktop_shell * shell)949 get_current_workspace(struct desktop_shell *shell)
950 {
951 	return get_workspace(shell, shell->workspaces.current);
952 }
953 
954 static void
activate_workspace(struct desktop_shell * shell,unsigned int index)955 activate_workspace(struct desktop_shell *shell, unsigned int index)
956 {
957 	struct workspace *ws;
958 
959 	ws = get_workspace(shell, index);
960 	weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
961 
962 	shell->workspaces.current = index;
963 }
964 
965 static unsigned int
get_output_height(struct weston_output * output)966 get_output_height(struct weston_output *output)
967 {
968 	return abs(output->region.extents.y1 - output->region.extents.y2);
969 }
970 
971 static struct weston_transform *
view_get_transform(struct weston_view * view)972 view_get_transform(struct weston_view *view)
973 {
974 	struct focus_surface *fsurf = NULL;
975 	struct shell_surface *shsurf = NULL;
976 
977 	if (is_focus_view(view)) {
978 		fsurf = get_focus_surface(view->surface);
979 		return &fsurf->workspace_transform;
980 	}
981 
982 	shsurf = get_shell_surface(view->surface);
983 	if (shsurf)
984 		return &shsurf->workspace_transform;
985 
986 	return NULL;
987 }
988 
989 static void
view_translate(struct workspace * ws,struct weston_view * view,double d)990 view_translate(struct workspace *ws, struct weston_view *view, double d)
991 {
992 	struct weston_transform *transform = view_get_transform(view);
993 
994 	if (!transform)
995 		return;
996 
997 	if (wl_list_empty(&transform->link))
998 		wl_list_insert(view->geometry.transformation_list.prev,
999 			       &transform->link);
1000 
1001 	weston_matrix_init(&transform->matrix);
1002 	weston_matrix_translate(&transform->matrix,
1003 				0.0, d, 0.0);
1004 	weston_view_geometry_dirty(view);
1005 }
1006 
1007 static void
workspace_translate_out(struct workspace * ws,double fraction)1008 workspace_translate_out(struct workspace *ws, double fraction)
1009 {
1010 	struct weston_view *view;
1011 	unsigned int height;
1012 	double d;
1013 
1014 	wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
1015 		height = get_output_height(view->surface->output);
1016 		d = height * fraction;
1017 
1018 		view_translate(ws, view, d);
1019 	}
1020 }
1021 
1022 static void
workspace_translate_in(struct workspace * ws,double fraction)1023 workspace_translate_in(struct workspace *ws, double fraction)
1024 {
1025 	struct weston_view *view;
1026 	unsigned int height;
1027 	double d;
1028 
1029 	wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
1030 		height = get_output_height(view->surface->output);
1031 
1032 		if (fraction > 0)
1033 			d = -(height - height * fraction);
1034 		else
1035 			d = height + height * fraction;
1036 
1037 		view_translate(ws, view, d);
1038 	}
1039 }
1040 
1041 static void
reverse_workspace_change_animation(struct desktop_shell * shell,unsigned int index,struct workspace * from,struct workspace * to)1042 reverse_workspace_change_animation(struct desktop_shell *shell,
1043 				   unsigned int index,
1044 				   struct workspace *from,
1045 				   struct workspace *to)
1046 {
1047 	shell->workspaces.current = index;
1048 
1049 	shell->workspaces.anim_to = to;
1050 	shell->workspaces.anim_from = from;
1051 	shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1052 	shell->workspaces.anim_timestamp = (struct timespec) { 0 };
1053 
1054 	weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1055 	weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
1056 
1057 	weston_compositor_schedule_repaint(shell->compositor);
1058 }
1059 
1060 static void
workspace_deactivate_transforms(struct workspace * ws)1061 workspace_deactivate_transforms(struct workspace *ws)
1062 {
1063 	struct weston_view *view;
1064 	struct weston_transform *transform;
1065 
1066 	wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
1067 		transform = view_get_transform(view);
1068 		if (!transform)
1069 			continue;
1070 
1071 		if (!wl_list_empty(&transform->link)) {
1072 			wl_list_remove(&transform->link);
1073 			wl_list_init(&transform->link);
1074 		}
1075 		weston_view_geometry_dirty(view);
1076 	}
1077 }
1078 
1079 static void
finish_workspace_change_animation(struct desktop_shell * shell,struct workspace * from,struct workspace * to)1080 finish_workspace_change_animation(struct desktop_shell *shell,
1081 				  struct workspace *from,
1082 				  struct workspace *to)
1083 {
1084 	struct weston_view *view;
1085 
1086 	weston_compositor_schedule_repaint(shell->compositor);
1087 
1088 	/* Views that extend past the bottom of the output are still
1089 	 * visible after the workspace animation ends but before its layer
1090 	 * is hidden. In that case, we need to damage below those views so
1091 	 * that the screen is properly repainted. */
1092 	wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
1093 		weston_view_damage_below(view);
1094 
1095 	wl_list_remove(&shell->workspaces.animation.link);
1096 	workspace_deactivate_transforms(from);
1097 	workspace_deactivate_transforms(to);
1098 	shell->workspaces.anim_to = NULL;
1099 
1100 	weston_layer_unset_position(&shell->workspaces.anim_from->layer);
1101 }
1102 
1103 static void
animate_workspace_change_frame(struct weston_animation * animation,struct weston_output * output,const struct timespec * time)1104 animate_workspace_change_frame(struct weston_animation *animation,
1105 			       struct weston_output *output,
1106 			       const struct timespec *time)
1107 {
1108 	struct desktop_shell *shell =
1109 		container_of(animation, struct desktop_shell,
1110 			     workspaces.animation);
1111 	struct workspace *from = shell->workspaces.anim_from;
1112 	struct workspace *to = shell->workspaces.anim_to;
1113 	int64_t t;
1114 	double x, y;
1115 
1116 	if (workspace_is_empty(from) && workspace_is_empty(to)) {
1117 		finish_workspace_change_animation(shell, from, to);
1118 		return;
1119 	}
1120 
1121 	if (timespec_is_zero(&shell->workspaces.anim_timestamp)) {
1122 		if (shell->workspaces.anim_current == 0.0)
1123 			shell->workspaces.anim_timestamp = *time;
1124 		else
1125 			timespec_add_msec(&shell->workspaces.anim_timestamp,
1126 				time,
1127 				/* Inverse of movement function 'y' below. */
1128 				-(asin(1.0 - shell->workspaces.anim_current) *
1129 				  DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1130 				  M_2_PI));
1131 	}
1132 
1133 	t = timespec_sub_to_msec(time, &shell->workspaces.anim_timestamp);
1134 
1135 	/*
1136 	 * x = [0, π/2]
1137 	 * y(x) = sin(x)
1138 	 */
1139 	x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1140 	y = sin(x);
1141 
1142 	if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
1143 		weston_compositor_schedule_repaint(shell->compositor);
1144 
1145 		workspace_translate_out(from, shell->workspaces.anim_dir * y);
1146 		workspace_translate_in(to, shell->workspaces.anim_dir * y);
1147 		shell->workspaces.anim_current = y;
1148 
1149 		weston_compositor_schedule_repaint(shell->compositor);
1150 	}
1151 	else
1152 		finish_workspace_change_animation(shell, from, to);
1153 }
1154 
1155 static void
animate_workspace_change(struct desktop_shell * shell,unsigned int index,struct workspace * from,struct workspace * to)1156 animate_workspace_change(struct desktop_shell *shell,
1157 			 unsigned int index,
1158 			 struct workspace *from,
1159 			 struct workspace *to)
1160 {
1161 	struct weston_output *output;
1162 
1163 	int dir;
1164 
1165 	if (index > shell->workspaces.current)
1166 		dir = -1;
1167 	else
1168 		dir = 1;
1169 
1170 	shell->workspaces.current = index;
1171 
1172 	shell->workspaces.anim_dir = dir;
1173 	shell->workspaces.anim_from = from;
1174 	shell->workspaces.anim_to = to;
1175 	shell->workspaces.anim_current = 0.0;
1176 	shell->workspaces.anim_timestamp = (struct timespec) { 0 };
1177 
1178 	output = container_of(shell->compositor->output_list.next,
1179 			      struct weston_output, link);
1180 	wl_list_insert(&output->animation_list,
1181 		       &shell->workspaces.animation.link);
1182 
1183 	weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1184 	weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
1185 
1186 	workspace_translate_in(to, 0);
1187 
1188 	restore_focus_state(shell, to);
1189 
1190 	weston_compositor_schedule_repaint(shell->compositor);
1191 }
1192 
1193 static void
update_workspace(struct desktop_shell * shell,unsigned int index,struct workspace * from,struct workspace * to)1194 update_workspace(struct desktop_shell *shell, unsigned int index,
1195 		 struct workspace *from, struct workspace *to)
1196 {
1197 	shell->workspaces.current = index;
1198 	weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1199 	weston_layer_unset_position(&from->layer);
1200 }
1201 
1202 static void
change_workspace(struct desktop_shell * shell,unsigned int index)1203 change_workspace(struct desktop_shell *shell, unsigned int index)
1204 {
1205 	struct workspace *from;
1206 	struct workspace *to;
1207 	struct focus_state *state;
1208 
1209 	if (index == shell->workspaces.current)
1210 		return;
1211 
1212 	/* Don't change workspace when there is any fullscreen surfaces. */
1213 	if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
1214 		return;
1215 
1216 	from = get_current_workspace(shell);
1217 	to = get_workspace(shell, index);
1218 
1219 	if (shell->workspaces.anim_from == to &&
1220 	    shell->workspaces.anim_to == from) {
1221 		restore_focus_state(shell, to);
1222 		reverse_workspace_change_animation(shell, index, from, to);
1223 		return;
1224 	}
1225 
1226 	if (shell->workspaces.anim_to != NULL)
1227 		finish_workspace_change_animation(shell,
1228 						  shell->workspaces.anim_from,
1229 						  shell->workspaces.anim_to);
1230 
1231 	restore_focus_state(shell, to);
1232 
1233 	if (shell->focus_animation_type != ANIMATION_NONE) {
1234 		wl_list_for_each(state, &from->focus_list, link)
1235 			if (state->keyboard_focus)
1236 				animate_focus_change(shell, from,
1237 						     get_default_view(state->keyboard_focus), NULL);
1238 
1239 		wl_list_for_each(state, &to->focus_list, link)
1240 			if (state->keyboard_focus)
1241 				animate_focus_change(shell, to,
1242 						     NULL, get_default_view(state->keyboard_focus));
1243 	}
1244 
1245 	if (workspace_is_empty(to) && workspace_is_empty(from))
1246 		update_workspace(shell, index, from, to);
1247 	else
1248 		animate_workspace_change(shell, index, from, to);
1249 }
1250 
1251 static bool
workspace_has_only(struct workspace * ws,struct weston_surface * surface)1252 workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1253 {
1254 	struct wl_list *list = &ws->layer.view_list.link;
1255 	struct wl_list *e;
1256 
1257 	if (wl_list_empty(list))
1258 		return false;
1259 
1260 	e = list->next;
1261 
1262 	if (e->next != list)
1263 		return false;
1264 
1265 	return container_of(e, struct weston_view, layer_link.link)->surface == surface;
1266 }
1267 
1268 static void
surface_keyboard_focus_lost(struct weston_surface * surface)1269 surface_keyboard_focus_lost(struct weston_surface *surface)
1270 {
1271 	struct weston_compositor *compositor = surface->compositor;
1272 	struct weston_seat *seat;
1273 	struct weston_surface *focus;
1274 
1275 	wl_list_for_each(seat, &compositor->seat_list, link) {
1276 		struct weston_keyboard *keyboard =
1277 			weston_seat_get_keyboard(seat);
1278 
1279 		if (!keyboard)
1280 			continue;
1281 
1282 		focus = weston_surface_get_main_surface(keyboard->focus);
1283 		if (focus == surface)
1284 			weston_keyboard_set_focus(keyboard, NULL);
1285 	}
1286 }
1287 
1288 static void
take_surface_to_workspace_by_seat(struct desktop_shell * shell,struct weston_seat * seat,unsigned int index)1289 take_surface_to_workspace_by_seat(struct desktop_shell *shell,
1290 				  struct weston_seat *seat,
1291 				  unsigned int index)
1292 {
1293 	struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
1294 	struct weston_surface *surface;
1295 	struct weston_view *view;
1296 	struct shell_surface *shsurf;
1297 	struct workspace *from;
1298 	struct workspace *to;
1299 	struct focus_state *state;
1300 
1301 	surface = weston_surface_get_main_surface(keyboard->focus);
1302 	view = get_default_view(surface);
1303 	if (view == NULL ||
1304 	    index == shell->workspaces.current ||
1305 	    is_focus_view(view))
1306 		return;
1307 
1308 	from = get_current_workspace(shell);
1309 	to = get_workspace(shell, index);
1310 
1311 	weston_layer_entry_remove(&view->layer_link);
1312 	weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
1313 
1314 	shsurf = get_shell_surface(surface);
1315 	if (shsurf != NULL)
1316 		shell_surface_update_child_surface_layers(shsurf);
1317 
1318 	replace_focus_state(shell, to, seat);
1319 	drop_focus_state(shell, from, surface);
1320 
1321 	if (shell->workspaces.anim_from == to &&
1322 	    shell->workspaces.anim_to == from) {
1323 		reverse_workspace_change_animation(shell, index, from, to);
1324 
1325 		return;
1326 	}
1327 
1328 	if (shell->workspaces.anim_to != NULL)
1329 		finish_workspace_change_animation(shell,
1330 						  shell->workspaces.anim_from,
1331 						  shell->workspaces.anim_to);
1332 
1333 	if (workspace_is_empty(from) &&
1334 	    workspace_has_only(to, surface))
1335 		update_workspace(shell, index, from, to);
1336 	else {
1337 		if (shsurf != NULL &&
1338 		    wl_list_empty(&shsurf->workspace_transform.link))
1339 			wl_list_insert(&shell->workspaces.anim_sticky_list,
1340 				       &shsurf->workspace_transform.link);
1341 
1342 		animate_workspace_change(shell, index, from, to);
1343 	}
1344 
1345 	state = ensure_focus_state(shell, seat);
1346 	if (state != NULL)
1347 		focus_state_set_focus(state, surface);
1348 }
1349 
1350 static void
touch_move_grab_down(struct weston_touch_grab * grab,const struct timespec * time,int touch_id,wl_fixed_t x,wl_fixed_t y)1351 touch_move_grab_down(struct weston_touch_grab *grab,
1352 		     const struct timespec *time,
1353 		     int touch_id, wl_fixed_t x, wl_fixed_t y)
1354 {
1355 }
1356 
1357 static void
touch_move_grab_up(struct weston_touch_grab * grab,const struct timespec * time,int touch_id)1358 touch_move_grab_up(struct weston_touch_grab *grab, const struct timespec *time,
1359 		   int touch_id)
1360 {
1361 	struct weston_touch_move_grab *move =
1362 		(struct weston_touch_move_grab *) container_of(
1363 			grab, struct shell_touch_grab, grab);
1364 
1365 	if (touch_id == 0)
1366 		move->active = 0;
1367 
1368 	if (grab->touch->num_tp == 0) {
1369 		shell_touch_grab_end(&move->base);
1370 		free(move);
1371 	}
1372 }
1373 
1374 static void
touch_move_grab_motion(struct weston_touch_grab * grab,const struct timespec * time,int touch_id,wl_fixed_t x,wl_fixed_t y)1375 touch_move_grab_motion(struct weston_touch_grab *grab,
1376 		       const struct timespec *time, int touch_id,
1377 		       wl_fixed_t x, wl_fixed_t y)
1378 {
1379 	struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1380 	struct shell_surface *shsurf = move->base.shsurf;
1381 	struct weston_surface *es;
1382 	int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1383 	int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1384 
1385 	if (!shsurf || !move->active)
1386 		return;
1387 
1388 	es = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1389 
1390 	weston_view_set_position(shsurf->view, dx, dy);
1391 
1392 	weston_compositor_schedule_repaint(es->compositor);
1393 }
1394 
1395 static void
touch_move_grab_frame(struct weston_touch_grab * grab)1396 touch_move_grab_frame(struct weston_touch_grab *grab)
1397 {
1398 }
1399 
1400 static void
touch_move_grab_cancel(struct weston_touch_grab * grab)1401 touch_move_grab_cancel(struct weston_touch_grab *grab)
1402 {
1403 	struct weston_touch_move_grab *move =
1404 		(struct weston_touch_move_grab *) container_of(
1405 			grab, struct shell_touch_grab, grab);
1406 
1407 	shell_touch_grab_end(&move->base);
1408 	free(move);
1409 }
1410 
1411 static const struct weston_touch_grab_interface touch_move_grab_interface = {
1412 	touch_move_grab_down,
1413 	touch_move_grab_up,
1414 	touch_move_grab_motion,
1415 	touch_move_grab_frame,
1416 	touch_move_grab_cancel,
1417 };
1418 
1419 static int
surface_touch_move(struct shell_surface * shsurf,struct weston_touch * touch)1420 surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
1421 {
1422 	struct weston_touch_move_grab *move;
1423 
1424 	if (!shsurf)
1425 		return -1;
1426 
1427 	if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1428 	    weston_desktop_surface_get_maximized(shsurf->desktop_surface))
1429 		return 0;
1430 
1431 	move = malloc(sizeof *move);
1432 	if (!move)
1433 		return -1;
1434 
1435 	move->active = 1;
1436 	move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
1437 		   touch->grab_x;
1438 	move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
1439 		   touch->grab_y;
1440 
1441 	shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1442 			       touch);
1443 
1444 	return 0;
1445 }
1446 
1447 static void
noop_grab_focus(struct weston_pointer_grab * grab)1448 noop_grab_focus(struct weston_pointer_grab *grab)
1449 {
1450 }
1451 
1452 static void
noop_grab_axis(struct weston_pointer_grab * grab,const struct timespec * time,struct weston_pointer_axis_event * event)1453 noop_grab_axis(struct weston_pointer_grab *grab,
1454 	       const struct timespec *time,
1455 	       struct weston_pointer_axis_event *event)
1456 {
1457 }
1458 
1459 static void
noop_grab_axis_source(struct weston_pointer_grab * grab,uint32_t source)1460 noop_grab_axis_source(struct weston_pointer_grab *grab,
1461 		      uint32_t source)
1462 {
1463 }
1464 
1465 static void
noop_grab_frame(struct weston_pointer_grab * grab)1466 noop_grab_frame(struct weston_pointer_grab *grab)
1467 {
1468 }
1469 
1470 static void
constrain_position(struct weston_move_grab * move,int * cx,int * cy)1471 constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1472 {
1473 	struct shell_surface *shsurf = move->base.shsurf;
1474 	struct weston_surface *surface =
1475 		weston_desktop_surface_get_surface(shsurf->desktop_surface);
1476 	struct weston_pointer *pointer = move->base.grab.pointer;
1477 	int x, y, bottom;
1478 	const int safety = 50;
1479 	pixman_rectangle32_t area;
1480 	struct weston_geometry geometry;
1481 
1482 	x = wl_fixed_to_int(pointer->x + move->dx);
1483 	y = wl_fixed_to_int(pointer->y + move->dy);
1484 
1485 	if (shsurf->shell->panel_position ==
1486 	    WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
1487 		get_output_work_area(shsurf->shell, surface->output, &area);
1488 		geometry =
1489 			weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1490 
1491 		bottom = y + geometry.height + geometry.y;
1492 		if (bottom - safety < area.y)
1493 			y = area.y + safety - geometry.height
1494 			  - geometry.y;
1495 
1496 		if (move->client_initiated &&
1497 		    y + geometry.y < area.y)
1498 			y = area.y - geometry.y;
1499 	}
1500 
1501 	*cx = x;
1502 	*cy = y;
1503 }
1504 
1505 static void
move_grab_motion(struct weston_pointer_grab * grab,const struct timespec * time,struct weston_pointer_motion_event * event)1506 move_grab_motion(struct weston_pointer_grab *grab,
1507 		 const struct timespec *time,
1508 		 struct weston_pointer_motion_event *event)
1509 {
1510 	struct weston_move_grab *move = (struct weston_move_grab *) grab;
1511 	struct weston_pointer *pointer = grab->pointer;
1512 	struct shell_surface *shsurf = move->base.shsurf;
1513 	struct weston_surface *surface;
1514 	int cx, cy;
1515 
1516 	weston_pointer_move(pointer, event);
1517 	if (!shsurf)
1518 		return;
1519 
1520 	surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1521 
1522 	constrain_position(move, &cx, &cy);
1523 
1524 	weston_view_set_position(shsurf->view, cx, cy);
1525 
1526 	weston_compositor_schedule_repaint(surface->compositor);
1527 }
1528 
1529 static void
move_grab_button(struct weston_pointer_grab * grab,const struct timespec * time,uint32_t button,uint32_t state_w)1530 move_grab_button(struct weston_pointer_grab *grab,
1531 		 const struct timespec *time, uint32_t button, uint32_t state_w)
1532 {
1533 	struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1534 						    grab);
1535 	struct weston_pointer *pointer = grab->pointer;
1536 	enum wl_pointer_button_state state = state_w;
1537 
1538 	if (pointer->button_count == 0 &&
1539 	    state == WL_POINTER_BUTTON_STATE_RELEASED) {
1540 		shell_grab_end(shell_grab);
1541 		free(grab);
1542 	}
1543 }
1544 
1545 static void
move_grab_cancel(struct weston_pointer_grab * grab)1546 move_grab_cancel(struct weston_pointer_grab *grab)
1547 {
1548 	struct shell_grab *shell_grab =
1549 		container_of(grab, struct shell_grab, grab);
1550 
1551 	shell_grab_end(shell_grab);
1552 	free(grab);
1553 }
1554 
1555 static const struct weston_pointer_grab_interface move_grab_interface = {
1556 	noop_grab_focus,
1557 	move_grab_motion,
1558 	move_grab_button,
1559 	noop_grab_axis,
1560 	noop_grab_axis_source,
1561 	noop_grab_frame,
1562 	move_grab_cancel,
1563 };
1564 
1565 static int
surface_move(struct shell_surface * shsurf,struct weston_pointer * pointer,bool client_initiated)1566 surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
1567 	     bool client_initiated)
1568 {
1569 	struct weston_move_grab *move;
1570 
1571 	if (!shsurf)
1572 		return -1;
1573 
1574 	if (shsurf->grabbed ||
1575 	    weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1576 	    weston_desktop_surface_get_maximized(shsurf->desktop_surface))
1577 		return 0;
1578 
1579 	move = malloc(sizeof *move);
1580 	if (!move)
1581 		return -1;
1582 
1583 	move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
1584 		   pointer->grab_x;
1585 	move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
1586 		   pointer->grab_y;
1587 	move->client_initiated = client_initiated;
1588 
1589 	shell_grab_start(&move->base, &move_grab_interface, shsurf,
1590 			 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
1591 
1592 	return 0;
1593 }
1594 
1595 struct weston_resize_grab {
1596 	struct shell_grab base;
1597 	uint32_t edges;
1598 	int32_t width, height;
1599 };
1600 
1601 static void
resize_grab_motion(struct weston_pointer_grab * grab,const struct timespec * time,struct weston_pointer_motion_event * event)1602 resize_grab_motion(struct weston_pointer_grab *grab,
1603 		   const struct timespec *time,
1604 		   struct weston_pointer_motion_event *event)
1605 {
1606 	struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1607 	struct weston_pointer *pointer = grab->pointer;
1608 	struct shell_surface *shsurf = resize->base.shsurf;
1609 	int32_t width, height;
1610 	struct weston_size min_size, max_size;
1611 	wl_fixed_t from_x, from_y;
1612 	wl_fixed_t to_x, to_y;
1613 
1614 	weston_pointer_move(pointer, event);
1615 
1616 	if (!shsurf)
1617 		return;
1618 
1619 	weston_view_from_global_fixed(shsurf->view,
1620 				      pointer->grab_x, pointer->grab_y,
1621 				      &from_x, &from_y);
1622 	weston_view_from_global_fixed(shsurf->view,
1623 				      pointer->x, pointer->y, &to_x, &to_y);
1624 
1625 	width = resize->width;
1626 	if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1627 		width += wl_fixed_to_int(from_x - to_x);
1628 	} else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1629 		width += wl_fixed_to_int(to_x - from_x);
1630 	}
1631 
1632 	height = resize->height;
1633 	if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1634 		height += wl_fixed_to_int(from_y - to_y);
1635 	} else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1636 		height += wl_fixed_to_int(to_y - from_y);
1637 	}
1638 
1639 	max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
1640 	min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
1641 
1642 	min_size.width = MAX(1, min_size.width);
1643 	min_size.height = MAX(1, min_size.height);
1644 
1645 	if (width < min_size.width)
1646 		width = min_size.width;
1647 	else if (max_size.width > 0 && width > max_size.width)
1648 		width = max_size.width;
1649 	if (height < min_size.height)
1650 		height = min_size.height;
1651 	else if (max_size.width > 0 && width > max_size.width)
1652 		width = max_size.width;
1653 	weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
1654 }
1655 
1656 static void
resize_grab_button(struct weston_pointer_grab * grab,const struct timespec * time,uint32_t button,uint32_t state_w)1657 resize_grab_button(struct weston_pointer_grab *grab,
1658 		   const struct timespec *time,
1659 		   uint32_t button, uint32_t state_w)
1660 {
1661 	struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1662 	struct weston_pointer *pointer = grab->pointer;
1663 	enum wl_pointer_button_state state = state_w;
1664 
1665 	if (pointer->button_count == 0 &&
1666 	    state == WL_POINTER_BUTTON_STATE_RELEASED) {
1667 		if (resize->base.shsurf != NULL) {
1668 			struct weston_desktop_surface *desktop_surface =
1669 				resize->base.shsurf->desktop_surface;
1670 			weston_desktop_surface_set_resizing(desktop_surface,
1671 							    false);
1672 		}
1673 
1674 		shell_grab_end(&resize->base);
1675 		free(grab);
1676 	}
1677 }
1678 
1679 static void
resize_grab_cancel(struct weston_pointer_grab * grab)1680 resize_grab_cancel(struct weston_pointer_grab *grab)
1681 {
1682 	struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1683 
1684 	if (resize->base.shsurf != NULL) {
1685 		struct weston_desktop_surface *desktop_surface =
1686 			resize->base.shsurf->desktop_surface;
1687 		weston_desktop_surface_set_resizing(desktop_surface, false);
1688 	}
1689 
1690 	shell_grab_end(&resize->base);
1691 	free(grab);
1692 }
1693 
1694 static const struct weston_pointer_grab_interface resize_grab_interface = {
1695 	noop_grab_focus,
1696 	resize_grab_motion,
1697 	resize_grab_button,
1698 	noop_grab_axis,
1699 	noop_grab_axis_source,
1700 	noop_grab_frame,
1701 	resize_grab_cancel,
1702 };
1703 
1704 /*
1705  * Returns the bounding box of a surface and all its sub-surfaces,
1706  * in surface-local coordinates. */
1707 static void
surface_subsurfaces_boundingbox(struct weston_surface * surface,int32_t * x,int32_t * y,int32_t * w,int32_t * h)1708 surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1709 				int32_t *y, int32_t *w, int32_t *h) {
1710 	pixman_region32_t region;
1711 	pixman_box32_t *box;
1712 	struct weston_subsurface *subsurface;
1713 
1714 	pixman_region32_init_rect(&region, 0, 0,
1715 	                          surface->width,
1716 	                          surface->height);
1717 
1718 	wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1719 		pixman_region32_union_rect(&region, &region,
1720 		                           subsurface->position.x,
1721 		                           subsurface->position.y,
1722 		                           subsurface->surface->width,
1723 		                           subsurface->surface->height);
1724 	}
1725 
1726 	box = pixman_region32_extents(&region);
1727 	if (x)
1728 		*x = box->x1;
1729 	if (y)
1730 		*y = box->y1;
1731 	if (w)
1732 		*w = box->x2 - box->x1;
1733 	if (h)
1734 		*h = box->y2 - box->y1;
1735 
1736 	pixman_region32_fini(&region);
1737 }
1738 
1739 static int
surface_resize(struct shell_surface * shsurf,struct weston_pointer * pointer,uint32_t edges)1740 surface_resize(struct shell_surface *shsurf,
1741 	       struct weston_pointer *pointer, uint32_t edges)
1742 {
1743 	struct weston_resize_grab *resize;
1744 	const unsigned resize_topbottom =
1745 		WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1746 	const unsigned resize_leftright =
1747 		WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1748 	const unsigned resize_any = resize_topbottom | resize_leftright;
1749 	struct weston_geometry geometry;
1750 
1751 	if (shsurf->grabbed ||
1752 	    weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1753 	    weston_desktop_surface_get_maximized(shsurf->desktop_surface))
1754 		return 0;
1755 
1756 	/* Check for invalid edge combinations. */
1757 	if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1758 	    (edges & resize_topbottom) == resize_topbottom ||
1759 	    (edges & resize_leftright) == resize_leftright)
1760 		return 0;
1761 
1762 	resize = malloc(sizeof *resize);
1763 	if (!resize)
1764 		return -1;
1765 
1766 	resize->edges = edges;
1767 
1768 	geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1769 	resize->width = geometry.width;
1770 	resize->height = geometry.height;
1771 
1772 	shsurf->resize_edges = edges;
1773 	weston_desktop_surface_set_resizing(shsurf->desktop_surface, true);
1774 	shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1775 			 pointer, edges);
1776 
1777 	return 0;
1778 }
1779 
1780 static void
busy_cursor_grab_focus(struct weston_pointer_grab * base)1781 busy_cursor_grab_focus(struct weston_pointer_grab *base)
1782 {
1783 	struct shell_grab *grab = (struct shell_grab *) base;
1784 	struct weston_pointer *pointer = base->pointer;
1785 	struct weston_desktop_surface *desktop_surface;
1786 	struct weston_view *view;
1787 	wl_fixed_t sx, sy;
1788 
1789 	view = weston_compositor_pick_view(pointer->seat->compositor,
1790 					   pointer->x, pointer->y,
1791 					   &sx, &sy);
1792 	desktop_surface = weston_surface_get_desktop_surface(view->surface);
1793 
1794 	if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
1795 		shell_grab_end(grab);
1796 		free(grab);
1797 	}
1798 }
1799 
1800 static void
busy_cursor_grab_motion(struct weston_pointer_grab * grab,const struct timespec * time,struct weston_pointer_motion_event * event)1801 busy_cursor_grab_motion(struct weston_pointer_grab *grab,
1802 			const struct timespec *time,
1803 			struct weston_pointer_motion_event *event)
1804 {
1805 	weston_pointer_move(grab->pointer, event);
1806 }
1807 
1808 static void
busy_cursor_grab_button(struct weston_pointer_grab * base,const struct timespec * time,uint32_t button,uint32_t state)1809 busy_cursor_grab_button(struct weston_pointer_grab *base,
1810 			const struct timespec *time,
1811 			uint32_t button, uint32_t state)
1812 {
1813 	struct shell_grab *grab = (struct shell_grab *) base;
1814 	struct shell_surface *shsurf = grab->shsurf;
1815 	struct weston_pointer *pointer = grab->grab.pointer;
1816 	struct weston_seat *seat = pointer->seat;
1817 
1818 	if (shsurf && button == BTN_LEFT && state) {
1819 		activate(shsurf->shell, shsurf->view, seat,
1820 			 WESTON_ACTIVATE_FLAG_CONFIGURE);
1821 		surface_move(shsurf, pointer, false);
1822 	} else if (shsurf && button == BTN_RIGHT && state) {
1823 		activate(shsurf->shell, shsurf->view, seat,
1824 			 WESTON_ACTIVATE_FLAG_CONFIGURE);
1825 		surface_rotate(shsurf, pointer);
1826 	}
1827 }
1828 
1829 static void
busy_cursor_grab_cancel(struct weston_pointer_grab * base)1830 busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1831 {
1832 	struct shell_grab *grab = (struct shell_grab *) base;
1833 
1834 	shell_grab_end(grab);
1835 	free(grab);
1836 }
1837 
1838 static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
1839 	busy_cursor_grab_focus,
1840 	busy_cursor_grab_motion,
1841 	busy_cursor_grab_button,
1842 	noop_grab_axis,
1843 	noop_grab_axis_source,
1844 	noop_grab_frame,
1845 	busy_cursor_grab_cancel,
1846 };
1847 
1848 static void
handle_pointer_focus(struct wl_listener * listener,void * data)1849 handle_pointer_focus(struct wl_listener *listener, void *data)
1850 {
1851 	struct weston_pointer *pointer = data;
1852 	struct weston_view *view = pointer->focus;
1853 	struct shell_surface *shsurf;
1854 	struct weston_desktop_client *client;
1855 
1856 	if (!view)
1857 		return;
1858 
1859 	shsurf = get_shell_surface(view->surface);
1860 	if (!shsurf)
1861 		return;
1862 
1863 	client = weston_desktop_surface_get_client(shsurf->desktop_surface);
1864 
1865 	if (shsurf->unresponsive)
1866 		set_busy_cursor(shsurf, pointer);
1867 	else
1868 		weston_desktop_client_ping(client);
1869 }
1870 
1871 static void
shell_surface_lose_keyboard_focus(struct shell_surface * shsurf)1872 shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1873 {
1874 	if (--shsurf->focus_count == 0)
1875 		weston_desktop_surface_set_activated(shsurf->desktop_surface, false);
1876 }
1877 
1878 static void
shell_surface_gain_keyboard_focus(struct shell_surface * shsurf)1879 shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1880 {
1881 	if (shsurf->focus_count++ == 0)
1882 		weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
1883 }
1884 
1885 static void
handle_keyboard_focus(struct wl_listener * listener,void * data)1886 handle_keyboard_focus(struct wl_listener *listener, void *data)
1887 {
1888 	struct weston_keyboard *keyboard = data;
1889 	struct shell_seat *seat = get_shell_seat(keyboard->seat);
1890 
1891 	if (seat->focused_surface) {
1892 		struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1893 		if (shsurf)
1894 			shell_surface_lose_keyboard_focus(shsurf);
1895 	}
1896 
1897 	seat->focused_surface = weston_surface_get_main_surface(keyboard->focus);
1898 
1899 	if (seat->focused_surface) {
1900 		struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1901 		if (shsurf)
1902 			shell_surface_gain_keyboard_focus(shsurf);
1903 	}
1904 }
1905 
1906 /* The surface will be inserted into the list immediately after the link
1907  * returned by this function (i.e. will be stacked immediately above the
1908  * returned link). */
1909 static struct weston_layer_entry *
shell_surface_calculate_layer_link(struct shell_surface * shsurf)1910 shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1911 {
1912 	struct workspace *ws;
1913 
1914 	if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
1915 	    !shsurf->state.lowered) {
1916 		return &shsurf->shell->fullscreen_layer.view_list;
1917 	}
1918 
1919 	/* Move the surface to a normal workspace layer so that surfaces
1920 	 * which were previously fullscreen or transient are no longer
1921 	 * rendered on top. */
1922 	ws = get_current_workspace(shsurf->shell);
1923 	return &ws->layer.view_list;
1924 }
1925 
1926 static void
shell_surface_update_child_surface_layers(struct shell_surface * shsurf)1927 shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
1928 {
1929 	weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
1930 }
1931 
1932 /* Update the surface’s layer. Mark both the old and new views as having dirty
1933  * geometry to ensure the changes are redrawn.
1934  *
1935  * If any child surfaces exist and are mapped, ensure they’re in the same layer
1936  * as this surface. */
1937 static void
shell_surface_update_layer(struct shell_surface * shsurf)1938 shell_surface_update_layer(struct shell_surface *shsurf)
1939 {
1940 	struct weston_surface *surface =
1941 		weston_desktop_surface_get_surface(shsurf->desktop_surface);
1942 	struct weston_layer_entry *new_layer_link;
1943 
1944 	new_layer_link = shell_surface_calculate_layer_link(shsurf);
1945 
1946 	if (new_layer_link == NULL)
1947 		return;
1948 	if (new_layer_link == &shsurf->view->layer_link)
1949 		return;
1950 
1951 	weston_view_geometry_dirty(shsurf->view);
1952 	weston_layer_entry_remove(&shsurf->view->layer_link);
1953 	weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
1954 	weston_view_geometry_dirty(shsurf->view);
1955 	weston_surface_damage(surface);
1956 
1957 	shell_surface_update_child_surface_layers(shsurf);
1958 }
1959 
1960 static void
notify_output_destroy(struct wl_listener * listener,void * data)1961 notify_output_destroy(struct wl_listener *listener, void *data)
1962 {
1963 	struct shell_surface *shsurf =
1964 		container_of(listener,
1965 			     struct shell_surface, output_destroy_listener);
1966 
1967 	shsurf->output = NULL;
1968 	shsurf->output_destroy_listener.notify = NULL;
1969 }
1970 
1971 static void
shell_surface_set_output(struct shell_surface * shsurf,struct weston_output * output)1972 shell_surface_set_output(struct shell_surface *shsurf,
1973                          struct weston_output *output)
1974 {
1975 	struct weston_surface *es =
1976 		weston_desktop_surface_get_surface(shsurf->desktop_surface);
1977 
1978 	/* get the default output, if the client set it as NULL
1979 	   check whether the output is available */
1980 	if (output)
1981 		shsurf->output = output;
1982 	else if (es->output)
1983 		shsurf->output = es->output;
1984 	else
1985 		shsurf->output = get_default_output(es->compositor);
1986 
1987 	if (shsurf->output_destroy_listener.notify) {
1988 		wl_list_remove(&shsurf->output_destroy_listener.link);
1989 		shsurf->output_destroy_listener.notify = NULL;
1990 	}
1991 
1992 	if (!shsurf->output)
1993 		return;
1994 
1995 	shsurf->output_destroy_listener.notify = notify_output_destroy;
1996 	wl_signal_add(&shsurf->output->destroy_signal,
1997 		      &shsurf->output_destroy_listener);
1998 }
1999 
2000 static void
2001 weston_view_set_initial_position(struct weston_view *view,
2002 				 struct desktop_shell *shell);
2003 
2004 static void
unset_fullscreen(struct shell_surface * shsurf)2005 unset_fullscreen(struct shell_surface *shsurf)
2006 {
2007 	/* Unset the fullscreen output, driver configuration and transforms. */
2008 	wl_list_remove(&shsurf->fullscreen.transform.link);
2009 	wl_list_init(&shsurf->fullscreen.transform.link);
2010 
2011 	if (shsurf->fullscreen.black_view)
2012 		weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2013 	shsurf->fullscreen.black_view = NULL;
2014 
2015 	if (shsurf->saved_position_valid)
2016 		weston_view_set_position(shsurf->view,
2017 					 shsurf->saved_x, shsurf->saved_y);
2018 	else
2019 		weston_view_set_initial_position(shsurf->view, shsurf->shell);
2020 	shsurf->saved_position_valid = false;
2021 
2022 	if (shsurf->saved_rotation_valid) {
2023 		wl_list_insert(&shsurf->view->geometry.transformation_list,
2024 		               &shsurf->rotation.transform.link);
2025 		shsurf->saved_rotation_valid = false;
2026 	}
2027 }
2028 
2029 static void
unset_maximized(struct shell_surface * shsurf)2030 unset_maximized(struct shell_surface *shsurf)
2031 {
2032 	struct weston_surface *surface =
2033 		weston_desktop_surface_get_surface(shsurf->desktop_surface);
2034 
2035 	/* undo all maximized things here */
2036 	shell_surface_set_output(shsurf, get_default_output(surface->compositor));
2037 
2038 	if (shsurf->saved_position_valid)
2039 		weston_view_set_position(shsurf->view,
2040 					 shsurf->saved_x, shsurf->saved_y);
2041 	else
2042 		weston_view_set_initial_position(shsurf->view, shsurf->shell);
2043 	shsurf->saved_position_valid = false;
2044 
2045 	if (shsurf->saved_rotation_valid) {
2046 		wl_list_insert(&shsurf->view->geometry.transformation_list,
2047 			       &shsurf->rotation.transform.link);
2048 		shsurf->saved_rotation_valid = false;
2049 	}
2050 }
2051 
2052 static void
set_minimized(struct weston_surface * surface)2053 set_minimized(struct weston_surface *surface)
2054 {
2055 	struct shell_surface *shsurf;
2056 	struct workspace *current_ws;
2057 	struct weston_view *view;
2058 
2059 	view = get_default_view(surface);
2060 	if (!view)
2061 		return;
2062 
2063 	assert(weston_surface_get_main_surface(view->surface) == view->surface);
2064 
2065 	shsurf = get_shell_surface(surface);
2066 	current_ws = get_current_workspace(shsurf->shell);
2067 
2068 	weston_layer_entry_remove(&view->layer_link);
2069 	weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
2070 
2071 	drop_focus_state(shsurf->shell, current_ws, view->surface);
2072 	surface_keyboard_focus_lost(surface);
2073 
2074 	shell_surface_update_child_surface_layers(shsurf);
2075 	weston_view_damage_below(view);
2076 }
2077 
2078 
2079 static struct desktop_shell *
shell_surface_get_shell(struct shell_surface * shsurf)2080 shell_surface_get_shell(struct shell_surface *shsurf)
2081 {
2082 	return shsurf->shell;
2083 }
2084 
2085 static int
black_surface_get_label(struct weston_surface * surface,char * buf,size_t len)2086 black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2087 {
2088 	struct weston_view *fs_view = surface->committed_private;
2089 	struct weston_surface *fs_surface = fs_view->surface;
2090 	int n;
2091 	int rem;
2092 	int ret;
2093 
2094 	n = snprintf(buf, len, "black background surface for ");
2095 	if (n < 0)
2096 		return n;
2097 
2098 	rem = (int)len - n;
2099 	if (rem < 0)
2100 		rem = 0;
2101 
2102 	if (fs_surface->get_label)
2103 		ret = fs_surface->get_label(fs_surface, buf + n, rem);
2104 	else
2105 		ret = snprintf(buf + n, rem, "<unknown>");
2106 
2107 	if (ret < 0)
2108 		return n;
2109 
2110 	return n + ret;
2111 }
2112 
2113 static void
2114 black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
2115 
2116 static struct weston_view *
create_black_surface(struct weston_compositor * ec,struct weston_view * fs_view,float x,float y,int w,int h)2117 create_black_surface(struct weston_compositor *ec,
2118 		     struct weston_view *fs_view,
2119 		     float x, float y, int w, int h)
2120 {
2121 	struct weston_surface *surface = NULL;
2122 	struct weston_view *view;
2123 
2124 	surface = weston_surface_create(ec);
2125 	if (surface == NULL) {
2126 		weston_log("no memory\n");
2127 		return NULL;
2128 	}
2129 	view = weston_view_create(surface);
2130 	if (surface == NULL) {
2131 		weston_log("no memory\n");
2132 		weston_surface_destroy(surface);
2133 		return NULL;
2134 	}
2135 
2136 	surface->committed = black_surface_committed;
2137 	surface->committed_private = fs_view;
2138 	weston_surface_set_label_func(surface, black_surface_get_label);
2139 	weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
2140 	pixman_region32_fini(&surface->opaque);
2141 	pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
2142 	pixman_region32_fini(&surface->input);
2143 	pixman_region32_init_rect(&surface->input, 0, 0, w, h);
2144 
2145 	weston_surface_set_size(surface, w, h);
2146 	weston_view_set_position(view, x, y);
2147 
2148 	return view;
2149 }
2150 
2151 static void
shell_ensure_fullscreen_black_view(struct shell_surface * shsurf)2152 shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2153 {
2154 	struct weston_surface *surface =
2155 		weston_desktop_surface_get_surface(shsurf->desktop_surface);
2156 	struct weston_output *output = shsurf->fullscreen_output;
2157 
2158 	assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
2159 
2160 	if (!shsurf->fullscreen.black_view)
2161 		shsurf->fullscreen.black_view =
2162 			create_black_surface(surface->compositor,
2163 			                     shsurf->view,
2164 			                     output->x, output->y,
2165 			                     output->width,
2166 			                     output->height);
2167 
2168 	weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2169 	weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2170 	weston_layer_entry_insert(&shsurf->view->layer_link,
2171 				  &shsurf->fullscreen.black_view->layer_link);
2172 	weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2173 	weston_surface_damage(surface);
2174 
2175 	shsurf->fullscreen.black_view->is_mapped = true;
2176 	shsurf->state.lowered = false;
2177 }
2178 
2179 /* Create black surface and append it to the associated fullscreen surface.
2180  * Handle size dismatch and positioning according to the method. */
2181 static void
shell_configure_fullscreen(struct shell_surface * shsurf)2182 shell_configure_fullscreen(struct shell_surface *shsurf)
2183 {
2184 	struct weston_surface *surface =
2185 		weston_desktop_surface_get_surface(shsurf->desktop_surface);
2186 	int32_t surf_x, surf_y, surf_width, surf_height;
2187 
2188 	/* Reverse the effect of lower_fullscreen_layer() */
2189 	weston_layer_entry_remove(&shsurf->view->layer_link);
2190 	weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2191 				  &shsurf->view->layer_link);
2192 
2193 	if (!shsurf->fullscreen_output) {
2194 		/* If there is no output, there's not much we can do.
2195 		 * Position the window somewhere, whatever. */
2196 		weston_view_set_position(shsurf->view, 0, 0);
2197 		return;
2198 	}
2199 
2200 	shell_ensure_fullscreen_black_view(shsurf);
2201 
2202 	surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
2203 	                                &surf_width, &surf_height);
2204 
2205 	if (surface->buffer_ref.buffer)
2206 		center_on_output(shsurf->view, shsurf->fullscreen_output);
2207 }
2208 
2209 static void
shell_map_fullscreen(struct shell_surface * shsurf)2210 shell_map_fullscreen(struct shell_surface *shsurf)
2211 {
2212 	shell_configure_fullscreen(shsurf);
2213 }
2214 
2215 static struct weston_output *
get_focused_output(struct weston_compositor * compositor)2216 get_focused_output(struct weston_compositor *compositor)
2217 {
2218 	struct weston_seat *seat;
2219 	struct weston_output *output = NULL;
2220 
2221 	wl_list_for_each(seat, &compositor->seat_list, link) {
2222 		struct weston_touch *touch = weston_seat_get_touch(seat);
2223 		struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2224 		struct weston_keyboard *keyboard =
2225 			weston_seat_get_keyboard(seat);
2226 
2227 		/* Priority has touch focus, then pointer and
2228 		 * then keyboard focus. We should probably have
2229 		 * three for loops and check first for touch,
2230 		 * then for pointer, etc. but unless somebody has some
2231 		 * objections, I think this is sufficient. */
2232 		if (touch && touch->focus)
2233 			output = touch->focus->output;
2234 		else if (pointer && pointer->focus)
2235 			output = pointer->focus->output;
2236 		else if (keyboard && keyboard->focus)
2237 			output = keyboard->focus->output;
2238 
2239 		if (output)
2240 			break;
2241 	}
2242 
2243 	return output;
2244 }
2245 
2246 static void
destroy_shell_seat(struct wl_listener * listener,void * data)2247 destroy_shell_seat(struct wl_listener *listener, void *data)
2248 {
2249 	struct shell_seat *shseat =
2250 		container_of(listener,
2251 			     struct shell_seat, seat_destroy_listener);
2252 
2253 	wl_list_remove(&shseat->seat_destroy_listener.link);
2254 	free(shseat);
2255 }
2256 
2257 static void
shell_seat_caps_changed(struct wl_listener * listener,void * data)2258 shell_seat_caps_changed(struct wl_listener *listener, void *data)
2259 {
2260 	struct weston_keyboard *keyboard;
2261 	struct weston_pointer *pointer;
2262 	struct shell_seat *seat;
2263 
2264 	seat = container_of(listener, struct shell_seat, caps_changed_listener);
2265 	keyboard = weston_seat_get_keyboard(seat->seat);
2266 	pointer = weston_seat_get_pointer(seat->seat);
2267 
2268 	if (keyboard &&
2269 	    wl_list_empty(&seat->keyboard_focus_listener.link)) {
2270 		wl_signal_add(&keyboard->focus_signal,
2271 			      &seat->keyboard_focus_listener);
2272 	} else if (!keyboard) {
2273 		wl_list_remove(&seat->keyboard_focus_listener.link);
2274 		wl_list_init(&seat->keyboard_focus_listener.link);
2275 	}
2276 
2277 	if (pointer &&
2278 	    wl_list_empty(&seat->pointer_focus_listener.link)) {
2279 		wl_signal_add(&pointer->focus_signal,
2280 			      &seat->pointer_focus_listener);
2281 	} else if (!pointer) {
2282 		wl_list_remove(&seat->pointer_focus_listener.link);
2283 		wl_list_init(&seat->pointer_focus_listener.link);
2284 	}
2285 }
2286 
2287 static struct shell_seat *
create_shell_seat(struct weston_seat * seat)2288 create_shell_seat(struct weston_seat *seat)
2289 {
2290 	struct shell_seat *shseat;
2291 
2292 	shseat = calloc(1, sizeof *shseat);
2293 	if (!shseat) {
2294 		weston_log("no memory to allocate shell seat\n");
2295 		return NULL;
2296 	}
2297 
2298 	shseat->seat = seat;
2299 
2300 	shseat->seat_destroy_listener.notify = destroy_shell_seat;
2301 	wl_signal_add(&seat->destroy_signal,
2302 	              &shseat->seat_destroy_listener);
2303 
2304 	shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2305 	wl_list_init(&shseat->keyboard_focus_listener.link);
2306 
2307 	shseat->pointer_focus_listener.notify = handle_pointer_focus;
2308 	wl_list_init(&shseat->pointer_focus_listener.link);
2309 
2310 	shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2311 	wl_signal_add(&seat->updated_caps_signal,
2312 		      &shseat->caps_changed_listener);
2313 	shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2314 
2315 	return shseat;
2316 }
2317 
2318 static struct shell_seat *
get_shell_seat(struct weston_seat * seat)2319 get_shell_seat(struct weston_seat *seat)
2320 {
2321 	struct wl_listener *listener;
2322 
2323 	listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2324 	assert(listener != NULL);
2325 
2326 	return container_of(listener,
2327 			    struct shell_seat, seat_destroy_listener);
2328 }
2329 
2330 static void
fade_out_done_idle_cb(void * data)2331 fade_out_done_idle_cb(void *data)
2332 {
2333 	struct shell_surface *shsurf = data;
2334 
2335 	weston_surface_destroy(shsurf->view->surface);
2336 
2337 	if (shsurf->output_destroy_listener.notify) {
2338 		wl_list_remove(&shsurf->output_destroy_listener.link);
2339 		shsurf->output_destroy_listener.notify = NULL;
2340 	}
2341 
2342 	free(shsurf);
2343 }
2344 
2345 static void
fade_out_done(struct weston_view_animation * animation,void * data)2346 fade_out_done(struct weston_view_animation *animation, void *data)
2347 {
2348 	struct shell_surface *shsurf = data;
2349 	struct wl_event_loop *loop;
2350 
2351 	loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
2352 
2353 	if (weston_view_is_mapped(shsurf->view)) {
2354 		weston_view_unmap(shsurf->view);
2355 		wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
2356 	}
2357 }
2358 
2359 struct shell_surface *
get_shell_surface(struct weston_surface * surface)2360 get_shell_surface(struct weston_surface *surface)
2361 {
2362 	if (weston_surface_is_desktop_surface(surface)) {
2363 		struct weston_desktop_surface *desktop_surface =
2364 			weston_surface_get_desktop_surface(surface);
2365 		return weston_desktop_surface_get_user_data(desktop_surface);
2366 	}
2367 	return NULL;
2368 }
2369 
2370 /*
2371  * libweston-desktop
2372  */
2373 
2374 static void
desktop_surface_added(struct weston_desktop_surface * desktop_surface,void * shell)2375 desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2376 		      void *shell)
2377 {
2378 	struct weston_desktop_client *client =
2379 		weston_desktop_surface_get_client(desktop_surface);
2380 	struct wl_client *wl_client =
2381 		weston_desktop_client_get_client(client);
2382 	struct weston_view *view;
2383 	struct shell_surface *shsurf;
2384 	struct weston_surface *surface =
2385 		weston_desktop_surface_get_surface(desktop_surface);
2386 
2387 	view = weston_desktop_surface_create_view(desktop_surface);
2388 	if (!view)
2389 		return;
2390 
2391 	shsurf = calloc(1, sizeof *shsurf);
2392 	if (!shsurf) {
2393 		if (wl_client)
2394 			wl_client_post_no_memory(wl_client);
2395 		else
2396 			weston_log("no memory to allocate shell surface\n");
2397 		return;
2398 	}
2399 
2400 	weston_surface_set_label_func(surface, shell_surface_get_label);
2401 
2402 	shsurf->shell = (struct desktop_shell *) shell;
2403 	shsurf->unresponsive = 0;
2404 	shsurf->saved_position_valid = false;
2405 	shsurf->saved_rotation_valid = false;
2406 	shsurf->desktop_surface = desktop_surface;
2407 	shsurf->view = view;
2408 	shsurf->fullscreen.black_view = NULL;
2409 	wl_list_init(&shsurf->fullscreen.transform.link);
2410 
2411 	shell_surface_set_output(
2412 		shsurf, get_default_output(shsurf->shell->compositor));
2413 
2414 	wl_signal_init(&shsurf->destroy_signal);
2415 
2416 	/* empty when not in use */
2417 	wl_list_init(&shsurf->rotation.transform.link);
2418 	weston_matrix_init(&shsurf->rotation.rotation);
2419 
2420 	wl_list_init(&shsurf->workspace_transform.link);
2421 
2422 	/*
2423 	 * initialize list as well as link. The latter allows to use
2424 	 * wl_list_remove() even when this surface is not in another list.
2425 	 */
2426 	wl_list_init(&shsurf->children_list);
2427 	wl_list_init(&shsurf->children_link);
2428 
2429 	weston_desktop_surface_set_user_data(desktop_surface, shsurf);
2430 	weston_desktop_surface_set_activated(desktop_surface,
2431 					     shsurf->focus_count > 0);
2432 }
2433 
2434 static void
desktop_surface_removed(struct weston_desktop_surface * desktop_surface,void * shell)2435 desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2436 			void *shell)
2437 {
2438 	struct shell_surface *shsurf =
2439 		weston_desktop_surface_get_user_data(desktop_surface);
2440 	struct shell_surface *shsurf_child, *tmp;
2441 	struct weston_surface *surface =
2442 		weston_desktop_surface_get_surface(desktop_surface);
2443 
2444 	if (!shsurf)
2445 		return;
2446 
2447 	wl_list_for_each_safe(shsurf_child, tmp, &shsurf->children_list, children_link) {
2448 		wl_list_remove(&shsurf_child->children_link);
2449 		wl_list_init(&shsurf_child->children_link);
2450 	}
2451 	wl_list_remove(&shsurf->children_link);
2452 
2453 	wl_signal_emit(&shsurf->destroy_signal, shsurf);
2454 
2455 	if (shsurf->fullscreen.black_view)
2456 		weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2457 
2458 	weston_surface_set_label_func(surface, NULL);
2459 	weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2460 	shsurf->desktop_surface = NULL;
2461 
2462 	weston_desktop_surface_unlink_view(shsurf->view);
2463 	if (weston_surface_is_mapped(surface) &&
2464 	    shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
2465 		pixman_region32_fini(&surface->pending.input);
2466 		pixman_region32_init(&surface->pending.input);
2467 		pixman_region32_fini(&surface->input);
2468 		pixman_region32_init(&surface->input);
2469 		weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2470 				fade_out_done, shsurf);
2471 	} else {
2472 		weston_view_destroy(shsurf->view);
2473 
2474 		if (shsurf->output_destroy_listener.notify) {
2475 			wl_list_remove(&shsurf->output_destroy_listener.link);
2476 			shsurf->output_destroy_listener.notify = NULL;
2477 		}
2478 
2479 		free(shsurf);
2480 	}
2481 }
2482 
2483 static void
set_maximized_position(struct desktop_shell * shell,struct shell_surface * shsurf)2484 set_maximized_position(struct desktop_shell *shell,
2485 		       struct shell_surface *shsurf)
2486 {
2487 	pixman_rectangle32_t area;
2488 	struct weston_geometry geometry;
2489 
2490 	get_output_work_area(shell, shsurf->output, &area);
2491 	geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2492 
2493 	weston_view_set_position(shsurf->view,
2494 				 area.x - geometry.x,
2495 				 area.y - geometry.y);
2496 }
2497 
2498 static void
set_position_from_xwayland(struct shell_surface * shsurf)2499 set_position_from_xwayland(struct shell_surface *shsurf)
2500 {
2501 	struct weston_geometry geometry;
2502 	float x;
2503 	float y;
2504 
2505 	assert(shsurf->xwayland.is_set);
2506 
2507 	geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2508 	x = shsurf->xwayland.x - geometry.x;
2509 	y = shsurf->xwayland.y - geometry.y;
2510 
2511 	weston_view_set_position(shsurf->view, x, y);
2512 
2513 #ifdef WM_DEBUG
2514 	weston_log("%s: XWM %d, %d; geometry %d, %d; view %f, %f\n",
2515 		   __func__, shsurf->xwayland.x, shsurf->xwayland.y,
2516 		   geometry.x, geometry.y, x, y);
2517 #endif
2518 }
2519 
2520 static void
map(struct desktop_shell * shell,struct shell_surface * shsurf,int32_t sx,int32_t sy)2521 map(struct desktop_shell *shell, struct shell_surface *shsurf,
2522     int32_t sx, int32_t sy)
2523 {
2524 	struct weston_surface *surface =
2525 		weston_desktop_surface_get_surface(shsurf->desktop_surface);
2526 	struct weston_compositor *compositor = shell->compositor;
2527 	struct weston_seat *seat;
2528 
2529 	/* initial positioning, see also configure() */
2530 	if (shsurf->state.fullscreen) {
2531 		center_on_output(shsurf->view, shsurf->fullscreen_output);
2532 		shell_map_fullscreen(shsurf);
2533 	} else if (shsurf->state.maximized) {
2534 		set_maximized_position(shell, shsurf);
2535 	} else if (shsurf->xwayland.is_set) {
2536 		set_position_from_xwayland(shsurf);
2537 	} else {
2538 		weston_view_set_initial_position(shsurf->view, shell);
2539 	}
2540 
2541 	/* Surface stacking order, see also activate(). */
2542 	shell_surface_update_layer(shsurf);
2543 
2544 	weston_view_update_transform(shsurf->view);
2545 	shsurf->view->is_mapped = true;
2546 	if (shsurf->state.maximized) {
2547 		surface->output = shsurf->output;
2548 		weston_view_set_output(shsurf->view, shsurf->output);
2549 	}
2550 
2551 	if (!shell->locked) {
2552 		wl_list_for_each(seat, &compositor->seat_list, link)
2553 			activate(shell, shsurf->view, seat,
2554 				 WESTON_ACTIVATE_FLAG_CONFIGURE);
2555 	}
2556 
2557 	if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
2558 		switch (shell->win_animation_type) {
2559 		case ANIMATION_FADE:
2560 			weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2561 			break;
2562 		case ANIMATION_ZOOM:
2563 			weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2564 			break;
2565 		case ANIMATION_NONE:
2566 		default:
2567 			break;
2568 		}
2569 	}
2570 }
2571 
2572 static void
desktop_surface_committed(struct weston_desktop_surface * desktop_surface,int32_t sx,int32_t sy,void * data)2573 desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2574 			  int32_t sx, int32_t sy, void *data)
2575 {
2576 	struct shell_surface *shsurf =
2577 		weston_desktop_surface_get_user_data(desktop_surface);
2578 	struct weston_surface *surface =
2579 		weston_desktop_surface_get_surface(desktop_surface);
2580 	struct weston_view *view = shsurf->view;
2581 	struct desktop_shell *shell = data;
2582 	bool was_fullscreen;
2583 	bool was_maximized;
2584 
2585 	if (surface->width == 0)
2586 		return;
2587 
2588 	was_fullscreen = shsurf->state.fullscreen;
2589 	was_maximized = shsurf->state.maximized;
2590 
2591 	shsurf->state.fullscreen =
2592 		weston_desktop_surface_get_fullscreen(desktop_surface);
2593 	shsurf->state.maximized =
2594 		weston_desktop_surface_get_maximized(desktop_surface);
2595 
2596 	if (!weston_surface_is_mapped(surface)) {
2597 		map(shell, shsurf, sx, sy);
2598 		surface->is_mapped = true;
2599 		if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2600 			++surface->ref_count;
2601 		return;
2602 	}
2603 
2604 	if (sx == 0 && sy == 0 &&
2605 	    shsurf->last_width == surface->width &&
2606 	    shsurf->last_height == surface->height &&
2607 	    was_fullscreen == shsurf->state.fullscreen &&
2608 	    was_maximized == shsurf->state.maximized)
2609 	    return;
2610 
2611 	if (was_fullscreen)
2612 		unset_fullscreen(shsurf);
2613 	if (was_maximized)
2614 		unset_maximized(shsurf);
2615 
2616 	if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2617 	    !shsurf->saved_position_valid) {
2618 		shsurf->saved_x = shsurf->view->geometry.x;
2619 		shsurf->saved_y = shsurf->view->geometry.y;
2620 		shsurf->saved_position_valid = true;
2621 
2622 		if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2623 			wl_list_remove(&shsurf->rotation.transform.link);
2624 			wl_list_init(&shsurf->rotation.transform.link);
2625 			weston_view_geometry_dirty(shsurf->view);
2626 			shsurf->saved_rotation_valid = true;
2627 		}
2628 	}
2629 
2630 	if (shsurf->state.fullscreen) {
2631 		shell_configure_fullscreen(shsurf);
2632 	} else if (shsurf->state.maximized) {
2633 		set_maximized_position(shell, shsurf);
2634 		surface->output = shsurf->output;
2635 	} else {
2636 		float from_x, from_y;
2637 		float to_x, to_y;
2638 		float x, y;
2639 
2640 		if (shsurf->resize_edges) {
2641 			sx = 0;
2642 			sy = 0;
2643 		}
2644 
2645 		if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2646 			sx = shsurf->last_width - surface->width;
2647 		if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2648 			sy = shsurf->last_height - surface->height;
2649 
2650 		weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2651 		weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2652 		x = shsurf->view->geometry.x + to_x - from_x;
2653 		y = shsurf->view->geometry.y + to_y - from_y;
2654 
2655 		weston_view_set_position(shsurf->view, x, y);
2656 	}
2657 
2658 	shsurf->last_width = surface->width;
2659 	shsurf->last_height = surface->height;
2660 
2661 	/* XXX: would a fullscreen surface need the same handling? */
2662 	if (surface->output) {
2663 		wl_list_for_each(view, &surface->views, surface_link)
2664 			weston_view_update_transform(view);
2665 	}
2666 }
2667 
2668 static void
get_maximized_size(struct shell_surface * shsurf,int32_t * width,int32_t * height)2669 get_maximized_size(struct shell_surface *shsurf, int32_t *width, int32_t *height)
2670 {
2671 	struct desktop_shell *shell;
2672 	pixman_rectangle32_t area;
2673 
2674 	shell = shell_surface_get_shell(shsurf);
2675 	get_output_work_area(shell, shsurf->output, &area);
2676 
2677 	*width = area.width;
2678 	*height = area.height;
2679 }
2680 
2681 static void
set_fullscreen(struct shell_surface * shsurf,bool fullscreen,struct weston_output * output)2682 set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2683 	       struct weston_output *output)
2684 {
2685 	struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2686 	struct weston_surface *surface =
2687 		weston_desktop_surface_get_surface(shsurf->desktop_surface);
2688 	int32_t width = 0, height = 0;
2689 
2690 	if (fullscreen) {
2691 		/* handle clients launching in fullscreen */
2692 		if (output == NULL && !weston_surface_is_mapped(surface)) {
2693 			/* Set the output to the one that has focus currently. */
2694 			output = get_focused_output(surface->compositor);
2695 		}
2696 
2697 		shell_surface_set_output(shsurf, output);
2698 		shsurf->fullscreen_output = shsurf->output;
2699 
2700 		if (shsurf->output) {
2701 			width = shsurf->output->width;
2702 			height = shsurf->output->height;
2703 		}
2704 	} else if (weston_desktop_surface_get_maximized(desktop_surface)) {
2705 		get_maximized_size(shsurf, &width, &height);
2706 	}
2707 	weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2708 	weston_desktop_surface_set_size(desktop_surface, width, height);
2709 }
2710 
2711 static void
desktop_surface_move(struct weston_desktop_surface * desktop_surface,struct weston_seat * seat,uint32_t serial,void * shell)2712 desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2713 		     struct weston_seat *seat, uint32_t serial, void *shell)
2714 {
2715 	struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2716 	struct weston_touch *touch = weston_seat_get_touch(seat);
2717 	struct shell_surface *shsurf =
2718 		weston_desktop_surface_get_user_data(desktop_surface);
2719 	struct weston_surface *surface =
2720 		weston_desktop_surface_get_surface(shsurf->desktop_surface);
2721 	struct wl_resource *resource = surface->resource;
2722 	struct weston_surface *focus;
2723 
2724 	if (pointer &&
2725 	    pointer->focus &&
2726 	    pointer->button_count > 0 &&
2727 	    pointer->grab_serial == serial) {
2728 		focus = weston_surface_get_main_surface(pointer->focus->surface);
2729 		if ((focus == surface) &&
2730 		    (surface_move(shsurf, pointer, true) < 0))
2731 			wl_resource_post_no_memory(resource);
2732 	} else if (touch &&
2733 		   touch->focus &&
2734 		   touch->grab_serial == serial) {
2735 		focus = weston_surface_get_main_surface(touch->focus->surface);
2736 		if ((focus == surface) &&
2737 		    (surface_touch_move(shsurf, touch) < 0))
2738 			wl_resource_post_no_memory(resource);
2739 	}
2740 }
2741 
2742 static void
desktop_surface_resize(struct weston_desktop_surface * desktop_surface,struct weston_seat * seat,uint32_t serial,enum weston_desktop_surface_edge edges,void * shell)2743 desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2744 		       struct weston_seat *seat, uint32_t serial,
2745 		       enum weston_desktop_surface_edge edges, void *shell)
2746 {
2747 	struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2748 	struct shell_surface *shsurf =
2749 		weston_desktop_surface_get_user_data(desktop_surface);
2750 	struct weston_surface *surface =
2751 		weston_desktop_surface_get_surface(shsurf->desktop_surface);
2752 	struct wl_resource *resource = surface->resource;
2753 	struct weston_surface *focus;
2754 
2755 	if (!pointer ||
2756 	    pointer->button_count == 0 ||
2757 	    pointer->grab_serial != serial ||
2758 	    pointer->focus == NULL)
2759 		return;
2760 
2761 	focus = weston_surface_get_main_surface(pointer->focus->surface);
2762 	if (focus != surface)
2763 		return;
2764 
2765 	if (surface_resize(shsurf, pointer, edges) < 0)
2766 		wl_resource_post_no_memory(resource);
2767 }
2768 
2769 static void
desktop_surface_set_parent(struct weston_desktop_surface * desktop_surface,struct weston_desktop_surface * parent,void * shell)2770 desktop_surface_set_parent(struct weston_desktop_surface *desktop_surface,
2771 			   struct weston_desktop_surface *parent,
2772 			   void *shell)
2773 {
2774 	struct shell_surface *shsurf_parent;
2775 	struct shell_surface *shsurf =
2776 		weston_desktop_surface_get_user_data(desktop_surface);
2777 
2778 	/* unlink any potential child */
2779 	wl_list_remove(&shsurf->children_link);
2780 
2781 	if (parent) {
2782 		shsurf_parent = weston_desktop_surface_get_user_data(parent);
2783 		wl_list_insert(shsurf_parent->children_list.prev,
2784 			       &shsurf->children_link);
2785 	} else {
2786 		wl_list_init(&shsurf->children_link);
2787 	}
2788 }
2789 
2790 static void
desktop_surface_fullscreen_requested(struct weston_desktop_surface * desktop_surface,bool fullscreen,struct weston_output * output,void * shell)2791 desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2792 				     bool fullscreen,
2793 				     struct weston_output *output, void *shell)
2794 {
2795 	struct shell_surface *shsurf =
2796 		weston_desktop_surface_get_user_data(desktop_surface);
2797 
2798 	set_fullscreen(shsurf, fullscreen, output);
2799 }
2800 
2801 static void
set_maximized(struct shell_surface * shsurf,bool maximized)2802 set_maximized(struct shell_surface *shsurf, bool maximized)
2803 {
2804 	struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2805 	struct weston_surface *surface =
2806 		weston_desktop_surface_get_surface(shsurf->desktop_surface);
2807 	int32_t width = 0, height = 0;
2808 
2809 	if (maximized) {
2810 		struct weston_output *output;
2811 
2812 		if (!weston_surface_is_mapped(surface))
2813 			output = get_focused_output(surface->compositor);
2814 		else
2815 			output = surface->output;
2816 
2817 		shell_surface_set_output(shsurf, output);
2818 
2819 		get_maximized_size(shsurf, &width, &height);
2820 	}
2821 	weston_desktop_surface_set_maximized(desktop_surface, maximized);
2822 	weston_desktop_surface_set_size(desktop_surface, width, height);
2823 }
2824 
2825 static void
desktop_surface_maximized_requested(struct weston_desktop_surface * desktop_surface,bool maximized,void * shell)2826 desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2827 				    bool maximized, void *shell)
2828 {
2829 	struct shell_surface *shsurf =
2830 		weston_desktop_surface_get_user_data(desktop_surface);
2831 
2832 	set_maximized(shsurf, maximized);
2833 }
2834 
2835 static void
desktop_surface_minimized_requested(struct weston_desktop_surface * desktop_surface,void * shell)2836 desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2837 				    void *shell)
2838 {
2839 	struct weston_surface *surface =
2840 		weston_desktop_surface_get_surface(desktop_surface);
2841 
2842 	 /* apply compositor's own minimization logic (hide) */
2843 	set_minimized(surface);
2844 }
2845 
2846 static void
set_busy_cursor(struct shell_surface * shsurf,struct weston_pointer * pointer)2847 set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
2848 {
2849 	struct shell_grab *grab;
2850 
2851 	if (pointer->grab->interface == &busy_cursor_grab_interface)
2852 		return;
2853 
2854 	grab = malloc(sizeof *grab);
2855 	if (!grab)
2856 		return;
2857 
2858 	shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2859 			 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2860 	/* Mark the shsurf as ungrabbed so that button binding is able
2861 	 * to move it. */
2862 	shsurf->grabbed = 0;
2863 }
2864 
2865 static void
end_busy_cursor(struct weston_compositor * compositor,struct weston_desktop_client * desktop_client)2866 end_busy_cursor(struct weston_compositor *compositor,
2867 		struct weston_desktop_client *desktop_client)
2868 {
2869 	struct shell_surface *shsurf;
2870 	struct shell_grab *grab;
2871 	struct weston_seat *seat;
2872 
2873 	wl_list_for_each(seat, &compositor->seat_list, link) {
2874 		struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2875 		struct weston_desktop_client *grab_client;
2876 
2877 		if (!pointer)
2878 			continue;
2879 
2880 		if (pointer->grab->interface != &busy_cursor_grab_interface)
2881 			continue;
2882 
2883 		grab = (struct shell_grab *) pointer->grab;
2884 		shsurf = grab->shsurf;
2885 		if (!shsurf)
2886 			continue;
2887 
2888 		grab_client =
2889 			weston_desktop_surface_get_client(shsurf->desktop_surface);
2890 		if (grab_client  == desktop_client) {
2891 			shell_grab_end(grab);
2892 			free(grab);
2893 		}
2894 	}
2895 }
2896 
2897 static void
desktop_surface_set_unresponsive(struct weston_desktop_surface * desktop_surface,void * user_data)2898 desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2899 				 void *user_data)
2900 {
2901 	struct shell_surface *shsurf =
2902 		weston_desktop_surface_get_user_data(desktop_surface);
2903 	bool *unresponsive = user_data;
2904 
2905 	shsurf->unresponsive = *unresponsive;
2906 }
2907 
2908 static void
desktop_surface_ping_timeout(struct weston_desktop_client * desktop_client,void * shell_)2909 desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2910 			     void *shell_)
2911 {
2912 	struct desktop_shell *shell = shell_;
2913 	struct shell_surface *shsurf;
2914 	struct weston_seat *seat;
2915 	bool unresponsive = true;
2916 
2917 	weston_desktop_client_for_each_surface(desktop_client,
2918 					       desktop_surface_set_unresponsive,
2919 					       &unresponsive);
2920 
2921 
2922 	wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2923 		struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2924 		struct weston_desktop_client *grab_client;
2925 
2926 		if (!pointer || !pointer->focus)
2927 			continue;
2928 
2929 		shsurf = get_shell_surface(pointer->focus->surface);
2930 		if (!shsurf)
2931 			continue;
2932 
2933 		grab_client =
2934 			weston_desktop_surface_get_client(shsurf->desktop_surface);
2935 		if (grab_client == desktop_client)
2936 			set_busy_cursor(shsurf, pointer);
2937 	}
2938 }
2939 
2940 static void
desktop_surface_pong(struct weston_desktop_client * desktop_client,void * shell_)2941 desktop_surface_pong(struct weston_desktop_client *desktop_client,
2942 		     void *shell_)
2943 {
2944 	struct desktop_shell *shell = shell_;
2945 	bool unresponsive = false;
2946 
2947 	weston_desktop_client_for_each_surface(desktop_client,
2948 					       desktop_surface_set_unresponsive,
2949 					       &unresponsive);
2950 	end_busy_cursor(shell->compositor, desktop_client);
2951 }
2952 
2953 static void
desktop_surface_set_xwayland_position(struct weston_desktop_surface * surface,int32_t x,int32_t y,void * shell_)2954 desktop_surface_set_xwayland_position(struct weston_desktop_surface *surface,
2955 				      int32_t x, int32_t y, void *shell_)
2956 {
2957 	struct shell_surface *shsurf =
2958 		weston_desktop_surface_get_user_data(surface);
2959 
2960 	shsurf->xwayland.x = x;
2961 	shsurf->xwayland.y = y;
2962 	shsurf->xwayland.is_set = true;
2963 }
2964 
2965 static const struct weston_desktop_api shell_desktop_api = {
2966 	.struct_size = sizeof(struct weston_desktop_api),
2967 	.surface_added = desktop_surface_added,
2968 	.surface_removed = desktop_surface_removed,
2969 	.committed = desktop_surface_committed,
2970 	.move = desktop_surface_move,
2971 	.resize = desktop_surface_resize,
2972 	.set_parent = desktop_surface_set_parent,
2973 	.fullscreen_requested = desktop_surface_fullscreen_requested,
2974 	.maximized_requested = desktop_surface_maximized_requested,
2975 	.minimized_requested = desktop_surface_minimized_requested,
2976 	.ping_timeout = desktop_surface_ping_timeout,
2977 	.pong = desktop_surface_pong,
2978 	.set_xwayland_position = desktop_surface_set_xwayland_position,
2979 };
2980 
2981 /* ************************ *
2982  * end of libweston-desktop *
2983  * ************************ */
2984 static void
configure_static_view(struct weston_view * ev,struct weston_layer * layer,int x,int y)2985 configure_static_view(struct weston_view *ev, struct weston_layer *layer, int x, int y)
2986 {
2987 	struct weston_view *v, *next;
2988 
2989 	if (!ev->output)
2990 		return;
2991 
2992 	wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
2993 		if (v->output == ev->output && v != ev) {
2994 			weston_view_unmap(v);
2995 			v->surface->committed = NULL;
2996 			weston_surface_set_label_func(v->surface, NULL);
2997 		}
2998 	}
2999 
3000 	weston_view_set_position(ev, ev->output->x + x, ev->output->y + y);
3001 	ev->surface->is_mapped = true;
3002 	ev->is_mapped = true;
3003 
3004 	if (wl_list_empty(&ev->layer_link.link)) {
3005 		weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
3006 		weston_compositor_schedule_repaint(ev->surface->compositor);
3007 	}
3008 }
3009 
3010 
3011 static struct shell_output *
find_shell_output_from_weston_output(struct desktop_shell * shell,struct weston_output * output)3012 find_shell_output_from_weston_output(struct desktop_shell *shell,
3013 				     struct weston_output *output)
3014 {
3015 	struct shell_output *shell_output;
3016 
3017 	wl_list_for_each(shell_output, &shell->output_list, link) {
3018 		if (shell_output->output == output)
3019 			return shell_output;
3020 	}
3021 
3022 	return NULL;
3023 }
3024 
3025 static int
background_get_label(struct weston_surface * surface,char * buf,size_t len)3026 background_get_label(struct weston_surface *surface, char *buf, size_t len)
3027 {
3028 	return snprintf(buf, len, "background for output %s",
3029 			(surface->output ? surface->output->name : "NULL"));
3030 }
3031 
3032 static void
background_committed(struct weston_surface * es,int32_t sx,int32_t sy)3033 background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
3034 {
3035 	struct desktop_shell *shell = es->committed_private;
3036 	struct weston_view *view;
3037 
3038 	view = container_of(es->views.next, struct weston_view, surface_link);
3039 
3040 	configure_static_view(view, &shell->background_layer, 0, 0);
3041 }
3042 
3043 static void
handle_background_surface_destroy(struct wl_listener * listener,void * data)3044 handle_background_surface_destroy(struct wl_listener *listener, void *data)
3045 {
3046 	struct shell_output *output =
3047 	    container_of(listener, struct shell_output, background_surface_listener);
3048 
3049 	weston_log("background surface gone\n");
3050 	wl_list_remove(&output->background_surface_listener.link);
3051 	output->background_surface = NULL;
3052 }
3053 
3054 static void
desktop_shell_set_background(struct wl_client * client,struct wl_resource * resource,struct wl_resource * output_resource,struct wl_resource * surface_resource)3055 desktop_shell_set_background(struct wl_client *client,
3056 			     struct wl_resource *resource,
3057 			     struct wl_resource *output_resource,
3058 			     struct wl_resource *surface_resource)
3059 {
3060 	struct desktop_shell *shell = wl_resource_get_user_data(resource);
3061 	struct weston_surface *surface =
3062 		wl_resource_get_user_data(surface_resource);
3063 	struct shell_output *sh_output;
3064 	struct weston_view *view, *next;
3065 
3066 	if (surface->committed) {
3067 		wl_resource_post_error(surface_resource,
3068 				       WL_DISPLAY_ERROR_INVALID_OBJECT,
3069 				       "surface role already assigned");
3070 		return;
3071 	}
3072 
3073 	wl_list_for_each_safe(view, next, &surface->views, surface_link)
3074 		weston_view_destroy(view);
3075 	view = weston_view_create(surface);
3076 
3077 	surface->committed = background_committed;
3078 	surface->committed_private = shell;
3079 	weston_surface_set_label_func(surface, background_get_label);
3080 	surface->output = weston_head_from_resource(output_resource)->output;
3081 	weston_view_set_output(view, surface->output);
3082 
3083 	sh_output = find_shell_output_from_weston_output(shell, surface->output);
3084 	if (sh_output->background_surface) {
3085 		/* The output already has a background, tell our helper
3086 		 * there is no need for another one. */
3087 		weston_desktop_shell_send_configure(resource, 0,
3088 						    surface_resource,
3089 						    0, 0);
3090 	} else {
3091 		weston_desktop_shell_send_configure(resource, 0,
3092 						    surface_resource,
3093 						    surface->output->width,
3094 						    surface->output->height);
3095 
3096 		sh_output->background_surface = surface;
3097 
3098 		sh_output->background_surface_listener.notify =
3099 					handle_background_surface_destroy;
3100 		wl_signal_add(&surface->destroy_signal,
3101 			      &sh_output->background_surface_listener);
3102 	}
3103 }
3104 
3105 static int
panel_get_label(struct weston_surface * surface,char * buf,size_t len)3106 panel_get_label(struct weston_surface *surface, char *buf, size_t len)
3107 {
3108 	return snprintf(buf, len, "panel for output %s",
3109 			(surface->output ? surface->output->name : "NULL"));
3110 }
3111 
3112 static void
panel_committed(struct weston_surface * es,int32_t sx,int32_t sy)3113 panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
3114 {
3115 	struct desktop_shell *shell = es->committed_private;
3116 	struct weston_view *view;
3117 	int width, height;
3118 	int x = 0, y = 0;
3119 
3120 	view = container_of(es->views.next, struct weston_view, surface_link);
3121 
3122 	get_panel_size(shell, view, &width, &height);
3123 	switch (shell->panel_position) {
3124 	case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
3125 		break;
3126 	case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
3127 		y = view->output->height - height;
3128 		break;
3129 	case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
3130 		break;
3131 	case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
3132 		x = view->output->width - width;
3133 		break;
3134 	}
3135 
3136 	configure_static_view(view, &shell->panel_layer, x, y);
3137 }
3138 
3139 static void
handle_panel_surface_destroy(struct wl_listener * listener,void * data)3140 handle_panel_surface_destroy(struct wl_listener *listener, void *data)
3141 {
3142 	struct shell_output *output =
3143 	    container_of(listener, struct shell_output, panel_surface_listener);
3144 
3145 	weston_log("panel surface gone\n");
3146 	wl_list_remove(&output->panel_surface_listener.link);
3147 	output->panel_surface = NULL;
3148 }
3149 
3150 
3151 static void
desktop_shell_set_panel(struct wl_client * client,struct wl_resource * resource,struct wl_resource * output_resource,struct wl_resource * surface_resource)3152 desktop_shell_set_panel(struct wl_client *client,
3153 			struct wl_resource *resource,
3154 			struct wl_resource *output_resource,
3155 			struct wl_resource *surface_resource)
3156 {
3157 	struct desktop_shell *shell = wl_resource_get_user_data(resource);
3158 	struct weston_surface *surface =
3159 		wl_resource_get_user_data(surface_resource);
3160 	struct weston_view *view, *next;
3161 	struct shell_output *sh_output;
3162 
3163 	if (surface->committed) {
3164 		wl_resource_post_error(surface_resource,
3165 				       WL_DISPLAY_ERROR_INVALID_OBJECT,
3166 				       "surface role already assigned");
3167 		return;
3168 	}
3169 
3170 	wl_list_for_each_safe(view, next, &surface->views, surface_link)
3171 		weston_view_destroy(view);
3172 	view = weston_view_create(surface);
3173 
3174 	surface->committed = panel_committed;
3175 	surface->committed_private = shell;
3176 	weston_surface_set_label_func(surface, panel_get_label);
3177 	surface->output = weston_head_from_resource(output_resource)->output;
3178 	weston_view_set_output(view, surface->output);
3179 
3180 	sh_output = find_shell_output_from_weston_output(shell, surface->output);
3181 	if (sh_output->panel_surface) {
3182 		/* The output already has a panel, tell our helper
3183 		 * there is no need for another one. */
3184 		weston_desktop_shell_send_configure(resource, 0,
3185 						    surface_resource,
3186 						    0, 0);
3187 	} else {
3188 		weston_desktop_shell_send_configure(resource, 0,
3189 						    surface_resource,
3190 						    surface->output->width,
3191 						    surface->output->height);
3192 
3193 		sh_output->panel_surface = surface;
3194 
3195 		sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
3196 		wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
3197 	}
3198 }
3199 
3200 static int
lock_surface_get_label(struct weston_surface * surface,char * buf,size_t len)3201 lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
3202 {
3203 	return snprintf(buf, len, "lock window");
3204 }
3205 
3206 static void
lock_surface_committed(struct weston_surface * surface,int32_t sx,int32_t sy)3207 lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
3208 {
3209 	struct desktop_shell *shell = surface->committed_private;
3210 	struct weston_view *view;
3211 
3212 	view = container_of(surface->views.next, struct weston_view, surface_link);
3213 
3214 	if (surface->width == 0)
3215 		return;
3216 
3217 	center_on_output(view, get_default_output(shell->compositor));
3218 
3219 	if (!weston_surface_is_mapped(surface)) {
3220 		weston_layer_entry_insert(&shell->lock_layer.view_list,
3221 					  &view->layer_link);
3222 		weston_view_update_transform(view);
3223 		surface->is_mapped = true;
3224 		view->is_mapped = true;
3225 		shell_fade(shell, FADE_IN);
3226 	}
3227 }
3228 
3229 static void
handle_lock_surface_destroy(struct wl_listener * listener,void * data)3230 handle_lock_surface_destroy(struct wl_listener *listener, void *data)
3231 {
3232 	struct desktop_shell *shell =
3233 	    container_of(listener, struct desktop_shell, lock_surface_listener);
3234 
3235 	weston_log("lock surface gone\n");
3236 	shell->lock_surface = NULL;
3237 }
3238 
3239 static void
desktop_shell_set_lock_surface(struct wl_client * client,struct wl_resource * resource,struct wl_resource * surface_resource)3240 desktop_shell_set_lock_surface(struct wl_client *client,
3241 			       struct wl_resource *resource,
3242 			       struct wl_resource *surface_resource)
3243 {
3244 	struct desktop_shell *shell = wl_resource_get_user_data(resource);
3245 	struct weston_surface *surface =
3246 		wl_resource_get_user_data(surface_resource);
3247 
3248 	shell->prepare_event_sent = false;
3249 
3250 	if (!shell->locked)
3251 		return;
3252 
3253 	shell->lock_surface = surface;
3254 
3255 	shell->lock_surface_listener.notify = handle_lock_surface_destroy;
3256 	wl_signal_add(&surface->destroy_signal,
3257 		      &shell->lock_surface_listener);
3258 
3259 	weston_view_create(surface);
3260 	surface->committed = lock_surface_committed;
3261 	surface->committed_private = shell;
3262 	weston_surface_set_label_func(surface, lock_surface_get_label);
3263 }
3264 
3265 static void
resume_desktop(struct desktop_shell * shell)3266 resume_desktop(struct desktop_shell *shell)
3267 {
3268 	struct workspace *ws = get_current_workspace(shell);
3269 
3270 	weston_layer_unset_position(&shell->lock_layer);
3271 
3272 	if (shell->showing_input_panels)
3273 		weston_layer_set_position(&shell->input_panel_layer,
3274 					  WESTON_LAYER_POSITION_TOP_UI);
3275 	weston_layer_set_position(&shell->fullscreen_layer,
3276 				  WESTON_LAYER_POSITION_FULLSCREEN);
3277 	weston_layer_set_position(&shell->panel_layer,
3278 				  WESTON_LAYER_POSITION_UI);
3279 	weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
3280 
3281 	restore_focus_state(shell, get_current_workspace(shell));
3282 
3283 	shell->locked = false;
3284 	shell_fade(shell, FADE_IN);
3285 	weston_compositor_damage_all(shell->compositor);
3286 }
3287 
3288 static void
desktop_shell_unlock(struct wl_client * client,struct wl_resource * resource)3289 desktop_shell_unlock(struct wl_client *client,
3290 		     struct wl_resource *resource)
3291 {
3292 	struct desktop_shell *shell = wl_resource_get_user_data(resource);
3293 
3294 	shell->prepare_event_sent = false;
3295 
3296 	if (shell->locked)
3297 		resume_desktop(shell);
3298 }
3299 
3300 static void
desktop_shell_set_grab_surface(struct wl_client * client,struct wl_resource * resource,struct wl_resource * surface_resource)3301 desktop_shell_set_grab_surface(struct wl_client *client,
3302 			       struct wl_resource *resource,
3303 			       struct wl_resource *surface_resource)
3304 {
3305 	struct desktop_shell *shell = wl_resource_get_user_data(resource);
3306 
3307 	shell->grab_surface = wl_resource_get_user_data(surface_resource);
3308 	weston_view_create(shell->grab_surface);
3309 }
3310 
3311 static void
desktop_shell_desktop_ready(struct wl_client * client,struct wl_resource * resource)3312 desktop_shell_desktop_ready(struct wl_client *client,
3313 			    struct wl_resource *resource)
3314 {
3315 	struct desktop_shell *shell = wl_resource_get_user_data(resource);
3316 
3317 	shell_fade_startup(shell);
3318 }
3319 
3320 static void
desktop_shell_set_panel_position(struct wl_client * client,struct wl_resource * resource,uint32_t position)3321 desktop_shell_set_panel_position(struct wl_client *client,
3322 				 struct wl_resource *resource,
3323 				 uint32_t position)
3324 {
3325 	struct desktop_shell *shell = wl_resource_get_user_data(resource);
3326 
3327 	if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3328 	    position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3329 	    position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3330 	    position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
3331 		wl_resource_post_error(resource,
3332 				       WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
3333 				       "bad position argument");
3334 		return;
3335 	}
3336 
3337 	shell->panel_position = position;
3338 }
3339 
3340 static const struct weston_desktop_shell_interface desktop_shell_implementation = {
3341 	desktop_shell_set_background,
3342 	desktop_shell_set_panel,
3343 	desktop_shell_set_lock_surface,
3344 	desktop_shell_unlock,
3345 	desktop_shell_set_grab_surface,
3346 	desktop_shell_desktop_ready,
3347 	desktop_shell_set_panel_position
3348 };
3349 
3350 static void
move_binding(struct weston_pointer * pointer,const struct timespec * time,uint32_t button,void * data)3351 move_binding(struct weston_pointer *pointer, const struct timespec *time,
3352 	     uint32_t button, void *data)
3353 {
3354 	struct weston_surface *focus;
3355 	struct weston_surface *surface;
3356 	struct shell_surface *shsurf;
3357 
3358 	if (pointer->focus == NULL)
3359 		return;
3360 
3361 	focus = pointer->focus->surface;
3362 
3363 	surface = weston_surface_get_main_surface(focus);
3364 	if (surface == NULL)
3365 		return;
3366 
3367 	shsurf = get_shell_surface(surface);
3368 	if (shsurf == NULL ||
3369 	    weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3370 	    weston_desktop_surface_get_maximized(shsurf->desktop_surface))
3371 		return;
3372 
3373 	surface_move(shsurf, pointer, false);
3374 }
3375 
3376 static void
maximize_binding(struct weston_keyboard * keyboard,const struct timespec * time,uint32_t button,void * data)3377 maximize_binding(struct weston_keyboard *keyboard, const struct timespec *time,
3378 		 uint32_t button, void *data)
3379 {
3380 	struct weston_surface *focus = keyboard->focus;
3381 	struct weston_surface *surface;
3382 	struct shell_surface *shsurf;
3383 
3384 	surface = weston_surface_get_main_surface(focus);
3385 	if (surface == NULL)
3386 		return;
3387 
3388 	shsurf = get_shell_surface(surface);
3389 	if (shsurf == NULL)
3390 		return;
3391 
3392 	set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
3393 }
3394 
3395 static void
fullscreen_binding(struct weston_keyboard * keyboard,const struct timespec * time,uint32_t button,void * data)3396 fullscreen_binding(struct weston_keyboard *keyboard,
3397 		   const struct timespec *time, uint32_t button, void *data)
3398 {
3399 	struct weston_surface *focus = keyboard->focus;
3400 	struct weston_surface *surface;
3401 	struct shell_surface *shsurf;
3402 	bool fullscreen;
3403 
3404 	surface = weston_surface_get_main_surface(focus);
3405 	if (surface == NULL)
3406 		return;
3407 
3408 	shsurf = get_shell_surface(surface);
3409 	if (shsurf == NULL)
3410 		return;
3411 
3412 	fullscreen =
3413 		weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
3414 
3415 	set_fullscreen(shsurf, !fullscreen, NULL);
3416 }
3417 
3418 static void
touch_move_binding(struct weston_touch * touch,const struct timespec * time,void * data)3419 touch_move_binding(struct weston_touch *touch, const struct timespec *time, void *data)
3420 {
3421 	struct weston_surface *focus;
3422 	struct weston_surface *surface;
3423 	struct shell_surface *shsurf;
3424 
3425 	if (touch->focus == NULL)
3426 		return;
3427 
3428 	focus = touch->focus->surface;
3429 	surface = weston_surface_get_main_surface(focus);
3430 	if (surface == NULL)
3431 		return;
3432 
3433 	shsurf = get_shell_surface(surface);
3434 	if (shsurf == NULL ||
3435 	    weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3436 	    weston_desktop_surface_get_maximized(shsurf->desktop_surface))
3437 		return;
3438 
3439 	surface_touch_move(shsurf, touch);
3440 }
3441 
3442 static void
resize_binding(struct weston_pointer * pointer,const struct timespec * time,uint32_t button,void * data)3443 resize_binding(struct weston_pointer *pointer, const struct timespec *time,
3444 	       uint32_t button, void *data)
3445 {
3446 	struct weston_surface *focus;
3447 	struct weston_surface *surface;
3448 	uint32_t edges = 0;
3449 	int32_t x, y;
3450 	struct shell_surface *shsurf;
3451 
3452 	if (pointer->focus == NULL)
3453 		return;
3454 
3455 	focus = pointer->focus->surface;
3456 
3457 	surface = weston_surface_get_main_surface(focus);
3458 	if (surface == NULL)
3459 		return;
3460 
3461 	shsurf = get_shell_surface(surface);
3462 	if (shsurf == NULL ||
3463 	    weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3464 	    weston_desktop_surface_get_maximized(shsurf->desktop_surface))
3465 		return;
3466 
3467 	weston_view_from_global(shsurf->view,
3468 				wl_fixed_to_int(pointer->grab_x),
3469 				wl_fixed_to_int(pointer->grab_y),
3470 				&x, &y);
3471 
3472 	if (x < surface->width / 3)
3473 		edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
3474 	else if (x < 2 * surface->width / 3)
3475 		edges |= 0;
3476 	else
3477 		edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
3478 
3479 	if (y < surface->height / 3)
3480 		edges |= WL_SHELL_SURFACE_RESIZE_TOP;
3481 	else if (y < 2 * surface->height / 3)
3482 		edges |= 0;
3483 	else
3484 		edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
3485 
3486 	surface_resize(shsurf, pointer, edges);
3487 }
3488 
3489 static void
surface_opacity_binding(struct weston_pointer * pointer,const struct timespec * time,struct weston_pointer_axis_event * event,void * data)3490 surface_opacity_binding(struct weston_pointer *pointer,
3491 			const struct timespec *time,
3492 			struct weston_pointer_axis_event *event,
3493 			void *data)
3494 {
3495 	float step = 0.005;
3496 	struct shell_surface *shsurf;
3497 	struct weston_surface *focus = pointer->focus->surface;
3498 	struct weston_surface *surface;
3499 
3500 	/* XXX: broken for windows containing sub-surfaces */
3501 	surface = weston_surface_get_main_surface(focus);
3502 	if (surface == NULL)
3503 		return;
3504 
3505 	shsurf = get_shell_surface(surface);
3506 	if (!shsurf)
3507 		return;
3508 
3509 	shsurf->view->alpha -= event->value * step;
3510 
3511 	if (shsurf->view->alpha > 1.0)
3512 		shsurf->view->alpha = 1.0;
3513 	if (shsurf->view->alpha < step)
3514 		shsurf->view->alpha = step;
3515 
3516 	weston_view_geometry_dirty(shsurf->view);
3517 	weston_surface_damage(surface);
3518 }
3519 
3520 static void
do_zoom(struct weston_seat * seat,const struct timespec * time,uint32_t key,uint32_t axis,double value)3521 do_zoom(struct weston_seat *seat, const struct timespec *time, uint32_t key,
3522 	uint32_t axis, double value)
3523 {
3524 	struct weston_compositor *compositor = seat->compositor;
3525 	struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3526 	struct weston_output *output;
3527 	float increment;
3528 
3529 	if (!pointer) {
3530 		weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3531 		return;
3532 	}
3533 
3534 	wl_list_for_each(output, &compositor->output_list, link) {
3535 		if (pixman_region32_contains_point(&output->region,
3536 						   wl_fixed_to_double(pointer->x),
3537 						   wl_fixed_to_double(pointer->y),
3538 						   NULL)) {
3539 			if (key == KEY_PAGEUP)
3540 				increment = output->zoom.increment;
3541 			else if (key == KEY_PAGEDOWN)
3542 				increment = -output->zoom.increment;
3543 			else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
3544 				/* For every pixel zoom 20th of a step */
3545 				increment = output->zoom.increment *
3546 					    -value / 20.0;
3547 			else
3548 				increment = 0;
3549 
3550 			output->zoom.level += increment;
3551 
3552 			if (output->zoom.level < 0.0)
3553 				output->zoom.level = 0.0;
3554 			else if (output->zoom.level > output->zoom.max_level)
3555 				output->zoom.level = output->zoom.max_level;
3556 
3557 			if (!output->zoom.active) {
3558 				if (output->zoom.level <= 0.0)
3559 					continue;
3560 				weston_output_activate_zoom(output, seat);
3561 			}
3562 
3563 			output->zoom.spring_z.target = output->zoom.level;
3564 
3565 			weston_output_update_zoom(output);
3566 		}
3567 	}
3568 }
3569 
3570 static void
zoom_axis_binding(struct weston_pointer * pointer,const struct timespec * time,struct weston_pointer_axis_event * event,void * data)3571 zoom_axis_binding(struct weston_pointer *pointer, const struct timespec *time,
3572 		  struct weston_pointer_axis_event *event,
3573 		  void *data)
3574 {
3575 	do_zoom(pointer->seat, time, 0, event->axis, event->value);
3576 }
3577 
3578 static void
zoom_key_binding(struct weston_keyboard * keyboard,const struct timespec * time,uint32_t key,void * data)3579 zoom_key_binding(struct weston_keyboard *keyboard, const struct timespec *time,
3580 		 uint32_t key, void *data)
3581 {
3582 	do_zoom(keyboard->seat, time, key, 0, 0);
3583 }
3584 
3585 static void
terminate_binding(struct weston_keyboard * keyboard,const struct timespec * time,uint32_t key,void * data)3586 terminate_binding(struct weston_keyboard *keyboard, const struct timespec *time,
3587 		  uint32_t key, void *data)
3588 {
3589 	struct weston_compositor *compositor = data;
3590 
3591 	weston_compositor_exit(compositor);
3592 }
3593 
3594 static void
rotate_grab_motion(struct weston_pointer_grab * grab,const struct timespec * time,struct weston_pointer_motion_event * event)3595 rotate_grab_motion(struct weston_pointer_grab *grab,
3596 		   const struct timespec *time,
3597 		   struct weston_pointer_motion_event *event)
3598 {
3599 	struct rotate_grab *rotate =
3600 		container_of(grab, struct rotate_grab, base.grab);
3601 	struct weston_pointer *pointer = grab->pointer;
3602 	struct shell_surface *shsurf = rotate->base.shsurf;
3603 	struct weston_surface *surface;
3604 	float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
3605 
3606 	weston_pointer_move(pointer, event);
3607 
3608 	if (!shsurf)
3609 		return;
3610 
3611 	surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
3612 
3613 	cx = 0.5f * surface->width;
3614 	cy = 0.5f * surface->height;
3615 
3616 	dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3617 	dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
3618 	r = sqrtf(dx * dx + dy * dy);
3619 
3620 	wl_list_remove(&shsurf->rotation.transform.link);
3621 	weston_view_geometry_dirty(shsurf->view);
3622 
3623 	if (r > 20.0f) {
3624 		struct weston_matrix *matrix =
3625 			&shsurf->rotation.transform.matrix;
3626 
3627 		weston_matrix_init(&rotate->rotation);
3628 		weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
3629 
3630 		weston_matrix_init(matrix);
3631 		weston_matrix_translate(matrix, -cx, -cy, 0.0f);
3632 		weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
3633 		weston_matrix_multiply(matrix, &rotate->rotation);
3634 		weston_matrix_translate(matrix, cx, cy, 0.0f);
3635 
3636 		wl_list_insert(
3637 			&shsurf->view->geometry.transformation_list,
3638 			&shsurf->rotation.transform.link);
3639 	} else {
3640 		wl_list_init(&shsurf->rotation.transform.link);
3641 		weston_matrix_init(&shsurf->rotation.rotation);
3642 		weston_matrix_init(&rotate->rotation);
3643 	}
3644 
3645 	/* We need to adjust the position of the surface
3646 	 * in case it was resized in a rotated state before */
3647 	cposx = shsurf->view->geometry.x + cx;
3648 	cposy = shsurf->view->geometry.y + cy;
3649 	dposx = rotate->center.x - cposx;
3650 	dposy = rotate->center.y - cposy;
3651 	if (dposx != 0.0f || dposy != 0.0f) {
3652 		weston_view_set_position(shsurf->view,
3653 					 shsurf->view->geometry.x + dposx,
3654 					 shsurf->view->geometry.y + dposy);
3655 	}
3656 
3657 	/* Repaint implies weston_view_update_transform(), which
3658 	 * lazily applies the damage due to rotation update.
3659 	 */
3660 	weston_compositor_schedule_repaint(surface->compositor);
3661 }
3662 
3663 static void
rotate_grab_button(struct weston_pointer_grab * grab,const struct timespec * time,uint32_t button,uint32_t state_w)3664 rotate_grab_button(struct weston_pointer_grab *grab,
3665 		   const struct timespec *time,
3666 		   uint32_t button, uint32_t state_w)
3667 {
3668 	struct rotate_grab *rotate =
3669 		container_of(grab, struct rotate_grab, base.grab);
3670 	struct weston_pointer *pointer = grab->pointer;
3671 	struct shell_surface *shsurf = rotate->base.shsurf;
3672 	enum wl_pointer_button_state state = state_w;
3673 
3674 	if (pointer->button_count == 0 &&
3675 	    state == WL_POINTER_BUTTON_STATE_RELEASED) {
3676 		if (shsurf)
3677 			weston_matrix_multiply(&shsurf->rotation.rotation,
3678 					       &rotate->rotation);
3679 		shell_grab_end(&rotate->base);
3680 		free(rotate);
3681 	}
3682 }
3683 
3684 static void
rotate_grab_cancel(struct weston_pointer_grab * grab)3685 rotate_grab_cancel(struct weston_pointer_grab *grab)
3686 {
3687 	struct rotate_grab *rotate =
3688 		container_of(grab, struct rotate_grab, base.grab);
3689 
3690 	shell_grab_end(&rotate->base);
3691 	free(rotate);
3692 }
3693 
3694 static const struct weston_pointer_grab_interface rotate_grab_interface = {
3695 	noop_grab_focus,
3696 	rotate_grab_motion,
3697 	rotate_grab_button,
3698 	noop_grab_axis,
3699 	noop_grab_axis_source,
3700 	noop_grab_frame,
3701 	rotate_grab_cancel,
3702 };
3703 
3704 static void
surface_rotate(struct shell_surface * shsurf,struct weston_pointer * pointer)3705 surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
3706 {
3707 	struct weston_surface *surface =
3708 		weston_desktop_surface_get_surface(shsurf->desktop_surface);
3709 	struct rotate_grab *rotate;
3710 	float dx, dy;
3711 	float r;
3712 
3713 	rotate = malloc(sizeof *rotate);
3714 	if (!rotate)
3715 		return;
3716 
3717 	weston_view_to_global_float(shsurf->view,
3718 				    surface->width * 0.5f,
3719 				    surface->height * 0.5f,
3720 				    &rotate->center.x, &rotate->center.y);
3721 
3722 	dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3723 	dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
3724 	r = sqrtf(dx * dx + dy * dy);
3725 	if (r > 20.0f) {
3726 		struct weston_matrix inverse;
3727 
3728 		weston_matrix_init(&inverse);
3729 		weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
3730 		weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
3731 
3732 		weston_matrix_init(&rotate->rotation);
3733 		weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
3734 	} else {
3735 		weston_matrix_init(&shsurf->rotation.rotation);
3736 		weston_matrix_init(&rotate->rotation);
3737 	}
3738 
3739 	shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
3740 			 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
3741 }
3742 
3743 static void
rotate_binding(struct weston_pointer * pointer,const struct timespec * time,uint32_t button,void * data)3744 rotate_binding(struct weston_pointer *pointer, const struct timespec *time,
3745 	       uint32_t button, void *data)
3746 {
3747 	struct weston_surface *focus;
3748 	struct weston_surface *base_surface;
3749 	struct shell_surface *surface;
3750 
3751 	if (pointer->focus == NULL)
3752 		return;
3753 
3754 	focus = pointer->focus->surface;
3755 
3756 	base_surface = weston_surface_get_main_surface(focus);
3757 	if (base_surface == NULL)
3758 		return;
3759 
3760 	surface = get_shell_surface(base_surface);
3761 	if (surface == NULL ||
3762 	    weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3763 	    weston_desktop_surface_get_maximized(surface->desktop_surface))
3764 		return;
3765 
3766 	surface_rotate(surface, pointer);
3767 }
3768 
3769 /* Move all fullscreen layers down to the current workspace and hide their
3770  * black views. The surfaces' state is set to both fullscreen and lowered,
3771  * and this is reversed when such a surface is re-configured, see
3772  * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3773  *
3774  * lowering_output = NULL - Lower on all outputs, else only lower on the
3775  *                   specified output.
3776  *
3777  * This should be used when implementing shell-wide overlays, such as
3778  * the alt-tab switcher, which need to de-promote fullscreen layers. */
3779 void
lower_fullscreen_layer(struct desktop_shell * shell,struct weston_output * lowering_output)3780 lower_fullscreen_layer(struct desktop_shell *shell,
3781 		       struct weston_output *lowering_output)
3782 {
3783 	struct workspace *ws;
3784 	struct weston_view *view, *prev;
3785 
3786 	ws = get_current_workspace(shell);
3787 	wl_list_for_each_reverse_safe(view, prev,
3788 				      &shell->fullscreen_layer.view_list.link,
3789 				      layer_link.link) {
3790 		struct shell_surface *shsurf = get_shell_surface(view->surface);
3791 
3792 		if (!shsurf)
3793 			continue;
3794 
3795 		/* Only lower surfaces which have lowering_output as their fullscreen
3796 		 * output, unless a NULL output asks for lowering on all outputs.
3797 		 */
3798 		if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3799 			continue;
3800 
3801 		/* We can have a non-fullscreen popup for a fullscreen surface
3802 		 * in the fullscreen layer. */
3803 		if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
3804 			/* Hide the black view */
3805 			weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3806 			wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
3807 			weston_view_damage_below(shsurf->fullscreen.black_view);
3808 
3809 		}
3810 
3811 		/* Lower the view to the workspace layer */
3812 		weston_layer_entry_remove(&view->layer_link);
3813 		weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
3814 		weston_view_damage_below(view);
3815 		weston_surface_damage(view->surface);
3816 
3817 		shsurf->state.lowered = true;
3818 	}
3819 }
3820 
get_last_child(struct shell_surface * shsurf)3821 static struct shell_surface *get_last_child(struct shell_surface *shsurf)
3822 {
3823 	struct shell_surface *shsurf_child;
3824 
3825 	wl_list_for_each_reverse(shsurf_child, &shsurf->children_list, children_link) {
3826 		if (weston_view_is_mapped(shsurf_child->view))
3827 			return shsurf_child;
3828 	}
3829 
3830 	return NULL;
3831 }
3832 
3833 void
activate(struct desktop_shell * shell,struct weston_view * view,struct weston_seat * seat,uint32_t flags)3834 activate(struct desktop_shell *shell, struct weston_view *view,
3835 	 struct weston_seat *seat, uint32_t flags)
3836 {
3837 	struct weston_surface *es = view->surface;
3838 	struct weston_surface *main_surface;
3839 	struct focus_state *state;
3840 	struct workspace *ws;
3841 	struct weston_surface *old_es;
3842 	struct shell_surface *shsurf, *shsurf_child;
3843 
3844 	main_surface = weston_surface_get_main_surface(es);
3845 	shsurf = get_shell_surface(main_surface);
3846 	assert(shsurf);
3847 
3848 	shsurf_child = get_last_child(shsurf);
3849 	if (shsurf_child) {
3850 		/* Activate last xdg child instead of parent. */
3851 		activate(shell, shsurf_child->view, seat, flags);
3852 		return;
3853 	}
3854 
3855 	/* Only demote fullscreen surfaces on the output of activated shsurf.
3856 	 * Leave fullscreen surfaces on unrelated outputs alone. */
3857 	if (shsurf->output)
3858 		lower_fullscreen_layer(shell, shsurf->output);
3859 
3860 	weston_view_activate(view, seat, flags);
3861 
3862 	state = ensure_focus_state(shell, seat);
3863 	if (state == NULL)
3864 		return;
3865 
3866 	old_es = state->keyboard_focus;
3867 	focus_state_set_focus(state, es);
3868 
3869 	if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3870 	    flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
3871 		shell_configure_fullscreen(shsurf);
3872 
3873 	/* Update the surface’s layer. This brings it to the top of the stacking
3874 	 * order as appropriate. */
3875 	shell_surface_update_layer(shsurf);
3876 
3877 	if (shell->focus_animation_type != ANIMATION_NONE) {
3878 		ws = get_current_workspace(shell);
3879 		animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
3880 	}
3881 }
3882 
3883 /* no-op func for checking black surface */
3884 static void
black_surface_committed(struct weston_surface * es,int32_t sx,int32_t sy)3885 black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
3886 {
3887 }
3888 
3889 static bool
is_black_surface_view(struct weston_view * view,struct weston_view ** fs_view)3890 is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
3891 {
3892 	struct weston_surface *surface = view->surface;
3893 
3894 	if (surface->committed == black_surface_committed) {
3895 		if (fs_view)
3896 			*fs_view = surface->committed_private;
3897 		return true;
3898 	}
3899 	return false;
3900 }
3901 
3902 static void
activate_binding(struct weston_seat * seat,struct desktop_shell * shell,struct weston_view * focus_view,uint32_t flags)3903 activate_binding(struct weston_seat *seat,
3904 		 struct desktop_shell *shell,
3905 		 struct weston_view *focus_view,
3906 		 uint32_t flags)
3907 {
3908 	struct weston_view *main_view;
3909 	struct weston_surface *main_surface;
3910 
3911 	if (!focus_view)
3912 		return;
3913 
3914 	if (is_black_surface_view(focus_view, &main_view))
3915 		focus_view = main_view;
3916 
3917 	main_surface = weston_surface_get_main_surface(focus_view->surface);
3918 	if (!get_shell_surface(main_surface))
3919 		return;
3920 
3921 	activate(shell, focus_view, seat, flags);
3922 }
3923 
3924 static void
click_to_activate_binding(struct weston_pointer * pointer,const struct timespec * time,uint32_t button,void * data)3925 click_to_activate_binding(struct weston_pointer *pointer,
3926 		          const struct timespec *time,
3927 			  uint32_t button, void *data)
3928 {
3929 	if (pointer->grab != &pointer->default_grab)
3930 		return;
3931 	if (pointer->focus == NULL)
3932 		return;
3933 
3934 	activate_binding(pointer->seat, data, pointer->focus,
3935 			 WESTON_ACTIVATE_FLAG_CLICKED |
3936 			 WESTON_ACTIVATE_FLAG_CONFIGURE);
3937 }
3938 
3939 static void
touch_to_activate_binding(struct weston_touch * touch,const struct timespec * time,void * data)3940 touch_to_activate_binding(struct weston_touch *touch,
3941 			  const struct timespec *time,
3942 			  void *data)
3943 {
3944 	if (touch->grab != &touch->default_grab)
3945 		return;
3946 	if (touch->focus == NULL)
3947 		return;
3948 
3949 	activate_binding(touch->seat, data, touch->focus,
3950 			 WESTON_ACTIVATE_FLAG_CONFIGURE);
3951 }
3952 
3953 static void
unfocus_all_seats(struct desktop_shell * shell)3954 unfocus_all_seats(struct desktop_shell *shell)
3955 {
3956 	struct weston_seat *seat, *next;
3957 
3958 	wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
3959 		struct weston_keyboard *keyboard =
3960 			weston_seat_get_keyboard(seat);
3961 
3962 		if (!keyboard)
3963 			continue;
3964 
3965 		weston_keyboard_set_focus(keyboard, NULL);
3966 	}
3967 }
3968 
3969 static void
lock(struct desktop_shell * shell)3970 lock(struct desktop_shell *shell)
3971 {
3972 	struct workspace *ws = get_current_workspace(shell);
3973 
3974 	if (shell->locked) {
3975 		weston_compositor_sleep(shell->compositor);
3976 		return;
3977 	}
3978 
3979 	shell->locked = true;
3980 
3981 	/* Hide all surfaces by removing the fullscreen, panel and
3982 	 * toplevel layers.  This way nothing else can show or receive
3983 	 * input events while we are locked. */
3984 
3985 	weston_layer_unset_position(&shell->panel_layer);
3986 	weston_layer_unset_position(&shell->fullscreen_layer);
3987 	if (shell->showing_input_panels)
3988 		weston_layer_unset_position(&shell->input_panel_layer);
3989 	weston_layer_unset_position(&ws->layer);
3990 
3991 	weston_layer_set_position(&shell->lock_layer,
3992 				  WESTON_LAYER_POSITION_LOCK);
3993 
3994 	weston_compositor_sleep(shell->compositor);
3995 
3996 	/* Remove the keyboard focus on all seats. This will be
3997 	 * restored to the workspace's saved state via
3998 	 * restore_focus_state when the compositor is unlocked */
3999 	unfocus_all_seats(shell);
4000 
4001 	/* TODO: disable bindings that should not work while locked. */
4002 
4003 	/* All this must be undone in resume_desktop(). */
4004 }
4005 
4006 static void
unlock(struct desktop_shell * shell)4007 unlock(struct desktop_shell *shell)
4008 {
4009 	struct wl_resource *shell_resource;
4010 
4011 	if (!shell->locked || shell->lock_surface) {
4012 		shell_fade(shell, FADE_IN);
4013 		return;
4014 	}
4015 
4016 	/* If desktop-shell client has gone away, unlock immediately. */
4017 	if (!shell->child.desktop_shell) {
4018 		resume_desktop(shell);
4019 		return;
4020 	}
4021 
4022 	if (shell->prepare_event_sent)
4023 		return;
4024 
4025 	shell_resource = shell->child.desktop_shell;
4026 	weston_desktop_shell_send_prepare_lock_surface(shell_resource);
4027 	shell->prepare_event_sent = true;
4028 }
4029 
4030 static void
shell_fade_done_for_output(struct weston_view_animation * animation,void * data)4031 shell_fade_done_for_output(struct weston_view_animation *animation, void *data)
4032 {
4033 	struct shell_output *shell_output = data;
4034 	struct desktop_shell *shell = shell_output->shell;
4035 
4036 	shell_output->fade.animation = NULL;
4037 	switch (shell_output->fade.type) {
4038 	case FADE_IN:
4039 		weston_surface_destroy(shell_output->fade.view->surface);
4040 		shell_output->fade.view = NULL;
4041 		break;
4042 	case FADE_OUT:
4043 		lock(shell);
4044 		break;
4045 	default:
4046 		break;
4047 	}
4048 }
4049 
4050 static struct weston_view *
shell_fade_create_surface_for_output(struct desktop_shell * shell,struct shell_output * shell_output)4051 shell_fade_create_surface_for_output(struct desktop_shell *shell, struct shell_output *shell_output)
4052 {
4053 	struct weston_compositor *compositor = shell->compositor;
4054 	struct weston_surface *surface;
4055 	struct weston_view *view;
4056 
4057 	surface = weston_surface_create(compositor);
4058 	if (!surface)
4059 		return NULL;
4060 
4061 	view = weston_view_create(surface);
4062 	if (!view) {
4063 		weston_surface_destroy(surface);
4064 		return NULL;
4065 	}
4066 
4067 	weston_surface_set_size(surface, shell_output->output->width, shell_output->output->height);
4068 	weston_view_set_position(view, shell_output->output->x, shell_output->output->y);
4069 	weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
4070 	weston_layer_entry_insert(&compositor->fade_layer.view_list,
4071 				  &view->layer_link);
4072 	pixman_region32_init(&surface->input);
4073 	surface->is_mapped = true;
4074 	view->is_mapped = true;
4075 
4076 	return view;
4077 }
4078 
4079 static void
shell_fade(struct desktop_shell * shell,enum fade_type type)4080 shell_fade(struct desktop_shell *shell, enum fade_type type)
4081 {
4082 	float tint;
4083 	struct shell_output *shell_output;
4084 
4085 	switch (type) {
4086 	case FADE_IN:
4087 		tint = 0.0;
4088 		break;
4089 	case FADE_OUT:
4090 		tint = 1.0;
4091 		break;
4092 	default:
4093 		weston_log("shell: invalid fade type\n");
4094 		return;
4095 	}
4096 
4097 	/* Create a separate fade surface for each output */
4098 	wl_list_for_each(shell_output, &shell->output_list, link) {
4099 		shell_output->fade.type = type;
4100 
4101 		if (shell_output->fade.view == NULL) {
4102 			shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4103 			if (!shell_output->fade.view)
4104 				continue;
4105 
4106 			shell_output->fade.view->alpha = 1.0 - tint;
4107 			weston_view_update_transform(shell_output->fade.view);
4108 		}
4109 
4110 		if (shell_output->fade.view->output == NULL) {
4111 			/* If the black view gets a NULL output, we lost the
4112 			 * last output and we'll just cancel the fade.  This
4113 			 * happens when you close the last window under the
4114 			 * X11 or Wayland backends. */
4115 			shell->locked = false;
4116 			weston_surface_destroy(shell_output->fade.view->surface);
4117 			shell_output->fade.view = NULL;
4118 		} else if (shell_output->fade.animation) {
4119 			weston_fade_update(shell_output->fade.animation, tint);
4120 		} else {
4121 			shell_output->fade.animation =
4122 				weston_fade_run(shell_output->fade.view,
4123 						1.0 - tint, tint, 300.0,
4124 						shell_fade_done_for_output, shell_output);
4125 		}
4126 	}
4127 }
4128 
4129 static void
do_shell_fade_startup(void * data)4130 do_shell_fade_startup(void *data)
4131 {
4132 	struct desktop_shell *shell = data;
4133 	struct shell_output *shell_output;
4134 
4135 	if (shell->startup_animation_type == ANIMATION_FADE) {
4136 		shell_fade(shell, FADE_IN);
4137 	} else {
4138 		weston_log("desktop shell: "
4139 			   "unexpected fade-in animation type %d\n",
4140 			   shell->startup_animation_type);
4141 		wl_list_for_each(shell_output, &shell->output_list, link) {
4142 			weston_surface_destroy(shell_output->fade.view->surface);
4143 			shell_output->fade.view = NULL;
4144 		}
4145 	}
4146 }
4147 
4148 static void
shell_fade_startup(struct desktop_shell * shell)4149 shell_fade_startup(struct desktop_shell *shell)
4150 {
4151 	struct wl_event_loop *loop;
4152 	struct shell_output *shell_output;
4153 	bool has_fade = false;
4154 
4155 	wl_list_for_each(shell_output, &shell->output_list, link) {
4156 		if (!shell_output->fade.startup_timer)
4157 			continue;
4158 
4159 		wl_event_source_remove(shell_output->fade.startup_timer);
4160 		shell_output->fade.startup_timer = NULL;
4161 		has_fade = true;
4162 	}
4163 
4164 	if (has_fade) {
4165 		loop = wl_display_get_event_loop(shell->compositor->wl_display);
4166 		wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4167 	}
4168 }
4169 
4170 static int
fade_startup_timeout(void * data)4171 fade_startup_timeout(void *data)
4172 {
4173 	struct desktop_shell *shell = data;
4174 
4175 	shell_fade_startup(shell);
4176 	return 0;
4177 }
4178 
4179 static void
shell_fade_init(struct desktop_shell * shell)4180 shell_fade_init(struct desktop_shell *shell)
4181 {
4182 	/* Make compositor output all black, and wait for the desktop-shell
4183 	 * client to signal it is ready, then fade in. The timer triggers a
4184 	 * fade-in, in case the desktop-shell client takes too long.
4185 	 */
4186 
4187 	struct wl_event_loop *loop;
4188 	struct shell_output *shell_output;
4189 
4190 	if (shell->startup_animation_type == ANIMATION_NONE)
4191 		return;
4192 
4193 	wl_list_for_each(shell_output, &shell->output_list, link) {
4194 		if (shell_output->fade.view != NULL) {
4195 			weston_log("%s: warning: fade surface already exists\n",
4196 				   __func__);
4197 			continue;
4198 		}
4199 
4200 		shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4201 		if (!shell_output->fade.view)
4202 			continue;
4203 
4204 		weston_view_update_transform(shell_output->fade.view);
4205 		weston_surface_damage(shell_output->fade.view->surface);
4206 
4207 		loop = wl_display_get_event_loop(shell->compositor->wl_display);
4208 		shell_output->fade.startup_timer =
4209 			wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4210 		wl_event_source_timer_update(shell_output->fade.startup_timer, 15000);
4211 	}
4212 }
4213 
4214 static void
idle_handler(struct wl_listener * listener,void * data)4215 idle_handler(struct wl_listener *listener, void *data)
4216 {
4217 	struct desktop_shell *shell =
4218 		container_of(listener, struct desktop_shell, idle_listener);
4219 
4220 	struct weston_seat *seat;
4221 
4222 	wl_list_for_each(seat, &shell->compositor->seat_list, link)
4223 		weston_seat_break_desktop_grabs(seat);
4224 
4225 	shell_fade(shell, FADE_OUT);
4226 	/* lock() is called from shell_fade_done_for_output() */
4227 }
4228 
4229 static void
wake_handler(struct wl_listener * listener,void * data)4230 wake_handler(struct wl_listener *listener, void *data)
4231 {
4232 	struct desktop_shell *shell =
4233 		container_of(listener, struct desktop_shell, wake_listener);
4234 
4235 	unlock(shell);
4236 }
4237 
4238 static void
transform_handler(struct wl_listener * listener,void * data)4239 transform_handler(struct wl_listener *listener, void *data)
4240 {
4241 	struct weston_surface *surface = data;
4242 	struct shell_surface *shsurf = get_shell_surface(surface);
4243 	const struct weston_xwayland_surface_api *api;
4244 	int x, y;
4245 
4246 	if (!shsurf)
4247 		return;
4248 
4249 	api = shsurf->shell->xwayland_surface_api;
4250 	if (!api) {
4251 		api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
4252 		shsurf->shell->xwayland_surface_api = api;
4253 	}
4254 
4255 	if (!api || !api->is_xwayland_surface(surface))
4256 		return;
4257 
4258 	if (!weston_view_is_mapped(shsurf->view))
4259 		return;
4260 
4261 	x = shsurf->view->geometry.x;
4262 	y = shsurf->view->geometry.y;
4263 
4264 	api->send_position(surface, x, y);
4265 }
4266 
4267 static void
center_on_output(struct weston_view * view,struct weston_output * output)4268 center_on_output(struct weston_view *view, struct weston_output *output)
4269 {
4270 	int32_t surf_x, surf_y, width, height;
4271 	float x, y;
4272 
4273 	if (!output) {
4274 		weston_view_set_position(view, 0, 0);
4275 		return;
4276 	}
4277 
4278 	surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
4279 
4280 	x = output->x + (output->width - width) / 2 - surf_x / 2;
4281 	y = output->y + (output->height - height) / 2 - surf_y / 2;
4282 
4283 	weston_view_set_position(view, x, y);
4284 }
4285 
4286 static void
weston_view_set_initial_position(struct weston_view * view,struct desktop_shell * shell)4287 weston_view_set_initial_position(struct weston_view *view,
4288 				 struct desktop_shell *shell)
4289 {
4290 	struct weston_compositor *compositor = shell->compositor;
4291 	int ix = 0, iy = 0;
4292 	int32_t range_x, range_y;
4293 	int32_t x, y;
4294 	struct weston_output *output, *target_output = NULL;
4295 	struct weston_seat *seat;
4296 	pixman_rectangle32_t area;
4297 
4298 	/* As a heuristic place the new window on the same output as the
4299 	 * pointer. Falling back to the output containing 0, 0.
4300 	 *
4301 	 * TODO: Do something clever for touch too?
4302 	 */
4303 	wl_list_for_each(seat, &compositor->seat_list, link) {
4304 		struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4305 
4306 		if (pointer) {
4307 			ix = wl_fixed_to_int(pointer->x);
4308 			iy = wl_fixed_to_int(pointer->y);
4309 			break;
4310 		}
4311 	}
4312 
4313 	wl_list_for_each(output, &compositor->output_list, link) {
4314 		if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4315 			target_output = output;
4316 			break;
4317 		}
4318 	}
4319 
4320 	if (!target_output) {
4321 		weston_view_set_position(view, 10 + random() % 400,
4322 					 10 + random() % 400);
4323 		return;
4324 	}
4325 
4326 	/* Valid range within output where the surface will still be onscreen.
4327 	 * If this is negative it means that the surface is bigger than
4328 	 * output.
4329 	 */
4330 	get_output_work_area(shell, target_output, &area);
4331 
4332 	x = area.x;
4333 	y = area.y;
4334 	range_x = area.width - view->surface->width;
4335 	range_y = area.height - view->surface->height;
4336 
4337 	if (range_x > 0)
4338 		x += random() % range_x;
4339 
4340 	if (range_y > 0)
4341 		y += random() % range_y;
4342 
4343 	weston_view_set_position(view, x, y);
4344 }
4345 
4346 static bool
check_desktop_shell_crash_too_early(struct desktop_shell * shell)4347 check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4348 {
4349 	struct timespec now;
4350 
4351 	if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4352 		return false;
4353 
4354 	/*
4355 	 * If the shell helper client dies before the session has been
4356 	 * up for roughly 30 seconds, better just make Weston shut down,
4357 	 * because the user likely has no way to interact with the desktop
4358 	 * anyway.
4359 	 */
4360 	if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4361 		weston_log("Error: %s apparently cannot run at all.\n",
4362 			   shell->client);
4363 		weston_log_continue(STAMP_SPACE "Quitting...");
4364 		weston_compositor_exit_with_code(shell->compositor,
4365 						 EXIT_FAILURE);
4366 
4367 		return true;
4368 	}
4369 
4370 	return false;
4371 }
4372 
4373 static void launch_desktop_shell_process(void *data);
4374 
4375 static void
respawn_desktop_shell_process(struct desktop_shell * shell)4376 respawn_desktop_shell_process(struct desktop_shell *shell)
4377 {
4378 	struct timespec time;
4379 
4380 	/* if desktop-shell dies more than 5 times in 30 seconds, give up */
4381 	weston_compositor_get_time(&time);
4382 	if (timespec_sub_to_msec(&time, &shell->child.deathstamp) > 30000) {
4383 		shell->child.deathstamp = time;
4384 		shell->child.deathcount = 0;
4385 	}
4386 
4387 	shell->child.deathcount++;
4388 	if (shell->child.deathcount > 5) {
4389 		weston_log("%s disconnected, giving up.\n", shell->client);
4390 		return;
4391 	}
4392 
4393 	weston_log("%s disconnected, respawning...\n", shell->client);
4394 	launch_desktop_shell_process(shell);
4395 }
4396 
4397 static void
desktop_shell_client_destroy(struct wl_listener * listener,void * data)4398 desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4399 {
4400 	struct desktop_shell *shell;
4401 
4402 	shell = container_of(listener, struct desktop_shell,
4403 			     child.client_destroy_listener);
4404 
4405 	wl_list_remove(&shell->child.client_destroy_listener.link);
4406 	shell->child.client = NULL;
4407 	/*
4408 	 * unbind_desktop_shell() will reset shell->child.desktop_shell
4409 	 * before the respawned process has a chance to create a new
4410 	 * desktop_shell object, because we are being called from the
4411 	 * wl_client destructor which destroys all wl_resources before
4412 	 * returning.
4413 	 */
4414 
4415 	if (!check_desktop_shell_crash_too_early(shell))
4416 		respawn_desktop_shell_process(shell);
4417 
4418 	shell_fade_startup(shell);
4419 }
4420 
4421 static void
launch_desktop_shell_process(void * data)4422 launch_desktop_shell_process(void *data)
4423 {
4424 	struct desktop_shell *shell = data;
4425 
4426 	shell->child.client = weston_client_start(shell->compositor,
4427 						  shell->client);
4428 
4429 	if (!shell->child.client) {
4430 		weston_log("not able to start %s\n", shell->client);
4431 		return;
4432 	}
4433 
4434 	shell->child.client_destroy_listener.notify =
4435 		desktop_shell_client_destroy;
4436 	wl_client_add_destroy_listener(shell->child.client,
4437 				       &shell->child.client_destroy_listener);
4438 }
4439 
4440 static void
unbind_desktop_shell(struct wl_resource * resource)4441 unbind_desktop_shell(struct wl_resource *resource)
4442 {
4443 	struct desktop_shell *shell = wl_resource_get_user_data(resource);
4444 
4445 	if (shell->locked)
4446 		resume_desktop(shell);
4447 
4448 	shell->child.desktop_shell = NULL;
4449 	shell->prepare_event_sent = false;
4450 }
4451 
4452 static void
bind_desktop_shell(struct wl_client * client,void * data,uint32_t version,uint32_t id)4453 bind_desktop_shell(struct wl_client *client,
4454 		   void *data, uint32_t version, uint32_t id)
4455 {
4456 	struct desktop_shell *shell = data;
4457 	struct wl_resource *resource;
4458 
4459 	resource = wl_resource_create(client, &weston_desktop_shell_interface,
4460 				      1, id);
4461 
4462 	if (client == shell->child.client) {
4463 		wl_resource_set_implementation(resource,
4464 					       &desktop_shell_implementation,
4465 					       shell, unbind_desktop_shell);
4466 		shell->child.desktop_shell = resource;
4467 		return;
4468 	}
4469 
4470 	wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4471 			       "permission to bind desktop_shell denied");
4472 }
4473 
4474 struct switcher {
4475 	struct desktop_shell *shell;
4476 	struct weston_view *current;
4477 	struct wl_listener listener;
4478 	struct weston_keyboard_grab grab;
4479 	struct wl_array minimized_array;
4480 };
4481 
4482 static void
switcher_next(struct switcher * switcher)4483 switcher_next(struct switcher *switcher)
4484 {
4485 	struct weston_view *view;
4486 	struct weston_view *first = NULL, *prev = NULL, *next = NULL;
4487 	struct shell_surface *shsurf;
4488 	struct workspace *ws = get_current_workspace(switcher->shell);
4489 
4490 	 /* temporary re-display minimized surfaces */
4491 	struct weston_view *tmp;
4492 	struct weston_view **minimized;
4493 	wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4494 		weston_layer_entry_remove(&view->layer_link);
4495 		weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
4496 		minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4497 		*minimized = view;
4498 	}
4499 
4500 	wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
4501 		shsurf = get_shell_surface(view->surface);
4502 		if (shsurf) {
4503 			if (first == NULL)
4504 				first = view;
4505 			if (prev == switcher->current)
4506 				next = view;
4507 			prev = view;
4508 			view->alpha = 0.25;
4509 			weston_view_geometry_dirty(view);
4510 			weston_surface_damage(view->surface);
4511 		}
4512 
4513 		if (is_black_surface_view(view, NULL)) {
4514 			view->alpha = 0.25;
4515 			weston_view_geometry_dirty(view);
4516 			weston_surface_damage(view->surface);
4517 		}
4518 	}
4519 
4520 	if (next == NULL)
4521 		next = first;
4522 
4523 	if (next == NULL)
4524 		return;
4525 
4526 	wl_list_remove(&switcher->listener.link);
4527 	wl_signal_add(&next->destroy_signal, &switcher->listener);
4528 
4529 	switcher->current = next;
4530 	wl_list_for_each(view, &next->surface->views, surface_link)
4531 		view->alpha = 1.0;
4532 
4533 	shsurf = get_shell_surface(switcher->current->surface);
4534 	if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
4535 		shsurf->fullscreen.black_view->alpha = 1.0;
4536 }
4537 
4538 static void
switcher_handle_view_destroy(struct wl_listener * listener,void * data)4539 switcher_handle_view_destroy(struct wl_listener *listener, void *data)
4540 {
4541 	struct switcher *switcher =
4542 		container_of(listener, struct switcher, listener);
4543 
4544 	switcher_next(switcher);
4545 }
4546 
4547 static void
switcher_destroy(struct switcher * switcher)4548 switcher_destroy(struct switcher *switcher)
4549 {
4550 	struct weston_view *view;
4551 	struct weston_keyboard *keyboard = switcher->grab.keyboard;
4552 	struct workspace *ws = get_current_workspace(switcher->shell);
4553 
4554 	wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
4555 		if (is_focus_view(view))
4556 			continue;
4557 
4558 		view->alpha = 1.0;
4559 		weston_surface_damage(view->surface);
4560 	}
4561 
4562 	if (switcher->current) {
4563 		activate(switcher->shell, switcher->current,
4564 			 keyboard->seat,
4565 			 WESTON_ACTIVATE_FLAG_CONFIGURE);
4566 	}
4567 
4568 	wl_list_remove(&switcher->listener.link);
4569 	weston_keyboard_end_grab(keyboard);
4570 	if (keyboard->input_method_resource)
4571 		keyboard->grab = &keyboard->input_method_grab;
4572 
4573 	 /* re-hide surfaces that were temporary shown during the switch */
4574 	struct weston_view **minimized;
4575 	wl_array_for_each(minimized, &switcher->minimized_array) {
4576 		/* with the exception of the current selected */
4577 		if ((*minimized)->surface != switcher->current->surface) {
4578 			weston_layer_entry_remove(&(*minimized)->layer_link);
4579 			weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
4580 			weston_view_damage_below(*minimized);
4581 		}
4582 	}
4583 	wl_array_release(&switcher->minimized_array);
4584 
4585 	free(switcher);
4586 }
4587 
4588 static void
switcher_key(struct weston_keyboard_grab * grab,const struct timespec * time,uint32_t key,uint32_t state_w)4589 switcher_key(struct weston_keyboard_grab *grab,
4590 	     const struct timespec *time, uint32_t key, uint32_t state_w)
4591 {
4592 	struct switcher *switcher = container_of(grab, struct switcher, grab);
4593 	enum wl_keyboard_key_state state = state_w;
4594 
4595 	if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
4596 		switcher_next(switcher);
4597 }
4598 
4599 static void
switcher_modifier(struct weston_keyboard_grab * grab,uint32_t serial,uint32_t mods_depressed,uint32_t mods_latched,uint32_t mods_locked,uint32_t group)4600 switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
4601 		  uint32_t mods_depressed, uint32_t mods_latched,
4602 		  uint32_t mods_locked, uint32_t group)
4603 {
4604 	struct switcher *switcher = container_of(grab, struct switcher, grab);
4605 	struct weston_seat *seat = grab->keyboard->seat;
4606 
4607 	if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4608 		switcher_destroy(switcher);
4609 }
4610 
4611 static void
switcher_cancel(struct weston_keyboard_grab * grab)4612 switcher_cancel(struct weston_keyboard_grab *grab)
4613 {
4614 	struct switcher *switcher = container_of(grab, struct switcher, grab);
4615 
4616 	switcher_destroy(switcher);
4617 }
4618 
4619 static const struct weston_keyboard_grab_interface switcher_grab = {
4620 	switcher_key,
4621 	switcher_modifier,
4622 	switcher_cancel,
4623 };
4624 
4625 static void
switcher_binding(struct weston_keyboard * keyboard,const struct timespec * time,uint32_t key,void * data)4626 switcher_binding(struct weston_keyboard *keyboard, const struct timespec *time,
4627 		 uint32_t key, void *data)
4628 {
4629 	struct desktop_shell *shell = data;
4630 	struct switcher *switcher;
4631 
4632 	switcher = malloc(sizeof *switcher);
4633 	if (!switcher)
4634 		return;
4635 
4636 	switcher->shell = shell;
4637 	switcher->current = NULL;
4638 	switcher->listener.notify = switcher_handle_view_destroy;
4639 	wl_list_init(&switcher->listener.link);
4640 	wl_array_init(&switcher->minimized_array);
4641 
4642 	lower_fullscreen_layer(switcher->shell, NULL);
4643 	switcher->grab.interface = &switcher_grab;
4644 	weston_keyboard_start_grab(keyboard, &switcher->grab);
4645 	weston_keyboard_set_focus(keyboard, NULL);
4646 	switcher_next(switcher);
4647 }
4648 
4649 static void
backlight_binding(struct weston_keyboard * keyboard,const struct timespec * time,uint32_t key,void * data)4650 backlight_binding(struct weston_keyboard *keyboard, const struct timespec *time,
4651 		  uint32_t key, void *data)
4652 {
4653 	struct weston_compositor *compositor = data;
4654 	struct weston_output *output;
4655 	long backlight_new = 0;
4656 
4657 	/* TODO: we're limiting to simple use cases, where we assume just
4658 	 * control on the primary display. We'd have to extend later if we
4659 	 * ever get support for setting backlights on random desktop LCD
4660 	 * panels though */
4661 	output = get_default_output(compositor);
4662 	if (!output)
4663 		return;
4664 
4665 	if (!output->set_backlight)
4666 		return;
4667 
4668 	if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4669 		backlight_new = output->backlight_current - 25;
4670 	else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4671 		backlight_new = output->backlight_current + 25;
4672 
4673 	if (backlight_new < 5)
4674 		backlight_new = 5;
4675 	if (backlight_new > 255)
4676 		backlight_new = 255;
4677 
4678 	output->backlight_current = backlight_new;
4679 	output->set_backlight(output, output->backlight_current);
4680 }
4681 
4682 static void
force_kill_binding(struct weston_keyboard * keyboard,const struct timespec * time,uint32_t key,void * data)4683 force_kill_binding(struct weston_keyboard *keyboard,
4684 		   const struct timespec *time, uint32_t key, void *data)
4685 {
4686 	struct weston_surface *focus_surface;
4687 	struct wl_client *client;
4688 	struct desktop_shell *shell = data;
4689 	struct weston_compositor *compositor = shell->compositor;
4690 	pid_t pid;
4691 
4692 	focus_surface = keyboard->focus;
4693 	if (!focus_surface)
4694 		return;
4695 
4696 	wl_signal_emit(&compositor->kill_signal, focus_surface);
4697 
4698 	client = wl_resource_get_client(focus_surface->resource);
4699 	wl_client_get_credentials(client, &pid, NULL, NULL);
4700 
4701 	/* Skip clients that we launched ourselves (the credentials of
4702 	 * the socketpair is ours) */
4703 	if (pid == getpid())
4704 		return;
4705 
4706 	kill(pid, SIGKILL);
4707 }
4708 
4709 static void
workspace_up_binding(struct weston_keyboard * keyboard,const struct timespec * time,uint32_t key,void * data)4710 workspace_up_binding(struct weston_keyboard *keyboard,
4711 		     const struct timespec *time, uint32_t key, void *data)
4712 {
4713 	struct desktop_shell *shell = data;
4714 	unsigned int new_index = shell->workspaces.current;
4715 
4716 	if (shell->locked)
4717 		return;
4718 	if (new_index != 0)
4719 		new_index--;
4720 
4721 	change_workspace(shell, new_index);
4722 }
4723 
4724 static void
workspace_down_binding(struct weston_keyboard * keyboard,const struct timespec * time,uint32_t key,void * data)4725 workspace_down_binding(struct weston_keyboard *keyboard,
4726 		       const struct timespec *time, uint32_t key, void *data)
4727 {
4728 	struct desktop_shell *shell = data;
4729 	unsigned int new_index = shell->workspaces.current;
4730 
4731 	if (shell->locked)
4732 		return;
4733 	if (new_index < shell->workspaces.num - 1)
4734 		new_index++;
4735 
4736 	change_workspace(shell, new_index);
4737 }
4738 
4739 static void
workspace_f_binding(struct weston_keyboard * keyboard,const struct timespec * time,uint32_t key,void * data)4740 workspace_f_binding(struct weston_keyboard *keyboard,
4741 		    const struct timespec *time, uint32_t key, void *data)
4742 {
4743 	struct desktop_shell *shell = data;
4744 	unsigned int new_index;
4745 
4746 	if (shell->locked)
4747 		return;
4748 	new_index = key - KEY_F1;
4749 	if (new_index >= shell->workspaces.num)
4750 		new_index = shell->workspaces.num - 1;
4751 
4752 	change_workspace(shell, new_index);
4753 }
4754 
4755 static void
workspace_move_surface_up_binding(struct weston_keyboard * keyboard,const struct timespec * time,uint32_t key,void * data)4756 workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
4757 				  const struct timespec *time, uint32_t key,
4758 				  void *data)
4759 {
4760 	struct desktop_shell *shell = data;
4761 	unsigned int new_index = shell->workspaces.current;
4762 
4763 	if (shell->locked)
4764 		return;
4765 
4766 	if (new_index != 0)
4767 		new_index--;
4768 
4769 	take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
4770 }
4771 
4772 static void
workspace_move_surface_down_binding(struct weston_keyboard * keyboard,const struct timespec * time,uint32_t key,void * data)4773 workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
4774 				    const struct timespec *time, uint32_t key,
4775 				    void *data)
4776 {
4777 	struct desktop_shell *shell = data;
4778 	unsigned int new_index = shell->workspaces.current;
4779 
4780 	if (shell->locked)
4781 		return;
4782 
4783 	if (new_index < shell->workspaces.num - 1)
4784 		new_index++;
4785 
4786 	take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
4787 }
4788 
4789 static void
shell_reposition_view_on_output_change(struct weston_view * view)4790 shell_reposition_view_on_output_change(struct weston_view *view)
4791 {
4792 	struct weston_output *output, *first_output;
4793 	struct weston_compositor *ec = view->surface->compositor;
4794 	struct shell_surface *shsurf;
4795 	float x, y;
4796 	int visible;
4797 
4798 	if (wl_list_empty(&ec->output_list))
4799 		return;
4800 
4801 	x = view->geometry.x;
4802 	y = view->geometry.y;
4803 
4804 	/* At this point the destroyed output is not in the list anymore.
4805 	 * If the view is still visible somewhere, we leave where it is,
4806 	 * otherwise, move it to the first output. */
4807 	visible = 0;
4808 	wl_list_for_each(output, &ec->output_list, link) {
4809 		if (pixman_region32_contains_point(&output->region,
4810 						   x, y, NULL)) {
4811 			visible = 1;
4812 			break;
4813 		}
4814 	}
4815 
4816 	if (!visible) {
4817 		first_output = container_of(ec->output_list.next,
4818 					    struct weston_output, link);
4819 
4820 		x = first_output->x + first_output->width / 4;
4821 		y = first_output->y + first_output->height / 4;
4822 
4823 		weston_view_set_position(view, x, y);
4824 	} else {
4825 		weston_view_geometry_dirty(view);
4826 	}
4827 
4828 
4829 	shsurf = get_shell_surface(view->surface);
4830 	if (!shsurf)
4831 		return;
4832 
4833 	shsurf->saved_position_valid = false;
4834 	set_maximized(shsurf, false);
4835 	set_fullscreen(shsurf, false, NULL);
4836 }
4837 
4838 void
shell_for_each_layer(struct desktop_shell * shell,shell_for_each_layer_func_t func,void * data)4839 shell_for_each_layer(struct desktop_shell *shell,
4840 		     shell_for_each_layer_func_t func, void *data)
4841 {
4842 	struct workspace **ws;
4843 
4844 	func(shell, &shell->fullscreen_layer, data);
4845 	func(shell, &shell->panel_layer, data);
4846 	func(shell, &shell->background_layer, data);
4847 	func(shell, &shell->lock_layer, data);
4848 	func(shell, &shell->input_panel_layer, data);
4849 
4850 	wl_array_for_each(ws, &shell->workspaces.array)
4851 		func(shell, &(*ws)->layer, data);
4852 }
4853 
4854 static void
shell_output_changed_move_layer(struct desktop_shell * shell,struct weston_layer * layer,void * data)4855 shell_output_changed_move_layer(struct desktop_shell *shell,
4856 				struct weston_layer *layer,
4857 				void *data)
4858 {
4859 	struct weston_view *view;
4860 
4861 	wl_list_for_each(view, &layer->view_list.link, layer_link.link)
4862 		shell_reposition_view_on_output_change(view);
4863 
4864 }
4865 
4866 static void
handle_output_destroy(struct wl_listener * listener,void * data)4867 handle_output_destroy(struct wl_listener *listener, void *data)
4868 {
4869 	struct shell_output *output_listener =
4870 		container_of(listener, struct shell_output, destroy_listener);
4871 	struct desktop_shell *shell = output_listener->shell;
4872 
4873 	shell_for_each_layer(shell, shell_output_changed_move_layer, NULL);
4874 
4875 	if (output_listener->panel_surface)
4876 		wl_list_remove(&output_listener->panel_surface_listener.link);
4877 	if (output_listener->background_surface)
4878 		wl_list_remove(&output_listener->background_surface_listener.link);
4879 	wl_list_remove(&output_listener->destroy_listener.link);
4880 	wl_list_remove(&output_listener->link);
4881 	free(output_listener);
4882 }
4883 
4884 static void
shell_resize_surface_to_output(struct desktop_shell * shell,struct weston_surface * surface,const struct weston_output * output)4885 shell_resize_surface_to_output(struct desktop_shell *shell,
4886 				struct weston_surface *surface,
4887 				const struct weston_output *output)
4888 {
4889 	if (!surface)
4890 		return;
4891 
4892 	weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4893 					surface->resource,
4894 					output->width,
4895 					output->height);
4896 }
4897 
4898 
4899 static void
handle_output_resized(struct wl_listener * listener,void * data)4900 handle_output_resized(struct wl_listener *listener, void *data)
4901 {
4902 	struct desktop_shell *shell =
4903 		container_of(listener, struct desktop_shell, resized_listener);
4904 	struct weston_output *output = (struct weston_output *)data;
4905 	struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4906 
4907 	shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4908 	shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4909 }
4910 
4911 static void
create_shell_output(struct desktop_shell * shell,struct weston_output * output)4912 create_shell_output(struct desktop_shell *shell,
4913 					struct weston_output *output)
4914 {
4915 	struct shell_output *shell_output;
4916 
4917 	shell_output = zalloc(sizeof *shell_output);
4918 	if (shell_output == NULL)
4919 		return;
4920 
4921 	shell_output->output = output;
4922 	shell_output->shell = shell;
4923 	shell_output->destroy_listener.notify = handle_output_destroy;
4924 	wl_signal_add(&output->destroy_signal,
4925 		      &shell_output->destroy_listener);
4926 	wl_list_insert(shell->output_list.prev, &shell_output->link);
4927 
4928 	if (wl_list_length(&shell->output_list) == 1)
4929 		shell_for_each_layer(shell,
4930 				     shell_output_changed_move_layer, NULL);
4931 }
4932 
4933 static void
handle_output_create(struct wl_listener * listener,void * data)4934 handle_output_create(struct wl_listener *listener, void *data)
4935 {
4936 	struct desktop_shell *shell =
4937 		container_of(listener, struct desktop_shell, output_create_listener);
4938 	struct weston_output *output = (struct weston_output *)data;
4939 
4940 	create_shell_output(shell, output);
4941 }
4942 
4943 static void
handle_output_move_layer(struct desktop_shell * shell,struct weston_layer * layer,void * data)4944 handle_output_move_layer(struct desktop_shell *shell,
4945 			 struct weston_layer *layer, void *data)
4946 {
4947 	struct weston_output *output = data;
4948 	struct weston_view *view;
4949 	float x, y;
4950 
4951 	wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
4952 		if (view->output != output)
4953 			continue;
4954 
4955 		x = view->geometry.x + output->move_x;
4956 		y = view->geometry.y + output->move_y;
4957 		weston_view_set_position(view, x, y);
4958 	}
4959 }
4960 
4961 static void
handle_output_move(struct wl_listener * listener,void * data)4962 handle_output_move(struct wl_listener *listener, void *data)
4963 {
4964 	struct desktop_shell *shell;
4965 
4966 	shell = container_of(listener, struct desktop_shell,
4967 			     output_move_listener);
4968 
4969 	shell_for_each_layer(shell, handle_output_move_layer, data);
4970 }
4971 
4972 static void
setup_output_destroy_handler(struct weston_compositor * ec,struct desktop_shell * shell)4973 setup_output_destroy_handler(struct weston_compositor *ec,
4974 							struct desktop_shell *shell)
4975 {
4976 	struct weston_output *output;
4977 
4978 	wl_list_init(&shell->output_list);
4979 	wl_list_for_each(output, &ec->output_list, link)
4980 		create_shell_output(shell, output);
4981 
4982 	shell->output_create_listener.notify = handle_output_create;
4983 	wl_signal_add(&ec->output_created_signal,
4984 				&shell->output_create_listener);
4985 
4986 	shell->output_move_listener.notify = handle_output_move;
4987 	wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
4988 }
4989 
4990 static void
shell_destroy(struct wl_listener * listener,void * data)4991 shell_destroy(struct wl_listener *listener, void *data)
4992 {
4993 	struct desktop_shell *shell =
4994 		container_of(listener, struct desktop_shell, destroy_listener);
4995 	struct workspace **ws;
4996 	struct shell_output *shell_output, *tmp;
4997 
4998 	/* Force state to unlocked so we don't try to fade */
4999 	shell->locked = false;
5000 
5001 	if (shell->child.client) {
5002 		/* disable respawn */
5003 		wl_list_remove(&shell->child.client_destroy_listener.link);
5004 		wl_client_destroy(shell->child.client);
5005 	}
5006 
5007 	wl_list_remove(&shell->destroy_listener.link);
5008 	wl_list_remove(&shell->idle_listener.link);
5009 	wl_list_remove(&shell->wake_listener.link);
5010 	wl_list_remove(&shell->transform_listener.link);
5011 
5012 // OHOS remove text_backend
5013 //	text_backend_destroy(shell->text_backend);
5014 	input_panel_destroy(shell);
5015 
5016 	wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5017 		wl_list_remove(&shell_output->destroy_listener.link);
5018 		wl_list_remove(&shell_output->link);
5019 		free(shell_output);
5020 	}
5021 
5022 	wl_list_remove(&shell->output_create_listener.link);
5023 	wl_list_remove(&shell->output_move_listener.link);
5024 	wl_list_remove(&shell->resized_listener.link);
5025 
5026 	wl_array_for_each(ws, &shell->workspaces.array)
5027 		workspace_destroy(*ws);
5028 	wl_array_release(&shell->workspaces.array);
5029 
5030 	free(shell->client);
5031 	free(shell);
5032 }
5033 
5034 static void
shell_add_bindings(struct weston_compositor * ec,struct desktop_shell * shell)5035 shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5036 {
5037 	uint32_t mod;
5038 	int i, num_workspace_bindings;
5039 
5040 	if (shell->allow_zap)
5041 		weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5042 					          MODIFIER_CTRL | MODIFIER_ALT,
5043 					          terminate_binding, ec);
5044 
5045 	/* fixed bindings */
5046 	weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5047 					     click_to_activate_binding,
5048 					     shell);
5049 	weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
5050 					     click_to_activate_binding,
5051 					     shell);
5052 	weston_compositor_add_touch_binding(ec, 0,
5053 					    touch_to_activate_binding,
5054 					    shell);
5055 	weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5056 				          backlight_binding, ec);
5057 	weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5058 				          backlight_binding, ec);
5059 
5060 	/* configurable bindings */
5061 	if (shell->exposay_modifier)
5062 		weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5063 						       exposay_binding, shell);
5064 
5065 	mod = shell->binding_modifier;
5066 	if (!mod)
5067 		return;
5068 
5069 	/* This binding is not configurable, but is only enabled if there is a
5070 	 * valid binding modifier. */
5071 	weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5072 				           MODIFIER_SUPER | MODIFIER_ALT,
5073 				           surface_opacity_binding, NULL);
5074 
5075 	weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5076 					   mod, zoom_axis_binding,
5077 					   NULL);
5078 
5079 	weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5080 					  zoom_key_binding, NULL);
5081 	weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5082 					  zoom_key_binding, NULL);
5083 	weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5084 					  maximize_binding, NULL);
5085 	weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5086 					  fullscreen_binding, NULL);
5087 	weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5088 					     shell);
5089 	weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
5090 	weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5091 					     resize_binding, shell);
5092 	weston_compositor_add_button_binding(ec, BTN_LEFT,
5093 					     mod | MODIFIER_SHIFT,
5094 					     resize_binding, shell);
5095 
5096 	if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5097 		weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5098 						     rotate_binding, NULL);
5099 
5100 	weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5101 					  shell);
5102 	weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5103 					  ec);
5104 	weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5105 					  ec);
5106 	weston_compositor_add_key_binding(ec, KEY_K, mod,
5107 				          force_kill_binding, shell);
5108 	weston_compositor_add_key_binding(ec, KEY_UP, mod,
5109 					  workspace_up_binding, shell);
5110 	weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5111 					  workspace_down_binding, shell);
5112 	weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5113 					  workspace_move_surface_up_binding,
5114 					  shell);
5115 	weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5116 					  workspace_move_surface_down_binding,
5117 					  shell);
5118 
5119 	/* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5120 	if (shell->workspaces.num > 1) {
5121 		num_workspace_bindings = shell->workspaces.num;
5122 		if (num_workspace_bindings > 6)
5123 			num_workspace_bindings = 6;
5124 		for (i = 0; i < num_workspace_bindings; i++)
5125 			weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5126 							  workspace_f_binding,
5127 							  shell);
5128 	}
5129 
5130 	weston_install_debug_key_binding(ec, mod);
5131 }
5132 
5133 static void
handle_seat_created(struct wl_listener * listener,void * data)5134 handle_seat_created(struct wl_listener *listener, void *data)
5135 {
5136 	struct weston_seat *seat = data;
5137 
5138 	create_shell_seat(seat);
5139 }
5140 
5141 WL_EXPORT int
wet_shell_init(struct weston_compositor * ec,int * argc,char * argv[])5142 wet_shell_init(struct weston_compositor *ec,
5143 	       int *argc, char *argv[])
5144 {
5145 	struct weston_seat *seat;
5146 	struct desktop_shell *shell;
5147 	struct workspace **pws;
5148 	unsigned int i;
5149 	struct wl_event_loop *loop;
5150 
5151 	shell = zalloc(sizeof *shell);
5152 	if (shell == NULL)
5153 		return -1;
5154 
5155 	shell->compositor = ec;
5156 
5157 	if (!weston_compositor_add_destroy_listener_once(ec,
5158 							 &shell->destroy_listener,
5159 							 shell_destroy)) {
5160 		free(shell);
5161 		return 0;
5162 	}
5163 
5164 	shell->idle_listener.notify = idle_handler;
5165 	wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5166 	shell->wake_listener.notify = wake_handler;
5167 	wl_signal_add(&ec->wake_signal, &shell->wake_listener);
5168 	shell->transform_listener.notify = transform_handler;
5169 	wl_signal_add(&ec->transform_signal, &shell->transform_listener);
5170 
5171 	weston_layer_init(&shell->fullscreen_layer, ec);
5172 	weston_layer_init(&shell->panel_layer, ec);
5173 	weston_layer_init(&shell->background_layer, ec);
5174 	weston_layer_init(&shell->lock_layer, ec);
5175 	weston_layer_init(&shell->input_panel_layer, ec);
5176 
5177 	weston_layer_set_position(&shell->fullscreen_layer,
5178 				  WESTON_LAYER_POSITION_FULLSCREEN);
5179 	weston_layer_set_position(&shell->panel_layer,
5180 				  WESTON_LAYER_POSITION_UI);
5181 	weston_layer_set_position(&shell->background_layer,
5182 				  WESTON_LAYER_POSITION_BACKGROUND);
5183 
5184 	wl_array_init(&shell->workspaces.array);
5185 	wl_list_init(&shell->workspaces.client_list);
5186 
5187 	if (input_panel_setup(shell) < 0)
5188 		return -1;
5189 
5190 // OHOS remove text_backend
5191 //	shell->text_backend = text_backend_init(ec);
5192 //	if (!shell->text_backend)
5193 //		return -1;
5194 
5195 	shell_configuration(shell);
5196 
5197 	shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5198 	shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5199 
5200 	for (i = 0; i < shell->workspaces.num; i++) {
5201 		pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5202 		if (pws == NULL)
5203 			return -1;
5204 
5205 		*pws = workspace_create(shell);
5206 		if (*pws == NULL)
5207 			return -1;
5208 	}
5209 	activate_workspace(shell, 0);
5210 
5211 	weston_layer_init(&shell->minimized_layer, ec);
5212 
5213 	wl_list_init(&shell->workspaces.anim_sticky_list);
5214 	wl_list_init(&shell->workspaces.animation.link);
5215 	shell->workspaces.animation.frame = animate_workspace_change_frame;
5216 
5217 	shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
5218 	if (!shell->desktop)
5219 		return -1;
5220 
5221 	if (wl_global_create(ec->wl_display,
5222 			     &weston_desktop_shell_interface, 1,
5223 			     shell, bind_desktop_shell) == NULL)
5224 		return -1;
5225 
5226 	weston_compositor_get_time(&shell->child.deathstamp);
5227 
5228 	shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
5229 
5230 	setup_output_destroy_handler(ec, shell);
5231 
5232 	loop = wl_display_get_event_loop(ec->wl_display);
5233 // OHOS
5234 //	wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
5235 
5236 	wl_list_for_each(seat, &ec->seat_list, link)
5237 		handle_seat_created(NULL, seat);
5238 	shell->seat_create_listener.notify = handle_seat_created;
5239 	wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
5240 
5241 	shell->resized_listener.notify = handle_output_resized;
5242 	wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
5243 
5244 	screenshooter_create(ec);
5245 
5246 	shell_add_bindings(ec, shell);
5247 
5248 	shell_fade_init(shell);
5249 
5250 	clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
5251 
5252 	return 0;
5253 }
5254