blob: f126393a06bfcf0f9216af0d7bb39799f7cc33d1 [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"
Alexandros Frantzis8250a612017-11-16 18:20:52 +020044#include "shared/timespec-util.h"
Pekka Paalanen8ebd9812019-04-04 16:02:14 +030045#include <libweston-desktop/libweston-desktop.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050046
Jonas Ådahle3cddce2012-06-13 00:01:22 +020047#define DEFAULT_NUM_WORKSPACES 1
Jonas Ådahl62fcd042012-06-13 00:01:23 +020048#define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
Jonas Ådahle3cddce2012-06-13 00:01:22 +020049
Jonas Ådahl04769742012-06-13 00:01:24 +020050struct focus_state {
Quentin Glidicfff39812016-08-15 10:56:52 +020051 struct desktop_shell *shell;
Jonas Ådahl04769742012-06-13 00:01:24 +020052 struct weston_seat *seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -040053 struct workspace *ws;
Jonas Ådahl04769742012-06-13 00:01:24 +020054 struct weston_surface *keyboard_focus;
55 struct wl_list link;
56 struct wl_listener seat_destroy_listener;
57 struct wl_listener surface_destroy_listener;
58};
59
Philip Withnall648a4dd2013-11-25 18:01:44 +000060/*
61 * Surface stacking and ordering.
62 *
63 * This is handled using several linked lists of surfaces, organised into
64 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
65 * above all of the surfaces in the layer below. The set of layers is static and
66 * in the following order (top-most first):
67 * • Lock layer (only ever displayed on its own)
68 * • Cursor layer
Manuel Bachmann805d2f52014-03-05 12:21:34 +010069 * • Input panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000070 * • Fullscreen layer
71 * • Panel layer
Philip Withnall648a4dd2013-11-25 18:01:44 +000072 * • Workspace layers
73 * • Background layer
74 *
75 * The list of layers may be manipulated to remove whole layers of surfaces from
76 * display. For example, when locking the screen, all layers except the lock
77 * layer are removed.
78 *
79 * A surface’s layer is modified on configuring the surface, in
80 * set_surface_type() (which is only called when the surface’s type change is
81 * _committed_). If a surface’s type changes (e.g. when making a window
82 * fullscreen) its layer changes too.
83 *
84 * In order to allow popup and transient surfaces to be correctly stacked above
85 * their parent surfaces, each surface tracks both its parent surface, and a
86 * linked list of its children. When a surface’s layer is updated, so are the
87 * layers of its children. Note that child surfaces are *not* the same as
88 * subsurfaces — child/parent surfaces are purely for maintaining stacking
89 * order.
90 *
91 * The children_link list of siblings of a surface (i.e. those surfaces which
92 * have the same parent) only contains weston_surfaces which have a
93 * shell_surface. Stacking is not implemented for non-shell_surface
94 * weston_surfaces. This means that the following implication does *not* hold:
95 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
96 */
97
Pekka Paalanen56cdea92011-11-23 16:14:12 +020098struct shell_surface {
Jason Ekstrand651f00e2013-06-14 10:07:54 -050099 struct wl_signal destroy_signal;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200100
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200101 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500102 struct weston_view *view;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600103 int32_t last_width, last_height;
Kristian Høgsberg160fe752014-03-11 10:19:10 -0700104
Tiago Vignattibe143262012-04-16 17:31:41 +0300105 struct desktop_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200106
Stefan Agnera8da2082019-06-23 14:53:59 +0200107 struct wl_list children_list;
108 struct wl_list children_link;
109
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500110 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800111 bool saved_position_valid;
Alex Wu7bcb8bd2012-04-27 09:07:24 +0800112 bool saved_rotation_valid;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700113 int unresponsive, grabbed;
Kristian Høgsberg44cd1962014-02-05 21:36:04 -0800114 uint32_t resize_edges;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100115
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500116 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200117 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500118 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200119 } rotation;
120
121 struct {
Alex Wu4539b082012-03-01 12:57:46 +0800122 struct weston_transform transform; /* matrix from x, y */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500123 struct weston_view *black_view;
Alex Wu4539b082012-03-01 12:57:46 +0800124 } fullscreen;
125
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200126 struct weston_transform workspace_transform;
127
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500128 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500129 struct weston_output *output;
Semi Malinen99f8c082018-05-02 11:10:32 +0200130 struct wl_listener output_destroy_listener;
Rafael Antognolli03b16592013-12-03 15:35:42 -0200131
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700132 struct surface_state {
Quentin Glidic18a81ac2016-08-16 14:26:03 +0200133 bool fullscreen;
134 bool maximized;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +0100135 bool lowered;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200136 } state;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700137
Pekka Paalanen77a6d952016-11-16 15:17:14 +0200138 struct {
139 bool is_set;
140 int32_t x;
141 int32_t y;
142 } xwayland;
143
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500144 int focus_count;
Derek Foreman039e9be2015-04-14 17:09:06 -0500145
146 bool destroying;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200147};
148
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300149struct shell_grab {
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400150 struct weston_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300151 struct shell_surface *shsurf;
152 struct wl_listener shsurf_destroy_listener;
153};
154
Rusty Lynch1084da52013-08-15 09:10:08 -0700155struct shell_touch_grab {
156 struct weston_touch_grab grab;
157 struct shell_surface *shsurf;
158 struct wl_listener shsurf_destroy_listener;
159 struct weston_touch *touch;
160};
161
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300162struct weston_move_grab {
163 struct shell_grab base;
Daniel Stone103db7f2012-05-08 17:17:55 +0100164 wl_fixed_t dx, dy;
Derek Foremancf7d95a2015-06-03 15:53:22 -0500165 bool client_initiated;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500166};
167
Rusty Lynch1084da52013-08-15 09:10:08 -0700168struct weston_touch_move_grab {
169 struct shell_touch_grab base;
Kristian Høgsberg8e80a312014-01-17 15:18:10 -0800170 int active;
Rusty Lynch1084da52013-08-15 09:10:08 -0700171 wl_fixed_t dx, dy;
172};
173
Pekka Paalanen460099f2012-01-20 16:48:25 +0200174struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300175 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500176 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200177 struct {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200178 float x;
179 float y;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200180 } center;
181};
182
Giulio Camuffo5085a752013-03-25 21:42:45 +0100183struct shell_seat {
184 struct weston_seat *seat;
185 struct wl_listener seat_destroy_listener;
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -0500186 struct weston_surface *focused_surface;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100187
Jason Ekstrand024177c2014-04-21 19:42:58 -0500188 struct wl_listener caps_changed_listener;
189 struct wl_listener pointer_focus_listener;
190 struct wl_listener keyboard_focus_listener;
Giulio Camuffo5085a752013-03-25 21:42:45 +0100191};
192
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800193
Alex Wubd3354b2012-04-17 17:20:49 +0800194static struct desktop_shell *
195shell_surface_get_shell(struct shell_surface *shsurf);
196
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500197static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200198set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
199
200static void
Derek Foreman74de4692015-07-15 13:00:39 -0500201surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -0500202
Pekka Paalanen79346ab2013-05-22 18:03:09 +0300203static void
204shell_fade_startup(struct desktop_shell *shell);
205
Bryce Harrington9ad4de12016-09-09 13:16:02 -0700206static void
207shell_fade(struct desktop_shell *shell, enum fade_type type);
208
Philip Withnallbecb77e2013-11-25 18:01:30 +0000209static struct shell_seat *
210get_shell_seat(struct weston_seat *seat);
211
Jonny Lambd73c6942014-08-20 15:53:20 +0200212static void
213get_output_panel_size(struct desktop_shell *shell,
214 struct weston_output *output,
215 int *width, int *height);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -0700216
Philip Withnall648a4dd2013-11-25 18:01:44 +0000217static void
218shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
219
Pekka Paalanen8274d902014-08-06 19:36:51 +0300220static int
221shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
222{
Pekka Paalanen8274d902014-08-06 19:36:51 +0300223 const char *t, *c;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200224 struct weston_desktop_surface *desktop_surface =
225 weston_surface_get_desktop_surface(surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300226
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200227 t = weston_desktop_surface_get_title(desktop_surface);
228 c = weston_desktop_surface_get_app_id(desktop_surface);
Pekka Paalanen8274d902014-08-06 19:36:51 +0300229
230 return snprintf(buf, len, "%s window%s%s%s%s%s",
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200231 "top-level",
Pekka Paalanen8274d902014-08-06 19:36:51 +0300232 t ? " '" : "", t ?: "", t ? "'" : "",
233 c ? " of " : "", c ?: "");
234}
235
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500236static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400237destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300238{
239 struct shell_grab *grab;
240
241 grab = container_of(listener, struct shell_grab,
242 shsurf_destroy_listener);
243
244 grab->shsurf = NULL;
245}
246
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800247struct weston_view *
Jason Ekstranda7af7042013-10-12 22:38:11 -0500248get_default_view(struct weston_surface *surface)
249{
250 struct shell_surface *shsurf;
251 struct weston_view *view;
252
253 if (!surface || wl_list_empty(&surface->views))
254 return NULL;
255
256 shsurf = get_shell_surface(surface);
257 if (shsurf)
258 return shsurf->view;
259
260 wl_list_for_each(view, &surface->views, surface_link)
261 if (weston_view_is_mapped(view))
262 return view;
263
264 return container_of(surface->views.next, struct weston_view, surface_link);
265}
266
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300267static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300268shell_grab_start(struct shell_grab *grab,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400269 const struct weston_pointer_grab_interface *interface,
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300270 struct shell_surface *shsurf,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400271 struct weston_pointer *pointer,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800272 enum weston_desktop_shell_cursor cursor)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300273{
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300274 struct desktop_shell *shell = shsurf->shell;
275
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200276 weston_seat_break_desktop_grabs(pointer->seat);
Kristian Høgsberg57e09072012-10-30 14:07:27 -0400277
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300278 grab->grab.interface = interface;
279 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400280 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
Jason Ekstrand651f00e2013-06-14 10:07:54 -0500281 wl_signal_add(&shsurf->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400282 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300283
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700284 shsurf->grabbed = 1;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -0400285 weston_pointer_start_grab(pointer, &grab->grab);
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400286 if (shell->child.desktop_shell) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800287 weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400288 cursor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500289 weston_pointer_set_focus(pointer,
290 get_default_view(shell->grab_surface),
Kristian Høgsbergc9974a02013-07-03 19:24:57 -0400291 wl_fixed_from_int(0),
292 wl_fixed_from_int(0));
293 }
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300294}
295
Jonny Lambd73c6942014-08-20 15:53:20 +0200296static void
Quentin Glidic581df062016-06-23 18:55:19 +0200297get_panel_size(struct desktop_shell *shell,
298 struct weston_view *view,
299 int *width,
300 int *height)
301{
302 float x1, y1;
303 float x2, y2;
304 weston_view_to_global_float(view, 0, 0, &x1, &y1);
305 weston_view_to_global_float(view,
306 view->surface->width,
307 view->surface->height,
308 &x2, &y2);
309 *width = (int)(x2 - x1);
310 *height = (int)(y2 - y1);
311}
312
313static void
Jonny Lambd73c6942014-08-20 15:53:20 +0200314get_output_panel_size(struct desktop_shell *shell,
315 struct weston_output *output,
316 int *width,
317 int *height)
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700318{
319 struct weston_view *view;
Jonny Lambd73c6942014-08-20 15:53:20 +0200320
321 *width = 0;
322 *height = 0;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700323
324 if (!output)
Jonny Lambd73c6942014-08-20 15:53:20 +0200325 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700326
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300327 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
Quentin Glidic581df062016-06-23 18:55:19 +0200328 if (view->surface->output == output) {
329 get_panel_size(shell, view, width, height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200330 return;
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700331 }
332 }
333
Jonny Lambd73c6942014-08-20 15:53:20 +0200334 /* the correct view wasn't found */
335}
336
Leandro Ribeiro0c59e922020-01-24 17:53:44 -0300337void
Quentin Glidicfff39812016-08-15 10:56:52 +0200338get_output_work_area(struct desktop_shell *shell,
Jonny Lambd73c6942014-08-20 15:53:20 +0200339 struct weston_output *output,
340 pixman_rectangle32_t *area)
341{
342 int32_t panel_width = 0, panel_height = 0;
343
Pekka Paalanen99372ba2018-05-02 10:21:55 +0200344 if (!output) {
345 area->x = 0;
346 area->y = 0;
347 area->width = 0;
348 area->height = 0;
349
350 return;
351 }
352
Derek Foremanf814c5d2015-04-15 12:23:54 -0500353 area->x = output->x;
354 area->y = output->y;
Jonny Lambd73c6942014-08-20 15:53:20 +0200355
356 get_output_panel_size(shell, output, &panel_width, &panel_height);
Jonny Lambd73c6942014-08-20 15:53:20 +0200357 switch (shell->panel_position) {
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800358 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
Jonny Lambd73c6942014-08-20 15:53:20 +0200359 default:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500360 area->y += panel_height;
Daniel Stone2ef9b1a2017-03-13 16:31:36 +0000361 /* fallthrough */
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800362 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
Jonny Lambd73c6942014-08-20 15:53:20 +0200363 area->width = output->width;
364 area->height = output->height - panel_height;
365 break;
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800366 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
Derek Foremanf814c5d2015-04-15 12:23:54 -0500367 area->x += panel_width;
Daniel Stone2ef9b1a2017-03-13 16:31:36 +0000368 /* fallthrough */
Jonas Ådahl6d6fb612015-11-17 16:00:33 +0800369 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
Jonny Lambd73c6942014-08-20 15:53:20 +0200370 area->width = output->width - panel_width;
371 area->height = output->height;
372 break;
373 }
Jasper St. Pierre6458ec32014-04-11 16:00:31 -0700374}
375
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400376static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300377shell_grab_end(struct shell_grab *grab)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300378{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700379 if (grab->shsurf) {
Kristian Høgsberg47b5dca2012-06-07 18:08:04 -0400380 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700381 grab->shsurf->grabbed = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400382
383 if (grab->shsurf->resize_edges) {
384 grab->shsurf->resize_edges = 0;
Jasper St. Pierre5befdda2014-05-06 08:50:47 -0400385 }
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700386 }
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +0300387
Kristian Høgsberg9e5d7d12013-07-22 16:31:53 -0700388 weston_pointer_end_grab(grab->grab.pointer);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300389}
390
391static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700392shell_touch_grab_start(struct shell_touch_grab *grab,
393 const struct weston_touch_grab_interface *interface,
394 struct shell_surface *shsurf,
395 struct weston_touch *touch)
396{
397 struct desktop_shell *shell = shsurf->shell;
U. Artie Eoffcf5737a2014-01-17 10:08:25 -0800398
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200399 weston_seat_break_desktop_grabs(touch->seat);
Kristian Høgsberg74071e02014-04-29 15:01:16 -0700400
Rusty Lynch1084da52013-08-15 09:10:08 -0700401 grab->grab.interface = interface;
402 grab->shsurf = shsurf;
403 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
404 wl_signal_add(&shsurf->destroy_signal,
405 &grab->shsurf_destroy_listener);
406
407 grab->touch = touch;
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700408 shsurf->grabbed = 1;
Rusty Lynch1084da52013-08-15 09:10:08 -0700409
410 weston_touch_start_grab(touch, &grab->grab);
411 if (shell->child.desktop_shell)
Derek Foreman4c93c082015-04-30 16:45:41 -0500412 weston_touch_set_focus(touch,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500413 get_default_view(shell->grab_surface));
Rusty Lynch1084da52013-08-15 09:10:08 -0700414}
415
416static void
417shell_touch_grab_end(struct shell_touch_grab *grab)
418{
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700419 if (grab->shsurf) {
Rusty Lynch1084da52013-08-15 09:10:08 -0700420 wl_list_remove(&grab->shsurf_destroy_listener.link);
Kristian Høgsbergc85f1d42013-10-24 16:52:00 -0700421 grab->shsurf->grabbed = 0;
422 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700423
424 weston_touch_end_grab(grab->touch);
425}
426
427static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500428center_on_output(struct weston_view *view,
Alex Wu4539b082012-03-01 12:57:46 +0800429 struct weston_output *output);
430
Daniel Stone496ca172012-05-30 16:31:42 +0100431static enum weston_keyboard_modifier
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300432get_modifier(char *modifier)
433{
434 if (!modifier)
435 return MODIFIER_SUPER;
436
437 if (!strcmp("ctrl", modifier))
438 return MODIFIER_CTRL;
439 else if (!strcmp("alt", modifier))
440 return MODIFIER_ALT;
441 else if (!strcmp("super", modifier))
442 return MODIFIER_SUPER;
Bob Ham553d1242016-01-12 10:21:49 +0000443 else if (!strcmp("none", modifier))
444 return 0;
Tiago Vignatti0b52d482012-04-20 18:54:25 +0300445 else
446 return MODIFIER_SUPER;
447}
448
Juan Zhaoe10d2792012-04-25 19:09:52 +0800449static enum animation_type
450get_animation_type(char *animation)
451{
U. Artie Eoffb5719102014-01-15 14:26:31 -0800452 if (!animation)
453 return ANIMATION_NONE;
454
Juan Zhaoe10d2792012-04-25 19:09:52 +0800455 if (!strcmp("zoom", animation))
456 return ANIMATION_ZOOM;
457 else if (!strcmp("fade", animation))
458 return ANIMATION_FADE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100459 else if (!strcmp("dim-layer", animation))
460 return ANIMATION_DIM_LAYER;
Juan Zhaoe10d2792012-04-25 19:09:52 +0800461 else
462 return ANIMATION_NONE;
463}
464
Alex Wu4539b082012-03-01 12:57:46 +0800465static void
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400466shell_configuration(struct desktop_shell *shell)
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200467{
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400468 struct weston_config_section *section;
Derek Foremanc7210432014-08-21 11:32:38 -0500469 char *s, *client;
Daniel Stone51d995a2019-11-26 00:14:24 +0000470 bool allow_zap;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200471
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300472 section = weston_config_get_section(wet_get_config(shell->compositor),
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400473 "shell", NULL, NULL);
Marius Vlad64fbd0f2018-12-15 15:51:57 +0200474 client = wet_get_libexec_path(WESTON_SHELL_CLIENT);
Daniel Stonee03c1112016-11-24 20:45:45 +0000475 weston_config_section_get_string(section, "client", &s, client);
Derek Foremanc7210432014-08-21 11:32:38 -0500476 free(client);
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100477 shell->client = s;
Bob Ham744e6532016-01-12 10:21:48 +0000478
479 weston_config_section_get_bool(section,
480 "allow-zap", &allow_zap, true);
481 shell->allow_zap = allow_zap;
482
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +0100483 weston_config_section_get_string(section,
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400484 "binding-modifier", &s, "super");
485 shell->binding_modifier = get_modifier(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200486 free(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800487
488 weston_config_section_get_string(section,
489 "exposay-modifier", &s, "none");
Bob Ham553d1242016-01-12 10:21:49 +0000490 shell->exposay_modifier = get_modifier(s);
Kristian Høgsbergd56ab4e2014-01-16 16:51:52 -0800491 free(s);
492
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400493 weston_config_section_get_string(section, "animation", &s, "none");
494 shell->win_animation_type = get_animation_type(s);
Quentin Glidic8418c292013-06-18 09:11:03 +0200495 free(s);
Jonny Lambf322f8e2014-08-12 15:13:30 +0200496 weston_config_section_get_string(section, "close-animation", &s, "fade");
497 shell->win_close_animation_type = get_animation_type(s);
498 free(s);
Kristian Høgsberg724c8d92013-10-16 11:38:24 -0700499 weston_config_section_get_string(section,
500 "startup-animation", &s, "fade");
501 shell->startup_animation_type = get_animation_type(s);
502 free(s);
Kristian Høgsberg912e0a12013-10-30 08:59:55 -0700503 if (shell->startup_animation_type == ANIMATION_ZOOM)
504 shell->startup_animation_type = ANIMATION_NONE;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100505 weston_config_section_get_string(section, "focus-animation", &s, "none");
506 shell->focus_animation_type = get_animation_type(s);
507 free(s);
Kristian Høgsberg673a8892013-05-23 21:40:56 -0400508 weston_config_section_get_uint(section, "num-workspaces",
509 &shell->workspaces.num,
510 DEFAULT_NUM_WORKSPACES);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200511}
512
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800513struct weston_output *
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100514get_default_output(struct weston_compositor *compositor)
515{
Armin Krezović10b06182016-06-23 11:59:30 +0200516 if (wl_list_empty(&compositor->output_list))
517 return NULL;
518
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100519 return container_of(compositor->output_list.next,
520 struct weston_output, link);
521}
522
Pekka Paalanen8274d902014-08-06 19:36:51 +0300523static int
524focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
525{
526 return snprintf(buf, len, "focus highlight effect for output %s",
Miguel A. Vico620f68d2019-09-25 11:23:13 -0700527 (surface->output ? surface->output->name : "NULL"));
Pekka Paalanen8274d902014-08-06 19:36:51 +0300528}
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100529
530/* no-op func for checking focus surface */
531static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200532focus_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100533{
534}
535
536static struct focus_surface *
537get_focus_surface(struct weston_surface *surface)
538{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200539 if (surface->committed == focus_surface_committed)
540 return surface->committed_private;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100541 else
542 return NULL;
543}
544
545static bool
546is_focus_surface (struct weston_surface *es)
547{
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200548 return (es->committed == focus_surface_committed);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100549}
550
551static bool
552is_focus_view (struct weston_view *view)
553{
554 return is_focus_surface (view->surface);
555}
556
557static struct focus_surface *
558create_focus_surface(struct weston_compositor *ec,
559 struct weston_output *output)
560{
561 struct focus_surface *fsurf = NULL;
562 struct weston_surface *surface = NULL;
563
564 fsurf = malloc(sizeof *fsurf);
565 if (!fsurf)
566 return NULL;
567
568 fsurf->surface = weston_surface_create(ec);
569 surface = fsurf->surface;
570 if (surface == NULL) {
571 free(fsurf);
572 return NULL;
573 }
574
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200575 surface->committed = focus_surface_committed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100576 surface->output = output;
Armin Krezović4663aca2016-06-30 06:04:29 +0200577 surface->is_mapped = true;
Quentin Glidic2edc3d52016-08-12 10:41:33 +0200578 surface->committed_private = fsurf;
Pekka Paalanen8274d902014-08-06 19:36:51 +0300579 weston_surface_set_label_func(surface, focus_surface_get_label);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100580
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800581 fsurf->view = weston_view_create(surface);
582 if (fsurf->view == NULL) {
583 weston_surface_destroy(surface);
584 free(fsurf);
585 return NULL;
586 }
Semi Malinene7a52fb2018-04-26 11:08:10 +0200587 weston_view_set_output(fsurf->view, output);
Armin Krezović4663aca2016-06-30 06:04:29 +0200588 fsurf->view->is_mapped = true;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100589
Jason Ekstrand5c11a332013-12-04 20:32:03 -0600590 weston_surface_set_size(surface, output->width, output->height);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600591 weston_view_set_position(fsurf->view, output->x, output->y);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100592 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
593 pixman_region32_fini(&surface->opaque);
594 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
595 output->width, output->height);
596 pixman_region32_fini(&surface->input);
597 pixman_region32_init(&surface->input);
598
599 wl_list_init(&fsurf->workspace_transform.link);
600
601 return fsurf;
602}
603
604static void
605focus_surface_destroy(struct focus_surface *fsurf)
606{
607 weston_surface_destroy(fsurf->surface);
608 free(fsurf);
609}
610
611static void
612focus_animation_done(struct weston_view_animation *animation, void *data)
613{
614 struct workspace *ws = data;
615
616 ws->focus_animation = NULL;
617}
618
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200619static void
Jonas Ådahl04769742012-06-13 00:01:24 +0200620focus_state_destroy(struct focus_state *state)
621{
622 wl_list_remove(&state->seat_destroy_listener.link);
623 wl_list_remove(&state->surface_destroy_listener.link);
624 free(state);
625}
626
627static void
628focus_state_seat_destroy(struct wl_listener *listener, void *data)
629{
630 struct focus_state *state = container_of(listener,
631 struct focus_state,
632 seat_destroy_listener);
633
634 wl_list_remove(&state->link);
635 focus_state_destroy(state);
636}
637
638static void
639focus_state_surface_destroy(struct wl_listener *listener, void *data)
640{
641 struct focus_state *state = container_of(listener,
642 struct focus_state,
Kristian Høgsbergb8e0d0f2012-07-31 10:30:26 -0400643 surface_destroy_listener);
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200644 struct weston_surface *main_surface;
645 struct weston_view *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500646 struct weston_view *view;
Jonas Ådahl04769742012-06-13 00:01:24 +0200647
Pekka Paalanen01388e22013-04-25 13:57:44 +0300648 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
649
Kristian Høgsberge3778222012-07-31 17:29:30 -0400650 next = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300651 wl_list_for_each(view,
652 &state->ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500653 if (view->surface == main_surface)
Kristian Høgsberge3778222012-07-31 17:29:30 -0400654 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100655 if (is_focus_view(view))
656 continue;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +0200657 if (!get_shell_surface(view->surface))
658 continue;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400659
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200660 next = view;
Kristian Høgsberge3778222012-07-31 17:29:30 -0400661 break;
662 }
663
Pekka Paalanen01388e22013-04-25 13:57:44 +0300664 /* if the focus was a sub-surface, activate its main surface */
665 if (main_surface != state->keyboard_focus)
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +0200666 next = get_default_view(main_surface);
Pekka Paalanen01388e22013-04-25 13:57:44 +0300667
Kristian Høgsberge3778222012-07-31 17:29:30 -0400668 if (next) {
Greg V15d3d302018-12-07 01:33:34 +0300669 if (state->keyboard_focus) {
670 wl_list_remove(&state->surface_destroy_listener.link);
671 wl_list_init(&state->surface_destroy_listener.link);
672 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100673 state->keyboard_focus = NULL;
Quentin Glidicfff39812016-08-15 10:56:52 +0200674 activate(state->shell, next, state->seat,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +0200675 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge3778222012-07-31 17:29:30 -0400676 } else {
Quentin Glidicfff39812016-08-15 10:56:52 +0200677 if (state->shell->focus_animation_type == ANIMATION_DIM_LAYER) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100678 if (state->ws->focus_animation)
679 weston_view_animation_destroy(state->ws->focus_animation);
680
681 state->ws->focus_animation = weston_fade_run(
682 state->ws->fsurf_front->view,
683 state->ws->fsurf_front->view->alpha, 0.0, 300,
684 focus_animation_done, state->ws);
685 }
686
Kristian Høgsberge3778222012-07-31 17:29:30 -0400687 wl_list_remove(&state->link);
688 focus_state_destroy(state);
689 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200690}
691
692static struct focus_state *
Quentin Glidicfff39812016-08-15 10:56:52 +0200693focus_state_create(struct desktop_shell *shell, struct weston_seat *seat,
694 struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200695{
Jonas Ådahl04769742012-06-13 00:01:24 +0200696 struct focus_state *state;
Jonas Ådahl04769742012-06-13 00:01:24 +0200697
698 state = malloc(sizeof *state);
699 if (state == NULL)
700 return NULL;
701
Quentin Glidicfff39812016-08-15 10:56:52 +0200702 state->shell = shell;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100703 state->keyboard_focus = NULL;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400704 state->ws = ws;
Jonas Ådahl04769742012-06-13 00:01:24 +0200705 state->seat = seat;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400706 wl_list_insert(&ws->focus_list, &state->link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200707
708 state->seat_destroy_listener.notify = focus_state_seat_destroy;
709 state->surface_destroy_listener.notify = focus_state_surface_destroy;
Kristian Høgsberg49124542013-05-06 22:27:40 -0400710 wl_signal_add(&seat->destroy_signal,
Jonas Ådahl04769742012-06-13 00:01:24 +0200711 &state->seat_destroy_listener);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400712 wl_list_init(&state->surface_destroy_listener.link);
Jonas Ådahl04769742012-06-13 00:01:24 +0200713
714 return state;
715}
716
Jonas Ådahl8538b222012-08-29 22:13:03 +0200717static struct focus_state *
718ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
719{
720 struct workspace *ws = get_current_workspace(shell);
721 struct focus_state *state;
722
723 wl_list_for_each(state, &ws->focus_list, link)
724 if (state->seat == seat)
725 break;
726
727 if (&state->link == &ws->focus_list)
Quentin Glidicfff39812016-08-15 10:56:52 +0200728 state = focus_state_create(shell, seat, ws);
Jonas Ådahl8538b222012-08-29 22:13:03 +0200729
730 return state;
731}
732
Jonas Ådahl04769742012-06-13 00:01:24 +0200733static void
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800734focus_state_set_focus(struct focus_state *state,
735 struct weston_surface *surface)
736{
737 if (state->keyboard_focus) {
738 wl_list_remove(&state->surface_destroy_listener.link);
739 wl_list_init(&state->surface_destroy_listener.link);
740 }
741
742 state->keyboard_focus = surface;
743 if (surface)
744 wl_signal_add(&surface->destroy_signal,
745 &state->surface_destroy_listener);
746}
747
748static void
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -0400749restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
Jonas Ådahl04769742012-06-13 00:01:24 +0200750{
751 struct focus_state *state, *next;
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400752 struct weston_surface *surface;
Neil Roberts4237f502014-04-09 16:33:31 +0100753 struct wl_list pending_seat_list;
754 struct weston_seat *seat, *next_seat;
755
756 /* Temporarily steal the list of seats so that we can keep
757 * track of the seats we've already processed */
758 wl_list_init(&pending_seat_list);
759 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
760 wl_list_init(&shell->compositor->seat_list);
Jonas Ådahl04769742012-06-13 00:01:24 +0200761
762 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500763 struct weston_keyboard *keyboard =
764 weston_seat_get_keyboard(state->seat);
765
Neil Roberts4237f502014-04-09 16:33:31 +0100766 wl_list_remove(&state->seat->link);
767 wl_list_insert(&shell->compositor->seat_list,
768 &state->seat->link);
769
Derek Foreman1281a362015-07-31 16:55:32 -0500770 if (!keyboard)
Kristian Høgsberge61d2f42014-01-17 12:18:53 -0800771 continue;
772
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -0400773 surface = state->keyboard_focus;
Jonas Ådahl56899442012-08-29 22:12:59 +0200774
Derek Foreman1281a362015-07-31 16:55:32 -0500775 weston_keyboard_set_focus(keyboard, surface);
Jonas Ådahl04769742012-06-13 00:01:24 +0200776 }
Neil Roberts4237f502014-04-09 16:33:31 +0100777
778 /* For any remaining seats that we don't have a focus state
779 * for we'll reset the keyboard focus to NULL */
780 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500781 struct weston_keyboard *keyboard =
782 weston_seat_get_keyboard(seat);
783
Neil Roberts4237f502014-04-09 16:33:31 +0100784 wl_list_insert(&shell->compositor->seat_list, &seat->link);
785
Derek Foreman1281a362015-07-31 16:55:32 -0500786 if (!keyboard)
Neil Roberts4237f502014-04-09 16:33:31 +0100787 continue;
788
Derek Foreman1281a362015-07-31 16:55:32 -0500789 weston_keyboard_set_focus(keyboard, NULL);
Neil Roberts4237f502014-04-09 16:33:31 +0100790 }
Jonas Ådahl04769742012-06-13 00:01:24 +0200791}
792
793static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200794replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
795 struct weston_seat *seat)
796{
Derek Foreman1281a362015-07-31 16:55:32 -0500797 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200798 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200799
800 wl_list_for_each(state, &ws->focus_list, link) {
801 if (state->seat == seat) {
Derek Foreman1281a362015-07-31 16:55:32 -0500802 focus_state_set_focus(state, keyboard->focus);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200803 return;
804 }
805 }
806}
807
808static void
809drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
810 struct weston_surface *surface)
811{
812 struct focus_state *state;
813
814 wl_list_for_each(state, &ws->focus_list, link)
815 if (state->keyboard_focus == surface)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -0800816 focus_state_set_focus(state, NULL);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +0200817}
818
819static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100820animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
821 struct weston_view *from, struct weston_view *to)
822{
823 struct weston_output *output;
824 bool focus_surface_created = false;
825
826 /* FIXME: Only support dim animation using two layers */
827 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
828 return;
829
830 output = get_default_output(shell->compositor);
831 if (ws->fsurf_front == NULL && (from || to)) {
832 ws->fsurf_front = create_focus_surface(shell->compositor, output);
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800833 if (ws->fsurf_front == NULL)
834 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100835 ws->fsurf_front->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800836
837 ws->fsurf_back = create_focus_surface(shell->compositor, output);
838 if (ws->fsurf_back == NULL) {
839 focus_surface_destroy(ws->fsurf_front);
840 return;
841 }
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100842 ws->fsurf_back->view->alpha = 0.0;
U. Artie Eoff0b23b2b2014-01-15 14:45:59 -0800843
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100844 focus_surface_created = true;
845 } else {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300846 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
847 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100848 }
849
850 if (ws->focus_animation) {
851 weston_view_animation_destroy(ws->focus_animation);
852 ws->focus_animation = NULL;
853 }
854
855 if (to)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300856 weston_layer_entry_insert(&to->layer_link,
857 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100858 else if (from)
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300859 weston_layer_entry_insert(&ws->layer.view_list,
860 &ws->fsurf_front->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100861
862 if (focus_surface_created) {
863 ws->focus_animation = weston_fade_run(
864 ws->fsurf_front->view,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200865 ws->fsurf_front->view->alpha, 0.4, 300,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100866 focus_animation_done, ws);
867 } else if (from) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300868 weston_layer_entry_insert(&from->layer_link,
869 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100870 ws->focus_animation = weston_stable_fade_run(
871 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200872 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100873 focus_animation_done, ws);
874 } else if (to) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300875 weston_layer_entry_insert(&ws->layer.view_list,
876 &ws->fsurf_back->view->layer_link);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100877 ws->focus_animation = weston_stable_fade_run(
878 ws->fsurf_front->view, 0.0,
Jonny Lamb7e7d4852014-05-22 22:41:34 +0200879 ws->fsurf_back->view, 0.4,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100880 focus_animation_done, ws);
881 }
882}
883
884static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200885workspace_destroy(struct workspace *ws)
886{
Jonas Ådahl04769742012-06-13 00:01:24 +0200887 struct focus_state *state, *next;
888
889 wl_list_for_each_safe(state, next, &ws->focus_list, link)
890 focus_state_destroy(state);
891
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100892 if (ws->fsurf_front)
893 focus_surface_destroy(ws->fsurf_front);
894 if (ws->fsurf_back)
895 focus_surface_destroy(ws->fsurf_back);
896
Pekka Paalanen4bb326b2021-05-14 14:37:46 +0300897 weston_layer_fini(&ws->layer);
898
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200899 free(ws);
900}
901
Jonas Ådahl04769742012-06-13 00:01:24 +0200902static void
903seat_destroyed(struct wl_listener *listener, void *data)
904{
905 struct weston_seat *seat = data;
906 struct focus_state *state, *next;
907 struct workspace *ws = container_of(listener,
908 struct workspace,
909 seat_destroyed_listener);
910
911 wl_list_for_each_safe(state, next, &ws->focus_list, link)
912 if (state->seat == seat)
913 wl_list_remove(&state->link);
914}
915
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200916static struct workspace *
Quentin Glidic82681572016-12-17 13:40:51 +0100917workspace_create(struct desktop_shell *shell)
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200918{
919 struct workspace *ws = malloc(sizeof *ws);
920 if (ws == NULL)
921 return NULL;
922
Quentin Glidic82681572016-12-17 13:40:51 +0100923 weston_layer_init(&ws->layer, shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200924
Jonas Ådahl04769742012-06-13 00:01:24 +0200925 wl_list_init(&ws->focus_list);
926 wl_list_init(&ws->seat_destroyed_listener.link);
927 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100928 ws->fsurf_front = NULL;
929 ws->fsurf_back = NULL;
930 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200931
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200932 return ws;
933}
934
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200935static int
936workspace_is_empty(struct workspace *ws)
937{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300938 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200939}
940
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200941static struct workspace *
942get_workspace(struct desktop_shell *shell, unsigned int index)
943{
944 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200945 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200946 pws += index;
947 return *pws;
948}
949
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800950struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200951get_current_workspace(struct desktop_shell *shell)
952{
953 return get_workspace(shell, shell->workspaces.current);
954}
955
956static void
957activate_workspace(struct desktop_shell *shell, unsigned int index)
958{
959 struct workspace *ws;
960
961 ws = get_workspace(shell, index);
Quentin Glidic82681572016-12-17 13:40:51 +0100962 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200963
964 shell->workspaces.current = index;
965}
966
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200967static unsigned int
968get_output_height(struct weston_output *output)
969{
970 return abs(output->region.extents.y1 - output->region.extents.y2);
971}
972
Greg Vf57774e2018-07-24 23:21:55 +0300973static struct weston_transform *
974view_get_transform(struct weston_view *view)
975{
976 struct focus_surface *fsurf = NULL;
977 struct shell_surface *shsurf = NULL;
978
979 if (is_focus_view(view)) {
980 fsurf = get_focus_surface(view->surface);
981 return &fsurf->workspace_transform;
982 }
983
984 shsurf = get_shell_surface(view->surface);
985 if (shsurf)
986 return &shsurf->workspace_transform;
987
988 return NULL;
989}
990
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200991static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100992view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200993{
Greg Vf57774e2018-07-24 23:21:55 +0300994 struct weston_transform *transform = view_get_transform(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200995
Greg Vf57774e2018-07-24 23:21:55 +0300996 if (!transform)
997 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100998
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200999 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001000 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001001 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001002
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001003 weston_matrix_init(&transform->matrix);
1004 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001005 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001006 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001007}
1008
1009static void
1010workspace_translate_out(struct workspace *ws, double fraction)
1011{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001012 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001013 unsigned int height;
1014 double d;
1015
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001016 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001017 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001018 d = height * fraction;
1019
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001020 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001021 }
1022}
1023
1024static void
1025workspace_translate_in(struct workspace *ws, double fraction)
1026{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001027 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001028 unsigned int height;
1029 double d;
1030
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001031 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001032 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001033
1034 if (fraction > 0)
1035 d = -(height - height * fraction);
1036 else
1037 d = height + height * fraction;
1038
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001039 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001040 }
1041}
1042
1043static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001044reverse_workspace_change_animation(struct desktop_shell *shell,
1045 unsigned int index,
1046 struct workspace *from,
1047 struct workspace *to)
1048{
1049 shell->workspaces.current = index;
1050
1051 shell->workspaces.anim_to = to;
1052 shell->workspaces.anim_from = from;
1053 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001054 shell->workspaces.anim_timestamp = (struct timespec) { 0 };
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001055
Quentin Glidic82681572016-12-17 13:40:51 +01001056 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1057 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
1058
Scott Moreau4272e632012-08-13 09:58:41 -06001059 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001060}
1061
1062static void
1063workspace_deactivate_transforms(struct workspace *ws)
1064{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001065 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001066 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001067
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001068 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Greg Vf57774e2018-07-24 23:21:55 +03001069 transform = view_get_transform(view);
1070 if (!transform)
1071 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001072
1073 if (!wl_list_empty(&transform->link)) {
1074 wl_list_remove(&transform->link);
1075 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001076 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001077 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001078 }
1079}
1080
1081static void
1082finish_workspace_change_animation(struct desktop_shell *shell,
1083 struct workspace *from,
1084 struct workspace *to)
1085{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001086 struct weston_view *view;
1087
Scott Moreau4272e632012-08-13 09:58:41 -06001088 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001089
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001090 /* Views that extend past the bottom of the output are still
1091 * visible after the workspace animation ends but before its layer
1092 * is hidden. In that case, we need to damage below those views so
1093 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001094 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001095 weston_view_damage_below(view);
1096
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001097 wl_list_remove(&shell->workspaces.animation.link);
1098 workspace_deactivate_transforms(from);
1099 workspace_deactivate_transforms(to);
1100 shell->workspaces.anim_to = NULL;
1101
Quentin Glidic82681572016-12-17 13:40:51 +01001102 weston_layer_unset_position(&shell->workspaces.anim_from->layer);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001103}
1104
1105static void
1106animate_workspace_change_frame(struct weston_animation *animation,
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001107 struct weston_output *output,
1108 const struct timespec *time)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001109{
1110 struct desktop_shell *shell =
1111 container_of(animation, struct desktop_shell,
1112 workspaces.animation);
1113 struct workspace *from = shell->workspaces.anim_from;
1114 struct workspace *to = shell->workspaces.anim_to;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001115 int64_t t;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001116 double x, y;
1117
1118 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1119 finish_workspace_change_animation(shell, from, to);
1120 return;
1121 }
1122
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001123 if (timespec_is_zero(&shell->workspaces.anim_timestamp)) {
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001124 if (shell->workspaces.anim_current == 0.0)
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001125 shell->workspaces.anim_timestamp = *time;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001126 else
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001127 timespec_add_msec(&shell->workspaces.anim_timestamp,
1128 time,
Emmanuel Gil Peyrot426c2462019-02-20 16:33:32 +01001129 /* Inverse of movement function 'y' below. */
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001130 -(asin(1.0 - shell->workspaces.anim_current) *
1131 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1132 M_2_PI));
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001133 }
1134
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001135 t = timespec_sub_to_msec(time, &shell->workspaces.anim_timestamp);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001136
1137 /*
1138 * x = [0, π/2]
1139 * y(x) = sin(x)
1140 */
1141 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1142 y = sin(x);
1143
1144 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001145 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001146
1147 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1148 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1149 shell->workspaces.anim_current = y;
1150
Scott Moreau4272e632012-08-13 09:58:41 -06001151 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001152 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001153 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001154 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001155}
1156
1157static void
1158animate_workspace_change(struct desktop_shell *shell,
1159 unsigned int index,
1160 struct workspace *from,
1161 struct workspace *to)
1162{
1163 struct weston_output *output;
1164
1165 int dir;
1166
1167 if (index > shell->workspaces.current)
1168 dir = -1;
1169 else
1170 dir = 1;
1171
1172 shell->workspaces.current = index;
1173
1174 shell->workspaces.anim_dir = dir;
1175 shell->workspaces.anim_from = from;
1176 shell->workspaces.anim_to = to;
1177 shell->workspaces.anim_current = 0.0;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001178 shell->workspaces.anim_timestamp = (struct timespec) { 0 };
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001179
1180 output = container_of(shell->compositor->output_list.next,
1181 struct weston_output, link);
1182 wl_list_insert(&output->animation_list,
1183 &shell->workspaces.animation.link);
1184
Quentin Glidic82681572016-12-17 13:40:51 +01001185 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1186 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001187
1188 workspace_translate_in(to, 0);
1189
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001190 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001191
Scott Moreau4272e632012-08-13 09:58:41 -06001192 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001193}
1194
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001195static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001196update_workspace(struct desktop_shell *shell, unsigned int index,
1197 struct workspace *from, struct workspace *to)
1198{
1199 shell->workspaces.current = index;
Quentin Glidic82681572016-12-17 13:40:51 +01001200 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1201 weston_layer_unset_position(&from->layer);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001202}
1203
1204static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001205change_workspace(struct desktop_shell *shell, unsigned int index)
1206{
1207 struct workspace *from;
1208 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001209 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001210
1211 if (index == shell->workspaces.current)
1212 return;
1213
1214 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001215 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001216 return;
1217
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001218 from = get_current_workspace(shell);
1219 to = get_workspace(shell, index);
1220
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001221 if (shell->workspaces.anim_from == to &&
1222 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001223 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001224 reverse_workspace_change_animation(shell, index, from, to);
1225 return;
1226 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001227
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001228 if (shell->workspaces.anim_to != NULL)
1229 finish_workspace_change_animation(shell,
1230 shell->workspaces.anim_from,
1231 shell->workspaces.anim_to);
1232
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001233 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001234
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001235 if (shell->focus_animation_type != ANIMATION_NONE) {
1236 wl_list_for_each(state, &from->focus_list, link)
1237 if (state->keyboard_focus)
1238 animate_focus_change(shell, from,
1239 get_default_view(state->keyboard_focus), NULL);
1240
1241 wl_list_for_each(state, &to->focus_list, link)
1242 if (state->keyboard_focus)
1243 animate_focus_change(shell, to,
1244 NULL, get_default_view(state->keyboard_focus));
1245 }
1246
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001247 if (workspace_is_empty(to) && workspace_is_empty(from))
1248 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001249 else
1250 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001251}
1252
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001253static bool
1254workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1255{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001256 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001257 struct wl_list *e;
1258
1259 if (wl_list_empty(list))
1260 return false;
1261
1262 e = list->next;
1263
1264 if (e->next != list)
1265 return false;
1266
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001267 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001268}
1269
1270static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001271surface_keyboard_focus_lost(struct weston_surface *surface)
1272{
1273 struct weston_compositor *compositor = surface->compositor;
1274 struct weston_seat *seat;
1275 struct weston_surface *focus;
1276
1277 wl_list_for_each(seat, &compositor->seat_list, link) {
1278 struct weston_keyboard *keyboard =
1279 weston_seat_get_keyboard(seat);
1280
1281 if (!keyboard)
1282 continue;
1283
1284 focus = weston_surface_get_main_surface(keyboard->focus);
1285 if (focus == surface)
1286 weston_keyboard_set_focus(keyboard, NULL);
1287 }
1288}
1289
1290static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001291take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001292 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001293 unsigned int index)
1294{
Derek Foreman1281a362015-07-31 16:55:32 -05001295 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001296 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001297 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001298 struct shell_surface *shsurf;
1299 struct workspace *from;
1300 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001301 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001302
Derek Foreman1281a362015-07-31 16:55:32 -05001303 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001304 view = get_default_view(surface);
1305 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001306 index == shell->workspaces.current ||
1307 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001308 return;
1309
1310 from = get_current_workspace(shell);
1311 to = get_workspace(shell, index);
1312
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001313 weston_layer_entry_remove(&view->layer_link);
1314 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001315
Philip Withnall659163d2013-11-25 18:01:36 +00001316 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001317 if (shsurf != NULL)
1318 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001319
Jonas Ådahle9d22502012-08-29 22:13:01 +02001320 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001321 drop_focus_state(shell, from, surface);
1322
1323 if (shell->workspaces.anim_from == to &&
1324 shell->workspaces.anim_to == from) {
1325 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001326
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001327 return;
1328 }
1329
1330 if (shell->workspaces.anim_to != NULL)
1331 finish_workspace_change_animation(shell,
1332 shell->workspaces.anim_from,
1333 shell->workspaces.anim_to);
1334
1335 if (workspace_is_empty(from) &&
1336 workspace_has_only(to, surface))
1337 update_workspace(shell, index, from, to);
1338 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001339 if (shsurf != NULL &&
1340 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001341 wl_list_insert(&shell->workspaces.anim_sticky_list,
1342 &shsurf->workspace_transform.link);
1343
1344 animate_workspace_change(shell, index, from, to);
1345 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001346
Jonas Ådahl8538b222012-08-29 22:13:03 +02001347 state = ensure_focus_state(shell, seat);
1348 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001349 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001350}
1351
1352static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02001353touch_move_grab_down(struct weston_touch_grab *grab,
1354 const struct timespec *time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001355 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001356{
1357}
1358
1359static void
Alexandros Frantzis27a51b82017-11-16 18:20:59 +02001360touch_move_grab_up(struct weston_touch_grab *grab, const struct timespec *time,
1361 int touch_id)
Rusty Lynch1084da52013-08-15 09:10:08 -07001362{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001363 struct weston_touch_move_grab *move =
1364 (struct weston_touch_move_grab *) container_of(
1365 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001366
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001367 if (touch_id == 0)
1368 move->active = 0;
1369
Jonas Ådahl9484b692013-12-02 22:05:03 +01001370 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001371 shell_touch_grab_end(&move->base);
1372 free(move);
1373 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001374}
1375
1376static void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +02001377touch_move_grab_motion(struct weston_touch_grab *grab,
1378 const struct timespec *time, int touch_id,
1379 wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001380{
1381 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1382 struct shell_surface *shsurf = move->base.shsurf;
1383 struct weston_surface *es;
1384 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1385 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1386
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001387 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001388 return;
1389
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001390 es = weston_desktop_surface_get_surface(shsurf->desktop_surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001391
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001392 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001393
1394 weston_compositor_schedule_repaint(es->compositor);
1395}
1396
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001397static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001398touch_move_grab_frame(struct weston_touch_grab *grab)
1399{
1400}
1401
1402static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001403touch_move_grab_cancel(struct weston_touch_grab *grab)
1404{
1405 struct weston_touch_move_grab *move =
1406 (struct weston_touch_move_grab *) container_of(
1407 grab, struct shell_touch_grab, grab);
1408
1409 shell_touch_grab_end(&move->base);
1410 free(move);
1411}
1412
Rusty Lynch1084da52013-08-15 09:10:08 -07001413static const struct weston_touch_grab_interface touch_move_grab_interface = {
1414 touch_move_grab_down,
1415 touch_move_grab_up,
1416 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001417 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001418 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001419};
1420
1421static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001422surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001423{
1424 struct weston_touch_move_grab *move;
1425
1426 if (!shsurf)
1427 return -1;
1428
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001429 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1430 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Rusty Lynch1084da52013-08-15 09:10:08 -07001431 return 0;
1432
1433 move = malloc(sizeof *move);
1434 if (!move)
1435 return -1;
1436
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001437 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001438 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001439 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001440 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001441 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001442
1443 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001444 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001445
1446 return 0;
1447}
1448
1449static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001450noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001451{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001452}
1453
1454static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001455noop_grab_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02001456 const struct timespec *time,
1457 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001458{
1459}
1460
1461static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001462noop_grab_axis_source(struct weston_pointer_grab *grab,
1463 uint32_t source)
1464{
1465}
1466
1467static void
1468noop_grab_frame(struct weston_pointer_grab *grab)
1469{
1470}
1471
1472static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001473constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1474{
1475 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001476 struct weston_surface *surface =
1477 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001478 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001479 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001480 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001481 pixman_rectangle32_t area;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001482 struct weston_geometry geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001483
1484 x = wl_fixed_to_int(pointer->x + move->dx);
1485 y = wl_fixed_to_int(pointer->y + move->dy);
1486
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001487 if (shsurf->shell->panel_position ==
1488 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001489 get_output_work_area(shsurf->shell, surface->output, &area);
1490 geometry =
1491 weston_desktop_surface_get_geometry(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001492
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001493 bottom = y + geometry.height + geometry.y;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001494 if (bottom - safety < area.y)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001495 y = area.y + safety - geometry.height
1496 - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001497
1498 if (move->client_initiated &&
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001499 y + geometry.y < area.y)
1500 y = area.y - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001501 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001502
1503 *cx = x;
1504 *cy = y;
1505}
1506
1507static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001508move_grab_motion(struct weston_pointer_grab *grab,
1509 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001510 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001511{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001512 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001513 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001514 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001515 struct weston_surface *surface;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001516 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001517
Jonas Ådahld2510102014-10-05 21:39:14 +02001518 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001519 if (!shsurf)
1520 return;
1521
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001522 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1523
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001524 constrain_position(move, &cx, &cy);
1525
1526 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001527
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001528 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001529}
1530
1531static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001532move_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001533 const struct timespec *time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001534{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001535 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1536 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001537 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001538 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001539
Daniel Stone4dbadb12012-05-30 16:31:51 +01001540 if (pointer->button_count == 0 &&
1541 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001542 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001543 free(grab);
1544 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001545}
1546
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001547static void
1548move_grab_cancel(struct weston_pointer_grab *grab)
1549{
1550 struct shell_grab *shell_grab =
1551 container_of(grab, struct shell_grab, grab);
1552
1553 shell_grab_end(shell_grab);
1554 free(grab);
1555}
1556
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001557static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001558 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001559 move_grab_motion,
1560 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001561 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001562 noop_grab_axis_source,
1563 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001564 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001565};
1566
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001567static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001568surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001569 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001570{
1571 struct weston_move_grab *move;
1572
1573 if (!shsurf)
1574 return -1;
1575
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001576 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001577 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1578 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001579 return 0;
1580
1581 move = malloc(sizeof *move);
1582 if (!move)
1583 return -1;
1584
Jason Ekstranda7af7042013-10-12 22:38:11 -05001585 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001586 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001587 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001588 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001589 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001590
1591 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001592 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001593
1594 return 0;
1595}
1596
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001597struct weston_resize_grab {
1598 struct shell_grab base;
1599 uint32_t edges;
1600 int32_t width, height;
1601};
1602
1603static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001604resize_grab_motion(struct weston_pointer_grab *grab,
1605 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001606 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001607{
1608 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001609 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001610 struct shell_surface *shsurf = resize->base.shsurf;
1611 int32_t width, height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001612 struct weston_size min_size, max_size;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001613 wl_fixed_t from_x, from_y;
1614 wl_fixed_t to_x, to_y;
1615
Jonas Ådahld2510102014-10-05 21:39:14 +02001616 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001617
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001618 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001619 return;
1620
Jason Ekstranda7af7042013-10-12 22:38:11 -05001621 weston_view_from_global_fixed(shsurf->view,
1622 pointer->grab_x, pointer->grab_y,
1623 &from_x, &from_y);
1624 weston_view_from_global_fixed(shsurf->view,
1625 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001626
1627 width = resize->width;
1628 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1629 width += wl_fixed_to_int(from_x - to_x);
1630 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1631 width += wl_fixed_to_int(to_x - from_x);
1632 }
1633
1634 height = resize->height;
1635 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1636 height += wl_fixed_to_int(from_y - to_y);
1637 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1638 height += wl_fixed_to_int(to_y - from_y);
1639 }
1640
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001641 max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
1642 min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
1643
1644 min_size.width = MAX(1, min_size.width);
1645 min_size.height = MAX(1, min_size.height);
1646
1647 if (width < min_size.width)
1648 width = min_size.width;
1649 else if (max_size.width > 0 && width > max_size.width)
1650 width = max_size.width;
1651 if (height < min_size.height)
1652 height = min_size.height;
1653 else if (max_size.width > 0 && width > max_size.width)
1654 width = max_size.width;
1655 weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001656}
1657
1658static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001659resize_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001660 const struct timespec *time,
1661 uint32_t button, uint32_t state_w)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001662{
1663 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001664 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001665 enum wl_pointer_button_state state = state_w;
1666
1667 if (pointer->button_count == 0 &&
1668 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Greg Vec3f7792018-11-08 00:24:56 +03001669 if (resize->base.shsurf != NULL) {
1670 struct weston_desktop_surface *desktop_surface =
1671 resize->base.shsurf->desktop_surface;
1672 weston_desktop_surface_set_resizing(desktop_surface,
1673 false);
1674 }
1675
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001676 shell_grab_end(&resize->base);
1677 free(grab);
1678 }
1679}
1680
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001681static void
1682resize_grab_cancel(struct weston_pointer_grab *grab)
1683{
1684 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1685
Greg Vec3f7792018-11-08 00:24:56 +03001686 if (resize->base.shsurf != NULL) {
1687 struct weston_desktop_surface *desktop_surface =
1688 resize->base.shsurf->desktop_surface;
1689 weston_desktop_surface_set_resizing(desktop_surface, false);
1690 }
1691
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001692 shell_grab_end(&resize->base);
1693 free(grab);
1694}
1695
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001696static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001697 noop_grab_focus,
1698 resize_grab_motion,
1699 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001700 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001701 noop_grab_axis_source,
1702 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001703 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001704};
1705
Giulio Camuffob8366642013-04-25 13:57:46 +03001706/*
1707 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001708 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001709static void
1710surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1711 int32_t *y, int32_t *w, int32_t *h) {
1712 pixman_region32_t region;
1713 pixman_box32_t *box;
1714 struct weston_subsurface *subsurface;
1715
1716 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001717 surface->width,
1718 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001719
1720 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1721 pixman_region32_union_rect(&region, &region,
1722 subsurface->position.x,
1723 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001724 subsurface->surface->width,
1725 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001726 }
1727
1728 box = pixman_region32_extents(&region);
1729 if (x)
1730 *x = box->x1;
1731 if (y)
1732 *y = box->y1;
1733 if (w)
1734 *w = box->x2 - box->x1;
1735 if (h)
1736 *h = box->y2 - box->y1;
1737
1738 pixman_region32_fini(&region);
1739}
1740
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001741static int
1742surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001743 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001744{
1745 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001746 const unsigned resize_topbottom =
1747 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1748 const unsigned resize_leftright =
1749 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1750 const unsigned resize_any = resize_topbottom | resize_leftright;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001751 struct weston_geometry geometry;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001752
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001753 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001754 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1755 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001756 return 0;
1757
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001758 /* Check for invalid edge combinations. */
1759 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1760 (edges & resize_topbottom) == resize_topbottom ||
1761 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001762 return 0;
1763
1764 resize = malloc(sizeof *resize);
1765 if (!resize)
1766 return -1;
1767
1768 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001769
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001770 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1771 resize->width = geometry.width;
1772 resize->height = geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001773
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001774 shsurf->resize_edges = edges;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001775 weston_desktop_surface_set_resizing(shsurf->desktop_surface, true);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001776 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001777 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001778
1779 return 0;
1780}
1781
1782static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001783busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001784{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001785 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001786 struct weston_pointer *pointer = base->pointer;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001787 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001788 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001789 wl_fixed_t sx, sy;
1790
Jason Ekstranda7af7042013-10-12 22:38:11 -05001791 view = weston_compositor_pick_view(pointer->seat->compositor,
1792 pointer->x, pointer->y,
1793 &sx, &sy);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001794 desktop_surface = weston_surface_get_desktop_surface(view->surface);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001795
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001796 if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001797 shell_grab_end(grab);
1798 free(grab);
1799 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001800}
1801
1802static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001803busy_cursor_grab_motion(struct weston_pointer_grab *grab,
1804 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001805 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001806{
Jonas Ådahld2510102014-10-05 21:39:14 +02001807 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001808}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001809
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001810static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001811busy_cursor_grab_button(struct weston_pointer_grab *base,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001812 const struct timespec *time,
1813 uint32_t button, uint32_t state)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001814{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001815 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001816 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05001817 struct weston_pointer *pointer = grab->grab.pointer;
1818 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001819
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001820 if (shsurf && button == BTN_LEFT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001821 activate(shsurf->shell, shsurf->view, seat,
1822 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman794fa0e2015-07-15 13:00:38 -05001823 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001824 } else if (shsurf && button == BTN_RIGHT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001825 activate(shsurf->shell, shsurf->view, seat,
1826 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman74de4692015-07-15 13:00:39 -05001827 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001828 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001829}
1830
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001831static void
1832busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1833{
1834 struct shell_grab *grab = (struct shell_grab *) base;
1835
1836 shell_grab_end(grab);
1837 free(grab);
1838}
1839
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001840static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001841 busy_cursor_grab_focus,
1842 busy_cursor_grab_motion,
1843 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001844 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001845 noop_grab_axis_source,
1846 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001847 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001848};
1849
1850static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001851handle_pointer_focus(struct wl_listener *listener, void *data)
1852{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001853 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001854 struct weston_view *view = pointer->focus;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001855 struct shell_surface *shsurf;
1856 struct weston_desktop_client *client;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001857
Jason Ekstranda7af7042013-10-12 22:38:11 -05001858 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001859 return;
1860
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001861 shsurf = get_shell_surface(view->surface);
1862 if (!shsurf)
1863 return;
1864
1865 client = weston_desktop_surface_get_client(shsurf->desktop_surface);
1866
1867 if (shsurf->unresponsive)
1868 set_busy_cursor(shsurf, pointer);
1869 else
1870 weston_desktop_client_ping(client);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001871}
1872
1873static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001874shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1875{
1876 if (--shsurf->focus_count == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001877 weston_desktop_surface_set_activated(shsurf->desktop_surface, false);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001878}
1879
1880static void
1881shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1882{
1883 if (shsurf->focus_count++ == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001884 weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001885}
1886
1887static void
1888handle_keyboard_focus(struct wl_listener *listener, void *data)
1889{
1890 struct weston_keyboard *keyboard = data;
1891 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1892
1893 if (seat->focused_surface) {
1894 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1895 if (shsurf)
1896 shell_surface_lose_keyboard_focus(shsurf);
1897 }
1898
Philipp Kerlingba8a0d02017-07-26 12:02:15 +02001899 seat->focused_surface = weston_surface_get_main_surface(keyboard->focus);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001900
1901 if (seat->focused_surface) {
1902 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1903 if (shsurf)
1904 shell_surface_gain_keyboard_focus(shsurf);
1905 }
1906}
1907
Philip Withnall07926d92013-11-25 18:01:40 +00001908/* The surface will be inserted into the list immediately after the link
1909 * returned by this function (i.e. will be stacked immediately above the
1910 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001911static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00001912shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1913{
1914 struct workspace *ws;
1915
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001916 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
1917 !shsurf->state.lowered) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001918 return &shsurf->shell->fullscreen_layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001919 }
1920
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001921 /* Move the surface to a normal workspace layer so that surfaces
1922 * which were previously fullscreen or transient are no longer
1923 * rendered on top. */
1924 ws = get_current_workspace(shsurf->shell);
1925 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001926}
1927
Philip Withnall648a4dd2013-11-25 18:01:44 +00001928static void
1929shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
1930{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001931 weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001932}
1933
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001934/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00001935 * geometry to ensure the changes are redrawn.
1936 *
1937 * If any child surfaces exist and are mapped, ensure they’re in the same layer
1938 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001939static void
1940shell_surface_update_layer(struct shell_surface *shsurf)
1941{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001942 struct weston_surface *surface =
1943 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001944 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001945
1946 new_layer_link = shell_surface_calculate_layer_link(shsurf);
1947
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001948 if (new_layer_link == NULL)
1949 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001950 if (new_layer_link == &shsurf->view->layer_link)
1951 return;
1952
1953 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001954 weston_layer_entry_remove(&shsurf->view->layer_link);
1955 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001956 weston_view_geometry_dirty(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001957 weston_surface_damage(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001958
1959 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001960}
1961
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001962static void
Semi Malinen99f8c082018-05-02 11:10:32 +02001963notify_output_destroy(struct wl_listener *listener, void *data)
1964{
1965 struct shell_surface *shsurf =
1966 container_of(listener,
1967 struct shell_surface, output_destroy_listener);
1968
1969 shsurf->output = NULL;
1970 shsurf->output_destroy_listener.notify = NULL;
1971}
1972
1973static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00001974shell_surface_set_output(struct shell_surface *shsurf,
1975 struct weston_output *output)
1976{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001977 struct weston_surface *es =
1978 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001979
1980 /* get the default output, if the client set it as NULL
Abdur Rehman7f1da1f2017-01-01 19:46:33 +05001981 check whether the output is available */
Philip Withnall352e7ed2013-11-25 18:01:35 +00001982 if (output)
1983 shsurf->output = output;
1984 else if (es->output)
1985 shsurf->output = es->output;
1986 else
1987 shsurf->output = get_default_output(es->compositor);
Semi Malinen99f8c082018-05-02 11:10:32 +02001988
1989 if (shsurf->output_destroy_listener.notify) {
1990 wl_list_remove(&shsurf->output_destroy_listener.link);
1991 shsurf->output_destroy_listener.notify = NULL;
1992 }
1993
1994 if (!shsurf->output)
1995 return;
1996
1997 shsurf->output_destroy_listener.notify = notify_output_destroy;
1998 wl_signal_add(&shsurf->output->destroy_signal,
1999 &shsurf->output_destroy_listener);
Philip Withnall352e7ed2013-11-25 18:01:35 +00002000}
2001
2002static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002003weston_view_set_initial_position(struct weston_view *view,
2004 struct desktop_shell *shell);
2005
2006static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002007unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002008{
Philip Withnallf85fe842013-11-25 18:01:37 +00002009 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wu4539b082012-03-01 12:57:46 +08002010 wl_list_remove(&shsurf->fullscreen.transform.link);
2011 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002012
Jason Ekstranda7af7042013-10-12 22:38:11 -05002013 if (shsurf->fullscreen.black_view)
2014 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2015 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002016
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002017 if (shsurf->saved_position_valid)
2018 weston_view_set_position(shsurf->view,
2019 shsurf->saved_x, shsurf->saved_y);
2020 else
2021 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02002022 shsurf->saved_position_valid = false;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002023
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002024 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002025 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002026 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002027 shsurf->saved_rotation_valid = false;
2028 }
Alex Wu4539b082012-03-01 12:57:46 +08002029}
2030
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002031static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002032unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002033{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002034 struct weston_surface *surface =
2035 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2036
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002037 /* undo all maximized things here */
Semi Malinen99f8c082018-05-02 11:10:32 +02002038 shell_surface_set_output(shsurf, get_default_output(surface->compositor));
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002039
2040 if (shsurf->saved_position_valid)
2041 weston_view_set_position(shsurf->view,
2042 shsurf->saved_x, shsurf->saved_y);
2043 else
2044 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02002045 shsurf->saved_position_valid = false;
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002046
2047 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002048 wl_list_insert(&shsurf->view->geometry.transformation_list,
2049 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002050 shsurf->saved_rotation_valid = false;
2051 }
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002052}
2053
Philip Withnallbecb77e2013-11-25 18:01:30 +00002054static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002055set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002056{
2057 struct shell_surface *shsurf;
2058 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002059 struct weston_view *view;
2060
2061 view = get_default_view(surface);
2062 if (!view)
2063 return;
2064
2065 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2066
2067 shsurf = get_shell_surface(surface);
2068 current_ws = get_current_workspace(shsurf->shell);
2069
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002070 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002071 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002072
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002073 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002074 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002075
2076 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002077 weston_view_damage_below(view);
2078}
2079
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002080
Tiago Vignattibe143262012-04-16 17:31:41 +03002081static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002082shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002083{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002084 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002085}
2086
Pekka Paalanen8274d902014-08-06 19:36:51 +03002087static int
2088black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2089{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002090 struct weston_view *fs_view = surface->committed_private;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002091 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002092 int n;
2093 int rem;
2094 int ret;
2095
2096 n = snprintf(buf, len, "black background surface for ");
2097 if (n < 0)
2098 return n;
2099
2100 rem = (int)len - n;
2101 if (rem < 0)
2102 rem = 0;
2103
2104 if (fs_surface->get_label)
2105 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2106 else
2107 ret = snprintf(buf + n, rem, "<unknown>");
2108
2109 if (ret < 0)
2110 return n;
2111
2112 return n + ret;
2113}
2114
Alex Wu21858432012-04-01 20:13:08 +08002115static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002116black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002117
Jason Ekstranda7af7042013-10-12 22:38:11 -05002118static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002119create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002120 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002121 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002122{
2123 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002124 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002125
2126 surface = weston_surface_create(ec);
2127 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002128 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002129 return NULL;
2130 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002131 view = weston_view_create(surface);
2132 if (surface == NULL) {
2133 weston_log("no memory\n");
2134 weston_surface_destroy(surface);
2135 return NULL;
2136 }
Alex Wu4539b082012-03-01 12:57:46 +08002137
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002138 surface->committed = black_surface_committed;
2139 surface->committed_private = fs_view;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002140 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002141 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002142 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002143 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002144 pixman_region32_fini(&surface->input);
2145 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002146
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002147 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002148 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002149
2150 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002151}
2152
Philip Withnalled8f1a92013-11-25 18:01:43 +00002153static void
2154shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2155{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002156 struct weston_surface *surface =
2157 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002158 struct weston_output *output = shsurf->fullscreen_output;
2159
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002160 assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
Philip Withnalled8f1a92013-11-25 18:01:43 +00002161
2162 if (!shsurf->fullscreen.black_view)
2163 shsurf->fullscreen.black_view =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002164 create_black_surface(surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002165 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002166 output->x, output->y,
2167 output->width,
2168 output->height);
2169
2170 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002171 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2172 weston_layer_entry_insert(&shsurf->view->layer_link,
2173 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002174 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002175 weston_surface_damage(surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002176
Armin Krezović4663aca2016-06-30 06:04:29 +02002177 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002178 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002179}
2180
Alex Wu4539b082012-03-01 12:57:46 +08002181/* Create black surface and append it to the associated fullscreen surface.
2182 * Handle size dismatch and positioning according to the method. */
2183static void
2184shell_configure_fullscreen(struct shell_surface *shsurf)
2185{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002186 struct weston_surface *surface =
2187 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffob8366642013-04-25 13:57:46 +03002188 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002189
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002190 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002191 weston_layer_entry_remove(&shsurf->view->layer_link);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002192 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2193 &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002194
Pekka Paalanenc63acc42018-05-02 10:21:58 +02002195 if (!shsurf->fullscreen_output) {
2196 /* If there is no output, there's not much we can do.
2197 * Position the window somewhere, whatever. */
2198 weston_view_set_position(shsurf->view, 0, 0);
2199 return;
2200 }
2201
Philip Withnalled8f1a92013-11-25 18:01:43 +00002202 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002203
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002204 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002205 &surf_width, &surf_height);
2206
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002207 if (surface->buffer_ref.buffer)
2208 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002209}
2210
Alex Wu4539b082012-03-01 12:57:46 +08002211static void
2212shell_map_fullscreen(struct shell_surface *shsurf)
2213{
Alex Wubd3354b2012-04-17 17:20:49 +08002214 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002215}
2216
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002217static struct weston_output *
2218get_focused_output(struct weston_compositor *compositor)
2219{
2220 struct weston_seat *seat;
2221 struct weston_output *output = NULL;
2222
2223 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002224 struct weston_touch *touch = weston_seat_get_touch(seat);
2225 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2226 struct weston_keyboard *keyboard =
2227 weston_seat_get_keyboard(seat);
2228
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002229 /* Priority has touch focus, then pointer and
2230 * then keyboard focus. We should probably have
Emmanuel Gil Peyrot426c2462019-02-20 16:33:32 +01002231 * three for loops and check first for touch,
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002232 * then for pointer, etc. but unless somebody has some
2233 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002234 if (touch && touch->focus)
2235 output = touch->focus->output;
2236 else if (pointer && pointer->focus)
2237 output = pointer->focus->output;
2238 else if (keyboard && keyboard->focus)
2239 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002240
2241 if (output)
2242 break;
2243 }
2244
2245 return output;
2246}
2247
2248static void
Giulio Camuffo5085a752013-03-25 21:42:45 +01002249destroy_shell_seat(struct wl_listener *listener, void *data)
2250{
2251 struct shell_seat *shseat =
2252 container_of(listener,
2253 struct shell_seat, seat_destroy_listener);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002254
2255 wl_list_remove(&shseat->seat_destroy_listener.link);
2256 free(shseat);
2257}
2258
Jason Ekstrand024177c2014-04-21 19:42:58 -05002259static void
2260shell_seat_caps_changed(struct wl_listener *listener, void *data)
2261{
Derek Foreman1281a362015-07-31 16:55:32 -05002262 struct weston_keyboard *keyboard;
2263 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05002264 struct shell_seat *seat;
2265
2266 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002267 keyboard = weston_seat_get_keyboard(seat->seat);
2268 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002269
Derek Foreman1281a362015-07-31 16:55:32 -05002270 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002271 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002272 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002273 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002274 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05002275 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002276 wl_list_init(&seat->keyboard_focus_listener.link);
2277 }
2278
Derek Foreman1281a362015-07-31 16:55:32 -05002279 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002280 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002281 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002282 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002283 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05002284 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002285 wl_list_init(&seat->pointer_focus_listener.link);
2286 }
2287}
2288
Giulio Camuffo5085a752013-03-25 21:42:45 +01002289static struct shell_seat *
2290create_shell_seat(struct weston_seat *seat)
2291{
2292 struct shell_seat *shseat;
2293
2294 shseat = calloc(1, sizeof *shseat);
2295 if (!shseat) {
2296 weston_log("no memory to allocate shell seat\n");
2297 return NULL;
2298 }
2299
2300 shseat->seat = seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002301
2302 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2303 wl_signal_add(&seat->destroy_signal,
2304 &shseat->seat_destroy_listener);
2305
Jason Ekstrand024177c2014-04-21 19:42:58 -05002306 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2307 wl_list_init(&shseat->keyboard_focus_listener.link);
2308
2309 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2310 wl_list_init(&shseat->pointer_focus_listener.link);
2311
2312 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2313 wl_signal_add(&seat->updated_caps_signal,
2314 &shseat->caps_changed_listener);
2315 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2316
Giulio Camuffo5085a752013-03-25 21:42:45 +01002317 return shseat;
2318}
2319
2320static struct shell_seat *
2321get_shell_seat(struct weston_seat *seat)
2322{
2323 struct wl_listener *listener;
2324
2325 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002326 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002327
2328 return container_of(listener,
2329 struct shell_seat, seat_destroy_listener);
2330}
2331
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002332static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002333fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002334{
2335 struct shell_surface *shsurf = data;
2336
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002337 weston_surface_destroy(shsurf->view->surface);
Pekka Paalanen99628002018-05-22 12:48:35 +03002338
2339 if (shsurf->output_destroy_listener.notify) {
2340 wl_list_remove(&shsurf->output_destroy_listener.link);
2341 shsurf->output_destroy_listener.notify = NULL;
2342 }
2343
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002344 free(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002345}
2346
2347static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002348fade_out_done(struct weston_view_animation *animation, void *data)
2349{
2350 struct shell_surface *shsurf = data;
2351 struct wl_event_loop *loop;
2352
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002353 loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
Derek Foreman039e9be2015-04-14 17:09:06 -05002354
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002355 if (weston_view_is_mapped(shsurf->view)) {
Tomohito Esaki5898cd32019-04-01 17:50:14 +09002356 weston_view_unmap(shsurf->view);
Derek Foreman039e9be2015-04-14 17:09:06 -05002357 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
Derek Foreman039e9be2015-04-14 17:09:06 -05002358 }
2359}
2360
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002361struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002362get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002363{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002364 if (weston_surface_is_desktop_surface(surface)) {
2365 struct weston_desktop_surface *desktop_surface =
2366 weston_surface_get_desktop_surface(surface);
2367 return weston_desktop_surface_get_user_data(desktop_surface);
2368 }
2369 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002370}
2371
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002372/*
2373 * libweston-desktop
2374 */
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002375
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002376static void
2377desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2378 void *shell)
2379{
2380 struct weston_desktop_client *client =
2381 weston_desktop_surface_get_client(desktop_surface);
2382 struct wl_client *wl_client =
2383 weston_desktop_client_get_client(client);
2384 struct weston_view *view;
2385 struct shell_surface *shsurf;
2386 struct weston_surface *surface =
2387 weston_desktop_surface_get_surface(desktop_surface);
2388
2389 view = weston_desktop_surface_create_view(desktop_surface);
2390 if (!view)
2391 return;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002392
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002393 shsurf = calloc(1, sizeof *shsurf);
2394 if (!shsurf) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002395 if (wl_client)
2396 wl_client_post_no_memory(wl_client);
2397 else
2398 weston_log("no memory to allocate shell surface\n");
2399 return;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002400 }
2401
Pekka Paalanen8274d902014-08-06 19:36:51 +03002402 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002403
Tiago Vignattibc052c92012-04-19 16:18:18 +03002404 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002405 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002406 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002407 shsurf->saved_rotation_valid = false;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002408 shsurf->desktop_surface = desktop_surface;
2409 shsurf->view = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002410 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002411 wl_list_init(&shsurf->fullscreen.transform.link);
2412
Semi Malinen99f8c082018-05-02 11:10:32 +02002413 shell_surface_set_output(
2414 shsurf, get_default_output(shsurf->shell->compositor));
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002415
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002416 wl_signal_init(&shsurf->destroy_signal);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002417
Pekka Paalanen460099f2012-01-20 16:48:25 +02002418 /* empty when not in use */
2419 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002420 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002421
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002422 wl_list_init(&shsurf->workspace_transform.link);
2423
Stefan Agnera8da2082019-06-23 14:53:59 +02002424 /*
2425 * initialize list as well as link. The latter allows to use
2426 * wl_list_remove() even when this surface is not in another list.
2427 */
2428 wl_list_init(&shsurf->children_list);
2429 wl_list_init(&shsurf->children_link);
2430
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002431 weston_desktop_surface_set_user_data(desktop_surface, shsurf);
2432 weston_desktop_surface_set_activated(desktop_surface,
2433 shsurf->focus_count > 0);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002434}
2435
Tiago Vignattibc052c92012-04-19 16:18:18 +03002436static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002437desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2438 void *shell)
2439{
2440 struct shell_surface *shsurf =
2441 weston_desktop_surface_get_user_data(desktop_surface);
Stefan Agnera8da2082019-06-23 14:53:59 +02002442 struct shell_surface *shsurf_child, *tmp;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002443 struct weston_surface *surface =
2444 weston_desktop_surface_get_surface(desktop_surface);
2445
2446 if (!shsurf)
2447 return;
2448
Stefan Agnera8da2082019-06-23 14:53:59 +02002449 wl_list_for_each_safe(shsurf_child, tmp, &shsurf->children_list, children_link) {
2450 wl_list_remove(&shsurf_child->children_link);
2451 wl_list_init(&shsurf_child->children_link);
2452 }
2453 wl_list_remove(&shsurf->children_link);
2454
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002455 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2456
2457 if (shsurf->fullscreen.black_view)
2458 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2459
2460 weston_surface_set_label_func(surface, NULL);
2461 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2462 shsurf->desktop_surface = NULL;
2463
Quentin Glidicf01ecee2016-08-16 10:52:46 +02002464 weston_desktop_surface_unlink_view(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002465 if (weston_surface_is_mapped(surface) &&
2466 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
2467 pixman_region32_fini(&surface->pending.input);
2468 pixman_region32_init(&surface->pending.input);
2469 pixman_region32_fini(&surface->input);
2470 pixman_region32_init(&surface->input);
2471 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2472 fade_out_done, shsurf);
2473 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002474 weston_view_destroy(shsurf->view);
Pekka Paalanen99628002018-05-22 12:48:35 +03002475
2476 if (shsurf->output_destroy_listener.notify) {
2477 wl_list_remove(&shsurf->output_destroy_listener.link);
2478 shsurf->output_destroy_listener.notify = NULL;
2479 }
2480
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002481 free(shsurf);
2482 }
2483}
2484
2485static void
2486set_maximized_position(struct desktop_shell *shell,
2487 struct shell_surface *shsurf)
2488{
2489 pixman_rectangle32_t area;
2490 struct weston_geometry geometry;
2491
2492 get_output_work_area(shell, shsurf->output, &area);
2493 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2494
2495 weston_view_set_position(shsurf->view,
2496 area.x - geometry.x,
2497 area.y - geometry.y);
2498}
2499
2500static void
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002501set_position_from_xwayland(struct shell_surface *shsurf)
2502{
2503 struct weston_geometry geometry;
2504 float x;
2505 float y;
2506
2507 assert(shsurf->xwayland.is_set);
2508
2509 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2510 x = shsurf->xwayland.x - geometry.x;
2511 y = shsurf->xwayland.y - geometry.y;
2512
2513 weston_view_set_position(shsurf->view, x, y);
2514
2515#ifdef WM_DEBUG
2516 weston_log("%s: XWM %d, %d; geometry %d, %d; view %f, %f\n",
2517 __func__, shsurf->xwayland.x, shsurf->xwayland.y,
2518 geometry.x, geometry.y, x, y);
2519#endif
2520}
2521
2522static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002523map(struct desktop_shell *shell, struct shell_surface *shsurf,
2524 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002525{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002526 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002527 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2528 struct weston_compositor *compositor = shell->compositor;
2529 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002530
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002531 /* initial positioning, see also configure() */
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002532 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002533 center_on_output(shsurf->view, shsurf->fullscreen_output);
2534 shell_map_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002535 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002536 set_maximized_position(shell, shsurf);
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002537 } else if (shsurf->xwayland.is_set) {
2538 set_position_from_xwayland(shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002539 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002540 weston_view_set_initial_position(shsurf->view, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002541 }
2542
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002543 /* Surface stacking order, see also activate(). */
2544 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002545
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002546 weston_view_update_transform(shsurf->view);
2547 shsurf->view->is_mapped = true;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002548 if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002549 surface->output = shsurf->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02002550 weston_view_set_output(shsurf->view, shsurf->output);
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002551 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002552
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002553 if (!shell->locked) {
2554 wl_list_for_each(seat, &compositor->seat_list, link)
2555 activate(shell, shsurf->view, seat,
2556 WESTON_ACTIVATE_FLAG_CONFIGURE);
2557 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002558
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002559 if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002560 switch (shell->win_animation_type) {
2561 case ANIMATION_FADE:
2562 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2563 break;
2564 case ANIMATION_ZOOM:
2565 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2566 break;
2567 case ANIMATION_NONE:
2568 default:
2569 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002570 }
2571 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002572}
2573
2574static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002575desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2576 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002577{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002578 struct shell_surface *shsurf =
2579 weston_desktop_surface_get_user_data(desktop_surface);
2580 struct weston_surface *surface =
2581 weston_desktop_surface_get_surface(desktop_surface);
2582 struct weston_view *view = shsurf->view;
2583 struct desktop_shell *shell = data;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002584 bool was_fullscreen;
2585 bool was_maximized;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002586
2587 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002588 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002589
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002590 was_fullscreen = shsurf->state.fullscreen;
2591 was_maximized = shsurf->state.maximized;
2592
2593 shsurf->state.fullscreen =
2594 weston_desktop_surface_get_fullscreen(desktop_surface);
2595 shsurf->state.maximized =
2596 weston_desktop_surface_get_maximized(desktop_surface);
2597
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002598 if (!weston_surface_is_mapped(surface)) {
2599 map(shell, shsurf, sx, sy);
2600 surface->is_mapped = true;
2601 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2602 ++surface->ref_count;
2603 return;
2604 }
2605
2606 if (sx == 0 && sy == 0 &&
2607 shsurf->last_width == surface->width &&
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002608 shsurf->last_height == surface->height &&
2609 was_fullscreen == shsurf->state.fullscreen &&
2610 was_maximized == shsurf->state.maximized)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002611 return;
2612
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002613 if (was_fullscreen)
2614 unset_fullscreen(shsurf);
2615 if (was_maximized)
2616 unset_maximized(shsurf);
2617
Quentin Glidic920cf042016-08-16 14:26:20 +02002618 if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2619 !shsurf->saved_position_valid) {
2620 shsurf->saved_x = shsurf->view->geometry.x;
2621 shsurf->saved_y = shsurf->view->geometry.y;
2622 shsurf->saved_position_valid = true;
2623
2624 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2625 wl_list_remove(&shsurf->rotation.transform.link);
2626 wl_list_init(&shsurf->rotation.transform.link);
2627 weston_view_geometry_dirty(shsurf->view);
2628 shsurf->saved_rotation_valid = true;
2629 }
2630 }
2631
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002632 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002633 shell_configure_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002634 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002635 set_maximized_position(shell, shsurf);
2636 surface->output = shsurf->output;
2637 } else {
2638 float from_x, from_y;
2639 float to_x, to_y;
2640 float x, y;
2641
2642 if (shsurf->resize_edges) {
2643 sx = 0;
2644 sy = 0;
2645 }
2646
2647 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2648 sx = shsurf->last_width - surface->width;
2649 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2650 sy = shsurf->last_height - surface->height;
2651
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002652 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2653 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2654 x = shsurf->view->geometry.x + to_x - from_x;
2655 y = shsurf->view->geometry.y + to_y - from_y;
2656
2657 weston_view_set_position(shsurf->view, x, y);
2658 }
2659
Emmanuel Gil Peyrotc3941792017-04-04 18:49:33 +01002660 shsurf->last_width = surface->width;
2661 shsurf->last_height = surface->height;
2662
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002663 /* XXX: would a fullscreen surface need the same handling? */
2664 if (surface->output) {
2665 wl_list_for_each(view, &surface->views, surface_link)
2666 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002667 }
2668}
2669
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002670static void
Derek Foreman927d9e22017-10-06 14:37:44 -05002671get_maximized_size(struct shell_surface *shsurf, int32_t *width, int32_t *height)
2672{
2673 struct desktop_shell *shell;
2674 pixman_rectangle32_t area;
2675
2676 shell = shell_surface_get_shell(shsurf);
2677 get_output_work_area(shell, shsurf->output, &area);
2678
2679 *width = area.width;
2680 *height = area.height;
2681}
2682
2683static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002684set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2685 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002686{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002687 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2688 struct weston_surface *surface =
2689 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2690 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002691
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002692 if (fullscreen) {
2693 /* handle clients launching in fullscreen */
2694 if (output == NULL && !weston_surface_is_mapped(surface)) {
2695 /* Set the output to the one that has focus currently. */
2696 output = get_focused_output(surface->compositor);
2697 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002698
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002699 shell_surface_set_output(shsurf, output);
2700 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002701
Marius Vlad6ebda362020-03-28 00:23:14 +02002702 if (shsurf->output) {
2703 width = shsurf->output->width;
2704 height = shsurf->output->height;
2705 }
Derek Foremane1af3d82017-10-06 14:37:45 -05002706 } else if (weston_desktop_surface_get_maximized(desktop_surface)) {
2707 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002708 }
2709 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2710 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002711}
2712
2713static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002714desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2715 struct weston_seat *seat, uint32_t serial, void *shell)
2716{
2717 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2718 struct weston_touch *touch = weston_seat_get_touch(seat);
2719 struct shell_surface *shsurf =
2720 weston_desktop_surface_get_user_data(desktop_surface);
2721 struct weston_surface *surface =
2722 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2723 struct wl_resource *resource = surface->resource;
2724 struct weston_surface *focus;
2725
2726 if (pointer &&
2727 pointer->focus &&
2728 pointer->button_count > 0 &&
2729 pointer->grab_serial == serial) {
2730 focus = weston_surface_get_main_surface(pointer->focus->surface);
2731 if ((focus == surface) &&
2732 (surface_move(shsurf, pointer, true) < 0))
2733 wl_resource_post_no_memory(resource);
2734 } else if (touch &&
2735 touch->focus &&
2736 touch->grab_serial == serial) {
2737 focus = weston_surface_get_main_surface(touch->focus->surface);
2738 if ((focus == surface) &&
2739 (surface_touch_move(shsurf, touch) < 0))
2740 wl_resource_post_no_memory(resource);
2741 }
2742}
2743
2744static void
2745desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2746 struct weston_seat *seat, uint32_t serial,
2747 enum weston_desktop_surface_edge edges, void *shell)
2748{
2749 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2750 struct shell_surface *shsurf =
2751 weston_desktop_surface_get_user_data(desktop_surface);
2752 struct weston_surface *surface =
2753 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2754 struct wl_resource *resource = surface->resource;
2755 struct weston_surface *focus;
2756
2757 if (!pointer ||
2758 pointer->button_count == 0 ||
2759 pointer->grab_serial != serial ||
2760 pointer->focus == NULL)
2761 return;
2762
2763 focus = weston_surface_get_main_surface(pointer->focus->surface);
2764 if (focus != surface)
2765 return;
2766
2767 if (surface_resize(shsurf, pointer, edges) < 0)
2768 wl_resource_post_no_memory(resource);
2769}
2770
2771static void
Stefan Agnera8da2082019-06-23 14:53:59 +02002772desktop_surface_set_parent(struct weston_desktop_surface *desktop_surface,
2773 struct weston_desktop_surface *parent,
2774 void *shell)
2775{
Marius Vlada27658e2020-01-17 12:32:55 +02002776 struct shell_surface *shsurf_parent;
Stefan Agnera8da2082019-06-23 14:53:59 +02002777 struct shell_surface *shsurf =
2778 weston_desktop_surface_get_user_data(desktop_surface);
Stefan Agnera8da2082019-06-23 14:53:59 +02002779
Marius Vlada27658e2020-01-17 12:32:55 +02002780 /* unlink any potential child */
2781 wl_list_remove(&shsurf->children_link);
2782
2783 if (parent) {
2784 shsurf_parent = weston_desktop_surface_get_user_data(parent);
2785 wl_list_insert(shsurf_parent->children_list.prev,
2786 &shsurf->children_link);
2787 } else {
2788 wl_list_init(&shsurf->children_link);
2789 }
Stefan Agnera8da2082019-06-23 14:53:59 +02002790}
2791
2792static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002793desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2794 bool fullscreen,
2795 struct weston_output *output, void *shell)
2796{
2797 struct shell_surface *shsurf =
2798 weston_desktop_surface_get_user_data(desktop_surface);
2799
2800 set_fullscreen(shsurf, fullscreen, output);
2801}
2802
2803static void
2804set_maximized(struct shell_surface *shsurf, bool maximized)
2805{
2806 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2807 struct weston_surface *surface =
2808 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2809 int32_t width = 0, height = 0;
2810
2811 if (maximized) {
2812 struct weston_output *output;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002813
2814 if (!weston_surface_is_mapped(surface))
2815 output = get_focused_output(surface->compositor);
2816 else
2817 output = surface->output;
2818
2819 shell_surface_set_output(shsurf, output);
2820
Derek Foreman927d9e22017-10-06 14:37:44 -05002821 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002822 }
2823 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2824 weston_desktop_surface_set_size(desktop_surface, width, height);
2825}
2826
2827static void
2828desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2829 bool maximized, void *shell)
2830{
2831 struct shell_surface *shsurf =
2832 weston_desktop_surface_get_user_data(desktop_surface);
2833
2834 set_maximized(shsurf, maximized);
2835}
2836
2837static void
2838desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2839 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002840{
2841 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002842 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002843
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002844 /* apply compositor's own minimization logic (hide) */
2845 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002846}
2847
Rafael Antognollie2a34552013-12-03 15:35:45 -02002848static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002849set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002850{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002851 struct shell_grab *grab;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002852
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002853 if (pointer->grab->interface == &busy_cursor_grab_interface)
2854 return;
2855
2856 grab = malloc(sizeof *grab);
2857 if (!grab)
2858 return;
2859
2860 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2861 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2862 /* Mark the shsurf as ungrabbed so that button binding is able
2863 * to move it. */
2864 shsurf->grabbed = 0;
2865}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002866
2867static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002868end_busy_cursor(struct weston_compositor *compositor,
2869 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002870{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002871 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002872 struct shell_grab *grab;
2873 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002874
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002875 wl_list_for_each(seat, &compositor->seat_list, link) {
2876 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2877 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002878
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002879 if (!pointer)
2880 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002881
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002882 if (pointer->grab->interface != &busy_cursor_grab_interface)
2883 continue;
2884
2885 grab = (struct shell_grab *) pointer->grab;
2886 shsurf = grab->shsurf;
2887 if (!shsurf)
2888 continue;
2889
2890 grab_client =
2891 weston_desktop_surface_get_client(shsurf->desktop_surface);
2892 if (grab_client == desktop_client) {
2893 shell_grab_end(grab);
2894 free(grab);
2895 }
2896 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002897}
2898
2899static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002900desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2901 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002902{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002903 struct shell_surface *shsurf =
2904 weston_desktop_surface_get_user_data(desktop_surface);
2905 bool *unresponsive = user_data;
2906
2907 shsurf->unresponsive = *unresponsive;
2908}
2909
2910static void
2911desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2912 void *shell_)
2913{
2914 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002915 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002916 struct weston_seat *seat;
2917 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002918
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002919 weston_desktop_client_for_each_surface(desktop_client,
2920 desktop_surface_set_unresponsive,
2921 &unresponsive);
2922
2923
2924 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2925 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2926 struct weston_desktop_client *grab_client;
2927
2928 if (!pointer || !pointer->focus)
2929 continue;
2930
2931 shsurf = get_shell_surface(pointer->focus->surface);
2932 if (!shsurf)
2933 continue;
2934
2935 grab_client =
2936 weston_desktop_surface_get_client(shsurf->desktop_surface);
2937 if (grab_client == desktop_client)
2938 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002939 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002940}
2941
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002942static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002943desktop_surface_pong(struct weston_desktop_client *desktop_client,
2944 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002945{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002946 struct desktop_shell *shell = shell_;
2947 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002948
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002949 weston_desktop_client_for_each_surface(desktop_client,
2950 desktop_surface_set_unresponsive,
2951 &unresponsive);
2952 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002953}
2954
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002955static void
2956desktop_surface_set_xwayland_position(struct weston_desktop_surface *surface,
2957 int32_t x, int32_t y, void *shell_)
2958{
2959 struct shell_surface *shsurf =
2960 weston_desktop_surface_get_user_data(surface);
2961
2962 shsurf->xwayland.x = x;
2963 shsurf->xwayland.y = y;
2964 shsurf->xwayland.is_set = true;
2965}
2966
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002967static const struct weston_desktop_api shell_desktop_api = {
2968 .struct_size = sizeof(struct weston_desktop_api),
2969 .surface_added = desktop_surface_added,
2970 .surface_removed = desktop_surface_removed,
2971 .committed = desktop_surface_committed,
2972 .move = desktop_surface_move,
2973 .resize = desktop_surface_resize,
Stefan Agnera8da2082019-06-23 14:53:59 +02002974 .set_parent = desktop_surface_set_parent,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002975 .fullscreen_requested = desktop_surface_fullscreen_requested,
2976 .maximized_requested = desktop_surface_maximized_requested,
2977 .minimized_requested = desktop_surface_minimized_requested,
2978 .ping_timeout = desktop_surface_ping_timeout,
2979 .pong = desktop_surface_pong,
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002980 .set_xwayland_position = desktop_surface_set_xwayland_position,
Rafael Antognollie2a34552013-12-03 15:35:45 -02002981};
2982
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002983/* ************************ *
2984 * end of libweston-desktop *
2985 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04002986static void
Quentin Glidice8bf9592016-06-23 18:55:20 +02002987configure_static_view(struct weston_view *ev, struct weston_layer *layer, int x, int y)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002988{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002989 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002990
Semi Malinene7a52fb2018-04-26 11:08:10 +02002991 if (!ev->output)
2992 return;
2993
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002994 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002995 if (v->output == ev->output && v != ev) {
2996 weston_view_unmap(v);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002997 v->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002998 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002999 }
3000 }
3001
Quentin Glidice8bf9592016-06-23 18:55:20 +02003002 weston_view_set_position(ev, ev->output->x + x, ev->output->y + y);
Armin Krezović4663aca2016-06-30 06:04:29 +02003003 ev->surface->is_mapped = true;
3004 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003005
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003006 if (wl_list_empty(&ev->layer_link.link)) {
3007 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003008 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003009 }
3010}
3011
David Fort50d962f2016-06-09 17:55:52 +02003012
3013static struct shell_output *
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003014find_shell_output_from_weston_output(struct desktop_shell *shell,
3015 struct weston_output *output)
David Fort50d962f2016-06-09 17:55:52 +02003016{
3017 struct shell_output *shell_output;
3018
3019 wl_list_for_each(shell_output, &shell->output_list, link) {
3020 if (shell_output->output == output)
3021 return shell_output;
3022 }
3023
3024 return NULL;
3025}
3026
Pekka Paalanen8274d902014-08-06 19:36:51 +03003027static int
3028background_get_label(struct weston_surface *surface, char *buf, size_t len)
3029{
3030 return snprintf(buf, len, "background for output %s",
Miguel A. Vico620f68d2019-09-25 11:23:13 -07003031 (surface->output ? surface->output->name : "NULL"));
Pekka Paalanen8274d902014-08-06 19:36:51 +03003032}
3033
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003034static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003035background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003036{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003037 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003038 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003039
Jason Ekstranda7af7042013-10-12 22:38:11 -05003040 view = container_of(es->views.next, struct weston_view, surface_link);
3041
Quentin Glidice8bf9592016-06-23 18:55:20 +02003042 configure_static_view(view, &shell->background_layer, 0, 0);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003043}
3044
3045static void
David Fort50d962f2016-06-09 17:55:52 +02003046handle_background_surface_destroy(struct wl_listener *listener, void *data)
3047{
3048 struct shell_output *output =
3049 container_of(listener, struct shell_output, background_surface_listener);
3050
3051 weston_log("background surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09003052 wl_list_remove(&output->background_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02003053 output->background_surface = NULL;
3054}
3055
3056static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003057desktop_shell_set_background(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);
David Fort50d962f2016-06-09 17:55:52 +02003065 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003066 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003067
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003068 if (surface->committed) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -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 = background_committed;
3080 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003081 weston_surface_set_label_func(surface, background_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 Paalanenff5e88d2017-12-07 11:44:18 +02003086 if (sh_output->background_surface) {
3087 /* The output already has a background, 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 Paalanenff5e88d2017-12-07 11:44:18 +02003098 sh_output->background_surface = surface;
3099
3100 sh_output->background_surface_listener.notify =
3101 handle_background_surface_destroy;
3102 wl_signal_add(&surface->destroy_signal,
3103 &sh_output->background_surface_listener);
3104 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003105}
3106
Pekka Paalanen8274d902014-08-06 19:36:51 +03003107static int
3108panel_get_label(struct weston_surface *surface, char *buf, size_t len)
3109{
3110 return snprintf(buf, len, "panel for output %s",
Miguel A. Vico620f68d2019-09-25 11:23:13 -07003111 (surface->output ? surface->output->name : "NULL"));
Pekka Paalanen8274d902014-08-06 19:36:51 +03003112}
3113
Kristian Høgsberg75840622011-09-06 13:48:16 -04003114static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003115panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003116{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003117 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003118 struct weston_view *view;
Quentin Glidice8bf9592016-06-23 18:55:20 +02003119 int width, height;
3120 int x = 0, y = 0;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003121
Jason Ekstranda7af7042013-10-12 22:38:11 -05003122 view = container_of(es->views.next, struct weston_view, surface_link);
3123
Quentin Glidice8bf9592016-06-23 18:55:20 +02003124 get_panel_size(shell, view, &width, &height);
3125 switch (shell->panel_position) {
3126 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
3127 break;
3128 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
3129 y = view->output->height - height;
3130 break;
3131 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
3132 break;
3133 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
3134 x = view->output->width - width;
3135 break;
3136 }
3137
3138 configure_static_view(view, &shell->panel_layer, x, y);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003139}
3140
3141static void
David Fort50d962f2016-06-09 17:55:52 +02003142handle_panel_surface_destroy(struct wl_listener *listener, void *data)
3143{
3144 struct shell_output *output =
3145 container_of(listener, struct shell_output, panel_surface_listener);
3146
3147 weston_log("panel surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09003148 wl_list_remove(&output->panel_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02003149 output->panel_surface = NULL;
3150}
3151
3152
3153static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003154desktop_shell_set_panel(struct wl_client *client,
3155 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003156 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003157 struct wl_resource *surface_resource)
3158{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003159 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003160 struct weston_surface *surface =
3161 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003162 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02003163 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003164
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003165 if (surface->committed) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003166 wl_resource_post_error(surface_resource,
3167 WL_DISPLAY_ERROR_INVALID_OBJECT,
3168 "surface role already assigned");
3169 return;
3170 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003171
Jason Ekstranda7af7042013-10-12 22:38:11 -05003172 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3173 weston_view_destroy(view);
3174 view = weston_view_create(surface);
3175
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003176 surface->committed = panel_committed;
3177 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003178 weston_surface_set_label_func(surface, panel_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03003179 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02003180 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02003181
3182 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003183 if (sh_output->panel_surface) {
3184 /* The output already has a panel, tell our helper
3185 * there is no need for another one. */
3186 weston_desktop_shell_send_configure(resource, 0,
3187 surface_resource,
3188 0, 0);
3189 } else {
3190 weston_desktop_shell_send_configure(resource, 0,
3191 surface_resource,
3192 surface->output->width,
3193 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003194
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003195 sh_output->panel_surface = surface;
3196
3197 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
3198 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
3199 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003200}
3201
Pekka Paalanen8274d902014-08-06 19:36:51 +03003202static int
3203lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
3204{
3205 return snprintf(buf, len, "lock window");
3206}
3207
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003208static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003209lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003210{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003211 struct desktop_shell *shell = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003212 struct weston_view *view;
3213
3214 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003215
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003216 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003217 return;
3218
Jason Ekstranda7af7042013-10-12 22:38:11 -05003219 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003220
3221 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003222 weston_layer_entry_insert(&shell->lock_layer.view_list,
3223 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003224 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003225 surface->is_mapped = true;
3226 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003227 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003228 }
3229}
3230
3231static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003232handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003233{
Tiago Vignattibe143262012-04-16 17:31:41 +03003234 struct desktop_shell *shell =
3235 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003236
Martin Minarik6d118362012-06-07 18:01:59 +02003237 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003238 shell->lock_surface = NULL;
3239}
3240
3241static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003242desktop_shell_set_lock_surface(struct wl_client *client,
3243 struct wl_resource *resource,
3244 struct wl_resource *surface_resource)
3245{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003246 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003247 struct weston_surface *surface =
3248 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003249
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003250 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003251
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003252 if (!shell->locked)
3253 return;
3254
Pekka Paalanen98262232011-12-01 10:42:22 +02003255 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003256
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003257 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003258 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003259 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003260
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003261 weston_view_create(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003262 surface->committed = lock_surface_committed;
3263 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003264 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003265}
3266
3267static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003268resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003269{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003270 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003271
Quentin Glidic82681572016-12-17 13:40:51 +01003272 weston_layer_unset_position(&shell->lock_layer);
3273
3274 if (shell->showing_input_panels)
3275 weston_layer_set_position(&shell->input_panel_layer,
3276 WESTON_LAYER_POSITION_TOP_UI);
3277 weston_layer_set_position(&shell->fullscreen_layer,
3278 WESTON_LAYER_POSITION_FULLSCREEN);
3279 weston_layer_set_position(&shell->panel_layer,
3280 WESTON_LAYER_POSITION_UI);
3281 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003282
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003283 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003284
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003285 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003286 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003287 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003288}
3289
3290static void
3291desktop_shell_unlock(struct wl_client *client,
3292 struct wl_resource *resource)
3293{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003294 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003295
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003296 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003297
3298 if (shell->locked)
3299 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003300}
3301
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003302static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003303desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003304 struct wl_resource *resource,
3305 struct wl_resource *surface_resource)
3306{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003307 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003308
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003309 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003310 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003311}
3312
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003313static void
3314desktop_shell_desktop_ready(struct wl_client *client,
3315 struct wl_resource *resource)
3316{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003317 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003318
3319 shell_fade_startup(shell);
3320}
3321
Jonny Lamb765760d2014-08-20 15:53:19 +02003322static void
3323desktop_shell_set_panel_position(struct wl_client *client,
3324 struct wl_resource *resource,
3325 uint32_t position)
3326{
3327 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3328
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003329 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3330 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3331 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3332 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02003333 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003334 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02003335 "bad position argument");
3336 return;
3337 }
3338
3339 shell->panel_position = position;
3340}
3341
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003342static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04003343 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003344 desktop_shell_set_panel,
3345 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003346 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003347 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02003348 desktop_shell_desktop_ready,
3349 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04003350};
3351
3352static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003353move_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003354 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003355{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003356 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003357 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003358 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003359
Derek Foreman8ae2db52015-07-15 13:00:36 -05003360 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003361 return;
3362
Derek Foreman8ae2db52015-07-15 13:00:36 -05003363 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003364
Pekka Paalanen01388e22013-04-25 13:57:44 +03003365 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003366 if (surface == NULL)
3367 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003368
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003369 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003370 if (shsurf == NULL ||
3371 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3372 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003373 return;
3374
Derek Foreman794fa0e2015-07-15 13:00:38 -05003375 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003376}
3377
3378static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003379maximize_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003380 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003381{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003382 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003383 struct weston_surface *surface;
3384 struct shell_surface *shsurf;
3385
3386 surface = weston_surface_get_main_surface(focus);
3387 if (surface == NULL)
3388 return;
3389
3390 shsurf = get_shell_surface(surface);
3391 if (shsurf == NULL)
3392 return;
3393
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003394 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003395}
3396
3397static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003398fullscreen_binding(struct weston_keyboard *keyboard,
3399 const struct timespec *time, uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003400{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003401 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003402 struct weston_surface *surface;
3403 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003404 bool fullscreen;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003405
3406 surface = weston_surface_get_main_surface(focus);
3407 if (surface == NULL)
3408 return;
3409
3410 shsurf = get_shell_surface(surface);
3411 if (shsurf == NULL)
3412 return;
3413
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003414 fullscreen =
3415 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003416
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003417 set_fullscreen(shsurf, !fullscreen, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003418}
3419
3420static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003421touch_move_binding(struct weston_touch *touch, const struct timespec *time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01003422{
Derek Foreman362656b2014-09-04 10:23:05 -05003423 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01003424 struct weston_surface *surface;
3425 struct shell_surface *shsurf;
3426
Derek Foreman8ae2db52015-07-15 13:00:36 -05003427 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05003428 return;
3429
Derek Foreman8ae2db52015-07-15 13:00:36 -05003430 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003431 surface = weston_surface_get_main_surface(focus);
3432 if (surface == NULL)
3433 return;
3434
3435 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003436 if (shsurf == NULL ||
3437 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3438 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Neil Robertsaba0f252013-10-03 16:43:05 +01003439 return;
3440
Derek Foremanb7674ae2015-07-15 13:00:37 -05003441 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01003442}
3443
3444static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003445resize_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003446 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003447{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003448 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003449 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003450 uint32_t edges = 0;
3451 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003452 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003453
Derek Foreman8ae2db52015-07-15 13:00:36 -05003454 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003455 return;
3456
Derek Foreman8ae2db52015-07-15 13:00:36 -05003457 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003458
Pekka Paalanen01388e22013-04-25 13:57:44 +03003459 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003460 if (surface == NULL)
3461 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003462
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003463 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003464 if (shsurf == NULL ||
3465 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3466 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003467 return;
3468
Jason Ekstranda7af7042013-10-12 22:38:11 -05003469 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003470 wl_fixed_to_int(pointer->grab_x),
3471 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05003472 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003473
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003474 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003475 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003476 else if (x < 2 * surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003477 edges |= 0;
3478 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003479 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003480
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003481 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003482 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003483 else if (y < 2 * surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003484 edges |= 0;
3485 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003486 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003487
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003488 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003489}
3490
3491static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003492surface_opacity_binding(struct weston_pointer *pointer,
3493 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003494 struct weston_pointer_axis_event *event,
3495 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003496{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003497 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003498 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003499 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003500 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003501
Pekka Paalanen01388e22013-04-25 13:57:44 +03003502 /* XXX: broken for windows containing sub-surfaces */
3503 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003504 if (surface == NULL)
3505 return;
3506
3507 shsurf = get_shell_surface(surface);
3508 if (!shsurf)
3509 return;
3510
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003511 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003512
Jason Ekstranda7af7042013-10-12 22:38:11 -05003513 if (shsurf->view->alpha > 1.0)
3514 shsurf->view->alpha = 1.0;
3515 if (shsurf->view->alpha < step)
3516 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003517
Jason Ekstranda7af7042013-10-12 22:38:11 -05003518 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003519 weston_surface_damage(surface);
3520}
3521
3522static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003523do_zoom(struct weston_seat *seat, const struct timespec *time, uint32_t key,
3524 uint32_t axis, double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003525{
Derek Foreman8aeeac82015-01-30 13:24:36 -06003526 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05003527 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003528 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003529 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003530
Derek Foreman1281a362015-07-31 16:55:32 -05003531 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06003532 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3533 return;
3534 }
3535
Scott Moreauccbf29d2012-02-22 14:21:41 -07003536 wl_list_for_each(output, &compositor->output_list, link) {
3537 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05003538 wl_fixed_to_double(pointer->x),
3539 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003540 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003541 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003542 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003543 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003544 increment = -output->zoom.increment;
3545 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003546 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003547 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003548 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003549 else
3550 increment = 0;
3551
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003552 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003553
Scott Moreaue6603982012-06-11 13:07:51 -06003554 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003555 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003556 else if (output->zoom.level > output->zoom.max_level)
3557 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05003558
3559 if (!output->zoom.active) {
3560 if (output->zoom.level <= 0.0)
3561 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05003562 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003563 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003564
Scott Moreaue6603982012-06-11 13:07:51 -06003565 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003566
Jason Ekstranda7af7042013-10-12 22:38:11 -05003567 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003568 }
3569 }
3570}
3571
Scott Moreauccbf29d2012-02-22 14:21:41 -07003572static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003573zoom_axis_binding(struct weston_pointer *pointer, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003574 struct weston_pointer_axis_event *event,
3575 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003576{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003577 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003578}
3579
3580static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003581zoom_key_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003582 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003583{
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003584 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003585}
3586
3587static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003588terminate_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003589 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003590{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003591 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003592
Pekka Paalanen052032d2019-02-06 10:44:37 +02003593 weston_compositor_exit(compositor);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003594}
3595
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003596static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003597rotate_grab_motion(struct weston_pointer_grab *grab,
3598 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003599 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003600{
3601 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003602 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003603 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003604 struct shell_surface *shsurf = rotate->base.shsurf;
Sergey Bugaev14ef2012019-02-11 22:55:09 +03003605 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003606 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003607
Jonas Ådahld2510102014-10-05 21:39:14 +02003608 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003609
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003610 if (!shsurf)
3611 return;
3612
Sergey Bugaev14ef2012019-02-11 22:55:09 +03003613 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
3614
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003615 cx = 0.5f * surface->width;
3616 cy = 0.5f * surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003617
Daniel Stone37816df2012-05-16 18:45:18 +01003618 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3619 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003620 r = sqrtf(dx * dx + dy * dy);
3621
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003622 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003623 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003624
3625 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003626 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003627 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003628
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003629 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003630 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003631
3632 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003633 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003634 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003635 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003636 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003637
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003638 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003639 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003640 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003641 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003642 wl_list_init(&shsurf->rotation.transform.link);
3643 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003644 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003645 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003646
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003647 /* We need to adjust the position of the surface
3648 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003649 cposx = shsurf->view->geometry.x + cx;
3650 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003651 dposx = rotate->center.x - cposx;
3652 dposy = rotate->center.y - cposy;
3653 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003654 weston_view_set_position(shsurf->view,
3655 shsurf->view->geometry.x + dposx,
3656 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003657 }
3658
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003659 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003660 * lazily applies the damage due to rotation update.
3661 */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003662 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003663}
3664
3665static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003666rotate_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003667 const struct timespec *time,
3668 uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003669{
3670 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003671 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003672 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003673 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003674 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003675
Daniel Stone4dbadb12012-05-30 16:31:51 +01003676 if (pointer->button_count == 0 &&
3677 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003678 if (shsurf)
3679 weston_matrix_multiply(&shsurf->rotation.rotation,
3680 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003681 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003682 free(rotate);
3683 }
3684}
3685
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003686static void
3687rotate_grab_cancel(struct weston_pointer_grab *grab)
3688{
3689 struct rotate_grab *rotate =
3690 container_of(grab, struct rotate_grab, base.grab);
3691
3692 shell_grab_end(&rotate->base);
3693 free(rotate);
3694}
3695
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003696static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003697 noop_grab_focus,
3698 rotate_grab_motion,
3699 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003700 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003701 noop_grab_axis_source,
3702 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003703 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003704};
3705
3706static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003707surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003708{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003709 struct weston_surface *surface =
3710 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003711 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003712 float dx, dy;
3713 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003714
Pekka Paalanen460099f2012-01-20 16:48:25 +02003715 rotate = malloc(sizeof *rotate);
3716 if (!rotate)
3717 return;
3718
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003719 weston_view_to_global_float(shsurf->view,
3720 surface->width * 0.5f,
3721 surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003722 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003723
Derek Foreman74de4692015-07-15 13:00:39 -05003724 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3725 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003726 r = sqrtf(dx * dx + dy * dy);
3727 if (r > 20.0f) {
3728 struct weston_matrix inverse;
3729
3730 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003731 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003732 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003733
3734 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003735 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003736 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003737 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003738 weston_matrix_init(&rotate->rotation);
3739 }
3740
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003741 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003742 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003743}
3744
3745static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003746rotate_binding(struct weston_pointer *pointer, const struct timespec *time,
3747 uint32_t button, void *data)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003748{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003749 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003750 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003751 struct shell_surface *surface;
3752
Derek Foreman8ae2db52015-07-15 13:00:36 -05003753 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003754 return;
3755
Derek Foreman8ae2db52015-07-15 13:00:36 -05003756 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003757
Pekka Paalanen01388e22013-04-25 13:57:44 +03003758 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003759 if (base_surface == NULL)
3760 return;
3761
3762 surface = get_shell_surface(base_surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003763 if (surface == NULL ||
3764 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3765 weston_desktop_surface_get_maximized(surface->desktop_surface))
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003766 return;
3767
Derek Foreman74de4692015-07-15 13:00:39 -05003768 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003769}
3770
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003771/* Move all fullscreen layers down to the current workspace and hide their
3772 * black views. The surfaces' state is set to both fullscreen and lowered,
3773 * and this is reversed when such a surface is re-configured, see
3774 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3775 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003776 * lowering_output = NULL - Lower on all outputs, else only lower on the
3777 * specified output.
3778 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003779 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003780 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003781void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003782lower_fullscreen_layer(struct desktop_shell *shell,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003783 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003784{
3785 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003786 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003787
3788 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003789 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003790 &shell->fullscreen_layer.view_list.link,
3791 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003792 struct shell_surface *shsurf = get_shell_surface(view->surface);
3793
3794 if (!shsurf)
3795 continue;
3796
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003797 /* Only lower surfaces which have lowering_output as their fullscreen
3798 * output, unless a NULL output asks for lowering on all outputs.
3799 */
3800 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3801 continue;
3802
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003803 /* We can have a non-fullscreen popup for a fullscreen surface
3804 * in the fullscreen layer. */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003805 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003806 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003807 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3808 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07003809 weston_view_damage_below(shsurf->fullscreen.black_view);
3810
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003811 }
3812
3813 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003814 weston_layer_entry_remove(&view->layer_link);
3815 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003816 weston_view_damage_below(view);
3817 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003818
3819 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003820 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003821}
3822
Stefan Agnera8da2082019-06-23 14:53:59 +02003823static struct shell_surface *get_last_child(struct shell_surface *shsurf)
3824{
3825 struct shell_surface *shsurf_child;
3826
3827 wl_list_for_each_reverse(shsurf_child, &shsurf->children_list, children_link) {
3828 if (weston_view_is_mapped(shsurf_child->view))
3829 return shsurf_child;
3830 }
3831
3832 return NULL;
3833}
3834
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003835void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003836activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003837 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003838{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003839 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003840 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003841 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003842 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003843 struct weston_surface *old_es;
Stefan Agnera8da2082019-06-23 14:53:59 +02003844 struct shell_surface *shsurf, *shsurf_child;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003845
Pekka Paalanen01388e22013-04-25 13:57:44 +03003846 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003847 shsurf = get_shell_surface(main_surface);
3848 assert(shsurf);
3849
Stefan Agnera8da2082019-06-23 14:53:59 +02003850 shsurf_child = get_last_child(shsurf);
3851 if (shsurf_child) {
3852 /* Activate last xdg child instead of parent. */
3853 activate(shell, shsurf_child->view, seat, flags);
3854 return;
3855 }
3856
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003857 /* Only demote fullscreen surfaces on the output of activated shsurf.
3858 * Leave fullscreen surfaces on unrelated outputs alone. */
Pekka Paalanen87860c22018-05-02 10:21:57 +02003859 if (shsurf->output)
3860 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003861
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003862 weston_view_activate(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003863
Jonas Ådahl8538b222012-08-29 22:13:03 +02003864 state = ensure_focus_state(shell, seat);
3865 if (state == NULL)
3866 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003867
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003868 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08003869 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003870
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003871 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3872 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02003873 shell_configure_fullscreen(shsurf);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003874
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01003875 /* Update the surface’s layer. This brings it to the top of the stacking
3876 * order as appropriate. */
3877 shell_surface_update_layer(shsurf);
3878
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003879 if (shell->focus_animation_type != ANIMATION_NONE) {
3880 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003881 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003882 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003883}
3884
Alex Wu21858432012-04-01 20:13:08 +08003885/* no-op func for checking black surface */
3886static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003887black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08003888{
3889}
3890
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003891static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003892is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08003893{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003894 struct weston_surface *surface = view->surface;
3895
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003896 if (surface->committed == black_surface_committed) {
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003897 if (fs_view)
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003898 *fs_view = surface->committed_private;
Alex Wu21858432012-04-01 20:13:08 +08003899 return true;
3900 }
3901 return false;
3902}
3903
Kristian Høgsberg75840622011-09-06 13:48:16 -04003904static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003905activate_binding(struct weston_seat *seat,
3906 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003907 struct weston_view *focus_view,
3908 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003909{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003910 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003911 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003912
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003913 if (!focus_view)
3914 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003915
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003916 if (is_black_surface_view(focus_view, &main_view))
3917 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08003918
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003919 main_surface = weston_surface_get_main_surface(focus_view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003920 if (!get_shell_surface(main_surface))
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003921 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003922
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003923 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01003924}
3925
3926static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003927click_to_activate_binding(struct weston_pointer *pointer,
3928 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003929 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003930{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003931 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003932 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003933 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08003934 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003935
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003936 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003937 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003938 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01003939}
3940
3941static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003942touch_to_activate_binding(struct weston_touch *touch,
3943 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003944 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003945{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003946 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003947 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003948 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08003949 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003950
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003951 activate_binding(touch->seat, data, touch->focus,
3952 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003953}
3954
3955static void
Neil Robertsb4a91702014-04-09 16:33:32 +01003956unfocus_all_seats(struct desktop_shell *shell)
3957{
3958 struct weston_seat *seat, *next;
3959
3960 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003961 struct weston_keyboard *keyboard =
3962 weston_seat_get_keyboard(seat);
3963
3964 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01003965 continue;
3966
Derek Foreman1281a362015-07-31 16:55:32 -05003967 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01003968 }
3969}
3970
3971static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003972lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003973{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003974 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003975
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003976 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003977 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003978 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003979 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003980
3981 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003982
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003983 /* Hide all surfaces by removing the fullscreen, panel and
3984 * toplevel layers. This way nothing else can show or receive
3985 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003986
Quentin Glidic82681572016-12-17 13:40:51 +01003987 weston_layer_unset_position(&shell->panel_layer);
3988 weston_layer_unset_position(&shell->fullscreen_layer);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003989 if (shell->showing_input_panels)
Quentin Glidic82681572016-12-17 13:40:51 +01003990 weston_layer_unset_position(&shell->input_panel_layer);
3991 weston_layer_unset_position(&ws->layer);
3992
3993 weston_layer_set_position(&shell->lock_layer,
3994 WESTON_LAYER_POSITION_LOCK);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003995
Derek Foreman004b4a12015-07-20 16:28:13 -05003996 weston_compositor_sleep(shell->compositor);
3997
Neil Robertsb4a91702014-04-09 16:33:32 +01003998 /* Remove the keyboard focus on all seats. This will be
3999 * restored to the workspace's saved state via
4000 * restore_focus_state when the compositor is unlocked */
4001 unfocus_all_seats(shell);
4002
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004003 /* TODO: disable bindings that should not work while locked. */
4004
4005 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004006}
4007
4008static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004009unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004010{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004011 struct wl_resource *shell_resource;
4012
Pekka Paalanend81c2162011-11-16 13:47:34 +02004013 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004014 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004015 return;
4016 }
4017
4018 /* If desktop-shell client has gone away, unlock immediately. */
4019 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004020 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004021 return;
4022 }
4023
4024 if (shell->prepare_event_sent)
4025 return;
4026
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004027 shell_resource = shell->child.desktop_shell;
4028 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004029 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004030}
4031
4032static void
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004033shell_fade_done_for_output(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004034{
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004035 struct shell_output *shell_output = data;
4036 struct desktop_shell *shell = shell_output->shell;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004037
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004038 shell_output->fade.animation = NULL;
4039 switch (shell_output->fade.type) {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004040 case FADE_IN:
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004041 weston_surface_destroy(shell_output->fade.view->surface);
4042 shell_output->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004043 break;
4044 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004045 lock(shell);
4046 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004047 default:
4048 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004049 }
4050}
4051
Jason Ekstranda7af7042013-10-12 22:38:11 -05004052static struct weston_view *
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004053shell_fade_create_surface_for_output(struct desktop_shell *shell, struct shell_output *shell_output)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004054{
4055 struct weston_compositor *compositor = shell->compositor;
4056 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004057 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004058
4059 surface = weston_surface_create(compositor);
4060 if (!surface)
4061 return NULL;
4062
Jason Ekstranda7af7042013-10-12 22:38:11 -05004063 view = weston_view_create(surface);
4064 if (!view) {
4065 weston_surface_destroy(surface);
4066 return NULL;
4067 }
4068
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004069 weston_surface_set_size(surface, shell_output->output->width, shell_output->output->height);
4070 weston_view_set_position(view, shell_output->output->x, shell_output->output->y);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004071 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004072 weston_layer_entry_insert(&compositor->fade_layer.view_list,
4073 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004074 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02004075 surface->is_mapped = true;
4076 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004077
Jason Ekstranda7af7042013-10-12 22:38:11 -05004078 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004079}
4080
4081static void
4082shell_fade(struct desktop_shell *shell, enum fade_type type)
4083{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004084 float tint;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004085 struct shell_output *shell_output;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004086
4087 switch (type) {
4088 case FADE_IN:
4089 tint = 0.0;
4090 break;
4091 case FADE_OUT:
4092 tint = 1.0;
4093 break;
4094 default:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07004095 weston_log("shell: invalid fade type\n");
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004096 return;
4097 }
4098
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004099 /* Create a separate fade surface for each output */
4100 wl_list_for_each(shell_output, &shell->output_list, link) {
4101 shell_output->fade.type = type;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004102
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004103 if (shell_output->fade.view == NULL) {
4104 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4105 if (!shell_output->fade.view)
4106 continue;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004107
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004108 shell_output->fade.view->alpha = 1.0 - tint;
4109 weston_view_update_transform(shell_output->fade.view);
4110 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004111
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004112 if (shell_output->fade.view->output == NULL) {
4113 /* If the black view gets a NULL output, we lost the
4114 * last output and we'll just cancel the fade. This
4115 * happens when you close the last window under the
4116 * X11 or Wayland backends. */
4117 shell->locked = false;
4118 weston_surface_destroy(shell_output->fade.view->surface);
4119 shell_output->fade.view = NULL;
4120 } else if (shell_output->fade.animation) {
4121 weston_fade_update(shell_output->fade.animation, tint);
4122 } else {
4123 shell_output->fade.animation =
4124 weston_fade_run(shell_output->fade.view,
4125 1.0 - tint, tint, 300.0,
4126 shell_fade_done_for_output, shell_output);
4127 }
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004128 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004129}
4130
4131static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004132do_shell_fade_startup(void *data)
4133{
4134 struct desktop_shell *shell = data;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004135 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004136
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004137 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004138 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004139 } else {
4140 weston_log("desktop shell: "
4141 "unexpected fade-in animation type %d\n",
4142 shell->startup_animation_type);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004143 wl_list_for_each(shell_output, &shell->output_list, link) {
4144 weston_surface_destroy(shell_output->fade.view->surface);
4145 shell_output->fade.view = NULL;
4146 }
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004147 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004148}
4149
4150static void
4151shell_fade_startup(struct desktop_shell *shell)
4152{
4153 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004154 struct shell_output *shell_output;
4155 bool has_fade = false;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004156
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004157 wl_list_for_each(shell_output, &shell->output_list, link) {
4158 if (!shell_output->fade.startup_timer)
4159 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004160
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004161 wl_event_source_remove(shell_output->fade.startup_timer);
4162 shell_output->fade.startup_timer = NULL;
4163 has_fade = true;
4164 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004165
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004166 if (has_fade) {
4167 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4168 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4169 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004170}
4171
4172static int
4173fade_startup_timeout(void *data)
4174{
4175 struct desktop_shell *shell = data;
4176
4177 shell_fade_startup(shell);
4178 return 0;
4179}
4180
4181static void
4182shell_fade_init(struct desktop_shell *shell)
4183{
4184 /* Make compositor output all black, and wait for the desktop-shell
4185 * client to signal it is ready, then fade in. The timer triggers a
4186 * fade-in, in case the desktop-shell client takes too long.
4187 */
4188
4189 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004190 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004191
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004192 if (shell->startup_animation_type == ANIMATION_NONE)
4193 return;
4194
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004195 wl_list_for_each(shell_output, &shell->output_list, link) {
4196 if (shell_output->fade.view != NULL) {
4197 weston_log("%s: warning: fade surface already exists\n",
4198 __func__);
4199 continue;
4200 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004201
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004202 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4203 if (!shell_output->fade.view)
4204 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004205
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004206 weston_view_update_transform(shell_output->fade.view);
4207 weston_surface_damage(shell_output->fade.view->surface);
4208
4209 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4210 shell_output->fade.startup_timer =
4211 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4212 wl_event_source_timer_update(shell_output->fade.startup_timer, 15000);
4213 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004214}
4215
4216static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004217idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004218{
4219 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004220 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004221
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004222 struct weston_seat *seat;
4223
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004224 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4225 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004226
4227 shell_fade(shell, FADE_OUT);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004228 /* lock() is called from shell_fade_done_for_output() */
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004229}
4230
4231static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004232wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004233{
4234 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004235 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004236
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004237 unlock(shell);
4238}
4239
4240static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004241transform_handler(struct wl_listener *listener, void *data)
4242{
4243 struct weston_surface *surface = data;
4244 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004245 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004246 int x, y;
4247
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004248 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004249 return;
4250
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004251 api = shsurf->shell->xwayland_surface_api;
4252 if (!api) {
4253 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
4254 shsurf->shell->xwayland_surface_api = api;
4255 }
4256
4257 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004258 return;
4259
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004260 if (!weston_view_is_mapped(shsurf->view))
4261 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004262
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004263 x = shsurf->view->geometry.x;
4264 y = shsurf->view->geometry.y;
4265
4266 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004267}
4268
4269static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004270center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004271{
Giulio Camuffob8366642013-04-25 13:57:46 +03004272 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004273 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004274
Pekka Paalanen30aa5972018-05-02 10:21:56 +02004275 if (!output) {
4276 weston_view_set_position(view, 0, 0);
4277 return;
4278 }
4279
Jason Ekstranda7af7042013-10-12 22:38:11 -05004280 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004281
4282 x = output->x + (output->width - width) / 2 - surf_x / 2;
4283 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004284
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004285 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004286}
4287
4288static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004289weston_view_set_initial_position(struct weston_view *view,
4290 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004291{
4292 struct weston_compositor *compositor = shell->compositor;
4293 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004294 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004295 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004296 struct weston_output *output, *target_output = NULL;
4297 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004298 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004299
4300 /* As a heuristic place the new window on the same output as the
4301 * pointer. Falling back to the output containing 0, 0.
4302 *
4303 * TODO: Do something clever for touch too?
4304 */
4305 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004306 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4307
4308 if (pointer) {
4309 ix = wl_fixed_to_int(pointer->x);
4310 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004311 break;
4312 }
4313 }
4314
4315 wl_list_for_each(output, &compositor->output_list, link) {
4316 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4317 target_output = output;
4318 break;
4319 }
4320 }
4321
4322 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004323 weston_view_set_position(view, 10 + random() % 400,
4324 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004325 return;
4326 }
4327
4328 /* Valid range within output where the surface will still be onscreen.
4329 * If this is negative it means that the surface is bigger than
4330 * output.
4331 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004332 get_output_work_area(shell, target_output, &area);
4333
Derek Foremanf814c5d2015-04-15 12:23:54 -05004334 x = area.x;
4335 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004336 range_x = area.width - view->surface->width;
4337 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004338
Rob Bradford4cb88c72012-08-13 15:18:44 +01004339 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004340 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004341
4342 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004343 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004344
Jason Ekstranda7af7042013-10-12 22:38:11 -05004345 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004346}
4347
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004348static bool
4349check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4350{
4351 struct timespec now;
4352
4353 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4354 return false;
4355
4356 /*
4357 * If the shell helper client dies before the session has been
4358 * up for roughly 30 seconds, better just make Weston shut down,
4359 * because the user likely has no way to interact with the desktop
4360 * anyway.
4361 */
4362 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4363 weston_log("Error: %s apparently cannot run at all.\n",
4364 shell->client);
4365 weston_log_continue(STAMP_SPACE "Quitting...");
Pekka Paalanen052032d2019-02-06 10:44:37 +02004366 weston_compositor_exit_with_code(shell->compositor,
4367 EXIT_FAILURE);
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004368
4369 return true;
4370 }
4371
4372 return false;
4373}
4374
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004375static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004376
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004377static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004378respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004379{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004380 struct timespec time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004381
4382 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004383 weston_compositor_get_time(&time);
4384 if (timespec_sub_to_msec(&time, &shell->child.deathstamp) > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004385 shell->child.deathstamp = time;
4386 shell->child.deathcount = 0;
4387 }
4388
4389 shell->child.deathcount++;
4390 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004391 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004392 return;
4393 }
4394
Pekka Paalanen826dc142014-08-27 12:11:53 +03004395 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004396 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004397}
4398
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004399static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004400desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4401{
4402 struct desktop_shell *shell;
4403
4404 shell = container_of(listener, struct desktop_shell,
4405 child.client_destroy_listener);
4406
Pekka Paalanen826dc142014-08-27 12:11:53 +03004407 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004408 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004409 /*
4410 * unbind_desktop_shell() will reset shell->child.desktop_shell
4411 * before the respawned process has a chance to create a new
4412 * desktop_shell object, because we are being called from the
4413 * wl_client destructor which destroys all wl_resources before
4414 * returning.
4415 */
4416
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004417 if (!check_desktop_shell_crash_too_early(shell))
4418 respawn_desktop_shell_process(shell);
4419
Pekka Paalanen826dc142014-08-27 12:11:53 +03004420 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004421}
4422
4423static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004424launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004425{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004426 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004427
Pekka Paalanen826dc142014-08-27 12:11:53 +03004428 shell->child.client = weston_client_start(shell->compositor,
4429 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004430
Arnaud Vrac42631192014-08-25 20:56:46 +02004431 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004432 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004433 return;
4434 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004435
4436 shell->child.client_destroy_listener.notify =
4437 desktop_shell_client_destroy;
4438 wl_client_add_destroy_listener(shell->child.client,
4439 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004440}
4441
4442static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004443unbind_desktop_shell(struct wl_resource *resource)
4444{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004445 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004446
4447 if (shell->locked)
4448 resume_desktop(shell);
4449
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004450 shell->child.desktop_shell = NULL;
4451 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004452}
4453
4454static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004455bind_desktop_shell(struct wl_client *client,
4456 void *data, uint32_t version, uint32_t id)
4457{
Tiago Vignattibe143262012-04-16 17:31:41 +03004458 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004459 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004460
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004461 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4462 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004463
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004464 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004465 wl_resource_set_implementation(resource,
4466 &desktop_shell_implementation,
4467 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004468 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004469 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004470 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004471
4472 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4473 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004474}
4475
Kristian Høgsberg07045392012-02-19 18:52:44 -05004476struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004477 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004478 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004479 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004480 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004481 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004482};
4483
4484static void
4485switcher_next(struct switcher *switcher)
4486{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004487 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004488 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004489 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004490 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004491
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004492 /* temporary re-display minimized surfaces */
4493 struct weston_view *tmp;
4494 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004495 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4496 weston_layer_entry_remove(&view->layer_link);
4497 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004498 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4499 *minimized = view;
4500 }
4501
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004502 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004503 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004504 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004505 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004506 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004507 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004508 next = view;
4509 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004510 view->alpha = 0.25;
4511 weston_view_geometry_dirty(view);
4512 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004513 }
Alex Wu1659daa2012-04-01 20:13:09 +08004514
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004515 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004516 view->alpha = 0.25;
4517 weston_view_geometry_dirty(view);
4518 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004519 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004520 }
4521
4522 if (next == NULL)
4523 next = first;
4524
Alex Wu07b26062012-03-12 16:06:01 +08004525 if (next == NULL)
4526 return;
4527
Kristian Høgsberg07045392012-02-19 18:52:44 -05004528 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004529 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004530
4531 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004532 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004533 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004534
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004535 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004536 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004537 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004538}
4539
4540static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004541switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004542{
4543 struct switcher *switcher =
4544 container_of(listener, struct switcher, listener);
4545
4546 switcher_next(switcher);
4547}
4548
4549static void
Daniel Stone351eb612012-05-31 15:27:47 -04004550switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004551{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004552 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004553 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004554 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004555
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004556 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004557 if (is_focus_view(view))
4558 continue;
4559
Jason Ekstranda7af7042013-10-12 22:38:11 -05004560 view->alpha = 1.0;
4561 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004562 }
4563
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004564 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004565 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004566 keyboard->seat,
4567 WESTON_ACTIVATE_FLAG_CONFIGURE);
4568 }
4569
Kristian Høgsberg07045392012-02-19 18:52:44 -05004570 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004571 weston_keyboard_end_grab(keyboard);
4572 if (keyboard->input_method_resource)
4573 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004574
4575 /* re-hide surfaces that were temporary shown during the switch */
4576 struct weston_view **minimized;
4577 wl_array_for_each(minimized, &switcher->minimized_array) {
4578 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004579 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004580 weston_layer_entry_remove(&(*minimized)->layer_link);
4581 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004582 weston_view_damage_below(*minimized);
4583 }
4584 }
4585 wl_array_release(&switcher->minimized_array);
4586
Kristian Høgsberg07045392012-02-19 18:52:44 -05004587 free(switcher);
4588}
4589
4590static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004591switcher_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004592 const struct timespec *time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004593{
4594 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004595 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004596
Daniel Stonec9785ea2012-05-30 16:31:52 +01004597 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004598 switcher_next(switcher);
4599}
4600
4601static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004602switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004603 uint32_t mods_depressed, uint32_t mods_latched,
4604 uint32_t mods_locked, uint32_t group)
4605{
4606 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004607 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004608
Daniel Stone351eb612012-05-31 15:27:47 -04004609 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4610 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004611}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004612
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004613static void
4614switcher_cancel(struct weston_keyboard_grab *grab)
4615{
4616 struct switcher *switcher = container_of(grab, struct switcher, grab);
4617
4618 switcher_destroy(switcher);
4619}
4620
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004621static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004622 switcher_key,
4623 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004624 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004625};
4626
4627static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004628switcher_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004629 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004630{
Tiago Vignattibe143262012-04-16 17:31:41 +03004631 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004632 struct switcher *switcher;
4633
4634 switcher = malloc(sizeof *switcher);
ganjingc1e71512020-07-28 15:28:45 +08004635 if (!switcher)
4636 return;
Pekka Paalanen4bb326b2021-05-14 14:37:46 +03004637
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004638 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004639 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004640 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004641 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004642 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004643
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004644 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004645 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004646 weston_keyboard_start_grab(keyboard, &switcher->grab);
4647 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004648 switcher_next(switcher);
4649}
4650
Pekka Paalanen3c647232011-12-22 13:43:43 +02004651static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004652backlight_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004653 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004654{
4655 struct weston_compositor *compositor = data;
4656 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004657 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004658
4659 /* TODO: we're limiting to simple use cases, where we assume just
4660 * control on the primary display. We'd have to extend later if we
4661 * ever get support for setting backlights on random desktop LCD
4662 * panels though */
4663 output = get_default_output(compositor);
4664 if (!output)
4665 return;
4666
4667 if (!output->set_backlight)
4668 return;
4669
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004670 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4671 backlight_new = output->backlight_current - 25;
4672 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4673 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004674
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004675 if (backlight_new < 5)
4676 backlight_new = 5;
4677 if (backlight_new > 255)
4678 backlight_new = 255;
4679
4680 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004681 output->set_backlight(output, output->backlight_current);
4682}
4683
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004684static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004685force_kill_binding(struct weston_keyboard *keyboard,
4686 const struct timespec *time, uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004687{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004688 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004689 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004690 struct desktop_shell *shell = data;
4691 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004692 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004693
Derek Foreman8ae2db52015-07-15 13:00:36 -05004694 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004695 if (!focus_surface)
4696 return;
4697
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004698 wl_signal_emit(&compositor->kill_signal, focus_surface);
4699
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004700 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004701 wl_client_get_credentials(client, &pid, NULL, NULL);
4702
4703 /* Skip clients that we launched ourselves (the credentials of
4704 * the socketpair is ours) */
4705 if (pid == getpid())
4706 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004707
Daniel Stone325fc2d2012-05-30 16:31:58 +01004708 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004709}
4710
4711static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004712workspace_up_binding(struct weston_keyboard *keyboard,
4713 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004714{
4715 struct desktop_shell *shell = data;
4716 unsigned int new_index = shell->workspaces.current;
4717
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004718 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004719 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004720 if (new_index != 0)
4721 new_index--;
4722
4723 change_workspace(shell, new_index);
4724}
4725
4726static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004727workspace_down_binding(struct weston_keyboard *keyboard,
4728 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004729{
4730 struct desktop_shell *shell = data;
4731 unsigned int new_index = shell->workspaces.current;
4732
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004733 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004734 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004735 if (new_index < shell->workspaces.num - 1)
4736 new_index++;
4737
4738 change_workspace(shell, new_index);
4739}
4740
4741static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004742workspace_f_binding(struct weston_keyboard *keyboard,
4743 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004744{
4745 struct desktop_shell *shell = data;
4746 unsigned int new_index;
4747
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004748 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004749 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004750 new_index = key - KEY_F1;
4751 if (new_index >= shell->workspaces.num)
4752 new_index = shell->workspaces.num - 1;
4753
4754 change_workspace(shell, new_index);
4755}
4756
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004757static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004758workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004759 const struct timespec *time, uint32_t key,
4760 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004761{
4762 struct desktop_shell *shell = data;
4763 unsigned int new_index = shell->workspaces.current;
4764
4765 if (shell->locked)
4766 return;
4767
4768 if (new_index != 0)
4769 new_index--;
4770
Derek Foreman8ae2db52015-07-15 13:00:36 -05004771 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004772}
4773
4774static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004775workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004776 const struct timespec *time, uint32_t key,
4777 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004778{
4779 struct desktop_shell *shell = data;
4780 unsigned int new_index = shell->workspaces.current;
4781
4782 if (shell->locked)
4783 return;
4784
4785 if (new_index < shell->workspaces.num - 1)
4786 new_index++;
4787
Derek Foreman8ae2db52015-07-15 13:00:36 -05004788 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004789}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004790
4791static void
Harish Krupodc3edc02019-04-20 17:50:18 +05304792shell_reposition_view_on_output_change(struct weston_view *view)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004793{
4794 struct weston_output *output, *first_output;
4795 struct weston_compositor *ec = view->surface->compositor;
4796 struct shell_surface *shsurf;
4797 float x, y;
4798 int visible;
4799
Harish Krupo2dff4dd2019-04-20 18:10:56 +05304800 if (wl_list_empty(&ec->output_list))
4801 return;
4802
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004803 x = view->geometry.x;
4804 y = view->geometry.y;
4805
4806 /* At this point the destroyed output is not in the list anymore.
4807 * If the view is still visible somewhere, we leave where it is,
4808 * otherwise, move it to the first output. */
4809 visible = 0;
4810 wl_list_for_each(output, &ec->output_list, link) {
4811 if (pixman_region32_contains_point(&output->region,
4812 x, y, NULL)) {
4813 visible = 1;
4814 break;
4815 }
4816 }
4817
4818 if (!visible) {
4819 first_output = container_of(ec->output_list.next,
4820 struct weston_output, link);
4821
4822 x = first_output->x + first_output->width / 4;
4823 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004824
4825 weston_view_set_position(view, x, y);
4826 } else {
4827 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004828 }
4829
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004830
4831 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004832 if (!shsurf)
4833 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004834
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004835 shsurf->saved_position_valid = false;
4836 set_maximized(shsurf, false);
4837 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004838}
4839
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004840void
4841shell_for_each_layer(struct desktop_shell *shell,
4842 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004843{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004844 struct workspace **ws;
4845
4846 func(shell, &shell->fullscreen_layer, data);
4847 func(shell, &shell->panel_layer, data);
4848 func(shell, &shell->background_layer, data);
4849 func(shell, &shell->lock_layer, data);
4850 func(shell, &shell->input_panel_layer, data);
4851
4852 wl_array_for_each(ws, &shell->workspaces.array)
4853 func(shell, &(*ws)->layer, data);
4854}
4855
4856static void
Harish Krupodc3edc02019-04-20 17:50:18 +05304857shell_output_changed_move_layer(struct desktop_shell *shell,
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004858 struct weston_layer *layer,
4859 void *data)
4860{
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004861 struct weston_view *view;
4862
Marius Vladea40d6d2018-02-08 18:46:42 +02004863 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Harish Krupodc3edc02019-04-20 17:50:18 +05304864 shell_reposition_view_on_output_change(view);
4865
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004866}
4867
4868static void
Pekka Paalanen719ca872021-05-24 16:53:40 +03004869shell_output_destroy(struct shell_output *shell_output)
Xiong Zhang6b481422013-10-23 13:58:32 +08004870{
Pekka Paalanen0d5e4ff2021-05-24 16:13:21 +03004871 struct desktop_shell *shell = shell_output->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004872
Harish Krupodc3edc02019-04-20 17:50:18 +05304873 shell_for_each_layer(shell, shell_output_changed_move_layer, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004874
Pekka Paalanen719ca872021-05-24 16:53:40 +03004875 if (shell_output->fade.animation) {
4876 weston_view_animation_destroy(shell_output->fade.animation);
4877 shell_output->fade.animation = NULL;
4878 }
4879
4880 if (shell_output->fade.view) {
4881 /* destroys the view as well */
4882 weston_surface_destroy(shell_output->fade.view->surface);
4883 }
4884
4885 if (shell_output->fade.startup_timer)
4886 wl_event_source_remove(shell_output->fade.startup_timer);
4887
Pekka Paalanen0d5e4ff2021-05-24 16:13:21 +03004888 if (shell_output->panel_surface)
4889 wl_list_remove(&shell_output->panel_surface_listener.link);
4890 if (shell_output->background_surface)
4891 wl_list_remove(&shell_output->background_surface_listener.link);
4892 wl_list_remove(&shell_output->destroy_listener.link);
4893 wl_list_remove(&shell_output->link);
4894 free(shell_output);
Xiong Zhang6b481422013-10-23 13:58:32 +08004895}
4896
4897static void
Pekka Paalanen719ca872021-05-24 16:53:40 +03004898handle_output_destroy(struct wl_listener *listener, void *data)
4899{
4900 struct shell_output *shell_output =
4901 container_of(listener, struct shell_output, destroy_listener);
4902
4903 shell_output_destroy(shell_output);
4904}
4905
4906static void
David Fort50d962f2016-06-09 17:55:52 +02004907shell_resize_surface_to_output(struct desktop_shell *shell,
4908 struct weston_surface *surface,
4909 const struct weston_output *output)
4910{
4911 if (!surface)
4912 return;
4913
4914 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4915 surface->resource,
4916 output->width,
4917 output->height);
4918}
4919
4920
4921static void
4922handle_output_resized(struct wl_listener *listener, void *data)
4923{
4924 struct desktop_shell *shell =
4925 container_of(listener, struct desktop_shell, resized_listener);
4926 struct weston_output *output = (struct weston_output *)data;
4927 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4928
4929 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4930 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4931}
4932
4933static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004934create_shell_output(struct desktop_shell *shell,
4935 struct weston_output *output)
4936{
4937 struct shell_output *shell_output;
4938
4939 shell_output = zalloc(sizeof *shell_output);
4940 if (shell_output == NULL)
4941 return;
4942
4943 shell_output->output = output;
4944 shell_output->shell = shell;
4945 shell_output->destroy_listener.notify = handle_output_destroy;
4946 wl_signal_add(&output->destroy_signal,
4947 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004948 wl_list_insert(shell->output_list.prev, &shell_output->link);
Harish Krupodc3edc02019-04-20 17:50:18 +05304949
4950 if (wl_list_length(&shell->output_list) == 1)
4951 shell_for_each_layer(shell,
4952 shell_output_changed_move_layer, NULL);
Xiong Zhang6b481422013-10-23 13:58:32 +08004953}
4954
4955static void
4956handle_output_create(struct wl_listener *listener, void *data)
4957{
4958 struct desktop_shell *shell =
4959 container_of(listener, struct desktop_shell, output_create_listener);
4960 struct weston_output *output = (struct weston_output *)data;
4961
4962 create_shell_output(shell, output);
4963}
4964
4965static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004966handle_output_move_layer(struct desktop_shell *shell,
4967 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004968{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004969 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004970 struct weston_view *view;
4971 float x, y;
4972
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004973 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004974 if (view->output != output)
4975 continue;
4976
4977 x = view->geometry.x + output->move_x;
4978 y = view->geometry.y + output->move_y;
4979 weston_view_set_position(view, x, y);
4980 }
4981}
4982
4983static void
4984handle_output_move(struct wl_listener *listener, void *data)
4985{
4986 struct desktop_shell *shell;
4987
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004988 shell = container_of(listener, struct desktop_shell,
4989 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004990
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004991 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004992}
4993
4994static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004995setup_output_destroy_handler(struct weston_compositor *ec,
4996 struct desktop_shell *shell)
4997{
4998 struct weston_output *output;
4999
5000 wl_list_init(&shell->output_list);
5001 wl_list_for_each(output, &ec->output_list, link)
5002 create_shell_output(shell, output);
5003
5004 shell->output_create_listener.notify = handle_output_create;
5005 wl_signal_add(&ec->output_created_signal,
5006 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005007
5008 shell->output_move_listener.notify = handle_output_move;
5009 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08005010}
5011
5012static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005013shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005014{
Tiago Vignattibe143262012-04-16 17:31:41 +03005015 struct desktop_shell *shell =
5016 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005017 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005018 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005019
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005020 /* Force state to unlocked so we don't try to fade */
5021 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005022
5023 if (shell->child.client) {
5024 /* disable respawn */
5025 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005026 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03005027 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005028
Harsha M Mb8b2c722018-08-07 19:05:02 +05305029 wl_list_remove(&shell->destroy_listener.link);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005030 wl_list_remove(&shell->idle_listener.link);
5031 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005032 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005033
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03005034 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005035 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005036
Pekka Paalanen719ca872021-05-24 16:53:40 +03005037 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link)
5038 shell_output_destroy(shell_output);
Xiong Zhang6b481422013-10-23 13:58:32 +08005039
5040 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07005041 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02005042 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005043
Pekka Paalanenabd72922021-05-24 14:42:00 +03005044 weston_desktop_destroy(shell->desktop);
5045
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005046 wl_array_for_each(ws, &shell->workspaces.array)
5047 workspace_destroy(*ws);
5048 wl_array_release(&shell->workspaces.array);
5049
Pekka Paalanen4bb326b2021-05-14 14:37:46 +03005050 weston_layer_fini(&shell->fullscreen_layer);
5051 weston_layer_fini(&shell->panel_layer);
5052 weston_layer_fini(&shell->background_layer);
5053 weston_layer_fini(&shell->lock_layer);
5054 weston_layer_fini(&shell->input_panel_layer);
5055 weston_layer_fini(&shell->minimized_layer);
5056
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005057 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005058 free(shell);
5059}
5060
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005061static void
5062shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5063{
5064 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005065 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005066
Bob Ham744e6532016-01-12 10:21:48 +00005067 if (shell->allow_zap)
5068 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5069 MODIFIER_CTRL | MODIFIER_ALT,
5070 terminate_binding, ec);
5071
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005072 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005073 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5074 click_to_activate_binding,
5075 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07005076 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
5077 click_to_activate_binding,
5078 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005079 weston_compositor_add_touch_binding(ec, 0,
5080 touch_to_activate_binding,
5081 shell);
Bob Ham553d1242016-01-12 10:21:49 +00005082 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5083 backlight_binding, ec);
5084 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5085 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005086
5087 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00005088 if (shell->exposay_modifier)
5089 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5090 exposay_binding, shell);
5091
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005092 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00005093 if (!mod)
5094 return;
5095
Ian Ray13404c42017-09-18 15:22:01 +03005096 /* This binding is not configurable, but is only enabled if there is a
5097 * valid binding modifier. */
5098 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5099 MODIFIER_SUPER | MODIFIER_ALT,
5100 surface_opacity_binding, NULL);
5101
Ian Rayab7c0b62017-09-18 15:22:00 +03005102 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5103 mod, zoom_axis_binding,
5104 NULL);
5105
Daniel Stone325fc2d2012-05-30 16:31:58 +01005106 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5107 zoom_key_binding, NULL);
5108 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5109 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005110 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5111 maximize_binding, NULL);
5112 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5113 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005114 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5115 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005116 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05005117 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005118 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08005119 weston_compositor_add_button_binding(ec, BTN_LEFT,
5120 mod | MODIFIER_SHIFT,
5121 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005122
5123 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05005124 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005125 rotate_binding, NULL);
5126
Daniel Stone325fc2d2012-05-30 16:31:58 +01005127 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5128 shell);
5129 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5130 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005131 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5132 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005133 weston_compositor_add_key_binding(ec, KEY_K, mod,
5134 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005135 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5136 workspace_up_binding, shell);
5137 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5138 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005139 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5140 workspace_move_surface_up_binding,
5141 shell);
5142 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5143 workspace_move_surface_down_binding,
5144 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005145
5146 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5147 if (shell->workspaces.num > 1) {
5148 num_workspace_bindings = shell->workspaces.num;
5149 if (num_workspace_bindings > 6)
5150 num_workspace_bindings = 6;
5151 for (i = 0; i < num_workspace_bindings; i++)
5152 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5153 workspace_f_binding,
5154 shell);
5155 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005156
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02005157 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005158}
5159
Jason Ekstrand024177c2014-04-21 19:42:58 -05005160static void
5161handle_seat_created(struct wl_listener *listener, void *data)
5162{
5163 struct weston_seat *seat = data;
5164
5165 create_shell_seat(seat);
5166}
5167
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005168WL_EXPORT int
Quentin Glidicda01c1d2016-12-02 14:17:08 +01005169wet_shell_init(struct weston_compositor *ec,
5170 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005171{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005172 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005173 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005174 struct workspace **pws;
5175 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005176 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005177
Peter Huttererf3d62272013-08-08 11:57:05 +10005178 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005179 if (shell == NULL)
5180 return -1;
5181
Kristian Høgsberg75840622011-09-06 13:48:16 -04005182 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005183
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02005184 if (!weston_compositor_add_destroy_listener_once(ec,
5185 &shell->destroy_listener,
5186 shell_destroy)) {
5187 free(shell);
5188 return 0;
5189 }
5190
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005191 shell->idle_listener.notify = idle_handler;
5192 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5193 shell->wake_listener.notify = wake_handler;
5194 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005195 shell->transform_listener.notify = transform_handler;
5196 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005197
Quentin Glidic82681572016-12-17 13:40:51 +01005198 weston_layer_init(&shell->fullscreen_layer, ec);
5199 weston_layer_init(&shell->panel_layer, ec);
5200 weston_layer_init(&shell->background_layer, ec);
5201 weston_layer_init(&shell->lock_layer, ec);
5202 weston_layer_init(&shell->input_panel_layer, ec);
5203
5204 weston_layer_set_position(&shell->fullscreen_layer,
5205 WESTON_LAYER_POSITION_FULLSCREEN);
5206 weston_layer_set_position(&shell->panel_layer,
5207 WESTON_LAYER_POSITION_UI);
5208 weston_layer_set_position(&shell->background_layer,
5209 WESTON_LAYER_POSITION_BACKGROUND);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005210
5211 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005212 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005213
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005214 if (input_panel_setup(shell) < 0)
5215 return -1;
5216
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03005217 shell->text_backend = text_backend_init(ec);
5218 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00005219 return -1;
5220
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005221 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005222
Daniel Stonedf8133b2013-11-19 11:37:14 +01005223 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5224 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5225
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005226 for (i = 0; i < shell->workspaces.num; i++) {
5227 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5228 if (pws == NULL)
5229 return -1;
5230
Quentin Glidic82681572016-12-17 13:40:51 +01005231 *pws = workspace_create(shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005232 if (*pws == NULL)
5233 return -1;
5234 }
5235 activate_workspace(shell, 0);
5236
Quentin Glidic82681572016-12-17 13:40:51 +01005237 weston_layer_init(&shell->minimized_layer, ec);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01005238
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005239 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005240 wl_list_init(&shell->workspaces.animation.link);
5241 shell->workspaces.animation.frame = animate_workspace_change_frame;
5242
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02005243 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
5244 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005245 return -1;
5246
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005247 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005248 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005249 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005250 return -1;
5251
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02005252 weston_compositor_get_time(&shell->child.deathstamp);
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005253
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005254 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02005255
Xiong Zhang6b481422013-10-23 13:58:32 +08005256 setup_output_destroy_handler(ec, shell);
5257
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005258 loop = wl_display_get_event_loop(ec->wl_display);
5259 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005260
Jason Ekstrand024177c2014-04-21 19:42:58 -05005261 wl_list_for_each(seat, &ec->seat_list, link)
5262 handle_seat_created(NULL, seat);
5263 shell->seat_create_listener.notify = handle_seat_created;
5264 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005265
David Fort50d962f2016-06-09 17:55:52 +02005266 shell->resized_listener.notify = handle_output_resized;
5267 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
5268
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01005269 screenshooter_create(ec);
5270
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005271 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005272
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005273 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005274
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005275 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
5276
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005277 return 0;
5278}