blob: 90caf0fd1ce49b805019972fa02a539e2c19da5f [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
Pekka Paalanen8274d902014-08-06 19:36:51 +0300223static int
224shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
225{
Pekka Paalanen8274d902014-08-06 19:36:51 +0300226 const char *t, *c;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200227 struct weston_desktop_surface *desktop_surface =
228 weston_surface_get_desktop_surface(surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300229
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200230 t = weston_desktop_surface_get_title(desktop_surface);
231 c = weston_desktop_surface_get_app_id(desktop_surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300232
233 return snprintf(buf, len, "%s window%s%s%s%s%s",
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200234 "top-level",
Pekka Paalanen8274d902014-08-06 19:36:51 +0300235 t ? " '" : "", t ?: "", t ? "'" : "",
236 c ? " of " : "", c ?: "");
237}
238
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500239static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400240destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300241{
242 struct shell_grab *grab;
243
244 grab = container_of(listener, struct shell_grab,
245 shsurf_destroy_listener);
246
247 grab->shsurf = NULL;
248}
249
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800250struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500251get_default_view(struct weston_surface *surface)
252{
253 struct shell_surface *shsurf;
254 struct weston_view *view;
255
256 if (!surface || wl_list_empty(&surface->views))
257 return NULL;
258
259 shsurf = get_shell_surface(surface);
260 if (shsurf)
261 return shsurf->view;
262
263 wl_list_for_each(view, &surface->views, surface_link)
264 if (weston_view_is_mapped(view))
265 return view;
266
267 return container_of(surface->views.next, struct weston_view, surface_link);
268}
269
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300270static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300271shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400272 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300273 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400274 struct weston_pointer *pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800275 enum weston_desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300276{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300277 struct desktop_shell *shell = shsurf->shell;
278
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200279 weston_seat_break_desktop_grabs(pointer->seat);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400280
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300281 grab->grab.interface = interface;
282 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400283 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500284 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400285 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300286
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700287 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400288 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400289 if (shell->child.desktop_shell) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800290 weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400291 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500292 weston_pointer_set_focus(pointer,
293 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400294 wl_fixed_from_int(0),
295 wl_fixed_from_int(0));
296 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300297}
298
Jonny Lambd73c6942014-08-20 15:53:20 +0200299static void
Quentin Glidic581df062016-06-23 18:55:19 +0200300get_panel_size(struct desktop_shell *shell,
301 struct weston_view *view,
302 int *width,
303 int *height)
304{
305 float x1, y1;
306 float x2, y2;
307 weston_view_to_global_float(view, 0, 0, &x1, &y1);
308 weston_view_to_global_float(view,
309 view->surface->width,
310 view->surface->height,
311 &x2, &y2);
312 *width = (int)(x2 - x1);
313 *height = (int)(y2 - y1);
314}
315
316static void
Jonny Lambd73c6942014-08-20 15:53:20 +0200317get_output_panel_size(struct desktop_shell *shell,
318 struct weston_output *output,
319 int *width,
320 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700321{
322 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200323
324 *width = 0;
325 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700326
327 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200328 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700329
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300330 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Quentin Glidic581df062016-06-23 18:55:19 +0200331 if (view->surface->output == output) {
332 get_panel_size(shell, view, width, height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200333 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700334 }
335 }
336
Jonny Lambd73c6942014-08-20 15:53:20 +0200337 /* the correct view wasn't found */
338}
339
Leandro Ribeiro0c59e922020-01-24 17:53:44 -0300340void
Quentin Glidicfff39812016-08-15 10:56:52 +0200341get_output_work_area(struct desktop_shell *shell,
Jonny Lambd73c6942014-08-20 15:53:20 +0200342 struct weston_output *output,
343 pixman_rectangle32_t *area)
344{
345 int32_t panel_width = 0, panel_height = 0;
346
Pekka Paalanen99372ba2018-05-02 10:21:55 +0200347 if (!output) {
348 area->x = 0;
349 area->y = 0;
350 area->width = 0;
351 area->height = 0;
352
353 return;
354 }
355
Derek Foremanf814c5d2015-04-15 12:23:54 -0500356 area->x = output->x;
357 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200358
359 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200360 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800361 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
Jonny Lambd73c6942014-08-20 15:53:20 +0200362 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500363 area->y += panel_height;
Daniel Stone2ef9b1a2017-03-13 16:31:36 +0000364 /* fallthrough */
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800365 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200366 area->width = output->width;
367 area->height = output->height - panel_height;
368 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800369 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500370 area->x += panel_width;
Daniel Stone2ef9b1a2017-03-13 16:31:36 +0000371 /* fallthrough */
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800372 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200373 area->width = output->width - panel_width;
374 area->height = output->height;
375 break;
376 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700377}
378
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400379static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300380shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300381{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700382 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400383 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700384 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400385
386 if (grab->shsurf->resize_edges) {
387 grab->shsurf->resize_edges = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400388 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700389 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300390
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700391 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300392}
393
394static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700395shell_touch_grab_start(struct shell_touch_grab *grab,
396 const struct weston_touch_grab_interface *interface,
397 struct shell_surface *shsurf,
398 struct weston_touch *touch)
399{
400 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800401
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200402 weston_seat_break_desktop_grabs(touch->seat);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700403
Rusty Lynch1084da52013-08-15 09:10:08 -0700404 grab->grab.interface = interface;
405 grab->shsurf = shsurf;
406 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
407 wl_signal_add(&shsurf->destroy_signal,
408 &grab->shsurf_destroy_listener);
409
410 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700411 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700412
413 weston_touch_start_grab(touch, &grab->grab);
414 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500415 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500416 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700417}
418
419static void
420shell_touch_grab_end(struct shell_touch_grab *grab)
421{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700422 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700423 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700424 grab->shsurf->grabbed = 0;
425 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700426
427 weston_touch_end_grab(grab->touch);
428}
429
Daniel Stone496ca172012-05-30 16:31:42 +0100430static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300431get_modifier(char *modifier)
432{
433 if (!modifier)
434 return MODIFIER_SUPER;
435
436 if (!strcmp("ctrl", modifier))
437 return MODIFIER_CTRL;
438 else if (!strcmp("alt", modifier))
439 return MODIFIER_ALT;
440 else if (!strcmp("super", modifier))
441 return MODIFIER_SUPER;
Bob Ham553d1242016-01-12 10:21:49 +0000442 else if (!strcmp("none", modifier))
443 return 0;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300444 else
445 return MODIFIER_SUPER;
446}
447
Juan Zhaoe10d2792012-04-25 19:09:52 +0800448static enum animation_type
449get_animation_type(char *animation)
450{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800451 if (!animation)
452 return ANIMATION_NONE;
453
Juan Zhaoe10d2792012-04-25 19:09:52 +0800454 if (!strcmp("zoom", animation))
455 return ANIMATION_ZOOM;
456 else if (!strcmp("fade", animation))
457 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100458 else if (!strcmp("dim-layer", animation))
459 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800460 else
461 return ANIMATION_NONE;
462}
463
Alex Wu4539b082012-03-01 12:57:46 +0800464static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400465shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200466{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400467 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500468 char *s, *client;
Daniel Stone51d995a2019-11-26 00:14:24 +0000469 bool allow_zap;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200470
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300471 section = weston_config_get_section(wet_get_config(shell->compositor),
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400472 "shell", NULL, NULL);
Marius Vlad64fbd0f2018-12-15 15:51:57 +0200473 client = wet_get_libexec_path(WESTON_SHELL_CLIENT);
Daniel Stonee03c1112016-11-24 20:45:45 +0000474 weston_config_section_get_string(section, "client", &s, client);
Derek Foremanc7210432014-08-21 11:32:38 -0500475 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100476 shell->client = s;
Bob Ham744e6532016-01-12 10:21:48 +0000477
478 weston_config_section_get_bool(section,
479 "allow-zap", &allow_zap, true);
480 shell->allow_zap = allow_zap;
481
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100482 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400483 "binding-modifier", &s, "super");
484 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200485 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800486
487 weston_config_section_get_string(section,
488 "exposay-modifier", &s, "none");
Bob Ham553d1242016-01-12 10:21:49 +0000489 shell->exposay_modifier = get_modifier(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800490 free(s);
491
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400492 weston_config_section_get_string(section, "animation", &s, "none");
493 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200494 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200495 weston_config_section_get_string(section, "close-animation", &s, "fade");
496 shell->win_close_animation_type = get_animation_type(s);
497 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700498 weston_config_section_get_string(section,
499 "startup-animation", &s, "fade");
500 shell->startup_animation_type = get_animation_type(s);
501 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700502 if (shell->startup_animation_type == ANIMATION_ZOOM)
503 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100504 weston_config_section_get_string(section, "focus-animation", &s, "none");
505 shell->focus_animation_type = get_animation_type(s);
506 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400507 weston_config_section_get_uint(section, "num-workspaces",
508 &shell->workspaces.num,
509 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200510}
511
Pekka Paalanen8274d902014-08-06 19:36:51 +0300512static int
513focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
514{
515 return snprintf(buf, len, "focus highlight effect for output %s",
Miguel A. Vico620f68d2019-09-25 11:23:13 -0700516 (surface->output ? surface->output->name : "NULL"));
Pekka Paalanen8274d902014-08-06 19:36:51 +0300517}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100518
519/* no-op func for checking focus surface */
520static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200521focus_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100522{
523}
524
525static struct focus_surface *
526get_focus_surface(struct weston_surface *surface)
527{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200528 if (surface->committed == focus_surface_committed)
529 return surface->committed_private;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100530 else
531 return NULL;
532}
533
534static bool
535is_focus_surface (struct weston_surface *es)
536{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200537 return (es->committed == focus_surface_committed);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100538}
539
540static bool
541is_focus_view (struct weston_view *view)
542{
543 return is_focus_surface (view->surface);
544}
545
546static struct focus_surface *
547create_focus_surface(struct weston_compositor *ec,
548 struct weston_output *output)
549{
550 struct focus_surface *fsurf = NULL;
551 struct weston_surface *surface = NULL;
552
553 fsurf = malloc(sizeof *fsurf);
554 if (!fsurf)
555 return NULL;
556
557 fsurf->surface = weston_surface_create(ec);
558 surface = fsurf->surface;
559 if (surface == NULL) {
560 free(fsurf);
561 return NULL;
562 }
563
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200564 surface->committed = focus_surface_committed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100565 surface->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200566 surface->is_mapped = true;
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200567 surface->committed_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300568 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100569
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800570 fsurf->view = weston_view_create(surface);
571 if (fsurf->view == NULL) {
572 weston_surface_destroy(surface);
573 free(fsurf);
574 return NULL;
575 }
Semi Malinene7a52fb2018-04-26 11:08:10 +0200576 weston_view_set_output(fsurf->view, output);
Armin Krezović4663aca2016-06-30 06:04:29 +0200577 fsurf->view->is_mapped = true;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100578
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600579 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600580 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100581 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
582 pixman_region32_fini(&surface->opaque);
583 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
584 output->width, output->height);
585 pixman_region32_fini(&surface->input);
586 pixman_region32_init(&surface->input);
587
588 wl_list_init(&fsurf->workspace_transform.link);
589
590 return fsurf;
591}
592
593static void
594focus_surface_destroy(struct focus_surface *fsurf)
595{
596 weston_surface_destroy(fsurf->surface);
597 free(fsurf);
598}
599
600static void
601focus_animation_done(struct weston_view_animation *animation, void *data)
602{
603 struct workspace *ws = data;
604
605 ws->focus_animation = NULL;
606}
607
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200608static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200609focus_state_destroy(struct focus_state *state)
610{
611 wl_list_remove(&state->seat_destroy_listener.link);
612 wl_list_remove(&state->surface_destroy_listener.link);
613 free(state);
614}
615
616static void
617focus_state_seat_destroy(struct wl_listener *listener, void *data)
618{
619 struct focus_state *state = container_of(listener,
620 struct focus_state,
621 seat_destroy_listener);
622
623 wl_list_remove(&state->link);
624 focus_state_destroy(state);
625}
626
627static void
628focus_state_surface_destroy(struct wl_listener *listener, void *data)
629{
630 struct focus_state *state = container_of(listener,
631 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400632 surface_destroy_listener);
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200633 struct weston_surface *main_surface;
634 struct weston_view *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500635 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200636
Pekka Paalanen01388e22013-04-25 13:57:44 +0300637 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
638
Kristian Høgsberge3778222012-07-31 17:29:30 -0400639 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300640 wl_list_for_each(view,
641 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500642 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400643 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100644 if (is_focus_view(view))
645 continue;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200646 if (!get_shell_surface(view->surface))
647 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400648
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200649 next = view;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400650 break;
651 }
652
Pekka Paalanen01388e22013-04-25 13:57:44 +0300653 /* if the focus was a sub-surface, activate its main surface */
654 if (main_surface != state->keyboard_focus)
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200655 next = get_default_view(main_surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300656
Kristian Høgsberge3778222012-07-31 17:29:30 -0400657 if (next) {
Greg V15d3d302018-12-07 01:33:34 +0300658 if (state->keyboard_focus) {
659 wl_list_remove(&state->surface_destroy_listener.link);
660 wl_list_init(&state->surface_destroy_listener.link);
661 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100662 state->keyboard_focus = NULL;
Quentin Glidicfff39812016-08-15 10:56:52 +0200663 activate(state->shell, next, state->seat,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +0200664 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400665 } else {
Quentin Glidicfff39812016-08-15 10:56:52 +0200666 if (state->shell->focus_animation_type == ANIMATION_DIM_LAYER) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100667 if (state->ws->focus_animation)
668 weston_view_animation_destroy(state->ws->focus_animation);
669
670 state->ws->focus_animation = weston_fade_run(
671 state->ws->fsurf_front->view,
672 state->ws->fsurf_front->view->alpha, 0.0, 300,
673 focus_animation_done, state->ws);
674 }
675
Kristian Høgsberge3778222012-07-31 17:29:30 -0400676 wl_list_remove(&state->link);
677 focus_state_destroy(state);
678 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200679}
680
681static struct focus_state *
Quentin Glidicfff39812016-08-15 10:56:52 +0200682focus_state_create(struct desktop_shell *shell, struct weston_seat *seat,
683 struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200684{
Jonas Ådahl04769742012-06-13 00:01:24 +0200685 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200686
687 state = malloc(sizeof *state);
688 if (state == NULL)
689 return NULL;
690
Quentin Glidicfff39812016-08-15 10:56:52 +0200691 state->shell = shell;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100692 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400693 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200694 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400695 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200696
697 state->seat_destroy_listener.notify = focus_state_seat_destroy;
698 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400699 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200700 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400701 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200702
703 return state;
704}
705
Jonas Ådahl8538b222012-08-29 22:13:03 +0200706static struct focus_state *
707ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
708{
709 struct workspace *ws = get_current_workspace(shell);
710 struct focus_state *state;
711
712 wl_list_for_each(state, &ws->focus_list, link)
713 if (state->seat == seat)
714 break;
715
716 if (&state->link == &ws->focus_list)
Quentin Glidicfff39812016-08-15 10:56:52 +0200717 state = focus_state_create(shell, seat, ws);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200718
719 return state;
720}
721
Jonas Ådahl04769742012-06-13 00:01:24 +0200722static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800723focus_state_set_focus(struct focus_state *state,
724 struct weston_surface *surface)
725{
726 if (state->keyboard_focus) {
727 wl_list_remove(&state->surface_destroy_listener.link);
728 wl_list_init(&state->surface_destroy_listener.link);
729 }
730
731 state->keyboard_focus = surface;
732 if (surface)
733 wl_signal_add(&surface->destroy_signal,
734 &state->surface_destroy_listener);
735}
736
737static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400738restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200739{
740 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400741 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100742 struct wl_list pending_seat_list;
743 struct weston_seat *seat, *next_seat;
744
745 /* Temporarily steal the list of seats so that we can keep
746 * track of the seats we've already processed */
747 wl_list_init(&pending_seat_list);
748 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
749 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200750
751 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500752 struct weston_keyboard *keyboard =
753 weston_seat_get_keyboard(state->seat);
754
Neil Roberts4237f502014-04-09 16:33:31 +0100755 wl_list_remove(&state->seat->link);
756 wl_list_insert(&shell->compositor->seat_list,
757 &state->seat->link);
758
Derek Foreman1281a362015-07-31 16:55:32 -0500759 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800760 continue;
761
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400762 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200763
Derek Foreman1281a362015-07-31 16:55:32 -0500764 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200765 }
Neil Roberts4237f502014-04-09 16:33:31 +0100766
767 /* For any remaining seats that we don't have a focus state
768 * for we'll reset the keyboard focus to NULL */
769 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500770 struct weston_keyboard *keyboard =
771 weston_seat_get_keyboard(seat);
772
Neil Roberts4237f502014-04-09 16:33:31 +0100773 wl_list_insert(&shell->compositor->seat_list, &seat->link);
774
Derek Foreman1281a362015-07-31 16:55:32 -0500775 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100776 continue;
777
Derek Foreman1281a362015-07-31 16:55:32 -0500778 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100779 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200780}
781
782static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200783replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
784 struct weston_seat *seat)
785{
Derek Foreman1281a362015-07-31 16:55:32 -0500786 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200787 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200788
789 wl_list_for_each(state, &ws->focus_list, link) {
790 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500791 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200792 return;
793 }
794 }
795}
796
797static void
798drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
799 struct weston_surface *surface)
800{
801 struct focus_state *state;
802
803 wl_list_for_each(state, &ws->focus_list, link)
804 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800805 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200806}
807
808static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100809animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
810 struct weston_view *from, struct weston_view *to)
811{
812 struct weston_output *output;
813 bool focus_surface_created = false;
814
815 /* FIXME: Only support dim animation using two layers */
816 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
817 return;
818
819 output = get_default_output(shell->compositor);
820 if (ws->fsurf_front == NULL && (from || to)) {
821 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800822 if (ws->fsurf_front == NULL)
823 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100824 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800825
826 ws->fsurf_back = create_focus_surface(shell->compositor, output);
827 if (ws->fsurf_back == NULL) {
828 focus_surface_destroy(ws->fsurf_front);
829 return;
830 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100831 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800832
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100833 focus_surface_created = true;
834 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300835 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
836 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100837 }
838
839 if (ws->focus_animation) {
840 weston_view_animation_destroy(ws->focus_animation);
841 ws->focus_animation = NULL;
842 }
843
844 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300845 weston_layer_entry_insert(&to->layer_link,
846 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100847 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300848 weston_layer_entry_insert(&ws->layer.view_list,
849 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100850
851 if (focus_surface_created) {
852 ws->focus_animation = weston_fade_run(
853 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200854 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100855 focus_animation_done, ws);
856 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300857 weston_layer_entry_insert(&from->layer_link,
858 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100859 ws->focus_animation = weston_stable_fade_run(
860 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200861 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100862 focus_animation_done, ws);
863 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300864 weston_layer_entry_insert(&ws->layer.view_list,
865 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100866 ws->focus_animation = weston_stable_fade_run(
867 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200868 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100869 focus_animation_done, ws);
870 }
871}
872
873static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200874workspace_destroy(struct workspace *ws)
875{
Jonas Ådahl04769742012-06-13 00:01:24 +0200876 struct focus_state *state, *next;
877
878 wl_list_for_each_safe(state, next, &ws->focus_list, link)
879 focus_state_destroy(state);
880
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100881 if (ws->fsurf_front)
882 focus_surface_destroy(ws->fsurf_front);
883 if (ws->fsurf_back)
884 focus_surface_destroy(ws->fsurf_back);
885
Pekka Paalanen4bb326b2021-05-14 14:37:46 +0300886 weston_layer_fini(&ws->layer);
887
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200888 free(ws);
889}
890
Jonas Ådahl04769742012-06-13 00:01:24 +0200891static void
892seat_destroyed(struct wl_listener *listener, void *data)
893{
894 struct weston_seat *seat = data;
895 struct focus_state *state, *next;
896 struct workspace *ws = container_of(listener,
897 struct workspace,
898 seat_destroyed_listener);
899
900 wl_list_for_each_safe(state, next, &ws->focus_list, link)
901 if (state->seat == seat)
902 wl_list_remove(&state->link);
903}
904
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200905static struct workspace *
Quentin Glidic82681572016-12-17 13:40:51 +0100906workspace_create(struct desktop_shell *shell)
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200907{
908 struct workspace *ws = malloc(sizeof *ws);
909 if (ws == NULL)
910 return NULL;
911
Quentin Glidic82681572016-12-17 13:40:51 +0100912 weston_layer_init(&ws->layer, shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200913
Jonas Ådahl04769742012-06-13 00:01:24 +0200914 wl_list_init(&ws->focus_list);
915 wl_list_init(&ws->seat_destroyed_listener.link);
916 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100917 ws->fsurf_front = NULL;
918 ws->fsurf_back = NULL;
919 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200920
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200921 return ws;
922}
923
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200924static int
925workspace_is_empty(struct workspace *ws)
926{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300927 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200928}
929
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200930static struct workspace *
931get_workspace(struct desktop_shell *shell, unsigned int index)
932{
933 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200934 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200935 pws += index;
936 return *pws;
937}
938
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800939struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200940get_current_workspace(struct desktop_shell *shell)
941{
942 return get_workspace(shell, shell->workspaces.current);
943}
944
945static void
946activate_workspace(struct desktop_shell *shell, unsigned int index)
947{
948 struct workspace *ws;
949
950 ws = get_workspace(shell, index);
Quentin Glidic82681572016-12-17 13:40:51 +0100951 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200952
953 shell->workspaces.current = index;
954}
955
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200956static unsigned int
957get_output_height(struct weston_output *output)
958{
959 return abs(output->region.extents.y1 - output->region.extents.y2);
960}
961
Greg Vf57774e2018-07-24 23:21:55 +0300962static struct weston_transform *
963view_get_transform(struct weston_view *view)
964{
965 struct focus_surface *fsurf = NULL;
966 struct shell_surface *shsurf = NULL;
967
968 if (is_focus_view(view)) {
969 fsurf = get_focus_surface(view->surface);
970 return &fsurf->workspace_transform;
971 }
972
973 shsurf = get_shell_surface(view->surface);
974 if (shsurf)
975 return &shsurf->workspace_transform;
976
977 return NULL;
978}
979
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200980static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100981view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200982{
Greg Vf57774e2018-07-24 23:21:55 +0300983 struct weston_transform *transform = view_get_transform(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200984
Greg Vf57774e2018-07-24 23:21:55 +0300985 if (!transform)
986 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100987
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200988 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500989 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100990 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200991
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100992 weston_matrix_init(&transform->matrix);
993 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200994 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500995 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200996}
997
998static void
999workspace_translate_out(struct workspace *ws, double fraction)
1000{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001001 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001002 unsigned int height;
1003 double d;
1004
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001005 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001006 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001007 d = height * fraction;
1008
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001009 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001010 }
1011}
1012
1013static void
1014workspace_translate_in(struct workspace *ws, double fraction)
1015{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001016 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001017 unsigned int height;
1018 double d;
1019
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001020 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001021 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001022
1023 if (fraction > 0)
1024 d = -(height - height * fraction);
1025 else
1026 d = height + height * fraction;
1027
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001028 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001029 }
1030}
1031
1032static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001033reverse_workspace_change_animation(struct desktop_shell *shell,
1034 unsigned int index,
1035 struct workspace *from,
1036 struct workspace *to)
1037{
1038 shell->workspaces.current = index;
1039
1040 shell->workspaces.anim_to = to;
1041 shell->workspaces.anim_from = from;
1042 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001043 shell->workspaces.anim_timestamp = (struct timespec) { 0 };
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001044
Quentin Glidic82681572016-12-17 13:40:51 +01001045 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1046 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
1047
Scott Moreau4272e632012-08-13 09:58:41 -06001048 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001049}
1050
1051static void
1052workspace_deactivate_transforms(struct workspace *ws)
1053{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001054 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001055 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001056
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001057 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Greg Vf57774e2018-07-24 23:21:55 +03001058 transform = view_get_transform(view);
1059 if (!transform)
1060 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001061
1062 if (!wl_list_empty(&transform->link)) {
1063 wl_list_remove(&transform->link);
1064 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001065 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001066 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001067 }
1068}
1069
1070static void
1071finish_workspace_change_animation(struct desktop_shell *shell,
1072 struct workspace *from,
1073 struct workspace *to)
1074{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001075 struct weston_view *view;
1076
Scott Moreau4272e632012-08-13 09:58:41 -06001077 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001078
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001079 /* Views that extend past the bottom of the output are still
1080 * visible after the workspace animation ends but before its layer
1081 * is hidden. In that case, we need to damage below those views so
1082 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001083 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001084 weston_view_damage_below(view);
1085
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001086 wl_list_remove(&shell->workspaces.animation.link);
1087 workspace_deactivate_transforms(from);
1088 workspace_deactivate_transforms(to);
1089 shell->workspaces.anim_to = NULL;
1090
Quentin Glidic82681572016-12-17 13:40:51 +01001091 weston_layer_unset_position(&shell->workspaces.anim_from->layer);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001092}
1093
1094static void
1095animate_workspace_change_frame(struct weston_animation *animation,
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001096 struct weston_output *output,
1097 const struct timespec *time)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001098{
1099 struct desktop_shell *shell =
1100 container_of(animation, struct desktop_shell,
1101 workspaces.animation);
1102 struct workspace *from = shell->workspaces.anim_from;
1103 struct workspace *to = shell->workspaces.anim_to;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001104 int64_t t;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001105 double x, y;
1106
1107 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1108 finish_workspace_change_animation(shell, from, to);
1109 return;
1110 }
1111
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001112 if (timespec_is_zero(&shell->workspaces.anim_timestamp)) {
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001113 if (shell->workspaces.anim_current == 0.0)
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001114 shell->workspaces.anim_timestamp = *time;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001115 else
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001116 timespec_add_msec(&shell->workspaces.anim_timestamp,
1117 time,
Emmanuel Gil Peyrot426c2462019-02-20 16:33:32 +01001118 /* Inverse of movement function 'y' below. */
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001119 -(asin(1.0 - shell->workspaces.anim_current) *
1120 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1121 M_2_PI));
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001122 }
1123
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001124 t = timespec_sub_to_msec(time, &shell->workspaces.anim_timestamp);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001125
1126 /*
1127 * x = [0, π/2]
1128 * y(x) = sin(x)
1129 */
1130 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1131 y = sin(x);
1132
1133 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001134 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001135
1136 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1137 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1138 shell->workspaces.anim_current = y;
1139
Scott Moreau4272e632012-08-13 09:58:41 -06001140 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001141 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001142 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001143 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001144}
1145
1146static void
1147animate_workspace_change(struct desktop_shell *shell,
1148 unsigned int index,
1149 struct workspace *from,
1150 struct workspace *to)
1151{
1152 struct weston_output *output;
1153
1154 int dir;
1155
1156 if (index > shell->workspaces.current)
1157 dir = -1;
1158 else
1159 dir = 1;
1160
1161 shell->workspaces.current = index;
1162
1163 shell->workspaces.anim_dir = dir;
1164 shell->workspaces.anim_from = from;
1165 shell->workspaces.anim_to = to;
1166 shell->workspaces.anim_current = 0.0;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001167 shell->workspaces.anim_timestamp = (struct timespec) { 0 };
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001168
1169 output = container_of(shell->compositor->output_list.next,
1170 struct weston_output, link);
1171 wl_list_insert(&output->animation_list,
1172 &shell->workspaces.animation.link);
1173
Quentin Glidic82681572016-12-17 13:40:51 +01001174 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1175 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001176
1177 workspace_translate_in(to, 0);
1178
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001179 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001180
Scott Moreau4272e632012-08-13 09:58:41 -06001181 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001182}
1183
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001184static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001185update_workspace(struct desktop_shell *shell, unsigned int index,
1186 struct workspace *from, struct workspace *to)
1187{
1188 shell->workspaces.current = index;
Quentin Glidic82681572016-12-17 13:40:51 +01001189 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1190 weston_layer_unset_position(&from->layer);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001191}
1192
1193static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001194change_workspace(struct desktop_shell *shell, unsigned int index)
1195{
1196 struct workspace *from;
1197 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001198 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001199
1200 if (index == shell->workspaces.current)
1201 return;
1202
1203 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001204 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001205 return;
1206
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001207 from = get_current_workspace(shell);
1208 to = get_workspace(shell, index);
1209
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001210 if (shell->workspaces.anim_from == to &&
1211 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001212 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001213 reverse_workspace_change_animation(shell, index, from, to);
1214 return;
1215 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001216
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001217 if (shell->workspaces.anim_to != NULL)
1218 finish_workspace_change_animation(shell,
1219 shell->workspaces.anim_from,
1220 shell->workspaces.anim_to);
1221
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001222 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001223
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001224 if (shell->focus_animation_type != ANIMATION_NONE) {
1225 wl_list_for_each(state, &from->focus_list, link)
1226 if (state->keyboard_focus)
1227 animate_focus_change(shell, from,
1228 get_default_view(state->keyboard_focus), NULL);
1229
1230 wl_list_for_each(state, &to->focus_list, link)
1231 if (state->keyboard_focus)
1232 animate_focus_change(shell, to,
1233 NULL, get_default_view(state->keyboard_focus));
1234 }
1235
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001236 if (workspace_is_empty(to) && workspace_is_empty(from))
1237 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001238 else
1239 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001240}
1241
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001242static bool
1243workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1244{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001245 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001246 struct wl_list *e;
1247
1248 if (wl_list_empty(list))
1249 return false;
1250
1251 e = list->next;
1252
1253 if (e->next != list)
1254 return false;
1255
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001256 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001257}
1258
1259static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001260surface_keyboard_focus_lost(struct weston_surface *surface)
1261{
1262 struct weston_compositor *compositor = surface->compositor;
1263 struct weston_seat *seat;
1264 struct weston_surface *focus;
1265
1266 wl_list_for_each(seat, &compositor->seat_list, link) {
1267 struct weston_keyboard *keyboard =
1268 weston_seat_get_keyboard(seat);
1269
1270 if (!keyboard)
1271 continue;
1272
1273 focus = weston_surface_get_main_surface(keyboard->focus);
1274 if (focus == surface)
1275 weston_keyboard_set_focus(keyboard, NULL);
1276 }
1277}
1278
1279static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001280take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001281 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001282 unsigned int index)
1283{
Derek Foreman1281a362015-07-31 16:55:32 -05001284 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001285 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001286 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001287 struct shell_surface *shsurf;
1288 struct workspace *from;
1289 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001290 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001291
Derek Foreman1281a362015-07-31 16:55:32 -05001292 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001293 view = get_default_view(surface);
1294 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001295 index == shell->workspaces.current ||
1296 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001297 return;
1298
1299 from = get_current_workspace(shell);
1300 to = get_workspace(shell, index);
1301
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001302 weston_layer_entry_remove(&view->layer_link);
1303 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001304
Philip Withnall659163d2013-11-25 18:01:36 +00001305 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001306 if (shsurf != NULL)
1307 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001308
Jonas Ådahle9d22502012-08-29 22:13:01 +02001309 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001310 drop_focus_state(shell, from, surface);
1311
1312 if (shell->workspaces.anim_from == to &&
1313 shell->workspaces.anim_to == from) {
1314 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001315
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001316 return;
1317 }
1318
1319 if (shell->workspaces.anim_to != NULL)
1320 finish_workspace_change_animation(shell,
1321 shell->workspaces.anim_from,
1322 shell->workspaces.anim_to);
1323
1324 if (workspace_is_empty(from) &&
1325 workspace_has_only(to, surface))
1326 update_workspace(shell, index, from, to);
1327 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001328 if (shsurf != NULL &&
1329 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001330 wl_list_insert(&shell->workspaces.anim_sticky_list,
1331 &shsurf->workspace_transform.link);
1332
1333 animate_workspace_change(shell, index, from, to);
1334 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001335
Jonas Ådahl8538b222012-08-29 22:13:03 +02001336 state = ensure_focus_state(shell, seat);
1337 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001338 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001339}
1340
1341static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02001342touch_move_grab_down(struct weston_touch_grab *grab,
1343 const struct timespec *time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001344 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001345{
1346}
1347
1348static void
Alexandros Frantzis27a51b82017-11-16 18:20:59 +02001349touch_move_grab_up(struct weston_touch_grab *grab, const struct timespec *time,
1350 int touch_id)
Rusty Lynch1084da52013-08-15 09:10:08 -07001351{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001352 struct weston_touch_move_grab *move =
1353 (struct weston_touch_move_grab *) container_of(
1354 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001355
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001356 if (touch_id == 0)
1357 move->active = 0;
1358
Jonas Ådahl9484b692013-12-02 22:05:03 +01001359 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001360 shell_touch_grab_end(&move->base);
1361 free(move);
1362 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001363}
1364
1365static void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +02001366touch_move_grab_motion(struct weston_touch_grab *grab,
1367 const struct timespec *time, int touch_id,
1368 wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001369{
1370 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1371 struct shell_surface *shsurf = move->base.shsurf;
1372 struct weston_surface *es;
1373 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1374 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1375
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001376 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001377 return;
1378
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001379 es = weston_desktop_surface_get_surface(shsurf->desktop_surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001380
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001381 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001382
1383 weston_compositor_schedule_repaint(es->compositor);
1384}
1385
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001386static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001387touch_move_grab_frame(struct weston_touch_grab *grab)
1388{
1389}
1390
1391static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001392touch_move_grab_cancel(struct weston_touch_grab *grab)
1393{
1394 struct weston_touch_move_grab *move =
1395 (struct weston_touch_move_grab *) container_of(
1396 grab, struct shell_touch_grab, grab);
1397
1398 shell_touch_grab_end(&move->base);
1399 free(move);
1400}
1401
Rusty Lynch1084da52013-08-15 09:10:08 -07001402static const struct weston_touch_grab_interface touch_move_grab_interface = {
1403 touch_move_grab_down,
1404 touch_move_grab_up,
1405 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001406 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001407 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001408};
1409
1410static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001411surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001412{
1413 struct weston_touch_move_grab *move;
1414
1415 if (!shsurf)
1416 return -1;
1417
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001418 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1419 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Rusty Lynch1084da52013-08-15 09:10:08 -07001420 return 0;
1421
1422 move = malloc(sizeof *move);
1423 if (!move)
1424 return -1;
1425
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001426 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001427 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001428 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001429 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001430 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001431
1432 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001433 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001434
1435 return 0;
1436}
1437
1438static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001439noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001440{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001441}
1442
1443static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001444noop_grab_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02001445 const struct timespec *time,
1446 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001447{
1448}
1449
1450static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001451noop_grab_axis_source(struct weston_pointer_grab *grab,
1452 uint32_t source)
1453{
1454}
1455
1456static void
1457noop_grab_frame(struct weston_pointer_grab *grab)
1458{
1459}
1460
1461static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001462constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1463{
1464 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001465 struct weston_surface *surface =
1466 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001467 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001468 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001469 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001470 pixman_rectangle32_t area;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001471 struct weston_geometry geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001472
1473 x = wl_fixed_to_int(pointer->x + move->dx);
1474 y = wl_fixed_to_int(pointer->y + move->dy);
1475
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001476 if (shsurf->shell->panel_position ==
1477 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001478 get_output_work_area(shsurf->shell, surface->output, &area);
1479 geometry =
1480 weston_desktop_surface_get_geometry(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001481
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001482 bottom = y + geometry.height + geometry.y;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001483 if (bottom - safety < area.y)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001484 y = area.y + safety - geometry.height
1485 - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001486
1487 if (move->client_initiated &&
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001488 y + geometry.y < area.y)
1489 y = area.y - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001490 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001491
1492 *cx = x;
1493 *cy = y;
1494}
1495
1496static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001497move_grab_motion(struct weston_pointer_grab *grab,
1498 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001499 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001500{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001501 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001502 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001503 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001504 struct weston_surface *surface;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001505 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001506
Jonas Ådahld2510102014-10-05 21:39:14 +02001507 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001508 if (!shsurf)
1509 return;
1510
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001511 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1512
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001513 constrain_position(move, &cx, &cy);
1514
1515 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001516
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001517 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001518}
1519
1520static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001521move_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001522 const struct timespec *time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001523{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001524 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1525 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001526 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001527 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001528
Daniel Stone4dbadb12012-05-30 16:31:51 +01001529 if (pointer->button_count == 0 &&
1530 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001531 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001532 free(grab);
1533 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001534}
1535
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001536static void
1537move_grab_cancel(struct weston_pointer_grab *grab)
1538{
1539 struct shell_grab *shell_grab =
1540 container_of(grab, struct shell_grab, grab);
1541
1542 shell_grab_end(shell_grab);
1543 free(grab);
1544}
1545
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001546static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001547 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001548 move_grab_motion,
1549 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001550 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001551 noop_grab_axis_source,
1552 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001553 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001554};
1555
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001556static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001557surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001558 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001559{
1560 struct weston_move_grab *move;
1561
1562 if (!shsurf)
1563 return -1;
1564
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001565 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001566 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1567 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001568 return 0;
1569
1570 move = malloc(sizeof *move);
1571 if (!move)
1572 return -1;
1573
Jason Ekstranda7af7042013-10-12 22:38:11 -05001574 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001575 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001576 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001577 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001578 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001579
1580 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001581 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001582
1583 return 0;
1584}
1585
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001586struct weston_resize_grab {
1587 struct shell_grab base;
1588 uint32_t edges;
1589 int32_t width, height;
1590};
1591
1592static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001593resize_grab_motion(struct weston_pointer_grab *grab,
1594 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001595 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001596{
1597 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001598 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001599 struct shell_surface *shsurf = resize->base.shsurf;
1600 int32_t width, height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001601 struct weston_size min_size, max_size;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001602 wl_fixed_t from_x, from_y;
1603 wl_fixed_t to_x, to_y;
1604
Jonas Ådahld2510102014-10-05 21:39:14 +02001605 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001606
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001607 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001608 return;
1609
Jason Ekstranda7af7042013-10-12 22:38:11 -05001610 weston_view_from_global_fixed(shsurf->view,
1611 pointer->grab_x, pointer->grab_y,
1612 &from_x, &from_y);
1613 weston_view_from_global_fixed(shsurf->view,
1614 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001615
1616 width = resize->width;
1617 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1618 width += wl_fixed_to_int(from_x - to_x);
1619 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1620 width += wl_fixed_to_int(to_x - from_x);
1621 }
1622
1623 height = resize->height;
1624 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1625 height += wl_fixed_to_int(from_y - to_y);
1626 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1627 height += wl_fixed_to_int(to_y - from_y);
1628 }
1629
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001630 max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
1631 min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
1632
1633 min_size.width = MAX(1, min_size.width);
1634 min_size.height = MAX(1, min_size.height);
1635
1636 if (width < min_size.width)
1637 width = min_size.width;
1638 else if (max_size.width > 0 && width > max_size.width)
1639 width = max_size.width;
1640 if (height < min_size.height)
1641 height = min_size.height;
1642 else if (max_size.width > 0 && width > max_size.width)
1643 width = max_size.width;
1644 weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001645}
1646
1647static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001648resize_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001649 const struct timespec *time,
1650 uint32_t button, uint32_t state_w)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001651{
1652 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001653 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001654 enum wl_pointer_button_state state = state_w;
1655
1656 if (pointer->button_count == 0 &&
1657 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Greg Vec3f7792018-11-08 00:24:56 +03001658 if (resize->base.shsurf != NULL) {
1659 struct weston_desktop_surface *desktop_surface =
1660 resize->base.shsurf->desktop_surface;
1661 weston_desktop_surface_set_resizing(desktop_surface,
1662 false);
1663 }
1664
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001665 shell_grab_end(&resize->base);
1666 free(grab);
1667 }
1668}
1669
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001670static void
1671resize_grab_cancel(struct weston_pointer_grab *grab)
1672{
1673 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1674
Greg Vec3f7792018-11-08 00:24:56 +03001675 if (resize->base.shsurf != NULL) {
1676 struct weston_desktop_surface *desktop_surface =
1677 resize->base.shsurf->desktop_surface;
1678 weston_desktop_surface_set_resizing(desktop_surface, false);
1679 }
1680
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001681 shell_grab_end(&resize->base);
1682 free(grab);
1683}
1684
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001685static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001686 noop_grab_focus,
1687 resize_grab_motion,
1688 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001689 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001690 noop_grab_axis_source,
1691 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001692 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001693};
1694
Giulio Camuffob8366642013-04-25 13:57:46 +03001695
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001696static int
1697surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001698 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001699{
1700 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001701 const unsigned resize_topbottom =
1702 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1703 const unsigned resize_leftright =
1704 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1705 const unsigned resize_any = resize_topbottom | resize_leftright;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001706 struct weston_geometry geometry;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001707
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001708 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001709 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1710 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001711 return 0;
1712
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001713 /* Check for invalid edge combinations. */
1714 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1715 (edges & resize_topbottom) == resize_topbottom ||
1716 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001717 return 0;
1718
1719 resize = malloc(sizeof *resize);
1720 if (!resize)
1721 return -1;
1722
1723 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001724
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001725 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1726 resize->width = geometry.width;
1727 resize->height = geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001728
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001729 shsurf->resize_edges = edges;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001730 weston_desktop_surface_set_resizing(shsurf->desktop_surface, true);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001731 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001732 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001733
1734 return 0;
1735}
1736
1737static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001738busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001739{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001740 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001741 struct weston_pointer *pointer = base->pointer;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001742 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001743 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001744 wl_fixed_t sx, sy;
1745
Jason Ekstranda7af7042013-10-12 22:38:11 -05001746 view = weston_compositor_pick_view(pointer->seat->compositor,
1747 pointer->x, pointer->y,
1748 &sx, &sy);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001749 desktop_surface = weston_surface_get_desktop_surface(view->surface);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001750
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001751 if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001752 shell_grab_end(grab);
1753 free(grab);
1754 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001755}
1756
1757static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001758busy_cursor_grab_motion(struct weston_pointer_grab *grab,
1759 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001760 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001761{
Jonas Ådahld2510102014-10-05 21:39:14 +02001762 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001763}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001764
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001765static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001766busy_cursor_grab_button(struct weston_pointer_grab *base,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001767 const struct timespec *time,
1768 uint32_t button, uint32_t state)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001769{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001770 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001771 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05001772 struct weston_pointer *pointer = grab->grab.pointer;
1773 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001774
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001775 if (shsurf && button == BTN_LEFT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001776 activate(shsurf->shell, shsurf->view, seat,
1777 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman794fa0e2015-07-15 13:00:38 -05001778 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001779 } else if (shsurf && button == BTN_RIGHT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001780 activate(shsurf->shell, shsurf->view, seat,
1781 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman74de4692015-07-15 13:00:39 -05001782 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001783 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001784}
1785
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001786static void
1787busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1788{
1789 struct shell_grab *grab = (struct shell_grab *) base;
1790
1791 shell_grab_end(grab);
1792 free(grab);
1793}
1794
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001795static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001796 busy_cursor_grab_focus,
1797 busy_cursor_grab_motion,
1798 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001799 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001800 noop_grab_axis_source,
1801 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001802 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001803};
1804
1805static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001806handle_pointer_focus(struct wl_listener *listener, void *data)
1807{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001808 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001809 struct weston_view *view = pointer->focus;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001810 struct shell_surface *shsurf;
1811 struct weston_desktop_client *client;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001812
Jason Ekstranda7af7042013-10-12 22:38:11 -05001813 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001814 return;
1815
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001816 shsurf = get_shell_surface(view->surface);
1817 if (!shsurf)
1818 return;
1819
1820 client = weston_desktop_surface_get_client(shsurf->desktop_surface);
1821
1822 if (shsurf->unresponsive)
1823 set_busy_cursor(shsurf, pointer);
1824 else
1825 weston_desktop_client_ping(client);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001826}
1827
1828static void
Marius Vladab39e1d2021-03-05 21:40:22 +02001829shell_surface_deactivate(struct shell_surface *shsurf)
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001830{
1831 if (--shsurf->focus_count == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001832 weston_desktop_surface_set_activated(shsurf->desktop_surface, false);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001833}
1834
1835static void
Marius Vladab39e1d2021-03-05 21:40:22 +02001836shell_surface_activate(struct shell_surface *shsurf)
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001837{
1838 if (shsurf->focus_count++ == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001839 weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001840}
1841
Philip Withnall07926d92013-11-25 18:01:40 +00001842/* The surface will be inserted into the list immediately after the link
1843 * returned by this function (i.e. will be stacked immediately above the
1844 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001845static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00001846shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1847{
1848 struct workspace *ws;
1849
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001850 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
1851 !shsurf->state.lowered) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001852 return &shsurf->shell->fullscreen_layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001853 }
1854
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001855 /* Move the surface to a normal workspace layer so that surfaces
1856 * which were previously fullscreen or transient are no longer
1857 * rendered on top. */
1858 ws = get_current_workspace(shsurf->shell);
1859 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001860}
1861
Philip Withnall648a4dd2013-11-25 18:01:44 +00001862static void
1863shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
1864{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001865 weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001866}
1867
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001868/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00001869 * geometry to ensure the changes are redrawn.
1870 *
1871 * If any child surfaces exist and are mapped, ensure they’re in the same layer
1872 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001873static void
1874shell_surface_update_layer(struct shell_surface *shsurf)
1875{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001876 struct weston_surface *surface =
1877 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001878 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001879
1880 new_layer_link = shell_surface_calculate_layer_link(shsurf);
1881
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001882 if (new_layer_link == NULL)
1883 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001884 if (new_layer_link == &shsurf->view->layer_link)
1885 return;
1886
1887 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001888 weston_layer_entry_remove(&shsurf->view->layer_link);
1889 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001890 weston_view_geometry_dirty(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001891 weston_surface_damage(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001892
1893 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001894}
1895
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001896static void
Semi Malinen99f8c082018-05-02 11:10:32 +02001897notify_output_destroy(struct wl_listener *listener, void *data)
1898{
1899 struct shell_surface *shsurf =
1900 container_of(listener,
1901 struct shell_surface, output_destroy_listener);
1902
1903 shsurf->output = NULL;
1904 shsurf->output_destroy_listener.notify = NULL;
1905}
1906
1907static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00001908shell_surface_set_output(struct shell_surface *shsurf,
1909 struct weston_output *output)
1910{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001911 struct weston_surface *es =
1912 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001913
1914 /* get the default output, if the client set it as NULL
Abdur Rehman7f1da1f2017-01-01 19:46:33 +05001915 check whether the output is available */
Philip Withnall352e7ed2013-11-25 18:01:35 +00001916 if (output)
1917 shsurf->output = output;
1918 else if (es->output)
1919 shsurf->output = es->output;
1920 else
1921 shsurf->output = get_default_output(es->compositor);
Semi Malinen99f8c082018-05-02 11:10:32 +02001922
1923 if (shsurf->output_destroy_listener.notify) {
1924 wl_list_remove(&shsurf->output_destroy_listener.link);
1925 shsurf->output_destroy_listener.notify = NULL;
1926 }
1927
1928 if (!shsurf->output)
1929 return;
1930
1931 shsurf->output_destroy_listener.notify = notify_output_destroy;
1932 wl_signal_add(&shsurf->output->destroy_signal,
1933 &shsurf->output_destroy_listener);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001934}
1935
1936static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001937weston_view_set_initial_position(struct weston_view *view,
1938 struct desktop_shell *shell);
1939
1940static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001941unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08001942{
Philip Withnallf85fe842013-11-25 18:01:37 +00001943 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wu4539b082012-03-01 12:57:46 +08001944 wl_list_remove(&shsurf->fullscreen.transform.link);
1945 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00001946
Jason Ekstranda7af7042013-10-12 22:38:11 -05001947 if (shsurf->fullscreen.black_view)
1948 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
1949 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00001950
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001951 if (shsurf->saved_position_valid)
1952 weston_view_set_position(shsurf->view,
1953 shsurf->saved_x, shsurf->saved_y);
1954 else
1955 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02001956 shsurf->saved_position_valid = false;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001957
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001958 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001959 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00001960 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001961 shsurf->saved_rotation_valid = false;
1962 }
Alex Wu4539b082012-03-01 12:57:46 +08001963}
1964
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001965static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001966unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001967{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001968 struct weston_surface *surface =
1969 weston_desktop_surface_get_surface(shsurf->desktop_surface);
1970
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001971 /* undo all maximized things here */
Semi Malinen99f8c082018-05-02 11:10:32 +02001972 shell_surface_set_output(shsurf, get_default_output(surface->compositor));
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001973
1974 if (shsurf->saved_position_valid)
1975 weston_view_set_position(shsurf->view,
1976 shsurf->saved_x, shsurf->saved_y);
1977 else
1978 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02001979 shsurf->saved_position_valid = false;
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001980
1981 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001982 wl_list_insert(&shsurf->view->geometry.transformation_list,
1983 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001984 shsurf->saved_rotation_valid = false;
1985 }
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001986}
1987
Philip Withnallbecb77e2013-11-25 18:01:30 +00001988static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01001989set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001990{
1991 struct shell_surface *shsurf;
1992 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001993 struct weston_view *view;
Jonas Ådahl56958aa2021-10-01 11:12:21 +02001994 struct weston_subsurface *subsurface;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001995
1996 view = get_default_view(surface);
1997 if (!view)
1998 return;
1999
2000 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2001
2002 shsurf = get_shell_surface(surface);
2003 current_ws = get_current_workspace(shsurf->shell);
2004
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002005 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002006 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002007
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002008 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002009 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002010
2011 shell_surface_update_child_surface_layers(shsurf);
Jonas Ådahl56958aa2021-10-01 11:12:21 +02002012
2013 weston_view_damage_below(shsurf->view);
2014 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
2015 wl_list_for_each(view, &subsurface->surface->views, surface_link)
2016 weston_view_damage_below(view);
2017 }
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002018}
2019
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002020
Tiago Vignattibe143262012-04-16 17:31:41 +03002021static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002022shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002023{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002024 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002025}
2026
Pekka Paalanen8274d902014-08-06 19:36:51 +03002027static int
2028black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2029{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002030 struct weston_view *fs_view = surface->committed_private;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002031 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002032 int n;
2033 int rem;
2034 int ret;
2035
2036 n = snprintf(buf, len, "black background surface for ");
2037 if (n < 0)
2038 return n;
2039
2040 rem = (int)len - n;
2041 if (rem < 0)
2042 rem = 0;
2043
2044 if (fs_surface->get_label)
2045 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2046 else
2047 ret = snprintf(buf + n, rem, "<unknown>");
2048
2049 if (ret < 0)
2050 return n;
2051
2052 return n + ret;
2053}
2054
Alex Wu21858432012-04-01 20:13:08 +08002055static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002056black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002057
Jason Ekstranda7af7042013-10-12 22:38:11 -05002058static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002059create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002060 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002061 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002062{
2063 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002064 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002065
2066 surface = weston_surface_create(ec);
2067 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002068 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002069 return NULL;
2070 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002071 view = weston_view_create(surface);
2072 if (surface == NULL) {
2073 weston_log("no memory\n");
2074 weston_surface_destroy(surface);
2075 return NULL;
2076 }
Alex Wu4539b082012-03-01 12:57:46 +08002077
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002078 surface->committed = black_surface_committed;
2079 surface->committed_private = fs_view;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002080 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002081 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002082 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002083 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002084 pixman_region32_fini(&surface->input);
2085 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002086
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002087 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002088 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002089
2090 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002091}
2092
Philip Withnalled8f1a92013-11-25 18:01:43 +00002093static void
2094shell_ensure_fullscreen_black_view(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);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002098 struct weston_output *output = shsurf->fullscreen_output;
2099
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002100 assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
Philip Withnalled8f1a92013-11-25 18:01:43 +00002101
2102 if (!shsurf->fullscreen.black_view)
2103 shsurf->fullscreen.black_view =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002104 create_black_surface(surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002105 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002106 output->x, output->y,
2107 output->width,
2108 output->height);
2109
2110 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002111 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2112 weston_layer_entry_insert(&shsurf->view->layer_link,
2113 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002114 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002115 weston_surface_damage(surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002116
Armin Krezović4663aca2016-06-30 06:04:29 +02002117 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002118 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002119}
2120
Alex Wu4539b082012-03-01 12:57:46 +08002121/* Create black surface and append it to the associated fullscreen surface.
2122 * Handle size dismatch and positioning according to the method. */
2123static void
2124shell_configure_fullscreen(struct shell_surface *shsurf)
2125{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002126 struct weston_surface *surface =
2127 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffob8366642013-04-25 13:57:46 +03002128 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002129
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002130 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002131 weston_layer_entry_remove(&shsurf->view->layer_link);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002132 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2133 &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002134
Pekka Paalanenc63acc42018-05-02 10:21:58 +02002135 if (!shsurf->fullscreen_output) {
2136 /* If there is no output, there's not much we can do.
2137 * Position the window somewhere, whatever. */
2138 weston_view_set_position(shsurf->view, 0, 0);
2139 return;
2140 }
2141
Philip Withnalled8f1a92013-11-25 18:01:43 +00002142 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002143
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002144 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002145 &surf_width, &surf_height);
2146
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002147 if (surface->buffer_ref.buffer)
2148 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002149}
2150
Alex Wu4539b082012-03-01 12:57:46 +08002151static void
2152shell_map_fullscreen(struct shell_surface *shsurf)
2153{
Alex Wubd3354b2012-04-17 17:20:49 +08002154 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002155}
2156
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002157static void
Marius Vladc114fd62021-08-26 16:37:24 +03002158desktop_shell_destroy_seat(struct shell_seat *shseat)
2159{
Marius Vladf3584cf2021-10-26 19:40:44 +03002160
Marius Vlada7392c82021-08-26 16:38:43 +03002161 wl_list_remove(&shseat->keyboard_focus_listener.link);
2162 wl_list_remove(&shseat->caps_changed_listener.link);
2163 wl_list_remove(&shseat->pointer_focus_listener.link);
Marius Vladc114fd62021-08-26 16:37:24 +03002164 wl_list_remove(&shseat->seat_destroy_listener.link);
2165
2166 wl_list_remove(&shseat->link);
2167 free(shseat);
2168}
2169
2170static void
Giulio Camuffo5085a752013-03-25 21:42:45 +01002171destroy_shell_seat(struct wl_listener *listener, void *data)
2172{
2173 struct shell_seat *shseat =
2174 container_of(listener,
2175 struct shell_seat, seat_destroy_listener);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002176
Marius Vladc114fd62021-08-26 16:37:24 +03002177 desktop_shell_destroy_seat(shseat);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002178}
2179
Jason Ekstrand024177c2014-04-21 19:42:58 -05002180static void
2181shell_seat_caps_changed(struct wl_listener *listener, void *data)
2182{
Derek Foreman1281a362015-07-31 16:55:32 -05002183 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05002184 struct shell_seat *seat;
2185
2186 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002187 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002188
Derek Foreman1281a362015-07-31 16:55:32 -05002189 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002190 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002191 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002192 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002193 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05002194 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002195 wl_list_init(&seat->pointer_focus_listener.link);
2196 }
2197}
2198
Giulio Camuffo5085a752013-03-25 21:42:45 +01002199static struct shell_seat *
Marius Vladc114fd62021-08-26 16:37:24 +03002200create_shell_seat(struct desktop_shell *shell, struct weston_seat *seat)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002201{
2202 struct shell_seat *shseat;
2203
2204 shseat = calloc(1, sizeof *shseat);
2205 if (!shseat) {
2206 weston_log("no memory to allocate shell seat\n");
2207 return NULL;
2208 }
2209
2210 shseat->seat = seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002211
2212 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2213 wl_signal_add(&seat->destroy_signal,
2214 &shseat->seat_destroy_listener);
2215
Jason Ekstrand024177c2014-04-21 19:42:58 -05002216 wl_list_init(&shseat->keyboard_focus_listener.link);
2217
2218 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2219 wl_list_init(&shseat->pointer_focus_listener.link);
2220
2221 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2222 wl_signal_add(&seat->updated_caps_signal,
2223 &shseat->caps_changed_listener);
2224 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2225
Marius Vladc114fd62021-08-26 16:37:24 +03002226 wl_list_insert(&shell->seat_list, &shseat->link);
2227
Giulio Camuffo5085a752013-03-25 21:42:45 +01002228 return shseat;
2229}
2230
2231static struct shell_seat *
2232get_shell_seat(struct weston_seat *seat)
2233{
2234 struct wl_listener *listener;
2235
2236 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002237 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002238
2239 return container_of(listener,
2240 struct shell_seat, seat_destroy_listener);
2241}
2242
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002243static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002244fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002245{
2246 struct shell_surface *shsurf = data;
2247
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002248 weston_surface_destroy(shsurf->view->surface);
Pekka Paalanen99628002018-05-22 12:48:35 +03002249
2250 if (shsurf->output_destroy_listener.notify) {
2251 wl_list_remove(&shsurf->output_destroy_listener.link);
2252 shsurf->output_destroy_listener.notify = NULL;
2253 }
2254
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002255 free(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002256}
2257
2258static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002259fade_out_done(struct weston_view_animation *animation, void *data)
2260{
2261 struct shell_surface *shsurf = data;
2262 struct wl_event_loop *loop;
2263
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002264 loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
Derek Foreman039e9be2015-04-14 17:09:06 -05002265
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002266 if (weston_view_is_mapped(shsurf->view)) {
Tomohito Esaki5898cd32019-04-01 17:50:14 +09002267 weston_view_unmap(shsurf->view);
Derek Foreman039e9be2015-04-14 17:09:06 -05002268 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
Derek Foreman039e9be2015-04-14 17:09:06 -05002269 }
2270}
2271
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002272struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002273get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002274{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002275 if (weston_surface_is_desktop_surface(surface)) {
2276 struct weston_desktop_surface *desktop_surface =
2277 weston_surface_get_desktop_surface(surface);
2278 return weston_desktop_surface_get_user_data(desktop_surface);
2279 }
2280 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002281}
2282
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002283/*
2284 * libweston-desktop
2285 */
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002286
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002287static void
2288desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2289 void *shell)
2290{
2291 struct weston_desktop_client *client =
2292 weston_desktop_surface_get_client(desktop_surface);
2293 struct wl_client *wl_client =
2294 weston_desktop_client_get_client(client);
2295 struct weston_view *view;
2296 struct shell_surface *shsurf;
2297 struct weston_surface *surface =
2298 weston_desktop_surface_get_surface(desktop_surface);
2299
2300 view = weston_desktop_surface_create_view(desktop_surface);
2301 if (!view)
2302 return;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002303
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002304 shsurf = calloc(1, sizeof *shsurf);
2305 if (!shsurf) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002306 if (wl_client)
2307 wl_client_post_no_memory(wl_client);
2308 else
2309 weston_log("no memory to allocate shell surface\n");
2310 return;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002311 }
2312
Pekka Paalanen8274d902014-08-06 19:36:51 +03002313 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002314
Tiago Vignattibc052c92012-04-19 16:18:18 +03002315 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002316 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002317 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002318 shsurf->saved_rotation_valid = false;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002319 shsurf->desktop_surface = desktop_surface;
2320 shsurf->view = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002321 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002322 wl_list_init(&shsurf->fullscreen.transform.link);
2323
Semi Malinen99f8c082018-05-02 11:10:32 +02002324 shell_surface_set_output(
2325 shsurf, get_default_output(shsurf->shell->compositor));
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002326
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002327 wl_signal_init(&shsurf->destroy_signal);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002328
Pekka Paalanen460099f2012-01-20 16:48:25 +02002329 /* empty when not in use */
2330 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002331 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002332
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002333 wl_list_init(&shsurf->workspace_transform.link);
2334
Stefan Agnera8da2082019-06-23 14:53:59 +02002335 /*
2336 * initialize list as well as link. The latter allows to use
2337 * wl_list_remove() even when this surface is not in another list.
2338 */
2339 wl_list_init(&shsurf->children_list);
2340 wl_list_init(&shsurf->children_link);
2341
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002342 weston_desktop_surface_set_user_data(desktop_surface, shsurf);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002343}
2344
Tiago Vignattibc052c92012-04-19 16:18:18 +03002345static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002346desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2347 void *shell)
2348{
2349 struct shell_surface *shsurf =
2350 weston_desktop_surface_get_user_data(desktop_surface);
Stefan Agnera8da2082019-06-23 14:53:59 +02002351 struct shell_surface *shsurf_child, *tmp;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002352 struct weston_surface *surface =
2353 weston_desktop_surface_get_surface(desktop_surface);
Marius Vladf12697b2021-03-05 21:44:26 +02002354 struct weston_seat *seat;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002355
2356 if (!shsurf)
2357 return;
2358
Stefan Agnera8da2082019-06-23 14:53:59 +02002359 wl_list_for_each_safe(shsurf_child, tmp, &shsurf->children_list, children_link) {
2360 wl_list_remove(&shsurf_child->children_link);
2361 wl_list_init(&shsurf_child->children_link);
2362 }
2363 wl_list_remove(&shsurf->children_link);
2364
Marius Vladf12697b2021-03-05 21:44:26 +02002365 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2366 struct shell_seat *shseat = get_shell_seat(seat);
2367 /* activate() controls the focused surface activation and
2368 * removal of a surface requires invalidating the
2369 * focused_surface to avoid activate() use a stale (and just
2370 * removed) surface when attempting to de-activate it. It will
2371 * also update the focused_surface once it has a chance to run.
2372 */
2373 if (surface == shseat->focused_surface)
2374 shseat->focused_surface = NULL;
2375 }
2376
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002377 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2378
2379 if (shsurf->fullscreen.black_view)
2380 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2381
2382 weston_surface_set_label_func(surface, NULL);
2383 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2384 shsurf->desktop_surface = NULL;
2385
Quentin Glidicf01ecee2016-08-16 10:52:46 +02002386 weston_desktop_surface_unlink_view(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002387 if (weston_surface_is_mapped(surface) &&
2388 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
Erik Kurzinger04918f32021-05-18 11:49:37 -04002389
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02002390 if (shsurf->shell->compositor->state == WESTON_COMPOSITOR_ACTIVE) {
2391 pixman_region32_fini(&surface->pending.input);
2392 pixman_region32_init(&surface->pending.input);
2393 pixman_region32_fini(&surface->input);
2394 pixman_region32_init(&surface->input);
2395 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2396 fade_out_done, shsurf);
2397 return;
2398 } else {
Marius Vladc30cb292021-08-02 15:47:16 +03002399 weston_surface_destroy(surface);
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02002400 }
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002401 }
Erik Kurzinger04918f32021-05-18 11:49:37 -04002402
Marius Vladc30cb292021-08-02 15:47:16 +03002403 weston_view_destroy(shsurf->view);
Erik Kurzinger04918f32021-05-18 11:49:37 -04002404
2405 if (shsurf->output_destroy_listener.notify) {
2406 wl_list_remove(&shsurf->output_destroy_listener.link);
2407 shsurf->output_destroy_listener.notify = NULL;
2408 }
2409
2410 free(shsurf);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002411}
2412
2413static void
2414set_maximized_position(struct desktop_shell *shell,
2415 struct shell_surface *shsurf)
2416{
2417 pixman_rectangle32_t area;
2418 struct weston_geometry geometry;
2419
2420 get_output_work_area(shell, shsurf->output, &area);
2421 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2422
2423 weston_view_set_position(shsurf->view,
2424 area.x - geometry.x,
2425 area.y - geometry.y);
2426}
2427
2428static void
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002429set_position_from_xwayland(struct shell_surface *shsurf)
2430{
2431 struct weston_geometry geometry;
2432 float x;
2433 float y;
2434
2435 assert(shsurf->xwayland.is_set);
2436
2437 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2438 x = shsurf->xwayland.x - geometry.x;
2439 y = shsurf->xwayland.y - geometry.y;
2440
2441 weston_view_set_position(shsurf->view, x, y);
2442
2443#ifdef WM_DEBUG
2444 weston_log("%s: XWM %d, %d; geometry %d, %d; view %f, %f\n",
2445 __func__, shsurf->xwayland.x, shsurf->xwayland.y,
2446 geometry.x, geometry.y, x, y);
2447#endif
2448}
2449
2450static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002451map(struct desktop_shell *shell, struct shell_surface *shsurf,
2452 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002453{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002454 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002455 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2456 struct weston_compositor *compositor = shell->compositor;
2457 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002458
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002459 /* initial positioning, see also configure() */
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002460 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002461 center_on_output(shsurf->view, shsurf->fullscreen_output);
2462 shell_map_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002463 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002464 set_maximized_position(shell, shsurf);
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002465 } else if (shsurf->xwayland.is_set) {
2466 set_position_from_xwayland(shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002467 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002468 weston_view_set_initial_position(shsurf->view, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002469 }
2470
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002471 /* Surface stacking order, see also activate(). */
2472 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002473
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002474 weston_view_update_transform(shsurf->view);
2475 shsurf->view->is_mapped = true;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002476 if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002477 surface->output = shsurf->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02002478 weston_view_set_output(shsurf->view, shsurf->output);
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002479 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002480
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002481 if (!shell->locked) {
2482 wl_list_for_each(seat, &compositor->seat_list, link)
2483 activate(shell, shsurf->view, seat,
2484 WESTON_ACTIVATE_FLAG_CONFIGURE);
2485 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002486
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002487 if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002488 switch (shell->win_animation_type) {
2489 case ANIMATION_FADE:
2490 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2491 break;
2492 case ANIMATION_ZOOM:
2493 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2494 break;
2495 case ANIMATION_NONE:
2496 default:
2497 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002498 }
2499 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002500}
2501
2502static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002503desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2504 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002505{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002506 struct shell_surface *shsurf =
2507 weston_desktop_surface_get_user_data(desktop_surface);
2508 struct weston_surface *surface =
2509 weston_desktop_surface_get_surface(desktop_surface);
2510 struct weston_view *view = shsurf->view;
2511 struct desktop_shell *shell = data;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002512 bool was_fullscreen;
2513 bool was_maximized;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002514
2515 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002516 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002517
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002518 was_fullscreen = shsurf->state.fullscreen;
2519 was_maximized = shsurf->state.maximized;
2520
2521 shsurf->state.fullscreen =
2522 weston_desktop_surface_get_fullscreen(desktop_surface);
2523 shsurf->state.maximized =
2524 weston_desktop_surface_get_maximized(desktop_surface);
2525
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002526 if (!weston_surface_is_mapped(surface)) {
2527 map(shell, shsurf, sx, sy);
2528 surface->is_mapped = true;
2529 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2530 ++surface->ref_count;
2531 return;
2532 }
2533
2534 if (sx == 0 && sy == 0 &&
2535 shsurf->last_width == surface->width &&
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002536 shsurf->last_height == surface->height &&
2537 was_fullscreen == shsurf->state.fullscreen &&
2538 was_maximized == shsurf->state.maximized)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002539 return;
2540
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002541 if (was_fullscreen)
2542 unset_fullscreen(shsurf);
2543 if (was_maximized)
2544 unset_maximized(shsurf);
2545
Quentin Glidic920cf042016-08-16 14:26:20 +02002546 if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2547 !shsurf->saved_position_valid) {
2548 shsurf->saved_x = shsurf->view->geometry.x;
2549 shsurf->saved_y = shsurf->view->geometry.y;
2550 shsurf->saved_position_valid = true;
2551
2552 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2553 wl_list_remove(&shsurf->rotation.transform.link);
2554 wl_list_init(&shsurf->rotation.transform.link);
2555 weston_view_geometry_dirty(shsurf->view);
2556 shsurf->saved_rotation_valid = true;
2557 }
2558 }
2559
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002560 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002561 shell_configure_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002562 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002563 set_maximized_position(shell, shsurf);
2564 surface->output = shsurf->output;
2565 } else {
2566 float from_x, from_y;
2567 float to_x, to_y;
2568 float x, y;
2569
2570 if (shsurf->resize_edges) {
2571 sx = 0;
2572 sy = 0;
2573 }
2574
2575 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2576 sx = shsurf->last_width - surface->width;
2577 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2578 sy = shsurf->last_height - surface->height;
2579
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002580 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2581 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2582 x = shsurf->view->geometry.x + to_x - from_x;
2583 y = shsurf->view->geometry.y + to_y - from_y;
2584
2585 weston_view_set_position(shsurf->view, x, y);
2586 }
2587
Emmanuel Gil Peyrotc3941792017-04-04 18:49:33 +01002588 shsurf->last_width = surface->width;
2589 shsurf->last_height = surface->height;
2590
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002591 /* XXX: would a fullscreen surface need the same handling? */
2592 if (surface->output) {
2593 wl_list_for_each(view, &surface->views, surface_link)
2594 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002595 }
2596}
2597
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002598static void
Derek Foreman927d9e22017-10-06 14:37:44 -05002599get_maximized_size(struct shell_surface *shsurf, int32_t *width, int32_t *height)
2600{
2601 struct desktop_shell *shell;
2602 pixman_rectangle32_t area;
2603
2604 shell = shell_surface_get_shell(shsurf);
2605 get_output_work_area(shell, shsurf->output, &area);
2606
2607 *width = area.width;
2608 *height = area.height;
2609}
2610
2611static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002612set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2613 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002614{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002615 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2616 struct weston_surface *surface =
2617 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2618 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002619
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002620 if (fullscreen) {
2621 /* handle clients launching in fullscreen */
2622 if (output == NULL && !weston_surface_is_mapped(surface)) {
2623 /* Set the output to the one that has focus currently. */
2624 output = get_focused_output(surface->compositor);
2625 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002626
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002627 shell_surface_set_output(shsurf, output);
2628 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002629
Marius Vlad6ebda362020-03-28 00:23:14 +02002630 if (shsurf->output) {
2631 width = shsurf->output->width;
2632 height = shsurf->output->height;
2633 }
Derek Foremane1af3d82017-10-06 14:37:45 -05002634 } else if (weston_desktop_surface_get_maximized(desktop_surface)) {
2635 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002636 }
2637 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2638 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002639}
2640
2641static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002642desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2643 struct weston_seat *seat, uint32_t serial, void *shell)
2644{
2645 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2646 struct weston_touch *touch = weston_seat_get_touch(seat);
2647 struct shell_surface *shsurf =
2648 weston_desktop_surface_get_user_data(desktop_surface);
2649 struct weston_surface *surface =
2650 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2651 struct wl_resource *resource = surface->resource;
2652 struct weston_surface *focus;
2653
2654 if (pointer &&
2655 pointer->focus &&
2656 pointer->button_count > 0 &&
2657 pointer->grab_serial == serial) {
2658 focus = weston_surface_get_main_surface(pointer->focus->surface);
2659 if ((focus == surface) &&
2660 (surface_move(shsurf, pointer, true) < 0))
2661 wl_resource_post_no_memory(resource);
2662 } else if (touch &&
2663 touch->focus &&
2664 touch->grab_serial == serial) {
2665 focus = weston_surface_get_main_surface(touch->focus->surface);
2666 if ((focus == surface) &&
2667 (surface_touch_move(shsurf, touch) < 0))
2668 wl_resource_post_no_memory(resource);
2669 }
2670}
2671
2672static void
2673desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2674 struct weston_seat *seat, uint32_t serial,
2675 enum weston_desktop_surface_edge edges, void *shell)
2676{
2677 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2678 struct shell_surface *shsurf =
2679 weston_desktop_surface_get_user_data(desktop_surface);
2680 struct weston_surface *surface =
2681 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2682 struct wl_resource *resource = surface->resource;
2683 struct weston_surface *focus;
2684
2685 if (!pointer ||
2686 pointer->button_count == 0 ||
2687 pointer->grab_serial != serial ||
2688 pointer->focus == NULL)
2689 return;
2690
2691 focus = weston_surface_get_main_surface(pointer->focus->surface);
2692 if (focus != surface)
2693 return;
2694
2695 if (surface_resize(shsurf, pointer, edges) < 0)
2696 wl_resource_post_no_memory(resource);
2697}
2698
2699static void
Stefan Agnera8da2082019-06-23 14:53:59 +02002700desktop_surface_set_parent(struct weston_desktop_surface *desktop_surface,
2701 struct weston_desktop_surface *parent,
2702 void *shell)
2703{
Marius Vlada27658e2020-01-17 12:32:55 +02002704 struct shell_surface *shsurf_parent;
Stefan Agnera8da2082019-06-23 14:53:59 +02002705 struct shell_surface *shsurf =
2706 weston_desktop_surface_get_user_data(desktop_surface);
Stefan Agnera8da2082019-06-23 14:53:59 +02002707
Marius Vlada27658e2020-01-17 12:32:55 +02002708 /* unlink any potential child */
2709 wl_list_remove(&shsurf->children_link);
2710
2711 if (parent) {
2712 shsurf_parent = weston_desktop_surface_get_user_data(parent);
2713 wl_list_insert(shsurf_parent->children_list.prev,
2714 &shsurf->children_link);
2715 } else {
2716 wl_list_init(&shsurf->children_link);
2717 }
Stefan Agnera8da2082019-06-23 14:53:59 +02002718}
2719
2720static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002721desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2722 bool fullscreen,
2723 struct weston_output *output, void *shell)
2724{
2725 struct shell_surface *shsurf =
2726 weston_desktop_surface_get_user_data(desktop_surface);
2727
2728 set_fullscreen(shsurf, fullscreen, output);
2729}
2730
2731static void
2732set_maximized(struct shell_surface *shsurf, bool maximized)
2733{
2734 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2735 struct weston_surface *surface =
2736 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2737 int32_t width = 0, height = 0;
2738
2739 if (maximized) {
2740 struct weston_output *output;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002741
2742 if (!weston_surface_is_mapped(surface))
2743 output = get_focused_output(surface->compositor);
2744 else
2745 output = surface->output;
2746
2747 shell_surface_set_output(shsurf, output);
2748
Derek Foreman927d9e22017-10-06 14:37:44 -05002749 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002750 }
2751 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2752 weston_desktop_surface_set_size(desktop_surface, width, height);
2753}
2754
2755static void
2756desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2757 bool maximized, void *shell)
2758{
2759 struct shell_surface *shsurf =
2760 weston_desktop_surface_get_user_data(desktop_surface);
2761
2762 set_maximized(shsurf, maximized);
2763}
2764
2765static void
2766desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2767 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002768{
2769 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002770 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002771
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002772 /* apply compositor's own minimization logic (hide) */
2773 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002774}
2775
Rafael Antognollie2a34552013-12-03 15:35:45 -02002776static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002777set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002778{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002779 struct shell_grab *grab;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002780
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002781 if (pointer->grab->interface == &busy_cursor_grab_interface)
2782 return;
2783
2784 grab = malloc(sizeof *grab);
2785 if (!grab)
2786 return;
2787
2788 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2789 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2790 /* Mark the shsurf as ungrabbed so that button binding is able
2791 * to move it. */
2792 shsurf->grabbed = 0;
2793}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002794
2795static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002796end_busy_cursor(struct weston_compositor *compositor,
2797 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002798{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002799 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002800 struct shell_grab *grab;
2801 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002802
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002803 wl_list_for_each(seat, &compositor->seat_list, link) {
2804 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2805 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002806
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002807 if (!pointer)
2808 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002809
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002810 if (pointer->grab->interface != &busy_cursor_grab_interface)
2811 continue;
2812
2813 grab = (struct shell_grab *) pointer->grab;
2814 shsurf = grab->shsurf;
2815 if (!shsurf)
2816 continue;
2817
2818 grab_client =
2819 weston_desktop_surface_get_client(shsurf->desktop_surface);
2820 if (grab_client == desktop_client) {
2821 shell_grab_end(grab);
2822 free(grab);
2823 }
2824 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002825}
2826
2827static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002828desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2829 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002830{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002831 struct shell_surface *shsurf =
2832 weston_desktop_surface_get_user_data(desktop_surface);
2833 bool *unresponsive = user_data;
2834
2835 shsurf->unresponsive = *unresponsive;
2836}
2837
2838static void
2839desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2840 void *shell_)
2841{
2842 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002843 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002844 struct weston_seat *seat;
2845 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002846
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002847 weston_desktop_client_for_each_surface(desktop_client,
2848 desktop_surface_set_unresponsive,
2849 &unresponsive);
2850
2851
2852 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2853 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2854 struct weston_desktop_client *grab_client;
2855
2856 if (!pointer || !pointer->focus)
2857 continue;
2858
2859 shsurf = get_shell_surface(pointer->focus->surface);
2860 if (!shsurf)
2861 continue;
2862
2863 grab_client =
2864 weston_desktop_surface_get_client(shsurf->desktop_surface);
2865 if (grab_client == desktop_client)
2866 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002867 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002868}
2869
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002870static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002871desktop_surface_pong(struct weston_desktop_client *desktop_client,
2872 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002873{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002874 struct desktop_shell *shell = shell_;
2875 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002876
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002877 weston_desktop_client_for_each_surface(desktop_client,
2878 desktop_surface_set_unresponsive,
2879 &unresponsive);
2880 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002881}
2882
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002883static void
2884desktop_surface_set_xwayland_position(struct weston_desktop_surface *surface,
2885 int32_t x, int32_t y, void *shell_)
2886{
2887 struct shell_surface *shsurf =
2888 weston_desktop_surface_get_user_data(surface);
2889
2890 shsurf->xwayland.x = x;
2891 shsurf->xwayland.y = y;
2892 shsurf->xwayland.is_set = true;
2893}
2894
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002895static const struct weston_desktop_api shell_desktop_api = {
2896 .struct_size = sizeof(struct weston_desktop_api),
2897 .surface_added = desktop_surface_added,
2898 .surface_removed = desktop_surface_removed,
2899 .committed = desktop_surface_committed,
2900 .move = desktop_surface_move,
2901 .resize = desktop_surface_resize,
Stefan Agnera8da2082019-06-23 14:53:59 +02002902 .set_parent = desktop_surface_set_parent,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002903 .fullscreen_requested = desktop_surface_fullscreen_requested,
2904 .maximized_requested = desktop_surface_maximized_requested,
2905 .minimized_requested = desktop_surface_minimized_requested,
2906 .ping_timeout = desktop_surface_ping_timeout,
2907 .pong = desktop_surface_pong,
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002908 .set_xwayland_position = desktop_surface_set_xwayland_position,
Rafael Antognollie2a34552013-12-03 15:35:45 -02002909};
2910
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002911/* ************************ *
2912 * end of libweston-desktop *
2913 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04002914static void
Quentin Glidice8bf9592016-06-23 18:55:20 +02002915configure_static_view(struct weston_view *ev, struct weston_layer *layer, int x, int y)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002916{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002917 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002918
Semi Malinene7a52fb2018-04-26 11:08:10 +02002919 if (!ev->output)
2920 return;
2921
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002922 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002923 if (v->output == ev->output && v != ev) {
2924 weston_view_unmap(v);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002925 v->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002926 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002927 }
2928 }
2929
Quentin Glidice8bf9592016-06-23 18:55:20 +02002930 weston_view_set_position(ev, ev->output->x + x, ev->output->y + y);
Armin Krezović4663aca2016-06-30 06:04:29 +02002931 ev->surface->is_mapped = true;
2932 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002933
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002934 if (wl_list_empty(&ev->layer_link.link)) {
2935 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002936 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002937 }
2938}
2939
David Fort50d962f2016-06-09 17:55:52 +02002940
2941static struct shell_output *
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002942find_shell_output_from_weston_output(struct desktop_shell *shell,
2943 struct weston_output *output)
David Fort50d962f2016-06-09 17:55:52 +02002944{
2945 struct shell_output *shell_output;
2946
2947 wl_list_for_each(shell_output, &shell->output_list, link) {
2948 if (shell_output->output == output)
2949 return shell_output;
2950 }
2951
2952 return NULL;
2953}
2954
Pekka Paalanen8274d902014-08-06 19:36:51 +03002955static int
2956background_get_label(struct weston_surface *surface, char *buf, size_t len)
2957{
2958 return snprintf(buf, len, "background for output %s",
Miguel A. Vico620f68d2019-09-25 11:23:13 -07002959 (surface->output ? surface->output->name : "NULL"));
Pekka Paalanen8274d902014-08-06 19:36:51 +03002960}
2961
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002962static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002963background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002964{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002965 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002966 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002967
Jason Ekstranda7af7042013-10-12 22:38:11 -05002968 view = container_of(es->views.next, struct weston_view, surface_link);
2969
Quentin Glidice8bf9592016-06-23 18:55:20 +02002970 configure_static_view(view, &shell->background_layer, 0, 0);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002971}
2972
2973static void
David Fort50d962f2016-06-09 17:55:52 +02002974handle_background_surface_destroy(struct wl_listener *listener, void *data)
2975{
2976 struct shell_output *output =
2977 container_of(listener, struct shell_output, background_surface_listener);
2978
2979 weston_log("background surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09002980 wl_list_remove(&output->background_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02002981 output->background_surface = NULL;
2982}
2983
2984static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002985desktop_shell_set_background(struct wl_client *client,
2986 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002987 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002988 struct wl_resource *surface_resource)
2989{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002990 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002991 struct weston_surface *surface =
2992 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02002993 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002994 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002995
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002996 if (surface->committed) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002997 wl_resource_post_error(surface_resource,
2998 WL_DISPLAY_ERROR_INVALID_OBJECT,
2999 "surface role already assigned");
3000 return;
3001 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003002
Jason Ekstranda7af7042013-10-12 22:38:11 -05003003 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3004 weston_view_destroy(view);
3005 view = weston_view_create(surface);
3006
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003007 surface->committed = background_committed;
3008 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003009 weston_surface_set_label_func(surface, background_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03003010 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02003011 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02003012
3013 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02003014 if (sh_output->background_surface) {
3015 /* The output already has a background, tell our helper
3016 * there is no need for another one. */
3017 weston_desktop_shell_send_configure(resource, 0,
3018 surface_resource,
3019 0, 0);
3020 } else {
3021 weston_desktop_shell_send_configure(resource, 0,
3022 surface_resource,
3023 surface->output->width,
3024 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003025
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02003026 sh_output->background_surface = surface;
3027
3028 sh_output->background_surface_listener.notify =
3029 handle_background_surface_destroy;
3030 wl_signal_add(&surface->destroy_signal,
3031 &sh_output->background_surface_listener);
3032 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003033}
3034
Pekka Paalanen8274d902014-08-06 19:36:51 +03003035static int
3036panel_get_label(struct weston_surface *surface, char *buf, size_t len)
3037{
3038 return snprintf(buf, len, "panel for output %s",
Miguel A. Vico620f68d2019-09-25 11:23:13 -07003039 (surface->output ? surface->output->name : "NULL"));
Pekka Paalanen8274d902014-08-06 19:36:51 +03003040}
3041
Kristian Høgsberg75840622011-09-06 13:48:16 -04003042static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003043panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003044{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003045 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003046 struct weston_view *view;
Quentin Glidice8bf9592016-06-23 18:55:20 +02003047 int width, height;
3048 int x = 0, y = 0;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003049
Jason Ekstranda7af7042013-10-12 22:38:11 -05003050 view = container_of(es->views.next, struct weston_view, surface_link);
3051
Quentin Glidice8bf9592016-06-23 18:55:20 +02003052 get_panel_size(shell, view, &width, &height);
3053 switch (shell->panel_position) {
3054 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
3055 break;
3056 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
3057 y = view->output->height - height;
3058 break;
3059 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
3060 break;
3061 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
3062 x = view->output->width - width;
3063 break;
3064 }
3065
3066 configure_static_view(view, &shell->panel_layer, x, y);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003067}
3068
3069static void
David Fort50d962f2016-06-09 17:55:52 +02003070handle_panel_surface_destroy(struct wl_listener *listener, void *data)
3071{
3072 struct shell_output *output =
3073 container_of(listener, struct shell_output, panel_surface_listener);
3074
3075 weston_log("panel surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09003076 wl_list_remove(&output->panel_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02003077 output->panel_surface = NULL;
3078}
3079
3080
3081static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003082desktop_shell_set_panel(struct wl_client *client,
3083 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003084 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003085 struct wl_resource *surface_resource)
3086{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003087 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003088 struct weston_surface *surface =
3089 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003090 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02003091 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003092
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003093 if (surface->committed) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003094 wl_resource_post_error(surface_resource,
3095 WL_DISPLAY_ERROR_INVALID_OBJECT,
3096 "surface role already assigned");
3097 return;
3098 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003099
Jason Ekstranda7af7042013-10-12 22:38:11 -05003100 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3101 weston_view_destroy(view);
3102 view = weston_view_create(surface);
3103
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003104 surface->committed = panel_committed;
3105 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003106 weston_surface_set_label_func(surface, panel_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03003107 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02003108 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02003109
3110 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003111 if (sh_output->panel_surface) {
3112 /* The output already has a panel, tell our helper
3113 * there is no need for another one. */
3114 weston_desktop_shell_send_configure(resource, 0,
3115 surface_resource,
3116 0, 0);
3117 } else {
3118 weston_desktop_shell_send_configure(resource, 0,
3119 surface_resource,
3120 surface->output->width,
3121 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003122
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003123 sh_output->panel_surface = surface;
3124
3125 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
3126 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
3127 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003128}
3129
Pekka Paalanen8274d902014-08-06 19:36:51 +03003130static int
3131lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
3132{
3133 return snprintf(buf, len, "lock window");
3134}
3135
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003136static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003137lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003138{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003139 struct desktop_shell *shell = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003140 struct weston_view *view;
3141
3142 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003143
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003144 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003145 return;
3146
Jason Ekstranda7af7042013-10-12 22:38:11 -05003147 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003148
3149 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003150 weston_layer_entry_insert(&shell->lock_layer.view_list,
3151 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003152 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003153 surface->is_mapped = true;
3154 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003155 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003156 }
3157}
3158
3159static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003160handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003161{
Tiago Vignattibe143262012-04-16 17:31:41 +03003162 struct desktop_shell *shell =
3163 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003164
Martin Minarik6d118362012-06-07 18:01:59 +02003165 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003166 shell->lock_surface = NULL;
3167}
3168
3169static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003170desktop_shell_set_lock_surface(struct wl_client *client,
3171 struct wl_resource *resource,
3172 struct wl_resource *surface_resource)
3173{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003174 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003175 struct weston_surface *surface =
3176 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003177
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003178 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003179
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003180 if (!shell->locked)
3181 return;
3182
Pekka Paalanen98262232011-12-01 10:42:22 +02003183 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003184
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003185 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003186 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003187 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003188
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003189 weston_view_create(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003190 surface->committed = lock_surface_committed;
3191 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003192 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003193}
3194
3195static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003196resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003197{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003198 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003199
Quentin Glidic82681572016-12-17 13:40:51 +01003200 weston_layer_unset_position(&shell->lock_layer);
3201
3202 if (shell->showing_input_panels)
3203 weston_layer_set_position(&shell->input_panel_layer,
3204 WESTON_LAYER_POSITION_TOP_UI);
3205 weston_layer_set_position(&shell->fullscreen_layer,
3206 WESTON_LAYER_POSITION_FULLSCREEN);
3207 weston_layer_set_position(&shell->panel_layer,
3208 WESTON_LAYER_POSITION_UI);
3209 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003210
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003211 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003212
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003213 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003214 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003215 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003216}
3217
3218static void
3219desktop_shell_unlock(struct wl_client *client,
3220 struct wl_resource *resource)
3221{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003222 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003223
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003224 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003225
3226 if (shell->locked)
3227 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003228}
3229
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003230static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003231desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003232 struct wl_resource *resource,
3233 struct wl_resource *surface_resource)
3234{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003235 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003236
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003237 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003238 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003239}
3240
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003241static void
3242desktop_shell_desktop_ready(struct wl_client *client,
3243 struct wl_resource *resource)
3244{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003245 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003246
3247 shell_fade_startup(shell);
3248}
3249
Jonny Lamb765760d2014-08-20 15:53:19 +02003250static void
3251desktop_shell_set_panel_position(struct wl_client *client,
3252 struct wl_resource *resource,
3253 uint32_t position)
3254{
3255 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3256
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003257 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3258 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3259 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3260 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02003261 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003262 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02003263 "bad position argument");
3264 return;
3265 }
3266
3267 shell->panel_position = position;
3268}
3269
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003270static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04003271 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003272 desktop_shell_set_panel,
3273 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003274 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003275 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02003276 desktop_shell_desktop_ready,
3277 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04003278};
3279
3280static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003281move_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003282 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003283{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003284 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003285 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003286 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003287
Derek Foreman8ae2db52015-07-15 13:00:36 -05003288 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003289 return;
3290
Derek Foreman8ae2db52015-07-15 13:00:36 -05003291 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003292
Pekka Paalanen01388e22013-04-25 13:57:44 +03003293 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003294 if (surface == NULL)
3295 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003296
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003297 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003298 if (shsurf == NULL ||
3299 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3300 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003301 return;
3302
Derek Foreman794fa0e2015-07-15 13:00:38 -05003303 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003304}
3305
3306static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003307maximize_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003308 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003309{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003310 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003311 struct weston_surface *surface;
3312 struct shell_surface *shsurf;
3313
3314 surface = weston_surface_get_main_surface(focus);
3315 if (surface == NULL)
3316 return;
3317
3318 shsurf = get_shell_surface(surface);
3319 if (shsurf == NULL)
3320 return;
3321
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003322 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003323}
3324
3325static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003326fullscreen_binding(struct weston_keyboard *keyboard,
3327 const struct timespec *time, uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003328{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003329 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003330 struct weston_surface *surface;
3331 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003332 bool fullscreen;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003333
3334 surface = weston_surface_get_main_surface(focus);
3335 if (surface == NULL)
3336 return;
3337
3338 shsurf = get_shell_surface(surface);
3339 if (shsurf == NULL)
3340 return;
3341
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003342 fullscreen =
3343 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003344
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003345 set_fullscreen(shsurf, !fullscreen, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003346}
3347
3348static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003349touch_move_binding(struct weston_touch *touch, const struct timespec *time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01003350{
Derek Foreman362656b2014-09-04 10:23:05 -05003351 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01003352 struct weston_surface *surface;
3353 struct shell_surface *shsurf;
3354
Derek Foreman8ae2db52015-07-15 13:00:36 -05003355 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05003356 return;
3357
Derek Foreman8ae2db52015-07-15 13:00:36 -05003358 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003359 surface = weston_surface_get_main_surface(focus);
3360 if (surface == NULL)
3361 return;
3362
3363 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003364 if (shsurf == NULL ||
3365 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3366 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Neil Robertsaba0f252013-10-03 16:43:05 +01003367 return;
3368
Derek Foremanb7674ae2015-07-15 13:00:37 -05003369 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01003370}
3371
3372static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003373resize_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003374 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003375{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003376 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003377 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003378 uint32_t edges = 0;
3379 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003380 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003381
Derek Foreman8ae2db52015-07-15 13:00:36 -05003382 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003383 return;
3384
Derek Foreman8ae2db52015-07-15 13:00:36 -05003385 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003386
Pekka Paalanen01388e22013-04-25 13:57:44 +03003387 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003388 if (surface == NULL)
3389 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003390
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003391 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003392 if (shsurf == NULL ||
3393 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3394 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003395 return;
3396
Jason Ekstranda7af7042013-10-12 22:38:11 -05003397 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003398 wl_fixed_to_int(pointer->grab_x),
3399 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05003400 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003401
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003402 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003403 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003404 else if (x < 2 * surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003405 edges |= 0;
3406 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003407 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003408
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003409 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003410 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003411 else if (y < 2 * surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003412 edges |= 0;
3413 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003414 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003415
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003416 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003417}
3418
3419static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003420surface_opacity_binding(struct weston_pointer *pointer,
3421 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003422 struct weston_pointer_axis_event *event,
3423 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003424{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003425 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003426 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003427 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003428 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003429
Pekka Paalanen01388e22013-04-25 13:57:44 +03003430 /* XXX: broken for windows containing sub-surfaces */
3431 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003432 if (surface == NULL)
3433 return;
3434
3435 shsurf = get_shell_surface(surface);
3436 if (!shsurf)
3437 return;
3438
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003439 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003440
Jason Ekstranda7af7042013-10-12 22:38:11 -05003441 if (shsurf->view->alpha > 1.0)
3442 shsurf->view->alpha = 1.0;
3443 if (shsurf->view->alpha < step)
3444 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003445
Jason Ekstranda7af7042013-10-12 22:38:11 -05003446 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003447 weston_surface_damage(surface);
3448}
3449
3450static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003451do_zoom(struct weston_seat *seat, const struct timespec *time, uint32_t key,
3452 uint32_t axis, double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003453{
Derek Foreman8aeeac82015-01-30 13:24:36 -06003454 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05003455 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003456 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003457 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003458
Derek Foreman1281a362015-07-31 16:55:32 -05003459 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06003460 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3461 return;
3462 }
3463
Scott Moreauccbf29d2012-02-22 14:21:41 -07003464 wl_list_for_each(output, &compositor->output_list, link) {
3465 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05003466 wl_fixed_to_double(pointer->x),
3467 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003468 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003469 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003470 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003471 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003472 increment = -output->zoom.increment;
3473 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003474 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003475 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003476 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003477 else
3478 increment = 0;
3479
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003480 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003481
Scott Moreaue6603982012-06-11 13:07:51 -06003482 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003483 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003484 else if (output->zoom.level > output->zoom.max_level)
3485 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05003486
3487 if (!output->zoom.active) {
3488 if (output->zoom.level <= 0.0)
3489 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05003490 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003491 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003492
Scott Moreaue6603982012-06-11 13:07:51 -06003493 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003494
Jason Ekstranda7af7042013-10-12 22:38:11 -05003495 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003496 }
3497 }
3498}
3499
Scott Moreauccbf29d2012-02-22 14:21:41 -07003500static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003501zoom_axis_binding(struct weston_pointer *pointer, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003502 struct weston_pointer_axis_event *event,
3503 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003504{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003505 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003506}
3507
3508static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003509zoom_key_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003510 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003511{
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003512 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003513}
3514
3515static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003516terminate_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003517 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003518{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003519 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003520
Pekka Paalanen052032d2019-02-06 10:44:37 +02003521 weston_compositor_exit(compositor);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003522}
3523
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003524static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003525rotate_grab_motion(struct weston_pointer_grab *grab,
3526 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003527 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003528{
3529 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003530 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003531 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003532 struct shell_surface *shsurf = rotate->base.shsurf;
Sergey Bugaev14ef2012019-02-11 22:55:09 +03003533 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003534 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003535
Jonas Ådahld2510102014-10-05 21:39:14 +02003536 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003537
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003538 if (!shsurf)
3539 return;
3540
Sergey Bugaev14ef2012019-02-11 22:55:09 +03003541 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
3542
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003543 cx = 0.5f * surface->width;
3544 cy = 0.5f * surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003545
Daniel Stone37816df2012-05-16 18:45:18 +01003546 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3547 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003548 r = sqrtf(dx * dx + dy * dy);
3549
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003550 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003551 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003552
3553 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003554 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003555 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003556
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003557 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003558 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003559
3560 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003561 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003562 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003563 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003564 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003565
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003566 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003567 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003568 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003569 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003570 wl_list_init(&shsurf->rotation.transform.link);
3571 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003572 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003573 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003574
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003575 /* We need to adjust the position of the surface
3576 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003577 cposx = shsurf->view->geometry.x + cx;
3578 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003579 dposx = rotate->center.x - cposx;
3580 dposy = rotate->center.y - cposy;
3581 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003582 weston_view_set_position(shsurf->view,
3583 shsurf->view->geometry.x + dposx,
3584 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003585 }
3586
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003587 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003588 * lazily applies the damage due to rotation update.
3589 */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003590 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003591}
3592
3593static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003594rotate_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003595 const struct timespec *time,
3596 uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003597{
3598 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003599 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003600 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003601 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003602 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003603
Daniel Stone4dbadb12012-05-30 16:31:51 +01003604 if (pointer->button_count == 0 &&
3605 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003606 if (shsurf)
3607 weston_matrix_multiply(&shsurf->rotation.rotation,
3608 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003609 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003610 free(rotate);
3611 }
3612}
3613
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003614static void
3615rotate_grab_cancel(struct weston_pointer_grab *grab)
3616{
3617 struct rotate_grab *rotate =
3618 container_of(grab, struct rotate_grab, base.grab);
3619
3620 shell_grab_end(&rotate->base);
3621 free(rotate);
3622}
3623
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003624static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003625 noop_grab_focus,
3626 rotate_grab_motion,
3627 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003628 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003629 noop_grab_axis_source,
3630 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003631 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003632};
3633
3634static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003635surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003636{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003637 struct weston_surface *surface =
3638 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003639 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003640 float dx, dy;
3641 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003642
Pekka Paalanen460099f2012-01-20 16:48:25 +02003643 rotate = malloc(sizeof *rotate);
3644 if (!rotate)
3645 return;
3646
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003647 weston_view_to_global_float(shsurf->view,
3648 surface->width * 0.5f,
3649 surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003650 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003651
Derek Foreman74de4692015-07-15 13:00:39 -05003652 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3653 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003654 r = sqrtf(dx * dx + dy * dy);
3655 if (r > 20.0f) {
3656 struct weston_matrix inverse;
3657
3658 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003659 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003660 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003661
3662 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003663 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003664 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003665 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003666 weston_matrix_init(&rotate->rotation);
3667 }
3668
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003669 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003670 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003671}
3672
3673static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003674rotate_binding(struct weston_pointer *pointer, const struct timespec *time,
3675 uint32_t button, void *data)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003676{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003677 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003678 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003679 struct shell_surface *surface;
3680
Derek Foreman8ae2db52015-07-15 13:00:36 -05003681 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003682 return;
3683
Derek Foreman8ae2db52015-07-15 13:00:36 -05003684 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003685
Pekka Paalanen01388e22013-04-25 13:57:44 +03003686 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003687 if (base_surface == NULL)
3688 return;
3689
3690 surface = get_shell_surface(base_surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003691 if (surface == NULL ||
3692 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3693 weston_desktop_surface_get_maximized(surface->desktop_surface))
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003694 return;
3695
Derek Foreman74de4692015-07-15 13:00:39 -05003696 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003697}
3698
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003699/* Move all fullscreen layers down to the current workspace and hide their
3700 * black views. The surfaces' state is set to both fullscreen and lowered,
3701 * and this is reversed when such a surface is re-configured, see
3702 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3703 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003704 * lowering_output = NULL - Lower on all outputs, else only lower on the
3705 * specified output.
3706 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003707 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003708 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003709void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003710lower_fullscreen_layer(struct desktop_shell *shell,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003711 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003712{
3713 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003714 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003715
3716 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003717 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003718 &shell->fullscreen_layer.view_list.link,
3719 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003720 struct shell_surface *shsurf = get_shell_surface(view->surface);
3721
3722 if (!shsurf)
3723 continue;
3724
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003725 /* Only lower surfaces which have lowering_output as their fullscreen
3726 * output, unless a NULL output asks for lowering on all outputs.
3727 */
3728 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3729 continue;
3730
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003731 /* We can have a non-fullscreen popup for a fullscreen surface
3732 * in the fullscreen layer. */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003733 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003734 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003735 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3736 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07003737 weston_view_damage_below(shsurf->fullscreen.black_view);
3738
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003739 }
3740
3741 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003742 weston_layer_entry_remove(&view->layer_link);
3743 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003744 weston_view_damage_below(view);
3745 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003746
3747 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003748 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003749}
3750
Stefan Agnera8da2082019-06-23 14:53:59 +02003751static struct shell_surface *get_last_child(struct shell_surface *shsurf)
3752{
3753 struct shell_surface *shsurf_child;
3754
3755 wl_list_for_each_reverse(shsurf_child, &shsurf->children_list, children_link) {
3756 if (weston_view_is_mapped(shsurf_child->view))
3757 return shsurf_child;
3758 }
3759
3760 return NULL;
3761}
3762
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003763void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003764activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003765 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003766{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003767 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003768 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003769 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003770 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003771 struct weston_surface *old_es;
Stefan Agnera8da2082019-06-23 14:53:59 +02003772 struct shell_surface *shsurf, *shsurf_child;
Marius Vladf12697b2021-03-05 21:44:26 +02003773 struct shell_seat *shseat = get_shell_seat(seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003774
Pekka Paalanen01388e22013-04-25 13:57:44 +03003775 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003776 shsurf = get_shell_surface(main_surface);
3777 assert(shsurf);
3778
Stefan Agnera8da2082019-06-23 14:53:59 +02003779 shsurf_child = get_last_child(shsurf);
3780 if (shsurf_child) {
3781 /* Activate last xdg child instead of parent. */
3782 activate(shell, shsurf_child->view, seat, flags);
3783 return;
3784 }
3785
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003786 /* Only demote fullscreen surfaces on the output of activated shsurf.
3787 * Leave fullscreen surfaces on unrelated outputs alone. */
Pekka Paalanen87860c22018-05-02 10:21:57 +02003788 if (shsurf->output)
3789 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003790
Marius Vladd6ccc8b2021-03-05 22:07:30 +02003791 weston_view_activate_input(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003792
Marius Vladf12697b2021-03-05 21:44:26 +02003793 if (shseat->focused_surface) {
3794 struct shell_surface *current_focus =
3795 get_shell_surface(shseat->focused_surface);
3796 assert(current_focus);
3797 shell_surface_deactivate(current_focus);
3798 }
3799
3800 shseat->focused_surface = main_surface;
3801 shell_surface_activate(shsurf);
3802
Jonas Ådahl8538b222012-08-29 22:13:03 +02003803 state = ensure_focus_state(shell, seat);
3804 if (state == NULL)
3805 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003806
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003807 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08003808 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003809
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003810 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3811 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02003812 shell_configure_fullscreen(shsurf);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003813
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01003814 /* Update the surface’s layer. This brings it to the top of the stacking
3815 * order as appropriate. */
3816 shell_surface_update_layer(shsurf);
3817
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003818 if (shell->focus_animation_type != ANIMATION_NONE) {
3819 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003820 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003821 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003822}
3823
Alex Wu21858432012-04-01 20:13:08 +08003824/* no-op func for checking black surface */
3825static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003826black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08003827{
3828}
3829
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003830static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003831is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08003832{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003833 struct weston_surface *surface = view->surface;
3834
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003835 if (surface->committed == black_surface_committed) {
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003836 if (fs_view)
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003837 *fs_view = surface->committed_private;
Alex Wu21858432012-04-01 20:13:08 +08003838 return true;
3839 }
3840 return false;
3841}
3842
Kristian Høgsberg75840622011-09-06 13:48:16 -04003843static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003844activate_binding(struct weston_seat *seat,
3845 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003846 struct weston_view *focus_view,
3847 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003848{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003849 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003850 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003851
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003852 if (!focus_view)
3853 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003854
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003855 if (is_black_surface_view(focus_view, &main_view))
3856 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08003857
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003858 main_surface = weston_surface_get_main_surface(focus_view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003859 if (!get_shell_surface(main_surface))
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003860 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003861
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003862 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01003863}
3864
3865static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003866click_to_activate_binding(struct weston_pointer *pointer,
3867 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003868 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003869{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003870 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003871 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003872 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08003873 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003874
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003875 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003876 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003877 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01003878}
3879
3880static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003881touch_to_activate_binding(struct weston_touch *touch,
3882 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003883 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003884{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003885 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003886 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003887 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08003888 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003889
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003890 activate_binding(touch->seat, data, touch->focus,
3891 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003892}
3893
3894static void
Neil Robertsb4a91702014-04-09 16:33:32 +01003895unfocus_all_seats(struct desktop_shell *shell)
3896{
3897 struct weston_seat *seat, *next;
3898
3899 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003900 struct weston_keyboard *keyboard =
3901 weston_seat_get_keyboard(seat);
3902
3903 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01003904 continue;
3905
Derek Foreman1281a362015-07-31 16:55:32 -05003906 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01003907 }
3908}
3909
3910static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003911lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003912{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003913 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003914
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003915 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003916 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003917 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003918 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003919
3920 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003921
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003922 /* Hide all surfaces by removing the fullscreen, panel and
3923 * toplevel layers. This way nothing else can show or receive
3924 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003925
Quentin Glidic82681572016-12-17 13:40:51 +01003926 weston_layer_unset_position(&shell->panel_layer);
3927 weston_layer_unset_position(&shell->fullscreen_layer);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003928 if (shell->showing_input_panels)
Quentin Glidic82681572016-12-17 13:40:51 +01003929 weston_layer_unset_position(&shell->input_panel_layer);
3930 weston_layer_unset_position(&ws->layer);
3931
3932 weston_layer_set_position(&shell->lock_layer,
3933 WESTON_LAYER_POSITION_LOCK);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003934
Derek Foreman004b4a12015-07-20 16:28:13 -05003935 weston_compositor_sleep(shell->compositor);
3936
Neil Robertsb4a91702014-04-09 16:33:32 +01003937 /* Remove the keyboard focus on all seats. This will be
3938 * restored to the workspace's saved state via
3939 * restore_focus_state when the compositor is unlocked */
3940 unfocus_all_seats(shell);
3941
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003942 /* TODO: disable bindings that should not work while locked. */
3943
3944 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003945}
3946
3947static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003948unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003949{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003950 struct wl_resource *shell_resource;
3951
Pekka Paalanend81c2162011-11-16 13:47:34 +02003952 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003953 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003954 return;
3955 }
3956
3957 /* If desktop-shell client has gone away, unlock immediately. */
3958 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003959 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003960 return;
3961 }
3962
3963 if (shell->prepare_event_sent)
3964 return;
3965
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003966 shell_resource = shell->child.desktop_shell;
3967 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003968 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003969}
3970
3971static void
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003972shell_fade_done_for_output(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003973{
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003974 struct shell_output *shell_output = data;
3975 struct desktop_shell *shell = shell_output->shell;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003976
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003977 shell_output->fade.animation = NULL;
3978 switch (shell_output->fade.type) {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003979 case FADE_IN:
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003980 weston_surface_destroy(shell_output->fade.view->surface);
3981 shell_output->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003982 break;
3983 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003984 lock(shell);
3985 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00003986 default:
3987 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003988 }
3989}
3990
Jason Ekstranda7af7042013-10-12 22:38:11 -05003991static struct weston_view *
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003992shell_fade_create_surface_for_output(struct desktop_shell *shell, struct shell_output *shell_output)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003993{
3994 struct weston_compositor *compositor = shell->compositor;
3995 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003996 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003997
3998 surface = weston_surface_create(compositor);
3999 if (!surface)
4000 return NULL;
4001
Jason Ekstranda7af7042013-10-12 22:38:11 -05004002 view = weston_view_create(surface);
4003 if (!view) {
4004 weston_surface_destroy(surface);
4005 return NULL;
4006 }
4007
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004008 weston_surface_set_size(surface, shell_output->output->width, shell_output->output->height);
4009 weston_view_set_position(view, shell_output->output->x, shell_output->output->y);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004010 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004011 weston_layer_entry_insert(&compositor->fade_layer.view_list,
4012 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004013 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02004014 surface->is_mapped = true;
4015 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004016
Jason Ekstranda7af7042013-10-12 22:38:11 -05004017 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004018}
4019
4020static void
4021shell_fade(struct desktop_shell *shell, enum fade_type type)
4022{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004023 float tint;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004024 struct shell_output *shell_output;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004025
4026 switch (type) {
4027 case FADE_IN:
4028 tint = 0.0;
4029 break;
4030 case FADE_OUT:
4031 tint = 1.0;
4032 break;
4033 default:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07004034 weston_log("shell: invalid fade type\n");
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004035 return;
4036 }
4037
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004038 /* Create a separate fade surface for each output */
4039 wl_list_for_each(shell_output, &shell->output_list, link) {
4040 shell_output->fade.type = type;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004041
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004042 if (shell_output->fade.view == NULL) {
4043 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4044 if (!shell_output->fade.view)
4045 continue;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004046
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004047 shell_output->fade.view->alpha = 1.0 - tint;
4048 weston_view_update_transform(shell_output->fade.view);
4049 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004050
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004051 if (shell_output->fade.view->output == NULL) {
4052 /* If the black view gets a NULL output, we lost the
4053 * last output and we'll just cancel the fade. This
4054 * happens when you close the last window under the
4055 * X11 or Wayland backends. */
4056 shell->locked = false;
4057 weston_surface_destroy(shell_output->fade.view->surface);
4058 shell_output->fade.view = NULL;
4059 } else if (shell_output->fade.animation) {
4060 weston_fade_update(shell_output->fade.animation, tint);
4061 } else {
4062 shell_output->fade.animation =
4063 weston_fade_run(shell_output->fade.view,
4064 1.0 - tint, tint, 300.0,
4065 shell_fade_done_for_output, shell_output);
4066 }
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004067 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004068}
4069
4070static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004071do_shell_fade_startup(void *data)
4072{
4073 struct desktop_shell *shell = data;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004074 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004075
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004076 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004077 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004078 } else {
4079 weston_log("desktop shell: "
4080 "unexpected fade-in animation type %d\n",
4081 shell->startup_animation_type);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004082 wl_list_for_each(shell_output, &shell->output_list, link) {
4083 weston_surface_destroy(shell_output->fade.view->surface);
4084 shell_output->fade.view = NULL;
4085 }
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004086 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004087}
4088
4089static void
4090shell_fade_startup(struct desktop_shell *shell)
4091{
4092 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004093 struct shell_output *shell_output;
4094 bool has_fade = false;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004095
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004096 wl_list_for_each(shell_output, &shell->output_list, link) {
4097 if (!shell_output->fade.startup_timer)
4098 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004099
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004100 wl_event_source_remove(shell_output->fade.startup_timer);
4101 shell_output->fade.startup_timer = NULL;
4102 has_fade = true;
4103 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004104
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004105 if (has_fade) {
4106 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4107 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4108 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004109}
4110
4111static int
4112fade_startup_timeout(void *data)
4113{
4114 struct desktop_shell *shell = data;
4115
4116 shell_fade_startup(shell);
4117 return 0;
4118}
4119
4120static void
4121shell_fade_init(struct desktop_shell *shell)
4122{
4123 /* Make compositor output all black, and wait for the desktop-shell
4124 * client to signal it is ready, then fade in. The timer triggers a
4125 * fade-in, in case the desktop-shell client takes too long.
4126 */
4127
4128 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004129 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004130
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004131 if (shell->startup_animation_type == ANIMATION_NONE)
4132 return;
4133
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004134 wl_list_for_each(shell_output, &shell->output_list, link) {
4135 if (shell_output->fade.view != NULL) {
4136 weston_log("%s: warning: fade surface already exists\n",
4137 __func__);
4138 continue;
4139 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004140
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004141 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4142 if (!shell_output->fade.view)
4143 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004144
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004145 weston_view_update_transform(shell_output->fade.view);
4146 weston_surface_damage(shell_output->fade.view->surface);
4147
4148 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4149 shell_output->fade.startup_timer =
4150 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4151 wl_event_source_timer_update(shell_output->fade.startup_timer, 15000);
4152 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004153}
4154
4155static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004156idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004157{
4158 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004159 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004160
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004161 struct weston_seat *seat;
4162
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004163 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4164 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004165
4166 shell_fade(shell, FADE_OUT);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004167 /* lock() is called from shell_fade_done_for_output() */
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004168}
4169
4170static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004171wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004172{
4173 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004174 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004175
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004176 unlock(shell);
4177}
4178
4179static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004180transform_handler(struct wl_listener *listener, void *data)
4181{
4182 struct weston_surface *surface = data;
4183 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004184 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004185 int x, y;
4186
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004187 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004188 return;
4189
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004190 api = shsurf->shell->xwayland_surface_api;
4191 if (!api) {
4192 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
4193 shsurf->shell->xwayland_surface_api = api;
4194 }
4195
4196 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004197 return;
4198
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004199 if (!weston_view_is_mapped(shsurf->view))
4200 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004201
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004202 x = shsurf->view->geometry.x;
4203 y = shsurf->view->geometry.y;
4204
4205 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004206}
4207
4208static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004209weston_view_set_initial_position(struct weston_view *view,
4210 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004211{
4212 struct weston_compositor *compositor = shell->compositor;
4213 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004214 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004215 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004216 struct weston_output *output, *target_output = NULL;
4217 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004218 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004219
4220 /* As a heuristic place the new window on the same output as the
4221 * pointer. Falling back to the output containing 0, 0.
4222 *
4223 * TODO: Do something clever for touch too?
4224 */
4225 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004226 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4227
4228 if (pointer) {
4229 ix = wl_fixed_to_int(pointer->x);
4230 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004231 break;
4232 }
4233 }
4234
4235 wl_list_for_each(output, &compositor->output_list, link) {
4236 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4237 target_output = output;
4238 break;
4239 }
4240 }
4241
4242 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004243 weston_view_set_position(view, 10 + random() % 400,
4244 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004245 return;
4246 }
4247
4248 /* Valid range within output where the surface will still be onscreen.
4249 * If this is negative it means that the surface is bigger than
4250 * output.
4251 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004252 get_output_work_area(shell, target_output, &area);
4253
Derek Foremanf814c5d2015-04-15 12:23:54 -05004254 x = area.x;
4255 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004256 range_x = area.width - view->surface->width;
4257 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004258
Rob Bradford4cb88c72012-08-13 15:18:44 +01004259 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004260 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004261
4262 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004263 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004264
Jason Ekstranda7af7042013-10-12 22:38:11 -05004265 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004266}
4267
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004268static bool
4269check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4270{
4271 struct timespec now;
4272
4273 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4274 return false;
4275
4276 /*
4277 * If the shell helper client dies before the session has been
4278 * up for roughly 30 seconds, better just make Weston shut down,
4279 * because the user likely has no way to interact with the desktop
4280 * anyway.
4281 */
4282 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4283 weston_log("Error: %s apparently cannot run at all.\n",
4284 shell->client);
4285 weston_log_continue(STAMP_SPACE "Quitting...");
Pekka Paalanen052032d2019-02-06 10:44:37 +02004286 weston_compositor_exit_with_code(shell->compositor,
4287 EXIT_FAILURE);
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004288
4289 return true;
4290 }
4291
4292 return false;
4293}
4294
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004295static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004296
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004297static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004298respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004299{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004300 struct timespec time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004301
4302 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004303 weston_compositor_get_time(&time);
4304 if (timespec_sub_to_msec(&time, &shell->child.deathstamp) > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004305 shell->child.deathstamp = time;
4306 shell->child.deathcount = 0;
4307 }
4308
4309 shell->child.deathcount++;
4310 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004311 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004312 return;
4313 }
4314
Pekka Paalanen826dc142014-08-27 12:11:53 +03004315 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004316 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004317}
4318
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004319static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004320desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4321{
4322 struct desktop_shell *shell;
4323
4324 shell = container_of(listener, struct desktop_shell,
4325 child.client_destroy_listener);
4326
Pekka Paalanen826dc142014-08-27 12:11:53 +03004327 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004328 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004329 /*
4330 * unbind_desktop_shell() will reset shell->child.desktop_shell
4331 * before the respawned process has a chance to create a new
4332 * desktop_shell object, because we are being called from the
4333 * wl_client destructor which destroys all wl_resources before
4334 * returning.
4335 */
4336
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004337 if (!check_desktop_shell_crash_too_early(shell))
4338 respawn_desktop_shell_process(shell);
4339
Pekka Paalanen826dc142014-08-27 12:11:53 +03004340 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004341}
4342
4343static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004344launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004345{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004346 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004347
Pekka Paalanen826dc142014-08-27 12:11:53 +03004348 shell->child.client = weston_client_start(shell->compositor,
4349 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004350
Arnaud Vrac42631192014-08-25 20:56:46 +02004351 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004352 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004353 return;
4354 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004355
4356 shell->child.client_destroy_listener.notify =
4357 desktop_shell_client_destroy;
4358 wl_client_add_destroy_listener(shell->child.client,
4359 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004360}
4361
4362static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004363unbind_desktop_shell(struct wl_resource *resource)
4364{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004365 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004366
4367 if (shell->locked)
4368 resume_desktop(shell);
4369
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004370 shell->child.desktop_shell = NULL;
4371 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004372}
4373
4374static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004375bind_desktop_shell(struct wl_client *client,
4376 void *data, uint32_t version, uint32_t id)
4377{
Tiago Vignattibe143262012-04-16 17:31:41 +03004378 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004379 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004380
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004381 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4382 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004383
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004384 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004385 wl_resource_set_implementation(resource,
4386 &desktop_shell_implementation,
4387 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004388 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004389 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004390 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004391
4392 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4393 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004394}
4395
Kristian Høgsberg07045392012-02-19 18:52:44 -05004396struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004397 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004398 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004399 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004400 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004401 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004402};
4403
4404static void
4405switcher_next(struct switcher *switcher)
4406{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004407 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004408 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004409 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004410 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004411
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004412 /* temporary re-display minimized surfaces */
4413 struct weston_view *tmp;
4414 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004415 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4416 weston_layer_entry_remove(&view->layer_link);
4417 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004418 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4419 *minimized = view;
4420 }
4421
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004422 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004423 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004424 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004425 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004426 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004427 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004428 next = view;
4429 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004430 view->alpha = 0.25;
4431 weston_view_geometry_dirty(view);
4432 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004433 }
Alex Wu1659daa2012-04-01 20:13:09 +08004434
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004435 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004436 view->alpha = 0.25;
4437 weston_view_geometry_dirty(view);
4438 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004439 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004440 }
4441
4442 if (next == NULL)
4443 next = first;
4444
Alex Wu07b26062012-03-12 16:06:01 +08004445 if (next == NULL)
4446 return;
4447
Kristian Høgsberg07045392012-02-19 18:52:44 -05004448 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004449 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004450
4451 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004452 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004453 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004454
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004455 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004456 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004457 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004458}
4459
4460static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004461switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004462{
4463 struct switcher *switcher =
4464 container_of(listener, struct switcher, listener);
4465
4466 switcher_next(switcher);
4467}
4468
4469static void
Daniel Stone351eb612012-05-31 15:27:47 -04004470switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004471{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004472 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004473 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004474 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004475
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004476 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004477 if (is_focus_view(view))
4478 continue;
4479
Jason Ekstranda7af7042013-10-12 22:38:11 -05004480 view->alpha = 1.0;
4481 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004482 }
4483
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004484 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004485 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004486 keyboard->seat,
4487 WESTON_ACTIVATE_FLAG_CONFIGURE);
4488 }
4489
Kristian Høgsberg07045392012-02-19 18:52:44 -05004490 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004491 weston_keyboard_end_grab(keyboard);
4492 if (keyboard->input_method_resource)
4493 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004494
4495 /* re-hide surfaces that were temporary shown during the switch */
4496 struct weston_view **minimized;
4497 wl_array_for_each(minimized, &switcher->minimized_array) {
4498 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004499 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004500 weston_layer_entry_remove(&(*minimized)->layer_link);
4501 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004502 weston_view_damage_below(*minimized);
4503 }
4504 }
4505 wl_array_release(&switcher->minimized_array);
4506
Kristian Høgsberg07045392012-02-19 18:52:44 -05004507 free(switcher);
4508}
4509
4510static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004511switcher_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004512 const struct timespec *time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004513{
4514 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004515 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004516
Daniel Stonec9785ea2012-05-30 16:31:52 +01004517 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004518 switcher_next(switcher);
4519}
4520
4521static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004522switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004523 uint32_t mods_depressed, uint32_t mods_latched,
4524 uint32_t mods_locked, uint32_t group)
4525{
4526 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004527 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004528
Daniel Stone351eb612012-05-31 15:27:47 -04004529 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4530 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004531}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004532
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004533static void
4534switcher_cancel(struct weston_keyboard_grab *grab)
4535{
4536 struct switcher *switcher = container_of(grab, struct switcher, grab);
4537
4538 switcher_destroy(switcher);
4539}
4540
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004541static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004542 switcher_key,
4543 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004544 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004545};
4546
4547static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004548switcher_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004549 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004550{
Tiago Vignattibe143262012-04-16 17:31:41 +03004551 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004552 struct switcher *switcher;
4553
4554 switcher = malloc(sizeof *switcher);
ganjingc1e71512020-07-28 15:28:45 +08004555 if (!switcher)
4556 return;
Pekka Paalanen4bb326b2021-05-14 14:37:46 +03004557
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004558 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004559 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004560 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004561 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004562 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004563
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004564 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004565 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004566 weston_keyboard_start_grab(keyboard, &switcher->grab);
4567 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004568 switcher_next(switcher);
4569}
4570
Pekka Paalanen3c647232011-12-22 13:43:43 +02004571static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004572backlight_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004573 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004574{
4575 struct weston_compositor *compositor = data;
4576 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004577 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004578
4579 /* TODO: we're limiting to simple use cases, where we assume just
4580 * control on the primary display. We'd have to extend later if we
4581 * ever get support for setting backlights on random desktop LCD
4582 * panels though */
4583 output = get_default_output(compositor);
4584 if (!output)
4585 return;
4586
4587 if (!output->set_backlight)
4588 return;
4589
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004590 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4591 backlight_new = output->backlight_current - 25;
4592 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4593 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004594
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004595 if (backlight_new < 5)
4596 backlight_new = 5;
4597 if (backlight_new > 255)
4598 backlight_new = 255;
4599
4600 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004601 output->set_backlight(output, output->backlight_current);
4602}
4603
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004604static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004605force_kill_binding(struct weston_keyboard *keyboard,
4606 const struct timespec *time, uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004607{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004608 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004609 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004610 struct desktop_shell *shell = data;
4611 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004612 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004613
Derek Foreman8ae2db52015-07-15 13:00:36 -05004614 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004615 if (!focus_surface)
4616 return;
4617
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004618 wl_signal_emit(&compositor->kill_signal, focus_surface);
4619
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004620 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004621 wl_client_get_credentials(client, &pid, NULL, NULL);
4622
4623 /* Skip clients that we launched ourselves (the credentials of
4624 * the socketpair is ours) */
4625 if (pid == getpid())
4626 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004627
Daniel Stone325fc2d2012-05-30 16:31:58 +01004628 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004629}
4630
4631static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004632workspace_up_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 = shell->workspaces.current;
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 if (new_index != 0)
4641 new_index--;
4642
4643 change_workspace(shell, new_index);
4644}
4645
4646static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004647workspace_down_binding(struct weston_keyboard *keyboard,
4648 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004649{
4650 struct desktop_shell *shell = data;
4651 unsigned int new_index = shell->workspaces.current;
4652
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004653 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004654 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004655 if (new_index < shell->workspaces.num - 1)
4656 new_index++;
4657
4658 change_workspace(shell, new_index);
4659}
4660
4661static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004662workspace_f_binding(struct weston_keyboard *keyboard,
4663 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004664{
4665 struct desktop_shell *shell = data;
4666 unsigned int new_index;
4667
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004668 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004669 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004670 new_index = key - KEY_F1;
4671 if (new_index >= shell->workspaces.num)
4672 new_index = shell->workspaces.num - 1;
4673
4674 change_workspace(shell, new_index);
4675}
4676
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004677static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004678workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004679 const struct timespec *time, uint32_t key,
4680 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004681{
4682 struct desktop_shell *shell = data;
4683 unsigned int new_index = shell->workspaces.current;
4684
4685 if (shell->locked)
4686 return;
4687
4688 if (new_index != 0)
4689 new_index--;
4690
Derek Foreman8ae2db52015-07-15 13:00:36 -05004691 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004692}
4693
4694static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004695workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004696 const struct timespec *time, uint32_t key,
4697 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004698{
4699 struct desktop_shell *shell = data;
4700 unsigned int new_index = shell->workspaces.current;
4701
4702 if (shell->locked)
4703 return;
4704
4705 if (new_index < shell->workspaces.num - 1)
4706 new_index++;
4707
Derek Foreman8ae2db52015-07-15 13:00:36 -05004708 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004709}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004710
4711static void
Harish Krupodc3edc02019-04-20 17:50:18 +05304712shell_reposition_view_on_output_change(struct weston_view *view)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004713{
4714 struct weston_output *output, *first_output;
4715 struct weston_compositor *ec = view->surface->compositor;
4716 struct shell_surface *shsurf;
4717 float x, y;
4718 int visible;
4719
Harish Krupo2dff4dd2019-04-20 18:10:56 +05304720 if (wl_list_empty(&ec->output_list))
4721 return;
4722
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004723 x = view->geometry.x;
4724 y = view->geometry.y;
4725
4726 /* At this point the destroyed output is not in the list anymore.
4727 * If the view is still visible somewhere, we leave where it is,
4728 * otherwise, move it to the first output. */
4729 visible = 0;
4730 wl_list_for_each(output, &ec->output_list, link) {
4731 if (pixman_region32_contains_point(&output->region,
4732 x, y, NULL)) {
4733 visible = 1;
4734 break;
4735 }
4736 }
4737
4738 if (!visible) {
4739 first_output = container_of(ec->output_list.next,
4740 struct weston_output, link);
4741
4742 x = first_output->x + first_output->width / 4;
4743 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004744
4745 weston_view_set_position(view, x, y);
4746 } else {
4747 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004748 }
4749
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004750
4751 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004752 if (!shsurf)
4753 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004754
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004755 shsurf->saved_position_valid = false;
4756 set_maximized(shsurf, false);
4757 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004758}
4759
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004760void
4761shell_for_each_layer(struct desktop_shell *shell,
4762 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004763{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004764 struct workspace **ws;
4765
4766 func(shell, &shell->fullscreen_layer, data);
4767 func(shell, &shell->panel_layer, data);
4768 func(shell, &shell->background_layer, data);
4769 func(shell, &shell->lock_layer, data);
4770 func(shell, &shell->input_panel_layer, data);
4771
4772 wl_array_for_each(ws, &shell->workspaces.array)
4773 func(shell, &(*ws)->layer, data);
4774}
4775
4776static void
Harish Krupodc3edc02019-04-20 17:50:18 +05304777shell_output_changed_move_layer(struct desktop_shell *shell,
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004778 struct weston_layer *layer,
4779 void *data)
4780{
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004781 struct weston_view *view;
4782
Marius Vladea40d6d2018-02-08 18:46:42 +02004783 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Harish Krupodc3edc02019-04-20 17:50:18 +05304784 shell_reposition_view_on_output_change(view);
4785
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004786}
4787
4788static void
Pekka Paalanen719ca872021-05-24 16:53:40 +03004789shell_output_destroy(struct shell_output *shell_output)
Xiong Zhang6b481422013-10-23 13:58:32 +08004790{
Pekka Paalanen0d5e4ff2021-05-24 16:13:21 +03004791 struct desktop_shell *shell = shell_output->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004792
Harish Krupodc3edc02019-04-20 17:50:18 +05304793 shell_for_each_layer(shell, shell_output_changed_move_layer, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004794
Pekka Paalanen719ca872021-05-24 16:53:40 +03004795 if (shell_output->fade.animation) {
4796 weston_view_animation_destroy(shell_output->fade.animation);
4797 shell_output->fade.animation = NULL;
4798 }
4799
4800 if (shell_output->fade.view) {
4801 /* destroys the view as well */
4802 weston_surface_destroy(shell_output->fade.view->surface);
4803 }
4804
4805 if (shell_output->fade.startup_timer)
4806 wl_event_source_remove(shell_output->fade.startup_timer);
4807
Pekka Paalanen0d5e4ff2021-05-24 16:13:21 +03004808 if (shell_output->panel_surface)
4809 wl_list_remove(&shell_output->panel_surface_listener.link);
4810 if (shell_output->background_surface)
4811 wl_list_remove(&shell_output->background_surface_listener.link);
4812 wl_list_remove(&shell_output->destroy_listener.link);
4813 wl_list_remove(&shell_output->link);
4814 free(shell_output);
Xiong Zhang6b481422013-10-23 13:58:32 +08004815}
4816
4817static void
Pekka Paalanen719ca872021-05-24 16:53:40 +03004818handle_output_destroy(struct wl_listener *listener, void *data)
4819{
4820 struct shell_output *shell_output =
4821 container_of(listener, struct shell_output, destroy_listener);
4822
4823 shell_output_destroy(shell_output);
4824}
4825
4826static void
David Fort50d962f2016-06-09 17:55:52 +02004827shell_resize_surface_to_output(struct desktop_shell *shell,
4828 struct weston_surface *surface,
4829 const struct weston_output *output)
4830{
4831 if (!surface)
4832 return;
4833
4834 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4835 surface->resource,
4836 output->width,
4837 output->height);
4838}
4839
4840
4841static void
4842handle_output_resized(struct wl_listener *listener, void *data)
4843{
4844 struct desktop_shell *shell =
4845 container_of(listener, struct desktop_shell, resized_listener);
4846 struct weston_output *output = (struct weston_output *)data;
4847 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4848
4849 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4850 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4851}
4852
4853static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004854create_shell_output(struct desktop_shell *shell,
4855 struct weston_output *output)
4856{
4857 struct shell_output *shell_output;
4858
4859 shell_output = zalloc(sizeof *shell_output);
4860 if (shell_output == NULL)
4861 return;
4862
4863 shell_output->output = output;
4864 shell_output->shell = shell;
4865 shell_output->destroy_listener.notify = handle_output_destroy;
4866 wl_signal_add(&output->destroy_signal,
4867 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004868 wl_list_insert(shell->output_list.prev, &shell_output->link);
Harish Krupodc3edc02019-04-20 17:50:18 +05304869
4870 if (wl_list_length(&shell->output_list) == 1)
4871 shell_for_each_layer(shell,
4872 shell_output_changed_move_layer, NULL);
Xiong Zhang6b481422013-10-23 13:58:32 +08004873}
4874
4875static void
4876handle_output_create(struct wl_listener *listener, void *data)
4877{
4878 struct desktop_shell *shell =
4879 container_of(listener, struct desktop_shell, output_create_listener);
4880 struct weston_output *output = (struct weston_output *)data;
4881
4882 create_shell_output(shell, output);
4883}
4884
4885static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004886handle_output_move_layer(struct desktop_shell *shell,
4887 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004888{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004889 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004890 struct weston_view *view;
4891 float x, y;
4892
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004893 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004894 if (view->output != output)
4895 continue;
4896
4897 x = view->geometry.x + output->move_x;
4898 y = view->geometry.y + output->move_y;
4899 weston_view_set_position(view, x, y);
4900 }
4901}
4902
4903static void
4904handle_output_move(struct wl_listener *listener, void *data)
4905{
4906 struct desktop_shell *shell;
4907
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004908 shell = container_of(listener, struct desktop_shell,
4909 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004910
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004911 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004912}
4913
4914static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004915setup_output_destroy_handler(struct weston_compositor *ec,
4916 struct desktop_shell *shell)
4917{
4918 struct weston_output *output;
4919
4920 wl_list_init(&shell->output_list);
4921 wl_list_for_each(output, &ec->output_list, link)
4922 create_shell_output(shell, output);
4923
4924 shell->output_create_listener.notify = handle_output_create;
4925 wl_signal_add(&ec->output_created_signal,
4926 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004927
4928 shell->output_move_listener.notify = handle_output_move;
4929 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08004930}
4931
4932static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004933shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004934{
Tiago Vignattibe143262012-04-16 17:31:41 +03004935 struct desktop_shell *shell =
4936 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004937 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08004938 struct shell_output *shell_output, *tmp;
Marius Vladc114fd62021-08-26 16:37:24 +03004939 struct shell_seat *shseat, *shseat_next;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004940
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08004941 /* Force state to unlocked so we don't try to fade */
4942 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004943
4944 if (shell->child.client) {
4945 /* disable respawn */
4946 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004947 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03004948 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004949
Harsha M Mb8b2c722018-08-07 19:05:02 +05304950 wl_list_remove(&shell->destroy_listener.link);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004951 wl_list_remove(&shell->idle_listener.link);
4952 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004953 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004954
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004955 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004956 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004957
Pekka Paalanen719ca872021-05-24 16:53:40 +03004958 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link)
4959 shell_output_destroy(shell_output);
Xiong Zhang6b481422013-10-23 13:58:32 +08004960
4961 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07004962 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02004963 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004964
Marius Vladc114fd62021-08-26 16:37:24 +03004965 wl_list_for_each_safe(shseat, shseat_next, &shell->seat_list, link)
4966 desktop_shell_destroy_seat(shseat);
4967
Pekka Paalanenabd72922021-05-24 14:42:00 +03004968 weston_desktop_destroy(shell->desktop);
4969
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004970 wl_array_for_each(ws, &shell->workspaces.array)
4971 workspace_destroy(*ws);
4972 wl_array_release(&shell->workspaces.array);
4973
Pekka Paalanen4bb326b2021-05-14 14:37:46 +03004974 weston_layer_fini(&shell->fullscreen_layer);
4975 weston_layer_fini(&shell->panel_layer);
4976 weston_layer_fini(&shell->background_layer);
4977 weston_layer_fini(&shell->lock_layer);
4978 weston_layer_fini(&shell->input_panel_layer);
4979 weston_layer_fini(&shell->minimized_layer);
4980
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004981 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004982 free(shell);
4983}
4984
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004985static void
4986shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4987{
4988 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004989 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004990
Bob Ham744e6532016-01-12 10:21:48 +00004991 if (shell->allow_zap)
4992 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4993 MODIFIER_CTRL | MODIFIER_ALT,
4994 terminate_binding, ec);
4995
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004996 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01004997 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4998 click_to_activate_binding,
4999 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07005000 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
5001 click_to_activate_binding,
5002 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005003 weston_compositor_add_touch_binding(ec, 0,
5004 touch_to_activate_binding,
5005 shell);
Bob Ham553d1242016-01-12 10:21:49 +00005006 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5007 backlight_binding, ec);
5008 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5009 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005010
5011 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00005012 if (shell->exposay_modifier)
5013 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5014 exposay_binding, shell);
5015
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005016 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00005017 if (!mod)
5018 return;
5019
Ian Ray13404c42017-09-18 15:22:01 +03005020 /* This binding is not configurable, but is only enabled if there is a
5021 * valid binding modifier. */
5022 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5023 MODIFIER_SUPER | MODIFIER_ALT,
5024 surface_opacity_binding, NULL);
5025
Ian Rayab7c0b62017-09-18 15:22:00 +03005026 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5027 mod, zoom_axis_binding,
5028 NULL);
5029
Daniel Stone325fc2d2012-05-30 16:31:58 +01005030 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5031 zoom_key_binding, NULL);
5032 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5033 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005034 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5035 maximize_binding, NULL);
5036 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5037 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005038 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5039 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005040 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05005041 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005042 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08005043 weston_compositor_add_button_binding(ec, BTN_LEFT,
5044 mod | MODIFIER_SHIFT,
5045 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005046
5047 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05005048 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005049 rotate_binding, NULL);
5050
Daniel Stone325fc2d2012-05-30 16:31:58 +01005051 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5052 shell);
5053 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5054 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005055 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5056 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005057 weston_compositor_add_key_binding(ec, KEY_K, mod,
5058 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005059 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5060 workspace_up_binding, shell);
5061 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5062 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005063 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5064 workspace_move_surface_up_binding,
5065 shell);
5066 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5067 workspace_move_surface_down_binding,
5068 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005069
5070 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5071 if (shell->workspaces.num > 1) {
5072 num_workspace_bindings = shell->workspaces.num;
5073 if (num_workspace_bindings > 6)
5074 num_workspace_bindings = 6;
5075 for (i = 0; i < num_workspace_bindings; i++)
5076 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5077 workspace_f_binding,
5078 shell);
5079 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005080
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02005081 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005082}
5083
Jason Ekstrand024177c2014-04-21 19:42:58 -05005084static void
5085handle_seat_created(struct wl_listener *listener, void *data)
5086{
5087 struct weston_seat *seat = data;
Marius Vladc114fd62021-08-26 16:37:24 +03005088 struct desktop_shell *shell =
5089 container_of(listener, struct desktop_shell, seat_create_listener);
Jason Ekstrand024177c2014-04-21 19:42:58 -05005090
Marius Vladc114fd62021-08-26 16:37:24 +03005091 create_shell_seat(shell, seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05005092}
5093
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005094WL_EXPORT int
Quentin Glidicda01c1d2016-12-02 14:17:08 +01005095wet_shell_init(struct weston_compositor *ec,
5096 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005097{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005098 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005099 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005100 struct workspace **pws;
5101 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005102 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005103
Peter Huttererf3d62272013-08-08 11:57:05 +10005104 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005105 if (shell == NULL)
5106 return -1;
5107
Kristian Høgsberg75840622011-09-06 13:48:16 -04005108 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005109
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02005110 if (!weston_compositor_add_destroy_listener_once(ec,
5111 &shell->destroy_listener,
5112 shell_destroy)) {
5113 free(shell);
5114 return 0;
5115 }
5116
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005117 shell->idle_listener.notify = idle_handler;
5118 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5119 shell->wake_listener.notify = wake_handler;
5120 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005121 shell->transform_listener.notify = transform_handler;
5122 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005123
Quentin Glidic82681572016-12-17 13:40:51 +01005124 weston_layer_init(&shell->fullscreen_layer, ec);
5125 weston_layer_init(&shell->panel_layer, ec);
5126 weston_layer_init(&shell->background_layer, ec);
5127 weston_layer_init(&shell->lock_layer, ec);
5128 weston_layer_init(&shell->input_panel_layer, ec);
5129
5130 weston_layer_set_position(&shell->fullscreen_layer,
5131 WESTON_LAYER_POSITION_FULLSCREEN);
5132 weston_layer_set_position(&shell->panel_layer,
5133 WESTON_LAYER_POSITION_UI);
5134 weston_layer_set_position(&shell->background_layer,
5135 WESTON_LAYER_POSITION_BACKGROUND);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005136
5137 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005138 wl_list_init(&shell->workspaces.client_list);
Marius Vladc114fd62021-08-26 16:37:24 +03005139 wl_list_init(&shell->seat_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005140
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005141 if (input_panel_setup(shell) < 0)
5142 return -1;
5143
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03005144 shell->text_backend = text_backend_init(ec);
5145 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00005146 return -1;
5147
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005148 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005149
Daniel Stonedf8133b2013-11-19 11:37:14 +01005150 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5151 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5152
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005153 for (i = 0; i < shell->workspaces.num; i++) {
5154 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5155 if (pws == NULL)
5156 return -1;
5157
Quentin Glidic82681572016-12-17 13:40:51 +01005158 *pws = workspace_create(shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005159 if (*pws == NULL)
5160 return -1;
5161 }
5162 activate_workspace(shell, 0);
5163
Quentin Glidic82681572016-12-17 13:40:51 +01005164 weston_layer_init(&shell->minimized_layer, ec);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01005165
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005166 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005167 wl_list_init(&shell->workspaces.animation.link);
5168 shell->workspaces.animation.frame = animate_workspace_change_frame;
5169
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02005170 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
5171 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005172 return -1;
5173
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005174 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005175 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005176 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005177 return -1;
5178
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02005179 weston_compositor_get_time(&shell->child.deathstamp);
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005180
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005181 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02005182
Xiong Zhang6b481422013-10-23 13:58:32 +08005183 setup_output_destroy_handler(ec, shell);
5184
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005185 loop = wl_display_get_event_loop(ec->wl_display);
5186 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005187
Jason Ekstrand024177c2014-04-21 19:42:58 -05005188 wl_list_for_each(seat, &ec->seat_list, link)
Marius Vladc114fd62021-08-26 16:37:24 +03005189 create_shell_seat(shell, seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05005190 shell->seat_create_listener.notify = handle_seat_created;
5191 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005192
David Fort50d962f2016-06-09 17:55:52 +02005193 shell->resized_listener.notify = handle_output_resized;
5194 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
5195
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01005196 screenshooter_create(ec);
5197
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005198 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005199
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005200 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005201
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005202 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
5203
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005204 return 0;
5205}