blob: c1c126e8184c06703417e115dad36b4f11e1d7bb [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
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200897 free(ws);
898}
899
Jonas Ådahl04769742012-06-13 00:01:24 +0200900static void
901seat_destroyed(struct wl_listener *listener, void *data)
902{
903 struct weston_seat *seat = data;
904 struct focus_state *state, *next;
905 struct workspace *ws = container_of(listener,
906 struct workspace,
907 seat_destroyed_listener);
908
909 wl_list_for_each_safe(state, next, &ws->focus_list, link)
910 if (state->seat == seat)
911 wl_list_remove(&state->link);
912}
913
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200914static struct workspace *
Quentin Glidic82681572016-12-17 13:40:51 +0100915workspace_create(struct desktop_shell *shell)
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200916{
917 struct workspace *ws = malloc(sizeof *ws);
918 if (ws == NULL)
919 return NULL;
920
Quentin Glidic82681572016-12-17 13:40:51 +0100921 weston_layer_init(&ws->layer, shell->compositor);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200922
Jonas Ådahl04769742012-06-13 00:01:24 +0200923 wl_list_init(&ws->focus_list);
924 wl_list_init(&ws->seat_destroyed_listener.link);
925 ws->seat_destroyed_listener.notify = seat_destroyed;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100926 ws->fsurf_front = NULL;
927 ws->fsurf_back = NULL;
928 ws->focus_animation = NULL;
Jonas Ådahl04769742012-06-13 00:01:24 +0200929
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200930 return ws;
931}
932
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200933static int
934workspace_is_empty(struct workspace *ws)
935{
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300936 return wl_list_empty(&ws->layer.view_list.link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200937}
938
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200939static struct workspace *
940get_workspace(struct desktop_shell *shell, unsigned int index)
941{
942 struct workspace **pws = shell->workspaces.array.data;
Philipp Brüschweiler067abf62012-09-01 16:03:05 +0200943 assert(index < shell->workspaces.num);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200944 pws += index;
945 return *pws;
946}
947
Kristian Høgsberg1ef23132013-12-04 00:20:01 -0800948struct workspace *
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200949get_current_workspace(struct desktop_shell *shell)
950{
951 return get_workspace(shell, shell->workspaces.current);
952}
953
954static void
955activate_workspace(struct desktop_shell *shell, unsigned int index)
956{
957 struct workspace *ws;
958
959 ws = get_workspace(shell, index);
Quentin Glidic82681572016-12-17 13:40:51 +0100960 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Jonas Ådahle3cddce2012-06-13 00:01:22 +0200961
962 shell->workspaces.current = index;
963}
964
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200965static unsigned int
966get_output_height(struct weston_output *output)
967{
968 return abs(output->region.extents.y1 - output->region.extents.y2);
969}
970
Greg Vf57774e2018-07-24 23:21:55 +0300971static struct weston_transform *
972view_get_transform(struct weston_view *view)
973{
974 struct focus_surface *fsurf = NULL;
975 struct shell_surface *shsurf = NULL;
976
977 if (is_focus_view(view)) {
978 fsurf = get_focus_surface(view->surface);
979 return &fsurf->workspace_transform;
980 }
981
982 shsurf = get_shell_surface(view->surface);
983 if (shsurf)
984 return &shsurf->workspace_transform;
985
986 return NULL;
987}
988
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200989static void
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100990view_translate(struct workspace *ws, struct weston_view *view, double d)
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200991{
Greg Vf57774e2018-07-24 23:21:55 +0300992 struct weston_transform *transform = view_get_transform(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200993
Greg Vf57774e2018-07-24 23:21:55 +0300994 if (!transform)
995 return;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100996
Jonas Ådahl62fcd042012-06-13 00:01:23 +0200997 if (wl_list_empty(&transform->link))
Jason Ekstranda7af7042013-10-12 22:38:11 -0500998 wl_list_insert(view->geometry.transformation_list.prev,
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +0100999 &transform->link);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001000
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001001 weston_matrix_init(&transform->matrix);
1002 weston_matrix_translate(&transform->matrix,
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001003 0.0, d, 0.0);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001004 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001005}
1006
1007static void
1008workspace_translate_out(struct workspace *ws, double fraction)
1009{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001010 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001011 unsigned int height;
1012 double d;
1013
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001014 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001015 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001016 d = height * fraction;
1017
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001018 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001019 }
1020}
1021
1022static void
1023workspace_translate_in(struct workspace *ws, double fraction)
1024{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001025 struct weston_view *view;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001026 unsigned int height;
1027 double d;
1028
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001029 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001030 height = get_output_height(view->surface->output);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001031
1032 if (fraction > 0)
1033 d = -(height - height * fraction);
1034 else
1035 d = height + height * fraction;
1036
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001037 view_translate(ws, view, d);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001038 }
1039}
1040
1041static void
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001042reverse_workspace_change_animation(struct desktop_shell *shell,
1043 unsigned int index,
1044 struct workspace *from,
1045 struct workspace *to)
1046{
1047 shell->workspaces.current = index;
1048
1049 shell->workspaces.anim_to = to;
1050 shell->workspaces.anim_from = from;
1051 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001052 shell->workspaces.anim_timestamp = (struct timespec) { 0 };
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001053
Quentin Glidic82681572016-12-17 13:40:51 +01001054 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1055 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
1056
Scott Moreau4272e632012-08-13 09:58:41 -06001057 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001058}
1059
1060static void
1061workspace_deactivate_transforms(struct workspace *ws)
1062{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001063 struct weston_view *view;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001064 struct weston_transform *transform;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001065
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001066 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Greg Vf57774e2018-07-24 23:21:55 +03001067 transform = view_get_transform(view);
1068 if (!transform)
1069 continue;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001070
1071 if (!wl_list_empty(&transform->link)) {
1072 wl_list_remove(&transform->link);
1073 wl_list_init(&transform->link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001074 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001075 weston_view_geometry_dirty(view);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001076 }
1077}
1078
1079static void
1080finish_workspace_change_animation(struct desktop_shell *shell,
1081 struct workspace *from,
1082 struct workspace *to)
1083{
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001084 struct weston_view *view;
1085
Scott Moreau4272e632012-08-13 09:58:41 -06001086 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001087
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001088 /* Views that extend past the bottom of the output are still
1089 * visible after the workspace animation ends but before its layer
1090 * is hidden. In that case, we need to damage below those views so
1091 * that the screen is properly repainted. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001092 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
Ander Conselvan de Oliveira9c6217e2014-05-07 11:57:26 +03001093 weston_view_damage_below(view);
1094
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001095 wl_list_remove(&shell->workspaces.animation.link);
1096 workspace_deactivate_transforms(from);
1097 workspace_deactivate_transforms(to);
1098 shell->workspaces.anim_to = NULL;
1099
Quentin Glidic82681572016-12-17 13:40:51 +01001100 weston_layer_unset_position(&shell->workspaces.anim_from->layer);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001101}
1102
1103static void
1104animate_workspace_change_frame(struct weston_animation *animation,
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001105 struct weston_output *output,
1106 const struct timespec *time)
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001107{
1108 struct desktop_shell *shell =
1109 container_of(animation, struct desktop_shell,
1110 workspaces.animation);
1111 struct workspace *from = shell->workspaces.anim_from;
1112 struct workspace *to = shell->workspaces.anim_to;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001113 int64_t t;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001114 double x, y;
1115
1116 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1117 finish_workspace_change_animation(shell, from, to);
1118 return;
1119 }
1120
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001121 if (timespec_is_zero(&shell->workspaces.anim_timestamp)) {
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001122 if (shell->workspaces.anim_current == 0.0)
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001123 shell->workspaces.anim_timestamp = *time;
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001124 else
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001125 timespec_add_msec(&shell->workspaces.anim_timestamp,
1126 time,
Emmanuel Gil Peyrot426c2462019-02-20 16:33:32 +01001127 /* Inverse of movement function 'y' below. */
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001128 -(asin(1.0 - shell->workspaces.anim_current) *
1129 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1130 M_2_PI));
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001131 }
1132
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001133 t = timespec_sub_to_msec(time, &shell->workspaces.anim_timestamp);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001134
1135 /*
1136 * x = [0, π/2]
1137 * y(x) = sin(x)
1138 */
1139 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1140 y = sin(x);
1141
1142 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
Scott Moreau4272e632012-08-13 09:58:41 -06001143 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001144
1145 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1146 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1147 shell->workspaces.anim_current = y;
1148
Scott Moreau4272e632012-08-13 09:58:41 -06001149 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001150 }
Jonas Ådahl04769742012-06-13 00:01:24 +02001151 else
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001152 finish_workspace_change_animation(shell, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001153}
1154
1155static void
1156animate_workspace_change(struct desktop_shell *shell,
1157 unsigned int index,
1158 struct workspace *from,
1159 struct workspace *to)
1160{
1161 struct weston_output *output;
1162
1163 int dir;
1164
1165 if (index > shell->workspaces.current)
1166 dir = -1;
1167 else
1168 dir = 1;
1169
1170 shell->workspaces.current = index;
1171
1172 shell->workspaces.anim_dir = dir;
1173 shell->workspaces.anim_from = from;
1174 shell->workspaces.anim_to = to;
1175 shell->workspaces.anim_current = 0.0;
Alexandros Frantzis8250a612017-11-16 18:20:52 +02001176 shell->workspaces.anim_timestamp = (struct timespec) { 0 };
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001177
1178 output = container_of(shell->compositor->output_list.next,
1179 struct weston_output, link);
1180 wl_list_insert(&output->animation_list,
1181 &shell->workspaces.animation.link);
1182
Quentin Glidic82681572016-12-17 13:40:51 +01001183 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1184 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001185
1186 workspace_translate_in(to, 0);
1187
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04001188 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001189
Scott Moreau4272e632012-08-13 09:58:41 -06001190 weston_compositor_schedule_repaint(shell->compositor);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001191}
1192
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001193static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001194update_workspace(struct desktop_shell *shell, unsigned int index,
1195 struct workspace *from, struct workspace *to)
1196{
1197 shell->workspaces.current = index;
Quentin Glidic82681572016-12-17 13:40:51 +01001198 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1199 weston_layer_unset_position(&from->layer);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001200}
1201
1202static void
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001203change_workspace(struct desktop_shell *shell, unsigned int index)
1204{
1205 struct workspace *from;
1206 struct workspace *to;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001207 struct focus_state *state;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001208
1209 if (index == shell->workspaces.current)
1210 return;
1211
1212 /* Don't change workspace when there is any fullscreen surfaces. */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001213 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001214 return;
1215
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001216 from = get_current_workspace(shell);
1217 to = get_workspace(shell, index);
1218
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001219 if (shell->workspaces.anim_from == to &&
1220 shell->workspaces.anim_to == from) {
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001221 restore_focus_state(shell, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001222 reverse_workspace_change_animation(shell, index, from, to);
1223 return;
1224 }
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001225
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001226 if (shell->workspaces.anim_to != NULL)
1227 finish_workspace_change_animation(shell,
1228 shell->workspaces.anim_from,
1229 shell->workspaces.anim_to);
1230
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001231 restore_focus_state(shell, to);
Jonas Ådahl04769742012-06-13 00:01:24 +02001232
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001233 if (shell->focus_animation_type != ANIMATION_NONE) {
1234 wl_list_for_each(state, &from->focus_list, link)
1235 if (state->keyboard_focus)
1236 animate_focus_change(shell, from,
1237 get_default_view(state->keyboard_focus), NULL);
1238
1239 wl_list_for_each(state, &to->focus_list, link)
1240 if (state->keyboard_focus)
1241 animate_focus_change(shell, to,
1242 NULL, get_default_view(state->keyboard_focus));
1243 }
1244
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001245 if (workspace_is_empty(to) && workspace_is_empty(from))
1246 update_workspace(shell, index, from, to);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02001247 else
1248 animate_workspace_change(shell, index, from, to);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001249}
1250
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001251static bool
1252workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1253{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001254 struct wl_list *list = &ws->layer.view_list.link;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001255 struct wl_list *e;
1256
1257 if (wl_list_empty(list))
1258 return false;
1259
1260 e = list->next;
1261
1262 if (e->next != list)
1263 return false;
1264
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001265 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001266}
1267
1268static void
Jonas Ådahl22faea12014-10-15 22:02:06 +02001269surface_keyboard_focus_lost(struct weston_surface *surface)
1270{
1271 struct weston_compositor *compositor = surface->compositor;
1272 struct weston_seat *seat;
1273 struct weston_surface *focus;
1274
1275 wl_list_for_each(seat, &compositor->seat_list, link) {
1276 struct weston_keyboard *keyboard =
1277 weston_seat_get_keyboard(seat);
1278
1279 if (!keyboard)
1280 continue;
1281
1282 focus = weston_surface_get_main_surface(keyboard->focus);
1283 if (focus == surface)
1284 weston_keyboard_set_focus(keyboard, NULL);
1285 }
1286}
1287
1288static void
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001289take_surface_to_workspace_by_seat(struct desktop_shell *shell,
Kristian Høgsberge3148752013-05-06 23:19:49 -04001290 struct weston_seat *seat,
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001291 unsigned int index)
1292{
Derek Foreman1281a362015-07-31 16:55:32 -05001293 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
Pekka Paalanen01388e22013-04-25 13:57:44 +03001294 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295 struct weston_view *view;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001296 struct shell_surface *shsurf;
1297 struct workspace *from;
1298 struct workspace *to;
Jonas Ådahl8538b222012-08-29 22:13:03 +02001299 struct focus_state *state;
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001300
Derek Foreman1281a362015-07-31 16:55:32 -05001301 surface = weston_surface_get_main_surface(keyboard->focus);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001302 view = get_default_view(surface);
1303 if (view == NULL ||
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01001304 index == shell->workspaces.current ||
1305 is_focus_view(view))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001306 return;
1307
1308 from = get_current_workspace(shell);
1309 to = get_workspace(shell, index);
1310
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001311 weston_layer_entry_remove(&view->layer_link);
1312 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001313
Philip Withnall659163d2013-11-25 18:01:36 +00001314 shsurf = get_shell_surface(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001315 if (shsurf != NULL)
1316 shell_surface_update_child_surface_layers(shsurf);
Philip Withnall659163d2013-11-25 18:01:36 +00001317
Jonas Ådahle9d22502012-08-29 22:13:01 +02001318 replace_focus_state(shell, to, seat);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001319 drop_focus_state(shell, from, surface);
1320
1321 if (shell->workspaces.anim_from == to &&
1322 shell->workspaces.anim_to == from) {
1323 reverse_workspace_change_animation(shell, index, from, to);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001324
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001325 return;
1326 }
1327
1328 if (shell->workspaces.anim_to != NULL)
1329 finish_workspace_change_animation(shell,
1330 shell->workspaces.anim_from,
1331 shell->workspaces.anim_to);
1332
1333 if (workspace_is_empty(from) &&
1334 workspace_has_only(to, surface))
1335 update_workspace(shell, index, from, to);
1336 else {
Philip Withnall2c3849b2013-11-25 18:01:45 +00001337 if (shsurf != NULL &&
1338 wl_list_empty(&shsurf->workspace_transform.link))
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02001339 wl_list_insert(&shell->workspaces.anim_sticky_list,
1340 &shsurf->workspace_transform.link);
1341
1342 animate_workspace_change(shell, index, from, to);
1343 }
Jonas Ådahle9d22502012-08-29 22:13:01 +02001344
Jonas Ådahl8538b222012-08-29 22:13:03 +02001345 state = ensure_focus_state(shell, seat);
1346 if (state != NULL)
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08001347 focus_state_set_focus(state, surface);
Jonas Ådahle9d22502012-08-29 22:13:01 +02001348}
1349
1350static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02001351touch_move_grab_down(struct weston_touch_grab *grab,
1352 const struct timespec *time,
Giulio Camuffo61ed7b62015-07-08 11:55:28 +03001353 int touch_id, wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001354{
1355}
1356
1357static void
Alexandros Frantzis27a51b82017-11-16 18:20:59 +02001358touch_move_grab_up(struct weston_touch_grab *grab, const struct timespec *time,
1359 int touch_id)
Rusty Lynch1084da52013-08-15 09:10:08 -07001360{
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001361 struct weston_touch_move_grab *move =
1362 (struct weston_touch_move_grab *) container_of(
1363 grab, struct shell_touch_grab, grab);
Neil Robertse14aa4f2013-10-03 16:43:07 +01001364
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001365 if (touch_id == 0)
1366 move->active = 0;
1367
Jonas Ådahl9484b692013-12-02 22:05:03 +01001368 if (grab->touch->num_tp == 0) {
Jonas Ådahl1c6e63e2013-10-25 23:18:04 +02001369 shell_touch_grab_end(&move->base);
1370 free(move);
1371 }
Rusty Lynch1084da52013-08-15 09:10:08 -07001372}
1373
1374static void
Alexandros Frantzis7d2abcf2017-11-16 18:21:00 +02001375touch_move_grab_motion(struct weston_touch_grab *grab,
1376 const struct timespec *time, int touch_id,
1377 wl_fixed_t x, wl_fixed_t y)
Rusty Lynch1084da52013-08-15 09:10:08 -07001378{
1379 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1380 struct shell_surface *shsurf = move->base.shsurf;
1381 struct weston_surface *es;
1382 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1383 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1384
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001385 if (!shsurf || !move->active)
Rusty Lynch1084da52013-08-15 09:10:08 -07001386 return;
1387
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001388 es = weston_desktop_surface_get_surface(shsurf->desktop_surface);
Rusty Lynch1084da52013-08-15 09:10:08 -07001389
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001390 weston_view_set_position(shsurf->view, dx, dy);
Rusty Lynch1084da52013-08-15 09:10:08 -07001391
1392 weston_compositor_schedule_repaint(es->compositor);
1393}
1394
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001395static void
Jonas Ådahl1679f232014-04-12 09:39:51 +02001396touch_move_grab_frame(struct weston_touch_grab *grab)
1397{
1398}
1399
1400static void
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001401touch_move_grab_cancel(struct weston_touch_grab *grab)
1402{
1403 struct weston_touch_move_grab *move =
1404 (struct weston_touch_move_grab *) container_of(
1405 grab, struct shell_touch_grab, grab);
1406
1407 shell_touch_grab_end(&move->base);
1408 free(move);
1409}
1410
Rusty Lynch1084da52013-08-15 09:10:08 -07001411static const struct weston_touch_grab_interface touch_move_grab_interface = {
1412 touch_move_grab_down,
1413 touch_move_grab_up,
1414 touch_move_grab_motion,
Jonas Ådahl1679f232014-04-12 09:39:51 +02001415 touch_move_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001416 touch_move_grab_cancel,
Rusty Lynch1084da52013-08-15 09:10:08 -07001417};
1418
1419static int
Derek Foremanb7674ae2015-07-15 13:00:37 -05001420surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
Rusty Lynch1084da52013-08-15 09:10:08 -07001421{
1422 struct weston_touch_move_grab *move;
1423
1424 if (!shsurf)
1425 return -1;
1426
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001427 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1428 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Rusty Lynch1084da52013-08-15 09:10:08 -07001429 return 0;
1430
1431 move = malloc(sizeof *move);
1432 if (!move)
1433 return -1;
1434
Kristian Høgsberg8e80a312014-01-17 15:18:10 -08001435 move->active = 1;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001436 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001437 touch->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001438 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001439 touch->grab_y;
Rusty Lynch1084da52013-08-15 09:10:08 -07001440
1441 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
Derek Foremanb7674ae2015-07-15 13:00:37 -05001442 touch);
Rusty Lynch1084da52013-08-15 09:10:08 -07001443
1444 return 0;
1445}
1446
1447static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001448noop_grab_focus(struct weston_pointer_grab *grab)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001449{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001450}
1451
1452static void
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001453noop_grab_axis(struct weston_pointer_grab *grab,
Alexandros Frantzis80321942017-11-16 18:20:56 +02001454 const struct timespec *time,
1455 struct weston_pointer_axis_event *event)
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001456{
1457}
1458
1459static void
Peter Hutterer87743e92016-01-18 16:38:22 +10001460noop_grab_axis_source(struct weston_pointer_grab *grab,
1461 uint32_t source)
1462{
1463}
1464
1465static void
1466noop_grab_frame(struct weston_pointer_grab *grab)
1467{
1468}
1469
1470static void
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001471constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1472{
1473 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001474 struct weston_surface *surface =
1475 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001476 struct weston_pointer *pointer = move->base.grab.pointer;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001477 int x, y, bottom;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001478 const int safety = 50;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001479 pixman_rectangle32_t area;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001480 struct weston_geometry geometry;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001481
1482 x = wl_fixed_to_int(pointer->x + move->dx);
1483 y = wl_fixed_to_int(pointer->y + move->dy);
1484
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001485 if (shsurf->shell->panel_position ==
1486 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001487 get_output_work_area(shsurf->shell, surface->output, &area);
1488 geometry =
1489 weston_desktop_surface_get_geometry(shsurf->desktop_surface);
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001490
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001491 bottom = y + geometry.height + geometry.y;
Derek Foreman6feb0f92015-04-15 12:23:56 -05001492 if (bottom - safety < area.y)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001493 y = area.y + safety - geometry.height
1494 - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001495
1496 if (move->client_initiated &&
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001497 y + geometry.y < area.y)
1498 y = area.y - geometry.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02001499 }
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001500
1501 *cx = x;
1502 *cy = y;
1503}
1504
1505static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001506move_grab_motion(struct weston_pointer_grab *grab,
1507 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001508 struct weston_pointer_motion_event *event)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001509{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001510 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001511 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001512 struct shell_surface *shsurf = move->base.shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001513 struct weston_surface *surface;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001514 int cx, cy;
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001515
Jonas Ådahld2510102014-10-05 21:39:14 +02001516 weston_pointer_move(pointer, event);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001517 if (!shsurf)
1518 return;
1519
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001520 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1521
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001522 constrain_position(move, &cx, &cy);
1523
1524 weston_view_set_position(shsurf->view, cx, cy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001525
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001526 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001527}
1528
1529static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001530move_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001531 const struct timespec *time, uint32_t button, uint32_t state_w)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001532{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001533 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1534 grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001535 struct weston_pointer *pointer = grab->pointer;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001536 enum wl_pointer_button_state state = state_w;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001537
Daniel Stone4dbadb12012-05-30 16:31:51 +01001538 if (pointer->button_count == 0 &&
1539 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03001540 shell_grab_end(shell_grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001541 free(grab);
1542 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001543}
1544
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001545static void
1546move_grab_cancel(struct weston_pointer_grab *grab)
1547{
1548 struct shell_grab *shell_grab =
1549 container_of(grab, struct shell_grab, grab);
1550
1551 shell_grab_end(shell_grab);
1552 free(grab);
1553}
1554
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001555static const struct weston_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001556 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001557 move_grab_motion,
1558 move_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001559 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001560 noop_grab_axis_source,
1561 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001562 move_grab_cancel,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001563};
1564
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001565static int
Derek Foreman794fa0e2015-07-15 13:00:38 -05001566surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
Derek Foremancf7d95a2015-06-03 15:53:22 -05001567 bool client_initiated)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001568{
1569 struct weston_move_grab *move;
1570
1571 if (!shsurf)
1572 return -1;
1573
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001574 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001575 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1576 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001577 return 0;
1578
1579 move = malloc(sizeof *move);
1580 if (!move)
1581 return -1;
1582
Jason Ekstranda7af7042013-10-12 22:38:11 -05001583 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001584 pointer->grab_x;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001585 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001586 pointer->grab_y;
Kristian Høgsbergae356ae2014-04-29 16:03:54 -07001587 move->client_initiated = client_initiated;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001588
1589 shell_grab_start(&move->base, &move_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08001590 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001591
1592 return 0;
1593}
1594
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001595struct weston_resize_grab {
1596 struct shell_grab base;
1597 uint32_t edges;
1598 int32_t width, height;
1599};
1600
1601static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001602resize_grab_motion(struct weston_pointer_grab *grab,
1603 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001604 struct weston_pointer_motion_event *event)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001605{
1606 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001607 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001608 struct shell_surface *shsurf = resize->base.shsurf;
1609 int32_t width, height;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001610 struct weston_size min_size, max_size;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001611 wl_fixed_t from_x, from_y;
1612 wl_fixed_t to_x, to_y;
1613
Jonas Ådahld2510102014-10-05 21:39:14 +02001614 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01001615
Kristian Høgsberge0b9d5b2014-04-30 13:45:49 -07001616 if (!shsurf)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001617 return;
1618
Jason Ekstranda7af7042013-10-12 22:38:11 -05001619 weston_view_from_global_fixed(shsurf->view,
1620 pointer->grab_x, pointer->grab_y,
1621 &from_x, &from_y);
1622 weston_view_from_global_fixed(shsurf->view,
1623 pointer->x, pointer->y, &to_x, &to_y);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001624
1625 width = resize->width;
1626 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1627 width += wl_fixed_to_int(from_x - to_x);
1628 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1629 width += wl_fixed_to_int(to_x - from_x);
1630 }
1631
1632 height = resize->height;
1633 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1634 height += wl_fixed_to_int(from_y - to_y);
1635 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1636 height += wl_fixed_to_int(to_y - from_y);
1637 }
1638
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001639 max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
1640 min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
1641
1642 min_size.width = MAX(1, min_size.width);
1643 min_size.height = MAX(1, min_size.height);
1644
1645 if (width < min_size.width)
1646 width = min_size.width;
1647 else if (max_size.width > 0 && width > max_size.width)
1648 width = max_size.width;
1649 if (height < min_size.height)
1650 height = min_size.height;
1651 else if (max_size.width > 0 && width > max_size.width)
1652 width = max_size.width;
1653 weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001654}
1655
1656static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001657resize_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001658 const struct timespec *time,
1659 uint32_t button, uint32_t state_w)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001660{
1661 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001662 struct weston_pointer *pointer = grab->pointer;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001663 enum wl_pointer_button_state state = state_w;
1664
1665 if (pointer->button_count == 0 &&
1666 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Greg Vec3f7792018-11-08 00:24:56 +03001667 if (resize->base.shsurf != NULL) {
1668 struct weston_desktop_surface *desktop_surface =
1669 resize->base.shsurf->desktop_surface;
1670 weston_desktop_surface_set_resizing(desktop_surface,
1671 false);
1672 }
1673
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001674 shell_grab_end(&resize->base);
1675 free(grab);
1676 }
1677}
1678
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001679static void
1680resize_grab_cancel(struct weston_pointer_grab *grab)
1681{
1682 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1683
Greg Vec3f7792018-11-08 00:24:56 +03001684 if (resize->base.shsurf != NULL) {
1685 struct weston_desktop_surface *desktop_surface =
1686 resize->base.shsurf->desktop_surface;
1687 weston_desktop_surface_set_resizing(desktop_surface, false);
1688 }
1689
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001690 shell_grab_end(&resize->base);
1691 free(grab);
1692}
1693
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001694static const struct weston_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001695 noop_grab_focus,
1696 resize_grab_motion,
1697 resize_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001698 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001699 noop_grab_axis_source,
1700 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001701 resize_grab_cancel,
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001702};
1703
Giulio Camuffob8366642013-04-25 13:57:46 +03001704/*
1705 * Returns the bounding box of a surface and all its sub-surfaces,
Yong Bakosbbb783a2016-04-28 11:59:06 -05001706 * in surface-local coordinates. */
Giulio Camuffob8366642013-04-25 13:57:46 +03001707static void
1708surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1709 int32_t *y, int32_t *w, int32_t *h) {
1710 pixman_region32_t region;
1711 pixman_box32_t *box;
1712 struct weston_subsurface *subsurface;
1713
1714 pixman_region32_init_rect(&region, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001715 surface->width,
1716 surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001717
1718 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1719 pixman_region32_union_rect(&region, &region,
1720 subsurface->position.x,
1721 subsurface->position.y,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001722 subsurface->surface->width,
1723 subsurface->surface->height);
Giulio Camuffob8366642013-04-25 13:57:46 +03001724 }
1725
1726 box = pixman_region32_extents(&region);
1727 if (x)
1728 *x = box->x1;
1729 if (y)
1730 *y = box->y1;
1731 if (w)
1732 *w = box->x2 - box->x1;
1733 if (h)
1734 *h = box->y2 - box->y1;
1735
1736 pixman_region32_fini(&region);
1737}
1738
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001739static int
1740surface_resize(struct shell_surface *shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001741 struct weston_pointer *pointer, uint32_t edges)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001742{
1743 struct weston_resize_grab *resize;
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001744 const unsigned resize_topbottom =
1745 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1746 const unsigned resize_leftright =
1747 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1748 const unsigned resize_any = resize_topbottom | resize_leftright;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001749 struct weston_geometry geometry;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001750
Kristian Høgsberga4b620e2014-04-30 16:05:49 -07001751 if (shsurf->grabbed ||
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001752 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1753 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001754 return 0;
1755
Ondřej Majerechae9c4fc2014-08-21 15:47:22 +02001756 /* Check for invalid edge combinations. */
1757 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1758 (edges & resize_topbottom) == resize_topbottom ||
1759 (edges & resize_leftright) == resize_leftright)
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001760 return 0;
1761
1762 resize = malloc(sizeof *resize);
1763 if (!resize)
1764 return -1;
1765
1766 resize->edges = edges;
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001767
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001768 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1769 resize->width = geometry.width;
1770 resize->height = geometry.height;
Jasper St. Pierrebd65e502014-07-14 16:28:48 -04001771
Kristian Høgsberg44cd1962014-02-05 21:36:04 -08001772 shsurf->resize_edges = edges;
Philipp Kerlingc5f12412017-07-28 14:11:58 +02001773 weston_desktop_surface_set_resizing(shsurf->desktop_surface, true);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001774 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
Derek Foreman8fbebbd2015-07-15 13:00:40 -05001775 pointer, edges);
Kristian Høgsberg9e31bff2012-08-01 00:08:07 -04001776
1777 return 0;
1778}
1779
1780static void
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001781busy_cursor_grab_focus(struct weston_pointer_grab *base)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001782{
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001783 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001784 struct weston_pointer *pointer = base->pointer;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001785 struct weston_desktop_surface *desktop_surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001786 struct weston_view *view;
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001787 wl_fixed_t sx, sy;
1788
Jason Ekstranda7af7042013-10-12 22:38:11 -05001789 view = weston_compositor_pick_view(pointer->seat->compositor,
1790 pointer->x, pointer->y,
1791 &sx, &sy);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001792 desktop_surface = weston_surface_get_desktop_surface(view->surface);
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001793
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001794 if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08001795 shell_grab_end(grab);
1796 free(grab);
1797 }
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001798}
1799
1800static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02001801busy_cursor_grab_motion(struct weston_pointer_grab *grab,
1802 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02001803 struct weston_pointer_motion_event *event)
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001804{
Jonas Ådahld2510102014-10-05 21:39:14 +02001805 weston_pointer_move(grab->pointer, event);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001806}
Scott Moreauc3e54eb2012-04-17 19:06:20 -06001807
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001808static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001809busy_cursor_grab_button(struct weston_pointer_grab *base,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02001810 const struct timespec *time,
1811 uint32_t button, uint32_t state)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001812{
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001813 struct shell_grab *grab = (struct shell_grab *) base;
Kristian Høgsberge122b7b2013-05-08 16:47:00 -04001814 struct shell_surface *shsurf = grab->shsurf;
Derek Foreman794fa0e2015-07-15 13:00:38 -05001815 struct weston_pointer *pointer = grab->grab.pointer;
1816 struct weston_seat *seat = pointer->seat;
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001817
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001818 if (shsurf && button == BTN_LEFT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001819 activate(shsurf->shell, shsurf->view, seat,
1820 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman794fa0e2015-07-15 13:00:38 -05001821 surface_move(shsurf, pointer, false);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05001822 } else if (shsurf && button == BTN_RIGHT && state) {
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02001823 activate(shsurf->shell, shsurf->view, seat,
1824 WESTON_ACTIVATE_FLAG_CONFIGURE);
Derek Foreman74de4692015-07-15 13:00:39 -05001825 surface_rotate(shsurf, pointer);
Kristian Høgsbergbbe98392012-08-01 00:20:21 -04001826 }
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001827}
1828
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001829static void
1830busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1831{
1832 struct shell_grab *grab = (struct shell_grab *) base;
1833
1834 shell_grab_end(grab);
1835 free(grab);
1836}
1837
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001838static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001839 busy_cursor_grab_focus,
1840 busy_cursor_grab_motion,
1841 busy_cursor_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02001842 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10001843 noop_grab_axis_source,
1844 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02001845 busy_cursor_grab_cancel,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001846};
1847
1848static void
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001849handle_pointer_focus(struct wl_listener *listener, void *data)
1850{
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001851 struct weston_pointer *pointer = data;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001852 struct weston_view *view = pointer->focus;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001853 struct shell_surface *shsurf;
1854 struct weston_desktop_client *client;
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001855
Jason Ekstranda7af7042013-10-12 22:38:11 -05001856 if (!view)
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001857 return;
1858
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001859 shsurf = get_shell_surface(view->surface);
1860 if (!shsurf)
1861 return;
1862
1863 client = weston_desktop_surface_get_client(shsurf->desktop_surface);
1864
1865 if (shsurf->unresponsive)
1866 set_busy_cursor(shsurf, pointer);
1867 else
1868 weston_desktop_client_ping(client);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04001869}
1870
1871static void
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001872shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1873{
1874 if (--shsurf->focus_count == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001875 weston_desktop_surface_set_activated(shsurf->desktop_surface, false);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001876}
1877
1878static void
1879shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1880{
1881 if (shsurf->focus_count++ == 0)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001882 weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001883}
1884
1885static void
1886handle_keyboard_focus(struct wl_listener *listener, void *data)
1887{
1888 struct weston_keyboard *keyboard = data;
1889 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1890
1891 if (seat->focused_surface) {
1892 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1893 if (shsurf)
1894 shell_surface_lose_keyboard_focus(shsurf);
1895 }
1896
Philipp Kerlingba8a0d02017-07-26 12:02:15 +02001897 seat->focused_surface = weston_surface_get_main_surface(keyboard->focus);
Jasper St. Pierrefaf27a92013-12-09 17:36:28 -05001898
1899 if (seat->focused_surface) {
1900 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1901 if (shsurf)
1902 shell_surface_gain_keyboard_focus(shsurf);
1903 }
1904}
1905
Philip Withnall07926d92013-11-25 18:01:40 +00001906/* The surface will be inserted into the list immediately after the link
1907 * returned by this function (i.e. will be stacked immediately above the
1908 * returned link). */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001909static struct weston_layer_entry *
Philip Withnall07926d92013-11-25 18:01:40 +00001910shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1911{
1912 struct workspace *ws;
1913
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001914 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
1915 !shsurf->state.lowered) {
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001916 return &shsurf->shell->fullscreen_layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001917 }
1918
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001919 /* Move the surface to a normal workspace layer so that surfaces
1920 * which were previously fullscreen or transient are no longer
1921 * rendered on top. */
1922 ws = get_current_workspace(shsurf->shell);
1923 return &ws->layer.view_list;
Philip Withnall07926d92013-11-25 18:01:40 +00001924}
1925
Philip Withnall648a4dd2013-11-25 18:01:44 +00001926static void
1927shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
1928{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001929 weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001930}
1931
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001932/* Update the surface’s layer. Mark both the old and new views as having dirty
Philip Withnall648a4dd2013-11-25 18:01:44 +00001933 * geometry to ensure the changes are redrawn.
1934 *
1935 * If any child surfaces exist and are mapped, ensure they’re in the same layer
1936 * as this surface. */
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001937static void
1938shell_surface_update_layer(struct shell_surface *shsurf)
1939{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001940 struct weston_surface *surface =
1941 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001942 struct weston_layer_entry *new_layer_link;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001943
1944 new_layer_link = shell_surface_calculate_layer_link(shsurf);
1945
Ander Conselvan de Oliveiraef6a7e42014-04-30 14:15:14 +03001946 if (new_layer_link == NULL)
1947 return;
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001948 if (new_layer_link == &shsurf->view->layer_link)
1949 return;
1950
1951 weston_view_geometry_dirty(shsurf->view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001952 weston_layer_entry_remove(&shsurf->view->layer_link);
1953 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001954 weston_view_geometry_dirty(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001955 weston_surface_damage(surface);
Philip Withnall648a4dd2013-11-25 18:01:44 +00001956
1957 shell_surface_update_child_surface_layers(shsurf);
Philip Withnalle1d75ae2013-11-25 18:01:41 +00001958}
1959
Alexander Larssonf82b6ca2013-05-28 16:23:39 +02001960static void
Semi Malinen99f8c082018-05-02 11:10:32 +02001961notify_output_destroy(struct wl_listener *listener, void *data)
1962{
1963 struct shell_surface *shsurf =
1964 container_of(listener,
1965 struct shell_surface, output_destroy_listener);
1966
1967 shsurf->output = NULL;
1968 shsurf->output_destroy_listener.notify = NULL;
1969}
1970
1971static void
Philip Withnall352e7ed2013-11-25 18:01:35 +00001972shell_surface_set_output(struct shell_surface *shsurf,
1973 struct weston_output *output)
1974{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02001975 struct weston_surface *es =
1976 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001977
1978 /* get the default output, if the client set it as NULL
Abdur Rehman7f1da1f2017-01-01 19:46:33 +05001979 check whether the output is available */
Philip Withnall352e7ed2013-11-25 18:01:35 +00001980 if (output)
1981 shsurf->output = output;
1982 else if (es->output)
1983 shsurf->output = es->output;
1984 else
1985 shsurf->output = get_default_output(es->compositor);
Semi Malinen99f8c082018-05-02 11:10:32 +02001986
1987 if (shsurf->output_destroy_listener.notify) {
1988 wl_list_remove(&shsurf->output_destroy_listener.link);
1989 shsurf->output_destroy_listener.notify = NULL;
1990 }
1991
1992 if (!shsurf->output)
1993 return;
1994
1995 shsurf->output_destroy_listener.notify = notify_output_destroy;
1996 wl_signal_add(&shsurf->output->destroy_signal,
1997 &shsurf->output_destroy_listener);
Philip Withnall352e7ed2013-11-25 18:01:35 +00001998}
1999
2000static void
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002001weston_view_set_initial_position(struct weston_view *view,
2002 struct desktop_shell *shell);
2003
2004static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002005unset_fullscreen(struct shell_surface *shsurf)
Alex Wu4539b082012-03-01 12:57:46 +08002006{
Philip Withnallf85fe842013-11-25 18:01:37 +00002007 /* Unset the fullscreen output, driver configuration and transforms. */
Alex Wu4539b082012-03-01 12:57:46 +08002008 wl_list_remove(&shsurf->fullscreen.transform.link);
2009 wl_list_init(&shsurf->fullscreen.transform.link);
Philip Withnallf85fe842013-11-25 18:01:37 +00002010
Jason Ekstranda7af7042013-10-12 22:38:11 -05002011 if (shsurf->fullscreen.black_view)
2012 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2013 shsurf->fullscreen.black_view = NULL;
Philip Withnallf85fe842013-11-25 18:01:37 +00002014
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002015 if (shsurf->saved_position_valid)
2016 weston_view_set_position(shsurf->view,
2017 shsurf->saved_x, shsurf->saved_y);
2018 else
2019 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02002020 shsurf->saved_position_valid = false;
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002021
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002022 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002023 wl_list_insert(&shsurf->view->geometry.transformation_list,
Philip Withnallbecb77e2013-11-25 18:01:30 +00002024 &shsurf->rotation.transform.link);
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002025 shsurf->saved_rotation_valid = false;
2026 }
Alex Wu4539b082012-03-01 12:57:46 +08002027}
2028
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002029static void
Philip Withnallbecb77e2013-11-25 18:01:30 +00002030unset_maximized(struct shell_surface *shsurf)
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002031{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002032 struct weston_surface *surface =
2033 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2034
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002035 /* undo all maximized things here */
Semi Malinen99f8c082018-05-02 11:10:32 +02002036 shell_surface_set_output(shsurf, get_default_output(surface->compositor));
Zhang, Xiong Y31236932013-12-13 22:10:57 +02002037
2038 if (shsurf->saved_position_valid)
2039 weston_view_set_position(shsurf->view,
2040 shsurf->saved_x, shsurf->saved_y);
2041 else
2042 weston_view_set_initial_position(shsurf->view, shsurf->shell);
Quentin Glidic920cf042016-08-16 14:26:20 +02002043 shsurf->saved_position_valid = false;
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002044
2045 if (shsurf->saved_rotation_valid) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002046 wl_list_insert(&shsurf->view->geometry.transformation_list,
2047 &shsurf->rotation.transform.link);
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002048 shsurf->saved_rotation_valid = false;
2049 }
Rafal Mielniczukfffdcdd2013-03-11 19:26:54 +01002050}
2051
Philip Withnallbecb77e2013-11-25 18:01:30 +00002052static void
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002053set_minimized(struct weston_surface *surface)
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002054{
2055 struct shell_surface *shsurf;
2056 struct workspace *current_ws;
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002057 struct weston_view *view;
2058
2059 view = get_default_view(surface);
2060 if (!view)
2061 return;
2062
2063 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2064
2065 shsurf = get_shell_surface(surface);
2066 current_ws = get_current_workspace(shsurf->shell);
2067
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002068 weston_layer_entry_remove(&view->layer_link);
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002069 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002070
Manuel Bachmanndc1c3e42015-02-16 11:52:13 +01002071 drop_focus_state(shsurf->shell, current_ws, view->surface);
Jonas Ådahl22faea12014-10-15 22:02:06 +02002072 surface_keyboard_focus_lost(surface);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002073
2074 shell_surface_update_child_surface_layers(shsurf);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01002075 weston_view_damage_below(view);
2076}
2077
Kristian Høgsberg7f366e72012-04-27 17:20:01 -04002078
Tiago Vignattibe143262012-04-16 17:31:41 +03002079static struct desktop_shell *
Juan Zhao96879df2012-02-07 08:45:41 +08002080shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02002081{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002082 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +08002083}
2084
Pekka Paalanen8274d902014-08-06 19:36:51 +03002085static int
2086black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2087{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002088 struct weston_view *fs_view = surface->committed_private;
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002089 struct weston_surface *fs_surface = fs_view->surface;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002090 int n;
2091 int rem;
2092 int ret;
2093
2094 n = snprintf(buf, len, "black background surface for ");
2095 if (n < 0)
2096 return n;
2097
2098 rem = (int)len - n;
2099 if (rem < 0)
2100 rem = 0;
2101
2102 if (fs_surface->get_label)
2103 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2104 else
2105 ret = snprintf(buf + n, rem, "<unknown>");
2106
2107 if (ret < 0)
2108 return n;
2109
2110 return n + ret;
2111}
2112
Alex Wu21858432012-04-01 20:13:08 +08002113static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002114black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
Alex Wu21858432012-04-01 20:13:08 +08002115
Jason Ekstranda7af7042013-10-12 22:38:11 -05002116static struct weston_view *
Alex Wu4539b082012-03-01 12:57:46 +08002117create_black_surface(struct weston_compositor *ec,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002118 struct weston_view *fs_view,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02002119 float x, float y, int w, int h)
Alex Wu4539b082012-03-01 12:57:46 +08002120{
2121 struct weston_surface *surface = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002122 struct weston_view *view;
Alex Wu4539b082012-03-01 12:57:46 +08002123
2124 surface = weston_surface_create(ec);
2125 if (surface == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002126 weston_log("no memory\n");
Alex Wu4539b082012-03-01 12:57:46 +08002127 return NULL;
2128 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002129 view = weston_view_create(surface);
2130 if (surface == NULL) {
2131 weston_log("no memory\n");
2132 weston_surface_destroy(surface);
2133 return NULL;
2134 }
Alex Wu4539b082012-03-01 12:57:46 +08002135
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002136 surface->committed = black_surface_committed;
2137 surface->committed_private = fs_view;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002138 weston_surface_set_label_func(surface, black_surface_get_label);
Alex Wu4539b082012-03-01 12:57:46 +08002139 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
Pekka Paalanen71f6f3b2012-10-10 12:49:26 +03002140 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg61f00f52012-08-03 16:31:36 -04002141 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
Jonas Ådahl33619a42013-01-15 21:25:55 +01002142 pixman_region32_fini(&surface->input);
2143 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002144
Jason Ekstrand6228ee22014-01-01 15:58:57 -06002145 weston_surface_set_size(surface, w, h);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002146 weston_view_set_position(view, x, y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002147
2148 return view;
Alex Wu4539b082012-03-01 12:57:46 +08002149}
2150
Philip Withnalled8f1a92013-11-25 18:01:43 +00002151static void
2152shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2153{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002154 struct weston_surface *surface =
2155 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002156 struct weston_output *output = shsurf->fullscreen_output;
2157
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002158 assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
Philip Withnalled8f1a92013-11-25 18:01:43 +00002159
2160 if (!shsurf->fullscreen.black_view)
2161 shsurf->fullscreen.black_view =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002162 create_black_surface(surface->compositor,
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02002163 shsurf->view,
Philip Withnalled8f1a92013-11-25 18:01:43 +00002164 output->x, output->y,
2165 output->width,
2166 output->height);
2167
2168 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002169 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2170 weston_layer_entry_insert(&shsurf->view->layer_link,
2171 &shsurf->fullscreen.black_view->layer_link);
Philip Withnalled8f1a92013-11-25 18:01:43 +00002172 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002173 weston_surface_damage(surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002174
Armin Krezović4663aca2016-06-30 06:04:29 +02002175 shsurf->fullscreen.black_view->is_mapped = true;
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002176 shsurf->state.lowered = false;
Philip Withnalled8f1a92013-11-25 18:01:43 +00002177}
2178
Alex Wu4539b082012-03-01 12:57:46 +08002179/* Create black surface and append it to the associated fullscreen surface.
2180 * Handle size dismatch and positioning according to the method. */
2181static void
2182shell_configure_fullscreen(struct shell_surface *shsurf)
2183{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002184 struct weston_surface *surface =
2185 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Giulio Camuffob8366642013-04-25 13:57:46 +03002186 int32_t surf_x, surf_y, surf_width, surf_height;
Alex Wu4539b082012-03-01 12:57:46 +08002187
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002188 /* Reverse the effect of lower_fullscreen_layer() */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002189 weston_layer_entry_remove(&shsurf->view->layer_link);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002190 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2191 &shsurf->view->layer_link);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01002192
Pekka Paalanenc63acc42018-05-02 10:21:58 +02002193 if (!shsurf->fullscreen_output) {
2194 /* If there is no output, there's not much we can do.
2195 * Position the window somewhere, whatever. */
2196 weston_view_set_position(shsurf->view, 0, 0);
2197 return;
2198 }
2199
Philip Withnalled8f1a92013-11-25 18:01:43 +00002200 shell_ensure_fullscreen_black_view(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002201
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002202 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
Giulio Camuffob8366642013-04-25 13:57:46 +03002203 &surf_width, &surf_height);
2204
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002205 if (surface->buffer_ref.buffer)
2206 center_on_output(shsurf->view, shsurf->fullscreen_output);
Alex Wu4539b082012-03-01 12:57:46 +08002207}
2208
Alex Wu4539b082012-03-01 12:57:46 +08002209static void
2210shell_map_fullscreen(struct shell_surface *shsurf)
2211{
Alex Wubd3354b2012-04-17 17:20:49 +08002212 shell_configure_fullscreen(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +08002213}
2214
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002215static struct weston_output *
2216get_focused_output(struct weston_compositor *compositor)
2217{
2218 struct weston_seat *seat;
2219 struct weston_output *output = NULL;
2220
2221 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05002222 struct weston_touch *touch = weston_seat_get_touch(seat);
2223 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2224 struct weston_keyboard *keyboard =
2225 weston_seat_get_keyboard(seat);
2226
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002227 /* Priority has touch focus, then pointer and
2228 * then keyboard focus. We should probably have
Emmanuel Gil Peyrot426c2462019-02-20 16:33:32 +01002229 * three for loops and check first for touch,
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002230 * then for pointer, etc. but unless somebody has some
2231 * objections, I think this is sufficient. */
Derek Foreman1281a362015-07-31 16:55:32 -05002232 if (touch && touch->focus)
2233 output = touch->focus->output;
2234 else if (pointer && pointer->focus)
2235 output = pointer->focus->output;
2236 else if (keyboard && keyboard->focus)
2237 output = keyboard->focus->output;
Giulio Camuffo6b4b2412015-01-29 19:06:49 +02002238
2239 if (output)
2240 break;
2241 }
2242
2243 return output;
2244}
2245
2246static void
Giulio Camuffo5085a752013-03-25 21:42:45 +01002247destroy_shell_seat(struct wl_listener *listener, void *data)
2248{
2249 struct shell_seat *shseat =
2250 container_of(listener,
2251 struct shell_seat, seat_destroy_listener);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002252
2253 wl_list_remove(&shseat->seat_destroy_listener.link);
2254 free(shseat);
2255}
2256
Jason Ekstrand024177c2014-04-21 19:42:58 -05002257static void
2258shell_seat_caps_changed(struct wl_listener *listener, void *data)
2259{
Derek Foreman1281a362015-07-31 16:55:32 -05002260 struct weston_keyboard *keyboard;
2261 struct weston_pointer *pointer;
Jason Ekstrand024177c2014-04-21 19:42:58 -05002262 struct shell_seat *seat;
2263
2264 seat = container_of(listener, struct shell_seat, caps_changed_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002265 keyboard = weston_seat_get_keyboard(seat->seat);
2266 pointer = weston_seat_get_pointer(seat->seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002267
Derek Foreman1281a362015-07-31 16:55:32 -05002268 if (keyboard &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002269 wl_list_empty(&seat->keyboard_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002270 wl_signal_add(&keyboard->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002271 &seat->keyboard_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002272 } else if (!keyboard) {
Derek Foreman60d97312015-07-15 13:00:45 -05002273 wl_list_remove(&seat->keyboard_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002274 wl_list_init(&seat->keyboard_focus_listener.link);
2275 }
2276
Derek Foreman1281a362015-07-31 16:55:32 -05002277 if (pointer &&
Jason Ekstrand024177c2014-04-21 19:42:58 -05002278 wl_list_empty(&seat->pointer_focus_listener.link)) {
Derek Foreman1281a362015-07-31 16:55:32 -05002279 wl_signal_add(&pointer->focus_signal,
Jason Ekstrand024177c2014-04-21 19:42:58 -05002280 &seat->pointer_focus_listener);
Derek Foreman1281a362015-07-31 16:55:32 -05002281 } else if (!pointer) {
Derek Foreman60d97312015-07-15 13:00:45 -05002282 wl_list_remove(&seat->pointer_focus_listener.link);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002283 wl_list_init(&seat->pointer_focus_listener.link);
2284 }
2285}
2286
Giulio Camuffo5085a752013-03-25 21:42:45 +01002287static struct shell_seat *
2288create_shell_seat(struct weston_seat *seat)
2289{
2290 struct shell_seat *shseat;
2291
2292 shseat = calloc(1, sizeof *shseat);
2293 if (!shseat) {
2294 weston_log("no memory to allocate shell seat\n");
2295 return NULL;
2296 }
2297
2298 shseat->seat = seat;
Giulio Camuffo5085a752013-03-25 21:42:45 +01002299
2300 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2301 wl_signal_add(&seat->destroy_signal,
2302 &shseat->seat_destroy_listener);
2303
Jason Ekstrand024177c2014-04-21 19:42:58 -05002304 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2305 wl_list_init(&shseat->keyboard_focus_listener.link);
2306
2307 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2308 wl_list_init(&shseat->pointer_focus_listener.link);
2309
2310 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2311 wl_signal_add(&seat->updated_caps_signal,
2312 &shseat->caps_changed_listener);
2313 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2314
Giulio Camuffo5085a752013-03-25 21:42:45 +01002315 return shseat;
2316}
2317
2318static struct shell_seat *
2319get_shell_seat(struct weston_seat *seat)
2320{
2321 struct wl_listener *listener;
2322
2323 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
Jason Ekstrand024177c2014-04-21 19:42:58 -05002324 assert(listener != NULL);
Giulio Camuffo5085a752013-03-25 21:42:45 +01002325
2326 return container_of(listener,
2327 struct shell_seat, seat_destroy_listener);
2328}
2329
Kristian Høgsbergb810eb52013-02-12 20:07:05 -05002330static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002331fade_out_done_idle_cb(void *data)
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002332{
2333 struct shell_surface *shsurf = data;
2334
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002335 weston_surface_destroy(shsurf->view->surface);
Pekka Paalanen99628002018-05-22 12:48:35 +03002336
2337 if (shsurf->output_destroy_listener.notify) {
2338 wl_list_remove(&shsurf->output_destroy_listener.link);
2339 shsurf->output_destroy_listener.notify = NULL;
2340 }
2341
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002342 free(shsurf);
Kristian Høgsberg160fe752014-03-11 10:19:10 -07002343}
2344
2345static void
Derek Foreman039e9be2015-04-14 17:09:06 -05002346fade_out_done(struct weston_view_animation *animation, void *data)
2347{
2348 struct shell_surface *shsurf = data;
2349 struct wl_event_loop *loop;
2350
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002351 loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
Derek Foreman039e9be2015-04-14 17:09:06 -05002352
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002353 if (weston_view_is_mapped(shsurf->view)) {
Tomohito Esaki5898cd32019-04-01 17:50:14 +09002354 weston_view_unmap(shsurf->view);
Derek Foreman039e9be2015-04-14 17:09:06 -05002355 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
Derek Foreman039e9be2015-04-14 17:09:06 -05002356 }
2357}
2358
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08002359struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002360get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002361{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002362 if (weston_surface_is_desktop_surface(surface)) {
2363 struct weston_desktop_surface *desktop_surface =
2364 weston_surface_get_desktop_surface(surface);
2365 return weston_desktop_surface_get_user_data(desktop_surface);
2366 }
2367 return NULL;
Pekka Paalanenec2b32f2011-11-28 15:12:34 +02002368}
2369
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002370/*
2371 * libweston-desktop
2372 */
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002373
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002374static void
2375desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2376 void *shell)
2377{
2378 struct weston_desktop_client *client =
2379 weston_desktop_surface_get_client(desktop_surface);
2380 struct wl_client *wl_client =
2381 weston_desktop_client_get_client(client);
2382 struct weston_view *view;
2383 struct shell_surface *shsurf;
2384 struct weston_surface *surface =
2385 weston_desktop_surface_get_surface(desktop_surface);
2386
2387 view = weston_desktop_surface_create_view(desktop_surface);
2388 if (!view)
2389 return;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002390
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002391 shsurf = calloc(1, sizeof *shsurf);
2392 if (!shsurf) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002393 if (wl_client)
2394 wl_client_post_no_memory(wl_client);
2395 else
2396 weston_log("no memory to allocate shell surface\n");
2397 return;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002398 }
2399
Pekka Paalanen8274d902014-08-06 19:36:51 +03002400 weston_surface_set_label_func(surface, shell_surface_get_label);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002401
Tiago Vignattibc052c92012-04-19 16:18:18 +03002402 shsurf->shell = (struct desktop_shell *) shell;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002403 shsurf->unresponsive = 0;
Alex Wu4539b082012-03-01 12:57:46 +08002404 shsurf->saved_position_valid = false;
Alex Wu7bcb8bd2012-04-27 09:07:24 +08002405 shsurf->saved_rotation_valid = false;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002406 shsurf->desktop_surface = desktop_surface;
2407 shsurf->view = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002408 shsurf->fullscreen.black_view = NULL;
Alex Wu4539b082012-03-01 12:57:46 +08002409 wl_list_init(&shsurf->fullscreen.transform.link);
2410
Semi Malinen99f8c082018-05-02 11:10:32 +02002411 shell_surface_set_output(
2412 shsurf, get_default_output(shsurf->shell->compositor));
Jasper St. Pierre9aa8ce62014-04-11 16:18:54 -07002413
Jason Ekstrand651f00e2013-06-14 10:07:54 -05002414 wl_signal_init(&shsurf->destroy_signal);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02002415
Pekka Paalanen460099f2012-01-20 16:48:25 +02002416 /* empty when not in use */
2417 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05002418 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02002419
Jonas Ådahl62fcd042012-06-13 00:01:23 +02002420 wl_list_init(&shsurf->workspace_transform.link);
2421
Stefan Agnera8da2082019-06-23 14:53:59 +02002422 /*
2423 * initialize list as well as link. The latter allows to use
2424 * wl_list_remove() even when this surface is not in another list.
2425 */
2426 wl_list_init(&shsurf->children_list);
2427 wl_list_init(&shsurf->children_link);
2428
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002429 weston_desktop_surface_set_user_data(desktop_surface, shsurf);
2430 weston_desktop_surface_set_activated(desktop_surface,
2431 shsurf->focus_count > 0);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002432}
2433
Tiago Vignattibc052c92012-04-19 16:18:18 +03002434static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002435desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2436 void *shell)
2437{
2438 struct shell_surface *shsurf =
2439 weston_desktop_surface_get_user_data(desktop_surface);
Stefan Agnera8da2082019-06-23 14:53:59 +02002440 struct shell_surface *shsurf_child, *tmp;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002441 struct weston_surface *surface =
2442 weston_desktop_surface_get_surface(desktop_surface);
2443
2444 if (!shsurf)
2445 return;
2446
Stefan Agnera8da2082019-06-23 14:53:59 +02002447 wl_list_for_each_safe(shsurf_child, tmp, &shsurf->children_list, children_link) {
2448 wl_list_remove(&shsurf_child->children_link);
2449 wl_list_init(&shsurf_child->children_link);
2450 }
2451 wl_list_remove(&shsurf->children_link);
2452
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002453 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2454
2455 if (shsurf->fullscreen.black_view)
2456 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2457
2458 weston_surface_set_label_func(surface, NULL);
2459 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2460 shsurf->desktop_surface = NULL;
2461
Quentin Glidicf01ecee2016-08-16 10:52:46 +02002462 weston_desktop_surface_unlink_view(shsurf->view);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002463 if (weston_surface_is_mapped(surface) &&
2464 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
2465 pixman_region32_fini(&surface->pending.input);
2466 pixman_region32_init(&surface->pending.input);
2467 pixman_region32_fini(&surface->input);
2468 pixman_region32_init(&surface->input);
2469 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2470 fade_out_done, shsurf);
2471 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002472 weston_view_destroy(shsurf->view);
Pekka Paalanen99628002018-05-22 12:48:35 +03002473
2474 if (shsurf->output_destroy_listener.notify) {
2475 wl_list_remove(&shsurf->output_destroy_listener.link);
2476 shsurf->output_destroy_listener.notify = NULL;
2477 }
2478
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002479 free(shsurf);
2480 }
2481}
2482
2483static void
2484set_maximized_position(struct desktop_shell *shell,
2485 struct shell_surface *shsurf)
2486{
2487 pixman_rectangle32_t area;
2488 struct weston_geometry geometry;
2489
2490 get_output_work_area(shell, shsurf->output, &area);
2491 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2492
2493 weston_view_set_position(shsurf->view,
2494 area.x - geometry.x,
2495 area.y - geometry.y);
2496}
2497
2498static void
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002499set_position_from_xwayland(struct shell_surface *shsurf)
2500{
2501 struct weston_geometry geometry;
2502 float x;
2503 float y;
2504
2505 assert(shsurf->xwayland.is_set);
2506
2507 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2508 x = shsurf->xwayland.x - geometry.x;
2509 y = shsurf->xwayland.y - geometry.y;
2510
2511 weston_view_set_position(shsurf->view, x, y);
2512
2513#ifdef WM_DEBUG
2514 weston_log("%s: XWM %d, %d; geometry %d, %d; view %f, %f\n",
2515 __func__, shsurf->xwayland.x, shsurf->xwayland.y,
2516 geometry.x, geometry.y, x, y);
2517#endif
2518}
2519
2520static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002521map(struct desktop_shell *shell, struct shell_surface *shsurf,
2522 int32_t sx, int32_t sy)
Tiago Vignattibc052c92012-04-19 16:18:18 +03002523{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002524 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002525 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2526 struct weston_compositor *compositor = shell->compositor;
2527 struct weston_seat *seat;
Tiago Vignattibc052c92012-04-19 16:18:18 +03002528
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002529 /* initial positioning, see also configure() */
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002530 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002531 center_on_output(shsurf->view, shsurf->fullscreen_output);
2532 shell_map_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002533 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002534 set_maximized_position(shell, shsurf);
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002535 } else if (shsurf->xwayland.is_set) {
2536 set_position_from_xwayland(shsurf);
Jasper St. Pierre66bc9492015-02-13 14:01:55 +08002537 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002538 weston_view_set_initial_position(shsurf->view, shell);
Marek Chalupa052917a2014-09-02 11:35:12 +02002539 }
2540
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002541 /* Surface stacking order, see also activate(). */
2542 shell_surface_update_layer(shsurf);
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002543
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002544 weston_view_update_transform(shsurf->view);
2545 shsurf->view->is_mapped = true;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002546 if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002547 surface->output = shsurf->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02002548 weston_view_set_output(shsurf->view, shsurf->output);
Jasper St. Pierre973d7872014-05-06 08:44:29 -04002549 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002550
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002551 if (!shell->locked) {
2552 wl_list_for_each(seat, &compositor->seat_list, link)
2553 activate(shell, shsurf->view, seat,
2554 WESTON_ACTIVATE_FLAG_CONFIGURE);
2555 }
Jasper St. Pierreab2c1082014-04-10 10:41:46 -07002556
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002557 if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002558 switch (shell->win_animation_type) {
2559 case ANIMATION_FADE:
2560 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2561 break;
2562 case ANIMATION_ZOOM:
2563 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2564 break;
2565 case ANIMATION_NONE:
2566 default:
2567 break;
Jonas Ådahl49d77d22015-03-18 16:20:51 +08002568 }
2569 }
Jasper St. Pierre084aa0b2015-02-13 14:02:00 +08002570}
2571
2572static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002573desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2574 int32_t sx, int32_t sy, void *data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002575{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002576 struct shell_surface *shsurf =
2577 weston_desktop_surface_get_user_data(desktop_surface);
2578 struct weston_surface *surface =
2579 weston_desktop_surface_get_surface(desktop_surface);
2580 struct weston_view *view = shsurf->view;
2581 struct desktop_shell *shell = data;
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002582 bool was_fullscreen;
2583 bool was_maximized;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002584
2585 if (surface->width == 0)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002586 return;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002587
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002588 was_fullscreen = shsurf->state.fullscreen;
2589 was_maximized = shsurf->state.maximized;
2590
2591 shsurf->state.fullscreen =
2592 weston_desktop_surface_get_fullscreen(desktop_surface);
2593 shsurf->state.maximized =
2594 weston_desktop_surface_get_maximized(desktop_surface);
2595
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002596 if (!weston_surface_is_mapped(surface)) {
2597 map(shell, shsurf, sx, sy);
2598 surface->is_mapped = true;
2599 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2600 ++surface->ref_count;
2601 return;
2602 }
2603
2604 if (sx == 0 && sy == 0 &&
2605 shsurf->last_width == surface->width &&
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002606 shsurf->last_height == surface->height &&
2607 was_fullscreen == shsurf->state.fullscreen &&
2608 was_maximized == shsurf->state.maximized)
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002609 return;
2610
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002611 if (was_fullscreen)
2612 unset_fullscreen(shsurf);
2613 if (was_maximized)
2614 unset_maximized(shsurf);
2615
Quentin Glidic920cf042016-08-16 14:26:20 +02002616 if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2617 !shsurf->saved_position_valid) {
2618 shsurf->saved_x = shsurf->view->geometry.x;
2619 shsurf->saved_y = shsurf->view->geometry.y;
2620 shsurf->saved_position_valid = true;
2621
2622 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2623 wl_list_remove(&shsurf->rotation.transform.link);
2624 wl_list_init(&shsurf->rotation.transform.link);
2625 weston_view_geometry_dirty(shsurf->view);
2626 shsurf->saved_rotation_valid = true;
2627 }
2628 }
2629
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002630 if (shsurf->state.fullscreen) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002631 shell_configure_fullscreen(shsurf);
Quentin Glidic18a81ac2016-08-16 14:26:03 +02002632 } else if (shsurf->state.maximized) {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002633 set_maximized_position(shell, shsurf);
2634 surface->output = shsurf->output;
2635 } else {
2636 float from_x, from_y;
2637 float to_x, to_y;
2638 float x, y;
2639
2640 if (shsurf->resize_edges) {
2641 sx = 0;
2642 sy = 0;
2643 }
2644
2645 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2646 sx = shsurf->last_width - surface->width;
2647 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2648 sy = shsurf->last_height - surface->height;
2649
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002650 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2651 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2652 x = shsurf->view->geometry.x + to_x - from_x;
2653 y = shsurf->view->geometry.y + to_y - from_y;
2654
2655 weston_view_set_position(shsurf->view, x, y);
2656 }
2657
Emmanuel Gil Peyrotc3941792017-04-04 18:49:33 +01002658 shsurf->last_width = surface->width;
2659 shsurf->last_height = surface->height;
2660
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002661 /* XXX: would a fullscreen surface need the same handling? */
2662 if (surface->output) {
2663 wl_list_for_each(view, &surface->views, surface_link)
2664 weston_view_update_transform(view);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002665 }
2666}
2667
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002668static void
Derek Foreman927d9e22017-10-06 14:37:44 -05002669get_maximized_size(struct shell_surface *shsurf, int32_t *width, int32_t *height)
2670{
2671 struct desktop_shell *shell;
2672 pixman_rectangle32_t area;
2673
2674 shell = shell_surface_get_shell(shsurf);
2675 get_output_work_area(shell, shsurf->output, &area);
2676
2677 *width = area.width;
2678 *height = area.height;
2679}
2680
2681static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002682set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2683 struct weston_output *output)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002684{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002685 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2686 struct weston_surface *surface =
2687 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2688 int32_t width = 0, height = 0;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002689
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002690 if (fullscreen) {
2691 /* handle clients launching in fullscreen */
2692 if (output == NULL && !weston_surface_is_mapped(surface)) {
2693 /* Set the output to the one that has focus currently. */
2694 output = get_focused_output(surface->compositor);
2695 }
Pekka Paalanene972b272014-10-01 14:03:56 +03002696
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002697 shell_surface_set_output(shsurf, output);
2698 shsurf->fullscreen_output = shsurf->output;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002699
Marius Vlad6ebda362020-03-28 00:23:14 +02002700 if (shsurf->output) {
2701 width = shsurf->output->width;
2702 height = shsurf->output->height;
2703 }
Derek Foremane1af3d82017-10-06 14:37:45 -05002704 } else if (weston_desktop_surface_get_maximized(desktop_surface)) {
2705 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002706 }
2707 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2708 weston_desktop_surface_set_size(desktop_surface, width, height);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002709}
2710
2711static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002712desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2713 struct weston_seat *seat, uint32_t serial, void *shell)
2714{
2715 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2716 struct weston_touch *touch = weston_seat_get_touch(seat);
2717 struct shell_surface *shsurf =
2718 weston_desktop_surface_get_user_data(desktop_surface);
2719 struct weston_surface *surface =
2720 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2721 struct wl_resource *resource = surface->resource;
2722 struct weston_surface *focus;
2723
2724 if (pointer &&
2725 pointer->focus &&
2726 pointer->button_count > 0 &&
2727 pointer->grab_serial == serial) {
2728 focus = weston_surface_get_main_surface(pointer->focus->surface);
2729 if ((focus == surface) &&
2730 (surface_move(shsurf, pointer, true) < 0))
2731 wl_resource_post_no_memory(resource);
2732 } else if (touch &&
2733 touch->focus &&
2734 touch->grab_serial == serial) {
2735 focus = weston_surface_get_main_surface(touch->focus->surface);
2736 if ((focus == surface) &&
2737 (surface_touch_move(shsurf, touch) < 0))
2738 wl_resource_post_no_memory(resource);
2739 }
2740}
2741
2742static void
2743desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2744 struct weston_seat *seat, uint32_t serial,
2745 enum weston_desktop_surface_edge edges, void *shell)
2746{
2747 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2748 struct shell_surface *shsurf =
2749 weston_desktop_surface_get_user_data(desktop_surface);
2750 struct weston_surface *surface =
2751 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2752 struct wl_resource *resource = surface->resource;
2753 struct weston_surface *focus;
2754
2755 if (!pointer ||
2756 pointer->button_count == 0 ||
2757 pointer->grab_serial != serial ||
2758 pointer->focus == NULL)
2759 return;
2760
2761 focus = weston_surface_get_main_surface(pointer->focus->surface);
2762 if (focus != surface)
2763 return;
2764
2765 if (surface_resize(shsurf, pointer, edges) < 0)
2766 wl_resource_post_no_memory(resource);
2767}
2768
2769static void
Stefan Agnera8da2082019-06-23 14:53:59 +02002770desktop_surface_set_parent(struct weston_desktop_surface *desktop_surface,
2771 struct weston_desktop_surface *parent,
2772 void *shell)
2773{
Marius Vlada27658e2020-01-17 12:32:55 +02002774 struct shell_surface *shsurf_parent;
Stefan Agnera8da2082019-06-23 14:53:59 +02002775 struct shell_surface *shsurf =
2776 weston_desktop_surface_get_user_data(desktop_surface);
Stefan Agnera8da2082019-06-23 14:53:59 +02002777
Marius Vlada27658e2020-01-17 12:32:55 +02002778 /* unlink any potential child */
2779 wl_list_remove(&shsurf->children_link);
2780
2781 if (parent) {
2782 shsurf_parent = weston_desktop_surface_get_user_data(parent);
2783 wl_list_insert(shsurf_parent->children_list.prev,
2784 &shsurf->children_link);
2785 } else {
2786 wl_list_init(&shsurf->children_link);
2787 }
Stefan Agnera8da2082019-06-23 14:53:59 +02002788}
2789
2790static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002791desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2792 bool fullscreen,
2793 struct weston_output *output, void *shell)
2794{
2795 struct shell_surface *shsurf =
2796 weston_desktop_surface_get_user_data(desktop_surface);
2797
2798 set_fullscreen(shsurf, fullscreen, output);
2799}
2800
2801static void
2802set_maximized(struct shell_surface *shsurf, bool maximized)
2803{
2804 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2805 struct weston_surface *surface =
2806 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2807 int32_t width = 0, height = 0;
2808
2809 if (maximized) {
2810 struct weston_output *output;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002811
2812 if (!weston_surface_is_mapped(surface))
2813 output = get_focused_output(surface->compositor);
2814 else
2815 output = surface->output;
2816
2817 shell_surface_set_output(shsurf, output);
2818
Derek Foreman927d9e22017-10-06 14:37:44 -05002819 get_maximized_size(shsurf, &width, &height);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002820 }
2821 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2822 weston_desktop_surface_set_size(desktop_surface, width, height);
2823}
2824
2825static void
2826desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2827 bool maximized, void *shell)
2828{
2829 struct shell_surface *shsurf =
2830 weston_desktop_surface_get_user_data(desktop_surface);
2831
2832 set_maximized(shsurf, maximized);
2833}
2834
2835static void
2836desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2837 void *shell)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002838{
2839 struct weston_surface *surface =
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002840 weston_desktop_surface_get_surface(desktop_surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002841
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002842 /* apply compositor's own minimization logic (hide) */
2843 set_minimized(surface);
Rafael Antognollie2a34552013-12-03 15:35:45 -02002844}
2845
Rafael Antognollie2a34552013-12-03 15:35:45 -02002846static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002847set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002848{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002849 struct shell_grab *grab;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002850
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002851 if (pointer->grab->interface == &busy_cursor_grab_interface)
2852 return;
2853
2854 grab = malloc(sizeof *grab);
2855 if (!grab)
2856 return;
2857
2858 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2859 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2860 /* Mark the shsurf as ungrabbed so that button binding is able
2861 * to move it. */
2862 shsurf->grabbed = 0;
2863}
Rafael Antognollie2a34552013-12-03 15:35:45 -02002864
2865static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002866end_busy_cursor(struct weston_compositor *compositor,
2867 struct weston_desktop_client *desktop_client)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002868{
Jonas Ådahlee45a552015-03-18 16:37:47 +08002869 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002870 struct shell_grab *grab;
2871 struct weston_seat *seat;
Jonas Ådahl24185e22015-02-27 18:37:41 +08002872
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002873 wl_list_for_each(seat, &compositor->seat_list, link) {
2874 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2875 struct weston_desktop_client *grab_client;
Pekka Paalanene972b272014-10-01 14:03:56 +03002876
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002877 if (!pointer)
2878 continue;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002879
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002880 if (pointer->grab->interface != &busy_cursor_grab_interface)
2881 continue;
2882
2883 grab = (struct shell_grab *) pointer->grab;
2884 shsurf = grab->shsurf;
2885 if (!shsurf)
2886 continue;
2887
2888 grab_client =
2889 weston_desktop_surface_get_client(shsurf->desktop_surface);
2890 if (grab_client == desktop_client) {
2891 shell_grab_end(grab);
2892 free(grab);
2893 }
2894 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002895}
2896
2897static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002898desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2899 void *user_data)
Rafael Antognollie2a34552013-12-03 15:35:45 -02002900{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002901 struct shell_surface *shsurf =
2902 weston_desktop_surface_get_user_data(desktop_surface);
2903 bool *unresponsive = user_data;
2904
2905 shsurf->unresponsive = *unresponsive;
2906}
2907
2908static void
2909desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2910 void *shell_)
2911{
2912 struct desktop_shell *shell = shell_;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002913 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002914 struct weston_seat *seat;
2915 bool unresponsive = true;
Rafael Antognollie2a34552013-12-03 15:35:45 -02002916
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002917 weston_desktop_client_for_each_surface(desktop_client,
2918 desktop_surface_set_unresponsive,
2919 &unresponsive);
2920
2921
2922 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2923 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2924 struct weston_desktop_client *grab_client;
2925
2926 if (!pointer || !pointer->focus)
2927 continue;
2928
2929 shsurf = get_shell_surface(pointer->focus->surface);
2930 if (!shsurf)
2931 continue;
2932
2933 grab_client =
2934 weston_desktop_surface_get_client(shsurf->desktop_surface);
2935 if (grab_client == desktop_client)
2936 set_busy_cursor(shsurf, pointer);
Jonas Ådahlbf48e212015-02-06 10:15:28 +08002937 }
Rafael Antognollie2a34552013-12-03 15:35:45 -02002938}
2939
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002940static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002941desktop_surface_pong(struct weston_desktop_client *desktop_client,
2942 void *shell_)
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002943{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002944 struct desktop_shell *shell = shell_;
2945 bool unresponsive = false;
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002946
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002947 weston_desktop_client_for_each_surface(desktop_client,
2948 desktop_surface_set_unresponsive,
2949 &unresponsive);
2950 end_busy_cursor(shell->compositor, desktop_client);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -08002951}
2952
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002953static void
2954desktop_surface_set_xwayland_position(struct weston_desktop_surface *surface,
2955 int32_t x, int32_t y, void *shell_)
2956{
2957 struct shell_surface *shsurf =
2958 weston_desktop_surface_get_user_data(surface);
2959
2960 shsurf->xwayland.x = x;
2961 shsurf->xwayland.y = y;
2962 shsurf->xwayland.is_set = true;
2963}
2964
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002965static const struct weston_desktop_api shell_desktop_api = {
2966 .struct_size = sizeof(struct weston_desktop_api),
2967 .surface_added = desktop_surface_added,
2968 .surface_removed = desktop_surface_removed,
2969 .committed = desktop_surface_committed,
2970 .move = desktop_surface_move,
2971 .resize = desktop_surface_resize,
Stefan Agnera8da2082019-06-23 14:53:59 +02002972 .set_parent = desktop_surface_set_parent,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002973 .fullscreen_requested = desktop_surface_fullscreen_requested,
2974 .maximized_requested = desktop_surface_maximized_requested,
2975 .minimized_requested = desktop_surface_minimized_requested,
2976 .ping_timeout = desktop_surface_ping_timeout,
2977 .pong = desktop_surface_pong,
Pekka Paalanen77a6d952016-11-16 15:17:14 +02002978 .set_xwayland_position = desktop_surface_set_xwayland_position,
Rafael Antognollie2a34552013-12-03 15:35:45 -02002979};
2980
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02002981/* ************************ *
2982 * end of libweston-desktop *
2983 * ************************ */
Kristian Høgsberg07937562011-04-12 17:25:42 -04002984static void
Quentin Glidice8bf9592016-06-23 18:55:20 +02002985configure_static_view(struct weston_view *ev, struct weston_layer *layer, int x, int y)
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002986{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002987 struct weston_view *v, *next;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002988
Semi Malinene7a52fb2018-04-26 11:08:10 +02002989 if (!ev->output)
2990 return;
2991
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002992 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002993 if (v->output == ev->output && v != ev) {
2994 weston_view_unmap(v);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02002995 v->surface->committed = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002996 weston_surface_set_label_func(v->surface, NULL);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04002997 }
2998 }
2999
Quentin Glidice8bf9592016-06-23 18:55:20 +02003000 weston_view_set_position(ev, ev->output->x + x, ev->output->y + y);
Armin Krezović4663aca2016-06-30 06:04:29 +02003001 ev->surface->is_mapped = true;
3002 ev->is_mapped = true;
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003003
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003004 if (wl_list_empty(&ev->layer_link.link)) {
3005 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003006 weston_compositor_schedule_repaint(ev->surface->compositor);
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003007 }
3008}
3009
David Fort50d962f2016-06-09 17:55:52 +02003010
3011static struct shell_output *
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003012find_shell_output_from_weston_output(struct desktop_shell *shell,
3013 struct weston_output *output)
David Fort50d962f2016-06-09 17:55:52 +02003014{
3015 struct shell_output *shell_output;
3016
3017 wl_list_for_each(shell_output, &shell->output_list, link) {
3018 if (shell_output->output == output)
3019 return shell_output;
3020 }
3021
3022 return NULL;
3023}
3024
Pekka Paalanen8274d902014-08-06 19:36:51 +03003025static int
3026background_get_label(struct weston_surface *surface, char *buf, size_t len)
3027{
3028 return snprintf(buf, len, "background for output %s",
Miguel A. Vico620f68d2019-09-25 11:23:13 -07003029 (surface->output ? surface->output->name : "NULL"));
Pekka Paalanen8274d902014-08-06 19:36:51 +03003030}
3031
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003032static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003033background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003034{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003035 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003036 struct weston_view *view;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003037
Jason Ekstranda7af7042013-10-12 22:38:11 -05003038 view = container_of(es->views.next, struct weston_view, surface_link);
3039
Quentin Glidice8bf9592016-06-23 18:55:20 +02003040 configure_static_view(view, &shell->background_layer, 0, 0);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003041}
3042
3043static void
David Fort50d962f2016-06-09 17:55:52 +02003044handle_background_surface_destroy(struct wl_listener *listener, void *data)
3045{
3046 struct shell_output *output =
3047 container_of(listener, struct shell_output, background_surface_listener);
3048
3049 weston_log("background surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09003050 wl_list_remove(&output->background_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02003051 output->background_surface = NULL;
3052}
3053
3054static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003055desktop_shell_set_background(struct wl_client *client,
3056 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003057 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003058 struct wl_resource *surface_resource)
3059{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003060 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003061 struct weston_surface *surface =
3062 wl_resource_get_user_data(surface_resource);
David Fort50d962f2016-06-09 17:55:52 +02003063 struct shell_output *sh_output;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003064 struct weston_view *view, *next;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003065
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003066 if (surface->committed) {
Kristian Høgsberg962342c2012-06-26 16:29:50 -04003067 wl_resource_post_error(surface_resource,
3068 WL_DISPLAY_ERROR_INVALID_OBJECT,
3069 "surface role already assigned");
3070 return;
3071 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003072
Jason Ekstranda7af7042013-10-12 22:38:11 -05003073 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3074 weston_view_destroy(view);
3075 view = weston_view_create(surface);
3076
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003077 surface->committed = background_committed;
3078 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003079 weston_surface_set_label_func(surface, background_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03003080 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02003081 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02003082
3083 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02003084 if (sh_output->background_surface) {
3085 /* The output already has a background, tell our helper
3086 * there is no need for another one. */
3087 weston_desktop_shell_send_configure(resource, 0,
3088 surface_resource,
3089 0, 0);
3090 } else {
3091 weston_desktop_shell_send_configure(resource, 0,
3092 surface_resource,
3093 surface->output->width,
3094 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003095
Pekka Paalanenff5e88d2017-12-07 11:44:18 +02003096 sh_output->background_surface = surface;
3097
3098 sh_output->background_surface_listener.notify =
3099 handle_background_surface_destroy;
3100 wl_signal_add(&surface->destroy_signal,
3101 &sh_output->background_surface_listener);
3102 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003103}
3104
Pekka Paalanen8274d902014-08-06 19:36:51 +03003105static int
3106panel_get_label(struct weston_surface *surface, char *buf, size_t len)
3107{
3108 return snprintf(buf, len, "panel for output %s",
Miguel A. Vico620f68d2019-09-25 11:23:13 -07003109 (surface->output ? surface->output->name : "NULL"));
Pekka Paalanen8274d902014-08-06 19:36:51 +03003110}
3111
Kristian Høgsberg75840622011-09-06 13:48:16 -04003112static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003113panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003114{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003115 struct desktop_shell *shell = es->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003116 struct weston_view *view;
Quentin Glidice8bf9592016-06-23 18:55:20 +02003117 int width, height;
3118 int x = 0, y = 0;
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003119
Jason Ekstranda7af7042013-10-12 22:38:11 -05003120 view = container_of(es->views.next, struct weston_view, surface_link);
3121
Quentin Glidice8bf9592016-06-23 18:55:20 +02003122 get_panel_size(shell, view, &width, &height);
3123 switch (shell->panel_position) {
3124 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
3125 break;
3126 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
3127 y = view->output->height - height;
3128 break;
3129 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
3130 break;
3131 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
3132 x = view->output->width - width;
3133 break;
3134 }
3135
3136 configure_static_view(view, &shell->panel_layer, x, y);
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003137}
3138
3139static void
David Fort50d962f2016-06-09 17:55:52 +02003140handle_panel_surface_destroy(struct wl_listener *listener, void *data)
3141{
3142 struct shell_output *output =
3143 container_of(listener, struct shell_output, panel_surface_listener);
3144
3145 weston_log("panel surface gone\n");
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09003146 wl_list_remove(&output->panel_surface_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02003147 output->panel_surface = NULL;
3148}
3149
3150
3151static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04003152desktop_shell_set_panel(struct wl_client *client,
3153 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003154 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04003155 struct wl_resource *surface_resource)
3156{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003157 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003158 struct weston_surface *surface =
3159 wl_resource_get_user_data(surface_resource);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003160 struct weston_view *view, *next;
David Fort50d962f2016-06-09 17:55:52 +02003161 struct shell_output *sh_output;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003162
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003163 if (surface->committed) {
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04003164 wl_resource_post_error(surface_resource,
3165 WL_DISPLAY_ERROR_INVALID_OBJECT,
3166 "surface role already assigned");
3167 return;
3168 }
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003169
Jason Ekstranda7af7042013-10-12 22:38:11 -05003170 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3171 weston_view_destroy(view);
3172 view = weston_view_create(surface);
3173
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003174 surface->committed = panel_committed;
3175 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003176 weston_surface_set_label_func(surface, panel_get_label);
Pekka Paalanen055c1132017-03-27 16:31:25 +03003177 surface->output = weston_head_from_resource(output_resource)->output;
Semi Malinene7a52fb2018-04-26 11:08:10 +02003178 weston_view_set_output(view, surface->output);
David Fort50d962f2016-06-09 17:55:52 +02003179
3180 sh_output = find_shell_output_from_weston_output(shell, surface->output);
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003181 if (sh_output->panel_surface) {
3182 /* The output already has a panel, tell our helper
3183 * there is no need for another one. */
3184 weston_desktop_shell_send_configure(resource, 0,
3185 surface_resource,
3186 0, 0);
3187 } else {
3188 weston_desktop_shell_send_configure(resource, 0,
3189 surface_resource,
3190 surface->output->width,
3191 surface->output->height);
David Fort50d962f2016-06-09 17:55:52 +02003192
Pekka Paalanen1a0239e2017-12-07 11:54:11 +02003193 sh_output->panel_surface = surface;
3194
3195 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
3196 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
3197 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003198}
3199
Pekka Paalanen8274d902014-08-06 19:36:51 +03003200static int
3201lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
3202{
3203 return snprintf(buf, len, "lock window");
3204}
3205
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003206static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003207lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003208{
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003209 struct desktop_shell *shell = surface->committed_private;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003210 struct weston_view *view;
3211
3212 view = container_of(surface->views.next, struct weston_view, surface_link);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003213
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003214 if (surface->width == 0)
Giulio Camuffo184df502013-02-21 11:29:21 +01003215 return;
3216
Jason Ekstranda7af7042013-10-12 22:38:11 -05003217 center_on_output(view, get_default_output(shell->compositor));
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003218
3219 if (!weston_surface_is_mapped(surface)) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003220 weston_layer_entry_insert(&shell->lock_layer.view_list,
3221 &view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003222 weston_view_update_transform(view);
Armin Krezović4663aca2016-06-30 06:04:29 +02003223 surface->is_mapped = true;
3224 view->is_mapped = true;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003225 shell_fade(shell, FADE_IN);
Kristian Høgsberg730c94d2012-06-26 21:44:35 -04003226 }
3227}
3228
3229static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003230handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003231{
Tiago Vignattibe143262012-04-16 17:31:41 +03003232 struct desktop_shell *shell =
3233 container_of(listener, struct desktop_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003234
Martin Minarik6d118362012-06-07 18:01:59 +02003235 weston_log("lock surface gone\n");
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05003236 shell->lock_surface = NULL;
3237}
3238
3239static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003240desktop_shell_set_lock_surface(struct wl_client *client,
3241 struct wl_resource *resource,
3242 struct wl_resource *surface_resource)
3243{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003244 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003245 struct weston_surface *surface =
3246 wl_resource_get_user_data(surface_resource);
Pekka Paalanen98262232011-12-01 10:42:22 +02003247
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003248 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003249
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003250 if (!shell->locked)
3251 return;
3252
Pekka Paalanen98262232011-12-01 10:42:22 +02003253 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003254
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003255 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003256 wl_signal_add(&surface->destroy_signal,
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003257 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02003258
Kristian Høgsbergaa2ee8b2013-10-30 15:49:45 -07003259 weston_view_create(surface);
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003260 surface->committed = lock_surface_committed;
3261 surface->committed_private = shell;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003262 weston_surface_set_label_func(surface, lock_surface_get_label);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003263}
3264
3265static void
Tiago Vignattibe143262012-04-16 17:31:41 +03003266resume_desktop(struct desktop_shell *shell)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003267{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003268 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanen77346a62011-11-30 16:26:35 +02003269
Quentin Glidic82681572016-12-17 13:40:51 +01003270 weston_layer_unset_position(&shell->lock_layer);
3271
3272 if (shell->showing_input_panels)
3273 weston_layer_set_position(&shell->input_panel_layer,
3274 WESTON_LAYER_POSITION_TOP_UI);
3275 weston_layer_set_position(&shell->fullscreen_layer,
3276 WESTON_LAYER_POSITION_FULLSCREEN);
3277 weston_layer_set_position(&shell->panel_layer,
3278 WESTON_LAYER_POSITION_UI);
3279 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003280
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003281 restore_focus_state(shell, get_current_workspace(shell));
Jonas Ådahl04769742012-06-13 00:01:24 +02003282
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003283 shell->locked = false;
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003284 shell_fade(shell, FADE_IN);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02003285 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003286}
3287
3288static void
3289desktop_shell_unlock(struct wl_client *client,
3290 struct wl_resource *resource)
3291{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003292 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003293
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003294 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003295
3296 if (shell->locked)
3297 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003298}
3299
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003300static void
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003301desktop_shell_set_grab_surface(struct wl_client *client,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003302 struct wl_resource *resource,
3303 struct wl_resource *surface_resource)
3304{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003305 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003306
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003307 shell->grab_surface = wl_resource_get_user_data(surface_resource);
Kristian Høgsberg48588f82013-10-24 15:51:35 -07003308 weston_view_create(shell->grab_surface);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003309}
3310
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003311static void
3312desktop_shell_desktop_ready(struct wl_client *client,
3313 struct wl_resource *resource)
3314{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05003315 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003316
3317 shell_fade_startup(shell);
3318}
3319
Jonny Lamb765760d2014-08-20 15:53:19 +02003320static void
3321desktop_shell_set_panel_position(struct wl_client *client,
3322 struct wl_resource *resource,
3323 uint32_t position)
3324{
3325 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3326
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003327 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3328 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3329 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3330 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
Jonny Lamb765760d2014-08-20 15:53:19 +02003331 wl_resource_post_error(resource,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003332 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
Jonny Lamb765760d2014-08-20 15:53:19 +02003333 "bad position argument");
3334 return;
3335 }
3336
3337 shell->panel_position = position;
3338}
3339
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003340static const struct weston_desktop_shell_interface desktop_shell_implementation = {
Kristian Høgsberg75840622011-09-06 13:48:16 -04003341 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003342 desktop_shell_set_panel,
3343 desktop_shell_set_lock_surface,
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04003344 desktop_shell_unlock,
Pekka Paalanen79346ab2013-05-22 18:03:09 +03003345 desktop_shell_set_grab_surface,
Jonny Lamb765760d2014-08-20 15:53:19 +02003346 desktop_shell_desktop_ready,
3347 desktop_shell_set_panel_position
Kristian Høgsberg75840622011-09-06 13:48:16 -04003348};
3349
3350static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003351move_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003352 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003353{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003354 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003355 struct weston_surface *surface;
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003356 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003357
Derek Foreman8ae2db52015-07-15 13:00:36 -05003358 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003359 return;
3360
Derek Foreman8ae2db52015-07-15 13:00:36 -05003361 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003362
Pekka Paalanen01388e22013-04-25 13:57:44 +03003363 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003364 if (surface == NULL)
3365 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003366
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003367 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003368 if (shsurf == NULL ||
3369 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3370 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Kristian Høgsberg938b8fa2012-05-18 13:46:27 -04003371 return;
3372
Derek Foreman794fa0e2015-07-15 13:00:38 -05003373 surface_move(shsurf, pointer, false);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003374}
3375
3376static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003377maximize_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003378 uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003379{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003380 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003381 struct weston_surface *surface;
3382 struct shell_surface *shsurf;
3383
3384 surface = weston_surface_get_main_surface(focus);
3385 if (surface == NULL)
3386 return;
3387
3388 shsurf = get_shell_surface(surface);
3389 if (shsurf == NULL)
3390 return;
3391
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003392 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003393}
3394
3395static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003396fullscreen_binding(struct weston_keyboard *keyboard,
3397 const struct timespec *time, uint32_t button, void *data)
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003398{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003399 struct weston_surface *focus = keyboard->focus;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003400 struct weston_surface *surface;
3401 struct shell_surface *shsurf;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003402 bool fullscreen;
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003403
3404 surface = weston_surface_get_main_surface(focus);
3405 if (surface == NULL)
3406 return;
3407
3408 shsurf = get_shell_surface(surface);
3409 if (shsurf == NULL)
3410 return;
3411
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003412 fullscreen =
3413 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003414
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003415 set_fullscreen(shsurf, !fullscreen, NULL);
Rafael Antognolliea997ac2013-12-03 15:35:48 -02003416}
3417
3418static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003419touch_move_binding(struct weston_touch *touch, const struct timespec *time, void *data)
Neil Robertsaba0f252013-10-03 16:43:05 +01003420{
Derek Foreman362656b2014-09-04 10:23:05 -05003421 struct weston_surface *focus;
Neil Robertsaba0f252013-10-03 16:43:05 +01003422 struct weston_surface *surface;
3423 struct shell_surface *shsurf;
3424
Derek Foreman8ae2db52015-07-15 13:00:36 -05003425 if (touch->focus == NULL)
Derek Foreman362656b2014-09-04 10:23:05 -05003426 return;
3427
Derek Foreman8ae2db52015-07-15 13:00:36 -05003428 focus = touch->focus->surface;
Neil Robertsaba0f252013-10-03 16:43:05 +01003429 surface = weston_surface_get_main_surface(focus);
3430 if (surface == NULL)
3431 return;
3432
3433 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003434 if (shsurf == NULL ||
3435 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3436 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Neil Robertsaba0f252013-10-03 16:43:05 +01003437 return;
3438
Derek Foremanb7674ae2015-07-15 13:00:37 -05003439 surface_touch_move(shsurf, touch);
Neil Robertsaba0f252013-10-03 16:43:05 +01003440}
3441
3442static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003443resize_binding(struct weston_pointer *pointer, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003444 uint32_t button, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003445{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003446 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003447 struct weston_surface *surface;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003448 uint32_t edges = 0;
3449 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003450 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05003451
Derek Foreman8ae2db52015-07-15 13:00:36 -05003452 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003453 return;
3454
Derek Foreman8ae2db52015-07-15 13:00:36 -05003455 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003456
Pekka Paalanen01388e22013-04-25 13:57:44 +03003457 surface = weston_surface_get_main_surface(focus);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01003458 if (surface == NULL)
3459 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003460
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003461 shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003462 if (shsurf == NULL ||
3463 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3464 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02003465 return;
3466
Jason Ekstranda7af7042013-10-12 22:38:11 -05003467 weston_view_from_global(shsurf->view,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003468 wl_fixed_to_int(pointer->grab_x),
3469 wl_fixed_to_int(pointer->grab_y),
Jason Ekstranda7af7042013-10-12 22:38:11 -05003470 &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04003471
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003472 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003473 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003474 else if (x < 2 * surface->width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003475 edges |= 0;
3476 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003477 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003478
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003479 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003480 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003481 else if (y < 2 * surface->height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04003482 edges |= 0;
3483 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02003484 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04003485
Derek Foreman8fbebbd2015-07-15 13:00:40 -05003486 surface_resize(shsurf, pointer, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003487}
3488
3489static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003490surface_opacity_binding(struct weston_pointer *pointer,
3491 const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003492 struct weston_pointer_axis_event *event,
3493 void *data)
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003494{
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003495 float step = 0.005;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003496 struct shell_surface *shsurf;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003497 struct weston_surface *focus = pointer->focus->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003498 struct weston_surface *surface;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003499
Pekka Paalanen01388e22013-04-25 13:57:44 +03003500 /* XXX: broken for windows containing sub-surfaces */
3501 surface = weston_surface_get_main_surface(focus);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003502 if (surface == NULL)
3503 return;
3504
3505 shsurf = get_shell_surface(surface);
3506 if (!shsurf)
3507 return;
3508
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003509 shsurf->view->alpha -= event->value * step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003510
Jason Ekstranda7af7042013-10-12 22:38:11 -05003511 if (shsurf->view->alpha > 1.0)
3512 shsurf->view->alpha = 1.0;
3513 if (shsurf->view->alpha < step)
3514 shsurf->view->alpha = step;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003515
Jason Ekstranda7af7042013-10-12 22:38:11 -05003516 weston_view_geometry_dirty(shsurf->view);
Scott Moreaua3aa9c92012-03-22 11:01:03 -06003517 weston_surface_damage(surface);
3518}
3519
3520static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003521do_zoom(struct weston_seat *seat, const struct timespec *time, uint32_t key,
3522 uint32_t axis, double value)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003523{
Derek Foreman8aeeac82015-01-30 13:24:36 -06003524 struct weston_compositor *compositor = seat->compositor;
Derek Foreman1281a362015-07-31 16:55:32 -05003525 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003526 struct weston_output *output;
Scott Moreaue6603982012-06-11 13:07:51 -06003527 float increment;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003528
Derek Foreman1281a362015-07-31 16:55:32 -05003529 if (!pointer) {
Derek Foreman7ef3bed2015-01-06 14:28:13 -06003530 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3531 return;
3532 }
3533
Scott Moreauccbf29d2012-02-22 14:21:41 -07003534 wl_list_for_each(output, &compositor->output_list, link) {
3535 if (pixman_region32_contains_point(&output->region,
Derek Foreman1281a362015-07-31 16:55:32 -05003536 wl_fixed_to_double(pointer->x),
3537 wl_fixed_to_double(pointer->y),
Daniel Stone103db7f2012-05-08 17:17:55 +01003538 NULL)) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01003539 if (key == KEY_PAGEUP)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003540 increment = output->zoom.increment;
Daniel Stone325fc2d2012-05-30 16:31:58 +01003541 else if (key == KEY_PAGEDOWN)
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003542 increment = -output->zoom.increment;
3543 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
Jonas Ådahlb0b87ba2012-09-27 18:40:42 +02003544 /* For every pixel zoom 20th of a step */
Daniel Stone0c1e46e2012-05-30 16:31:59 +01003545 increment = output->zoom.increment *
Giulio Camuffo90a6fc62016-03-22 17:44:54 +02003546 -value / 20.0;
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003547 else
3548 increment = 0;
3549
Kristian Høgsberg9b68af02012-05-22 12:55:18 -04003550 output->zoom.level += increment;
Scott Moreauc6d7f602012-02-23 22:28:37 -07003551
Scott Moreaue6603982012-06-11 13:07:51 -06003552 if (output->zoom.level < 0.0)
Scott Moreau850ca422012-05-21 15:21:25 -06003553 output->zoom.level = 0.0;
Scott Moreaue6603982012-06-11 13:07:51 -06003554 else if (output->zoom.level > output->zoom.max_level)
3555 output->zoom.level = output->zoom.max_level;
Derek Foreman25bd8a72015-07-23 14:55:13 -05003556
3557 if (!output->zoom.active) {
3558 if (output->zoom.level <= 0.0)
3559 continue;
Derek Foreman22276a52015-07-23 14:55:15 -05003560 weston_output_activate_zoom(output, seat);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04003561 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07003562
Scott Moreaue6603982012-06-11 13:07:51 -06003563 output->zoom.spring_z.target = output->zoom.level;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003564
Jason Ekstranda7af7042013-10-12 22:38:11 -05003565 weston_output_update_zoom(output);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003566 }
3567 }
3568}
3569
Scott Moreauccbf29d2012-02-22 14:21:41 -07003570static void
Alexandros Frantzis80321942017-11-16 18:20:56 +02003571zoom_axis_binding(struct weston_pointer *pointer, const struct timespec *time,
Peter Hutterer89b6a492016-01-18 15:58:17 +10003572 struct weston_pointer_axis_event *event,
3573 void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003574{
Peter Hutterer89b6a492016-01-18 15:58:17 +10003575 do_zoom(pointer->seat, time, 0, event->axis, event->value);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003576}
3577
3578static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003579zoom_key_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003580 uint32_t key, void *data)
Daniel Stone325fc2d2012-05-30 16:31:58 +01003581{
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003582 do_zoom(keyboard->seat, time, key, 0, 0);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003583}
3584
3585static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02003586terminate_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003587 uint32_t key, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003588{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003589 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003590
Pekka Paalanen052032d2019-02-06 10:44:37 +02003591 weston_compositor_exit(compositor);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003592}
3593
Emilio Pozuelo Monfort03251b62013-11-19 11:37:16 +01003594static void
Alexandros Frantzis84b31f82017-11-24 18:01:46 +02003595rotate_grab_motion(struct weston_pointer_grab *grab,
3596 const struct timespec *time,
Jonas Ådahld2510102014-10-05 21:39:14 +02003597 struct weston_pointer_motion_event *event)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003598{
3599 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003600 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003601 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003602 struct shell_surface *shsurf = rotate->base.shsurf;
Sergey Bugaev14ef2012019-02-11 22:55:09 +03003603 struct weston_surface *surface;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003604 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003605
Jonas Ådahld2510102014-10-05 21:39:14 +02003606 weston_pointer_move(pointer, event);
Giulio Camuffo1959ab82013-11-14 23:42:52 +01003607
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003608 if (!shsurf)
3609 return;
3610
Sergey Bugaev14ef2012019-02-11 22:55:09 +03003611 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
3612
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003613 cx = 0.5f * surface->width;
3614 cy = 0.5f * surface->height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003615
Daniel Stone37816df2012-05-16 18:45:18 +01003616 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3617 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003618 r = sqrtf(dx * dx + dy * dy);
3619
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003620 wl_list_remove(&shsurf->rotation.transform.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003621 weston_view_geometry_dirty(shsurf->view);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003622
3623 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003624 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003625 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003626
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003627 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003628 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003629
3630 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003631 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003632 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003633 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02003634 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003635
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02003636 wl_list_insert(
Jason Ekstranda7af7042013-10-12 22:38:11 -05003637 &shsurf->view->geometry.transformation_list,
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003638 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003639 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003640 wl_list_init(&shsurf->rotation.transform.link);
3641 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003642 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003643 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003644
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003645 /* We need to adjust the position of the surface
3646 * in case it was resized in a rotated state before */
Jason Ekstranda7af7042013-10-12 22:38:11 -05003647 cposx = shsurf->view->geometry.x + cx;
3648 cposy = shsurf->view->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003649 dposx = rotate->center.x - cposx;
3650 dposy = rotate->center.y - cposy;
3651 if (dposx != 0.0f || dposy != 0.0f) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003652 weston_view_set_position(shsurf->view,
3653 shsurf->view->geometry.x + dposx,
3654 shsurf->view->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003655 }
3656
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003657 /* Repaint implies weston_view_update_transform(), which
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02003658 * lazily applies the damage due to rotation update.
3659 */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003660 weston_compositor_schedule_repaint(surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003661}
3662
3663static void
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003664rotate_grab_button(struct weston_pointer_grab *grab,
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003665 const struct timespec *time,
3666 uint32_t button, uint32_t state_w)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003667{
3668 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003669 container_of(grab, struct rotate_grab, base.grab);
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003670 struct weston_pointer *pointer = grab->pointer;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003671 struct shell_surface *shsurf = rotate->base.shsurf;
Daniel Stone4dbadb12012-05-30 16:31:51 +01003672 enum wl_pointer_button_state state = state_w;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003673
Daniel Stone4dbadb12012-05-30 16:31:51 +01003674 if (pointer->button_count == 0 &&
3675 state == WL_POINTER_BUTTON_STATE_RELEASED) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03003676 if (shsurf)
3677 weston_matrix_multiply(&shsurf->rotation.rotation,
3678 &rotate->rotation);
Ander Conselvan de Oliveirab9d2a0f2012-06-28 18:08:05 +03003679 shell_grab_end(&rotate->base);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003680 free(rotate);
3681 }
3682}
3683
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003684static void
3685rotate_grab_cancel(struct weston_pointer_grab *grab)
3686{
3687 struct rotate_grab *rotate =
3688 container_of(grab, struct rotate_grab, base.grab);
3689
3690 shell_grab_end(&rotate->base);
3691 free(rotate);
3692}
3693
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04003694static const struct weston_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02003695 noop_grab_focus,
3696 rotate_grab_motion,
3697 rotate_grab_button,
Jonas Ådahl0336ca02014-10-04 16:28:29 +02003698 noop_grab_axis,
Peter Hutterer87743e92016-01-18 16:38:22 +10003699 noop_grab_axis_source,
3700 noop_grab_frame,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02003701 rotate_grab_cancel,
Pekka Paalanen460099f2012-01-20 16:48:25 +02003702};
3703
3704static void
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003705surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
Pekka Paalanen460099f2012-01-20 16:48:25 +02003706{
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003707 struct weston_surface *surface =
3708 weston_desktop_surface_get_surface(shsurf->desktop_surface);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003709 struct rotate_grab *rotate;
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02003710 float dx, dy;
3711 float r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02003712
Pekka Paalanen460099f2012-01-20 16:48:25 +02003713 rotate = malloc(sizeof *rotate);
3714 if (!rotate)
3715 return;
3716
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003717 weston_view_to_global_float(shsurf->view,
3718 surface->width * 0.5f,
3719 surface->height * 0.5f,
Jason Ekstranda7af7042013-10-12 22:38:11 -05003720 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003721
Derek Foreman74de4692015-07-15 13:00:39 -05003722 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3723 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003724 r = sqrtf(dx * dx + dy * dy);
3725 if (r > 20.0f) {
3726 struct weston_matrix inverse;
3727
3728 weston_matrix_init(&inverse);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003729 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003730 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01003731
3732 weston_matrix_init(&rotate->rotation);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003733 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003734 } else {
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003735 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05003736 weston_matrix_init(&rotate->rotation);
3737 }
3738
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003739 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08003740 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
Pekka Paalanen460099f2012-01-20 16:48:25 +02003741}
3742
3743static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003744rotate_binding(struct weston_pointer *pointer, const struct timespec *time,
3745 uint32_t button, void *data)
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003746{
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003747 struct weston_surface *focus;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003748 struct weston_surface *base_surface;
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003749 struct shell_surface *surface;
3750
Derek Foreman8ae2db52015-07-15 13:00:36 -05003751 if (pointer->focus == NULL)
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003752 return;
3753
Derek Foreman8ae2db52015-07-15 13:00:36 -05003754 focus = pointer->focus->surface;
Rafal Mielniczukb2917a22014-01-05 20:04:59 +01003755
Pekka Paalanen01388e22013-04-25 13:57:44 +03003756 base_surface = weston_surface_get_main_surface(focus);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003757 if (base_surface == NULL)
3758 return;
3759
3760 surface = get_shell_surface(base_surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003761 if (surface == NULL ||
3762 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3763 weston_desktop_surface_get_maximized(surface->desktop_surface))
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003764 return;
3765
Derek Foreman74de4692015-07-15 13:00:39 -05003766 surface_rotate(surface, pointer);
Kristian Høgsberg0c369032013-02-14 21:31:44 -05003767}
3768
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003769/* Move all fullscreen layers down to the current workspace and hide their
3770 * black views. The surfaces' state is set to both fullscreen and lowered,
3771 * and this is reversed when such a surface is re-configured, see
3772 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3773 *
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003774 * lowering_output = NULL - Lower on all outputs, else only lower on the
3775 * specified output.
3776 *
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003777 * This should be used when implementing shell-wide overlays, such as
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003778 * the alt-tab switcher, which need to de-promote fullscreen layers. */
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003779void
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003780lower_fullscreen_layer(struct desktop_shell *shell,
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003781 struct weston_output *lowering_output)
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003782{
3783 struct workspace *ws;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003784 struct weston_view *view, *prev;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003785
3786 ws = get_current_workspace(shell);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003787 wl_list_for_each_reverse_safe(view, prev,
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003788 &shell->fullscreen_layer.view_list.link,
3789 layer_link.link) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003790 struct shell_surface *shsurf = get_shell_surface(view->surface);
3791
3792 if (!shsurf)
3793 continue;
3794
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003795 /* Only lower surfaces which have lowering_output as their fullscreen
3796 * output, unless a NULL output asks for lowering on all outputs.
3797 */
3798 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3799 continue;
3800
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003801 /* We can have a non-fullscreen popup for a fullscreen surface
3802 * in the fullscreen layer. */
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003803 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003804 /* Hide the black view */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003805 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3806 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
Kristian Høgsbergc9915132014-05-09 16:24:07 -07003807 weston_view_damage_below(shsurf->fullscreen.black_view);
3808
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003809 }
3810
3811 /* Lower the view to the workspace layer */
Giulio Camuffo412e6a52014-07-09 22:12:56 +03003812 weston_layer_entry_remove(&view->layer_link);
3813 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003814 weston_view_damage_below(view);
3815 weston_surface_damage(view->surface);
Emilio Pozuelo Monfort9e7c7592014-01-30 14:01:10 +01003816
3817 shsurf->state.lowered = true;
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003818 }
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04003819}
3820
Stefan Agnera8da2082019-06-23 14:53:59 +02003821static struct shell_surface *get_last_child(struct shell_surface *shsurf)
3822{
3823 struct shell_surface *shsurf_child;
3824
3825 wl_list_for_each_reverse(shsurf_child, &shsurf->children_list, children_link) {
3826 if (weston_view_is_mapped(shsurf_child->view))
3827 return shsurf_child;
3828 }
3829
3830 return NULL;
3831}
3832
Kristian Høgsberg1ef23132013-12-04 00:20:01 -08003833void
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003834activate(struct desktop_shell *shell, struct weston_view *view,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003835 struct weston_seat *seat, uint32_t flags)
Kristian Høgsberg75840622011-09-06 13:48:16 -04003836{
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02003837 struct weston_surface *es = view->surface;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003838 struct weston_surface *main_surface;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003839 struct focus_state *state;
Jonas Ådahl8538b222012-08-29 22:13:03 +02003840 struct workspace *ws;
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003841 struct weston_surface *old_es;
Stefan Agnera8da2082019-06-23 14:53:59 +02003842 struct shell_surface *shsurf, *shsurf_child;
Kristian Høgsberg75840622011-09-06 13:48:16 -04003843
Pekka Paalanen01388e22013-04-25 13:57:44 +03003844 main_surface = weston_surface_get_main_surface(es);
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003845 shsurf = get_shell_surface(main_surface);
3846 assert(shsurf);
3847
Stefan Agnera8da2082019-06-23 14:53:59 +02003848 shsurf_child = get_last_child(shsurf);
3849 if (shsurf_child) {
3850 /* Activate last xdg child instead of parent. */
3851 activate(shell, shsurf_child->view, seat, flags);
3852 return;
3853 }
3854
Mario Kleiner9f4d6552015-06-21 21:25:08 +02003855 /* Only demote fullscreen surfaces on the output of activated shsurf.
3856 * Leave fullscreen surfaces on unrelated outputs alone. */
Pekka Paalanen87860c22018-05-02 10:21:57 +02003857 if (shsurf->output)
3858 lower_fullscreen_layer(shell, shsurf->output);
Pekka Paalanen01388e22013-04-25 13:57:44 +03003859
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003860 weston_view_activate(view, seat, flags);
Kristian Høgsberg75840622011-09-06 13:48:16 -04003861
Jonas Ådahl8538b222012-08-29 22:13:03 +02003862 state = ensure_focus_state(shell, seat);
3863 if (state == NULL)
3864 return;
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003865
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003866 old_es = state->keyboard_focus;
Kristian Høgsbergd500bf12014-01-22 12:25:20 -08003867 focus_state_set_focus(state, es);
Kristian Høgsberg2f5faff2012-07-31 16:36:34 -04003868
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003869 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3870 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
Rafael Antognolli03b16592013-12-03 15:35:42 -02003871 shell_configure_fullscreen(shsurf);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003872
Emilio Pozuelo Monfort7908bff2014-01-30 13:49:39 +01003873 /* Update the surface’s layer. This brings it to the top of the stacking
3874 * order as appropriate. */
3875 shell_surface_update_layer(shsurf);
3876
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003877 if (shell->focus_animation_type != ANIMATION_NONE) {
3878 ws = get_current_workspace(shell);
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01003879 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
Philip Withnall83ffd9d2013-11-25 18:01:42 +00003880 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04003881}
3882
Alex Wu21858432012-04-01 20:13:08 +08003883/* no-op func for checking black surface */
3884static void
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003885black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
Alex Wu21858432012-04-01 20:13:08 +08003886{
3887}
3888
Quentin Glidicc0d79ce2013-01-29 14:16:13 +01003889static bool
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003890is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
Alex Wu21858432012-04-01 20:13:08 +08003891{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003892 struct weston_surface *surface = view->surface;
3893
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003894 if (surface->committed == black_surface_committed) {
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003895 if (fs_view)
Quentin Glidic2edc3d52016-08-12 10:41:33 +02003896 *fs_view = surface->committed_private;
Alex Wu21858432012-04-01 20:13:08 +08003897 return true;
3898 }
3899 return false;
3900}
3901
Kristian Høgsberg75840622011-09-06 13:48:16 -04003902static void
Neil Robertsa28c6932013-10-03 16:43:04 +01003903activate_binding(struct weston_seat *seat,
3904 struct desktop_shell *shell,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003905 struct weston_view *focus_view,
3906 uint32_t flags)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003907{
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003908 struct weston_view *main_view;
Pekka Paalanen01388e22013-04-25 13:57:44 +03003909 struct weston_surface *main_surface;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003910
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003911 if (!focus_view)
3912 return;
Alex Wu9c35e6b2012-03-05 14:13:13 +08003913
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003914 if (is_black_surface_view(focus_view, &main_view))
3915 focus_view = main_view;
Alex Wu4539b082012-03-01 12:57:46 +08003916
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02003917 main_surface = weston_surface_get_main_surface(focus_view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02003918 if (!get_shell_surface(main_surface))
Kristian Høgsberg0636ac32012-06-27 10:22:15 -04003919 return;
Kristian Høgsberg85b2e4b2012-06-21 16:49:42 -04003920
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003921 activate(shell, focus_view, seat, flags);
Neil Robertsa28c6932013-10-03 16:43:04 +01003922}
3923
3924static void
Alexandros Frantzis215bedc2017-11-16 18:20:55 +02003925click_to_activate_binding(struct weston_pointer *pointer,
3926 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003927 uint32_t button, void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003928{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003929 if (pointer->grab != &pointer->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003930 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003931 if (pointer->focus == NULL)
Kristian Høgsberg7c4f6cc2014-01-01 16:28:32 -08003932 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003933
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003934 activate_binding(pointer->seat, data, pointer->focus,
Jonas Ådahl94e2e2d2014-10-18 18:42:19 +02003935 WESTON_ACTIVATE_FLAG_CLICKED |
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003936 WESTON_ACTIVATE_FLAG_CONFIGURE);
Neil Robertsa28c6932013-10-03 16:43:04 +01003937}
3938
3939static void
Alexandros Frantzis9448deb2017-11-16 18:20:58 +02003940touch_to_activate_binding(struct weston_touch *touch,
3941 const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05003942 void *data)
Neil Robertsa28c6932013-10-03 16:43:04 +01003943{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003944 if (touch->grab != &touch->default_grab)
Neil Robertsa28c6932013-10-03 16:43:04 +01003945 return;
Derek Foreman8ae2db52015-07-15 13:00:36 -05003946 if (touch->focus == NULL)
Kristian Høgsberg0ed67502014-01-02 23:00:11 -08003947 return;
Neil Robertsa28c6932013-10-03 16:43:04 +01003948
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02003949 activate_binding(touch->seat, data, touch->focus,
3950 WESTON_ACTIVATE_FLAG_CONFIGURE);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05003951}
3952
3953static void
Neil Robertsb4a91702014-04-09 16:33:32 +01003954unfocus_all_seats(struct desktop_shell *shell)
3955{
3956 struct weston_seat *seat, *next;
3957
3958 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05003959 struct weston_keyboard *keyboard =
3960 weston_seat_get_keyboard(seat);
3961
3962 if (!keyboard)
Neil Robertsb4a91702014-04-09 16:33:32 +01003963 continue;
3964
Derek Foreman1281a362015-07-31 16:55:32 -05003965 weston_keyboard_set_focus(keyboard, NULL);
Neil Robertsb4a91702014-04-09 16:33:32 +01003966 }
3967}
3968
3969static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003970lock(struct desktop_shell *shell)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003971{
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04003972 struct workspace *ws = get_current_workspace(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003973
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003974 if (shell->locked) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003975 weston_compositor_sleep(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003976 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003977 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003978
3979 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003980
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003981 /* Hide all surfaces by removing the fullscreen, panel and
3982 * toplevel layers. This way nothing else can show or receive
3983 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003984
Quentin Glidic82681572016-12-17 13:40:51 +01003985 weston_layer_unset_position(&shell->panel_layer);
3986 weston_layer_unset_position(&shell->fullscreen_layer);
Philipp Brüschweiler711fda82012-08-09 18:50:43 +02003987 if (shell->showing_input_panels)
Quentin Glidic82681572016-12-17 13:40:51 +01003988 weston_layer_unset_position(&shell->input_panel_layer);
3989 weston_layer_unset_position(&ws->layer);
3990
3991 weston_layer_set_position(&shell->lock_layer,
3992 WESTON_LAYER_POSITION_LOCK);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02003993
Derek Foreman004b4a12015-07-20 16:28:13 -05003994 weston_compositor_sleep(shell->compositor);
3995
Neil Robertsb4a91702014-04-09 16:33:32 +01003996 /* Remove the keyboard focus on all seats. This will be
3997 * restored to the workspace's saved state via
3998 * restore_focus_state when the compositor is unlocked */
3999 unfocus_all_seats(shell);
4000
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004001 /* TODO: disable bindings that should not work while locked. */
4002
4003 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004004}
4005
4006static void
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004007unlock(struct desktop_shell *shell)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004008{
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004009 struct wl_resource *shell_resource;
4010
Pekka Paalanend81c2162011-11-16 13:47:34 +02004011 if (!shell->locked || shell->lock_surface) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02004012 shell_fade(shell, FADE_IN);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004013 return;
4014 }
4015
4016 /* If desktop-shell client has gone away, unlock immediately. */
4017 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02004018 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004019 return;
4020 }
4021
4022 if (shell->prepare_event_sent)
4023 return;
4024
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004025 shell_resource = shell->child.desktop_shell;
4026 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004027 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004028}
4029
4030static void
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004031shell_fade_done_for_output(struct weston_view_animation *animation, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004032{
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004033 struct shell_output *shell_output = data;
4034 struct desktop_shell *shell = shell_output->shell;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004035
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004036 shell_output->fade.animation = NULL;
4037 switch (shell_output->fade.type) {
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004038 case FADE_IN:
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004039 weston_surface_destroy(shell_output->fade.view->surface);
4040 shell_output->fade.view = NULL;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004041 break;
4042 case FADE_OUT:
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004043 lock(shell);
4044 break;
Philip Withnall4a86a0a2013-11-25 18:01:32 +00004045 default:
4046 break;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004047 }
4048}
4049
Jason Ekstranda7af7042013-10-12 22:38:11 -05004050static struct weston_view *
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004051shell_fade_create_surface_for_output(struct desktop_shell *shell, struct shell_output *shell_output)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004052{
4053 struct weston_compositor *compositor = shell->compositor;
4054 struct weston_surface *surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004055 struct weston_view *view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004056
4057 surface = weston_surface_create(compositor);
4058 if (!surface)
4059 return NULL;
4060
Jason Ekstranda7af7042013-10-12 22:38:11 -05004061 view = weston_view_create(surface);
4062 if (!view) {
4063 weston_surface_destroy(surface);
4064 return NULL;
4065 }
4066
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004067 weston_surface_set_size(surface, shell_output->output->width, shell_output->output->height);
4068 weston_view_set_position(view, shell_output->output->x, shell_output->output->y);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004069 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004070 weston_layer_entry_insert(&compositor->fade_layer.view_list,
4071 &view->layer_link);
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004072 pixman_region32_init(&surface->input);
Armin Krezović4663aca2016-06-30 06:04:29 +02004073 surface->is_mapped = true;
4074 view->is_mapped = true;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004075
Jason Ekstranda7af7042013-10-12 22:38:11 -05004076 return view;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004077}
4078
4079static void
4080shell_fade(struct desktop_shell *shell, enum fade_type type)
4081{
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004082 float tint;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004083 struct shell_output *shell_output;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004084
4085 switch (type) {
4086 case FADE_IN:
4087 tint = 0.0;
4088 break;
4089 case FADE_OUT:
4090 tint = 1.0;
4091 break;
4092 default:
Bryce Harringtonb3197f42016-08-30 12:05:27 -07004093 weston_log("shell: invalid fade type\n");
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004094 return;
4095 }
4096
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004097 /* Create a separate fade surface for each output */
4098 wl_list_for_each(shell_output, &shell->output_list, link) {
4099 shell_output->fade.type = type;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004100
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004101 if (shell_output->fade.view == NULL) {
4102 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4103 if (!shell_output->fade.view)
4104 continue;
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004105
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004106 shell_output->fade.view->alpha = 1.0 - tint;
4107 weston_view_update_transform(shell_output->fade.view);
4108 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004109
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004110 if (shell_output->fade.view->output == NULL) {
4111 /* If the black view gets a NULL output, we lost the
4112 * last output and we'll just cancel the fade. This
4113 * happens when you close the last window under the
4114 * X11 or Wayland backends. */
4115 shell->locked = false;
4116 weston_surface_destroy(shell_output->fade.view->surface);
4117 shell_output->fade.view = NULL;
4118 } else if (shell_output->fade.animation) {
4119 weston_fade_update(shell_output->fade.animation, tint);
4120 } else {
4121 shell_output->fade.animation =
4122 weston_fade_run(shell_output->fade.view,
4123 1.0 - tint, tint, 300.0,
4124 shell_fade_done_for_output, shell_output);
4125 }
Kristian Høgsberg87d3b612014-01-19 21:48:10 -08004126 }
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004127}
4128
4129static void
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004130do_shell_fade_startup(void *data)
4131{
4132 struct desktop_shell *shell = data;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004133 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004134
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004135 if (shell->startup_animation_type == ANIMATION_FADE) {
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004136 shell_fade(shell, FADE_IN);
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004137 } else {
4138 weston_log("desktop shell: "
4139 "unexpected fade-in animation type %d\n",
4140 shell->startup_animation_type);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004141 wl_list_for_each(shell_output, &shell->output_list, link) {
4142 weston_surface_destroy(shell_output->fade.view->surface);
4143 shell_output->fade.view = NULL;
4144 }
Kristian Høgsberg724c8d92013-10-16 11:38:24 -07004145 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004146}
4147
4148static void
4149shell_fade_startup(struct desktop_shell *shell)
4150{
4151 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004152 struct shell_output *shell_output;
4153 bool has_fade = false;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004154
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004155 wl_list_for_each(shell_output, &shell->output_list, link) {
4156 if (!shell_output->fade.startup_timer)
4157 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004158
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004159 wl_event_source_remove(shell_output->fade.startup_timer);
4160 shell_output->fade.startup_timer = NULL;
4161 has_fade = true;
4162 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004163
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004164 if (has_fade) {
4165 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4166 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4167 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004168}
4169
4170static int
4171fade_startup_timeout(void *data)
4172{
4173 struct desktop_shell *shell = data;
4174
4175 shell_fade_startup(shell);
4176 return 0;
4177}
4178
4179static void
4180shell_fade_init(struct desktop_shell *shell)
4181{
4182 /* Make compositor output all black, and wait for the desktop-shell
4183 * client to signal it is ready, then fade in. The timer triggers a
4184 * fade-in, in case the desktop-shell client takes too long.
4185 */
4186
4187 struct wl_event_loop *loop;
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004188 struct shell_output *shell_output;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004189
Pekka Paalanen23ed5f22015-05-26 11:54:52 +03004190 if (shell->startup_animation_type == ANIMATION_NONE)
4191 return;
4192
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004193 wl_list_for_each(shell_output, &shell->output_list, link) {
4194 if (shell_output->fade.view != NULL) {
4195 weston_log("%s: warning: fade surface already exists\n",
4196 __func__);
4197 continue;
4198 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004199
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004200 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4201 if (!shell_output->fade.view)
4202 continue;
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004203
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004204 weston_view_update_transform(shell_output->fade.view);
4205 weston_surface_damage(shell_output->fade.view->surface);
4206
4207 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4208 shell_output->fade.startup_timer =
4209 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4210 wl_event_source_timer_update(shell_output->fade.startup_timer, 15000);
4211 }
Pekka Paalanen79346ab2013-05-22 18:03:09 +03004212}
4213
4214static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004215idle_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004216{
4217 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004218 container_of(listener, struct desktop_shell, idle_listener);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004219
Kristian Høgsberg27d5fa82014-01-17 16:22:50 -08004220 struct weston_seat *seat;
4221
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004222 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4223 weston_seat_break_desktop_grabs(seat);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004224
4225 shell_fade(shell, FADE_OUT);
Bryce Harrington9ad4de12016-09-09 13:16:02 -07004226 /* lock() is called from shell_fade_done_for_output() */
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004227}
4228
4229static void
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004230wake_handler(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004231{
4232 struct desktop_shell *shell =
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004233 container_of(listener, struct desktop_shell, wake_listener);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004234
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02004235 unlock(shell);
4236}
4237
4238static void
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004239transform_handler(struct wl_listener *listener, void *data)
4240{
4241 struct weston_surface *surface = data;
4242 struct shell_surface *shsurf = get_shell_surface(surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004243 const struct weston_xwayland_surface_api *api;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004244 int x, y;
4245
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004246 if (!shsurf)
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004247 return;
4248
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004249 api = shsurf->shell->xwayland_surface_api;
4250 if (!api) {
4251 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
4252 shsurf->shell->xwayland_surface_api = api;
4253 }
4254
4255 if (!api || !api->is_xwayland_surface(surface))
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004256 return;
4257
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004258 if (!weston_view_is_mapped(shsurf->view))
4259 return;
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004260
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004261 x = shsurf->view->geometry.x;
4262 y = shsurf->view->geometry.y;
4263
4264 api->send_position(surface, x, y);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02004265}
4266
4267static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004268center_on_output(struct weston_view *view, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02004269{
Giulio Camuffob8366642013-04-25 13:57:46 +03004270 int32_t surf_x, surf_y, width, height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004271 float x, y;
Pekka Paalanen77346a62011-11-30 16:26:35 +02004272
Pekka Paalanen30aa5972018-05-02 10:21:56 +02004273 if (!output) {
4274 weston_view_set_position(view, 0, 0);
4275 return;
4276 }
4277
Jason Ekstranda7af7042013-10-12 22:38:11 -05004278 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
Giulio Camuffob8366642013-04-25 13:57:46 +03004279
4280 x = output->x + (output->width - width) / 2 - surf_x / 2;
4281 y = output->y + (output->height - height) / 2 - surf_y / 2;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02004282
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06004283 weston_view_set_position(view, x, y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02004284}
4285
4286static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05004287weston_view_set_initial_position(struct weston_view *view,
4288 struct desktop_shell *shell)
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004289{
4290 struct weston_compositor *compositor = shell->compositor;
4291 int ix = 0, iy = 0;
Jonny Lambd73c6942014-08-20 15:53:20 +02004292 int32_t range_x, range_y;
Derek Foremanf814c5d2015-04-15 12:23:54 -05004293 int32_t x, y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004294 struct weston_output *output, *target_output = NULL;
4295 struct weston_seat *seat;
Jonny Lambd73c6942014-08-20 15:53:20 +02004296 pixman_rectangle32_t area;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004297
4298 /* As a heuristic place the new window on the same output as the
4299 * pointer. Falling back to the output containing 0, 0.
4300 *
4301 * TODO: Do something clever for touch too?
4302 */
4303 wl_list_for_each(seat, &compositor->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05004304 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4305
4306 if (pointer) {
4307 ix = wl_fixed_to_int(pointer->x);
4308 iy = wl_fixed_to_int(pointer->y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004309 break;
4310 }
4311 }
4312
4313 wl_list_for_each(output, &compositor->output_list, link) {
4314 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4315 target_output = output;
4316 break;
4317 }
4318 }
4319
4320 if (!target_output) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004321 weston_view_set_position(view, 10 + random() % 400,
4322 10 + random() % 400);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004323 return;
4324 }
4325
4326 /* Valid range within output where the surface will still be onscreen.
4327 * If this is negative it means that the surface is bigger than
4328 * output.
4329 */
Jonny Lambd73c6942014-08-20 15:53:20 +02004330 get_output_work_area(shell, target_output, &area);
4331
Derek Foremanf814c5d2015-04-15 12:23:54 -05004332 x = area.x;
4333 y = area.y;
Jonny Lambd73c6942014-08-20 15:53:20 +02004334 range_x = area.width - view->surface->width;
4335 range_y = area.height - view->surface->height;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004336
Rob Bradford4cb88c72012-08-13 15:18:44 +01004337 if (range_x > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004338 x += random() % range_x;
Rob Bradford4cb88c72012-08-13 15:18:44 +01004339
4340 if (range_y > 0)
Derek Foremanf814c5d2015-04-15 12:23:54 -05004341 y += random() % range_y;
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004342
Jason Ekstranda7af7042013-10-12 22:38:11 -05004343 weston_view_set_position(view, x, y);
Rob Bradfordac63e5b2012-08-13 14:07:52 +01004344}
4345
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004346static bool
4347check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4348{
4349 struct timespec now;
4350
4351 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4352 return false;
4353
4354 /*
4355 * If the shell helper client dies before the session has been
4356 * up for roughly 30 seconds, better just make Weston shut down,
4357 * because the user likely has no way to interact with the desktop
4358 * anyway.
4359 */
4360 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4361 weston_log("Error: %s apparently cannot run at all.\n",
4362 shell->client);
4363 weston_log_continue(STAMP_SPACE "Quitting...");
Pekka Paalanen052032d2019-02-06 10:44:37 +02004364 weston_compositor_exit_with_code(shell->compositor,
4365 EXIT_FAILURE);
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004366
4367 return true;
4368 }
4369
4370 return false;
4371}
4372
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004373static void launch_desktop_shell_process(void *data);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004374
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04004375static void
Pekka Paalanen826dc142014-08-27 12:11:53 +03004376respawn_desktop_shell_process(struct desktop_shell *shell)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004377{
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004378 struct timespec time;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004379
4380 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02004381 weston_compositor_get_time(&time);
4382 if (timespec_sub_to_msec(&time, &shell->child.deathstamp) > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004383 shell->child.deathstamp = time;
4384 shell->child.deathcount = 0;
4385 }
4386
4387 shell->child.deathcount++;
4388 if (shell->child.deathcount > 5) {
Pekka Paalanen826dc142014-08-27 12:11:53 +03004389 weston_log("%s disconnected, giving up.\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004390 return;
4391 }
4392
Pekka Paalanen826dc142014-08-27 12:11:53 +03004393 weston_log("%s disconnected, respawning...\n", shell->client);
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02004394 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004395}
4396
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004397static void
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004398desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4399{
4400 struct desktop_shell *shell;
4401
4402 shell = container_of(listener, struct desktop_shell,
4403 child.client_destroy_listener);
4404
Pekka Paalanen826dc142014-08-27 12:11:53 +03004405 wl_list_remove(&shell->child.client_destroy_listener.link);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004406 shell->child.client = NULL;
Pekka Paalanen826dc142014-08-27 12:11:53 +03004407 /*
4408 * unbind_desktop_shell() will reset shell->child.desktop_shell
4409 * before the respawned process has a chance to create a new
4410 * desktop_shell object, because we are being called from the
4411 * wl_client destructor which destroys all wl_resources before
4412 * returning.
4413 */
4414
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03004415 if (!check_desktop_shell_crash_too_early(shell))
4416 respawn_desktop_shell_process(shell);
4417
Pekka Paalanen826dc142014-08-27 12:11:53 +03004418 shell_fade_startup(shell);
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004419}
4420
4421static void
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004422launch_desktop_shell_process(void *data)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004423{
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03004424 struct desktop_shell *shell = data;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004425
Pekka Paalanen826dc142014-08-27 12:11:53 +03004426 shell->child.client = weston_client_start(shell->compositor,
4427 shell->client);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02004428
Arnaud Vrac42631192014-08-25 20:56:46 +02004429 if (!shell->child.client) {
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01004430 weston_log("not able to start %s\n", shell->client);
Arnaud Vrac42631192014-08-25 20:56:46 +02004431 return;
4432 }
Ander Conselvan de Oliveira312ea4c2013-12-20 21:07:01 +02004433
4434 shell->child.client_destroy_listener.notify =
4435 desktop_shell_client_destroy;
4436 wl_client_add_destroy_listener(shell->child.client,
4437 &shell->child.client_destroy_listener);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02004438}
4439
4440static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004441unbind_desktop_shell(struct wl_resource *resource)
4442{
Jason Ekstrand651f00e2013-06-14 10:07:54 -05004443 struct desktop_shell *shell = wl_resource_get_user_data(resource);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05004444
4445 if (shell->locked)
4446 resume_desktop(shell);
4447
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004448 shell->child.desktop_shell = NULL;
4449 shell->prepare_event_sent = false;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004450}
4451
4452static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04004453bind_desktop_shell(struct wl_client *client,
4454 void *data, uint32_t version, uint32_t id)
4455{
Tiago Vignattibe143262012-04-16 17:31:41 +03004456 struct desktop_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004457 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04004458
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08004459 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4460 1, id);
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004461
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004462 if (client == shell->child.client) {
Jason Ekstranda85118c2013-06-27 20:17:02 -05004463 wl_resource_set_implementation(resource,
4464 &desktop_shell_implementation,
4465 shell, unbind_desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004466 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004467 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02004468 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02004469
4470 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4471 "permission to bind desktop_shell denied");
Kristian Høgsberg75840622011-09-06 13:48:16 -04004472}
4473
Kristian Høgsberg07045392012-02-19 18:52:44 -05004474struct switcher {
Tiago Vignattibe143262012-04-16 17:31:41 +03004475 struct desktop_shell *shell;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004476 struct weston_view *current;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004477 struct wl_listener listener;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004478 struct weston_keyboard_grab grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004479 struct wl_array minimized_array;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004480};
4481
4482static void
4483switcher_next(struct switcher *switcher)
4484{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004485 struct weston_view *view;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004486 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04004487 struct shell_surface *shsurf;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004488 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004489
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004490 /* temporary re-display minimized surfaces */
4491 struct weston_view *tmp;
4492 struct weston_view **minimized;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004493 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4494 weston_layer_entry_remove(&view->layer_link);
4495 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004496 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4497 *minimized = view;
4498 }
4499
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004500 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Rafael Antognollied207b42013-12-03 15:35:43 -02004501 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004502 if (shsurf) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05004503 if (first == NULL)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004504 first = view;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004505 if (prev == switcher->current)
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004506 next = view;
4507 prev = view;
Jason Ekstranda7af7042013-10-12 22:38:11 -05004508 view->alpha = 0.25;
4509 weston_view_geometry_dirty(view);
4510 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004511 }
Alex Wu1659daa2012-04-01 20:13:09 +08004512
Jonas Ådahl7bfb1132014-10-18 12:23:20 +02004513 if (is_black_surface_view(view, NULL)) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05004514 view->alpha = 0.25;
4515 weston_view_geometry_dirty(view);
4516 weston_surface_damage(view->surface);
Alex Wu1659daa2012-04-01 20:13:09 +08004517 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05004518 }
4519
4520 if (next == NULL)
4521 next = first;
4522
Alex Wu07b26062012-03-12 16:06:01 +08004523 if (next == NULL)
4524 return;
4525
Kristian Høgsberg07045392012-02-19 18:52:44 -05004526 wl_list_remove(&switcher->listener.link);
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004527 wl_signal_add(&next->destroy_signal, &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004528
4529 switcher->current = next;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004530 wl_list_for_each(view, &next->surface->views, surface_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05004531 view->alpha = 1.0;
Alex Wu1659daa2012-04-01 20:13:09 +08004532
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004533 shsurf = get_shell_surface(switcher->current->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004534 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
Jason Ekstranda7af7042013-10-12 22:38:11 -05004535 shsurf->fullscreen.black_view->alpha = 1.0;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004536}
4537
4538static void
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004539switcher_handle_view_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004540{
4541 struct switcher *switcher =
4542 container_of(listener, struct switcher, listener);
4543
4544 switcher_next(switcher);
4545}
4546
4547static void
Daniel Stone351eb612012-05-31 15:27:47 -04004548switcher_destroy(struct switcher *switcher)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004549{
Jason Ekstranda7af7042013-10-12 22:38:11 -05004550 struct weston_view *view;
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004551 struct weston_keyboard *keyboard = switcher->grab.keyboard;
Kristian Høgsbergb0d8e772012-06-18 16:34:58 -04004552 struct workspace *ws = get_current_workspace(switcher->shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004553
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004554 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
Louis-Francis Ratté-Boulianneb482dbd2013-11-19 11:37:11 +01004555 if (is_focus_view(view))
4556 continue;
4557
Jason Ekstranda7af7042013-10-12 22:38:11 -05004558 view->alpha = 1.0;
4559 weston_surface_damage(view->surface);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004560 }
4561
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004562 if (switcher->current) {
Jonas Ådahl1fa6ded2014-10-18 13:24:53 +02004563 activate(switcher->shell, switcher->current,
Jonas Ådahl4361b4e2014-10-18 18:20:16 +02004564 keyboard->seat,
4565 WESTON_ACTIVATE_FLAG_CONFIGURE);
4566 }
4567
Kristian Høgsberg07045392012-02-19 18:52:44 -05004568 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004569 weston_keyboard_end_grab(keyboard);
4570 if (keyboard->input_method_resource)
4571 keyboard->grab = &keyboard->input_method_grab;
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004572
4573 /* re-hide surfaces that were temporary shown during the switch */
4574 struct weston_view **minimized;
4575 wl_array_for_each(minimized, &switcher->minimized_array) {
4576 /* with the exception of the current selected */
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004577 if ((*minimized)->surface != switcher->current->surface) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004578 weston_layer_entry_remove(&(*minimized)->layer_link);
4579 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004580 weston_view_damage_below(*minimized);
4581 }
4582 }
4583 wl_array_release(&switcher->minimized_array);
4584
Kristian Høgsberg07045392012-02-19 18:52:44 -05004585 free(switcher);
4586}
4587
4588static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004589switcher_key(struct weston_keyboard_grab *grab,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004590 const struct timespec *time, uint32_t key, uint32_t state_w)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004591{
4592 struct switcher *switcher = container_of(grab, struct switcher, grab);
Daniel Stonec9785ea2012-05-30 16:31:52 +01004593 enum wl_keyboard_key_state state = state_w;
Daniel Stone351eb612012-05-31 15:27:47 -04004594
Daniel Stonec9785ea2012-05-30 16:31:52 +01004595 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
Daniel Stone351eb612012-05-31 15:27:47 -04004596 switcher_next(switcher);
4597}
4598
4599static void
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004600switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
Daniel Stone351eb612012-05-31 15:27:47 -04004601 uint32_t mods_depressed, uint32_t mods_latched,
4602 uint32_t mods_locked, uint32_t group)
4603{
4604 struct switcher *switcher = container_of(grab, struct switcher, grab);
Derek Foreman8aeeac82015-01-30 13:24:36 -06004605 struct weston_seat *seat = grab->keyboard->seat;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004606
Daniel Stone351eb612012-05-31 15:27:47 -04004607 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4608 switcher_destroy(switcher);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04004609}
Kristian Høgsberg07045392012-02-19 18:52:44 -05004610
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004611static void
4612switcher_cancel(struct weston_keyboard_grab *grab)
4613{
4614 struct switcher *switcher = container_of(grab, struct switcher, grab);
4615
4616 switcher_destroy(switcher);
4617}
4618
Kristian Høgsberg29139d42013-04-18 15:25:39 -04004619static const struct weston_keyboard_grab_interface switcher_grab = {
Daniel Stone351eb612012-05-31 15:27:47 -04004620 switcher_key,
4621 switcher_modifier,
Jonas Ådahl1ea343e2013-10-25 23:18:05 +02004622 switcher_cancel,
Kristian Høgsberg07045392012-02-19 18:52:44 -05004623};
4624
4625static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004626switcher_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004627 uint32_t key, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05004628{
Tiago Vignattibe143262012-04-16 17:31:41 +03004629 struct desktop_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004630 struct switcher *switcher;
4631
4632 switcher = malloc(sizeof *switcher);
ganjingc1e71512020-07-28 15:28:45 +08004633 if (!switcher)
4634 return;
4635
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004636 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004637 switcher->current = NULL;
Jonas Ådahl90c90b22014-10-18 13:09:56 +02004638 switcher->listener.notify = switcher_handle_view_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05004639 wl_list_init(&switcher->listener.link);
Manuel Bachmannc1ae2e82014-02-26 15:53:11 +01004640 wl_array_init(&switcher->minimized_array);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004641
Mario Kleiner9f4d6552015-06-21 21:25:08 +02004642 lower_fullscreen_layer(switcher->shell, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004643 switcher->grab.interface = &switcher_grab;
Derek Foreman8ae2db52015-07-15 13:00:36 -05004644 weston_keyboard_start_grab(keyboard, &switcher->grab);
4645 weston_keyboard_set_focus(keyboard, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05004646 switcher_next(switcher);
4647}
4648
Pekka Paalanen3c647232011-12-22 13:43:43 +02004649static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004650backlight_binding(struct weston_keyboard *keyboard, const struct timespec *time,
Derek Foreman8ae2db52015-07-15 13:00:36 -05004651 uint32_t key, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004652{
4653 struct weston_compositor *compositor = data;
4654 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004655 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004656
4657 /* TODO: we're limiting to simple use cases, where we assume just
4658 * control on the primary display. We'd have to extend later if we
4659 * ever get support for setting backlights on random desktop LCD
4660 * panels though */
4661 output = get_default_output(compositor);
4662 if (!output)
4663 return;
4664
4665 if (!output->set_backlight)
4666 return;
4667
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004668 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4669 backlight_new = output->backlight_current - 25;
4670 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4671 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004672
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03004673 if (backlight_new < 5)
4674 backlight_new = 5;
4675 if (backlight_new > 255)
4676 backlight_new = 255;
4677
4678 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004679 output->set_backlight(output, output->backlight_current);
4680}
4681
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05004682static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004683force_kill_binding(struct weston_keyboard *keyboard,
4684 const struct timespec *time, uint32_t key, void *data)
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004685{
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04004686 struct weston_surface *focus_surface;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004687 struct wl_client *client;
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004688 struct desktop_shell *shell = data;
4689 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004690 pid_t pid;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004691
Derek Foreman8ae2db52015-07-15 13:00:36 -05004692 focus_surface = keyboard->focus;
Philipp Brüschweiler6cef0092012-08-13 21:27:27 +02004693 if (!focus_surface)
4694 return;
4695
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004696 wl_signal_emit(&compositor->kill_signal, focus_surface);
4697
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05004698 client = wl_resource_get_client(focus_surface->resource);
Tiago Vignatti920f1972012-09-27 17:48:35 +03004699 wl_client_get_credentials(client, &pid, NULL, NULL);
4700
4701 /* Skip clients that we launched ourselves (the credentials of
4702 * the socketpair is ours) */
4703 if (pid == getpid())
4704 return;
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004705
Daniel Stone325fc2d2012-05-30 16:31:58 +01004706 kill(pid, SIGKILL);
Kristian Høgsberg92a57db2012-05-26 13:41:06 -04004707}
4708
4709static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004710workspace_up_binding(struct weston_keyboard *keyboard,
4711 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004712{
4713 struct desktop_shell *shell = data;
4714 unsigned int new_index = shell->workspaces.current;
4715
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004716 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004717 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004718 if (new_index != 0)
4719 new_index--;
4720
4721 change_workspace(shell, new_index);
4722}
4723
4724static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004725workspace_down_binding(struct weston_keyboard *keyboard,
4726 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004727{
4728 struct desktop_shell *shell = data;
4729 unsigned int new_index = shell->workspaces.current;
4730
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004731 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004732 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004733 if (new_index < shell->workspaces.num - 1)
4734 new_index++;
4735
4736 change_workspace(shell, new_index);
4737}
4738
4739static void
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004740workspace_f_binding(struct weston_keyboard *keyboard,
4741 const struct timespec *time, uint32_t key, void *data)
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004742{
4743 struct desktop_shell *shell = data;
4744 unsigned int new_index;
4745
Kristian Høgsbergce345b02012-06-25 21:35:29 -04004746 if (shell->locked)
Kristian Høgsberg4476aaf2012-06-25 14:03:13 -04004747 return;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004748 new_index = key - KEY_F1;
4749 if (new_index >= shell->workspaces.num)
4750 new_index = shell->workspaces.num - 1;
4751
4752 change_workspace(shell, new_index);
4753}
4754
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004755static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004756workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004757 const struct timespec *time, uint32_t key,
4758 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004759{
4760 struct desktop_shell *shell = data;
4761 unsigned int new_index = shell->workspaces.current;
4762
4763 if (shell->locked)
4764 return;
4765
4766 if (new_index != 0)
4767 new_index--;
4768
Derek Foreman8ae2db52015-07-15 13:00:36 -05004769 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004770}
4771
4772static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05004773workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
Alexandros Frantzis47e79c82017-11-16 18:20:57 +02004774 const struct timespec *time, uint32_t key,
4775 void *data)
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004776{
4777 struct desktop_shell *shell = data;
4778 unsigned int new_index = shell->workspaces.current;
4779
4780 if (shell->locked)
4781 return;
4782
4783 if (new_index < shell->workspaces.num - 1)
4784 new_index++;
4785
Derek Foreman8ae2db52015-07-15 13:00:36 -05004786 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02004787}
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004788
4789static void
Harish Krupodc3edc02019-04-20 17:50:18 +05304790shell_reposition_view_on_output_change(struct weston_view *view)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004791{
4792 struct weston_output *output, *first_output;
4793 struct weston_compositor *ec = view->surface->compositor;
4794 struct shell_surface *shsurf;
4795 float x, y;
4796 int visible;
4797
Harish Krupo2dff4dd2019-04-20 18:10:56 +05304798 if (wl_list_empty(&ec->output_list))
4799 return;
4800
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004801 x = view->geometry.x;
4802 y = view->geometry.y;
4803
4804 /* At this point the destroyed output is not in the list anymore.
4805 * If the view is still visible somewhere, we leave where it is,
4806 * otherwise, move it to the first output. */
4807 visible = 0;
4808 wl_list_for_each(output, &ec->output_list, link) {
4809 if (pixman_region32_contains_point(&output->region,
4810 x, y, NULL)) {
4811 visible = 1;
4812 break;
4813 }
4814 }
4815
4816 if (!visible) {
4817 first_output = container_of(ec->output_list.next,
4818 struct weston_output, link);
4819
4820 x = first_output->x + first_output->width / 4;
4821 y = first_output->y + first_output->height / 4;
Xiong Zhang62899f52014-03-07 16:27:19 +08004822
4823 weston_view_set_position(view, x, y);
4824 } else {
4825 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004826 }
4827
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004828
4829 shsurf = get_shell_surface(view->surface);
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004830 if (!shsurf)
4831 return;
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004832
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02004833 shsurf->saved_position_valid = false;
4834 set_maximized(shsurf, false);
4835 set_fullscreen(shsurf, false, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004836}
4837
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004838void
4839shell_for_each_layer(struct desktop_shell *shell,
4840 shell_for_each_layer_func_t func, void *data)
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004841{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004842 struct workspace **ws;
4843
4844 func(shell, &shell->fullscreen_layer, data);
4845 func(shell, &shell->panel_layer, data);
4846 func(shell, &shell->background_layer, data);
4847 func(shell, &shell->lock_layer, data);
4848 func(shell, &shell->input_panel_layer, data);
4849
4850 wl_array_for_each(ws, &shell->workspaces.array)
4851 func(shell, &(*ws)->layer, data);
4852}
4853
4854static void
Harish Krupodc3edc02019-04-20 17:50:18 +05304855shell_output_changed_move_layer(struct desktop_shell *shell,
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004856 struct weston_layer *layer,
4857 void *data)
4858{
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004859 struct weston_view *view;
4860
Marius Vladea40d6d2018-02-08 18:46:42 +02004861 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Harish Krupodc3edc02019-04-20 17:50:18 +05304862 shell_reposition_view_on_output_change(view);
4863
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004864}
4865
4866static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004867handle_output_destroy(struct wl_listener *listener, void *data)
4868{
4869 struct shell_output *output_listener =
4870 container_of(listener, struct shell_output, destroy_listener);
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004871 struct desktop_shell *shell = output_listener->shell;
Xiong Zhang6b481422013-10-23 13:58:32 +08004872
Harish Krupodc3edc02019-04-20 17:50:18 +05304873 shell_for_each_layer(shell, shell_output_changed_move_layer, NULL);
Ander Conselvan de Oliveirac94d6222014-01-29 18:47:54 +02004874
Tomohito Esakibf31f6c2018-01-31 15:15:45 +09004875 if (output_listener->panel_surface)
4876 wl_list_remove(&output_listener->panel_surface_listener.link);
4877 if (output_listener->background_surface)
4878 wl_list_remove(&output_listener->background_surface_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08004879 wl_list_remove(&output_listener->destroy_listener.link);
4880 wl_list_remove(&output_listener->link);
4881 free(output_listener);
4882}
4883
4884static void
David Fort50d962f2016-06-09 17:55:52 +02004885shell_resize_surface_to_output(struct desktop_shell *shell,
4886 struct weston_surface *surface,
4887 const struct weston_output *output)
4888{
4889 if (!surface)
4890 return;
4891
4892 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4893 surface->resource,
4894 output->width,
4895 output->height);
4896}
4897
4898
4899static void
4900handle_output_resized(struct wl_listener *listener, void *data)
4901{
4902 struct desktop_shell *shell =
4903 container_of(listener, struct desktop_shell, resized_listener);
4904 struct weston_output *output = (struct weston_output *)data;
4905 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4906
4907 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4908 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4909}
4910
4911static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004912create_shell_output(struct desktop_shell *shell,
4913 struct weston_output *output)
4914{
4915 struct shell_output *shell_output;
4916
4917 shell_output = zalloc(sizeof *shell_output);
4918 if (shell_output == NULL)
4919 return;
4920
4921 shell_output->output = output;
4922 shell_output->shell = shell;
4923 shell_output->destroy_listener.notify = handle_output_destroy;
4924 wl_signal_add(&output->destroy_signal,
4925 &shell_output->destroy_listener);
Kristian Høgsberga3a0e182013-10-23 23:36:04 -07004926 wl_list_insert(shell->output_list.prev, &shell_output->link);
Harish Krupodc3edc02019-04-20 17:50:18 +05304927
4928 if (wl_list_length(&shell->output_list) == 1)
4929 shell_for_each_layer(shell,
4930 shell_output_changed_move_layer, NULL);
Xiong Zhang6b481422013-10-23 13:58:32 +08004931}
4932
4933static void
4934handle_output_create(struct wl_listener *listener, void *data)
4935{
4936 struct desktop_shell *shell =
4937 container_of(listener, struct desktop_shell, output_create_listener);
4938 struct weston_output *output = (struct weston_output *)data;
4939
4940 create_shell_output(shell, output);
4941}
4942
4943static void
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004944handle_output_move_layer(struct desktop_shell *shell,
4945 struct weston_layer *layer, void *data)
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004946{
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004947 struct weston_output *output = data;
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004948 struct weston_view *view;
4949 float x, y;
4950
Giulio Camuffo412e6a52014-07-09 22:12:56 +03004951 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004952 if (view->output != output)
4953 continue;
4954
4955 x = view->geometry.x + output->move_x;
4956 y = view->geometry.y + output->move_y;
4957 weston_view_set_position(view, x, y);
4958 }
4959}
4960
4961static void
4962handle_output_move(struct wl_listener *listener, void *data)
4963{
4964 struct desktop_shell *shell;
4965
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004966 shell = container_of(listener, struct desktop_shell,
4967 output_move_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004968
Ander Conselvan de Oliveira304996d2014-04-11 13:57:15 +03004969 shell_for_each_layer(shell, handle_output_move_layer, data);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004970}
4971
4972static void
Xiong Zhang6b481422013-10-23 13:58:32 +08004973setup_output_destroy_handler(struct weston_compositor *ec,
4974 struct desktop_shell *shell)
4975{
4976 struct weston_output *output;
4977
4978 wl_list_init(&shell->output_list);
4979 wl_list_for_each(output, &ec->output_list, link)
4980 create_shell_output(shell, output);
4981
4982 shell->output_create_listener.notify = handle_output_create;
4983 wl_signal_add(&ec->output_created_signal,
4984 &shell->output_create_listener);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004985
4986 shell->output_move_listener.notify = handle_output_move;
4987 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
Xiong Zhang6b481422013-10-23 13:58:32 +08004988}
4989
4990static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004991shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02004992{
Tiago Vignattibe143262012-04-16 17:31:41 +03004993 struct desktop_shell *shell =
4994 container_of(listener, struct desktop_shell, destroy_listener);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02004995 struct workspace **ws;
Xiong Zhang6b481422013-10-23 13:58:32 +08004996 struct shell_output *shell_output, *tmp;
Pekka Paalanen3c647232011-12-22 13:43:43 +02004997
Kristian Høgsberg17bccae2014-01-16 16:46:28 -08004998 /* Force state to unlocked so we don't try to fade */
4999 shell->locked = false;
Pekka Paalanen826dc142014-08-27 12:11:53 +03005000
5001 if (shell->child.client) {
5002 /* disable respawn */
5003 wl_list_remove(&shell->child.client_destroy_listener.link);
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005004 wl_client_destroy(shell->child.client);
Pekka Paalanen826dc142014-08-27 12:11:53 +03005005 }
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02005006
Harsha M Mb8b2c722018-08-07 19:05:02 +05305007 wl_list_remove(&shell->destroy_listener.link);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005008 wl_list_remove(&shell->idle_listener.link);
5009 wl_list_remove(&shell->wake_listener.link);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005010 wl_list_remove(&shell->transform_listener.link);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005011
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03005012 text_backend_destroy(shell->text_backend);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005013 input_panel_destroy(shell);
Kristian Høgsberg88c16072012-05-16 08:04:19 -04005014
Xiong Zhang6b481422013-10-23 13:58:32 +08005015 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5016 wl_list_remove(&shell_output->destroy_listener.link);
5017 wl_list_remove(&shell_output->link);
5018 free(shell_output);
5019 }
5020
5021 wl_list_remove(&shell->output_create_listener.link);
Kristian Høgsberg6d50b0f2014-04-30 20:46:25 -07005022 wl_list_remove(&shell->output_move_listener.link);
David Fort50d962f2016-06-09 17:55:52 +02005023 wl_list_remove(&shell->resized_listener.link);
Xiong Zhang6b481422013-10-23 13:58:32 +08005024
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005025 wl_array_for_each(ws, &shell->workspaces.array)
5026 workspace_destroy(*ws);
5027 wl_array_release(&shell->workspaces.array);
5028
Emilio Pozuelo Monfort46ce7982013-11-20 13:22:29 +01005029 free(shell->client);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005030 free(shell);
5031}
5032
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005033static void
5034shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5035{
5036 uint32_t mod;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005037 int i, num_workspace_bindings;
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005038
Bob Ham744e6532016-01-12 10:21:48 +00005039 if (shell->allow_zap)
5040 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5041 MODIFIER_CTRL | MODIFIER_ALT,
5042 terminate_binding, ec);
5043
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005044 /* fixed bindings */
Daniel Stone325fc2d2012-05-30 16:31:58 +01005045 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5046 click_to_activate_binding,
5047 shell);
Kristian Høgsbergf0ce5812014-04-07 11:52:17 -07005048 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
5049 click_to_activate_binding,
5050 shell);
Neil Robertsa28c6932013-10-03 16:43:04 +01005051 weston_compositor_add_touch_binding(ec, 0,
5052 touch_to_activate_binding,
5053 shell);
Bob Ham553d1242016-01-12 10:21:49 +00005054 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5055 backlight_binding, ec);
5056 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5057 backlight_binding, ec);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005058
5059 /* configurable bindings */
Bob Ham553d1242016-01-12 10:21:49 +00005060 if (shell->exposay_modifier)
5061 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5062 exposay_binding, shell);
5063
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005064 mod = shell->binding_modifier;
Bob Ham553d1242016-01-12 10:21:49 +00005065 if (!mod)
5066 return;
5067
Ian Ray13404c42017-09-18 15:22:01 +03005068 /* This binding is not configurable, but is only enabled if there is a
5069 * valid binding modifier. */
5070 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5071 MODIFIER_SUPER | MODIFIER_ALT,
5072 surface_opacity_binding, NULL);
5073
Ian Rayab7c0b62017-09-18 15:22:00 +03005074 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5075 mod, zoom_axis_binding,
5076 NULL);
5077
Daniel Stone325fc2d2012-05-30 16:31:58 +01005078 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5079 zoom_key_binding, NULL);
5080 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5081 zoom_key_binding, NULL);
Kristian Høgsberg211b5172014-01-11 13:10:21 -08005082 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5083 maximize_binding, NULL);
5084 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5085 fullscreen_binding, NULL);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005086 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5087 shell);
Neil Robertsaba0f252013-10-03 16:43:05 +01005088 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
Derek Foreman2217f3f2015-06-25 16:03:30 -05005089 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
Daniel Stone325fc2d2012-05-30 16:31:58 +01005090 resize_binding, shell);
Kristian Høgsberg0837fa92014-01-20 10:35:26 -08005091 weston_compositor_add_button_binding(ec, BTN_LEFT,
5092 mod | MODIFIER_SHIFT,
5093 resize_binding, shell);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005094
5095 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
Derek Foreman2217f3f2015-06-25 16:03:30 -05005096 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005097 rotate_binding, NULL);
5098
Daniel Stone325fc2d2012-05-30 16:31:58 +01005099 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5100 shell);
5101 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5102 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005103 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5104 ec);
Daniel Stone325fc2d2012-05-30 16:31:58 +01005105 weston_compositor_add_key_binding(ec, KEY_K, mod,
5106 force_kill_binding, shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005107 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5108 workspace_up_binding, shell);
5109 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5110 workspace_down_binding, shell);
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005111 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5112 workspace_move_surface_up_binding,
5113 shell);
5114 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5115 workspace_move_surface_down_binding,
5116 shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005117
5118 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5119 if (shell->workspaces.num > 1) {
5120 num_workspace_bindings = shell->workspaces.num;
5121 if (num_workspace_bindings > 6)
5122 num_workspace_bindings = 6;
5123 for (i = 0; i < num_workspace_bindings; i++)
5124 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5125 workspace_f_binding,
5126 shell);
5127 }
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02005128
Pekka Paalanen2829f7c2015-02-19 17:02:13 +02005129 weston_install_debug_key_binding(ec, mod);
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005130}
5131
Jason Ekstrand024177c2014-04-21 19:42:58 -05005132static void
5133handle_seat_created(struct wl_listener *listener, void *data)
5134{
5135 struct weston_seat *seat = data;
5136
5137 create_shell_seat(seat);
5138}
5139
Kristian Høgsberg1c562182011-05-02 22:09:20 -04005140WL_EXPORT int
Quentin Glidicda01c1d2016-12-02 14:17:08 +01005141wet_shell_init(struct weston_compositor *ec,
5142 int *argc, char *argv[])
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005143{
Kristian Høgsbergf4d2f232012-08-10 10:05:39 -04005144 struct weston_seat *seat;
Tiago Vignattibe143262012-04-16 17:31:41 +03005145 struct desktop_shell *shell;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005146 struct workspace **pws;
5147 unsigned int i;
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005148 struct wl_event_loop *loop;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005149
Peter Huttererf3d62272013-08-08 11:57:05 +10005150 shell = zalloc(sizeof *shell);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005151 if (shell == NULL)
5152 return -1;
5153
Kristian Høgsberg75840622011-09-06 13:48:16 -04005154 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005155
Pekka Paalanen6ffbba32019-11-06 12:59:32 +02005156 if (!weston_compositor_add_destroy_listener_once(ec,
5157 &shell->destroy_listener,
5158 shell_destroy)) {
5159 free(shell);
5160 return 0;
5161 }
5162
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02005163 shell->idle_listener.notify = idle_handler;
5164 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5165 shell->wake_listener.notify = wake_handler;
5166 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
Giulio Camuffof05d18f2015-12-11 20:57:05 +02005167 shell->transform_listener.notify = transform_handler;
5168 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005169
Quentin Glidic82681572016-12-17 13:40:51 +01005170 weston_layer_init(&shell->fullscreen_layer, ec);
5171 weston_layer_init(&shell->panel_layer, ec);
5172 weston_layer_init(&shell->background_layer, ec);
5173 weston_layer_init(&shell->lock_layer, ec);
5174 weston_layer_init(&shell->input_panel_layer, ec);
5175
5176 weston_layer_set_position(&shell->fullscreen_layer,
5177 WESTON_LAYER_POSITION_FULLSCREEN);
5178 weston_layer_set_position(&shell->panel_layer,
5179 WESTON_LAYER_POSITION_UI);
5180 weston_layer_set_position(&shell->background_layer,
5181 WESTON_LAYER_POSITION_BACKGROUND);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005182
5183 wl_array_init(&shell->workspaces.array);
Jonas Ådahle9d22502012-08-29 22:13:01 +02005184 wl_list_init(&shell->workspaces.client_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05005185
Kristian Høgsberg677a5f52013-12-04 11:00:19 -08005186 if (input_panel_setup(shell) < 0)
5187 return -1;
5188
Pekka Paalanenaa9536a2015-06-24 16:09:17 +03005189 shell->text_backend = text_backend_init(ec);
5190 if (!shell->text_backend)
Murray Calavera9a51cd72015-06-10 21:16:02 +00005191 return -1;
5192
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005193 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02005194
Daniel Stonedf8133b2013-11-19 11:37:14 +01005195 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5196 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5197
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005198 for (i = 0; i < shell->workspaces.num; i++) {
5199 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5200 if (pws == NULL)
5201 return -1;
5202
Quentin Glidic82681572016-12-17 13:40:51 +01005203 *pws = workspace_create(shell);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02005204 if (*pws == NULL)
5205 return -1;
5206 }
5207 activate_workspace(shell, 0);
5208
Quentin Glidic82681572016-12-17 13:40:51 +01005209 weston_layer_init(&shell->minimized_layer, ec);
Manuel Bachmann50c87db2014-02-26 15:52:13 +01005210
Jonas Ådahl8de6a1d2012-08-29 22:13:00 +02005211 wl_list_init(&shell->workspaces.anim_sticky_list);
Jonas Ådahl62fcd042012-06-13 00:01:23 +02005212 wl_list_init(&shell->workspaces.animation.link);
5213 shell->workspaces.animation.frame = animate_workspace_change_frame;
5214
Quentin Glidic8f9d90a2016-08-12 10:41:36 +02005215 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
5216 if (!shell->desktop)
Rafael Antognollie2a34552013-12-03 15:35:45 -02005217 return -1;
5218
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005219 if (wl_global_create(ec->wl_display,
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005220 &weston_desktop_shell_interface, 1,
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04005221 shell, bind_desktop_shell) == NULL)
Kristian Høgsberg75840622011-09-06 13:48:16 -04005222 return -1;
5223
Alexandros Frantzis409b01f2017-11-16 18:21:01 +02005224 weston_compositor_get_time(&shell->child.deathstamp);
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005225
Jonas Ådahl6d6fb612015-11-17 16:00:33 +08005226 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
Jonny Lamb765760d2014-08-20 15:53:19 +02005227
Xiong Zhang6b481422013-10-23 13:58:32 +08005228 setup_output_destroy_handler(ec, shell);
5229
Tiago Vignattib7dbbd62012-09-25 17:57:01 +03005230 loop = wl_display_get_event_loop(ec->wl_display);
5231 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02005232
Jason Ekstrand024177c2014-04-21 19:42:58 -05005233 wl_list_for_each(seat, &ec->seat_list, link)
5234 handle_seat_created(NULL, seat);
5235 shell->seat_create_listener.notify = handle_seat_created;
5236 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
Kristian Høgsbergd56bd902012-06-05 09:58:51 -04005237
David Fort50d962f2016-06-09 17:55:52 +02005238 shell->resized_listener.notify = handle_output_resized;
5239 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
5240
Giulio Camuffoc6ab3d52013-12-11 23:45:12 +01005241 screenshooter_create(ec);
5242
Tiago Vignatti0b52d482012-04-20 18:54:25 +03005243 shell_add_bindings(ec, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04005244
Pekka Paalanen79346ab2013-05-22 18:03:09 +03005245 shell_fade_init(shell);
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02005246
Pekka Paalanen2e62e4a2014-08-28 11:41:26 +03005247 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
5248
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05005249 return 0;
5250}