blob: 9ade17a8fe485d9419055470c4720a75c3120e77 [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Daniel Stonedf8133b2013-11-19 11:37:14 +01004 * Copyright © 2013 Raspberry Pi Foundation
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005 *
Bryce Harringtonaf637c22015-06-11 12:55:55 -07006 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050012 *
Bryce Harringtonaf637c22015-06-11 12:55:55 -070013 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050024 */
25
Daniel Stonec228e232013-05-22 18:03:19 +030026#include "config.h"
27
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050028#include <stdlib.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030029#include <stdint.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040030#include <stdio.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050031#include <string.h>
32#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040033#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020034#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020035#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020036#include <math.h>
Kristian Høgsberg92a57db2012-05-26 13:41:06 -040037#include <sys/types.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038
Kristian Høgsberg1ef23132013-12-04 00:20:01 -080039#include "shell.h"
Pekka Paalanen58f98c92016-06-03 16:45:21 +030040#include "compositor/weston.h"
Jonas Ådahl6d6fb612015-11-17 16:00:33 +080041#include "weston-desktop-shell-server-protocol.h"
Pekka Paalanen91b10102019-04-04 14:27:31 +030042#include <libweston/config-parser.h>
Jon Cruzd618f682015-06-15 15:37:09 -070043#include "shared/helpers.h"
Marius Vladd98c7e82021-03-08 18:02:32 +020044#include "shared/shell-utils.h"
Alexandros Frantzis8250a612017-11-16 18:20:52 +020045#include "shared/timespec-util.h"
Pekka Paalanen8ebd9812019-04-04 16:02:14 +030046#include <libweston-desktop/libweston-desktop.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050047
Jonas Ådahle3cddce2012-06-13 00:01:22 +020048#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020049#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020050
Jonas Ådahl04769742012-06-13 00:01:24 +020051struct focus_state {
Quentin Glidicfff39812016-08-15 10:56:52 +020052 struct desktop_shell *shell;
Jonas Ådahl04769742012-06-13 00:01:24 +020053 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040054 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020055 struct weston_surface *keyboard_focus;
56 struct wl_list link;
57 struct wl_listener seat_destroy_listener;
58 struct wl_listener surface_destroy_listener;
59};
60
Philip Withnall648a4dd2013-11-25 18:01:44 +000061/*
62 * Surface stacking and ordering.
63 *
64 * This is handled using several linked lists of surfaces, organised into
65 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
66 * above all of the surfaces in the layer below. The set of layers is static and
67 * in the following order (top-most first):
68 * • Lock layer (only ever displayed on its own)
69 * • Cursor layer
Manuel Bachmann805d2f52014-03-05 12:21:34 +010070 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000071 * • Fullscreen layer
72 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000073 * • Workspace layers
74 * • Background layer
75 *
76 * The list of layers may be manipulated to remove whole layers of surfaces from
77 * display. For example, when locking the screen, all layers except the lock
78 * layer are removed.
79 *
80 * A surface’s layer is modified on configuring the surface, in
81 * set_surface_type() (which is only called when the surface’s type change is
82 * _committed_). If a surface’s type changes (e.g. when making a window
83 * fullscreen) its layer changes too.
84 *
85 * In order to allow popup and transient surfaces to be correctly stacked above
86 * their parent surfaces, each surface tracks both its parent surface, and a
87 * linked list of its children. When a surface’s layer is updated, so are the
88 * layers of its children. Note that child surfaces are *not* the same as
89 * subsurfaces — child/parent surfaces are purely for maintaining stacking
90 * order.
91 *
92 * The children_link list of siblings of a surface (i.e. those surfaces which
93 * have the same parent) only contains weston_surfaces which have a
94 * shell_surface. Stacking is not implemented for non-shell_surface
95 * weston_surfaces. This means that the following implication does *not* hold:
96 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
97 */
98
Pekka Paalanen56cdea92011-11-23 16:14:12 +020099struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500100 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200101
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200102 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500103 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600104 int32_t last_width, last_height;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700105
Tiago Vignattibe143262012-04-16 17:31:41 +0300106 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200107
Stefan Agnera8da2082019-06-23 14:53:59 +0200108 struct wl_list children_list;
109 struct wl_list children_link;
110
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500111 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800112 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800113 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700114 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800115 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100116
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500117 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200118 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500119 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200120 } rotation;
121
122 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800123 struct weston_transform transform; /* matrix from x, y */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500124 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800125 } fullscreen;
126
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200127 struct weston_transform workspace_transform;
128
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500129 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500130 struct weston_output *output;
Semi Malinen99f8c082018-05-02 11:10:32 +0200131 struct wl_listener output_destroy_listener;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200132
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700133 struct surface_state {
Quentin Glidic18a81ac2016-08-16 14:26:03 +0200134 bool fullscreen;
135 bool maximized;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100136 bool lowered;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200137 } state;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700138
Pekka Paalanen77a6d952016-11-16 15:17:14 +0200139 struct {
140 bool is_set;
141 int32_t x;
142 int32_t y;
143 } xwayland;
144
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500145 int focus_count;
Derek Foreman039e9be2015-04-14 17:09:06 -0500146
147 bool destroying;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200148};
149
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300150struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400151 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300152 struct shell_surface *shsurf;
153 struct wl_listener shsurf_destroy_listener;
154};
155
Rusty Lynch1084da52013-08-15 09:10:08 -0700156struct shell_touch_grab {
157 struct weston_touch_grab grab;
158 struct shell_surface *shsurf;
159 struct wl_listener shsurf_destroy_listener;
160 struct weston_touch *touch;
161};
162
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300163struct weston_move_grab {
164 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100165 wl_fixed_t dx, dy;
Derek Foremancf7d95a2015-06-03 15:53:22 -0500166 bool client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500167};
168
Rusty Lynch1084da52013-08-15 09:10:08 -0700169struct weston_touch_move_grab {
170 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800171 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700172 wl_fixed_t dx, dy;
173};
174
Pekka Paalanen460099f2012-01-20 16:48:25 +0200175struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300176 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500177 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200178 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200179 float x;
180 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200181 } center;
182};
183
Giulio Camuffo5085a752013-03-25 21:42:45 +0100184struct shell_seat {
185 struct weston_seat *seat;
186 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500187 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100188
Jason Ekstrand024177c2014-04-21 19:42:58 -0500189 struct wl_listener caps_changed_listener;
190 struct wl_listener pointer_focus_listener;
191 struct wl_listener keyboard_focus_listener;
Marius Vladc114fd62021-08-26 16:37:24 +0300192
193 struct wl_list link; /** shell::seat_list */
Giulio Camuffo5085a752013-03-25 21:42:45 +0100194};
195
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800196
Alex Wubd3354b2012-04-17 17:20:49 +0800197static struct desktop_shell *
198shell_surface_get_shell(struct shell_surface *shsurf);
199
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500200static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200201set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
202
203static void
Derek Foreman74de4692015-07-15 13:00:39 -0500204surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500205
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300206static void
207shell_fade_startup(struct desktop_shell *shell);
208
Bryce Harrington9ad4de12016-09-09 13:16:02 -0700209static void
210shell_fade(struct desktop_shell *shell, enum fade_type type);
211
Philip Withnallbecb77e2013-11-25 18:01:30 +0000212static struct shell_seat *
213get_shell_seat(struct weston_seat *seat);
214
Jonny Lambd73c6942014-08-20 15:53:20 +0200215static void
216get_output_panel_size(struct desktop_shell *shell,
217 struct weston_output *output,
218 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700219
Philip Withnall648a4dd2013-11-25 18:01:44 +0000220static void
221shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
222
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500223static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400224destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300225{
226 struct shell_grab *grab;
227
228 grab = container_of(listener, struct shell_grab,
229 shsurf_destroy_listener);
230
231 grab->shsurf = NULL;
232}
233
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800234struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500235get_default_view(struct weston_surface *surface)
236{
237 struct shell_surface *shsurf;
238 struct weston_view *view;
239
240 if (!surface || wl_list_empty(&surface->views))
241 return NULL;
242
243 shsurf = get_shell_surface(surface);
244 if (shsurf)
245 return shsurf->view;
246
247 wl_list_for_each(view, &surface->views, surface_link)
248 if (weston_view_is_mapped(view))
249 return view;
250
251 return container_of(surface->views.next, struct weston_view, surface_link);
252}
253
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300254static void
Marius Vladd25d63e2021-11-10 17:50:50 +0200255desktop_shell_destroy_surface(struct shell_surface *shsurf)
256{
257 struct shell_surface *shsurf_child, *tmp;
258
259 wl_list_for_each_safe(shsurf_child, tmp, &shsurf->children_list, children_link) {
260 wl_list_remove(&shsurf_child->children_link);
261 wl_list_init(&shsurf_child->children_link);
262 }
263 wl_list_remove(&shsurf->children_link);
264
265 wl_signal_emit(&shsurf->destroy_signal, shsurf);
266
267 weston_view_destroy(shsurf->view);
268 if (shsurf->output_destroy_listener.notify) {
269 wl_list_remove(&shsurf->output_destroy_listener.link);
270 shsurf->output_destroy_listener.notify = NULL;
271 }
272
273 free(shsurf);
274}
275
276static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300277shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400278 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300279 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400280 struct weston_pointer *pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800281 enum weston_desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300282{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300283 struct desktop_shell *shell = shsurf->shell;
284
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200285 weston_seat_break_desktop_grabs(pointer->seat);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400286
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300287 grab->grab.interface = interface;
288 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400289 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500290 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400291 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300292
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700293 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400294 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400295 if (shell->child.desktop_shell) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800296 weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400297 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500298 weston_pointer_set_focus(pointer,
299 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400300 wl_fixed_from_int(0),
301 wl_fixed_from_int(0));
302 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300303}
304
Jonny Lambd73c6942014-08-20 15:53:20 +0200305static void
Quentin Glidic581df062016-06-23 18:55:19 +0200306get_panel_size(struct desktop_shell *shell,
307 struct weston_view *view,
308 int *width,
309 int *height)
310{
311 float x1, y1;
312 float x2, y2;
313 weston_view_to_global_float(view, 0, 0, &x1, &y1);
314 weston_view_to_global_float(view,
315 view->surface->width,
316 view->surface->height,
317 &x2, &y2);
318 *width = (int)(x2 - x1);
319 *height = (int)(y2 - y1);
320}
321
322static void
Jonny Lambd73c6942014-08-20 15:53:20 +0200323get_output_panel_size(struct desktop_shell *shell,
324 struct weston_output *output,
325 int *width,
326 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700327{
328 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200329
330 *width = 0;
331 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700332
333 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200334 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700335
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300336 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Quentin Glidic581df062016-06-23 18:55:19 +0200337 if (view->surface->output == output) {
338 get_panel_size(shell, view, width, height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200339 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700340 }
341 }
342
Jonny Lambd73c6942014-08-20 15:53:20 +0200343 /* the correct view wasn't found */
344}
345
Leandro Ribeiro0c59e922020-01-24 17:53:44 -0300346void
Quentin Glidicfff39812016-08-15 10:56:52 +0200347get_output_work_area(struct desktop_shell *shell,
Jonny Lambd73c6942014-08-20 15:53:20 +0200348 struct weston_output *output,
349 pixman_rectangle32_t *area)
350{
351 int32_t panel_width = 0, panel_height = 0;
352
Pekka Paalanen99372ba2018-05-02 10:21:55 +0200353 if (!output) {
354 area->x = 0;
355 area->y = 0;
356 area->width = 0;
357 area->height = 0;
358
359 return;
360 }
361
Derek Foremanf814c5d2015-04-15 12:23:54 -0500362 area->x = output->x;
363 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200364
365 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200366 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800367 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
Jonny Lambd73c6942014-08-20 15:53:20 +0200368 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500369 area->y += panel_height;
Daniel Stone2ef9b1a2017-03-13 16:31:36 +0000370 /* fallthrough */
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800371 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200372 area->width = output->width;
373 area->height = output->height - panel_height;
374 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800375 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500376 area->x += panel_width;
Daniel Stone2ef9b1a2017-03-13 16:31:36 +0000377 /* fallthrough */
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800378 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200379 area->width = output->width - panel_width;
380 area->height = output->height;
381 break;
382 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700383}
384
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400385static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300386shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300387{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700388 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400389 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700390 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400391
392 if (grab->shsurf->resize_edges) {
393 grab->shsurf->resize_edges = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400394 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700395 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300396
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700397 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300398}
399
400static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700401shell_touch_grab_start(struct shell_touch_grab *grab,
402 const struct weston_touch_grab_interface *interface,
403 struct shell_surface *shsurf,
404 struct weston_touch *touch)
405{
406 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800407
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200408 weston_seat_break_desktop_grabs(touch->seat);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700409
Rusty Lynch1084da52013-08-15 09:10:08 -0700410 grab->grab.interface = interface;
411 grab->shsurf = shsurf;
412 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
413 wl_signal_add(&shsurf->destroy_signal,
414 &grab->shsurf_destroy_listener);
415
416 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700417 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700418
419 weston_touch_start_grab(touch, &grab->grab);
420 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500421 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500422 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700423}
424
425static void
426shell_touch_grab_end(struct shell_touch_grab *grab)
427{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700428 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700429 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700430 grab->shsurf->grabbed = 0;
431 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700432
433 weston_touch_end_grab(grab->touch);
434}
435
Daniel Stone496ca172012-05-30 16:31:42 +0100436static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300437get_modifier(char *modifier)
438{
439 if (!modifier)
440 return MODIFIER_SUPER;
441
442 if (!strcmp("ctrl", modifier))
443 return MODIFIER_CTRL;
444 else if (!strcmp("alt", modifier))
445 return MODIFIER_ALT;
446 else if (!strcmp("super", modifier))
447 return MODIFIER_SUPER;
Bob Ham553d1242016-01-12 10:21:49 +0000448 else if (!strcmp("none", modifier))
449 return 0;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300450 else
451 return MODIFIER_SUPER;
452}
453
Juan Zhaoe10d2792012-04-25 19:09:52 +0800454static enum animation_type
455get_animation_type(char *animation)
456{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800457 if (!animation)
458 return ANIMATION_NONE;
459
Juan Zhaoe10d2792012-04-25 19:09:52 +0800460 if (!strcmp("zoom", animation))
461 return ANIMATION_ZOOM;
462 else if (!strcmp("fade", animation))
463 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100464 else if (!strcmp("dim-layer", animation))
465 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800466 else
467 return ANIMATION_NONE;
468}
469
Alex Wu4539b082012-03-01 12:57:46 +0800470static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400471shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200472{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400473 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500474 char *s, *client;
Daniel Stone51d995a2019-11-26 00:14:24 +0000475 bool allow_zap;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200476
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300477 section = weston_config_get_section(wet_get_config(shell->compositor),
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400478 "shell", NULL, NULL);
Marius Vlad64fbd0f2018-12-15 15:51:57 +0200479 client = wet_get_libexec_path(WESTON_SHELL_CLIENT);
Daniel Stonee03c1112016-11-24 20:45:45 +0000480 weston_config_section_get_string(section, "client", &s, client);
Derek Foremanc7210432014-08-21 11:32:38 -0500481 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100482 shell->client = s;
Bob Ham744e6532016-01-12 10:21:48 +0000483
484 weston_config_section_get_bool(section,
485 "allow-zap", &allow_zap, true);
486 shell->allow_zap = allow_zap;
487
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100488 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400489 "binding-modifier", &s, "super");
490 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200491 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800492
493 weston_config_section_get_string(section,
494 "exposay-modifier", &s, "none");
Bob Ham553d1242016-01-12 10:21:49 +0000495 shell->exposay_modifier = get_modifier(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800496 free(s);
497
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400498 weston_config_section_get_string(section, "animation", &s, "none");
499 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200500 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200501 weston_config_section_get_string(section, "close-animation", &s, "fade");
502 shell->win_close_animation_type = get_animation_type(s);
503 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700504 weston_config_section_get_string(section,
505 "startup-animation", &s, "fade");
506 shell->startup_animation_type = get_animation_type(s);
507 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700508 if (shell->startup_animation_type == ANIMATION_ZOOM)
509 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100510 weston_config_section_get_string(section, "focus-animation", &s, "none");
511 shell->focus_animation_type = get_animation_type(s);
512 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400513 weston_config_section_get_uint(section, "num-workspaces",
514 &shell->workspaces.num,
515 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200516}
517
Pekka Paalanen8274d902014-08-06 19:36:51 +0300518static int
519focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
520{
521 return snprintf(buf, len, "focus highlight effect for output %s",
Miguel A. Vico620f68d2019-09-25 11:23:13 -0700522 (surface->output ? surface->output->name : "NULL"));
Pekka Paalanen8274d902014-08-06 19:36:51 +0300523}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100524
525/* no-op func for checking focus surface */
526static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200527focus_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100528{
529}
530
531static struct focus_surface *
532get_focus_surface(struct weston_surface *surface)
533{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200534 if (surface->committed == focus_surface_committed)
535 return surface->committed_private;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100536 else
537 return NULL;
538}
539
540static bool
541is_focus_surface (struct weston_surface *es)
542{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200543 return (es->committed == focus_surface_committed);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100544}
545
546static bool
547is_focus_view (struct weston_view *view)
548{
549 return is_focus_surface (view->surface);
550}
551
552static struct focus_surface *
553create_focus_surface(struct weston_compositor *ec,
554 struct weston_output *output)
555{
556 struct focus_surface *fsurf = NULL;
557 struct weston_surface *surface = NULL;
558
559 fsurf = malloc(sizeof *fsurf);
560 if (!fsurf)
561 return NULL;
562
563 fsurf->surface = weston_surface_create(ec);
564 surface = fsurf->surface;
565 if (surface == NULL) {
566 free(fsurf);
567 return NULL;
568 }
569
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200570 surface->committed = focus_surface_committed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100571 surface->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200572 surface->is_mapped = true;
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200573 surface->committed_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300574 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100575
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800576 fsurf->view = weston_view_create(surface);
577 if (fsurf->view == NULL) {
578 weston_surface_destroy(surface);
579 free(fsurf);
580 return NULL;
581 }
Semi Malinene7a52fb2018-04-26 11:08:10 +0200582 weston_view_set_output(fsurf->view, output);
Armin Krezović4663aca2016-06-30 06:04:29 +0200583 fsurf->view->is_mapped = true;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100584
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600585 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600586 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100587 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
588 pixman_region32_fini(&surface->opaque);
589 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
590 output->width, output->height);
591 pixman_region32_fini(&surface->input);
592 pixman_region32_init(&surface->input);
593
594 wl_list_init(&fsurf->workspace_transform.link);
595
596 return fsurf;
597}
598
599static void
600focus_surface_destroy(struct focus_surface *fsurf)
601{
602 weston_surface_destroy(fsurf->surface);
603 free(fsurf);
604}
605
606static void
607focus_animation_done(struct weston_view_animation *animation, void *data)
608{
609 struct workspace *ws = data;
610
611 ws->focus_animation = NULL;
612}
613
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200614static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200615focus_state_destroy(struct focus_state *state)
616{
617 wl_list_remove(&state->seat_destroy_listener.link);
618 wl_list_remove(&state->surface_destroy_listener.link);
619 free(state);
620}
621
622static void
623focus_state_seat_destroy(struct wl_listener *listener, void *data)
624{
625 struct focus_state *state = container_of(listener,
626 struct focus_state,
627 seat_destroy_listener);
628
629 wl_list_remove(&state->link);
630 focus_state_destroy(state);
631}
632
633static void
634focus_state_surface_destroy(struct wl_listener *listener, void *data)
635{
636 struct focus_state *state = container_of(listener,
637 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400638 surface_destroy_listener);
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200639 struct weston_surface *main_surface;
640 struct weston_view *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500641 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200642
Pekka Paalanen01388e22013-04-25 13:57:44 +0300643 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
644
Kristian Høgsberge3778222012-07-31 17:29:30 -0400645 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300646 wl_list_for_each(view,
647 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500648 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400649 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100650 if (is_focus_view(view))
651 continue;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200652 if (!get_shell_surface(view->surface))
653 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400654
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200655 next = view;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400656 break;
657 }
658
Pekka Paalanen01388e22013-04-25 13:57:44 +0300659 /* if the focus was a sub-surface, activate its main surface */
660 if (main_surface != state->keyboard_focus)
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200661 next = get_default_view(main_surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300662
Kristian Høgsberge3778222012-07-31 17:29:30 -0400663 if (next) {
Greg V15d3d302018-12-07 01:33:34 +0300664 if (state->keyboard_focus) {
665 wl_list_remove(&state->surface_destroy_listener.link);
666 wl_list_init(&state->surface_destroy_listener.link);
667 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100668 state->keyboard_focus = NULL;
Quentin Glidicfff39812016-08-15 10:56:52 +0200669 activate(state->shell, next, state->seat,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +0200670 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400671 } else {
Quentin Glidicfff39812016-08-15 10:56:52 +0200672 if (state->shell->focus_animation_type == ANIMATION_DIM_LAYER) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100673 if (state->ws->focus_animation)
674 weston_view_animation_destroy(state->ws->focus_animation);
675
676 state->ws->focus_animation = weston_fade_run(
677 state->ws->fsurf_front->view,
678 state->ws->fsurf_front->view->alpha, 0.0, 300,
679 focus_animation_done, state->ws);
680 }
681
Kristian Høgsberge3778222012-07-31 17:29:30 -0400682 wl_list_remove(&state->link);
683 focus_state_destroy(state);
684 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200685}
686
687static struct focus_state *
Quentin Glidicfff39812016-08-15 10:56:52 +0200688focus_state_create(struct desktop_shell *shell, struct weston_seat *seat,
689 struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200690{
Jonas Ådahl04769742012-06-13 00:01:24 +0200691 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200692
693 state = malloc(sizeof *state);
694 if (state == NULL)
695 return NULL;
696
Quentin Glidicfff39812016-08-15 10:56:52 +0200697 state->shell = shell;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100698 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400699 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200700 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400701 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200702
703 state->seat_destroy_listener.notify = focus_state_seat_destroy;
704 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400705 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200706 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400707 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200708
709 return state;
710}
711
Jonas Ådahl8538b222012-08-29 22:13:03 +0200712static struct focus_state *
713ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
714{
715 struct workspace *ws = get_current_workspace(shell);
716 struct focus_state *state;
717
718 wl_list_for_each(state, &ws->focus_list, link)
719 if (state->seat == seat)
720 break;
721
722 if (&state->link == &ws->focus_list)
Quentin Glidicfff39812016-08-15 10:56:52 +0200723 state = focus_state_create(shell, seat, ws);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200724
725 return state;
726}
727
Jonas Ådahl04769742012-06-13 00:01:24 +0200728static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800729focus_state_set_focus(struct focus_state *state,
730 struct weston_surface *surface)
731{
732 if (state->keyboard_focus) {
733 wl_list_remove(&state->surface_destroy_listener.link);
734 wl_list_init(&state->surface_destroy_listener.link);
735 }
736
737 state->keyboard_focus = surface;
738 if (surface)
739 wl_signal_add(&surface->destroy_signal,
740 &state->surface_destroy_listener);
741}
742
743static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400744restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200745{
746 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400747 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100748 struct wl_list pending_seat_list;
749 struct weston_seat *seat, *next_seat;
750
751 /* Temporarily steal the list of seats so that we can keep
752 * track of the seats we've already processed */
753 wl_list_init(&pending_seat_list);
754 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
755 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200756
757 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500758 struct weston_keyboard *keyboard =
759 weston_seat_get_keyboard(state->seat);
760
Neil Roberts4237f502014-04-09 16:33:31 +0100761 wl_list_remove(&state->seat->link);
762 wl_list_insert(&shell->compositor->seat_list,
763 &state->seat->link);
764
Derek Foreman1281a362015-07-31 16:55:32 -0500765 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800766 continue;
767
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400768 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200769
Derek Foreman1281a362015-07-31 16:55:32 -0500770 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200771 }
Neil Roberts4237f502014-04-09 16:33:31 +0100772
773 /* For any remaining seats that we don't have a focus state
774 * for we'll reset the keyboard focus to NULL */
775 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500776 struct weston_keyboard *keyboard =
777 weston_seat_get_keyboard(seat);
778
Neil Roberts4237f502014-04-09 16:33:31 +0100779 wl_list_insert(&shell->compositor->seat_list, &seat->link);
780
Derek Foreman1281a362015-07-31 16:55:32 -0500781 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100782 continue;
783
Derek Foreman1281a362015-07-31 16:55:32 -0500784 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100785 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200786}
787
788static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200789replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
790 struct weston_seat *seat)
791{
Derek Foreman1281a362015-07-31 16:55:32 -0500792 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200793 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200794
795 wl_list_for_each(state, &ws->focus_list, link) {
796 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500797 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200798 return;
799 }
800 }
801}
802
803static void
804drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
805 struct weston_surface *surface)
806{
807 struct focus_state *state;
808
809 wl_list_for_each(state, &ws->focus_list, link)
810 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800811 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200812}
813
814static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100815animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
816 struct weston_view *from, struct weston_view *to)
817{
818 struct weston_output *output;
819 bool focus_surface_created = false;
820
821 /* FIXME: Only support dim animation using two layers */
822 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
823 return;
824
825 output = get_default_output(shell->compositor);
826 if (ws->fsurf_front == NULL && (from || to)) {
827 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800828 if (ws->fsurf_front == NULL)
829 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100830 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800831
832 ws->fsurf_back = create_focus_surface(shell->compositor, output);
833 if (ws->fsurf_back == NULL) {
834 focus_surface_destroy(ws->fsurf_front);
835 return;
836 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100837 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800838
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100839 focus_surface_created = true;
840 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300841 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
842 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100843 }
844
845 if (ws->focus_animation) {
846 weston_view_animation_destroy(ws->focus_animation);
847 ws->focus_animation = NULL;
848 }
849
850 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300851 weston_layer_entry_insert(&to->layer_link,
852 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100853 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300854 weston_layer_entry_insert(&ws->layer.view_list,
855 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100856
857 if (focus_surface_created) {
858 ws->focus_animation = weston_fade_run(
859 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200860 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100861 focus_animation_done, ws);
862 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300863 weston_layer_entry_insert(&from->layer_link,
864 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100865 ws->focus_animation = weston_stable_fade_run(
866 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200867 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100868 focus_animation_done, ws);
869 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300870 weston_layer_entry_insert(&ws->layer.view_list,
871 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100872 ws->focus_animation = weston_stable_fade_run(
873 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200874 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100875 focus_animation_done, ws);
876 }
877}
878
879static void
Marius Vladd25d63e2021-11-10 17:50:50 +0200880desktop_shell_destroy_views_on_layer(struct weston_layer *layer);
881
882static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200883workspace_destroy(struct workspace *ws)
884{
Jonas Ådahl04769742012-06-13 00:01:24 +0200885 struct focus_state *state, *next;
886
887 wl_list_for_each_safe(state, next, &ws->focus_list, link)
888 focus_state_destroy(state);
889
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100890 if (ws->fsurf_front)
891 focus_surface_destroy(ws->fsurf_front);
892 if (ws->fsurf_back)
893 focus_surface_destroy(ws->fsurf_back);
894
Marius Vladd25d63e2021-11-10 17:50:50 +0200895 desktop_shell_destroy_views_on_layer(&ws->layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200896 free(ws);
897}
898
Jonas Ådahl04769742012-06-13 00:01:24 +0200899static void
900seat_destroyed(struct wl_listener *listener, void *data)
901{
902 struct weston_seat *seat = data;
903 struct focus_state *state, *next;
904 struct workspace *ws = container_of(listener,
905 struct workspace,
906 seat_destroyed_listener);
907
908 wl_list_for_each_safe(state, next, &ws->focus_list, link)
909 if (state->seat == seat)
910 wl_list_remove(&state->link);
911}
912
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200913static struct workspace *
Quentin Glidic82681572016-12-17 13:40:51 +0100914workspace_create(struct desktop_shell *shell)
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200915{
916 struct workspace *ws = malloc(sizeof *ws);
917 if (ws == NULL)
918 return NULL;
919
Quentin Glidic82681572016-12-17 13:40:51 +0100920 weston_layer_init(&ws->layer, shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200921
Jonas Ådahl04769742012-06-13 00:01:24 +0200922 wl_list_init(&ws->focus_list);
923 wl_list_init(&ws->seat_destroyed_listener.link);
924 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100925 ws->fsurf_front = NULL;
926 ws->fsurf_back = NULL;
927 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200928
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200929 return ws;
930}
931
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200932static int
933workspace_is_empty(struct workspace *ws)
934{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300935 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200936}
937
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200938static struct workspace *
939get_workspace(struct desktop_shell *shell, unsigned int index)
940{
941 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200942 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200943 pws += index;
944 return *pws;
945}
946
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800947struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200948get_current_workspace(struct desktop_shell *shell)
949{
950 return get_workspace(shell, shell->workspaces.current);
951}
952
953static void
954activate_workspace(struct desktop_shell *shell, unsigned int index)
955{
956 struct workspace *ws;
957
958 ws = get_workspace(shell, index);
Quentin Glidic82681572016-12-17 13:40:51 +0100959 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200960
961 shell->workspaces.current = index;
962}
963
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200964static unsigned int
965get_output_height(struct weston_output *output)
966{
967 return abs(output->region.extents.y1 - output->region.extents.y2);
968}
969
Greg Vf57774e2018-07-24 23:21:55 +0300970static struct weston_transform *
971view_get_transform(struct weston_view *view)
972{
973 struct focus_surface *fsurf = NULL;
974 struct shell_surface *shsurf = NULL;
975
976 if (is_focus_view(view)) {
977 fsurf = get_focus_surface(view->surface);
978 return &fsurf->workspace_transform;
979 }
980
981 shsurf = get_shell_surface(view->surface);
982 if (shsurf)
983 return &shsurf->workspace_transform;
984
985 return NULL;
986}
987
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200988static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100989view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200990{
Greg Vf57774e2018-07-24 23:21:55 +0300991 struct weston_transform *transform = view_get_transform(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200992
Greg Vf57774e2018-07-24 23:21:55 +0300993 if (!transform)
994 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100995
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200996 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500997 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100998 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200999
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001000 weston_matrix_init(&transform->matrix);
1001 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001002 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001003 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001004}
1005
1006static void
1007workspace_translate_out(struct workspace *ws, double fraction)
1008{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001009 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001010 unsigned int height;
1011 double d;
1012
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001013 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001014 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001015 d = height * fraction;
1016
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001017 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001018 }
1019}
1020
1021static void
1022workspace_translate_in(struct workspace *ws, double fraction)
1023{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001024 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001025 unsigned int height;
1026 double d;
1027
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001028 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001029 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001030
1031 if (fraction > 0)
1032 d = -(height - height * fraction);
1033 else
1034 d = height + height * fraction;
1035
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001036 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001037 }
1038}
1039
1040static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001041reverse_workspace_change_animation(struct desktop_shell *shell,
1042 unsigned int index,
1043 struct workspace *from,
1044 struct workspace *to)
1045{
1046 shell->workspaces.current = index;
1047
1048 shell->workspaces.anim_to = to;
1049 shell->workspaces.anim_from = from;
1050 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001051 shell->workspaces.anim_timestamp = (struct timespec) { 0 };
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001052
Quentin Glidic82681572016-12-17 13:40:51 +01001053 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1054 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
1055
Scott Moreau4272e632012-08-13 09:58:41 -06001056 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001057}
1058
1059static void
1060workspace_deactivate_transforms(struct workspace *ws)
1061{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001062 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001063 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001064
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001065 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Greg Vf57774e2018-07-24 23:21:55 +03001066 transform = view_get_transform(view);
1067 if (!transform)
1068 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001069
1070 if (!wl_list_empty(&transform->link)) {
1071 wl_list_remove(&transform->link);
1072 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001073 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001074 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001075 }
1076}
1077
1078static void
1079finish_workspace_change_animation(struct desktop_shell *shell,
1080 struct workspace *from,
1081 struct workspace *to)
1082{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001083 struct weston_view *view;
1084
Scott Moreau4272e632012-08-13 09:58:41 -06001085 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001086
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001087 /* Views that extend past the bottom of the output are still
1088 * visible after the workspace animation ends but before its layer
1089 * is hidden. In that case, we need to damage below those views so
1090 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001091 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001092 weston_view_damage_below(view);
1093
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001094 wl_list_remove(&shell->workspaces.animation.link);
1095 workspace_deactivate_transforms(from);
1096 workspace_deactivate_transforms(to);
1097 shell->workspaces.anim_to = NULL;
1098
Quentin Glidic82681572016-12-17 13:40:51 +01001099 weston_layer_unset_position(&shell->workspaces.anim_from->layer);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001100}
1101
1102static void
1103animate_workspace_change_frame(struct weston_animation *animation,
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001104 struct weston_output *output,
1105 const struct timespec *time)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001106{
1107 struct desktop_shell *shell =
1108 container_of(animation, struct desktop_shell,
1109 workspaces.animation);
1110 struct workspace *from = shell->workspaces.anim_from;
1111 struct workspace *to = shell->workspaces.anim_to;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001112 int64_t t;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001113 double x, y;
1114
1115 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1116 finish_workspace_change_animation(shell, from, to);
1117 return;
1118 }
1119
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001120 if (timespec_is_zero(&shell->workspaces.anim_timestamp)) {
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001121 if (shell->workspaces.anim_current == 0.0)
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001122 shell->workspaces.anim_timestamp = *time;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001123 else
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001124 timespec_add_msec(&shell->workspaces.anim_timestamp,
1125 time,
Emmanuel Gil Peyrot426c2462019-02-20 16:33:32 +01001126 /* Inverse of movement function 'y' below. */
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001127 -(asin(1.0 - shell->workspaces.anim_current) *
1128 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1129 M_2_PI));
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001130 }
1131
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001132 t = timespec_sub_to_msec(time, &shell->workspaces.anim_timestamp);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001133
1134 /*
1135 * x = [0, π/2]
1136 * y(x) = sin(x)
1137 */
1138 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1139 y = sin(x);
1140
1141 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001142 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001143
1144 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1145 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1146 shell->workspaces.anim_current = y;
1147
Scott Moreau4272e632012-08-13 09:58:41 -06001148 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001149 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001150 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001151 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001152}
1153
1154static void
1155animate_workspace_change(struct desktop_shell *shell,
1156 unsigned int index,
1157 struct workspace *from,
1158 struct workspace *to)
1159{
1160 struct weston_output *output;
1161
1162 int dir;
1163
1164 if (index > shell->workspaces.current)
1165 dir = -1;
1166 else
1167 dir = 1;
1168
1169 shell->workspaces.current = index;
1170
1171 shell->workspaces.anim_dir = dir;
1172 shell->workspaces.anim_from = from;
1173 shell->workspaces.anim_to = to;
1174 shell->workspaces.anim_current = 0.0;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001175 shell->workspaces.anim_timestamp = (struct timespec) { 0 };
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001176
1177 output = container_of(shell->compositor->output_list.next,
1178 struct weston_output, link);
1179 wl_list_insert(&output->animation_list,
1180 &shell->workspaces.animation.link);
1181
Quentin Glidic82681572016-12-17 13:40:51 +01001182 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1183 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001184
1185 workspace_translate_in(to, 0);
1186
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001187 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001188
Scott Moreau4272e632012-08-13 09:58:41 -06001189 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001190}
1191
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001192static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001193update_workspace(struct desktop_shell *shell, unsigned int index,
1194 struct workspace *from, struct workspace *to)
1195{
1196 shell->workspaces.current = index;
Quentin Glidic82681572016-12-17 13:40:51 +01001197 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1198 weston_layer_unset_position(&from->layer);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001199}
1200
1201static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001202change_workspace(struct desktop_shell *shell, unsigned int index)
1203{
1204 struct workspace *from;
1205 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001206 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001207
1208 if (index == shell->workspaces.current)
1209 return;
1210
1211 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001212 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001213 return;
1214
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001215 from = get_current_workspace(shell);
1216 to = get_workspace(shell, index);
1217
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001218 if (shell->workspaces.anim_from == to &&
1219 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001220 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001221 reverse_workspace_change_animation(shell, index, from, to);
1222 return;
1223 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001224
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001225 if (shell->workspaces.anim_to != NULL)
1226 finish_workspace_change_animation(shell,
1227 shell->workspaces.anim_from,
1228 shell->workspaces.anim_to);
1229
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001230 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001231
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001232 if (shell->focus_animation_type != ANIMATION_NONE) {
1233 wl_list_for_each(state, &from->focus_list, link)
1234 if (state->keyboard_focus)
1235 animate_focus_change(shell, from,
1236 get_default_view(state->keyboard_focus), NULL);
1237
1238 wl_list_for_each(state, &to->focus_list, link)
1239 if (state->keyboard_focus)
1240 animate_focus_change(shell, to,
1241 NULL, get_default_view(state->keyboard_focus));
1242 }
1243
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001244 if (workspace_is_empty(to) && workspace_is_empty(from))
1245 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001246 else
1247 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001248}
1249
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001250static bool
1251workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1252{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001253 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001254 struct wl_list *e;
1255
1256 if (wl_list_empty(list))
1257 return false;
1258
1259 e = list->next;
1260
1261 if (e->next != list)
1262 return false;
1263
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001264 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001265}
1266
1267static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001268surface_keyboard_focus_lost(struct weston_surface *surface)
1269{
1270 struct weston_compositor *compositor = surface->compositor;
1271 struct weston_seat *seat;
1272 struct weston_surface *focus;
1273
1274 wl_list_for_each(seat, &compositor->seat_list, link) {
1275 struct weston_keyboard *keyboard =
1276 weston_seat_get_keyboard(seat);
1277
1278 if (!keyboard)
1279 continue;
1280
1281 focus = weston_surface_get_main_surface(keyboard->focus);
1282 if (focus == surface)
1283 weston_keyboard_set_focus(keyboard, NULL);
1284 }
1285}
1286
1287static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001288take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001289 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001290 unsigned int index)
1291{
Derek Foreman1281a362015-07-31 16:55:32 -05001292 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001293 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001294 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001295 struct shell_surface *shsurf;
1296 struct workspace *from;
1297 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001298 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001299
Derek Foreman1281a362015-07-31 16:55:32 -05001300 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001301 view = get_default_view(surface);
1302 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001303 index == shell->workspaces.current ||
1304 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001305 return;
1306
1307 from = get_current_workspace(shell);
1308 to = get_workspace(shell, index);
1309
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001310 weston_layer_entry_remove(&view->layer_link);
1311 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001312
Philip Withnall659163d2013-11-25 18:01:36 +00001313 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001314 if (shsurf != NULL)
1315 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001316
Jonas Ådahle9d22502012-08-29 22:13:01 +02001317 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001318 drop_focus_state(shell, from, surface);
1319
1320 if (shell->workspaces.anim_from == to &&
1321 shell->workspaces.anim_to == from) {
1322 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001323
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001324 return;
1325 }
1326
1327 if (shell->workspaces.anim_to != NULL)
1328 finish_workspace_change_animation(shell,
1329 shell->workspaces.anim_from,
1330 shell->workspaces.anim_to);
1331
1332 if (workspace_is_empty(from) &&
1333 workspace_has_only(to, surface))
1334 update_workspace(shell, index, from, to);
1335 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001336 if (shsurf != NULL &&
1337 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001338 wl_list_insert(&shell->workspaces.anim_sticky_list,
1339 &shsurf->workspace_transform.link);
1340
1341 animate_workspace_change(shell, index, from, to);
1342 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001343
Jonas Ådahl8538b222012-08-29 22:13:03 +02001344 state = ensure_focus_state(shell, seat);
1345 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001346 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001347}
1348
1349static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02001350touch_move_grab_down(struct weston_touch_grab *grab,
1351 const struct timespec *time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001352 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001353{
1354}
1355
1356static void
Alexandros Frantzis27a51b82017-11-16 18:20:59 +02001357touch_move_grab_up(struct weston_touch_grab *grab, const struct timespec *time,
1358 int touch_id)
Rusty Lynch1084da52013-08-15 09:10:08 -07001359{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001360 struct weston_touch_move_grab *move =
1361 (struct weston_touch_move_grab *) container_of(
1362 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001363
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001364 if (touch_id == 0)
1365 move->active = 0;
1366
Jonas Ådahl9484b692013-12-02 22:05:03 +01001367 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001368 shell_touch_grab_end(&move->base);
1369 free(move);
1370 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001371}
1372
1373static void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +02001374touch_move_grab_motion(struct weston_touch_grab *grab,
1375 const struct timespec *time, int touch_id,
1376 wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001377{
1378 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1379 struct shell_surface *shsurf = move->base.shsurf;
1380 struct weston_surface *es;
1381 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1382 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1383
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001384 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001385 return;
1386
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001387 es = weston_desktop_surface_get_surface(shsurf->desktop_surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001388
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001389 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001390
1391 weston_compositor_schedule_repaint(es->compositor);
1392}
1393
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001394static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001395touch_move_grab_frame(struct weston_touch_grab *grab)
1396{
1397}
1398
1399static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001400touch_move_grab_cancel(struct weston_touch_grab *grab)
1401{
1402 struct weston_touch_move_grab *move =
1403 (struct weston_touch_move_grab *) container_of(
1404 grab, struct shell_touch_grab, grab);
1405
1406 shell_touch_grab_end(&move->base);
1407 free(move);
1408}
1409
Rusty Lynch1084da52013-08-15 09:10:08 -07001410static const struct weston_touch_grab_interface touch_move_grab_interface = {
1411 touch_move_grab_down,
1412 touch_move_grab_up,
1413 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001414 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001415 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001416};
1417
1418static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001419surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001420{
1421 struct weston_touch_move_grab *move;
1422
1423 if (!shsurf)
1424 return -1;
1425
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001426 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1427 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Rusty Lynch1084da52013-08-15 09:10:08 -07001428 return 0;
1429
1430 move = malloc(sizeof *move);
1431 if (!move)
1432 return -1;
1433
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001434 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001435 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001436 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001437 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001438 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001439
1440 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001441 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001442
1443 return 0;
1444}
1445
1446static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001447noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001448{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001449}
1450
1451static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001452noop_grab_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02001453 const struct timespec *time,
1454 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001455{
1456}
1457
1458static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001459noop_grab_axis_source(struct weston_pointer_grab *grab,
1460 uint32_t source)
1461{
1462}
1463
1464static void
1465noop_grab_frame(struct weston_pointer_grab *grab)
1466{
1467}
1468
1469static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001470constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1471{
1472 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001473 struct weston_surface *surface =
1474 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001475 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001476 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001477 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001478 pixman_rectangle32_t area;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001479 struct weston_geometry geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001480
1481 x = wl_fixed_to_int(pointer->x + move->dx);
1482 y = wl_fixed_to_int(pointer->y + move->dy);
1483
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001484 if (shsurf->shell->panel_position ==
1485 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001486 get_output_work_area(shsurf->shell, surface->output, &area);
1487 geometry =
1488 weston_desktop_surface_get_geometry(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001489
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001490 bottom = y + geometry.height + geometry.y;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001491 if (bottom - safety < area.y)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001492 y = area.y + safety - geometry.height
1493 - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001494
1495 if (move->client_initiated &&
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001496 y + geometry.y < area.y)
1497 y = area.y - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001498 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001499
1500 *cx = x;
1501 *cy = y;
1502}
1503
1504static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001505move_grab_motion(struct weston_pointer_grab *grab,
1506 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001507 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001508{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001509 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001510 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001511 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001512 struct weston_surface *surface;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001513 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001514
Jonas Ådahld2510102014-10-05 21:39:14 +02001515 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001516 if (!shsurf)
1517 return;
1518
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001519 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1520
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001521 constrain_position(move, &cx, &cy);
1522
1523 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001524
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001525 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001526}
1527
1528static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001529move_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001530 const struct timespec *time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001531{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001532 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1533 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001534 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001535 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001536
Daniel Stone4dbadb12012-05-30 16:31:51 +01001537 if (pointer->button_count == 0 &&
1538 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001539 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001540 free(grab);
1541 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001542}
1543
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001544static void
1545move_grab_cancel(struct weston_pointer_grab *grab)
1546{
1547 struct shell_grab *shell_grab =
1548 container_of(grab, struct shell_grab, grab);
1549
1550 shell_grab_end(shell_grab);
1551 free(grab);
1552}
1553
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001554static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001555 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001556 move_grab_motion,
1557 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001558 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001559 noop_grab_axis_source,
1560 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001561 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001562};
1563
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001564static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001565surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001566 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001567{
1568 struct weston_move_grab *move;
1569
1570 if (!shsurf)
1571 return -1;
1572
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001573 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001574 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1575 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001576 return 0;
1577
1578 move = malloc(sizeof *move);
1579 if (!move)
1580 return -1;
1581
Jason Ekstranda7af7042013-10-12 22:38:11 -05001582 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001583 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001584 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001585 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001586 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001587
1588 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001589 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001590
1591 return 0;
1592}
1593
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001594struct weston_resize_grab {
1595 struct shell_grab base;
1596 uint32_t edges;
1597 int32_t width, height;
1598};
1599
1600static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001601resize_grab_motion(struct weston_pointer_grab *grab,
1602 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001603 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001604{
1605 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001606 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001607 struct shell_surface *shsurf = resize->base.shsurf;
1608 int32_t width, height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001609 struct weston_size min_size, max_size;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001610 wl_fixed_t from_x, from_y;
1611 wl_fixed_t to_x, to_y;
1612
Jonas Ådahld2510102014-10-05 21:39:14 +02001613 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001614
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001615 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001616 return;
1617
Jason Ekstranda7af7042013-10-12 22:38:11 -05001618 weston_view_from_global_fixed(shsurf->view,
1619 pointer->grab_x, pointer->grab_y,
1620 &from_x, &from_y);
1621 weston_view_from_global_fixed(shsurf->view,
1622 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001623
1624 width = resize->width;
1625 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1626 width += wl_fixed_to_int(from_x - to_x);
1627 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1628 width += wl_fixed_to_int(to_x - from_x);
1629 }
1630
1631 height = resize->height;
1632 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1633 height += wl_fixed_to_int(from_y - to_y);
1634 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1635 height += wl_fixed_to_int(to_y - from_y);
1636 }
1637
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001638 max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
1639 min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
1640
1641 min_size.width = MAX(1, min_size.width);
1642 min_size.height = MAX(1, min_size.height);
1643
1644 if (width < min_size.width)
1645 width = min_size.width;
1646 else if (max_size.width > 0 && width > max_size.width)
1647 width = max_size.width;
1648 if (height < min_size.height)
1649 height = min_size.height;
1650 else if (max_size.width > 0 && width > max_size.width)
1651 width = max_size.width;
1652 weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001653}
1654
1655static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001656resize_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001657 const struct timespec *time,
1658 uint32_t button, uint32_t state_w)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001659{
1660 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001661 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001662 enum wl_pointer_button_state state = state_w;
1663
1664 if (pointer->button_count == 0 &&
1665 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Greg Vec3f7792018-11-08 00:24:56 +03001666 if (resize->base.shsurf != NULL) {
1667 struct weston_desktop_surface *desktop_surface =
1668 resize->base.shsurf->desktop_surface;
1669 weston_desktop_surface_set_resizing(desktop_surface,
1670 false);
1671 }
1672
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001673 shell_grab_end(&resize->base);
1674 free(grab);
1675 }
1676}
1677
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001678static void
1679resize_grab_cancel(struct weston_pointer_grab *grab)
1680{
1681 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1682
Greg Vec3f7792018-11-08 00:24:56 +03001683 if (resize->base.shsurf != NULL) {
1684 struct weston_desktop_surface *desktop_surface =
1685 resize->base.shsurf->desktop_surface;
1686 weston_desktop_surface_set_resizing(desktop_surface, false);
1687 }
1688
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001689 shell_grab_end(&resize->base);
1690 free(grab);
1691}
1692
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001693static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001694 noop_grab_focus,
1695 resize_grab_motion,
1696 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001697 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001698 noop_grab_axis_source,
1699 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001700 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001701};
1702
Giulio Camuffob8366642013-04-25 13:57:46 +03001703
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001704static int
1705surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001706 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001707{
1708 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001709 const unsigned resize_topbottom =
1710 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1711 const unsigned resize_leftright =
1712 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1713 const unsigned resize_any = resize_topbottom | resize_leftright;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001714 struct weston_geometry geometry;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001715
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001716 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001717 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1718 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001719 return 0;
1720
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001721 /* Check for invalid edge combinations. */
1722 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1723 (edges & resize_topbottom) == resize_topbottom ||
1724 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001725 return 0;
1726
1727 resize = malloc(sizeof *resize);
1728 if (!resize)
1729 return -1;
1730
1731 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001732
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001733 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1734 resize->width = geometry.width;
1735 resize->height = geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001736
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001737 shsurf->resize_edges = edges;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001738 weston_desktop_surface_set_resizing(shsurf->desktop_surface, true);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001739 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001740 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001741
1742 return 0;
1743}
1744
1745static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001746busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001747{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001748 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001749 struct weston_pointer *pointer = base->pointer;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001750 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001751 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001752 wl_fixed_t sx, sy;
1753
Jason Ekstranda7af7042013-10-12 22:38:11 -05001754 view = weston_compositor_pick_view(pointer->seat->compositor,
1755 pointer->x, pointer->y,
1756 &sx, &sy);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001757 desktop_surface = weston_surface_get_desktop_surface(view->surface);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001758
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001759 if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001760 shell_grab_end(grab);
1761 free(grab);
1762 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001763}
1764
1765static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001766busy_cursor_grab_motion(struct weston_pointer_grab *grab,
1767 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001768 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001769{
Jonas Ådahld2510102014-10-05 21:39:14 +02001770 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001771}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001772
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001773static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001774busy_cursor_grab_button(struct weston_pointer_grab *base,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001775 const struct timespec *time,
1776 uint32_t button, uint32_t state)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001777{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001778 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001779 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05001780 struct weston_pointer *pointer = grab->grab.pointer;
1781 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001782
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001783 if (shsurf && button == BTN_LEFT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001784 activate(shsurf->shell, shsurf->view, seat,
1785 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman794fa0e2015-07-15 13:00:38 -05001786 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001787 } else if (shsurf && button == BTN_RIGHT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001788 activate(shsurf->shell, shsurf->view, seat,
1789 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman74de4692015-07-15 13:00:39 -05001790 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001791 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001792}
1793
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001794static void
1795busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1796{
1797 struct shell_grab *grab = (struct shell_grab *) base;
1798
1799 shell_grab_end(grab);
1800 free(grab);
1801}
1802
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001803static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001804 busy_cursor_grab_focus,
1805 busy_cursor_grab_motion,
1806 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001807 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001808 noop_grab_axis_source,
1809 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001810 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001811};
1812
1813static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001814handle_pointer_focus(struct wl_listener *listener, void *data)
1815{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001816 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001817 struct weston_view *view = pointer->focus;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001818 struct shell_surface *shsurf;
1819 struct weston_desktop_client *client;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001820
Jason Ekstranda7af7042013-10-12 22:38:11 -05001821 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001822 return;
1823
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001824 shsurf = get_shell_surface(view->surface);
1825 if (!shsurf)
1826 return;
1827
1828 client = weston_desktop_surface_get_client(shsurf->desktop_surface);
1829
1830 if (shsurf->unresponsive)
1831 set_busy_cursor(shsurf, pointer);
1832 else
1833 weston_desktop_client_ping(client);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001834}
1835
1836static void
Marius Vladab39e1d2021-03-05 21:40:22 +02001837shell_surface_deactivate(struct shell_surface *shsurf)
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001838{
1839 if (--shsurf->focus_count == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001840 weston_desktop_surface_set_activated(shsurf->desktop_surface, false);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001841}
1842
1843static void
Marius Vladab39e1d2021-03-05 21:40:22 +02001844shell_surface_activate(struct shell_surface *shsurf)
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001845{
1846 if (shsurf->focus_count++ == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001847 weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001848}
1849
Philip Withnall07926d92013-11-25 18:01:40 +00001850/* The surface will be inserted into the list immediately after the link
1851 * returned by this function (i.e. will be stacked immediately above the
1852 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001853static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00001854shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1855{
1856 struct workspace *ws;
1857
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001858 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
1859 !shsurf->state.lowered) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001860 return &shsurf->shell->fullscreen_layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001861 }
1862
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001863 /* Move the surface to a normal workspace layer so that surfaces
1864 * which were previously fullscreen or transient are no longer
1865 * rendered on top. */
1866 ws = get_current_workspace(shsurf->shell);
1867 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001868}
1869
Philip Withnall648a4dd2013-11-25 18:01:44 +00001870static void
1871shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
1872{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001873 weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001874}
1875
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001876/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00001877 * geometry to ensure the changes are redrawn.
1878 *
1879 * If any child surfaces exist and are mapped, ensure they’re in the same layer
1880 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001881static void
1882shell_surface_update_layer(struct shell_surface *shsurf)
1883{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001884 struct weston_surface *surface =
1885 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001886 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001887
1888 new_layer_link = shell_surface_calculate_layer_link(shsurf);
1889
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001890 if (new_layer_link == NULL)
1891 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001892 if (new_layer_link == &shsurf->view->layer_link)
1893 return;
1894
1895 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001896 weston_layer_entry_remove(&shsurf->view->layer_link);
1897 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001898 weston_view_geometry_dirty(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001899 weston_surface_damage(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001900
1901 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001902}
1903
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001904static void
Semi Malinen99f8c082018-05-02 11:10:32 +02001905notify_output_destroy(struct wl_listener *listener, void *data)
1906{
1907 struct shell_surface *shsurf =
1908 container_of(listener,
1909 struct shell_surface, output_destroy_listener);
1910
1911 shsurf->output = NULL;
1912 shsurf->output_destroy_listener.notify = NULL;
1913}
1914
1915static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00001916shell_surface_set_output(struct shell_surface *shsurf,
1917 struct weston_output *output)
1918{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001919 struct weston_surface *es =
1920 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001921
1922 /* get the default output, if the client set it as NULL
Abdur Rehman7f1da1f2017-01-01 19:46:33 +05001923 check whether the output is available */
Philip Withnall352e7ed2013-11-25 18:01:35 +00001924 if (output)
1925 shsurf->output = output;
1926 else if (es->output)
1927 shsurf->output = es->output;
1928 else
1929 shsurf->output = get_default_output(es->compositor);
Semi Malinen99f8c082018-05-02 11:10:32 +02001930
1931 if (shsurf->output_destroy_listener.notify) {
1932 wl_list_remove(&shsurf->output_destroy_listener.link);
1933 shsurf->output_destroy_listener.notify = NULL;
1934 }
1935
1936 if (!shsurf->output)
1937 return;
1938
1939 shsurf->output_destroy_listener.notify = notify_output_destroy;
1940 wl_signal_add(&shsurf->output->destroy_signal,
1941 &shsurf->output_destroy_listener);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001942}
1943
1944static void
leng.fang32af9fc2024-06-13 11:22:15 +08001945weston_view_set_initial_position(struct shell_surface *shsurf,
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001946 struct desktop_shell *shell);
1947
1948static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001949unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08001950{
Philip Withnallf85fe842013-11-25 18:01:37 +00001951 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wu4539b082012-03-01 12:57:46 +08001952 wl_list_remove(&shsurf->fullscreen.transform.link);
1953 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00001954
Jason Ekstranda7af7042013-10-12 22:38:11 -05001955 if (shsurf->fullscreen.black_view)
1956 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
1957 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00001958
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001959 if (shsurf->saved_position_valid)
1960 weston_view_set_position(shsurf->view,
1961 shsurf->saved_x, shsurf->saved_y);
1962 else
leng.fang32af9fc2024-06-13 11:22:15 +08001963 weston_view_set_initial_position(shsurf, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02001964 shsurf->saved_position_valid = false;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001965
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001966 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001967 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00001968 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08001969 shsurf->saved_rotation_valid = false;
1970 }
Alex Wu4539b082012-03-01 12:57:46 +08001971}
1972
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001973static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00001974unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001975{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001976 struct weston_surface *surface =
1977 weston_desktop_surface_get_surface(shsurf->desktop_surface);
1978
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001979 /* undo all maximized things here */
Semi Malinen99f8c082018-05-02 11:10:32 +02001980 shell_surface_set_output(shsurf, get_default_output(surface->compositor));
Zhang, Xiong Y31236932013-12-13 22:10:57 +02001981
1982 if (shsurf->saved_position_valid)
1983 weston_view_set_position(shsurf->view,
1984 shsurf->saved_x, shsurf->saved_y);
1985 else
leng.fang32af9fc2024-06-13 11:22:15 +08001986 weston_view_set_initial_position(shsurf, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02001987 shsurf->saved_position_valid = false;
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001988
1989 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001990 wl_list_insert(&shsurf->view->geometry.transformation_list,
1991 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001992 shsurf->saved_rotation_valid = false;
1993 }
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01001994}
1995
Philip Withnallbecb77e2013-11-25 18:01:30 +00001996static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01001997set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01001998{
1999 struct shell_surface *shsurf;
2000 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002001 struct weston_view *view;
Jonas Ådahl56958aa2021-10-01 11:12:21 +02002002 struct weston_subsurface *subsurface;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002003
2004 view = get_default_view(surface);
2005 if (!view)
2006 return;
2007
2008 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2009
2010 shsurf = get_shell_surface(surface);
2011 current_ws = get_current_workspace(shsurf->shell);
2012
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002013 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002014 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002015
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002016 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002017 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002018
2019 shell_surface_update_child_surface_layers(shsurf);
Jonas Ådahl56958aa2021-10-01 11:12:21 +02002020
2021 weston_view_damage_below(shsurf->view);
2022 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
2023 wl_list_for_each(view, &subsurface->surface->views, surface_link)
2024 weston_view_damage_below(view);
2025 }
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002026}
2027
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002028
Tiago Vignattibe143262012-04-16 17:31:41 +03002029static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002030shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002031{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002032 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002033}
2034
Pekka Paalanen8274d902014-08-06 19:36:51 +03002035static int
2036black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2037{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002038 struct weston_view *fs_view = surface->committed_private;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002039 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002040 int n;
2041 int rem;
2042 int ret;
2043
2044 n = snprintf(buf, len, "black background surface for ");
2045 if (n < 0)
2046 return n;
2047
2048 rem = (int)len - n;
2049 if (rem < 0)
2050 rem = 0;
2051
2052 if (fs_surface->get_label)
2053 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2054 else
2055 ret = snprintf(buf + n, rem, "<unknown>");
2056
2057 if (ret < 0)
2058 return n;
2059
2060 return n + ret;
2061}
2062
Alex Wu21858432012-04-01 20:13:08 +08002063static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002064black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002065
Jason Ekstranda7af7042013-10-12 22:38:11 -05002066static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002067create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002068 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002069 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002070{
Marius Vlad2be09372021-03-08 22:45:52 +02002071 struct weston_solid_color_surface surface_data = {};
Alex Wu4539b082012-03-01 12:57:46 +08002072
Marius Vlad2be09372021-03-08 22:45:52 +02002073 surface_data.surface_committed = black_surface_committed;
2074 surface_data.get_label = black_surface_get_label;
2075 surface_data.surface_private = fs_view;
Alex Wu4539b082012-03-01 12:57:46 +08002076
Marius Vlad2be09372021-03-08 22:45:52 +02002077 surface_data.r = 0;
2078 surface_data.g = 0;
2079 surface_data.b = 0;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002080
Marius Vlad2be09372021-03-08 22:45:52 +02002081 struct weston_view *view =
2082 create_solid_color_surface(ec, &surface_data, x, y, w, h);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002083
2084 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002085}
2086
Philip Withnalled8f1a92013-11-25 18:01:43 +00002087static void
2088shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2089{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002090 struct weston_surface *surface =
2091 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002092 struct weston_output *output = shsurf->fullscreen_output;
2093
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002094 assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
Philip Withnalled8f1a92013-11-25 18:01:43 +00002095
2096 if (!shsurf->fullscreen.black_view)
2097 shsurf->fullscreen.black_view =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002098 create_black_surface(surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002099 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002100 output->x, output->y,
2101 output->width,
2102 output->height);
2103
2104 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002105 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2106 weston_layer_entry_insert(&shsurf->view->layer_link,
2107 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002108 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002109 weston_surface_damage(surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002110
Armin Krezović4663aca2016-06-30 06:04:29 +02002111 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002112 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002113}
2114
Alex Wu4539b082012-03-01 12:57:46 +08002115/* Create black surface and append it to the associated fullscreen surface.
2116 * Handle size dismatch and positioning according to the method. */
2117static void
2118shell_configure_fullscreen(struct shell_surface *shsurf)
2119{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002120 struct weston_surface *surface =
2121 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffob8366642013-04-25 13:57:46 +03002122 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002123
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002124 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002125 weston_layer_entry_remove(&shsurf->view->layer_link);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002126 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2127 &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002128
Pekka Paalanenc63acc42018-05-02 10:21:58 +02002129 if (!shsurf->fullscreen_output) {
2130 /* If there is no output, there's not much we can do.
2131 * Position the window somewhere, whatever. */
2132 weston_view_set_position(shsurf->view, 0, 0);
2133 return;
2134 }
2135
Philip Withnalled8f1a92013-11-25 18:01:43 +00002136 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002137
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002138 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002139 &surf_width, &surf_height);
2140
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002141 if (surface->buffer_ref.buffer)
2142 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002143}
2144
Alex Wu4539b082012-03-01 12:57:46 +08002145static void
2146shell_map_fullscreen(struct shell_surface *shsurf)
2147{
Alex Wubd3354b2012-04-17 17:20:49 +08002148 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002149}
2150
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002151static void
Marius Vladc114fd62021-08-26 16:37:24 +03002152desktop_shell_destroy_seat(struct shell_seat *shseat)
2153{
Marius Vladf3584cf2021-10-26 19:40:44 +03002154
Marius Vlada7392c82021-08-26 16:38:43 +03002155 wl_list_remove(&shseat->keyboard_focus_listener.link);
2156 wl_list_remove(&shseat->caps_changed_listener.link);
2157 wl_list_remove(&shseat->pointer_focus_listener.link);
Marius Vladc114fd62021-08-26 16:37:24 +03002158 wl_list_remove(&shseat->seat_destroy_listener.link);
2159
2160 wl_list_remove(&shseat->link);
2161 free(shseat);
2162}
2163
2164static void
Giulio Camuffo5085a752013-03-25 21:42:45 +01002165destroy_shell_seat(struct wl_listener *listener, void *data)
2166{
2167 struct shell_seat *shseat =
2168 container_of(listener,
2169 struct shell_seat, seat_destroy_listener);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002170
Marius Vladc114fd62021-08-26 16:37:24 +03002171 desktop_shell_destroy_seat(shseat);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002172}
2173
Jason Ekstrand024177c2014-04-21 19:42:58 -05002174static void
2175shell_seat_caps_changed(struct wl_listener *listener, void *data)
2176{
Derek Foreman1281a362015-07-31 16:55:32 -05002177 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05002178 struct shell_seat *seat;
2179
2180 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002181 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002182
Derek Foreman1281a362015-07-31 16:55:32 -05002183 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002184 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002185 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002186 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002187 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05002188 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002189 wl_list_init(&seat->pointer_focus_listener.link);
2190 }
2191}
2192
Giulio Camuffo5085a752013-03-25 21:42:45 +01002193static struct shell_seat *
Marius Vladc114fd62021-08-26 16:37:24 +03002194create_shell_seat(struct desktop_shell *shell, struct weston_seat *seat)
Giulio Camuffo5085a752013-03-25 21:42:45 +01002195{
2196 struct shell_seat *shseat;
2197
2198 shseat = calloc(1, sizeof *shseat);
2199 if (!shseat) {
2200 weston_log("no memory to allocate shell seat\n");
2201 return NULL;
2202 }
2203
2204 shseat->seat = seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002205
2206 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2207 wl_signal_add(&seat->destroy_signal,
2208 &shseat->seat_destroy_listener);
2209
Jason Ekstrand024177c2014-04-21 19:42:58 -05002210 wl_list_init(&shseat->keyboard_focus_listener.link);
2211
2212 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2213 wl_list_init(&shseat->pointer_focus_listener.link);
2214
2215 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2216 wl_signal_add(&seat->updated_caps_signal,
2217 &shseat->caps_changed_listener);
2218 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2219
Marius Vladc114fd62021-08-26 16:37:24 +03002220 wl_list_insert(&shell->seat_list, &shseat->link);
2221
Giulio Camuffo5085a752013-03-25 21:42:45 +01002222 return shseat;
2223}
2224
2225static struct shell_seat *
2226get_shell_seat(struct weston_seat *seat)
2227{
2228 struct wl_listener *listener;
2229
Marius Vlad137c7932021-11-20 19:14:45 +02002230 if (!seat)
2231 return NULL;
2232
Giulio Camuffo5085a752013-03-25 21:42:45 +01002233 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Marius Vlad137c7932021-11-20 19:14:45 +02002234 if (!listener)
2235 return NULL;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002236
2237 return container_of(listener,
2238 struct shell_seat, seat_destroy_listener);
2239}
2240
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002241static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002242fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002243{
2244 struct shell_surface *shsurf = data;
Marius Vladd25d63e2021-11-10 17:50:50 +02002245 desktop_shell_destroy_surface(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002246}
2247
2248static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002249fade_out_done(struct weston_view_animation *animation, void *data)
2250{
2251 struct shell_surface *shsurf = data;
2252 struct wl_event_loop *loop;
2253
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002254 loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
Derek Foreman039e9be2015-04-14 17:09:06 -05002255
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002256 if (weston_view_is_mapped(shsurf->view)) {
Tomohito Esaki5898cd32019-04-01 17:50:14 +09002257 weston_view_unmap(shsurf->view);
Derek Foreman039e9be2015-04-14 17:09:06 -05002258 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
Derek Foreman039e9be2015-04-14 17:09:06 -05002259 }
2260}
2261
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002262struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002263get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002264{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002265 if (weston_surface_is_desktop_surface(surface)) {
2266 struct weston_desktop_surface *desktop_surface =
2267 weston_surface_get_desktop_surface(surface);
2268 return weston_desktop_surface_get_user_data(desktop_surface);
2269 }
2270 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002271}
2272
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002273/*
2274 * libweston-desktop
2275 */
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002276
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002277static void
2278desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2279 void *shell)
2280{
2281 struct weston_desktop_client *client =
2282 weston_desktop_surface_get_client(desktop_surface);
2283 struct wl_client *wl_client =
2284 weston_desktop_client_get_client(client);
2285 struct weston_view *view;
2286 struct shell_surface *shsurf;
2287 struct weston_surface *surface =
2288 weston_desktop_surface_get_surface(desktop_surface);
2289
2290 view = weston_desktop_surface_create_view(desktop_surface);
2291 if (!view)
2292 return;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002293
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002294 shsurf = calloc(1, sizeof *shsurf);
2295 if (!shsurf) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002296 if (wl_client)
2297 wl_client_post_no_memory(wl_client);
2298 else
2299 weston_log("no memory to allocate shell surface\n");
2300 return;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002301 }
2302
Marius Vlad2be09372021-03-08 22:45:52 +02002303 weston_surface_set_label_func(surface, surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002304
Tiago Vignattibc052c92012-04-19 16:18:18 +03002305 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002306 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002307 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002308 shsurf->saved_rotation_valid = false;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002309 shsurf->desktop_surface = desktop_surface;
2310 shsurf->view = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002311 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002312 wl_list_init(&shsurf->fullscreen.transform.link);
2313
Semi Malinen99f8c082018-05-02 11:10:32 +02002314 shell_surface_set_output(
2315 shsurf, get_default_output(shsurf->shell->compositor));
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002316
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002317 wl_signal_init(&shsurf->destroy_signal);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002318
Pekka Paalanen460099f2012-01-20 16:48:25 +02002319 /* empty when not in use */
2320 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002321 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002322
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002323 wl_list_init(&shsurf->workspace_transform.link);
2324
Stefan Agnera8da2082019-06-23 14:53:59 +02002325 /*
2326 * initialize list as well as link. The latter allows to use
2327 * wl_list_remove() even when this surface is not in another list.
2328 */
2329 wl_list_init(&shsurf->children_list);
2330 wl_list_init(&shsurf->children_link);
2331
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002332 weston_desktop_surface_set_user_data(desktop_surface, shsurf);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002333}
2334
Tiago Vignattibc052c92012-04-19 16:18:18 +03002335static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002336desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2337 void *shell)
2338{
2339 struct shell_surface *shsurf =
2340 weston_desktop_surface_get_user_data(desktop_surface);
2341 struct weston_surface *surface =
2342 weston_desktop_surface_get_surface(desktop_surface);
Marius Vladf12697b2021-03-05 21:44:26 +02002343 struct weston_seat *seat;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002344
2345 if (!shsurf)
2346 return;
2347
Marius Vladf12697b2021-03-05 21:44:26 +02002348 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2349 struct shell_seat *shseat = get_shell_seat(seat);
2350 /* activate() controls the focused surface activation and
2351 * removal of a surface requires invalidating the
2352 * focused_surface to avoid activate() use a stale (and just
2353 * removed) surface when attempting to de-activate it. It will
2354 * also update the focused_surface once it has a chance to run.
2355 */
Marius Vlad137c7932021-11-20 19:14:45 +02002356 if (shseat && surface == shseat->focused_surface)
Marius Vladf12697b2021-03-05 21:44:26 +02002357 shseat->focused_surface = NULL;
2358 }
2359
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002360 if (shsurf->fullscreen.black_view)
2361 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2362
2363 weston_surface_set_label_func(surface, NULL);
2364 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2365 shsurf->desktop_surface = NULL;
2366
Quentin Glidicf01ecee2016-08-16 10:52:46 +02002367 weston_desktop_surface_unlink_view(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002368 if (weston_surface_is_mapped(surface) &&
2369 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
Erik Kurzinger04918f32021-05-18 11:49:37 -04002370
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02002371 if (shsurf->shell->compositor->state == WESTON_COMPOSITOR_ACTIVE) {
2372 pixman_region32_fini(&surface->pending.input);
2373 pixman_region32_init(&surface->pending.input);
2374 pixman_region32_fini(&surface->input);
2375 pixman_region32_init(&surface->input);
2376 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2377 fade_out_done, shsurf);
2378 return;
2379 } else {
Marius Vladc30cb292021-08-02 15:47:16 +03002380 weston_surface_destroy(surface);
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02002381 }
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002382 }
Erik Kurzinger04918f32021-05-18 11:49:37 -04002383
Marius Vladd25d63e2021-11-10 17:50:50 +02002384 desktop_shell_destroy_surface(shsurf);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002385}
2386
2387static void
2388set_maximized_position(struct desktop_shell *shell,
2389 struct shell_surface *shsurf)
2390{
2391 pixman_rectangle32_t area;
2392 struct weston_geometry geometry;
2393
2394 get_output_work_area(shell, shsurf->output, &area);
2395 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2396
2397 weston_view_set_position(shsurf->view,
2398 area.x - geometry.x,
2399 area.y - geometry.y);
2400}
2401
2402static void
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002403set_position_from_xwayland(struct shell_surface *shsurf)
2404{
2405 struct weston_geometry geometry;
2406 float x;
2407 float y;
2408
2409 assert(shsurf->xwayland.is_set);
2410
2411 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2412 x = shsurf->xwayland.x - geometry.x;
2413 y = shsurf->xwayland.y - geometry.y;
2414
2415 weston_view_set_position(shsurf->view, x, y);
2416
2417#ifdef WM_DEBUG
2418 weston_log("%s: XWM %d, %d; geometry %d, %d; view %f, %f\n",
2419 __func__, shsurf->xwayland.x, shsurf->xwayland.y,
2420 geometry.x, geometry.y, x, y);
2421#endif
2422}
2423
2424static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002425map(struct desktop_shell *shell, struct shell_surface *shsurf,
2426 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002427{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002428 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002429 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2430 struct weston_compositor *compositor = shell->compositor;
2431 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002432
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002433 /* initial positioning, see also configure() */
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002434 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002435 center_on_output(shsurf->view, shsurf->fullscreen_output);
2436 shell_map_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002437 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002438 set_maximized_position(shell, shsurf);
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002439 } else if (shsurf->xwayland.is_set) {
2440 set_position_from_xwayland(shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002441 } else {
leng.fang32af9fc2024-06-13 11:22:15 +08002442 weston_view_set_initial_position(shsurf, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002443 }
2444
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002445 /* Surface stacking order, see also activate(). */
2446 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002447
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002448 weston_view_update_transform(shsurf->view);
2449 shsurf->view->is_mapped = true;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002450 if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002451 surface->output = shsurf->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02002452 weston_view_set_output(shsurf->view, shsurf->output);
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002453 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002454
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002455 if (!shell->locked) {
2456 wl_list_for_each(seat, &compositor->seat_list, link)
2457 activate(shell, shsurf->view, seat,
2458 WESTON_ACTIVATE_FLAG_CONFIGURE);
2459 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002460
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002461 if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002462 switch (shell->win_animation_type) {
2463 case ANIMATION_FADE:
2464 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2465 break;
2466 case ANIMATION_ZOOM:
2467 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2468 break;
2469 case ANIMATION_NONE:
2470 default:
2471 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002472 }
2473 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002474}
2475
2476static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002477desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2478 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002479{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002480 struct shell_surface *shsurf =
2481 weston_desktop_surface_get_user_data(desktop_surface);
2482 struct weston_surface *surface =
2483 weston_desktop_surface_get_surface(desktop_surface);
2484 struct weston_view *view = shsurf->view;
2485 struct desktop_shell *shell = data;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002486 bool was_fullscreen;
2487 bool was_maximized;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002488
2489 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002490 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002491
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002492 was_fullscreen = shsurf->state.fullscreen;
2493 was_maximized = shsurf->state.maximized;
2494
2495 shsurf->state.fullscreen =
2496 weston_desktop_surface_get_fullscreen(desktop_surface);
2497 shsurf->state.maximized =
2498 weston_desktop_surface_get_maximized(desktop_surface);
2499
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002500 if (!weston_surface_is_mapped(surface)) {
2501 map(shell, shsurf, sx, sy);
2502 surface->is_mapped = true;
2503 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2504 ++surface->ref_count;
2505 return;
2506 }
2507
2508 if (sx == 0 && sy == 0 &&
2509 shsurf->last_width == surface->width &&
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002510 shsurf->last_height == surface->height &&
2511 was_fullscreen == shsurf->state.fullscreen &&
2512 was_maximized == shsurf->state.maximized)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002513 return;
2514
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002515 if (was_fullscreen)
2516 unset_fullscreen(shsurf);
2517 if (was_maximized)
2518 unset_maximized(shsurf);
2519
Quentin Glidic920cf042016-08-16 14:26:20 +02002520 if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2521 !shsurf->saved_position_valid) {
2522 shsurf->saved_x = shsurf->view->geometry.x;
2523 shsurf->saved_y = shsurf->view->geometry.y;
2524 shsurf->saved_position_valid = true;
2525
2526 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2527 wl_list_remove(&shsurf->rotation.transform.link);
2528 wl_list_init(&shsurf->rotation.transform.link);
2529 weston_view_geometry_dirty(shsurf->view);
2530 shsurf->saved_rotation_valid = true;
2531 }
2532 }
2533
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002534 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002535 shell_configure_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002536 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002537 set_maximized_position(shell, shsurf);
2538 surface->output = shsurf->output;
2539 } else {
2540 float from_x, from_y;
2541 float to_x, to_y;
2542 float x, y;
2543
2544 if (shsurf->resize_edges) {
2545 sx = 0;
2546 sy = 0;
2547 }
2548
2549 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2550 sx = shsurf->last_width - surface->width;
2551 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2552 sy = shsurf->last_height - surface->height;
2553
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002554 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2555 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2556 x = shsurf->view->geometry.x + to_x - from_x;
2557 y = shsurf->view->geometry.y + to_y - from_y;
2558
2559 weston_view_set_position(shsurf->view, x, y);
2560 }
2561
Emmanuel Gil Peyrotc3941792017-04-04 18:49:33 +01002562 shsurf->last_width = surface->width;
2563 shsurf->last_height = surface->height;
2564
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002565 /* XXX: would a fullscreen surface need the same handling? */
2566 if (surface->output) {
2567 wl_list_for_each(view, &surface->views, surface_link)
2568 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002569 }
2570}
2571
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002572static void
Derek Foreman927d9e22017-10-06 14:37:44 -05002573get_maximized_size(struct shell_surface *shsurf, int32_t *width, int32_t *height)
2574{
2575 struct desktop_shell *shell;
2576 pixman_rectangle32_t area;
2577
2578 shell = shell_surface_get_shell(shsurf);
2579 get_output_work_area(shell, shsurf->output, &area);
2580
2581 *width = area.width;
2582 *height = area.height;
2583}
2584
2585static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002586set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2587 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002588{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002589 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2590 struct weston_surface *surface =
2591 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2592 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002593
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002594 if (fullscreen) {
2595 /* handle clients launching in fullscreen */
2596 if (output == NULL && !weston_surface_is_mapped(surface)) {
2597 /* Set the output to the one that has focus currently. */
2598 output = get_focused_output(surface->compositor);
2599 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002600
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002601 shell_surface_set_output(shsurf, output);
2602 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002603
Marius Vlad6ebda362020-03-28 00:23:14 +02002604 if (shsurf->output) {
2605 width = shsurf->output->width;
2606 height = shsurf->output->height;
2607 }
Derek Foremane1af3d82017-10-06 14:37:45 -05002608 } else if (weston_desktop_surface_get_maximized(desktop_surface)) {
2609 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002610 }
2611 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2612 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002613}
2614
2615static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002616desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2617 struct weston_seat *seat, uint32_t serial, void *shell)
2618{
2619 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2620 struct weston_touch *touch = weston_seat_get_touch(seat);
2621 struct shell_surface *shsurf =
2622 weston_desktop_surface_get_user_data(desktop_surface);
2623 struct weston_surface *surface =
2624 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2625 struct wl_resource *resource = surface->resource;
2626 struct weston_surface *focus;
2627
2628 if (pointer &&
2629 pointer->focus &&
2630 pointer->button_count > 0 &&
2631 pointer->grab_serial == serial) {
2632 focus = weston_surface_get_main_surface(pointer->focus->surface);
2633 if ((focus == surface) &&
2634 (surface_move(shsurf, pointer, true) < 0))
2635 wl_resource_post_no_memory(resource);
2636 } else if (touch &&
2637 touch->focus &&
2638 touch->grab_serial == serial) {
2639 focus = weston_surface_get_main_surface(touch->focus->surface);
2640 if ((focus == surface) &&
2641 (surface_touch_move(shsurf, touch) < 0))
2642 wl_resource_post_no_memory(resource);
2643 }
2644}
2645
2646static void
2647desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2648 struct weston_seat *seat, uint32_t serial,
2649 enum weston_desktop_surface_edge edges, void *shell)
2650{
2651 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2652 struct shell_surface *shsurf =
2653 weston_desktop_surface_get_user_data(desktop_surface);
2654 struct weston_surface *surface =
2655 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2656 struct wl_resource *resource = surface->resource;
2657 struct weston_surface *focus;
2658
2659 if (!pointer ||
2660 pointer->button_count == 0 ||
2661 pointer->grab_serial != serial ||
2662 pointer->focus == NULL)
2663 return;
2664
2665 focus = weston_surface_get_main_surface(pointer->focus->surface);
2666 if (focus != surface)
2667 return;
2668
2669 if (surface_resize(shsurf, pointer, edges) < 0)
2670 wl_resource_post_no_memory(resource);
2671}
2672
2673static void
Stefan Agnera8da2082019-06-23 14:53:59 +02002674desktop_surface_set_parent(struct weston_desktop_surface *desktop_surface,
2675 struct weston_desktop_surface *parent,
2676 void *shell)
2677{
Marius Vlada27658e2020-01-17 12:32:55 +02002678 struct shell_surface *shsurf_parent;
Stefan Agnera8da2082019-06-23 14:53:59 +02002679 struct shell_surface *shsurf =
2680 weston_desktop_surface_get_user_data(desktop_surface);
Stefan Agnera8da2082019-06-23 14:53:59 +02002681
Marius Vlada27658e2020-01-17 12:32:55 +02002682 /* unlink any potential child */
2683 wl_list_remove(&shsurf->children_link);
2684
2685 if (parent) {
2686 shsurf_parent = weston_desktop_surface_get_user_data(parent);
2687 wl_list_insert(shsurf_parent->children_list.prev,
2688 &shsurf->children_link);
2689 } else {
2690 wl_list_init(&shsurf->children_link);
2691 }
Stefan Agnera8da2082019-06-23 14:53:59 +02002692}
2693
2694static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002695desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2696 bool fullscreen,
2697 struct weston_output *output, void *shell)
2698{
2699 struct shell_surface *shsurf =
2700 weston_desktop_surface_get_user_data(desktop_surface);
2701
2702 set_fullscreen(shsurf, fullscreen, output);
2703}
2704
2705static void
2706set_maximized(struct shell_surface *shsurf, bool maximized)
2707{
2708 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2709 struct weston_surface *surface =
2710 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2711 int32_t width = 0, height = 0;
2712
2713 if (maximized) {
2714 struct weston_output *output;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002715
2716 if (!weston_surface_is_mapped(surface))
2717 output = get_focused_output(surface->compositor);
2718 else
2719 output = surface->output;
2720
2721 shell_surface_set_output(shsurf, output);
2722
Derek Foreman927d9e22017-10-06 14:37:44 -05002723 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002724 }
2725 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2726 weston_desktop_surface_set_size(desktop_surface, width, height);
2727}
2728
2729static void
2730desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2731 bool maximized, void *shell)
2732{
2733 struct shell_surface *shsurf =
2734 weston_desktop_surface_get_user_data(desktop_surface);
2735
2736 set_maximized(shsurf, maximized);
2737}
2738
2739static void
2740desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2741 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002742{
2743 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002744 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002745
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002746 /* apply compositor's own minimization logic (hide) */
2747 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002748}
2749
Rafael Antognollie2a34552013-12-03 15:35:45 -02002750static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002751set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002752{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002753 struct shell_grab *grab;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002754
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002755 if (pointer->grab->interface == &busy_cursor_grab_interface)
2756 return;
2757
2758 grab = malloc(sizeof *grab);
2759 if (!grab)
2760 return;
2761
2762 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2763 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2764 /* Mark the shsurf as ungrabbed so that button binding is able
2765 * to move it. */
2766 shsurf->grabbed = 0;
2767}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002768
2769static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002770end_busy_cursor(struct weston_compositor *compositor,
2771 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002772{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002773 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002774 struct shell_grab *grab;
2775 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002776
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002777 wl_list_for_each(seat, &compositor->seat_list, link) {
2778 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2779 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002780
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002781 if (!pointer)
2782 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002783
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002784 if (pointer->grab->interface != &busy_cursor_grab_interface)
2785 continue;
2786
2787 grab = (struct shell_grab *) pointer->grab;
2788 shsurf = grab->shsurf;
2789 if (!shsurf)
2790 continue;
2791
2792 grab_client =
2793 weston_desktop_surface_get_client(shsurf->desktop_surface);
2794 if (grab_client == desktop_client) {
2795 shell_grab_end(grab);
2796 free(grab);
2797 }
2798 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002799}
2800
2801static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002802desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2803 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002804{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002805 struct shell_surface *shsurf =
2806 weston_desktop_surface_get_user_data(desktop_surface);
2807 bool *unresponsive = user_data;
2808
2809 shsurf->unresponsive = *unresponsive;
2810}
2811
2812static void
2813desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2814 void *shell_)
2815{
2816 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002817 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002818 struct weston_seat *seat;
2819 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002820
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002821 weston_desktop_client_for_each_surface(desktop_client,
2822 desktop_surface_set_unresponsive,
2823 &unresponsive);
2824
2825
2826 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2827 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2828 struct weston_desktop_client *grab_client;
2829
2830 if (!pointer || !pointer->focus)
2831 continue;
2832
2833 shsurf = get_shell_surface(pointer->focus->surface);
2834 if (!shsurf)
2835 continue;
2836
2837 grab_client =
2838 weston_desktop_surface_get_client(shsurf->desktop_surface);
2839 if (grab_client == desktop_client)
2840 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002841 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002842}
2843
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002844static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002845desktop_surface_pong(struct weston_desktop_client *desktop_client,
2846 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002847{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002848 struct desktop_shell *shell = shell_;
2849 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002850
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002851 weston_desktop_client_for_each_surface(desktop_client,
2852 desktop_surface_set_unresponsive,
2853 &unresponsive);
2854 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002855}
2856
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002857static void
2858desktop_surface_set_xwayland_position(struct weston_desktop_surface *surface,
2859 int32_t x, int32_t y, void *shell_)
2860{
2861 struct shell_surface *shsurf =
2862 weston_desktop_surface_get_user_data(surface);
2863
2864 shsurf->xwayland.x = x;
2865 shsurf->xwayland.y = y;
2866 shsurf->xwayland.is_set = true;
2867}
2868
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002869static const struct weston_desktop_api shell_desktop_api = {
2870 .struct_size = sizeof(struct weston_desktop_api),
2871 .surface_added = desktop_surface_added,
2872 .surface_removed = desktop_surface_removed,
2873 .committed = desktop_surface_committed,
2874 .move = desktop_surface_move,
2875 .resize = desktop_surface_resize,
Stefan Agnera8da2082019-06-23 14:53:59 +02002876 .set_parent = desktop_surface_set_parent,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002877 .fullscreen_requested = desktop_surface_fullscreen_requested,
2878 .maximized_requested = desktop_surface_maximized_requested,
2879 .minimized_requested = desktop_surface_minimized_requested,
2880 .ping_timeout = desktop_surface_ping_timeout,
2881 .pong = desktop_surface_pong,
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002882 .set_xwayland_position = desktop_surface_set_xwayland_position,
Rafael Antognollie2a34552013-12-03 15:35:45 -02002883};
2884
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002885/* ************************ *
2886 * end of libweston-desktop *
2887 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04002888static void
Quentin Glidice8bf9592016-06-23 18:55:20 +02002889configure_static_view(struct weston_view *ev, struct weston_layer *layer, int x, int y)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002890{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002891 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002892
Semi Malinene7a52fb2018-04-26 11:08:10 +02002893 if (!ev->output)
2894 return;
2895
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002896 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002897 if (v->output == ev->output && v != ev) {
2898 weston_view_unmap(v);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002899 v->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002900 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002901 }
2902 }
2903
Quentin Glidice8bf9592016-06-23 18:55:20 +02002904 weston_view_set_position(ev, ev->output->x + x, ev->output->y + y);
Armin Krezović4663aca2016-06-30 06:04:29 +02002905 ev->surface->is_mapped = true;
2906 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002907
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002908 if (wl_list_empty(&ev->layer_link.link)) {
2909 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002910 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002911 }
2912}
2913
David Fort50d962f2016-06-09 17:55:52 +02002914
2915static struct shell_output *
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002916find_shell_output_from_weston_output(struct desktop_shell *shell,
2917 struct weston_output *output)
David Fort50d962f2016-06-09 17:55:52 +02002918{
2919 struct shell_output *shell_output;
2920
2921 wl_list_for_each(shell_output, &shell->output_list, link) {
2922 if (shell_output->output == output)
2923 return shell_output;
2924 }
2925
2926 return NULL;
2927}
2928
Pekka Paalanen8274d902014-08-06 19:36:51 +03002929static int
2930background_get_label(struct weston_surface *surface, char *buf, size_t len)
2931{
2932 return snprintf(buf, len, "background for output %s",
Miguel A. Vico620f68d2019-09-25 11:23:13 -07002933 (surface->output ? surface->output->name : "NULL"));
Pekka Paalanen8274d902014-08-06 19:36:51 +03002934}
2935
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002936static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002937background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002938{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002939 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002940 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002941
Jason Ekstranda7af7042013-10-12 22:38:11 -05002942 view = container_of(es->views.next, struct weston_view, surface_link);
2943
Quentin Glidice8bf9592016-06-23 18:55:20 +02002944 configure_static_view(view, &shell->background_layer, 0, 0);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04002945}
2946
2947static void
David Fort50d962f2016-06-09 17:55:52 +02002948handle_background_surface_destroy(struct wl_listener *listener, void *data)
2949{
2950 struct shell_output *output =
2951 container_of(listener, struct shell_output, background_surface_listener);
2952
2953 weston_log("background surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09002954 wl_list_remove(&output->background_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02002955 output->background_surface = NULL;
2956}
2957
2958static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04002959desktop_shell_set_background(struct wl_client *client,
2960 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002961 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04002962 struct wl_resource *surface_resource)
2963{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002964 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002965 struct weston_surface *surface =
2966 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02002967 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002968 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04002969
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002970 if (surface->committed) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002971 wl_resource_post_error(surface_resource,
2972 WL_DISPLAY_ERROR_INVALID_OBJECT,
2973 "surface role already assigned");
2974 return;
2975 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002976
Jason Ekstranda7af7042013-10-12 22:38:11 -05002977 wl_list_for_each_safe(view, next, &surface->views, surface_link)
2978 weston_view_destroy(view);
2979 view = weston_view_create(surface);
leng.fang32af9fc2024-06-13 11:22:15 +08002980 weston_view_opacity(view, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002981
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002982 surface->committed = background_committed;
2983 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002984 weston_surface_set_label_func(surface, background_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03002985 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02002986 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02002987
2988 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02002989 if (sh_output->background_surface) {
2990 /* The output already has a background, tell our helper
2991 * there is no need for another one. */
2992 weston_desktop_shell_send_configure(resource, 0,
2993 surface_resource,
2994 0, 0);
2995 } else {
2996 weston_desktop_shell_send_configure(resource, 0,
2997 surface_resource,
2998 surface->output->width,
2999 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003000
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02003001 sh_output->background_surface = surface;
3002
3003 sh_output->background_surface_listener.notify =
3004 handle_background_surface_destroy;
3005 wl_signal_add(&surface->destroy_signal,
3006 &sh_output->background_surface_listener);
3007 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003008}
3009
Pekka Paalanen8274d902014-08-06 19:36:51 +03003010static int
3011panel_get_label(struct weston_surface *surface, char *buf, size_t len)
3012{
3013 return snprintf(buf, len, "panel for output %s",
Miguel A. Vico620f68d2019-09-25 11:23:13 -07003014 (surface->output ? surface->output->name : "NULL"));
Pekka Paalanen8274d902014-08-06 19:36:51 +03003015}
3016
Kristian Høgsberg75840622011-09-06 13:48:16 -04003017static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003018panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003019{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003020 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003021 struct weston_view *view;
Quentin Glidice8bf9592016-06-23 18:55:20 +02003022 int width, height;
3023 int x = 0, y = 0;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003024
Jason Ekstranda7af7042013-10-12 22:38:11 -05003025 view = container_of(es->views.next, struct weston_view, surface_link);
3026
Quentin Glidice8bf9592016-06-23 18:55:20 +02003027 get_panel_size(shell, view, &width, &height);
3028 switch (shell->panel_position) {
3029 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
3030 break;
3031 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
3032 y = view->output->height - height;
3033 break;
3034 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
3035 break;
3036 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
3037 x = view->output->width - width;
3038 break;
3039 }
3040
3041 configure_static_view(view, &shell->panel_layer, x, y);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003042}
3043
3044static void
David Fort50d962f2016-06-09 17:55:52 +02003045handle_panel_surface_destroy(struct wl_listener *listener, void *data)
3046{
3047 struct shell_output *output =
3048 container_of(listener, struct shell_output, panel_surface_listener);
3049
3050 weston_log("panel surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09003051 wl_list_remove(&output->panel_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02003052 output->panel_surface = NULL;
3053}
3054
3055
3056static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003057desktop_shell_set_panel(struct wl_client *client,
3058 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003059 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003060 struct wl_resource *surface_resource)
3061{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003062 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003063 struct weston_surface *surface =
3064 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003065 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02003066 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003067
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003068 if (surface->committed) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003069 wl_resource_post_error(surface_resource,
3070 WL_DISPLAY_ERROR_INVALID_OBJECT,
3071 "surface role already assigned");
3072 return;
3073 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003074
Jason Ekstranda7af7042013-10-12 22:38:11 -05003075 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3076 weston_view_destroy(view);
3077 view = weston_view_create(surface);
3078
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003079 surface->committed = panel_committed;
3080 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003081 weston_surface_set_label_func(surface, panel_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03003082 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02003083 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02003084
3085 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003086 if (sh_output->panel_surface) {
3087 /* The output already has a panel, tell our helper
3088 * there is no need for another one. */
3089 weston_desktop_shell_send_configure(resource, 0,
3090 surface_resource,
3091 0, 0);
3092 } else {
3093 weston_desktop_shell_send_configure(resource, 0,
3094 surface_resource,
3095 surface->output->width,
3096 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003097
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003098 sh_output->panel_surface = surface;
3099
3100 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
3101 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
3102 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003103}
3104
Pekka Paalanen8274d902014-08-06 19:36:51 +03003105static int
3106lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
3107{
3108 return snprintf(buf, len, "lock window");
3109}
3110
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003111static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003112lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003113{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003114 struct desktop_shell *shell = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003115 struct weston_view *view;
3116
3117 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003118
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003119 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003120 return;
3121
Jason Ekstranda7af7042013-10-12 22:38:11 -05003122 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003123
3124 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003125 weston_layer_entry_insert(&shell->lock_layer.view_list,
3126 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003127 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003128 surface->is_mapped = true;
3129 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003130 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003131 }
3132}
3133
3134static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003135handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003136{
Tiago Vignattibe143262012-04-16 17:31:41 +03003137 struct desktop_shell *shell =
3138 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003139
Martin Minarik6d118362012-06-07 18:01:59 +02003140 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003141 shell->lock_surface = NULL;
3142}
3143
3144static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003145desktop_shell_set_lock_surface(struct wl_client *client,
3146 struct wl_resource *resource,
3147 struct wl_resource *surface_resource)
3148{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003149 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003150 struct weston_surface *surface =
3151 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003152
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003153 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003154
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003155 if (!shell->locked)
3156 return;
3157
Pekka Paalanen98262232011-12-01 10:42:22 +02003158 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003159
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003160 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003161 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003162 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003163
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003164 weston_view_create(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003165 surface->committed = lock_surface_committed;
3166 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003167 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003168}
3169
3170static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003171resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003172{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003173 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003174
Quentin Glidic82681572016-12-17 13:40:51 +01003175 weston_layer_unset_position(&shell->lock_layer);
3176
3177 if (shell->showing_input_panels)
3178 weston_layer_set_position(&shell->input_panel_layer,
3179 WESTON_LAYER_POSITION_TOP_UI);
3180 weston_layer_set_position(&shell->fullscreen_layer,
3181 WESTON_LAYER_POSITION_FULLSCREEN);
3182 weston_layer_set_position(&shell->panel_layer,
3183 WESTON_LAYER_POSITION_UI);
3184 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003185
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003186 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003187
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003188 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003189 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003190 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003191}
3192
3193static void
3194desktop_shell_unlock(struct wl_client *client,
3195 struct wl_resource *resource)
3196{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003197 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003198
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003199 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003200
3201 if (shell->locked)
3202 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003203}
3204
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003205static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003206desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003207 struct wl_resource *resource,
3208 struct wl_resource *surface_resource)
3209{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003210 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003211
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003212 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003213 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003214}
3215
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003216static void
3217desktop_shell_desktop_ready(struct wl_client *client,
3218 struct wl_resource *resource)
3219{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003220 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003221
3222 shell_fade_startup(shell);
3223}
3224
Jonny Lamb765760d2014-08-20 15:53:19 +02003225static void
3226desktop_shell_set_panel_position(struct wl_client *client,
3227 struct wl_resource *resource,
3228 uint32_t position)
3229{
3230 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3231
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003232 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3233 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3234 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3235 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02003236 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003237 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02003238 "bad position argument");
3239 return;
3240 }
3241
3242 shell->panel_position = position;
3243}
3244
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003245static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04003246 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003247 desktop_shell_set_panel,
3248 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003249 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003250 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02003251 desktop_shell_desktop_ready,
3252 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04003253};
3254
3255static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003256move_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003257 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003258{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003259 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003260 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003261 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003262
Derek Foreman8ae2db52015-07-15 13:00:36 -05003263 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003264 return;
3265
Derek Foreman8ae2db52015-07-15 13:00:36 -05003266 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003267
Pekka Paalanen01388e22013-04-25 13:57:44 +03003268 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003269 if (surface == NULL)
3270 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003271
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003272 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003273 if (shsurf == NULL ||
3274 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3275 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003276 return;
3277
Derek Foreman794fa0e2015-07-15 13:00:38 -05003278 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003279}
3280
3281static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003282maximize_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003283 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003284{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003285 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003286 struct weston_surface *surface;
3287 struct shell_surface *shsurf;
3288
3289 surface = weston_surface_get_main_surface(focus);
3290 if (surface == NULL)
3291 return;
3292
3293 shsurf = get_shell_surface(surface);
3294 if (shsurf == NULL)
3295 return;
3296
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003297 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003298}
3299
3300static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003301fullscreen_binding(struct weston_keyboard *keyboard,
3302 const struct timespec *time, uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003303{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003304 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003305 struct weston_surface *surface;
3306 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003307 bool fullscreen;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003308
3309 surface = weston_surface_get_main_surface(focus);
3310 if (surface == NULL)
3311 return;
3312
3313 shsurf = get_shell_surface(surface);
3314 if (shsurf == NULL)
3315 return;
3316
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003317 fullscreen =
3318 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003319
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003320 set_fullscreen(shsurf, !fullscreen, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003321}
3322
3323static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003324touch_move_binding(struct weston_touch *touch, const struct timespec *time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01003325{
Derek Foreman362656b2014-09-04 10:23:05 -05003326 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01003327 struct weston_surface *surface;
3328 struct shell_surface *shsurf;
3329
Derek Foreman8ae2db52015-07-15 13:00:36 -05003330 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05003331 return;
3332
Derek Foreman8ae2db52015-07-15 13:00:36 -05003333 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003334 surface = weston_surface_get_main_surface(focus);
3335 if (surface == NULL)
3336 return;
3337
3338 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003339 if (shsurf == NULL ||
3340 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3341 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Neil Robertsaba0f252013-10-03 16:43:05 +01003342 return;
3343
Derek Foremanb7674ae2015-07-15 13:00:37 -05003344 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01003345}
3346
3347static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003348resize_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003349 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003350{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003351 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003352 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003353 uint32_t edges = 0;
3354 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003355 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003356
Derek Foreman8ae2db52015-07-15 13:00:36 -05003357 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003358 return;
3359
Derek Foreman8ae2db52015-07-15 13:00:36 -05003360 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003361
Pekka Paalanen01388e22013-04-25 13:57:44 +03003362 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003363 if (surface == NULL)
3364 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003365
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003366 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003367 if (shsurf == NULL ||
3368 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3369 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003370 return;
3371
Jason Ekstranda7af7042013-10-12 22:38:11 -05003372 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003373 wl_fixed_to_int(pointer->grab_x),
3374 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05003375 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003376
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003377 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003378 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003379 else if (x < 2 * surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003380 edges |= 0;
3381 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003382 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003383
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003384 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003385 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003386 else if (y < 2 * surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003387 edges |= 0;
3388 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003389 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003390
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003391 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003392}
3393
3394static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003395surface_opacity_binding(struct weston_pointer *pointer,
3396 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003397 struct weston_pointer_axis_event *event,
3398 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003399{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003400 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003401 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003402 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003403 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003404
Pekka Paalanen01388e22013-04-25 13:57:44 +03003405 /* XXX: broken for windows containing sub-surfaces */
3406 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003407 if (surface == NULL)
3408 return;
3409
3410 shsurf = get_shell_surface(surface);
3411 if (!shsurf)
3412 return;
3413
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003414 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003415
Jason Ekstranda7af7042013-10-12 22:38:11 -05003416 if (shsurf->view->alpha > 1.0)
3417 shsurf->view->alpha = 1.0;
3418 if (shsurf->view->alpha < step)
3419 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003420
Jason Ekstranda7af7042013-10-12 22:38:11 -05003421 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003422 weston_surface_damage(surface);
3423}
3424
3425static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003426do_zoom(struct weston_seat *seat, const struct timespec *time, uint32_t key,
3427 uint32_t axis, double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003428{
Derek Foreman8aeeac82015-01-30 13:24:36 -06003429 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05003430 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003431 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003432 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003433
Derek Foreman1281a362015-07-31 16:55:32 -05003434 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06003435 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3436 return;
3437 }
3438
Scott Moreauccbf29d2012-02-22 14:21:41 -07003439 wl_list_for_each(output, &compositor->output_list, link) {
3440 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05003441 wl_fixed_to_double(pointer->x),
3442 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003443 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003444 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003445 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003446 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003447 increment = -output->zoom.increment;
3448 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003449 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003450 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003451 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003452 else
3453 increment = 0;
3454
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003455 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003456
Scott Moreaue6603982012-06-11 13:07:51 -06003457 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003458 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003459 else if (output->zoom.level > output->zoom.max_level)
3460 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05003461
3462 if (!output->zoom.active) {
3463 if (output->zoom.level <= 0.0)
3464 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05003465 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003466 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003467
Scott Moreaue6603982012-06-11 13:07:51 -06003468 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003469
Jason Ekstranda7af7042013-10-12 22:38:11 -05003470 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003471 }
3472 }
3473}
3474
Scott Moreauccbf29d2012-02-22 14:21:41 -07003475static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003476zoom_axis_binding(struct weston_pointer *pointer, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003477 struct weston_pointer_axis_event *event,
3478 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003479{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003480 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003481}
3482
3483static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003484zoom_key_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003485 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003486{
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003487 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003488}
3489
3490static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003491terminate_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003492 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003493{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003494 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003495
Pekka Paalanen052032d2019-02-06 10:44:37 +02003496 weston_compositor_exit(compositor);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003497}
3498
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003499static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003500rotate_grab_motion(struct weston_pointer_grab *grab,
3501 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003502 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003503{
3504 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003505 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003506 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003507 struct shell_surface *shsurf = rotate->base.shsurf;
Sergey Bugaev14ef2012019-02-11 22:55:09 +03003508 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003509 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003510
Jonas Ådahld2510102014-10-05 21:39:14 +02003511 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003512
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003513 if (!shsurf)
3514 return;
3515
Sergey Bugaev14ef2012019-02-11 22:55:09 +03003516 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
3517
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003518 cx = 0.5f * surface->width;
3519 cy = 0.5f * surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003520
Daniel Stone37816df2012-05-16 18:45:18 +01003521 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3522 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003523 r = sqrtf(dx * dx + dy * dy);
3524
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003525 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003526 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003527
3528 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003529 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003530 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003531
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003532 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003533 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003534
3535 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003536 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003537 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003538 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003539 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003540
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003541 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003542 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003543 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003544 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003545 wl_list_init(&shsurf->rotation.transform.link);
3546 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003547 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003548 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003549
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003550 /* We need to adjust the position of the surface
3551 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003552 cposx = shsurf->view->geometry.x + cx;
3553 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003554 dposx = rotate->center.x - cposx;
3555 dposy = rotate->center.y - cposy;
3556 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003557 weston_view_set_position(shsurf->view,
3558 shsurf->view->geometry.x + dposx,
3559 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003560 }
3561
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003562 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003563 * lazily applies the damage due to rotation update.
3564 */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003565 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003566}
3567
3568static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003569rotate_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003570 const struct timespec *time,
3571 uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003572{
3573 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003574 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003575 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003576 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003577 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003578
Daniel Stone4dbadb12012-05-30 16:31:51 +01003579 if (pointer->button_count == 0 &&
3580 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003581 if (shsurf)
3582 weston_matrix_multiply(&shsurf->rotation.rotation,
3583 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003584 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003585 free(rotate);
3586 }
3587}
3588
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003589static void
3590rotate_grab_cancel(struct weston_pointer_grab *grab)
3591{
3592 struct rotate_grab *rotate =
3593 container_of(grab, struct rotate_grab, base.grab);
3594
3595 shell_grab_end(&rotate->base);
3596 free(rotate);
3597}
3598
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003599static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003600 noop_grab_focus,
3601 rotate_grab_motion,
3602 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003603 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003604 noop_grab_axis_source,
3605 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003606 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003607};
3608
3609static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003610surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003611{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003612 struct weston_surface *surface =
3613 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003614 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003615 float dx, dy;
3616 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003617
Pekka Paalanen460099f2012-01-20 16:48:25 +02003618 rotate = malloc(sizeof *rotate);
3619 if (!rotate)
3620 return;
3621
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003622 weston_view_to_global_float(shsurf->view,
3623 surface->width * 0.5f,
3624 surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003625 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003626
Derek Foreman74de4692015-07-15 13:00:39 -05003627 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3628 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003629 r = sqrtf(dx * dx + dy * dy);
3630 if (r > 20.0f) {
3631 struct weston_matrix inverse;
3632
3633 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003634 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003635 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003636
3637 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003638 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003639 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003640 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003641 weston_matrix_init(&rotate->rotation);
3642 }
3643
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003644 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003645 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003646}
3647
3648static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003649rotate_binding(struct weston_pointer *pointer, const struct timespec *time,
3650 uint32_t button, void *data)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003651{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003652 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003653 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003654 struct shell_surface *surface;
3655
Derek Foreman8ae2db52015-07-15 13:00:36 -05003656 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003657 return;
3658
Derek Foreman8ae2db52015-07-15 13:00:36 -05003659 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003660
Pekka Paalanen01388e22013-04-25 13:57:44 +03003661 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003662 if (base_surface == NULL)
3663 return;
3664
3665 surface = get_shell_surface(base_surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003666 if (surface == NULL ||
3667 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3668 weston_desktop_surface_get_maximized(surface->desktop_surface))
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003669 return;
3670
Derek Foreman74de4692015-07-15 13:00:39 -05003671 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003672}
3673
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003674/* Move all fullscreen layers down to the current workspace and hide their
3675 * black views. The surfaces' state is set to both fullscreen and lowered,
3676 * and this is reversed when such a surface is re-configured, see
3677 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3678 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003679 * lowering_output = NULL - Lower on all outputs, else only lower on the
3680 * specified output.
3681 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003682 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003683 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003684void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003685lower_fullscreen_layer(struct desktop_shell *shell,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003686 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003687{
3688 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003689 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003690
3691 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003692 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003693 &shell->fullscreen_layer.view_list.link,
3694 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003695 struct shell_surface *shsurf = get_shell_surface(view->surface);
3696
3697 if (!shsurf)
3698 continue;
3699
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003700 /* Only lower surfaces which have lowering_output as their fullscreen
3701 * output, unless a NULL output asks for lowering on all outputs.
3702 */
3703 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3704 continue;
3705
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003706 /* We can have a non-fullscreen popup for a fullscreen surface
3707 * in the fullscreen layer. */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003708 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003709 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003710 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3711 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07003712 weston_view_damage_below(shsurf->fullscreen.black_view);
3713
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003714 }
3715
3716 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003717 weston_layer_entry_remove(&view->layer_link);
3718 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003719 weston_view_damage_below(view);
3720 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003721
3722 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003723 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003724}
3725
Stefan Agnera8da2082019-06-23 14:53:59 +02003726static struct shell_surface *get_last_child(struct shell_surface *shsurf)
3727{
3728 struct shell_surface *shsurf_child;
3729
3730 wl_list_for_each_reverse(shsurf_child, &shsurf->children_list, children_link) {
3731 if (weston_view_is_mapped(shsurf_child->view))
3732 return shsurf_child;
3733 }
3734
3735 return NULL;
3736}
3737
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003738void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003739activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003740 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003741{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003742 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003743 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003744 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003745 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003746 struct weston_surface *old_es;
Stefan Agnera8da2082019-06-23 14:53:59 +02003747 struct shell_surface *shsurf, *shsurf_child;
Marius Vladf12697b2021-03-05 21:44:26 +02003748 struct shell_seat *shseat = get_shell_seat(seat);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003749
Pekka Paalanen01388e22013-04-25 13:57:44 +03003750 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003751 shsurf = get_shell_surface(main_surface);
3752 assert(shsurf);
3753
Stefan Agnera8da2082019-06-23 14:53:59 +02003754 shsurf_child = get_last_child(shsurf);
3755 if (shsurf_child) {
3756 /* Activate last xdg child instead of parent. */
3757 activate(shell, shsurf_child->view, seat, flags);
3758 return;
3759 }
3760
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003761 /* Only demote fullscreen surfaces on the output of activated shsurf.
3762 * Leave fullscreen surfaces on unrelated outputs alone. */
Pekka Paalanen87860c22018-05-02 10:21:57 +02003763 if (shsurf->output)
3764 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003765
Marius Vladd6ccc8b2021-03-05 22:07:30 +02003766 weston_view_activate_input(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003767
Marius Vlad137c7932021-11-20 19:14:45 +02003768 if (shseat && shseat->focused_surface) {
Marius Vladf12697b2021-03-05 21:44:26 +02003769 struct shell_surface *current_focus =
3770 get_shell_surface(shseat->focused_surface);
3771 assert(current_focus);
3772 shell_surface_deactivate(current_focus);
3773 }
3774
Marius Vlad137c7932021-11-20 19:14:45 +02003775 if (shseat)
3776 shseat->focused_surface = main_surface;
Marius Vladf12697b2021-03-05 21:44:26 +02003777 shell_surface_activate(shsurf);
3778
Jonas Ådahl8538b222012-08-29 22:13:03 +02003779 state = ensure_focus_state(shell, seat);
3780 if (state == NULL)
3781 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003782
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003783 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08003784 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003785
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003786 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3787 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02003788 shell_configure_fullscreen(shsurf);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003789
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01003790 /* Update the surface’s layer. This brings it to the top of the stacking
3791 * order as appropriate. */
3792 shell_surface_update_layer(shsurf);
3793
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003794 if (shell->focus_animation_type != ANIMATION_NONE) {
3795 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003796 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003797 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003798}
3799
Alex Wu21858432012-04-01 20:13:08 +08003800/* no-op func for checking black surface */
3801static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003802black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08003803{
3804}
3805
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003806static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003807is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08003808{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003809 struct weston_surface *surface = view->surface;
3810
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003811 if (surface->committed == black_surface_committed) {
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003812 if (fs_view)
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003813 *fs_view = surface->committed_private;
Alex Wu21858432012-04-01 20:13:08 +08003814 return true;
3815 }
3816 return false;
3817}
3818
Kristian Høgsberg75840622011-09-06 13:48:16 -04003819static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003820activate_binding(struct weston_seat *seat,
3821 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003822 struct weston_view *focus_view,
3823 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003824{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003825 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003826 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003827
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003828 if (!focus_view)
3829 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003830
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003831 if (is_black_surface_view(focus_view, &main_view))
3832 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08003833
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003834 main_surface = weston_surface_get_main_surface(focus_view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003835 if (!get_shell_surface(main_surface))
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003836 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003837
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003838 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01003839}
3840
3841static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003842click_to_activate_binding(struct weston_pointer *pointer,
3843 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003844 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003845{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003846 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003847 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003848 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08003849 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003850
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003851 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003852 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003853 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01003854}
3855
3856static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003857touch_to_activate_binding(struct weston_touch *touch,
3858 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003859 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003860{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003861 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003862 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003863 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08003864 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003865
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003866 activate_binding(touch->seat, data, touch->focus,
3867 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003868}
3869
3870static void
Neil Robertsb4a91702014-04-09 16:33:32 +01003871unfocus_all_seats(struct desktop_shell *shell)
3872{
3873 struct weston_seat *seat, *next;
3874
3875 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003876 struct weston_keyboard *keyboard =
3877 weston_seat_get_keyboard(seat);
3878
3879 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01003880 continue;
3881
Derek Foreman1281a362015-07-31 16:55:32 -05003882 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01003883 }
3884}
3885
3886static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003887lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003888{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003889 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003890
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003891 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003892 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003893 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003894 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003895
3896 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003897
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003898 /* Hide all surfaces by removing the fullscreen, panel and
3899 * toplevel layers. This way nothing else can show or receive
3900 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003901
Quentin Glidic82681572016-12-17 13:40:51 +01003902 weston_layer_unset_position(&shell->panel_layer);
3903 weston_layer_unset_position(&shell->fullscreen_layer);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003904 if (shell->showing_input_panels)
Quentin Glidic82681572016-12-17 13:40:51 +01003905 weston_layer_unset_position(&shell->input_panel_layer);
3906 weston_layer_unset_position(&ws->layer);
3907
3908 weston_layer_set_position(&shell->lock_layer,
3909 WESTON_LAYER_POSITION_LOCK);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003910
Derek Foreman004b4a12015-07-20 16:28:13 -05003911 weston_compositor_sleep(shell->compositor);
3912
Neil Robertsb4a91702014-04-09 16:33:32 +01003913 /* Remove the keyboard focus on all seats. This will be
3914 * restored to the workspace's saved state via
3915 * restore_focus_state when the compositor is unlocked */
3916 unfocus_all_seats(shell);
3917
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003918 /* TODO: disable bindings that should not work while locked. */
3919
3920 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003921}
3922
3923static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003924unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003925{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003926 struct wl_resource *shell_resource;
3927
Pekka Paalanend81c2162011-11-16 13:47:34 +02003928 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003929 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003930 return;
3931 }
3932
3933 /* If desktop-shell client has gone away, unlock immediately. */
3934 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003935 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003936 return;
3937 }
3938
3939 if (shell->prepare_event_sent)
3940 return;
3941
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003942 shell_resource = shell->child.desktop_shell;
3943 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003944 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003945}
3946
3947static void
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003948shell_fade_done_for_output(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003949{
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003950 struct shell_output *shell_output = data;
3951 struct desktop_shell *shell = shell_output->shell;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003952
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003953 shell_output->fade.animation = NULL;
3954 switch (shell_output->fade.type) {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003955 case FADE_IN:
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003956 weston_surface_destroy(shell_output->fade.view->surface);
3957 shell_output->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003958 break;
3959 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003960 lock(shell);
3961 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00003962 default:
3963 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003964 }
3965}
3966
Jason Ekstranda7af7042013-10-12 22:38:11 -05003967static struct weston_view *
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003968shell_fade_create_surface_for_output(struct desktop_shell *shell, struct shell_output *shell_output)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003969{
3970 struct weston_compositor *compositor = shell->compositor;
3971 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003972 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003973
3974 surface = weston_surface_create(compositor);
3975 if (!surface)
3976 return NULL;
3977
Jason Ekstranda7af7042013-10-12 22:38:11 -05003978 view = weston_view_create(surface);
3979 if (!view) {
3980 weston_surface_destroy(surface);
3981 return NULL;
3982 }
3983
Bryce Harrington9ad4de12016-09-09 13:16:02 -07003984 weston_surface_set_size(surface, shell_output->output->width, shell_output->output->height);
3985 weston_view_set_position(view, shell_output->output->x, shell_output->output->y);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003986 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003987 weston_layer_entry_insert(&compositor->fade_layer.view_list,
3988 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003989 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02003990 surface->is_mapped = true;
3991 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003992
Jason Ekstranda7af7042013-10-12 22:38:11 -05003993 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003994}
3995
3996static void
3997shell_fade(struct desktop_shell *shell, enum fade_type type)
3998{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003999 float tint;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004000 struct shell_output *shell_output;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004001
4002 switch (type) {
4003 case FADE_IN:
4004 tint = 0.0;
4005 break;
4006 case FADE_OUT:
4007 tint = 1.0;
4008 break;
4009 default:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07004010 weston_log("shell: invalid fade type\n");
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004011 return;
4012 }
4013
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004014 /* Create a separate fade surface for each output */
4015 wl_list_for_each(shell_output, &shell->output_list, link) {
4016 shell_output->fade.type = type;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004017
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004018 if (shell_output->fade.view == NULL) {
4019 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4020 if (!shell_output->fade.view)
4021 continue;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004022
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004023 shell_output->fade.view->alpha = 1.0 - tint;
4024 weston_view_update_transform(shell_output->fade.view);
4025 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004026
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004027 if (shell_output->fade.view->output == NULL) {
4028 /* If the black view gets a NULL output, we lost the
4029 * last output and we'll just cancel the fade. This
4030 * happens when you close the last window under the
4031 * X11 or Wayland backends. */
4032 shell->locked = false;
4033 weston_surface_destroy(shell_output->fade.view->surface);
4034 shell_output->fade.view = NULL;
4035 } else if (shell_output->fade.animation) {
4036 weston_fade_update(shell_output->fade.animation, tint);
4037 } else {
4038 shell_output->fade.animation =
4039 weston_fade_run(shell_output->fade.view,
4040 1.0 - tint, tint, 300.0,
4041 shell_fade_done_for_output, shell_output);
4042 }
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004043 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004044}
4045
4046static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004047do_shell_fade_startup(void *data)
4048{
4049 struct desktop_shell *shell = data;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004050 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004051
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004052 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004053 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004054 } else {
4055 weston_log("desktop shell: "
4056 "unexpected fade-in animation type %d\n",
4057 shell->startup_animation_type);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004058 wl_list_for_each(shell_output, &shell->output_list, link) {
4059 weston_surface_destroy(shell_output->fade.view->surface);
4060 shell_output->fade.view = NULL;
4061 }
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004062 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004063}
4064
4065static void
4066shell_fade_startup(struct desktop_shell *shell)
4067{
4068 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004069 struct shell_output *shell_output;
4070 bool has_fade = false;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004071
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004072 wl_list_for_each(shell_output, &shell->output_list, link) {
4073 if (!shell_output->fade.startup_timer)
4074 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004075
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004076 wl_event_source_remove(shell_output->fade.startup_timer);
4077 shell_output->fade.startup_timer = NULL;
4078 has_fade = true;
4079 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004080
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004081 if (has_fade) {
4082 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4083 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4084 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004085}
4086
4087static int
4088fade_startup_timeout(void *data)
4089{
4090 struct desktop_shell *shell = data;
4091
4092 shell_fade_startup(shell);
4093 return 0;
4094}
4095
4096static void
4097shell_fade_init(struct desktop_shell *shell)
4098{
4099 /* Make compositor output all black, and wait for the desktop-shell
4100 * client to signal it is ready, then fade in. The timer triggers a
4101 * fade-in, in case the desktop-shell client takes too long.
4102 */
4103
4104 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004105 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004106
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004107 if (shell->startup_animation_type == ANIMATION_NONE)
4108 return;
4109
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004110 wl_list_for_each(shell_output, &shell->output_list, link) {
4111 if (shell_output->fade.view != NULL) {
4112 weston_log("%s: warning: fade surface already exists\n",
4113 __func__);
4114 continue;
4115 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004116
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004117 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4118 if (!shell_output->fade.view)
4119 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004120
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004121 weston_view_update_transform(shell_output->fade.view);
4122 weston_surface_damage(shell_output->fade.view->surface);
4123
4124 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4125 shell_output->fade.startup_timer =
4126 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4127 wl_event_source_timer_update(shell_output->fade.startup_timer, 15000);
4128 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004129}
4130
4131static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004132idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004133{
4134 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004135 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004136
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004137 struct weston_seat *seat;
4138
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004139 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4140 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004141
4142 shell_fade(shell, FADE_OUT);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004143 /* lock() is called from shell_fade_done_for_output() */
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004144}
4145
4146static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004147wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004148{
4149 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004150 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004151
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004152 unlock(shell);
4153}
4154
4155static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004156transform_handler(struct wl_listener *listener, void *data)
4157{
4158 struct weston_surface *surface = data;
4159 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004160 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004161 int x, y;
4162
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004163 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004164 return;
4165
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004166 api = shsurf->shell->xwayland_surface_api;
4167 if (!api) {
4168 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
4169 shsurf->shell->xwayland_surface_api = api;
4170 }
4171
4172 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004173 return;
4174
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004175 if (!weston_view_is_mapped(shsurf->view))
4176 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004177
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004178 x = shsurf->view->geometry.x;
4179 y = shsurf->view->geometry.y;
4180
4181 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004182}
4183
4184static void
leng.fang32af9fc2024-06-13 11:22:15 +08004185weston_view_set_initial_position(struct shell_surface *shsurf,
Jason Ekstranda7af7042013-10-12 22:38:11 -05004186 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004187{
4188 struct weston_compositor *compositor = shell->compositor;
leng.fang32af9fc2024-06-13 11:22:15 +08004189 struct weston_view *view = shsurf->view;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004190 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004191 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004192 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004193 struct weston_output *output, *target_output = NULL;
4194 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004195 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004196
4197 /* As a heuristic place the new window on the same output as the
4198 * pointer. Falling back to the output containing 0, 0.
4199 *
4200 * TODO: Do something clever for touch too?
4201 */
4202 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004203 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4204
4205 if (pointer) {
4206 ix = wl_fixed_to_int(pointer->x);
4207 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004208 break;
4209 }
4210 }
4211
leng.fang32af9fc2024-06-13 11:22:15 +08004212 if (shsurf->fullscreen_output != NULL)
4213 target_output = shsurf->fullscreen_output;
4214 else{
4215 wl_list_for_each(output, &compositor->output_list, link) {
4216 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4217 target_output = output;
4218 break;
4219 }
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004220 }
4221 }
4222
4223 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004224 weston_view_set_position(view, 10 + random() % 400,
4225 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004226 return;
4227 }
4228
4229 /* Valid range within output where the surface will still be onscreen.
4230 * If this is negative it means that the surface is bigger than
4231 * output.
4232 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004233 get_output_work_area(shell, target_output, &area);
4234
Derek Foremanf814c5d2015-04-15 12:23:54 -05004235 x = area.x;
4236 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004237 range_x = area.width - view->surface->width;
4238 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004239
Rob Bradford4cb88c72012-08-13 15:18:44 +01004240 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004241 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004242
4243 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004244 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004245
Jason Ekstranda7af7042013-10-12 22:38:11 -05004246 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004247}
4248
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004249static bool
4250check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4251{
4252 struct timespec now;
4253
4254 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4255 return false;
4256
4257 /*
4258 * If the shell helper client dies before the session has been
4259 * up for roughly 30 seconds, better just make Weston shut down,
4260 * because the user likely has no way to interact with the desktop
4261 * anyway.
4262 */
4263 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4264 weston_log("Error: %s apparently cannot run at all.\n",
4265 shell->client);
4266 weston_log_continue(STAMP_SPACE "Quitting...");
Pekka Paalanen052032d2019-02-06 10:44:37 +02004267 weston_compositor_exit_with_code(shell->compositor,
4268 EXIT_FAILURE);
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004269
4270 return true;
4271 }
4272
4273 return false;
4274}
4275
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004276static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004277
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004278static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004279respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004280{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004281 struct timespec time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004282
4283 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004284 weston_compositor_get_time(&time);
4285 if (timespec_sub_to_msec(&time, &shell->child.deathstamp) > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004286 shell->child.deathstamp = time;
4287 shell->child.deathcount = 0;
4288 }
4289
4290 shell->child.deathcount++;
4291 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004292 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004293 return;
4294 }
4295
Pekka Paalanen826dc142014-08-27 12:11:53 +03004296 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004297 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004298}
4299
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004300static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004301desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4302{
4303 struct desktop_shell *shell;
4304
4305 shell = container_of(listener, struct desktop_shell,
4306 child.client_destroy_listener);
4307
Pekka Paalanen826dc142014-08-27 12:11:53 +03004308 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004309 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004310 /*
4311 * unbind_desktop_shell() will reset shell->child.desktop_shell
4312 * before the respawned process has a chance to create a new
4313 * desktop_shell object, because we are being called from the
4314 * wl_client destructor which destroys all wl_resources before
4315 * returning.
4316 */
4317
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004318 if (!check_desktop_shell_crash_too_early(shell))
4319 respawn_desktop_shell_process(shell);
4320
Pekka Paalanen826dc142014-08-27 12:11:53 +03004321 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004322}
4323
4324static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004325launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004326{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004327 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004328
Pekka Paalanen826dc142014-08-27 12:11:53 +03004329 shell->child.client = weston_client_start(shell->compositor,
4330 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004331
Arnaud Vrac42631192014-08-25 20:56:46 +02004332 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004333 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004334 return;
4335 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004336
4337 shell->child.client_destroy_listener.notify =
4338 desktop_shell_client_destroy;
4339 wl_client_add_destroy_listener(shell->child.client,
4340 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004341}
4342
4343static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004344unbind_desktop_shell(struct wl_resource *resource)
4345{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004346 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004347
4348 if (shell->locked)
4349 resume_desktop(shell);
4350
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004351 shell->child.desktop_shell = NULL;
4352 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004353}
4354
4355static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004356bind_desktop_shell(struct wl_client *client,
4357 void *data, uint32_t version, uint32_t id)
4358{
Tiago Vignattibe143262012-04-16 17:31:41 +03004359 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004360 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004361
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004362 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4363 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004364
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004365 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004366 wl_resource_set_implementation(resource,
4367 &desktop_shell_implementation,
4368 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004369 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004370 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004371 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004372
4373 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4374 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004375}
4376
Kristian Høgsberg07045392012-02-19 18:52:44 -05004377struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004378 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004379 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004380 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004381 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004382 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004383};
4384
4385static void
4386switcher_next(struct switcher *switcher)
4387{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004388 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004389 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004390 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004391 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004392
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004393 /* temporary re-display minimized surfaces */
4394 struct weston_view *tmp;
4395 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004396 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4397 weston_layer_entry_remove(&view->layer_link);
4398 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004399 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4400 *minimized = view;
4401 }
4402
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004403 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004404 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004405 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004406 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004407 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004408 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004409 next = view;
4410 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004411 view->alpha = 0.25;
4412 weston_view_geometry_dirty(view);
4413 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004414 }
Alex Wu1659daa2012-04-01 20:13:09 +08004415
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004416 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004417 view->alpha = 0.25;
4418 weston_view_geometry_dirty(view);
4419 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004420 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004421 }
4422
4423 if (next == NULL)
4424 next = first;
4425
Alex Wu07b26062012-03-12 16:06:01 +08004426 if (next == NULL)
4427 return;
4428
Kristian Høgsberg07045392012-02-19 18:52:44 -05004429 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004430 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004431
4432 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004433 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004434 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004435
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004436 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004437 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004438 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004439}
4440
4441static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004442switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004443{
4444 struct switcher *switcher =
4445 container_of(listener, struct switcher, listener);
4446
4447 switcher_next(switcher);
4448}
4449
4450static void
Daniel Stone351eb612012-05-31 15:27:47 -04004451switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004452{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004453 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004454 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004455 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004456
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004457 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004458 if (is_focus_view(view))
4459 continue;
4460
Jason Ekstranda7af7042013-10-12 22:38:11 -05004461 view->alpha = 1.0;
4462 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004463 }
4464
Marius Vlad5699dba2021-10-15 20:20:17 +03004465 if (switcher->current && get_shell_surface(switcher->current->surface)) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004466 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004467 keyboard->seat,
4468 WESTON_ACTIVATE_FLAG_CONFIGURE);
4469 }
4470
Kristian Høgsberg07045392012-02-19 18:52:44 -05004471 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004472 weston_keyboard_end_grab(keyboard);
4473 if (keyboard->input_method_resource)
4474 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004475
4476 /* re-hide surfaces that were temporary shown during the switch */
4477 struct weston_view **minimized;
4478 wl_array_for_each(minimized, &switcher->minimized_array) {
4479 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004480 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004481 weston_layer_entry_remove(&(*minimized)->layer_link);
4482 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004483 weston_view_damage_below(*minimized);
4484 }
4485 }
4486 wl_array_release(&switcher->minimized_array);
4487
Kristian Høgsberg07045392012-02-19 18:52:44 -05004488 free(switcher);
4489}
4490
4491static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004492switcher_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004493 const struct timespec *time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004494{
4495 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004496 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004497
Daniel Stonec9785ea2012-05-30 16:31:52 +01004498 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004499 switcher_next(switcher);
4500}
4501
4502static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004503switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004504 uint32_t mods_depressed, uint32_t mods_latched,
4505 uint32_t mods_locked, uint32_t group)
4506{
4507 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004508 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004509
Daniel Stone351eb612012-05-31 15:27:47 -04004510 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4511 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004512}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004513
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004514static void
4515switcher_cancel(struct weston_keyboard_grab *grab)
4516{
4517 struct switcher *switcher = container_of(grab, struct switcher, grab);
4518
4519 switcher_destroy(switcher);
4520}
4521
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004522static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004523 switcher_key,
4524 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004525 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004526};
4527
4528static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004529switcher_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004530 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004531{
Tiago Vignattibe143262012-04-16 17:31:41 +03004532 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004533 struct switcher *switcher;
4534
4535 switcher = malloc(sizeof *switcher);
ganjingc1e71512020-07-28 15:28:45 +08004536 if (!switcher)
4537 return;
Pekka Paalanen4bb326b2021-05-14 14:37:46 +03004538
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004539 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004540 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004541 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004542 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004543 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004544
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004545 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004546 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004547 weston_keyboard_start_grab(keyboard, &switcher->grab);
4548 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004549 switcher_next(switcher);
4550}
4551
Pekka Paalanen3c647232011-12-22 13:43:43 +02004552static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004553backlight_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004554 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004555{
4556 struct weston_compositor *compositor = data;
4557 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004558 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004559
4560 /* TODO: we're limiting to simple use cases, where we assume just
4561 * control on the primary display. We'd have to extend later if we
4562 * ever get support for setting backlights on random desktop LCD
4563 * panels though */
4564 output = get_default_output(compositor);
4565 if (!output)
4566 return;
4567
4568 if (!output->set_backlight)
4569 return;
4570
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004571 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4572 backlight_new = output->backlight_current - 25;
4573 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4574 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004575
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004576 if (backlight_new < 5)
4577 backlight_new = 5;
4578 if (backlight_new > 255)
4579 backlight_new = 255;
4580
4581 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004582 output->set_backlight(output, output->backlight_current);
4583}
4584
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004585static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004586force_kill_binding(struct weston_keyboard *keyboard,
4587 const struct timespec *time, uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004588{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004589 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004590 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004591 struct desktop_shell *shell = data;
4592 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004593 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004594
Derek Foreman8ae2db52015-07-15 13:00:36 -05004595 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004596 if (!focus_surface)
4597 return;
4598
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004599 wl_signal_emit(&compositor->kill_signal, focus_surface);
4600
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004601 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004602 wl_client_get_credentials(client, &pid, NULL, NULL);
4603
4604 /* Skip clients that we launched ourselves (the credentials of
4605 * the socketpair is ours) */
4606 if (pid == getpid())
4607 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004608
Daniel Stone325fc2d2012-05-30 16:31:58 +01004609 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004610}
4611
4612static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004613workspace_up_binding(struct weston_keyboard *keyboard,
4614 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004615{
4616 struct desktop_shell *shell = data;
4617 unsigned int new_index = shell->workspaces.current;
4618
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004619 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004620 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004621 if (new_index != 0)
4622 new_index--;
4623
4624 change_workspace(shell, new_index);
4625}
4626
4627static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004628workspace_down_binding(struct weston_keyboard *keyboard,
4629 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004630{
4631 struct desktop_shell *shell = data;
4632 unsigned int new_index = shell->workspaces.current;
4633
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004634 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004635 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004636 if (new_index < shell->workspaces.num - 1)
4637 new_index++;
4638
4639 change_workspace(shell, new_index);
4640}
4641
4642static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004643workspace_f_binding(struct weston_keyboard *keyboard,
4644 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004645{
4646 struct desktop_shell *shell = data;
4647 unsigned int new_index;
4648
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004649 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004650 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004651 new_index = key - KEY_F1;
4652 if (new_index >= shell->workspaces.num)
4653 new_index = shell->workspaces.num - 1;
4654
4655 change_workspace(shell, new_index);
4656}
4657
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004658static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004659workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004660 const struct timespec *time, uint32_t key,
4661 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004662{
4663 struct desktop_shell *shell = data;
4664 unsigned int new_index = shell->workspaces.current;
4665
4666 if (shell->locked)
4667 return;
4668
4669 if (new_index != 0)
4670 new_index--;
4671
Derek Foreman8ae2db52015-07-15 13:00:36 -05004672 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004673}
4674
4675static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004676workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004677 const struct timespec *time, uint32_t key,
4678 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004679{
4680 struct desktop_shell *shell = data;
4681 unsigned int new_index = shell->workspaces.current;
4682
4683 if (shell->locked)
4684 return;
4685
4686 if (new_index < shell->workspaces.num - 1)
4687 new_index++;
4688
Derek Foreman8ae2db52015-07-15 13:00:36 -05004689 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004690}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004691
4692static void
Harish Krupodc3edc02019-04-20 17:50:18 +05304693shell_reposition_view_on_output_change(struct weston_view *view)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004694{
4695 struct weston_output *output, *first_output;
4696 struct weston_compositor *ec = view->surface->compositor;
4697 struct shell_surface *shsurf;
4698 float x, y;
4699 int visible;
4700
Harish Krupo2dff4dd2019-04-20 18:10:56 +05304701 if (wl_list_empty(&ec->output_list))
4702 return;
4703
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004704 x = view->geometry.x;
4705 y = view->geometry.y;
4706
4707 /* At this point the destroyed output is not in the list anymore.
4708 * If the view is still visible somewhere, we leave where it is,
4709 * otherwise, move it to the first output. */
4710 visible = 0;
4711 wl_list_for_each(output, &ec->output_list, link) {
4712 if (pixman_region32_contains_point(&output->region,
4713 x, y, NULL)) {
4714 visible = 1;
4715 break;
4716 }
4717 }
4718
4719 if (!visible) {
4720 first_output = container_of(ec->output_list.next,
4721 struct weston_output, link);
4722
4723 x = first_output->x + first_output->width / 4;
4724 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004725
leng.fang32af9fc2024-06-13 11:22:15 +08004726 x = 0.0; // change default position to (0, 0) if not visible
4727 y = 0.0;
Xiong Zhang62899f52014-03-07 16:27:19 +08004728 weston_view_set_position(view, x, y);
4729 } else {
4730 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004731 }
4732
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004733
4734 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004735 if (!shsurf)
4736 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004737
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004738 shsurf->saved_position_valid = false;
4739 set_maximized(shsurf, false);
4740 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004741}
4742
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004743void
4744shell_for_each_layer(struct desktop_shell *shell,
4745 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004746{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004747 struct workspace **ws;
4748
4749 func(shell, &shell->fullscreen_layer, data);
4750 func(shell, &shell->panel_layer, data);
4751 func(shell, &shell->background_layer, data);
4752 func(shell, &shell->lock_layer, data);
4753 func(shell, &shell->input_panel_layer, data);
4754
4755 wl_array_for_each(ws, &shell->workspaces.array)
4756 func(shell, &(*ws)->layer, data);
4757}
4758
4759static void
Harish Krupodc3edc02019-04-20 17:50:18 +05304760shell_output_changed_move_layer(struct desktop_shell *shell,
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004761 struct weston_layer *layer,
4762 void *data)
4763{
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004764 struct weston_view *view;
4765
Marius Vladea40d6d2018-02-08 18:46:42 +02004766 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Harish Krupodc3edc02019-04-20 17:50:18 +05304767 shell_reposition_view_on_output_change(view);
4768
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004769}
4770
4771static void
Pekka Paalanen719ca872021-05-24 16:53:40 +03004772shell_output_destroy(struct shell_output *shell_output)
Xiong Zhang6b481422013-10-23 13:58:32 +08004773{
Pekka Paalanen0d5e4ff2021-05-24 16:13:21 +03004774 struct desktop_shell *shell = shell_output->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004775
Harish Krupodc3edc02019-04-20 17:50:18 +05304776 shell_for_each_layer(shell, shell_output_changed_move_layer, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004777
Pekka Paalanen719ca872021-05-24 16:53:40 +03004778 if (shell_output->fade.animation) {
4779 weston_view_animation_destroy(shell_output->fade.animation);
4780 shell_output->fade.animation = NULL;
4781 }
4782
4783 if (shell_output->fade.view) {
4784 /* destroys the view as well */
4785 weston_surface_destroy(shell_output->fade.view->surface);
4786 }
4787
4788 if (shell_output->fade.startup_timer)
4789 wl_event_source_remove(shell_output->fade.startup_timer);
4790
Pekka Paalanen0d5e4ff2021-05-24 16:13:21 +03004791 if (shell_output->panel_surface)
4792 wl_list_remove(&shell_output->panel_surface_listener.link);
4793 if (shell_output->background_surface)
4794 wl_list_remove(&shell_output->background_surface_listener.link);
4795 wl_list_remove(&shell_output->destroy_listener.link);
4796 wl_list_remove(&shell_output->link);
4797 free(shell_output);
Xiong Zhang6b481422013-10-23 13:58:32 +08004798}
4799
4800static void
Pekka Paalanen719ca872021-05-24 16:53:40 +03004801handle_output_destroy(struct wl_listener *listener, void *data)
4802{
4803 struct shell_output *shell_output =
4804 container_of(listener, struct shell_output, destroy_listener);
4805
4806 shell_output_destroy(shell_output);
4807}
4808
4809static void
David Fort50d962f2016-06-09 17:55:52 +02004810shell_resize_surface_to_output(struct desktop_shell *shell,
4811 struct weston_surface *surface,
4812 const struct weston_output *output)
4813{
4814 if (!surface)
4815 return;
4816
4817 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4818 surface->resource,
4819 output->width,
4820 output->height);
4821}
4822
4823
4824static void
4825handle_output_resized(struct wl_listener *listener, void *data)
4826{
4827 struct desktop_shell *shell =
4828 container_of(listener, struct desktop_shell, resized_listener);
4829 struct weston_output *output = (struct weston_output *)data;
4830 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4831
4832 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4833 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4834}
4835
4836static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004837create_shell_output(struct desktop_shell *shell,
4838 struct weston_output *output)
4839{
4840 struct shell_output *shell_output;
4841
4842 shell_output = zalloc(sizeof *shell_output);
4843 if (shell_output == NULL)
4844 return;
4845
4846 shell_output->output = output;
4847 shell_output->shell = shell;
4848 shell_output->destroy_listener.notify = handle_output_destroy;
4849 wl_signal_add(&output->destroy_signal,
4850 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004851 wl_list_insert(shell->output_list.prev, &shell_output->link);
Harish Krupodc3edc02019-04-20 17:50:18 +05304852
4853 if (wl_list_length(&shell->output_list) == 1)
4854 shell_for_each_layer(shell,
4855 shell_output_changed_move_layer, NULL);
Xiong Zhang6b481422013-10-23 13:58:32 +08004856}
4857
4858static void
4859handle_output_create(struct wl_listener *listener, void *data)
4860{
4861 struct desktop_shell *shell =
4862 container_of(listener, struct desktop_shell, output_create_listener);
4863 struct weston_output *output = (struct weston_output *)data;
4864
4865 create_shell_output(shell, output);
4866}
4867
4868static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004869handle_output_move_layer(struct desktop_shell *shell,
4870 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004871{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004872 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004873 struct weston_view *view;
4874 float x, y;
4875
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004876 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004877 if (view->output != output)
4878 continue;
4879
4880 x = view->geometry.x + output->move_x;
4881 y = view->geometry.y + output->move_y;
4882 weston_view_set_position(view, x, y);
4883 }
4884}
4885
4886static void
4887handle_output_move(struct wl_listener *listener, void *data)
4888{
4889 struct desktop_shell *shell;
4890
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004891 shell = container_of(listener, struct desktop_shell,
4892 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004893
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004894 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004895}
4896
4897static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004898setup_output_destroy_handler(struct weston_compositor *ec,
4899 struct desktop_shell *shell)
4900{
4901 struct weston_output *output;
4902
4903 wl_list_init(&shell->output_list);
4904 wl_list_for_each(output, &ec->output_list, link)
4905 create_shell_output(shell, output);
4906
4907 shell->output_create_listener.notify = handle_output_create;
4908 wl_signal_add(&ec->output_created_signal,
4909 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004910
4911 shell->output_move_listener.notify = handle_output_move;
4912 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08004913}
4914
4915static void
Marius Vladd25d63e2021-11-10 17:50:50 +02004916desktop_shell_destroy_views_on_layer(struct weston_layer *layer)
4917{
4918 struct weston_view *view, *view_next;
4919
4920 wl_list_for_each_safe(view, view_next, &layer->view_list.link, layer_link.link) {
4921 struct shell_surface *shsurf =
4922 get_shell_surface(view->surface);
4923 /* fullscreen_layer is special as it would have a view with an
4924 * additional black_view created and added to its layer_link
4925 * fullscreen view. See shell_ensure_fullscreen_black_view()
4926 *
4927 * As that black_view it is not a weston_desktop_surface
4928 * we can't have a shsurf for it so we just destroy it like
4929 * we do it in desktop_surface_removed() */
4930 if (shsurf)
4931 desktop_shell_destroy_surface(shsurf);
4932 else
4933 weston_surface_destroy(view->surface);
4934 }
4935
4936 weston_layer_fini(layer);
4937}
4938
4939static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004940shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004941{
Tiago Vignattibe143262012-04-16 17:31:41 +03004942 struct desktop_shell *shell =
4943 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004944 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08004945 struct shell_output *shell_output, *tmp;
Marius Vladc114fd62021-08-26 16:37:24 +03004946 struct shell_seat *shseat, *shseat_next;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004947
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08004948 /* Force state to unlocked so we don't try to fade */
4949 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004950
4951 if (shell->child.client) {
4952 /* disable respawn */
4953 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004954 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03004955 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02004956
Harsha M Mb8b2c722018-08-07 19:05:02 +05304957 wl_list_remove(&shell->destroy_listener.link);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004958 wl_list_remove(&shell->idle_listener.link);
4959 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004960 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004961
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03004962 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08004963 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04004964
Pekka Paalanen719ca872021-05-24 16:53:40 +03004965 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link)
4966 shell_output_destroy(shell_output);
Xiong Zhang6b481422013-10-23 13:58:32 +08004967
4968 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07004969 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02004970 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004971
Marius Vladc114fd62021-08-26 16:37:24 +03004972 wl_list_for_each_safe(shseat, shseat_next, &shell->seat_list, link)
4973 desktop_shell_destroy_seat(shseat);
4974
Pekka Paalanenabd72922021-05-24 14:42:00 +03004975 weston_desktop_destroy(shell->desktop);
4976
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004977 wl_array_for_each(ws, &shell->workspaces.array)
4978 workspace_destroy(*ws);
4979 wl_array_release(&shell->workspaces.array);
4980
Marius Vladd25d63e2021-11-10 17:50:50 +02004981 desktop_shell_destroy_views_on_layer(&shell->panel_layer);
4982 desktop_shell_destroy_views_on_layer(&shell->background_layer);
4983 desktop_shell_destroy_views_on_layer(&shell->lock_layer);
4984 desktop_shell_destroy_views_on_layer(&shell->input_panel_layer);
4985 desktop_shell_destroy_views_on_layer(&shell->minimized_layer);
4986 desktop_shell_destroy_views_on_layer(&shell->fullscreen_layer);
Pekka Paalanen4bb326b2021-05-14 14:37:46 +03004987
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004988 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004989 free(shell);
4990}
4991
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004992static void
4993shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4994{
4995 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004996 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03004997
Bob Ham744e6532016-01-12 10:21:48 +00004998 if (shell->allow_zap)
4999 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5000 MODIFIER_CTRL | MODIFIER_ALT,
5001 terminate_binding, ec);
5002
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005003 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005004 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5005 click_to_activate_binding,
5006 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07005007 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
5008 click_to_activate_binding,
5009 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005010 weston_compositor_add_touch_binding(ec, 0,
5011 touch_to_activate_binding,
5012 shell);
Bob Ham553d1242016-01-12 10:21:49 +00005013 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5014 backlight_binding, ec);
5015 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5016 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005017
5018 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00005019 if (shell->exposay_modifier)
5020 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5021 exposay_binding, shell);
5022
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005023 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00005024 if (!mod)
5025 return;
5026
Ian Ray13404c42017-09-18 15:22:01 +03005027 /* This binding is not configurable, but is only enabled if there is a
5028 * valid binding modifier. */
5029 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5030 MODIFIER_SUPER | MODIFIER_ALT,
5031 surface_opacity_binding, NULL);
5032
Ian Rayab7c0b62017-09-18 15:22:00 +03005033 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5034 mod, zoom_axis_binding,
5035 NULL);
5036
Daniel Stone325fc2d2012-05-30 16:31:58 +01005037 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5038 zoom_key_binding, NULL);
5039 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5040 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005041 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5042 maximize_binding, NULL);
5043 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5044 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005045 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5046 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005047 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05005048 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005049 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08005050 weston_compositor_add_button_binding(ec, BTN_LEFT,
5051 mod | MODIFIER_SHIFT,
5052 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005053
5054 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05005055 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005056 rotate_binding, NULL);
5057
Daniel Stone325fc2d2012-05-30 16:31:58 +01005058 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5059 shell);
5060 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5061 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005062 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5063 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005064 weston_compositor_add_key_binding(ec, KEY_K, mod,
5065 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005066 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5067 workspace_up_binding, shell);
5068 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5069 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005070 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5071 workspace_move_surface_up_binding,
5072 shell);
5073 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5074 workspace_move_surface_down_binding,
5075 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005076
5077 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5078 if (shell->workspaces.num > 1) {
5079 num_workspace_bindings = shell->workspaces.num;
5080 if (num_workspace_bindings > 6)
5081 num_workspace_bindings = 6;
5082 for (i = 0; i < num_workspace_bindings; i++)
5083 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5084 workspace_f_binding,
5085 shell);
5086 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005087
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02005088 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005089}
5090
Jason Ekstrand024177c2014-04-21 19:42:58 -05005091static void
5092handle_seat_created(struct wl_listener *listener, void *data)
5093{
5094 struct weston_seat *seat = data;
Marius Vladc114fd62021-08-26 16:37:24 +03005095 struct desktop_shell *shell =
5096 container_of(listener, struct desktop_shell, seat_create_listener);
Jason Ekstrand024177c2014-04-21 19:42:58 -05005097
Marius Vladc114fd62021-08-26 16:37:24 +03005098 create_shell_seat(shell, seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05005099}
5100
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005101WL_EXPORT int
Quentin Glidicda01c1d2016-12-02 14:17:08 +01005102wet_shell_init(struct weston_compositor *ec,
5103 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005104{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005105 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005106 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005107 struct workspace **pws;
5108 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005109 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005110
Peter Huttererf3d62272013-08-08 11:57:05 +10005111 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005112 if (shell == NULL)
5113 return -1;
5114
Kristian Høgsberg75840622011-09-06 13:48:16 -04005115 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005116
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02005117 if (!weston_compositor_add_destroy_listener_once(ec,
5118 &shell->destroy_listener,
5119 shell_destroy)) {
5120 free(shell);
5121 return 0;
5122 }
5123
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005124 shell->idle_listener.notify = idle_handler;
5125 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5126 shell->wake_listener.notify = wake_handler;
5127 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005128 shell->transform_listener.notify = transform_handler;
5129 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005130
Quentin Glidic82681572016-12-17 13:40:51 +01005131 weston_layer_init(&shell->fullscreen_layer, ec);
5132 weston_layer_init(&shell->panel_layer, ec);
5133 weston_layer_init(&shell->background_layer, ec);
5134 weston_layer_init(&shell->lock_layer, ec);
5135 weston_layer_init(&shell->input_panel_layer, ec);
5136
5137 weston_layer_set_position(&shell->fullscreen_layer,
5138 WESTON_LAYER_POSITION_FULLSCREEN);
5139 weston_layer_set_position(&shell->panel_layer,
5140 WESTON_LAYER_POSITION_UI);
5141 weston_layer_set_position(&shell->background_layer,
5142 WESTON_LAYER_POSITION_BACKGROUND);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005143
5144 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005145 wl_list_init(&shell->workspaces.client_list);
Marius Vladc114fd62021-08-26 16:37:24 +03005146 wl_list_init(&shell->seat_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005147
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005148 if (input_panel_setup(shell) < 0)
5149 return -1;
5150
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03005151 shell->text_backend = text_backend_init(ec);
5152 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00005153 return -1;
5154
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005155 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005156
Daniel Stonedf8133b2013-11-19 11:37:14 +01005157 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5158 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5159
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005160 for (i = 0; i < shell->workspaces.num; i++) {
5161 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5162 if (pws == NULL)
5163 return -1;
5164
Quentin Glidic82681572016-12-17 13:40:51 +01005165 *pws = workspace_create(shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005166 if (*pws == NULL)
5167 return -1;
5168 }
5169 activate_workspace(shell, 0);
5170
Quentin Glidic82681572016-12-17 13:40:51 +01005171 weston_layer_init(&shell->minimized_layer, ec);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01005172
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005173 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005174 wl_list_init(&shell->workspaces.animation.link);
5175 shell->workspaces.animation.frame = animate_workspace_change_frame;
5176
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02005177 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
5178 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005179 return -1;
5180
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005181 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005182 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005183 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005184 return -1;
5185
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02005186 weston_compositor_get_time(&shell->child.deathstamp);
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005187
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005188 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02005189
Xiong Zhang6b481422013-10-23 13:58:32 +08005190 setup_output_destroy_handler(ec, shell);
5191
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005192 loop = wl_display_get_event_loop(ec->wl_display);
5193 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005194
Jason Ekstrand024177c2014-04-21 19:42:58 -05005195 wl_list_for_each(seat, &ec->seat_list, link)
Marius Vladc114fd62021-08-26 16:37:24 +03005196 create_shell_seat(shell, seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05005197 shell->seat_create_listener.notify = handle_seat_created;
5198 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005199
David Fort50d962f2016-06-09 17:55:52 +02005200 shell->resized_listener.notify = handle_output_resized;
5201 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
5202
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01005203 screenshooter_create(ec);
5204
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005205 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005206
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005207 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005208
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005209 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
5210
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005211 return 0;
5212}