blob: 2d6d7c20395b1c285763644ef0d63ad7a1bb7548 [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Daniel Stonedf8133b2013-11-19 11:37:14 +01004 * Copyright © 2013 Raspberry Pi Foundation
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005 *
Bryce Harringtonaf637c22015-06-11 12:55:55 -07006 * 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:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050012 *
Bryce Harringtonaf637c22015-06-11 12:55:55 -070013 * 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.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050024 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
27
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050028#include <stdlib.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030029#include <stdint.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040030#include <stdio.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050031#include <string.h>
32#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040033#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020034#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020035#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020036#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040037#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038
Kristian Høgsberg1ef23132013-12-04 00:20:01 -080039#include "shell.h"
Pekka Paalanen58f98c92016-06-03 16:45:21 +030040#include "compositor/weston.h"
Jonas Ådahl6d6fb612015-11-17 16:00:33 +080041#include "weston-desktop-shell-server-protocol.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070042#include "shared/config-parser.h"
Jon Cruzd618f682015-06-15 15:37:09 -070043#include "shared/helpers.h"
Alexandros Frantzis8250a612017-11-16 18:20:52 +020044#include "shared/timespec-util.h"
Quentin Glidic8f9d90a2016-08-12 10:41:36 +020045#include "libweston-desktop/libweston-desktop.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050046
Jonas Ådahle3cddce2012-06-13 00:01:22 +020047#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020048#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020049
Jonas Ådahl04769742012-06-13 00:01:24 +020050struct focus_state {
Quentin Glidicfff39812016-08-15 10:56:52 +020051 struct desktop_shell *shell;
Jonas Ådahl04769742012-06-13 00:01:24 +020052 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040053 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020054 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
Philip Withnall648a4dd2013-11-25 18:01:44 +000060/*
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
Manuel Bachmann805d2f52014-03-05 12:21:34 +010069 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000070 * • Fullscreen layer
71 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000072 * • 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
Pekka Paalanen56cdea92011-11-23 16:14:12 +020098struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -050099 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200100
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200101 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500102 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600103 int32_t last_width, last_height;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700104
Tiago Vignattibe143262012-04-16 17:31:41 +0300105 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200106
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500107 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800108 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800109 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700110 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800111 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100112
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500113 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200114 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500115 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200116 } rotation;
117
118 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800119 struct weston_transform transform; /* matrix from x, y */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500120 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800121 } fullscreen;
122
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200123 struct weston_transform workspace_transform;
124
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500125 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500126 struct weston_output *output;
Semi Malinen99f8c082018-05-02 11:10:32 +0200127 struct wl_listener output_destroy_listener;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200128
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700129 struct surface_state {
Quentin Glidic18a81ac2016-08-16 14:26:03 +0200130 bool fullscreen;
131 bool maximized;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100132 bool lowered;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200133 } state;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700134
Pekka Paalanen77a6d952016-11-16 15:17:14 +0200135 struct {
136 bool is_set;
137 int32_t x;
138 int32_t y;
139 } xwayland;
140
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500141 int focus_count;
Derek Foreman039e9be2015-04-14 17:09:06 -0500142
143 bool destroying;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200144};
145
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300146struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400147 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300148 struct shell_surface *shsurf;
149 struct wl_listener shsurf_destroy_listener;
150};
151
Rusty Lynch1084da52013-08-15 09:10:08 -0700152struct shell_touch_grab {
153 struct weston_touch_grab grab;
154 struct shell_surface *shsurf;
155 struct wl_listener shsurf_destroy_listener;
156 struct weston_touch *touch;
157};
158
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300159struct weston_move_grab {
160 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100161 wl_fixed_t dx, dy;
Derek Foremancf7d95a2015-06-03 15:53:22 -0500162 bool client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500163};
164
Rusty Lynch1084da52013-08-15 09:10:08 -0700165struct weston_touch_move_grab {
166 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800167 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700168 wl_fixed_t dx, dy;
169};
170
Pekka Paalanen460099f2012-01-20 16:48:25 +0200171struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300172 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500173 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200174 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200175 float x;
176 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200177 } center;
178};
179
Giulio Camuffo5085a752013-03-25 21:42:45 +0100180struct shell_seat {
181 struct weston_seat *seat;
182 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500183 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100184
Jason Ekstrand024177c2014-04-21 19:42:58 -0500185 struct wl_listener caps_changed_listener;
186 struct wl_listener pointer_focus_listener;
187 struct wl_listener keyboard_focus_listener;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100188};
189
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800190
Alex Wubd3354b2012-04-17 17:20:49 +0800191static struct desktop_shell *
192shell_surface_get_shell(struct shell_surface *shsurf);
193
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500194static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200195set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
196
197static void
Derek Foreman74de4692015-07-15 13:00:39 -0500198surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500199
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300200static void
201shell_fade_startup(struct desktop_shell *shell);
202
Bryce Harrington9ad4de12016-09-09 13:16:02 -0700203static void
204shell_fade(struct desktop_shell *shell, enum fade_type type);
205
Philip Withnallbecb77e2013-11-25 18:01:30 +0000206static struct shell_seat *
207get_shell_seat(struct weston_seat *seat);
208
Jonny Lambd73c6942014-08-20 15:53:20 +0200209static void
210get_output_panel_size(struct desktop_shell *shell,
211 struct weston_output *output,
212 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700213
Philip Withnall648a4dd2013-11-25 18:01:44 +0000214static void
215shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
216
Pekka Paalanen8274d902014-08-06 19:36:51 +0300217static int
218shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
219{
Pekka Paalanen8274d902014-08-06 19:36:51 +0300220 const char *t, *c;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200221 struct weston_desktop_surface *desktop_surface =
222 weston_surface_get_desktop_surface(surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300223
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200224 t = weston_desktop_surface_get_title(desktop_surface);
225 c = weston_desktop_surface_get_app_id(desktop_surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300226
227 return snprintf(buf, len, "%s window%s%s%s%s%s",
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200228 "top-level",
Pekka Paalanen8274d902014-08-06 19:36:51 +0300229 t ? " '" : "", t ?: "", t ? "'" : "",
230 c ? " of " : "", c ?: "");
231}
232
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500233static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400234destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300235{
236 struct shell_grab *grab;
237
238 grab = container_of(listener, struct shell_grab,
239 shsurf_destroy_listener);
240
241 grab->shsurf = NULL;
242}
243
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800244struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500245get_default_view(struct weston_surface *surface)
246{
247 struct shell_surface *shsurf;
248 struct weston_view *view;
249
250 if (!surface || wl_list_empty(&surface->views))
251 return NULL;
252
253 shsurf = get_shell_surface(surface);
254 if (shsurf)
255 return shsurf->view;
256
257 wl_list_for_each(view, &surface->views, surface_link)
258 if (weston_view_is_mapped(view))
259 return view;
260
261 return container_of(surface->views.next, struct weston_view, surface_link);
262}
263
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300264static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300265shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400266 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300267 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400268 struct weston_pointer *pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800269 enum weston_desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300270{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300271 struct desktop_shell *shell = shsurf->shell;
272
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200273 weston_seat_break_desktop_grabs(pointer->seat);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400274
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300275 grab->grab.interface = interface;
276 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400277 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500278 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400279 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300280
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700281 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400282 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400283 if (shell->child.desktop_shell) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800284 weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400285 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500286 weston_pointer_set_focus(pointer,
287 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400288 wl_fixed_from_int(0),
289 wl_fixed_from_int(0));
290 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300291}
292
Jonny Lambd73c6942014-08-20 15:53:20 +0200293static void
Quentin Glidic581df062016-06-23 18:55:19 +0200294get_panel_size(struct desktop_shell *shell,
295 struct weston_view *view,
296 int *width,
297 int *height)
298{
299 float x1, y1;
300 float x2, y2;
301 weston_view_to_global_float(view, 0, 0, &x1, &y1);
302 weston_view_to_global_float(view,
303 view->surface->width,
304 view->surface->height,
305 &x2, &y2);
306 *width = (int)(x2 - x1);
307 *height = (int)(y2 - y1);
308}
309
310static void
Jonny Lambd73c6942014-08-20 15:53:20 +0200311get_output_panel_size(struct desktop_shell *shell,
312 struct weston_output *output,
313 int *width,
314 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700315{
316 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200317
318 *width = 0;
319 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700320
321 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200322 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700323
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300324 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Quentin Glidic581df062016-06-23 18:55:19 +0200325 if (view->surface->output == output) {
326 get_panel_size(shell, view, width, height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200327 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700328 }
329 }
330
Jonny Lambd73c6942014-08-20 15:53:20 +0200331 /* the correct view wasn't found */
332}
333
334static void
Quentin Glidicfff39812016-08-15 10:56:52 +0200335get_output_work_area(struct desktop_shell *shell,
Jonny Lambd73c6942014-08-20 15:53:20 +0200336 struct weston_output *output,
337 pixman_rectangle32_t *area)
338{
339 int32_t panel_width = 0, panel_height = 0;
340
Pekka Paalanen99372ba2018-05-02 10:21:55 +0200341 if (!output) {
342 area->x = 0;
343 area->y = 0;
344 area->width = 0;
345 area->height = 0;
346
347 return;
348 }
349
Derek Foremanf814c5d2015-04-15 12:23:54 -0500350 area->x = output->x;
351 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200352
353 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200354 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800355 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
Jonny Lambd73c6942014-08-20 15:53:20 +0200356 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500357 area->y += panel_height;
Daniel Stone2ef9b1a2017-03-13 16:31:36 +0000358 /* fallthrough */
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800359 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200360 area->width = output->width;
361 area->height = output->height - panel_height;
362 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800363 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500364 area->x += panel_width;
Daniel Stone2ef9b1a2017-03-13 16:31:36 +0000365 /* fallthrough */
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800366 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200367 area->width = output->width - panel_width;
368 area->height = output->height;
369 break;
370 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700371}
372
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400373static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300374shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300375{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700376 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400377 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700378 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400379
380 if (grab->shsurf->resize_edges) {
381 grab->shsurf->resize_edges = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400382 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700383 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300384
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700385 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300386}
387
388static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700389shell_touch_grab_start(struct shell_touch_grab *grab,
390 const struct weston_touch_grab_interface *interface,
391 struct shell_surface *shsurf,
392 struct weston_touch *touch)
393{
394 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800395
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200396 weston_seat_break_desktop_grabs(touch->seat);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700397
Rusty Lynch1084da52013-08-15 09:10:08 -0700398 grab->grab.interface = interface;
399 grab->shsurf = shsurf;
400 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
401 wl_signal_add(&shsurf->destroy_signal,
402 &grab->shsurf_destroy_listener);
403
404 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700405 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700406
407 weston_touch_start_grab(touch, &grab->grab);
408 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500409 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500410 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700411}
412
413static void
414shell_touch_grab_end(struct shell_touch_grab *grab)
415{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700416 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700417 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700418 grab->shsurf->grabbed = 0;
419 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700420
421 weston_touch_end_grab(grab->touch);
422}
423
424static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500425center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800426 struct weston_output *output);
427
Daniel Stone496ca172012-05-30 16:31:42 +0100428static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300429get_modifier(char *modifier)
430{
431 if (!modifier)
432 return MODIFIER_SUPER;
433
434 if (!strcmp("ctrl", modifier))
435 return MODIFIER_CTRL;
436 else if (!strcmp("alt", modifier))
437 return MODIFIER_ALT;
438 else if (!strcmp("super", modifier))
439 return MODIFIER_SUPER;
Bob Ham553d1242016-01-12 10:21:49 +0000440 else if (!strcmp("none", modifier))
441 return 0;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300442 else
443 return MODIFIER_SUPER;
444}
445
Juan Zhaoe10d2792012-04-25 19:09:52 +0800446static enum animation_type
447get_animation_type(char *animation)
448{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800449 if (!animation)
450 return ANIMATION_NONE;
451
Juan Zhaoe10d2792012-04-25 19:09:52 +0800452 if (!strcmp("zoom", animation))
453 return ANIMATION_ZOOM;
454 else if (!strcmp("fade", animation))
455 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100456 else if (!strcmp("dim-layer", animation))
457 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800458 else
459 return ANIMATION_NONE;
460}
461
Alex Wu4539b082012-03-01 12:57:46 +0800462static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400463shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200464{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400465 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500466 char *s, *client;
Bob Ham744e6532016-01-12 10:21:48 +0000467 int allow_zap;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200468
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300469 section = weston_config_get_section(wet_get_config(shell->compositor),
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400470 "shell", NULL, NULL);
Marius Vlad64fbd0f2018-12-15 15:51:57 +0200471 client = wet_get_libexec_path(WESTON_SHELL_CLIENT);
Daniel Stonee03c1112016-11-24 20:45:45 +0000472 weston_config_section_get_string(section, "client", &s, client);
Derek Foremanc7210432014-08-21 11:32:38 -0500473 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100474 shell->client = s;
Bob Ham744e6532016-01-12 10:21:48 +0000475
476 weston_config_section_get_bool(section,
477 "allow-zap", &allow_zap, true);
478 shell->allow_zap = allow_zap;
479
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100480 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400481 "binding-modifier", &s, "super");
482 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200483 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800484
485 weston_config_section_get_string(section,
486 "exposay-modifier", &s, "none");
Bob Ham553d1242016-01-12 10:21:49 +0000487 shell->exposay_modifier = get_modifier(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800488 free(s);
489
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400490 weston_config_section_get_string(section, "animation", &s, "none");
491 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200492 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200493 weston_config_section_get_string(section, "close-animation", &s, "fade");
494 shell->win_close_animation_type = get_animation_type(s);
495 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700496 weston_config_section_get_string(section,
497 "startup-animation", &s, "fade");
498 shell->startup_animation_type = get_animation_type(s);
499 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700500 if (shell->startup_animation_type == ANIMATION_ZOOM)
501 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100502 weston_config_section_get_string(section, "focus-animation", &s, "none");
503 shell->focus_animation_type = get_animation_type(s);
504 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400505 weston_config_section_get_uint(section, "num-workspaces",
506 &shell->workspaces.num,
507 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200508}
509
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800510struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100511get_default_output(struct weston_compositor *compositor)
512{
Armin Krezović10b06182016-06-23 11:59:30 +0200513 if (wl_list_empty(&compositor->output_list))
514 return NULL;
515
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100516 return container_of(compositor->output_list.next,
517 struct weston_output, link);
518}
519
Pekka Paalanen8274d902014-08-06 19:36:51 +0300520static int
521focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
522{
523 return snprintf(buf, len, "focus highlight effect for output %s",
524 surface->output->name);
525}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100526
527/* no-op func for checking focus surface */
528static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200529focus_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100530{
531}
532
533static struct focus_surface *
534get_focus_surface(struct weston_surface *surface)
535{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200536 if (surface->committed == focus_surface_committed)
537 return surface->committed_private;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100538 else
539 return NULL;
540}
541
542static bool
543is_focus_surface (struct weston_surface *es)
544{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200545 return (es->committed == focus_surface_committed);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100546}
547
548static bool
549is_focus_view (struct weston_view *view)
550{
551 return is_focus_surface (view->surface);
552}
553
554static struct focus_surface *
555create_focus_surface(struct weston_compositor *ec,
556 struct weston_output *output)
557{
558 struct focus_surface *fsurf = NULL;
559 struct weston_surface *surface = NULL;
560
561 fsurf = malloc(sizeof *fsurf);
562 if (!fsurf)
563 return NULL;
564
565 fsurf->surface = weston_surface_create(ec);
566 surface = fsurf->surface;
567 if (surface == NULL) {
568 free(fsurf);
569 return NULL;
570 }
571
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200572 surface->committed = focus_surface_committed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100573 surface->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200574 surface->is_mapped = true;
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200575 surface->committed_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300576 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100577
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800578 fsurf->view = weston_view_create(surface);
579 if (fsurf->view == NULL) {
580 weston_surface_destroy(surface);
581 free(fsurf);
582 return NULL;
583 }
Semi Malinene7a52fb2018-04-26 11:08:10 +0200584 weston_view_set_output(fsurf->view, output);
Armin Krezović4663aca2016-06-30 06:04:29 +0200585 fsurf->view->is_mapped = true;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100586
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600587 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600588 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100589 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
590 pixman_region32_fini(&surface->opaque);
591 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
592 output->width, output->height);
593 pixman_region32_fini(&surface->input);
594 pixman_region32_init(&surface->input);
595
596 wl_list_init(&fsurf->workspace_transform.link);
597
598 return fsurf;
599}
600
601static void
602focus_surface_destroy(struct focus_surface *fsurf)
603{
604 weston_surface_destroy(fsurf->surface);
605 free(fsurf);
606}
607
608static void
609focus_animation_done(struct weston_view_animation *animation, void *data)
610{
611 struct workspace *ws = data;
612
613 ws->focus_animation = NULL;
614}
615
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200616static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200617focus_state_destroy(struct focus_state *state)
618{
619 wl_list_remove(&state->seat_destroy_listener.link);
620 wl_list_remove(&state->surface_destroy_listener.link);
621 free(state);
622}
623
624static void
625focus_state_seat_destroy(struct wl_listener *listener, void *data)
626{
627 struct focus_state *state = container_of(listener,
628 struct focus_state,
629 seat_destroy_listener);
630
631 wl_list_remove(&state->link);
632 focus_state_destroy(state);
633}
634
635static void
636focus_state_surface_destroy(struct wl_listener *listener, void *data)
637{
638 struct focus_state *state = container_of(listener,
639 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400640 surface_destroy_listener);
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200641 struct weston_surface *main_surface;
642 struct weston_view *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500643 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200644
Pekka Paalanen01388e22013-04-25 13:57:44 +0300645 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
646
Kristian Høgsberge3778222012-07-31 17:29:30 -0400647 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300648 wl_list_for_each(view,
649 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500650 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400651 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100652 if (is_focus_view(view))
653 continue;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200654 if (!get_shell_surface(view->surface))
655 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400656
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200657 next = view;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400658 break;
659 }
660
Pekka Paalanen01388e22013-04-25 13:57:44 +0300661 /* if the focus was a sub-surface, activate its main surface */
662 if (main_surface != state->keyboard_focus)
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200663 next = get_default_view(main_surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300664
Kristian Høgsberge3778222012-07-31 17:29:30 -0400665 if (next) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100666 state->keyboard_focus = NULL;
Quentin Glidicfff39812016-08-15 10:56:52 +0200667 activate(state->shell, next, state->seat,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +0200668 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400669 } else {
Quentin Glidicfff39812016-08-15 10:56:52 +0200670 if (state->shell->focus_animation_type == ANIMATION_DIM_LAYER) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100671 if (state->ws->focus_animation)
672 weston_view_animation_destroy(state->ws->focus_animation);
673
674 state->ws->focus_animation = weston_fade_run(
675 state->ws->fsurf_front->view,
676 state->ws->fsurf_front->view->alpha, 0.0, 300,
677 focus_animation_done, state->ws);
678 }
679
Kristian Høgsberge3778222012-07-31 17:29:30 -0400680 wl_list_remove(&state->link);
681 focus_state_destroy(state);
682 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200683}
684
685static struct focus_state *
Quentin Glidicfff39812016-08-15 10:56:52 +0200686focus_state_create(struct desktop_shell *shell, struct weston_seat *seat,
687 struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200688{
Jonas Ådahl04769742012-06-13 00:01:24 +0200689 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200690
691 state = malloc(sizeof *state);
692 if (state == NULL)
693 return NULL;
694
Quentin Glidicfff39812016-08-15 10:56:52 +0200695 state->shell = shell;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100696 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400697 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200698 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400699 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200700
701 state->seat_destroy_listener.notify = focus_state_seat_destroy;
702 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400703 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200704 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400705 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200706
707 return state;
708}
709
Jonas Ådahl8538b222012-08-29 22:13:03 +0200710static struct focus_state *
711ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
712{
713 struct workspace *ws = get_current_workspace(shell);
714 struct focus_state *state;
715
716 wl_list_for_each(state, &ws->focus_list, link)
717 if (state->seat == seat)
718 break;
719
720 if (&state->link == &ws->focus_list)
Quentin Glidicfff39812016-08-15 10:56:52 +0200721 state = focus_state_create(shell, seat, ws);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200722
723 return state;
724}
725
Jonas Ådahl04769742012-06-13 00:01:24 +0200726static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800727focus_state_set_focus(struct focus_state *state,
728 struct weston_surface *surface)
729{
730 if (state->keyboard_focus) {
731 wl_list_remove(&state->surface_destroy_listener.link);
732 wl_list_init(&state->surface_destroy_listener.link);
733 }
734
735 state->keyboard_focus = surface;
736 if (surface)
737 wl_signal_add(&surface->destroy_signal,
738 &state->surface_destroy_listener);
739}
740
741static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400742restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200743{
744 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400745 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100746 struct wl_list pending_seat_list;
747 struct weston_seat *seat, *next_seat;
748
749 /* Temporarily steal the list of seats so that we can keep
750 * track of the seats we've already processed */
751 wl_list_init(&pending_seat_list);
752 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
753 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200754
755 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500756 struct weston_keyboard *keyboard =
757 weston_seat_get_keyboard(state->seat);
758
Neil Roberts4237f502014-04-09 16:33:31 +0100759 wl_list_remove(&state->seat->link);
760 wl_list_insert(&shell->compositor->seat_list,
761 &state->seat->link);
762
Derek Foreman1281a362015-07-31 16:55:32 -0500763 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800764 continue;
765
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400766 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200767
Derek Foreman1281a362015-07-31 16:55:32 -0500768 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200769 }
Neil Roberts4237f502014-04-09 16:33:31 +0100770
771 /* For any remaining seats that we don't have a focus state
772 * for we'll reset the keyboard focus to NULL */
773 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500774 struct weston_keyboard *keyboard =
775 weston_seat_get_keyboard(seat);
776
Neil Roberts4237f502014-04-09 16:33:31 +0100777 wl_list_insert(&shell->compositor->seat_list, &seat->link);
778
Derek Foreman1281a362015-07-31 16:55:32 -0500779 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100780 continue;
781
Derek Foreman1281a362015-07-31 16:55:32 -0500782 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100783 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200784}
785
786static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200787replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
788 struct weston_seat *seat)
789{
Derek Foreman1281a362015-07-31 16:55:32 -0500790 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200791 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200792
793 wl_list_for_each(state, &ws->focus_list, link) {
794 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500795 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200796 return;
797 }
798 }
799}
800
801static void
802drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
803 struct weston_surface *surface)
804{
805 struct focus_state *state;
806
807 wl_list_for_each(state, &ws->focus_list, link)
808 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800809 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200810}
811
812static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100813animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
814 struct weston_view *from, struct weston_view *to)
815{
816 struct weston_output *output;
817 bool focus_surface_created = false;
818
819 /* FIXME: Only support dim animation using two layers */
820 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
821 return;
822
823 output = get_default_output(shell->compositor);
824 if (ws->fsurf_front == NULL && (from || to)) {
825 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800826 if (ws->fsurf_front == NULL)
827 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100828 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800829
830 ws->fsurf_back = create_focus_surface(shell->compositor, output);
831 if (ws->fsurf_back == NULL) {
832 focus_surface_destroy(ws->fsurf_front);
833 return;
834 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100835 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800836
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100837 focus_surface_created = true;
838 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300839 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
840 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100841 }
842
843 if (ws->focus_animation) {
844 weston_view_animation_destroy(ws->focus_animation);
845 ws->focus_animation = NULL;
846 }
847
848 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300849 weston_layer_entry_insert(&to->layer_link,
850 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100851 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300852 weston_layer_entry_insert(&ws->layer.view_list,
853 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100854
855 if (focus_surface_created) {
856 ws->focus_animation = weston_fade_run(
857 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200858 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100859 focus_animation_done, ws);
860 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300861 weston_layer_entry_insert(&from->layer_link,
862 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100863 ws->focus_animation = weston_stable_fade_run(
864 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200865 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100866 focus_animation_done, ws);
867 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300868 weston_layer_entry_insert(&ws->layer.view_list,
869 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100870 ws->focus_animation = weston_stable_fade_run(
871 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200872 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100873 focus_animation_done, ws);
874 }
875}
876
877static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200878workspace_destroy(struct workspace *ws)
879{
Jonas Ådahl04769742012-06-13 00:01:24 +0200880 struct focus_state *state, *next;
881
882 wl_list_for_each_safe(state, next, &ws->focus_list, link)
883 focus_state_destroy(state);
884
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100885 if (ws->fsurf_front)
886 focus_surface_destroy(ws->fsurf_front);
887 if (ws->fsurf_back)
888 focus_surface_destroy(ws->fsurf_back);
889
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200890 free(ws);
891}
892
Jonas Ådahl04769742012-06-13 00:01:24 +0200893static void
894seat_destroyed(struct wl_listener *listener, void *data)
895{
896 struct weston_seat *seat = data;
897 struct focus_state *state, *next;
898 struct workspace *ws = container_of(listener,
899 struct workspace,
900 seat_destroyed_listener);
901
902 wl_list_for_each_safe(state, next, &ws->focus_list, link)
903 if (state->seat == seat)
904 wl_list_remove(&state->link);
905}
906
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200907static struct workspace *
Quentin Glidic82681572016-12-17 13:40:51 +0100908workspace_create(struct desktop_shell *shell)
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200909{
910 struct workspace *ws = malloc(sizeof *ws);
911 if (ws == NULL)
912 return NULL;
913
Quentin Glidic82681572016-12-17 13:40:51 +0100914 weston_layer_init(&ws->layer, shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200915
Jonas Ådahl04769742012-06-13 00:01:24 +0200916 wl_list_init(&ws->focus_list);
917 wl_list_init(&ws->seat_destroyed_listener.link);
918 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100919 ws->fsurf_front = NULL;
920 ws->fsurf_back = NULL;
921 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200922
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200923 return ws;
924}
925
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200926static int
927workspace_is_empty(struct workspace *ws)
928{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300929 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200930}
931
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200932static struct workspace *
933get_workspace(struct desktop_shell *shell, unsigned int index)
934{
935 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200936 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200937 pws += index;
938 return *pws;
939}
940
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800941struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200942get_current_workspace(struct desktop_shell *shell)
943{
944 return get_workspace(shell, shell->workspaces.current);
945}
946
947static void
948activate_workspace(struct desktop_shell *shell, unsigned int index)
949{
950 struct workspace *ws;
951
952 ws = get_workspace(shell, index);
Quentin Glidic82681572016-12-17 13:40:51 +0100953 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200954
955 shell->workspaces.current = index;
956}
957
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200958static unsigned int
959get_output_height(struct weston_output *output)
960{
961 return abs(output->region.extents.y1 - output->region.extents.y2);
962}
963
Greg Vf57774e2018-07-24 23:21:55 +0300964static struct weston_transform *
965view_get_transform(struct weston_view *view)
966{
967 struct focus_surface *fsurf = NULL;
968 struct shell_surface *shsurf = NULL;
969
970 if (is_focus_view(view)) {
971 fsurf = get_focus_surface(view->surface);
972 return &fsurf->workspace_transform;
973 }
974
975 shsurf = get_shell_surface(view->surface);
976 if (shsurf)
977 return &shsurf->workspace_transform;
978
979 return NULL;
980}
981
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200982static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100983view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200984{
Greg Vf57774e2018-07-24 23:21:55 +0300985 struct weston_transform *transform = view_get_transform(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200986
Greg Vf57774e2018-07-24 23:21:55 +0300987 if (!transform)
988 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100989
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200990 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500991 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100992 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200993
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100994 weston_matrix_init(&transform->matrix);
995 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200996 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500997 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200998}
999
1000static void
1001workspace_translate_out(struct workspace *ws, double fraction)
1002{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001003 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001004 unsigned int height;
1005 double d;
1006
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001007 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001008 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001009 d = height * fraction;
1010
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001011 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001012 }
1013}
1014
1015static void
1016workspace_translate_in(struct workspace *ws, double fraction)
1017{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001018 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001019 unsigned int height;
1020 double d;
1021
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001022 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001023 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001024
1025 if (fraction > 0)
1026 d = -(height - height * fraction);
1027 else
1028 d = height + height * fraction;
1029
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001030 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001031 }
1032}
1033
1034static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001035reverse_workspace_change_animation(struct desktop_shell *shell,
1036 unsigned int index,
1037 struct workspace *from,
1038 struct workspace *to)
1039{
1040 shell->workspaces.current = index;
1041
1042 shell->workspaces.anim_to = to;
1043 shell->workspaces.anim_from = from;
1044 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001045 shell->workspaces.anim_timestamp = (struct timespec) { 0 };
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001046
Quentin Glidic82681572016-12-17 13:40:51 +01001047 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1048 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
1049
Scott Moreau4272e632012-08-13 09:58:41 -06001050 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001051}
1052
1053static void
1054workspace_deactivate_transforms(struct workspace *ws)
1055{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001056 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001057 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001058
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001059 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Greg Vf57774e2018-07-24 23:21:55 +03001060 transform = view_get_transform(view);
1061 if (!transform)
1062 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001063
1064 if (!wl_list_empty(&transform->link)) {
1065 wl_list_remove(&transform->link);
1066 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001067 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001068 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001069 }
1070}
1071
1072static void
1073finish_workspace_change_animation(struct desktop_shell *shell,
1074 struct workspace *from,
1075 struct workspace *to)
1076{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001077 struct weston_view *view;
1078
Scott Moreau4272e632012-08-13 09:58:41 -06001079 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001080
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001081 /* Views that extend past the bottom of the output are still
1082 * visible after the workspace animation ends but before its layer
1083 * is hidden. In that case, we need to damage below those views so
1084 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001085 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001086 weston_view_damage_below(view);
1087
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001088 wl_list_remove(&shell->workspaces.animation.link);
1089 workspace_deactivate_transforms(from);
1090 workspace_deactivate_transforms(to);
1091 shell->workspaces.anim_to = NULL;
1092
Quentin Glidic82681572016-12-17 13:40:51 +01001093 weston_layer_unset_position(&shell->workspaces.anim_from->layer);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001094}
1095
1096static void
1097animate_workspace_change_frame(struct weston_animation *animation,
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001098 struct weston_output *output,
1099 const struct timespec *time)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001100{
1101 struct desktop_shell *shell =
1102 container_of(animation, struct desktop_shell,
1103 workspaces.animation);
1104 struct workspace *from = shell->workspaces.anim_from;
1105 struct workspace *to = shell->workspaces.anim_to;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001106 int64_t t;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001107 double x, y;
1108
1109 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1110 finish_workspace_change_animation(shell, from, to);
1111 return;
1112 }
1113
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001114 if (timespec_is_zero(&shell->workspaces.anim_timestamp)) {
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001115 if (shell->workspaces.anim_current == 0.0)
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001116 shell->workspaces.anim_timestamp = *time;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001117 else
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001118 timespec_add_msec(&shell->workspaces.anim_timestamp,
1119 time,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001120 /* Invers of movement function 'y' below. */
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001121 -(asin(1.0 - shell->workspaces.anim_current) *
1122 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1123 M_2_PI));
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001124 }
1125
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001126 t = timespec_sub_to_msec(time, &shell->workspaces.anim_timestamp);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001127
1128 /*
1129 * x = [0, π/2]
1130 * y(x) = sin(x)
1131 */
1132 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1133 y = sin(x);
1134
1135 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001136 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001137
1138 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1139 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1140 shell->workspaces.anim_current = y;
1141
Scott Moreau4272e632012-08-13 09:58:41 -06001142 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001143 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001144 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001145 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001146}
1147
1148static void
1149animate_workspace_change(struct desktop_shell *shell,
1150 unsigned int index,
1151 struct workspace *from,
1152 struct workspace *to)
1153{
1154 struct weston_output *output;
1155
1156 int dir;
1157
1158 if (index > shell->workspaces.current)
1159 dir = -1;
1160 else
1161 dir = 1;
1162
1163 shell->workspaces.current = index;
1164
1165 shell->workspaces.anim_dir = dir;
1166 shell->workspaces.anim_from = from;
1167 shell->workspaces.anim_to = to;
1168 shell->workspaces.anim_current = 0.0;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001169 shell->workspaces.anim_timestamp = (struct timespec) { 0 };
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001170
1171 output = container_of(shell->compositor->output_list.next,
1172 struct weston_output, link);
1173 wl_list_insert(&output->animation_list,
1174 &shell->workspaces.animation.link);
1175
Quentin Glidic82681572016-12-17 13:40:51 +01001176 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1177 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001178
1179 workspace_translate_in(to, 0);
1180
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001181 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001182
Scott Moreau4272e632012-08-13 09:58:41 -06001183 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001184}
1185
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001186static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001187update_workspace(struct desktop_shell *shell, unsigned int index,
1188 struct workspace *from, struct workspace *to)
1189{
1190 shell->workspaces.current = index;
Quentin Glidic82681572016-12-17 13:40:51 +01001191 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1192 weston_layer_unset_position(&from->layer);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001193}
1194
1195static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001196change_workspace(struct desktop_shell *shell, unsigned int index)
1197{
1198 struct workspace *from;
1199 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001200 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001201
1202 if (index == shell->workspaces.current)
1203 return;
1204
1205 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001206 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001207 return;
1208
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001209 from = get_current_workspace(shell);
1210 to = get_workspace(shell, index);
1211
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001212 if (shell->workspaces.anim_from == to &&
1213 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001214 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001215 reverse_workspace_change_animation(shell, index, from, to);
1216 return;
1217 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001218
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001219 if (shell->workspaces.anim_to != NULL)
1220 finish_workspace_change_animation(shell,
1221 shell->workspaces.anim_from,
1222 shell->workspaces.anim_to);
1223
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001224 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001225
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001226 if (shell->focus_animation_type != ANIMATION_NONE) {
1227 wl_list_for_each(state, &from->focus_list, link)
1228 if (state->keyboard_focus)
1229 animate_focus_change(shell, from,
1230 get_default_view(state->keyboard_focus), NULL);
1231
1232 wl_list_for_each(state, &to->focus_list, link)
1233 if (state->keyboard_focus)
1234 animate_focus_change(shell, to,
1235 NULL, get_default_view(state->keyboard_focus));
1236 }
1237
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001238 if (workspace_is_empty(to) && workspace_is_empty(from))
1239 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001240 else
1241 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001242}
1243
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001244static bool
1245workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1246{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001247 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001248 struct wl_list *e;
1249
1250 if (wl_list_empty(list))
1251 return false;
1252
1253 e = list->next;
1254
1255 if (e->next != list)
1256 return false;
1257
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001258 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001259}
1260
1261static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001262surface_keyboard_focus_lost(struct weston_surface *surface)
1263{
1264 struct weston_compositor *compositor = surface->compositor;
1265 struct weston_seat *seat;
1266 struct weston_surface *focus;
1267
1268 wl_list_for_each(seat, &compositor->seat_list, link) {
1269 struct weston_keyboard *keyboard =
1270 weston_seat_get_keyboard(seat);
1271
1272 if (!keyboard)
1273 continue;
1274
1275 focus = weston_surface_get_main_surface(keyboard->focus);
1276 if (focus == surface)
1277 weston_keyboard_set_focus(keyboard, NULL);
1278 }
1279}
1280
1281static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001282take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001283 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001284 unsigned int index)
1285{
Derek Foreman1281a362015-07-31 16:55:32 -05001286 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001287 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001288 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001289 struct shell_surface *shsurf;
1290 struct workspace *from;
1291 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001292 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001293
Derek Foreman1281a362015-07-31 16:55:32 -05001294 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295 view = get_default_view(surface);
1296 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001297 index == shell->workspaces.current ||
1298 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001299 return;
1300
1301 from = get_current_workspace(shell);
1302 to = get_workspace(shell, index);
1303
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001304 weston_layer_entry_remove(&view->layer_link);
1305 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001306
Philip Withnall659163d2013-11-25 18:01:36 +00001307 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001308 if (shsurf != NULL)
1309 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001310
Jonas Ådahle9d22502012-08-29 22:13:01 +02001311 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001312 drop_focus_state(shell, from, surface);
1313
1314 if (shell->workspaces.anim_from == to &&
1315 shell->workspaces.anim_to == from) {
1316 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001317
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001318 return;
1319 }
1320
1321 if (shell->workspaces.anim_to != NULL)
1322 finish_workspace_change_animation(shell,
1323 shell->workspaces.anim_from,
1324 shell->workspaces.anim_to);
1325
1326 if (workspace_is_empty(from) &&
1327 workspace_has_only(to, surface))
1328 update_workspace(shell, index, from, to);
1329 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001330 if (shsurf != NULL &&
1331 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001332 wl_list_insert(&shell->workspaces.anim_sticky_list,
1333 &shsurf->workspace_transform.link);
1334
1335 animate_workspace_change(shell, index, from, to);
1336 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001337
Jonas Ådahl8538b222012-08-29 22:13:03 +02001338 state = ensure_focus_state(shell, seat);
1339 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001340 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001341}
1342
1343static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02001344touch_move_grab_down(struct weston_touch_grab *grab,
1345 const struct timespec *time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001346 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001347{
1348}
1349
1350static void
Alexandros Frantzis27a51b82017-11-16 18:20:59 +02001351touch_move_grab_up(struct weston_touch_grab *grab, const struct timespec *time,
1352 int touch_id)
Rusty Lynch1084da52013-08-15 09:10:08 -07001353{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001354 struct weston_touch_move_grab *move =
1355 (struct weston_touch_move_grab *) container_of(
1356 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001357
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001358 if (touch_id == 0)
1359 move->active = 0;
1360
Jonas Ådahl9484b692013-12-02 22:05:03 +01001361 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001362 shell_touch_grab_end(&move->base);
1363 free(move);
1364 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001365}
1366
1367static void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +02001368touch_move_grab_motion(struct weston_touch_grab *grab,
1369 const struct timespec *time, int touch_id,
1370 wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001371{
1372 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1373 struct shell_surface *shsurf = move->base.shsurf;
1374 struct weston_surface *es;
1375 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1376 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1377
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001378 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001379 return;
1380
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001381 es = weston_desktop_surface_get_surface(shsurf->desktop_surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001382
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001383 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001384
1385 weston_compositor_schedule_repaint(es->compositor);
1386}
1387
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001388static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001389touch_move_grab_frame(struct weston_touch_grab *grab)
1390{
1391}
1392
1393static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001394touch_move_grab_cancel(struct weston_touch_grab *grab)
1395{
1396 struct weston_touch_move_grab *move =
1397 (struct weston_touch_move_grab *) container_of(
1398 grab, struct shell_touch_grab, grab);
1399
1400 shell_touch_grab_end(&move->base);
1401 free(move);
1402}
1403
Rusty Lynch1084da52013-08-15 09:10:08 -07001404static const struct weston_touch_grab_interface touch_move_grab_interface = {
1405 touch_move_grab_down,
1406 touch_move_grab_up,
1407 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001408 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001409 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001410};
1411
1412static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001413surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001414{
1415 struct weston_touch_move_grab *move;
1416
1417 if (!shsurf)
1418 return -1;
1419
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001420 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1421 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Rusty Lynch1084da52013-08-15 09:10:08 -07001422 return 0;
1423
1424 move = malloc(sizeof *move);
1425 if (!move)
1426 return -1;
1427
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001428 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001429 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001430 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001431 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001432 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001433
1434 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001435 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001436
1437 return 0;
1438}
1439
1440static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001441noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001442{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001443}
1444
1445static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001446noop_grab_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02001447 const struct timespec *time,
1448 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001449{
1450}
1451
1452static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001453noop_grab_axis_source(struct weston_pointer_grab *grab,
1454 uint32_t source)
1455{
1456}
1457
1458static void
1459noop_grab_frame(struct weston_pointer_grab *grab)
1460{
1461}
1462
1463static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001464constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1465{
1466 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001467 struct weston_surface *surface =
1468 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001469 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001470 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001471 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001472 pixman_rectangle32_t area;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001473 struct weston_geometry geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001474
1475 x = wl_fixed_to_int(pointer->x + move->dx);
1476 y = wl_fixed_to_int(pointer->y + move->dy);
1477
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001478 if (shsurf->shell->panel_position ==
1479 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001480 get_output_work_area(shsurf->shell, surface->output, &area);
1481 geometry =
1482 weston_desktop_surface_get_geometry(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001483
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001484 bottom = y + geometry.height + geometry.y;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001485 if (bottom - safety < area.y)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001486 y = area.y + safety - geometry.height
1487 - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001488
1489 if (move->client_initiated &&
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001490 y + geometry.y < area.y)
1491 y = area.y - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001492 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001493
1494 *cx = x;
1495 *cy = y;
1496}
1497
1498static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001499move_grab_motion(struct weston_pointer_grab *grab,
1500 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001501 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001502{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001503 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001504 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001505 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001506 struct weston_surface *surface;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001507 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001508
Jonas Ådahld2510102014-10-05 21:39:14 +02001509 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001510 if (!shsurf)
1511 return;
1512
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001513 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1514
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001515 constrain_position(move, &cx, &cy);
1516
1517 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001518
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001519 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001520}
1521
1522static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001523move_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001524 const struct timespec *time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001525{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001526 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1527 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001528 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001529 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001530
Daniel Stone4dbadb12012-05-30 16:31:51 +01001531 if (pointer->button_count == 0 &&
1532 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001533 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001534 free(grab);
1535 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001536}
1537
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001538static void
1539move_grab_cancel(struct weston_pointer_grab *grab)
1540{
1541 struct shell_grab *shell_grab =
1542 container_of(grab, struct shell_grab, grab);
1543
1544 shell_grab_end(shell_grab);
1545 free(grab);
1546}
1547
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001548static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001549 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001550 move_grab_motion,
1551 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001552 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001553 noop_grab_axis_source,
1554 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001555 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001556};
1557
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001558static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001559surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001560 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001561{
1562 struct weston_move_grab *move;
1563
1564 if (!shsurf)
1565 return -1;
1566
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001567 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001568 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1569 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001570 return 0;
1571
1572 move = malloc(sizeof *move);
1573 if (!move)
1574 return -1;
1575
Jason Ekstranda7af7042013-10-12 22:38:11 -05001576 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001577 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001578 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001579 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001580 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001581
1582 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001583 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001584
1585 return 0;
1586}
1587
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001588struct weston_resize_grab {
1589 struct shell_grab base;
1590 uint32_t edges;
1591 int32_t width, height;
1592};
1593
1594static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001595resize_grab_motion(struct weston_pointer_grab *grab,
1596 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001597 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001598{
1599 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001600 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001601 struct shell_surface *shsurf = resize->base.shsurf;
1602 int32_t width, height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001603 struct weston_size min_size, max_size;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001604 wl_fixed_t from_x, from_y;
1605 wl_fixed_t to_x, to_y;
1606
Jonas Ådahld2510102014-10-05 21:39:14 +02001607 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001608
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001609 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001610 return;
1611
Jason Ekstranda7af7042013-10-12 22:38:11 -05001612 weston_view_from_global_fixed(shsurf->view,
1613 pointer->grab_x, pointer->grab_y,
1614 &from_x, &from_y);
1615 weston_view_from_global_fixed(shsurf->view,
1616 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001617
1618 width = resize->width;
1619 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1620 width += wl_fixed_to_int(from_x - to_x);
1621 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1622 width += wl_fixed_to_int(to_x - from_x);
1623 }
1624
1625 height = resize->height;
1626 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1627 height += wl_fixed_to_int(from_y - to_y);
1628 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1629 height += wl_fixed_to_int(to_y - from_y);
1630 }
1631
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001632 max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
1633 min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
1634
1635 min_size.width = MAX(1, min_size.width);
1636 min_size.height = MAX(1, min_size.height);
1637
1638 if (width < min_size.width)
1639 width = min_size.width;
1640 else if (max_size.width > 0 && width > max_size.width)
1641 width = max_size.width;
1642 if (height < min_size.height)
1643 height = min_size.height;
1644 else if (max_size.width > 0 && width > max_size.width)
1645 width = max_size.width;
1646 weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001647}
1648
1649static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001650resize_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001651 const struct timespec *time,
1652 uint32_t button, uint32_t state_w)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001653{
1654 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001655 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001656 enum wl_pointer_button_state state = state_w;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001657 struct weston_desktop_surface *desktop_surface =
1658 resize->base.shsurf->desktop_surface;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001659
1660 if (pointer->button_count == 0 &&
1661 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001662 weston_desktop_surface_set_resizing(desktop_surface, false);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001663 shell_grab_end(&resize->base);
1664 free(grab);
1665 }
1666}
1667
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001668static void
1669resize_grab_cancel(struct weston_pointer_grab *grab)
1670{
1671 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001672 struct weston_desktop_surface *desktop_surface =
1673 resize->base.shsurf->desktop_surface;
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001674
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001675 weston_desktop_surface_set_resizing(desktop_surface, false);
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001676 shell_grab_end(&resize->base);
1677 free(grab);
1678}
1679
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001680static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001681 noop_grab_focus,
1682 resize_grab_motion,
1683 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001684 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001685 noop_grab_axis_source,
1686 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001687 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001688};
1689
Giulio Camuffob8366642013-04-25 13:57:46 +03001690/*
1691 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001692 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001693static void
1694surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1695 int32_t *y, int32_t *w, int32_t *h) {
1696 pixman_region32_t region;
1697 pixman_box32_t *box;
1698 struct weston_subsurface *subsurface;
1699
1700 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001701 surface->width,
1702 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001703
1704 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1705 pixman_region32_union_rect(&region, &region,
1706 subsurface->position.x,
1707 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001708 subsurface->surface->width,
1709 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001710 }
1711
1712 box = pixman_region32_extents(&region);
1713 if (x)
1714 *x = box->x1;
1715 if (y)
1716 *y = box->y1;
1717 if (w)
1718 *w = box->x2 - box->x1;
1719 if (h)
1720 *h = box->y2 - box->y1;
1721
1722 pixman_region32_fini(&region);
1723}
1724
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001725static int
1726surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001727 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001728{
1729 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001730 const unsigned resize_topbottom =
1731 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1732 const unsigned resize_leftright =
1733 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1734 const unsigned resize_any = resize_topbottom | resize_leftright;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001735 struct weston_geometry geometry;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001736
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001737 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001738 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1739 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001740 return 0;
1741
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001742 /* Check for invalid edge combinations. */
1743 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1744 (edges & resize_topbottom) == resize_topbottom ||
1745 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001746 return 0;
1747
1748 resize = malloc(sizeof *resize);
1749 if (!resize)
1750 return -1;
1751
1752 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001753
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001754 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1755 resize->width = geometry.width;
1756 resize->height = geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001757
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001758 shsurf->resize_edges = edges;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001759 weston_desktop_surface_set_resizing(shsurf->desktop_surface, true);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001760 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001761 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001762
1763 return 0;
1764}
1765
1766static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001767busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001768{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001769 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001770 struct weston_pointer *pointer = base->pointer;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001771 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001772 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001773 wl_fixed_t sx, sy;
1774
Jason Ekstranda7af7042013-10-12 22:38:11 -05001775 view = weston_compositor_pick_view(pointer->seat->compositor,
1776 pointer->x, pointer->y,
1777 &sx, &sy);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001778 desktop_surface = weston_surface_get_desktop_surface(view->surface);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001779
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001780 if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001781 shell_grab_end(grab);
1782 free(grab);
1783 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001784}
1785
1786static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001787busy_cursor_grab_motion(struct weston_pointer_grab *grab,
1788 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001789 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001790{
Jonas Ådahld2510102014-10-05 21:39:14 +02001791 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001792}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001793
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001794static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001795busy_cursor_grab_button(struct weston_pointer_grab *base,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001796 const struct timespec *time,
1797 uint32_t button, uint32_t state)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001798{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001799 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001800 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05001801 struct weston_pointer *pointer = grab->grab.pointer;
1802 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001803
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001804 if (shsurf && button == BTN_LEFT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001805 activate(shsurf->shell, shsurf->view, seat,
1806 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman794fa0e2015-07-15 13:00:38 -05001807 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001808 } else if (shsurf && button == BTN_RIGHT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001809 activate(shsurf->shell, shsurf->view, seat,
1810 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman74de4692015-07-15 13:00:39 -05001811 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001812 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001813}
1814
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001815static void
1816busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1817{
1818 struct shell_grab *grab = (struct shell_grab *) base;
1819
1820 shell_grab_end(grab);
1821 free(grab);
1822}
1823
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001824static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001825 busy_cursor_grab_focus,
1826 busy_cursor_grab_motion,
1827 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001828 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001829 noop_grab_axis_source,
1830 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001831 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001832};
1833
1834static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001835handle_pointer_focus(struct wl_listener *listener, void *data)
1836{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001837 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001838 struct weston_view *view = pointer->focus;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001839 struct shell_surface *shsurf;
1840 struct weston_desktop_client *client;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001841
Jason Ekstranda7af7042013-10-12 22:38:11 -05001842 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001843 return;
1844
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001845 shsurf = get_shell_surface(view->surface);
1846 if (!shsurf)
1847 return;
1848
1849 client = weston_desktop_surface_get_client(shsurf->desktop_surface);
1850
1851 if (shsurf->unresponsive)
1852 set_busy_cursor(shsurf, pointer);
1853 else
1854 weston_desktop_client_ping(client);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001855}
1856
1857static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001858shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1859{
1860 if (--shsurf->focus_count == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001861 weston_desktop_surface_set_activated(shsurf->desktop_surface, false);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001862}
1863
1864static void
1865shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1866{
1867 if (shsurf->focus_count++ == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001868 weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001869}
1870
1871static void
1872handle_keyboard_focus(struct wl_listener *listener, void *data)
1873{
1874 struct weston_keyboard *keyboard = data;
1875 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1876
1877 if (seat->focused_surface) {
1878 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1879 if (shsurf)
1880 shell_surface_lose_keyboard_focus(shsurf);
1881 }
1882
Philipp Kerlingba8a0d02017-07-26 12:02:15 +02001883 seat->focused_surface = weston_surface_get_main_surface(keyboard->focus);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001884
1885 if (seat->focused_surface) {
1886 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1887 if (shsurf)
1888 shell_surface_gain_keyboard_focus(shsurf);
1889 }
1890}
1891
Philip Withnall07926d92013-11-25 18:01:40 +00001892/* The surface will be inserted into the list immediately after the link
1893 * returned by this function (i.e. will be stacked immediately above the
1894 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001895static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00001896shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1897{
1898 struct workspace *ws;
1899
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001900 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
1901 !shsurf->state.lowered) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001902 return &shsurf->shell->fullscreen_layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001903 }
1904
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001905 /* Move the surface to a normal workspace layer so that surfaces
1906 * which were previously fullscreen or transient are no longer
1907 * rendered on top. */
1908 ws = get_current_workspace(shsurf->shell);
1909 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001910}
1911
Philip Withnall648a4dd2013-11-25 18:01:44 +00001912static void
1913shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
1914{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001915 weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001916}
1917
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001918/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00001919 * geometry to ensure the changes are redrawn.
1920 *
1921 * If any child surfaces exist and are mapped, ensure they’re in the same layer
1922 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001923static void
1924shell_surface_update_layer(struct shell_surface *shsurf)
1925{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001926 struct weston_surface *surface =
1927 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001928 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001929
1930 new_layer_link = shell_surface_calculate_layer_link(shsurf);
1931
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001932 if (new_layer_link == NULL)
1933 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001934 if (new_layer_link == &shsurf->view->layer_link)
1935 return;
1936
1937 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001938 weston_layer_entry_remove(&shsurf->view->layer_link);
1939 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001940 weston_view_geometry_dirty(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001941 weston_surface_damage(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001942
1943 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001944}
1945
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001946static void
Semi Malinen99f8c082018-05-02 11:10:32 +02001947notify_output_destroy(struct wl_listener *listener, void *data)
1948{
1949 struct shell_surface *shsurf =
1950 container_of(listener,
1951 struct shell_surface, output_destroy_listener);
1952
1953 shsurf->output = NULL;
1954 shsurf->output_destroy_listener.notify = NULL;
1955}
1956
1957static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00001958shell_surface_set_output(struct shell_surface *shsurf,
1959 struct weston_output *output)
1960{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001961 struct weston_surface *es =
1962 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001963
1964 /* get the default output, if the client set it as NULL
Abdur Rehman7f1da1f2017-01-01 19:46:33 +05001965 check whether the output is available */
Philip Withnall352e7ed2013-11-25 18:01:35 +00001966 if (output)
1967 shsurf->output = output;
1968 else if (es->output)
1969 shsurf->output = es->output;
1970 else
1971 shsurf->output = get_default_output(es->compositor);
Semi Malinen99f8c082018-05-02 11:10:32 +02001972
1973 if (shsurf->output_destroy_listener.notify) {
1974 wl_list_remove(&shsurf->output_destroy_listener.link);
1975 shsurf->output_destroy_listener.notify = NULL;
1976 }
1977
1978 if (!shsurf->output)
1979 return;
1980
1981 shsurf->output_destroy_listener.notify = notify_output_destroy;
1982 wl_signal_add(&shsurf->output->destroy_signal,
1983 &shsurf->output_destroy_listener);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001984}
1985
1986static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001987weston_view_set_initial_position(struct weston_view *view,
1988 struct desktop_shell *shell);
1989
1990static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001991unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08001992{
Philip Withnallf85fe842013-11-25 18:01:37 +00001993 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wu4539b082012-03-01 12:57:46 +08001994 wl_list_remove(&shsurf->fullscreen.transform.link);
1995 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00001996
Jason Ekstranda7af7042013-10-12 22:38:11 -05001997 if (shsurf->fullscreen.black_view)
1998 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
1999 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002000
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002001 if (shsurf->saved_position_valid)
2002 weston_view_set_position(shsurf->view,
2003 shsurf->saved_x, shsurf->saved_y);
2004 else
2005 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02002006 shsurf->saved_position_valid = false;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002007
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002008 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002009 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002010 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002011 shsurf->saved_rotation_valid = false;
2012 }
Alex Wu4539b082012-03-01 12:57:46 +08002013}
2014
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002015static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002016unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002017{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002018 struct weston_surface *surface =
2019 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2020
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002021 /* undo all maximized things here */
Semi Malinen99f8c082018-05-02 11:10:32 +02002022 shell_surface_set_output(shsurf, get_default_output(surface->compositor));
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002023
2024 if (shsurf->saved_position_valid)
2025 weston_view_set_position(shsurf->view,
2026 shsurf->saved_x, shsurf->saved_y);
2027 else
2028 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02002029 shsurf->saved_position_valid = false;
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002030
2031 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002032 wl_list_insert(&shsurf->view->geometry.transformation_list,
2033 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002034 shsurf->saved_rotation_valid = false;
2035 }
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002036}
2037
Philip Withnallbecb77e2013-11-25 18:01:30 +00002038static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002039set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002040{
2041 struct shell_surface *shsurf;
2042 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002043 struct weston_view *view;
2044
2045 view = get_default_view(surface);
2046 if (!view)
2047 return;
2048
2049 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2050
2051 shsurf = get_shell_surface(surface);
2052 current_ws = get_current_workspace(shsurf->shell);
2053
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002054 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002055 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002056
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002057 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002058 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002059
2060 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002061 weston_view_damage_below(view);
2062}
2063
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002064
Tiago Vignattibe143262012-04-16 17:31:41 +03002065static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002066shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002067{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002068 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002069}
2070
Pekka Paalanen8274d902014-08-06 19:36:51 +03002071static int
2072black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2073{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002074 struct weston_view *fs_view = surface->committed_private;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002075 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002076 int n;
2077 int rem;
2078 int ret;
2079
2080 n = snprintf(buf, len, "black background surface for ");
2081 if (n < 0)
2082 return n;
2083
2084 rem = (int)len - n;
2085 if (rem < 0)
2086 rem = 0;
2087
2088 if (fs_surface->get_label)
2089 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2090 else
2091 ret = snprintf(buf + n, rem, "<unknown>");
2092
2093 if (ret < 0)
2094 return n;
2095
2096 return n + ret;
2097}
2098
Alex Wu21858432012-04-01 20:13:08 +08002099static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002100black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002101
Jason Ekstranda7af7042013-10-12 22:38:11 -05002102static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002103create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002104 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002105 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002106{
2107 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002108 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002109
2110 surface = weston_surface_create(ec);
2111 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002112 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002113 return NULL;
2114 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002115 view = weston_view_create(surface);
2116 if (surface == NULL) {
2117 weston_log("no memory\n");
2118 weston_surface_destroy(surface);
2119 return NULL;
2120 }
Alex Wu4539b082012-03-01 12:57:46 +08002121
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002122 surface->committed = black_surface_committed;
2123 surface->committed_private = fs_view;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002124 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002125 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002126 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002127 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002128 pixman_region32_fini(&surface->input);
2129 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002130
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002131 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002132 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002133
2134 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002135}
2136
Philip Withnalled8f1a92013-11-25 18:01:43 +00002137static void
2138shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2139{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002140 struct weston_surface *surface =
2141 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002142 struct weston_output *output = shsurf->fullscreen_output;
2143
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002144 assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
Philip Withnalled8f1a92013-11-25 18:01:43 +00002145
2146 if (!shsurf->fullscreen.black_view)
2147 shsurf->fullscreen.black_view =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002148 create_black_surface(surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002149 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002150 output->x, output->y,
2151 output->width,
2152 output->height);
2153
2154 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002155 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2156 weston_layer_entry_insert(&shsurf->view->layer_link,
2157 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002158 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002159 weston_surface_damage(surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002160
Armin Krezović4663aca2016-06-30 06:04:29 +02002161 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002162 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002163}
2164
Alex Wu4539b082012-03-01 12:57:46 +08002165/* Create black surface and append it to the associated fullscreen surface.
2166 * Handle size dismatch and positioning according to the method. */
2167static void
2168shell_configure_fullscreen(struct shell_surface *shsurf)
2169{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002170 struct weston_surface *surface =
2171 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffob8366642013-04-25 13:57:46 +03002172 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002173
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002174 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002175 weston_layer_entry_remove(&shsurf->view->layer_link);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002176 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2177 &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002178
Pekka Paalanenc63acc42018-05-02 10:21:58 +02002179 if (!shsurf->fullscreen_output) {
2180 /* If there is no output, there's not much we can do.
2181 * Position the window somewhere, whatever. */
2182 weston_view_set_position(shsurf->view, 0, 0);
2183 return;
2184 }
2185
Philip Withnalled8f1a92013-11-25 18:01:43 +00002186 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002187
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002188 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002189 &surf_width, &surf_height);
2190
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002191 if (surface->buffer_ref.buffer)
2192 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002193}
2194
Alex Wu4539b082012-03-01 12:57:46 +08002195static void
2196shell_map_fullscreen(struct shell_surface *shsurf)
2197{
Alex Wubd3354b2012-04-17 17:20:49 +08002198 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002199}
2200
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002201static struct weston_output *
2202get_focused_output(struct weston_compositor *compositor)
2203{
2204 struct weston_seat *seat;
2205 struct weston_output *output = NULL;
2206
2207 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002208 struct weston_touch *touch = weston_seat_get_touch(seat);
2209 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2210 struct weston_keyboard *keyboard =
2211 weston_seat_get_keyboard(seat);
2212
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002213 /* Priority has touch focus, then pointer and
2214 * then keyboard focus. We should probably have
2215 * three for loops and check frist for touch,
2216 * then for pointer, etc. but unless somebody has some
2217 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002218 if (touch && touch->focus)
2219 output = touch->focus->output;
2220 else if (pointer && pointer->focus)
2221 output = pointer->focus->output;
2222 else if (keyboard && keyboard->focus)
2223 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002224
2225 if (output)
2226 break;
2227 }
2228
2229 return output;
2230}
2231
2232static void
Giulio Camuffo5085a752013-03-25 21:42:45 +01002233destroy_shell_seat(struct wl_listener *listener, void *data)
2234{
2235 struct shell_seat *shseat =
2236 container_of(listener,
2237 struct shell_seat, seat_destroy_listener);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002238
2239 wl_list_remove(&shseat->seat_destroy_listener.link);
2240 free(shseat);
2241}
2242
Jason Ekstrand024177c2014-04-21 19:42:58 -05002243static void
2244shell_seat_caps_changed(struct wl_listener *listener, void *data)
2245{
Derek Foreman1281a362015-07-31 16:55:32 -05002246 struct weston_keyboard *keyboard;
2247 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05002248 struct shell_seat *seat;
2249
2250 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002251 keyboard = weston_seat_get_keyboard(seat->seat);
2252 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002253
Derek Foreman1281a362015-07-31 16:55:32 -05002254 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002255 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002256 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002257 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002258 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05002259 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002260 wl_list_init(&seat->keyboard_focus_listener.link);
2261 }
2262
Derek Foreman1281a362015-07-31 16:55:32 -05002263 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002264 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002265 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002266 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002267 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05002268 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002269 wl_list_init(&seat->pointer_focus_listener.link);
2270 }
2271}
2272
Giulio Camuffo5085a752013-03-25 21:42:45 +01002273static struct shell_seat *
2274create_shell_seat(struct weston_seat *seat)
2275{
2276 struct shell_seat *shseat;
2277
2278 shseat = calloc(1, sizeof *shseat);
2279 if (!shseat) {
2280 weston_log("no memory to allocate shell seat\n");
2281 return NULL;
2282 }
2283
2284 shseat->seat = seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002285
2286 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2287 wl_signal_add(&seat->destroy_signal,
2288 &shseat->seat_destroy_listener);
2289
Jason Ekstrand024177c2014-04-21 19:42:58 -05002290 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2291 wl_list_init(&shseat->keyboard_focus_listener.link);
2292
2293 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2294 wl_list_init(&shseat->pointer_focus_listener.link);
2295
2296 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2297 wl_signal_add(&seat->updated_caps_signal,
2298 &shseat->caps_changed_listener);
2299 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2300
Giulio Camuffo5085a752013-03-25 21:42:45 +01002301 return shseat;
2302}
2303
2304static struct shell_seat *
2305get_shell_seat(struct weston_seat *seat)
2306{
2307 struct wl_listener *listener;
2308
2309 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002310 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002311
2312 return container_of(listener,
2313 struct shell_seat, seat_destroy_listener);
2314}
2315
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002316static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002317fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002318{
2319 struct shell_surface *shsurf = data;
2320
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002321 weston_surface_destroy(shsurf->view->surface);
Pekka Paalanen99628002018-05-22 12:48:35 +03002322
2323 if (shsurf->output_destroy_listener.notify) {
2324 wl_list_remove(&shsurf->output_destroy_listener.link);
2325 shsurf->output_destroy_listener.notify = NULL;
2326 }
2327
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002328 free(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002329}
2330
2331static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002332fade_out_done(struct weston_view_animation *animation, void *data)
2333{
2334 struct shell_surface *shsurf = data;
2335 struct wl_event_loop *loop;
2336
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002337 loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
Derek Foreman039e9be2015-04-14 17:09:06 -05002338
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002339 if (weston_view_is_mapped(shsurf->view)) {
2340 shsurf->view->is_mapped = false;
Derek Foreman039e9be2015-04-14 17:09:06 -05002341 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
Derek Foreman039e9be2015-04-14 17:09:06 -05002342 }
2343}
2344
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002345struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002346get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002347{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002348 if (weston_surface_is_desktop_surface(surface)) {
2349 struct weston_desktop_surface *desktop_surface =
2350 weston_surface_get_desktop_surface(surface);
2351 return weston_desktop_surface_get_user_data(desktop_surface);
2352 }
2353 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002354}
2355
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002356/*
2357 * libweston-desktop
2358 */
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002359
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002360static void
2361desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2362 void *shell)
2363{
2364 struct weston_desktop_client *client =
2365 weston_desktop_surface_get_client(desktop_surface);
2366 struct wl_client *wl_client =
2367 weston_desktop_client_get_client(client);
2368 struct weston_view *view;
2369 struct shell_surface *shsurf;
2370 struct weston_surface *surface =
2371 weston_desktop_surface_get_surface(desktop_surface);
2372
2373 view = weston_desktop_surface_create_view(desktop_surface);
2374 if (!view)
2375 return;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002376
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002377 shsurf = calloc(1, sizeof *shsurf);
2378 if (!shsurf) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002379 if (wl_client)
2380 wl_client_post_no_memory(wl_client);
2381 else
2382 weston_log("no memory to allocate shell surface\n");
2383 return;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002384 }
2385
Pekka Paalanen8274d902014-08-06 19:36:51 +03002386 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002387
Tiago Vignattibc052c92012-04-19 16:18:18 +03002388 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002389 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002390 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002391 shsurf->saved_rotation_valid = false;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002392 shsurf->desktop_surface = desktop_surface;
2393 shsurf->view = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002394 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002395 wl_list_init(&shsurf->fullscreen.transform.link);
2396
Semi Malinen99f8c082018-05-02 11:10:32 +02002397 shell_surface_set_output(
2398 shsurf, get_default_output(shsurf->shell->compositor));
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002399
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002400 wl_signal_init(&shsurf->destroy_signal);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002401
Pekka Paalanen460099f2012-01-20 16:48:25 +02002402 /* empty when not in use */
2403 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002404 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002405
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002406 wl_list_init(&shsurf->workspace_transform.link);
2407
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002408 weston_desktop_surface_set_user_data(desktop_surface, shsurf);
2409 weston_desktop_surface_set_activated(desktop_surface,
2410 shsurf->focus_count > 0);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002411}
2412
Tiago Vignattibc052c92012-04-19 16:18:18 +03002413static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002414desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2415 void *shell)
2416{
2417 struct shell_surface *shsurf =
2418 weston_desktop_surface_get_user_data(desktop_surface);
2419 struct weston_surface *surface =
2420 weston_desktop_surface_get_surface(desktop_surface);
2421
2422 if (!shsurf)
2423 return;
2424
2425 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2426
2427 if (shsurf->fullscreen.black_view)
2428 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2429
2430 weston_surface_set_label_func(surface, NULL);
2431 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2432 shsurf->desktop_surface = NULL;
2433
Quentin Glidicf01ecee2016-08-16 10:52:46 +02002434 weston_desktop_surface_unlink_view(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002435 if (weston_surface_is_mapped(surface) &&
2436 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
2437 pixman_region32_fini(&surface->pending.input);
2438 pixman_region32_init(&surface->pending.input);
2439 pixman_region32_fini(&surface->input);
2440 pixman_region32_init(&surface->input);
2441 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2442 fade_out_done, shsurf);
2443 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002444 weston_view_destroy(shsurf->view);
Pekka Paalanen99628002018-05-22 12:48:35 +03002445
2446 if (shsurf->output_destroy_listener.notify) {
2447 wl_list_remove(&shsurf->output_destroy_listener.link);
2448 shsurf->output_destroy_listener.notify = NULL;
2449 }
2450
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002451 free(shsurf);
2452 }
2453}
2454
2455static void
2456set_maximized_position(struct desktop_shell *shell,
2457 struct shell_surface *shsurf)
2458{
2459 pixman_rectangle32_t area;
2460 struct weston_geometry geometry;
2461
2462 get_output_work_area(shell, shsurf->output, &area);
2463 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2464
2465 weston_view_set_position(shsurf->view,
2466 area.x - geometry.x,
2467 area.y - geometry.y);
2468}
2469
2470static void
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002471set_position_from_xwayland(struct shell_surface *shsurf)
2472{
2473 struct weston_geometry geometry;
2474 float x;
2475 float y;
2476
2477 assert(shsurf->xwayland.is_set);
2478
2479 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2480 x = shsurf->xwayland.x - geometry.x;
2481 y = shsurf->xwayland.y - geometry.y;
2482
2483 weston_view_set_position(shsurf->view, x, y);
2484
2485#ifdef WM_DEBUG
2486 weston_log("%s: XWM %d, %d; geometry %d, %d; view %f, %f\n",
2487 __func__, shsurf->xwayland.x, shsurf->xwayland.y,
2488 geometry.x, geometry.y, x, y);
2489#endif
2490}
2491
2492static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002493map(struct desktop_shell *shell, struct shell_surface *shsurf,
2494 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002495{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002496 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002497 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2498 struct weston_compositor *compositor = shell->compositor;
2499 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002500
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002501 /* initial positioning, see also configure() */
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002502 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002503 center_on_output(shsurf->view, shsurf->fullscreen_output);
2504 shell_map_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002505 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002506 set_maximized_position(shell, shsurf);
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002507 } else if (shsurf->xwayland.is_set) {
2508 set_position_from_xwayland(shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002509 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002510 weston_view_set_initial_position(shsurf->view, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002511 }
2512
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002513 /* Surface stacking order, see also activate(). */
2514 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002515
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002516 weston_view_update_transform(shsurf->view);
2517 shsurf->view->is_mapped = true;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002518 if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002519 surface->output = shsurf->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02002520 weston_view_set_output(shsurf->view, shsurf->output);
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002521 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002522
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002523 if (!shell->locked) {
2524 wl_list_for_each(seat, &compositor->seat_list, link)
2525 activate(shell, shsurf->view, seat,
2526 WESTON_ACTIVATE_FLAG_CONFIGURE);
2527 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002528
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002529 if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002530 switch (shell->win_animation_type) {
2531 case ANIMATION_FADE:
2532 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2533 break;
2534 case ANIMATION_ZOOM:
2535 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2536 break;
2537 case ANIMATION_NONE:
2538 default:
2539 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002540 }
2541 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002542}
2543
2544static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002545desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2546 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002547{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002548 struct shell_surface *shsurf =
2549 weston_desktop_surface_get_user_data(desktop_surface);
2550 struct weston_surface *surface =
2551 weston_desktop_surface_get_surface(desktop_surface);
2552 struct weston_view *view = shsurf->view;
2553 struct desktop_shell *shell = data;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002554 bool was_fullscreen;
2555 bool was_maximized;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002556
2557 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002558 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002559
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002560 was_fullscreen = shsurf->state.fullscreen;
2561 was_maximized = shsurf->state.maximized;
2562
2563 shsurf->state.fullscreen =
2564 weston_desktop_surface_get_fullscreen(desktop_surface);
2565 shsurf->state.maximized =
2566 weston_desktop_surface_get_maximized(desktop_surface);
2567
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002568 if (!weston_surface_is_mapped(surface)) {
2569 map(shell, shsurf, sx, sy);
2570 surface->is_mapped = true;
2571 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2572 ++surface->ref_count;
2573 return;
2574 }
2575
2576 if (sx == 0 && sy == 0 &&
2577 shsurf->last_width == surface->width &&
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002578 shsurf->last_height == surface->height &&
2579 was_fullscreen == shsurf->state.fullscreen &&
2580 was_maximized == shsurf->state.maximized)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002581 return;
2582
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002583 if (was_fullscreen)
2584 unset_fullscreen(shsurf);
2585 if (was_maximized)
2586 unset_maximized(shsurf);
2587
Quentin Glidic920cf042016-08-16 14:26:20 +02002588 if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2589 !shsurf->saved_position_valid) {
2590 shsurf->saved_x = shsurf->view->geometry.x;
2591 shsurf->saved_y = shsurf->view->geometry.y;
2592 shsurf->saved_position_valid = true;
2593
2594 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2595 wl_list_remove(&shsurf->rotation.transform.link);
2596 wl_list_init(&shsurf->rotation.transform.link);
2597 weston_view_geometry_dirty(shsurf->view);
2598 shsurf->saved_rotation_valid = true;
2599 }
2600 }
2601
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002602 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002603 shell_configure_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002604 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002605 set_maximized_position(shell, shsurf);
2606 surface->output = shsurf->output;
2607 } else {
2608 float from_x, from_y;
2609 float to_x, to_y;
2610 float x, y;
2611
2612 if (shsurf->resize_edges) {
2613 sx = 0;
2614 sy = 0;
2615 }
2616
2617 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2618 sx = shsurf->last_width - surface->width;
2619 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2620 sy = shsurf->last_height - surface->height;
2621
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002622 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2623 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2624 x = shsurf->view->geometry.x + to_x - from_x;
2625 y = shsurf->view->geometry.y + to_y - from_y;
2626
2627 weston_view_set_position(shsurf->view, x, y);
2628 }
2629
Emmanuel Gil Peyrotc3941792017-04-04 18:49:33 +01002630 shsurf->last_width = surface->width;
2631 shsurf->last_height = surface->height;
2632
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002633 /* XXX: would a fullscreen surface need the same handling? */
2634 if (surface->output) {
2635 wl_list_for_each(view, &surface->views, surface_link)
2636 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002637 }
2638}
2639
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002640static void
Derek Foreman927d9e22017-10-06 14:37:44 -05002641get_maximized_size(struct shell_surface *shsurf, int32_t *width, int32_t *height)
2642{
2643 struct desktop_shell *shell;
2644 pixman_rectangle32_t area;
2645
2646 shell = shell_surface_get_shell(shsurf);
2647 get_output_work_area(shell, shsurf->output, &area);
2648
2649 *width = area.width;
2650 *height = area.height;
2651}
2652
2653static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002654set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2655 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002656{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002657 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2658 struct weston_surface *surface =
2659 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2660 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002661
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002662 if (fullscreen) {
2663 /* handle clients launching in fullscreen */
2664 if (output == NULL && !weston_surface_is_mapped(surface)) {
2665 /* Set the output to the one that has focus currently. */
2666 output = get_focused_output(surface->compositor);
2667 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002668
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002669 shell_surface_set_output(shsurf, output);
2670 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002671
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002672 width = shsurf->output->width;
2673 height = shsurf->output->height;
Derek Foremane1af3d82017-10-06 14:37:45 -05002674 } else if (weston_desktop_surface_get_maximized(desktop_surface)) {
2675 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002676 }
2677 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2678 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002679}
2680
2681static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002682desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2683 struct weston_seat *seat, uint32_t serial, void *shell)
2684{
2685 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2686 struct weston_touch *touch = weston_seat_get_touch(seat);
2687 struct shell_surface *shsurf =
2688 weston_desktop_surface_get_user_data(desktop_surface);
2689 struct weston_surface *surface =
2690 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2691 struct wl_resource *resource = surface->resource;
2692 struct weston_surface *focus;
2693
2694 if (pointer &&
2695 pointer->focus &&
2696 pointer->button_count > 0 &&
2697 pointer->grab_serial == serial) {
2698 focus = weston_surface_get_main_surface(pointer->focus->surface);
2699 if ((focus == surface) &&
2700 (surface_move(shsurf, pointer, true) < 0))
2701 wl_resource_post_no_memory(resource);
2702 } else if (touch &&
2703 touch->focus &&
2704 touch->grab_serial == serial) {
2705 focus = weston_surface_get_main_surface(touch->focus->surface);
2706 if ((focus == surface) &&
2707 (surface_touch_move(shsurf, touch) < 0))
2708 wl_resource_post_no_memory(resource);
2709 }
2710}
2711
2712static void
2713desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2714 struct weston_seat *seat, uint32_t serial,
2715 enum weston_desktop_surface_edge edges, void *shell)
2716{
2717 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2718 struct shell_surface *shsurf =
2719 weston_desktop_surface_get_user_data(desktop_surface);
2720 struct weston_surface *surface =
2721 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2722 struct wl_resource *resource = surface->resource;
2723 struct weston_surface *focus;
2724
2725 if (!pointer ||
2726 pointer->button_count == 0 ||
2727 pointer->grab_serial != serial ||
2728 pointer->focus == NULL)
2729 return;
2730
2731 focus = weston_surface_get_main_surface(pointer->focus->surface);
2732 if (focus != surface)
2733 return;
2734
2735 if (surface_resize(shsurf, pointer, edges) < 0)
2736 wl_resource_post_no_memory(resource);
2737}
2738
2739static void
2740desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2741 bool fullscreen,
2742 struct weston_output *output, void *shell)
2743{
2744 struct shell_surface *shsurf =
2745 weston_desktop_surface_get_user_data(desktop_surface);
2746
2747 set_fullscreen(shsurf, fullscreen, output);
2748}
2749
2750static void
2751set_maximized(struct shell_surface *shsurf, bool maximized)
2752{
2753 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2754 struct weston_surface *surface =
2755 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2756 int32_t width = 0, height = 0;
2757
2758 if (maximized) {
2759 struct weston_output *output;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002760
2761 if (!weston_surface_is_mapped(surface))
2762 output = get_focused_output(surface->compositor);
2763 else
2764 output = surface->output;
2765
2766 shell_surface_set_output(shsurf, output);
2767
Derek Foreman927d9e22017-10-06 14:37:44 -05002768 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002769 }
2770 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2771 weston_desktop_surface_set_size(desktop_surface, width, height);
2772}
2773
2774static void
2775desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2776 bool maximized, void *shell)
2777{
2778 struct shell_surface *shsurf =
2779 weston_desktop_surface_get_user_data(desktop_surface);
2780
2781 set_maximized(shsurf, maximized);
2782}
2783
2784static void
2785desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2786 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002787{
2788 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002789 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002790
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002791 /* apply compositor's own minimization logic (hide) */
2792 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002793}
2794
Rafael Antognollie2a34552013-12-03 15:35:45 -02002795static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002796set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002797{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002798 struct shell_grab *grab;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002799
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002800 if (pointer->grab->interface == &busy_cursor_grab_interface)
2801 return;
2802
2803 grab = malloc(sizeof *grab);
2804 if (!grab)
2805 return;
2806
2807 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2808 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2809 /* Mark the shsurf as ungrabbed so that button binding is able
2810 * to move it. */
2811 shsurf->grabbed = 0;
2812}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002813
2814static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002815end_busy_cursor(struct weston_compositor *compositor,
2816 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002817{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002818 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002819 struct shell_grab *grab;
2820 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002821
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002822 wl_list_for_each(seat, &compositor->seat_list, link) {
2823 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2824 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002825
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002826 if (!pointer)
2827 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002828
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002829 if (pointer->grab->interface != &busy_cursor_grab_interface)
2830 continue;
2831
2832 grab = (struct shell_grab *) pointer->grab;
2833 shsurf = grab->shsurf;
2834 if (!shsurf)
2835 continue;
2836
2837 grab_client =
2838 weston_desktop_surface_get_client(shsurf->desktop_surface);
2839 if (grab_client == desktop_client) {
2840 shell_grab_end(grab);
2841 free(grab);
2842 }
2843 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002844}
2845
2846static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002847desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2848 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002849{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002850 struct shell_surface *shsurf =
2851 weston_desktop_surface_get_user_data(desktop_surface);
2852 bool *unresponsive = user_data;
2853
2854 shsurf->unresponsive = *unresponsive;
2855}
2856
2857static void
2858desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2859 void *shell_)
2860{
2861 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002862 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002863 struct weston_seat *seat;
2864 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002865
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002866 weston_desktop_client_for_each_surface(desktop_client,
2867 desktop_surface_set_unresponsive,
2868 &unresponsive);
2869
2870
2871 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2872 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2873 struct weston_desktop_client *grab_client;
2874
2875 if (!pointer || !pointer->focus)
2876 continue;
2877
2878 shsurf = get_shell_surface(pointer->focus->surface);
2879 if (!shsurf)
2880 continue;
2881
2882 grab_client =
2883 weston_desktop_surface_get_client(shsurf->desktop_surface);
2884 if (grab_client == desktop_client)
2885 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002886 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002887}
2888
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002889static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002890desktop_surface_pong(struct weston_desktop_client *desktop_client,
2891 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002892{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002893 struct desktop_shell *shell = shell_;
2894 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002895
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002896 weston_desktop_client_for_each_surface(desktop_client,
2897 desktop_surface_set_unresponsive,
2898 &unresponsive);
2899 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002900}
2901
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002902static void
2903desktop_surface_set_xwayland_position(struct weston_desktop_surface *surface,
2904 int32_t x, int32_t y, void *shell_)
2905{
2906 struct shell_surface *shsurf =
2907 weston_desktop_surface_get_user_data(surface);
2908
2909 shsurf->xwayland.x = x;
2910 shsurf->xwayland.y = y;
2911 shsurf->xwayland.is_set = true;
2912}
2913
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002914static const struct weston_desktop_api shell_desktop_api = {
2915 .struct_size = sizeof(struct weston_desktop_api),
2916 .surface_added = desktop_surface_added,
2917 .surface_removed = desktop_surface_removed,
2918 .committed = desktop_surface_committed,
2919 .move = desktop_surface_move,
2920 .resize = desktop_surface_resize,
2921 .fullscreen_requested = desktop_surface_fullscreen_requested,
2922 .maximized_requested = desktop_surface_maximized_requested,
2923 .minimized_requested = desktop_surface_minimized_requested,
2924 .ping_timeout = desktop_surface_ping_timeout,
2925 .pong = desktop_surface_pong,
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002926 .set_xwayland_position = desktop_surface_set_xwayland_position,
Rafael Antognollie2a34552013-12-03 15:35:45 -02002927};
2928
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002929/* ************************ *
2930 * end of libweston-desktop *
2931 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04002932static void
Quentin Glidice8bf9592016-06-23 18:55:20 +02002933configure_static_view(struct weston_view *ev, struct weston_layer *layer, int x, int y)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002934{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002935 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002936
Semi Malinene7a52fb2018-04-26 11:08:10 +02002937 if (!ev->output)
2938 return;
2939
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002940 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002941 if (v->output == ev->output && v != ev) {
2942 weston_view_unmap(v);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002943 v->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002944 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002945 }
2946 }
2947
Quentin Glidice8bf9592016-06-23 18:55:20 +02002948 weston_view_set_position(ev, ev->output->x + x, ev->output->y + y);
Armin Krezović4663aca2016-06-30 06:04:29 +02002949 ev->surface->is_mapped = true;
2950 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002951
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002952 if (wl_list_empty(&ev->layer_link.link)) {
2953 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002954 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002955 }
2956}
2957
David Fort50d962f2016-06-09 17:55:52 +02002958
2959static struct shell_output *
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002960find_shell_output_from_weston_output(struct desktop_shell *shell,
2961 struct weston_output *output)
David Fort50d962f2016-06-09 17:55:52 +02002962{
2963 struct shell_output *shell_output;
2964
2965 wl_list_for_each(shell_output, &shell->output_list, link) {
2966 if (shell_output->output == output)
2967 return shell_output;
2968 }
2969
2970 return NULL;
2971}
2972
Pekka Paalanen8274d902014-08-06 19:36:51 +03002973static int
2974background_get_label(struct weston_surface *surface, char *buf, size_t len)
2975{
2976 return snprintf(buf, len, "background for output %s",
2977 surface->output->name);
2978}
2979
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002980static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002981background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002982{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002983 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002984 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002985
Jason Ekstranda7af7042013-10-12 22:38:11 -05002986 view = container_of(es->views.next, struct weston_view, surface_link);
2987
Quentin Glidice8bf9592016-06-23 18:55:20 +02002988 configure_static_view(view, &shell->background_layer, 0, 0);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002989}
2990
2991static void
David Fort50d962f2016-06-09 17:55:52 +02002992handle_background_surface_destroy(struct wl_listener *listener, void *data)
2993{
2994 struct shell_output *output =
2995 container_of(listener, struct shell_output, background_surface_listener);
2996
2997 weston_log("background surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09002998 wl_list_remove(&output->background_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02002999 output->background_surface = NULL;
3000}
3001
3002static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003003desktop_shell_set_background(struct wl_client *client,
3004 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003005 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003006 struct wl_resource *surface_resource)
3007{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003008 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003009 struct weston_surface *surface =
3010 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02003011 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003012 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003013
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003014 if (surface->committed) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003015 wl_resource_post_error(surface_resource,
3016 WL_DISPLAY_ERROR_INVALID_OBJECT,
3017 "surface role already assigned");
3018 return;
3019 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003020
Jason Ekstranda7af7042013-10-12 22:38:11 -05003021 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3022 weston_view_destroy(view);
3023 view = weston_view_create(surface);
3024
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003025 surface->committed = background_committed;
3026 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003027 weston_surface_set_label_func(surface, background_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03003028 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02003029 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02003030
3031 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02003032 if (sh_output->background_surface) {
3033 /* The output already has a background, tell our helper
3034 * there is no need for another one. */
3035 weston_desktop_shell_send_configure(resource, 0,
3036 surface_resource,
3037 0, 0);
3038 } else {
3039 weston_desktop_shell_send_configure(resource, 0,
3040 surface_resource,
3041 surface->output->width,
3042 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003043
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02003044 sh_output->background_surface = surface;
3045
3046 sh_output->background_surface_listener.notify =
3047 handle_background_surface_destroy;
3048 wl_signal_add(&surface->destroy_signal,
3049 &sh_output->background_surface_listener);
3050 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003051}
3052
Pekka Paalanen8274d902014-08-06 19:36:51 +03003053static int
3054panel_get_label(struct weston_surface *surface, char *buf, size_t len)
3055{
3056 return snprintf(buf, len, "panel for output %s",
3057 surface->output->name);
3058}
3059
Kristian Høgsberg75840622011-09-06 13:48:16 -04003060static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003061panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003062{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003063 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003064 struct weston_view *view;
Quentin Glidice8bf9592016-06-23 18:55:20 +02003065 int width, height;
3066 int x = 0, y = 0;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003067
Jason Ekstranda7af7042013-10-12 22:38:11 -05003068 view = container_of(es->views.next, struct weston_view, surface_link);
3069
Quentin Glidice8bf9592016-06-23 18:55:20 +02003070 get_panel_size(shell, view, &width, &height);
3071 switch (shell->panel_position) {
3072 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
3073 break;
3074 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
3075 y = view->output->height - height;
3076 break;
3077 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
3078 break;
3079 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
3080 x = view->output->width - width;
3081 break;
3082 }
3083
3084 configure_static_view(view, &shell->panel_layer, x, y);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003085}
3086
3087static void
David Fort50d962f2016-06-09 17:55:52 +02003088handle_panel_surface_destroy(struct wl_listener *listener, void *data)
3089{
3090 struct shell_output *output =
3091 container_of(listener, struct shell_output, panel_surface_listener);
3092
3093 weston_log("panel surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09003094 wl_list_remove(&output->panel_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02003095 output->panel_surface = NULL;
3096}
3097
3098
3099static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003100desktop_shell_set_panel(struct wl_client *client,
3101 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003102 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003103 struct wl_resource *surface_resource)
3104{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003105 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003106 struct weston_surface *surface =
3107 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003108 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02003109 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003110
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003111 if (surface->committed) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003112 wl_resource_post_error(surface_resource,
3113 WL_DISPLAY_ERROR_INVALID_OBJECT,
3114 "surface role already assigned");
3115 return;
3116 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003117
Jason Ekstranda7af7042013-10-12 22:38:11 -05003118 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3119 weston_view_destroy(view);
3120 view = weston_view_create(surface);
3121
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003122 surface->committed = panel_committed;
3123 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003124 weston_surface_set_label_func(surface, panel_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03003125 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02003126 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02003127
3128 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003129 if (sh_output->panel_surface) {
3130 /* The output already has a panel, tell our helper
3131 * there is no need for another one. */
3132 weston_desktop_shell_send_configure(resource, 0,
3133 surface_resource,
3134 0, 0);
3135 } else {
3136 weston_desktop_shell_send_configure(resource, 0,
3137 surface_resource,
3138 surface->output->width,
3139 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003140
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003141 sh_output->panel_surface = surface;
3142
3143 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
3144 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
3145 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003146}
3147
Pekka Paalanen8274d902014-08-06 19:36:51 +03003148static int
3149lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
3150{
3151 return snprintf(buf, len, "lock window");
3152}
3153
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003154static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003155lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003156{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003157 struct desktop_shell *shell = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003158 struct weston_view *view;
3159
3160 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003161
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003162 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003163 return;
3164
Jason Ekstranda7af7042013-10-12 22:38:11 -05003165 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003166
3167 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003168 weston_layer_entry_insert(&shell->lock_layer.view_list,
3169 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003170 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003171 surface->is_mapped = true;
3172 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003173 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003174 }
3175}
3176
3177static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003178handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003179{
Tiago Vignattibe143262012-04-16 17:31:41 +03003180 struct desktop_shell *shell =
3181 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003182
Martin Minarik6d118362012-06-07 18:01:59 +02003183 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003184 shell->lock_surface = NULL;
3185}
3186
3187static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003188desktop_shell_set_lock_surface(struct wl_client *client,
3189 struct wl_resource *resource,
3190 struct wl_resource *surface_resource)
3191{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003192 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003193 struct weston_surface *surface =
3194 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003195
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003196 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003197
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003198 if (!shell->locked)
3199 return;
3200
Pekka Paalanen98262232011-12-01 10:42:22 +02003201 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003202
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003203 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003204 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003205 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003206
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003207 weston_view_create(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003208 surface->committed = lock_surface_committed;
3209 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003210 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003211}
3212
3213static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003214resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003215{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003216 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003217
Quentin Glidic82681572016-12-17 13:40:51 +01003218 weston_layer_unset_position(&shell->lock_layer);
3219
3220 if (shell->showing_input_panels)
3221 weston_layer_set_position(&shell->input_panel_layer,
3222 WESTON_LAYER_POSITION_TOP_UI);
3223 weston_layer_set_position(&shell->fullscreen_layer,
3224 WESTON_LAYER_POSITION_FULLSCREEN);
3225 weston_layer_set_position(&shell->panel_layer,
3226 WESTON_LAYER_POSITION_UI);
3227 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003228
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003229 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003230
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003231 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003232 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003233 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003234}
3235
3236static void
3237desktop_shell_unlock(struct wl_client *client,
3238 struct wl_resource *resource)
3239{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003240 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003241
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003242 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003243
3244 if (shell->locked)
3245 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003246}
3247
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003248static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003249desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003250 struct wl_resource *resource,
3251 struct wl_resource *surface_resource)
3252{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003253 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003254
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003255 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003256 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003257}
3258
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003259static void
3260desktop_shell_desktop_ready(struct wl_client *client,
3261 struct wl_resource *resource)
3262{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003263 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003264
3265 shell_fade_startup(shell);
3266}
3267
Jonny Lamb765760d2014-08-20 15:53:19 +02003268static void
3269desktop_shell_set_panel_position(struct wl_client *client,
3270 struct wl_resource *resource,
3271 uint32_t position)
3272{
3273 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3274
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003275 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3276 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3277 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3278 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02003279 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003280 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02003281 "bad position argument");
3282 return;
3283 }
3284
3285 shell->panel_position = position;
3286}
3287
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003288static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04003289 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003290 desktop_shell_set_panel,
3291 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003292 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003293 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02003294 desktop_shell_desktop_ready,
3295 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04003296};
3297
3298static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003299move_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003300 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003301{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003302 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003303 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003304 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003305
Derek Foreman8ae2db52015-07-15 13:00:36 -05003306 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003307 return;
3308
Derek Foreman8ae2db52015-07-15 13:00:36 -05003309 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003310
Pekka Paalanen01388e22013-04-25 13:57:44 +03003311 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003312 if (surface == NULL)
3313 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003314
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003315 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003316 if (shsurf == NULL ||
3317 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3318 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003319 return;
3320
Derek Foreman794fa0e2015-07-15 13:00:38 -05003321 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003322}
3323
3324static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003325maximize_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003326 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003327{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003328 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003329 struct weston_surface *surface;
3330 struct shell_surface *shsurf;
3331
3332 surface = weston_surface_get_main_surface(focus);
3333 if (surface == NULL)
3334 return;
3335
3336 shsurf = get_shell_surface(surface);
3337 if (shsurf == NULL)
3338 return;
3339
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003340 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003341}
3342
3343static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003344fullscreen_binding(struct weston_keyboard *keyboard,
3345 const struct timespec *time, uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003346{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003347 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003348 struct weston_surface *surface;
3349 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003350 bool fullscreen;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003351
3352 surface = weston_surface_get_main_surface(focus);
3353 if (surface == NULL)
3354 return;
3355
3356 shsurf = get_shell_surface(surface);
3357 if (shsurf == NULL)
3358 return;
3359
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003360 fullscreen =
3361 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003362
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003363 set_fullscreen(shsurf, !fullscreen, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003364}
3365
3366static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003367touch_move_binding(struct weston_touch *touch, const struct timespec *time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01003368{
Derek Foreman362656b2014-09-04 10:23:05 -05003369 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01003370 struct weston_surface *surface;
3371 struct shell_surface *shsurf;
3372
Derek Foreman8ae2db52015-07-15 13:00:36 -05003373 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05003374 return;
3375
Derek Foreman8ae2db52015-07-15 13:00:36 -05003376 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003377 surface = weston_surface_get_main_surface(focus);
3378 if (surface == NULL)
3379 return;
3380
3381 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003382 if (shsurf == NULL ||
3383 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3384 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Neil Robertsaba0f252013-10-03 16:43:05 +01003385 return;
3386
Derek Foremanb7674ae2015-07-15 13:00:37 -05003387 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01003388}
3389
3390static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003391resize_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003392 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003393{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003394 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003395 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003396 uint32_t edges = 0;
3397 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003398 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003399
Derek Foreman8ae2db52015-07-15 13:00:36 -05003400 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003401 return;
3402
Derek Foreman8ae2db52015-07-15 13:00:36 -05003403 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003404
Pekka Paalanen01388e22013-04-25 13:57:44 +03003405 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003406 if (surface == NULL)
3407 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003408
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003409 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003410 if (shsurf == NULL ||
3411 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3412 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003413 return;
3414
Jason Ekstranda7af7042013-10-12 22:38:11 -05003415 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003416 wl_fixed_to_int(pointer->grab_x),
3417 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05003418 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003419
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003420 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003421 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003422 else if (x < 2 * surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003423 edges |= 0;
3424 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003425 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003426
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003427 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003428 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003429 else if (y < 2 * surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003430 edges |= 0;
3431 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003432 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003433
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003434 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003435}
3436
3437static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003438surface_opacity_binding(struct weston_pointer *pointer,
3439 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003440 struct weston_pointer_axis_event *event,
3441 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003442{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003443 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003444 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003445 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003446 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003447
Pekka Paalanen01388e22013-04-25 13:57:44 +03003448 /* XXX: broken for windows containing sub-surfaces */
3449 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003450 if (surface == NULL)
3451 return;
3452
3453 shsurf = get_shell_surface(surface);
3454 if (!shsurf)
3455 return;
3456
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003457 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003458
Jason Ekstranda7af7042013-10-12 22:38:11 -05003459 if (shsurf->view->alpha > 1.0)
3460 shsurf->view->alpha = 1.0;
3461 if (shsurf->view->alpha < step)
3462 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003463
Jason Ekstranda7af7042013-10-12 22:38:11 -05003464 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003465 weston_surface_damage(surface);
3466}
3467
3468static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003469do_zoom(struct weston_seat *seat, const struct timespec *time, uint32_t key,
3470 uint32_t axis, double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003471{
Derek Foreman8aeeac82015-01-30 13:24:36 -06003472 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05003473 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003474 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003475 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003476
Derek Foreman1281a362015-07-31 16:55:32 -05003477 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06003478 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3479 return;
3480 }
3481
Scott Moreauccbf29d2012-02-22 14:21:41 -07003482 wl_list_for_each(output, &compositor->output_list, link) {
3483 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05003484 wl_fixed_to_double(pointer->x),
3485 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003486 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003487 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003488 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003489 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003490 increment = -output->zoom.increment;
3491 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003492 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003493 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003494 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003495 else
3496 increment = 0;
3497
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003498 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003499
Scott Moreaue6603982012-06-11 13:07:51 -06003500 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003501 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003502 else if (output->zoom.level > output->zoom.max_level)
3503 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05003504
3505 if (!output->zoom.active) {
3506 if (output->zoom.level <= 0.0)
3507 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05003508 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003509 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003510
Scott Moreaue6603982012-06-11 13:07:51 -06003511 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003512
Jason Ekstranda7af7042013-10-12 22:38:11 -05003513 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003514 }
3515 }
3516}
3517
Scott Moreauccbf29d2012-02-22 14:21:41 -07003518static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003519zoom_axis_binding(struct weston_pointer *pointer, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003520 struct weston_pointer_axis_event *event,
3521 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003522{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003523 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003524}
3525
3526static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003527zoom_key_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003528 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003529{
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003530 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003531}
3532
3533static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003534terminate_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003535 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003536{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003537 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003538
Daniel Stone325fc2d2012-05-30 16:31:58 +01003539 wl_display_terminate(compositor->wl_display);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003540}
3541
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003542static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003543rotate_grab_motion(struct weston_pointer_grab *grab,
3544 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003545 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003546{
3547 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003548 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003549 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003550 struct shell_surface *shsurf = rotate->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003551 struct weston_surface *surface =
3552 weston_desktop_surface_get_surface(shsurf->desktop_surface);
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003553 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003554
Jonas Ådahld2510102014-10-05 21:39:14 +02003555 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003556
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003557 if (!shsurf)
3558 return;
3559
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003560 cx = 0.5f * surface->width;
3561 cy = 0.5f * surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003562
Daniel Stone37816df2012-05-16 18:45:18 +01003563 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3564 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003565 r = sqrtf(dx * dx + dy * dy);
3566
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003567 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003568 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003569
3570 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003571 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003572 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003573
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003574 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003575 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003576
3577 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003578 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003579 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003580 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003581 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003582
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003583 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003584 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003585 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003586 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003587 wl_list_init(&shsurf->rotation.transform.link);
3588 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003589 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003590 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003591
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003592 /* We need to adjust the position of the surface
3593 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003594 cposx = shsurf->view->geometry.x + cx;
3595 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003596 dposx = rotate->center.x - cposx;
3597 dposy = rotate->center.y - cposy;
3598 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003599 weston_view_set_position(shsurf->view,
3600 shsurf->view->geometry.x + dposx,
3601 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003602 }
3603
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003604 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003605 * lazily applies the damage due to rotation update.
3606 */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003607 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003608}
3609
3610static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003611rotate_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003612 const struct timespec *time,
3613 uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003614{
3615 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003616 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003617 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003618 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003619 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003620
Daniel Stone4dbadb12012-05-30 16:31:51 +01003621 if (pointer->button_count == 0 &&
3622 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003623 if (shsurf)
3624 weston_matrix_multiply(&shsurf->rotation.rotation,
3625 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003626 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003627 free(rotate);
3628 }
3629}
3630
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003631static void
3632rotate_grab_cancel(struct weston_pointer_grab *grab)
3633{
3634 struct rotate_grab *rotate =
3635 container_of(grab, struct rotate_grab, base.grab);
3636
3637 shell_grab_end(&rotate->base);
3638 free(rotate);
3639}
3640
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003641static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003642 noop_grab_focus,
3643 rotate_grab_motion,
3644 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003645 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003646 noop_grab_axis_source,
3647 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003648 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003649};
3650
3651static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003652surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003653{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003654 struct weston_surface *surface =
3655 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003656 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003657 float dx, dy;
3658 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003659
Pekka Paalanen460099f2012-01-20 16:48:25 +02003660 rotate = malloc(sizeof *rotate);
3661 if (!rotate)
3662 return;
3663
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003664 weston_view_to_global_float(shsurf->view,
3665 surface->width * 0.5f,
3666 surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003667 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003668
Derek Foreman74de4692015-07-15 13:00:39 -05003669 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3670 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003671 r = sqrtf(dx * dx + dy * dy);
3672 if (r > 20.0f) {
3673 struct weston_matrix inverse;
3674
3675 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003676 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003677 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003678
3679 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003680 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003681 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003682 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003683 weston_matrix_init(&rotate->rotation);
3684 }
3685
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003686 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003687 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003688}
3689
3690static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003691rotate_binding(struct weston_pointer *pointer, const struct timespec *time,
3692 uint32_t button, void *data)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003693{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003694 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003695 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003696 struct shell_surface *surface;
3697
Derek Foreman8ae2db52015-07-15 13:00:36 -05003698 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003699 return;
3700
Derek Foreman8ae2db52015-07-15 13:00:36 -05003701 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003702
Pekka Paalanen01388e22013-04-25 13:57:44 +03003703 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003704 if (base_surface == NULL)
3705 return;
3706
3707 surface = get_shell_surface(base_surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003708 if (surface == NULL ||
3709 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3710 weston_desktop_surface_get_maximized(surface->desktop_surface))
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003711 return;
3712
Derek Foreman74de4692015-07-15 13:00:39 -05003713 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003714}
3715
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003716/* Move all fullscreen layers down to the current workspace and hide their
3717 * black views. The surfaces' state is set to both fullscreen and lowered,
3718 * and this is reversed when such a surface is re-configured, see
3719 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3720 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003721 * lowering_output = NULL - Lower on all outputs, else only lower on the
3722 * specified output.
3723 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003724 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003725 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003726void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003727lower_fullscreen_layer(struct desktop_shell *shell,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003728 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003729{
3730 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003731 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003732
3733 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003734 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003735 &shell->fullscreen_layer.view_list.link,
3736 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003737 struct shell_surface *shsurf = get_shell_surface(view->surface);
3738
3739 if (!shsurf)
3740 continue;
3741
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003742 /* Only lower surfaces which have lowering_output as their fullscreen
3743 * output, unless a NULL output asks for lowering on all outputs.
3744 */
3745 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3746 continue;
3747
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003748 /* We can have a non-fullscreen popup for a fullscreen surface
3749 * in the fullscreen layer. */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003750 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003751 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003752 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3753 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07003754 weston_view_damage_below(shsurf->fullscreen.black_view);
3755
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003756 }
3757
3758 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003759 weston_layer_entry_remove(&view->layer_link);
3760 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003761 weston_view_damage_below(view);
3762 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003763
3764 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003765 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003766}
3767
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003768void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003769activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003770 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003771{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003772 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003773 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003774 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003775 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003776 struct weston_surface *old_es;
Rafael Antognolli03b16592013-12-03 15:35:42 -02003777 struct shell_surface *shsurf;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003778
Pekka Paalanen01388e22013-04-25 13:57:44 +03003779 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003780 shsurf = get_shell_surface(main_surface);
3781 assert(shsurf);
3782
3783 /* Only demote fullscreen surfaces on the output of activated shsurf.
3784 * Leave fullscreen surfaces on unrelated outputs alone. */
Pekka Paalanen87860c22018-05-02 10:21:57 +02003785 if (shsurf->output)
3786 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003787
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003788 weston_view_activate(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003789
Jonas Ådahl8538b222012-08-29 22:13:03 +02003790 state = ensure_focus_state(shell, seat);
3791 if (state == NULL)
3792 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003793
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003794 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08003795 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003796
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003797 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3798 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02003799 shell_configure_fullscreen(shsurf);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003800
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01003801 /* Update the surface’s layer. This brings it to the top of the stacking
3802 * order as appropriate. */
3803 shell_surface_update_layer(shsurf);
3804
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003805 if (shell->focus_animation_type != ANIMATION_NONE) {
3806 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003807 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003808 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003809}
3810
Alex Wu21858432012-04-01 20:13:08 +08003811/* no-op func for checking black surface */
3812static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003813black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08003814{
3815}
3816
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003817static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003818is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08003819{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003820 struct weston_surface *surface = view->surface;
3821
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003822 if (surface->committed == black_surface_committed) {
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003823 if (fs_view)
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003824 *fs_view = surface->committed_private;
Alex Wu21858432012-04-01 20:13:08 +08003825 return true;
3826 }
3827 return false;
3828}
3829
Kristian Høgsberg75840622011-09-06 13:48:16 -04003830static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003831activate_binding(struct weston_seat *seat,
3832 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003833 struct weston_view *focus_view,
3834 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003835{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003836 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003837 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003838
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003839 if (!focus_view)
3840 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003841
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003842 if (is_black_surface_view(focus_view, &main_view))
3843 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08003844
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003845 main_surface = weston_surface_get_main_surface(focus_view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003846 if (!get_shell_surface(main_surface))
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003847 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003848
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003849 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01003850}
3851
3852static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003853click_to_activate_binding(struct weston_pointer *pointer,
3854 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003855 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003856{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003857 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003858 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003859 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08003860 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003861
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003862 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003863 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003864 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01003865}
3866
3867static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003868touch_to_activate_binding(struct weston_touch *touch,
3869 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003870 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003871{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003872 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003873 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003874 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08003875 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003876
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003877 activate_binding(touch->seat, data, touch->focus,
3878 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003879}
3880
3881static void
Neil Robertsb4a91702014-04-09 16:33:32 +01003882unfocus_all_seats(struct desktop_shell *shell)
3883{
3884 struct weston_seat *seat, *next;
3885
3886 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003887 struct weston_keyboard *keyboard =
3888 weston_seat_get_keyboard(seat);
3889
3890 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01003891 continue;
3892
Derek Foreman1281a362015-07-31 16:55:32 -05003893 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01003894 }
3895}
3896
3897static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003898lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003899{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003900 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003901
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003902 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003903 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003904 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003905 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003906
3907 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003908
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003909 /* Hide all surfaces by removing the fullscreen, panel and
3910 * toplevel layers. This way nothing else can show or receive
3911 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003912
Quentin Glidic82681572016-12-17 13:40:51 +01003913 weston_layer_unset_position(&shell->panel_layer);
3914 weston_layer_unset_position(&shell->fullscreen_layer);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003915 if (shell->showing_input_panels)
Quentin Glidic82681572016-12-17 13:40:51 +01003916 weston_layer_unset_position(&shell->input_panel_layer);
3917 weston_layer_unset_position(&ws->layer);
3918
3919 weston_layer_set_position(&shell->lock_layer,
3920 WESTON_LAYER_POSITION_LOCK);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003921
Derek Foreman004b4a12015-07-20 16:28:13 -05003922 weston_compositor_sleep(shell->compositor);
3923
Neil Robertsb4a91702014-04-09 16:33:32 +01003924 /* Remove the keyboard focus on all seats. This will be
3925 * restored to the workspace's saved state via
3926 * restore_focus_state when the compositor is unlocked */
3927 unfocus_all_seats(shell);
3928
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003929 /* TODO: disable bindings that should not work while locked. */
3930
3931 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003932}
3933
3934static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003935unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003936{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003937 struct wl_resource *shell_resource;
3938
Pekka Paalanend81c2162011-11-16 13:47:34 +02003939 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003940 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003941 return;
3942 }
3943
3944 /* If desktop-shell client has gone away, unlock immediately. */
3945 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003946 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003947 return;
3948 }
3949
3950 if (shell->prepare_event_sent)
3951 return;
3952
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003953 shell_resource = shell->child.desktop_shell;
3954 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003955 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003956}
3957
3958static void
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003959shell_fade_done_for_output(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003960{
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003961 struct shell_output *shell_output = data;
3962 struct desktop_shell *shell = shell_output->shell;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003963
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003964 shell_output->fade.animation = NULL;
3965 switch (shell_output->fade.type) {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003966 case FADE_IN:
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003967 weston_surface_destroy(shell_output->fade.view->surface);
3968 shell_output->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003969 break;
3970 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003971 lock(shell);
3972 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00003973 default:
3974 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003975 }
3976}
3977
Jason Ekstranda7af7042013-10-12 22:38:11 -05003978static struct weston_view *
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003979shell_fade_create_surface_for_output(struct desktop_shell *shell, struct shell_output *shell_output)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003980{
3981 struct weston_compositor *compositor = shell->compositor;
3982 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003983 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003984
3985 surface = weston_surface_create(compositor);
3986 if (!surface)
3987 return NULL;
3988
Jason Ekstranda7af7042013-10-12 22:38:11 -05003989 view = weston_view_create(surface);
3990 if (!view) {
3991 weston_surface_destroy(surface);
3992 return NULL;
3993 }
3994
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003995 weston_surface_set_size(surface, shell_output->output->width, shell_output->output->height);
3996 weston_view_set_position(view, shell_output->output->x, shell_output->output->y);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003997 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003998 weston_layer_entry_insert(&compositor->fade_layer.view_list,
3999 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004000 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02004001 surface->is_mapped = true;
4002 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004003
Jason Ekstranda7af7042013-10-12 22:38:11 -05004004 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004005}
4006
4007static void
4008shell_fade(struct desktop_shell *shell, enum fade_type type)
4009{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004010 float tint;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004011 struct shell_output *shell_output;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004012
4013 switch (type) {
4014 case FADE_IN:
4015 tint = 0.0;
4016 break;
4017 case FADE_OUT:
4018 tint = 1.0;
4019 break;
4020 default:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07004021 weston_log("shell: invalid fade type\n");
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004022 return;
4023 }
4024
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004025 /* Create a separate fade surface for each output */
4026 wl_list_for_each(shell_output, &shell->output_list, link) {
4027 shell_output->fade.type = type;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004028
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004029 if (shell_output->fade.view == NULL) {
4030 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4031 if (!shell_output->fade.view)
4032 continue;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004033
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004034 shell_output->fade.view->alpha = 1.0 - tint;
4035 weston_view_update_transform(shell_output->fade.view);
4036 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004037
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004038 if (shell_output->fade.view->output == NULL) {
4039 /* If the black view gets a NULL output, we lost the
4040 * last output and we'll just cancel the fade. This
4041 * happens when you close the last window under the
4042 * X11 or Wayland backends. */
4043 shell->locked = false;
4044 weston_surface_destroy(shell_output->fade.view->surface);
4045 shell_output->fade.view = NULL;
4046 } else if (shell_output->fade.animation) {
4047 weston_fade_update(shell_output->fade.animation, tint);
4048 } else {
4049 shell_output->fade.animation =
4050 weston_fade_run(shell_output->fade.view,
4051 1.0 - tint, tint, 300.0,
4052 shell_fade_done_for_output, shell_output);
4053 }
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004054 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004055}
4056
4057static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004058do_shell_fade_startup(void *data)
4059{
4060 struct desktop_shell *shell = data;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004061 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004062
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004063 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004064 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004065 } else {
4066 weston_log("desktop shell: "
4067 "unexpected fade-in animation type %d\n",
4068 shell->startup_animation_type);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004069 wl_list_for_each(shell_output, &shell->output_list, link) {
4070 weston_surface_destroy(shell_output->fade.view->surface);
4071 shell_output->fade.view = NULL;
4072 }
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004073 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004074}
4075
4076static void
4077shell_fade_startup(struct desktop_shell *shell)
4078{
4079 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004080 struct shell_output *shell_output;
4081 bool has_fade = false;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004082
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004083 wl_list_for_each(shell_output, &shell->output_list, link) {
4084 if (!shell_output->fade.startup_timer)
4085 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004086
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004087 wl_event_source_remove(shell_output->fade.startup_timer);
4088 shell_output->fade.startup_timer = NULL;
4089 has_fade = true;
4090 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004091
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004092 if (has_fade) {
4093 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4094 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4095 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004096}
4097
4098static int
4099fade_startup_timeout(void *data)
4100{
4101 struct desktop_shell *shell = data;
4102
4103 shell_fade_startup(shell);
4104 return 0;
4105}
4106
4107static void
4108shell_fade_init(struct desktop_shell *shell)
4109{
4110 /* Make compositor output all black, and wait for the desktop-shell
4111 * client to signal it is ready, then fade in. The timer triggers a
4112 * fade-in, in case the desktop-shell client takes too long.
4113 */
4114
4115 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004116 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004117
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004118 if (shell->startup_animation_type == ANIMATION_NONE)
4119 return;
4120
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004121 wl_list_for_each(shell_output, &shell->output_list, link) {
4122 if (shell_output->fade.view != NULL) {
4123 weston_log("%s: warning: fade surface already exists\n",
4124 __func__);
4125 continue;
4126 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004127
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004128 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4129 if (!shell_output->fade.view)
4130 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004131
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004132 weston_view_update_transform(shell_output->fade.view);
4133 weston_surface_damage(shell_output->fade.view->surface);
4134
4135 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4136 shell_output->fade.startup_timer =
4137 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4138 wl_event_source_timer_update(shell_output->fade.startup_timer, 15000);
4139 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004140}
4141
4142static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004143idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004144{
4145 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004146 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004147
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004148 struct weston_seat *seat;
4149
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004150 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4151 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004152
4153 shell_fade(shell, FADE_OUT);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004154 /* lock() is called from shell_fade_done_for_output() */
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004155}
4156
4157static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004158wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004159{
4160 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004161 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004162
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004163 unlock(shell);
4164}
4165
4166static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004167transform_handler(struct wl_listener *listener, void *data)
4168{
4169 struct weston_surface *surface = data;
4170 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004171 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004172 int x, y;
4173
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004174 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004175 return;
4176
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004177 api = shsurf->shell->xwayland_surface_api;
4178 if (!api) {
4179 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
4180 shsurf->shell->xwayland_surface_api = api;
4181 }
4182
4183 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004184 return;
4185
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004186 if (!weston_view_is_mapped(shsurf->view))
4187 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004188
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004189 x = shsurf->view->geometry.x;
4190 y = shsurf->view->geometry.y;
4191
4192 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004193}
4194
4195static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004196center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004197{
Giulio Camuffob8366642013-04-25 13:57:46 +03004198 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004199 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004200
Pekka Paalanen30aa5972018-05-02 10:21:56 +02004201 if (!output) {
4202 weston_view_set_position(view, 0, 0);
4203 return;
4204 }
4205
Jason Ekstranda7af7042013-10-12 22:38:11 -05004206 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004207
4208 x = output->x + (output->width - width) / 2 - surf_x / 2;
4209 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004210
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004211 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004212}
4213
4214static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004215weston_view_set_initial_position(struct weston_view *view,
4216 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004217{
4218 struct weston_compositor *compositor = shell->compositor;
4219 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004220 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004221 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004222 struct weston_output *output, *target_output = NULL;
4223 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004224 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004225
4226 /* As a heuristic place the new window on the same output as the
4227 * pointer. Falling back to the output containing 0, 0.
4228 *
4229 * TODO: Do something clever for touch too?
4230 */
4231 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004232 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4233
4234 if (pointer) {
4235 ix = wl_fixed_to_int(pointer->x);
4236 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004237 break;
4238 }
4239 }
4240
4241 wl_list_for_each(output, &compositor->output_list, link) {
4242 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4243 target_output = output;
4244 break;
4245 }
4246 }
4247
4248 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004249 weston_view_set_position(view, 10 + random() % 400,
4250 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004251 return;
4252 }
4253
4254 /* Valid range within output where the surface will still be onscreen.
4255 * If this is negative it means that the surface is bigger than
4256 * output.
4257 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004258 get_output_work_area(shell, target_output, &area);
4259
Derek Foremanf814c5d2015-04-15 12:23:54 -05004260 x = area.x;
4261 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004262 range_x = area.width - view->surface->width;
4263 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004264
Rob Bradford4cb88c72012-08-13 15:18:44 +01004265 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004266 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004267
4268 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004269 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004270
Jason Ekstranda7af7042013-10-12 22:38:11 -05004271 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004272}
4273
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004274static bool
4275check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4276{
4277 struct timespec now;
4278
4279 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4280 return false;
4281
4282 /*
4283 * If the shell helper client dies before the session has been
4284 * up for roughly 30 seconds, better just make Weston shut down,
4285 * because the user likely has no way to interact with the desktop
4286 * anyway.
4287 */
4288 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4289 weston_log("Error: %s apparently cannot run at all.\n",
4290 shell->client);
4291 weston_log_continue(STAMP_SPACE "Quitting...");
4292 wl_display_terminate(shell->compositor->wl_display);
4293
4294 return true;
4295 }
4296
4297 return false;
4298}
4299
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004300static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004301
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004302static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004303respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004304{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004305 struct timespec time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004306
4307 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004308 weston_compositor_get_time(&time);
4309 if (timespec_sub_to_msec(&time, &shell->child.deathstamp) > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004310 shell->child.deathstamp = time;
4311 shell->child.deathcount = 0;
4312 }
4313
4314 shell->child.deathcount++;
4315 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004316 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004317 return;
4318 }
4319
Pekka Paalanen826dc142014-08-27 12:11:53 +03004320 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004321 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004322}
4323
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004324static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004325desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4326{
4327 struct desktop_shell *shell;
4328
4329 shell = container_of(listener, struct desktop_shell,
4330 child.client_destroy_listener);
4331
Pekka Paalanen826dc142014-08-27 12:11:53 +03004332 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004333 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004334 /*
4335 * unbind_desktop_shell() will reset shell->child.desktop_shell
4336 * before the respawned process has a chance to create a new
4337 * desktop_shell object, because we are being called from the
4338 * wl_client destructor which destroys all wl_resources before
4339 * returning.
4340 */
4341
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004342 if (!check_desktop_shell_crash_too_early(shell))
4343 respawn_desktop_shell_process(shell);
4344
Pekka Paalanen826dc142014-08-27 12:11:53 +03004345 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004346}
4347
4348static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004349launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004350{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004351 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004352
Pekka Paalanen826dc142014-08-27 12:11:53 +03004353 shell->child.client = weston_client_start(shell->compositor,
4354 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004355
Arnaud Vrac42631192014-08-25 20:56:46 +02004356 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004357 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004358 return;
4359 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004360
4361 shell->child.client_destroy_listener.notify =
4362 desktop_shell_client_destroy;
4363 wl_client_add_destroy_listener(shell->child.client,
4364 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004365}
4366
4367static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004368unbind_desktop_shell(struct wl_resource *resource)
4369{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004370 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004371
4372 if (shell->locked)
4373 resume_desktop(shell);
4374
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004375 shell->child.desktop_shell = NULL;
4376 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004377}
4378
4379static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004380bind_desktop_shell(struct wl_client *client,
4381 void *data, uint32_t version, uint32_t id)
4382{
Tiago Vignattibe143262012-04-16 17:31:41 +03004383 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004384 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004385
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004386 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4387 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004388
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004389 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004390 wl_resource_set_implementation(resource,
4391 &desktop_shell_implementation,
4392 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004393 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004394 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004395 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004396
4397 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4398 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004399}
4400
Kristian Høgsberg07045392012-02-19 18:52:44 -05004401struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004402 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004403 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004404 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004405 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004406 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004407};
4408
4409static void
4410switcher_next(struct switcher *switcher)
4411{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004412 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004413 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004414 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004415 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004416
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004417 /* temporary re-display minimized surfaces */
4418 struct weston_view *tmp;
4419 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004420 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4421 weston_layer_entry_remove(&view->layer_link);
4422 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004423 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4424 *minimized = view;
4425 }
4426
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004427 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004428 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004429 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004430 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004431 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004432 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004433 next = view;
4434 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004435 view->alpha = 0.25;
4436 weston_view_geometry_dirty(view);
4437 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004438 }
Alex Wu1659daa2012-04-01 20:13:09 +08004439
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004440 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004441 view->alpha = 0.25;
4442 weston_view_geometry_dirty(view);
4443 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004444 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004445 }
4446
4447 if (next == NULL)
4448 next = first;
4449
Alex Wu07b26062012-03-12 16:06:01 +08004450 if (next == NULL)
4451 return;
4452
Kristian Høgsberg07045392012-02-19 18:52:44 -05004453 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004454 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004455
4456 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004457 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004458 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004459
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004460 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004461 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004462 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004463}
4464
4465static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004466switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004467{
4468 struct switcher *switcher =
4469 container_of(listener, struct switcher, listener);
4470
4471 switcher_next(switcher);
4472}
4473
4474static void
Daniel Stone351eb612012-05-31 15:27:47 -04004475switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004476{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004477 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004478 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004479 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004480
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004481 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004482 if (is_focus_view(view))
4483 continue;
4484
Jason Ekstranda7af7042013-10-12 22:38:11 -05004485 view->alpha = 1.0;
4486 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004487 }
4488
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004489 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004490 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004491 keyboard->seat,
4492 WESTON_ACTIVATE_FLAG_CONFIGURE);
4493 }
4494
Kristian Høgsberg07045392012-02-19 18:52:44 -05004495 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004496 weston_keyboard_end_grab(keyboard);
4497 if (keyboard->input_method_resource)
4498 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004499
4500 /* re-hide surfaces that were temporary shown during the switch */
4501 struct weston_view **minimized;
4502 wl_array_for_each(minimized, &switcher->minimized_array) {
4503 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004504 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004505 weston_layer_entry_remove(&(*minimized)->layer_link);
4506 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004507 weston_view_damage_below(*minimized);
4508 }
4509 }
4510 wl_array_release(&switcher->minimized_array);
4511
Kristian Høgsberg07045392012-02-19 18:52:44 -05004512 free(switcher);
4513}
4514
4515static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004516switcher_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004517 const struct timespec *time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004518{
4519 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004520 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004521
Daniel Stonec9785ea2012-05-30 16:31:52 +01004522 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004523 switcher_next(switcher);
4524}
4525
4526static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004527switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004528 uint32_t mods_depressed, uint32_t mods_latched,
4529 uint32_t mods_locked, uint32_t group)
4530{
4531 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004532 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004533
Daniel Stone351eb612012-05-31 15:27:47 -04004534 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4535 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004536}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004537
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004538static void
4539switcher_cancel(struct weston_keyboard_grab *grab)
4540{
4541 struct switcher *switcher = container_of(grab, struct switcher, grab);
4542
4543 switcher_destroy(switcher);
4544}
4545
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004546static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004547 switcher_key,
4548 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004549 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004550};
4551
4552static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004553switcher_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004554 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004555{
Tiago Vignattibe143262012-04-16 17:31:41 +03004556 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004557 struct switcher *switcher;
4558
4559 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004560 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004561 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004562 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004563 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004564 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004565
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004566 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004567 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004568 weston_keyboard_start_grab(keyboard, &switcher->grab);
4569 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004570 switcher_next(switcher);
4571}
4572
Pekka Paalanen3c647232011-12-22 13:43:43 +02004573static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004574backlight_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004575 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004576{
4577 struct weston_compositor *compositor = data;
4578 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004579 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004580
4581 /* TODO: we're limiting to simple use cases, where we assume just
4582 * control on the primary display. We'd have to extend later if we
4583 * ever get support for setting backlights on random desktop LCD
4584 * panels though */
4585 output = get_default_output(compositor);
4586 if (!output)
4587 return;
4588
4589 if (!output->set_backlight)
4590 return;
4591
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004592 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4593 backlight_new = output->backlight_current - 25;
4594 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4595 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004596
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004597 if (backlight_new < 5)
4598 backlight_new = 5;
4599 if (backlight_new > 255)
4600 backlight_new = 255;
4601
4602 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004603 output->set_backlight(output, output->backlight_current);
4604}
4605
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004606static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004607force_kill_binding(struct weston_keyboard *keyboard,
4608 const struct timespec *time, uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004609{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004610 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004611 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004612 struct desktop_shell *shell = data;
4613 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004614 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004615
Derek Foreman8ae2db52015-07-15 13:00:36 -05004616 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004617 if (!focus_surface)
4618 return;
4619
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004620 wl_signal_emit(&compositor->kill_signal, focus_surface);
4621
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004622 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004623 wl_client_get_credentials(client, &pid, NULL, NULL);
4624
4625 /* Skip clients that we launched ourselves (the credentials of
4626 * the socketpair is ours) */
4627 if (pid == getpid())
4628 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004629
Daniel Stone325fc2d2012-05-30 16:31:58 +01004630 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004631}
4632
4633static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004634workspace_up_binding(struct weston_keyboard *keyboard,
4635 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004636{
4637 struct desktop_shell *shell = data;
4638 unsigned int new_index = shell->workspaces.current;
4639
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004640 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004641 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004642 if (new_index != 0)
4643 new_index--;
4644
4645 change_workspace(shell, new_index);
4646}
4647
4648static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004649workspace_down_binding(struct weston_keyboard *keyboard,
4650 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004651{
4652 struct desktop_shell *shell = data;
4653 unsigned int new_index = shell->workspaces.current;
4654
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004655 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004656 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004657 if (new_index < shell->workspaces.num - 1)
4658 new_index++;
4659
4660 change_workspace(shell, new_index);
4661}
4662
4663static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004664workspace_f_binding(struct weston_keyboard *keyboard,
4665 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004666{
4667 struct desktop_shell *shell = data;
4668 unsigned int new_index;
4669
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004670 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004671 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004672 new_index = key - KEY_F1;
4673 if (new_index >= shell->workspaces.num)
4674 new_index = shell->workspaces.num - 1;
4675
4676 change_workspace(shell, new_index);
4677}
4678
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004679static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004680workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004681 const struct timespec *time, uint32_t key,
4682 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004683{
4684 struct desktop_shell *shell = data;
4685 unsigned int new_index = shell->workspaces.current;
4686
4687 if (shell->locked)
4688 return;
4689
4690 if (new_index != 0)
4691 new_index--;
4692
Derek Foreman8ae2db52015-07-15 13:00:36 -05004693 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004694}
4695
4696static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004697workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004698 const struct timespec *time, uint32_t key,
4699 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004700{
4701 struct desktop_shell *shell = data;
4702 unsigned int new_index = shell->workspaces.current;
4703
4704 if (shell->locked)
4705 return;
4706
4707 if (new_index < shell->workspaces.num - 1)
4708 new_index++;
4709
Derek Foreman8ae2db52015-07-15 13:00:36 -05004710 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004711}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004712
4713static void
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004714shell_reposition_view_on_output_destroy(struct weston_view *view)
4715{
4716 struct weston_output *output, *first_output;
4717 struct weston_compositor *ec = view->surface->compositor;
4718 struct shell_surface *shsurf;
4719 float x, y;
4720 int visible;
4721
4722 x = view->geometry.x;
4723 y = view->geometry.y;
4724
4725 /* At this point the destroyed output is not in the list anymore.
4726 * If the view is still visible somewhere, we leave where it is,
4727 * otherwise, move it to the first output. */
4728 visible = 0;
4729 wl_list_for_each(output, &ec->output_list, link) {
4730 if (pixman_region32_contains_point(&output->region,
4731 x, y, NULL)) {
4732 visible = 1;
4733 break;
4734 }
4735 }
4736
4737 if (!visible) {
4738 first_output = container_of(ec->output_list.next,
4739 struct weston_output, link);
4740
4741 x = first_output->x + first_output->width / 4;
4742 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004743
4744 weston_view_set_position(view, x, y);
4745 } else {
4746 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004747 }
4748
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004749
4750 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004751 if (!shsurf)
4752 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004753
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004754 shsurf->saved_position_valid = false;
4755 set_maximized(shsurf, false);
4756 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004757}
4758
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004759void
4760shell_for_each_layer(struct desktop_shell *shell,
4761 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004762{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004763 struct workspace **ws;
4764
4765 func(shell, &shell->fullscreen_layer, data);
4766 func(shell, &shell->panel_layer, data);
4767 func(shell, &shell->background_layer, data);
4768 func(shell, &shell->lock_layer, data);
4769 func(shell, &shell->input_panel_layer, data);
4770
4771 wl_array_for_each(ws, &shell->workspaces.array)
4772 func(shell, &(*ws)->layer, data);
4773}
4774
4775static void
4776shell_output_destroy_move_layer(struct desktop_shell *shell,
4777 struct weston_layer *layer,
4778 void *data)
4779{
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004780 struct weston_view *view;
4781
Marius Vladea40d6d2018-02-08 18:46:42 +02004782 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004783 shell_reposition_view_on_output_destroy(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004784}
4785
4786static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004787handle_output_destroy(struct wl_listener *listener, void *data)
4788{
4789 struct shell_output *output_listener =
4790 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004791 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004792
Pekka Paalanen9350bfd2018-02-13 16:18:05 +02004793 shell_for_each_layer(shell, shell_output_destroy_move_layer, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004794
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09004795 if (output_listener->panel_surface)
4796 wl_list_remove(&output_listener->panel_surface_listener.link);
4797 if (output_listener->background_surface)
4798 wl_list_remove(&output_listener->background_surface_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004799 wl_list_remove(&output_listener->destroy_listener.link);
4800 wl_list_remove(&output_listener->link);
4801 free(output_listener);
4802}
4803
4804static void
David Fort50d962f2016-06-09 17:55:52 +02004805shell_resize_surface_to_output(struct desktop_shell *shell,
4806 struct weston_surface *surface,
4807 const struct weston_output *output)
4808{
4809 if (!surface)
4810 return;
4811
4812 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4813 surface->resource,
4814 output->width,
4815 output->height);
4816}
4817
4818
4819static void
4820handle_output_resized(struct wl_listener *listener, void *data)
4821{
4822 struct desktop_shell *shell =
4823 container_of(listener, struct desktop_shell, resized_listener);
4824 struct weston_output *output = (struct weston_output *)data;
4825 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4826
4827 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4828 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4829}
4830
4831static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004832create_shell_output(struct desktop_shell *shell,
4833 struct weston_output *output)
4834{
4835 struct shell_output *shell_output;
4836
4837 shell_output = zalloc(sizeof *shell_output);
4838 if (shell_output == NULL)
4839 return;
4840
4841 shell_output->output = output;
4842 shell_output->shell = shell;
4843 shell_output->destroy_listener.notify = handle_output_destroy;
4844 wl_signal_add(&output->destroy_signal,
4845 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004846 wl_list_insert(shell->output_list.prev, &shell_output->link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004847}
4848
4849static void
4850handle_output_create(struct wl_listener *listener, void *data)
4851{
4852 struct desktop_shell *shell =
4853 container_of(listener, struct desktop_shell, output_create_listener);
4854 struct weston_output *output = (struct weston_output *)data;
4855
4856 create_shell_output(shell, output);
4857}
4858
4859static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004860handle_output_move_layer(struct desktop_shell *shell,
4861 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004862{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004863 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004864 struct weston_view *view;
4865 float x, y;
4866
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004867 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004868 if (view->output != output)
4869 continue;
4870
4871 x = view->geometry.x + output->move_x;
4872 y = view->geometry.y + output->move_y;
4873 weston_view_set_position(view, x, y);
4874 }
4875}
4876
4877static void
4878handle_output_move(struct wl_listener *listener, void *data)
4879{
4880 struct desktop_shell *shell;
4881
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004882 shell = container_of(listener, struct desktop_shell,
4883 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004884
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004885 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004886}
4887
4888static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004889setup_output_destroy_handler(struct weston_compositor *ec,
4890 struct desktop_shell *shell)
4891{
4892 struct weston_output *output;
4893
4894 wl_list_init(&shell->output_list);
4895 wl_list_for_each(output, &ec->output_list, link)
4896 create_shell_output(shell, output);
4897
4898 shell->output_create_listener.notify = handle_output_create;
4899 wl_signal_add(&ec->output_created_signal,
4900 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004901
4902 shell->output_move_listener.notify = handle_output_move;
4903 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08004904}
4905
4906static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004907shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004908{
Tiago Vignattibe143262012-04-16 17:31:41 +03004909 struct desktop_shell *shell =
4910 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004911 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08004912 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004913
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08004914 /* Force state to unlocked so we don't try to fade */
4915 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004916
4917 if (shell->child.client) {
4918 /* disable respawn */
4919 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004920 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03004921 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004922
Harsha M Mb8b2c722018-08-07 19:05:02 +05304923 wl_list_remove(&shell->destroy_listener.link);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004924 wl_list_remove(&shell->idle_listener.link);
4925 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004926 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004927
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004928 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004929 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004930
Xiong Zhang6b481422013-10-23 13:58:32 +08004931 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
4932 wl_list_remove(&shell_output->destroy_listener.link);
4933 wl_list_remove(&shell_output->link);
4934 free(shell_output);
4935 }
4936
4937 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07004938 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02004939 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004940
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004941 wl_array_for_each(ws, &shell->workspaces.array)
4942 workspace_destroy(*ws);
4943 wl_array_release(&shell->workspaces.array);
4944
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004945 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004946 free(shell);
4947}
4948
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004949static void
4950shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4951{
4952 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004953 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004954
Bob Ham744e6532016-01-12 10:21:48 +00004955 if (shell->allow_zap)
4956 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4957 MODIFIER_CTRL | MODIFIER_ALT,
4958 terminate_binding, ec);
4959
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004960 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004961 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4962 click_to_activate_binding,
4963 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07004964 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
4965 click_to_activate_binding,
4966 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01004967 weston_compositor_add_touch_binding(ec, 0,
4968 touch_to_activate_binding,
4969 shell);
Bob Ham553d1242016-01-12 10:21:49 +00004970 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4971 backlight_binding, ec);
4972 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4973 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004974
4975 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00004976 if (shell->exposay_modifier)
4977 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
4978 exposay_binding, shell);
4979
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004980 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00004981 if (!mod)
4982 return;
4983
Ian Ray13404c42017-09-18 15:22:01 +03004984 /* This binding is not configurable, but is only enabled if there is a
4985 * valid binding modifier. */
4986 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4987 MODIFIER_SUPER | MODIFIER_ALT,
4988 surface_opacity_binding, NULL);
4989
Ian Rayab7c0b62017-09-18 15:22:00 +03004990 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4991 mod, zoom_axis_binding,
4992 NULL);
4993
Daniel Stone325fc2d2012-05-30 16:31:58 +01004994 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4995 zoom_key_binding, NULL);
4996 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4997 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08004998 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
4999 maximize_binding, NULL);
5000 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5001 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005002 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5003 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005004 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05005005 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005006 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08005007 weston_compositor_add_button_binding(ec, BTN_LEFT,
5008 mod | MODIFIER_SHIFT,
5009 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005010
5011 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05005012 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005013 rotate_binding, NULL);
5014
Daniel Stone325fc2d2012-05-30 16:31:58 +01005015 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5016 shell);
5017 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5018 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005019 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5020 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005021 weston_compositor_add_key_binding(ec, KEY_K, mod,
5022 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005023 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5024 workspace_up_binding, shell);
5025 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5026 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005027 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5028 workspace_move_surface_up_binding,
5029 shell);
5030 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5031 workspace_move_surface_down_binding,
5032 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005033
5034 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5035 if (shell->workspaces.num > 1) {
5036 num_workspace_bindings = shell->workspaces.num;
5037 if (num_workspace_bindings > 6)
5038 num_workspace_bindings = 6;
5039 for (i = 0; i < num_workspace_bindings; i++)
5040 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5041 workspace_f_binding,
5042 shell);
5043 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005044
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02005045 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005046}
5047
Jason Ekstrand024177c2014-04-21 19:42:58 -05005048static void
5049handle_seat_created(struct wl_listener *listener, void *data)
5050{
5051 struct weston_seat *seat = data;
5052
5053 create_shell_seat(seat);
5054}
5055
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005056WL_EXPORT int
Quentin Glidicda01c1d2016-12-02 14:17:08 +01005057wet_shell_init(struct weston_compositor *ec,
5058 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005059{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005060 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005061 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005062 struct workspace **pws;
5063 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005064 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005065
Peter Huttererf3d62272013-08-08 11:57:05 +10005066 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005067 if (shell == NULL)
5068 return -1;
5069
Kristian Høgsberg75840622011-09-06 13:48:16 -04005070 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005071
5072 shell->destroy_listener.notify = shell_destroy;
5073 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005074 shell->idle_listener.notify = idle_handler;
5075 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5076 shell->wake_listener.notify = wake_handler;
5077 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005078 shell->transform_listener.notify = transform_handler;
5079 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005080
Quentin Glidic82681572016-12-17 13:40:51 +01005081 weston_layer_init(&shell->fullscreen_layer, ec);
5082 weston_layer_init(&shell->panel_layer, ec);
5083 weston_layer_init(&shell->background_layer, ec);
5084 weston_layer_init(&shell->lock_layer, ec);
5085 weston_layer_init(&shell->input_panel_layer, ec);
5086
5087 weston_layer_set_position(&shell->fullscreen_layer,
5088 WESTON_LAYER_POSITION_FULLSCREEN);
5089 weston_layer_set_position(&shell->panel_layer,
5090 WESTON_LAYER_POSITION_UI);
5091 weston_layer_set_position(&shell->background_layer,
5092 WESTON_LAYER_POSITION_BACKGROUND);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005093
5094 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005095 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005096
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005097 if (input_panel_setup(shell) < 0)
5098 return -1;
5099
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03005100 shell->text_backend = text_backend_init(ec);
5101 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00005102 return -1;
5103
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005104 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005105
Daniel Stonedf8133b2013-11-19 11:37:14 +01005106 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5107 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5108
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005109 for (i = 0; i < shell->workspaces.num; i++) {
5110 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5111 if (pws == NULL)
5112 return -1;
5113
Quentin Glidic82681572016-12-17 13:40:51 +01005114 *pws = workspace_create(shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005115 if (*pws == NULL)
5116 return -1;
5117 }
5118 activate_workspace(shell, 0);
5119
Quentin Glidic82681572016-12-17 13:40:51 +01005120 weston_layer_init(&shell->minimized_layer, ec);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01005121
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005122 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005123 wl_list_init(&shell->workspaces.animation.link);
5124 shell->workspaces.animation.frame = animate_workspace_change_frame;
5125
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02005126 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
5127 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005128 return -1;
5129
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005130 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005131 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005132 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005133 return -1;
5134
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02005135 weston_compositor_get_time(&shell->child.deathstamp);
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005136
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005137 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02005138
Xiong Zhang6b481422013-10-23 13:58:32 +08005139 setup_output_destroy_handler(ec, shell);
5140
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005141 loop = wl_display_get_event_loop(ec->wl_display);
5142 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005143
Jason Ekstrand024177c2014-04-21 19:42:58 -05005144 wl_list_for_each(seat, &ec->seat_list, link)
5145 handle_seat_created(NULL, seat);
5146 shell->seat_create_listener.notify = handle_seat_created;
5147 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005148
David Fort50d962f2016-06-09 17:55:52 +02005149 shell->resized_listener.notify = handle_output_resized;
5150 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
5151
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01005152 screenshooter_create(ec);
5153
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005154 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005155
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005156 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005157
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005158 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
5159
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005160 return 0;
5161}