blob: 7f84b6f3d36924be69e2091fce8dd717bb6dce6a [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"
Pekka Paalanen91b10102019-04-04 14:27:31 +030042#include <libweston/config-parser.h>
Jon Cruzd618f682015-06-15 15:37:09 -070043#include "shared/helpers.h"
Marius Vladd98c7e82021-03-08 18:02:32 +020044#include "shared/shell-utils.h"
Alexandros Frantzis8250a612017-11-16 18:20:52 +020045#include "shared/timespec-util.h"
Pekka Paalanen8ebd9812019-04-04 16:02:14 +030046#include <libweston-desktop/libweston-desktop.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050047
Jonas Ådahle3cddce2012-06-13 00:01:22 +020048#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020049#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020050
Jonas Ådahl04769742012-06-13 00:01:24 +020051struct focus_state {
Quentin Glidicfff39812016-08-15 10:56:52 +020052 struct desktop_shell *shell;
Jonas Ådahl04769742012-06-13 00:01:24 +020053 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040054 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020055 struct weston_surface *keyboard_focus;
56 struct wl_list link;
57 struct wl_listener seat_destroy_listener;
58 struct wl_listener surface_destroy_listener;
59};
60
Philip Withnall648a4dd2013-11-25 18:01:44 +000061/*
62 * Surface stacking and ordering.
63 *
64 * This is handled using several linked lists of surfaces, organised into
65 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
66 * above all of the surfaces in the layer below. The set of layers is static and
67 * in the following order (top-most first):
68 * • Lock layer (only ever displayed on its own)
69 * • Cursor layer
Manuel Bachmann805d2f52014-03-05 12:21:34 +010070 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000071 * • Fullscreen layer
72 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000073 * • Workspace layers
74 * • Background layer
75 *
76 * The list of layers may be manipulated to remove whole layers of surfaces from
77 * display. For example, when locking the screen, all layers except the lock
78 * layer are removed.
79 *
80 * A surface’s layer is modified on configuring the surface, in
81 * set_surface_type() (which is only called when the surface’s type change is
82 * _committed_). If a surface’s type changes (e.g. when making a window
83 * fullscreen) its layer changes too.
84 *
85 * In order to allow popup and transient surfaces to be correctly stacked above
86 * their parent surfaces, each surface tracks both its parent surface, and a
87 * linked list of its children. When a surface’s layer is updated, so are the
88 * layers of its children. Note that child surfaces are *not* the same as
89 * subsurfaces — child/parent surfaces are purely for maintaining stacking
90 * order.
91 *
92 * The children_link list of siblings of a surface (i.e. those surfaces which
93 * have the same parent) only contains weston_surfaces which have a
94 * shell_surface. Stacking is not implemented for non-shell_surface
95 * weston_surfaces. This means that the following implication does *not* hold:
96 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
97 */
98
Pekka Paalanen56cdea92011-11-23 16:14:12 +020099struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500100 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200101
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200102 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500103 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600104 int32_t last_width, last_height;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700105
Tiago Vignattibe143262012-04-16 17:31:41 +0300106 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200107
Stefan Agnera8da2082019-06-23 14:53:59 +0200108 struct wl_list children_list;
109 struct wl_list children_link;
110
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500111 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800112 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800113 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700114 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800115 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100116
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500117 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200118 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500119 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200120 } rotation;
121
122 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800123 struct weston_transform transform; /* matrix from x, y */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500124 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800125 } fullscreen;
126
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200127 struct weston_transform workspace_transform;
128
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500129 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500130 struct weston_output *output;
Semi Malinen99f8c082018-05-02 11:10:32 +0200131 struct wl_listener output_destroy_listener;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200132
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700133 struct surface_state {
Quentin Glidic18a81ac2016-08-16 14:26:03 +0200134 bool fullscreen;
135 bool maximized;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100136 bool lowered;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200137 } state;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700138
Pekka Paalanen77a6d952016-11-16 15:17:14 +0200139 struct {
140 bool is_set;
141 int32_t x;
142 int32_t y;
143 } xwayland;
144
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500145 int focus_count;
Derek Foreman039e9be2015-04-14 17:09:06 -0500146
147 bool destroying;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200148};
149
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300150struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400151 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300152 struct shell_surface *shsurf;
153 struct wl_listener shsurf_destroy_listener;
154};
155
Rusty Lynch1084da52013-08-15 09:10:08 -0700156struct shell_touch_grab {
157 struct weston_touch_grab grab;
158 struct shell_surface *shsurf;
159 struct wl_listener shsurf_destroy_listener;
160 struct weston_touch *touch;
161};
162
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300163struct weston_move_grab {
164 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100165 wl_fixed_t dx, dy;
Derek Foremancf7d95a2015-06-03 15:53:22 -0500166 bool client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500167};
168
Rusty Lynch1084da52013-08-15 09:10:08 -0700169struct weston_touch_move_grab {
170 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800171 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700172 wl_fixed_t dx, dy;
173};
174
Pekka Paalanen460099f2012-01-20 16:48:25 +0200175struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300176 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500177 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200178 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200179 float x;
180 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200181 } center;
182};
183
Giulio Camuffo5085a752013-03-25 21:42:45 +0100184struct shell_seat {
185 struct weston_seat *seat;
186 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500187 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100188
Jason Ekstrand024177c2014-04-21 19:42:58 -0500189 struct wl_listener caps_changed_listener;
190 struct wl_listener pointer_focus_listener;
191 struct wl_listener keyboard_focus_listener;
Marius Vladc114fd62021-08-26 16:37:24 +0300192
193 struct wl_list link; /** shell::seat_list */
Giulio Camuffo5085a752013-03-25 21:42:45 +0100194};
195
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800196
Alex Wubd3354b2012-04-17 17:20:49 +0800197static struct desktop_shell *
198shell_surface_get_shell(struct shell_surface *shsurf);
199
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500200static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200201set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
202
203static void
Derek Foreman74de4692015-07-15 13:00:39 -0500204surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500205
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300206static void
207shell_fade_startup(struct desktop_shell *shell);
208
Bryce Harrington9ad4de12016-09-09 13:16:02 -0700209static void
210shell_fade(struct desktop_shell *shell, enum fade_type type);
211
Philip Withnallbecb77e2013-11-25 18:01:30 +0000212static struct shell_seat *
213get_shell_seat(struct weston_seat *seat);
214
Jonny Lambd73c6942014-08-20 15:53:20 +0200215static void
216get_output_panel_size(struct desktop_shell *shell,
217 struct weston_output *output,
218 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700219
Philip Withnall648a4dd2013-11-25 18:01:44 +0000220static void
221shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
222
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500223static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400224destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300225{
226 struct shell_grab *grab;
227
228 grab = container_of(listener, struct shell_grab,
229 shsurf_destroy_listener);
230
231 grab->shsurf = NULL;
232}
233
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800234struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500235get_default_view(struct weston_surface *surface)
236{
237 struct shell_surface *shsurf;
238 struct weston_view *view;
239
240 if (!surface || wl_list_empty(&surface->views))
241 return NULL;
242
243 shsurf = get_shell_surface(surface);
244 if (shsurf)
245 return shsurf->view;
246
247 wl_list_for_each(view, &surface->views, surface_link)
248 if (weston_view_is_mapped(view))
249 return view;
250
251 return container_of(surface->views.next, struct weston_view, surface_link);
252}
253
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300254static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300255shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400256 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300257 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400258 struct weston_pointer *pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800259 enum weston_desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300260{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300261 struct desktop_shell *shell = shsurf->shell;
262
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200263 weston_seat_break_desktop_grabs(pointer->seat);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400264
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300265 grab->grab.interface = interface;
266 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400267 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500268 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400269 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300270
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700271 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400272 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400273 if (shell->child.desktop_shell) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800274 weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400275 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500276 weston_pointer_set_focus(pointer,
277 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400278 wl_fixed_from_int(0),
279 wl_fixed_from_int(0));
280 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300281}
282
Jonny Lambd73c6942014-08-20 15:53:20 +0200283static void
Quentin Glidic581df062016-06-23 18:55:19 +0200284get_panel_size(struct desktop_shell *shell,
285 struct weston_view *view,
286 int *width,
287 int *height)
288{
289 float x1, y1;
290 float x2, y2;
291 weston_view_to_global_float(view, 0, 0, &x1, &y1);
292 weston_view_to_global_float(view,
293 view->surface->width,
294 view->surface->height,
295 &x2, &y2);
296 *width = (int)(x2 - x1);
297 *height = (int)(y2 - y1);
298}
299
300static void
Jonny Lambd73c6942014-08-20 15:53:20 +0200301get_output_panel_size(struct desktop_shell *shell,
302 struct weston_output *output,
303 int *width,
304 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700305{
306 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200307
308 *width = 0;
309 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700310
311 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200312 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700313
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300314 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Quentin Glidic581df062016-06-23 18:55:19 +0200315 if (view->surface->output == output) {
316 get_panel_size(shell, view, width, height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200317 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700318 }
319 }
320
Jonny Lambd73c6942014-08-20 15:53:20 +0200321 /* the correct view wasn't found */
322}
323
Leandro Ribeiro0c59e922020-01-24 17:53:44 -0300324void
Quentin Glidicfff39812016-08-15 10:56:52 +0200325get_output_work_area(struct desktop_shell *shell,
Jonny Lambd73c6942014-08-20 15:53:20 +0200326 struct weston_output *output,
327 pixman_rectangle32_t *area)
328{
329 int32_t panel_width = 0, panel_height = 0;
330
Pekka Paalanen99372ba2018-05-02 10:21:55 +0200331 if (!output) {
332 area->x = 0;
333 area->y = 0;
334 area->width = 0;
335 area->height = 0;
336
337 return;
338 }
339
Derek Foremanf814c5d2015-04-15 12:23:54 -0500340 area->x = output->x;
341 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200342
343 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200344 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800345 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
Jonny Lambd73c6942014-08-20 15:53:20 +0200346 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500347 area->y += panel_height;
Daniel Stone2ef9b1a2017-03-13 16:31:36 +0000348 /* fallthrough */
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800349 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200350 area->width = output->width;
351 area->height = output->height - panel_height;
352 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800353 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500354 area->x += panel_width;
Daniel Stone2ef9b1a2017-03-13 16:31:36 +0000355 /* fallthrough */
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800356 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200357 area->width = output->width - panel_width;
358 area->height = output->height;
359 break;
360 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700361}
362
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400363static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300364shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300365{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700366 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400367 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700368 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400369
370 if (grab->shsurf->resize_edges) {
371 grab->shsurf->resize_edges = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400372 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700373 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300374
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700375 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300376}
377
378static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700379shell_touch_grab_start(struct shell_touch_grab *grab,
380 const struct weston_touch_grab_interface *interface,
381 struct shell_surface *shsurf,
382 struct weston_touch *touch)
383{
384 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800385
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200386 weston_seat_break_desktop_grabs(touch->seat);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700387
Rusty Lynch1084da52013-08-15 09:10:08 -0700388 grab->grab.interface = interface;
389 grab->shsurf = shsurf;
390 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
391 wl_signal_add(&shsurf->destroy_signal,
392 &grab->shsurf_destroy_listener);
393
394 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700395 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700396
397 weston_touch_start_grab(touch, &grab->grab);
398 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500399 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500400 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700401}
402
403static void
404shell_touch_grab_end(struct shell_touch_grab *grab)
405{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700406 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700407 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700408 grab->shsurf->grabbed = 0;
409 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700410
411 weston_touch_end_grab(grab->touch);
412}
413
Daniel Stone496ca172012-05-30 16:31:42 +0100414static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300415get_modifier(char *modifier)
416{
417 if (!modifier)
418 return MODIFIER_SUPER;
419
420 if (!strcmp("ctrl", modifier))
421 return MODIFIER_CTRL;
422 else if (!strcmp("alt", modifier))
423 return MODIFIER_ALT;
424 else if (!strcmp("super", modifier))
425 return MODIFIER_SUPER;
Bob Ham553d1242016-01-12 10:21:49 +0000426 else if (!strcmp("none", modifier))
427 return 0;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300428 else
429 return MODIFIER_SUPER;
430}
431
Juan Zhaoe10d2792012-04-25 19:09:52 +0800432static enum animation_type
433get_animation_type(char *animation)
434{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800435 if (!animation)
436 return ANIMATION_NONE;
437
Juan Zhaoe10d2792012-04-25 19:09:52 +0800438 if (!strcmp("zoom", animation))
439 return ANIMATION_ZOOM;
440 else if (!strcmp("fade", animation))
441 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100442 else if (!strcmp("dim-layer", animation))
443 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800444 else
445 return ANIMATION_NONE;
446}
447
Alex Wu4539b082012-03-01 12:57:46 +0800448static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400449shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200450{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400451 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500452 char *s, *client;
Daniel Stone51d995a2019-11-26 00:14:24 +0000453 bool allow_zap;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200454
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300455 section = weston_config_get_section(wet_get_config(shell->compositor),
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400456 "shell", NULL, NULL);
Marius Vlad64fbd0f2018-12-15 15:51:57 +0200457 client = wet_get_libexec_path(WESTON_SHELL_CLIENT);
Daniel Stonee03c1112016-11-24 20:45:45 +0000458 weston_config_section_get_string(section, "client", &s, client);
Derek Foremanc7210432014-08-21 11:32:38 -0500459 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100460 shell->client = s;
Bob Ham744e6532016-01-12 10:21:48 +0000461
462 weston_config_section_get_bool(section,
463 "allow-zap", &allow_zap, true);
464 shell->allow_zap = allow_zap;
465
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100466 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400467 "binding-modifier", &s, "super");
468 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200469 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800470
471 weston_config_section_get_string(section,
472 "exposay-modifier", &s, "none");
Bob Ham553d1242016-01-12 10:21:49 +0000473 shell->exposay_modifier = get_modifier(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800474 free(s);
475
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400476 weston_config_section_get_string(section, "animation", &s, "none");
477 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200478 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200479 weston_config_section_get_string(section, "close-animation", &s, "fade");
480 shell->win_close_animation_type = get_animation_type(s);
481 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700482 weston_config_section_get_string(section,
483 "startup-animation", &s, "fade");
484 shell->startup_animation_type = get_animation_type(s);
485 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700486 if (shell->startup_animation_type == ANIMATION_ZOOM)
487 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100488 weston_config_section_get_string(section, "focus-animation", &s, "none");
489 shell->focus_animation_type = get_animation_type(s);
490 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400491 weston_config_section_get_uint(section, "num-workspaces",
492 &shell->workspaces.num,
493 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200494}
495
Pekka Paalanen8274d902014-08-06 19:36:51 +0300496static int
497focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
498{
499 return snprintf(buf, len, "focus highlight effect for output %s",
Miguel A. Vico620f68d2019-09-25 11:23:13 -0700500 (surface->output ? surface->output->name : "NULL"));
Pekka Paalanen8274d902014-08-06 19:36:51 +0300501}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100502
503/* no-op func for checking focus surface */
504static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200505focus_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100506{
507}
508
509static struct focus_surface *
510get_focus_surface(struct weston_surface *surface)
511{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200512 if (surface->committed == focus_surface_committed)
513 return surface->committed_private;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100514 else
515 return NULL;
516}
517
518static bool
519is_focus_surface (struct weston_surface *es)
520{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200521 return (es->committed == focus_surface_committed);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100522}
523
524static bool
525is_focus_view (struct weston_view *view)
526{
527 return is_focus_surface (view->surface);
528}
529
530static struct focus_surface *
531create_focus_surface(struct weston_compositor *ec,
532 struct weston_output *output)
533{
534 struct focus_surface *fsurf = NULL;
535 struct weston_surface *surface = NULL;
536
537 fsurf = malloc(sizeof *fsurf);
538 if (!fsurf)
539 return NULL;
540
541 fsurf->surface = weston_surface_create(ec);
542 surface = fsurf->surface;
543 if (surface == NULL) {
544 free(fsurf);
545 return NULL;
546 }
547
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200548 surface->committed = focus_surface_committed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100549 surface->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200550 surface->is_mapped = true;
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200551 surface->committed_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300552 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100553
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800554 fsurf->view = weston_view_create(surface);
555 if (fsurf->view == NULL) {
556 weston_surface_destroy(surface);
557 free(fsurf);
558 return NULL;
559 }
Semi Malinene7a52fb2018-04-26 11:08:10 +0200560 weston_view_set_output(fsurf->view, output);
Armin Krezović4663aca2016-06-30 06:04:29 +0200561 fsurf->view->is_mapped = true;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100562
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600563 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600564 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100565 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
566 pixman_region32_fini(&surface->opaque);
567 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
568 output->width, output->height);
569 pixman_region32_fini(&surface->input);
570 pixman_region32_init(&surface->input);
571
572 wl_list_init(&fsurf->workspace_transform.link);
573
574 return fsurf;
575}
576
577static void
578focus_surface_destroy(struct focus_surface *fsurf)
579{
580 weston_surface_destroy(fsurf->surface);
581 free(fsurf);
582}
583
584static void
585focus_animation_done(struct weston_view_animation *animation, void *data)
586{
587 struct workspace *ws = data;
588
589 ws->focus_animation = NULL;
590}
591
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200592static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200593focus_state_destroy(struct focus_state *state)
594{
595 wl_list_remove(&state->seat_destroy_listener.link);
596 wl_list_remove(&state->surface_destroy_listener.link);
597 free(state);
598}
599
600static void
601focus_state_seat_destroy(struct wl_listener *listener, void *data)
602{
603 struct focus_state *state = container_of(listener,
604 struct focus_state,
605 seat_destroy_listener);
606
607 wl_list_remove(&state->link);
608 focus_state_destroy(state);
609}
610
611static void
612focus_state_surface_destroy(struct wl_listener *listener, void *data)
613{
614 struct focus_state *state = container_of(listener,
615 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400616 surface_destroy_listener);
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200617 struct weston_surface *main_surface;
618 struct weston_view *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500619 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200620
Pekka Paalanen01388e22013-04-25 13:57:44 +0300621 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
622
Kristian Høgsberge3778222012-07-31 17:29:30 -0400623 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300624 wl_list_for_each(view,
625 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500626 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400627 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100628 if (is_focus_view(view))
629 continue;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200630 if (!get_shell_surface(view->surface))
631 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400632
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200633 next = view;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400634 break;
635 }
636
Pekka Paalanen01388e22013-04-25 13:57:44 +0300637 /* if the focus was a sub-surface, activate its main surface */
638 if (main_surface != state->keyboard_focus)
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200639 next = get_default_view(main_surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300640
Kristian Høgsberge3778222012-07-31 17:29:30 -0400641 if (next) {
Greg V15d3d302018-12-07 01:33:34 +0300642 if (state->keyboard_focus) {
643 wl_list_remove(&state->surface_destroy_listener.link);
644 wl_list_init(&state->surface_destroy_listener.link);
645 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100646 state->keyboard_focus = NULL;
Quentin Glidicfff39812016-08-15 10:56:52 +0200647 activate(state->shell, next, state->seat,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +0200648 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400649 } else {
Quentin Glidicfff39812016-08-15 10:56:52 +0200650 if (state->shell->focus_animation_type == ANIMATION_DIM_LAYER) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100651 if (state->ws->focus_animation)
652 weston_view_animation_destroy(state->ws->focus_animation);
653
654 state->ws->focus_animation = weston_fade_run(
655 state->ws->fsurf_front->view,
656 state->ws->fsurf_front->view->alpha, 0.0, 300,
657 focus_animation_done, state->ws);
658 }
659
Kristian Høgsberge3778222012-07-31 17:29:30 -0400660 wl_list_remove(&state->link);
661 focus_state_destroy(state);
662 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200663}
664
665static struct focus_state *
Quentin Glidicfff39812016-08-15 10:56:52 +0200666focus_state_create(struct desktop_shell *shell, struct weston_seat *seat,
667 struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200668{
Jonas Ådahl04769742012-06-13 00:01:24 +0200669 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200670
671 state = malloc(sizeof *state);
672 if (state == NULL)
673 return NULL;
674
Quentin Glidicfff39812016-08-15 10:56:52 +0200675 state->shell = shell;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100676 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400677 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200678 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400679 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200680
681 state->seat_destroy_listener.notify = focus_state_seat_destroy;
682 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400683 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200684 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400685 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200686
687 return state;
688}
689
Jonas Ådahl8538b222012-08-29 22:13:03 +0200690static struct focus_state *
691ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
692{
693 struct workspace *ws = get_current_workspace(shell);
694 struct focus_state *state;
695
696 wl_list_for_each(state, &ws->focus_list, link)
697 if (state->seat == seat)
698 break;
699
700 if (&state->link == &ws->focus_list)
Quentin Glidicfff39812016-08-15 10:56:52 +0200701 state = focus_state_create(shell, seat, ws);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200702
703 return state;
704}
705
Jonas Ådahl04769742012-06-13 00:01:24 +0200706static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800707focus_state_set_focus(struct focus_state *state,
708 struct weston_surface *surface)
709{
710 if (state->keyboard_focus) {
711 wl_list_remove(&state->surface_destroy_listener.link);
712 wl_list_init(&state->surface_destroy_listener.link);
713 }
714
715 state->keyboard_focus = surface;
716 if (surface)
717 wl_signal_add(&surface->destroy_signal,
718 &state->surface_destroy_listener);
719}
720
721static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400722restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200723{
724 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400725 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100726 struct wl_list pending_seat_list;
727 struct weston_seat *seat, *next_seat;
728
729 /* Temporarily steal the list of seats so that we can keep
730 * track of the seats we've already processed */
731 wl_list_init(&pending_seat_list);
732 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
733 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200734
735 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500736 struct weston_keyboard *keyboard =
737 weston_seat_get_keyboard(state->seat);
738
Neil Roberts4237f502014-04-09 16:33:31 +0100739 wl_list_remove(&state->seat->link);
740 wl_list_insert(&shell->compositor->seat_list,
741 &state->seat->link);
742
Derek Foreman1281a362015-07-31 16:55:32 -0500743 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800744 continue;
745
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400746 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200747
Derek Foreman1281a362015-07-31 16:55:32 -0500748 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200749 }
Neil Roberts4237f502014-04-09 16:33:31 +0100750
751 /* For any remaining seats that we don't have a focus state
752 * for we'll reset the keyboard focus to NULL */
753 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500754 struct weston_keyboard *keyboard =
755 weston_seat_get_keyboard(seat);
756
Neil Roberts4237f502014-04-09 16:33:31 +0100757 wl_list_insert(&shell->compositor->seat_list, &seat->link);
758
Derek Foreman1281a362015-07-31 16:55:32 -0500759 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100760 continue;
761
Derek Foreman1281a362015-07-31 16:55:32 -0500762 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100763 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200764}
765
766static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200767replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
768 struct weston_seat *seat)
769{
Derek Foreman1281a362015-07-31 16:55:32 -0500770 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200771 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200772
773 wl_list_for_each(state, &ws->focus_list, link) {
774 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500775 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200776 return;
777 }
778 }
779}
780
781static void
782drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
783 struct weston_surface *surface)
784{
785 struct focus_state *state;
786
787 wl_list_for_each(state, &ws->focus_list, link)
788 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800789 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200790}
791
792static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100793animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
794 struct weston_view *from, struct weston_view *to)
795{
796 struct weston_output *output;
797 bool focus_surface_created = false;
798
799 /* FIXME: Only support dim animation using two layers */
800 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
801 return;
802
803 output = get_default_output(shell->compositor);
804 if (ws->fsurf_front == NULL && (from || to)) {
805 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800806 if (ws->fsurf_front == NULL)
807 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100808 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800809
810 ws->fsurf_back = create_focus_surface(shell->compositor, output);
811 if (ws->fsurf_back == NULL) {
812 focus_surface_destroy(ws->fsurf_front);
813 return;
814 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100815 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800816
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100817 focus_surface_created = true;
818 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300819 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
820 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100821 }
822
823 if (ws->focus_animation) {
824 weston_view_animation_destroy(ws->focus_animation);
825 ws->focus_animation = NULL;
826 }
827
828 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300829 weston_layer_entry_insert(&to->layer_link,
830 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100831 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300832 weston_layer_entry_insert(&ws->layer.view_list,
833 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100834
835 if (focus_surface_created) {
836 ws->focus_animation = weston_fade_run(
837 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200838 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100839 focus_animation_done, ws);
840 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300841 weston_layer_entry_insert(&from->layer_link,
842 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100843 ws->focus_animation = weston_stable_fade_run(
844 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200845 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100846 focus_animation_done, ws);
847 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300848 weston_layer_entry_insert(&ws->layer.view_list,
849 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100850 ws->focus_animation = weston_stable_fade_run(
851 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200852 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100853 focus_animation_done, ws);
854 }
855}
856
857static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200858workspace_destroy(struct workspace *ws)
859{
Jonas Ådahl04769742012-06-13 00:01:24 +0200860 struct focus_state *state, *next;
861
862 wl_list_for_each_safe(state, next, &ws->focus_list, link)
863 focus_state_destroy(state);
864
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100865 if (ws->fsurf_front)
866 focus_surface_destroy(ws->fsurf_front);
867 if (ws->fsurf_back)
868 focus_surface_destroy(ws->fsurf_back);
869
Pekka Paalanen4bb326b2021-05-14 14:37:46 +0300870 weston_layer_fini(&ws->layer);
871
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200872 free(ws);
873}
874
Jonas Ådahl04769742012-06-13 00:01:24 +0200875static void
876seat_destroyed(struct wl_listener *listener, void *data)
877{
878 struct weston_seat *seat = data;
879 struct focus_state *state, *next;
880 struct workspace *ws = container_of(listener,
881 struct workspace,
882 seat_destroyed_listener);
883
884 wl_list_for_each_safe(state, next, &ws->focus_list, link)
885 if (state->seat == seat)
886 wl_list_remove(&state->link);
887}
888
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200889static struct workspace *
Quentin Glidic82681572016-12-17 13:40:51 +0100890workspace_create(struct desktop_shell *shell)
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200891{
892 struct workspace *ws = malloc(sizeof *ws);
893 if (ws == NULL)
894 return NULL;
895
Quentin Glidic82681572016-12-17 13:40:51 +0100896 weston_layer_init(&ws->layer, shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200897
Jonas Ådahl04769742012-06-13 00:01:24 +0200898 wl_list_init(&ws->focus_list);
899 wl_list_init(&ws->seat_destroyed_listener.link);
900 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100901 ws->fsurf_front = NULL;
902 ws->fsurf_back = NULL;
903 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200904
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200905 return ws;
906}
907
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200908static int
909workspace_is_empty(struct workspace *ws)
910{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300911 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200912}
913
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200914static struct workspace *
915get_workspace(struct desktop_shell *shell, unsigned int index)
916{
917 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200918 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200919 pws += index;
920 return *pws;
921}
922
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800923struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200924get_current_workspace(struct desktop_shell *shell)
925{
926 return get_workspace(shell, shell->workspaces.current);
927}
928
929static void
930activate_workspace(struct desktop_shell *shell, unsigned int index)
931{
932 struct workspace *ws;
933
934 ws = get_workspace(shell, index);
Quentin Glidic82681572016-12-17 13:40:51 +0100935 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200936
937 shell->workspaces.current = index;
938}
939
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200940static unsigned int
941get_output_height(struct weston_output *output)
942{
943 return abs(output->region.extents.y1 - output->region.extents.y2);
944}
945
Greg Vf57774e2018-07-24 23:21:55 +0300946static struct weston_transform *
947view_get_transform(struct weston_view *view)
948{
949 struct focus_surface *fsurf = NULL;
950 struct shell_surface *shsurf = NULL;
951
952 if (is_focus_view(view)) {
953 fsurf = get_focus_surface(view->surface);
954 return &fsurf->workspace_transform;
955 }
956
957 shsurf = get_shell_surface(view->surface);
958 if (shsurf)
959 return &shsurf->workspace_transform;
960
961 return NULL;
962}
963
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200964static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100965view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200966{
Greg Vf57774e2018-07-24 23:21:55 +0300967 struct weston_transform *transform = view_get_transform(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200968
Greg Vf57774e2018-07-24 23:21:55 +0300969 if (!transform)
970 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100971
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200972 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500973 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100974 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200975
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100976 weston_matrix_init(&transform->matrix);
977 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200978 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500979 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200980}
981
982static void
983workspace_translate_out(struct workspace *ws, double fraction)
984{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500985 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200986 unsigned int height;
987 double d;
988
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300989 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500990 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200991 d = height * fraction;
992
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100993 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200994 }
995}
996
997static void
998workspace_translate_in(struct workspace *ws, double fraction)
999{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001000 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001001 unsigned int height;
1002 double d;
1003
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001004 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001005 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001006
1007 if (fraction > 0)
1008 d = -(height - height * fraction);
1009 else
1010 d = height + height * fraction;
1011
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001012 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001013 }
1014}
1015
1016static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001017reverse_workspace_change_animation(struct desktop_shell *shell,
1018 unsigned int index,
1019 struct workspace *from,
1020 struct workspace *to)
1021{
1022 shell->workspaces.current = index;
1023
1024 shell->workspaces.anim_to = to;
1025 shell->workspaces.anim_from = from;
1026 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001027 shell->workspaces.anim_timestamp = (struct timespec) { 0 };
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001028
Quentin Glidic82681572016-12-17 13:40:51 +01001029 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1030 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
1031
Scott Moreau4272e632012-08-13 09:58:41 -06001032 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001033}
1034
1035static void
1036workspace_deactivate_transforms(struct workspace *ws)
1037{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001038 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001039 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001040
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001041 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Greg Vf57774e2018-07-24 23:21:55 +03001042 transform = view_get_transform(view);
1043 if (!transform)
1044 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001045
1046 if (!wl_list_empty(&transform->link)) {
1047 wl_list_remove(&transform->link);
1048 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001049 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001050 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001051 }
1052}
1053
1054static void
1055finish_workspace_change_animation(struct desktop_shell *shell,
1056 struct workspace *from,
1057 struct workspace *to)
1058{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001059 struct weston_view *view;
1060
Scott Moreau4272e632012-08-13 09:58:41 -06001061 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001062
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001063 /* Views that extend past the bottom of the output are still
1064 * visible after the workspace animation ends but before its layer
1065 * is hidden. In that case, we need to damage below those views so
1066 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001067 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001068 weston_view_damage_below(view);
1069
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001070 wl_list_remove(&shell->workspaces.animation.link);
1071 workspace_deactivate_transforms(from);
1072 workspace_deactivate_transforms(to);
1073 shell->workspaces.anim_to = NULL;
1074
Quentin Glidic82681572016-12-17 13:40:51 +01001075 weston_layer_unset_position(&shell->workspaces.anim_from->layer);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001076}
1077
1078static void
1079animate_workspace_change_frame(struct weston_animation *animation,
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001080 struct weston_output *output,
1081 const struct timespec *time)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001082{
1083 struct desktop_shell *shell =
1084 container_of(animation, struct desktop_shell,
1085 workspaces.animation);
1086 struct workspace *from = shell->workspaces.anim_from;
1087 struct workspace *to = shell->workspaces.anim_to;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001088 int64_t t;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001089 double x, y;
1090
1091 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1092 finish_workspace_change_animation(shell, from, to);
1093 return;
1094 }
1095
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001096 if (timespec_is_zero(&shell->workspaces.anim_timestamp)) {
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001097 if (shell->workspaces.anim_current == 0.0)
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001098 shell->workspaces.anim_timestamp = *time;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001099 else
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001100 timespec_add_msec(&shell->workspaces.anim_timestamp,
1101 time,
Emmanuel Gil Peyrot426c2462019-02-20 16:33:32 +01001102 /* Inverse of movement function 'y' below. */
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001103 -(asin(1.0 - shell->workspaces.anim_current) *
1104 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1105 M_2_PI));
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001106 }
1107
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001108 t = timespec_sub_to_msec(time, &shell->workspaces.anim_timestamp);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001109
1110 /*
1111 * x = [0, π/2]
1112 * y(x) = sin(x)
1113 */
1114 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1115 y = sin(x);
1116
1117 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001118 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001119
1120 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1121 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1122 shell->workspaces.anim_current = y;
1123
Scott Moreau4272e632012-08-13 09:58:41 -06001124 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001125 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001126 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001127 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001128}
1129
1130static void
1131animate_workspace_change(struct desktop_shell *shell,
1132 unsigned int index,
1133 struct workspace *from,
1134 struct workspace *to)
1135{
1136 struct weston_output *output;
1137
1138 int dir;
1139
1140 if (index > shell->workspaces.current)
1141 dir = -1;
1142 else
1143 dir = 1;
1144
1145 shell->workspaces.current = index;
1146
1147 shell->workspaces.anim_dir = dir;
1148 shell->workspaces.anim_from = from;
1149 shell->workspaces.anim_to = to;
1150 shell->workspaces.anim_current = 0.0;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001151 shell->workspaces.anim_timestamp = (struct timespec) { 0 };
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001152
1153 output = container_of(shell->compositor->output_list.next,
1154 struct weston_output, link);
1155 wl_list_insert(&output->animation_list,
1156 &shell->workspaces.animation.link);
1157
Quentin Glidic82681572016-12-17 13:40:51 +01001158 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1159 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001160
1161 workspace_translate_in(to, 0);
1162
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001163 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001164
Scott Moreau4272e632012-08-13 09:58:41 -06001165 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001166}
1167
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001168static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001169update_workspace(struct desktop_shell *shell, unsigned int index,
1170 struct workspace *from, struct workspace *to)
1171{
1172 shell->workspaces.current = index;
Quentin Glidic82681572016-12-17 13:40:51 +01001173 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1174 weston_layer_unset_position(&from->layer);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001175}
1176
1177static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001178change_workspace(struct desktop_shell *shell, unsigned int index)
1179{
1180 struct workspace *from;
1181 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001182 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001183
1184 if (index == shell->workspaces.current)
1185 return;
1186
1187 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001188 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001189 return;
1190
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001191 from = get_current_workspace(shell);
1192 to = get_workspace(shell, index);
1193
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001194 if (shell->workspaces.anim_from == to &&
1195 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001196 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001197 reverse_workspace_change_animation(shell, index, from, to);
1198 return;
1199 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001200
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001201 if (shell->workspaces.anim_to != NULL)
1202 finish_workspace_change_animation(shell,
1203 shell->workspaces.anim_from,
1204 shell->workspaces.anim_to);
1205
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001206 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001207
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001208 if (shell->focus_animation_type != ANIMATION_NONE) {
1209 wl_list_for_each(state, &from->focus_list, link)
1210 if (state->keyboard_focus)
1211 animate_focus_change(shell, from,
1212 get_default_view(state->keyboard_focus), NULL);
1213
1214 wl_list_for_each(state, &to->focus_list, link)
1215 if (state->keyboard_focus)
1216 animate_focus_change(shell, to,
1217 NULL, get_default_view(state->keyboard_focus));
1218 }
1219
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001220 if (workspace_is_empty(to) && workspace_is_empty(from))
1221 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001222 else
1223 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001224}
1225
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001226static bool
1227workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1228{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001229 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001230 struct wl_list *e;
1231
1232 if (wl_list_empty(list))
1233 return false;
1234
1235 e = list->next;
1236
1237 if (e->next != list)
1238 return false;
1239
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001240 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001241}
1242
1243static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001244surface_keyboard_focus_lost(struct weston_surface *surface)
1245{
1246 struct weston_compositor *compositor = surface->compositor;
1247 struct weston_seat *seat;
1248 struct weston_surface *focus;
1249
1250 wl_list_for_each(seat, &compositor->seat_list, link) {
1251 struct weston_keyboard *keyboard =
1252 weston_seat_get_keyboard(seat);
1253
1254 if (!keyboard)
1255 continue;
1256
1257 focus = weston_surface_get_main_surface(keyboard->focus);
1258 if (focus == surface)
1259 weston_keyboard_set_focus(keyboard, NULL);
1260 }
1261}
1262
1263static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001264take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001265 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001266 unsigned int index)
1267{
Derek Foreman1281a362015-07-31 16:55:32 -05001268 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001269 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001270 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001271 struct shell_surface *shsurf;
1272 struct workspace *from;
1273 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001274 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001275
Derek Foreman1281a362015-07-31 16:55:32 -05001276 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001277 view = get_default_view(surface);
1278 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001279 index == shell->workspaces.current ||
1280 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001281 return;
1282
1283 from = get_current_workspace(shell);
1284 to = get_workspace(shell, index);
1285
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001286 weston_layer_entry_remove(&view->layer_link);
1287 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001288
Philip Withnall659163d2013-11-25 18:01:36 +00001289 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001290 if (shsurf != NULL)
1291 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001292
Jonas Ådahle9d22502012-08-29 22:13:01 +02001293 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001294 drop_focus_state(shell, from, surface);
1295
1296 if (shell->workspaces.anim_from == to &&
1297 shell->workspaces.anim_to == from) {
1298 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001299
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001300 return;
1301 }
1302
1303 if (shell->workspaces.anim_to != NULL)
1304 finish_workspace_change_animation(shell,
1305 shell->workspaces.anim_from,
1306 shell->workspaces.anim_to);
1307
1308 if (workspace_is_empty(from) &&
1309 workspace_has_only(to, surface))
1310 update_workspace(shell, index, from, to);
1311 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001312 if (shsurf != NULL &&
1313 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001314 wl_list_insert(&shell->workspaces.anim_sticky_list,
1315 &shsurf->workspace_transform.link);
1316
1317 animate_workspace_change(shell, index, from, to);
1318 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001319
Jonas Ådahl8538b222012-08-29 22:13:03 +02001320 state = ensure_focus_state(shell, seat);
1321 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001322 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001323}
1324
1325static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02001326touch_move_grab_down(struct weston_touch_grab *grab,
1327 const struct timespec *time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001328 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001329{
1330}
1331
1332static void
Alexandros Frantzis27a51b82017-11-16 18:20:59 +02001333touch_move_grab_up(struct weston_touch_grab *grab, const struct timespec *time,
1334 int touch_id)
Rusty Lynch1084da52013-08-15 09:10:08 -07001335{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001336 struct weston_touch_move_grab *move =
1337 (struct weston_touch_move_grab *) container_of(
1338 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001339
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001340 if (touch_id == 0)
1341 move->active = 0;
1342
Jonas Ådahl9484b692013-12-02 22:05:03 +01001343 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001344 shell_touch_grab_end(&move->base);
1345 free(move);
1346 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001347}
1348
1349static void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +02001350touch_move_grab_motion(struct weston_touch_grab *grab,
1351 const struct timespec *time, int touch_id,
1352 wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001353{
1354 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1355 struct shell_surface *shsurf = move->base.shsurf;
1356 struct weston_surface *es;
1357 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1358 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1359
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001360 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001361 return;
1362
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001363 es = weston_desktop_surface_get_surface(shsurf->desktop_surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001364
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001365 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001366
1367 weston_compositor_schedule_repaint(es->compositor);
1368}
1369
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001370static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001371touch_move_grab_frame(struct weston_touch_grab *grab)
1372{
1373}
1374
1375static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001376touch_move_grab_cancel(struct weston_touch_grab *grab)
1377{
1378 struct weston_touch_move_grab *move =
1379 (struct weston_touch_move_grab *) container_of(
1380 grab, struct shell_touch_grab, grab);
1381
1382 shell_touch_grab_end(&move->base);
1383 free(move);
1384}
1385
Rusty Lynch1084da52013-08-15 09:10:08 -07001386static const struct weston_touch_grab_interface touch_move_grab_interface = {
1387 touch_move_grab_down,
1388 touch_move_grab_up,
1389 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001390 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001391 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001392};
1393
1394static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001395surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001396{
1397 struct weston_touch_move_grab *move;
1398
1399 if (!shsurf)
1400 return -1;
1401
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001402 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1403 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Rusty Lynch1084da52013-08-15 09:10:08 -07001404 return 0;
1405
1406 move = malloc(sizeof *move);
1407 if (!move)
1408 return -1;
1409
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001410 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001411 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001412 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001413 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001414 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001415
1416 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001417 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001418
1419 return 0;
1420}
1421
1422static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001423noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001424{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001425}
1426
1427static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001428noop_grab_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02001429 const struct timespec *time,
1430 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001431{
1432}
1433
1434static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001435noop_grab_axis_source(struct weston_pointer_grab *grab,
1436 uint32_t source)
1437{
1438}
1439
1440static void
1441noop_grab_frame(struct weston_pointer_grab *grab)
1442{
1443}
1444
1445static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001446constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1447{
1448 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001449 struct weston_surface *surface =
1450 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001451 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001452 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001453 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001454 pixman_rectangle32_t area;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001455 struct weston_geometry geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001456
1457 x = wl_fixed_to_int(pointer->x + move->dx);
1458 y = wl_fixed_to_int(pointer->y + move->dy);
1459
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001460 if (shsurf->shell->panel_position ==
1461 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001462 get_output_work_area(shsurf->shell, surface->output, &area);
1463 geometry =
1464 weston_desktop_surface_get_geometry(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001465
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001466 bottom = y + geometry.height + geometry.y;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001467 if (bottom - safety < area.y)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001468 y = area.y + safety - geometry.height
1469 - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001470
1471 if (move->client_initiated &&
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001472 y + geometry.y < area.y)
1473 y = area.y - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001474 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001475
1476 *cx = x;
1477 *cy = y;
1478}
1479
1480static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001481move_grab_motion(struct weston_pointer_grab *grab,
1482 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001483 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001484{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001485 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001486 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001487 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001488 struct weston_surface *surface;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001489 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001490
Jonas Ådahld2510102014-10-05 21:39:14 +02001491 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001492 if (!shsurf)
1493 return;
1494
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001495 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1496
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001497 constrain_position(move, &cx, &cy);
1498
1499 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001500
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001501 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001502}
1503
1504static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001505move_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001506 const struct timespec *time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001507{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001508 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1509 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001510 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001511 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001512
Daniel Stone4dbadb12012-05-30 16:31:51 +01001513 if (pointer->button_count == 0 &&
1514 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001515 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001516 free(grab);
1517 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001518}
1519
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001520static void
1521move_grab_cancel(struct weston_pointer_grab *grab)
1522{
1523 struct shell_grab *shell_grab =
1524 container_of(grab, struct shell_grab, grab);
1525
1526 shell_grab_end(shell_grab);
1527 free(grab);
1528}
1529
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001530static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001531 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001532 move_grab_motion,
1533 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001534 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001535 noop_grab_axis_source,
1536 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001537 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001538};
1539
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001540static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001541surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001542 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001543{
1544 struct weston_move_grab *move;
1545
1546 if (!shsurf)
1547 return -1;
1548
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001549 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001550 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1551 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001552 return 0;
1553
1554 move = malloc(sizeof *move);
1555 if (!move)
1556 return -1;
1557
Jason Ekstranda7af7042013-10-12 22:38:11 -05001558 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001559 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001560 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001561 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001562 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001563
1564 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001565 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001566
1567 return 0;
1568}
1569
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001570struct weston_resize_grab {
1571 struct shell_grab base;
1572 uint32_t edges;
1573 int32_t width, height;
1574};
1575
1576static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001577resize_grab_motion(struct weston_pointer_grab *grab,
1578 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001579 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001580{
1581 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001582 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001583 struct shell_surface *shsurf = resize->base.shsurf;
1584 int32_t width, height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001585 struct weston_size min_size, max_size;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001586 wl_fixed_t from_x, from_y;
1587 wl_fixed_t to_x, to_y;
1588
Jonas Ådahld2510102014-10-05 21:39:14 +02001589 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001590
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001591 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001592 return;
1593
Jason Ekstranda7af7042013-10-12 22:38:11 -05001594 weston_view_from_global_fixed(shsurf->view,
1595 pointer->grab_x, pointer->grab_y,
1596 &from_x, &from_y);
1597 weston_view_from_global_fixed(shsurf->view,
1598 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001599
1600 width = resize->width;
1601 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1602 width += wl_fixed_to_int(from_x - to_x);
1603 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1604 width += wl_fixed_to_int(to_x - from_x);
1605 }
1606
1607 height = resize->height;
1608 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1609 height += wl_fixed_to_int(from_y - to_y);
1610 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1611 height += wl_fixed_to_int(to_y - from_y);
1612 }
1613
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001614 max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
1615 min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
1616
1617 min_size.width = MAX(1, min_size.width);
1618 min_size.height = MAX(1, min_size.height);
1619
1620 if (width < min_size.width)
1621 width = min_size.width;
1622 else if (max_size.width > 0 && width > max_size.width)
1623 width = max_size.width;
1624 if (height < min_size.height)
1625 height = min_size.height;
1626 else if (max_size.width > 0 && width > max_size.width)
1627 width = max_size.width;
1628 weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001629}
1630
1631static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001632resize_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001633 const struct timespec *time,
1634 uint32_t button, uint32_t state_w)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001635{
1636 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001637 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001638 enum wl_pointer_button_state state = state_w;
1639
1640 if (pointer->button_count == 0 &&
1641 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Greg Vec3f7792018-11-08 00:24:56 +03001642 if (resize->base.shsurf != NULL) {
1643 struct weston_desktop_surface *desktop_surface =
1644 resize->base.shsurf->desktop_surface;
1645 weston_desktop_surface_set_resizing(desktop_surface,
1646 false);
1647 }
1648
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001649 shell_grab_end(&resize->base);
1650 free(grab);
1651 }
1652}
1653
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001654static void
1655resize_grab_cancel(struct weston_pointer_grab *grab)
1656{
1657 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1658
Greg Vec3f7792018-11-08 00:24:56 +03001659 if (resize->base.shsurf != NULL) {
1660 struct weston_desktop_surface *desktop_surface =
1661 resize->base.shsurf->desktop_surface;
1662 weston_desktop_surface_set_resizing(desktop_surface, false);
1663 }
1664
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001665 shell_grab_end(&resize->base);
1666 free(grab);
1667}
1668
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001669static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001670 noop_grab_focus,
1671 resize_grab_motion,
1672 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001673 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001674 noop_grab_axis_source,
1675 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001676 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001677};
1678
Giulio Camuffob8366642013-04-25 13:57:46 +03001679
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001680static int
1681surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001682 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001683{
1684 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001685 const unsigned resize_topbottom =
1686 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1687 const unsigned resize_leftright =
1688 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1689 const unsigned resize_any = resize_topbottom | resize_leftright;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001690 struct weston_geometry geometry;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001691
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001692 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001693 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1694 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001695 return 0;
1696
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001697 /* Check for invalid edge combinations. */
1698 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1699 (edges & resize_topbottom) == resize_topbottom ||
1700 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001701 return 0;
1702
1703 resize = malloc(sizeof *resize);
1704 if (!resize)
1705 return -1;
1706
1707 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001708
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001709 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1710 resize->width = geometry.width;
1711 resize->height = geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001712
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001713 shsurf->resize_edges = edges;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001714 weston_desktop_surface_set_resizing(shsurf->desktop_surface, true);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001715 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001716 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001717
1718 return 0;
1719}
1720
1721static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001722busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001723{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001724 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001725 struct weston_pointer *pointer = base->pointer;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001726 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001727 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001728 wl_fixed_t sx, sy;
1729
Jason Ekstranda7af7042013-10-12 22:38:11 -05001730 view = weston_compositor_pick_view(pointer->seat->compositor,
1731 pointer->x, pointer->y,
1732 &sx, &sy);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001733 desktop_surface = weston_surface_get_desktop_surface(view->surface);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001734
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001735 if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001736 shell_grab_end(grab);
1737 free(grab);
1738 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001739}
1740
1741static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001742busy_cursor_grab_motion(struct weston_pointer_grab *grab,
1743 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001744 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001745{
Jonas Ådahld2510102014-10-05 21:39:14 +02001746 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001747}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001748
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001749static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001750busy_cursor_grab_button(struct weston_pointer_grab *base,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001751 const struct timespec *time,
1752 uint32_t button, uint32_t state)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001753{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001754 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001755 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05001756 struct weston_pointer *pointer = grab->grab.pointer;
1757 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001758
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001759 if (shsurf && button == BTN_LEFT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001760 activate(shsurf->shell, shsurf->view, seat,
1761 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman794fa0e2015-07-15 13:00:38 -05001762 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001763 } else if (shsurf && button == BTN_RIGHT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001764 activate(shsurf->shell, shsurf->view, seat,
1765 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman74de4692015-07-15 13:00:39 -05001766 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001767 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001768}
1769
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001770static void
1771busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1772{
1773 struct shell_grab *grab = (struct shell_grab *) base;
1774
1775 shell_grab_end(grab);
1776 free(grab);
1777}
1778
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001779static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001780 busy_cursor_grab_focus,
1781 busy_cursor_grab_motion,
1782 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001783 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001784 noop_grab_axis_source,
1785 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001786 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001787};
1788
1789static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001790handle_pointer_focus(struct wl_listener *listener, void *data)
1791{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001792 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001793 struct weston_view *view = pointer->focus;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001794 struct shell_surface *shsurf;
1795 struct weston_desktop_client *client;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001796
Jason Ekstranda7af7042013-10-12 22:38:11 -05001797 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001798 return;
1799
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001800 shsurf = get_shell_surface(view->surface);
1801 if (!shsurf)
1802 return;
1803
1804 client = weston_desktop_surface_get_client(shsurf->desktop_surface);
1805
1806 if (shsurf->unresponsive)
1807 set_busy_cursor(shsurf, pointer);
1808 else
1809 weston_desktop_client_ping(client);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001810}
1811
1812static void
Marius Vladab39e1d2021-03-05 21:40:22 +02001813shell_surface_deactivate(struct shell_surface *shsurf)
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001814{
1815 if (--shsurf->focus_count == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001816 weston_desktop_surface_set_activated(shsurf->desktop_surface, false);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001817}
1818
1819static void
Marius Vladab39e1d2021-03-05 21:40:22 +02001820shell_surface_activate(struct shell_surface *shsurf)
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001821{
1822 if (shsurf->focus_count++ == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001823 weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001824}
1825
Philip Withnall07926d92013-11-25 18:01:40 +00001826/* The surface will be inserted into the list immediately after the link
1827 * returned by this function (i.e. will be stacked immediately above the
1828 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001829static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00001830shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1831{
1832 struct workspace *ws;
1833
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001834 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
1835 !shsurf->state.lowered) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001836 return &shsurf->shell->fullscreen_layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001837 }
1838
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001839 /* Move the surface to a normal workspace layer so that surfaces
1840 * which were previously fullscreen or transient are no longer
1841 * rendered on top. */
1842 ws = get_current_workspace(shsurf->shell);
1843 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001844}
1845
Philip Withnall648a4dd2013-11-25 18:01:44 +00001846static void
1847shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
1848{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001849 weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001850}
1851
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001852/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00001853 * geometry to ensure the changes are redrawn.
1854 *
1855 * If any child surfaces exist and are mapped, ensure they’re in the same layer
1856 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001857static void
1858shell_surface_update_layer(struct shell_surface *shsurf)
1859{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001860 struct weston_surface *surface =
1861 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001862 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001863
1864 new_layer_link = shell_surface_calculate_layer_link(shsurf);
1865
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001866 if (new_layer_link == NULL)
1867 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001868 if (new_layer_link == &shsurf->view->layer_link)
1869 return;
1870
1871 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001872 weston_layer_entry_remove(&shsurf->view->layer_link);
1873 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001874 weston_view_geometry_dirty(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001875 weston_surface_damage(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001876
1877 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001878}
1879
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001880static void
Semi Malinen99f8c082018-05-02 11:10:32 +02001881notify_output_destroy(struct wl_listener *listener, void *data)
1882{
1883 struct shell_surface *shsurf =
1884 container_of(listener,
1885 struct shell_surface, output_destroy_listener);
1886
1887 shsurf->output = NULL;
1888 shsurf->output_destroy_listener.notify = NULL;
1889}
1890
1891static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00001892shell_surface_set_output(struct shell_surface *shsurf,
1893 struct weston_output *output)
1894{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001895 struct weston_surface *es =
1896 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001897
1898 /* get the default output, if the client set it as NULL
Abdur Rehman7f1da1f2017-01-01 19:46:33 +05001899 check whether the output is available */
Philip Withnall352e7ed2013-11-25 18:01:35 +00001900 if (output)
1901 shsurf->output = output;
1902 else if (es->output)
1903 shsurf->output = es->output;
1904 else
1905 shsurf->output = get_default_output(es->compositor);
Semi Malinen99f8c082018-05-02 11:10:32 +02001906
1907 if (shsurf->output_destroy_listener.notify) {
1908 wl_list_remove(&shsurf->output_destroy_listener.link);
1909 shsurf->output_destroy_listener.notify = NULL;
1910 }
1911
1912 if (!shsurf->output)
1913 return;
1914
1915 shsurf->output_destroy_listener.notify = notify_output_destroy;
1916 wl_signal_add(&shsurf->output->destroy_signal,
1917 &shsurf->output_destroy_listener);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001918}
1919
1920static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001921weston_view_set_initial_position(struct weston_view *view,
1922 struct desktop_shell *shell);
1923
1924static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001925unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08001926{
Philip Withnallf85fe842013-11-25 18:01:37 +00001927 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wu4539b082012-03-01 12:57:46 +08001928 wl_list_remove(&shsurf->fullscreen.transform.link);
1929 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00001930
Jason Ekstranda7af7042013-10-12 22:38:11 -05001931 if (shsurf->fullscreen.black_view)
1932 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
1933 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00001934
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001935 if (shsurf->saved_position_valid)
1936 weston_view_set_position(shsurf->view,
1937 shsurf->saved_x, shsurf->saved_y);
1938 else
1939 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02001940 shsurf->saved_position_valid = false;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001941
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001942 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001943 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00001944 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001945 shsurf->saved_rotation_valid = false;
1946 }
Alex Wu4539b082012-03-01 12:57:46 +08001947}
1948
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001949static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001950unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001951{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001952 struct weston_surface *surface =
1953 weston_desktop_surface_get_surface(shsurf->desktop_surface);
1954
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001955 /* undo all maximized things here */
Semi Malinen99f8c082018-05-02 11:10:32 +02001956 shell_surface_set_output(shsurf, get_default_output(surface->compositor));
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001957
1958 if (shsurf->saved_position_valid)
1959 weston_view_set_position(shsurf->view,
1960 shsurf->saved_x, shsurf->saved_y);
1961 else
1962 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02001963 shsurf->saved_position_valid = false;
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001964
1965 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001966 wl_list_insert(&shsurf->view->geometry.transformation_list,
1967 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001968 shsurf->saved_rotation_valid = false;
1969 }
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001970}
1971
Philip Withnallbecb77e2013-11-25 18:01:30 +00001972static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01001973set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001974{
1975 struct shell_surface *shsurf;
1976 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001977 struct weston_view *view;
Jonas Ådahl56958aa2021-10-01 11:12:21 +02001978 struct weston_subsurface *subsurface;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001979
1980 view = get_default_view(surface);
1981 if (!view)
1982 return;
1983
1984 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1985
1986 shsurf = get_shell_surface(surface);
1987 current_ws = get_current_workspace(shsurf->shell);
1988
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001989 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01001990 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001991
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01001992 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02001993 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001994
1995 shell_surface_update_child_surface_layers(shsurf);
Jonas Ådahl56958aa2021-10-01 11:12:21 +02001996
1997 weston_view_damage_below(shsurf->view);
1998 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1999 wl_list_for_each(view, &subsurface->surface->views, surface_link)
2000 weston_view_damage_below(view);
2001 }
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002002}
2003
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002004
Tiago Vignattibe143262012-04-16 17:31:41 +03002005static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002006shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002007{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002008 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002009}
2010
Pekka Paalanen8274d902014-08-06 19:36:51 +03002011static int
2012black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2013{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002014 struct weston_view *fs_view = surface->committed_private;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002015 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002016 int n;
2017 int rem;
2018 int ret;
2019
2020 n = snprintf(buf, len, "black background surface for ");
2021 if (n < 0)
2022 return n;
2023
2024 rem = (int)len - n;
2025 if (rem < 0)
2026 rem = 0;
2027
2028 if (fs_surface->get_label)
2029 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2030 else
2031 ret = snprintf(buf + n, rem, "<unknown>");
2032
2033 if (ret < 0)
2034 return n;
2035
2036 return n + ret;
2037}
2038
Alex Wu21858432012-04-01 20:13:08 +08002039static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002040black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002041
Jason Ekstranda7af7042013-10-12 22:38:11 -05002042static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002043create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002044 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002045 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002046{
Marius Vlad2be09372021-03-08 22:45:52 +02002047 struct weston_solid_color_surface surface_data = {};
Alex Wu4539b082012-03-01 12:57:46 +08002048
Marius Vlad2be09372021-03-08 22:45:52 +02002049 surface_data.surface_committed = black_surface_committed;
2050 surface_data.get_label = black_surface_get_label;
2051 surface_data.surface_private = fs_view;
Alex Wu4539b082012-03-01 12:57:46 +08002052
Marius Vlad2be09372021-03-08 22:45:52 +02002053 surface_data.r = 0;
2054 surface_data.g = 0;
2055 surface_data.b = 0;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002056
Marius Vlad2be09372021-03-08 22:45:52 +02002057 struct weston_view *view =
2058 create_solid_color_surface(ec, &surface_data, x, y, w, h);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002059
2060 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002061}
2062
Philip Withnalled8f1a92013-11-25 18:01:43 +00002063static void
2064shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2065{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002066 struct weston_surface *surface =
2067 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002068 struct weston_output *output = shsurf->fullscreen_output;
2069
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002070 assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
Philip Withnalled8f1a92013-11-25 18:01:43 +00002071
2072 if (!shsurf->fullscreen.black_view)
2073 shsurf->fullscreen.black_view =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002074 create_black_surface(surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002075 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002076 output->x, output->y,
2077 output->width,
2078 output->height);
2079
2080 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002081 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2082 weston_layer_entry_insert(&shsurf->view->layer_link,
2083 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002084 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002085 weston_surface_damage(surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002086
Armin Krezović4663aca2016-06-30 06:04:29 +02002087 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002088 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002089}
2090
Alex Wu4539b082012-03-01 12:57:46 +08002091/* Create black surface and append it to the associated fullscreen surface.
2092 * Handle size dismatch and positioning according to the method. */
2093static void
2094shell_configure_fullscreen(struct shell_surface *shsurf)
2095{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002096 struct weston_surface *surface =
2097 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffob8366642013-04-25 13:57:46 +03002098 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002099
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002100 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002101 weston_layer_entry_remove(&shsurf->view->layer_link);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002102 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2103 &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002104
Pekka Paalanenc63acc42018-05-02 10:21:58 +02002105 if (!shsurf->fullscreen_output) {
2106 /* If there is no output, there's not much we can do.
2107 * Position the window somewhere, whatever. */
2108 weston_view_set_position(shsurf->view, 0, 0);
2109 return;
2110 }
2111
Philip Withnalled8f1a92013-11-25 18:01:43 +00002112 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002113
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002114 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002115 &surf_width, &surf_height);
2116
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002117 if (surface->buffer_ref.buffer)
2118 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002119}
2120
Alex Wu4539b082012-03-01 12:57:46 +08002121static void
2122shell_map_fullscreen(struct shell_surface *shsurf)
2123{
Alex Wubd3354b2012-04-17 17:20:49 +08002124 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002125}
2126
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002127static void
Marius Vladc114fd62021-08-26 16:37:24 +03002128desktop_shell_destroy_seat(struct shell_seat *shseat)
2129{
Marius Vladf3584cf2021-10-26 19:40:44 +03002130
Marius Vlada7392c82021-08-26 16:38:43 +03002131 wl_list_remove(&shseat->keyboard_focus_listener.link);
2132 wl_list_remove(&shseat->caps_changed_listener.link);
2133 wl_list_remove(&shseat->pointer_focus_listener.link);
Marius Vladc114fd62021-08-26 16:37:24 +03002134 wl_list_remove(&shseat->seat_destroy_listener.link);
2135
2136 wl_list_remove(&shseat->link);
2137 free(shseat);
2138}
2139
2140static void
Giulio Camuffo5085a752013-03-25 21:42:45 +01002141destroy_shell_seat(struct wl_listener *listener, void *data)
2142{
2143 struct shell_seat *shseat =
2144 container_of(listener,
2145 struct shell_seat, seat_destroy_listener);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002146
Marius Vladc114fd62021-08-26 16:37:24 +03002147 desktop_shell_destroy_seat(shseat);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002148}
2149
Jason Ekstrand024177c2014-04-21 19:42:58 -05002150static void
2151shell_seat_caps_changed(struct wl_listener *listener, void *data)
2152{
Derek Foreman1281a362015-07-31 16:55:32 -05002153 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05002154 struct shell_seat *seat;
2155
2156 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002157 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002158
Derek Foreman1281a362015-07-31 16:55:32 -05002159 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002160 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002161 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002162 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002163 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05002164 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002165 wl_list_init(&seat->pointer_focus_listener.link);
2166 }
2167}
2168
Giulio Camuffo5085a752013-03-25 21:42:45 +01002169static struct shell_seat *
Marius Vladc114fd62021-08-26 16:37:24 +03002170create_shell_seat(struct desktop_shell *shell, struct weston_seat *seat)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002171{
2172 struct shell_seat *shseat;
2173
2174 shseat = calloc(1, sizeof *shseat);
2175 if (!shseat) {
2176 weston_log("no memory to allocate shell seat\n");
2177 return NULL;
2178 }
2179
2180 shseat->seat = seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002181
2182 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2183 wl_signal_add(&seat->destroy_signal,
2184 &shseat->seat_destroy_listener);
2185
Jason Ekstrand024177c2014-04-21 19:42:58 -05002186 wl_list_init(&shseat->keyboard_focus_listener.link);
2187
2188 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2189 wl_list_init(&shseat->pointer_focus_listener.link);
2190
2191 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2192 wl_signal_add(&seat->updated_caps_signal,
2193 &shseat->caps_changed_listener);
2194 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2195
Marius Vladc114fd62021-08-26 16:37:24 +03002196 wl_list_insert(&shell->seat_list, &shseat->link);
2197
Giulio Camuffo5085a752013-03-25 21:42:45 +01002198 return shseat;
2199}
2200
2201static struct shell_seat *
2202get_shell_seat(struct weston_seat *seat)
2203{
2204 struct wl_listener *listener;
2205
2206 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002207 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002208
2209 return container_of(listener,
2210 struct shell_seat, seat_destroy_listener);
2211}
2212
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002213static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002214fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002215{
2216 struct shell_surface *shsurf = data;
2217
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002218 weston_surface_destroy(shsurf->view->surface);
Pekka Paalanen99628002018-05-22 12:48:35 +03002219
2220 if (shsurf->output_destroy_listener.notify) {
2221 wl_list_remove(&shsurf->output_destroy_listener.link);
2222 shsurf->output_destroy_listener.notify = NULL;
2223 }
2224
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002225 free(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002226}
2227
2228static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002229fade_out_done(struct weston_view_animation *animation, void *data)
2230{
2231 struct shell_surface *shsurf = data;
2232 struct wl_event_loop *loop;
2233
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002234 loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
Derek Foreman039e9be2015-04-14 17:09:06 -05002235
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002236 if (weston_view_is_mapped(shsurf->view)) {
Tomohito Esaki5898cd32019-04-01 17:50:14 +09002237 weston_view_unmap(shsurf->view);
Derek Foreman039e9be2015-04-14 17:09:06 -05002238 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
Derek Foreman039e9be2015-04-14 17:09:06 -05002239 }
2240}
2241
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002242struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002243get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002244{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002245 if (weston_surface_is_desktop_surface(surface)) {
2246 struct weston_desktop_surface *desktop_surface =
2247 weston_surface_get_desktop_surface(surface);
2248 return weston_desktop_surface_get_user_data(desktop_surface);
2249 }
2250 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002251}
2252
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002253/*
2254 * libweston-desktop
2255 */
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002256
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002257static void
2258desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2259 void *shell)
2260{
2261 struct weston_desktop_client *client =
2262 weston_desktop_surface_get_client(desktop_surface);
2263 struct wl_client *wl_client =
2264 weston_desktop_client_get_client(client);
2265 struct weston_view *view;
2266 struct shell_surface *shsurf;
2267 struct weston_surface *surface =
2268 weston_desktop_surface_get_surface(desktop_surface);
2269
2270 view = weston_desktop_surface_create_view(desktop_surface);
2271 if (!view)
2272 return;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002273
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002274 shsurf = calloc(1, sizeof *shsurf);
2275 if (!shsurf) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002276 if (wl_client)
2277 wl_client_post_no_memory(wl_client);
2278 else
2279 weston_log("no memory to allocate shell surface\n");
2280 return;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002281 }
2282
Marius Vlad2be09372021-03-08 22:45:52 +02002283 weston_surface_set_label_func(surface, surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002284
Tiago Vignattibc052c92012-04-19 16:18:18 +03002285 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002286 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002287 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002288 shsurf->saved_rotation_valid = false;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002289 shsurf->desktop_surface = desktop_surface;
2290 shsurf->view = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002291 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002292 wl_list_init(&shsurf->fullscreen.transform.link);
2293
Semi Malinen99f8c082018-05-02 11:10:32 +02002294 shell_surface_set_output(
2295 shsurf, get_default_output(shsurf->shell->compositor));
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002296
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002297 wl_signal_init(&shsurf->destroy_signal);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002298
Pekka Paalanen460099f2012-01-20 16:48:25 +02002299 /* empty when not in use */
2300 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002301 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002302
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002303 wl_list_init(&shsurf->workspace_transform.link);
2304
Stefan Agnera8da2082019-06-23 14:53:59 +02002305 /*
2306 * initialize list as well as link. The latter allows to use
2307 * wl_list_remove() even when this surface is not in another list.
2308 */
2309 wl_list_init(&shsurf->children_list);
2310 wl_list_init(&shsurf->children_link);
2311
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002312 weston_desktop_surface_set_user_data(desktop_surface, shsurf);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002313}
2314
Tiago Vignattibc052c92012-04-19 16:18:18 +03002315static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002316desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2317 void *shell)
2318{
2319 struct shell_surface *shsurf =
2320 weston_desktop_surface_get_user_data(desktop_surface);
Stefan Agnera8da2082019-06-23 14:53:59 +02002321 struct shell_surface *shsurf_child, *tmp;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002322 struct weston_surface *surface =
2323 weston_desktop_surface_get_surface(desktop_surface);
Marius Vladf12697b2021-03-05 21:44:26 +02002324 struct weston_seat *seat;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002325
2326 if (!shsurf)
2327 return;
2328
Stefan Agnera8da2082019-06-23 14:53:59 +02002329 wl_list_for_each_safe(shsurf_child, tmp, &shsurf->children_list, children_link) {
2330 wl_list_remove(&shsurf_child->children_link);
2331 wl_list_init(&shsurf_child->children_link);
2332 }
2333 wl_list_remove(&shsurf->children_link);
2334
Marius Vladf12697b2021-03-05 21:44:26 +02002335 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2336 struct shell_seat *shseat = get_shell_seat(seat);
2337 /* activate() controls the focused surface activation and
2338 * removal of a surface requires invalidating the
2339 * focused_surface to avoid activate() use a stale (and just
2340 * removed) surface when attempting to de-activate it. It will
2341 * also update the focused_surface once it has a chance to run.
2342 */
2343 if (surface == shseat->focused_surface)
2344 shseat->focused_surface = NULL;
2345 }
2346
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002347 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2348
2349 if (shsurf->fullscreen.black_view)
2350 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2351
2352 weston_surface_set_label_func(surface, NULL);
2353 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2354 shsurf->desktop_surface = NULL;
2355
Quentin Glidicf01ecee2016-08-16 10:52:46 +02002356 weston_desktop_surface_unlink_view(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002357 if (weston_surface_is_mapped(surface) &&
2358 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
Erik Kurzinger04918f32021-05-18 11:49:37 -04002359
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02002360 if (shsurf->shell->compositor->state == WESTON_COMPOSITOR_ACTIVE) {
2361 pixman_region32_fini(&surface->pending.input);
2362 pixman_region32_init(&surface->pending.input);
2363 pixman_region32_fini(&surface->input);
2364 pixman_region32_init(&surface->input);
2365 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2366 fade_out_done, shsurf);
2367 return;
2368 } else {
Marius Vladc30cb292021-08-02 15:47:16 +03002369 weston_surface_destroy(surface);
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02002370 }
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002371 }
Erik Kurzinger04918f32021-05-18 11:49:37 -04002372
Marius Vladc30cb292021-08-02 15:47:16 +03002373 weston_view_destroy(shsurf->view);
Erik Kurzinger04918f32021-05-18 11:49:37 -04002374
2375 if (shsurf->output_destroy_listener.notify) {
2376 wl_list_remove(&shsurf->output_destroy_listener.link);
2377 shsurf->output_destroy_listener.notify = NULL;
2378 }
2379
2380 free(shsurf);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002381}
2382
2383static void
2384set_maximized_position(struct desktop_shell *shell,
2385 struct shell_surface *shsurf)
2386{
2387 pixman_rectangle32_t area;
2388 struct weston_geometry geometry;
2389
2390 get_output_work_area(shell, shsurf->output, &area);
2391 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2392
2393 weston_view_set_position(shsurf->view,
2394 area.x - geometry.x,
2395 area.y - geometry.y);
2396}
2397
2398static void
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002399set_position_from_xwayland(struct shell_surface *shsurf)
2400{
2401 struct weston_geometry geometry;
2402 float x;
2403 float y;
2404
2405 assert(shsurf->xwayland.is_set);
2406
2407 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2408 x = shsurf->xwayland.x - geometry.x;
2409 y = shsurf->xwayland.y - geometry.y;
2410
2411 weston_view_set_position(shsurf->view, x, y);
2412
2413#ifdef WM_DEBUG
2414 weston_log("%s: XWM %d, %d; geometry %d, %d; view %f, %f\n",
2415 __func__, shsurf->xwayland.x, shsurf->xwayland.y,
2416 geometry.x, geometry.y, x, y);
2417#endif
2418}
2419
2420static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002421map(struct desktop_shell *shell, struct shell_surface *shsurf,
2422 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002423{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002424 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002425 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2426 struct weston_compositor *compositor = shell->compositor;
2427 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002428
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002429 /* initial positioning, see also configure() */
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002430 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002431 center_on_output(shsurf->view, shsurf->fullscreen_output);
2432 shell_map_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002433 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002434 set_maximized_position(shell, shsurf);
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002435 } else if (shsurf->xwayland.is_set) {
2436 set_position_from_xwayland(shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002437 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002438 weston_view_set_initial_position(shsurf->view, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002439 }
2440
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002441 /* Surface stacking order, see also activate(). */
2442 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002443
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002444 weston_view_update_transform(shsurf->view);
2445 shsurf->view->is_mapped = true;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002446 if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002447 surface->output = shsurf->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02002448 weston_view_set_output(shsurf->view, shsurf->output);
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002449 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002450
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002451 if (!shell->locked) {
2452 wl_list_for_each(seat, &compositor->seat_list, link)
2453 activate(shell, shsurf->view, seat,
2454 WESTON_ACTIVATE_FLAG_CONFIGURE);
2455 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002456
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002457 if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002458 switch (shell->win_animation_type) {
2459 case ANIMATION_FADE:
2460 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2461 break;
2462 case ANIMATION_ZOOM:
2463 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2464 break;
2465 case ANIMATION_NONE:
2466 default:
2467 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002468 }
2469 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002470}
2471
2472static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002473desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2474 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002475{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002476 struct shell_surface *shsurf =
2477 weston_desktop_surface_get_user_data(desktop_surface);
2478 struct weston_surface *surface =
2479 weston_desktop_surface_get_surface(desktop_surface);
2480 struct weston_view *view = shsurf->view;
2481 struct desktop_shell *shell = data;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002482 bool was_fullscreen;
2483 bool was_maximized;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002484
2485 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002486 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002487
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002488 was_fullscreen = shsurf->state.fullscreen;
2489 was_maximized = shsurf->state.maximized;
2490
2491 shsurf->state.fullscreen =
2492 weston_desktop_surface_get_fullscreen(desktop_surface);
2493 shsurf->state.maximized =
2494 weston_desktop_surface_get_maximized(desktop_surface);
2495
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002496 if (!weston_surface_is_mapped(surface)) {
2497 map(shell, shsurf, sx, sy);
2498 surface->is_mapped = true;
2499 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2500 ++surface->ref_count;
2501 return;
2502 }
2503
2504 if (sx == 0 && sy == 0 &&
2505 shsurf->last_width == surface->width &&
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002506 shsurf->last_height == surface->height &&
2507 was_fullscreen == shsurf->state.fullscreen &&
2508 was_maximized == shsurf->state.maximized)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002509 return;
2510
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002511 if (was_fullscreen)
2512 unset_fullscreen(shsurf);
2513 if (was_maximized)
2514 unset_maximized(shsurf);
2515
Quentin Glidic920cf042016-08-16 14:26:20 +02002516 if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2517 !shsurf->saved_position_valid) {
2518 shsurf->saved_x = shsurf->view->geometry.x;
2519 shsurf->saved_y = shsurf->view->geometry.y;
2520 shsurf->saved_position_valid = true;
2521
2522 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2523 wl_list_remove(&shsurf->rotation.transform.link);
2524 wl_list_init(&shsurf->rotation.transform.link);
2525 weston_view_geometry_dirty(shsurf->view);
2526 shsurf->saved_rotation_valid = true;
2527 }
2528 }
2529
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002530 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002531 shell_configure_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002532 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002533 set_maximized_position(shell, shsurf);
2534 surface->output = shsurf->output;
2535 } else {
2536 float from_x, from_y;
2537 float to_x, to_y;
2538 float x, y;
2539
2540 if (shsurf->resize_edges) {
2541 sx = 0;
2542 sy = 0;
2543 }
2544
2545 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2546 sx = shsurf->last_width - surface->width;
2547 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2548 sy = shsurf->last_height - surface->height;
2549
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002550 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2551 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2552 x = shsurf->view->geometry.x + to_x - from_x;
2553 y = shsurf->view->geometry.y + to_y - from_y;
2554
2555 weston_view_set_position(shsurf->view, x, y);
2556 }
2557
Emmanuel Gil Peyrotc3941792017-04-04 18:49:33 +01002558 shsurf->last_width = surface->width;
2559 shsurf->last_height = surface->height;
2560
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002561 /* XXX: would a fullscreen surface need the same handling? */
2562 if (surface->output) {
2563 wl_list_for_each(view, &surface->views, surface_link)
2564 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002565 }
2566}
2567
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002568static void
Derek Foreman927d9e22017-10-06 14:37:44 -05002569get_maximized_size(struct shell_surface *shsurf, int32_t *width, int32_t *height)
2570{
2571 struct desktop_shell *shell;
2572 pixman_rectangle32_t area;
2573
2574 shell = shell_surface_get_shell(shsurf);
2575 get_output_work_area(shell, shsurf->output, &area);
2576
2577 *width = area.width;
2578 *height = area.height;
2579}
2580
2581static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002582set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2583 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002584{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002585 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2586 struct weston_surface *surface =
2587 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2588 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002589
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002590 if (fullscreen) {
2591 /* handle clients launching in fullscreen */
2592 if (output == NULL && !weston_surface_is_mapped(surface)) {
2593 /* Set the output to the one that has focus currently. */
2594 output = get_focused_output(surface->compositor);
2595 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002596
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002597 shell_surface_set_output(shsurf, output);
2598 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002599
Marius Vlad6ebda362020-03-28 00:23:14 +02002600 if (shsurf->output) {
2601 width = shsurf->output->width;
2602 height = shsurf->output->height;
2603 }
Derek Foremane1af3d82017-10-06 14:37:45 -05002604 } else if (weston_desktop_surface_get_maximized(desktop_surface)) {
2605 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002606 }
2607 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2608 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002609}
2610
2611static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002612desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2613 struct weston_seat *seat, uint32_t serial, void *shell)
2614{
2615 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2616 struct weston_touch *touch = weston_seat_get_touch(seat);
2617 struct shell_surface *shsurf =
2618 weston_desktop_surface_get_user_data(desktop_surface);
2619 struct weston_surface *surface =
2620 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2621 struct wl_resource *resource = surface->resource;
2622 struct weston_surface *focus;
2623
2624 if (pointer &&
2625 pointer->focus &&
2626 pointer->button_count > 0 &&
2627 pointer->grab_serial == serial) {
2628 focus = weston_surface_get_main_surface(pointer->focus->surface);
2629 if ((focus == surface) &&
2630 (surface_move(shsurf, pointer, true) < 0))
2631 wl_resource_post_no_memory(resource);
2632 } else if (touch &&
2633 touch->focus &&
2634 touch->grab_serial == serial) {
2635 focus = weston_surface_get_main_surface(touch->focus->surface);
2636 if ((focus == surface) &&
2637 (surface_touch_move(shsurf, touch) < 0))
2638 wl_resource_post_no_memory(resource);
2639 }
2640}
2641
2642static void
2643desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2644 struct weston_seat *seat, uint32_t serial,
2645 enum weston_desktop_surface_edge edges, void *shell)
2646{
2647 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2648 struct shell_surface *shsurf =
2649 weston_desktop_surface_get_user_data(desktop_surface);
2650 struct weston_surface *surface =
2651 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2652 struct wl_resource *resource = surface->resource;
2653 struct weston_surface *focus;
2654
2655 if (!pointer ||
2656 pointer->button_count == 0 ||
2657 pointer->grab_serial != serial ||
2658 pointer->focus == NULL)
2659 return;
2660
2661 focus = weston_surface_get_main_surface(pointer->focus->surface);
2662 if (focus != surface)
2663 return;
2664
2665 if (surface_resize(shsurf, pointer, edges) < 0)
2666 wl_resource_post_no_memory(resource);
2667}
2668
2669static void
Stefan Agnera8da2082019-06-23 14:53:59 +02002670desktop_surface_set_parent(struct weston_desktop_surface *desktop_surface,
2671 struct weston_desktop_surface *parent,
2672 void *shell)
2673{
Marius Vlada27658e2020-01-17 12:32:55 +02002674 struct shell_surface *shsurf_parent;
Stefan Agnera8da2082019-06-23 14:53:59 +02002675 struct shell_surface *shsurf =
2676 weston_desktop_surface_get_user_data(desktop_surface);
Stefan Agnera8da2082019-06-23 14:53:59 +02002677
Marius Vlada27658e2020-01-17 12:32:55 +02002678 /* unlink any potential child */
2679 wl_list_remove(&shsurf->children_link);
2680
2681 if (parent) {
2682 shsurf_parent = weston_desktop_surface_get_user_data(parent);
2683 wl_list_insert(shsurf_parent->children_list.prev,
2684 &shsurf->children_link);
2685 } else {
2686 wl_list_init(&shsurf->children_link);
2687 }
Stefan Agnera8da2082019-06-23 14:53:59 +02002688}
2689
2690static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002691desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2692 bool fullscreen,
2693 struct weston_output *output, void *shell)
2694{
2695 struct shell_surface *shsurf =
2696 weston_desktop_surface_get_user_data(desktop_surface);
2697
2698 set_fullscreen(shsurf, fullscreen, output);
2699}
2700
2701static void
2702set_maximized(struct shell_surface *shsurf, bool maximized)
2703{
2704 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2705 struct weston_surface *surface =
2706 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2707 int32_t width = 0, height = 0;
2708
2709 if (maximized) {
2710 struct weston_output *output;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002711
2712 if (!weston_surface_is_mapped(surface))
2713 output = get_focused_output(surface->compositor);
2714 else
2715 output = surface->output;
2716
2717 shell_surface_set_output(shsurf, output);
2718
Derek Foreman927d9e22017-10-06 14:37:44 -05002719 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002720 }
2721 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2722 weston_desktop_surface_set_size(desktop_surface, width, height);
2723}
2724
2725static void
2726desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2727 bool maximized, void *shell)
2728{
2729 struct shell_surface *shsurf =
2730 weston_desktop_surface_get_user_data(desktop_surface);
2731
2732 set_maximized(shsurf, maximized);
2733}
2734
2735static void
2736desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2737 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002738{
2739 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002740 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002741
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002742 /* apply compositor's own minimization logic (hide) */
2743 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002744}
2745
Rafael Antognollie2a34552013-12-03 15:35:45 -02002746static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002747set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002748{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002749 struct shell_grab *grab;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002750
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002751 if (pointer->grab->interface == &busy_cursor_grab_interface)
2752 return;
2753
2754 grab = malloc(sizeof *grab);
2755 if (!grab)
2756 return;
2757
2758 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2759 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2760 /* Mark the shsurf as ungrabbed so that button binding is able
2761 * to move it. */
2762 shsurf->grabbed = 0;
2763}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002764
2765static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002766end_busy_cursor(struct weston_compositor *compositor,
2767 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002768{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002769 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002770 struct shell_grab *grab;
2771 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002772
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002773 wl_list_for_each(seat, &compositor->seat_list, link) {
2774 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2775 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002776
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002777 if (!pointer)
2778 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002779
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002780 if (pointer->grab->interface != &busy_cursor_grab_interface)
2781 continue;
2782
2783 grab = (struct shell_grab *) pointer->grab;
2784 shsurf = grab->shsurf;
2785 if (!shsurf)
2786 continue;
2787
2788 grab_client =
2789 weston_desktop_surface_get_client(shsurf->desktop_surface);
2790 if (grab_client == desktop_client) {
2791 shell_grab_end(grab);
2792 free(grab);
2793 }
2794 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002795}
2796
2797static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002798desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2799 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002800{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002801 struct shell_surface *shsurf =
2802 weston_desktop_surface_get_user_data(desktop_surface);
2803 bool *unresponsive = user_data;
2804
2805 shsurf->unresponsive = *unresponsive;
2806}
2807
2808static void
2809desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2810 void *shell_)
2811{
2812 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002813 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002814 struct weston_seat *seat;
2815 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002816
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002817 weston_desktop_client_for_each_surface(desktop_client,
2818 desktop_surface_set_unresponsive,
2819 &unresponsive);
2820
2821
2822 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2823 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2824 struct weston_desktop_client *grab_client;
2825
2826 if (!pointer || !pointer->focus)
2827 continue;
2828
2829 shsurf = get_shell_surface(pointer->focus->surface);
2830 if (!shsurf)
2831 continue;
2832
2833 grab_client =
2834 weston_desktop_surface_get_client(shsurf->desktop_surface);
2835 if (grab_client == desktop_client)
2836 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002837 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002838}
2839
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002840static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002841desktop_surface_pong(struct weston_desktop_client *desktop_client,
2842 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002843{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002844 struct desktop_shell *shell = shell_;
2845 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002846
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002847 weston_desktop_client_for_each_surface(desktop_client,
2848 desktop_surface_set_unresponsive,
2849 &unresponsive);
2850 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002851}
2852
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002853static void
2854desktop_surface_set_xwayland_position(struct weston_desktop_surface *surface,
2855 int32_t x, int32_t y, void *shell_)
2856{
2857 struct shell_surface *shsurf =
2858 weston_desktop_surface_get_user_data(surface);
2859
2860 shsurf->xwayland.x = x;
2861 shsurf->xwayland.y = y;
2862 shsurf->xwayland.is_set = true;
2863}
2864
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002865static const struct weston_desktop_api shell_desktop_api = {
2866 .struct_size = sizeof(struct weston_desktop_api),
2867 .surface_added = desktop_surface_added,
2868 .surface_removed = desktop_surface_removed,
2869 .committed = desktop_surface_committed,
2870 .move = desktop_surface_move,
2871 .resize = desktop_surface_resize,
Stefan Agnera8da2082019-06-23 14:53:59 +02002872 .set_parent = desktop_surface_set_parent,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002873 .fullscreen_requested = desktop_surface_fullscreen_requested,
2874 .maximized_requested = desktop_surface_maximized_requested,
2875 .minimized_requested = desktop_surface_minimized_requested,
2876 .ping_timeout = desktop_surface_ping_timeout,
2877 .pong = desktop_surface_pong,
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002878 .set_xwayland_position = desktop_surface_set_xwayland_position,
Rafael Antognollie2a34552013-12-03 15:35:45 -02002879};
2880
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002881/* ************************ *
2882 * end of libweston-desktop *
2883 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04002884static void
Quentin Glidice8bf9592016-06-23 18:55:20 +02002885configure_static_view(struct weston_view *ev, struct weston_layer *layer, int x, int y)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002886{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002887 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002888
Semi Malinene7a52fb2018-04-26 11:08:10 +02002889 if (!ev->output)
2890 return;
2891
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002892 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002893 if (v->output == ev->output && v != ev) {
2894 weston_view_unmap(v);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002895 v->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002896 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002897 }
2898 }
2899
Quentin Glidice8bf9592016-06-23 18:55:20 +02002900 weston_view_set_position(ev, ev->output->x + x, ev->output->y + y);
Armin Krezović4663aca2016-06-30 06:04:29 +02002901 ev->surface->is_mapped = true;
2902 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002903
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002904 if (wl_list_empty(&ev->layer_link.link)) {
2905 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002906 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002907 }
2908}
2909
David Fort50d962f2016-06-09 17:55:52 +02002910
2911static struct shell_output *
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002912find_shell_output_from_weston_output(struct desktop_shell *shell,
2913 struct weston_output *output)
David Fort50d962f2016-06-09 17:55:52 +02002914{
2915 struct shell_output *shell_output;
2916
2917 wl_list_for_each(shell_output, &shell->output_list, link) {
2918 if (shell_output->output == output)
2919 return shell_output;
2920 }
2921
2922 return NULL;
2923}
2924
Pekka Paalanen8274d902014-08-06 19:36:51 +03002925static int
2926background_get_label(struct weston_surface *surface, char *buf, size_t len)
2927{
2928 return snprintf(buf, len, "background for output %s",
Miguel A. Vico620f68d2019-09-25 11:23:13 -07002929 (surface->output ? surface->output->name : "NULL"));
Pekka Paalanen8274d902014-08-06 19:36:51 +03002930}
2931
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002932static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002933background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002934{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002935 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002936 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002937
Jason Ekstranda7af7042013-10-12 22:38:11 -05002938 view = container_of(es->views.next, struct weston_view, surface_link);
2939
Quentin Glidice8bf9592016-06-23 18:55:20 +02002940 configure_static_view(view, &shell->background_layer, 0, 0);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002941}
2942
2943static void
David Fort50d962f2016-06-09 17:55:52 +02002944handle_background_surface_destroy(struct wl_listener *listener, void *data)
2945{
2946 struct shell_output *output =
2947 container_of(listener, struct shell_output, background_surface_listener);
2948
2949 weston_log("background surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09002950 wl_list_remove(&output->background_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02002951 output->background_surface = NULL;
2952}
2953
2954static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002955desktop_shell_set_background(struct wl_client *client,
2956 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002957 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002958 struct wl_resource *surface_resource)
2959{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002960 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002961 struct weston_surface *surface =
2962 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02002963 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002964 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002965
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002966 if (surface->committed) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002967 wl_resource_post_error(surface_resource,
2968 WL_DISPLAY_ERROR_INVALID_OBJECT,
2969 "surface role already assigned");
2970 return;
2971 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002972
Jason Ekstranda7af7042013-10-12 22:38:11 -05002973 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2974 weston_view_destroy(view);
2975 view = weston_view_create(surface);
2976
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002977 surface->committed = background_committed;
2978 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002979 weston_surface_set_label_func(surface, background_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03002980 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02002981 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02002982
2983 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02002984 if (sh_output->background_surface) {
2985 /* The output already has a background, tell our helper
2986 * there is no need for another one. */
2987 weston_desktop_shell_send_configure(resource, 0,
2988 surface_resource,
2989 0, 0);
2990 } else {
2991 weston_desktop_shell_send_configure(resource, 0,
2992 surface_resource,
2993 surface->output->width,
2994 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02002995
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02002996 sh_output->background_surface = surface;
2997
2998 sh_output->background_surface_listener.notify =
2999 handle_background_surface_destroy;
3000 wl_signal_add(&surface->destroy_signal,
3001 &sh_output->background_surface_listener);
3002 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003003}
3004
Pekka Paalanen8274d902014-08-06 19:36:51 +03003005static int
3006panel_get_label(struct weston_surface *surface, char *buf, size_t len)
3007{
3008 return snprintf(buf, len, "panel for output %s",
Miguel A. Vico620f68d2019-09-25 11:23:13 -07003009 (surface->output ? surface->output->name : "NULL"));
Pekka Paalanen8274d902014-08-06 19:36:51 +03003010}
3011
Kristian Høgsberg75840622011-09-06 13:48:16 -04003012static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003013panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003014{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003015 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003016 struct weston_view *view;
Quentin Glidice8bf9592016-06-23 18:55:20 +02003017 int width, height;
3018 int x = 0, y = 0;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003019
Jason Ekstranda7af7042013-10-12 22:38:11 -05003020 view = container_of(es->views.next, struct weston_view, surface_link);
3021
Quentin Glidice8bf9592016-06-23 18:55:20 +02003022 get_panel_size(shell, view, &width, &height);
3023 switch (shell->panel_position) {
3024 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
3025 break;
3026 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
3027 y = view->output->height - height;
3028 break;
3029 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
3030 break;
3031 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
3032 x = view->output->width - width;
3033 break;
3034 }
3035
3036 configure_static_view(view, &shell->panel_layer, x, y);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003037}
3038
3039static void
David Fort50d962f2016-06-09 17:55:52 +02003040handle_panel_surface_destroy(struct wl_listener *listener, void *data)
3041{
3042 struct shell_output *output =
3043 container_of(listener, struct shell_output, panel_surface_listener);
3044
3045 weston_log("panel surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09003046 wl_list_remove(&output->panel_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02003047 output->panel_surface = NULL;
3048}
3049
3050
3051static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003052desktop_shell_set_panel(struct wl_client *client,
3053 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003054 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003055 struct wl_resource *surface_resource)
3056{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003057 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003058 struct weston_surface *surface =
3059 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003060 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02003061 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003062
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003063 if (surface->committed) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003064 wl_resource_post_error(surface_resource,
3065 WL_DISPLAY_ERROR_INVALID_OBJECT,
3066 "surface role already assigned");
3067 return;
3068 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003069
Jason Ekstranda7af7042013-10-12 22:38:11 -05003070 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3071 weston_view_destroy(view);
3072 view = weston_view_create(surface);
3073
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003074 surface->committed = panel_committed;
3075 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003076 weston_surface_set_label_func(surface, panel_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03003077 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02003078 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02003079
3080 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003081 if (sh_output->panel_surface) {
3082 /* The output already has a panel, tell our helper
3083 * there is no need for another one. */
3084 weston_desktop_shell_send_configure(resource, 0,
3085 surface_resource,
3086 0, 0);
3087 } else {
3088 weston_desktop_shell_send_configure(resource, 0,
3089 surface_resource,
3090 surface->output->width,
3091 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003092
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003093 sh_output->panel_surface = surface;
3094
3095 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
3096 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
3097 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003098}
3099
Pekka Paalanen8274d902014-08-06 19:36:51 +03003100static int
3101lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
3102{
3103 return snprintf(buf, len, "lock window");
3104}
3105
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003106static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003107lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003108{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003109 struct desktop_shell *shell = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003110 struct weston_view *view;
3111
3112 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003113
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003114 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003115 return;
3116
Jason Ekstranda7af7042013-10-12 22:38:11 -05003117 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003118
3119 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003120 weston_layer_entry_insert(&shell->lock_layer.view_list,
3121 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003122 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003123 surface->is_mapped = true;
3124 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003125 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003126 }
3127}
3128
3129static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003130handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003131{
Tiago Vignattibe143262012-04-16 17:31:41 +03003132 struct desktop_shell *shell =
3133 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003134
Martin Minarik6d118362012-06-07 18:01:59 +02003135 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003136 shell->lock_surface = NULL;
3137}
3138
3139static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003140desktop_shell_set_lock_surface(struct wl_client *client,
3141 struct wl_resource *resource,
3142 struct wl_resource *surface_resource)
3143{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003144 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003145 struct weston_surface *surface =
3146 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003147
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003148 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003149
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003150 if (!shell->locked)
3151 return;
3152
Pekka Paalanen98262232011-12-01 10:42:22 +02003153 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003154
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003155 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003156 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003157 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003158
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003159 weston_view_create(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003160 surface->committed = lock_surface_committed;
3161 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003162 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003163}
3164
3165static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003166resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003167{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003168 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003169
Quentin Glidic82681572016-12-17 13:40:51 +01003170 weston_layer_unset_position(&shell->lock_layer);
3171
3172 if (shell->showing_input_panels)
3173 weston_layer_set_position(&shell->input_panel_layer,
3174 WESTON_LAYER_POSITION_TOP_UI);
3175 weston_layer_set_position(&shell->fullscreen_layer,
3176 WESTON_LAYER_POSITION_FULLSCREEN);
3177 weston_layer_set_position(&shell->panel_layer,
3178 WESTON_LAYER_POSITION_UI);
3179 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003180
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003181 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003182
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003183 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003184 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003185 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003186}
3187
3188static void
3189desktop_shell_unlock(struct wl_client *client,
3190 struct wl_resource *resource)
3191{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003192 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003193
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003194 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003195
3196 if (shell->locked)
3197 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003198}
3199
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003200static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003201desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003202 struct wl_resource *resource,
3203 struct wl_resource *surface_resource)
3204{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003205 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003206
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003207 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003208 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003209}
3210
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003211static void
3212desktop_shell_desktop_ready(struct wl_client *client,
3213 struct wl_resource *resource)
3214{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003215 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003216
3217 shell_fade_startup(shell);
3218}
3219
Jonny Lamb765760d2014-08-20 15:53:19 +02003220static void
3221desktop_shell_set_panel_position(struct wl_client *client,
3222 struct wl_resource *resource,
3223 uint32_t position)
3224{
3225 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3226
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003227 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3228 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3229 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3230 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02003231 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003232 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02003233 "bad position argument");
3234 return;
3235 }
3236
3237 shell->panel_position = position;
3238}
3239
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003240static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04003241 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003242 desktop_shell_set_panel,
3243 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003244 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003245 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02003246 desktop_shell_desktop_ready,
3247 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04003248};
3249
3250static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003251move_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003252 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003253{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003254 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003255 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003256 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003257
Derek Foreman8ae2db52015-07-15 13:00:36 -05003258 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003259 return;
3260
Derek Foreman8ae2db52015-07-15 13:00:36 -05003261 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003262
Pekka Paalanen01388e22013-04-25 13:57:44 +03003263 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003264 if (surface == NULL)
3265 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003266
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003267 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003268 if (shsurf == NULL ||
3269 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3270 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003271 return;
3272
Derek Foreman794fa0e2015-07-15 13:00:38 -05003273 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003274}
3275
3276static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003277maximize_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003278 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003279{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003280 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003281 struct weston_surface *surface;
3282 struct shell_surface *shsurf;
3283
3284 surface = weston_surface_get_main_surface(focus);
3285 if (surface == NULL)
3286 return;
3287
3288 shsurf = get_shell_surface(surface);
3289 if (shsurf == NULL)
3290 return;
3291
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003292 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003293}
3294
3295static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003296fullscreen_binding(struct weston_keyboard *keyboard,
3297 const struct timespec *time, uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003298{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003299 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003300 struct weston_surface *surface;
3301 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003302 bool fullscreen;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003303
3304 surface = weston_surface_get_main_surface(focus);
3305 if (surface == NULL)
3306 return;
3307
3308 shsurf = get_shell_surface(surface);
3309 if (shsurf == NULL)
3310 return;
3311
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003312 fullscreen =
3313 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003314
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003315 set_fullscreen(shsurf, !fullscreen, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003316}
3317
3318static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003319touch_move_binding(struct weston_touch *touch, const struct timespec *time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01003320{
Derek Foreman362656b2014-09-04 10:23:05 -05003321 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01003322 struct weston_surface *surface;
3323 struct shell_surface *shsurf;
3324
Derek Foreman8ae2db52015-07-15 13:00:36 -05003325 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05003326 return;
3327
Derek Foreman8ae2db52015-07-15 13:00:36 -05003328 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003329 surface = weston_surface_get_main_surface(focus);
3330 if (surface == NULL)
3331 return;
3332
3333 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003334 if (shsurf == NULL ||
3335 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3336 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Neil Robertsaba0f252013-10-03 16:43:05 +01003337 return;
3338
Derek Foremanb7674ae2015-07-15 13:00:37 -05003339 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01003340}
3341
3342static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003343resize_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003344 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003345{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003346 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003347 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003348 uint32_t edges = 0;
3349 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003350 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003351
Derek Foreman8ae2db52015-07-15 13:00:36 -05003352 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003353 return;
3354
Derek Foreman8ae2db52015-07-15 13:00:36 -05003355 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003356
Pekka Paalanen01388e22013-04-25 13:57:44 +03003357 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003358 if (surface == NULL)
3359 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003360
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003361 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003362 if (shsurf == NULL ||
3363 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3364 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003365 return;
3366
Jason Ekstranda7af7042013-10-12 22:38:11 -05003367 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003368 wl_fixed_to_int(pointer->grab_x),
3369 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05003370 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003371
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003372 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003373 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003374 else if (x < 2 * surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003375 edges |= 0;
3376 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003377 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003378
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003379 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003380 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003381 else if (y < 2 * surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003382 edges |= 0;
3383 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003384 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003385
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003386 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003387}
3388
3389static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003390surface_opacity_binding(struct weston_pointer *pointer,
3391 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003392 struct weston_pointer_axis_event *event,
3393 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003394{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003395 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003396 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003397 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003398 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003399
Pekka Paalanen01388e22013-04-25 13:57:44 +03003400 /* XXX: broken for windows containing sub-surfaces */
3401 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003402 if (surface == NULL)
3403 return;
3404
3405 shsurf = get_shell_surface(surface);
3406 if (!shsurf)
3407 return;
3408
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003409 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003410
Jason Ekstranda7af7042013-10-12 22:38:11 -05003411 if (shsurf->view->alpha > 1.0)
3412 shsurf->view->alpha = 1.0;
3413 if (shsurf->view->alpha < step)
3414 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003415
Jason Ekstranda7af7042013-10-12 22:38:11 -05003416 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003417 weston_surface_damage(surface);
3418}
3419
3420static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003421do_zoom(struct weston_seat *seat, const struct timespec *time, uint32_t key,
3422 uint32_t axis, double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003423{
Derek Foreman8aeeac82015-01-30 13:24:36 -06003424 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05003425 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003426 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003427 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003428
Derek Foreman1281a362015-07-31 16:55:32 -05003429 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06003430 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3431 return;
3432 }
3433
Scott Moreauccbf29d2012-02-22 14:21:41 -07003434 wl_list_for_each(output, &compositor->output_list, link) {
3435 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05003436 wl_fixed_to_double(pointer->x),
3437 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003438 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003439 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003440 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003441 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003442 increment = -output->zoom.increment;
3443 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003444 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003445 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003446 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003447 else
3448 increment = 0;
3449
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003450 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003451
Scott Moreaue6603982012-06-11 13:07:51 -06003452 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003453 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003454 else if (output->zoom.level > output->zoom.max_level)
3455 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05003456
3457 if (!output->zoom.active) {
3458 if (output->zoom.level <= 0.0)
3459 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05003460 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003461 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003462
Scott Moreaue6603982012-06-11 13:07:51 -06003463 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003464
Jason Ekstranda7af7042013-10-12 22:38:11 -05003465 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003466 }
3467 }
3468}
3469
Scott Moreauccbf29d2012-02-22 14:21:41 -07003470static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003471zoom_axis_binding(struct weston_pointer *pointer, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003472 struct weston_pointer_axis_event *event,
3473 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003474{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003475 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003476}
3477
3478static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003479zoom_key_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003480 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003481{
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003482 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003483}
3484
3485static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003486terminate_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003487 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003488{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003489 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003490
Pekka Paalanen052032d2019-02-06 10:44:37 +02003491 weston_compositor_exit(compositor);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003492}
3493
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003494static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003495rotate_grab_motion(struct weston_pointer_grab *grab,
3496 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003497 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003498{
3499 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003500 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003501 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003502 struct shell_surface *shsurf = rotate->base.shsurf;
Sergey Bugaev14ef2012019-02-11 22:55:09 +03003503 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003504 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003505
Jonas Ådahld2510102014-10-05 21:39:14 +02003506 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003507
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003508 if (!shsurf)
3509 return;
3510
Sergey Bugaev14ef2012019-02-11 22:55:09 +03003511 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
3512
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003513 cx = 0.5f * surface->width;
3514 cy = 0.5f * surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003515
Daniel Stone37816df2012-05-16 18:45:18 +01003516 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3517 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003518 r = sqrtf(dx * dx + dy * dy);
3519
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003520 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003521 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003522
3523 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003524 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003525 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003526
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003527 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003528 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003529
3530 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003531 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003532 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003533 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003534 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003535
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003536 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003537 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003538 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003539 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003540 wl_list_init(&shsurf->rotation.transform.link);
3541 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003542 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003543 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003544
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003545 /* We need to adjust the position of the surface
3546 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003547 cposx = shsurf->view->geometry.x + cx;
3548 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003549 dposx = rotate->center.x - cposx;
3550 dposy = rotate->center.y - cposy;
3551 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003552 weston_view_set_position(shsurf->view,
3553 shsurf->view->geometry.x + dposx,
3554 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003555 }
3556
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003557 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003558 * lazily applies the damage due to rotation update.
3559 */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003560 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003561}
3562
3563static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003564rotate_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003565 const struct timespec *time,
3566 uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003567{
3568 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003569 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003570 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003571 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003572 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003573
Daniel Stone4dbadb12012-05-30 16:31:51 +01003574 if (pointer->button_count == 0 &&
3575 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003576 if (shsurf)
3577 weston_matrix_multiply(&shsurf->rotation.rotation,
3578 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003579 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003580 free(rotate);
3581 }
3582}
3583
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003584static void
3585rotate_grab_cancel(struct weston_pointer_grab *grab)
3586{
3587 struct rotate_grab *rotate =
3588 container_of(grab, struct rotate_grab, base.grab);
3589
3590 shell_grab_end(&rotate->base);
3591 free(rotate);
3592}
3593
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003594static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003595 noop_grab_focus,
3596 rotate_grab_motion,
3597 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003598 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003599 noop_grab_axis_source,
3600 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003601 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003602};
3603
3604static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003605surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003606{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003607 struct weston_surface *surface =
3608 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003609 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003610 float dx, dy;
3611 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003612
Pekka Paalanen460099f2012-01-20 16:48:25 +02003613 rotate = malloc(sizeof *rotate);
3614 if (!rotate)
3615 return;
3616
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003617 weston_view_to_global_float(shsurf->view,
3618 surface->width * 0.5f,
3619 surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003620 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003621
Derek Foreman74de4692015-07-15 13:00:39 -05003622 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3623 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003624 r = sqrtf(dx * dx + dy * dy);
3625 if (r > 20.0f) {
3626 struct weston_matrix inverse;
3627
3628 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003629 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003630 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003631
3632 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003633 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003634 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003635 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003636 weston_matrix_init(&rotate->rotation);
3637 }
3638
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003639 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003640 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003641}
3642
3643static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003644rotate_binding(struct weston_pointer *pointer, const struct timespec *time,
3645 uint32_t button, void *data)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003646{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003647 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003648 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003649 struct shell_surface *surface;
3650
Derek Foreman8ae2db52015-07-15 13:00:36 -05003651 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003652 return;
3653
Derek Foreman8ae2db52015-07-15 13:00:36 -05003654 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003655
Pekka Paalanen01388e22013-04-25 13:57:44 +03003656 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003657 if (base_surface == NULL)
3658 return;
3659
3660 surface = get_shell_surface(base_surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003661 if (surface == NULL ||
3662 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3663 weston_desktop_surface_get_maximized(surface->desktop_surface))
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003664 return;
3665
Derek Foreman74de4692015-07-15 13:00:39 -05003666 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003667}
3668
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003669/* Move all fullscreen layers down to the current workspace and hide their
3670 * black views. The surfaces' state is set to both fullscreen and lowered,
3671 * and this is reversed when such a surface is re-configured, see
3672 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3673 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003674 * lowering_output = NULL - Lower on all outputs, else only lower on the
3675 * specified output.
3676 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003677 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003678 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003679void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003680lower_fullscreen_layer(struct desktop_shell *shell,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003681 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003682{
3683 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003684 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003685
3686 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003687 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003688 &shell->fullscreen_layer.view_list.link,
3689 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003690 struct shell_surface *shsurf = get_shell_surface(view->surface);
3691
3692 if (!shsurf)
3693 continue;
3694
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003695 /* Only lower surfaces which have lowering_output as their fullscreen
3696 * output, unless a NULL output asks for lowering on all outputs.
3697 */
3698 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3699 continue;
3700
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003701 /* We can have a non-fullscreen popup for a fullscreen surface
3702 * in the fullscreen layer. */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003703 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003704 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003705 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3706 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07003707 weston_view_damage_below(shsurf->fullscreen.black_view);
3708
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003709 }
3710
3711 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003712 weston_layer_entry_remove(&view->layer_link);
3713 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003714 weston_view_damage_below(view);
3715 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003716
3717 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003718 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003719}
3720
Stefan Agnera8da2082019-06-23 14:53:59 +02003721static struct shell_surface *get_last_child(struct shell_surface *shsurf)
3722{
3723 struct shell_surface *shsurf_child;
3724
3725 wl_list_for_each_reverse(shsurf_child, &shsurf->children_list, children_link) {
3726 if (weston_view_is_mapped(shsurf_child->view))
3727 return shsurf_child;
3728 }
3729
3730 return NULL;
3731}
3732
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003733void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003734activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003735 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003736{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003737 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003738 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003739 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003740 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003741 struct weston_surface *old_es;
Stefan Agnera8da2082019-06-23 14:53:59 +02003742 struct shell_surface *shsurf, *shsurf_child;
Marius Vladf12697b2021-03-05 21:44:26 +02003743 struct shell_seat *shseat = get_shell_seat(seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003744
Pekka Paalanen01388e22013-04-25 13:57:44 +03003745 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003746 shsurf = get_shell_surface(main_surface);
3747 assert(shsurf);
3748
Stefan Agnera8da2082019-06-23 14:53:59 +02003749 shsurf_child = get_last_child(shsurf);
3750 if (shsurf_child) {
3751 /* Activate last xdg child instead of parent. */
3752 activate(shell, shsurf_child->view, seat, flags);
3753 return;
3754 }
3755
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003756 /* Only demote fullscreen surfaces on the output of activated shsurf.
3757 * Leave fullscreen surfaces on unrelated outputs alone. */
Pekka Paalanen87860c22018-05-02 10:21:57 +02003758 if (shsurf->output)
3759 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003760
Marius Vladd6ccc8b2021-03-05 22:07:30 +02003761 weston_view_activate_input(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003762
Marius Vladf12697b2021-03-05 21:44:26 +02003763 if (shseat->focused_surface) {
3764 struct shell_surface *current_focus =
3765 get_shell_surface(shseat->focused_surface);
3766 assert(current_focus);
3767 shell_surface_deactivate(current_focus);
3768 }
3769
3770 shseat->focused_surface = main_surface;
3771 shell_surface_activate(shsurf);
3772
Jonas Ådahl8538b222012-08-29 22:13:03 +02003773 state = ensure_focus_state(shell, seat);
3774 if (state == NULL)
3775 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003776
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003777 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08003778 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003779
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003780 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3781 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02003782 shell_configure_fullscreen(shsurf);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003783
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01003784 /* Update the surface’s layer. This brings it to the top of the stacking
3785 * order as appropriate. */
3786 shell_surface_update_layer(shsurf);
3787
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003788 if (shell->focus_animation_type != ANIMATION_NONE) {
3789 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003790 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003791 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003792}
3793
Alex Wu21858432012-04-01 20:13:08 +08003794/* no-op func for checking black surface */
3795static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003796black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08003797{
3798}
3799
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003800static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003801is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08003802{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003803 struct weston_surface *surface = view->surface;
3804
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003805 if (surface->committed == black_surface_committed) {
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003806 if (fs_view)
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003807 *fs_view = surface->committed_private;
Alex Wu21858432012-04-01 20:13:08 +08003808 return true;
3809 }
3810 return false;
3811}
3812
Kristian Høgsberg75840622011-09-06 13:48:16 -04003813static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003814activate_binding(struct weston_seat *seat,
3815 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003816 struct weston_view *focus_view,
3817 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003818{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003819 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003820 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003821
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003822 if (!focus_view)
3823 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003824
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003825 if (is_black_surface_view(focus_view, &main_view))
3826 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08003827
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003828 main_surface = weston_surface_get_main_surface(focus_view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003829 if (!get_shell_surface(main_surface))
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003830 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003831
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003832 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01003833}
3834
3835static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003836click_to_activate_binding(struct weston_pointer *pointer,
3837 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003838 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003839{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003840 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003841 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003842 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08003843 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003844
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003845 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003846 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003847 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01003848}
3849
3850static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003851touch_to_activate_binding(struct weston_touch *touch,
3852 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003853 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003854{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003855 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003856 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003857 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08003858 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003859
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003860 activate_binding(touch->seat, data, touch->focus,
3861 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003862}
3863
3864static void
Neil Robertsb4a91702014-04-09 16:33:32 +01003865unfocus_all_seats(struct desktop_shell *shell)
3866{
3867 struct weston_seat *seat, *next;
3868
3869 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003870 struct weston_keyboard *keyboard =
3871 weston_seat_get_keyboard(seat);
3872
3873 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01003874 continue;
3875
Derek Foreman1281a362015-07-31 16:55:32 -05003876 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01003877 }
3878}
3879
3880static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003881lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003882{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003883 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003884
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003885 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003886 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003887 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003888 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003889
3890 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003891
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003892 /* Hide all surfaces by removing the fullscreen, panel and
3893 * toplevel layers. This way nothing else can show or receive
3894 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003895
Quentin Glidic82681572016-12-17 13:40:51 +01003896 weston_layer_unset_position(&shell->panel_layer);
3897 weston_layer_unset_position(&shell->fullscreen_layer);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003898 if (shell->showing_input_panels)
Quentin Glidic82681572016-12-17 13:40:51 +01003899 weston_layer_unset_position(&shell->input_panel_layer);
3900 weston_layer_unset_position(&ws->layer);
3901
3902 weston_layer_set_position(&shell->lock_layer,
3903 WESTON_LAYER_POSITION_LOCK);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003904
Derek Foreman004b4a12015-07-20 16:28:13 -05003905 weston_compositor_sleep(shell->compositor);
3906
Neil Robertsb4a91702014-04-09 16:33:32 +01003907 /* Remove the keyboard focus on all seats. This will be
3908 * restored to the workspace's saved state via
3909 * restore_focus_state when the compositor is unlocked */
3910 unfocus_all_seats(shell);
3911
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003912 /* TODO: disable bindings that should not work while locked. */
3913
3914 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003915}
3916
3917static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003918unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003919{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003920 struct wl_resource *shell_resource;
3921
Pekka Paalanend81c2162011-11-16 13:47:34 +02003922 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003923 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003924 return;
3925 }
3926
3927 /* If desktop-shell client has gone away, unlock immediately. */
3928 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003929 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003930 return;
3931 }
3932
3933 if (shell->prepare_event_sent)
3934 return;
3935
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003936 shell_resource = shell->child.desktop_shell;
3937 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003938 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003939}
3940
3941static void
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003942shell_fade_done_for_output(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003943{
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003944 struct shell_output *shell_output = data;
3945 struct desktop_shell *shell = shell_output->shell;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003946
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003947 shell_output->fade.animation = NULL;
3948 switch (shell_output->fade.type) {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003949 case FADE_IN:
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003950 weston_surface_destroy(shell_output->fade.view->surface);
3951 shell_output->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003952 break;
3953 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003954 lock(shell);
3955 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00003956 default:
3957 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003958 }
3959}
3960
Jason Ekstranda7af7042013-10-12 22:38:11 -05003961static struct weston_view *
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003962shell_fade_create_surface_for_output(struct desktop_shell *shell, struct shell_output *shell_output)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003963{
3964 struct weston_compositor *compositor = shell->compositor;
3965 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003966 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003967
3968 surface = weston_surface_create(compositor);
3969 if (!surface)
3970 return NULL;
3971
Jason Ekstranda7af7042013-10-12 22:38:11 -05003972 view = weston_view_create(surface);
3973 if (!view) {
3974 weston_surface_destroy(surface);
3975 return NULL;
3976 }
3977
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003978 weston_surface_set_size(surface, shell_output->output->width, shell_output->output->height);
3979 weston_view_set_position(view, shell_output->output->x, shell_output->output->y);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003980 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003981 weston_layer_entry_insert(&compositor->fade_layer.view_list,
3982 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003983 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02003984 surface->is_mapped = true;
3985 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003986
Jason Ekstranda7af7042013-10-12 22:38:11 -05003987 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003988}
3989
3990static void
3991shell_fade(struct desktop_shell *shell, enum fade_type type)
3992{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003993 float tint;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003994 struct shell_output *shell_output;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003995
3996 switch (type) {
3997 case FADE_IN:
3998 tint = 0.0;
3999 break;
4000 case FADE_OUT:
4001 tint = 1.0;
4002 break;
4003 default:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07004004 weston_log("shell: invalid fade type\n");
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004005 return;
4006 }
4007
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004008 /* Create a separate fade surface for each output */
4009 wl_list_for_each(shell_output, &shell->output_list, link) {
4010 shell_output->fade.type = type;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004011
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004012 if (shell_output->fade.view == NULL) {
4013 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4014 if (!shell_output->fade.view)
4015 continue;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004016
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004017 shell_output->fade.view->alpha = 1.0 - tint;
4018 weston_view_update_transform(shell_output->fade.view);
4019 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004020
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004021 if (shell_output->fade.view->output == NULL) {
4022 /* If the black view gets a NULL output, we lost the
4023 * last output and we'll just cancel the fade. This
4024 * happens when you close the last window under the
4025 * X11 or Wayland backends. */
4026 shell->locked = false;
4027 weston_surface_destroy(shell_output->fade.view->surface);
4028 shell_output->fade.view = NULL;
4029 } else if (shell_output->fade.animation) {
4030 weston_fade_update(shell_output->fade.animation, tint);
4031 } else {
4032 shell_output->fade.animation =
4033 weston_fade_run(shell_output->fade.view,
4034 1.0 - tint, tint, 300.0,
4035 shell_fade_done_for_output, shell_output);
4036 }
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004037 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004038}
4039
4040static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004041do_shell_fade_startup(void *data)
4042{
4043 struct desktop_shell *shell = data;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004044 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004045
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004046 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004047 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004048 } else {
4049 weston_log("desktop shell: "
4050 "unexpected fade-in animation type %d\n",
4051 shell->startup_animation_type);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004052 wl_list_for_each(shell_output, &shell->output_list, link) {
4053 weston_surface_destroy(shell_output->fade.view->surface);
4054 shell_output->fade.view = NULL;
4055 }
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004056 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004057}
4058
4059static void
4060shell_fade_startup(struct desktop_shell *shell)
4061{
4062 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004063 struct shell_output *shell_output;
4064 bool has_fade = false;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004065
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004066 wl_list_for_each(shell_output, &shell->output_list, link) {
4067 if (!shell_output->fade.startup_timer)
4068 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004069
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004070 wl_event_source_remove(shell_output->fade.startup_timer);
4071 shell_output->fade.startup_timer = NULL;
4072 has_fade = true;
4073 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004074
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004075 if (has_fade) {
4076 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4077 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4078 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004079}
4080
4081static int
4082fade_startup_timeout(void *data)
4083{
4084 struct desktop_shell *shell = data;
4085
4086 shell_fade_startup(shell);
4087 return 0;
4088}
4089
4090static void
4091shell_fade_init(struct desktop_shell *shell)
4092{
4093 /* Make compositor output all black, and wait for the desktop-shell
4094 * client to signal it is ready, then fade in. The timer triggers a
4095 * fade-in, in case the desktop-shell client takes too long.
4096 */
4097
4098 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004099 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004100
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004101 if (shell->startup_animation_type == ANIMATION_NONE)
4102 return;
4103
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004104 wl_list_for_each(shell_output, &shell->output_list, link) {
4105 if (shell_output->fade.view != NULL) {
4106 weston_log("%s: warning: fade surface already exists\n",
4107 __func__);
4108 continue;
4109 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004110
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004111 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4112 if (!shell_output->fade.view)
4113 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004114
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004115 weston_view_update_transform(shell_output->fade.view);
4116 weston_surface_damage(shell_output->fade.view->surface);
4117
4118 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4119 shell_output->fade.startup_timer =
4120 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4121 wl_event_source_timer_update(shell_output->fade.startup_timer, 15000);
4122 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004123}
4124
4125static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004126idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004127{
4128 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004129 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004130
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004131 struct weston_seat *seat;
4132
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004133 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4134 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004135
4136 shell_fade(shell, FADE_OUT);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004137 /* lock() is called from shell_fade_done_for_output() */
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004138}
4139
4140static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004141wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004142{
4143 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004144 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004145
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004146 unlock(shell);
4147}
4148
4149static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004150transform_handler(struct wl_listener *listener, void *data)
4151{
4152 struct weston_surface *surface = data;
4153 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004154 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004155 int x, y;
4156
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004157 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004158 return;
4159
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004160 api = shsurf->shell->xwayland_surface_api;
4161 if (!api) {
4162 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
4163 shsurf->shell->xwayland_surface_api = api;
4164 }
4165
4166 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004167 return;
4168
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004169 if (!weston_view_is_mapped(shsurf->view))
4170 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004171
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004172 x = shsurf->view->geometry.x;
4173 y = shsurf->view->geometry.y;
4174
4175 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004176}
4177
4178static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004179weston_view_set_initial_position(struct weston_view *view,
4180 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004181{
4182 struct weston_compositor *compositor = shell->compositor;
4183 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004184 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004185 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004186 struct weston_output *output, *target_output = NULL;
4187 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004188 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004189
4190 /* As a heuristic place the new window on the same output as the
4191 * pointer. Falling back to the output containing 0, 0.
4192 *
4193 * TODO: Do something clever for touch too?
4194 */
4195 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004196 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4197
4198 if (pointer) {
4199 ix = wl_fixed_to_int(pointer->x);
4200 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004201 break;
4202 }
4203 }
4204
4205 wl_list_for_each(output, &compositor->output_list, link) {
4206 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4207 target_output = output;
4208 break;
4209 }
4210 }
4211
4212 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004213 weston_view_set_position(view, 10 + random() % 400,
4214 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004215 return;
4216 }
4217
4218 /* Valid range within output where the surface will still be onscreen.
4219 * If this is negative it means that the surface is bigger than
4220 * output.
4221 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004222 get_output_work_area(shell, target_output, &area);
4223
Derek Foremanf814c5d2015-04-15 12:23:54 -05004224 x = area.x;
4225 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004226 range_x = area.width - view->surface->width;
4227 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004228
Rob Bradford4cb88c72012-08-13 15:18:44 +01004229 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004230 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004231
4232 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004233 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004234
Jason Ekstranda7af7042013-10-12 22:38:11 -05004235 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004236}
4237
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004238static bool
4239check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4240{
4241 struct timespec now;
4242
4243 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4244 return false;
4245
4246 /*
4247 * If the shell helper client dies before the session has been
4248 * up for roughly 30 seconds, better just make Weston shut down,
4249 * because the user likely has no way to interact with the desktop
4250 * anyway.
4251 */
4252 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4253 weston_log("Error: %s apparently cannot run at all.\n",
4254 shell->client);
4255 weston_log_continue(STAMP_SPACE "Quitting...");
Pekka Paalanen052032d2019-02-06 10:44:37 +02004256 weston_compositor_exit_with_code(shell->compositor,
4257 EXIT_FAILURE);
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004258
4259 return true;
4260 }
4261
4262 return false;
4263}
4264
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004265static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004266
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004267static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004268respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004269{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004270 struct timespec time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004271
4272 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004273 weston_compositor_get_time(&time);
4274 if (timespec_sub_to_msec(&time, &shell->child.deathstamp) > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004275 shell->child.deathstamp = time;
4276 shell->child.deathcount = 0;
4277 }
4278
4279 shell->child.deathcount++;
4280 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004281 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004282 return;
4283 }
4284
Pekka Paalanen826dc142014-08-27 12:11:53 +03004285 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004286 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004287}
4288
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004289static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004290desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4291{
4292 struct desktop_shell *shell;
4293
4294 shell = container_of(listener, struct desktop_shell,
4295 child.client_destroy_listener);
4296
Pekka Paalanen826dc142014-08-27 12:11:53 +03004297 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004298 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004299 /*
4300 * unbind_desktop_shell() will reset shell->child.desktop_shell
4301 * before the respawned process has a chance to create a new
4302 * desktop_shell object, because we are being called from the
4303 * wl_client destructor which destroys all wl_resources before
4304 * returning.
4305 */
4306
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004307 if (!check_desktop_shell_crash_too_early(shell))
4308 respawn_desktop_shell_process(shell);
4309
Pekka Paalanen826dc142014-08-27 12:11:53 +03004310 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004311}
4312
4313static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004314launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004315{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004316 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004317
Pekka Paalanen826dc142014-08-27 12:11:53 +03004318 shell->child.client = weston_client_start(shell->compositor,
4319 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004320
Arnaud Vrac42631192014-08-25 20:56:46 +02004321 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004322 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004323 return;
4324 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004325
4326 shell->child.client_destroy_listener.notify =
4327 desktop_shell_client_destroy;
4328 wl_client_add_destroy_listener(shell->child.client,
4329 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004330}
4331
4332static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004333unbind_desktop_shell(struct wl_resource *resource)
4334{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004335 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004336
4337 if (shell->locked)
4338 resume_desktop(shell);
4339
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004340 shell->child.desktop_shell = NULL;
4341 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004342}
4343
4344static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004345bind_desktop_shell(struct wl_client *client,
4346 void *data, uint32_t version, uint32_t id)
4347{
Tiago Vignattibe143262012-04-16 17:31:41 +03004348 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004349 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004350
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004351 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4352 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004353
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004354 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004355 wl_resource_set_implementation(resource,
4356 &desktop_shell_implementation,
4357 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004358 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004359 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004360 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004361
4362 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4363 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004364}
4365
Kristian Høgsberg07045392012-02-19 18:52:44 -05004366struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004367 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004368 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004369 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004370 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004371 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004372};
4373
4374static void
4375switcher_next(struct switcher *switcher)
4376{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004377 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004378 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004379 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004380 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004381
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004382 /* temporary re-display minimized surfaces */
4383 struct weston_view *tmp;
4384 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004385 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4386 weston_layer_entry_remove(&view->layer_link);
4387 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004388 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4389 *minimized = view;
4390 }
4391
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004392 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004393 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004394 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004395 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004396 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004397 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004398 next = view;
4399 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004400 view->alpha = 0.25;
4401 weston_view_geometry_dirty(view);
4402 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004403 }
Alex Wu1659daa2012-04-01 20:13:09 +08004404
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004405 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004406 view->alpha = 0.25;
4407 weston_view_geometry_dirty(view);
4408 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004409 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004410 }
4411
4412 if (next == NULL)
4413 next = first;
4414
Alex Wu07b26062012-03-12 16:06:01 +08004415 if (next == NULL)
4416 return;
4417
Kristian Høgsberg07045392012-02-19 18:52:44 -05004418 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004419 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004420
4421 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004422 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004423 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004424
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004425 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004426 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004427 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004428}
4429
4430static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004431switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004432{
4433 struct switcher *switcher =
4434 container_of(listener, struct switcher, listener);
4435
4436 switcher_next(switcher);
4437}
4438
4439static void
Daniel Stone351eb612012-05-31 15:27:47 -04004440switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004441{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004442 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004443 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004444 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004445
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004446 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004447 if (is_focus_view(view))
4448 continue;
4449
Jason Ekstranda7af7042013-10-12 22:38:11 -05004450 view->alpha = 1.0;
4451 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004452 }
4453
Marius Vlad5699dba2021-10-15 20:20:17 +03004454 if (switcher->current && get_shell_surface(switcher->current->surface)) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004455 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004456 keyboard->seat,
4457 WESTON_ACTIVATE_FLAG_CONFIGURE);
4458 }
4459
Kristian Høgsberg07045392012-02-19 18:52:44 -05004460 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004461 weston_keyboard_end_grab(keyboard);
4462 if (keyboard->input_method_resource)
4463 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004464
4465 /* re-hide surfaces that were temporary shown during the switch */
4466 struct weston_view **minimized;
4467 wl_array_for_each(minimized, &switcher->minimized_array) {
4468 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004469 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004470 weston_layer_entry_remove(&(*minimized)->layer_link);
4471 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004472 weston_view_damage_below(*minimized);
4473 }
4474 }
4475 wl_array_release(&switcher->minimized_array);
4476
Kristian Høgsberg07045392012-02-19 18:52:44 -05004477 free(switcher);
4478}
4479
4480static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004481switcher_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004482 const struct timespec *time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004483{
4484 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004485 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004486
Daniel Stonec9785ea2012-05-30 16:31:52 +01004487 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004488 switcher_next(switcher);
4489}
4490
4491static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004492switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004493 uint32_t mods_depressed, uint32_t mods_latched,
4494 uint32_t mods_locked, uint32_t group)
4495{
4496 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004497 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004498
Daniel Stone351eb612012-05-31 15:27:47 -04004499 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4500 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004501}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004502
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004503static void
4504switcher_cancel(struct weston_keyboard_grab *grab)
4505{
4506 struct switcher *switcher = container_of(grab, struct switcher, grab);
4507
4508 switcher_destroy(switcher);
4509}
4510
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004511static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004512 switcher_key,
4513 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004514 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004515};
4516
4517static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004518switcher_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004519 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004520{
Tiago Vignattibe143262012-04-16 17:31:41 +03004521 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004522 struct switcher *switcher;
4523
4524 switcher = malloc(sizeof *switcher);
ganjingc1e71512020-07-28 15:28:45 +08004525 if (!switcher)
4526 return;
Pekka Paalanen4bb326b2021-05-14 14:37:46 +03004527
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004528 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004529 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004530 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004531 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004532 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004533
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004534 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004535 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004536 weston_keyboard_start_grab(keyboard, &switcher->grab);
4537 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004538 switcher_next(switcher);
4539}
4540
Pekka Paalanen3c647232011-12-22 13:43:43 +02004541static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004542backlight_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004543 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004544{
4545 struct weston_compositor *compositor = data;
4546 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004547 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004548
4549 /* TODO: we're limiting to simple use cases, where we assume just
4550 * control on the primary display. We'd have to extend later if we
4551 * ever get support for setting backlights on random desktop LCD
4552 * panels though */
4553 output = get_default_output(compositor);
4554 if (!output)
4555 return;
4556
4557 if (!output->set_backlight)
4558 return;
4559
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004560 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4561 backlight_new = output->backlight_current - 25;
4562 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4563 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004564
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004565 if (backlight_new < 5)
4566 backlight_new = 5;
4567 if (backlight_new > 255)
4568 backlight_new = 255;
4569
4570 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004571 output->set_backlight(output, output->backlight_current);
4572}
4573
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004574static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004575force_kill_binding(struct weston_keyboard *keyboard,
4576 const struct timespec *time, uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004577{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004578 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004579 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004580 struct desktop_shell *shell = data;
4581 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004582 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004583
Derek Foreman8ae2db52015-07-15 13:00:36 -05004584 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004585 if (!focus_surface)
4586 return;
4587
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004588 wl_signal_emit(&compositor->kill_signal, focus_surface);
4589
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004590 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004591 wl_client_get_credentials(client, &pid, NULL, NULL);
4592
4593 /* Skip clients that we launched ourselves (the credentials of
4594 * the socketpair is ours) */
4595 if (pid == getpid())
4596 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004597
Daniel Stone325fc2d2012-05-30 16:31:58 +01004598 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004599}
4600
4601static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004602workspace_up_binding(struct weston_keyboard *keyboard,
4603 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004604{
4605 struct desktop_shell *shell = data;
4606 unsigned int new_index = shell->workspaces.current;
4607
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004608 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004609 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004610 if (new_index != 0)
4611 new_index--;
4612
4613 change_workspace(shell, new_index);
4614}
4615
4616static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004617workspace_down_binding(struct weston_keyboard *keyboard,
4618 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004619{
4620 struct desktop_shell *shell = data;
4621 unsigned int new_index = shell->workspaces.current;
4622
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004623 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004624 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004625 if (new_index < shell->workspaces.num - 1)
4626 new_index++;
4627
4628 change_workspace(shell, new_index);
4629}
4630
4631static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004632workspace_f_binding(struct weston_keyboard *keyboard,
4633 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004634{
4635 struct desktop_shell *shell = data;
4636 unsigned int new_index;
4637
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004638 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004639 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004640 new_index = key - KEY_F1;
4641 if (new_index >= shell->workspaces.num)
4642 new_index = shell->workspaces.num - 1;
4643
4644 change_workspace(shell, new_index);
4645}
4646
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004647static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004648workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004649 const struct timespec *time, uint32_t key,
4650 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004651{
4652 struct desktop_shell *shell = data;
4653 unsigned int new_index = shell->workspaces.current;
4654
4655 if (shell->locked)
4656 return;
4657
4658 if (new_index != 0)
4659 new_index--;
4660
Derek Foreman8ae2db52015-07-15 13:00:36 -05004661 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004662}
4663
4664static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004665workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004666 const struct timespec *time, uint32_t key,
4667 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004668{
4669 struct desktop_shell *shell = data;
4670 unsigned int new_index = shell->workspaces.current;
4671
4672 if (shell->locked)
4673 return;
4674
4675 if (new_index < shell->workspaces.num - 1)
4676 new_index++;
4677
Derek Foreman8ae2db52015-07-15 13:00:36 -05004678 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004679}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004680
4681static void
Harish Krupodc3edc02019-04-20 17:50:18 +05304682shell_reposition_view_on_output_change(struct weston_view *view)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004683{
4684 struct weston_output *output, *first_output;
4685 struct weston_compositor *ec = view->surface->compositor;
4686 struct shell_surface *shsurf;
4687 float x, y;
4688 int visible;
4689
Harish Krupo2dff4dd2019-04-20 18:10:56 +05304690 if (wl_list_empty(&ec->output_list))
4691 return;
4692
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004693 x = view->geometry.x;
4694 y = view->geometry.y;
4695
4696 /* At this point the destroyed output is not in the list anymore.
4697 * If the view is still visible somewhere, we leave where it is,
4698 * otherwise, move it to the first output. */
4699 visible = 0;
4700 wl_list_for_each(output, &ec->output_list, link) {
4701 if (pixman_region32_contains_point(&output->region,
4702 x, y, NULL)) {
4703 visible = 1;
4704 break;
4705 }
4706 }
4707
4708 if (!visible) {
4709 first_output = container_of(ec->output_list.next,
4710 struct weston_output, link);
4711
4712 x = first_output->x + first_output->width / 4;
4713 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004714
4715 weston_view_set_position(view, x, y);
4716 } else {
4717 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004718 }
4719
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004720
4721 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004722 if (!shsurf)
4723 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004724
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004725 shsurf->saved_position_valid = false;
4726 set_maximized(shsurf, false);
4727 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004728}
4729
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004730void
4731shell_for_each_layer(struct desktop_shell *shell,
4732 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004733{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004734 struct workspace **ws;
4735
4736 func(shell, &shell->fullscreen_layer, data);
4737 func(shell, &shell->panel_layer, data);
4738 func(shell, &shell->background_layer, data);
4739 func(shell, &shell->lock_layer, data);
4740 func(shell, &shell->input_panel_layer, data);
4741
4742 wl_array_for_each(ws, &shell->workspaces.array)
4743 func(shell, &(*ws)->layer, data);
4744}
4745
4746static void
Harish Krupodc3edc02019-04-20 17:50:18 +05304747shell_output_changed_move_layer(struct desktop_shell *shell,
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004748 struct weston_layer *layer,
4749 void *data)
4750{
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004751 struct weston_view *view;
4752
Marius Vladea40d6d2018-02-08 18:46:42 +02004753 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Harish Krupodc3edc02019-04-20 17:50:18 +05304754 shell_reposition_view_on_output_change(view);
4755
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004756}
4757
4758static void
Pekka Paalanen719ca872021-05-24 16:53:40 +03004759shell_output_destroy(struct shell_output *shell_output)
Xiong Zhang6b481422013-10-23 13:58:32 +08004760{
Pekka Paalanen0d5e4ff2021-05-24 16:13:21 +03004761 struct desktop_shell *shell = shell_output->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004762
Harish Krupodc3edc02019-04-20 17:50:18 +05304763 shell_for_each_layer(shell, shell_output_changed_move_layer, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004764
Pekka Paalanen719ca872021-05-24 16:53:40 +03004765 if (shell_output->fade.animation) {
4766 weston_view_animation_destroy(shell_output->fade.animation);
4767 shell_output->fade.animation = NULL;
4768 }
4769
4770 if (shell_output->fade.view) {
4771 /* destroys the view as well */
4772 weston_surface_destroy(shell_output->fade.view->surface);
4773 }
4774
4775 if (shell_output->fade.startup_timer)
4776 wl_event_source_remove(shell_output->fade.startup_timer);
4777
Pekka Paalanen0d5e4ff2021-05-24 16:13:21 +03004778 if (shell_output->panel_surface)
4779 wl_list_remove(&shell_output->panel_surface_listener.link);
4780 if (shell_output->background_surface)
4781 wl_list_remove(&shell_output->background_surface_listener.link);
4782 wl_list_remove(&shell_output->destroy_listener.link);
4783 wl_list_remove(&shell_output->link);
4784 free(shell_output);
Xiong Zhang6b481422013-10-23 13:58:32 +08004785}
4786
4787static void
Pekka Paalanen719ca872021-05-24 16:53:40 +03004788handle_output_destroy(struct wl_listener *listener, void *data)
4789{
4790 struct shell_output *shell_output =
4791 container_of(listener, struct shell_output, destroy_listener);
4792
4793 shell_output_destroy(shell_output);
4794}
4795
4796static void
David Fort50d962f2016-06-09 17:55:52 +02004797shell_resize_surface_to_output(struct desktop_shell *shell,
4798 struct weston_surface *surface,
4799 const struct weston_output *output)
4800{
4801 if (!surface)
4802 return;
4803
4804 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4805 surface->resource,
4806 output->width,
4807 output->height);
4808}
4809
4810
4811static void
4812handle_output_resized(struct wl_listener *listener, void *data)
4813{
4814 struct desktop_shell *shell =
4815 container_of(listener, struct desktop_shell, resized_listener);
4816 struct weston_output *output = (struct weston_output *)data;
4817 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4818
4819 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4820 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4821}
4822
4823static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004824create_shell_output(struct desktop_shell *shell,
4825 struct weston_output *output)
4826{
4827 struct shell_output *shell_output;
4828
4829 shell_output = zalloc(sizeof *shell_output);
4830 if (shell_output == NULL)
4831 return;
4832
4833 shell_output->output = output;
4834 shell_output->shell = shell;
4835 shell_output->destroy_listener.notify = handle_output_destroy;
4836 wl_signal_add(&output->destroy_signal,
4837 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004838 wl_list_insert(shell->output_list.prev, &shell_output->link);
Harish Krupodc3edc02019-04-20 17:50:18 +05304839
4840 if (wl_list_length(&shell->output_list) == 1)
4841 shell_for_each_layer(shell,
4842 shell_output_changed_move_layer, NULL);
Xiong Zhang6b481422013-10-23 13:58:32 +08004843}
4844
4845static void
4846handle_output_create(struct wl_listener *listener, void *data)
4847{
4848 struct desktop_shell *shell =
4849 container_of(listener, struct desktop_shell, output_create_listener);
4850 struct weston_output *output = (struct weston_output *)data;
4851
4852 create_shell_output(shell, output);
4853}
4854
4855static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004856handle_output_move_layer(struct desktop_shell *shell,
4857 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004858{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004859 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004860 struct weston_view *view;
4861 float x, y;
4862
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004863 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004864 if (view->output != output)
4865 continue;
4866
4867 x = view->geometry.x + output->move_x;
4868 y = view->geometry.y + output->move_y;
4869 weston_view_set_position(view, x, y);
4870 }
4871}
4872
4873static void
4874handle_output_move(struct wl_listener *listener, void *data)
4875{
4876 struct desktop_shell *shell;
4877
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004878 shell = container_of(listener, struct desktop_shell,
4879 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004880
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004881 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004882}
4883
4884static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004885setup_output_destroy_handler(struct weston_compositor *ec,
4886 struct desktop_shell *shell)
4887{
4888 struct weston_output *output;
4889
4890 wl_list_init(&shell->output_list);
4891 wl_list_for_each(output, &ec->output_list, link)
4892 create_shell_output(shell, output);
4893
4894 shell->output_create_listener.notify = handle_output_create;
4895 wl_signal_add(&ec->output_created_signal,
4896 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004897
4898 shell->output_move_listener.notify = handle_output_move;
4899 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08004900}
4901
4902static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004903shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004904{
Tiago Vignattibe143262012-04-16 17:31:41 +03004905 struct desktop_shell *shell =
4906 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004907 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08004908 struct shell_output *shell_output, *tmp;
Marius Vladc114fd62021-08-26 16:37:24 +03004909 struct shell_seat *shseat, *shseat_next;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004910
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08004911 /* Force state to unlocked so we don't try to fade */
4912 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004913
4914 if (shell->child.client) {
4915 /* disable respawn */
4916 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004917 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03004918 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004919
Harsha M Mb8b2c722018-08-07 19:05:02 +05304920 wl_list_remove(&shell->destroy_listener.link);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004921 wl_list_remove(&shell->idle_listener.link);
4922 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004923 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004924
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004925 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004926 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004927
Pekka Paalanen719ca872021-05-24 16:53:40 +03004928 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link)
4929 shell_output_destroy(shell_output);
Xiong Zhang6b481422013-10-23 13:58:32 +08004930
4931 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07004932 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02004933 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004934
Marius Vladc114fd62021-08-26 16:37:24 +03004935 wl_list_for_each_safe(shseat, shseat_next, &shell->seat_list, link)
4936 desktop_shell_destroy_seat(shseat);
4937
Pekka Paalanenabd72922021-05-24 14:42:00 +03004938 weston_desktop_destroy(shell->desktop);
4939
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004940 wl_array_for_each(ws, &shell->workspaces.array)
4941 workspace_destroy(*ws);
4942 wl_array_release(&shell->workspaces.array);
4943
Pekka Paalanen4bb326b2021-05-14 14:37:46 +03004944 weston_layer_fini(&shell->fullscreen_layer);
4945 weston_layer_fini(&shell->panel_layer);
4946 weston_layer_fini(&shell->background_layer);
4947 weston_layer_fini(&shell->lock_layer);
4948 weston_layer_fini(&shell->input_panel_layer);
4949 weston_layer_fini(&shell->minimized_layer);
4950
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004951 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004952 free(shell);
4953}
4954
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004955static void
4956shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4957{
4958 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004959 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004960
Bob Ham744e6532016-01-12 10:21:48 +00004961 if (shell->allow_zap)
4962 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4963 MODIFIER_CTRL | MODIFIER_ALT,
4964 terminate_binding, ec);
4965
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004966 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004967 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4968 click_to_activate_binding,
4969 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07004970 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
4971 click_to_activate_binding,
4972 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01004973 weston_compositor_add_touch_binding(ec, 0,
4974 touch_to_activate_binding,
4975 shell);
Bob Ham553d1242016-01-12 10:21:49 +00004976 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4977 backlight_binding, ec);
4978 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4979 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004980
4981 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00004982 if (shell->exposay_modifier)
4983 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
4984 exposay_binding, shell);
4985
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004986 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00004987 if (!mod)
4988 return;
4989
Ian Ray13404c42017-09-18 15:22:01 +03004990 /* This binding is not configurable, but is only enabled if there is a
4991 * valid binding modifier. */
4992 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4993 MODIFIER_SUPER | MODIFIER_ALT,
4994 surface_opacity_binding, NULL);
4995
Ian Rayab7c0b62017-09-18 15:22:00 +03004996 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4997 mod, zoom_axis_binding,
4998 NULL);
4999
Daniel Stone325fc2d2012-05-30 16:31:58 +01005000 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5001 zoom_key_binding, NULL);
5002 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5003 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005004 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5005 maximize_binding, NULL);
5006 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5007 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005008 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5009 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005010 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05005011 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005012 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08005013 weston_compositor_add_button_binding(ec, BTN_LEFT,
5014 mod | MODIFIER_SHIFT,
5015 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005016
5017 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05005018 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005019 rotate_binding, NULL);
5020
Daniel Stone325fc2d2012-05-30 16:31:58 +01005021 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5022 shell);
5023 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5024 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005025 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5026 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005027 weston_compositor_add_key_binding(ec, KEY_K, mod,
5028 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005029 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5030 workspace_up_binding, shell);
5031 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5032 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005033 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5034 workspace_move_surface_up_binding,
5035 shell);
5036 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5037 workspace_move_surface_down_binding,
5038 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005039
5040 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5041 if (shell->workspaces.num > 1) {
5042 num_workspace_bindings = shell->workspaces.num;
5043 if (num_workspace_bindings > 6)
5044 num_workspace_bindings = 6;
5045 for (i = 0; i < num_workspace_bindings; i++)
5046 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5047 workspace_f_binding,
5048 shell);
5049 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005050
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02005051 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005052}
5053
Jason Ekstrand024177c2014-04-21 19:42:58 -05005054static void
5055handle_seat_created(struct wl_listener *listener, void *data)
5056{
5057 struct weston_seat *seat = data;
Marius Vladc114fd62021-08-26 16:37:24 +03005058 struct desktop_shell *shell =
5059 container_of(listener, struct desktop_shell, seat_create_listener);
Jason Ekstrand024177c2014-04-21 19:42:58 -05005060
Marius Vladc114fd62021-08-26 16:37:24 +03005061 create_shell_seat(shell, seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05005062}
5063
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005064WL_EXPORT int
Quentin Glidicda01c1d2016-12-02 14:17:08 +01005065wet_shell_init(struct weston_compositor *ec,
5066 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005067{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005068 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005069 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005070 struct workspace **pws;
5071 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005072 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005073
Peter Huttererf3d62272013-08-08 11:57:05 +10005074 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005075 if (shell == NULL)
5076 return -1;
5077
Kristian Høgsberg75840622011-09-06 13:48:16 -04005078 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005079
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02005080 if (!weston_compositor_add_destroy_listener_once(ec,
5081 &shell->destroy_listener,
5082 shell_destroy)) {
5083 free(shell);
5084 return 0;
5085 }
5086
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005087 shell->idle_listener.notify = idle_handler;
5088 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5089 shell->wake_listener.notify = wake_handler;
5090 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005091 shell->transform_listener.notify = transform_handler;
5092 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005093
Quentin Glidic82681572016-12-17 13:40:51 +01005094 weston_layer_init(&shell->fullscreen_layer, ec);
5095 weston_layer_init(&shell->panel_layer, ec);
5096 weston_layer_init(&shell->background_layer, ec);
5097 weston_layer_init(&shell->lock_layer, ec);
5098 weston_layer_init(&shell->input_panel_layer, ec);
5099
5100 weston_layer_set_position(&shell->fullscreen_layer,
5101 WESTON_LAYER_POSITION_FULLSCREEN);
5102 weston_layer_set_position(&shell->panel_layer,
5103 WESTON_LAYER_POSITION_UI);
5104 weston_layer_set_position(&shell->background_layer,
5105 WESTON_LAYER_POSITION_BACKGROUND);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005106
5107 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005108 wl_list_init(&shell->workspaces.client_list);
Marius Vladc114fd62021-08-26 16:37:24 +03005109 wl_list_init(&shell->seat_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005110
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005111 if (input_panel_setup(shell) < 0)
5112 return -1;
5113
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03005114 shell->text_backend = text_backend_init(ec);
5115 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00005116 return -1;
5117
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005118 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005119
Daniel Stonedf8133b2013-11-19 11:37:14 +01005120 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5121 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5122
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005123 for (i = 0; i < shell->workspaces.num; i++) {
5124 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5125 if (pws == NULL)
5126 return -1;
5127
Quentin Glidic82681572016-12-17 13:40:51 +01005128 *pws = workspace_create(shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005129 if (*pws == NULL)
5130 return -1;
5131 }
5132 activate_workspace(shell, 0);
5133
Quentin Glidic82681572016-12-17 13:40:51 +01005134 weston_layer_init(&shell->minimized_layer, ec);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01005135
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005136 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005137 wl_list_init(&shell->workspaces.animation.link);
5138 shell->workspaces.animation.frame = animate_workspace_change_frame;
5139
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02005140 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
5141 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005142 return -1;
5143
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005144 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005145 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005146 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005147 return -1;
5148
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02005149 weston_compositor_get_time(&shell->child.deathstamp);
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005150
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005151 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02005152
Xiong Zhang6b481422013-10-23 13:58:32 +08005153 setup_output_destroy_handler(ec, shell);
5154
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005155 loop = wl_display_get_event_loop(ec->wl_display);
5156 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005157
Jason Ekstrand024177c2014-04-21 19:42:58 -05005158 wl_list_for_each(seat, &ec->seat_list, link)
Marius Vladc114fd62021-08-26 16:37:24 +03005159 create_shell_seat(shell, seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05005160 shell->seat_create_listener.notify = handle_seat_created;
5161 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005162
David Fort50d962f2016-06-09 17:55:52 +02005163 shell->resized_listener.notify = handle_output_resized;
5164 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
5165
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01005166 screenshooter_create(ec);
5167
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005168 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005169
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005170 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005171
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005172 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
5173
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005174 return 0;
5175}