blob: d57e2b0db118e0e10a2c4540753640cd1d36fe4e [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;
Jonas Ådahl56958aa2021-10-01 11:12:21 +02002060 struct weston_subsurface *subsurface;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002061
2062 view = get_default_view(surface);
2063 if (!view)
2064 return;
2065
2066 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2067
2068 shsurf = get_shell_surface(surface);
2069 current_ws = get_current_workspace(shsurf->shell);
2070
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002071 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002072 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002073
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002074 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002075 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002076
2077 shell_surface_update_child_surface_layers(shsurf);
Jonas Ådahl56958aa2021-10-01 11:12:21 +02002078
2079 weston_view_damage_below(shsurf->view);
2080 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
2081 wl_list_for_each(view, &subsurface->surface->views, surface_link)
2082 weston_view_damage_below(view);
2083 }
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002084}
2085
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002086
Tiago Vignattibe143262012-04-16 17:31:41 +03002087static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002088shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002089{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002090 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002091}
2092
Pekka Paalanen8274d902014-08-06 19:36:51 +03002093static int
2094black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2095{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002096 struct weston_view *fs_view = surface->committed_private;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002097 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002098 int n;
2099 int rem;
2100 int ret;
2101
2102 n = snprintf(buf, len, "black background surface for ");
2103 if (n < 0)
2104 return n;
2105
2106 rem = (int)len - n;
2107 if (rem < 0)
2108 rem = 0;
2109
2110 if (fs_surface->get_label)
2111 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2112 else
2113 ret = snprintf(buf + n, rem, "<unknown>");
2114
2115 if (ret < 0)
2116 return n;
2117
2118 return n + ret;
2119}
2120
Alex Wu21858432012-04-01 20:13:08 +08002121static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002122black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002123
Jason Ekstranda7af7042013-10-12 22:38:11 -05002124static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002125create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002126 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002127 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002128{
2129 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002130 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002131
2132 surface = weston_surface_create(ec);
2133 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002134 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002135 return NULL;
2136 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002137 view = weston_view_create(surface);
2138 if (surface == NULL) {
2139 weston_log("no memory\n");
2140 weston_surface_destroy(surface);
2141 return NULL;
2142 }
Alex Wu4539b082012-03-01 12:57:46 +08002143
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002144 surface->committed = black_surface_committed;
2145 surface->committed_private = fs_view;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002146 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002147 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002148 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002149 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002150 pixman_region32_fini(&surface->input);
2151 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002152
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002153 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002154 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002155
2156 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002157}
2158
Philip Withnalled8f1a92013-11-25 18:01:43 +00002159static void
2160shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2161{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002162 struct weston_surface *surface =
2163 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002164 struct weston_output *output = shsurf->fullscreen_output;
2165
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002166 assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
Philip Withnalled8f1a92013-11-25 18:01:43 +00002167
2168 if (!shsurf->fullscreen.black_view)
2169 shsurf->fullscreen.black_view =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002170 create_black_surface(surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002171 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002172 output->x, output->y,
2173 output->width,
2174 output->height);
2175
2176 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002177 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2178 weston_layer_entry_insert(&shsurf->view->layer_link,
2179 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002180 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002181 weston_surface_damage(surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002182
Armin Krezović4663aca2016-06-30 06:04:29 +02002183 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002184 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002185}
2186
Alex Wu4539b082012-03-01 12:57:46 +08002187/* Create black surface and append it to the associated fullscreen surface.
2188 * Handle size dismatch and positioning according to the method. */
2189static void
2190shell_configure_fullscreen(struct shell_surface *shsurf)
2191{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002192 struct weston_surface *surface =
2193 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffob8366642013-04-25 13:57:46 +03002194 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002195
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002196 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002197 weston_layer_entry_remove(&shsurf->view->layer_link);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002198 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2199 &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002200
Pekka Paalanenc63acc42018-05-02 10:21:58 +02002201 if (!shsurf->fullscreen_output) {
2202 /* If there is no output, there's not much we can do.
2203 * Position the window somewhere, whatever. */
2204 weston_view_set_position(shsurf->view, 0, 0);
2205 return;
2206 }
2207
Philip Withnalled8f1a92013-11-25 18:01:43 +00002208 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002209
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002210 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002211 &surf_width, &surf_height);
2212
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002213 if (surface->buffer_ref.buffer)
2214 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002215}
2216
Alex Wu4539b082012-03-01 12:57:46 +08002217static void
2218shell_map_fullscreen(struct shell_surface *shsurf)
2219{
Alex Wubd3354b2012-04-17 17:20:49 +08002220 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002221}
2222
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002223static struct weston_output *
2224get_focused_output(struct weston_compositor *compositor)
2225{
2226 struct weston_seat *seat;
2227 struct weston_output *output = NULL;
2228
2229 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002230 struct weston_touch *touch = weston_seat_get_touch(seat);
2231 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2232 struct weston_keyboard *keyboard =
2233 weston_seat_get_keyboard(seat);
2234
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002235 /* Priority has touch focus, then pointer and
2236 * then keyboard focus. We should probably have
Emmanuel Gil Peyrot426c2462019-02-20 16:33:32 +01002237 * three for loops and check first for touch,
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002238 * then for pointer, etc. but unless somebody has some
2239 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002240 if (touch && touch->focus)
2241 output = touch->focus->output;
2242 else if (pointer && pointer->focus)
2243 output = pointer->focus->output;
2244 else if (keyboard && keyboard->focus)
2245 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002246
2247 if (output)
2248 break;
2249 }
2250
2251 return output;
2252}
2253
2254static void
Giulio Camuffo5085a752013-03-25 21:42:45 +01002255destroy_shell_seat(struct wl_listener *listener, void *data)
2256{
2257 struct shell_seat *shseat =
2258 container_of(listener,
2259 struct shell_seat, seat_destroy_listener);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002260
2261 wl_list_remove(&shseat->seat_destroy_listener.link);
2262 free(shseat);
2263}
2264
Jason Ekstrand024177c2014-04-21 19:42:58 -05002265static void
2266shell_seat_caps_changed(struct wl_listener *listener, void *data)
2267{
Derek Foreman1281a362015-07-31 16:55:32 -05002268 struct weston_keyboard *keyboard;
2269 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05002270 struct shell_seat *seat;
2271
2272 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002273 keyboard = weston_seat_get_keyboard(seat->seat);
2274 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002275
Derek Foreman1281a362015-07-31 16:55:32 -05002276 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002277 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002278 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002279 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002280 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05002281 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002282 wl_list_init(&seat->keyboard_focus_listener.link);
2283 }
2284
Derek Foreman1281a362015-07-31 16:55:32 -05002285 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002286 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002287 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002288 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002289 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05002290 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002291 wl_list_init(&seat->pointer_focus_listener.link);
2292 }
2293}
2294
Giulio Camuffo5085a752013-03-25 21:42:45 +01002295static struct shell_seat *
2296create_shell_seat(struct weston_seat *seat)
2297{
2298 struct shell_seat *shseat;
2299
2300 shseat = calloc(1, sizeof *shseat);
2301 if (!shseat) {
2302 weston_log("no memory to allocate shell seat\n");
2303 return NULL;
2304 }
2305
2306 shseat->seat = seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002307
2308 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2309 wl_signal_add(&seat->destroy_signal,
2310 &shseat->seat_destroy_listener);
2311
Jason Ekstrand024177c2014-04-21 19:42:58 -05002312 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2313 wl_list_init(&shseat->keyboard_focus_listener.link);
2314
2315 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2316 wl_list_init(&shseat->pointer_focus_listener.link);
2317
2318 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2319 wl_signal_add(&seat->updated_caps_signal,
2320 &shseat->caps_changed_listener);
2321 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2322
Giulio Camuffo5085a752013-03-25 21:42:45 +01002323 return shseat;
2324}
2325
2326static struct shell_seat *
2327get_shell_seat(struct weston_seat *seat)
2328{
2329 struct wl_listener *listener;
2330
2331 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002332 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002333
2334 return container_of(listener,
2335 struct shell_seat, seat_destroy_listener);
2336}
2337
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002338static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002339fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002340{
2341 struct shell_surface *shsurf = data;
2342
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002343 weston_surface_destroy(shsurf->view->surface);
Pekka Paalanen99628002018-05-22 12:48:35 +03002344
2345 if (shsurf->output_destroy_listener.notify) {
2346 wl_list_remove(&shsurf->output_destroy_listener.link);
2347 shsurf->output_destroy_listener.notify = NULL;
2348 }
2349
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002350 free(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002351}
2352
2353static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002354fade_out_done(struct weston_view_animation *animation, void *data)
2355{
2356 struct shell_surface *shsurf = data;
2357 struct wl_event_loop *loop;
2358
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002359 loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
Derek Foreman039e9be2015-04-14 17:09:06 -05002360
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002361 if (weston_view_is_mapped(shsurf->view)) {
Tomohito Esaki5898cd32019-04-01 17:50:14 +09002362 weston_view_unmap(shsurf->view);
Derek Foreman039e9be2015-04-14 17:09:06 -05002363 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
Derek Foreman039e9be2015-04-14 17:09:06 -05002364 }
2365}
2366
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002367struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002368get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002369{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002370 if (weston_surface_is_desktop_surface(surface)) {
2371 struct weston_desktop_surface *desktop_surface =
2372 weston_surface_get_desktop_surface(surface);
2373 return weston_desktop_surface_get_user_data(desktop_surface);
2374 }
2375 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002376}
2377
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002378/*
2379 * libweston-desktop
2380 */
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002381
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002382static void
2383desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2384 void *shell)
2385{
2386 struct weston_desktop_client *client =
2387 weston_desktop_surface_get_client(desktop_surface);
2388 struct wl_client *wl_client =
2389 weston_desktop_client_get_client(client);
2390 struct weston_view *view;
2391 struct shell_surface *shsurf;
2392 struct weston_surface *surface =
2393 weston_desktop_surface_get_surface(desktop_surface);
2394
2395 view = weston_desktop_surface_create_view(desktop_surface);
2396 if (!view)
2397 return;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002398
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002399 shsurf = calloc(1, sizeof *shsurf);
2400 if (!shsurf) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002401 if (wl_client)
2402 wl_client_post_no_memory(wl_client);
2403 else
2404 weston_log("no memory to allocate shell surface\n");
2405 return;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002406 }
2407
Pekka Paalanen8274d902014-08-06 19:36:51 +03002408 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002409
Tiago Vignattibc052c92012-04-19 16:18:18 +03002410 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002411 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002412 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002413 shsurf->saved_rotation_valid = false;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002414 shsurf->desktop_surface = desktop_surface;
2415 shsurf->view = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002416 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002417 wl_list_init(&shsurf->fullscreen.transform.link);
2418
Semi Malinen99f8c082018-05-02 11:10:32 +02002419 shell_surface_set_output(
2420 shsurf, get_default_output(shsurf->shell->compositor));
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002421
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002422 wl_signal_init(&shsurf->destroy_signal);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002423
Pekka Paalanen460099f2012-01-20 16:48:25 +02002424 /* empty when not in use */
2425 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002426 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002427
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002428 wl_list_init(&shsurf->workspace_transform.link);
2429
Stefan Agnera8da2082019-06-23 14:53:59 +02002430 /*
2431 * initialize list as well as link. The latter allows to use
2432 * wl_list_remove() even when this surface is not in another list.
2433 */
2434 wl_list_init(&shsurf->children_list);
2435 wl_list_init(&shsurf->children_link);
2436
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002437 weston_desktop_surface_set_user_data(desktop_surface, shsurf);
2438 weston_desktop_surface_set_activated(desktop_surface,
2439 shsurf->focus_count > 0);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002440}
2441
Tiago Vignattibc052c92012-04-19 16:18:18 +03002442static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002443desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2444 void *shell)
2445{
2446 struct shell_surface *shsurf =
2447 weston_desktop_surface_get_user_data(desktop_surface);
Stefan Agnera8da2082019-06-23 14:53:59 +02002448 struct shell_surface *shsurf_child, *tmp;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002449 struct weston_surface *surface =
2450 weston_desktop_surface_get_surface(desktop_surface);
2451
2452 if (!shsurf)
2453 return;
2454
Stefan Agnera8da2082019-06-23 14:53:59 +02002455 wl_list_for_each_safe(shsurf_child, tmp, &shsurf->children_list, children_link) {
2456 wl_list_remove(&shsurf_child->children_link);
2457 wl_list_init(&shsurf_child->children_link);
2458 }
2459 wl_list_remove(&shsurf->children_link);
2460
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002461 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2462
2463 if (shsurf->fullscreen.black_view)
2464 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2465
2466 weston_surface_set_label_func(surface, NULL);
2467 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2468 shsurf->desktop_surface = NULL;
2469
Quentin Glidicf01ecee2016-08-16 10:52:46 +02002470 weston_desktop_surface_unlink_view(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002471 if (weston_surface_is_mapped(surface) &&
2472 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
Erik Kurzinger04918f32021-05-18 11:49:37 -04002473
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02002474 if (shsurf->shell->compositor->state == WESTON_COMPOSITOR_ACTIVE) {
2475 pixman_region32_fini(&surface->pending.input);
2476 pixman_region32_init(&surface->pending.input);
2477 pixman_region32_fini(&surface->input);
2478 pixman_region32_init(&surface->input);
2479 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2480 fade_out_done, shsurf);
2481 return;
2482 } else {
Marius Vladc30cb292021-08-02 15:47:16 +03002483 weston_surface_destroy(surface);
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +02002484 }
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002485 }
Erik Kurzinger04918f32021-05-18 11:49:37 -04002486
Marius Vladc30cb292021-08-02 15:47:16 +03002487 weston_view_destroy(shsurf->view);
Erik Kurzinger04918f32021-05-18 11:49:37 -04002488
2489 if (shsurf->output_destroy_listener.notify) {
2490 wl_list_remove(&shsurf->output_destroy_listener.link);
2491 shsurf->output_destroy_listener.notify = NULL;
2492 }
2493
2494 free(shsurf);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002495}
2496
2497static void
2498set_maximized_position(struct desktop_shell *shell,
2499 struct shell_surface *shsurf)
2500{
2501 pixman_rectangle32_t area;
2502 struct weston_geometry geometry;
2503
2504 get_output_work_area(shell, shsurf->output, &area);
2505 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2506
2507 weston_view_set_position(shsurf->view,
2508 area.x - geometry.x,
2509 area.y - geometry.y);
2510}
2511
2512static void
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002513set_position_from_xwayland(struct shell_surface *shsurf)
2514{
2515 struct weston_geometry geometry;
2516 float x;
2517 float y;
2518
2519 assert(shsurf->xwayland.is_set);
2520
2521 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2522 x = shsurf->xwayland.x - geometry.x;
2523 y = shsurf->xwayland.y - geometry.y;
2524
2525 weston_view_set_position(shsurf->view, x, y);
2526
2527#ifdef WM_DEBUG
2528 weston_log("%s: XWM %d, %d; geometry %d, %d; view %f, %f\n",
2529 __func__, shsurf->xwayland.x, shsurf->xwayland.y,
2530 geometry.x, geometry.y, x, y);
2531#endif
2532}
2533
2534static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002535map(struct desktop_shell *shell, struct shell_surface *shsurf,
2536 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002537{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002538 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002539 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2540 struct weston_compositor *compositor = shell->compositor;
2541 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002542
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002543 /* initial positioning, see also configure() */
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002544 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002545 center_on_output(shsurf->view, shsurf->fullscreen_output);
2546 shell_map_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002547 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002548 set_maximized_position(shell, shsurf);
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002549 } else if (shsurf->xwayland.is_set) {
2550 set_position_from_xwayland(shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002551 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002552 weston_view_set_initial_position(shsurf->view, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002553 }
2554
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002555 /* Surface stacking order, see also activate(). */
2556 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002557
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002558 weston_view_update_transform(shsurf->view);
2559 shsurf->view->is_mapped = true;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002560 if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002561 surface->output = shsurf->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02002562 weston_view_set_output(shsurf->view, shsurf->output);
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002563 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002564
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002565 if (!shell->locked) {
2566 wl_list_for_each(seat, &compositor->seat_list, link)
2567 activate(shell, shsurf->view, seat,
2568 WESTON_ACTIVATE_FLAG_CONFIGURE);
2569 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002570
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002571 if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002572 switch (shell->win_animation_type) {
2573 case ANIMATION_FADE:
2574 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2575 break;
2576 case ANIMATION_ZOOM:
2577 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2578 break;
2579 case ANIMATION_NONE:
2580 default:
2581 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002582 }
2583 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002584}
2585
2586static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002587desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2588 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002589{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002590 struct shell_surface *shsurf =
2591 weston_desktop_surface_get_user_data(desktop_surface);
2592 struct weston_surface *surface =
2593 weston_desktop_surface_get_surface(desktop_surface);
2594 struct weston_view *view = shsurf->view;
2595 struct desktop_shell *shell = data;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002596 bool was_fullscreen;
2597 bool was_maximized;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002598
2599 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002600 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002601
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002602 was_fullscreen = shsurf->state.fullscreen;
2603 was_maximized = shsurf->state.maximized;
2604
2605 shsurf->state.fullscreen =
2606 weston_desktop_surface_get_fullscreen(desktop_surface);
2607 shsurf->state.maximized =
2608 weston_desktop_surface_get_maximized(desktop_surface);
2609
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002610 if (!weston_surface_is_mapped(surface)) {
2611 map(shell, shsurf, sx, sy);
2612 surface->is_mapped = true;
2613 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2614 ++surface->ref_count;
2615 return;
2616 }
2617
2618 if (sx == 0 && sy == 0 &&
2619 shsurf->last_width == surface->width &&
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002620 shsurf->last_height == surface->height &&
2621 was_fullscreen == shsurf->state.fullscreen &&
2622 was_maximized == shsurf->state.maximized)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002623 return;
2624
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002625 if (was_fullscreen)
2626 unset_fullscreen(shsurf);
2627 if (was_maximized)
2628 unset_maximized(shsurf);
2629
Quentin Glidic920cf042016-08-16 14:26:20 +02002630 if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2631 !shsurf->saved_position_valid) {
2632 shsurf->saved_x = shsurf->view->geometry.x;
2633 shsurf->saved_y = shsurf->view->geometry.y;
2634 shsurf->saved_position_valid = true;
2635
2636 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2637 wl_list_remove(&shsurf->rotation.transform.link);
2638 wl_list_init(&shsurf->rotation.transform.link);
2639 weston_view_geometry_dirty(shsurf->view);
2640 shsurf->saved_rotation_valid = true;
2641 }
2642 }
2643
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002644 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002645 shell_configure_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002646 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002647 set_maximized_position(shell, shsurf);
2648 surface->output = shsurf->output;
2649 } else {
2650 float from_x, from_y;
2651 float to_x, to_y;
2652 float x, y;
2653
2654 if (shsurf->resize_edges) {
2655 sx = 0;
2656 sy = 0;
2657 }
2658
2659 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2660 sx = shsurf->last_width - surface->width;
2661 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2662 sy = shsurf->last_height - surface->height;
2663
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002664 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2665 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2666 x = shsurf->view->geometry.x + to_x - from_x;
2667 y = shsurf->view->geometry.y + to_y - from_y;
2668
2669 weston_view_set_position(shsurf->view, x, y);
2670 }
2671
Emmanuel Gil Peyrotc3941792017-04-04 18:49:33 +01002672 shsurf->last_width = surface->width;
2673 shsurf->last_height = surface->height;
2674
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002675 /* XXX: would a fullscreen surface need the same handling? */
2676 if (surface->output) {
2677 wl_list_for_each(view, &surface->views, surface_link)
2678 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002679 }
2680}
2681
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002682static void
Derek Foreman927d9e22017-10-06 14:37:44 -05002683get_maximized_size(struct shell_surface *shsurf, int32_t *width, int32_t *height)
2684{
2685 struct desktop_shell *shell;
2686 pixman_rectangle32_t area;
2687
2688 shell = shell_surface_get_shell(shsurf);
2689 get_output_work_area(shell, shsurf->output, &area);
2690
2691 *width = area.width;
2692 *height = area.height;
2693}
2694
2695static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002696set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2697 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002698{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002699 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2700 struct weston_surface *surface =
2701 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2702 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002703
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002704 if (fullscreen) {
2705 /* handle clients launching in fullscreen */
2706 if (output == NULL && !weston_surface_is_mapped(surface)) {
2707 /* Set the output to the one that has focus currently. */
2708 output = get_focused_output(surface->compositor);
2709 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002710
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002711 shell_surface_set_output(shsurf, output);
2712 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002713
Marius Vlad6ebda362020-03-28 00:23:14 +02002714 if (shsurf->output) {
2715 width = shsurf->output->width;
2716 height = shsurf->output->height;
2717 }
Derek Foremane1af3d82017-10-06 14:37:45 -05002718 } else if (weston_desktop_surface_get_maximized(desktop_surface)) {
2719 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002720 }
2721 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2722 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002723}
2724
2725static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002726desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2727 struct weston_seat *seat, uint32_t serial, void *shell)
2728{
2729 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2730 struct weston_touch *touch = weston_seat_get_touch(seat);
2731 struct shell_surface *shsurf =
2732 weston_desktop_surface_get_user_data(desktop_surface);
2733 struct weston_surface *surface =
2734 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2735 struct wl_resource *resource = surface->resource;
2736 struct weston_surface *focus;
2737
2738 if (pointer &&
2739 pointer->focus &&
2740 pointer->button_count > 0 &&
2741 pointer->grab_serial == serial) {
2742 focus = weston_surface_get_main_surface(pointer->focus->surface);
2743 if ((focus == surface) &&
2744 (surface_move(shsurf, pointer, true) < 0))
2745 wl_resource_post_no_memory(resource);
2746 } else if (touch &&
2747 touch->focus &&
2748 touch->grab_serial == serial) {
2749 focus = weston_surface_get_main_surface(touch->focus->surface);
2750 if ((focus == surface) &&
2751 (surface_touch_move(shsurf, touch) < 0))
2752 wl_resource_post_no_memory(resource);
2753 }
2754}
2755
2756static void
2757desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2758 struct weston_seat *seat, uint32_t serial,
2759 enum weston_desktop_surface_edge edges, void *shell)
2760{
2761 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2762 struct shell_surface *shsurf =
2763 weston_desktop_surface_get_user_data(desktop_surface);
2764 struct weston_surface *surface =
2765 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2766 struct wl_resource *resource = surface->resource;
2767 struct weston_surface *focus;
2768
2769 if (!pointer ||
2770 pointer->button_count == 0 ||
2771 pointer->grab_serial != serial ||
2772 pointer->focus == NULL)
2773 return;
2774
2775 focus = weston_surface_get_main_surface(pointer->focus->surface);
2776 if (focus != surface)
2777 return;
2778
2779 if (surface_resize(shsurf, pointer, edges) < 0)
2780 wl_resource_post_no_memory(resource);
2781}
2782
2783static void
Stefan Agnera8da2082019-06-23 14:53:59 +02002784desktop_surface_set_parent(struct weston_desktop_surface *desktop_surface,
2785 struct weston_desktop_surface *parent,
2786 void *shell)
2787{
Marius Vlada27658e2020-01-17 12:32:55 +02002788 struct shell_surface *shsurf_parent;
Stefan Agnera8da2082019-06-23 14:53:59 +02002789 struct shell_surface *shsurf =
2790 weston_desktop_surface_get_user_data(desktop_surface);
Stefan Agnera8da2082019-06-23 14:53:59 +02002791
Marius Vlada27658e2020-01-17 12:32:55 +02002792 /* unlink any potential child */
2793 wl_list_remove(&shsurf->children_link);
2794
2795 if (parent) {
2796 shsurf_parent = weston_desktop_surface_get_user_data(parent);
2797 wl_list_insert(shsurf_parent->children_list.prev,
2798 &shsurf->children_link);
2799 } else {
2800 wl_list_init(&shsurf->children_link);
2801 }
Stefan Agnera8da2082019-06-23 14:53:59 +02002802}
2803
2804static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002805desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2806 bool fullscreen,
2807 struct weston_output *output, void *shell)
2808{
2809 struct shell_surface *shsurf =
2810 weston_desktop_surface_get_user_data(desktop_surface);
2811
2812 set_fullscreen(shsurf, fullscreen, output);
2813}
2814
2815static void
2816set_maximized(struct shell_surface *shsurf, bool maximized)
2817{
2818 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2819 struct weston_surface *surface =
2820 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2821 int32_t width = 0, height = 0;
2822
2823 if (maximized) {
2824 struct weston_output *output;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002825
2826 if (!weston_surface_is_mapped(surface))
2827 output = get_focused_output(surface->compositor);
2828 else
2829 output = surface->output;
2830
2831 shell_surface_set_output(shsurf, output);
2832
Derek Foreman927d9e22017-10-06 14:37:44 -05002833 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002834 }
2835 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2836 weston_desktop_surface_set_size(desktop_surface, width, height);
2837}
2838
2839static void
2840desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2841 bool maximized, void *shell)
2842{
2843 struct shell_surface *shsurf =
2844 weston_desktop_surface_get_user_data(desktop_surface);
2845
2846 set_maximized(shsurf, maximized);
2847}
2848
2849static void
2850desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2851 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002852{
2853 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002854 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002855
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002856 /* apply compositor's own minimization logic (hide) */
2857 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002858}
2859
Rafael Antognollie2a34552013-12-03 15:35:45 -02002860static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002861set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002862{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002863 struct shell_grab *grab;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002864
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002865 if (pointer->grab->interface == &busy_cursor_grab_interface)
2866 return;
2867
2868 grab = malloc(sizeof *grab);
2869 if (!grab)
2870 return;
2871
2872 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2873 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2874 /* Mark the shsurf as ungrabbed so that button binding is able
2875 * to move it. */
2876 shsurf->grabbed = 0;
2877}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002878
2879static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002880end_busy_cursor(struct weston_compositor *compositor,
2881 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002882{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002883 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002884 struct shell_grab *grab;
2885 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002886
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002887 wl_list_for_each(seat, &compositor->seat_list, link) {
2888 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2889 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002890
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002891 if (!pointer)
2892 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002893
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002894 if (pointer->grab->interface != &busy_cursor_grab_interface)
2895 continue;
2896
2897 grab = (struct shell_grab *) pointer->grab;
2898 shsurf = grab->shsurf;
2899 if (!shsurf)
2900 continue;
2901
2902 grab_client =
2903 weston_desktop_surface_get_client(shsurf->desktop_surface);
2904 if (grab_client == desktop_client) {
2905 shell_grab_end(grab);
2906 free(grab);
2907 }
2908 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002909}
2910
2911static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002912desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2913 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002914{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002915 struct shell_surface *shsurf =
2916 weston_desktop_surface_get_user_data(desktop_surface);
2917 bool *unresponsive = user_data;
2918
2919 shsurf->unresponsive = *unresponsive;
2920}
2921
2922static void
2923desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2924 void *shell_)
2925{
2926 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002927 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002928 struct weston_seat *seat;
2929 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002930
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002931 weston_desktop_client_for_each_surface(desktop_client,
2932 desktop_surface_set_unresponsive,
2933 &unresponsive);
2934
2935
2936 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2937 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2938 struct weston_desktop_client *grab_client;
2939
2940 if (!pointer || !pointer->focus)
2941 continue;
2942
2943 shsurf = get_shell_surface(pointer->focus->surface);
2944 if (!shsurf)
2945 continue;
2946
2947 grab_client =
2948 weston_desktop_surface_get_client(shsurf->desktop_surface);
2949 if (grab_client == desktop_client)
2950 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002951 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002952}
2953
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002954static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002955desktop_surface_pong(struct weston_desktop_client *desktop_client,
2956 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002957{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002958 struct desktop_shell *shell = shell_;
2959 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002960
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002961 weston_desktop_client_for_each_surface(desktop_client,
2962 desktop_surface_set_unresponsive,
2963 &unresponsive);
2964 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002965}
2966
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002967static void
2968desktop_surface_set_xwayland_position(struct weston_desktop_surface *surface,
2969 int32_t x, int32_t y, void *shell_)
2970{
2971 struct shell_surface *shsurf =
2972 weston_desktop_surface_get_user_data(surface);
2973
2974 shsurf->xwayland.x = x;
2975 shsurf->xwayland.y = y;
2976 shsurf->xwayland.is_set = true;
2977}
2978
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002979static const struct weston_desktop_api shell_desktop_api = {
2980 .struct_size = sizeof(struct weston_desktop_api),
2981 .surface_added = desktop_surface_added,
2982 .surface_removed = desktop_surface_removed,
2983 .committed = desktop_surface_committed,
2984 .move = desktop_surface_move,
2985 .resize = desktop_surface_resize,
Stefan Agnera8da2082019-06-23 14:53:59 +02002986 .set_parent = desktop_surface_set_parent,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002987 .fullscreen_requested = desktop_surface_fullscreen_requested,
2988 .maximized_requested = desktop_surface_maximized_requested,
2989 .minimized_requested = desktop_surface_minimized_requested,
2990 .ping_timeout = desktop_surface_ping_timeout,
2991 .pong = desktop_surface_pong,
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002992 .set_xwayland_position = desktop_surface_set_xwayland_position,
Rafael Antognollie2a34552013-12-03 15:35:45 -02002993};
2994
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002995/* ************************ *
2996 * end of libweston-desktop *
2997 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04002998static void
Quentin Glidice8bf9592016-06-23 18:55:20 +02002999configure_static_view(struct weston_view *ev, struct weston_layer *layer, int x, int y)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003000{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003001 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003002
Semi Malinene7a52fb2018-04-26 11:08:10 +02003003 if (!ev->output)
3004 return;
3005
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003006 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003007 if (v->output == ev->output && v != ev) {
3008 weston_view_unmap(v);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003009 v->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003010 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003011 }
3012 }
3013
Quentin Glidice8bf9592016-06-23 18:55:20 +02003014 weston_view_set_position(ev, ev->output->x + x, ev->output->y + y);
Armin Krezović4663aca2016-06-30 06:04:29 +02003015 ev->surface->is_mapped = true;
3016 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003017
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003018 if (wl_list_empty(&ev->layer_link.link)) {
3019 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003020 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003021 }
3022}
3023
David Fort50d962f2016-06-09 17:55:52 +02003024
3025static struct shell_output *
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003026find_shell_output_from_weston_output(struct desktop_shell *shell,
3027 struct weston_output *output)
David Fort50d962f2016-06-09 17:55:52 +02003028{
3029 struct shell_output *shell_output;
3030
3031 wl_list_for_each(shell_output, &shell->output_list, link) {
3032 if (shell_output->output == output)
3033 return shell_output;
3034 }
3035
3036 return NULL;
3037}
3038
Pekka Paalanen8274d902014-08-06 19:36:51 +03003039static int
3040background_get_label(struct weston_surface *surface, char *buf, size_t len)
3041{
3042 return snprintf(buf, len, "background for output %s",
Miguel A. Vico620f68d2019-09-25 11:23:13 -07003043 (surface->output ? surface->output->name : "NULL"));
Pekka Paalanen8274d902014-08-06 19:36:51 +03003044}
3045
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003046static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003047background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003048{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003049 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003050 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003051
Jason Ekstranda7af7042013-10-12 22:38:11 -05003052 view = container_of(es->views.next, struct weston_view, surface_link);
3053
Quentin Glidice8bf9592016-06-23 18:55:20 +02003054 configure_static_view(view, &shell->background_layer, 0, 0);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003055}
3056
3057static void
David Fort50d962f2016-06-09 17:55:52 +02003058handle_background_surface_destroy(struct wl_listener *listener, void *data)
3059{
3060 struct shell_output *output =
3061 container_of(listener, struct shell_output, background_surface_listener);
3062
3063 weston_log("background surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09003064 wl_list_remove(&output->background_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02003065 output->background_surface = NULL;
3066}
3067
3068static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003069desktop_shell_set_background(struct wl_client *client,
3070 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003071 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003072 struct wl_resource *surface_resource)
3073{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003074 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003075 struct weston_surface *surface =
3076 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02003077 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003078 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003079
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003080 if (surface->committed) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003081 wl_resource_post_error(surface_resource,
3082 WL_DISPLAY_ERROR_INVALID_OBJECT,
3083 "surface role already assigned");
3084 return;
3085 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003086
Jason Ekstranda7af7042013-10-12 22:38:11 -05003087 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3088 weston_view_destroy(view);
3089 view = weston_view_create(surface);
3090
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003091 surface->committed = background_committed;
3092 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003093 weston_surface_set_label_func(surface, background_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03003094 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02003095 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02003096
3097 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02003098 if (sh_output->background_surface) {
3099 /* The output already has a background, tell our helper
3100 * there is no need for another one. */
3101 weston_desktop_shell_send_configure(resource, 0,
3102 surface_resource,
3103 0, 0);
3104 } else {
3105 weston_desktop_shell_send_configure(resource, 0,
3106 surface_resource,
3107 surface->output->width,
3108 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003109
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02003110 sh_output->background_surface = surface;
3111
3112 sh_output->background_surface_listener.notify =
3113 handle_background_surface_destroy;
3114 wl_signal_add(&surface->destroy_signal,
3115 &sh_output->background_surface_listener);
3116 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003117}
3118
Pekka Paalanen8274d902014-08-06 19:36:51 +03003119static int
3120panel_get_label(struct weston_surface *surface, char *buf, size_t len)
3121{
3122 return snprintf(buf, len, "panel for output %s",
Miguel A. Vico620f68d2019-09-25 11:23:13 -07003123 (surface->output ? surface->output->name : "NULL"));
Pekka Paalanen8274d902014-08-06 19:36:51 +03003124}
3125
Kristian Høgsberg75840622011-09-06 13:48:16 -04003126static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003127panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003128{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003129 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003130 struct weston_view *view;
Quentin Glidice8bf9592016-06-23 18:55:20 +02003131 int width, height;
3132 int x = 0, y = 0;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003133
Jason Ekstranda7af7042013-10-12 22:38:11 -05003134 view = container_of(es->views.next, struct weston_view, surface_link);
3135
Quentin Glidice8bf9592016-06-23 18:55:20 +02003136 get_panel_size(shell, view, &width, &height);
3137 switch (shell->panel_position) {
3138 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
3139 break;
3140 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
3141 y = view->output->height - height;
3142 break;
3143 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
3144 break;
3145 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
3146 x = view->output->width - width;
3147 break;
3148 }
3149
3150 configure_static_view(view, &shell->panel_layer, x, y);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003151}
3152
3153static void
David Fort50d962f2016-06-09 17:55:52 +02003154handle_panel_surface_destroy(struct wl_listener *listener, void *data)
3155{
3156 struct shell_output *output =
3157 container_of(listener, struct shell_output, panel_surface_listener);
3158
3159 weston_log("panel surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09003160 wl_list_remove(&output->panel_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02003161 output->panel_surface = NULL;
3162}
3163
3164
3165static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003166desktop_shell_set_panel(struct wl_client *client,
3167 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003168 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003169 struct wl_resource *surface_resource)
3170{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003171 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003172 struct weston_surface *surface =
3173 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003174 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02003175 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003176
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003177 if (surface->committed) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003178 wl_resource_post_error(surface_resource,
3179 WL_DISPLAY_ERROR_INVALID_OBJECT,
3180 "surface role already assigned");
3181 return;
3182 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003183
Jason Ekstranda7af7042013-10-12 22:38:11 -05003184 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3185 weston_view_destroy(view);
3186 view = weston_view_create(surface);
3187
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003188 surface->committed = panel_committed;
3189 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003190 weston_surface_set_label_func(surface, panel_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03003191 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02003192 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02003193
3194 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003195 if (sh_output->panel_surface) {
3196 /* The output already has a panel, tell our helper
3197 * there is no need for another one. */
3198 weston_desktop_shell_send_configure(resource, 0,
3199 surface_resource,
3200 0, 0);
3201 } else {
3202 weston_desktop_shell_send_configure(resource, 0,
3203 surface_resource,
3204 surface->output->width,
3205 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003206
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003207 sh_output->panel_surface = surface;
3208
3209 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
3210 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
3211 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003212}
3213
Pekka Paalanen8274d902014-08-06 19:36:51 +03003214static int
3215lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
3216{
3217 return snprintf(buf, len, "lock window");
3218}
3219
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003220static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003221lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003222{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003223 struct desktop_shell *shell = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003224 struct weston_view *view;
3225
3226 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003227
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003228 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003229 return;
3230
Jason Ekstranda7af7042013-10-12 22:38:11 -05003231 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003232
3233 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003234 weston_layer_entry_insert(&shell->lock_layer.view_list,
3235 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003236 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003237 surface->is_mapped = true;
3238 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003239 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003240 }
3241}
3242
3243static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003244handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003245{
Tiago Vignattibe143262012-04-16 17:31:41 +03003246 struct desktop_shell *shell =
3247 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003248
Martin Minarik6d118362012-06-07 18:01:59 +02003249 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003250 shell->lock_surface = NULL;
3251}
3252
3253static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003254desktop_shell_set_lock_surface(struct wl_client *client,
3255 struct wl_resource *resource,
3256 struct wl_resource *surface_resource)
3257{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003258 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003259 struct weston_surface *surface =
3260 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003261
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003262 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003263
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003264 if (!shell->locked)
3265 return;
3266
Pekka Paalanen98262232011-12-01 10:42:22 +02003267 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003268
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003269 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003270 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003271 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003272
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003273 weston_view_create(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003274 surface->committed = lock_surface_committed;
3275 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003276 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003277}
3278
3279static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003280resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003281{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003282 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003283
Quentin Glidic82681572016-12-17 13:40:51 +01003284 weston_layer_unset_position(&shell->lock_layer);
3285
3286 if (shell->showing_input_panels)
3287 weston_layer_set_position(&shell->input_panel_layer,
3288 WESTON_LAYER_POSITION_TOP_UI);
3289 weston_layer_set_position(&shell->fullscreen_layer,
3290 WESTON_LAYER_POSITION_FULLSCREEN);
3291 weston_layer_set_position(&shell->panel_layer,
3292 WESTON_LAYER_POSITION_UI);
3293 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003294
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003295 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003296
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003297 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003298 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003299 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003300}
3301
3302static void
3303desktop_shell_unlock(struct wl_client *client,
3304 struct wl_resource *resource)
3305{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003306 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003307
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003308 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003309
3310 if (shell->locked)
3311 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003312}
3313
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003314static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003315desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003316 struct wl_resource *resource,
3317 struct wl_resource *surface_resource)
3318{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003319 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003320
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003321 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003322 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003323}
3324
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003325static void
3326desktop_shell_desktop_ready(struct wl_client *client,
3327 struct wl_resource *resource)
3328{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003329 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003330
3331 shell_fade_startup(shell);
3332}
3333
Jonny Lamb765760d2014-08-20 15:53:19 +02003334static void
3335desktop_shell_set_panel_position(struct wl_client *client,
3336 struct wl_resource *resource,
3337 uint32_t position)
3338{
3339 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3340
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003341 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3342 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3343 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3344 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02003345 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003346 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02003347 "bad position argument");
3348 return;
3349 }
3350
3351 shell->panel_position = position;
3352}
3353
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003354static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04003355 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003356 desktop_shell_set_panel,
3357 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003358 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003359 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02003360 desktop_shell_desktop_ready,
3361 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04003362};
3363
3364static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003365move_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003366 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003367{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003368 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003369 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003370 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003371
Derek Foreman8ae2db52015-07-15 13:00:36 -05003372 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003373 return;
3374
Derek Foreman8ae2db52015-07-15 13:00:36 -05003375 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003376
Pekka Paalanen01388e22013-04-25 13:57:44 +03003377 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003378 if (surface == NULL)
3379 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003380
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003381 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003382 if (shsurf == NULL ||
3383 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3384 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003385 return;
3386
Derek Foreman794fa0e2015-07-15 13:00:38 -05003387 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003388}
3389
3390static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003391maximize_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003392 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003393{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003394 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003395 struct weston_surface *surface;
3396 struct shell_surface *shsurf;
3397
3398 surface = weston_surface_get_main_surface(focus);
3399 if (surface == NULL)
3400 return;
3401
3402 shsurf = get_shell_surface(surface);
3403 if (shsurf == NULL)
3404 return;
3405
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003406 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003407}
3408
3409static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003410fullscreen_binding(struct weston_keyboard *keyboard,
3411 const struct timespec *time, uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003412{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003413 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003414 struct weston_surface *surface;
3415 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003416 bool fullscreen;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003417
3418 surface = weston_surface_get_main_surface(focus);
3419 if (surface == NULL)
3420 return;
3421
3422 shsurf = get_shell_surface(surface);
3423 if (shsurf == NULL)
3424 return;
3425
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003426 fullscreen =
3427 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003428
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003429 set_fullscreen(shsurf, !fullscreen, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003430}
3431
3432static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003433touch_move_binding(struct weston_touch *touch, const struct timespec *time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01003434{
Derek Foreman362656b2014-09-04 10:23:05 -05003435 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01003436 struct weston_surface *surface;
3437 struct shell_surface *shsurf;
3438
Derek Foreman8ae2db52015-07-15 13:00:36 -05003439 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05003440 return;
3441
Derek Foreman8ae2db52015-07-15 13:00:36 -05003442 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003443 surface = weston_surface_get_main_surface(focus);
3444 if (surface == NULL)
3445 return;
3446
3447 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003448 if (shsurf == NULL ||
3449 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3450 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Neil Robertsaba0f252013-10-03 16:43:05 +01003451 return;
3452
Derek Foremanb7674ae2015-07-15 13:00:37 -05003453 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01003454}
3455
3456static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003457resize_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003458 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003459{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003460 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003461 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003462 uint32_t edges = 0;
3463 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003464 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003465
Derek Foreman8ae2db52015-07-15 13:00:36 -05003466 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003467 return;
3468
Derek Foreman8ae2db52015-07-15 13:00:36 -05003469 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003470
Pekka Paalanen01388e22013-04-25 13:57:44 +03003471 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003472 if (surface == NULL)
3473 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003474
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003475 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003476 if (shsurf == NULL ||
3477 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3478 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003479 return;
3480
Jason Ekstranda7af7042013-10-12 22:38:11 -05003481 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003482 wl_fixed_to_int(pointer->grab_x),
3483 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05003484 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003485
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003486 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003487 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003488 else if (x < 2 * surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003489 edges |= 0;
3490 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003491 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003492
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003493 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003494 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003495 else if (y < 2 * surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003496 edges |= 0;
3497 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003498 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003499
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003500 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003501}
3502
3503static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003504surface_opacity_binding(struct weston_pointer *pointer,
3505 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003506 struct weston_pointer_axis_event *event,
3507 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003508{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003509 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003510 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003511 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003512 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003513
Pekka Paalanen01388e22013-04-25 13:57:44 +03003514 /* XXX: broken for windows containing sub-surfaces */
3515 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003516 if (surface == NULL)
3517 return;
3518
3519 shsurf = get_shell_surface(surface);
3520 if (!shsurf)
3521 return;
3522
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003523 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003524
Jason Ekstranda7af7042013-10-12 22:38:11 -05003525 if (shsurf->view->alpha > 1.0)
3526 shsurf->view->alpha = 1.0;
3527 if (shsurf->view->alpha < step)
3528 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003529
Jason Ekstranda7af7042013-10-12 22:38:11 -05003530 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003531 weston_surface_damage(surface);
3532}
3533
3534static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003535do_zoom(struct weston_seat *seat, const struct timespec *time, uint32_t key,
3536 uint32_t axis, double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003537{
Derek Foreman8aeeac82015-01-30 13:24:36 -06003538 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05003539 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003540 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003541 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003542
Derek Foreman1281a362015-07-31 16:55:32 -05003543 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06003544 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3545 return;
3546 }
3547
Scott Moreauccbf29d2012-02-22 14:21:41 -07003548 wl_list_for_each(output, &compositor->output_list, link) {
3549 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05003550 wl_fixed_to_double(pointer->x),
3551 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003552 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003553 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003554 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003555 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003556 increment = -output->zoom.increment;
3557 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003558 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003559 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003560 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003561 else
3562 increment = 0;
3563
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003564 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003565
Scott Moreaue6603982012-06-11 13:07:51 -06003566 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003567 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003568 else if (output->zoom.level > output->zoom.max_level)
3569 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05003570
3571 if (!output->zoom.active) {
3572 if (output->zoom.level <= 0.0)
3573 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05003574 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003575 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003576
Scott Moreaue6603982012-06-11 13:07:51 -06003577 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003578
Jason Ekstranda7af7042013-10-12 22:38:11 -05003579 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003580 }
3581 }
3582}
3583
Scott Moreauccbf29d2012-02-22 14:21:41 -07003584static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003585zoom_axis_binding(struct weston_pointer *pointer, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003586 struct weston_pointer_axis_event *event,
3587 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003588{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003589 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003590}
3591
3592static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003593zoom_key_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003594 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003595{
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003596 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003597}
3598
3599static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003600terminate_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003601 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003602{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003603 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003604
Pekka Paalanen052032d2019-02-06 10:44:37 +02003605 weston_compositor_exit(compositor);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003606}
3607
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003608static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003609rotate_grab_motion(struct weston_pointer_grab *grab,
3610 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003611 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003612{
3613 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003614 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003615 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003616 struct shell_surface *shsurf = rotate->base.shsurf;
Sergey Bugaev14ef2012019-02-11 22:55:09 +03003617 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003618 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003619
Jonas Ådahld2510102014-10-05 21:39:14 +02003620 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003621
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003622 if (!shsurf)
3623 return;
3624
Sergey Bugaev14ef2012019-02-11 22:55:09 +03003625 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
3626
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003627 cx = 0.5f * surface->width;
3628 cy = 0.5f * surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003629
Daniel Stone37816df2012-05-16 18:45:18 +01003630 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3631 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003632 r = sqrtf(dx * dx + dy * dy);
3633
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003634 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003635 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003636
3637 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003638 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003639 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003640
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003641 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003642 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003643
3644 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003645 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003646 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003647 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003648 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003649
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003650 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003651 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003652 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003653 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003654 wl_list_init(&shsurf->rotation.transform.link);
3655 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003656 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003657 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003658
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003659 /* We need to adjust the position of the surface
3660 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003661 cposx = shsurf->view->geometry.x + cx;
3662 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003663 dposx = rotate->center.x - cposx;
3664 dposy = rotate->center.y - cposy;
3665 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003666 weston_view_set_position(shsurf->view,
3667 shsurf->view->geometry.x + dposx,
3668 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003669 }
3670
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003671 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003672 * lazily applies the damage due to rotation update.
3673 */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003674 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003675}
3676
3677static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003678rotate_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003679 const struct timespec *time,
3680 uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003681{
3682 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003683 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003684 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003685 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003686 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003687
Daniel Stone4dbadb12012-05-30 16:31:51 +01003688 if (pointer->button_count == 0 &&
3689 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003690 if (shsurf)
3691 weston_matrix_multiply(&shsurf->rotation.rotation,
3692 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003693 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003694 free(rotate);
3695 }
3696}
3697
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003698static void
3699rotate_grab_cancel(struct weston_pointer_grab *grab)
3700{
3701 struct rotate_grab *rotate =
3702 container_of(grab, struct rotate_grab, base.grab);
3703
3704 shell_grab_end(&rotate->base);
3705 free(rotate);
3706}
3707
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003708static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003709 noop_grab_focus,
3710 rotate_grab_motion,
3711 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003712 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003713 noop_grab_axis_source,
3714 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003715 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003716};
3717
3718static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003719surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003720{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003721 struct weston_surface *surface =
3722 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003723 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003724 float dx, dy;
3725 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003726
Pekka Paalanen460099f2012-01-20 16:48:25 +02003727 rotate = malloc(sizeof *rotate);
3728 if (!rotate)
3729 return;
3730
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003731 weston_view_to_global_float(shsurf->view,
3732 surface->width * 0.5f,
3733 surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003734 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003735
Derek Foreman74de4692015-07-15 13:00:39 -05003736 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3737 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003738 r = sqrtf(dx * dx + dy * dy);
3739 if (r > 20.0f) {
3740 struct weston_matrix inverse;
3741
3742 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003743 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003744 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003745
3746 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003747 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003748 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003749 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003750 weston_matrix_init(&rotate->rotation);
3751 }
3752
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003753 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003754 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003755}
3756
3757static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003758rotate_binding(struct weston_pointer *pointer, const struct timespec *time,
3759 uint32_t button, void *data)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003760{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003761 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003762 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003763 struct shell_surface *surface;
3764
Derek Foreman8ae2db52015-07-15 13:00:36 -05003765 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003766 return;
3767
Derek Foreman8ae2db52015-07-15 13:00:36 -05003768 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003769
Pekka Paalanen01388e22013-04-25 13:57:44 +03003770 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003771 if (base_surface == NULL)
3772 return;
3773
3774 surface = get_shell_surface(base_surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003775 if (surface == NULL ||
3776 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3777 weston_desktop_surface_get_maximized(surface->desktop_surface))
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003778 return;
3779
Derek Foreman74de4692015-07-15 13:00:39 -05003780 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003781}
3782
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003783/* Move all fullscreen layers down to the current workspace and hide their
3784 * black views. The surfaces' state is set to both fullscreen and lowered,
3785 * and this is reversed when such a surface is re-configured, see
3786 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3787 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003788 * lowering_output = NULL - Lower on all outputs, else only lower on the
3789 * specified output.
3790 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003791 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003792 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003793void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003794lower_fullscreen_layer(struct desktop_shell *shell,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003795 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003796{
3797 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003798 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003799
3800 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003801 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003802 &shell->fullscreen_layer.view_list.link,
3803 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003804 struct shell_surface *shsurf = get_shell_surface(view->surface);
3805
3806 if (!shsurf)
3807 continue;
3808
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003809 /* Only lower surfaces which have lowering_output as their fullscreen
3810 * output, unless a NULL output asks for lowering on all outputs.
3811 */
3812 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3813 continue;
3814
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003815 /* We can have a non-fullscreen popup for a fullscreen surface
3816 * in the fullscreen layer. */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003817 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003818 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003819 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3820 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07003821 weston_view_damage_below(shsurf->fullscreen.black_view);
3822
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003823 }
3824
3825 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003826 weston_layer_entry_remove(&view->layer_link);
3827 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003828 weston_view_damage_below(view);
3829 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003830
3831 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003832 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003833}
3834
Stefan Agnera8da2082019-06-23 14:53:59 +02003835static struct shell_surface *get_last_child(struct shell_surface *shsurf)
3836{
3837 struct shell_surface *shsurf_child;
3838
3839 wl_list_for_each_reverse(shsurf_child, &shsurf->children_list, children_link) {
3840 if (weston_view_is_mapped(shsurf_child->view))
3841 return shsurf_child;
3842 }
3843
3844 return NULL;
3845}
3846
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003847void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003848activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003849 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003850{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003851 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003852 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003853 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003854 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003855 struct weston_surface *old_es;
Stefan Agnera8da2082019-06-23 14:53:59 +02003856 struct shell_surface *shsurf, *shsurf_child;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003857
Pekka Paalanen01388e22013-04-25 13:57:44 +03003858 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003859 shsurf = get_shell_surface(main_surface);
3860 assert(shsurf);
3861
Stefan Agnera8da2082019-06-23 14:53:59 +02003862 shsurf_child = get_last_child(shsurf);
3863 if (shsurf_child) {
3864 /* Activate last xdg child instead of parent. */
3865 activate(shell, shsurf_child->view, seat, flags);
3866 return;
3867 }
3868
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003869 /* Only demote fullscreen surfaces on the output of activated shsurf.
3870 * Leave fullscreen surfaces on unrelated outputs alone. */
Pekka Paalanen87860c22018-05-02 10:21:57 +02003871 if (shsurf->output)
3872 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003873
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003874 weston_view_activate(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003875
Jonas Ådahl8538b222012-08-29 22:13:03 +02003876 state = ensure_focus_state(shell, seat);
3877 if (state == NULL)
3878 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003879
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003880 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08003881 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003882
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003883 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3884 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02003885 shell_configure_fullscreen(shsurf);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003886
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01003887 /* Update the surface’s layer. This brings it to the top of the stacking
3888 * order as appropriate. */
3889 shell_surface_update_layer(shsurf);
3890
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003891 if (shell->focus_animation_type != ANIMATION_NONE) {
3892 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003893 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003894 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003895}
3896
Alex Wu21858432012-04-01 20:13:08 +08003897/* no-op func for checking black surface */
3898static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003899black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08003900{
3901}
3902
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003903static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003904is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08003905{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003906 struct weston_surface *surface = view->surface;
3907
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003908 if (surface->committed == black_surface_committed) {
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003909 if (fs_view)
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003910 *fs_view = surface->committed_private;
Alex Wu21858432012-04-01 20:13:08 +08003911 return true;
3912 }
3913 return false;
3914}
3915
Kristian Høgsberg75840622011-09-06 13:48:16 -04003916static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003917activate_binding(struct weston_seat *seat,
3918 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003919 struct weston_view *focus_view,
3920 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003921{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003922 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003923 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003924
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003925 if (!focus_view)
3926 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003927
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003928 if (is_black_surface_view(focus_view, &main_view))
3929 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08003930
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003931 main_surface = weston_surface_get_main_surface(focus_view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003932 if (!get_shell_surface(main_surface))
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003933 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003934
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003935 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01003936}
3937
3938static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003939click_to_activate_binding(struct weston_pointer *pointer,
3940 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003941 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003942{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003943 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003944 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003945 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08003946 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003947
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003948 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003949 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003950 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01003951}
3952
3953static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003954touch_to_activate_binding(struct weston_touch *touch,
3955 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003956 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003957{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003958 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003959 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003960 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08003961 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003962
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003963 activate_binding(touch->seat, data, touch->focus,
3964 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003965}
3966
3967static void
Neil Robertsb4a91702014-04-09 16:33:32 +01003968unfocus_all_seats(struct desktop_shell *shell)
3969{
3970 struct weston_seat *seat, *next;
3971
3972 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003973 struct weston_keyboard *keyboard =
3974 weston_seat_get_keyboard(seat);
3975
3976 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01003977 continue;
3978
Derek Foreman1281a362015-07-31 16:55:32 -05003979 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01003980 }
3981}
3982
3983static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003984lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003985{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003986 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003987
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003988 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003989 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003990 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003991 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003992
3993 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003994
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003995 /* Hide all surfaces by removing the fullscreen, panel and
3996 * toplevel layers. This way nothing else can show or receive
3997 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003998
Quentin Glidic82681572016-12-17 13:40:51 +01003999 weston_layer_unset_position(&shell->panel_layer);
4000 weston_layer_unset_position(&shell->fullscreen_layer);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02004001 if (shell->showing_input_panels)
Quentin Glidic82681572016-12-17 13:40:51 +01004002 weston_layer_unset_position(&shell->input_panel_layer);
4003 weston_layer_unset_position(&ws->layer);
4004
4005 weston_layer_set_position(&shell->lock_layer,
4006 WESTON_LAYER_POSITION_LOCK);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004007
Derek Foreman004b4a12015-07-20 16:28:13 -05004008 weston_compositor_sleep(shell->compositor);
4009
Neil Robertsb4a91702014-04-09 16:33:32 +01004010 /* Remove the keyboard focus on all seats. This will be
4011 * restored to the workspace's saved state via
4012 * restore_focus_state when the compositor is unlocked */
4013 unfocus_all_seats(shell);
4014
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004015 /* TODO: disable bindings that should not work while locked. */
4016
4017 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004018}
4019
4020static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004021unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004022{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004023 struct wl_resource *shell_resource;
4024
Pekka Paalanend81c2162011-11-16 13:47:34 +02004025 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004026 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004027 return;
4028 }
4029
4030 /* If desktop-shell client has gone away, unlock immediately. */
4031 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004032 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004033 return;
4034 }
4035
4036 if (shell->prepare_event_sent)
4037 return;
4038
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004039 shell_resource = shell->child.desktop_shell;
4040 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004041 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004042}
4043
4044static void
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004045shell_fade_done_for_output(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004046{
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004047 struct shell_output *shell_output = data;
4048 struct desktop_shell *shell = shell_output->shell;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004049
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004050 shell_output->fade.animation = NULL;
4051 switch (shell_output->fade.type) {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004052 case FADE_IN:
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004053 weston_surface_destroy(shell_output->fade.view->surface);
4054 shell_output->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004055 break;
4056 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004057 lock(shell);
4058 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004059 default:
4060 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004061 }
4062}
4063
Jason Ekstranda7af7042013-10-12 22:38:11 -05004064static struct weston_view *
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004065shell_fade_create_surface_for_output(struct desktop_shell *shell, struct shell_output *shell_output)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004066{
4067 struct weston_compositor *compositor = shell->compositor;
4068 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004069 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004070
4071 surface = weston_surface_create(compositor);
4072 if (!surface)
4073 return NULL;
4074
Jason Ekstranda7af7042013-10-12 22:38:11 -05004075 view = weston_view_create(surface);
4076 if (!view) {
4077 weston_surface_destroy(surface);
4078 return NULL;
4079 }
4080
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004081 weston_surface_set_size(surface, shell_output->output->width, shell_output->output->height);
4082 weston_view_set_position(view, shell_output->output->x, shell_output->output->y);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004083 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004084 weston_layer_entry_insert(&compositor->fade_layer.view_list,
4085 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004086 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02004087 surface->is_mapped = true;
4088 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004089
Jason Ekstranda7af7042013-10-12 22:38:11 -05004090 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004091}
4092
4093static void
4094shell_fade(struct desktop_shell *shell, enum fade_type type)
4095{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004096 float tint;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004097 struct shell_output *shell_output;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004098
4099 switch (type) {
4100 case FADE_IN:
4101 tint = 0.0;
4102 break;
4103 case FADE_OUT:
4104 tint = 1.0;
4105 break;
4106 default:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07004107 weston_log("shell: invalid fade type\n");
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004108 return;
4109 }
4110
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004111 /* Create a separate fade surface for each output */
4112 wl_list_for_each(shell_output, &shell->output_list, link) {
4113 shell_output->fade.type = type;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004114
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004115 if (shell_output->fade.view == NULL) {
4116 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4117 if (!shell_output->fade.view)
4118 continue;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004119
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004120 shell_output->fade.view->alpha = 1.0 - tint;
4121 weston_view_update_transform(shell_output->fade.view);
4122 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004123
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004124 if (shell_output->fade.view->output == NULL) {
4125 /* If the black view gets a NULL output, we lost the
4126 * last output and we'll just cancel the fade. This
4127 * happens when you close the last window under the
4128 * X11 or Wayland backends. */
4129 shell->locked = false;
4130 weston_surface_destroy(shell_output->fade.view->surface);
4131 shell_output->fade.view = NULL;
4132 } else if (shell_output->fade.animation) {
4133 weston_fade_update(shell_output->fade.animation, tint);
4134 } else {
4135 shell_output->fade.animation =
4136 weston_fade_run(shell_output->fade.view,
4137 1.0 - tint, tint, 300.0,
4138 shell_fade_done_for_output, shell_output);
4139 }
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004140 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004141}
4142
4143static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004144do_shell_fade_startup(void *data)
4145{
4146 struct desktop_shell *shell = data;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004147 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004148
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004149 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004150 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004151 } else {
4152 weston_log("desktop shell: "
4153 "unexpected fade-in animation type %d\n",
4154 shell->startup_animation_type);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004155 wl_list_for_each(shell_output, &shell->output_list, link) {
4156 weston_surface_destroy(shell_output->fade.view->surface);
4157 shell_output->fade.view = NULL;
4158 }
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004159 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004160}
4161
4162static void
4163shell_fade_startup(struct desktop_shell *shell)
4164{
4165 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004166 struct shell_output *shell_output;
4167 bool has_fade = false;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004168
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004169 wl_list_for_each(shell_output, &shell->output_list, link) {
4170 if (!shell_output->fade.startup_timer)
4171 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004172
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004173 wl_event_source_remove(shell_output->fade.startup_timer);
4174 shell_output->fade.startup_timer = NULL;
4175 has_fade = true;
4176 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004177
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004178 if (has_fade) {
4179 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4180 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4181 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004182}
4183
4184static int
4185fade_startup_timeout(void *data)
4186{
4187 struct desktop_shell *shell = data;
4188
4189 shell_fade_startup(shell);
4190 return 0;
4191}
4192
4193static void
4194shell_fade_init(struct desktop_shell *shell)
4195{
4196 /* Make compositor output all black, and wait for the desktop-shell
4197 * client to signal it is ready, then fade in. The timer triggers a
4198 * fade-in, in case the desktop-shell client takes too long.
4199 */
4200
4201 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004202 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004203
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004204 if (shell->startup_animation_type == ANIMATION_NONE)
4205 return;
4206
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004207 wl_list_for_each(shell_output, &shell->output_list, link) {
4208 if (shell_output->fade.view != NULL) {
4209 weston_log("%s: warning: fade surface already exists\n",
4210 __func__);
4211 continue;
4212 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004213
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004214 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4215 if (!shell_output->fade.view)
4216 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004217
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004218 weston_view_update_transform(shell_output->fade.view);
4219 weston_surface_damage(shell_output->fade.view->surface);
4220
4221 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4222 shell_output->fade.startup_timer =
4223 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4224 wl_event_source_timer_update(shell_output->fade.startup_timer, 15000);
4225 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004226}
4227
4228static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004229idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004230{
4231 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004232 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004233
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004234 struct weston_seat *seat;
4235
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004236 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4237 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004238
4239 shell_fade(shell, FADE_OUT);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004240 /* lock() is called from shell_fade_done_for_output() */
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004241}
4242
4243static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004244wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004245{
4246 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004247 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004248
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004249 unlock(shell);
4250}
4251
4252static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004253transform_handler(struct wl_listener *listener, void *data)
4254{
4255 struct weston_surface *surface = data;
4256 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004257 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004258 int x, y;
4259
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004260 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004261 return;
4262
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004263 api = shsurf->shell->xwayland_surface_api;
4264 if (!api) {
4265 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
4266 shsurf->shell->xwayland_surface_api = api;
4267 }
4268
4269 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004270 return;
4271
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004272 if (!weston_view_is_mapped(shsurf->view))
4273 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004274
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004275 x = shsurf->view->geometry.x;
4276 y = shsurf->view->geometry.y;
4277
4278 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004279}
4280
4281static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004282center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004283{
Giulio Camuffob8366642013-04-25 13:57:46 +03004284 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004285 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004286
Pekka Paalanen30aa5972018-05-02 10:21:56 +02004287 if (!output) {
4288 weston_view_set_position(view, 0, 0);
4289 return;
4290 }
4291
Jason Ekstranda7af7042013-10-12 22:38:11 -05004292 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004293
4294 x = output->x + (output->width - width) / 2 - surf_x / 2;
4295 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004296
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004297 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004298}
4299
4300static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004301weston_view_set_initial_position(struct weston_view *view,
4302 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004303{
4304 struct weston_compositor *compositor = shell->compositor;
4305 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004306 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004307 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004308 struct weston_output *output, *target_output = NULL;
4309 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004310 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004311
4312 /* As a heuristic place the new window on the same output as the
4313 * pointer. Falling back to the output containing 0, 0.
4314 *
4315 * TODO: Do something clever for touch too?
4316 */
4317 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004318 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4319
4320 if (pointer) {
4321 ix = wl_fixed_to_int(pointer->x);
4322 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004323 break;
4324 }
4325 }
4326
4327 wl_list_for_each(output, &compositor->output_list, link) {
4328 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4329 target_output = output;
4330 break;
4331 }
4332 }
4333
4334 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004335 weston_view_set_position(view, 10 + random() % 400,
4336 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004337 return;
4338 }
4339
4340 /* Valid range within output where the surface will still be onscreen.
4341 * If this is negative it means that the surface is bigger than
4342 * output.
4343 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004344 get_output_work_area(shell, target_output, &area);
4345
Derek Foremanf814c5d2015-04-15 12:23:54 -05004346 x = area.x;
4347 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004348 range_x = area.width - view->surface->width;
4349 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004350
Rob Bradford4cb88c72012-08-13 15:18:44 +01004351 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004352 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004353
4354 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004355 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004356
Jason Ekstranda7af7042013-10-12 22:38:11 -05004357 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004358}
4359
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004360static bool
4361check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4362{
4363 struct timespec now;
4364
4365 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4366 return false;
4367
4368 /*
4369 * If the shell helper client dies before the session has been
4370 * up for roughly 30 seconds, better just make Weston shut down,
4371 * because the user likely has no way to interact with the desktop
4372 * anyway.
4373 */
4374 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4375 weston_log("Error: %s apparently cannot run at all.\n",
4376 shell->client);
4377 weston_log_continue(STAMP_SPACE "Quitting...");
Pekka Paalanen052032d2019-02-06 10:44:37 +02004378 weston_compositor_exit_with_code(shell->compositor,
4379 EXIT_FAILURE);
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004380
4381 return true;
4382 }
4383
4384 return false;
4385}
4386
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004387static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004388
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004389static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004390respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004391{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004392 struct timespec time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004393
4394 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004395 weston_compositor_get_time(&time);
4396 if (timespec_sub_to_msec(&time, &shell->child.deathstamp) > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004397 shell->child.deathstamp = time;
4398 shell->child.deathcount = 0;
4399 }
4400
4401 shell->child.deathcount++;
4402 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004403 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004404 return;
4405 }
4406
Pekka Paalanen826dc142014-08-27 12:11:53 +03004407 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004408 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004409}
4410
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004411static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004412desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4413{
4414 struct desktop_shell *shell;
4415
4416 shell = container_of(listener, struct desktop_shell,
4417 child.client_destroy_listener);
4418
Pekka Paalanen826dc142014-08-27 12:11:53 +03004419 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004420 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004421 /*
4422 * unbind_desktop_shell() will reset shell->child.desktop_shell
4423 * before the respawned process has a chance to create a new
4424 * desktop_shell object, because we are being called from the
4425 * wl_client destructor which destroys all wl_resources before
4426 * returning.
4427 */
4428
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004429 if (!check_desktop_shell_crash_too_early(shell))
4430 respawn_desktop_shell_process(shell);
4431
Pekka Paalanen826dc142014-08-27 12:11:53 +03004432 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004433}
4434
4435static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004436launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004437{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004438 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004439
Pekka Paalanen826dc142014-08-27 12:11:53 +03004440 shell->child.client = weston_client_start(shell->compositor,
4441 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004442
Arnaud Vrac42631192014-08-25 20:56:46 +02004443 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004444 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004445 return;
4446 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004447
4448 shell->child.client_destroy_listener.notify =
4449 desktop_shell_client_destroy;
4450 wl_client_add_destroy_listener(shell->child.client,
4451 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004452}
4453
4454static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004455unbind_desktop_shell(struct wl_resource *resource)
4456{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004457 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004458
4459 if (shell->locked)
4460 resume_desktop(shell);
4461
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004462 shell->child.desktop_shell = NULL;
4463 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004464}
4465
4466static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004467bind_desktop_shell(struct wl_client *client,
4468 void *data, uint32_t version, uint32_t id)
4469{
Tiago Vignattibe143262012-04-16 17:31:41 +03004470 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004471 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004472
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004473 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4474 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004475
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004476 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004477 wl_resource_set_implementation(resource,
4478 &desktop_shell_implementation,
4479 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004480 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004481 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004482 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004483
4484 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4485 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004486}
4487
Kristian Høgsberg07045392012-02-19 18:52:44 -05004488struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004489 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004490 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004491 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004492 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004493 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004494};
4495
4496static void
4497switcher_next(struct switcher *switcher)
4498{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004499 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004500 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004501 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004502 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004503
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004504 /* temporary re-display minimized surfaces */
4505 struct weston_view *tmp;
4506 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004507 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4508 weston_layer_entry_remove(&view->layer_link);
4509 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004510 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4511 *minimized = view;
4512 }
4513
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004514 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004515 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004516 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004517 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004518 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004519 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004520 next = view;
4521 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004522 view->alpha = 0.25;
4523 weston_view_geometry_dirty(view);
4524 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004525 }
Alex Wu1659daa2012-04-01 20:13:09 +08004526
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004527 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004528 view->alpha = 0.25;
4529 weston_view_geometry_dirty(view);
4530 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004531 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004532 }
4533
4534 if (next == NULL)
4535 next = first;
4536
Alex Wu07b26062012-03-12 16:06:01 +08004537 if (next == NULL)
4538 return;
4539
Kristian Høgsberg07045392012-02-19 18:52:44 -05004540 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004541 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004542
4543 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004544 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004545 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004546
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004547 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004548 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004549 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004550}
4551
4552static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004553switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004554{
4555 struct switcher *switcher =
4556 container_of(listener, struct switcher, listener);
4557
4558 switcher_next(switcher);
4559}
4560
4561static void
Daniel Stone351eb612012-05-31 15:27:47 -04004562switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004563{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004564 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004565 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004566 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004567
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004568 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004569 if (is_focus_view(view))
4570 continue;
4571
Jason Ekstranda7af7042013-10-12 22:38:11 -05004572 view->alpha = 1.0;
4573 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004574 }
4575
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004576 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004577 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004578 keyboard->seat,
4579 WESTON_ACTIVATE_FLAG_CONFIGURE);
4580 }
4581
Kristian Høgsberg07045392012-02-19 18:52:44 -05004582 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004583 weston_keyboard_end_grab(keyboard);
4584 if (keyboard->input_method_resource)
4585 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004586
4587 /* re-hide surfaces that were temporary shown during the switch */
4588 struct weston_view **minimized;
4589 wl_array_for_each(minimized, &switcher->minimized_array) {
4590 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004591 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004592 weston_layer_entry_remove(&(*minimized)->layer_link);
4593 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004594 weston_view_damage_below(*minimized);
4595 }
4596 }
4597 wl_array_release(&switcher->minimized_array);
4598
Kristian Høgsberg07045392012-02-19 18:52:44 -05004599 free(switcher);
4600}
4601
4602static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004603switcher_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004604 const struct timespec *time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004605{
4606 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004607 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004608
Daniel Stonec9785ea2012-05-30 16:31:52 +01004609 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004610 switcher_next(switcher);
4611}
4612
4613static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004614switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004615 uint32_t mods_depressed, uint32_t mods_latched,
4616 uint32_t mods_locked, uint32_t group)
4617{
4618 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004619 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004620
Daniel Stone351eb612012-05-31 15:27:47 -04004621 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4622 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004623}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004624
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004625static void
4626switcher_cancel(struct weston_keyboard_grab *grab)
4627{
4628 struct switcher *switcher = container_of(grab, struct switcher, grab);
4629
4630 switcher_destroy(switcher);
4631}
4632
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004633static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004634 switcher_key,
4635 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004636 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004637};
4638
4639static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004640switcher_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004641 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004642{
Tiago Vignattibe143262012-04-16 17:31:41 +03004643 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004644 struct switcher *switcher;
4645
4646 switcher = malloc(sizeof *switcher);
ganjingc1e71512020-07-28 15:28:45 +08004647 if (!switcher)
4648 return;
Pekka Paalanen4bb326b2021-05-14 14:37:46 +03004649
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004650 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004651 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004652 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004653 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004654 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004655
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004656 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004657 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004658 weston_keyboard_start_grab(keyboard, &switcher->grab);
4659 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004660 switcher_next(switcher);
4661}
4662
Pekka Paalanen3c647232011-12-22 13:43:43 +02004663static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004664backlight_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004665 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004666{
4667 struct weston_compositor *compositor = data;
4668 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004669 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004670
4671 /* TODO: we're limiting to simple use cases, where we assume just
4672 * control on the primary display. We'd have to extend later if we
4673 * ever get support for setting backlights on random desktop LCD
4674 * panels though */
4675 output = get_default_output(compositor);
4676 if (!output)
4677 return;
4678
4679 if (!output->set_backlight)
4680 return;
4681
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004682 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4683 backlight_new = output->backlight_current - 25;
4684 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4685 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004686
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004687 if (backlight_new < 5)
4688 backlight_new = 5;
4689 if (backlight_new > 255)
4690 backlight_new = 255;
4691
4692 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004693 output->set_backlight(output, output->backlight_current);
4694}
4695
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004696static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004697force_kill_binding(struct weston_keyboard *keyboard,
4698 const struct timespec *time, uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004699{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004700 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004701 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004702 struct desktop_shell *shell = data;
4703 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004704 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004705
Derek Foreman8ae2db52015-07-15 13:00:36 -05004706 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004707 if (!focus_surface)
4708 return;
4709
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004710 wl_signal_emit(&compositor->kill_signal, focus_surface);
4711
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004712 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004713 wl_client_get_credentials(client, &pid, NULL, NULL);
4714
4715 /* Skip clients that we launched ourselves (the credentials of
4716 * the socketpair is ours) */
4717 if (pid == getpid())
4718 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004719
Daniel Stone325fc2d2012-05-30 16:31:58 +01004720 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004721}
4722
4723static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004724workspace_up_binding(struct weston_keyboard *keyboard,
4725 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004726{
4727 struct desktop_shell *shell = data;
4728 unsigned int new_index = shell->workspaces.current;
4729
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004730 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004731 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004732 if (new_index != 0)
4733 new_index--;
4734
4735 change_workspace(shell, new_index);
4736}
4737
4738static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004739workspace_down_binding(struct weston_keyboard *keyboard,
4740 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004741{
4742 struct desktop_shell *shell = data;
4743 unsigned int new_index = shell->workspaces.current;
4744
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004745 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004746 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004747 if (new_index < shell->workspaces.num - 1)
4748 new_index++;
4749
4750 change_workspace(shell, new_index);
4751}
4752
4753static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004754workspace_f_binding(struct weston_keyboard *keyboard,
4755 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004756{
4757 struct desktop_shell *shell = data;
4758 unsigned int new_index;
4759
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004760 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004761 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004762 new_index = key - KEY_F1;
4763 if (new_index >= shell->workspaces.num)
4764 new_index = shell->workspaces.num - 1;
4765
4766 change_workspace(shell, new_index);
4767}
4768
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004769static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004770workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004771 const struct timespec *time, uint32_t key,
4772 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004773{
4774 struct desktop_shell *shell = data;
4775 unsigned int new_index = shell->workspaces.current;
4776
4777 if (shell->locked)
4778 return;
4779
4780 if (new_index != 0)
4781 new_index--;
4782
Derek Foreman8ae2db52015-07-15 13:00:36 -05004783 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004784}
4785
4786static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004787workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004788 const struct timespec *time, uint32_t key,
4789 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004790{
4791 struct desktop_shell *shell = data;
4792 unsigned int new_index = shell->workspaces.current;
4793
4794 if (shell->locked)
4795 return;
4796
4797 if (new_index < shell->workspaces.num - 1)
4798 new_index++;
4799
Derek Foreman8ae2db52015-07-15 13:00:36 -05004800 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004801}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004802
4803static void
Harish Krupodc3edc02019-04-20 17:50:18 +05304804shell_reposition_view_on_output_change(struct weston_view *view)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004805{
4806 struct weston_output *output, *first_output;
4807 struct weston_compositor *ec = view->surface->compositor;
4808 struct shell_surface *shsurf;
4809 float x, y;
4810 int visible;
4811
Harish Krupo2dff4dd2019-04-20 18:10:56 +05304812 if (wl_list_empty(&ec->output_list))
4813 return;
4814
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004815 x = view->geometry.x;
4816 y = view->geometry.y;
4817
4818 /* At this point the destroyed output is not in the list anymore.
4819 * If the view is still visible somewhere, we leave where it is,
4820 * otherwise, move it to the first output. */
4821 visible = 0;
4822 wl_list_for_each(output, &ec->output_list, link) {
4823 if (pixman_region32_contains_point(&output->region,
4824 x, y, NULL)) {
4825 visible = 1;
4826 break;
4827 }
4828 }
4829
4830 if (!visible) {
4831 first_output = container_of(ec->output_list.next,
4832 struct weston_output, link);
4833
4834 x = first_output->x + first_output->width / 4;
4835 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004836
4837 weston_view_set_position(view, x, y);
4838 } else {
4839 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004840 }
4841
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004842
4843 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004844 if (!shsurf)
4845 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004846
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004847 shsurf->saved_position_valid = false;
4848 set_maximized(shsurf, false);
4849 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004850}
4851
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004852void
4853shell_for_each_layer(struct desktop_shell *shell,
4854 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004855{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004856 struct workspace **ws;
4857
4858 func(shell, &shell->fullscreen_layer, data);
4859 func(shell, &shell->panel_layer, data);
4860 func(shell, &shell->background_layer, data);
4861 func(shell, &shell->lock_layer, data);
4862 func(shell, &shell->input_panel_layer, data);
4863
4864 wl_array_for_each(ws, &shell->workspaces.array)
4865 func(shell, &(*ws)->layer, data);
4866}
4867
4868static void
Harish Krupodc3edc02019-04-20 17:50:18 +05304869shell_output_changed_move_layer(struct desktop_shell *shell,
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004870 struct weston_layer *layer,
4871 void *data)
4872{
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004873 struct weston_view *view;
4874
Marius Vladea40d6d2018-02-08 18:46:42 +02004875 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Harish Krupodc3edc02019-04-20 17:50:18 +05304876 shell_reposition_view_on_output_change(view);
4877
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004878}
4879
4880static void
Pekka Paalanen719ca872021-05-24 16:53:40 +03004881shell_output_destroy(struct shell_output *shell_output)
Xiong Zhang6b481422013-10-23 13:58:32 +08004882{
Pekka Paalanen0d5e4ff2021-05-24 16:13:21 +03004883 struct desktop_shell *shell = shell_output->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004884
Harish Krupodc3edc02019-04-20 17:50:18 +05304885 shell_for_each_layer(shell, shell_output_changed_move_layer, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004886
Pekka Paalanen719ca872021-05-24 16:53:40 +03004887 if (shell_output->fade.animation) {
4888 weston_view_animation_destroy(shell_output->fade.animation);
4889 shell_output->fade.animation = NULL;
4890 }
4891
4892 if (shell_output->fade.view) {
4893 /* destroys the view as well */
4894 weston_surface_destroy(shell_output->fade.view->surface);
4895 }
4896
4897 if (shell_output->fade.startup_timer)
4898 wl_event_source_remove(shell_output->fade.startup_timer);
4899
Pekka Paalanen0d5e4ff2021-05-24 16:13:21 +03004900 if (shell_output->panel_surface)
4901 wl_list_remove(&shell_output->panel_surface_listener.link);
4902 if (shell_output->background_surface)
4903 wl_list_remove(&shell_output->background_surface_listener.link);
4904 wl_list_remove(&shell_output->destroy_listener.link);
4905 wl_list_remove(&shell_output->link);
4906 free(shell_output);
Xiong Zhang6b481422013-10-23 13:58:32 +08004907}
4908
4909static void
Pekka Paalanen719ca872021-05-24 16:53:40 +03004910handle_output_destroy(struct wl_listener *listener, void *data)
4911{
4912 struct shell_output *shell_output =
4913 container_of(listener, struct shell_output, destroy_listener);
4914
4915 shell_output_destroy(shell_output);
4916}
4917
4918static void
David Fort50d962f2016-06-09 17:55:52 +02004919shell_resize_surface_to_output(struct desktop_shell *shell,
4920 struct weston_surface *surface,
4921 const struct weston_output *output)
4922{
4923 if (!surface)
4924 return;
4925
4926 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4927 surface->resource,
4928 output->width,
4929 output->height);
4930}
4931
4932
4933static void
4934handle_output_resized(struct wl_listener *listener, void *data)
4935{
4936 struct desktop_shell *shell =
4937 container_of(listener, struct desktop_shell, resized_listener);
4938 struct weston_output *output = (struct weston_output *)data;
4939 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4940
4941 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4942 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4943}
4944
4945static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004946create_shell_output(struct desktop_shell *shell,
4947 struct weston_output *output)
4948{
4949 struct shell_output *shell_output;
4950
4951 shell_output = zalloc(sizeof *shell_output);
4952 if (shell_output == NULL)
4953 return;
4954
4955 shell_output->output = output;
4956 shell_output->shell = shell;
4957 shell_output->destroy_listener.notify = handle_output_destroy;
4958 wl_signal_add(&output->destroy_signal,
4959 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004960 wl_list_insert(shell->output_list.prev, &shell_output->link);
Harish Krupodc3edc02019-04-20 17:50:18 +05304961
4962 if (wl_list_length(&shell->output_list) == 1)
4963 shell_for_each_layer(shell,
4964 shell_output_changed_move_layer, NULL);
Xiong Zhang6b481422013-10-23 13:58:32 +08004965}
4966
4967static void
4968handle_output_create(struct wl_listener *listener, void *data)
4969{
4970 struct desktop_shell *shell =
4971 container_of(listener, struct desktop_shell, output_create_listener);
4972 struct weston_output *output = (struct weston_output *)data;
4973
4974 create_shell_output(shell, output);
4975}
4976
4977static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004978handle_output_move_layer(struct desktop_shell *shell,
4979 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004980{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004981 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004982 struct weston_view *view;
4983 float x, y;
4984
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004985 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004986 if (view->output != output)
4987 continue;
4988
4989 x = view->geometry.x + output->move_x;
4990 y = view->geometry.y + output->move_y;
4991 weston_view_set_position(view, x, y);
4992 }
4993}
4994
4995static void
4996handle_output_move(struct wl_listener *listener, void *data)
4997{
4998 struct desktop_shell *shell;
4999
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005000 shell = container_of(listener, struct desktop_shell,
5001 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005002
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03005003 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005004}
5005
5006static void
Xiong Zhang6b481422013-10-23 13:58:32 +08005007setup_output_destroy_handler(struct weston_compositor *ec,
5008 struct desktop_shell *shell)
5009{
5010 struct weston_output *output;
5011
5012 wl_list_init(&shell->output_list);
5013 wl_list_for_each(output, &ec->output_list, link)
5014 create_shell_output(shell, output);
5015
5016 shell->output_create_listener.notify = handle_output_create;
5017 wl_signal_add(&ec->output_created_signal,
5018 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02005019
5020 shell->output_move_listener.notify = handle_output_move;
5021 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08005022}
5023
5024static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005025shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02005026{
Tiago Vignattibe143262012-04-16 17:31:41 +03005027 struct desktop_shell *shell =
5028 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005029 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08005030 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02005031
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08005032 /* Force state to unlocked so we don't try to fade */
5033 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005034
5035 if (shell->child.client) {
5036 /* disable respawn */
5037 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005038 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03005039 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005040
Harsha M Mb8b2c722018-08-07 19:05:02 +05305041 wl_list_remove(&shell->destroy_listener.link);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005042 wl_list_remove(&shell->idle_listener.link);
5043 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005044 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005045
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03005046 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005047 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005048
Pekka Paalanen719ca872021-05-24 16:53:40 +03005049 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link)
5050 shell_output_destroy(shell_output);
Xiong Zhang6b481422013-10-23 13:58:32 +08005051
5052 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07005053 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02005054 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005055
Pekka Paalanenabd72922021-05-24 14:42:00 +03005056 weston_desktop_destroy(shell->desktop);
5057
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005058 wl_array_for_each(ws, &shell->workspaces.array)
5059 workspace_destroy(*ws);
5060 wl_array_release(&shell->workspaces.array);
5061
Pekka Paalanen4bb326b2021-05-14 14:37:46 +03005062 weston_layer_fini(&shell->fullscreen_layer);
5063 weston_layer_fini(&shell->panel_layer);
5064 weston_layer_fini(&shell->background_layer);
5065 weston_layer_fini(&shell->lock_layer);
5066 weston_layer_fini(&shell->input_panel_layer);
5067 weston_layer_fini(&shell->minimized_layer);
5068
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005069 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005070 free(shell);
5071}
5072
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005073static void
5074shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5075{
5076 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005077 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005078
Bob Ham744e6532016-01-12 10:21:48 +00005079 if (shell->allow_zap)
5080 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5081 MODIFIER_CTRL | MODIFIER_ALT,
5082 terminate_binding, ec);
5083
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005084 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005085 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5086 click_to_activate_binding,
5087 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07005088 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
5089 click_to_activate_binding,
5090 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005091 weston_compositor_add_touch_binding(ec, 0,
5092 touch_to_activate_binding,
5093 shell);
Bob Ham553d1242016-01-12 10:21:49 +00005094 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5095 backlight_binding, ec);
5096 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5097 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005098
5099 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00005100 if (shell->exposay_modifier)
5101 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5102 exposay_binding, shell);
5103
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005104 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00005105 if (!mod)
5106 return;
5107
Ian Ray13404c42017-09-18 15:22:01 +03005108 /* This binding is not configurable, but is only enabled if there is a
5109 * valid binding modifier. */
5110 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5111 MODIFIER_SUPER | MODIFIER_ALT,
5112 surface_opacity_binding, NULL);
5113
Ian Rayab7c0b62017-09-18 15:22:00 +03005114 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5115 mod, zoom_axis_binding,
5116 NULL);
5117
Daniel Stone325fc2d2012-05-30 16:31:58 +01005118 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5119 zoom_key_binding, NULL);
5120 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5121 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005122 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5123 maximize_binding, NULL);
5124 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5125 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005126 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5127 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005128 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05005129 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005130 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08005131 weston_compositor_add_button_binding(ec, BTN_LEFT,
5132 mod | MODIFIER_SHIFT,
5133 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005134
5135 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05005136 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005137 rotate_binding, NULL);
5138
Daniel Stone325fc2d2012-05-30 16:31:58 +01005139 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5140 shell);
5141 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5142 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005143 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5144 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005145 weston_compositor_add_key_binding(ec, KEY_K, mod,
5146 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005147 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5148 workspace_up_binding, shell);
5149 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5150 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005151 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5152 workspace_move_surface_up_binding,
5153 shell);
5154 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5155 workspace_move_surface_down_binding,
5156 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005157
5158 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5159 if (shell->workspaces.num > 1) {
5160 num_workspace_bindings = shell->workspaces.num;
5161 if (num_workspace_bindings > 6)
5162 num_workspace_bindings = 6;
5163 for (i = 0; i < num_workspace_bindings; i++)
5164 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5165 workspace_f_binding,
5166 shell);
5167 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005168
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02005169 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005170}
5171
Jason Ekstrand024177c2014-04-21 19:42:58 -05005172static void
5173handle_seat_created(struct wl_listener *listener, void *data)
5174{
5175 struct weston_seat *seat = data;
5176
5177 create_shell_seat(seat);
5178}
5179
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005180WL_EXPORT int
Quentin Glidicda01c1d2016-12-02 14:17:08 +01005181wet_shell_init(struct weston_compositor *ec,
5182 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005183{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005184 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005185 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005186 struct workspace **pws;
5187 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005188 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005189
Peter Huttererf3d62272013-08-08 11:57:05 +10005190 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005191 if (shell == NULL)
5192 return -1;
5193
Kristian Høgsberg75840622011-09-06 13:48:16 -04005194 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005195
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02005196 if (!weston_compositor_add_destroy_listener_once(ec,
5197 &shell->destroy_listener,
5198 shell_destroy)) {
5199 free(shell);
5200 return 0;
5201 }
5202
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005203 shell->idle_listener.notify = idle_handler;
5204 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5205 shell->wake_listener.notify = wake_handler;
5206 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005207 shell->transform_listener.notify = transform_handler;
5208 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005209
Quentin Glidic82681572016-12-17 13:40:51 +01005210 weston_layer_init(&shell->fullscreen_layer, ec);
5211 weston_layer_init(&shell->panel_layer, ec);
5212 weston_layer_init(&shell->background_layer, ec);
5213 weston_layer_init(&shell->lock_layer, ec);
5214 weston_layer_init(&shell->input_panel_layer, ec);
5215
5216 weston_layer_set_position(&shell->fullscreen_layer,
5217 WESTON_LAYER_POSITION_FULLSCREEN);
5218 weston_layer_set_position(&shell->panel_layer,
5219 WESTON_LAYER_POSITION_UI);
5220 weston_layer_set_position(&shell->background_layer,
5221 WESTON_LAYER_POSITION_BACKGROUND);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005222
5223 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005224 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005225
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005226 if (input_panel_setup(shell) < 0)
5227 return -1;
5228
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03005229 shell->text_backend = text_backend_init(ec);
5230 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00005231 return -1;
5232
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005233 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005234
Daniel Stonedf8133b2013-11-19 11:37:14 +01005235 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5236 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5237
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005238 for (i = 0; i < shell->workspaces.num; i++) {
5239 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5240 if (pws == NULL)
5241 return -1;
5242
Quentin Glidic82681572016-12-17 13:40:51 +01005243 *pws = workspace_create(shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005244 if (*pws == NULL)
5245 return -1;
5246 }
5247 activate_workspace(shell, 0);
5248
Quentin Glidic82681572016-12-17 13:40:51 +01005249 weston_layer_init(&shell->minimized_layer, ec);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01005250
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005251 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005252 wl_list_init(&shell->workspaces.animation.link);
5253 shell->workspaces.animation.frame = animate_workspace_change_frame;
5254
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02005255 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
5256 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005257 return -1;
5258
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005259 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005260 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005261 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005262 return -1;
5263
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02005264 weston_compositor_get_time(&shell->child.deathstamp);
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005265
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005266 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02005267
Xiong Zhang6b481422013-10-23 13:58:32 +08005268 setup_output_destroy_handler(ec, shell);
5269
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005270 loop = wl_display_get_event_loop(ec->wl_display);
5271 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005272
Jason Ekstrand024177c2014-04-21 19:42:58 -05005273 wl_list_for_each(seat, &ec->seat_list, link)
5274 handle_seat_created(NULL, seat);
5275 shell->seat_create_listener.notify = handle_seat_created;
5276 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005277
David Fort50d962f2016-06-09 17:55:52 +02005278 shell->resized_listener.notify = handle_output_resized;
5279 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
5280
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01005281 screenshooter_create(ec);
5282
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005283 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005284
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005285 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005286
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005287 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
5288
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005289 return 0;
5290}