blob: 0d0daa3f8c4cdfab9b1e6eab72a7d6a067dcb533 [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) {
Erik Kurzinger04918f32021-05-18 11:49:37 -04002467
2468 if (shsurf->shell->compositor->state == WESTON_COMPOSITOR_ACTIVE) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002469 pixman_region32_fini(&surface->pending.input);
2470 pixman_region32_init(&surface->pending.input);
2471 pixman_region32_fini(&surface->input);
2472 pixman_region32_init(&surface->input);
2473 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2474 fade_out_done, shsurf);
Erik Kurzinger04918f32021-05-18 11:49:37 -04002475 return;
2476 } else {
2477 --surface->ref_count;
2478 }
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002479 }
Erik Kurzinger04918f32021-05-18 11:49:37 -04002480
2481 weston_surface_destroy(surface);
2482
2483 if (shsurf->output_destroy_listener.notify) {
2484 wl_list_remove(&shsurf->output_destroy_listener.link);
2485 shsurf->output_destroy_listener.notify = NULL;
2486 }
2487
2488 free(shsurf);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002489}
2490
2491static void
2492set_maximized_position(struct desktop_shell *shell,
2493 struct shell_surface *shsurf)
2494{
2495 pixman_rectangle32_t area;
2496 struct weston_geometry geometry;
2497
2498 get_output_work_area(shell, shsurf->output, &area);
2499 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2500
2501 weston_view_set_position(shsurf->view,
2502 area.x - geometry.x,
2503 area.y - geometry.y);
2504}
2505
2506static void
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002507set_position_from_xwayland(struct shell_surface *shsurf)
2508{
2509 struct weston_geometry geometry;
2510 float x;
2511 float y;
2512
2513 assert(shsurf->xwayland.is_set);
2514
2515 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2516 x = shsurf->xwayland.x - geometry.x;
2517 y = shsurf->xwayland.y - geometry.y;
2518
2519 weston_view_set_position(shsurf->view, x, y);
2520
2521#ifdef WM_DEBUG
2522 weston_log("%s: XWM %d, %d; geometry %d, %d; view %f, %f\n",
2523 __func__, shsurf->xwayland.x, shsurf->xwayland.y,
2524 geometry.x, geometry.y, x, y);
2525#endif
2526}
2527
2528static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002529map(struct desktop_shell *shell, struct shell_surface *shsurf,
2530 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002531{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002532 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002533 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2534 struct weston_compositor *compositor = shell->compositor;
2535 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002536
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002537 /* initial positioning, see also configure() */
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002538 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002539 center_on_output(shsurf->view, shsurf->fullscreen_output);
2540 shell_map_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002541 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002542 set_maximized_position(shell, shsurf);
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002543 } else if (shsurf->xwayland.is_set) {
2544 set_position_from_xwayland(shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002545 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002546 weston_view_set_initial_position(shsurf->view, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002547 }
2548
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002549 /* Surface stacking order, see also activate(). */
2550 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002551
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002552 weston_view_update_transform(shsurf->view);
2553 shsurf->view->is_mapped = true;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002554 if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002555 surface->output = shsurf->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02002556 weston_view_set_output(shsurf->view, shsurf->output);
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002557 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002558
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002559 if (!shell->locked) {
2560 wl_list_for_each(seat, &compositor->seat_list, link)
2561 activate(shell, shsurf->view, seat,
2562 WESTON_ACTIVATE_FLAG_CONFIGURE);
2563 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002564
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002565 if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002566 switch (shell->win_animation_type) {
2567 case ANIMATION_FADE:
2568 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2569 break;
2570 case ANIMATION_ZOOM:
2571 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2572 break;
2573 case ANIMATION_NONE:
2574 default:
2575 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002576 }
2577 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002578}
2579
2580static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002581desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2582 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002583{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002584 struct shell_surface *shsurf =
2585 weston_desktop_surface_get_user_data(desktop_surface);
2586 struct weston_surface *surface =
2587 weston_desktop_surface_get_surface(desktop_surface);
2588 struct weston_view *view = shsurf->view;
2589 struct desktop_shell *shell = data;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002590 bool was_fullscreen;
2591 bool was_maximized;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002592
2593 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002594 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002595
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002596 was_fullscreen = shsurf->state.fullscreen;
2597 was_maximized = shsurf->state.maximized;
2598
2599 shsurf->state.fullscreen =
2600 weston_desktop_surface_get_fullscreen(desktop_surface);
2601 shsurf->state.maximized =
2602 weston_desktop_surface_get_maximized(desktop_surface);
2603
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002604 if (!weston_surface_is_mapped(surface)) {
2605 map(shell, shsurf, sx, sy);
2606 surface->is_mapped = true;
2607 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2608 ++surface->ref_count;
2609 return;
2610 }
2611
2612 if (sx == 0 && sy == 0 &&
2613 shsurf->last_width == surface->width &&
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002614 shsurf->last_height == surface->height &&
2615 was_fullscreen == shsurf->state.fullscreen &&
2616 was_maximized == shsurf->state.maximized)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002617 return;
2618
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002619 if (was_fullscreen)
2620 unset_fullscreen(shsurf);
2621 if (was_maximized)
2622 unset_maximized(shsurf);
2623
Quentin Glidic920cf042016-08-16 14:26:20 +02002624 if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2625 !shsurf->saved_position_valid) {
2626 shsurf->saved_x = shsurf->view->geometry.x;
2627 shsurf->saved_y = shsurf->view->geometry.y;
2628 shsurf->saved_position_valid = true;
2629
2630 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2631 wl_list_remove(&shsurf->rotation.transform.link);
2632 wl_list_init(&shsurf->rotation.transform.link);
2633 weston_view_geometry_dirty(shsurf->view);
2634 shsurf->saved_rotation_valid = true;
2635 }
2636 }
2637
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002638 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002639 shell_configure_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002640 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002641 set_maximized_position(shell, shsurf);
2642 surface->output = shsurf->output;
2643 } else {
2644 float from_x, from_y;
2645 float to_x, to_y;
2646 float x, y;
2647
2648 if (shsurf->resize_edges) {
2649 sx = 0;
2650 sy = 0;
2651 }
2652
2653 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2654 sx = shsurf->last_width - surface->width;
2655 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2656 sy = shsurf->last_height - surface->height;
2657
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002658 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2659 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2660 x = shsurf->view->geometry.x + to_x - from_x;
2661 y = shsurf->view->geometry.y + to_y - from_y;
2662
2663 weston_view_set_position(shsurf->view, x, y);
2664 }
2665
Emmanuel Gil Peyrotc3941792017-04-04 18:49:33 +01002666 shsurf->last_width = surface->width;
2667 shsurf->last_height = surface->height;
2668
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002669 /* XXX: would a fullscreen surface need the same handling? */
2670 if (surface->output) {
2671 wl_list_for_each(view, &surface->views, surface_link)
2672 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002673 }
2674}
2675
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002676static void
Derek Foreman927d9e22017-10-06 14:37:44 -05002677get_maximized_size(struct shell_surface *shsurf, int32_t *width, int32_t *height)
2678{
2679 struct desktop_shell *shell;
2680 pixman_rectangle32_t area;
2681
2682 shell = shell_surface_get_shell(shsurf);
2683 get_output_work_area(shell, shsurf->output, &area);
2684
2685 *width = area.width;
2686 *height = area.height;
2687}
2688
2689static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002690set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2691 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002692{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002693 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2694 struct weston_surface *surface =
2695 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2696 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002697
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002698 if (fullscreen) {
2699 /* handle clients launching in fullscreen */
2700 if (output == NULL && !weston_surface_is_mapped(surface)) {
2701 /* Set the output to the one that has focus currently. */
2702 output = get_focused_output(surface->compositor);
2703 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002704
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002705 shell_surface_set_output(shsurf, output);
2706 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002707
Marius Vlad6ebda362020-03-28 00:23:14 +02002708 if (shsurf->output) {
2709 width = shsurf->output->width;
2710 height = shsurf->output->height;
2711 }
Derek Foremane1af3d82017-10-06 14:37:45 -05002712 } else if (weston_desktop_surface_get_maximized(desktop_surface)) {
2713 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002714 }
2715 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2716 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002717}
2718
2719static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002720desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2721 struct weston_seat *seat, uint32_t serial, void *shell)
2722{
2723 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2724 struct weston_touch *touch = weston_seat_get_touch(seat);
2725 struct shell_surface *shsurf =
2726 weston_desktop_surface_get_user_data(desktop_surface);
2727 struct weston_surface *surface =
2728 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2729 struct wl_resource *resource = surface->resource;
2730 struct weston_surface *focus;
2731
2732 if (pointer &&
2733 pointer->focus &&
2734 pointer->button_count > 0 &&
2735 pointer->grab_serial == serial) {
2736 focus = weston_surface_get_main_surface(pointer->focus->surface);
2737 if ((focus == surface) &&
2738 (surface_move(shsurf, pointer, true) < 0))
2739 wl_resource_post_no_memory(resource);
2740 } else if (touch &&
2741 touch->focus &&
2742 touch->grab_serial == serial) {
2743 focus = weston_surface_get_main_surface(touch->focus->surface);
2744 if ((focus == surface) &&
2745 (surface_touch_move(shsurf, touch) < 0))
2746 wl_resource_post_no_memory(resource);
2747 }
2748}
2749
2750static void
2751desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2752 struct weston_seat *seat, uint32_t serial,
2753 enum weston_desktop_surface_edge edges, void *shell)
2754{
2755 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2756 struct shell_surface *shsurf =
2757 weston_desktop_surface_get_user_data(desktop_surface);
2758 struct weston_surface *surface =
2759 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2760 struct wl_resource *resource = surface->resource;
2761 struct weston_surface *focus;
2762
2763 if (!pointer ||
2764 pointer->button_count == 0 ||
2765 pointer->grab_serial != serial ||
2766 pointer->focus == NULL)
2767 return;
2768
2769 focus = weston_surface_get_main_surface(pointer->focus->surface);
2770 if (focus != surface)
2771 return;
2772
2773 if (surface_resize(shsurf, pointer, edges) < 0)
2774 wl_resource_post_no_memory(resource);
2775}
2776
2777static void
Stefan Agnera8da2082019-06-23 14:53:59 +02002778desktop_surface_set_parent(struct weston_desktop_surface *desktop_surface,
2779 struct weston_desktop_surface *parent,
2780 void *shell)
2781{
Marius Vlada27658e2020-01-17 12:32:55 +02002782 struct shell_surface *shsurf_parent;
Stefan Agnera8da2082019-06-23 14:53:59 +02002783 struct shell_surface *shsurf =
2784 weston_desktop_surface_get_user_data(desktop_surface);
Stefan Agnera8da2082019-06-23 14:53:59 +02002785
Marius Vlada27658e2020-01-17 12:32:55 +02002786 /* unlink any potential child */
2787 wl_list_remove(&shsurf->children_link);
2788
2789 if (parent) {
2790 shsurf_parent = weston_desktop_surface_get_user_data(parent);
2791 wl_list_insert(shsurf_parent->children_list.prev,
2792 &shsurf->children_link);
2793 } else {
2794 wl_list_init(&shsurf->children_link);
2795 }
Stefan Agnera8da2082019-06-23 14:53:59 +02002796}
2797
2798static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002799desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2800 bool fullscreen,
2801 struct weston_output *output, void *shell)
2802{
2803 struct shell_surface *shsurf =
2804 weston_desktop_surface_get_user_data(desktop_surface);
2805
2806 set_fullscreen(shsurf, fullscreen, output);
2807}
2808
2809static void
2810set_maximized(struct shell_surface *shsurf, bool maximized)
2811{
2812 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2813 struct weston_surface *surface =
2814 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2815 int32_t width = 0, height = 0;
2816
2817 if (maximized) {
2818 struct weston_output *output;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002819
2820 if (!weston_surface_is_mapped(surface))
2821 output = get_focused_output(surface->compositor);
2822 else
2823 output = surface->output;
2824
2825 shell_surface_set_output(shsurf, output);
2826
Derek Foreman927d9e22017-10-06 14:37:44 -05002827 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002828 }
2829 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2830 weston_desktop_surface_set_size(desktop_surface, width, height);
2831}
2832
2833static void
2834desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2835 bool maximized, void *shell)
2836{
2837 struct shell_surface *shsurf =
2838 weston_desktop_surface_get_user_data(desktop_surface);
2839
2840 set_maximized(shsurf, maximized);
2841}
2842
2843static void
2844desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2845 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002846{
2847 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002848 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002849
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002850 /* apply compositor's own minimization logic (hide) */
2851 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002852}
2853
Rafael Antognollie2a34552013-12-03 15:35:45 -02002854static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002855set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002856{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002857 struct shell_grab *grab;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002858
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002859 if (pointer->grab->interface == &busy_cursor_grab_interface)
2860 return;
2861
2862 grab = malloc(sizeof *grab);
2863 if (!grab)
2864 return;
2865
2866 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2867 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2868 /* Mark the shsurf as ungrabbed so that button binding is able
2869 * to move it. */
2870 shsurf->grabbed = 0;
2871}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002872
2873static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002874end_busy_cursor(struct weston_compositor *compositor,
2875 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002876{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002877 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002878 struct shell_grab *grab;
2879 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002880
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002881 wl_list_for_each(seat, &compositor->seat_list, link) {
2882 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2883 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002884
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002885 if (!pointer)
2886 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002887
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002888 if (pointer->grab->interface != &busy_cursor_grab_interface)
2889 continue;
2890
2891 grab = (struct shell_grab *) pointer->grab;
2892 shsurf = grab->shsurf;
2893 if (!shsurf)
2894 continue;
2895
2896 grab_client =
2897 weston_desktop_surface_get_client(shsurf->desktop_surface);
2898 if (grab_client == desktop_client) {
2899 shell_grab_end(grab);
2900 free(grab);
2901 }
2902 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002903}
2904
2905static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002906desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2907 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002908{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002909 struct shell_surface *shsurf =
2910 weston_desktop_surface_get_user_data(desktop_surface);
2911 bool *unresponsive = user_data;
2912
2913 shsurf->unresponsive = *unresponsive;
2914}
2915
2916static void
2917desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2918 void *shell_)
2919{
2920 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002921 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002922 struct weston_seat *seat;
2923 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002924
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002925 weston_desktop_client_for_each_surface(desktop_client,
2926 desktop_surface_set_unresponsive,
2927 &unresponsive);
2928
2929
2930 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2931 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2932 struct weston_desktop_client *grab_client;
2933
2934 if (!pointer || !pointer->focus)
2935 continue;
2936
2937 shsurf = get_shell_surface(pointer->focus->surface);
2938 if (!shsurf)
2939 continue;
2940
2941 grab_client =
2942 weston_desktop_surface_get_client(shsurf->desktop_surface);
2943 if (grab_client == desktop_client)
2944 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002945 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002946}
2947
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002948static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002949desktop_surface_pong(struct weston_desktop_client *desktop_client,
2950 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002951{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002952 struct desktop_shell *shell = shell_;
2953 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002954
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002955 weston_desktop_client_for_each_surface(desktop_client,
2956 desktop_surface_set_unresponsive,
2957 &unresponsive);
2958 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002959}
2960
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002961static void
2962desktop_surface_set_xwayland_position(struct weston_desktop_surface *surface,
2963 int32_t x, int32_t y, void *shell_)
2964{
2965 struct shell_surface *shsurf =
2966 weston_desktop_surface_get_user_data(surface);
2967
2968 shsurf->xwayland.x = x;
2969 shsurf->xwayland.y = y;
2970 shsurf->xwayland.is_set = true;
2971}
2972
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002973static const struct weston_desktop_api shell_desktop_api = {
2974 .struct_size = sizeof(struct weston_desktop_api),
2975 .surface_added = desktop_surface_added,
2976 .surface_removed = desktop_surface_removed,
2977 .committed = desktop_surface_committed,
2978 .move = desktop_surface_move,
2979 .resize = desktop_surface_resize,
Stefan Agnera8da2082019-06-23 14:53:59 +02002980 .set_parent = desktop_surface_set_parent,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002981 .fullscreen_requested = desktop_surface_fullscreen_requested,
2982 .maximized_requested = desktop_surface_maximized_requested,
2983 .minimized_requested = desktop_surface_minimized_requested,
2984 .ping_timeout = desktop_surface_ping_timeout,
2985 .pong = desktop_surface_pong,
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002986 .set_xwayland_position = desktop_surface_set_xwayland_position,
Rafael Antognollie2a34552013-12-03 15:35:45 -02002987};
2988
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002989/* ************************ *
2990 * end of libweston-desktop *
2991 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04002992static void
Quentin Glidice8bf9592016-06-23 18:55:20 +02002993configure_static_view(struct weston_view *ev, struct weston_layer *layer, int x, int y)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002994{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002995 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002996
Semi Malinene7a52fb2018-04-26 11:08:10 +02002997 if (!ev->output)
2998 return;
2999
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003000 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003001 if (v->output == ev->output && v != ev) {
3002 weston_view_unmap(v);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003003 v->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003004 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003005 }
3006 }
3007
Quentin Glidice8bf9592016-06-23 18:55:20 +02003008 weston_view_set_position(ev, ev->output->x + x, ev->output->y + y);
Armin Krezović4663aca2016-06-30 06:04:29 +02003009 ev->surface->is_mapped = true;
3010 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003011
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003012 if (wl_list_empty(&ev->layer_link.link)) {
3013 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003014 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003015 }
3016}
3017
David Fort50d962f2016-06-09 17:55:52 +02003018
3019static struct shell_output *
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003020find_shell_output_from_weston_output(struct desktop_shell *shell,
3021 struct weston_output *output)
David Fort50d962f2016-06-09 17:55:52 +02003022{
3023 struct shell_output *shell_output;
3024
3025 wl_list_for_each(shell_output, &shell->output_list, link) {
3026 if (shell_output->output == output)
3027 return shell_output;
3028 }
3029
3030 return NULL;
3031}
3032
Pekka Paalanen8274d902014-08-06 19:36:51 +03003033static int
3034background_get_label(struct weston_surface *surface, char *buf, size_t len)
3035{
3036 return snprintf(buf, len, "background for output %s",
Miguel A. Vico620f68d2019-09-25 11:23:13 -07003037 (surface->output ? surface->output->name : "NULL"));
Pekka Paalanen8274d902014-08-06 19:36:51 +03003038}
3039
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003040static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003041background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003042{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003043 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003044 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003045
Jason Ekstranda7af7042013-10-12 22:38:11 -05003046 view = container_of(es->views.next, struct weston_view, surface_link);
3047
Quentin Glidice8bf9592016-06-23 18:55:20 +02003048 configure_static_view(view, &shell->background_layer, 0, 0);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003049}
3050
3051static void
David Fort50d962f2016-06-09 17:55:52 +02003052handle_background_surface_destroy(struct wl_listener *listener, void *data)
3053{
3054 struct shell_output *output =
3055 container_of(listener, struct shell_output, background_surface_listener);
3056
3057 weston_log("background surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09003058 wl_list_remove(&output->background_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02003059 output->background_surface = NULL;
3060}
3061
3062static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003063desktop_shell_set_background(struct wl_client *client,
3064 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003065 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003066 struct wl_resource *surface_resource)
3067{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003068 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003069 struct weston_surface *surface =
3070 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02003071 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003072 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003073
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003074 if (surface->committed) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003075 wl_resource_post_error(surface_resource,
3076 WL_DISPLAY_ERROR_INVALID_OBJECT,
3077 "surface role already assigned");
3078 return;
3079 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003080
Jason Ekstranda7af7042013-10-12 22:38:11 -05003081 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3082 weston_view_destroy(view);
3083 view = weston_view_create(surface);
3084
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003085 surface->committed = background_committed;
3086 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003087 weston_surface_set_label_func(surface, background_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03003088 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02003089 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02003090
3091 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02003092 if (sh_output->background_surface) {
3093 /* The output already has a background, tell our helper
3094 * there is no need for another one. */
3095 weston_desktop_shell_send_configure(resource, 0,
3096 surface_resource,
3097 0, 0);
3098 } else {
3099 weston_desktop_shell_send_configure(resource, 0,
3100 surface_resource,
3101 surface->output->width,
3102 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003103
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02003104 sh_output->background_surface = surface;
3105
3106 sh_output->background_surface_listener.notify =
3107 handle_background_surface_destroy;
3108 wl_signal_add(&surface->destroy_signal,
3109 &sh_output->background_surface_listener);
3110 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003111}
3112
Pekka Paalanen8274d902014-08-06 19:36:51 +03003113static int
3114panel_get_label(struct weston_surface *surface, char *buf, size_t len)
3115{
3116 return snprintf(buf, len, "panel for output %s",
Miguel A. Vico620f68d2019-09-25 11:23:13 -07003117 (surface->output ? surface->output->name : "NULL"));
Pekka Paalanen8274d902014-08-06 19:36:51 +03003118}
3119
Kristian Høgsberg75840622011-09-06 13:48:16 -04003120static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003121panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003122{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003123 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003124 struct weston_view *view;
Quentin Glidice8bf9592016-06-23 18:55:20 +02003125 int width, height;
3126 int x = 0, y = 0;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003127
Jason Ekstranda7af7042013-10-12 22:38:11 -05003128 view = container_of(es->views.next, struct weston_view, surface_link);
3129
Quentin Glidice8bf9592016-06-23 18:55:20 +02003130 get_panel_size(shell, view, &width, &height);
3131 switch (shell->panel_position) {
3132 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
3133 break;
3134 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
3135 y = view->output->height - height;
3136 break;
3137 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
3138 break;
3139 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
3140 x = view->output->width - width;
3141 break;
3142 }
3143
3144 configure_static_view(view, &shell->panel_layer, x, y);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003145}
3146
3147static void
David Fort50d962f2016-06-09 17:55:52 +02003148handle_panel_surface_destroy(struct wl_listener *listener, void *data)
3149{
3150 struct shell_output *output =
3151 container_of(listener, struct shell_output, panel_surface_listener);
3152
3153 weston_log("panel surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09003154 wl_list_remove(&output->panel_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02003155 output->panel_surface = NULL;
3156}
3157
3158
3159static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003160desktop_shell_set_panel(struct wl_client *client,
3161 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003162 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003163 struct wl_resource *surface_resource)
3164{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003165 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003166 struct weston_surface *surface =
3167 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003168 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02003169 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003170
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003171 if (surface->committed) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003172 wl_resource_post_error(surface_resource,
3173 WL_DISPLAY_ERROR_INVALID_OBJECT,
3174 "surface role already assigned");
3175 return;
3176 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003177
Jason Ekstranda7af7042013-10-12 22:38:11 -05003178 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3179 weston_view_destroy(view);
3180 view = weston_view_create(surface);
3181
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003182 surface->committed = panel_committed;
3183 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003184 weston_surface_set_label_func(surface, panel_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03003185 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02003186 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02003187
3188 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003189 if (sh_output->panel_surface) {
3190 /* The output already has a panel, tell our helper
3191 * there is no need for another one. */
3192 weston_desktop_shell_send_configure(resource, 0,
3193 surface_resource,
3194 0, 0);
3195 } else {
3196 weston_desktop_shell_send_configure(resource, 0,
3197 surface_resource,
3198 surface->output->width,
3199 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003200
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003201 sh_output->panel_surface = surface;
3202
3203 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
3204 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
3205 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003206}
3207
Pekka Paalanen8274d902014-08-06 19:36:51 +03003208static int
3209lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
3210{
3211 return snprintf(buf, len, "lock window");
3212}
3213
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003214static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003215lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003216{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003217 struct desktop_shell *shell = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003218 struct weston_view *view;
3219
3220 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003221
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003222 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003223 return;
3224
Jason Ekstranda7af7042013-10-12 22:38:11 -05003225 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003226
3227 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003228 weston_layer_entry_insert(&shell->lock_layer.view_list,
3229 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003230 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003231 surface->is_mapped = true;
3232 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003233 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003234 }
3235}
3236
3237static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003238handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003239{
Tiago Vignattibe143262012-04-16 17:31:41 +03003240 struct desktop_shell *shell =
3241 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003242
Martin Minarik6d118362012-06-07 18:01:59 +02003243 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003244 shell->lock_surface = NULL;
3245}
3246
3247static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003248desktop_shell_set_lock_surface(struct wl_client *client,
3249 struct wl_resource *resource,
3250 struct wl_resource *surface_resource)
3251{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003252 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003253 struct weston_surface *surface =
3254 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003255
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003256 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003257
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003258 if (!shell->locked)
3259 return;
3260
Pekka Paalanen98262232011-12-01 10:42:22 +02003261 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003262
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003263 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003264 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003265 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003266
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003267 weston_view_create(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003268 surface->committed = lock_surface_committed;
3269 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003270 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003271}
3272
3273static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003274resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003275{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003276 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003277
Quentin Glidic82681572016-12-17 13:40:51 +01003278 weston_layer_unset_position(&shell->lock_layer);
3279
3280 if (shell->showing_input_panels)
3281 weston_layer_set_position(&shell->input_panel_layer,
3282 WESTON_LAYER_POSITION_TOP_UI);
3283 weston_layer_set_position(&shell->fullscreen_layer,
3284 WESTON_LAYER_POSITION_FULLSCREEN);
3285 weston_layer_set_position(&shell->panel_layer,
3286 WESTON_LAYER_POSITION_UI);
3287 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003288
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003289 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003290
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003291 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003292 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003293 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003294}
3295
3296static void
3297desktop_shell_unlock(struct wl_client *client,
3298 struct wl_resource *resource)
3299{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003300 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003301
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003302 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003303
3304 if (shell->locked)
3305 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003306}
3307
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003308static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003309desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003310 struct wl_resource *resource,
3311 struct wl_resource *surface_resource)
3312{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003313 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003314
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003315 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003316 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003317}
3318
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003319static void
3320desktop_shell_desktop_ready(struct wl_client *client,
3321 struct wl_resource *resource)
3322{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003323 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003324
3325 shell_fade_startup(shell);
3326}
3327
Jonny Lamb765760d2014-08-20 15:53:19 +02003328static void
3329desktop_shell_set_panel_position(struct wl_client *client,
3330 struct wl_resource *resource,
3331 uint32_t position)
3332{
3333 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3334
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003335 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3336 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3337 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3338 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02003339 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003340 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02003341 "bad position argument");
3342 return;
3343 }
3344
3345 shell->panel_position = position;
3346}
3347
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003348static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04003349 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003350 desktop_shell_set_panel,
3351 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003352 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003353 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02003354 desktop_shell_desktop_ready,
3355 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04003356};
3357
3358static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003359move_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003360 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003361{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003362 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003363 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003364 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003365
Derek Foreman8ae2db52015-07-15 13:00:36 -05003366 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003367 return;
3368
Derek Foreman8ae2db52015-07-15 13:00:36 -05003369 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003370
Pekka Paalanen01388e22013-04-25 13:57:44 +03003371 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003372 if (surface == NULL)
3373 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003374
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003375 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003376 if (shsurf == NULL ||
3377 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3378 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003379 return;
3380
Derek Foreman794fa0e2015-07-15 13:00:38 -05003381 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003382}
3383
3384static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003385maximize_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003386 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003387{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003388 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003389 struct weston_surface *surface;
3390 struct shell_surface *shsurf;
3391
3392 surface = weston_surface_get_main_surface(focus);
3393 if (surface == NULL)
3394 return;
3395
3396 shsurf = get_shell_surface(surface);
3397 if (shsurf == NULL)
3398 return;
3399
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003400 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003401}
3402
3403static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003404fullscreen_binding(struct weston_keyboard *keyboard,
3405 const struct timespec *time, uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003406{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003407 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003408 struct weston_surface *surface;
3409 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003410 bool fullscreen;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003411
3412 surface = weston_surface_get_main_surface(focus);
3413 if (surface == NULL)
3414 return;
3415
3416 shsurf = get_shell_surface(surface);
3417 if (shsurf == NULL)
3418 return;
3419
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003420 fullscreen =
3421 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003422
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003423 set_fullscreen(shsurf, !fullscreen, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003424}
3425
3426static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003427touch_move_binding(struct weston_touch *touch, const struct timespec *time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01003428{
Derek Foreman362656b2014-09-04 10:23:05 -05003429 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01003430 struct weston_surface *surface;
3431 struct shell_surface *shsurf;
3432
Derek Foreman8ae2db52015-07-15 13:00:36 -05003433 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05003434 return;
3435
Derek Foreman8ae2db52015-07-15 13:00:36 -05003436 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003437 surface = weston_surface_get_main_surface(focus);
3438 if (surface == NULL)
3439 return;
3440
3441 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003442 if (shsurf == NULL ||
3443 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3444 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Neil Robertsaba0f252013-10-03 16:43:05 +01003445 return;
3446
Derek Foremanb7674ae2015-07-15 13:00:37 -05003447 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01003448}
3449
3450static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003451resize_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003452 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003453{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003454 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003455 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003456 uint32_t edges = 0;
3457 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003458 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003459
Derek Foreman8ae2db52015-07-15 13:00:36 -05003460 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003461 return;
3462
Derek Foreman8ae2db52015-07-15 13:00:36 -05003463 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003464
Pekka Paalanen01388e22013-04-25 13:57:44 +03003465 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003466 if (surface == NULL)
3467 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003468
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003469 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003470 if (shsurf == NULL ||
3471 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3472 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003473 return;
3474
Jason Ekstranda7af7042013-10-12 22:38:11 -05003475 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003476 wl_fixed_to_int(pointer->grab_x),
3477 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05003478 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003479
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003480 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003481 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003482 else if (x < 2 * surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003483 edges |= 0;
3484 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003485 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003486
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003487 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003488 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003489 else if (y < 2 * surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003490 edges |= 0;
3491 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003492 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003493
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003494 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003495}
3496
3497static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003498surface_opacity_binding(struct weston_pointer *pointer,
3499 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003500 struct weston_pointer_axis_event *event,
3501 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003502{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003503 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003504 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003505 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003506 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003507
Pekka Paalanen01388e22013-04-25 13:57:44 +03003508 /* XXX: broken for windows containing sub-surfaces */
3509 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003510 if (surface == NULL)
3511 return;
3512
3513 shsurf = get_shell_surface(surface);
3514 if (!shsurf)
3515 return;
3516
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003517 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003518
Jason Ekstranda7af7042013-10-12 22:38:11 -05003519 if (shsurf->view->alpha > 1.0)
3520 shsurf->view->alpha = 1.0;
3521 if (shsurf->view->alpha < step)
3522 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003523
Jason Ekstranda7af7042013-10-12 22:38:11 -05003524 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003525 weston_surface_damage(surface);
3526}
3527
3528static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003529do_zoom(struct weston_seat *seat, const struct timespec *time, uint32_t key,
3530 uint32_t axis, double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003531{
Derek Foreman8aeeac82015-01-30 13:24:36 -06003532 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05003533 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003534 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003535 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003536
Derek Foreman1281a362015-07-31 16:55:32 -05003537 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06003538 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3539 return;
3540 }
3541
Scott Moreauccbf29d2012-02-22 14:21:41 -07003542 wl_list_for_each(output, &compositor->output_list, link) {
3543 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05003544 wl_fixed_to_double(pointer->x),
3545 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003546 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003547 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003548 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003549 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003550 increment = -output->zoom.increment;
3551 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003552 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003553 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003554 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003555 else
3556 increment = 0;
3557
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003558 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003559
Scott Moreaue6603982012-06-11 13:07:51 -06003560 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003561 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003562 else if (output->zoom.level > output->zoom.max_level)
3563 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05003564
3565 if (!output->zoom.active) {
3566 if (output->zoom.level <= 0.0)
3567 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05003568 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003569 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003570
Scott Moreaue6603982012-06-11 13:07:51 -06003571 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003572
Jason Ekstranda7af7042013-10-12 22:38:11 -05003573 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003574 }
3575 }
3576}
3577
Scott Moreauccbf29d2012-02-22 14:21:41 -07003578static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003579zoom_axis_binding(struct weston_pointer *pointer, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003580 struct weston_pointer_axis_event *event,
3581 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003582{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003583 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003584}
3585
3586static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003587zoom_key_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003588 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003589{
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003590 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003591}
3592
3593static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003594terminate_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003595 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003596{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003597 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003598
Pekka Paalanen052032d2019-02-06 10:44:37 +02003599 weston_compositor_exit(compositor);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003600}
3601
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003602static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003603rotate_grab_motion(struct weston_pointer_grab *grab,
3604 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003605 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003606{
3607 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003608 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003609 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003610 struct shell_surface *shsurf = rotate->base.shsurf;
Sergey Bugaev14ef2012019-02-11 22:55:09 +03003611 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003612 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003613
Jonas Ådahld2510102014-10-05 21:39:14 +02003614 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003615
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003616 if (!shsurf)
3617 return;
3618
Sergey Bugaev14ef2012019-02-11 22:55:09 +03003619 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
3620
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003621 cx = 0.5f * surface->width;
3622 cy = 0.5f * surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003623
Daniel Stone37816df2012-05-16 18:45:18 +01003624 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3625 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003626 r = sqrtf(dx * dx + dy * dy);
3627
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003628 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003629 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003630
3631 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003632 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003633 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003634
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003635 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003636 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003637
3638 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003639 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003640 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003641 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003642 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003643
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003644 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003645 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003646 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003647 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003648 wl_list_init(&shsurf->rotation.transform.link);
3649 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003650 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003651 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003652
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003653 /* We need to adjust the position of the surface
3654 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003655 cposx = shsurf->view->geometry.x + cx;
3656 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003657 dposx = rotate->center.x - cposx;
3658 dposy = rotate->center.y - cposy;
3659 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003660 weston_view_set_position(shsurf->view,
3661 shsurf->view->geometry.x + dposx,
3662 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003663 }
3664
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003665 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003666 * lazily applies the damage due to rotation update.
3667 */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003668 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003669}
3670
3671static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003672rotate_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003673 const struct timespec *time,
3674 uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003675{
3676 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003677 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003678 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003679 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003680 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003681
Daniel Stone4dbadb12012-05-30 16:31:51 +01003682 if (pointer->button_count == 0 &&
3683 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003684 if (shsurf)
3685 weston_matrix_multiply(&shsurf->rotation.rotation,
3686 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003687 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003688 free(rotate);
3689 }
3690}
3691
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003692static void
3693rotate_grab_cancel(struct weston_pointer_grab *grab)
3694{
3695 struct rotate_grab *rotate =
3696 container_of(grab, struct rotate_grab, base.grab);
3697
3698 shell_grab_end(&rotate->base);
3699 free(rotate);
3700}
3701
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003702static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003703 noop_grab_focus,
3704 rotate_grab_motion,
3705 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003706 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003707 noop_grab_axis_source,
3708 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003709 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003710};
3711
3712static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003713surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003714{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003715 struct weston_surface *surface =
3716 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003717 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003718 float dx, dy;
3719 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003720
Pekka Paalanen460099f2012-01-20 16:48:25 +02003721 rotate = malloc(sizeof *rotate);
3722 if (!rotate)
3723 return;
3724
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003725 weston_view_to_global_float(shsurf->view,
3726 surface->width * 0.5f,
3727 surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003728 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003729
Derek Foreman74de4692015-07-15 13:00:39 -05003730 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3731 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003732 r = sqrtf(dx * dx + dy * dy);
3733 if (r > 20.0f) {
3734 struct weston_matrix inverse;
3735
3736 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003737 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003738 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003739
3740 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003741 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003742 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003743 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003744 weston_matrix_init(&rotate->rotation);
3745 }
3746
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003747 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003748 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003749}
3750
3751static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003752rotate_binding(struct weston_pointer *pointer, const struct timespec *time,
3753 uint32_t button, void *data)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003754{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003755 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003756 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003757 struct shell_surface *surface;
3758
Derek Foreman8ae2db52015-07-15 13:00:36 -05003759 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003760 return;
3761
Derek Foreman8ae2db52015-07-15 13:00:36 -05003762 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003763
Pekka Paalanen01388e22013-04-25 13:57:44 +03003764 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003765 if (base_surface == NULL)
3766 return;
3767
3768 surface = get_shell_surface(base_surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003769 if (surface == NULL ||
3770 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3771 weston_desktop_surface_get_maximized(surface->desktop_surface))
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003772 return;
3773
Derek Foreman74de4692015-07-15 13:00:39 -05003774 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003775}
3776
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003777/* Move all fullscreen layers down to the current workspace and hide their
3778 * black views. The surfaces' state is set to both fullscreen and lowered,
3779 * and this is reversed when such a surface is re-configured, see
3780 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3781 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003782 * lowering_output = NULL - Lower on all outputs, else only lower on the
3783 * specified output.
3784 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003785 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003786 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003787void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003788lower_fullscreen_layer(struct desktop_shell *shell,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003789 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003790{
3791 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003792 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003793
3794 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003795 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003796 &shell->fullscreen_layer.view_list.link,
3797 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003798 struct shell_surface *shsurf = get_shell_surface(view->surface);
3799
3800 if (!shsurf)
3801 continue;
3802
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003803 /* Only lower surfaces which have lowering_output as their fullscreen
3804 * output, unless a NULL output asks for lowering on all outputs.
3805 */
3806 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3807 continue;
3808
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003809 /* We can have a non-fullscreen popup for a fullscreen surface
3810 * in the fullscreen layer. */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003811 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003812 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003813 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3814 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07003815 weston_view_damage_below(shsurf->fullscreen.black_view);
3816
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003817 }
3818
3819 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003820 weston_layer_entry_remove(&view->layer_link);
3821 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003822 weston_view_damage_below(view);
3823 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003824
3825 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003826 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003827}
3828
Stefan Agnera8da2082019-06-23 14:53:59 +02003829static struct shell_surface *get_last_child(struct shell_surface *shsurf)
3830{
3831 struct shell_surface *shsurf_child;
3832
3833 wl_list_for_each_reverse(shsurf_child, &shsurf->children_list, children_link) {
3834 if (weston_view_is_mapped(shsurf_child->view))
3835 return shsurf_child;
3836 }
3837
3838 return NULL;
3839}
3840
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003841void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003842activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003843 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003844{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003845 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003846 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003847 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003848 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003849 struct weston_surface *old_es;
Stefan Agnera8da2082019-06-23 14:53:59 +02003850 struct shell_surface *shsurf, *shsurf_child;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003851
Pekka Paalanen01388e22013-04-25 13:57:44 +03003852 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003853 shsurf = get_shell_surface(main_surface);
3854 assert(shsurf);
3855
Stefan Agnera8da2082019-06-23 14:53:59 +02003856 shsurf_child = get_last_child(shsurf);
3857 if (shsurf_child) {
3858 /* Activate last xdg child instead of parent. */
3859 activate(shell, shsurf_child->view, seat, flags);
3860 return;
3861 }
3862
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003863 /* Only demote fullscreen surfaces on the output of activated shsurf.
3864 * Leave fullscreen surfaces on unrelated outputs alone. */
Pekka Paalanen87860c22018-05-02 10:21:57 +02003865 if (shsurf->output)
3866 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003867
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003868 weston_view_activate(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003869
Jonas Ådahl8538b222012-08-29 22:13:03 +02003870 state = ensure_focus_state(shell, seat);
3871 if (state == NULL)
3872 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003873
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003874 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08003875 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003876
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003877 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3878 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02003879 shell_configure_fullscreen(shsurf);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003880
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01003881 /* Update the surface’s layer. This brings it to the top of the stacking
3882 * order as appropriate. */
3883 shell_surface_update_layer(shsurf);
3884
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003885 if (shell->focus_animation_type != ANIMATION_NONE) {
3886 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003887 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003888 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003889}
3890
Alex Wu21858432012-04-01 20:13:08 +08003891/* no-op func for checking black surface */
3892static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003893black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08003894{
3895}
3896
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003897static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003898is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08003899{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003900 struct weston_surface *surface = view->surface;
3901
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003902 if (surface->committed == black_surface_committed) {
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003903 if (fs_view)
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003904 *fs_view = surface->committed_private;
Alex Wu21858432012-04-01 20:13:08 +08003905 return true;
3906 }
3907 return false;
3908}
3909
Kristian Høgsberg75840622011-09-06 13:48:16 -04003910static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003911activate_binding(struct weston_seat *seat,
3912 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003913 struct weston_view *focus_view,
3914 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003915{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003916 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003917 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003918
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003919 if (!focus_view)
3920 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003921
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003922 if (is_black_surface_view(focus_view, &main_view))
3923 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08003924
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003925 main_surface = weston_surface_get_main_surface(focus_view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003926 if (!get_shell_surface(main_surface))
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003927 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003928
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003929 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01003930}
3931
3932static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003933click_to_activate_binding(struct weston_pointer *pointer,
3934 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003935 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003936{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003937 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003938 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003939 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08003940 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003941
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003942 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003943 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003944 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01003945}
3946
3947static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003948touch_to_activate_binding(struct weston_touch *touch,
3949 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003950 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003951{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003952 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003953 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003954 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08003955 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003956
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003957 activate_binding(touch->seat, data, touch->focus,
3958 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003959}
3960
3961static void
Neil Robertsb4a91702014-04-09 16:33:32 +01003962unfocus_all_seats(struct desktop_shell *shell)
3963{
3964 struct weston_seat *seat, *next;
3965
3966 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003967 struct weston_keyboard *keyboard =
3968 weston_seat_get_keyboard(seat);
3969
3970 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01003971 continue;
3972
Derek Foreman1281a362015-07-31 16:55:32 -05003973 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01003974 }
3975}
3976
3977static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003978lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003979{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003980 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003981
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003982 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003983 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003984 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003985 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003986
3987 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003988
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003989 /* Hide all surfaces by removing the fullscreen, panel and
3990 * toplevel layers. This way nothing else can show or receive
3991 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003992
Quentin Glidic82681572016-12-17 13:40:51 +01003993 weston_layer_unset_position(&shell->panel_layer);
3994 weston_layer_unset_position(&shell->fullscreen_layer);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003995 if (shell->showing_input_panels)
Quentin Glidic82681572016-12-17 13:40:51 +01003996 weston_layer_unset_position(&shell->input_panel_layer);
3997 weston_layer_unset_position(&ws->layer);
3998
3999 weston_layer_set_position(&shell->lock_layer,
4000 WESTON_LAYER_POSITION_LOCK);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004001
Derek Foreman004b4a12015-07-20 16:28:13 -05004002 weston_compositor_sleep(shell->compositor);
4003
Neil Robertsb4a91702014-04-09 16:33:32 +01004004 /* Remove the keyboard focus on all seats. This will be
4005 * restored to the workspace's saved state via
4006 * restore_focus_state when the compositor is unlocked */
4007 unfocus_all_seats(shell);
4008
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004009 /* TODO: disable bindings that should not work while locked. */
4010
4011 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004012}
4013
4014static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004015unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004016{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004017 struct wl_resource *shell_resource;
4018
Pekka Paalanend81c2162011-11-16 13:47:34 +02004019 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004020 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004021 return;
4022 }
4023
4024 /* If desktop-shell client has gone away, unlock immediately. */
4025 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004026 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004027 return;
4028 }
4029
4030 if (shell->prepare_event_sent)
4031 return;
4032
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004033 shell_resource = shell->child.desktop_shell;
4034 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004035 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004036}
4037
4038static void
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004039shell_fade_done_for_output(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004040{
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004041 struct shell_output *shell_output = data;
4042 struct desktop_shell *shell = shell_output->shell;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004043
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004044 shell_output->fade.animation = NULL;
4045 switch (shell_output->fade.type) {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004046 case FADE_IN:
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004047 weston_surface_destroy(shell_output->fade.view->surface);
4048 shell_output->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004049 break;
4050 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004051 lock(shell);
4052 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004053 default:
4054 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004055 }
4056}
4057
Jason Ekstranda7af7042013-10-12 22:38:11 -05004058static struct weston_view *
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004059shell_fade_create_surface_for_output(struct desktop_shell *shell, struct shell_output *shell_output)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004060{
4061 struct weston_compositor *compositor = shell->compositor;
4062 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004063 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004064
4065 surface = weston_surface_create(compositor);
4066 if (!surface)
4067 return NULL;
4068
Jason Ekstranda7af7042013-10-12 22:38:11 -05004069 view = weston_view_create(surface);
4070 if (!view) {
4071 weston_surface_destroy(surface);
4072 return NULL;
4073 }
4074
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004075 weston_surface_set_size(surface, shell_output->output->width, shell_output->output->height);
4076 weston_view_set_position(view, shell_output->output->x, shell_output->output->y);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004077 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004078 weston_layer_entry_insert(&compositor->fade_layer.view_list,
4079 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004080 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02004081 surface->is_mapped = true;
4082 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004083
Jason Ekstranda7af7042013-10-12 22:38:11 -05004084 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004085}
4086
4087static void
4088shell_fade(struct desktop_shell *shell, enum fade_type type)
4089{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004090 float tint;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004091 struct shell_output *shell_output;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004092
4093 switch (type) {
4094 case FADE_IN:
4095 tint = 0.0;
4096 break;
4097 case FADE_OUT:
4098 tint = 1.0;
4099 break;
4100 default:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07004101 weston_log("shell: invalid fade type\n");
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004102 return;
4103 }
4104
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004105 /* Create a separate fade surface for each output */
4106 wl_list_for_each(shell_output, &shell->output_list, link) {
4107 shell_output->fade.type = type;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004108
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004109 if (shell_output->fade.view == NULL) {
4110 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4111 if (!shell_output->fade.view)
4112 continue;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004113
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004114 shell_output->fade.view->alpha = 1.0 - tint;
4115 weston_view_update_transform(shell_output->fade.view);
4116 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004117
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004118 if (shell_output->fade.view->output == NULL) {
4119 /* If the black view gets a NULL output, we lost the
4120 * last output and we'll just cancel the fade. This
4121 * happens when you close the last window under the
4122 * X11 or Wayland backends. */
4123 shell->locked = false;
4124 weston_surface_destroy(shell_output->fade.view->surface);
4125 shell_output->fade.view = NULL;
4126 } else if (shell_output->fade.animation) {
4127 weston_fade_update(shell_output->fade.animation, tint);
4128 } else {
4129 shell_output->fade.animation =
4130 weston_fade_run(shell_output->fade.view,
4131 1.0 - tint, tint, 300.0,
4132 shell_fade_done_for_output, shell_output);
4133 }
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004134 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004135}
4136
4137static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004138do_shell_fade_startup(void *data)
4139{
4140 struct desktop_shell *shell = data;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004141 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004142
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004143 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004144 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004145 } else {
4146 weston_log("desktop shell: "
4147 "unexpected fade-in animation type %d\n",
4148 shell->startup_animation_type);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004149 wl_list_for_each(shell_output, &shell->output_list, link) {
4150 weston_surface_destroy(shell_output->fade.view->surface);
4151 shell_output->fade.view = NULL;
4152 }
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004153 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004154}
4155
4156static void
4157shell_fade_startup(struct desktop_shell *shell)
4158{
4159 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004160 struct shell_output *shell_output;
4161 bool has_fade = false;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004162
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004163 wl_list_for_each(shell_output, &shell->output_list, link) {
4164 if (!shell_output->fade.startup_timer)
4165 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004166
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004167 wl_event_source_remove(shell_output->fade.startup_timer);
4168 shell_output->fade.startup_timer = NULL;
4169 has_fade = true;
4170 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004171
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004172 if (has_fade) {
4173 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4174 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4175 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004176}
4177
4178static int
4179fade_startup_timeout(void *data)
4180{
4181 struct desktop_shell *shell = data;
4182
4183 shell_fade_startup(shell);
4184 return 0;
4185}
4186
4187static void
4188shell_fade_init(struct desktop_shell *shell)
4189{
4190 /* Make compositor output all black, and wait for the desktop-shell
4191 * client to signal it is ready, then fade in. The timer triggers a
4192 * fade-in, in case the desktop-shell client takes too long.
4193 */
4194
4195 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004196 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004197
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004198 if (shell->startup_animation_type == ANIMATION_NONE)
4199 return;
4200
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004201 wl_list_for_each(shell_output, &shell->output_list, link) {
4202 if (shell_output->fade.view != NULL) {
4203 weston_log("%s: warning: fade surface already exists\n",
4204 __func__);
4205 continue;
4206 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004207
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004208 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4209 if (!shell_output->fade.view)
4210 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004211
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004212 weston_view_update_transform(shell_output->fade.view);
4213 weston_surface_damage(shell_output->fade.view->surface);
4214
4215 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4216 shell_output->fade.startup_timer =
4217 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4218 wl_event_source_timer_update(shell_output->fade.startup_timer, 15000);
4219 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004220}
4221
4222static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004223idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004224{
4225 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004226 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004227
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004228 struct weston_seat *seat;
4229
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004230 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4231 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004232
4233 shell_fade(shell, FADE_OUT);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004234 /* lock() is called from shell_fade_done_for_output() */
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004235}
4236
4237static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004238wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004239{
4240 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004241 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004242
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004243 unlock(shell);
4244}
4245
4246static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004247transform_handler(struct wl_listener *listener, void *data)
4248{
4249 struct weston_surface *surface = data;
4250 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004251 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004252 int x, y;
4253
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004254 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004255 return;
4256
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004257 api = shsurf->shell->xwayland_surface_api;
4258 if (!api) {
4259 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
4260 shsurf->shell->xwayland_surface_api = api;
4261 }
4262
4263 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004264 return;
4265
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004266 if (!weston_view_is_mapped(shsurf->view))
4267 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004268
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004269 x = shsurf->view->geometry.x;
4270 y = shsurf->view->geometry.y;
4271
4272 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004273}
4274
4275static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004276center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004277{
Giulio Camuffob8366642013-04-25 13:57:46 +03004278 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004279 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004280
Pekka Paalanen30aa5972018-05-02 10:21:56 +02004281 if (!output) {
4282 weston_view_set_position(view, 0, 0);
4283 return;
4284 }
4285
Jason Ekstranda7af7042013-10-12 22:38:11 -05004286 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004287
4288 x = output->x + (output->width - width) / 2 - surf_x / 2;
4289 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004290
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004291 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004292}
4293
4294static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004295weston_view_set_initial_position(struct weston_view *view,
4296 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004297{
4298 struct weston_compositor *compositor = shell->compositor;
4299 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004300 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004301 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004302 struct weston_output *output, *target_output = NULL;
4303 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004304 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004305
4306 /* As a heuristic place the new window on the same output as the
4307 * pointer. Falling back to the output containing 0, 0.
4308 *
4309 * TODO: Do something clever for touch too?
4310 */
4311 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004312 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4313
4314 if (pointer) {
4315 ix = wl_fixed_to_int(pointer->x);
4316 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004317 break;
4318 }
4319 }
4320
4321 wl_list_for_each(output, &compositor->output_list, link) {
4322 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4323 target_output = output;
4324 break;
4325 }
4326 }
4327
4328 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004329 weston_view_set_position(view, 10 + random() % 400,
4330 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004331 return;
4332 }
4333
4334 /* Valid range within output where the surface will still be onscreen.
4335 * If this is negative it means that the surface is bigger than
4336 * output.
4337 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004338 get_output_work_area(shell, target_output, &area);
4339
Derek Foremanf814c5d2015-04-15 12:23:54 -05004340 x = area.x;
4341 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004342 range_x = area.width - view->surface->width;
4343 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004344
Rob Bradford4cb88c72012-08-13 15:18:44 +01004345 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004346 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004347
4348 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004349 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004350
Jason Ekstranda7af7042013-10-12 22:38:11 -05004351 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004352}
4353
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004354static bool
4355check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4356{
4357 struct timespec now;
4358
4359 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4360 return false;
4361
4362 /*
4363 * If the shell helper client dies before the session has been
4364 * up for roughly 30 seconds, better just make Weston shut down,
4365 * because the user likely has no way to interact with the desktop
4366 * anyway.
4367 */
4368 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4369 weston_log("Error: %s apparently cannot run at all.\n",
4370 shell->client);
4371 weston_log_continue(STAMP_SPACE "Quitting...");
Pekka Paalanen052032d2019-02-06 10:44:37 +02004372 weston_compositor_exit_with_code(shell->compositor,
4373 EXIT_FAILURE);
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004374
4375 return true;
4376 }
4377
4378 return false;
4379}
4380
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004381static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004382
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004383static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004384respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004385{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004386 struct timespec time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004387
4388 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004389 weston_compositor_get_time(&time);
4390 if (timespec_sub_to_msec(&time, &shell->child.deathstamp) > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004391 shell->child.deathstamp = time;
4392 shell->child.deathcount = 0;
4393 }
4394
4395 shell->child.deathcount++;
4396 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004397 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004398 return;
4399 }
4400
Pekka Paalanen826dc142014-08-27 12:11:53 +03004401 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004402 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004403}
4404
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004405static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004406desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4407{
4408 struct desktop_shell *shell;
4409
4410 shell = container_of(listener, struct desktop_shell,
4411 child.client_destroy_listener);
4412
Pekka Paalanen826dc142014-08-27 12:11:53 +03004413 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004414 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004415 /*
4416 * unbind_desktop_shell() will reset shell->child.desktop_shell
4417 * before the respawned process has a chance to create a new
4418 * desktop_shell object, because we are being called from the
4419 * wl_client destructor which destroys all wl_resources before
4420 * returning.
4421 */
4422
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004423 if (!check_desktop_shell_crash_too_early(shell))
4424 respawn_desktop_shell_process(shell);
4425
Pekka Paalanen826dc142014-08-27 12:11:53 +03004426 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004427}
4428
4429static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004430launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004431{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004432 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004433
Pekka Paalanen826dc142014-08-27 12:11:53 +03004434 shell->child.client = weston_client_start(shell->compositor,
4435 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004436
Arnaud Vrac42631192014-08-25 20:56:46 +02004437 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004438 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004439 return;
4440 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004441
4442 shell->child.client_destroy_listener.notify =
4443 desktop_shell_client_destroy;
4444 wl_client_add_destroy_listener(shell->child.client,
4445 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004446}
4447
4448static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004449unbind_desktop_shell(struct wl_resource *resource)
4450{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004451 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004452
4453 if (shell->locked)
4454 resume_desktop(shell);
4455
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004456 shell->child.desktop_shell = NULL;
4457 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004458}
4459
4460static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004461bind_desktop_shell(struct wl_client *client,
4462 void *data, uint32_t version, uint32_t id)
4463{
Tiago Vignattibe143262012-04-16 17:31:41 +03004464 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004465 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004466
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004467 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4468 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004469
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004470 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004471 wl_resource_set_implementation(resource,
4472 &desktop_shell_implementation,
4473 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004474 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004475 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004476 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004477
4478 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4479 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004480}
4481
Kristian Høgsberg07045392012-02-19 18:52:44 -05004482struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004483 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004484 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004485 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004486 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004487 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004488};
4489
4490static void
4491switcher_next(struct switcher *switcher)
4492{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004493 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004494 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004495 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004496 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004497
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004498 /* temporary re-display minimized surfaces */
4499 struct weston_view *tmp;
4500 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004501 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4502 weston_layer_entry_remove(&view->layer_link);
4503 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004504 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4505 *minimized = view;
4506 }
4507
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004508 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004509 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004510 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004511 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004512 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004513 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004514 next = view;
4515 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004516 view->alpha = 0.25;
4517 weston_view_geometry_dirty(view);
4518 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004519 }
Alex Wu1659daa2012-04-01 20:13:09 +08004520
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004521 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004522 view->alpha = 0.25;
4523 weston_view_geometry_dirty(view);
4524 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004525 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004526 }
4527
4528 if (next == NULL)
4529 next = first;
4530
Alex Wu07b26062012-03-12 16:06:01 +08004531 if (next == NULL)
4532 return;
4533
Kristian Høgsberg07045392012-02-19 18:52:44 -05004534 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004535 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004536
4537 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004538 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004539 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004540
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004541 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004542 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004543 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004544}
4545
4546static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004547switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004548{
4549 struct switcher *switcher =
4550 container_of(listener, struct switcher, listener);
4551
4552 switcher_next(switcher);
4553}
4554
4555static void
Daniel Stone351eb612012-05-31 15:27:47 -04004556switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004557{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004558 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004559 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004560 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004561
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004562 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004563 if (is_focus_view(view))
4564 continue;
4565
Jason Ekstranda7af7042013-10-12 22:38:11 -05004566 view->alpha = 1.0;
4567 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004568 }
4569
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004570 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004571 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004572 keyboard->seat,
4573 WESTON_ACTIVATE_FLAG_CONFIGURE);
4574 }
4575
Kristian Høgsberg07045392012-02-19 18:52:44 -05004576 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004577 weston_keyboard_end_grab(keyboard);
4578 if (keyboard->input_method_resource)
4579 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004580
4581 /* re-hide surfaces that were temporary shown during the switch */
4582 struct weston_view **minimized;
4583 wl_array_for_each(minimized, &switcher->minimized_array) {
4584 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004585 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004586 weston_layer_entry_remove(&(*minimized)->layer_link);
4587 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004588 weston_view_damage_below(*minimized);
4589 }
4590 }
4591 wl_array_release(&switcher->minimized_array);
4592
Kristian Høgsberg07045392012-02-19 18:52:44 -05004593 free(switcher);
4594}
4595
4596static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004597switcher_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004598 const struct timespec *time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004599{
4600 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004601 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004602
Daniel Stonec9785ea2012-05-30 16:31:52 +01004603 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004604 switcher_next(switcher);
4605}
4606
4607static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004608switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004609 uint32_t mods_depressed, uint32_t mods_latched,
4610 uint32_t mods_locked, uint32_t group)
4611{
4612 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004613 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004614
Daniel Stone351eb612012-05-31 15:27:47 -04004615 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4616 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004617}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004618
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004619static void
4620switcher_cancel(struct weston_keyboard_grab *grab)
4621{
4622 struct switcher *switcher = container_of(grab, struct switcher, grab);
4623
4624 switcher_destroy(switcher);
4625}
4626
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004627static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004628 switcher_key,
4629 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004630 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004631};
4632
4633static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004634switcher_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004635 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004636{
Tiago Vignattibe143262012-04-16 17:31:41 +03004637 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004638 struct switcher *switcher;
4639
4640 switcher = malloc(sizeof *switcher);
ganjingc1e71512020-07-28 15:28:45 +08004641 if (!switcher)
4642 return;
Pekka Paalanen4bb326b2021-05-14 14:37:46 +03004643
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004644 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004645 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004646 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004647 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004648 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004649
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004650 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004651 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004652 weston_keyboard_start_grab(keyboard, &switcher->grab);
4653 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004654 switcher_next(switcher);
4655}
4656
Pekka Paalanen3c647232011-12-22 13:43:43 +02004657static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004658backlight_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004659 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004660{
4661 struct weston_compositor *compositor = data;
4662 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004663 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004664
4665 /* TODO: we're limiting to simple use cases, where we assume just
4666 * control on the primary display. We'd have to extend later if we
4667 * ever get support for setting backlights on random desktop LCD
4668 * panels though */
4669 output = get_default_output(compositor);
4670 if (!output)
4671 return;
4672
4673 if (!output->set_backlight)
4674 return;
4675
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004676 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4677 backlight_new = output->backlight_current - 25;
4678 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4679 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004680
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004681 if (backlight_new < 5)
4682 backlight_new = 5;
4683 if (backlight_new > 255)
4684 backlight_new = 255;
4685
4686 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004687 output->set_backlight(output, output->backlight_current);
4688}
4689
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004690static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004691force_kill_binding(struct weston_keyboard *keyboard,
4692 const struct timespec *time, uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004693{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004694 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004695 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004696 struct desktop_shell *shell = data;
4697 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004698 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004699
Derek Foreman8ae2db52015-07-15 13:00:36 -05004700 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004701 if (!focus_surface)
4702 return;
4703
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004704 wl_signal_emit(&compositor->kill_signal, focus_surface);
4705
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004706 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004707 wl_client_get_credentials(client, &pid, NULL, NULL);
4708
4709 /* Skip clients that we launched ourselves (the credentials of
4710 * the socketpair is ours) */
4711 if (pid == getpid())
4712 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004713
Daniel Stone325fc2d2012-05-30 16:31:58 +01004714 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004715}
4716
4717static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004718workspace_up_binding(struct weston_keyboard *keyboard,
4719 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004720{
4721 struct desktop_shell *shell = data;
4722 unsigned int new_index = shell->workspaces.current;
4723
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004724 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004725 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004726 if (new_index != 0)
4727 new_index--;
4728
4729 change_workspace(shell, new_index);
4730}
4731
4732static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004733workspace_down_binding(struct weston_keyboard *keyboard,
4734 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004735{
4736 struct desktop_shell *shell = data;
4737 unsigned int new_index = shell->workspaces.current;
4738
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004739 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004740 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004741 if (new_index < shell->workspaces.num - 1)
4742 new_index++;
4743
4744 change_workspace(shell, new_index);
4745}
4746
4747static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004748workspace_f_binding(struct weston_keyboard *keyboard,
4749 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004750{
4751 struct desktop_shell *shell = data;
4752 unsigned int new_index;
4753
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004754 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004755 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004756 new_index = key - KEY_F1;
4757 if (new_index >= shell->workspaces.num)
4758 new_index = shell->workspaces.num - 1;
4759
4760 change_workspace(shell, new_index);
4761}
4762
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004763static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004764workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004765 const struct timespec *time, uint32_t key,
4766 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004767{
4768 struct desktop_shell *shell = data;
4769 unsigned int new_index = shell->workspaces.current;
4770
4771 if (shell->locked)
4772 return;
4773
4774 if (new_index != 0)
4775 new_index--;
4776
Derek Foreman8ae2db52015-07-15 13:00:36 -05004777 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004778}
4779
4780static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004781workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004782 const struct timespec *time, uint32_t key,
4783 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004784{
4785 struct desktop_shell *shell = data;
4786 unsigned int new_index = shell->workspaces.current;
4787
4788 if (shell->locked)
4789 return;
4790
4791 if (new_index < shell->workspaces.num - 1)
4792 new_index++;
4793
Derek Foreman8ae2db52015-07-15 13:00:36 -05004794 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004795}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004796
4797static void
Harish Krupodc3edc02019-04-20 17:50:18 +05304798shell_reposition_view_on_output_change(struct weston_view *view)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004799{
4800 struct weston_output *output, *first_output;
4801 struct weston_compositor *ec = view->surface->compositor;
4802 struct shell_surface *shsurf;
4803 float x, y;
4804 int visible;
4805
Harish Krupo2dff4dd2019-04-20 18:10:56 +05304806 if (wl_list_empty(&ec->output_list))
4807 return;
4808
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004809 x = view->geometry.x;
4810 y = view->geometry.y;
4811
4812 /* At this point the destroyed output is not in the list anymore.
4813 * If the view is still visible somewhere, we leave where it is,
4814 * otherwise, move it to the first output. */
4815 visible = 0;
4816 wl_list_for_each(output, &ec->output_list, link) {
4817 if (pixman_region32_contains_point(&output->region,
4818 x, y, NULL)) {
4819 visible = 1;
4820 break;
4821 }
4822 }
4823
4824 if (!visible) {
4825 first_output = container_of(ec->output_list.next,
4826 struct weston_output, link);
4827
4828 x = first_output->x + first_output->width / 4;
4829 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004830
4831 weston_view_set_position(view, x, y);
4832 } else {
4833 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004834 }
4835
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004836
4837 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004838 if (!shsurf)
4839 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004840
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004841 shsurf->saved_position_valid = false;
4842 set_maximized(shsurf, false);
4843 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004844}
4845
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004846void
4847shell_for_each_layer(struct desktop_shell *shell,
4848 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004849{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004850 struct workspace **ws;
4851
4852 func(shell, &shell->fullscreen_layer, data);
4853 func(shell, &shell->panel_layer, data);
4854 func(shell, &shell->background_layer, data);
4855 func(shell, &shell->lock_layer, data);
4856 func(shell, &shell->input_panel_layer, data);
4857
4858 wl_array_for_each(ws, &shell->workspaces.array)
4859 func(shell, &(*ws)->layer, data);
4860}
4861
4862static void
Harish Krupodc3edc02019-04-20 17:50:18 +05304863shell_output_changed_move_layer(struct desktop_shell *shell,
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004864 struct weston_layer *layer,
4865 void *data)
4866{
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004867 struct weston_view *view;
4868
Marius Vladea40d6d2018-02-08 18:46:42 +02004869 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Harish Krupodc3edc02019-04-20 17:50:18 +05304870 shell_reposition_view_on_output_change(view);
4871
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004872}
4873
4874static void
Pekka Paalanen719ca872021-05-24 16:53:40 +03004875shell_output_destroy(struct shell_output *shell_output)
Xiong Zhang6b481422013-10-23 13:58:32 +08004876{
Pekka Paalanen0d5e4ff2021-05-24 16:13:21 +03004877 struct desktop_shell *shell = shell_output->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004878
Harish Krupodc3edc02019-04-20 17:50:18 +05304879 shell_for_each_layer(shell, shell_output_changed_move_layer, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004880
Pekka Paalanen719ca872021-05-24 16:53:40 +03004881 if (shell_output->fade.animation) {
4882 weston_view_animation_destroy(shell_output->fade.animation);
4883 shell_output->fade.animation = NULL;
4884 }
4885
4886 if (shell_output->fade.view) {
4887 /* destroys the view as well */
4888 weston_surface_destroy(shell_output->fade.view->surface);
4889 }
4890
4891 if (shell_output->fade.startup_timer)
4892 wl_event_source_remove(shell_output->fade.startup_timer);
4893
Pekka Paalanen0d5e4ff2021-05-24 16:13:21 +03004894 if (shell_output->panel_surface)
4895 wl_list_remove(&shell_output->panel_surface_listener.link);
4896 if (shell_output->background_surface)
4897 wl_list_remove(&shell_output->background_surface_listener.link);
4898 wl_list_remove(&shell_output->destroy_listener.link);
4899 wl_list_remove(&shell_output->link);
4900 free(shell_output);
Xiong Zhang6b481422013-10-23 13:58:32 +08004901}
4902
4903static void
Pekka Paalanen719ca872021-05-24 16:53:40 +03004904handle_output_destroy(struct wl_listener *listener, void *data)
4905{
4906 struct shell_output *shell_output =
4907 container_of(listener, struct shell_output, destroy_listener);
4908
4909 shell_output_destroy(shell_output);
4910}
4911
4912static void
David Fort50d962f2016-06-09 17:55:52 +02004913shell_resize_surface_to_output(struct desktop_shell *shell,
4914 struct weston_surface *surface,
4915 const struct weston_output *output)
4916{
4917 if (!surface)
4918 return;
4919
4920 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4921 surface->resource,
4922 output->width,
4923 output->height);
4924}
4925
4926
4927static void
4928handle_output_resized(struct wl_listener *listener, void *data)
4929{
4930 struct desktop_shell *shell =
4931 container_of(listener, struct desktop_shell, resized_listener);
4932 struct weston_output *output = (struct weston_output *)data;
4933 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4934
4935 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4936 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4937}
4938
4939static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004940create_shell_output(struct desktop_shell *shell,
4941 struct weston_output *output)
4942{
4943 struct shell_output *shell_output;
4944
4945 shell_output = zalloc(sizeof *shell_output);
4946 if (shell_output == NULL)
4947 return;
4948
4949 shell_output->output = output;
4950 shell_output->shell = shell;
4951 shell_output->destroy_listener.notify = handle_output_destroy;
4952 wl_signal_add(&output->destroy_signal,
4953 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004954 wl_list_insert(shell->output_list.prev, &shell_output->link);
Harish Krupodc3edc02019-04-20 17:50:18 +05304955
4956 if (wl_list_length(&shell->output_list) == 1)
4957 shell_for_each_layer(shell,
4958 shell_output_changed_move_layer, NULL);
Xiong Zhang6b481422013-10-23 13:58:32 +08004959}
4960
4961static void
4962handle_output_create(struct wl_listener *listener, void *data)
4963{
4964 struct desktop_shell *shell =
4965 container_of(listener, struct desktop_shell, output_create_listener);
4966 struct weston_output *output = (struct weston_output *)data;
4967
4968 create_shell_output(shell, output);
4969}
4970
4971static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004972handle_output_move_layer(struct desktop_shell *shell,
4973 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004974{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004975 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004976 struct weston_view *view;
4977 float x, y;
4978
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004979 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004980 if (view->output != output)
4981 continue;
4982
4983 x = view->geometry.x + output->move_x;
4984 y = view->geometry.y + output->move_y;
4985 weston_view_set_position(view, x, y);
4986 }
4987}
4988
4989static void
4990handle_output_move(struct wl_listener *listener, void *data)
4991{
4992 struct desktop_shell *shell;
4993
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004994 shell = container_of(listener, struct desktop_shell,
4995 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004996
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004997 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004998}
4999
5000static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005001setup_output_destroy_handler(struct weston_compositor *ec,
5002 struct desktop_shell *shell)
5003{
5004 struct weston_output *output;
5005
5006 wl_list_init(&shell->output_list);
5007 wl_list_for_each(output, &ec->output_list, link)
5008 create_shell_output(shell, output);
5009
5010 shell->output_create_listener.notify = handle_output_create;
5011 wl_signal_add(&ec->output_created_signal,
5012 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005013
5014 shell->output_move_listener.notify = handle_output_move;
5015 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08005016}
5017
5018static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005019shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005020{
Tiago Vignattibe143262012-04-16 17:31:41 +03005021 struct desktop_shell *shell =
5022 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005023 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005024 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005025
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005026 /* Force state to unlocked so we don't try to fade */
5027 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005028
5029 if (shell->child.client) {
5030 /* disable respawn */
5031 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005032 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03005033 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005034
Harsha M Mb8b2c722018-08-07 19:05:02 +05305035 wl_list_remove(&shell->destroy_listener.link);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005036 wl_list_remove(&shell->idle_listener.link);
5037 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005038 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005039
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03005040 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005041 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005042
Pekka Paalanen719ca872021-05-24 16:53:40 +03005043 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link)
5044 shell_output_destroy(shell_output);
Xiong Zhang6b481422013-10-23 13:58:32 +08005045
5046 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07005047 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02005048 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005049
Pekka Paalanenabd72922021-05-24 14:42:00 +03005050 weston_desktop_destroy(shell->desktop);
5051
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005052 wl_array_for_each(ws, &shell->workspaces.array)
5053 workspace_destroy(*ws);
5054 wl_array_release(&shell->workspaces.array);
5055
Pekka Paalanen4bb326b2021-05-14 14:37:46 +03005056 weston_layer_fini(&shell->fullscreen_layer);
5057 weston_layer_fini(&shell->panel_layer);
5058 weston_layer_fini(&shell->background_layer);
5059 weston_layer_fini(&shell->lock_layer);
5060 weston_layer_fini(&shell->input_panel_layer);
5061 weston_layer_fini(&shell->minimized_layer);
5062
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005063 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005064 free(shell);
5065}
5066
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005067static void
5068shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5069{
5070 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005071 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005072
Bob Ham744e6532016-01-12 10:21:48 +00005073 if (shell->allow_zap)
5074 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5075 MODIFIER_CTRL | MODIFIER_ALT,
5076 terminate_binding, ec);
5077
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005078 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005079 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5080 click_to_activate_binding,
5081 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07005082 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
5083 click_to_activate_binding,
5084 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005085 weston_compositor_add_touch_binding(ec, 0,
5086 touch_to_activate_binding,
5087 shell);
Bob Ham553d1242016-01-12 10:21:49 +00005088 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5089 backlight_binding, ec);
5090 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5091 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005092
5093 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00005094 if (shell->exposay_modifier)
5095 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5096 exposay_binding, shell);
5097
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005098 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00005099 if (!mod)
5100 return;
5101
Ian Ray13404c42017-09-18 15:22:01 +03005102 /* This binding is not configurable, but is only enabled if there is a
5103 * valid binding modifier. */
5104 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5105 MODIFIER_SUPER | MODIFIER_ALT,
5106 surface_opacity_binding, NULL);
5107
Ian Rayab7c0b62017-09-18 15:22:00 +03005108 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5109 mod, zoom_axis_binding,
5110 NULL);
5111
Daniel Stone325fc2d2012-05-30 16:31:58 +01005112 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5113 zoom_key_binding, NULL);
5114 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5115 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005116 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5117 maximize_binding, NULL);
5118 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5119 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005120 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5121 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005122 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05005123 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005124 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08005125 weston_compositor_add_button_binding(ec, BTN_LEFT,
5126 mod | MODIFIER_SHIFT,
5127 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005128
5129 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05005130 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005131 rotate_binding, NULL);
5132
Daniel Stone325fc2d2012-05-30 16:31:58 +01005133 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5134 shell);
5135 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5136 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005137 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5138 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005139 weston_compositor_add_key_binding(ec, KEY_K, mod,
5140 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005141 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5142 workspace_up_binding, shell);
5143 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5144 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005145 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5146 workspace_move_surface_up_binding,
5147 shell);
5148 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5149 workspace_move_surface_down_binding,
5150 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005151
5152 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5153 if (shell->workspaces.num > 1) {
5154 num_workspace_bindings = shell->workspaces.num;
5155 if (num_workspace_bindings > 6)
5156 num_workspace_bindings = 6;
5157 for (i = 0; i < num_workspace_bindings; i++)
5158 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5159 workspace_f_binding,
5160 shell);
5161 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005162
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02005163 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005164}
5165
Jason Ekstrand024177c2014-04-21 19:42:58 -05005166static void
5167handle_seat_created(struct wl_listener *listener, void *data)
5168{
5169 struct weston_seat *seat = data;
5170
5171 create_shell_seat(seat);
5172}
5173
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005174WL_EXPORT int
Quentin Glidicda01c1d2016-12-02 14:17:08 +01005175wet_shell_init(struct weston_compositor *ec,
5176 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005177{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005178 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005179 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005180 struct workspace **pws;
5181 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005182 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005183
Peter Huttererf3d62272013-08-08 11:57:05 +10005184 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005185 if (shell == NULL)
5186 return -1;
5187
Kristian Høgsberg75840622011-09-06 13:48:16 -04005188 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005189
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02005190 if (!weston_compositor_add_destroy_listener_once(ec,
5191 &shell->destroy_listener,
5192 shell_destroy)) {
5193 free(shell);
5194 return 0;
5195 }
5196
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005197 shell->idle_listener.notify = idle_handler;
5198 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5199 shell->wake_listener.notify = wake_handler;
5200 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005201 shell->transform_listener.notify = transform_handler;
5202 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005203
Quentin Glidic82681572016-12-17 13:40:51 +01005204 weston_layer_init(&shell->fullscreen_layer, ec);
5205 weston_layer_init(&shell->panel_layer, ec);
5206 weston_layer_init(&shell->background_layer, ec);
5207 weston_layer_init(&shell->lock_layer, ec);
5208 weston_layer_init(&shell->input_panel_layer, ec);
5209
5210 weston_layer_set_position(&shell->fullscreen_layer,
5211 WESTON_LAYER_POSITION_FULLSCREEN);
5212 weston_layer_set_position(&shell->panel_layer,
5213 WESTON_LAYER_POSITION_UI);
5214 weston_layer_set_position(&shell->background_layer,
5215 WESTON_LAYER_POSITION_BACKGROUND);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005216
5217 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005218 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005219
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005220 if (input_panel_setup(shell) < 0)
5221 return -1;
5222
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03005223 shell->text_backend = text_backend_init(ec);
5224 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00005225 return -1;
5226
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005227 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005228
Daniel Stonedf8133b2013-11-19 11:37:14 +01005229 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5230 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5231
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005232 for (i = 0; i < shell->workspaces.num; i++) {
5233 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5234 if (pws == NULL)
5235 return -1;
5236
Quentin Glidic82681572016-12-17 13:40:51 +01005237 *pws = workspace_create(shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005238 if (*pws == NULL)
5239 return -1;
5240 }
5241 activate_workspace(shell, 0);
5242
Quentin Glidic82681572016-12-17 13:40:51 +01005243 weston_layer_init(&shell->minimized_layer, ec);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01005244
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005245 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005246 wl_list_init(&shell->workspaces.animation.link);
5247 shell->workspaces.animation.frame = animate_workspace_change_frame;
5248
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02005249 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
5250 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005251 return -1;
5252
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005253 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005254 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005255 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005256 return -1;
5257
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02005258 weston_compositor_get_time(&shell->child.deathstamp);
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005259
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005260 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02005261
Xiong Zhang6b481422013-10-23 13:58:32 +08005262 setup_output_destroy_handler(ec, shell);
5263
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005264 loop = wl_display_get_event_loop(ec->wl_display);
5265 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005266
Jason Ekstrand024177c2014-04-21 19:42:58 -05005267 wl_list_for_each(seat, &ec->seat_list, link)
5268 handle_seat_created(NULL, seat);
5269 shell->seat_create_listener.notify = handle_seat_created;
5270 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005271
David Fort50d962f2016-06-09 17:55:52 +02005272 shell->resized_listener.notify = handle_output_resized;
5273 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
5274
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01005275 screenshooter_create(ec);
5276
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005277 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005278
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005279 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005280
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005281 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
5282
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005283 return 0;
5284}